def get_private(dicom): '''get private tags ''' datasets = [dicom] private_tags = [] while len(datasets) > 0: ds = datasets.pop(0) taglist = sorted(ds.keys()) for tag in taglist: with tag_in_exception(tag): if tag in ds: try: data_element = ds[tag] if data_element.tag.is_private: bot.debug(data_element.name) private_tags.append(data_element) if tag in ds and data_element.VR == "SQ": sequence = data_element.value for dataset in sequence: datasets.append(dataset) except IndexError: bot.debug("tag %s key present without value" % tag) except NotImplementedError: bot.debug('tag %s is invalid, skipping' % tag) return private_tags
def walk(self, callback, recursive=True): """Iterate through the DataElements and run `callback` on each. Visit all DataElements, possibly recursing into sequences and their datasets. The callback function is called for each DataElement (including SQ element). Can be used to perform an operation on certain types of DataElements. E.g., `remove_private_tags`() finds all private tags and deletes them. DataElement`s will come back in DICOM order (by increasing tag number within their dataset). Parameters ---------- callback A callable that takes two arguments: * a Dataset * a DataElement belonging to that Dataset recursive : bool Flag to indicate whether to recurse into Sequences. """ 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 write_dataset(fp, dataset, parent_encoding=default_encoding): """Write a Dataset dictionary to the file. Return the total length written. Attempt to correct ambiguous VR elements when explicit little/big encoding Elements that can't be corrected will be returned unchanged. """ _harmonize_properties(dataset, fp) if not fp.is_implicit_VR and not dataset.is_original_encoding: dataset = correct_ambiguous_vr(dataset, fp.is_little_endian) 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: # do not write retired Group Length (see PS3.5, 7.2) if tag.element == 0 and tag.group > 6: continue with tag_in_exception(tag): write_data_element(fp, dataset.get_item(tag), dataset_encoding) return fp.tell() - fpStart
def write_dataset( fp: DicomIO, dataset: Dataset, parent_encoding: Union[str, List[str]] = default_encoding) -> int: """Write a Dataset dictionary to the file. Return the total length written. """ _harmonize_properties(dataset, fp) if None in (dataset.is_little_endian, dataset.is_implicit_VR): name = dataset.__class__.__name__ raise AttributeError( f"'{name}.is_little_endian' and '{name}.is_implicit_VR' must " f"be set appropriately before saving") if not dataset.is_original_encoding: dataset = correct_ambiguous_vr(dataset, fp.is_little_endian) dataset_encoding = cast( Union[None, str, List[str]], dataset.get('SpecificCharacterSet', parent_encoding)) fpStart = fp.tell() # data_elements must be written in tag order tags = sorted(dataset.keys()) for tag in tags: # do not write retired Group Length (see PS3.5, 7.2) if tag.element == 0 and tag.group > 6: continue with tag_in_exception(tag): write_data_element(fp, dataset.get_item(tag), dataset_encoding) return fp.tell() - fpStart
def write_dataset(fp, dataset, parent_encoding=default_encoding): """Write a Dataset dictionary to the file. Return the total length written. Attempt to correct ambiguous VR elements when explicit little/big encoding Elements that can't be corrected will be returned unchanged. """ if not fp.is_implicit_VR: dataset = correct_ambiguous_vr(dataset, fp.is_little_endian) 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
def _pretty_str(self, indent=0, top_level_only=False): """Return a string of the DataElements in the 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(), therefore the top_level_only flag. This function recurses, with increasing indentation levels. Parameters ---------- index : int The indent level offset (default 0) top_level_only : bool When True, only create a string for the top level elements, i.e. exclude elements within any Sequences (default False). Returns ------- str A string representation of the Dataset. """ 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 DataElements in the 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(), therefore the top_level_only flag. This function recurses, with increasing indentation levels. Parameters ---------- index : int The indent level offset (default 0) top_level_only : bool When True, only create a string for the top level elements, i.e. exclude elements within any Sequences (default False). Returns ------- str A string representation of the Dataset. """ 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 test(): tag = Tag(0x00100010) with tag_in_exception(tag) as tag: raise ValueError('Test message.')