nibabel icon indicating copy to clipboard operation
nibabel copied to clipboard

parse_ascconv errors

Open bpinsard opened this issue 6 months ago • 0 comments

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.

bpinsard avatar Aug 23 '24 13:08 bpinsard