-
Notifications
You must be signed in to change notification settings - Fork 259
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
Implement arithmetic and logical operator support? #939
Comments
I think we could probably do something like: class OperableImage:
def _op(self, other, op):
# Check header... need to have axes in the same places
# Check affine
# Check shape... maybe some limited broadcasting
# Check types? Problematic types will be caught by nummpy, but might be cheaper to check before loading data.
dataobj = op(np.asanyarray(self.dataobj), np.asanyarray(other.dataobj))
return self.__class__(dataobj, self.affine, self.header)
__and__ = partial(_op, op=operator.__and__)
... This seems like it could easily fit into The user definitely gives up control of memory management, e.g., caching, so we'll need to be clear about that tradeoff in the docs. We also have something like this with nibabel/nibabel/streamlines/array_sequence.py Lines 58 to 89 in 516434c
nibabel/nibabel/streamlines/array_sequence.py Lines 459 to 506 in 516434c
So perhaps worth thinking about how to turn this into an abstract base class that both objects could derive from. |
Yes - nice idea. |
I will do it. Any idea where should the code live? Inside |
No, |
Sounds good. We can finalise the names etc later. |
This has been suggested before (by me, and probably others), but it would be quite helpful to have nibabel classes (at least in the nifti hierarchy) support arithmetic and logical operations as syntactic sugar for common operations—at least in cases where the semantics can be defined with little or no ambiguity.
E.g.,
img1 & img2
would give the logical conjunction of two images;img1 - img2
the difference; etc. It doesn't seem like users are likely to be confused about what these operators do, and obviously they would only work for operands with identical dimensions/affines.The text was updated successfully, but these errors were encountered: