-
Notifications
You must be signed in to change notification settings - Fork 18
ENH: Collapse linear and nonlinear transforms chains #170
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
Open
oesteban
wants to merge
24
commits into
master
Choose a base branch
from
enh/89-collapse-nonlinear
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
fbc9228
ENH: Collapse linear and nonlinear transforms chains
oesteban 0b13408
enh: read X5 transform files
oesteban a22a6d0
refactor: simplify X5 loader
oesteban 2d3ba2f
test: cover x5.from_filename
oesteban eeb4d5d
enh: enable loading of X5 affines
oesteban 6b000c8
Merge pull request #244 from nipy/codex/exclude-tests-from-coverage-r…
oesteban 8e50969
fix: process exceptions when trying to open X5
oesteban b02077e
tst: add round-trip test to linear mappings
oesteban 1b544cb
move flake8 config to pyproject
oesteban c2f9bde
Merge pull request #245 from nipy/codex/migrate-flake8-config-to-pypr…
oesteban 33d91ad
hotfix: make tox pickup flake8 config from ``pyproject.toml``
oesteban 82f58c1
Merge pull request #243 from nipy/codex/add-support-for-affine-and-li…
oesteban 55e6937
tst: refactor io/lta to reduce one partial line
oesteban a94e577
Merge pull request #246 from nipy/tst/one-less-partial-lta
oesteban 987eaa8
Add failing test for serialized resampling
oesteban 5f08a36
enh: add docstring and doctests for `nitransforms.io.get_linear_factory`
oesteban 5da27b1
FIX: recompute targets for serialized per-volume resampling
oesteban b562eb3
fix: remove ``__iter__()`` as iterator protocol is not met
oesteban 72cd04f
fix: recompute coordinates per volume in serial resampling
oesteban 4e159c2
fix: generalize targets, test all branches
oesteban f1efba1
Merge pull request #247 from nipy/codex/investigate-4d-dataset-resamp…
oesteban a7265e4
Merge branch 'master' into tst/increase-coverage
oesteban 6f497c0
Merge pull request #248 from nipy/tst/increase-coverage
oesteban ac69db0
Merge branch 'master' into enh/89-collapse-nonlinear
oesteban File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -8,7 +8,6 @@ | |||||||||||||||||||||||||||
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## | ||||||||||||||||||||||||||||
"""Common interface for transforms.""" | ||||||||||||||||||||||||||||
from collections.abc import Iterable | ||||||||||||||||||||||||||||
import numpy as np | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
from .base import ( | ||||||||||||||||||||||||||||
TransformBase, | ||||||||||||||||||||||||||||
|
@@ -145,17 +144,17 @@ def map(self, x, inverse=False): | |||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
return x | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
def asaffine(self, indices=None): | ||||||||||||||||||||||||||||
def collapse(self): | ||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||
Combine a succession of linear transforms into one. | ||||||||||||||||||||||||||||
Combine a succession of transforms into one. | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
Example | ||||||||||||||||||||||||||||
------ | ||||||||||||||||||||||||||||
>>> chain = TransformChain(transforms=[ | ||||||||||||||||||||||||||||
... Affine.from_matvec(vec=(2, -10, 3)), | ||||||||||||||||||||||||||||
... Affine.from_matvec(vec=(-2, 10, -3)), | ||||||||||||||||||||||||||||
... ]) | ||||||||||||||||||||||||||||
>>> chain.asaffine() | ||||||||||||||||||||||||||||
>>> chain.collapse() | ||||||||||||||||||||||||||||
array([[1., 0., 0., 0.], | ||||||||||||||||||||||||||||
[0., 1., 0., 0.], | ||||||||||||||||||||||||||||
[0., 0., 1., 0.], | ||||||||||||||||||||||||||||
|
@@ -165,15 +164,15 @@ def asaffine(self, indices=None): | |||||||||||||||||||||||||||
... Affine.from_matvec(vec=(1, 2, 3)), | ||||||||||||||||||||||||||||
... Affine.from_matvec(mat=[[0, 1, 0], [0, 0, 1], [1, 0, 0]]), | ||||||||||||||||||||||||||||
... ]) | ||||||||||||||||||||||||||||
>>> chain.asaffine() | ||||||||||||||||||||||||||||
>>> chain.collapse() | ||||||||||||||||||||||||||||
array([[0., 1., 0., 2.], | ||||||||||||||||||||||||||||
[0., 0., 1., 3.], | ||||||||||||||||||||||||||||
[1., 0., 0., 1.], | ||||||||||||||||||||||||||||
[0., 0., 0., 1.]]) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
>>> np.allclose( | ||||||||||||||||||||||||||||
... chain.map((4, -2, 1)), | ||||||||||||||||||||||||||||
... chain.asaffine().map((4, -2, 1)), | ||||||||||||||||||||||||||||
... chain.collapse().map((4, -2, 1)), | ||||||||||||||||||||||||||||
... ) | ||||||||||||||||||||||||||||
True | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
@@ -183,9 +182,8 @@ def asaffine(self, indices=None): | |||||||||||||||||||||||||||
The indices of the values to extract. | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||
affines = self.transforms if indices is None else np.take(self.transforms, indices) | ||||||||||||||||||||||||||||
retval = affines[0] | ||||||||||||||||||||||||||||
for xfm in affines[1:]: | ||||||||||||||||||||||||||||
retval = self.transforms[-1] | ||||||||||||||||||||||||||||
for xfm in reversed(self.transforms[:-1]): | ||||||||||||||||||||||||||||
retval = xfm @ retval | ||||||||||||||||||||||||||||
return retval | ||||||||||||||||||||||||||||
Comment on lines
+185
to
188
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
But we can also just use a reduce (I've added the imports above if you want to go this way):
Suggested change
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.