Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parse_ascconv errors #1343

Open
bpinsard opened this issue Aug 23, 2024 · 3 comments
Open

parse_ascconv errors #1343

bpinsard opened this issue Aug 23, 2024 · 3 comments

Comments

@bpinsard
Copy link
Contributor

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
Copy link
Contributor

moloney commented Nov 21, 2024

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

@bpinsard
Copy link
Contributor Author

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?

@moloney
Copy link
Contributor

moloney commented Dec 5, 2024

I believe that was the original idea. For now I created a branch of dcmstack that uses csa_header for parsing meta data to help test it out on more data: moloney/dcmstack#96

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants