Args: img: target data content that save into file. The image should be channel-first, shape: `[C,H,W,[D]]`. meta_data: key-value pairs of metadata corresponding to the data. filename: str or file-like object which to save img. If specifie
(
self, img: torch.Tensor | np.ndarray, meta_data: dict | None = None, filename: str | PathLike | None = None
)
| 470 | return self |
| 471 | |
| 472 | def __call__( |
| 473 | self, img: torch.Tensor | np.ndarray, meta_data: dict | None = None, filename: str | PathLike | None = None |
| 474 | ): |
| 475 | """ |
| 476 | Args: |
| 477 | img: target data content that save into file. The image should be channel-first, shape: `[C,H,W,[D]]`. |
| 478 | meta_data: key-value pairs of metadata corresponding to the data. |
| 479 | filename: str or file-like object which to save img. |
| 480 | If specified, will ignore `self.output_name_formatter` and `self.folder_layout`. |
| 481 | """ |
| 482 | meta_data = img.meta if isinstance(img, MetaTensor) else meta_data |
| 483 | if filename is not None: |
| 484 | filename = f"{filename}{self.output_ext}" |
| 485 | else: |
| 486 | kw = self.fname_formatter(meta_data, self) |
| 487 | filename = self.folder_layout.filename(**kw) |
| 488 | |
| 489 | if meta_data: |
| 490 | meta_spatial_shape = ensure_tuple(meta_data.get("spatial_shape", ())) |
| 491 | if len(meta_spatial_shape) >= len(img.shape): |
| 492 | self.data_kwargs["channel_dim"] = None |
| 493 | elif is_no_channel(self.data_kwargs.get("channel_dim")): |
| 494 | warnings.warn( |
| 495 | f"data shape {img.shape} (with spatial shape {meta_spatial_shape}) " |
| 496 | f"but SaveImage `channel_dim` is set to {self.data_kwargs.get('channel_dim')} no channel." |
| 497 | ) |
| 498 | |
| 499 | err = [] |
| 500 | for writer_cls in self.writers: |
| 501 | try: |
| 502 | writer_obj = writer_cls(**self.init_kwargs) |
| 503 | writer_obj.set_data_array(data_array=img, **self.data_kwargs) |
| 504 | writer_obj.set_metadata(meta_dict=meta_data, **self.meta_kwargs) |
| 505 | writer_obj.write(filename, **self.write_kwargs) |
| 506 | self.writer_obj = writer_obj |
| 507 | except Exception as e: |
| 508 | err.append(traceback.format_exc()) |
| 509 | logging.getLogger(self.__class__.__name__).debug(e, exc_info=True) |
| 510 | logging.getLogger(self.__class__.__name__).info( |
| 511 | f"{writer_cls.__class__.__name__}: unable to write {filename}.\n" |
| 512 | ) |
| 513 | else: |
| 514 | self._data_index += 1 |
| 515 | if self.savepath_in_metadict and meta_data is not None: |
| 516 | meta_data[MetaKeys.SAVED_TO] = filename |
| 517 | return img |
| 518 | msg = "\n".join([f"{e}" for e in err]) |
| 519 | raise RuntimeError( |
| 520 | f"{self.__class__.__name__} cannot find a suitable writer for {filename}.\n" |
| 521 | " Please install the writer libraries, see also the installation instructions:\n" |
| 522 | " https://monai.readthedocs.io/en/latest/installation.html#installing-the-recommended-dependencies.\n" |
| 523 | f" The current registered writers for {self.output_ext}: {self.writers}.\n{msg}" |
| 524 | ) |
| 525 | |
| 526 | |
| 527 | class WriteFileMapping(Transform): |
nothing calls this directly
no test coverage detected