Exemplo n.º 1
0
    def test_repeated_measurement(self):
        """test recording repeatedly measured values"""

        a = q.Measurement([10, 9.8, 9.9, 10.1, 10.2])
        assert isinstance(a, RepeatedlyMeasuredValue)
        assert a.value == 10
        assert a.error == pytest.approx(0.070710730438880)
        assert a.std == pytest.approx(0.158114)
        a.use_std_for_uncertainty()
        assert a.error == pytest.approx(0.158114)

        assert isinstance(a.raw_data, np.ndarray)
        assert not isinstance(a.raw_data, ExperimentalValueArray)

        b = q.Measurement([10, 9.8, 9.9, 10.1, 10.2],
                          [0.5, 0.3, 0.1, 0.2, 0.2])
        assert isinstance(b, RepeatedlyMeasuredValue)
        assert b.mean == 10
        assert b.value == 10
        assert b.error == pytest.approx(0.070710730438880)

        assert isinstance(b.raw_data, ExperimentalValueArray)
        assert all(b.raw_data == [10, 9.8, 9.9, 10.1, 10.2])

        with pytest.raises(ValueError):
            q.Measurement([10, 9.8, 9.9, 10.1, 10.2], [0.5, 0.3, 0.1, 0.2])
Exemplo n.º 2
0
    def test_record_measurement_array(self):
        """tests for recording a measurement array in different ways"""

        a = q.MeasurementArray([1, 2, 3, 4, 5])
        assert isinstance(a, ExperimentalValueArray)
        assert all(a.values == [1, 2, 3, 4, 5])
        assert all(a.errors == [0, 0, 0, 0, 0])
        assert str(a) == "[ 1 +/- 0, 2 +/- 0, 3 +/- 0, 4 +/- 0, 5 +/- 0 ]"

        with pytest.raises(TypeError):
            q.MeasurementArray(1)

        b = q.MeasurementArray([1, 2, 3, 4, 5], 0.5)
        assert all(b.errors == [0.5, 0.5, 0.5, 0.5, 0.5])
        c = q.MeasurementArray([1, 2, 3, 4, 5], relative_error=0.1)
        assert c.errors == pytest.approx([0.1, 0.2, 0.3, 0.4, 0.5])
        d = q.MeasurementArray([1, 2, 3, 4, 5], [0.1, 0.2, 0.3, 0.4, 0.5])
        assert all(d.errors == [0.1, 0.2, 0.3, 0.4, 0.5])
        e = q.MeasurementArray([1, 2, 3, 4, 5],
                               relative_error=[0.1, 0.2, 0.3, 0.4, 0.5])
        assert e.errors == pytest.approx([0.1, 0.4, 0.9, 1.6, 2.5])

        with pytest.raises(ValueError):
            q.MeasurementArray([1, 2, 3, 4, 5], [0.1, 0.2, 0.3, 0.4])
        with pytest.raises(ValueError):
            q.MeasurementArray([1, 2, 3, 4, 5],
                               relative_error=[0.1, 0.2, 0.3, 0.4])
        with pytest.raises(TypeError):
            q.MeasurementArray([1, 2, 3, 4, 5], '1')
        with pytest.raises(TypeError):
            q.MeasurementArray([1, 2, 3, 4, 5], [0.1, 0.2, 0.3, 0.4, '0.5'])
        with pytest.raises(TypeError):
            q.MeasurementArray([1, 2, 3, 4, '5'])
        with pytest.raises(ValueError):
            q.MeasurementArray([1, 2, 3, 4, 5], -0.5)
        with pytest.raises(ValueError):
            q.MeasurementArray([1, 2, 3, 4, 5], [0.5, 0.5, 0.5, 0.5, -0.5])

        f = q.MeasurementArray(data=[1, 2, 3, 4],
                               error=0.5,
                               name="test",
                               unit="m")
        assert f.name == "test"
        assert f.unit == "m"
        assert str(
            f
        ) == "test = [ 1.0 +/- 0.5, 2.0 +/- 0.5, 3.0 +/- 0.5, 4.0 +/- 0.5 ] (m)"
        assert str(f[0]) == "test_0 = 1.0 +/- 0.5 [m]"
        assert str(f[-1]) == "test_3 = 4.0 +/- 0.5 [m]"

        g = q.MeasurementArray([q.Measurement(5, 0.5),
                                q.Measurement(10, 0.5)],
                               name="test",
                               unit="m")
        assert str(g[-1]) == "test_1 = 10.0 +/- 0.5 [m]"

        h = q.MeasurementArray([q.Measurement(5, 0.5),
                                q.Measurement(10, 0.5)],
                               error=0.1)
        assert str(h[-1]) == "10.0 +/- 0.1"
Exemplo n.º 3
0
    def test_trig_functions(self):
        """tests for trigonometric functions"""

        a = q.Measurement(0.7853981633974483)
        b = q.Measurement(45)
        c = q.Measurement(0.5)

        res = q.sin(a)
        assert res.value == pytest.approx(0.7071067811865475244)

        res = q.sind(b)
        assert res.value == pytest.approx(0.7071067811865475244)

        res = q.cos(a)
        assert res.value == pytest.approx(0.7071067811865475244)

        res = q.cosd(b)
        assert res.value == pytest.approx(0.7071067811865475244)

        res = q.tan(a)
        assert res.value == pytest.approx(1)

        res = q.tand(b)
        assert res.value == pytest.approx(1)

        res = q.sec(a)
        assert res.value == pytest.approx(1.4142135623730950488)

        res = q.secd(b)
        assert res.value == pytest.approx(1.4142135623730950488)

        res = q.csc(a)
        assert res.value == pytest.approx(1.4142135623730950488)

        res = q.cscd(b)
        assert res.value == pytest.approx(1.4142135623730950488)

        res = q.cot(a)
        assert res.value == pytest.approx(1)

        res = q.cotd(b)
        assert res.value == pytest.approx(1)

        res = q.asin(c)
        assert res.value == pytest.approx(0.523598775598298873077)

        res = q.acos(c)
        assert res.value == pytest.approx(1.047197551196597746154)

        res = q.atan(c)
        assert res.value == pytest.approx(0.463647609000806116214)
Exemplo n.º 4
0
    def test_value_comparison(self):
        """tests for comparing values"""

        a = q.Measurement(4, 0.5, unit="m")
        b = q.Measurement(10, 2, unit="m")
        c = q.Measurement(10, 1)

        assert a < b
        assert a <= b
        assert b >= a
        assert a > 2
        assert 3 < b
        assert a == 4
        assert 10 == b
        assert b == c
Exemplo n.º 5
0
    def test_equivalent_units(self):
        """tests for pre-defined compound units"""

        units.define_unit('N', 'kg*m/s^2')
        units.define_unit('J', 'N*m')

        a = q.Measurement(5, unit='N')
        b = q.Measurement(2, unit='J')
        c = q.Measurement(2, unit='m')

        res = a * b / c
        assert res.unit == 'N^2'

        res = a / c
        q.set_unit_style(q.UnitStyle.FRACTION)
        assert res.unit == 'kg/s^2'
Exemplo n.º 6
0
    def test_repeated_measurement_setters(self):
        """test that the setters for repeated measurements behave correctly"""

        a = q.Measurement([10, 9.8, 9.9, 10.1, 10.2],
                          [0.5, 0.3, 0.1, 0.2, 0.2])
        assert isinstance(a, RepeatedlyMeasuredValue)
        a.use_error_weighted_mean_as_value()
        assert a.error_weighted_mean == 9.971399730820997
        assert a.value == 9.971399730820997
        a.use_error_on_mean_for_uncertainty()
        assert a.error_on_mean == pytest.approx(0.070710678118654)
        assert a.error == pytest.approx(0.070710678118654)
        a.use_propagated_error_for_uncertainty()
        assert a.propagated_error == 0.0778236955614928
        assert a.error == 0.0778236955614928

        with pytest.raises(TypeError):
            a.value = '15'

        with pytest.warns(UserWarning):
            a.value = 15

        assert not isinstance(a, RepeatedlyMeasuredValue)
        assert isinstance(a, MeasuredValue)
        assert a.value == 15
Exemplo n.º 7
0
    def test_math_functions(self):
        """tests for math functions on single values"""

        a = q.Measurement(4, 0.5, unit="m")

        res = q.sqrt(a)
        assert res.value == 2
        assert res.error == 0.125
        assert res.unit == "m^(1/2)"
        assert q.sqrt(4) == 2

        with pytest.raises(UndefinedOperationError):
            q.sqrt("a")

        res = q.exp(a)
        assert res.value == pytest.approx(54.598150033144239)
        assert res.error == pytest.approx(27.299075016572120)

        res = q.log(a)
        assert res.value == pytest.approx(1.3862943611198906)
        assert res.error == 0.125

        res = q.log(2, a)
        assert res.value == 2
        assert res.error == pytest.approx(0.1803368801111204)

        res = q.log10(a)
        assert res.value == pytest.approx(0.6020599913279624)
        assert res.error == pytest.approx(0.0542868102379065)

        with pytest.raises(TypeError):
            q.log(2, a, 2)
Exemplo n.º 8
0
    def test_composite_operations(self):
        """tests combining several operations"""

        d = q.Measurement(5, 0.1, unit="m")
        m = q.Measurement(10, 0.5, unit="kg")
        t = q.Measurement(2, 0.1, unit="s")

        v = d / t
        e = 1 / 2 * m * (v**2)

        assert e.value == 31.25
        assert e.error == pytest.approx(3.7107319021993495716)
        assert e.unit == "kg⋅m^2⋅s^-2"

        res = (d**2)**(1 / 3)
        assert res.unit == "m^(2/3)"
Exemplo n.º 9
0
    def test_correlation_for_repeated_measurements(self):
        """test covariance and correlation settings between repeated measurements"""

        a = q.Measurement([0.8, 0.9, 1, 1.1])
        b = q.Measurement([2, 2.2, 2.1, 2.3])

        assert q.get_correlation(a, b) == 0
        assert q.get_covariance(a, b) == 0

        q.set_correlation(a, b)
        assert q.get_correlation(a, b) == pytest.approx(0.8)
        assert q.get_covariance(a, b) == pytest.approx(0.01333333333)

        q.set_correlation(a, b, 0)
        assert q.get_correlation(a, b) == 0
        assert q.get_covariance(a, b) == 0

        a.set_covariance(b)
        assert q.get_correlation(a, b) == pytest.approx(0.8)
        assert q.get_covariance(a, b) == pytest.approx(0.01333333333)

        q.set_covariance(a, b, 0)
        assert q.get_correlation(a, b) == 0
        assert q.get_covariance(a, b) == 0

        d = a + b
        with pytest.raises(IllegalArgumentError):
            a.get_covariance(0)
        with pytest.raises(IllegalArgumentError):
            a.get_correlation(0)
        with pytest.raises(IllegalArgumentError):
            a.set_covariance(0, 0)
        with pytest.raises(IllegalArgumentError):
            a.set_correlation(0, 0)
        with pytest.raises(IllegalArgumentError):
            a.set_covariance(d, 0)
        with pytest.raises(IllegalArgumentError):
            a.set_correlation(d, 0)

        c = q.Measurement([0, 1, 2])
        with pytest.raises(IllegalArgumentError):
            q.set_covariance(a, c)
        with pytest.raises(IllegalArgumentError):
            q.set_correlation(a, c)
Exemplo n.º 10
0
    def test_correlation_for_single_measurements(self):
        """test covariance and correlation between single measurements"""

        a = q.Measurement(5, 0.5)
        b = q.Measurement(6, 0.2)
        c = q.Measurement(5)
        d = a + b

        assert a.get_covariance(a) == 0.25
        assert a.get_correlation(a) == 1
        assert a.get_covariance(c) == 0
        assert a.get_correlation(c) == 0

        assert d.get_covariance(a) == 0
        assert d.get_correlation(a) == 0
        assert a.get_covariance(d) == 0
        assert a.get_correlation(d) == 0
        assert q.get_covariance(a, d) == 0
        assert q.get_correlation(a, d) == 0
Exemplo n.º 11
0
    def test_illegal_correlation_settings(self):
        """test illegal correlation and covariance settings"""

        a = q.Measurement(5, 0.5)
        b = q.Measurement(6, 0.2)
        c = q.Measurement(5)
        d = a + b

        with pytest.raises(IllegalArgumentError):
            q.set_correlation(a, 0, 0)
        with pytest.raises(IllegalArgumentError):
            q.set_covariance(a, 0, 0)
        with pytest.raises(IllegalArgumentError):
            a.set_correlation(0, 0)
        with pytest.raises(IllegalArgumentError):
            a.set_covariance(0, 0)
        with pytest.raises(UndefinedActionError):
            d.set_correlation(a, 0)
        with pytest.raises(UndefinedActionError):
            d.set_covariance(a, 0)
        with pytest.raises(IllegalArgumentError):
            a.set_covariance(d, 0)
        with pytest.raises(IllegalArgumentError):
            a.set_correlation(d, 0)
        with pytest.raises(ArithmeticError):
            a.set_covariance(c, 1)
        with pytest.raises(ArithmeticError):
            a.set_correlation(c, 1)

        with pytest.raises(IllegalArgumentError):
            a.get_correlation(0)
        with pytest.raises(IllegalArgumentError):
            a.get_covariance(0)
        with pytest.raises(IllegalArgumentError):
            q.get_correlation(a, 0)
        with pytest.raises(IllegalArgumentError):
            q.get_covariance(a, 0)

        with pytest.raises(ValueError):
            q.set_covariance(a, b, 100)
        with pytest.raises(ValueError):
            q.set_correlation(a, b, 2)
Exemplo n.º 12
0
    def test_manipulate_measurement_array(self):
        """tests for manipulating a measurement array"""

        a = q.MeasurementArray([1, 2, 3, 4], 0.5, name="test", unit="m")
        a = a.append(q.Measurement(5, 0.5))
        assert str(a[-1]) == "test_4 = 5.0 +/- 0.5 [m]"
        a = a.insert(1, (1.5, 0.5))
        assert str(a[1]) == "test_1 = 1.5 +/- 0.5 [m]"
        assert str(a[-1]) == "test_5 = 5.0 +/- 0.5 [m]"
        a = a.delete(1)
        assert str(a[1]) == "test_1 = 2.0 +/- 0.5 [m]"

        with pytest.raises(TypeError):
            a.name = 1
        with pytest.raises(TypeError) as e:
            a.unit = 1
        assert str(e.value) == "Cannot set unit to \"int\"!"

        a.name = "speed"
        a.unit = "m/s"
        assert a.name == "speed"
        assert a.unit == "m⋅s^-1"
        assert str(a[4]) == "speed_4 = 5.0 +/- 0.5 [m⋅s^-1]"

        a = a.append(6)
        assert str(a[5]) == "speed_5 = 6 +/- 0 [m⋅s^-1]"

        a[3] = 10
        assert str(a[3]) == "speed_3 = 10.0 +/- 0.5 [m⋅s^-1]"
        a[4] = (10, 0.6)
        assert str(a[4]) == "speed_4 = 10.0 +/- 0.6 [m⋅s^-1]"

        with pytest.raises(TypeError):
            a[2] = 'a'

        b = q.MeasurementArray([5, 6, 7], 0.5)
        b[-1] = (8, 0.5)

        a = a.append(b)
        assert str(a[-1]) == "speed_8 = 8.0 +/- 0.5 [m⋅s^-1]"

        a = a.append([8, 9, 10])
        assert str(a[-1]) == "speed_11 = 10 +/- 0 [m⋅s^-1]"
Exemplo n.º 13
0
    def test_measurement(self):
        """tests for single measurements"""

        a = q.Measurement(5)
        assert a.value == 5
        assert a.error == 0
        assert str(a) == "5 +/- 0"
        assert repr(a) == "MeasuredValue(5 +/- 0)"

        b = q.Measurement(5, 0.5)
        assert b.value == 5
        assert b.error == 0.5
        assert b.relative_error == 0.1
        assert b.std == 0.5
        assert str(b) == "5.0 +/- 0.5"
        assert repr(b) == "MeasuredValue(5.0 +/- 0.5)"

        c = q.Measurement(12.34, 0.05, name="energy", unit="kg*m^2*s^-2")
        assert str(c) == "energy = 12.34 +/- 0.05 [kg⋅m^2⋅s^-2]"

        q.set_sig_figs_for_error(2)
        assert str(c) == "energy = 12.340 +/- 0.050 [kg⋅m^2⋅s^-2]"
        q.set_sig_figs_for_value(4)
        assert str(c) == "energy = 12.34 +/- 0.05 [kg⋅m^2⋅s^-2]"
        q.set_unit_style(q.UnitStyle.FRACTION)
        assert str(c) == "energy = 12.34 +/- 0.05 [kg⋅m^2/s^2]"

        assert c.derivative(c) == 1
        assert c.derivative(b) == 0

        with pytest.raises(IllegalArgumentError):
            c.derivative(1)

        with pytest.raises(IllegalArgumentError):
            q.Measurement('12.34')

        with pytest.raises(IllegalArgumentError):
            q.Measurement(12.34, '0.05')

        with pytest.raises(TypeError):
            q.Measurement(5, unit=1)

        with pytest.raises(TypeError):
            q.Measurement(5, name=1)
Exemplo n.º 14
0
    def test_measurement_setters(self):
        """tests for changing values in a measured value"""

        a = q.Measurement(12.34, 0.05)
        a.value = 50
        assert a.value == 50
        a.error = 0.02
        assert a.error == 0.02
        a.relative_error = 0.05
        assert a.relative_error == 0.05
        assert a.error == 2.5
        a.name = "energy"
        assert a.name == "energy"
        a.unit = "kg*m^2/s^2"
        assert a.unit == "kg⋅m^2⋅s^-2"

        with pytest.raises(TypeError):
            a.value = '1'

        with pytest.raises(TypeError):
            a.error = '1'

        with pytest.raises(ValueError):
            a.error = -1

        with pytest.raises(TypeError):
            a.relative_error = '1'

        with pytest.raises(ValueError):
            a.relative_error = -1

        with pytest.raises(TypeError):
            a.name = 1

        with pytest.raises(TypeError):
            a.unit = 1
Exemplo n.º 15
0
    def test_elementary_operations(self):
        """tests for elementary arithmetic operations"""

        a = q.Measurement(4, 0.5, unit="m")
        b = q.Measurement(10, 2, unit="m")

        c = a + b
        assert c.value == 14
        assert c.error == pytest.approx(2.0615528128088303)
        assert str(c) == "14 +/- 2 [m]"

        c2 = a + 2
        assert c2.value == 6
        assert c2.error == 0.5
        assert str(c2) == "6.0 +/- 0.5 [m]"

        c3 = 5 + a
        assert c3.value == 9
        assert c3.error == 0.5

        c4 = a + (10, 2)
        assert c4.value == 14
        assert c4.error == pytest.approx(2.0615528128088303)
        assert str(c4) == "14 +/- 2"

        c5 = -a
        assert str(c5) == "-4.0 +/- 0.5 [m]"

        h = b - a
        assert h.value == 6
        assert h.error == pytest.approx(2.0615528128088303)
        assert str(h) == "6 +/- 2 [m]"

        h1 = a - 2
        assert h1.value == 2
        assert h1.error == 0.5
        assert str(h1) == "2.0 +/- 0.5 [m]"

        h2 = 5 - a
        assert h2.value == 1
        assert h2.error == 0.5

        f = q.Measurement(4, 0.5, unit="kg*m/s^2")
        d = q.Measurement(10, 2, unit="m")

        e = f * d
        assert e.value == 40
        assert e.error == pytest.approx(9.433981132056603)
        assert str(e) == "40 +/- 9 [kg⋅m^2⋅s^-2]"

        e1 = f * 2
        assert e1.value == 8
        assert str(e1) == "8 +/- 1 [kg⋅m⋅s^-2]"

        e2 = 2 * f
        assert e2.value == 8

        s = q.Measurement(10, 2, unit="m")
        t = q.Measurement(4, 0.5, unit="s")

        v = s / t
        assert v.value == 2.5
        assert v.error == pytest.approx(0.5896238207535377)
        assert str(v) == "2.5 +/- 0.6 [m⋅s^-1]"

        v1 = 20 / s
        assert v1.value == 2
        assert str(v1) == "2.0 +/- 0.4 [m^-1]"

        v2 = s / 2
        assert v2.value == 5

        with pytest.raises(UndefinedOperationError):
            s + 'a'

        k = q.Measurement(5, 0.5, unit="m")

        m = k**2
        assert str(m) == "25 +/- 5 [m^2]"

        n = 2**k
        assert n.value == 32
        assert n.error == pytest.approx(11.09035488895912495)
        assert str(n) == "30 +/- 10"
Exemplo n.º 16
0
    def test_vectorized_arithmetic(self):
        """tests for arithmetic with experimental value arrays"""

        a = q.MeasurementArray([1, 2, 3, 4, 5], 0.5, unit="s")

        res = a + 2
        assert all(res.values == [3, 4, 5, 6, 7])
        assert all(res.errors == [0.5, 0.5, 0.5, 0.5, 0.5])
        assert res.unit == "s"

        res = 2 + a
        assert all(res.values == [3, 4, 5, 6, 7])
        assert all(res.errors == [0.5, 0.5, 0.5, 0.5, 0.5])
        assert res.unit == "s"

        res = a + (2, 0.5)
        assert all(res.values == [3, 4, 5, 6, 7])

        res = (2, 0.5) + a
        assert all(res.values == [3, 4, 5, 6, 7])

        res = q.Measurement(2, 0.5) + a
        assert all(res.values == [3, 4, 5, 6, 7])

        res = a + [1, 2, 3, 4, 5]
        assert all(res.values == [2, 4, 6, 8, 10])

        res = [1, 2, 3, 4, 5] + a
        assert all(res.values == [2, 4, 6, 8, 10])

        res = a - 1
        assert all(res.values == [0, 1, 2, 3, 4])

        res = 10 - a
        assert all(res.values == [9, 8, 7, 6, 5])

        res = q.Measurement(10, 0.5) - a
        assert all(res.values == [9, 8, 7, 6, 5])

        res = a - [1, 2, 3, 4, 5]
        assert all(res.values == [0, 0, 0, 0, 0])

        res = [1, 2, 3, 4, 5] - a
        assert all(res.values == [0, 0, 0, 0, 0])

        res = a * 2
        assert all(res.values == [2, 4, 6, 8, 10])

        res = 2 * a
        assert all(res.values == [2, 4, 6, 8, 10])

        res = q.Measurement(2, 0.5) * a
        assert all(res.values == [2, 4, 6, 8, 10])

        b = q.MeasurementArray([10, 20, 30, 40, 50], 0.5, unit="m")

        res = b * a
        assert all(res.values == [10, 40, 90, 160, 250])
        assert res.unit == "m⋅s"

        res = [1, 2, 3, 4, 5] * a
        assert all(res.values == [1, 4, 9, 16, 25])

        res = a / 2
        assert all(res.values == [0.5, 1, 1.5, 2, 2.5])

        res = 2 / a
        assert all(res.values == [2, 1, 2 / 3, 2 / 4, 2 / 5])

        res = q.Measurement(2, 0.5) / a
        assert all(res.values == [2, 1, 2 / 3, 2 / 4, 2 / 5])

        res = b / a
        assert all(res.values == [10, 10, 10, 10, 10])
        assert res.unit == "m⋅s^-1"

        res = [1, 2, 3, 4, 5] / a
        assert all(res.values == [1, 1, 1, 1, 1])

        res = a**2
        assert all(res.values == [1, 4, 9, 16, 25])

        res = 2**a
        assert all(res.values == [2, 4, 8, 16, 32])

        res = q.Measurement(2, 0.5)**a
        assert all(res.values == [2, 4, 8, 16, 32])

        res = a**[2, 2, 2, 2, 2]
        assert all(res.values == [1, 4, 9, 16, 25])
        assert res.unit == "s^2"

        res = [2, 2, 2, 2, 2]**a
        assert all(res.values == [2, 4, 8, 16, 32])