def enumerate_elements_match_tag_path_patterns()

in python/de-identifier/research_pacs/de_identifier/dicom_tag_path_pattern.py [0:0]


def enumerate_elements_match_tag_path_patterns(ds, tag_paths, except_tag_paths, sequence_prefix=[]):
  """
  Enumerator that returns all data elements of a Dataset that matches any of the tag path patterns 
  in `tag_paths` but none of the tag path patterns in `except_tag_paths`. For each data element 
  returned, it provides (the data element, the data element 8 hexa-digit tag number, the parent 
  data element).
  
  Args:
    ds: pydicom Dataset
    tag_paths (list): List of tag path patterns
    except_tag_paths (list): List of tag path patterns
    sequence_prefix: Used for the function iteration
  
  """
  for elem in ds:
    elem_sequence = sequence_prefix + [elem]
    if elem.VR == 'SQ':
      for item in elem:
        yield from enumerate_elements_match_tag_path_patterns(item, tag_paths, except_tag_paths, elem_sequence)
    else:
      
      # Check if the data element matches none of the tag path patterns in `except_tag_paths`
      match_except_tag_paths = False
      for tag_path in except_tag_paths:
        if _elem_sequence_match_tag_path_pattern(elem_sequence, tag_path):
          match_except_tag_paths = True
          break
      if match_except_tag_paths:
        continue
      
      # Check if the data element matches at least one the tag path patterns in `tag_paths`
      match_tag_paths = False
      for tag_path in tag_paths:
        if _elem_sequence_match_tag_path_pattern(elem_sequence, tag_path):
          match_tag_paths = True
          break
      if match_tag_paths:
        elem_full_tag = '.'.join([_get_elem_tag_hexa(i) for i in elem_sequence])
        yield elem, elem_full_tag, ds