Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pytest pytest-asyncio openai python-dotenv Pillow matplotlib pandas aiohttp
pip install pytest pytest-asyncio openai python-dotenv Pillow matplotlib pandas aiohttp scikit-learn
pip install -e .

- name: Run tests
env:
OPENAI_API_KEY: "test-key-not-real"
MPLBACKEND: Agg
run: pytest tests/ -v
13 changes: 7 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import sys
from unittest.mock import MagicMock

# Mock packages not available in CI (private or heavy scientific deps)
# Must be first: mocks must be in sys.modules before any lars import chain
# is triggered (e.g. by patch() resolving a target string).
for _mod in [
"asksageclient",
"xradar",
"cmweather",
"pip_system_certs",
"sklearn",
"sklearn.metrics",
"sklearn.preprocessing",
"torch",
"torchvision",
]:
sys.modules[_mod] = MagicMock()

# Must be before pyplot is imported; on headless runners the default backend
# may require a display (Tk, Qt) and raise on import.
import matplotlib
matplotlib.use("Agg")
102 changes: 102 additions & 0 deletions tests/test_confusion_matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import io
import pytest
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

CSV_DATA = """\
label,llm_label
stratiform,stratiform
convective,convective
stratiform,convective
convective,stratiform
anvil,anvil
stratiform,stratiform
"""


@pytest.fixture
def sample_df():
return pd.read_csv(io.StringIO(CSV_DATA))


@pytest.fixture(autouse=True)
def close_figures():
yield
plt.close("all")


def test_title_is_set(sample_df):
from lars.util.confusion_matrix import plot_confusion_matrix

_, ax = plt.subplots()
plot_confusion_matrix(sample_df, ax=ax)
assert ax.get_title() == "Confusion Matrix"


def test_returns_none(sample_df):
from lars.util.confusion_matrix import plot_confusion_matrix

_, ax = plt.subplots()
result = plot_confusion_matrix(sample_df, ax=ax)
assert result is None


def test_normalized(sample_df):
from lars.util.confusion_matrix import plot_confusion_matrix

_, ax = plt.subplots()
plot_confusion_matrix(sample_df, normalize="true", ax=ax)
assert ax.get_title() == "Confusion Matrix"


def test_custom_column_names(sample_df):
from lars.util.confusion_matrix import plot_confusion_matrix

df = sample_df.rename(columns={"label": "true", "llm_label": "pred"})
_, ax = plt.subplots()
plot_confusion_matrix(df, label_col="true", pred_col="pred", ax=ax)
assert ax.get_title() == "Confusion Matrix"


def test_uses_gca_when_no_ax(sample_df):
from lars.util.confusion_matrix import plot_confusion_matrix

fig, ax = plt.subplots()
plt.sca(ax)
plot_confusion_matrix(sample_df)
assert ax.get_title() == "Confusion Matrix"


def test_confusion_matrix_counts(sample_df):
"""Labels sorted alphabetically: anvil=0, convective=1, stratiform=2.

True \\ Pred anvil convective stratiform
anvil 1 0 0
convective 0 1 1
stratiform 0 1 2
"""
from lars.util.confusion_matrix import plot_confusion_matrix

expected = np.array([[1, 0, 0],
[0, 1, 1],
[0, 1, 2]])

_, ax = plt.subplots()
plot_confusion_matrix(sample_df, ax=ax)
actual = ax.images[0].get_array()
np.testing.assert_array_equal(actual, expected)


def test_normalized_confusion_matrix_values(sample_df):
"""Row-normalized values for each true class."""
from lars.util.confusion_matrix import plot_confusion_matrix

expected = np.array([[1.0, 0.0, 0.0 ],
[0.0, 0.5, 0.5 ],
[0.0, 1.0 / 3.0, 2.0 / 3.0]])

_, ax = plt.subplots()
plot_confusion_matrix(sample_df, normalize="true", ax=ax)
actual = ax.images[0].get_array()
np.testing.assert_array_almost_equal(actual, expected)
Loading