|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +"""UNIT TESTS FOR CALC SUBPACKAGE. |
| 4 | +
|
| 5 | +This module contains unit tests for the calc subpackage. |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +import os |
| 10 | + |
| 11 | +import numpy as np |
| 12 | +from numpy import testing as npt |
| 13 | + |
| 14 | +from unittest import TestCase |
| 15 | + |
| 16 | +from cs_util import calc |
| 17 | + |
| 18 | + |
| 19 | +class CatTestCase(TestCase): |
| 20 | + """Test case for the ``cat`` module.""" |
| 21 | + |
| 22 | + def setUp(self): |
| 23 | + """Set test parameter values.""" |
| 24 | + self._x = np.array([1, 2, 3, 2, 4, 0.5, 1, 2, 3]) |
| 25 | + self._w = np.array([1, 0.5, 0.8, 0.9, 1.2, 1.5, 2, 3, 1.2]) |
| 26 | + self._mean_w = 1.929752067 |
| 27 | + self._std_w_uncor = 1.041109121 |
| 28 | + self._std_w_cor = 1.104262979 |
| 29 | + |
| 30 | + def tearDown(self): |
| 31 | + """Unset test parameter values.""" |
| 32 | + self._x = None |
| 33 | + self._w = None |
| 34 | + |
| 35 | + def test_weighted_avg_and_std(self): |
| 36 | + """Test ``cs_util.calc.weighted_avg_and_std`` method. |
| 37 | +
|
| 38 | + """ |
| 39 | + mean_w, std_w_uncor = calc.weighted_avg_and_std(self._x, self._w) |
| 40 | + npt.assert_almost_equal(mean_w, self._mean_w, err_msg='weighted means differ') |
| 41 | + npt.assert_almost_equal( |
| 42 | + std_w_uncor, |
| 43 | + self._std_w_uncor, |
| 44 | + err_msg='uncorrected weighted std differ', |
| 45 | + ) |
| 46 | + |
| 47 | + mean_w, std_w_cor = calc.weighted_avg_and_std( |
| 48 | + self._x, |
| 49 | + self._w, |
| 50 | + corrected=True |
| 51 | + ) |
| 52 | + npt.assert_almost_equal(mean_w, self._mean_w, err_msg='weighted means differ') |
| 53 | + npt.assert_almost_equal( |
| 54 | + std_w_cor, |
| 55 | + self._std_w_cor, |
| 56 | + err_msg='corrected weighted std differ', |
| 57 | + ) |
0 commit comments