Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
one still fails
  • Loading branch information
segasai committed Dec 4, 2024
1 parent 2deb96b commit af25b32
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
8 changes: 4 additions & 4 deletions py/desispec/fluxcalibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -1516,8 +1516,8 @@ def _calibrate_resolution_matrix(res_mat, calib, good_calib):
# now we align the diagonals to the banded storage
# of the resolution matrix
width = res_mat.shape[0]
M1 = [np.roll(icalib, _) for _ in np.arange(-width//2,(width//2)+1)[::-1]]
M2 = [1./rcalib for _ in np.arange(width)]
M1 = [np.roll(icalib, _) for _ in np.arange(-(width//2),(width//2)+1)[::-1]]
M2 = [rcalib for _ in np.arange(width)]
res_mat_out = res_mat * M1 * M2
# This has been tested with X0 and X1 being indentical in this calculation
# M1 = [np.roll(D1, _) for _ in np.arange(-5, 6)[::-1]]
Expand All @@ -1527,7 +1527,7 @@ def _calibrate_resolution_matrix(res_mat, calib, good_calib):
return res_mat_out


def _interpolate_bad_regions(spec, mask):
def _interpolate_bad_regions(spec,badmask):
"""
Interpolate bad regions
Arguments:
Expand All @@ -1537,7 +1537,7 @@ def _interpolate_bad_regions(spec, mask):
Returns:
ret Interpolated ndarray
"""
xind = np.nonzero(mask)[0]
xind = np.nonzero(badmask)[0]
if len(xind) == 0:
return spec
elif len(xind) == len(spec):
Expand Down
37 changes: 35 additions & 2 deletions py/desispec/test/test_flux_calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def test_apply_fluxcalibration(self):
ivar = np.random.uniform(0.9, 1.1, size=flux.shape)
origframe = Frame(wave, flux.copy(), ivar.copy(), spectrograph=0)

# efine fluxcalib object
# define fluxcalib object
calib = np.random.uniform(.5, 1.5, size=origframe.flux.shape)
mask = np.zeros(origframe.flux.shape, dtype=np.uint32)

Expand Down Expand Up @@ -179,7 +179,7 @@ def test_apply_fluxcalibration(self):
frame=copy.deepcopy(origframe)
apply_flux_calibration(frame,fc)
self.assertTrue(np.all(frame.ivar[0,0:10]==0.0))

# should also work even the calib =0 ??
#fcivar=np.ones_like(origframe.flux)
#calib=np.ones_like(origframe.flux)
Expand All @@ -199,6 +199,39 @@ def test_apply_fluxcalibration(self):
with self.assertRaises(SystemExit): #should be ValueError instead?
apply_flux_calibration(frame,fc)

# if the calibration is constant resolution should not change
resol = np.random.uniform(size=(flux.shape[0], 11, flux.shape[1]))
flux = np.random.uniform(0.9, 1.0, size=(nspec, nwave))
ivar = np.random.uniform(0.9, 1.0, size=(nspec, nwave))
frame = Frame(wave, flux.copy(), ivar.copy(), spectrograph=0,
resolution_data=resol.copy())
calib = flux * 0 + 10
mask = np.zeros(origframe.flux.shape, dtype=np.uint32)
fc = FluxCalib(origframe.wave, calib, ivar, mask)
apply_flux_calibration(frame, fc)
self.assertTrue(np.allclose(frame.resolution_data, resol))

# preservation of resolution equation
resol = np.random.uniform(size=(flux.shape[0], 11, flux.shape[1]))
# input spectra
flux0 = np.random.uniform(0.9, 1.0, size=(nspec, nwave))
flux0[:, :6] = 0
flux0[:, -7:] = 0
# convolving with the resolution matrix
flux_conv = np.array([desispec.resolution.Resolution(resol[i])@flux0[i] for i in range(nspec)])
flux0_calib = flux0 / calib
frame = Frame(wave, flux_conv.copy(), ivar.copy(), spectrograph=0,
resolution_data=resol.copy())
calib = np.random.uniform(.5, 1.5, size=origframe.flux.shape)
mask = np.zeros(origframe.flux.shape, dtype=np.uint32)
fc = FluxCalib(origframe.wave, calib, ivar, mask)
apply_flux_calibration(frame, fc)
flux1_conv = np.array([desispec.resolution.Resolution(
frame.resolution_data[i])@flux0_calib[i] for i in range(nspec)])
print (flux1_conv[:7,5:10], frame.flux[:7,5:10])
self.assertTrue(np.allclose(flux1_conv, frame.flux))


def test_isStdStar(self):
"""test isStdStar works for cmx, main, and sv1 fibermaps"""
from desispec.fluxcalibration import isStdStar
Expand Down

0 comments on commit af25b32

Please sign in to comment.