Skip to content

ENH: Add parser for Siemens "ASCCONV" text format #896

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

Closed
wants to merge 3 commits into from

Conversation

moloney
Copy link
Contributor

@moloney moloney commented Mar 24, 2020

This is a clean PR after I screwed up the merge in #418

This includes Matthew Brett's hard work improving the parser (from his 'ascconv-refactor' branch).

@codecov
Copy link

codecov bot commented Mar 24, 2020

Codecov Report

Merging #896 into master will increase coverage by 0.01%.
The diff coverage is 89.87%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #896      +/-   ##
==========================================
+ Coverage   91.70%   91.71%   +0.01%     
==========================================
  Files          96       98       +2     
  Lines       12311    12438     +127     
  Branches     2173     2191      +18     
==========================================
+ Hits        11290    11408     +118     
- Misses        684      688       +4     
- Partials      337      342       +5     
Impacted Files Coverage Δ
nibabel/nicom/ascconv.py 89.87% <89.87%> (ø)
nibabel/volumeutils.py 83.94% <0.00%> (-0.20%) ⬇️
nibabel/dft.py 80.36% <0.00%> (ø)
nibabel/affines.py 100.00% <0.00%> (ø)
nibabel/processing.py 100.00% <0.00%> (ø)
nibabel/wrapstruct.py 97.01% <0.00%> (ø)
nibabel/cmdline/conform.py 100.00% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 65d5fc6...cee8b22. Read the comment docs.

Copy link
Member

@effigies effigies left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks quite reasonable to me. The code is well-exercised, except for error cases. Would it be possible to add a malformed file that hits these?

@matthew-brett Since this includes your contributions, would you care to have a look through?

Copy link
Member

@matthew-brett matthew-brett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to make the changes I've asked for here, if y'all like.

@matthew-brett
Copy link
Member

Ok for me to add the fixes from my comments, and merge?

@effigies
Copy link
Member

Sounds good to me. @moloney, any objections?

Copy link
Member

@mgxd mgxd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tiny nitpick - this looks great, thanks @moloney and @matthew-brett!

matthew-brett and others added 2 commits May 19, 2020 16:54
Co-authored-by: Mathias Goncalves <[email protected]>
Co-authored-by: Mathias Goncalves <[email protected]>
@matthew-brett
Copy link
Member

Thanks - nitpicks applied!

Copy link
Member

@effigies effigies left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coming back to this. Here are suggestions for purging OrderedDict from this PR in favor of dict.

@moloney If you prefer, I can take over this to get it through final review.

if isinstance(target, ast.Attribute):
atoms.append(Atom(target, prev_target_type, target.attr))
target = target.value
prev_target_type = OrderedDict
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
prev_target_type = OrderedDict
prev_target_type = dict

Comment on lines +184 to +186
prot_dict : OrderedDict
Meta data pulled from the ASCCONV section.
attrs : OrderedDict
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
prot_dict : OrderedDict
Meta data pulled from the ASCCONV section.
attrs : OrderedDict
prot_dict : dict
Meta data pulled from the ASCCONV section.
attrs : dict

A line of the ASCCONV section could not be parsed.
'''
attrs, content = ASCCONV_RE.match(ascconv_str).groups()
attrs = OrderedDict((tuple(x.split('=')) for x in attrs.split()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
attrs = OrderedDict((tuple(x.split('=')) for x in attrs.split()))
attrs = dict(x.split('=') for x in attrs.split())

# Use Python's own parser to parse modified ASCCONV assignments
tree = ast.parse(content)

prot_dict = OrderedDict()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
prot_dict = OrderedDict()
prot_dict = dict()

with open(ASCCONV_INPUT, 'rt') as fobj:
contents = fobj.read()
ascconv_dict, attrs = ascconv.parse_ascconv(contents, str_delim='""')
assert attrs == OrderedDict()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert attrs == OrderedDict()
assert attrs == {}

@effigies
Copy link
Member

effigies commented Jul 6, 2020

Ok for me to add the fixes from my comments, and merge?

@matthew-brett Just saw this (again, probably...). Feel free to patch if you have time. Otherwise I can.

@matthew-brett
Copy link
Member

I can do the changes. I think we'll need OrderedDict unless we are dropping Python 3.6. Is that imminent?

@effigies
Copy link
Member

effigies commented Jul 6, 2020

No, we only just dropped 3.5.

@matthew-brett
Copy link
Member

On further investigation, dicts are insertion-ordered in CPython as of CPython 3.6, and guaranteed to be so ordered from Python 3.7. PyPy actually had this before CPython, so I believe Python dicts are insertion ordered in PyPy 3.6.

So our only worry for 3.6 would be some implementation other than CPython or PyPy, that has chosen some other algorithm for dicts, or kept the old CPython one for some reason. That seems very unlikely. So I think we can reasonably drop the requirement for OrderedDict soon.

But - maybe not with this PR. Maybe that should be a separate change.

What do y'all think?

@matthew-brett
Copy link
Member

I put some more edits from the comments here into #935

Sorry - I should have applied the new commits here, but I forgot how to do that.

@effigies
Copy link
Member

Hi Matthew. #896 looks identical to this. Did you push all of your commits?

@matthew-brett
Copy link
Member

matthew-brett commented Jul 11, 2020 via email

effigies added a commit that referenced this pull request Jul 12, 2020
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

Successfully merging this pull request may close these issues.

4 participants