MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / _get_array_data_from_gpu

Method _get_array_data_from_gpu

monai/data/image_reader.py:906–959  ·  view source on GitHub ↗

Get the raw array data of the image. This function is used when `to_gpu` is set to True. Args: img: a Pydicom dataset object. filename: the file path of the image.

(self, img, filename)

Source from the content-addressed store, hash-verified

904 return all_segs, metadata
905
906 def _get_array_data_from_gpu(self, img, filename):
907 """
908 Get the raw array data of the image. This function is used when `to_gpu` is set to True.
909
910 Args:
911 img: a Pydicom dataset object.
912 filename: the file path of the image.
913
914 """
915 rows = getattr(img, "Rows", None)
916 columns = getattr(img, "Columns", None)
917 bits_allocated = getattr(img, "BitsAllocated", None)
918 samples_per_pixel = getattr(img, "SamplesPerPixel", 1)
919 number_of_frames = getattr(img, "NumberOfFrames", 1)
920 pixel_representation = getattr(img, "PixelRepresentation", 1)
921
922 if rows is None or columns is None or bits_allocated is None:
923 warnings.warn(
924 f"dicom data: {filename} does not have Rows, Columns or BitsAllocated, falling back to CPU loading."
925 )
926
927 if not hasattr(img, "pixel_array"):
928 raise ValueError(f"dicom data: {filename} does not have pixel_array.")
929 data = img.pixel_array
930
931 return data
932
933 if bits_allocated == 8:
934 dtype = cp.int8 if pixel_representation == 1 else cp.uint8
935 elif bits_allocated == 16:
936 dtype = cp.int16 if pixel_representation == 1 else cp.uint16
937 elif bits_allocated == 32:
938 dtype = cp.int32 if pixel_representation == 1 else cp.uint32
939 else:
940 raise ValueError("Unsupported BitsAllocated value")
941
942 bytes_per_pixel = bits_allocated // 8
943 total_pixels = rows * columns * samples_per_pixel * number_of_frames
944 expected_pixel_data_length = total_pixels * bytes_per_pixel
945
946 pixel_data_tag = pydicom.tag.Tag(0x7FE0, 0x0010)
947 if pixel_data_tag not in img:
948 raise ValueError(f"dicom data: {filename} does not have pixel data.")
949
950 offset = img.get_item(pixel_data_tag, keep_deferred=True).value_tell
951
952 with kvikio.CuFile(filename, "r") as f:
953 buffer = cp.empty(expected_pixel_data_length, dtype=cp.int8)
954 f.read(buffer, expected_pixel_data_length, offset)
955
956 new_shape = (number_of_frames, rows, columns) if number_of_frames > 1 else (rows, columns)
957 data = buffer.view(dtype).reshape(new_shape)
958
959 return data
960
961 def _get_array_data(self, img, filename):
962 """

Callers 1

_get_array_dataMethod · 0.95

Calls 2

get_itemMethod · 0.80
readMethod · 0.45

Tested by

no test coverage detected