Utility transform to show the statistics of data for debug or analysis. It can be inserted into any place of a transform chain and check results of previous transforms. It support both `numpy.ndarray` and `torch.tensor` as input data, so it can be used in pre-processing and post-pro
| 638 | |
| 639 | |
| 640 | class DataStats(Transform): |
| 641 | """ |
| 642 | Utility transform to show the statistics of data for debug or analysis. |
| 643 | It can be inserted into any place of a transform chain and check results of previous transforms. |
| 644 | It support both `numpy.ndarray` and `torch.tensor` as input data, |
| 645 | so it can be used in pre-processing and post-processing. |
| 646 | |
| 647 | It gets logger from `logging.getLogger(name)`, we can setup a logger outside first with the same `name`. |
| 648 | If the log level of `logging.RootLogger` is higher than `INFO`, will add a separate `StreamHandler` |
| 649 | log handler with `INFO` level and record to `stdout`. |
| 650 | |
| 651 | """ |
| 652 | |
| 653 | backend = [TransformBackends.TORCH, TransformBackends.NUMPY] |
| 654 | |
| 655 | def __init__( |
| 656 | self, |
| 657 | prefix: str = "Data", |
| 658 | data_type: bool = True, |
| 659 | data_shape: bool = True, |
| 660 | value_range: bool = True, |
| 661 | data_value: bool = False, |
| 662 | meta_info: bool = False, |
| 663 | additional_info: Callable | None = None, |
| 664 | name: str = "DataStats", |
| 665 | ) -> None: |
| 666 | """ |
| 667 | Args: |
| 668 | prefix: will be printed in format: "{prefix} statistics". |
| 669 | data_type: whether to show the type of input data. |
| 670 | data_shape: whether to show the shape of input data. |
| 671 | value_range: whether to show the value range of input data. |
| 672 | data_value: whether to show the raw value of input data. |
| 673 | a typical example is to print some properties of Nifti image: affine, pixdim, etc. |
| 674 | meta_info: whether to show the data of MetaTensor. |
| 675 | additional_info: user can define callable function to extract additional info from input data. |
| 676 | name: identifier of `logging.logger` to use, defaulting to "DataStats". |
| 677 | |
| 678 | Raises: |
| 679 | TypeError: When ``additional_info`` is not an ``Optional[Callable]``. |
| 680 | |
| 681 | """ |
| 682 | if not isinstance(prefix, str): |
| 683 | raise ValueError(f"prefix must be a string, got {type(prefix)}.") |
| 684 | self.prefix = prefix |
| 685 | self.data_type = data_type |
| 686 | self.data_shape = data_shape |
| 687 | self.value_range = value_range |
| 688 | self.data_value = data_value |
| 689 | self.meta_info = meta_info |
| 690 | if additional_info is not None and not callable(additional_info): |
| 691 | raise TypeError(f"additional_info must be None or callable but is {type(additional_info).__name__}.") |
| 692 | self.additional_info = additional_info |
| 693 | self._logger_name = name |
| 694 | _logger = logging.getLogger(self._logger_name) |
| 695 | _logger.setLevel(logging.INFO) |
| 696 | if logging.root.getEffectiveLevel() > logging.INFO: |
| 697 | # Avoid duplicate stream handlers to be added when multiple DataStats are used in a chain. |
no outgoing calls
searching dependent graphs…