本文整理汇总了Python中pydicom.filereader.read_file函数的典型用法代码示例。如果您正苦于以下问题:Python read_file函数的具体用法?Python read_file怎么用?Python read_file使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了read_file函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setUp
def setUp(self):
self.jpeg_ls_lossless = read_file(jpeg_ls_lossless_name)
self.mr_small = read_file(mr_name)
self.emri_jpeg_ls_lossless = read_file(emri_jpeg_ls_lossless)
self.emri_small = read_file(emri_name)
self.original_handlers = pydicom.config.image_handlers
pydicom.config.image_handlers = [pillow_handler, numpy_handler]
示例2: test_multivalue_DA
def test_multivalue_DA(self):
"""Write DA/DT/TM data elements.........."""
multi_DA_expected = (date(1961, 8, 4), date(1963, 11, 22))
DA_expected = date(1961, 8, 4)
tzinfo = tzoffset('-0600', -21600)
multi_DT_expected = (datetime(1961, 8, 4),
datetime(1963, 11, 22, 12, 30, 0, 0,
tzoffset('-0600', -21600)))
multi_TM_expected = (time(1, 23, 45), time(11, 11, 11))
TM_expected = time(11, 11, 11, 1)
ds = read_file(datetime_name)
# Add date/time data elements
ds.CalibrationDate = MultiValue(DA, multi_DA_expected)
ds.DateOfLastCalibration = DA(DA_expected)
ds.ReferencedDateTime = MultiValue(DT, multi_DT_expected)
ds.CalibrationTime = MultiValue(TM, multi_TM_expected)
ds.TimeOfLastCalibration = TM(TM_expected)
ds.save_as(datetime_out)
# Now read it back in and check the values are as expected
ds = read_file(datetime_out)
self.assertSequenceEqual(multi_DA_expected, ds.CalibrationDate, "Multiple dates not written correctly (VR=DA)")
self.assertEqual(DA_expected, ds.DateOfLastCalibration, "Date not written correctly (VR=DA)")
self.assertSequenceEqual(multi_DT_expected, ds.ReferencedDateTime, "Multiple datetimes not written correctly (VR=DT)")
self.assertSequenceEqual(multi_TM_expected, ds.CalibrationTime, "Multiple times not written correctly (VR=TM)")
self.assertEqual(TM_expected, ds.TimeOfLastCalibration, "Time not written correctly (VR=DA)")
if os.path.exists(datetime_out):
os.remove(datetime_out) # get rid of the file
示例3: testValuesIdentical
def testValuesIdentical(self):
"""Deferred values exactly matches normal read..............."""
ds_norm = read_file(self.testfile_name)
ds_defer = read_file(self.testfile_name, defer_size=2000)
for data_elem in ds_norm:
tag = data_elem.tag
self.assertEqual(data_elem.value, ds_defer[tag].value, "Mismatched value for tag %r" % tag)
示例4: setUp
def setUp(self):
self.jpeg_2k = read_file(jpeg2000_name)
self.jpeg_2k_lossless = read_file(jpeg2000_lossless_name)
self.mr_small = read_file(mr_name)
self.emri_jpeg_2k_lossless = read_file(emri_jpeg_2k_lossless)
self.emri_small = read_file(emri_name)
self.original_handlers = pydicom.config.image_handlers
pydicom.config.image_handlers = [gdcm_handler]
示例5: testPrivateSQ
def testPrivateSQ(self):
"""Can read private undefined length SQ without error...................."""
# From issues 91, 97, 98. Bug introduced by fast reading, due to VR=None
# in raw data elements, then an undefined length private item VR is looked up,
# and there is no such tag, generating an exception
# Simply read the file, in 0.9.5 this generated an exception
read_file(priv_SQ_name)
示例6: testPlanarConfig
def testPlanarConfig(self):
px_data_ds = read_file(color_px_name)
pl_data_ds = read_file(color_pl_name)
assert px_data_ds.PlanarConfiguration != pl_data_ds.PlanarConfiguration
if have_numpy:
px_data = px_data_ds.pixel_array
pl_data = pl_data_ds.pixel_array
self.assertTrue(numpy.all(px_data == pl_data))
示例7: testwrite_short_uid
def testwrite_short_uid(self):
ds = read_file(rtplan_name)
ds.SOPInstanceUID = "1.2"
ds.save_as(rtplan_out)
ds = read_file(rtplan_out)
ds.save_as(rtplan_out)
self.assertEqual(ds.SOPInstanceUID, "1.2")
if os.path.exists(rtplan_out):
os.remove(rtplan_out) # get rid of the file
示例8: testNoPixelsRead
def testNoPixelsRead(self):
"""Returns all data elements before pixels using stop_before_pixels=False"""
# Just check the tags, and a couple of values
ctpartial = read_file(ct_name, stop_before_pixels=True)
ctpartial_tags = sorted(ctpartial.keys())
ctfull = read_file(ct_name)
ctfull_tags = sorted(ctfull.keys())
msg = "Tag list of partial CT read (except pixel tag and padding) did not match full read"
msg += "\nExpected: %r\nGot %r" % (ctfull_tags[:-2], ctpartial_tags)
missing = [Tag(0x7fe0, 0x10), Tag(0xfffc, 0xfffc)]
self.assertEqual(ctfull_tags, ctpartial_tags + missing, msg)
示例9: testNestedPrivateSQ
def testNestedPrivateSQ(self):
"""Can successfully read a private SQ which contains additional SQ's....."""
# From issue 113. When a private SQ of undefined length is used, the
# sequence is read in and the length of the SQ is determined upon
# identification of the SQ termination sequence. When using nested
# Sequences, the first termination sequence encountered actually
# belongs to the nested Sequence not the parent, therefore the
# remainder of the file is not read in properly
ds = read_file(nested_priv_SQ_name)
# Make sure that the entire dataset was read in
pixel_data_tag = TupleTag((0x7FE0, 0x10))
self.assertTrue(pixel_data_tag in ds, "Entire dataset was not parsed properly. PixelData is not present")
# Check that the DataElement is indeed a Sequence
tag = TupleTag((0x01, 0x01))
seq0 = ds[tag]
self.assertEqual(seq0.VR, "SQ", "First level sequence not parsed properly")
# Now verify the presence of the nested private SQ
seq1 = seq0[0][tag]
self.assertEqual(seq1.VR, "SQ", "Second level sequence not parsed properly")
# Now make sure the values that are parsed are correct
got = seq1[0][tag].value
expected = b"Double Nested SQ"
self.assertEqual(got, expected, "Expected a value of %s, got %s'" % (expected, got))
got = seq0[0][0x01, 0x02].value
expected = b"Nested SQ"
self.assertEqual(got, expected, "Expected a value of %s, got %s'" % (expected, got))
示例10: testCTPixelData
def testCTPixelData(self):
"""Check that we can read pixel data. Tests that we get last one in array."""
ct = read_file(ct_name)
expected = 909
got = ct.pixel_array[-1][-1]
msg = "Did not get correct value for last pixel: expected %d, got %r" % (expected, got)
self.assertEqual(expected, got, msg)
示例11: testCT
def testCT(self):
"""Returns correct values for sample data elements in test CT file...."""
ct = read_file(ct_name)
self.assertEqual(ct.file_meta.ImplementationClassUID, '1.3.6.1.4.1.5962.2',
"ImplementationClassUID not the expected value")
self.assertEqual(ct.file_meta.ImplementationClassUID,
ct.file_meta[0x2, 0x12].value,
"ImplementationClassUID does not match the value accessed by tag number")
# (0020, 0032) Image Position (Patient) [-158.13580300000001, -179.035797, -75.699996999999996]
got = ct.ImagePositionPatient
DS = pydicom.valuerep.DS
expected = [DS('-158.135803'), DS('-179.035797'), DS('-75.699997')]
self.assertTrue(got == expected, "ImagePosition(Patient) values not as expected."
"got {0}, expected {1}".format(got, expected))
self.assertEqual(ct.Rows, 128, "Rows not 128")
self.assertEqual(ct.Columns, 128, "Columns not 128")
self.assertEqual(ct.BitsStored, 16, "Bits Stored not 16")
self.assertEqual(len(ct.PixelData), 128 * 128 * 2, "Pixel data not expected length")
# Also test private elements name can be resolved:
expected = "[Duration of X-ray on]"
got = ct[(0x0043, 0x104e)].name
msg = "Mismatch in private tag name, expected '%s', got '%s'"
self.assertEqual(expected, got, msg % (expected, got))
示例12: testReadFileGivenFileObject
def testReadFileGivenFileObject(self):
"""filereader: can read using already opened file............"""
f = open(ct_name, "rb")
ct = read_file(f)
# Tests here simply repeat testCT -- perhaps should collapse the code together?
got = ct.ImagePositionPatient
DS = pydicom.valuerep.DS
expected = [DS("-158.135803"), DS("-179.035797"), DS("-75.699997")]
self.assertTrue(got == expected, "ImagePosition(Patient) values not as expected")
self.assertEqual(
ct.file_meta.ImplementationClassUID, "1.3.6.1.4.1.5962.2", "ImplementationClassUID not the expected value"
)
self.assertEqual(
ct.file_meta.ImplementationClassUID,
ct.file_meta[0x2, 0x12].value,
"ImplementationClassUID does not match the value accessed by tag number",
)
# (0020, 0032) Image Position (Patient) [-158.13580300000001, -179.035797, -75.699996999999996]
got = ct.ImagePositionPatient
expected = [DS("-158.135803"), DS("-179.035797"), DS("-75.699997")]
self.assertTrue(got == expected, "ImagePosition(Patient) values not as expected")
self.assertEqual(ct.Rows, 128, "Rows not 128")
self.assertEqual(ct.Columns, 128, "Columns not 128")
self.assertEqual(ct.BitsStored, 16, "Bits Stored not 16")
self.assertEqual(len(ct.PixelData), 128 * 128 * 2, "Pixel data not expected length")
# Should also be able to close the file ourselves without exception raised:
f.close()
示例13: testDeflate
def testDeflate(self):
"""Returns correct values for sample data elements in test compressed (zlib deflate) file"""
# Everything after group 2 is compressed. If we can read anything else, the decompression must have been ok.
ds = read_file(deflate_name)
got = ds.ConversionType
expected = "WSD"
self.assertEqual(got, expected, "Attempted to read deflated file data element Conversion Type, expected '%s', got '%s'" % (expected, got))
示例14: testRTstruct
def testRTstruct(self):
"""Returns correct values for sample elements in test RTSTRUCT file...."""
# RTSTRUCT test file has complex nested sequences -- see rtstruct.dump file
# Also has no DICOM header ... so tests 'force' argument of read_file
rtss = read_file(rtstruct_name, force=True)
expected = '1.2.840.10008.1.2' # implVR little endian
got = rtss.file_meta.TransferSyntaxUID
msg = "Expected transfer syntax %r, got %r" % (expected, got)
self.assertEqual(expected, got, msg)
frame_of_ref = rtss.ReferencedFrameOfReferenceSequence[0]
study = frame_of_ref.RTReferencedStudySequence[0]
uid = study.RTReferencedSeriesSequence[0].SeriesInstanceUID
expected = "1.2.826.0.1.3680043.8.498.2010020400001.2.1.1"
msg = "Expected Reference Series UID '%s', got '%s'" % (expected, uid)
self.assertEqual(expected, uid, msg)
got = rtss.ROIContourSequence[0].ContourSequence[2].ContourNumber
expected = 3
msg = "Expected Contour Number %d, got %r" % (expected, got)
self.assertEqual(expected, got, msg)
obs_seq0 = rtss.RTROIObservationsSequence[0]
got = obs_seq0.ROIPhysicalPropertiesSequence[0].ROIPhysicalProperty
expected = 'REL_ELEC_DENSITY'
msg = "Expected Physical Property '%s', got %r" % (expected, got)
self.assertEqual(expected, got, msg)
示例15: testExplicitVRBigEndianNoMeta
def testExplicitVRBigEndianNoMeta(self):
"""Read file without file meta with Big Endian Explicit VR dataset......."""
# Example file from CMS XiO 5.0 and above
# Still need to force read data since there is no 'DICM' marker present
ds = read_file(explicit_vr_be_no_meta, force=True)
got = ds.InstanceCreationDate
expected = "20150529"
self.assertEqual(got, expected, "Sample data element from dataset failed, expected '%s', got '%s'" % (expected, got))