Beispiel #1
0
 def test_initialisation_errors(self):
     expression = "x ** power"
     with pytest.raises(ValueError, match="Please provide"):
         self.axis = FunctionalDataAxis(
             expression=expression,
             power=2,)
     with pytest.raises(ValueError, match="The values of"):
         self.axis = FunctionalDataAxis(
             size=10,
             expression=expression,)
Beispiel #2
0
def test_nonuniformFDA(tmp_path, file, lazy):
    fname = tmp_path / file
    data = np.arange(10)
    x0 = UniformDataAxis(size=data.size, offset=1)
    axis = FunctionalDataAxis(expression='1/x', x=x0, navigate=False)
    s = Signal1D(data, axes=(axis.get_axis_dictionary(), ))
    if lazy:
        s = s.as_lazy()
    print(axis.get_axis_dictionary())
    s.save(fname, overwrite=True)
    s2 = load(fname)
    np.testing.assert_array_almost_equal(s.axes_manager[0].axis,
                                         s2.axes_manager[0].axis)
    assert (s2.axes_manager[0].is_uniform == False)
    assert (s2.axes_manager[0].navigate == False)
    assert (s2.axes_manager[0].size == data.size)
Beispiel #3
0
class TestReciprocalDataAxis:

    def setup_method(self, method):
        expression = "a / (x + 1) + b"
        self.axis = FunctionalDataAxis(size=10, expression=expression,
                                       a=0.1, b=10)

    def _test_initialisation_parameters(self, axis):
        assert axis.a == 0.1
        assert axis.b == 10
        def func(x): return 0.1 / (x + 1) + 10
        np.testing.assert_allclose(axis.axis, func(np.arange(10)))

    def test_initialisation_parameters(self):
        self._test_initialisation_parameters(self.axis)

    def test_create_axis(self):
        axis = create_axis(**self.axis.get_axis_dictionary())
        assert isinstance(axis, FunctionalDataAxis)
        self._test_initialisation_parameters(axis)


    @pytest.mark.parametrize("use_indices", (True, False))
    def test_crop(self, use_indices):
        axis = self.axis
        start, end = 10.05, 10.02
        if use_indices:
            start = axis.value2index(start)
            end = axis.value2index(end)
        axis.crop(start, end)
        assert axis.size == 3
        np.testing.assert_almost_equal(axis.axis[0], 10.05)
        np.testing.assert_almost_equal(axis.axis[-1], 10.025)
Beispiel #4
0
def test_check_axes_calibration():
    axisDA1 = DataAxis(axis=arange(10)**2)
    axisDA2 = DataAxis(axis=arange(10))
    axisDA3 = DataAxis(axis=arange(10), units='s')
    axisDA4 = DataAxis(axis=arange(10), units='seconds')
    expression = "x ** power"
    axisFDA1 = FunctionalDataAxis(size=10, expression=expression, power=2)
    axisFDA2 = FunctionalDataAxis(size=12, expression=expression, power=2)
    axisUDA1 = UniformDataAxis(size=10, scale=1, offset=10)
    axisUDA2 = UniformDataAxis(size=10, scale=1, offset=0)
    assert check_axes_calibration(axisDA1, axisDA1)
    assert not check_axes_calibration(axisDA1, axisDA2)
    assert check_axes_calibration(axisDA3, axisDA4)
    assert check_axes_calibration(axisFDA1, axisFDA1)
    assert not check_axes_calibration(axisFDA1, axisFDA2)
    assert check_axes_calibration(axisUDA1, axisUDA1)
    assert not check_axes_calibration(axisUDA1, axisUDA2)
    assert not check_axes_calibration(axisDA1, axisUDA1)
    assert not check_axes_calibration(axisFDA1, axisUDA1)
    assert check_axes_calibration(axisDA1, axisFDA1)
    assert check_axes_calibration(axisDA2, axisUDA2)
Beispiel #5
0
 def setup_method(self, method):
     expression = "a / (x + 1) + b"
     self.axis = FunctionalDataAxis(size=10, expression=expression,
                                    a=0.1, b=10)
Beispiel #6
0
 def test_update_from(self):
     ax2 = FunctionalDataAxis(size=2, units="nm", expression="x ** power", power=3)
     self.axis.update_from(ax2, attributes=("units", "power"))
     assert ((ax2.units, ax2.power) ==
             (self.axis.units, self.axis.power))
Beispiel #7
0
 def setup_method(self, method):
     expression = "x ** power"
     self.axis = FunctionalDataAxis(
         size=10,
         expression=expression,
         power=2,)
Beispiel #8
0
class TestFunctionalDataAxis:

    def setup_method(self, method):
        expression = "x ** power"
        self.axis = FunctionalDataAxis(
            size=10,
            expression=expression,
            power=2,)

    def test_initialisation_parameters(self):
        axis = self.axis
        assert axis.power == 2
        np.testing.assert_allclose(
            axis.axis,
            np.arange(10)**2)

    def test_create_axis(self):
        axis = create_axis(**self.axis.get_axis_dictionary())
        assert isinstance(axis, FunctionalDataAxis)

    def test_initialisation_errors(self):
        expression = "x ** power"
        with pytest.raises(ValueError, match="Please provide"):
            self.axis = FunctionalDataAxis(
                expression=expression,
                power=2,)
        with pytest.raises(ValueError, match="The values of"):
            self.axis = FunctionalDataAxis(
                size=10,
                expression=expression,)

    @pytest.mark.parametrize("use_indices", (True, False))
    def test_crop(self, use_indices):
        axis = self.axis
        start, end = 3.9, 72.6
        if use_indices:
            start = 2
            end = -1
        axis.crop(start, end)
        assert axis.size == 7
        np.testing.assert_almost_equal(axis.axis[0], 4.)
        np.testing.assert_almost_equal(axis.axis[-1], 64.)

    def test_convert_to_non_uniform_axis(self):
        axis = np.copy(self.axis.axis)
        is_binned = self.axis.is_binned
        navigate = self.axis.navigate
        self.axis.name = "parrot"
        self.axis.units = "plumage"
        s = Signal1D(np.arange(10), axes=[self.axis])
        index_in_array = s.axes_manager[0].index_in_array
        s.axes_manager[0].convert_to_non_uniform_axis()
        assert isinstance(s.axes_manager[0], DataAxis)
        assert s.axes_manager[0].name == "parrot"
        assert s.axes_manager[0].units == "plumage"
        assert s.axes_manager[0].size == 10
        assert s.axes_manager[0].low_value == 0
        assert s.axes_manager[0].high_value == 81
        np.testing.assert_allclose(s.axes_manager[0].axis, axis)
        with pytest.raises(AttributeError):
            s.axes_manager[0]._expression
        with pytest.raises(AttributeError):
            s.axes_manager[0]._function
        with pytest.raises(AttributeError):
            s.axes_manager[0].x
        assert index_in_array == s.axes_manager[0].index_in_array
        assert is_binned == s.axes_manager[0].is_binned
        assert navigate == s.axes_manager[0].navigate

    def test_update_from(self):
        ax2 = FunctionalDataAxis(size=2, units="nm", expression="x ** power", power=3)
        self.axis.update_from(ax2, attributes=("units", "power"))
        assert ((ax2.units, ax2.power) ==
                (self.axis.units, self.axis.power))

    def test_slice_me(self):
        assert self.axis._slice_me(slice(1, 5)) == slice(1, 5)
        assert self.axis.size == 4
        np.testing.assert_allclose(self.axis.axis, np.arange(1, 5)**2)

    def test_calibrate(self):
        with pytest.raises(TypeError, match="only for uniform axes"):
            self.axis.calibrate(value_tuple=(11,12), index_tuple=(0,5))

    def test_functional_value2index(self):
        #Tests for value2index
        #Works as intended
        assert self.axis.value2index(44.7) == 7
        assert self.axis.value2index(2.5, rounding=round) == 1
        assert self.axis.value2index(2.5, rounding=math.ceil) == 2
        assert self.axis.value2index(2.5, rounding=math.floor) == 1
        # Returns integer
        assert isinstance(self.axis.value2index(45), (int, np.integer))
        #Input None --> output None
        assert self.axis.value2index(None) == None
        #NaN in --> error out
        with pytest.raises(ValueError):
            self.axis.value2index(np.nan)
        #Values in out of bounds --> error out (both sides of axis)
        with pytest.raises(ValueError):
            self.axis.value2index(-2)
        with pytest.raises(ValueError):
            self.axis.value2index(111)
        #str in --> error out
        with pytest.raises(TypeError):
            self.axis.value2index("69")
        #Empty str in --> error out
        with pytest.raises(TypeError):
            self.axis.value2index("")

        #Tests with array Input
        #Array in --> array out
        arval = np.array([[0,4],[16.,36.]])
        assert np.all(self.axis.value2index(arval) == np.array([[0,2],[4,6]]))
        #One value out of bound in array in --> error out (both sides)
        arval[1,1] = 111
        with pytest.raises(ValueError):
            self.axis.value2index(arval)
        arval[1,1] = -0.3
        with pytest.raises(ValueError):
            self.axis.value2index(arval)
        #One NaN in array in --> error out
        arval[1,1] = np.nan
        with pytest.raises(ValueError):
            self.axis.value2index(arval)
        #Single-value-array-in --> scalar out
        arval = np.array([1.0])
        assert np.isscalar(self.axis.value2index(arval))