Exemple #1
0
    def __init__(self, ind, roi_data_manager):
        super(SingleTemperatureModel, self).__init__()
        self.ind = ind

        self.data_spectrum = Spectrum([], [])
        self.calibration_spectrum = Spectrum([], [])
        self.corrected_spectrum = Spectrum([], [])

        self._data_img = None
        self._data_img_x_calibration = None
        self._data_img_dimension = None

        self.data_roi_max = 0

        self.roi_data_manager = roi_data_manager

        self._calibration_img = None
        self._calibration_img_x_calibration = None
        self._calibration_img_dimension = None

        self.calibration_parameter = CalibrationParameter()

        self.temperature = np.NaN
        self.temperature_error = np.NaN
        self.fit_spectrum = Spectrum([], [])
Exemple #2
0
    def test_multiply_operator(self):
        x = np.linspace(0, 10, 100)
        spectrum1 = 2 * Spectrum(x, np.sin(x))

        spectrum2 = 2 * Spectrum(x, np.sin(x))

        self.assertTrue(np.array_equal(spectrum2._y, np.sin(x) * 2))
Exemple #3
0
    def test_setting_new_data(self):
        spec = Spectrum()
        x = np.linspace(0, 10)
        y = np.sin(x)
        spec.data = x, y

        new_x, new_y = spec.data
        self.array_almost_equal(new_x, x)
        self.array_almost_equal(new_y, y)
Exemple #4
0
    def test_setting_new_data(self):
        spec = Spectrum()
        x = np.linspace(0, 10)
        y = np.sin(x)
        spec.data = x, y

        new_x, new_y = spec.data
        self.array_almost_equal(new_x, x)
        self.array_almost_equal(new_y, y)
Exemple #5
0
    def test_background_out_of_range_throws_error(self):
        x1 = np.linspace(0, 10)
        x2 = np.linspace(-10, -1)

        spec = Spectrum(x1, x1)
        bkg = Spectrum(x2, x2)

        spec.set_background(bkg)
        with self.assertRaises(BkgNotInRangeError):
            _, test = spec.data
Exemple #6
0
    def test_background_out_of_range_throws_error(self):
        x1 = np.linspace(0, 10)
        x2 = np.linspace(-10, -1)

        spec = Spectrum(x1, x1)
        bkg = Spectrum(x2, x2)

        spec.set_background(bkg)
        with self.assertRaises(BkgNotInRangeError):
            _, test = spec.data
Exemple #7
0
    def _update_corrected_spectrum(self):
        if len(self.data_spectrum) is 0:
            self.corrected_spectrum = Spectrum([], [])
            return

        if len(self.calibration_spectrum) == len(self.data_spectrum):
            x, _ = self.data_spectrum.data
            lamp_spectrum = self.calibration_parameter.get_lamp_spectrum(x)
            self.corrected_spectrum = calculate_real_spectrum(
                self.data_spectrum, self.calibration_spectrum, lamp_spectrum)
        else:
            self.corrected_spectrum = Spectrum([], [])
Exemple #8
0
    def reset_calibration_data(self):
        self._calibration_img = None
        self._calibration_img_x_calibration = None
        self._calibration_img_dimension = None

        self.calibration_spectrum = Spectrum([], [])
        self.corrected_spectrum = Spectrum([], [])
        self.fit_spectrum = Spectrum([], [])

        self.temperature = np.NaN
        self.temperature_error = np.NaN
        self.fit_spectrum = Spectrum([], [])
        self.data_changed.emit()
Exemple #9
0
    def test_using_background_spectrum(self):
        x = np.linspace(-5, 5, 100)
        spec_y = x**2
        bkg_y = x

        spec = Spectrum(x, spec_y)
        bkg = Spectrum(x, bkg_y)

        spec.set_background(bkg)
        new_x, new_y = spec.data

        self.array_almost_equal(new_x, x)
        self.array_almost_equal(new_y, spec_y - bkg_y)
Exemple #10
0
    def test_using_background_spectrum(self):
        x = np.linspace(-5, 5, 100)
        spec_y = x ** 2
        bkg_y = x

        spec = Spectrum(x, spec_y)
        bkg = Spectrum(x, bkg_y)

        spec.set_background(bkg)
        new_x, new_y = spec.data

        self.array_almost_equal(new_x, x)
        self.array_almost_equal(new_y, spec_y - bkg_y)
Exemple #11
0
def fit_black_body_function(spectrum):
    try:
        param, cov = curve_fit(black_body_function,
                               spectrum._x,
                               spectrum._y,
                               p0=[2000, 1e-11])
        T = param[0]
        T_err = np.sqrt(cov[0, 0])

        return T, T_err, Spectrum(
            spectrum._x, black_body_function(spectrum._x, param[0], param[1]))
    except (RuntimeError, TypeError):
        return np.NaN, np.NaN, Spectrum([], [])
Exemple #12
0
    def test_using_background_spectrum_with_different_spacing(self):
        x = np.linspace(-5, 5, 100)
        spec_y = x**2
        x_bkg = np.linspace(-5, 5, 99)
        bkg_y = x_bkg

        spec = Spectrum(x, spec_y)
        bkg = Spectrum(x_bkg, bkg_y)

        spec.set_background(bkg)
        new_x, new_y = spec.data

        self.array_almost_equal(new_x, x)
        self.array_almost_equal(new_y, spec_y - x)
Exemple #13
0
    def test_using_background_spectrum_with_different_spacing(self):
        x = np.linspace(-5, 5, 100)
        spec_y = x ** 2
        x_bkg = np.linspace(-5, 5, 99)
        bkg_y = x_bkg

        spec = Spectrum(x, spec_y)
        bkg = Spectrum(x_bkg, bkg_y)

        spec.set_background(bkg)
        new_x, new_y = spec.data

        self.array_almost_equal(new_x, x)
        self.array_almost_equal(new_y, spec_y - x)
Exemple #14
0
def calculate_real_spectrum(data_spectrum, calibration_spectrum,
                            etalon_spectrum):
    response_y = calibration_spectrum._y / etalon_spectrum._y
    response_y[np.where(response_y == 0)] = np.NaN
    corrected_y = data_spectrum._y / response_y
    corrected_y = corrected_y / np.max(corrected_y) * np.max(data_spectrum._y)
    return Spectrum(data_spectrum._x, corrected_y)
Exemple #15
0
 def fit_data(self):
     if len(self.corrected_spectrum):
         self.temperature, self.temperature_error, self.fit_spectrum = \
             fit_black_body_function(self.corrected_spectrum)
     else:
         self.temperature = np.NaN
         self.temperature_error = np.NaN
         self.fit_spectrum = Spectrum([], [])
Exemple #16
0
    def test_plus_and_minus_operators_with_different_shapes(self):
        x = np.linspace(0, 10, 1000)
        x2 = np.linspace(0, 12, 1300)
        spectrum1 = Spectrum(x, np.sin(x))
        spectrum2 = Spectrum(x2, np.sin(x2))

        spectrum3 = spectrum1 + spectrum2
        self.array_almost_equal(spectrum3._x, spectrum1._x)
        self.array_almost_equal(spectrum3._y, spectrum1._y * 2, 2)

        spectrum3 = spectrum1 + spectrum1
        self.array_almost_equal(spectrum3._y, np.sin(x) * 2, 2)

        spectrum3 = spectrum1 - spectrum2
        self.array_almost_equal(spectrum3._y, np.sin(x) * 0, 2)

        spectrum3 = spectrum1 - spectrum1
        self.array_almost_equal(spectrum3._y, np.sin(x) * 0, 2)
Exemple #17
0
    def test_saving_a_file(self):
        x = np.linspace(-5, 5, 100)
        y = x**2
        spec = Spectrum(x, y)
        spec.save("test.dat")

        spec2 = Spectrum()
        spec2.load("test.dat")

        spec2_x, spec2_y = spec2.data
        self.array_almost_equal(spec2_x, x)
        self.array_almost_equal(spec2_y, y)

        os.remove("test.dat")
Exemple #18
0
    def test_plus_and_minus_operators(self):
        x = np.linspace(0, 10, 100)
        spectrum1 = Spectrum(x, np.sin(x))
        spectrum2 = Spectrum(x, np.sin(x))

        spectrum3 = spectrum1 + spectrum2
        self.assertTrue(np.array_equal(spectrum3._y, np.sin(x) * 2))
        self.assertTrue(np.array_equal(spectrum2._y, np.sin(x) * 1))
        self.assertTrue(np.array_equal(spectrum1._y, np.sin(x) * 1))

        spectrum3 = spectrum1 + spectrum1
        self.assertTrue(np.array_equal(spectrum3._y, np.sin(x) * 2))
        self.assertTrue(np.array_equal(spectrum1._y, np.sin(x) * 1))
        self.assertTrue(np.array_equal(spectrum1._y, np.sin(x) * 1))

        spectrum3 = spectrum2 - spectrum1
        self.assertTrue(np.array_equal(spectrum3._y, np.sin(x) * 0))
        self.assertTrue(np.array_equal(spectrum2._y, np.sin(x) * 1))
        self.assertTrue(np.array_equal(spectrum1._y, np.sin(x) * 1))

        spectrum3 = spectrum1 - spectrum1
        self.assertTrue(np.array_equal(spectrum3._y, np.sin(x) * 0))
        self.assertTrue(np.array_equal(spectrum1._y, np.sin(x) * 1))
        self.assertTrue(np.array_equal(spectrum1._y, np.sin(x) * 1))
Exemple #19
0
    def test_saving_a_file(self):
        x = np.linspace(-5, 5, 100)
        y = x ** 2
        spec = Spectrum(x, y)
        spec.save("test.dat")

        spec2 = Spectrum()
        spec2.load("test.dat")

        spec2_x, spec2_y = spec2.data
        self.array_almost_equal(spec2_x, x)
        self.array_almost_equal(spec2_y, y)

        os.remove("test.dat")
Exemple #20
0
 def get_etalon_spectrum(self):
     return Spectrum(self._etalon_x, self._etalon_y)
Exemple #21
0
 def get_lamp_spectrum(self, wavelength):
     return Spectrum(wavelength, self.get_lamp_y(wavelength))
Exemple #22
0
    def test_using_len(self):
        x = np.linspace(0, 10, 234)
        y = x**2
        spec = Spectrum(x, y)

        self.assertEqual(len(spec), 234)
Exemple #23
0
    def load_setting(self, filename):
        f = h5py.File(filename, 'r')
        ds_group = f['downstream_calibration']
        if 'image' in ds_group:
            self.ds_temperature_model.set_calibration_data(
                ds_group['image'][...],
                ds_group['image'].attrs['x_calibration'][...])
            self.ds_calibration_filename = ds_group['image'].attrs['filename']
            img_dimension = (
                self.ds_temperature_model.calibration_img.shape[1],
                self.ds_temperature_model.calibration_img.shape[0])
            self.roi_data_manager.set_roi(0, img_dimension,
                                          ds_group['roi'][...])
            self.ds_temperature_model._update_all_spectra()
        else:
            self.ds_temperature_model.reset_calibration_data()
            self.ds_calibration_filename = None
            self.ds_roi = [0, 0, 0, 0]

        etalon_data = ds_group['etalon_spectrum'][...]
        self.ds_temperature_model.calibration_parameter.set_etalon_spectrum(
            Spectrum(etalon_data[0, :], etalon_data[1, :]))
        self.ds_temperature_model.calibration_parameter.etalon_file_name = \
            ds_group['etalon_spectrum'].attrs['filename']

        modus = ds_group['modus'][...]
        self.ds_temperature_model.calibration_parameter.set_modus(modus)
        temperature = ds_group['temperature'][...]
        self.ds_temperature_model.calibration_parameter.set_temperature(
            temperature)

        us_group = f['upstream_calibration']
        if 'image' in us_group:
            self.us_temperature_model.set_calibration_data(
                us_group['image'][...],
                us_group['image'].attrs['x_calibration'][...])
            self.us_calibration_filename = us_group['image'].attrs['filename']
            img_dimension = (
                self.us_temperature_model.calibration_img.shape[1],
                self.us_temperature_model.calibration_img.shape[0])
            self.roi_data_manager.set_roi(1, img_dimension,
                                          us_group['roi'][...])
            self.us_temperature_model._update_all_spectra()
        else:
            self.us_temperature_model.reset_calibration_data()
            self.us_calibration_filename = None
            self.us_roi = [0, 0, 0, 0]

        etalon_data = us_group['etalon_spectrum'][...]
        self.us_temperature_model.calibration_parameter.set_etalon_spectrum(
            Spectrum(etalon_data[0, :], etalon_data[1, :]))
        self.us_temperature_model.calibration_parameter.etalon_file_name = \
            us_group['etalon_spectrum'].attrs['filename']

        self.us_temperature_model.calibration_parameter.set_modus(
            us_group['modus'][...])
        self.us_temperature_model.calibration_parameter.set_temperature(
            us_group['temperature'][...])

        self.ds_temperature_model._update_all_spectra()
        self.us_temperature_model._update_all_spectra()

        self.ds_temperature_model.fit_data()
        self.us_temperature_model.fit_data()

        self.us_calculations_changed.emit()
        self.ds_calculations_changed.emit()
        self.data_changed.emit()