From 97add955ee2cb11a2bc7b3b88ab53ac9c189d0ad Mon Sep 17 00:00:00 2001 From: Hannu Parviainen Date: Fri, 15 Nov 2024 19:20:58 +0000 Subject: [PATCH 1/7] Fixed Issue #229: increased the absolute and relative tolerances of 'assert_allclose'. --- specreduce/tests/test_tracing.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/specreduce/tests/test_tracing.py b/specreduce/tests/test_tracing.py index 867ef2c..f7ab343 100644 --- a/specreduce/tests/test_tracing.py +++ b/specreduce/tests/test_tracing.py @@ -203,21 +203,21 @@ def test_fit_trace_all_nan_cols(self): 4.6474359, 5.25, 5.8525641, 6.4551282, 7.0576923, 7.6602564] max_trace = FitTrace(img, peak_method='max') - np.testing.assert_allclose(truth, max_trace.trace) + np.testing.assert_allclose(truth, max_trace.trace, atol=0.05, rtol=1e-2) # peak_method = 'gaussian' truth = [1.947455, 2.383634, 2.8198131, 3.2559921, 3.6921712, 4.1283502, 4.5645293, 5.0007083, 5.4368874, 5.8730665, 6.3092455] max_trace = FitTrace(img, peak_method='gaussian') - np.testing.assert_allclose(truth, max_trace.trace) + np.testing.assert_allclose(truth, max_trace.trace, atol=0.05, rtol=1e-2) # peak_method = 'centroid' truth = [2.5318835, 2.782069, 3.0322546, 3.2824402, 3.5326257, 3.7828113, 4.0329969, 4.2831824, 4.533368, 4.7835536, 5.0337391] max_trace = FitTrace(img, peak_method='centroid') - np.testing.assert_allclose(truth, max_trace.trace) + np.testing.assert_allclose(truth, max_trace.trace, atol=0.05, rtol=1e-2) @pytest.mark.filterwarnings("ignore:The fit may be unsuccessful") @pytest.mark.filterwarnings("ignore:Model is linear in parameters") From 8e5baaed44bf61f2c4a543ab400d25887dadbe57 Mon Sep 17 00:00:00 2001 From: Hannu Parviainen Date: Fri, 15 Nov 2024 19:51:08 +0000 Subject: [PATCH 2/7] Changed to use the default relative tolerances in the 'assert_allclose' tests again. They don't matter in practise when we set the absolute tolerances to 0.05 pix. --- specreduce/tests/test_tracing.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/specreduce/tests/test_tracing.py b/specreduce/tests/test_tracing.py index f7ab343..bfd95dd 100644 --- a/specreduce/tests/test_tracing.py +++ b/specreduce/tests/test_tracing.py @@ -203,21 +203,21 @@ def test_fit_trace_all_nan_cols(self): 4.6474359, 5.25, 5.8525641, 6.4551282, 7.0576923, 7.6602564] max_trace = FitTrace(img, peak_method='max') - np.testing.assert_allclose(truth, max_trace.trace, atol=0.05, rtol=1e-2) + np.testing.assert_allclose(truth, max_trace.trace, atol=0.05) # peak_method = 'gaussian' truth = [1.947455, 2.383634, 2.8198131, 3.2559921, 3.6921712, 4.1283502, 4.5645293, 5.0007083, 5.4368874, 5.8730665, 6.3092455] max_trace = FitTrace(img, peak_method='gaussian') - np.testing.assert_allclose(truth, max_trace.trace, atol=0.05, rtol=1e-2) + np.testing.assert_allclose(truth, max_trace.trace, atol=0.05) # peak_method = 'centroid' truth = [2.5318835, 2.782069, 3.0322546, 3.2824402, 3.5326257, 3.7828113, 4.0329969, 4.2831824, 4.533368, 4.7835536, 5.0337391] max_trace = FitTrace(img, peak_method='centroid') - np.testing.assert_allclose(truth, max_trace.trace, atol=0.05, rtol=1e-2) + np.testing.assert_allclose(truth, max_trace.trace, atol=0.05) @pytest.mark.filterwarnings("ignore:The fit may be unsuccessful") @pytest.mark.filterwarnings("ignore:Model is linear in parameters") From 8b0cd2168551ad18191bb389ed483d4ec84ce14f Mon Sep 17 00:00:00 2001 From: Hannu Parviainen Date: Fri, 15 Nov 2024 20:07:04 +0000 Subject: [PATCH 3/7] Changed to use `astropy.modeling.fitting.LMLSQFitter` instead of `astropy.modeling.fitting.LevMarLSQFitter` in `specreduce.tracing.FitTrace`. --- specreduce/tracing.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/specreduce/tracing.py b/specreduce/tracing.py index 7964480..deb6af4 100644 --- a/specreduce/tracing.py +++ b/specreduce/tracing.py @@ -191,7 +191,7 @@ class FitTrace(Trace, _ImageParser): The 1-D polynomial model used to fit the trace to the bins' peak pixels. Spline1D models are fit with Astropy's 'SplineSmoothingFitter', while the other models are fit with the - 'LevMarLSQFitter'. [default: ``models.Polynomial1D(degree=1)``] + 'LMLSQFitter'. [default: ``models.Polynomial1D(degree=1)``] peak_method : string, optional One of ``gaussian``, ``centroid``, or ``max``. ``gaussian``: Fits a gaussian to the window within each bin and @@ -295,7 +295,7 @@ def _fit_trace(self, img): offset_init = models.Const1D(np.ma.median(ztot)) profile = g1d_init + offset_init - fitter = fitting.LevMarLSQFitter() + fitter = fitting.LMLSQFitter() popt_tot = fitter(profile, yy, ztot) # restrict fit to window (if one exists) @@ -396,7 +396,7 @@ def _fit_trace(self, img): # use given model to bin y-values; interpolate over all wavelengths fitter = (fitting.SplineSmoothingFitter() if isinstance(self.trace_model, models.Spline1D) - else fitting.LevMarLSQFitter()) + else fitting.LMLSQFitter()) self.trace_model_fit = fitter(self.trace_model, x_bins, y_bins) trace_x = np.arange(img.shape[self._disp_axis]) From 22927957b8eea3fb1878d756df52fe64028ab99d Mon Sep 17 00:00:00 2001 From: Hannu Parviainen Date: Tue, 26 Nov 2024 17:14:09 +0000 Subject: [PATCH 4/7] Changed FitTrace to use a linear fitter (LinearLSQFitter) when fitting a linear trace model. --- specreduce/tracing.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/specreduce/tracing.py b/specreduce/tracing.py index deb6af4..509bf70 100644 --- a/specreduce/tracing.py +++ b/specreduce/tracing.py @@ -394,9 +394,13 @@ def _fit_trace(self, img): y_bins = y_bins[y_finite] # use given model to bin y-values; interpolate over all wavelengths - fitter = (fitting.SplineSmoothingFitter() - if isinstance(self.trace_model, models.Spline1D) - else fitting.LMLSQFitter()) + if isinstance(self.trace_model, models.Spline1D): + fitter = fitting.SplineSmoothingFitter() + elif self.trace_model.linear: + fitter = fitting.LinearLSQFitter() + else: + fitter = fitting.LMLSQFitter() + self.trace_model_fit = fitter(self.trace_model, x_bins, y_bins) trace_x = np.arange(img.shape[self._disp_axis]) From ed5c3b8622297b57f143433c19a02238703bc725 Mon Sep 17 00:00:00 2001 From: Hannu Parviainen Date: Tue, 26 Nov 2024 17:46:06 +0000 Subject: [PATCH 5/7] Changed to use DogBoxLSQFitter instead of LMLSQFitter in '_fit_trace' when modeling the peak as a gaussian. --- specreduce/tracing.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/specreduce/tracing.py b/specreduce/tracing.py index 509bf70..68f77f1 100644 --- a/specreduce/tracing.py +++ b/specreduce/tracing.py @@ -190,7 +190,8 @@ class FitTrace(Trace, _ImageParser): or `~astropy.modeling.spline.Spline1D`, optional The 1-D polynomial model used to fit the trace to the bins' peak pixels. Spline1D models are fit with Astropy's - 'SplineSmoothingFitter', while the other models are fit with the + 'SplineSmoothingFitter', generic linear models are fit with the + 'LinearLSQFitter', while the other models are fit with the 'LMLSQFitter'. [default: ``models.Polynomial1D(degree=1)``] peak_method : string, optional One of ``gaussian``, ``centroid``, or ``max``. @@ -295,7 +296,7 @@ def _fit_trace(self, img): offset_init = models.Const1D(np.ma.median(ztot)) profile = g1d_init + offset_init - fitter = fitting.LMLSQFitter() + fitter = fitting.DogBoxLSQFitter() popt_tot = fitter(profile, yy, ztot) # restrict fit to window (if one exists) From f001054ccd9a2181b84186040f3c294ca291eb58 Mon Sep 17 00:00:00 2001 From: Hannu Parviainen Date: Tue, 26 Nov 2024 17:50:56 +0000 Subject: [PATCH 6/7] Relaxed the absolute tolerances to 0.1 pixels in test_fit_trace_all_nan_cols because the truth values were calculated with a different fitting method. --- specreduce/tests/test_tracing.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/specreduce/tests/test_tracing.py b/specreduce/tests/test_tracing.py index bfd95dd..c0fb63a 100644 --- a/specreduce/tests/test_tracing.py +++ b/specreduce/tests/test_tracing.py @@ -203,21 +203,21 @@ def test_fit_trace_all_nan_cols(self): 4.6474359, 5.25, 5.8525641, 6.4551282, 7.0576923, 7.6602564] max_trace = FitTrace(img, peak_method='max') - np.testing.assert_allclose(truth, max_trace.trace, atol=0.05) + np.testing.assert_allclose(truth, max_trace.trace, atol=0.1) # peak_method = 'gaussian' truth = [1.947455, 2.383634, 2.8198131, 3.2559921, 3.6921712, 4.1283502, 4.5645293, 5.0007083, 5.4368874, 5.8730665, 6.3092455] max_trace = FitTrace(img, peak_method='gaussian') - np.testing.assert_allclose(truth, max_trace.trace, atol=0.05) + np.testing.assert_allclose(truth, max_trace.trace, atol=0.1) # peak_method = 'centroid' truth = [2.5318835, 2.782069, 3.0322546, 3.2824402, 3.5326257, 3.7828113, 4.0329969, 4.2831824, 4.533368, 4.7835536, 5.0337391] max_trace = FitTrace(img, peak_method='centroid') - np.testing.assert_allclose(truth, max_trace.trace, atol=0.05) + np.testing.assert_allclose(truth, max_trace.trace, atol=0.1) @pytest.mark.filterwarnings("ignore:The fit may be unsuccessful") @pytest.mark.filterwarnings("ignore:Model is linear in parameters") From c053b3324246d4706a7f397a9ffba36211c4497a Mon Sep 17 00:00:00 2001 From: Hannu Parviainen Date: Tue, 26 Nov 2024 21:21:21 +0000 Subject: [PATCH 7/7] Updated the change log. --- CHANGES.rst | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index e7974c8..9c8d4ad 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -10,24 +10,32 @@ API Changes Bug Fixes ^^^^^^^^^ +Other changes +^^^^^^^^^^^^^ + +1.4.2 +------ + +Bug Fixes +^^^^^^^^^ +- Fixed Astropy v7.0 incompatibility bug [#229] in ``tracing.FitTrace``: changed to use + ``astropy.modeling.fitting.DogBoxLSQFitter`` when fitting a Gaussian peak model instead of + ``astropy.modeling.fitting.LevMarLSQFitter`` that may be deprecated in the future. Also + changed to use ``fitting.LMLSQFitter`` instead of ``fitting.LevMarLSQFitter`` when fitting + a generic nonlinear trace model. Other changes +^^^^^^^^^^^^^ +- Changed ``tracing.FitTrace`` to use ``astropy.modeling.fitting.LinearLSQFitter`` + if the trace model is linear. 1.4.1 (2024-06-20) ------------------ -New Features -^^^^^^^^^^^^ - -API Changes -^^^^^^^^^^^ - Bug Fixes ^^^^^^^^^ - Fix bug where Background one sided / two sided was not correctly assigning units to data. [#221] -Other changes -^^^^^^^^^^^^^ 1.4.0 (2024-05-29) ------------------