def data_element(self, name): """Return the full data_element instance for the given descriptive name. When using *named tags*, only the value is returned. If you want the whole data_element object, for example to change the data_element.VR, call this function with the name and the data_element instance is returned.""" tag = tag_for_name(name) if tag: return self[tag] return None
def data_element(self, name): """Return the full data_element instance for the given descriptive name :param name: a DICOM keyword :returns: a DataElement instance in this dataset with the given name If the tag for that name is not found, returns None """ tag = tag_for_name(name) if tag: return self[tag] return None
def __contains__(self, name): """Extend dict.__contains__() to handle DICOM keywords. This is called for code like: ``if 'SliceLocation' in dataset``. """ if isinstance(name, (str, unicode)): tag = tag_for_name(name) else: try: tag = Tag(name) except: return False if tag: return dict.__contains__(self, tag) else: return dict.__contains__(self, name) # will no doubt raise an exception
def __getattr__(self, name): """Intercept requests for unknown Dataset python-attribute names. If the name matches a Dicom keyword, return the value for the data_element with the corresponding tag. """ # __getattr__ only called if instance cannot find name in self.__dict__ # So, if name is not a dicom string, then is an error tag = tag_for_name(name) if tag is None: raise AttributeError("Dataset does not have attribute " "'{0:s}'.".format(name)) tag = Tag(tag) if tag not in self: raise AttributeError("Dataset does not have attribute " "'{0:s}'.".format(name)) else: # do have that dicom data_element return self[tag].value
def __contains__(self, name): """Extend dict.__contains__() to handle *named tags*. This is called for code like: ``if 'SliceLocation' in dataset``. """ if is_stringlike(name): tag = tag_for_name(name) else: try: tag = Tag(name) except: return False if tag: return dict.__contains__(self, tag) else: return dict.__contains__(self, name) # will no doubt raise an exception
def __getattr__(self, name): """Intercept requests for unknown Dataset python-attribute names. If the name matches a Dicom dictionary string (without blanks etc), then return the value for the data_element with the corresponding tag. """ # __getattr__ only called if instance cannot find name in self.__dict__ # So, if name is not a dicom string, then is an error tag = tag_for_name(name) if tag is None: raise AttributeError, "Dataset does not have attribute '%s'." % name tag = Tag(tag) if tag not in self: raise AttributeError, "Dataset does not have attribute '%s'." % name else: # do have that dicom data_element return self[tag].value
def __contains__(self, name): """Extend dict.__contains__() to handle DICOM keywords. This is called for code like: ``if 'SliceLocation' in dataset``. """ if isinstance(name, str): tag = tag_for_name(name) else: try: tag = Tag(name) except: return False if tag: return dict.__contains__(self, tag) else: return dict.__contains__(self, name) # will no doubt raise an exception
def __delattr__(self, name): """Intercept requests to delete an attribute by name, e.g. del ds.name If name is a DICOM keyword, then delete the corresponding tag and data_element. Else, delete an instance (python) attribute as any other class would do """ # First check if a valid DICOM keyword and if we have that data element tag = tag_for_name(name) if tag and tag in self: del self[tag] # If not a DICOM name in this dataset, check for regular instance name # can't do delete directly, that will call __delattr__ again elif name in self.__dict__: del self.__dict__[name] # Not found, raise an error in same style as python does else: raise AttributeError(name)
def __delattr__(self, name): """Intercept requests to delete an attribute by name, e.g. del ds.name If name is a DICOM keyword, then delete the corresponding tag and data_element. Else, delete an instance (python) attribute as any other class would do """ # First check if a valid DICOM keyword and if we have that data element tag = tag_for_name(name) if tag and tag in self: dict.__delitem__(self, tag) # direct to dict as we know we have key # If not a DICOM name in this dataset, check for regular instance name # can't do delete directly, that will call __delattr__ again elif name in self.__dict__: del self.__dict__[name] # Not found, raise an error in same style as python does else: raise AttributeError(name)
def __delattr__(self, name): """Intercept requests to delete an attribute by name, e.g. del ds.name If name is a dicom descriptive string (cleaned with CleanName), then delete the corresponding tag and data_element. Else, delete an instance (python) attribute as any other class would do. """ # First check if is a valid DICOM name and if we have that data element tag = tag_for_name(name) if tag and tag in self: del self[tag] # If not a DICOM name (or we don't have it), check for regular instance name # can't do delete directly, that will call __delattr__ again! elif name in self.__dict__: del self.__dict__[name] # Not found, raise an error in same style as python does else: raise AttributeError, name
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
def _tag_for(self, name): return datadict.tag_for_name(name)