Skip to content

Commit

Permalink
Collapse 3D y before calculating initial model guess for gaussian mod…
Browse files Browse the repository at this point in the history
…el (spacetelescope#3295)

* Collapse 3D y before calculating initial model guess for gaussian model

* Changelog

* Add test

* Add test that fails on main
  • Loading branch information
rosteen authored Nov 15, 2024
1 parent f715ffe commit 5d04203
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ Cubeviz

- Fixed "spectrum at spaxel" tool so it no longer resets spectral axis zoom. [#3249]

- Fixed initializing a Gaussian1D model component when ``Cube Fit`` is toggled on. [#3295]

Imviz
^^^^^

Expand Down
4 changes: 4 additions & 0 deletions jdaviz/configs/default/plugins/model_fitting/initializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ def initialize(self, instance, x, y):
The initialized model.
"""

if y.ndim == 3:
# For cube fitting, need to collapse before these calculations
y = np.nanmean(y, axis=(0, 1))

# X centroid estimates the position
centroid = np.sum(x * y) / np.sum(y)

Expand Down
15 changes: 13 additions & 2 deletions jdaviz/configs/default/plugins/model_fitting/tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,20 @@ def test_register_cube_model(cubeviz_helper, spectrum1d_cube):
assert test_label in cubeviz_helper.app.data_collection


def test_initialize_gaussian_with_cube(cubeviz_helper, spectrum1d_cube_larger):
with warnings.catch_warnings():
warnings.simplefilter('ignore')
cubeviz_helper.load_data(spectrum1d_cube_larger)
modelfit_plugin = cubeviz_helper.plugins['Model Fitting']

modelfit_plugin.cube_fit = True
# Check that we can initialize a Gaussian1D with cube fit toggled on
modelfit_plugin.create_model_component('Gaussian1D', 'G')


def test_fit_cube_no_wcs(cubeviz_helper):
# This is like when user do something to a cube outside of Jdaviz
# and then load it back into a new instance of Cubeviz for further analysis.
# This is like when user does something to a cube outside of Jdaviz
# and then loads it back into a new instance of Cubeviz for further analysis.
sp = Spectrum1D(flux=np.ones((7, 8, 9)) * u.nJy) # nx, ny, nz
cubeviz_helper.load_data(sp, data_label="test_cube")
mf = cubeviz_helper.plugins['Model Fitting']
Expand Down

0 comments on commit 5d04203

Please sign in to comment.