Skip to content

Commit

Permalink
add test for codecov
Browse files Browse the repository at this point in the history
  • Loading branch information
kkappler committed Mar 23, 2024
1 parent a481bc8 commit c49a393
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 3 deletions.
16 changes: 13 additions & 3 deletions aurora/transfer_function/regression/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,22 @@ def solve_underdetermined(self):
"""
20210806
This method was originally in TRME.m, but it does not depend in
general on using RME method so I am putting it in the base class.
general on using RME method so putting it in the base class.
We basically never get here and when we do we dont trust the results
We basically never get here and when we do, we don't trust the results
https://docs.scipy.org/doc/numpy-1.9.2/reference/generated/numpy.linalg.svd.html
https://www.mathworks.com/help/matlab/ref/double.svd.html
Note that the svd outputs are different in matlab and numpy
https://stackoverflow.com/questions/50930899/svd-command-in-python-v-s-matlab
"The SVD of a matrix can be written as
A = U S V^H
Where the ^H signifies the conjugate transpose. Matlab's svd command returns U, S and V,
while numpy.linalg.svd returns U, the diagonal of S, and V^H.
Thus, to get the same S and V as in Matlab you need to reconstruct the S and also get the V:
<ORIGINAL MATLAB>
<COMMENT>
Overdetermined problem...use svd to invert, return
Expand All @@ -167,7 +177,7 @@ def solve_underdetermined(self):
"""
U, s, V = np.linalg.svd(self.X, full_matrices=False)
S_inv = np.diag(1.0 / s)
self.b = (V.T @ S_inv @ U.T) * self.Y
self.b = (V.T.conj() @ S_inv @ U.T.conj()) * self.Y
if self.iter_control.return_covariance:
logger.warning(
"Warning covariances are not xarray, may break things downstream"
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ flake8
nbsphinx
numpydoc
pre-commit
pytest
sphinx_gallery
sphinx_rtd_theme
ipython
114 changes: 114 additions & 0 deletions tests/transfer_function/regression/test_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import unittest

import numpy as np
import pandas as pd
from aurora.transfer_function.regression.base import RegressionEstimator


def make_mini_dataset(n_rows=None):
"""
TODO: Make this a pytest fixture
Parameters
----------
n_rows
Returns
-------
"""
ex_data = np.array(
[
4.39080123e-07 - 2.41097397e-06j,
-2.33418464e-06 + 2.10752581e-06j,
1.38642624e-06 - 1.87333571e-06j,
]
)
hx_data = np.array(
[
7.00767250e-07 - 9.18819198e-07j,
-1.06648904e-07 + 8.19420154e-07j,
-1.02700963e-07 - 3.73904463e-07j,
]
)

hy_data = np.array(
[
1.94321684e-07 + 3.71934877e-07j,
1.15361101e-08 - 6.32581646e-07j,
3.86095787e-08 + 4.33155345e-07j,
]
)
timestamps = pd.date_range(
start=pd.Timestamp("1977-03-02T06:00:00"), periods=len(ex_data), freq="S"
)
frequency = 0.666 * np.ones(len(ex_data))

df = pd.DataFrame(
data={
"time": timestamps,
"frequency": frequency,
"ex": ex_data,
"hx": hx_data,
"hy": hy_data,
}
)
if n_rows:
df = df.iloc[0:n_rows]
df = df.set_index(["time", "frequency"])
xr_ds = df.to_xarray()
return xr_ds


class TestRegressionBase(unittest.TestCase):
""" """

@classmethod
def setUpClass(self):
self.dataset = make_mini_dataset(n_rows=1)
self.expected_solution = np.array(
[-0.04192569 - 0.36502722j, -3.65284496 - 4.05194938j]
)

def setUp(self):
pass

def test_regression(self):
dataset = make_mini_dataset()
X = dataset[["hx", "hy"]]
X = X.stack(observation=("frequency", "time"))
Y = dataset[
[
"ex",
]
]
Y = Y.stack(observation=("frequency", "time"))
re = RegressionEstimator(X=X, Y=Y)
re.estimate_ols()
difference = re.b - np.atleast_2d(self.expected_solution).T
assert np.isclose(difference, 0).all()
re.estimate()
difference = re.b - np.atleast_2d(self.expected_solution).T
assert np.isclose(difference, 0).all()

def test_underdetermined_regression(self):
""" """
dataset = make_mini_dataset(n_rows=1)
X = dataset[["hx", "hy"]]
X = X.stack(observation=("frequency", "time"))
Y = dataset[
[
"ex",
]
]
Y = Y.stack(observation=("frequency", "time"))
re = RegressionEstimator(X=X, Y=Y)
re.solve_underdetermined()
assert re.b is not None


def main():
unittest.main()

Check warning on line 110 in tests/transfer_function/regression/test_base.py

View check run for this annotation

Codecov / codecov/patch

tests/transfer_function/regression/test_base.py#L110

Added line #L110 was not covered by tests


if __name__ == "__main__":
main()

Check warning on line 114 in tests/transfer_function/regression/test_base.py

View check run for this annotation

Codecov / codecov/patch

tests/transfer_function/regression/test_base.py#L114

Added line #L114 was not covered by tests

0 comments on commit c49a393

Please sign in to comment.