def walk(self, callback, recursive=True): """Call the given function for all dataset data_elements (recurses). Visit all data_elements, recurse into sequences and their datasets (if specified), The callback function is called for each data_element (including SQ element). Can be used to perform an operation on certain types of data_elements. E.g., `remove_private_tags`() finds all private tags and deletes them. :param callback: a callable taking two arguments: a dataset, and a data_element belonging to that dataset. :param recursive: a boolean indicating whether to recurse into Sequences `DataElement`s will come back in DICOM order (by increasing tag number within their dataset) """ taglist = sorted(self.keys()) for tag in taglist: with tag_in_exception(tag): data_element = self[tag] callback(self, data_element) # self = this Dataset # 'tag in self' below needed in case callback deleted data_element if recursive and tag in self and data_element.VR == "SQ": sequence = data_element.value for dataset in sequence: dataset.walk(callback)
def _pretty_str(self, indent=0, top_level_only=False): """Return a string of the data_elements in this dataset, with indented levels. This private method is called by the __str__() method for handling print statements or str(dataset), and the __repr__() method. It is also used by top(), which is the reason for the top_level_only flag. This function recurses, with increasing indentation levels. """ strings = [] indent_str = self.indent_chars * indent nextindent_str = self.indent_chars * (indent + 1) for data_element in self: with tag_in_exception(data_element.tag): if data_element.VR == "SQ": # a sequence strings.append( indent_str + str(data_element.tag) + " %s %i item(s) ---- " % (data_element.description(), len(data_element.value)) ) if not top_level_only: for dataset in data_element.value: strings.append(dataset._pretty_str(indent + 1)) strings.append(nextindent_str + "---------") else: strings.append(indent_str + repr(data_element)) return "\n".join(strings)
def _pretty_str(self, indent=0, top_level_only=False): """Return a string of the data_elements in this dataset, with indented levels. This private method is called by the __str__() method for handling print statements or str(dataset), and the __repr__() method. It is also used by top(), which is the reason for the top_level_only flag. This function recurses, with increasing indentation levels. """ strings = [] indent_str = self.indent_chars * indent nextindent_str = self.indent_chars * (indent + 1) for data_element in self: with tag_in_exception(data_element.tag): if data_element.VR == "SQ": # a sequence strings.append( indent_str + str(data_element.tag) + " %s %i item(s) ---- " % (data_element.description(), len(data_element.value))) if not top_level_only: for dataset in data_element.value: strings.append(dataset._pretty_str(indent + 1)) strings.append(nextindent_str + "---------") else: strings.append(indent_str + repr(data_element)) return "\n".join(strings)
def write_dataset(fp, dataset, parent_encoding=default_encoding): """Write a Dataset dictionary to the file. Return the total length written.""" dataset_encoding = dataset.get('SpecificCharacterSet', parent_encoding) fpStart = fp.tell() # data_elements must be written in tag order tags = sorted(dataset.keys()) for tag in tags: with tag_in_exception(tag): # write_data_element(fp, dataset.get_item(tag), dataset_encoding) XXX for writing raw tags without converting to DataElement write_data_element(fp, dataset[tag], dataset_encoding) return fp.tell() - fpStart