-
Couldn't load subscription status.
- Fork 17
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
base: master
Are you sure you want to change the base?
Changes from all commits
fbc9228
0b13408
a22a6d0
2d3ba2f
eeb4d5d
6b000c8
8e50969
b02077e
1b544cb
c2f9bde
33d91ad
82f58c1
55e6937
a94e577
987eaa8
5f08a36
5da27b1
b562eb3
72cd04f
4e159c2
f1efba1
a7265e4
6f497c0
ac69db0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
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.