Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Load DS9 regions into Subsets #1463

Merged
merged 9 commits into from
Aug 12, 2022
Merged

Conversation

pllim
Copy link
Contributor

@pllim pllim commented Jul 6, 2022

Description

This pull request is to allow loading DS9 regions into native Glue subset ROIs instead of the "static regions" that do not work with most plugins.

This is a breaking change for the following APIs:

def load_static_regions_from_file(self, region_file, region_format='ds9', prefix='region',

def load_static_regions(self, regions, **kwargs):

def get_interactive_regions(self):

Pre-requisite for:

Close #1141

TODO

  • Add tests for new API.
  • Update notebooks (official and concepts).
  • Update docs.

Post-merge

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 change log needed? If yes, is it added to CHANGES.rst?
  • Is a milestone set?
  • After merge, any internal documentations need updating (e.g., JIRA, Innerspace)? 🐱

@pllim pllim added this to the 2.8 milestone Jul 6, 2022
@pllim pllim force-pushed the imviz-ds9-regions branch 2 times, most recently from 4cdbf45 to 7122375 Compare July 7, 2022 00:53
@codecov
Copy link

codecov bot commented Jul 7, 2022

Codecov Report

Merging #1463 (b9dd2db) into main (13572af) will increase coverage by 0.09%.
The diff coverage is 99.00%.

@@            Coverage Diff             @@
##             main    #1463      +/-   ##
==========================================
+ Coverage   85.52%   85.62%   +0.09%     
==========================================
  Files          94       94              
  Lines        9111     9209      +98     
==========================================
+ Hits         7792     7885      +93     
- Misses       1319     1324       +5     
Impacted Files Coverage Δ
jdaviz/configs/imviz/helper.py 97.11% <98.75%> (-1.18%) ⬇️
jdaviz/core/region_translators.py 98.85% <100.00%> (+0.34%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@pllim pllim force-pushed the imviz-ds9-regions branch 3 times, most recently from bffff54 to c370b4c Compare July 12, 2022 21:57
@pllim pllim force-pushed the imviz-ds9-regions branch from c370b4c to 9365b6d Compare July 15, 2022 16:52
@rosteen rosteen modified the milestones: 2.8, 2.9 Jul 21, 2022
@pllim pllim force-pushed the imviz-ds9-regions branch 2 times, most recently from 124c6cc to 3e0e591 Compare July 21, 2022 21:25
@pllim pllim added 💤 enhancement New feature or request imviz labels Jul 22, 2022
@pllim pllim marked this pull request as ready for review July 22, 2022 03:13
@pllim pllim requested a review from orifox July 22, 2022 03:13
@pllim
Copy link
Contributor Author

pllim commented Jul 22, 2022

This should be ready for review. With this patch, Ori can now load some common regions from DS9 file into Imviz for aperture photometry.

Copy link
Member

@kecnry kecnry left a comment

Choose a reason for hiding this comment

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

Just a few questions/suggestions from code-review. I still need to actually test and see how the static vs interactive subsets behave in the various subset/layer dropdowns before approving.

This looks like it deprecates but doesn't actually break existing API, is that right? So even though we don't officially have a semver policy (yet), there wouldn't be any need to bump the major version? Do we need to file a follow-up ticket/reminder to actually remove the deprecated methods?

docs/imviz/import_data.rst Outdated Show resolved Hide resolved
Comment on lines +235 to +238
WCS-to-pixel translation and mask creation, if needed, is relative
to the image defined by ``refdata_label``. Meanwhile, the rest of
the Subset operations are based on reference image as defined by Glue.
Copy link
Member

Choose a reason for hiding this comment

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

I worry that this distinction is too technical for a user. What other subset operations rely on the reference image instead of refdata_label and what are the consquences?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it is okay to be a little technical in the API docstring.

I cannot tell you what are the implications because I tried to think of the scenarios and my head started to hurt. It is always murky here because Subsets are global but the linked coordinates transformation depends on what is the reference data in a particular viewer (which can be anything because Imviz is so flexible).

Ideas to make this implementation less confusing while maintaining flexibility is welcome. Otherwise, this is good enough until I get bug reports from Ori?

Comment on lines 331 to 333
self.app.session.edit_subset_mode._mode = NewMode
self.default_viewer.apply_roi(state)
self.default_viewer.session.edit_subset_mode.edit_subset = None # No overwrite next iteration # noqa
Copy link
Member

Choose a reason for hiding this comment

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

does this need to be reverted after the API call to whatever the user had set? Or is there a way to load these regions directly into the state without having to fake them as drawn regions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

does this need to be reverted after the API call to whatever the user had set?

I don't think so? If user is using the API to load a bunch of Subset regions, and then user wants to interactively draw one, chances are user wants to draw a new one.

is there a way to load these regions directly into the state without having to fake them as drawn regions?

apply_roi is the only way I know how to do this. Ideas welcome!

Copy link
Member

Choose a reason for hiding this comment

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

My only idea would be if we could just add directly to app.data_collection.subset_groups, but I'm not sure if that's writeable or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Based on the implementation here, I cannot find a setter for the public property. In theory, you can write to almost anything in Python but since that would involve touching the private attribute, maybe we shouldn't go that route?

https://github.com/glue-viz/glue/blob/578369fa09046471c32fd9c0132961474f678248/glue/core/data_collection.py#L390-L395

jdaviz/configs/imviz/helper.py Outdated Show resolved Hide resolved
jdaviz/configs/imviz/helper.py Show resolved Hide resolved
Comment on lines +26 to +28
This is the opposite of what is offered by
``glue_astronomy.translators.regions.AstropyRegionsHandler.to_object``
but does not cover all the same shapes exactly.
Copy link
Member

Choose a reason for hiding this comment

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

should this belong in AstropyRegionsHandler.to_data then?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Theoretically, yes, but if you look at https://github.com/glue-viz/glue-astronomy/blob/main/glue_astronomy/translators/regions.py at the time of posting:

(a) to_data does not exist at all, so to implement one would include all the shapes that I do not care about as well, which is out of scope;
(b) getting anything into glue-astronomy takes too long and since this blocks Cubeviz feature, I want to avoid that overhead;
(c) I would also need to add more things to its to_object because it does not support all the shapes I want here (especially sky regions).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(d) If I could help it, I would say we get rid of glue-astronomy altogether but that is a fiery debate for another day.

Copy link
Member

Choose a reason for hiding this comment

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

ok, then let's leave it here for now (I can now definitely appreciate point b). As for points a and c, I think something in glue-astronomy is better than nothing and we could always raise NotImplementedError for things we don't need yet, but we can migrate this over if/when there is a need.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can always open a follow-up issue to port the code there. I'll add that as post-merge todo.

@kecnry
Copy link
Member

kecnry commented Jul 27, 2022

Only interactive subsets show in spatial subset dropdowns - is this always what we want? For the subset plugin, this probably makes sense since there isn't much of any information we can extract from the masked subsets (although maybe some day we'll want to be able to at least rename them there)... but is there anything preventing one of these masked subsets being used as the aperture in aperture photometry? This is likely the way that static regions behaved before, so I'd be ok with opening a new ticket and considering this out-of-scope, if you'd rather.

Also, is this the expected numbering behavior for the following snippet? I would've expected "Subset 1", "MaskedSubset 2", "MaskedSubset 3" (in other words, are the numbering supposed to be independent or shared)?

Screen Shot 2022-07-27 at 4 00 10 PM

from regions import PixCoord, CirclePixelRegion, PolygonPixelRegion

imviz.load_regions([CirclePixelRegion(center=PixCoord(x=6, y=2), radius=5),
                                 PolygonPixelRegion(vertices=PixCoord(x=[1, 2, 2], y=[1, 1, 2])),
                                 PolygonPixelRegion(vertices=PixCoord(x=[2, 3, 3], y=[2, 2, 3]))])

@pllim
Copy link
Contributor Author

pllim commented Aug 2, 2022

Just got to your review now, Kyle. Thanks for your patience! I will comment in multiple parts as I work through the comments. And I will apply your suggestions in local commit (since I have rebased locally to resolve conflicts before I saw your suggestions) but I will credit you as co-author. Hope that is okay.

This looks like it deprecates but doesn't actually break existing API, is that right?

Yes. As you can see in the diff in test files, changes to the deprecated functionality are minimal. There is a bug fix to prevent bad mask from creating ghost Subsets but I don't see it worth mentioning in change log since the method is deprecated anyway and new API with the fix is still new API.

Do we need to file a follow-up ticket/reminder to actually remove the deprecated methods?

Yes. Over at astropy, we open a follow-up issue to actually remove the deprecated functionality after at least 2 major releases, so I intend to follow similar practice here.

@pllim
Copy link
Contributor Author

pllim commented Aug 2, 2022

Only interactive subsets show in spatial subset dropdowns - is this always what we want?

Not sure what you mean. Your screenshot shows non-interactive MaskedSubset too. If you are talking about the Subset Tool plugin, I think there is a known bug that the non-interactive subsets do not show until after you interactively draw another one after you have loaded them. I thought there was a ticket but I cannot find it now. Maybe @rosteen remembers.

is there anything preventing one of these masked subsets being used as the aperture in aperture photometry?

Yes. photutils only takes certain aperture shapes. Space Invader isn't one of them, but maybe we can convince @larrybradley otherwise?

I would've expected "Subset 1", "MaskedSubset 2", "MaskedSubset 3" (in other words, are the numbering supposed to be independent or shared)?

I purposely made them independent. I don't see why they need to share numbering but I could be convinced otherwise.

@pllim pllim force-pushed the imviz-ds9-regions branch from 78720e5 to 1e39389 Compare August 2, 2022 20:41
@pllim

This comment was marked as resolved.

Copy link
Member

@kecnry kecnry left a comment

Choose a reason for hiding this comment

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

Ok, I think all my questions were answered or addressed, thanks! Anything remaining can be iterated in the future based on user-feedback.

And just to clarify one thread:

Only interactive subsets show in spatial subset dropdowns - is this always what we want?

Not sure what you mean. Your screenshot shows non-interactive MaskedSubset too. If you are talking about the Subset Tool plugin, I think there is a known bug that the non-interactive subsets do not show until after you interactively draw another one after you have loaded them.

I meant for all plugin subset dropdowns (subset tool, model fitting, aperture photometry, etc). But your answer regarding aperture photometry suggests maybe this is desired behavior, at least for now.

Copy link
Contributor

@ojustino ojustino left a comment

Choose a reason for hiding this comment

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

I tested this branch using the example FITS image and DS9 regions file from the ticket. Although all the regions are off the image, I didn't notice anything working differently than intended since Glue will still load those. I edited the regions file so a a few of their coordinates would land on frame and tried again, also with no issues. Both notebooks still work.

The tests added make sense, but should there be others for load_regions_from_file() that cover scenarios where region_format != 'ds9'? I guess those would really be a test that Regions.read() works as we expect.

jdaviz/configs/imviz/helper.py Show resolved Hide resolved
jdaviz/configs/imviz/helper.py Outdated Show resolved Hide resolved
docs/imviz/import_data.rst Outdated Show resolved Hide resolved
jdaviz/configs/imviz/helper.py Show resolved Hide resolved
@pllim
Copy link
Contributor Author

pllim commented Aug 12, 2022

cover scenarios where region_format != 'ds9'?

That is already tested in regions and we have not been asked to support anything other than DS9 files thus far, so I would say no.

@pllim pllim force-pushed the imviz-ds9-regions branch from 1bb1467 to e362521 Compare August 12, 2022 18:36
pllim and others added 8 commits August 12, 2022 16:27
for some shapes.

New API for Imviz regions and deprecate old ones.

Update tests.
and post 2.8 release follow-up
Co-authored-by: Kyle Conroy <kyleconroy@gmail.com>
Co-authored-by: ojustino <ojustino@users.noreply.github.com>
@pllim pllim force-pushed the imviz-ds9-regions branch from e362521 to 5453f94 Compare August 12, 2022 20:28
Copy link
Contributor

@ojustino ojustino left a comment

Choose a reason for hiding this comment

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

Thank you for the changes, @pllim. My last suggestions are just about wording, so I'll approve and leave their resolution to you.

jdaviz/configs/imviz/helper.py Outdated Show resolved Hide resolved
jdaviz/configs/imviz/helper.py Outdated Show resolved Hide resolved
Co-authored-by: ojustino <ojustino@users.noreply.github.com>
@pllim
Copy link
Contributor Author

pllim commented Aug 12, 2022

I opened follow-up issues. They are linked in the original post above. FYI. Thanks again for the thorough reviews!

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants