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_valid_no_warning(self, recwarn): """Test no warning issued when dataset matches JPEG data.""" index = get_indexed_datasets('1.2.840.10008.1.2.4.90') ds = index['966_fixed.dcm']['ds'] ds.pixel_array assert len(recwarn) == 0
def test_bad_decode(): """Test trying to decode bad data.""" index = get_indexed_datasets('1.2.840.10008.1.2.4.90') ds = index['966.dcm']['ds'] frame = next(generate_frames(ds)) msg = r"Error decoding the J2K data: failed to decode image" with pytest.raises(RuntimeError, match=msg): decode(frame)
def test_decode_format_raises(self): """Test decoding using invalid format raises.""" index = get_indexed_datasets('1.2.840.10008.1.2.4.90') ds = index['693_J2KR.dcm']['ds'] frame = next(generate_frames(ds)) msg = r"Unsupported 'j2k_format' value: 3" with pytest.raises(ValueError, match=msg): get_parameters(frame, j2k_format=3)
def test_non_conformant_raises(self): """Test that a non-conformant JPEG image raises an exception.""" ds_list = get_indexed_datasets('1.2.840.10008.1.2.4.90') # Image has invalid Se value in the SOS marker segment item = ds_list['966.dcm'] assert 0xC000 == item['Status'][1] msg = r"Error decoding the J2K data: failed to decode image" with pytest.raises(RuntimeError, match=msg): item['ds'].pixel_array
def test_decode_reshape_false(self): """Test decoding using invalid jpeg format raises.""" index = get_indexed_datasets('1.2.840.10008.1.2.4.90') ds = index['US1_J2KR.dcm']['ds'] frame = next(generate_frames(ds)) arr = decode(frame, reshape=False) assert arr.flags.writeable assert (ds.Rows * ds.Columns * ds.SamplesPerPixel, ) == arr.shape
def test_unsupported_syntax_raises(self): """Test exception gets raised if unsupported transfer syntax.""" index = get_indexed_datasets('1.2.840.10008.1.2.1') ds = index['CT_small.dcm']['ds'] assert '1.2.840.10008.1.2.1' == ds.file_meta.TransferSyntaxUID msg = (r"Unable to convert the pixel data as the transfer syntax " r"is not supported by the openjpeg pixel data handler.") with pytest.raises(NotImplementedError, match=msg): handler.get_pixeldata(ds)
def test_decode_bad_format_raises(self): """Test decoding using invalid jpeg format raises.""" index = get_indexed_datasets('1.2.840.10008.1.2.4.90') ds = index['MR_small_jp2klossless.dcm']['ds'] frame = next(generate_frames(ds)) msg = r"Unsupported 'j2k_format' value: 3" with pytest.raises(ValueError, match=msg): decode(frame, j2k_format=3)
def test_no_dataset(self): index = get_indexed_datasets('1.2.840.10008.1.2.4.90') ds = index['MR_small_jp2klossless.dcm']['ds'] frame = next(generate_frames(ds)) arr = decode_pixel_data(frame) assert arr.flags.writeable assert 'uint8' == arr.dtype length = ds.Rows * ds.Columns * ds.SamplesPerPixel * ds.BitsAllocated / 8 assert (length, ) == arr.shape
def setup(self): add_handler() self.ds = get_indexed_datasets(self.uid) # Check if is already supported, and if not add it self.has_tsyntax = True if self.uid not in handler.SUPPORTED_TRANSFER_SYNTAXES: self.has_tsyntax = False handler.SUPPORTED_TRANSFER_SYNTAXES.append(self.uid)
def test_invalid_pixel_data(self): """Test that warning issued if Pixel Data is not a J2K codestream.""" index = get_indexed_datasets('1.2.840.10008.1.2.4.90') ds = index['GDCMJ2K_TextGBR.dcm']['ds'] msg = (r"The \(7FE0,0010\) Pixel Data contains a JPEG 2000 codestream " r"with the optional JP2 file format header, which is " r"non-conformant to the DICOM Standard \(Part 5, Annex A.4.4\)") with pytest.warns(UserWarning, match=msg): ds.pixel_array
def test_missing_element_raises(self): """Test exception gets raised if required element missing.""" index = get_indexed_datasets('1.2.840.10008.1.2.4.90') ds = index['US1_J2KR.dcm']['ds'] del ds.BitsAllocated assert 'BitsAllocated' not in ds msg = (r"Unable to convert the pixel data as the following required " r"elements are missing from the dataset: BitsAllocated") with pytest.raises(AttributeError, match=msg): handler.get_pixeldata(ds)
def test_invalid_pixel_representation(self): """Test that warning issued when Pixel Representation doesn't match.""" index = get_indexed_datasets('1.2.840.10008.1.2.4.90') ds = index['TOSHIBA_J2K_SIZ0_PixRep1.dcm']['ds'] msg = ( r"The \(0028,0103\) Pixel Representation value '1' \(signed\) in " r"the dataset does not match the format of the values found in " r"the JPEG 2000 data 'unsigned'") with pytest.warns(UserWarning, match=msg): ds.pixel_array
def test_invalid_bits_stored(self): """Test that warning issued when Bits Stored doesn't match.""" index = get_indexed_datasets('1.2.840.10008.1.2.4.91') ds = index['OsirixFake16BitsStoredFakeSpacing.dcm']['ds'] msg = ( r"The \(0028,0101\) Bits Stored value '16' in the dataset does " r"not match the component precision value '11' found in the JPEG " r"2000 data. It's recommended that you change the Bits Stored " r"value to produce the correct output") with pytest.warns(UserWarning, match=msg): ds.pixel_array
def test_jpeg2000i(self, fname, info): """Test get_parameters() for the baseline datasets.""" index = get_indexed_datasets('1.2.840.10008.1.2.4.91') ds = index[fname]['ds'] frame = next(generate_frames(ds)) params = get_parameters(frame) assert (info[0], info[1]) == (params['rows'], params['columns']) assert info[2] == params['nr_components'] assert info[3] == params['precision'] assert info[4] == params['is_signed']
def test_signed_error(self): """Regression test for #30.""" index = get_indexed_datasets('1.2.840.10008.1.2.4.90') ds = index['693_J2KR.dcm']['ds'] frame = next(generate_frames(ds)) arr = decode(frame) #print(arr) #import matplotlib.pyplot as plt #plt.imshow(arr) #plt.show() assert -2000 == arr[0, 0]
def test_decode_bad_type_raises(self): """Test decoding using invalid type raises.""" index = get_indexed_datasets('1.2.840.10008.1.2.4.90') ds = index['MR_small_jp2klossless.dcm']['ds'] frame = tuple(next(generate_frames(ds))) assert not hasattr(frame, 'tell') and not isinstance(frame, bytes) msg = ( r"The Python object containing the encoded JPEG 2000 data must " r"either be bytes or have read\(\), tell\(\) and seek\(\) methods." ) with pytest.raises(TypeError, match=msg): decode(frame)
def test_invalid_samples_per_pixel(self): """Test that warning issued when Samples Per Pixel doesn't match.""" index = get_indexed_datasets('1.2.840.10008.1.2.4.90') ds = index['693_J2KR.dcm']['ds'] ds.SamplesPerPixel = 3 msg = ( r"The \(0028,0002\) Samples per Pixel value '3' in the dataset " r"does not match the number of components '1' found in the JPEG " r"2000 data. It's recommended that you change the Samples per " r"Pixel value to produce the correct output") with pytest.warns(UserWarning, match=msg): with pytest.raises(ValueError): ds.pixel_array
def test_decode_reshape_true(self): """Test decoding using invalid jpeg format raises.""" index = get_indexed_datasets('1.2.840.10008.1.2.4.90') ds = index['US1_J2KR.dcm']['ds'] frame = next(generate_frames(ds)) arr = decode(frame) assert arr.flags.writeable assert (ds.Rows, ds.Columns, ds.SamplesPerPixel) == arr.shape # Values checked against GDCM assert [[180, 26, 0], [172, 15, 0], [162, 9, 0], [152, 4, 0], [145, 0, 0], [132, 0, 0], [119, 0, 0], [106, 0, 0], [87, 0, 0], [37, 0, 0], [0, 0, 0], [50, 0, 0], [100, 0, 0], [109, 0, 0], [122, 0, 0], [135, 0, 0], [145, 0, 0], [155, 5, 0], [165, 11, 0], [175, 17, 0]] == arr[175:195, 28, :].tolist()
def test_decode_bytes(self): """Test decoding using bytes.""" index = get_indexed_datasets('1.2.840.10008.1.2.4.90') ds = index['MR_small_jp2klossless.dcm']['ds'] frame = next(generate_frames(ds)) assert isinstance(frame, bytes) arr = decode(frame) assert arr.flags.writeable assert 'int16' == arr.dtype assert (ds.Rows, ds.Columns) == arr.shape # It'd be nice to standardise the pixel value testing... 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:]) assert 862 == arr[-1, -1]
def setup(self): add_handler() self.ds = get_indexed_datasets(self.uid)
def setup(self): self.ds = get_indexed_datasets(self.uid)
def test_should_change_pi(self): """Test the pointless function.""" index = get_indexed_datasets('1.2.840.10008.1.2.4.90') ds = index['US1_J2KR.dcm']['ds'] func = (handler.should_change_PhotometricInterpretation_to_RGB) assert not func(ds)