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

ENH: Collapse linear and nonlinear transforms chains #170

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

oesteban
Copy link
Collaborator

Very undertested, but there is a new test that uses a "collapsed" transform from an ITK's .h5 file with one affine and one nonlinear (and it works).

BSpline transforms are not currently supported.

Related: #167, #169.
Resolves #89.

@codecov
Copy link

codecov bot commented Jul 20, 2022

Codecov Report

Patch coverage: 66.66% and project coverage change: -0.17 ⚠️

Comparison is base (54ad1ea) 98.59% compared to head (fbc9228) 98.42%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #170      +/-   ##
==========================================
- Coverage   98.59%   98.42%   -0.17%     
==========================================
  Files          13       13              
  Lines        1279     1273       -6     
  Branches      184      183       -1     
==========================================
- Hits         1261     1253       -8     
- Misses         10       11       +1     
- Partials        8        9       +1     
Flag Coverage Δ
travis 96.77% <66.66%> (-0.18%) ⬇️
unittests 98.37% <66.66%> (-0.17%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
nitransforms/linear.py 95.55% <33.33%> (-1.57%) ⬇️
nitransforms/manip.py 100.00% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@oesteban oesteban requested a review from effigies August 25, 2022 14:33
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.

Apologies for the slow response. I'm worried that this is doing something backwards driven by a misinterpretation of ITK's H5, rather than correcting an internal representation. I suspect what needs to happen is reversing the order of ITK's list when we take it in. From an API perspective, it seems almost guaranteed to trip up users if Aff(m1) @ Aff(m2) != Aff(m1 @ m2).

@@ -8,7 +8,6 @@
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
"""Common interface for transforms."""
from collections.abc import Iterable
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
from collections.abc import Iterable
from collections.abc import Iterable
from functools import reduce
import operator as op

Comment on lines +180 to 183
retval = self.transforms[-1]
for xfm in reversed(self.transforms[:-1]):
retval = xfm @ retval
return retval
Copy link
Member

Choose a reason for hiding this comment

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

I feel like it would be more intuitive to swap the arguments of the @ than to reverse the order of the list:

Suggested change
retval = self.transforms[-1]
for xfm in reversed(self.transforms[:-1]):
retval = xfm @ retval
return retval
retval = affines[0]
for xfm in affines[1:]:
retval = retval @ xfm
return retval

But we can also just use a reduce (I've added the imports above if you want to go this way):

Suggested change
retval = self.transforms[-1]
for xfm in reversed(self.transforms[:-1]):
retval = xfm @ retval
return retval
return reduce(op.matmul, self.transforms)

assert composed.reference is None
assert composed == nitl.Affine(mat1.dot(mat2))
assert composed == nitl.Affine(mat2 @ mat1)
Copy link
Member

Choose a reason for hiding this comment

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

Can you add comment on why we should expect Affine(mat1) @ Affine(mat2) == Affine(mat2 @ mat1)? This seems counterintuitive.

Very undertested, but currently there is a test that uses a "collapsed"
transform on an ITK's .h5 file with one affine and one nonlinear.

BSpline transforms not currently supported.

Resolves #89.
@oesteban
Copy link
Collaborator Author

I will be resuscitating this one over this week. Thanks for your patience!

@oesteban
Copy link
Collaborator Author

@mattcieslak also this (I'm remembering as I go) :D

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.

Extend the method to "collapse" transforms in a TransformChain to the nonlinear case
2 participants