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

Function iter_patch

monai/data/utils.py:255–338  ·  view source on GitHub ↗

Yield successive patches from `arr` of size `patch_size`. The iteration can start from position `start_pos` in `arr` but drawing from a padded array extended by the `patch_size` in each dimension (so these coordinates can be negative to start in the padded region). If `copy_back` is Tru

(
    arr: NdarrayOrTensor,
    patch_size: Sequence[int] | int = 0,
    start_pos: Sequence[int] = (),
    overlap: Sequence[float] | float = 0.0,
    copy_back: bool = True,
    mode: str | None = NumpyPadMode.WRAP,
    **pad_opts: dict,
)

Source from the content-addressed store, hash-verified

253
254
255def iter_patch(
256 arr: NdarrayOrTensor,
257 patch_size: Sequence[int] | int = 0,
258 start_pos: Sequence[int] = (),
259 overlap: Sequence[float] | float = 0.0,
260 copy_back: bool = True,
261 mode: str | None = NumpyPadMode.WRAP,
262 **pad_opts: dict,
263) -> Generator[tuple[NdarrayOrTensor, np.ndarray], None, None]:
264 """
265 Yield successive patches from `arr` of size `patch_size`. The iteration can start from position `start_pos` in `arr`
266 but drawing from a padded array extended by the `patch_size` in each dimension (so these coordinates can be negative
267 to start in the padded region). If `copy_back` is True the values from each patch are written back to `arr`.
268
269 Args:
270 arr: array to iterate over
271 patch_size: size of patches to generate slices for, 0 or None selects whole dimension.
272 For 0 or None, padding and overlap ratio of the corresponding dimension will be 0.
273 start_pos: starting position in the array, default is 0 for each dimension
274 overlap: the amount of overlap of neighboring patches in each dimension (a value between 0.0 and 1.0).
275 If only one float number is given, it will be applied to all dimensions. Defaults to 0.0.
276 copy_back: if True data from the yielded patches is copied back to `arr` once the generator completes
277 mode: available modes: (Numpy) {``"constant"``, ``"edge"``, ``"linear_ramp"``, ``"maximum"``,
278 ``"mean"``, ``"median"``, ``"minimum"``, ``"reflect"``, ``"symmetric"``, ``"wrap"``, ``"empty"``}
279 (PyTorch) {``"constant"``, ``"reflect"``, ``"replicate"``, ``"circular"``}.
280 One of the listed string values or a user supplied function.
281 If None, no wrapping is performed. Defaults to ``"wrap"``.
282 See also: https://numpy.org/doc/stable/reference/generated/numpy.pad.html
283 https://pytorch.org/docs/stable/generated/torch.nn.functional.pad.html
284 requires pytorch >= 1.10 for best compatibility.
285 pad_opts: other arguments for the `np.pad` or `torch.pad` function.
286 note that `np.pad` treats channel dimension as the first dimension.
287
288 Yields:
289 Patches of array data from `arr` which are views into a padded array which can be modified, if `copy_back` is
290 True these changes will be reflected in `arr` once the iteration completes.
291
292 Note:
293 coordinate format is:
294
295 [1st_dim_start, 1st_dim_end,
296 2nd_dim_start, 2nd_dim_end,
297 ...,
298 Nth_dim_start, Nth_dim_end]]
299
300 """
301
302 from monai.transforms.croppad.functional import pad_nd # needs to be here to avoid circular import
303
304 # ensure patchSize and startPos are the right length
305 patch_size_ = get_valid_patch_size(arr.shape, patch_size)
306 start_pos = ensure_tuple_size(start_pos, arr.ndim)
307
308 # set padded flag to false if pad mode is None
309 padded = bool(mode)
310 is_v = [bool(p) for p in ensure_tuple_size(patch_size, arr.ndim)] # whether a valid patch size provided
311 _pad_size = tuple(p if v and padded else 0 for p, v in zip(patch_size_, is_v)) # pad p if v else 0
312 _overlap = [op if v else 0.0 for op, v in zip(ensure_tuple_rep(overlap, arr.ndim), is_v)] # overlap if v else 0.0

Callers 3

__call__Method · 0.90
__call__Method · 0.90
test_iter_patchMethod · 0.90

Calls 5

ensure_tuple_sizeFunction · 0.90
ensure_tuple_repFunction · 0.90
pad_ndFunction · 0.90
get_valid_patch_sizeFunction · 0.85
iter_patch_slicesFunction · 0.85

Tested by 1

test_iter_patchMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…