def tag(self): """Return the element's tag as a pydicom.tag.Tag.""" tag = self.components[0] if self.is_sequence: tag = tag.split('[')[0] # (gggg,eeee) based tag if ',' in tag: group, element = tag.split(',') if '(' in group: group = group.replace('(', '') if ')' in element: element = element.replace(')', '') if len(group) != 4 or len(element) != 4: raise ValueError(f"Unable to parse element path component: " f"'{self.components[0]}'") return Tag(group, element) # From this point on we assume that a keyword was supplied kw = tag # Keyword based tag - private keywords not allowed if repeater_has_keyword(kw): raise ValueError( f"Repeating group elements must be specified using " f"(gggg,eeee): '{self.components[0]}'") tag = tag_for_keyword(kw) # Test against None as 0x00000000 is a valid tag if tag is not None: return Tag(tag) raise ValueError( f"Unable to parse element path component: '{self.components[0]}'")
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)
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)
def test_repeater_has_keyword(self): """Test repeater_has_keyword""" self.assertTrue(repeater_has_keyword('OverlayData')) self.assertFalse(repeater_has_keyword('PixelData'))
def test_repeater_has_keyword(self): """Test repeater_has_keyword""" self.assertTrue(repeater_has_keyword('OverlayData')) self.assertFalse(repeater_has_keyword('PixelData'))
def test_repeater_has_keyword(self): """Test repeater_has_keyword""" assert repeater_has_keyword('OverlayData') assert not repeater_has_keyword('PixelData')
def test_repeater_has_keyword(self): """Test repeater_has_keyword""" assert repeater_has_keyword('OverlayData') assert not repeater_has_keyword('PixelData')