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

Remove dependency on spectral-cube #1006

Merged
merged 2 commits into from
Feb 10, 2022

Conversation

pllim
Copy link
Contributor

@pllim pllim commented Dec 14, 2021

Description

This pull request is to remove dependency on spectral-cube but to do that we also need to refactor the Collapse plugin.

Fixes #922

Blocked by:

  • Decision on Collapse refactoring, e.g., do we even need to collapse to get spatial-spectral data?
  • New specutils release beyond v1.5.0 for Spectrum1D.collapse().
  • Fix CI

Blocks:

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? Milestone is only currently required for PRs related to Imviz MVP.
  • After merge, any internal documentations need updating (e.g., JIRA, Innerspace)?

@pllim pllim added this to the 2.2 milestone Dec 14, 2021
@pllim pllim marked this pull request as draft December 14, 2021 23:38
@github-actions github-actions bot added documentation Explanation of code and concepts testing labels Dec 14, 2021
@pllim pllim added the cubeviz label Dec 14, 2021

# FIXME: Does not work.
# collapsed_spec = spec.collapse(self.selected_func.lower(), axis=self.selected_axis)
# data = Data(coords=collapsed_spec.wcs)
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 am not sure how to use specutils.Spectrum1D.collapse to get spatial-spectral object correctly here. Particularly, the axis order might not be the same as spectral-cube; not sure, as the warnings about flipping axis from specutils is very confusing.

But more importantly, do we really need so many options for axis if people only ever wants to collapse to get spatial-spatial?

Copy link
Collaborator

Choose a reason for hiding this comment

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

In offline discussion we decided that giving "spectral" and "spatial" collapse options rather than numerical axes is the way to go here. None of us could think of a use case for collapsing only one spatial axis. FYI the specutils collapse method should take those two strings as valid axis inputs, so it is hopefully easy to implement.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As discussed in 2021-12-16 tag-up, "spectral" collapse already happens automatically on load, so having it also in the plugin would be redundant.

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 forgot what's the conclusion here. Do we still support "spectral" collapse or just get rid of it?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I would say leave it in 🤷

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 cannot make sense of the previous implementation using spectral-cube, so I removed "axis" option altogether. #yolo

@rosteen
Copy link
Collaborator

rosteen commented Dec 20, 2021

@pllim I'm hoping that astropy/specutils#911 resolves the spectral_slab blocker for this.

@codecov
Copy link

codecov bot commented Feb 1, 2022

Codecov Report

Attention: Patch coverage is 88.23529% with 4 lines in your changes missing coverage. Please review.

Project coverage is 75.42%. Comparing base (45dc2e1) to head (dcbe93d).
Report is 2660 commits behind head on main.

Files Patch % Lines
...daviz/configs/default/plugins/collapse/collapse.py 88.23% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1006      +/-   ##
==========================================
+ Coverage   75.32%   75.42%   +0.09%     
==========================================
  Files          80       80              
  Lines        6059     6042      -17     
==========================================
- Hits         4564     4557       -7     
+ Misses       1495     1485      -10     

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

@@ -111,11 +111,6 @@ try a different OS/browser combo.
Specviz
-------

Collapse Plugin spectral bounds do not match selected 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.

This entry is misleading because Specviz does not load Collapse and there is already a similar entry above for Cubeviz.

@@ -16,28 +14,15 @@
__all__ = ['Collapse']


spaxel = u.def_unit('spaxel', 1 * u.Unit(""))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is not used. Not sure why it is here. It is also defined in at least two other places in this package but removing those is out of scope here.

@@ -108,14 +88,14 @@ def vue_list_subsets(self, event):
temp_dict = {}
# Attempt to filter out spatial subsets
for key, region in temp_subsets.items():
if type(region) == RectanglePixelRegion:
if type(region) == SpectralRegion:
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 this was overlooked in #1046 ?


snackbar_message = SnackbarMessage(
f"Data set '{self._selected_data.label}' collapsed successfully.",
color="success",
sender=self)
self.hub.broadcast(snackbar_message)

if self.selected_axis == 0 and self.selected_viewer != 'None':
# Spatial-spatial image only.
if self.selected_viewer != 'None':
# replace the contents in the selected viewer with the results from this plugin
self.app.add_data_to_viewer(self.viewer_to_id.get(self.selected_viewer),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kecnry , I noticed something weird where while the collapsed image shows in the selected viewer, I am unable to use Layers menu to change the display. I have to deselect it from Data and re-select for Layers to work again. Not sure if this is a known issue or not.

Also, the spectral Subset somehow colors the whole collapsed image in the tint of the Subset color. I can disable it by clicking the eye icon on the Layers dropdown. I think this is a separate known issue already.

Copy link
Member

Choose a reason for hiding this comment

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

I know both of these are known issues. The second I think is expected behavior (so long as the current slice is within the subset), but I agree that we may want to reconsider that default once the slice indicator in #1013 is merged as there will already be visual indication of the slice being in a subset.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

p.s. While it is expected behavior, I wonder if it is the wrong behavior. I cannot see the image properly with the tint all over it, but yes, it is a separate issue for another time.

coll.add_replace_results = False
coll.vue_collapse()

assert len(dc) == 2
assert dc[1].label == 'Collapsed 1 test'
assert len(dc.external_links) == 3
assert len(dc.external_links) == 2
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 results changed here but I don't know what it means. Plugin seems to work though. 🤷

@@ -70,8 +70,6 @@ def test_region_from_subset_profile(jdaviz_app, spectral_cube_wcs):
subsets = jdaviz_app.get_subsets_from_viewer('spectrum-viewer', subset_type='spectral')
reg = subsets.get('Subset 1')

print(reg)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Leftover debugging from #1046 .

@pllim pllim marked this pull request as ready for review February 1, 2022 22:31
@pllim pllim force-pushed the no-spectral-cube-no-cry branch from 519ab0f to 54d04b4 Compare February 3, 2022 21:51
@javerbukh
Copy link
Contributor

While testing this, I noticed that collapsed data is in the wrong orientation. Is this a working as expected or a bug?
Screen Shot 2022-02-08 at 8 26 31 AM

@pllim

This comment was marked as resolved.

@kecnry
Copy link
Member

kecnry commented Feb 9, 2022

While testing this, I noticed that collapsed data is in the wrong orientation.

This rotation does not seem to occur in main (when choosing axis=0).

@pllim pllim force-pushed the no-spectral-cube-no-cry branch from 8c24245 to 96ed69e Compare February 9, 2022 21:15
if data_label not in self.data_collection.labels:
return
self._selected_data = self.data_collection[self.data_collection.labels.index(data_label)]
self._selected_cube = self._selected_data.get_object(cls=Spectrum1D, statistic=None)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: I feel like there is no point to extract the cube multiple times across the plugin. Here, I change it to extracting it only once when data is selected.

warnings.filterwarnings('ignore', message='No observer defined on WCS')
spec = spectral_slab(self._selected_cube, spec_min, spec_max)
# Spatial-spatial image only.
collapsed_spec = spec.collapse(self.selected_func.lower(), axis=-1).T # Quantity
Copy link
Contributor Author

@pllim pllim Feb 9, 2022

Choose a reason for hiding this comment

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

Note: Transpose here fixes the orientation problem that @javerbukh found.

xref astropy/specutils#921


from jdaviz.configs.default.plugins.collapse.collapse import Collapse


@pytest.mark.filterwarnings('ignore')
def test_linking_after_collapse(spectral_cube_wcs):
def test_linking_after_collapse(cubeviz_helper, spectral_cube_wcs):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: Might as well use Cubeviz here to be more realistic.


coll.selected_data_item = 'test'
# This is automatically called in notebook but not in this test.
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 don't know if this is frontend weirdness in unit test or a bug. Seems to work find in the plugin but please double check!

Copy link
Member

Choose a reason for hiding this comment

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

Is the intention here to update the populated spectral bounds? I think that's scheduled to be removed in the future anyways, but I am seeing it not being populated at start in this PR (but populated at start in main)...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct. Because it is my impression that this would automatically be picked up by @observe. So the way I refactored it, setting, self.selected_data_item actually calls self.selected_subset at the end, which I thought would kick off self._on_subset_selected() automatically to populate the bounds. Am I misunderstanding how @observe works?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, except the default values set when creating the traitlets don't trigger those. Your setup should work if you have the traitlet default to an empty string instead of "None" and let it update to "None" (and the bounds populate) when the data is loaded.

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 wasn't aware of that subtlety. I pushed a commit. Please see if that's correct. Thanks!

ignore::DeprecationWarning:glue
ignore::DeprecationWarning:bqplot
ignore::DeprecationWarning:bqplot_image_gl
ignore::DeprecationWarning:bqscales
ignore::DeprecationWarning:traittypes
ignore::DeprecationWarning:voila
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 see a new warning locally, so I need this line so the tests would run on my machine.

and refactor Collapse plugin to work with specutils.

Remove axis option from Collapse plugin.

Change Collapse function default to sum from mean.

Minor code clean-ups. Updated test and doc.
@pllim pllim force-pushed the no-spectral-cube-no-cry branch from 96ed69e to 762bb87 Compare February 9, 2022 21:27
Co-authored-by: Kyle Conroy <kyleconroy@gmail.com>
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.

Looks good! I'm still seeing that bug you mentioned of not being able to set the contrast and bias options for the collapsed data, but I think that's outside the scope of this PR. Nice work!

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.

Everything still seems to work (and am glad to be rid of the confusing axis options)! I think all of the remaining comments existed before and/or can be addressed later.

Comment on lines -35 to +25
selected_func = Unicode('Mean').tag(sync=True)
selected_func = Unicode('Sum').tag(sync=True)
Copy link
Member

Choose a reason for hiding this comment

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

All in favor of this change... but just a note that the default "function" for the spectral-viewer is still "Maximum" (that change is out-of-scope - I would have objected to a change here if this had been Max as well, but it was already different so 🤷‍♂️ )

@pllim pllim merged commit ca6df0a into spacetelescope:main Feb 10, 2022
@pllim pllim deleted the no-spectral-cube-no-cry branch February 10, 2022 16:04
@pllim
Copy link
Contributor Author

pllim commented Feb 10, 2022

Thanks for all the great reviews!

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.

Remove SpectralCube (Was: Build collapse method for Spectrum1D objects)
4 participants