Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_matching_tags(self):
"""Dataset: key and data_element.tag mismatch raises ValueError....."""
def set_wrong_tag():
ds[0x10, 0x10] = data_element
ds = Dataset()
data_element = DataElement((0x300a, 0x00b2), "SH", "unit001")
self.assertRaises(ValueError, set_wrong_tag)
def test_read_UR_explicit_little(self):
"""Check creation of DataElement from byte data works correctly."""
ds = read_file(self.fp_ex, force=True)
ref_elem = ds.get(0x00080120) # URNCodeValue
elem = DataElement(0x00080120, 'UR', 'http://test.com')
self.assertEqual(ref_elem, elem)
# Test trailing spaces ignored
ref_elem = ds.get(0x00081190) # RetrieveURL
elem = DataElement(0x00081190, 'UR', 'ftp://test.com')
self.assertEqual(ref_elem, elem)
def test_empty_AT(self):
"""Write empty AT correctly.........."""
# Was issue 74
data_elem = DataElement(0x00280009, "AT", [])
expected = hex2bytes((
" 28 00 09 00" # (0028,0009) Frame Increment Pointer
" 00 00 00 00" # length 0
))
write_data_element(self.f1, data_elem)
got = self.f1.getvalue()
msg = ("Did not write zero-length AT value correctly. "
"Expected %r, got %r") % (bytes2hex(expected), bytes2hex(got))
msg = "%r %r" % (type(expected), type(got))
msg = "'%r' '%r'" % (expected, got)
self.assertEqual(expected, got, msg)
def test_implicit_little(self):
"""Test encoding using implicit VR little endian."""
elem = DataElement(0x00100010, 'PN', 'CITIZEN^Snips')
bytestring = encode_element(elem, True, True)
self.assertEqual(bytestring, b'\x10\x00\x10\x00\x0e\x00\x00\x00\x43' \
b'\x49\x54\x49\x5a\x45\x4e\x5e\x53\x6e' \
"""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)
def blank_field(self, field):
"""Blank a field"""
element = self.get_nested_field(field)
# Assert we have a data element, and can blank a string
if element:
if not isinstance(element, DataElement):
bot.warning("Issue parsing %s as a DataElement, not blanked." % field)
elif element.VR in ["US", "SS"]:
element.value = ""
else:
bot.warning("Unrecognized VR for %s, skipping blank." % field)
:func:`~pydicom.tag.Tag` such as ``[0x0010, 0x0010]``,
``(0x10, 0x10)``, ``0x00100010``, etc.
VR : str
The 2 character DICOM value representation (see DICOM Standard,
Part 5, :dcm:`Section 6.2`).
value
The value of the data element. One of the following:
* a single string or number
* a :class:`list` or :class:`tuple` with all strings or all numbers
* a multi-value string with backslash separator
* for a sequence element, an empty :class:`list` or ``list`` of
:class:`Dataset`
"""
data_element = DataElement(tag, VR, value)
# use data_element.tag since DataElement verified it
self._dict[data_element.tag] = data_element
If a :class:`slice` is used then returns a :class:`Dataset` object
containing the corresponding
:class:`DataElements`.
"""
# If passed a slice, return a Dataset containing the corresponding
# DataElements
if isinstance(key, slice):
return self._dataset_slice(key)
if isinstance(key, BaseTag):
tag = key
else:
tag = Tag(key)
data_elem = self._dict[tag]
if isinstance(data_elem, DataElement):
if data_elem.VR == 'SQ' and data_elem.value:
# let a sequence know its parent dataset, as sequence items
# may need parent dataset tags to resolve ambiguous tags
data_elem.value.parent = self
return data_elem
elif isinstance(data_elem, tuple):
# If a deferred read, then go get the value now
if data_elem.value is None and data_elem.length != 0:
from pydicom.filereader import read_deferred_data_element
data_elem = read_deferred_data_element(
self.fileobj_type, self.filename, self.timestamp,
data_elem)
if tag != BaseTag(0x00080005):
character_set = self.read_encoding or self._character_set
else:
raise DICOMJSONError(
'Data element "{}" must have key "vr".'.format(tag)
)
supported_keys = {'Value', 'BulkDataURI', 'InlineBinary'}
val_key = None
for k in supported_keys:
if k in val:
val_key = k
break
if val_key is None:
logger.debug(
'data element has neither key "{}".'.format(
'" nor "'.join(supported_keys)
)
)
e = pydicom.dataelem.DataElement(
tag=tag, value=None, VR=vr
)
else:
e = _create_dataelement(key, val['vr'], val[val_key])
ds.add(e)
elem_value.append(ds)
elif vr == 'PN':
# Special case, see DICOM Part 18 Annex F2.2
elem_value = []
for v in value:
if not isinstance(v, dict):
# Some DICOMweb services get this wrong, so we workaround the
# the issue and warn the user rather than raising an error.
logger.warning(
'attribute with VR Person Name (PN) is not '
'formatted correctly'
if vr == 'US or SS': # US or SS is up to us as the data is already
if value < 0: # decoded. We therefore choose US, unless we
vr = 'SS' # need a signed value.
else:
vr = 'US'
if vr == 'SQ': # We have a sequence of datasets, so we recurse
seq_list = [__make_dataset([__dicomify(subkey, listvalue[subkey])
for subkey in listvalue.keys()])
for listvalue in value
]
seq = pydicom.sequence.Sequence(seq_list)
return pydicom.dataelem.DataElement(tag, vr, seq)
else:
return pydicom.dataelem.DataElement(tag, vr, value)