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

BUG: Fix get_subsets when XOR is involved #3124

Merged
merged 5 commits into from
Aug 8, 2024

Conversation

pllim
Copy link
Contributor

@pllim pllim commented Jul 30, 2024

Description

This pull request is to fix bug found in #2941 (comment)

Change log entry

  • Is a change log needed? If yes, is it added to CHANGES.rst? If you want to avoid merge conflicts,
    list the proposed change log here for review and add to CHANGES.rst before merge. If no, maintainer
    should add a no-changelog-entry-needed label.

Checklist for package maintainer(s)

This checklist is meant to remind the package maintainer(s) who will review this pull request of some common things to look for. This list is not exhaustive.

  • Are two approvals required? Branch protection rule does not check for the second approval. If a second approval is not necessary, please apply the trivial label.
  • Do the proposed changes actually accomplish desired goals? Also manually run the affected example notebooks, if necessary.
  • Do the proposed changes follow the STScI Style Guides?
  • Are tests added/updated as required? If so, do they follow the STScI Style Guides?
  • Are docs added/updated as required? If so, do they follow the STScI Style Guides?
  • Did the CI pass? If not, are the failures related?
  • Is a milestone set? Set this to bugfix milestone if this is a bug fix and needs to be released ASAP; otherwise, set this to the next major release milestone. Bugfix milestone also needs an accompanying backport label.
  • After merge, any internal documentations need updating (e.g., JIRA, Innerspace)? 🐱

@pllim pllim added bug Something isn't working 💤backport-v3.10.x on-merge: backport to v3.10.x labels Jul 30, 2024
@pllim pllim added this to the 3.10.4 milestone Jul 30, 2024
@pllim
Copy link
Contributor Author

pllim commented Jul 30, 2024

Was hoping we could simply hook into glue-astronomy region translator but it didn't work. Maybe the translator is incomplete. I think we wrote a lot of the stuff focused on 2D image region, not spectral region.

from glue.config import subset_state_translator

dc = specviz_helper.app.data_collection
subsets = dc.subset_groups
subset = subsets[0]
handler = subset_state_translator.get_handler_for('astropy-regions')
handler.to_object(subset.subsets[0])  # traceback

I feel like the proper solution is to implement a new spectral region translator in glue-astronomy but that is way more "story points" than we have available.


specviz_helper.app.session.edit_subset_mode.mode = XorMode
viewer.apply_roi(XRangeROI(6700, 7700))
viewer.apply_roi(XRangeROI(6100, 7600))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The bug gets triggered when XOR is not exactly the same limits as the other regions (and that is usually the case in practice).

merged_regions.append(last_merged)
else:
merged_regions.append(reg_as_tup[index])
merged_regions = self.get_subsets(subset_name, spectral_only=True)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure how controversial this is, but in order to fix the XOR bug, I basically have to move the "merge overlapping regions" calls into get_subsets when simplify_spectral=True. Otherwise, if you apply XOR and then another operator like OR, you get overlaps even if you ask for simplify_spectral=True, because to fix the bug, I have to chop up regions in the XOR operations or it is going to throw out some regions that do not overlap with the XOR limits. Hope this is not too confusing.

return True
return False

@staticmethod
def _merge_overlapping_spectral_regions_worker(spectral_region):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same logic as before but I find operating on SpectralRegion directly less complicated than forcing tuple of floats. Also I need SpectralRegion directly to reuse it somewhere else.

@@ -1304,14 +1309,33 @@ def is_there_overlap_spectral_subset(self, subset_name):
Returns True if the spectral subset with subset_name has overlapping
subregions.
"""
spectral_region = self.get_subsets(subset_name, spectral_only=True)
if not spectral_region or len(spectral_region) < 2:
spectral_region = self.get_subsets(subset_name, simplify_spectral=False, spectral_only=True)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because I have to move the "merge overlapping regions" calls into get_subsets when simplify_spectral=True, I have to force simplify_spectral=False here so this check is still correct.

@@ -1135,9 +1140,7 @@ def get_sub_regions(self, subset_state, simplify_spectral=True,
else:
if isinstance(two, list):
two[0]['glue_state'] = "AndNotState"
# Return two first so that we preserve the chronology of how
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure what this comment is about because when you add two SpectralRegion, it automatically reorders by ascending wavelength, so it is never chronological to begin with. That is why this comment is removed.

@pllim pllim removed the 💤backport-v3.10.x on-merge: backport to v3.10.x label Jul 31, 2024
@pllim pllim modified the milestones: 3.10.4, 4.0 Jul 31, 2024
@pllim pllim added the specviz label Jul 31, 2024
@pllim
Copy link
Contributor Author

pllim commented Jul 31, 2024

If this is too controversial, other options:

  1. Drop XOR from possible options, as Jesse suggested.
  2. Implement proper translation over at glue-astronomy.

@pllim pllim marked this pull request as ready for review July 31, 2024 19:40
Copy link

codecov bot commented Jul 31, 2024

Codecov Report

Attention: Patch coverage is 96.55172% with 2 lines in your changes missing coverage. Please review.

Project coverage is 88.78%. Comparing base (31ac58f) to head (6cb9ef3).
Report is 126 commits behind head on main.

Files with missing lines Patch % Lines
jdaviz/app.py 96.49% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3124      +/-   ##
==========================================
- Coverage   88.81%   88.78%   -0.04%     
==========================================
  Files         112      112              
  Lines       17427    17446      +19     
==========================================
+ Hits        15478    15489      +11     
- Misses       1949     1957       +8     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@javerbukh
Copy link
Contributor

I was still able to find a bug by doing the following order of spectral subsets (you can use PR #3082 to import the entire dictionary). I would expect a SpectralRegions object with 6 elements but app.get_subsets(simplify_spectral=True) only returns 2. The following is the return from app.get_subsets(simplify_spectral=False):

{'Subset 1': [{'name': 'RangeSubsetState',
   'glue_state': 'AndState',
   'region': Spectral Region, 1 sub-regions:
     (5.667263706861359 um, 6.768955332154135 um) ,
   'sky_region': None,
   'subset_state': <glue.core.subset.RangeSubsetState at 0x245fb7719f0>},
  {'name': 'RangeSubsetState',
   'glue_state': 'AndNotState',
   'region': Spectral Region, 1 sub-regions:
     (6.01443541526278 um, 6.495846921545017 um) ,
   'sky_region': None,
   'subset_state': <glue.core.subset.RangeSubsetState at 0x245fb7737f0>},
  {'name': 'RangeSubsetState',
   'glue_state': 'XorState',
   'region': Spectral Region, 1 sub-regions:
     (5.491363409920771 um, 6.912452830362193 um) ,
   'sky_region': None,
   'subset_state': <glue.core.subset.RangeSubsetState at 0x245fb61d420>},
  {'name': 'RangeSubsetState',
   'glue_state': 'OrState',
   'region': Spectral Region, 1 sub-regions:
     (7.01428986482661 um, 7.24110871431554 um) ,
   'sky_region': None,
   'subset_state': <glue.core.subset.RangeSubsetState at 0x245fb61e680>},
  {'name': 'RangeSubsetState',
   'glue_state': 'OrState',
   'region': Spectral Region, 1 sub-regions:
     (5.051612596937036 um, 5.204368130975595 um) ,
   'sky_region': None,
   'subset_state': <glue.core.subset.RangeSubsetState at 0x245fb61d060>},
  {'name': 'RangeSubsetState',
   'glue_state': 'XorState',
   'region': Spectral Region, 1 sub-regions:
     (6.06535393249499 um, 6.968000303706421 um) ,
   'sky_region': None,
   'subset_state': <glue.core.subset.RangeSubsetState at 0x245fde88610>}]}

image

@pllim
Copy link
Contributor Author

pllim commented Aug 2, 2024

@javerbukh , good catch. I guess I misunderstood how SpectralRegion.invert() behaves in a more complicated situation. I added more tests, including one that is similar to what you pointed out above. I might still have missed something, so please re-review. Thanks!

Copy link
Contributor

@javerbukh javerbukh left a comment

Choose a reason for hiding this comment

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

I'm so glad there is now at least one other person that understands this code 😄 Great job figuring out this bug, I could not replicate it with very complex composite subsets. Hopefully the second reviewer tries the same with similar results.

@pllim
Copy link
Contributor Author

pllim commented Aug 5, 2024

"Understand" is a strong word...

Copy link
Contributor

@bmorris3 bmorris3 left a comment

Choose a reason for hiding this comment

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

This looks good to me.

jdaviz/app.py Outdated Show resolved Hide resolved
@pllim pllim merged commit 7826a99 into spacetelescope:main Aug 8, 2024
16 checks passed
@pllim pllim deleted the xor-xor-binks branch August 8, 2024 21:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants