Exemplo n.º 1
0
def normalize_l2(spectrum: Spectrum, in_place: bool = False):
    sum_norm = np.linalg.norm(spectrum.y, ord=2)

    if not in_place:
        spectrum = spectrum.copy()

    spectrum.y /= sum_norm
    return spectrum
Exemplo n.º 2
0
def normalize_max(spectrum: Spectrum, in_place: bool = False):
    maximum = np.max(spectrum.y)

    if not in_place:
        spectrum = spectrum.copy()

    spectrum.y /= maximum
    return spectrum
Exemplo n.º 3
0
def normalize_sum(spectrum: Spectrum, value=1.0, in_place: bool = False):
    total = np.sum(spectrum.y)

    if not in_place:
        spectrum = spectrum.copy()

    spectrum.y /= total
    spectrum.y *= value
    return spectrum
Exemplo n.º 4
0
def normalize_z_max(spectrum: Spectrum, in_place: bool = False):
    mu = np.mean(spectrum.y)
    sig = np.std(spectrum.y)
    if not in_place:
        spectrum = spectrum.copy()

    spectrum.y = np.array([(y - mu) / sig for y in spectrum.y])
    spectrum.y /= np.max(spectrum.y)

    return spectrum
Exemplo n.º 5
0
def normalize_minmax(spectrum: Spectrum, in_place: bool = False):
    spec_max, spec_min = max(spectrum.y), min(spectrum.y)

    mins_array = spec_min * np.ones(shape=spectrum.y.shape, dtype='float')

    if not in_place:
        spectrum = spectrum.copy()

    spectrum.y -= mins_array
    spectrum.y /= (spec_max - spec_min)

    return spectrum
Exemplo n.º 6
0
def normalize_z(spectrum: Spectrum, in_place: bool = False):
    """
    Normalize by treating the spectrum as a normal distribution and finding the
    'z-score' associated with each point
    :param spectrum:
    :param in_place:
    :return:
    """

    mu = np.mean(spectrum.y)
    sig = np.std(spectrum.y)

    if not in_place:
        spectrum = spectrum.copy()

    spectrum.y = np.array([(y - mu) / sig for y in spectrum.y])

    return spectrum
Exemplo n.º 7
0
def normalize_0left_1right(spectrum: Spectrum, in_place: bool = False):
    """
    Normalize by setting the input value to 0 and the final value to about 1.
    :param spectrum: 
    :param in_place: 
    :return: 
    """

    to_sub = spectrum.y[0]

    if not in_place:
        spectrum = spectrum.copy()

    spectrum.y -= to_sub

    spectrum_final_avg = np.mean(spectrum.y[-5:])

    spectrum.y /= spectrum_final_avg

    return spectrum
Exemplo n.º 8
0
class SpectrumTest(PymatgenTest):
    def setUp(self):
        self.spec1 = Spectrum(np.arange(0, 10, 0.1), np.random.randn(100))
        self.spec2 = Spectrum(np.arange(0, 10, 0.1), np.random.randn(100))

        self.multi_spec1 = Spectrum(np.arange(0, 10, 0.1),
                                    np.random.randn(100, 2))
        self.multi_spec2 = Spectrum(np.arange(0, 10, 0.1),
                                    np.random.randn(100, 2))

    def test_normalize(self):
        self.spec1.normalize(mode="max")
        self.assertAlmostEqual(np.max(self.spec1.y), 1)
        self.spec1.normalize(mode="sum")
        self.assertAlmostEqual(np.sum(self.spec1.y), 1)

        self.multi_spec1.normalize(mode="sum")
        self.assertAlmostEqual(np.sum(self.multi_spec1.y[:, 0]), 1)
        self.assertAlmostEqual(np.sum(self.multi_spec1.y[:, 1]), 1)

        # XRD style mode.
        self.spec1.normalize(mode="max", value=100)
        self.assertAlmostEqual(np.max(self.spec1.y), 100)

    def test_operators(self):
        scaled_spect = 3 * self.spec1 + self.spec2
        self.assertTrue(
            np.allclose(scaled_spect.y, 3 * self.spec1.y + self.spec2.y))
        self.assertAlmostEqual(
            self.spec1.get_interpolated_value(0.05),
            (self.spec1.y[0] + self.spec1.y[1]) / 2,
        )

        scaled_spect = self.spec1 - self.spec2
        self.assertTrue(
            np.allclose(scaled_spect.y, self.spec1.y - self.spec2.y))

        scaled_spect = self.spec1 / 3
        self.assertTrue(np.allclose(scaled_spect.y, self.spec1.y / 3))

        scaled_spect = 3 * self.multi_spec1 + self.multi_spec2
        self.assertTrue(
            np.allclose(scaled_spect.y,
                        3 * self.multi_spec1.y + self.multi_spec2.y))
        self.assertArrayAlmostEqual(
            self.multi_spec1.get_interpolated_value(0.05),
            (self.multi_spec1.y[0, :] + self.multi_spec1.y[1, :]) / 2,
        )

    def test_smear(self):
        y = np.array(self.spec1.y)
        self.spec1.smear(0.2)
        self.assertFalse(np.allclose(y, self.spec1.y))
        self.assertAlmostEqual(sum(y), sum(self.spec1.y))

        y = np.array(self.multi_spec1.y)
        self.multi_spec1.smear(0.2)
        self.assertFalse(np.allclose(y, self.multi_spec1.y))
        self.assertArrayAlmostEqual(np.sum(y, axis=0),
                                    np.sum(self.multi_spec1.y, axis=0))

    def test_str(self):
        # Just make sure that these methods work.
        self.assertIsNotNone(str(self.spec1))
        self.assertIsNotNone(str(self.multi_spec1))

    def test_copy(self):
        spec1copy = self.spec1.copy()
        spec1copy.y[0] = spec1copy.y[0] + 1
        self.assertNotEqual(spec1copy.y[0], self.spec1.y[0])
        self.assertEqual(spec1copy.y[1], self.spec1.y[1])
Exemplo n.º 9
0
class SpectrumTest(PymatgenTest):
    def setUp(self):
        self.spec1 = Spectrum(np.arange(0, 10, 0.1), np.random.randn(100))
        self.spec2 = Spectrum(np.arange(0, 10, 0.1), np.random.randn(100))

        self.multi_spec1 = Spectrum(np.arange(0, 10, 0.1), np.random.randn(100, 2))
        self.multi_spec2 = Spectrum(np.arange(0, 10, 0.1), np.random.randn(100, 2))

    def test_normalize(self):
        self.spec1.normalize(mode="max")
        self.assertAlmostEqual(np.max(self.spec1.y), 1)
        self.spec1.normalize(mode="sum")
        self.assertAlmostEqual(np.sum(self.spec1.y), 1)

        self.multi_spec1.normalize(mode="sum")
        self.assertAlmostEqual(np.sum(self.multi_spec1.y[:, 0]), 1)
        self.assertAlmostEqual(np.sum(self.multi_spec1.y[:, 1]), 1)

        # XRD style mode.
        self.spec1.normalize(mode="max", value=100)
        self.assertAlmostEqual(np.max(self.spec1.y), 100)

    def test_operators(self):
        scaled_spect = 3 * self.spec1 + self.spec2
        self.assertTrue(np.allclose(scaled_spect.y, 3 * self.spec1.y + self.spec2.y))
        self.assertAlmostEqual(
            self.spec1.get_interpolated_value(0.05),
            (self.spec1.y[0] + self.spec1.y[1]) / 2,
        )

        scaled_spect = self.spec1 - self.spec2
        self.assertTrue(np.allclose(scaled_spect.y, self.spec1.y - self.spec2.y))

        scaled_spect = self.spec1 / 3
        self.assertTrue(np.allclose(scaled_spect.y, self.spec1.y / 3))

        scaled_spect = 3 * self.multi_spec1 + self.multi_spec2
        self.assertTrue(np.allclose(scaled_spect.y, 3 * self.multi_spec1.y + self.multi_spec2.y))
        self.assertArrayAlmostEqual(
            self.multi_spec1.get_interpolated_value(0.05),
            (self.multi_spec1.y[0, :] + self.multi_spec1.y[1, :]) / 2,
        )

    def test_smear(self):
        y = np.zeros(100)
        y[25] = 1
        y[50] = 1
        y[75] = 1
        spec = Spectrum(np.linspace(-10, 10, 100), y)
        spec.smear(0.3)
        self.assertFalse(np.allclose(y, spec.y))
        self.assertAlmostEqual(sum(y), sum(spec.y))

        # Test direct callable use of smearing.
        spec2 = Spectrum(np.linspace(-10, 10, 100), y)
        spec2.smear(0, func=lambda x: stats.norm.pdf(x, scale=0.3))
        self.assertTrue(np.allclose(spec.y, spec2.y))

        spec = Spectrum(np.linspace(-10, 10, 100), y)
        spec.smear(0.3, func="lorentzian")
        self.assertFalse(np.allclose(y, spec.y))
        self.assertAlmostEqual(sum(y), sum(spec.y))

        y = np.array(self.multi_spec1.y)
        self.multi_spec1.smear(0.2)
        self.assertFalse(np.allclose(y, self.multi_spec1.y))
        self.assertArrayAlmostEqual(np.sum(y, axis=0), np.sum(self.multi_spec1.y, axis=0))

    def test_str(self):
        # Just make sure that these methods work.
        self.assertIsNotNone(str(self.spec1))
        self.assertIsNotNone(str(self.multi_spec1))

    def test_copy(self):
        spec1copy = self.spec1.copy()
        spec1copy.y[0] = spec1copy.y[0] + 1
        self.assertNotEqual(spec1copy.y[0], self.spec1.y[0])
        self.assertEqual(spec1copy.y[1], self.spec1.y[1])
Exemplo n.º 10
0
class SpectrumTest(PymatgenTest):

    def setUp(self):
        self.spec1 = Spectrum(np.arange(0, 10, 0.1), np.random.randn(100))
        self.spec2 = Spectrum(np.arange(0, 10, 0.1), np.random.randn(100))

        self.multi_spec1 = Spectrum(np.arange(0, 10, 0.1),
                                    np.random.randn(100, 2))
        self.multi_spec2 = Spectrum(np.arange(0, 10, 0.1),
                                    np.random.randn(100, 2))

    def test_normalize(self):
        self.spec1.normalize(mode="max")
        self.assertAlmostEqual(np.max(self.spec1.y), 1)
        self.spec1.normalize(mode="sum")
        self.assertAlmostEqual(np.sum(self.spec1.y), 1)

        self.multi_spec1.normalize(mode="sum")
        self.assertAlmostEqual(np.sum(self.multi_spec1.y[:, 0]), 1)
        self.assertAlmostEqual(np.sum(self.multi_spec1.y[:, 1]), 1)

        # XRD style mode.
        self.spec1.normalize(mode="max", value=100)
        self.assertAlmostEqual(np.max(self.spec1.y), 100)

    def test_operators(self):
        scaled_spect = 3 * self.spec1 + self.spec2
        self.assertTrue(np.allclose(scaled_spect.y,
                                    3 * self.spec1.y + self.spec2.y))
        self.assertAlmostEqual(self.spec1.get_interpolated_value(0.05),
                               (self.spec1.y[0] + self.spec1.y[1]) / 2)

        scaled_spect = self.spec1 - self.spec2
        self.assertTrue(np.allclose(scaled_spect.y,
                                    self.spec1.y - self.spec2.y))

        scaled_spect = self.spec1 / 3
        self.assertTrue(np.allclose(scaled_spect.y,
                                    self.spec1.y / 3))

        scaled_spect = 3 * self.multi_spec1 + self.multi_spec2
        self.assertTrue(np.allclose(scaled_spect.y,
                                    3 * self.multi_spec1.y + self.multi_spec2.y))
        self.assertArrayAlmostEqual(
            self.multi_spec1.get_interpolated_value(0.05),
            (self.multi_spec1.y[0, :] + self.multi_spec1.y[1, :]) / 2)

    def test_smear(self):
        y = np.array(self.spec1.y)
        self.spec1.smear(0.2)
        self.assertFalse(np.allclose(y, self.spec1.y))
        self.assertAlmostEqual(sum(y), sum(self.spec1.y))

        y = np.array(self.multi_spec1.y)
        self.multi_spec1.smear(0.2)
        self.assertFalse(np.allclose(y, self.multi_spec1.y))
        self.assertArrayAlmostEqual(np.sum(y, axis=0),
                                    np.sum(self.multi_spec1.y, axis=0))

    def test_str(self):
        # Just make sure that these methods work.
        self.assertIsNotNone(str(self.spec1))
        self.assertIsNotNone(str(self.multi_spec1))

    def test_copy(self):
        spec1copy = self.spec1.copy()
        spec1copy.y[0] = spec1copy.y[0] + 1
        self.assertNotEqual(spec1copy.y[0], self.spec1.y[0])
        self.assertEqual(spec1copy.y[1], self.spec1.y[1])