def load_ctdata_from_NM(f): """load ct data from NM modality dicom and produce rttypes.volume.Volume""" dcm = dcmread(f) arr = dcm.pixel_array rescale_slope = dcm.get('RescaleSlope') rescale_int = dcm.get('RescaleIntercept') arr = arr * rescale_slope + rescale_int logger.debug('array shape: {!s}'.format(arr.shape)) logger.debug('array type: {!s}'.format(arr.dtype)) logger.debug('array min/max: {:0.2f}/{:0.2f}'.format(arr.min(), arr.max())) detinfo = dcm.get('DetectorInformationSequence')[0] start = [float(x) for x in detinfo.get('ImagePositionPatient')] orient = [float(x) for x in detinfo.get('ImageOrientationPatient')] assert orient == [0, 0, 0, 0, 0, 0] logger.debug('start: {!s}'.format(start)) spacing = [float(x) for x in dcm.get('PixelSpacing') ] + [float(dcm.get('SliceThickness'))] logger.debug('spacing: {!s}'.format(spacing)) frame = FrameOfReference(start=start, spacing=spacing, size=arr.shape[::-1]) logger.debug(str(frame)) vol = Volume.fromArray(arr, frame) vol.modality = 'CT' return vol
def test_trait_names(self): """Test Dataset.trait_names contains element keywords""" test_file = get_testdata_files('CT_small.dcm')[0] ds = dcmread(test_file, force=True) names = ds.trait_names() assert 'PatientName' in names assert 'save_as' in names assert 'PixelData' in names
def testRead(self): """Unicode: Can read a file with unicode characters in name...""" uni_name = u'test°' # verify first that we could encode file name in this environment try: _ = uni_name.encode(sys.getfilesystemencoding()) except UnicodeEncodeError: print("SKIP: Environment doesn't support unicode filenames") return try: dicomio.dcmread(uni_name) except UnicodeEncodeError: self.fail("UnicodeEncodeError generated for unicode name") # ignore file doesn't exist error except IOError: pass
def test_equality_file_meta(self): """Dataset: equality returns correct value if with metadata""" d = dcmread(self.test_file) e = dcmread(self.test_file) self.assertTrue(d == e) e.is_implicit_VR = not e.is_implicit_VR self.assertFalse(d == e) e.is_implicit_VR = not e.is_implicit_VR self.assertTrue(d == e) e.is_little_endian = not e.is_little_endian self.assertFalse(d == e) e.is_little_endian = not e.is_little_endian self.assertTrue(d == e) e.filename = 'test_filename.dcm' self.assertFalse(d == e)
def testLatin1(self): """charset: can read and decode latin_1 file........................""" ds = dicomio.dcmread(latin1_file) ds.decode() # Make sure don't get unicode encode error on converting to string expected = u'Buc^J\xe9r\xf4me' got = ds.PatientName self.assertEqual(expected, got, "Expected %r, got %r" % (expected, got))
def test_get_item(self): """Test Dataset.get_item""" ds = Dataset() ds.CommandGroupLength = 120 # 0000,0000 ds.SOPInstanceUID = '1.2.3.4' # 0008,0018 # Test non-deferred read assert ds.get_item(0x00000000) == ds[0x00000000] assert ds.get_item(0x00000000).value == 120 assert ds.get_item(0x00080018) == ds[0x00080018] assert ds.get_item(0x00080018).value == '1.2.3.4' # Test deferred read test_file = get_testdata_files('MR_small.dcm')[0] ds = dcmread(test_file, force=True, defer_size='0.8 kB') ds_ref = dcmread(test_file, force=True) # get_item will follow the deferred read branch assert ds.get_item((0x7fe00010)).value == ds_ref.PixelData
def testEncodingWithSpecificTags(self): """Encoding is correctly applied even if Specific Character Set is not in specific tags...""" ds = dicomio.dcmread(jp_file, specific_tags=['PatientName']) ds.decode() self.assertEqual(1, len(ds)) expected = ('Yamada^Tarou=' '\033$B;3ED\033(B^\033$BB@O:\033(B=' '\033$B$d$^$@\033(B^\033$B$?$m$&\033(B') self.assertEqual(expected, ds.PatientName)
def test_set_convert_private_elem_from_raw(self): """Test Dataset.__setitem__ with a raw private element""" test_file = get_testdata_files('CT_small.dcm')[0] ds = dcmread(test_file, force=True) # 'tag VR length value value_tell is_implicit_VR is_little_endian' elem = RawDataElement((0x0043, 0x1029), 'OB', 2, b'\x00\x01', 0, True, True) ds.__setitem__((0x0043, 0x1029), elem) assert ds[(0x0043, 0x1029)].value == b'\x00\x01' assert type(ds[(0x0043, 0x1029)]) == DataElement
def testNestedCharacterSets(self): """charset: can read and decode SQ with different encodings.........""" ds = dicomio.dcmread(sq_encoding_file) ds.decode() # These datasets inside of the SQ cannot be decoded with # default_encoding OR UTF-8 (the parent dataset's encoding). # Instead, we make sure that it is decoded using the # (0008,0005) tag of the dataset expected = (u'\uff94\uff8f\uff80\uff9e^\uff80\uff9b\uff73=' u'\u5c71\u7530^\u592a\u90ce=' u'\u3084\u307e\u3060^\u305f\u308d\u3046') got = ds[0x32, 0x1064][0].PatientName self.assertEqual(expected, got, "Expected %r, got %r" % (expected, got))
def test_with(self): """Test Dataset.__enter__ and __exit__.""" test_file = get_testdata_files('CT_small.dcm')[0] with dcmread(test_file) as ds: assert ds.PatientName == 'CompressedSamples^CT1'
def testMultiPN(self): """charset: can decode file with multi-valued data elements.........""" ds = dicomio.dcmread(multiPN_file) ds.decode()
def testExplicitISO2022_IR6(self): """charset: can decode file with multi-valued data elements.........""" ds = dicomio.dcmread(explicit_ir6_file) ds.decode()
def testStandardFile(self): """charset: can read and decode standard file without special char..""" ds = dicomio.dcmread(normal_file) ds.decode()