def test_little_endian_segment_order(self): """Test interpreting segment order as little endian.""" ds = dcmread(MR_RLE_1F) assert ds.file_meta.TransferSyntaxUID == RLELossless assert ds.BitsAllocated == 16 assert ds.SamplesPerPixel == 1 assert 'NumberOfFrames' not in ds assert ds.PixelRepresentation == 1 # signed # Big endian arr = get_pixeldata(ds, rle_segment_order='>') arr = reshape_pixel_array(ds, arr) assert (64, 64) == arr.shape assert (422, 319, 361) == tuple(arr[0, 31:34]) assert (366, 363, 322) == tuple(arr[31, :3]) assert (1369, 1129, 862) == tuple(arr[-1, -3:]) # Little endian arr = get_pixeldata(ds, rle_segment_order='<') arr = reshape_pixel_array(ds, arr) assert (64, 64) == arr.shape assert (-23039, 16129, 26881) == tuple(arr[0, 31:34]) assert (28161, 27393, 16897) == tuple(arr[31, :3]) assert (22789, 26884, 24067) == tuple(arr[-1, -3:])
def test_invalid_nr_frames_raises(self): """Test an invalid Number of Frames value raises exception.""" self.ds.SamplesPerPixel = 1 self.ds.NumberOfFrames = 0 # Need to escape brackets with pytest.raises(ValueError, match=r"value of 0 for \(0028,0008\)"): reshape_pixel_array(self.ds, RESHAPE_ARRAYS['1frame_1sample'])
def test_invalid_planar_conf_raises(self): self.ds.SamplesPerPixel = 3 self.ds.PlanarConfiguration = 2 # Need to escape brackets with pytest.raises(ValueError, match=r"value of 2 for \(0028,0006\)"): reshape_pixel_array(self.ds, RESHAPE_ARRAYS['1frame_3sample_0config'])
def test_invalid_samples_raises(self): """Test an invalid Samples per Pixel value raises exception.""" self.ds.SamplesPerPixel = 0 # Need to escape brackets with pytest.raises(ValueError, match=r"value of 0 for \(0028,0002\)"): reshape_pixel_array(self.ds, RESHAPE_ARRAYS['1frame_1sample'])
def test_2frame_1sample(self): """Test reshaping 2 frame, 1 sample/pixel.""" self.ds.NumberOfFrames = 2 self.ds.SamplesPerPixel = 1 arr = reshape_pixel_array(self.ds, RESHAPE_ARRAYS['2frame_1sample']) assert (2, 4, 5) == arr.shape assert np.array_equal(arr, self.ref_2_1)
def test_invalid_colourspace_warns(): """Test that using an unknown colourspace gives a warning.""" index = get_indexed_datasets('1.2.840.10008.1.2.4.50') ds = index['JPEGBaseline_1s_1f_u_08_08.dcm']['ds'] nr_frames = ds.get('NumberOfFrames', 1) frame = next(generate_pixel_data_frame(ds.PixelData, nr_frames)) msg = r"no colour transformation will be applied" ds.PhotometricInterpretation = 'ANY' with pytest.warns(UserWarning, match=msg): arr = decode_pixel_data(np.frombuffer(frame, 'uint8'), ds) arr = reshape_pixel_array(ds, arr) assert arr.flags.writeable assert 'uint8' == arr.dtype assert (ds.Rows, ds.Columns) == arr.shape # Reference values from GDCM handler assert 76 == arr[5, 50] assert 167 == arr[15, 50] assert 149 == arr[25, 50] assert 203 == arr[35, 50] assert 29 == arr[45, 50] assert 142 == arr[55, 50] assert 1 == arr[65, 50] assert 64 == arr[75, 50] assert 192 == arr[85, 50] assert 255 == arr[95, 50]
def test_jpeg2000i(self, fname, info): """Test get_parameters() for the j2k datasets.""" #info: (rows, columns, spp, bps) index = get_indexed_datasets('1.2.840.10008.1.2.4.91') ds = index[fname]['ds'] frame = next(generate_frames(ds)) arr = decode(BytesIO(frame), reshape=False) assert arr.flags.writeable ds.NumberOfFrames = 1 arr = arr.view(pixel_dtype(ds)) arr = reshape_pixel_array(ds, arr) #plt.imshow(arr) #plt.show() if info[2] == 1: assert (info[0], info[1]) == arr.shape else: assert (info[0], info[1], info[2]) == arr.shape if 1 <= info[3] <= 8: if info[4] == 1: assert arr.dtype == 'int8' else: assert arr.dtype == 'uint8' if 9 <= info[3] <= 16: if info[4] == 1: assert arr.dtype == 'int16' else: assert arr.dtype == 'uint16'
def test_uncompressed_syntaxes(self): """Test that uncompressed syntaxes use the dataset planar conf.""" for uid in UncompressedPixelTransferSyntaxes: self.ds.file_meta.TransferSyntaxUID = uid self.ds.PlanarConfiguration = 0 self.ds.NumberOfFrames = 1 self.ds.SamplesPerPixel = 3 arr = reshape_pixel_array(self.ds, RESHAPE_ARRAYS['1frame_3sample_0config']) assert (4, 5, 3) == arr.shape assert np.array_equal(arr, self.ref_1_3) self.ds.PlanarConfiguration = 1 arr = reshape_pixel_array(self.ds, RESHAPE_ARRAYS['1frame_3sample_1config']) assert (4, 5, 3) == arr.shape assert np.array_equal(arr, self.ref_1_3)
def test_2frame_3sample_1conf(self): """Test reshaping 2 frame, 3 sample/pixel for 1 planar config.""" self.ds.NumberOfFrames = 2 self.ds.SamplesPerPixel = 3 self.ds.PlanarConfiguration = 1 arr = reshape_pixel_array(self.ds, RESHAPE_ARRAYS['2frame_3sample_1config']) assert (2, 4, 5, 3) == arr.shape assert np.array_equal(arr, self.ref_2_3)
def test_1frame_3sample_0conf(self): """Test reshaping 1 frame, 3 sample/pixel for 0 planar config.""" self.ds.NumberOfFrames = 1 self.ds.SamplesPerPixel = 3 self.ds.PlanarConfiguration = 0 arr = reshape_pixel_array(self.ds, RESHAPE_ARRAYS['1frame_3sample_0config']) assert (4, 5, 3) == arr.shape assert np.array_equal(arr, self.ref_1_3)
def test_compressed_syntaxes_1conf(self): """Test the compressed syntaxes that are always 1 planar conf.""" for uid in ['1.2.840.10008.1.2.4.80', '1.2.840.10008.1.2.4.81', '1.2.840.10008.1.2.5']: self.ds.file_meta.TransferSyntaxUID = uid self.ds.PlanarConfiguration = 0 self.ds.NumberOfFrames = 1 self.ds.SamplesPerPixel = 3 arr = reshape_pixel_array(self.ds, RESHAPE_ARRAYS['1frame_3sample_1config']) assert (4, 5, 3) == arr.shape assert np.array_equal(arr, self.ref_1_3)
def test_compressed_syntaxes_1conf(self): """Test the compressed syntaxes that are always 1 planar conf.""" for uid in [ '1.2.840.10008.1.2.4.80', '1.2.840.10008.1.2.4.81', '1.2.840.10008.1.2.5' ]: self.ds.file_meta.TransferSyntaxUID = uid self.ds.PlanarConfiguration = 0 self.ds.NumberOfFrames = 1 self.ds.SamplesPerPixel = 3 arr = reshape_pixel_array(self.ds, RESHAPE_ARRAYS['1frame_3sample_1config']) assert (4, 5, 3) == arr.shape assert np.array_equal(arr, self.ref_1_3)
def test_cycle_32bit_1sample(self): """Test an encode/decode cycle for 32-bit 1 sample/pixel.""" ds = dcmread(EXPL_32_1_1F) ref = ds.pixel_array assert 32 == ds.BitsAllocated assert 1 == ds.SamplesPerPixel kwargs = RLELosslessEncoder.kwargs_from_ds(ds) encoded = _encode_frame(ds.PixelData, **kwargs) decoded = _rle_decode_frame( encoded, ds.Rows, ds.Columns, ds.SamplesPerPixel, ds.BitsAllocated ) arr = np.frombuffer(decoded, '<u4') arr = reshape_pixel_array(ds, arr) assert np.array_equal(ref, arr)
def test_functional(self): """Test function works OK.""" ds = dcmread(EXPL_16_3_1F) ref = ds.pixel_array assert ds.BitsAllocated == 16 assert ds.SamplesPerPixel == 3 assert ds.PixelRepresentation == 0 encoded = rle_encode_frame(ref) decoded = _rle_decode_frame( encoded, ds.Rows, ds.Columns, ds.SamplesPerPixel, ds.BitsAllocated ) ds.PlanarConfiguration = 1 arr = np.frombuffer(decoded, '<u2') arr = reshape_pixel_array(ds, arr) assert np.array_equal(ref, arr)
def test_cycle_u32_1s_1f(self): """Test an encode/decode cycle for 32-bit 1 sample/pixel.""" ds = dcmread(EXPL_32_1_1F) ref = ds.pixel_array assert ds.BitsAllocated == 32 assert ds.SamplesPerPixel == 1 assert ds.PixelRepresentation == 0 assert ds.PhotometricInterpretation == 'MONOCHROME2' enc = RLELosslessEncoder encoded = enc.encode(ds, idx=0, encoding_plugin='gdcm') decoded = _rle_decode_frame(encoded, ds.Rows, ds.Columns, ds.SamplesPerPixel, ds.BitsAllocated) arr = np.frombuffer(decoded, '>u4') arr = reshape_pixel_array(ds, arr) assert np.array_equal(ref, arr)
def test_cycle_32bit_3sample(self): """Test an encode/decode cycle for 32-bit 3 sample/pixel.""" ds = dcmread(EXPL_32_3_1F) ref = ds.pixel_array assert ds.BitsAllocated == 32 assert ds.SamplesPerPixel == 3 assert ds.PixelRepresentation == 0 kwargs = RLELosslessEncoder.kwargs_from_ds(ds) encoded = _encode_frame(ds.PixelData, **kwargs) decoded = _rle_decode_frame( encoded, ds.Rows, ds.Columns, ds.SamplesPerPixel, ds.BitsAllocated ) # The decoded data is planar configuration 1 ds.PlanarConfiguration = 1 arr = np.frombuffer(decoded, '<u4') arr = reshape_pixel_array(ds, arr) assert np.array_equal(ref, arr)
def test_cycle_u16_3s_1f(self): """Test an encode/decode cycle for 16-bit 3 sample/pixel.""" ds = dcmread(EXPL_16_3_1F) ref = ds.pixel_array assert ds.BitsAllocated == 16 assert ds.SamplesPerPixel == 3 assert ds.PixelRepresentation == 0 assert ds.PhotometricInterpretation == 'RGB' assert ds.PlanarConfiguration == 0 enc = RLELosslessEncoder encoded = enc.encode(ds, idx=0, encoding_plugin='gdcm') decoded = _rle_decode_frame(encoded, ds.Rows, ds.Columns, ds.SamplesPerPixel, ds.BitsAllocated) arr = np.frombuffer(decoded, '<u2') # The decoded data is planar configuration 1 ds.PlanarConfiguration = 1 arr = reshape_pixel_array(ds, arr) assert np.array_equal(ref, arr)
def test_big_endian_arr(self): """Test using a big endian array works.""" ds = dcmread(EXPL_16_3_1F) ref = ds.pixel_array assert ds.BitsAllocated == 16 assert ds.SamplesPerPixel == 3 assert ds.PixelRepresentation == 0 arr = ref.newbyteorder('>') assert id(arr) != id(ref) assert arr.dtype == '>u2' encoded = rle_encode_frame(arr) decoded = _rle_decode_frame( encoded, ds.Rows, ds.Columns, ds.SamplesPerPixel, ds.BitsAllocated ) ds.PlanarConfiguration = 1 arr = np.frombuffer(decoded, '<u2') arr = reshape_pixel_array(ds, arr) assert np.array_equal(ref, arr)
def as_array(ds: "Dataset") -> "np.ndarray": """Return the entire *Pixel Data* as an :class:`~numpy.ndarray`. .. versionadded:: 2.1 Parameters ---------- ds : pydicom.dataset.Dataset The :class:`Dataset` containing an :dcm:`Image Pixel <part03/sect_C.7.6.3.html>` module and the *Pixel Data* to be converted. Returns ------- numpy.ndarray The contents of (7FE0,0010) *Pixel Data* as an :class:`~numpy.ndarray` with shape (rows, columns), (rows, columns, components), (frames, rows, columns), or (frames, rows, columns, components) depending on the dataset. """ return reshape_pixel_array(ds, get_pixeldata(ds))
def test_1frame_1sample(self): """Test reshaping 1 frame, 1 sample/pixel.""" self.ds.SamplesPerPixel = 1 arr = reshape_pixel_array(self.ds, RESHAPE_ARRAYS['1frame_1sample']) assert (4, 5) == arr.shape assert np.array_equal(arr, self.ref_1_1)
def test_reshape_pixel_array_raises(self): """Test that reshape_pixel_array raises exception without numpy.""" with pytest.raises(ImportError, match="Numpy is required to reshape"): reshape_pixel_array(None, None)