Adjust or add the channel dimension of input data to ensure `channel_first` shape. This extracts the `original_channel_dim` info from provided meta_data dictionary or MetaTensor input. This value should state which dimension is the channel dimension so that it can be moved forward, or
| 173 | |
| 174 | |
| 175 | class EnsureChannelFirst(Transform): |
| 176 | """ |
| 177 | Adjust or add the channel dimension of input data to ensure `channel_first` shape. |
| 178 | |
| 179 | This extracts the `original_channel_dim` info from provided meta_data dictionary or MetaTensor input. This value |
| 180 | should state which dimension is the channel dimension so that it can be moved forward, or contain "no_channel" to |
| 181 | state no dimension is the channel and so a 1-size first dimension is to be added. |
| 182 | |
| 183 | Args: |
| 184 | strict_check: whether to raise an error when the meta information is insufficient. |
| 185 | channel_dim: This argument can be used to specify the original channel dimension (integer) of the input array. |
| 186 | It overrides the `original_channel_dim` from provided MetaTensor input. |
| 187 | If the input array doesn't have a channel dim, this value should be ``'no_channel'``. |
| 188 | If this is set to `None`, this class relies on `img` or `meta_dict` to provide the channel dimension. |
| 189 | """ |
| 190 | |
| 191 | backend = [TransformBackends.TORCH, TransformBackends.NUMPY] |
| 192 | |
| 193 | def __init__(self, strict_check: bool = True, channel_dim: None | str | int = None): |
| 194 | self.strict_check = strict_check |
| 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 |
no outgoing calls
searching dependent graphs…