Load NPY or NPZ format data based on Numpy library, they can be arrays or pickled objects. A typical usage is to load the `mask` data for classification task. It can load part of the npz file with specified `npz_keys`. Args: npz_keys: if loading npz file, only load the spec
| 1222 | |
| 1223 | |
| 1224 | class NumpyReader(ImageReader): |
| 1225 | """ |
| 1226 | Load NPY or NPZ format data based on Numpy library, they can be arrays or pickled objects. |
| 1227 | A typical usage is to load the `mask` data for classification task. |
| 1228 | It can load part of the npz file with specified `npz_keys`. |
| 1229 | |
| 1230 | Args: |
| 1231 | npz_keys: if loading npz file, only load the specified keys, if None, load all the items. |
| 1232 | stack the loaded items together to construct a new first dimension. |
| 1233 | channel_dim: if not None, explicitly specify the channel dim, otherwise, treat the array as no channel. |
| 1234 | allow_pickle: if True, allows loading pickled contents from NPY/NPZ files. Note that the default value of False |
| 1235 | prevents the risk of remote code execution, set this to True only for loading known trusted data. If this |
| 1236 | argument is False and pickled data is loaded, a ValueError will be raised. |
| 1237 | kwargs: additional args for `numpy.load` API except `allow_pickle`. more details about available args: |
| 1238 | https://numpy.org/doc/stable/reference/generated/numpy.load.html |
| 1239 | """ |
| 1240 | |
| 1241 | def __init__( |
| 1242 | self, |
| 1243 | npz_keys: KeysCollection | None = None, |
| 1244 | channel_dim: str | int | None = None, |
| 1245 | allow_pickle: bool = False, |
| 1246 | **kwargs, |
| 1247 | ): |
| 1248 | super().__init__() |
| 1249 | if npz_keys is not None: |
| 1250 | npz_keys = ensure_tuple(npz_keys) |
| 1251 | self.npz_keys = npz_keys |
| 1252 | self.channel_dim = float("nan") if channel_dim == "no_channel" else channel_dim |
| 1253 | self.allow_pickle = allow_pickle |
| 1254 | self.kwargs = kwargs |
| 1255 | |
| 1256 | def verify_suffix(self, filename: Sequence[PathLike] | PathLike) -> bool: |
| 1257 | """ |
| 1258 | Verify whether the specified file or files format is supported by Numpy reader. |
| 1259 | |
| 1260 | Args: |
| 1261 | filename: file name or a list of file names to read. |
| 1262 | if a list of files, verify all the suffixes. |
| 1263 | """ |
| 1264 | suffixes: Sequence[str] = ["npz", "npy"] |
| 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 | """ |
no outgoing calls
searching dependent graphs…