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

Class PatchIterd

monai/data/grid_dataset.py:102–157  ·  view source on GitHub ↗

Dictionary-based wrapper of :py:class:`monai.data.PatchIter`. Return a patch generator for dictionary data and the coordinate, Typically used with :py:class:`monai.data.GridPatchDataset`. Suppose all the expected fields specified by `keys` have same shape. Args: keys: k

Source from the content-addressed store, hash-verified

100
101
102class PatchIterd:
103 """
104 Dictionary-based wrapper of :py:class:`monai.data.PatchIter`.
105 Return a patch generator for dictionary data and the coordinate, Typically used
106 with :py:class:`monai.data.GridPatchDataset`.
107 Suppose all the expected fields specified by `keys` have same shape.
108
109 Args:
110 keys: keys of the corresponding items to iterate patches.
111 patch_size: size of patches to generate slices for, 0/None selects whole dimension
112 start_pos: starting position in the array, default is 0 for each dimension
113 mode: available modes: (Numpy) {``"constant"``, ``"edge"``, ``"linear_ramp"``, ``"maximum"``,
114 ``"mean"``, ``"median"``, ``"minimum"``, ``"reflect"``, ``"symmetric"``, ``"wrap"``, ``"empty"``}
115 (PyTorch) {``"constant"``, ``"reflect"``, ``"replicate"``, ``"circular"``}.
116 One of the listed string values or a user supplied function.
117 If None, no wrapping is performed. Defaults to ``"wrap"``.
118 See also: https://numpy.org/doc/stable/reference/generated/numpy.pad.html
119 https://pytorch.org/docs/stable/generated/torch.nn.functional.pad.html
120 requires pytorch >= 1.10 for best compatibility.
121 pad_opts: other arguments for the `np.pad` function.
122 note that `np.pad` treats channel dimension as the first dimension.
123
124 """
125
126 coords_key = "patch_coords"
127 original_spatial_shape_key = "original_spatial_shape"
128 start_pos_key = "start_pos"
129
130 def __init__(
131 self,
132 keys: KeysCollection,
133 patch_size: Sequence[int],
134 start_pos: Sequence[int] = (),
135 mode: str | None = NumpyPadMode.WRAP,
136 **pad_opts,
137 ):
138 self.keys = ensure_tuple(keys)
139 self.patch_iter = PatchIter(patch_size=patch_size, start_pos=start_pos, mode=mode, **pad_opts)
140
141 def __call__(
142 self, data: Mapping[Hashable, NdarrayTensor]
143 ) -> Generator[tuple[Mapping[Hashable, NdarrayTensor], np.ndarray], None, None]:
144 d = dict(data)
145 original_spatial_shape = d[first(self.keys)].shape[1:]
146
147 for patch in zip(*[self.patch_iter(d[key]) for key in self.keys]):
148 coords = patch[0][1] # use the coordinate of the first item
149 ret = {k: v[0] for k, v in zip(self.keys, patch)}
150 # fill in the extra keys with unmodified data
151 for k in set(d.keys()).difference(set(self.keys)):
152 ret[k] = deepcopy(d[k])
153 # also store the `coordinate`, `spatial shape of original image`, `start position` in the dictionary
154 ret[self.coords_key] = coords
155 ret[self.original_spatial_shape_key] = original_spatial_shape
156 ret[self.start_pos_key] = self.patch_iter.start_pos
157 yield ret, coords
158
159

Callers 2

test_patch_iterdMethod · 0.90
test_loading_dictMethod · 0.90

Calls

no outgoing calls

Tested by 2

test_patch_iterdMethod · 0.72
test_loading_dictMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…