nipy / nibabel

Python package to access a cacophony of neuro-imaging file formats
http://nipy.org/nibabel/
Other
656 stars 258 forks source link

parse_ascconv errors #1343

Open bpinsard opened 3 months ago

bpinsard commented 3 months ago

Are there examples of how to extract ascconv from a pydicom or dicomwrapper object?

wrapper= nibabel.nicom.dicomwrappers.wrapper_from_file('path/to/dicom')
csa = nibabel.nicom.csareader.get_csa_header(wrapper.dcm_data, 'series')
mrprot= csa['tags']['MrPhoenixProtocol']['items'][0]
asc=nibabel.nicom.ascconv.parse_ascconv(mrprot, '""') # 
--> 207     attrs, content = ASCCONV_RE.match(ascconv_str).groups()
    208     attrs = OrderedDict(tuple(x.split('=')) for x in attrs.split())
    209     # Normalize string start / end markers to something Python understands

AttributeError: 'NoneType' object has no attribute 'groups'

## hacking a bit for the regex to be smarter

nibabel.nicom.ascconv.ASCCONV_RE = re.compile(
     r'.*### ASCCONV BEGIN((?:\s*[^=\s]+=[^=\s]+)*) ###\n(.*?)\n### ASCCONV END ###.*',
     flags=re.M | re.S,
)

asc=nibabel.nicom.ascconv.parse_ascconv(mrprot, '""') #have to use double quote, as it seems to be the format when extracting the MrPhoenixProtocol the way above.

  Cell In[100], line 1
    asc=nibabel.nicom.ascconv.parse_ascconv(mrprot, '""')

  File ~/.virtualenvs/neuromod_data3.10/lib/python3.10/site-packages/nibabel/nicom/ascconv.py:212 in parse_ascconv
    tree = ast.parse(content)

  File /usr/lib/python3.10/ast.py:50 in parse
    return compile(source, filename, mode, flags,

  File <unknown>:1261
    sDiffusion.sFreeDiffusionData.sComment.0             = 0x55    # 'U'
                                                                    ^
SyntaxError: invalid syntax

Maybe ASCCONV format "evolved" and ast is no longer able to parse it when it contains new tag formats.

moloney commented 1 week ago

Could you check if this is fixed here: https://github.com/open-dicom/csa_header

bpinsard commented 5 days ago

Sorry for the late reply, and thanks for the pointer to that package. I tested it and it worked without issue, and the parsed structure is really great. Is the plan for nibabel to use that package in the near future to deprecate and replace the nibabel CSA parsers?