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

Method __call__

monai/transforms/utility/array.py:197–234  ·  view source on GitHub ↗

Apply the transform to `img`.

(self, img: torch.Tensor, meta_dict: Mapping | None = None)

Source from the content-addressed store, hash-verified

195 self.input_channel_dim = channel_dim
196
197 def __call__(self, img: torch.Tensor, meta_dict: Mapping | None = None) -> torch.Tensor:
198 """
199 Apply the transform to `img`.
200 """
201 if not isinstance(img, MetaTensor) and not isinstance(meta_dict, Mapping):
202 if self.input_channel_dim is None:
203 msg = "Metadata not available and channel_dim=None, EnsureChannelFirst is not in use."
204 if self.strict_check:
205 raise ValueError(msg)
206 warnings.warn(msg)
207 return img
208 else:
209 img = MetaTensor(img)
210
211 if isinstance(img, MetaTensor):
212 meta_dict = img.meta
213
214 channel_dim = meta_dict.get(MetaKeys.ORIGINAL_CHANNEL_DIM, None) if isinstance(meta_dict, Mapping) else None
215 if self.input_channel_dim is not None:
216 channel_dim = float("nan") if self.input_channel_dim == "no_channel" else self.input_channel_dim
217
218 if channel_dim is None:
219 msg = "Unknown original_channel_dim in the MetaTensor meta dict or `meta_dict` or `channel_dim`."
220 if self.strict_check:
221 raise ValueError(msg)
222 warnings.warn(msg)
223 return img
224
225 # track the original channel dim
226 if isinstance(meta_dict, dict):
227 meta_dict[MetaKeys.ORIGINAL_CHANNEL_DIM] = channel_dim
228
229 if is_no_channel(channel_dim):
230 result = img[None]
231 else:
232 result = moveaxis(img, int(channel_dim), 0) # type: ignore
233
234 return convert_to_tensor(result, track_meta=get_track_meta()) # type: ignore
235
236
237class RepeatChannel(Transform):

Callers

nothing calls this directly

Calls 6

MetaTensorClass · 0.90
is_no_channelFunction · 0.90
moveaxisFunction · 0.90
convert_to_tensorFunction · 0.90
get_track_metaFunction · 0.90
getMethod · 0.80

Tested by

no test coverage detected