File: test_mathfn.py

package info (click to toggle)
python-qmix 1.0.6-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,460 kB
  • sloc: python: 4,312; makefile: 215
file content (41 lines) | stat: -rw-r--r-- 1,228 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""Test the basic math functions that are included in QMix (qmix.mathfn.misc).

This includes two functions that are used to calculate basic derivatives.

"""

import pytest
import numpy as np

from qmix.mathfn.misc import slope, slope_span_n

# Test data
SLOPE = 3.
INTERCEPT = 5.
X = np.linspace(0, 10, 1001)
Y = X * SLOPE + INTERCEPT


def test_slope():
    """This function will take a simple derivative. Note that it is a centered
    derivative. For example, to find the derivative at index=10, this function
    will calculate the rise/run using the data at index=9 and index=11."""

    derivative = slope(X, Y)

    np.testing.assert_almost_equal(derivative, SLOPE, decimal=10)


def test_slope_span_n():
    """This function will take a simple derivative. Note that it is a centered
    derivative. For example, to find the derivative at index=10, this function
    will calculate the rise/run using the data at index=10-N/2 and 
    index=11+N/2 where N is the span of the derivative."""

    derivative = slope_span_n(X, Y, 11)

    np.testing.assert_almost_equal(derivative, SLOPE, decimal=10)

    # Span must be an uneven number
    with pytest.raises(AssertionError):
        derivative = slope_span_n(X, Y, 10)