Skip to content

Commit

Permalink
convert to pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
leoschwarz committed Jun 28, 2024
1 parent b033d58 commit 3777b76
Showing 1 changed file with 52 additions and 45 deletions.
97 changes: 52 additions & 45 deletions tests/unit/calibration/models/test_linear_model.py
Original file line number Diff line number Diff line change
@@ -1,63 +1,70 @@
import unittest
from functools import cached_property

import numpy as np
import pytest

from depiction.calibration.models.linear_model import LinearModel


class TestLinearModel(unittest.TestCase):
def setUp(self) -> None:
self.mock_coef = [1, 2]
@pytest.fixture
def mock_coef() -> list[float]:
return [1, 2]


@pytest.fixture
def mock_model(mock_coef: list[float]) -> LinearModel:
return LinearModel(coef=mock_coef)


def test_coef(mock_model: LinearModel) -> None:
np.testing.assert_array_equal(np.array([1, 2]), mock_model.coef, strict=True)


def test_raise_error_when_invalid_coef() -> None:
with pytest.raises(ValueError):
LinearModel(coef=[1, 2, 3])


def test_is_zero_when_false(mock_model: LinearModel) -> None:
assert not mock_model.is_zero


def test_is_zero_when_true(mock_model: LinearModel) -> None:
mock_coef = [0.0, 0]
assert LinearModel(coef=mock_coef).is_zero

@cached_property
def mock_model(self):
return LinearModel(coef=self.mock_coef)

def test_coef(self) -> None:
np.testing.assert_array_equal(np.array([1, 2]), self.mock_model.coef, strict=True)
def test_predict(mock_model: LinearModel) -> None:
np.testing.assert_array_equal(
np.array([3.0, 5]),
mock_model.predict([1.0, 2]),
)

def test_raise_error_when_invalid_coef(self) -> None:
with self.assertRaises(ValueError):
LinearModel(coef=[1, 2, 3])

def test_is_zero_when_false(self) -> None:
self.assertFalse(self.mock_model.is_zero)
def test_identity() -> None:
np.testing.assert_array_equal(
np.array([0, 1]),
LinearModel.identity().coef,
)

def test_is_zero_when_true(self) -> None:
self.assertTrue(LinearModel.zero().is_zero)
self.mock_coef = [0.0, 0]
self.assertTrue(self.mock_model.is_zero)

def test_predict(self) -> None:
np.testing.assert_array_equal(
np.array([3.0, 5]),
self.mock_model.predict([1.0, 2]),
)
def test_zero() -> None:
np.testing.assert_array_equal(
np.array([0, 0]),
LinearModel.zero().coef,
)

def test_identity(self) -> None:
np.testing.assert_array_equal(
np.array([0, 1]),
LinearModel.identity().coef,
)

def test_zero(self) -> None:
np.testing.assert_array_equal(
np.array([0, 0]),
LinearModel.zero().coef,
)
def test_fit_lsq() -> None:
model = LinearModel.fit_lsq(np.array([1, 2, 3]), np.array([4, 5, 6]))
assert model.intercept == pytest.approx(3, abs=1e-7)
assert model.slope == pytest.approx(1, abs=1e-7)

def test_fit_lsq(self) -> None:
model = LinearModel.fit_lsq(np.array([1, 2, 3]), np.array([4, 5, 6]))
self.assertAlmostEqual(3, model.intercept, places=7)
self.assertAlmostEqual(1, model.slope, places=7)

def test_fit_linear_siegelslopes(self) -> None:
mock_x = np.array([100, 200, 300])
mock_y = np.array([1, 2, 3])
model = LinearModel.fit_siegelslopes(mock_x, mock_y)
np.testing.assert_array_almost_equal(np.array([0, 0.01]), model.coef, decimal=7)
def test_fit_linear_siegelslopes() -> None:
mock_x = np.array([100, 200, 300])
mock_y = np.array([1, 2, 3])
model = LinearModel.fit_siegelslopes(mock_x, mock_y)
np.testing.assert_array_almost_equal(np.array([0, 0.01]), model.coef, decimal=7)


if __name__ == "__main__":
unittest.main()
pytest.main()

0 comments on commit 3777b76

Please sign in to comment.