def test_load_hdf5_from_file(hdf5_path: Union[str, Path], patient_id: str, acquisition_date: datetime, array_shape: np.ndarray, load_segmentation: bool) -> None: """ Assert that the HDF5 object is loaded with the expected attributes. """ hdf5_path = Path(hdf5_path) act_hdf5 = HDF5Object.from_file(hdf5_path, load_segmentation=load_segmentation) assert act_hdf5.patient_id == patient_id assert act_hdf5.acquisition_date.isoformat( ) == acquisition_date # type: ignore assert np.array_equal(act_hdf5.volume.shape, array_shape) if load_segmentation: assert act_hdf5.segmentation is not None assert np.array_equal(act_hdf5.segmentation.shape, array_shape) else: assert act_hdf5.segmentation is None
def load_hdf5_file(path_str: Union[str, Path], load_segmentation: bool = False) -> HDF5Object: """ Loads a single HDF5 file. :param path_str: The path of the HDF5 file that should be loaded. :param load_segmentation: If True, the `segmentation` field of the result object will be populated. If False, the field will be set to None. :return: HDF5Object """ def _is_valid_hdf5_path(_path: Path) -> bool: """ Validates a path for an image :param _path: :return: """ return _path.is_file() and is_hdf5_file_path(_path) path = Path(path_str) if path is None or not _is_valid_hdf5_path(path): raise ValueError(f"Invalid path: {path}") return HDF5Object.from_file(path, load_segmentation=load_segmentation)