-
Notifications
You must be signed in to change notification settings - Fork 76
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
implement unit conversion in specviz2d #3253
base: main
Are you sure you want to change the base?
Conversation
50e8194
to
b2d178d
Compare
22ca012
to
f2920e2
Compare
Codecov ReportAttention: Patch coverage is
❌ Your patch check has failed because the patch coverage (87.50%) is below the target coverage (90.00%). You can increase the patch coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## main #3253 +/- ##
==========================================
+ Coverage 87.51% 87.53% +0.02%
==========================================
Files 128 128
Lines 19901 19957 +56
==========================================
+ Hits 17416 17469 +53
- Misses 2485 2488 +3 ☔ View full report in Codecov by Sentry. |
self.has_angle = False | ||
self.has_sb = False |
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.
jdaviz/conftest.py
Outdated
@@ -260,7 +260,7 @@ def _create_spectrum1d_cube_with_fluxunit(fluxunit=u.Jy, shape=(2, 2, 4), with_u | |||
wcs_dict = {"CTYPE1": "RA---TAN", "CTYPE2": "DEC--TAN", "CTYPE3": "WAVE-LOG", | |||
"CRVAL1": 205, "CRVAL2": 27, "CRVAL3": 4.622e-7, | |||
"CDELT1": -0.0001, "CDELT2": 0.0001, "CDELT3": 8e-11, | |||
"CRPIX1": 0, "CRPIX2": 0, "CRPIX3": 0, | |||
"CRPIX1": 0, "CRPIX2": 0, "CRPIX3": 0, "PIXAR_SR": 10, |
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.
With this update to the test fixture, in test_spectral_extraction_with_correct_sum_units
, the scale factor is no longer the default value of 1, so we need to update the expected values in the test.
jdaviz/configs/cubeviz/plugins/spectral_extraction/tests/test_spectral_extraction.py
Outdated
Show resolved
Hide resolved
if 'value' in row_info: | ||
# format and cast flux value for Table | ||
row_info['value'] = f"{row_info['value']:.5e}" | ||
else: | ||
row_info['value'] = '' |
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.
why is this necessary? Can/should we move it to as_dict
instead?
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.
There was an issue arising where the value column in the table couldn't be ready properly. This was happening when casting value when it was numpy.float32 to float in coords_info.py. I added a check to only cast if the value is not already float.
spec = self.dataset.selected_spectrum | ||
# Needs Attention: | ||
# should conversion occur before or after call to _apply_subset_masks? | ||
if spec.flux.unit.to_string != self.app._get_display_unit('flux'): |
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.
Isn't this a method? Is it cheaper to compare units instead of casting to string?
if spec.flux.unit.to_string != self.app._get_display_unit('flux'): | |
if spec.flux.unit.to_string() != self.app._get_display_unit('flux'): |
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.
Good catch! Is it worth converting the display units to astropy.Unit or keep to string comparison?
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'm not sure, probably fine as is for now
# Needs Attention: | ||
# should conversion occur before or after call to _apply_subset_masks? | ||
if spec.flux.unit.to_string != self.app._get_display_unit('flux'): | ||
equivalencies = u.spectral_density(self.dataset.selected_spectrum.spectral_axis) | ||
spec = self.dataset.selected_spectrum.with_flux_unit(self.app._get_display_unit('flux'), | ||
equivalencies=equivalencies) |
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 this all be moved into the DatasetSelect.selected_spectrum
logic so its reusable and cached (and then the cache would need to be invalidated for a change in flux display units)?
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 added some logic to handle_display_unit
, and with that, this block is removed and selected_spectrum(use_display_units=True) replaces it.
self._dict['spectral_axis'] = wave.value | ||
self._dict['spectral_axis:unit'] = wave.unit.to_string() |
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.
where were these handled before (I don't see them in the else 🤔)?
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.
X-axis conversions were not previously handled in Specviz2d, so they were previously always using the native units and value when mousing over
# Note: spectra2d Wave loaded in meters, but we respect one spectral unit, so the meters in | ||
# converted to Angstrom (the spectra1d spectral unit). |
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.
Can you explain this a little more? Is it that a previous data entry was in angstroms or just that that is the default spectral display unit?
# Note: spectra2d Wave loaded in meters, but we respect one spectral unit, so the meters in | |
# converted to Angstrom (the spectra1d spectral unit). | |
# Note: spectra2d Wave loaded in meters, but we respect one spectral unit, so the meters is | |
# converted to Angstrom (the spectra1d spectral unit). |
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.
So the loaded 1D and 2D spectra have different native spectral units, with one in meters and one in angstroms and app-wide we're respecting one choice for spectral unit.
# TODO [specviz2d, mosviz] x_display_unit is not implemented in glue for image viewer | ||
# used by spectrum-2d-viewer |
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.
is this still relevant?
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.
For the MosvizImageView, I will make sure to update the comment to reflect this!
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.
When testing this with the specviz2d example notebook, the option to change flux unit disappears when you load the data. The screen shots below show an empty specviz instance, with the 'Flux' option appearing under the unit conversion plugin, and then after loading data when that dropdown disappears
What is included in the scope here for surface brightness logic, including coercing flux units to surface brightness, handling square pixel, and toggling the spectral axis? (Are images of 2d spectra even typically or commonly in surface brightness units?) |
|
663e158
to
15c3514
Compare
2745eef
to
9e031c9
Compare
Probably could be a follow-up - but do we want to invert the x-axis on the 2d spectrum to align with the 1d spectrum when switching between wavelength and frequency units? |
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.
Huge effort - just a few small comments and then I think this is good to go in and build on and hopefully didn't miss too many bugs that might slip through 🤞
@@ -18,12 +19,43 @@ def _is_matched_viewer(self, viewer): | |||
return isinstance(viewer, (MosvizProfile2DView, MosvizProfileView)) | |||
|
|||
def _map_limits(self, from_viewer, to_viewer, limits={}): |
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.
any (reasonable) way to increase test coverage in this method?
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'm not sure if codecov will be satisfied, but, I should be able to add to an existing test where the data_collection entry doesn't have the cid
s that were looking for.
A second addition could be to manually recreate the conversion with the pixel_to_world_limits
and vis versa in the test itself and assert whether the results are the same (but it will pretty much be identical to the logic in the method).
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 added a second test for when the data loaded doesn't have the Wave nor Wavelength component (I tried using data_collection[0].remove_component('Wave')
to no avail). I'm open to adding the pixel_to_world_limits test if coverage doesn't increase much from this recent test addition.
for key in components.keys(): | ||
if 'Wavelength' in str(key): | ||
cid = str(key) | ||
break | ||
elif 'Wave' in str(key): | ||
cid = str(key) | ||
break | ||
|
||
if cid is not None: | ||
native_unit = u.Unit(self.viewer.state.data_collection[0].get_component(cid).units) | ||
else: | ||
native_unit = '' |
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.
We could condense this a bit (but existing is fine too if you'd rather keep it)
for key in components.keys(): | |
if 'Wavelength' in str(key): | |
cid = str(key) | |
break | |
elif 'Wave' in str(key): | |
cid = str(key) | |
break | |
if cid is not None: | |
native_unit = u.Unit(self.viewer.state.data_collection[0].get_component(cid).units) | |
else: | |
native_unit = '' | |
for key in components.keys(): | |
strkey = str(key) | |
if 'Wavelength' in strkey or 'Wave' in strkey: | |
native_unit = u.Unit(self.viewer.state.data_collection[0].get_component(strkey).units) | |
break | |
else: | |
# no matches found | |
native_unit = '' |
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 was thinking too that casting each check was overkill, will be included in the next commit!
cube = spectrum1d_cube_custom_fluxunit(fluxunit=u.Jy / sq_angle_unit, | ||
cube = spectrum1d_cube_custom_fluxunit(fluxunit=u.MJy / sq_angle_unit, |
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.
why the change here?
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 just double-checked and will revert this with an update below where the flux_unit is changed (with the scientific notation for the updated values check).
@@ -708,6 +708,7 @@ def import_trace(self, trace): | |||
else: # pragma: no cover | |||
raise NotImplementedError(f"trace of type {trace.__class__.__name__} not supported") | |||
|
|||
# UPDATE HERE |
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.
can this be removed or do we need a follow-up ticket for something?
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.
This was just a note to myself that slipped through the cracks, will be removed shortly
Description
This pull request is to address implementing the Unit Conversion plugin in Specviz2d.
Change log entry
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, maintainershould 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.
trivial
label.