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

Threshold masking #1544

Merged
merged 14 commits into from
Aug 8, 2023
Merged

Threshold masking #1544

merged 14 commits into from
Aug 8, 2023

Conversation

bnmajor
Copy link
Collaborator

@bnmajor bnmajor commented Jul 18, 2023

Fixes #1528

Copy link
Collaborator

@psavery psavery left a comment

Choose a reason for hiding this comment

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

Looking great so far! A few things to fix:

  • Toggling visibility in the mask manager dialog for the threshold mask doesn't appear to do anything
  • If you remove the threshold mask from the mask manager and click "Apply" in the threshold dialog, the threshold mask doesn't come back (it looks like you have to exit the program to be able to make a new threshold mask)
  • I think we should consider eliminating the combo boxes and just having it display two spinboxes that are a range of values that will not be thresholded. This could default to -inf to inf, and if you wanted to, for instance, threshold all values below 0 and above 50, it would be 0 to 50. But let's see if this is okay with Saransh first. I think the text is a little confusing anyways - does "Less than" mean that all values less than that value will be thresholded (masked out) or kept? Looks like it means those values should be masked out. But if I try to keep only pixels between a certain range such as 0 and 50, it doesn't look like I can do that for some reason.

hexrd/ui/mask_manager_dialog.py Show resolved Hide resolved
hexrd/ui/mask_manager_dialog.py Show resolved Hide resolved
hexrd/ui/image_mode_widget.py Show resolved Hide resolved
hexrd/ui/threshold_mask_dialog.py Show resolved Hide resolved
hexrd/ui/threshold_mask_dialog.py Outdated Show resolved Hide resolved
hexrd/ui/threshold_mask_dialog.py Outdated Show resolved Hide resolved
hexrd/ui/resources/ui/image_mode_widget.ui Show resolved Hide resolved
hexrd/ui/threshold_mask_dialog.py Show resolved Hide resolved
hexrd/ui/create_raw_mask.py Outdated Show resolved Hide resolved
@bnmajor
Copy link
Collaborator Author

bnmajor commented Aug 2, 2023

If you remove the threshold mask from the mask manager and click "Apply" in the threshold dialog, the threshold mask doesn't come back (it looks like you have to exit the program to be able to make a new threshold mask)

This seems to work for me, but please let me know if I am misunderstanding what you mean:
thresholding

I think we should consider eliminating the combo boxes and just having it display two spinboxes that are a range of values that will not be thresholded. This could default to -inf to inf, and if you wanted to, for instance, threshold all values below 0 and above 50, it would be 0 to 50. But let's see if this is okay with Saransh first.

Sounds good! So something like this maybe?
new_thresholding
My only concern is the default range - if we are defaulting to (-inf, inf) doesn't this kind of impy that everything is being masked out? Maybe that's not really an issue since that's not how it would ever be used (and we can note the behavior in a tooltip like we are now) but I'm just wondering if it may be confusing. @saransh13 Any thoughts on behavior/dialog/defaults? This simplification would eliminate support for masking values equal to a certain value but maybe that is a non-issue.

I think the text is a little confusing anyways - does "Less than" mean that all values less than that value will be thresholded (masked out) or kept? Looks like it means those values should be masked out. But if I try to keep only pixels between a certain range such as 0 and 50, it doesn't look like I can do that for some reason.

Yes, after asking Saransh we should be and-ing masks but this is confusing with the current dialog, I agree. Another point in favor of your simplified suggestion honestly 👍

@psavery
Copy link
Collaborator

psavery commented Aug 2, 2023

If you remove the threshold mask from the mask manager and click "Apply" in the threshold dialog, the threshold mask doesn't come back (it looks like you have to exit the program to be able to make a new threshold mask)

This seems to work for me, but please let me know if I am misunderstanding what you mean:

I'll try it again tonight! (tomorrow for me)

Sounds good! So something like this maybe?

Yes that looks good!

My only concern is the default range - if we are defaulting to (-inf, inf) doesn't this kind of impy that everything is being masked out? Maybe that's not really an issue since that's not how it would ever be used (and we can note the behavior in a tooltip like we are now) but I'm just wondering if it may be confusing. @saransh13 Any thoughts on behavior/dialog/defaults? This simplification would eliminate support for masking values equal to a certain value but maybe that is a non-issue.

So, the range we are defining is the range of pixel values that will not be masked out, not the range of pixel values that will be masked out. I confirmed this with Saransh (and he thinks the range is better too).

So that means if it defaults to (-inf, inf), no pixels get masked out. Then if we change it to be (0, 50), then all pixels that do not have a value between 0 and 50 will be masked out. Does that sound good?

@bnmajor
Copy link
Collaborator Author

bnmajor commented Aug 2, 2023

So, the range we are defining is the range of pixel values that will not be masked out, not the range of pixel values that will be masked out. I confirmed this with Saransh (and he thinks the range is better too).

So that means if it defaults to (-inf, inf), no pixels get masked out. Then if we change it to be (0, 50), then all pixels that do not have a value between 0 and 50 will be masked out. Does that sound good?

Ah, okay. This is the opposite of what I thought he was asking for. In that case this makes sense. Thanks!

@bnmajor
Copy link
Collaborator Author

bnmajor commented Aug 2, 2023

  • Toggling visibility in the mask manager dialog for the threshold mask doesn't appear to do anything

This should be fixed now!

@bnmajor
Copy link
Collaborator Author

bnmajor commented Aug 2, 2023

@psavery @saransh13 I think this is all fixed up now and ready for a final review

@psavery
Copy link
Collaborator

psavery commented Aug 3, 2023

One more issue I see:

If I set the range to be 0 to inf, I get this error:

Traceback (most recent call last):
  File "/Users/patrick/virtualenvs/hexrd/src/hexrdgui/hexrd/ui/threshold_mask_dialog.py", line 68, in accept
    apply_threshold_mask()
  File "/Users/patrick/virtualenvs/hexrd/src/hexrdgui/hexrd/ui/create_raw_mask.py", line 21, in apply_threshold_mask
    mask = create_threshold_mask(img)
  File "/Users/patrick/virtualenvs/hexrd/src/hexrdgui/hexrd/ui/create_raw_mask.py", line 29, in create_threshold_mask
    lt_val, gt_val = HexrdConfig().threshold_values
ValueError: not enough values to unpack (expected 2, got 1)

We should also make sure setting one of the values in the range to inf or -inf works for saving/loading the mask too.

We also need to modify the cartesian and polar generation to take this masking into account, but I will look into that in a separate PR!

@bnmajor
Copy link
Collaborator Author

bnmajor commented Aug 3, 2023

@saransh13 I tested the example that you shared with me but it seems to work for me. Can you make sure your branch is up-to-date and try again?

@psavery Thanks for that! We're now saving the range regardless of value which fixes that bug.

Copy link
Collaborator

@psavery psavery left a comment

Choose a reason for hiding this comment

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

Let's just do the name change ("Range" -> "Unmasked Range") and then this PR looks good to me!

<item row="0" column="0">
<widget class="QLabel" name="comparison_label">
<property name="text">
<string>Range</string>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
<string>Range</string>
<string>Unmasked Range</string>

Can we change the text to "Unmasked Range" for clarity?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Changed

@@ -250,6 +239,8 @@ def write_all_masks(self, h5py_group=None):
for i, (det, mask) in enumerate(data):
parent = d.setdefault(det, {})
parent.setdefault(name, {})[str(i)] = mask
if self.threshold:
d['threshold'] = HexrdConfig()._threshold_data
Copy link
Collaborator

Choose a reason for hiding this comment

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

I tried writing out just the masks (via "Export Masks"), and sometimes, this 'threshold' group in the HDF5 file is just an empty group. This produces an error when we load in the masks.

Maybe there are some situations where HexrdConfig()._threshold_data is invalid?

But I can't actually reproduce this issue now. Let's keep an eye out for it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Okay, sounds good.

Copy link
Collaborator

@psavery psavery left a comment

Choose a reason for hiding this comment

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

LGTM

Signed-off-by: Brianna Major <[email protected]>
Signed-off-by: Brianna Major <[email protected]>
Signed-off-by: Brianna Major <[email protected]>
The visibility of the threshold mask was not being saved. Makes sure that it is.

Signed-off-by: Brianna Major <[email protected]>
Users are now provided two spin boxes that default to (-inf, inf). The range
input will represent the range of values that will *not* be masked.

Signed-off-by: Brianna Major <[email protected]>
Move threshold_mask_changed signal from threshold mask dialog to the
apply_threshold_mask function.

Signed-off-by: Brianna Major <[email protected]>
Signed-off-by: Brianna Major <[email protected]>
Signed-off-by: Brianna Major <[email protected]>
@bnmajor bnmajor merged commit e18cd7f into HEXRD:master Aug 8, 2023
9 checks passed
@bnmajor bnmajor deleted the threshold-masking branch August 8, 2023 16:34
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.

Threshold mask
2 participants