Ejemplo n.º 1
0
def test_to_array_spectrum(axes):
    s = LumiSpectrum(arange(5))
    a = s.to_array(axes=axes)
    if axes:
        assert_array_equal(s.axes_manager[0].axis, a[:, 0])
    else:
        assert_array_equal(s.data, a)
Ejemplo n.º 2
0
def test_joinspectra_linescan(average, scale, kind):
    s1 = LumiSpectrum(random((4, 64)))
    s2 = LumiSpectrum(random((4, 64)))
    s2.axes_manager.signal_axes[0].offset = 47
    s = join_spectra([s1, s2], r=7, average=average, scale=scale, kind=kind)
    assert s.axes_manager.signal_axes[0].size == 111
    assert s.axes_manager.signal_axes[0].scale == 1
Ejemplo n.º 3
0
def test_joinspectra2():
    s1 = LumiSpectrum(arange(32))
    s2 = LumiSpectrum(arange(32) + 25)
    s2.axes_manager.signal_axes[0].offset = 25
    s2.isig[3] = 0
    s = join_spectra([s1, s2], r=2, average=True, scale=True)
    assert s.data[-1] == 56
    assert s.data[28] == 28 / 3
Ejemplo n.º 4
0
def test_savetxt_spectrum(axes, tmp_path):
    s = LumiSpectrum(arange(5))
    fname = tmp_path / "test.txt"
    s.savetxt(fname, axes=axes)
    s2 = loadtxt(fname)
    if axes:
        assert_array_equal(s.axes_manager[0].axis, s2[:, 0])
    else:
        assert_array_equal(s.data, s2)
Ejemplo n.º 5
0
 def test_crop_edges(self):
     s1 = LumiSpectrum(np.ones((10, 10, 10)))
     s2 = LumiTransient(np.ones((10, 10, 10, 10)))
     s3 = LumiSpectrum(np.ones((3, 3, 10)))
     s1 = s1.crop_edges(crop_px=2)
     s2 = s2.crop_edges(crop_px=2)
     assert s1.axes_manager.navigation_shape[0] == 6
     assert s1.axes_manager.navigation_shape[1] == 6
     assert s2.axes_manager.navigation_shape[0] == 6
     assert s2.axes_manager.navigation_shape[1] == 6
     self.assertRaises(ValueError, s3.crop_edges, crop_px=2)
Ejemplo n.º 6
0
def test_joinspectra_errors():
    s1 = LumiSpectrum(ones(32))
    s2 = LumiSpectrum(ones(32) * 2)
    s2.axes_manager.signal_axes[0].offset = 25
    # Test that catch for r works
    raises(ValueError, join_spectra, [s1, s2])
    s2.axes_manager.signal_axes[0].offset = 35
    # Test that overlap catch works
    raises(ValueError, join_spectra, [s1, s2], r=2)
    s1.data *= -1
    s2.axes_manager.signal_axes[0].offset = 25
    raises(ValueError, join_spectra, [s1, s2], r=2)
Ejemplo n.º 7
0
def test_joinspectra(average, scale, kind):
    s1 = LumiSpectrum(arange(32))
    s2 = LumiSpectrum(arange(32) + 25)
    s3 = LumiSpectrum(arange(32) + 50)
    s2.axes_manager.signal_axes[0].offset = 25
    s3.axes_manager.signal_axes[0].offset = 50
    s = join_spectra([s1, s2, s3],
                     r=2,
                     average=average,
                     scale=scale,
                     kind=kind)
    assert s.data[-1] == 81
    assert s.axes_manager.signal_axes[0].scale == 1
    assert s.axes_manager.signal_axes[0].size == 82
Ejemplo n.º 8
0
 def test_remove_negative(self):
     s1 = LumiSpectrum(np.random.random((10, 10, 10))) - 0.3
     s2 = LumiTransient(np.random.random((10, 10, 10, 10))) - 0.3
     s3 = LumiTransient(np.random.random((10, 10, 10, 10))) - 0.3
     s1a = s1.remove_negative(inplace=False)
     s2a = s2.remove_negative(inplace=False)
     s3a = s3.remove_negative(inplace=False, basevalue=0.1)
     assert s3a.metadata.Signal.negative_removed == True
     assert np.all(s1a.data[s1 <= 0] == 1)
     assert np.all(s2a.data[s2 <= 0] == 1)
     assert np.all(s3a.data[s3 <= 0] == 0.1)
     s1.remove_negative(inplace=True)
     s2.remove_negative(inplace=True)
     s3.remove_negative(basevalue=0.1)
     assert s1 == s1a
     assert s2 == s2a
     assert s3 == s3a
Ejemplo n.º 9
0
def test_to_array_linescan(axes, transpose):
    s = LumiSpectrum(arange(20).reshape((4, 5)))
    a = s.to_array(axes=axes, transpose=transpose)
    if axes:
        if transpose:
            assert_array_equal(s.axes_manager[0].axis, a[0, 1:])
            assert_array_equal(s.axes_manager[1].axis, a[1:, 0])
            assert_array_equal(s.data.T, a[1:, 1:])
        else:
            assert_array_equal(s.axes_manager[1].axis, a[0, 1:])
            assert_array_equal(s.axes_manager[0].axis, a[1:, 0])
            assert_array_equal(s.data, a[1:, 1:])
    else:
        if transpose:
            assert_array_equal(s.data.T, a)
        else:
            assert_array_equal(s.data, a)
Ejemplo n.º 10
0
def test_savetxt_linescan(axes, transpose, tmp_path):
    s = LumiSpectrum(arange(20).reshape((4, 5)))
    fname = tmp_path / "test.txt"
    s.savetxt(fname, axes=axes, transpose=transpose)
    s2 = loadtxt(fname)
    if axes:
        if transpose:
            assert_array_equal(s.axes_manager[0].axis, s2[0, 1:])
            assert_array_equal(s.axes_manager[1].axis, s2[1:, 0])
            assert_array_equal(s.data.T, s2[1:, 1:])
        else:
            assert_array_equal(s.axes_manager[1].axis, s2[0, 1:])
            assert_array_equal(s.axes_manager[0].axis, s2[1:, 0])
            assert_array_equal(s.data, s2[1:, 1:])
    else:
        if transpose:
            assert_array_equal(s.data.T, s2)
        else:
            assert_array_equal(s.data, s2)
Ejemplo n.º 11
0
def test_joinspectra_FunctionalDA(average, scale, kind):
    try:
        from hyperspy.axes import FunctionalDataAxis
    except ImportError:
        skip("HyperSpy version doesn't support non-uniform axis")
    s1 = LumiSpectrum(ones(32))
    s2 = LumiSpectrum(ones(32) * 2)
    s2.axes_manager.signal_axes[0].offset = 25
    s1.axes_manager.signal_axes[0].convert_to_functional_data_axis(
        expression='x**2')
    s2.axes_manager.signal_axes[0].convert_to_functional_data_axis(
        expression='x**2')
    s = join_spectra([s1, s2], r=2, average=average, scale=scale, kind=kind)
    assert s.axes_manager.signal_axes[0].is_uniform == False
    assert s.axes_manager.signal_axes[0].size == 57
    assert s.axes_manager.signal_axes[0].axis[-1] == 3136
    assert s.data.size == 57
    if scale: assert s.data[-1] == 1
    else: assert s.data[-1] == 2
Ejemplo n.º 12
0
def test_joinspectra_nonuniform(average, scale, kind):
    try:
        from hyperspy.axes import UniformDataAxis
    except ImportError:
        skip("HyperSpy version doesn't support non-uniform axis")
    s1 = LumiSpectrum(arange(32))
    s2 = LumiSpectrum(arange(32) + 25)
    s2.axes_manager.signal_axes[0].offset = 25
    s1.axes_manager.signal_axes[0].convert_to_non_uniform_axis()
    s = join_spectra([s1, s2], r=2, average=average, scale=scale, kind=kind)
    assert s.axes_manager.signal_axes[0].is_uniform == False
    assert s.axes_manager.signal_axes[0].size == 57
    assert s.axes_manager.signal_axes[0].axis[-1] == 56
    assert s.data.size == 57
    assert s.data[-1] == 56
    s1 = LumiSpectrum(arange(12))
    s2 = LumiSpectrum(arange(12) + 3.8, axes=[DataAxis(axis=arange(12) + 3.8)])
    s = join_spectra([s1, s2], r=2, average=average, scale=scale, kind=kind)
    assert s.axes_manager[0].axis.size == 16
    assert s.data.size == 16
    assert s.data[-1] == 14.8
Ejemplo n.º 13
0
 def test_scale_by_exposure(self):
     s1 = LumiSpectrum(np.ones((10, 10, 10)))
     s2 = LumiTransient(np.ones((10, 10, 10, 10)))
     s3 = LumiSpectrum(np.ones((10, 10)))
     s4 = LumiSpectrum(np.ones((10)))
     s2.metadata.set_item("Acquisition_instrument.CL.exposure", 2)
     s3.metadata.set_item("Acquisition_instrument.CL.dwell_time", 0.5)
     s3.metadata.set_item("Signal.quantity", "Intensity (Counts)")
     s4.metadata.set_item("Signal.quantity", "Intensity (counts)")
     s1a = s1.scale_by_exposure(exposure=4)
     s2a = s2.scale_by_exposure()
     s3a = s3.scale_by_exposure()
     s4a = s4.scale_by_exposure(exposure=0.1)
     assert np.all(s1a.data == 0.25)
     assert np.all(s2a.data == 0.5)
     assert np.all(s3a.data == 2)
     assert np.all(s4a.data == 10)
     assert s3a.metadata.Signal.quantity == "Intensity (Counts/s)"
     assert s4a.metadata.Signal.quantity == "Intensity (counts/s)"
     assert s4a.metadata.Signal.scaled == True
     s1.scale_by_exposure(exposure=4, inplace=True)
     s2.scale_by_exposure(inplace=True)
     s3.scale_by_exposure(inplace=True)
     s4.scale_by_exposure(exposure=0.1, inplace=True)
     assert s1 == s1a
     assert s2 == s2a
     assert s3 == s3a
     assert s4 == s4a
     assert s3.metadata.Signal.quantity == "Intensity (Counts/s)"
     assert s4.metadata.Signal.quantity == "Intensity (counts/s)"
     # Test for errors
     s4 = LumiSpectrum(np.ones((10)))
     s4.normalize(inplace=True)
     with pytest.raises(AttributeError) as excinfo:
         s4.scale_by_exposure(inplace=True, exposure=0.5)
     assert str(
         excinfo.value) == "Data was normalized and cannot be " "scaled."
     s5 = LumiSpectrum(np.ones((10)))
     with pytest.raises(AttributeError) as excinfo:
         s5.scale_by_exposure(inplace=True)
     assert (str(excinfo.value) == "Exposure not given and can not be "
             "extracted automatically from metadata.")
     s5.scale_by_exposure(inplace=True, exposure=0.5)
     with pytest.raises(AttributeError) as excinfo:
         s5.scale_by_exposure(inplace=True, exposure=0.5)
     assert str(excinfo.value) == "Data was already scaled."
Ejemplo n.º 14
0
 def test_normalize(self):
     s1 = LumiSpectrum(np.random.random((10, 10, 10))) * 2
     s2 = LumiTransient(np.random.random((10, 10, 10, 10))) * 2
     s3 = LumiSpectrum(np.random.random((10, 10))) * 2
     s4 = LumiSpectrum(np.random.random((10))) * 2
     s4.metadata.set_item("Signal.quantity", "Intensity (counts)")
     s1a = s1.normalize()
     s2a = s2.normalize()
     s3a = s3.normalize()
     s4a = s4.normalize()
     assert s1a.max(axis=[0, 1, 2]).data[0] == 1
     assert s2a.max(axis=[0, 1, 2, 3]).data[0] == 1
     assert s3a.max(axis=[0, 1]).data[0] == 1
     assert s4a.max(axis=[0]).data[0] == 1
     assert s4a.metadata.Signal.quantity == "Normalized intensity"
     assert s4a.metadata.Signal.normalized == True
     s1a = s1.normalize(element_wise=True)
     s2a = s2.normalize(element_wise=True)
     s3a = s3.normalize(element_wise=True)
     s4a = s4.normalize(element_wise=True)
     assert np.all(s1a.max(axis=[2]).data[0] == 1)
     assert np.all(s2a.max(axis=[3]).data[0] == 1)
     assert np.all(s3a.max(axis=[1]).data[0] == 1)
     assert s4a.max(axis=[0]).data[0] == 1
     s1a = s1.normalize(pos=3)
     s2a = s2.normalize(pos=3, element_wise=True)
     s3a = s3.normalize(pos=3, element_wise=True)
     s4a = s4.normalize(pos=3)
     assert s1a.isig[3].max().data[0] == 1
     assert np.all(s2a.isig[3] == 1)
     assert np.all(s3a.isig[3] == 1)
     assert s4a.isig[3].data == 1
     s1.normalize(pos=3, inplace=True)
     s2.normalize(pos=3, element_wise=True, inplace=True)
     s3.normalize(pos=3, element_wise=True, inplace=True)
     s4.normalize(pos=3, inplace=True)
     assert s1a == s1
     assert s2a == s2
     assert s3a == s3
     assert s4a == s4
     assert s4.metadata.Signal.quantity == "Normalized intensity"
     with pytest.warns(UserWarning) as warninfo:
         s1.normalize(inplace=True)
     assert len(warninfo) == 1
     assert warninfo[0].message.args[0][:8] == "Data was"
Ejemplo n.º 15
0
def test_to_eV(jacobian):
    axis = DataAxis(size=20, offset=200, scale=10)
    data = ones(20)
    S1 = LumiSpectrum(data, axes=(axis.get_axis_dictionary(), ))

    if not "axis" in getfullargspec(DataAxis)[0]:
        raises(ImportError, S1.to_eV)
    try:
        from hyperspy.axes import UniformDataAxis
    except ImportError:
        skip("HyperSpy version doesn't support non-uniform axis")

    axis = UniformDataAxis(size=20, offset=200, scale=10)
    data = ones(20)
    S1 = LumiSpectrum(data, axes=(axis.get_axis_dictionary(), ))
    S2 = S1.to_eV(inplace=False, jacobian=jacobian)
    S1.axes_manager[0].units = "µm"
    S1.axes_manager[0].axis = axis.axis / 1000
    S1.data *= 1000
    S1.to_eV(jacobian=jacobian)
    assert S1.axes_manager[0].units == "eV"
    assert S2.axes_manager[0].name == "Energy"
    assert S2.axes_manager[0].size == 20
    assert S1.axes_manager[0].axis[0] == S2.axes_manager[0].axis[0]
    assert_allclose(S1.data, S2.data, 5e-4)
    nav = UniformDataAxis(size=4)
    # navigation dimension 1
    L1 = LumiSpectrum(
        ones((4, 20)),
        axes=[nav.get_axis_dictionary(),
              axis.get_axis_dictionary()])
    L2 = L1.to_eV(inplace=False, jacobian=jacobian)
    L1.to_eV(jacobian=jacobian)
    assert L1.axes_manager.signal_axes[0].units == "eV"
    assert L2.axes_manager.signal_axes[0].name == "Energy"
    assert L2.axes_manager.signal_axes[0].size == 20
    assert (L1.axes_manager.signal_axes[0].axis[0] ==
            L2.axes_manager.signal_axes[0].axis[0])
    assert_allclose(L1.data, L2.data, 5e-4)
    # navigation dimension 2
    M1 = LumiSpectrum(
        ones((4, 4, 20)),
        axes=[
            nav.get_axis_dictionary(),
            nav.get_axis_dictionary(),
            axis.get_axis_dictionary(),
        ],
    )
    M2 = M1.to_eV(inplace=False, jacobian=jacobian)
    M1.to_eV(jacobian=jacobian)
    assert M1.axes_manager.signal_axes[0].units == "eV"
    assert M2.axes_manager.signal_axes[0].name == "Energy"
    assert M2.axes_manager.signal_axes[0].size == 20
    assert (M1.axes_manager.signal_axes[0].axis[0] ==
            M2.axes_manager.signal_axes[0].axis[0])
    assert_allclose(M1.data, M2.data, 5e-4)
Ejemplo n.º 16
0
def test_to_array_dimension_error():
    s = LumiSpectrum(arange(60).reshape((3, 4, 5)))
    with raises(NotImplementedError):
        s.to_array()
Ejemplo n.º 17
0
def test_savetxt_dimension_error(tmp_path):
    s = LumiSpectrum(arange(60).reshape((3, 4, 5)))
    fname = tmp_path / "test.txt"
    with raises(NotImplementedError):
        s.savetxt(fname)