From 734037b10aa673247d1e35792e1ce3da5b028289 Mon Sep 17 00:00:00 2001 From: gibsongreen Date: Wed, 11 Dec 2024 15:56:14 -0500 Subject: [PATCH] add spec axis/pixar_sr eqvs to model fit, add test coverage --- .../plugins/model_fitting/model_fitting.py | 4 +- .../model_fitting/tests/test_fitting.py | 39 ++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/jdaviz/configs/default/plugins/model_fitting/model_fitting.py b/jdaviz/configs/default/plugins/model_fitting/model_fitting.py index 3d97678d4e..eeb7e36c91 100644 --- a/jdaviz/configs/default/plugins/model_fitting/model_fitting.py +++ b/jdaviz/configs/default/plugins/model_fitting/model_fitting.py @@ -983,7 +983,9 @@ def _fit_model_to_cube(self, add_data): sb_unit = self.app._get_display_unit('sb') if spec.flux.unit != sb_unit: # ensure specutils has access to jdaviz custom unit equivalencies - equivalencies = all_flux_unit_conversion_equivs() + pixar_sr = spec.meta.get('_pixel_scale_factor', None) + equivalencies = all_flux_unit_conversion_equivs(pixar_sr=pixar_sr, + cube_wave=spec.spectral_axis) spec = spec.with_flux_unit(sb_unit, equivalencies=equivalencies) snackbar_message = SnackbarMessage( diff --git a/jdaviz/configs/default/plugins/model_fitting/tests/test_fitting.py b/jdaviz/configs/default/plugins/model_fitting/tests/test_fitting.py index f63577c14a..4a0eb1a289 100644 --- a/jdaviz/configs/default/plugins/model_fitting/tests/test_fitting.py +++ b/jdaviz/configs/default/plugins/model_fitting/tests/test_fitting.py @@ -443,6 +443,29 @@ def test_cube_fit_with_subset_and_nans(cubeviz_helper): assert np.all(result.get_component("flux").data == 1) +def test_fit_with_count_units(cubeviz_helper): + flux = np.random.random((7, 8, 9)) * u.count + spectral_axis = np.linspace(4000, 5000, flux.shape[-1]) * u.AA + + spec = Spectrum1D(flux=flux, spectral_axis=spectral_axis) + cubeviz_helper.load_data(spec, data_label="test") + + mf = cubeviz_helper.plugins["Model Fitting"] + mf.cube_fit = True + mf.create_model_component("Const1D") + + # ensures specutils.Spectrum1D.with_flux_unit has access to Jdaviz custom equivalencies for + # PIX^2 unit + with warnings.catch_warnings(): + warnings.filterwarnings('ignore', message='Model is linear in parameters.*') + mf.calculate_fit() + + assert mf._obj.component_models[0]['parameters'][0]['unit'] == 'ct / pix2' + + model_flux = cubeviz_helper.app.data_collection[-1].get_component('flux') + assert model_flux.units == 'ct / pix2' + + def test_cube_fit_after_unit_change(cubeviz_helper, spectrum1d_cube_fluxunit_jy_per_steradian): cubeviz_helper.load_data(spectrum1d_cube_fluxunit_jy_per_steradian, data_label="test") @@ -480,4 +503,18 @@ def test_cube_fit_after_unit_change(cubeviz_helper, spectrum1d_cube_fluxunit_jy_ assert model_flux.units == 'Jy / sr' assert np.allclose(model_flux.data[:, :, 1], expected_result_slice * 1e6) - # ToDo: Add a test for a unit change that needs an equivalency + # ensure conversions that require the spectral axis/translations are handled by the plugin + uc.spectral_y_type = 'Surface Brightness' + uc.flux_unit = 'erg / (Angstrom s cm2)' + + mf.reestimate_model_parameters() + assert mf._obj.component_models[0]['parameters'][0]['unit'] == 'erg / (Angstrom s sr cm2)' + + # running this ensures specutils.Spectrum1D.with_flux_unit has Jdaviz custom equivalencies + # for spectral axis conversions and scale factor translations + with warnings.catch_warnings(): + warnings.filterwarnings('ignore', message='Model is linear in parameters.*') + mf.calculate_fit() + + model_flux = cubeviz_helper.app.data_collection[-1].get_component('flux') + assert model_flux.units == 'erg / (Angstrom s sr cm2)'