Get the raw array data of the image, converted to Numpy array. Args: img: a Nibabel image object loaded from an image file. filename: file name of the image.
(self, img, filename)
| 1191 | return np.asarray(size[:spatial_rank]) |
| 1192 | |
| 1193 | def _get_array_data(self, img, filename): |
| 1194 | """ |
| 1195 | Get the raw array data of the image, converted to Numpy array. |
| 1196 | |
| 1197 | Args: |
| 1198 | img: a Nibabel image object loaded from an image file. |
| 1199 | filename: file name of the image. |
| 1200 | |
| 1201 | """ |
| 1202 | if self.to_gpu: |
| 1203 | file_size = os.path.getsize(filename) |
| 1204 | image = cp.empty(file_size, dtype=cp.uint8) |
| 1205 | with kvikio.CuFile(filename, "r") as f: |
| 1206 | f.read(image) |
| 1207 | if filename.endswith(".nii.gz"): |
| 1208 | # for compressed data, have to tansfer to CPU to decompress |
| 1209 | # and then transfer back to GPU. It is not efficient compared to .nii file |
| 1210 | # and may be slower than CPU loading in some cases. |
| 1211 | warnings.warn("Loading compressed NIfTI file into GPU may not be efficient.") |
| 1212 | compressed_data = cp.asnumpy(image) |
| 1213 | with gzip.GzipFile(fileobj=io.BytesIO(compressed_data)) as gz_file: |
| 1214 | decompressed_data = gz_file.read() |
| 1215 | |
| 1216 | image = cp.frombuffer(decompressed_data, dtype=cp.uint8) |
| 1217 | data_shape = img.shape |
| 1218 | data_offset = img.dataobj.offset |
| 1219 | data_dtype = img.dataobj.dtype |
| 1220 | return image[data_offset:].view(data_dtype).reshape(data_shape, order="F") |
| 1221 | return np.asanyarray(img.dataobj) |
| 1222 | |
| 1223 | |
| 1224 | class NumpyReader(ImageReader): |