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

Function get_random_patch

monai/data/utils.py:105–127  ·  view source on GitHub ↗

Returns a tuple of slices to define a random patch in an array of shape `dims` with size `patch_size` or the as close to it as possible within the given dimension. It is expected that `patch_size` is a valid patch for a source of shape `dims` as returned by `get_valid_patch_size`.

(
    dims: Sequence[int], patch_size: Sequence[int], rand_state: np.random.RandomState | None = None
)

Source from the content-addressed store, hash-verified

103
104
105def get_random_patch(
106 dims: Sequence[int], patch_size: Sequence[int], rand_state: np.random.RandomState | None = None
107) -> tuple[slice, ...]:
108 """
109 Returns a tuple of slices to define a random patch in an array of shape `dims` with size `patch_size` or the as
110 close to it as possible within the given dimension. It is expected that `patch_size` is a valid patch for a source
111 of shape `dims` as returned by `get_valid_patch_size`.
112
113 Args:
114 dims: shape of source array
115 patch_size: shape of patch size to generate
116 rand_state: a random state object to generate random numbers from
117
118 Returns:
119 (tuple of slice): a tuple of slice objects defining the patch
120 """
121
122 # choose the minimal corner of the patch
123 rand_int = np.random.randint if rand_state is None else rand_state.randint
124 min_corner = tuple(rand_int(0, ms - ps + 1) if ms > ps else 0 for ms, ps in zip(dims, patch_size))
125
126 # create the slices for each dimension which define the patch in the source array
127 return tuple(slice(mc, mc + ps) for mc, ps in zip(min_corner, patch_size))
128
129
130def iter_patch_slices(

Callers 2

randomizeMethod · 0.90
randomizeMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…