本文整理汇总了Python中pydicom.dataelem.DataElement类的典型用法代码示例。如果您正苦于以下问题:Python DataElement类的具体用法?Python DataElement怎么用?Python DataElement使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DataElement类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_write_UR_explicit_little
def test_write_UR_explicit_little(self):
"""Test writing elements with VR of UR works correctly.
Elements with a VR of 'UR' use the newer explicit VR
encoded (see PS3.5 Section 7.1.2).
"""
# Even length URL
elem = DataElement(0x00080120, 'UR', 'ftp://bits')
encoded_elem = self.encode_element(elem, False, True)
# Tag pair (0008, 2001): 08 00 20 01
# VR (UR): \x55\x52
# Reserved: \x00\x00
# Length (4): \x0a\x00\x00\x00
# Value: \x66\x74\x70\x3a\x2f\x2f\x62\x69\x74\x73
ref_bytes = b'\x08\x00\x20\x01\x55\x52\x00\x00\x0a\x00\x00\x00' \
b'\x66\x74\x70\x3a\x2f\x2f\x62\x69\x74\x73'
self.assertEqual(encoded_elem, ref_bytes)
# Odd length URL has trailing \x20 (SPACE) padding
elem.value = 'ftp://bit'
encoded_elem = self.encode_element(elem, False, True)
ref_bytes = b'\x08\x00\x20\x01\x55\x52\x00\x00\x0a\x00\x00\x00' \
b'\x66\x74\x70\x3a\x2f\x2f\x62\x69\x74\x20'
self.assertEqual(encoded_elem, ref_bytes)
# Empty value
elem.value = ''
encoded_elem = self.encode_element(elem, False, True)
ref_bytes = b'\x08\x00\x20\x01\x55\x52\x00\x00\x00\x00\x00\x00'
self.assertEqual(encoded_elem, ref_bytes)
示例2: test_write_UC_implicit_little
def test_write_UC_implicit_little(self):
"""Test writing elements with VR of UC works correctly."""
# VM 1, even data
elem = DataElement(0x00189908, 'UC', 'Test')
encoded_elem = self.encode_element(elem)
# Tag pair (0018, 9908): 08 00 20 01
# Length (4): 04 00 00 00
# Value: \x54\x65\x73\x74
ref_bytes = b'\x18\x00\x08\x99\x04\x00\x00\x00\x54\x65\x73\x74'
self.assertEqual(encoded_elem, ref_bytes)
# VM 1, odd data - padded to even length
elem.value = 'Test.'
encoded_elem = self.encode_element(elem)
ref_bytes = b'\x18\x00\x08\x99\x06\x00\x00\x00\x54\x65\x73\x74\x2e\x20'
self.assertEqual(encoded_elem, ref_bytes)
# VM 3, even data
elem.value = ['Aa', 'B', 'C']
encoded_elem = self.encode_element(elem)
ref_bytes = b'\x18\x00\x08\x99\x06\x00\x00\x00\x41\x61\x5c\x42\x5c\x43'
self.assertEqual(encoded_elem, ref_bytes)
# VM 3, odd data - padded to even length
elem.value = ['A', 'B', 'C']
encoded_elem = self.encode_element(elem)
ref_bytes = b'\x18\x00\x08\x99\x06\x00\x00\x00\x41\x5c\x42\x5c\x43\x20'
self.assertEqual(encoded_elem, ref_bytes)
# Empty data
elem.value = ''
encoded_elem = self.encode_element(elem)
ref_bytes = b'\x18\x00\x08\x99\x00\x00\x00\x00'
self.assertEqual(encoded_elem, ref_bytes)
示例3: test_write_UR_implicit_little
def test_write_UR_implicit_little(self):
"""Test writing elements with VR of UR works correctly."""
# Even length URL
elem = DataElement(0x00080120, 'UR',
'http://github.com/darcymason/pydicom')
encoded_elem = self.encode_element(elem)
# Tag pair (0008, 2001): 08 00 20 01
# Length (36): 24 00 00 00
# Value: 68 to 6d
ref_bytes = b'\x08\x00\x20\x01\x24\x00\x00\x00\x68\x74' \
b'\x74\x70\x3a\x2f\x2f\x67\x69\x74\x68\x75' \
b'\x62\x2e\x63\x6f\x6d\x2f\x64\x61\x72\x63' \
b'\x79\x6d\x61\x73\x6f\x6e\x2f\x70\x79\x64' \
b'\x69\x63\x6f\x6d'
self.assertEqual(encoded_elem, ref_bytes)
# Odd length URL has trailing \x20 (SPACE) padding
elem.value = '../test/test.py'
encoded_elem = self.encode_element(elem)
# Tag pair (0008, 2001): 08 00 20 01
# Length (16): 10 00 00 00
# Value: 2e to 20
ref_bytes = b'\x08\x00\x20\x01\x10\x00\x00\x00\x2e\x2e' \
b'\x2f\x74\x65\x73\x74\x2f\x74\x65\x73\x74' \
b'\x2e\x70\x79\x20'
self.assertEqual(encoded_elem, ref_bytes)
# Empty value
elem.value = ''
encoded_elem = self.encode_element(elem)
self.assertEqual(encoded_elem, b'\x08\x00\x20\x01\x00\x00\x00\x00')
示例4: test_str_no_vr
def test_str_no_vr(self):
"""Test DataElement.__str__ output with no VR"""
elem = DataElement(0x00100010, 'PN', 'ANON')
assert "(0010, 0010) Patient's Name" in str(elem)
assert "PN: 'ANON'" in str(elem)
elem.showVR = False
assert "(0010, 0010) Patient's Name" in str(elem)
assert 'PN' not in str(elem)
示例5: test_equality_class_members
def test_equality_class_members(self):
"""Test equality is correct when ignored class members differ."""
dd = DataElement(0x00100010, 'PN', 'ANON')
dd.showVR = False
dd.file_tell = 10
dd.maxBytesToDisplay = 0
dd.descripWidth = 0
ee = DataElement(0x00100010, 'PN', 'ANON')
self.assertTrue(dd == ee)
示例6: test_description_unknown_private
def test_description_unknown_private(self):
"""Test DataElement.description with an unknown private element"""
elem = DataElement(0x00110010, 'LO', 12345)
elem.private_creator = 'TEST'
assert elem.description() == 'Private tag data'
elem = DataElement(0x00110F00, 'LO', 12345)
assert elem.tag.is_private
assert not hasattr(elem, 'private_creator')
assert elem.description() == 'Private tag data'
示例7: test_inequality_sequence
def test_inequality_sequence(self):
"""Test DataElement.__ne__ for sequence element"""
dd = DataElement(0x300A00B0, 'SQ', [])
assert not dd != dd
ee = DataElement(0x300A00B0, 'SQ', [])
assert not dd != ee
ee = DataElement(0x300A00B0, 'SQ', [Dataset()])
assert dd != ee
# Check value
dd.value = [Dataset()]
dd[0].PatientName = 'ANON'
ee[0].PatientName = 'ANON'
assert not dd != ee
ee[0].PatientName = 'ANONA'
assert dd != ee
示例8: test_write_OL_implicit_little
def test_write_OL_implicit_little(self):
"""Test writing elements with VR of OL works correctly."""
# TrackPointIndexList
bytestring = b'\x00\x01\x02\x03\x04\x05\x06\x07' \
b'\x01\x01\x02\x03'
elem = DataElement(0x00660129, 'OL', bytestring)
encoded_elem = self.encode_element(elem)
# Tag pair (0066, 0129): 66 00 29 01
# Length (12): 0c 00 00 00
# | Tag | Length | Value ->
ref_bytes = b'\x66\x00\x29\x01\x0c\x00\x00\x00' + bytestring
self.assertEqual(encoded_elem, ref_bytes)
# Empty data
elem.value = b''
encoded_elem = self.encode_element(elem)
ref_bytes = b'\x66\x00\x29\x01\x00\x00\x00\x00'
self.assertEqual(encoded_elem, ref_bytes)
示例9: test_write_OD_implicit_little
def test_write_OD_implicit_little(self):
"""Test writing elements with VR of OD works correctly."""
# VolumetricCurvePoints
bytestring = b'\x00\x01\x02\x03\x04\x05\x06\x07' \
b'\x01\x01\x02\x03\x04\x05\x06\x07'
elem = DataElement(0x0070150d, 'OD', bytestring)
encoded_elem = self.encode_element(elem)
# Tag pair (0070, 150d): 70 00 0d 15
# Length (16): 10 00 00 00
# | Tag | Length | Value ->
ref_bytes = b'\x70\x00\x0d\x15\x10\x00\x00\x00' + bytestring
self.assertEqual(encoded_elem, ref_bytes)
# Empty data
elem.value = b''
encoded_elem = self.encode_element(elem)
ref_bytes = b'\x70\x00\x0d\x15\x00\x00\x00\x00'
self.assertEqual(encoded_elem, ref_bytes)
示例10: testEqualityPrivateElement
def testEqualityPrivateElement(self):
"""DataElement: equality returns correct value for private elements"""
dd = DataElement(0x01110001, 'PN', 'ANON')
self.assertTrue(dd == dd)
ee = DataElement(0x01110001, 'PN', 'ANON')
self.assertTrue(dd == ee)
# Check value
ee.value = 'ANAN'
self.assertFalse(dd == ee)
# Check tag
ee = DataElement(0x01110002, 'PN', 'ANON')
self.assertFalse(dd == ee)
# Check VR
ee = DataElement(0x01110001, 'SH', 'ANON')
self.assertFalse(dd == ee)
示例11: test_write_UC_explicit_little
def test_write_UC_explicit_little(self):
"""Test writing elements with VR of UC works correctly.
Elements with a VR of 'UC' use the newer explicit VR
encoding (see PS3.5 Section 7.1.2).
"""
# VM 1, even data
elem = DataElement(0x00189908, 'UC', 'Test')
encoded_elem = self.encode_element(elem, False, True)
# Tag pair (0018, 9908): 08 00 20 01
# VR (UC): \x55\x43
# Reserved: \x00\x00
# Length (4): \x04\x00\x00\x00
# Value: \x54\x65\x73\x74
ref_bytes = b'\x18\x00\x08\x99\x55\x43\x00\x00\x04\x00\x00\x00' \
b'\x54\x65\x73\x74'
self.assertEqual(encoded_elem, ref_bytes)
# VM 1, odd data - padded to even length
elem.value = 'Test.'
encoded_elem = self.encode_element(elem, False, True)
ref_bytes = b'\x18\x00\x08\x99\x55\x43\x00\x00\x06\x00\x00\x00' \
b'\x54\x65\x73\x74\x2e\x20'
self.assertEqual(encoded_elem, ref_bytes)
# VM 3, even data
elem.value = ['Aa', 'B', 'C']
encoded_elem = self.encode_element(elem, False, True)
ref_bytes = b'\x18\x00\x08\x99\x55\x43\x00\x00\x06\x00\x00\x00' \
b'\x41\x61\x5c\x42\x5c\x43'
self.assertEqual(encoded_elem, ref_bytes)
# VM 3, odd data - padded to even length
elem.value = ['A', 'B', 'C']
encoded_elem = self.encode_element(elem, False, True)
ref_bytes = b'\x18\x00\x08\x99\x55\x43\x00\x00\x06\x00\x00\x00' \
b'\x41\x5c\x42\x5c\x43\x20'
self.assertEqual(encoded_elem, ref_bytes)
# Empty data
elem.value = ''
encoded_elem = self.encode_element(elem, False, True)
ref_bytes = b'\x18\x00\x08\x99\x55\x43\x00\x00\x00\x00\x00\x00'
self.assertEqual(encoded_elem, ref_bytes)
示例12: __setattr__
def __setattr__(self, name, value):
"""Intercept any attempts to set a value for an instance attribute.
If name is a dicom descriptive string (cleaned with CleanName),
then set the corresponding tag and data_element.
Else, set an instance (python) attribute as any other class would do.
"""
tag = tag_for_name(name)
if tag is not None: # successfully mapped name to a tag
if tag not in self: # don't have this tag yet->create the data_element instance
VR = dictionaryVR(tag)
data_element = DataElement(tag, VR, value)
else: # already have this data_element, just changing its value
data_element = self[tag]
data_element.value = value
# Now have data_element - store it in this dict
self[tag] = data_element
else: # name not in dicom dictionary - setting a non-dicom instance attribute
# XXX note if user mis-spells a dicom data_element - no error!!!
self.__dict__[name] = value
示例13: testEqualityStandardElement
def testEqualityStandardElement(self):
"""DataElement: equality returns correct value for simple elements"""
dd = DataElement(0x00100010, 'PN', 'ANON')
self.assertTrue(dd == dd)
ee = DataElement(0x00100010, 'PN', 'ANON')
self.assertTrue(dd == ee)
# Check value
ee.value = 'ANAN'
self.assertFalse(dd == ee)
# Check tag
ee = DataElement(0x00100011, 'PN', 'ANON')
self.assertFalse(dd == ee)
# Check VR
ee = DataElement(0x00100010, 'SH', 'ANON')
self.assertFalse(dd == ee)
dd = DataElement(0x00080018, 'UI', '1.2.3.4')
ee = DataElement(0x00080018, 'UI', '1.2.3.4')
self.assertTrue(dd == ee)
ee = DataElement(0x00080018, 'PN', '1.2.3.4')
self.assertFalse(dd == ee)
示例14: testEqualitySequenceElement
def testEqualitySequenceElement(self):
"""DataElement: equality returns correct value for sequence elements"""
dd = DataElement(0x300A00B0, 'SQ', [])
self.assertTrue(dd == dd)
ee = DataElement(0x300A00B0, 'SQ', [])
self.assertTrue(dd == ee)
# Check value
e = Dataset()
e.PatientName = 'ANON'
ee.value = [e]
self.assertFalse(dd == ee)
# Check tag
ee = DataElement(0x01110002, 'SQ', [])
self.assertFalse(dd == ee)
# Check VR
ee = DataElement(0x300A00B0, 'SH', [])
self.assertFalse(dd == ee)
# Check with dataset
dd = DataElement(0x300A00B0, 'SQ', [Dataset()])
dd.value[0].PatientName = 'ANON'
ee = DataElement(0x300A00B0, 'SQ', [Dataset()])
ee.value[0].PatientName = 'ANON'
self.assertTrue(dd == ee)
# Check uneven sequences
dd.value.append(Dataset())
dd.value[1].PatientName = 'ANON'
self.assertFalse(dd == ee)
ee.value.append(Dataset())
ee.value[1].PatientName = 'ANON'
self.assertTrue(dd == ee)
ee.value.append(Dataset())
ee.value[2].PatientName = 'ANON'
self.assertFalse(dd == ee)
示例15: __setattr__
def __setattr__(self, name, value):
"""Intercept any attempts to set a value for an instance attribute.
If name is a DICOM keyword, set the corresponding tag and DataElement.
Else, set an instance (python) attribute as any other class would do.
Parameters
----------
name : str
The element keyword for the DataElement you wish to add/change. If
`name` is not a DICOM element keyword then this will be the
name of the attribute to be added/changed.
value
The value for the attribute to be added/changed.
"""
tag = tag_for_keyword(name)
if tag is not None: # successfully mapped name to a tag
if tag not in self:
# don't have this tag yet->create the data_element instance
VR = dictionary_VR(tag)
data_element = DataElement(tag, VR, value)
else:
# already have this data_element, just changing its value
data_element = self[tag]
data_element.value = value
# Now have data_element - store it in this dict
self[tag] = data_element
elif repeater_has_keyword(name):
# Check if `name` is repeaters element
raise ValueError('{} is a DICOM repeating group '
'element and must be added using '
'the add() or add_new() methods.'
.format(name))
else:
# name not in dicom dictionary - setting a non-dicom instance
# attribute
# XXX note if user mis-spells a dicom data_element - no error!!!
super(Dataset, self).__setattr__(name, value)