Read image data from specified file or files, it can read a list of data files and stack them together as multi-channel data in `get_data()`. Note that the returned object is Numpy array or list of Numpy arrays. Args: data: file name or a list of file na
(self, data: Sequence[PathLike] | PathLike, **kwargs)
| 1265 | return is_supported_format(filename, suffixes) |
| 1266 | |
| 1267 | def read(self, data: Sequence[PathLike] | PathLike, **kwargs): |
| 1268 | """ |
| 1269 | Read image data from specified file or files, it can read a list of data files |
| 1270 | and stack them together as multi-channel data in `get_data()`. |
| 1271 | Note that the returned object is Numpy array or list of Numpy arrays. |
| 1272 | |
| 1273 | Args: |
| 1274 | data: file name or a list of file names to read. |
| 1275 | kwargs: additional args for `numpy.load` API except `allow_pickle`, will override `self.kwargs` for existing keys. |
| 1276 | More details about available args: |
| 1277 | https://numpy.org/doc/stable/reference/generated/numpy.load.html |
| 1278 | |
| 1279 | Raises: |
| 1280 | ValueError: when `self.allow_pickle` is False but loaded data contains pickled objects. |
| 1281 | """ |
| 1282 | img_: list[Nifti1Image] = [] |
| 1283 | |
| 1284 | filenames: Sequence[PathLike] = ensure_tuple(data) |
| 1285 | kwargs_ = self.kwargs.copy() |
| 1286 | kwargs_.update(kwargs) |
| 1287 | for name in filenames: |
| 1288 | try: |
| 1289 | img = np.load(name, allow_pickle=self.allow_pickle, **kwargs_) |
| 1290 | except ValueError as e: |
| 1291 | # if a ValueError is raised, this is likely about pickle loading so raise an exception about this |
| 1292 | raise ValueError( |
| 1293 | "MONAI default value for argument `allow_pickle` of `np.load` changed to `False`, " |
| 1294 | "explicitly pass `allow_pickle=True` as a constructor argument to NumpyReader " |
| 1295 | "to enable pickle loading." |
| 1296 | ) from e |
| 1297 | |
| 1298 | if Path(name).name.endswith(".npz"): |
| 1299 | # load expected items from NPZ file |
| 1300 | npz_keys = list(img.keys()) if self.npz_keys is None else self.npz_keys |
| 1301 | for k in npz_keys: |
| 1302 | img_.append(img[k]) |
| 1303 | else: |
| 1304 | img_.append(img) |
| 1305 | |
| 1306 | return img_ if len(img_) > 1 else img_[0] |
| 1307 | |
| 1308 | def get_data(self, img) -> tuple[np.ndarray, dict]: |
| 1309 | """ |