From 3626ddc1baa45874fda9e171854b87d75bc1e074 Mon Sep 17 00:00:00 2001 From: Marcel Zwiers Date: Fri, 27 Sep 2024 08:54:39 +0200 Subject: [PATCH] Avoid generating pydicom user warnings --- bidscoin/bids.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bidscoin/bids.py b/bidscoin/bids.py index 2e5a1c6b..f5b1ee4b 100644 --- a/bidscoin/bids.py +++ b/bidscoin/bids.py @@ -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: