-
Notifications
You must be signed in to change notification settings - Fork 75
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
Background subtraction in spectral extraction (cubeviz) #2859
Conversation
3ee7a89
to
601ee05
Compare
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2859 +/- ##
=======================================
Coverage 88.68% 88.69%
=======================================
Files 109 109
Lines 16996 17044 +48
=======================================
+ Hits 15073 15117 +44
- Misses 1923 1927 +4 ☔ View full report in Codecov by Sentry. |
jdaviz/configs/cubeviz/plugins/spectral_extraction/spectral_extraction.py
Outdated
Show resolved
Hide resolved
Another thing I noticed:
|
Is it possible that there are negative surface brightnesses in either of those apertures?
There are some known bugs with the preview disappearing that need to be addressed yet - but I'm quite sure that they are not introduced here and so probably don't need to block this PR (but maybe should block the next release). |
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.
Everything looks good to me and is working as expected!
Unrelated to this PR, I was moving and updating subsets to check the auto-updating. If I have a spectral subset, the spectrum created matches. If I update the color of the spatial subset in plot options, the spectrum doesn't update. I think we talked about this with Patrick that this was the intended behavior, so leaving this here just to make sure!
Yes, this was decided to be the desired behavior. |
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 works great when aperture_method == sum
. When I try it with mean, I see an all-zero extraction. Do you see that on your end?
Renormalization feature missing in sum
mode
I know this isn't in the ticket, but I think we should make a follow up ticket for one more feature. If you're using a background region, you almost always want to subtract that background such that the area of the source and background apertures don't influence the background measurement. When aperture_method == 'mean'
this isn't a problem, but when aperture_method == 'sum'
, you essentially want to renormalize by the areas of each region, like:
(bg to subtract from the source's aperture extraction) =
(bg sum) * (source region area) / (background region area)
so that the background gets applied correctly to each source aperture pixel. cc @camipacifici.
else: | ||
raise NotImplementedError(f"{aperture.__class__.__name__} is not supported") | ||
|
||
mask_weights = np.zeros(flux_cube.shape, dtype=np.float32) |
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 we specify the float size? I don't expect it matters much, but if it ever does matter, float promotion/demotion might be hard to trace back to this line in the future.
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 think I just migrated this over from your original implementation. What changes would you suggest 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 think you can just drop the dtype kwarg.
[subset_group] = [ | ||
subset_group for subset_group in self.app.data_collection.subset_groups | ||
if subset_group.label == self.selected] | ||
mask_weights = subset_group.subsets[0].to_mask().astype(np.float32) |
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 we specify the float size? I don't expect it matters much, but if it ever does matter, float promotion/demotion might be hard to trace back to this line in the future.
@bmorris3 naively I thought that the background spectrum was subtracted per spaxel before extraction. If that is not the case, then yes, that normalization is necessary. |
if self.function_selected.lower() == 'sum': | ||
# then scale according to aperture areas across the spectral axis (allowing for | ||
# independent wavelength-dependence btwn the aperture and background) | ||
bg_spec *= self.aperture_area_along_spectral / self.bg_area_along_spectral |
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.
@bmorris3 @camipacifici - normalization by aperture area is currently handled here. Is this different from what you had in mind?
@camipacifici - I think I found and fixed the bug resulting in the unexpected negative spectrum (it was incorrectly still using the aperture mask but weighing by the relative area ratios). Please let me know if it seems to act as expected on your end now.
|
8818a01
to
799b771
Compare
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.
Thanks, this is behaving as expected now. As for this comment, take it or leave it. We're unlikely to see a difference.
Description
This pull request implements support for background subtraction in cubeviz's spectral extraction.
Since this has now gone far beyond "collapsing", this PR also takes this and the 4.0 major version bump as an excuse to rename
collapse_to_spectrum(...)
toextract(...)
and then also adds a newextract_bg_spectrum(...)
(using the same name as in specviz2d in case we ever do want to support extracting the background as well... we could consider just naming this toextract_background(...)
if we don't want to account for that possibility down the road).Screen.Recording.2024-05-07.at.1.49.12.PM.mov
TODO:
*= aperture_area / background_area
?)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.