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
Add __getitem__
method to ROIs
#2288
Add __getitem__
method to ROIs
#2288
Conversation
This is very nice. A few comments/questions:
How far do you want to go in this PR? |
Just that it is not straightforwardly useful to get the
Thanks, done!
Done.
Yes, let's do that (where reasonable) in 2.0. There is also an inconsistency across HyperSpy in the order of |
After thinking about it I have added the feature for all ROIs, and I have taken a simpler, faster approach for the implementation. |
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.
Looks good to me. I added a few other comments.
@@ -463,7 +462,7 @@ def set_signal_range(self, x1=None, x2=None): | |||
|
|||
""" | |||
try: | |||
x1, x2 = signal_range_from_roi(x1) | |||
x1, x2 = x1 | |||
except TypeError: |
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.
Should the error below become a ValueError
in case the length of the tuple doesn't match?
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.
I am not sure about it: I think that if the length of the tuple doesn't match we want to raise the exception because the only valid inputs are a matching ROI or a number.
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.
Yes, I agree. Actually, it looks to me that it is not possible to get a TypeError
with/without this PR? If so, shall we removed it?
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.
I think that it is fine as is: you can get a TypeError
by passing a number as argument.
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.
Yes, indeed!
@@ -494,7 +493,7 @@ def remove_signal_range(self, x1=None, x2=None): | |||
|
|||
""" | |||
try: | |||
x1, x2 = signal_range_from_roi(x1) | |||
x1, x2 = x1 |
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.
Same as above.
@@ -529,7 +528,7 @@ def add_signal_range(self, x1=None, x2=None): | |||
|
|||
""" | |||
try: | |||
x1, x2 = signal_range_from_roi(x1) | |||
x1, x2 = x1 |
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.
Same as above.
hyperspy/roi.py
Outdated
@@ -979,6 +1038,7 @@ def __init__(self, x1, y1, x2, y2, linewidth=0): | |||
super(Line2DROI, self).__init__() | |||
self.x1, self.y1, self.x2, self.y2 = x1, y1, x2, y2 | |||
self.linewidth = linewidth | |||
self._tuple = (self.x1, self.y1, self.x2, self.y2) |
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.
linewidth
?
Thanks for the review. It should be ready for another iteration. |
__getitem__
method to some ROIs__getitem__
method to ROIs
hyperspy/roi.py
Outdated
@@ -980,6 +1069,12 @@ def __init__(self, x1, y1, x2, y2, linewidth=0): | |||
self.x1, self.y1, self.x2, self.y2 = x1, y1, x2, y2 | |||
self.linewidth = linewidth | |||
|
|||
def __getitem__(self, *args, **kwargs): | |||
_tuple = (self.x1, self.y1, self.x2, self.y2, self.linewidth) | |||
_tuple = (self.x1, self.y1, self.x2, self.y2) |
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.
I guess this line should be removed?
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.
Yes, thanks. It turns out that I forgot to add tests for Circle and Line2D. After adding them in the last commit I found 1 more bug of course...
Description of the change
Add
__getitem__
method to the following ROIs:Point1DROI
Point2DROI
SpanROI
RectangularROI
In this way, they can be used in place of tuples.
Progress of the PR
Minimal example of the bug fix or the new feature