From ce8e3bad52b3edce007a45fb29252b67d37f78d2 Mon Sep 17 00:00:00 2001 From: Naim Goksel Karacayli Date: Tue, 13 Jun 2023 08:38:56 -0700 Subject: [PATCH 1/4] test_camera.py redesigned to check for expected values row by row --- specsim/tests/test_camera.py | 39 ++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/specsim/tests/test_camera.py b/specsim/tests/test_camera.py index 2157fcb..ab33c1e 100644 --- a/specsim/tests/test_camera.py +++ b/specsim/tests/test_camera.py @@ -24,23 +24,46 @@ def test_resolution(): # @pytest.mark.xfail def test_downsampling(): + # Expected resolution matrix rows + from scipy.special import erf + def expected_resolution_row(x, R, a): + sqrt2 = np.sqrt(2) + + gamma_p = (x + (a / 2)) / R / sqrt2 + gamma_m = (x - (a / 2)) / R / sqrt2 + + return (erf(gamma_p) - erf(gamma_m)) / 2 + c = specsim.config.load_config('test') i = specsim.instrument.initialize(c) camera = i.cameras[0] - # Use an intermediate dense matrix for downsampling. - # This is the old implementation of get_output_resolution_matrix() - # which uses too much memory. n = len(camera._output_wavelength) m = camera._downsampling - i0 = camera.ccd_slice.start - camera.response_slice.start - R1 = (camera._resolution_matrix[: n * m, i0 : i0 + n * m].toarray() - .reshape(n, m, n, m).sum(axis=3).sum(axis=1) / float(m)) + rms_in = camera._rms_resolution[camera.ccd_slice.start] + bin_width_out = camera.output_pixel_size.value - # Use the new sparse implementation of get_output_resolution_matrix(). + # The new sparse implementation of get_output_resolution_matrix(). R2 = camera.get_output_resolution_matrix() + ndiags = R2.offsets.size + R2 = R2.toarray() + nrows, ncols = R2.shape + + pass_test = True + for jj in range(nrows): + i1 = max(0, jj - ndiags // 2) + i2 = min(ncols - 1, jj + ndiags // 2) + ss = np.s_[i1:i2] + + wave_out = ( + camera.output_wavelength.value[ss] + - camera.output_wavelength.value[jj] + ) + expected_row = expected_resolution_row(wave_out, rms_in, bin_width_out) - assert np.allclose(R1, R2.toarray()) + pass_test &= np.allclose(R2[jj, ss], expected_row, rtol=0.03) + + assert pass_test def test_output_pixel_size(): From f6a457c22665c67db49ede8aefd951ce8da9bbc6 Mon Sep 17 00:00:00 2001 From: Naim Goksel Karacayli Date: Tue, 13 Jun 2023 08:41:55 -0700 Subject: [PATCH 2/4] remove unused variables. pep8 syntax fix --- specsim/tests/test_camera.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/specsim/tests/test_camera.py b/specsim/tests/test_camera.py index ab33c1e..bb19da2 100644 --- a/specsim/tests/test_camera.py +++ b/specsim/tests/test_camera.py @@ -24,8 +24,9 @@ def test_resolution(): # @pytest.mark.xfail def test_downsampling(): - # Expected resolution matrix rows from scipy.special import erf + + # Expected resolution matrix rows def expected_resolution_row(x, R, a): sqrt2 = np.sqrt(2) @@ -38,8 +39,6 @@ def expected_resolution_row(x, R, a): i = specsim.instrument.initialize(c) camera = i.cameras[0] - n = len(camera._output_wavelength) - m = camera._downsampling rms_in = camera._rms_resolution[camera.ccd_slice.start] bin_width_out = camera.output_pixel_size.value From 80430636f62c0c6ae04b3350ccdd7a7af6948529 Mon Sep 17 00:00:00 2001 From: Benjamin Alan Weaver Date: Thu, 23 May 2024 10:40:45 -0700 Subject: [PATCH 3/4] reactivate test --- specsim/tests/test_camera.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/specsim/tests/test_camera.py b/specsim/tests/test_camera.py index bb19da2..681dbcd 100644 --- a/specsim/tests/test_camera.py +++ b/specsim/tests/test_camera.py @@ -19,10 +19,7 @@ def test_resolution(): R = i.cameras[0].get_output_resolution_matrix() assert np.allclose(R.sum(0)[3:-3], 1) -# -# As of 2024-05-14, this test is failing because the values are no longer close. -# -@pytest.mark.xfail + def test_downsampling(): from scipy.special import erf @@ -61,7 +58,7 @@ def expected_resolution_row(x, R, a): expected_row = expected_resolution_row(wave_out, rms_in, bin_width_out) pass_test &= np.allclose(R2[jj, ss], expected_row, rtol=0.03) - + assert pass_test From dc89b5cceaacaa4b78f715dc94a4990621fab498 Mon Sep 17 00:00:00 2001 From: Benjamin Alan Weaver Date: Thu, 23 May 2024 11:06:32 -0700 Subject: [PATCH 4/4] replace 'clobber' keyword with 'overwrite' --- specsim/fiberloss.py | 2 +- specsim/simulator.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/specsim/fiberloss.py b/specsim/fiberloss.py index 19b22cd..29bf2aa 100644 --- a/specsim/fiberloss.py +++ b/specsim/fiberloss.py @@ -271,7 +271,7 @@ def calculate(self, seeing_fwhm, scale, offset, blur_rms, data=self.image.array.copy(), header=header)) if saved_images_file is not None: - hdu_list.writeto(saved_images_file, clobber=True) + hdu_list.writeto(saved_images_file, overwrite=True) return fiberloss diff --git a/specsim/simulator.py b/specsim/simulator.py index 6ebb713..4a51972 100644 --- a/specsim/simulator.py +++ b/specsim/simulator.py @@ -634,7 +634,7 @@ def save(self, filename, clobber=True): hdus.append(astropy.io.fits.BinTableHDU( name=output.meta['name'], data=output.as_array())) # Write the file. - hdus.writeto(filename, clobber=clobber) + hdus.writeto(filename, overwrite=clobber) hdus.close() def plot(self, fiber=0, wavelength_min=None, wavelength_max=None,