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 DICOM keywords. This is called for code like: ``if 'SliceLocation' in dataset``. """ if isinstance(name, (str, compat.text_type)): 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 data_element(self, name): """Return the full data_element instance for the given descriptive name. Parameters ---------- name: str A DICOM keyword Returns ------- DataElement instance or None 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 __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 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 is not None 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 __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
parser.add_argument('-r','--queryRetrieveLevel', help='query retrieve level', default='PATIENT') args = parser.parse_args() if path.isfile(args.log): fileConfig(args.log) else: logging.warning("could not find configuration log file '%s'" % args.log) #starts our pacs instance pacs = Pacs( args.port, args.aet) pacs.connect(args.remotehost, args.remoteport, args.aec) ds = Dataset() ds.QueryRetrieveLevel = args.queryRetrieveLevel for k in args.keys: parts=k.split('=') tag = tag_for_name(parts[0]) ds.add_new(tag, dictionaryVR(tag) , parts[1]) items = pacs.query(ds) for i in items: print '---' print i