MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / get_spatial_dims

Function get_spatial_dims

monai/data/box_utils.py:365–454  ·  view source on GitHub ↗

Get spatial dimension for the giving setting and check the validity of them. Missing input is allowed. But at least one of the input value should be given. It raises ValueError if the dimensions of multiple inputs do not match with each other. Args: boxes: bounding boxes, N

(
    boxes: torch.Tensor | np.ndarray | None = None,
    points: torch.Tensor | np.ndarray | None = None,
    corners: Sequence | None = None,
    spatial_size: Sequence[int] | torch.Tensor | np.ndarray | None = None,
)

Source from the content-addressed store, hash-verified

363
364
365def get_spatial_dims(
366 boxes: torch.Tensor | np.ndarray | None = None,
367 points: torch.Tensor | np.ndarray | None = None,
368 corners: Sequence | None = None,
369 spatial_size: Sequence[int] | torch.Tensor | np.ndarray | None = None,
370) -> int:
371 """
372 Get spatial dimension for the giving setting and check the validity of them.
373 Missing input is allowed. But at least one of the input value should be given.
374 It raises ValueError if the dimensions of multiple inputs do not match with each other.
375
376 Args:
377 boxes: bounding boxes, Nx4 or Nx6 torch tensor or ndarray
378 points: point coordinates, [x, y] or [x, y, z], Nx2 or Nx3 torch tensor or ndarray
379 corners: corners of boxes, 4-element or 6-element tuple, each element is a Nx1 torch tensor or ndarray
380 spatial_size: The spatial size of the image where the boxes are attached.
381 len(spatial_size) should be in [2, 3].
382
383 Returns:
384 ``int``: spatial_dims, number of spatial dimensions of the bounding boxes.
385
386 Example:
387 .. code-block:: python
388
389 boxes = torch.ones(10,6)
390 get_spatial_dims(boxes, spatial_size=[100,200,200]) # will return 3
391 get_spatial_dims(boxes, spatial_size=[100,200]) # will raise ValueError
392 get_spatial_dims(boxes) # will return 3
393 """
394 spatial_dims_set = set()
395
396 # Check the validity of each input and add its corresponding spatial_dims to spatial_dims_set
397 if boxes is not None:
398 if len(boxes.shape) != 2:
399 if boxes.shape[0] == 0:
400 raise ValueError(
401 f"Currently we support only boxes with shape [N,4] or [N,6], "
402 f"got boxes with shape {boxes.shape}. "
403 f"Please reshape it with boxes = torch.reshape(boxes, [0, 4]) or torch.reshape(boxes, [0, 6])."
404 )
405 else:
406 raise ValueError(
407 f"Currently we support only boxes with shape [N,4] or [N,6], got boxes with shape {boxes.shape}."
408 )
409 if int(boxes.shape[1] / 2) not in SUPPORTED_SPATIAL_DIMS:
410 raise ValueError(
411 f"Currently we support only boxes with shape [N,4] or [N,6], got boxes with shape {boxes.shape}."
412 )
413 spatial_dims_set.add(int(boxes.shape[1] / 2))
414 if points is not None:
415 if len(points.shape) != 2:
416 if points.shape[0] == 0:
417 raise ValueError(
418 f"Currently we support only points with shape [N,2] or [N,3], "
419 f"got points with shape {points.shape}. "
420 f"Please reshape it with points = torch.reshape(points, [0, 2]) or torch.reshape(points, [0, 3])."
421 )
422 else:

Callers 15

_apply_affine_to_pointsFunction · 0.90
apply_affine_to_boxesFunction · 0.90
zoom_boxesFunction · 0.90
resize_boxesFunction · 0.90
flip_boxesFunction · 0.90
convert_box_to_maskFunction · 0.90
convert_mask_to_boxFunction · 0.90
swapaxes_boxesFunction · 0.90
rot90_boxesFunction · 0.90
__call__Method · 0.90
__call__Method · 0.90
__call__Method · 0.90

Calls 2

look_up_optionFunction · 0.90
addMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…