Skip to content

Commit

Permalink
Avoid generating pydicom user warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelzwiers committed Sep 27, 2024
1 parent ff617aa commit 3626ddc
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions bidscoin/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -1770,10 +1770,10 @@ def get_dicomfield(tagname: str, dicomfile: Path) -> Union[str, int]:
else:
dicomdata = _DICOMDICT_CACHE

try: # Try Pydicom's hexadecimal tag number first
value = eval(f"dicomdata[{tagname}].value") # NB: This may generate e.g. UserWarning: Invalid value 'filepath' used with the 'in' operator: must be an element tag as a 2-tuple or int, or an element keyword
except (NameError, KeyError, SyntaxError):
value = dicomdata.get(tagname,'') if tagname in dicomdata else '' # Then try and see if it is an attribute name. NB: Do not use dicomdata.get(tagname, '') to avoid using its class attributes (e.g. 'filename')
if re.fullmatch(r'\(?0x[\dA-Z]*,?(0x)?[\dA-Z]*\)?', tagname): # Try Pydicom's hexadecimal tag number first (must be a 2-tuple or int)
value = eval(f"dicomdata[{tagname}].value") # NB: This may generate e.g. UserWarning: Invalid value 'filepath' used with the 'in' operator: must be an element tag as a 2-tuple or int, or an element keyword
else:
value = dicomdata.get(tagname,'') if tagname in dicomdata else '' # Then try and see if it is an attribute name. NB: Do not use dicomdata.get(tagname, '') to avoid using its class attributes (e.g. 'filename')

# Try a recursive search
if not value and value != 0:
Expand Down

0 comments on commit 3626ddc

Please sign in to comment.