def _updateCalibratedData(self, bckg=None, coef=None): """ Try to update the data with new calibration. The two parameters are the same as compensate_spectrum_efficiency(). The input data comes from .raw and the calibrated data is saved in ._calibrated bckg (DataArray or None) coef (DataArray or None) raise ValueError: if the data and calibration data are not valid or compatible. In that case the current calibrated data is unchanged. """ data = self.raw[0] if data is None: self._calibrated = None return if bckg is None and coef is None: # make sure to not display any other error self._calibrated = data return if not (set(data.metadata.keys()) & {model.MD_WL_LIST, model.MD_WL_POLYNOMIAL}): raise ValueError("Spectrum data contains no wavelength information") # will raise an exception if incompatible calibrated = calibration.compensate_spectrum_efficiency(data, bckg, coef) self._calibrated = calibrated
def _updateCalibratedData(self, bckg=None, coef=None): """ Try to update the data with new calibration. The two parameters are the same as compensate_spectrum_efficiency(). The input data comes from .raw and the calibrated data is saved in ._calibrated bckg (DataArray or None) coef (DataArray or None) raise ValueError: if the data and calibration data are not valid or compatible. In that case the current calibrated data is unchanged. """ data = self.raw[0] if data is None: self._calibrated = None return if not (set(data.metadata.keys()) & {model.MD_WL_LIST, model.MD_WL_POLYNOMIAL}): raise ValueError( "Spectrum data contains no wavelength information") # will raise an exception if incompatible calibrated = calibration.compensate_spectrum_efficiency(data, bckg=bckg, coef=coef) self._calibrated = calibrated
def test_compensate(self): """Test applying efficiency compensation""" # Spectrum data = numpy.ones((251, 1, 1, 200, 300), dtype="uint16") wld = 433e-9 + numpy.array(range(data.shape[0])) * 0.1e-9 spec = model.DataArray(data, metadata={model.MD_WL_LIST: wld}) # Compensation data dcalib = numpy.array([1, 1.3, 2, 3.5, 4, 5, 0.1, 6, 9.1], dtype=numpy.float) dcalib.shape = (dcalib.shape[0], 1, 1, 1, 1) wl_calib = 400e-9 + numpy.array(range(dcalib.shape[0])) * 10e-9 calib = model.DataArray(dcalib, metadata={model.MD_WL_LIST: wl_calib}) compensated = calibration.compensate_spectrum_efficiency(spec, calib) self.assertEqual(spec.shape, compensated.shape) numpy.testing.assert_equal(spec.metadata[model.MD_WL_LIST], compensated.metadata[model.MD_WL_LIST]) for i in range(dcalib.shape[0] - 1): ca, cb = calib[i], calib[i + 1] wla, wlb = wl_calib[i], wl_calib[i + 1] # All the values between the 2 wavelengths should be compensated # between the 2 factors for vo, vc, wl in zip(spec[..., 3, 3], compensated[..., 3, 3], wld): if wla <= wl <= wlb: expa, expb = ca * vo, cb * vo minc, maxc = min(expa, expb), max(expa, expb) self.assertTrue(minc <= vc <= maxc)
def test_compensate_out(self): """Test applying efficiency compensation on an edge of calibration""" # Spectrum data = numpy.ones((251, 1, 1, 200, 300), dtype="uint16") wld = 333e-9 + numpy.array(range(data.shape[0])) * 0.1e-9 spec = model.DataArray(data, metadata={model.MD_WL_LIST: wld}) # Only from 400 nm => need to use the border (=1) for everything below dcalib = numpy.array([1, 1, 2, 3, 4, 5, 1, 6, 9], dtype=numpy.float) dcalib.shape = (dcalib.shape[0], 1, 1, 1, 1) wl_calib = 400e-9 + numpy.array(range(dcalib.shape[0])) * 10e-9 calib = model.DataArray(dcalib, metadata={model.MD_WL_LIST: wl_calib}) compensated = calibration.compensate_spectrum_efficiency(spec, coef=calib) self.assertEqual(spec.shape, compensated.shape) numpy.testing.assert_equal(spec.metadata[model.MD_WL_LIST], compensated.metadata[model.MD_WL_LIST]) # Value before the first calibration wavelength must be estimated for vo, vc, wl in zip(spec[..., 3, 3], compensated[..., 3, 3], wld): if wl <= wl_calib[0]: self.assertEqual(vo * dcalib[0], vc)
def test_compensate(self): """Test applying efficiency compensation""" # Spectrum data = numpy.ones((251, 1, 1, 200, 300), dtype="uint16") + 1 wld = 433e-9 + numpy.array(range(data.shape[0])) * 0.1e-9 spec = model.DataArray(data, metadata={model.MD_WL_LIST: wld}) # Background data dbckg = numpy.ones(data.shape, dtype=numpy.uint16) wl_bckg = 400e-9 + numpy.array(range(dbckg.shape[0])) * 10e-9 obckg = model.DataArray(dbckg, metadata={model.MD_WL_LIST: wl_bckg}) bckg = calibration.get_spectrum_data([obckg]) # Compensation data dcalib = numpy.array([1, 1.3, 2, 3.5, 4, 5, 0.1, 6, 9.1], dtype=numpy.float) dcalib.shape = (dcalib.shape[0], 1, 1, 1, 1) wl_calib = 400e-9 + numpy.array(range(dcalib.shape[0])) * 10e-9 calib = model.DataArray(dcalib, metadata={model.MD_WL_LIST: wl_calib}) compensated = calibration.compensate_spectrum_efficiency(spec, bckg, calib) self.assertEqual(spec.shape, compensated.shape) numpy.testing.assert_equal(spec.metadata[model.MD_WL_LIST], compensated.metadata[model.MD_WL_LIST]) for i in range(dcalib.shape[0] - 1): ca, cb = calib[i], calib[i + 1] wla, wlb = wl_calib[i], wl_calib[i + 1] # All the values between the 2 wavelengths should be compensated # between the 2 factors for vo, vb, vc, wl in zip(spec[..., 3, 3], bckg[..., 0, 0], compensated[..., 3, 3], wld): if wla <= wl <= wlb: expa, expb = (vo - vb) * ca, (vo - vb) * cb minc, maxc = min(expa, expb), max(expa, expb) self.assertTrue(minc <= vc <= maxc)