Ejemplo n.º 1
0
    def test_multiply_with_scalar_operator(self):
        x = np.linspace(0, 10, 100)
        spectrum1 = 2 * Pattern(x, np.sin(x))

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

        self.assertTrue(np.array_equal(spectrum2.y, np.sin(x) * 2))
Ejemplo n.º 2
0
    def test_background_out_of_range_throws_error(self):
        x1 = np.linspace(0, 10)
        x2 = np.linspace(-10, -1)

        spec = Pattern(x1, x1)
        background_spectrum = Pattern(x2, x2)

        with self.assertRaises(BkgNotInRangeError):
            spec.background_pattern = background_spectrum
Ejemplo n.º 3
0
    def test_using_background_spectrum(self):
        x = np.linspace(-5, 5, 100)
        spec_y = x**2
        bkg_y = x

        spec = Pattern(x, spec_y)
        background_spectrum = Pattern(x, bkg_y)

        spec.background_pattern = background_spectrum
        new_x, new_y = spec.data

        self.array_almost_equal(new_x, x)
        self.array_almost_equal(new_y, spec_y - bkg_y)
Ejemplo n.º 4
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 = Pattern(x, spec_y)
        background_spectrum = Pattern(x_bkg, bkg_y)

        spec.background_pattern = background_spectrum
        new_x, new_y = spec.data

        self.array_almost_equal(new_x, x)
        self.array_almost_equal(new_y, spec_y - x)
Ejemplo n.º 5
0
    def test_saving_a_file(self):
        x = np.linspace(-5, 5, 100)
        y = x**2
        spec = Pattern(x, y)
        filename = os.path.join(data_path, "test.dat")
        spec.save(filename)

        spec2 = Pattern()
        spec2.load(filename)

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

        os.remove(filename)
Ejemplo n.º 6
0
    def test_automatic_background_subtraction_with_roi(self):
        x = np.linspace(0, 24, 2500)
        y = np.zeros(x.shape)

        peaks = [
            [10, 3, 0.1],
            [12, 4, 0.1],
            [12, 6, 0.1],
        ]
        for peak in peaks:
            y += gaussian(x, peak[0], peak[1], peak[2])
        y_bkg = x * 0.4 + 5.0
        y_measurement = y + y_bkg

        roi = [1, 23]

        pattern = Pattern(x, y_measurement)

        auto_background_subtraction_parameters = [2, 50, 50]
        pattern.set_auto_background_subtraction(
            auto_background_subtraction_parameters, roi)

        x_spec, y_spec = pattern.data

        self.assertGreater(x_spec[0], roi[0])
        self.assertLess(x_spec[-1], roi[1])
Ejemplo n.º 7
0
 def add_overlay_file(self, filename):
     """
     Reads a 2-column (x,y) text file and adds it as overlay to the list of overlays
     :param filename: path of the file to be loaded
     """
     self.overlays.append(Pattern())
     self.overlays[-1].load(filename)
     self.overlay_added.emit()
Ejemplo n.º 8
0
 def add_overlay(self, x, y, name=''):
     """
     Adds an overlay to the list of overlays
     :param x: x-values
     :param y: y-values
     :param name: name of overlay to be used for displaying etc.
     """
     self.overlays.append(Pattern(x, y, name))
     self.overlay_added.emit()
Ejemplo n.º 9
0
    def test_plus_and_minus_operators_with_different_shapes(self):
        x = np.linspace(0, 10, 1000)
        x2 = np.linspace(0, 12, 1300)
        spectrum1 = Pattern(x, np.sin(x))
        spectrum2 = Pattern(x2, np.sin(x2))

        spectrum3 = spectrum1 + spectrum2
        self.array_almost_equal(spectrum3.x, spectrum1._original_x)
        self.array_almost_equal(spectrum3.y, spectrum1._original_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)
Ejemplo n.º 10
0
    def test_loading_chi_file(self):
        spec = Pattern()
        x, y = spec.data

        spec.load(os.path.join(data_path, 'spectrum_001.chi'))
        new_x, new_y = spec.data

        self.assertNotEqual(len(x), len(new_x))
        self.assertNotEqual(len(y), len(new_y))
Ejemplo n.º 11
0
    def test_setting_new_data(self):
        spec = Pattern()
        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)
Ejemplo n.º 12
0
 def add_spectrum_as_overlay(self):
     """
     Adds the current data spectrum as overlay to the list of overlays
     """
     current_spectrum = deepcopy(self.pattern)
     overlay_spectrum = Pattern(current_spectrum.x, current_spectrum.y,
                                current_spectrum.name)
     self.overlays.append(overlay_spectrum)
     self.overlay_added.emit()
Ejemplo n.º 13
0
    def __init__(self):
        super(PatternModel, self).__init__()
        self.pattern = Pattern()
        self.overlays = []
        self.phases = []

        self.file_iteration_mode = 'number'
        self.file_name_iterator = FileNameIterator()

        self.bkg_ind = -1
        self.pattern_filename = ''
Ejemplo n.º 14
0
    def test_plus_and_minus_operators(self):
        x = np.linspace(0, 10, 100)
        spectrum1 = Pattern(x, np.sin(x))
        spectrum2 = Pattern(x, np.sin(x))

        spectrum3 = spectrum1 + spectrum2
        self.assertTrue(np.array_equal(spectrum3.y, np.sin(x) * 2))
        self.assertTrue(np.array_equal(spectrum2.original_y, np.sin(x) * 1))
        self.assertTrue(np.array_equal(spectrum1.original_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.original_y, np.sin(x) * 1))
        self.assertTrue(np.array_equal(spectrum1.original_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.original_y, np.sin(x) * 1))
        self.assertTrue(np.array_equal(spectrum1.original_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.original_y, np.sin(x) * 1))
        self.assertTrue(np.array_equal(spectrum1.original_y, np.sin(x) * 1))
Ejemplo n.º 15
0
    def test_automatic_background_subtraction(self):
        x = np.linspace(0, 24, 2500)
        y = np.zeros(x.shape)

        peaks = [
            [10, 3, 0.1],
            [12, 4, 0.1],
            [12, 6, 0.1],
        ]
        for peak in peaks:
            y += gaussian(x, peak[0], peak[1], peak[2])
        y_bkg = x * 0.4 + 5.0
        y_measurement = y + y_bkg

        pattern = Pattern(x, y_measurement)

        auto_background_subtraction_parameters = [2, 50, 50]
        pattern.set_auto_background_subtraction(
            auto_background_subtraction_parameters)

        x_spec, y_spec = pattern.data

        self.array_almost_equal(y_spec, y)
Ejemplo n.º 16
0
 def test_loading_invalid_file(self):
     spec = Pattern()
     self.assertEqual(
         -1, spec.load(os.path.join(data_path, 'wrong_file_format.txt')))
Ejemplo n.º 17
0
    def test_using_len(self):
        x = np.linspace(0, 10, 234)
        y = x**2
        spec = Pattern(x, y)

        self.assertEqual(len(spec), 234)