From 18128708afa35bf2ccb6a58ecb2f4dc7b861a26a Mon Sep 17 00:00:00 2001 From: Austin Raney Date: Wed, 21 Aug 2024 16:54:04 -0400 Subject: [PATCH 1/5] refactor: update type hints, remove unused variables, etc. --- .../tests/test_calibration_catchment.py | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/python/ngen_cal/tests/test_calibration_catchment.py b/python/ngen_cal/tests/test_calibration_catchment.py index d13ee276..f0f52072 100644 --- a/python/ngen_cal/tests/test_calibration_catchment.py +++ b/python/ngen_cal/tests/test_calibration_catchment.py @@ -1,41 +1,41 @@ +""" +Test suite for calibratable_catchment +""" + +from __future__ import annotations + import pytest from typing import TYPE_CHECKING if TYPE_CHECKING: from ngen.cal.calibration_cathment import CalibrationCatchment -""" - Test suite for calibratable_catchment -""" -@pytest.mark.usefixtures("catchment") -def test_df(catchment: 'CalibrationCatchment') -> None: +def test_df(catchment: CalibrationCatchment) -> None: """ - Test the catchments proper construction of the parameter dataframe + Test the catchments proper construction of the parameter dataframe """ assert catchment.df.iloc[0]['param'] == 'some_param' assert catchment.df.iloc[0]['0'] == 0.5 assert catchment.df.iloc[0]['min'] == 0.0 assert catchment.df.iloc[0]['max'] == 1.0 -@pytest.mark.usefixtures("catchment2") -def test_output(catchment2: 'CalibrationCatchment', monkeypatch) -> None: +def test_output(catchment2: CalibrationCatchment, monkeypatch: pytest.MonkeyPatch) -> None: """ - Test proper handling of non-existent output + Test proper handling of non-existent output """ import pandas as pd monkeypatch.setattr(pd, "read_csv", lambda *args, **kwargs: FileNotFoundError()) output = catchment2.output - assert output == None + assert output is None -@pytest.mark.usefixtures("catchment") -def test_observed(catchment: 'CalibrationCatchment') -> None: +def test_observed(catchment: CalibrationCatchment) -> None: """ - Test proper handling of non-existent output + Test proper handling of non-existent output """ catchment.observed = None with pytest.raises(RuntimeError): - obs = catchment.observed + catchment.observed #TODO test catchment_set #TODO test evaluation_range? From 0407eb9961acb53046c7c0ccc6d5e354873ccdf1 Mon Sep 17 00:00:00 2001 From: Austin Raney Date: Wed, 21 Aug 2024 16:54:46 -0400 Subject: [PATCH 2/5] style: formatting --- .../tests/test_calibration_catchment.py | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/python/ngen_cal/tests/test_calibration_catchment.py b/python/ngen_cal/tests/test_calibration_catchment.py index f0f52072..f0ad99e8 100644 --- a/python/ngen_cal/tests/test_calibration_catchment.py +++ b/python/ngen_cal/tests/test_calibration_catchment.py @@ -4,9 +4,10 @@ from __future__ import annotations -import pytest from typing import TYPE_CHECKING +import pytest + if TYPE_CHECKING: from ngen.cal.calibration_cathment import CalibrationCatchment @@ -15,20 +16,26 @@ def test_df(catchment: CalibrationCatchment) -> None: """ Test the catchments proper construction of the parameter dataframe """ - assert catchment.df.iloc[0]['param'] == 'some_param' - assert catchment.df.iloc[0]['0'] == 0.5 - assert catchment.df.iloc[0]['min'] == 0.0 - assert catchment.df.iloc[0]['max'] == 1.0 + assert catchment.df.iloc[0]["param"] == "some_param" + assert catchment.df.iloc[0]["0"] == 0.5 + assert catchment.df.iloc[0]["min"] == 0.0 + assert catchment.df.iloc[0]["max"] == 1.0 + -def test_output(catchment2: CalibrationCatchment, monkeypatch: pytest.MonkeyPatch) -> None: +def test_output( + catchment2: CalibrationCatchment, + monkeypatch: pytest.MonkeyPatch, +) -> None: """ Test proper handling of non-existent output """ import pandas as pd + monkeypatch.setattr(pd, "read_csv", lambda *args, **kwargs: FileNotFoundError()) output = catchment2.output assert output is None + def test_observed(catchment: CalibrationCatchment) -> None: """ Test proper handling of non-existent output @@ -37,5 +44,6 @@ def test_observed(catchment: CalibrationCatchment) -> None: with pytest.raises(RuntimeError): catchment.observed -#TODO test catchment_set -#TODO test evaluation_range? + +# TODO test catchment_set +# TODO test evaluation_range? From 407e21791c4eac1a671ef487a1cb230c3eda78eb Mon Sep 17 00:00:00 2001 From: Austin Raney Date: Thu, 22 Aug 2024 09:39:04 -0400 Subject: [PATCH 3/5] chore(typing): fix return type hint --- python/ngen_cal/tests/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/ngen_cal/tests/conftest.py b/python/ngen_cal/tests/conftest.py index 678b1cf8..32c06a48 100644 --- a/python/ngen_cal/tests/conftest.py +++ b/python/ngen_cal/tests/conftest.py @@ -175,7 +175,7 @@ def catchment(nexus, fabric, workdir, mocker) -> Generator[CalibrationCatchment, return catchment @pytest.fixture -def catchment2(nexus, fabric, workdir) -> Generator[CalibrationCatchment, None, None]: +def catchment2(nexus, fabric, workdir) -> CalibrationCatchment: """ A hy_features catchment implementing the calibratable interface Doesn't mock output, can be used to test semantics of erronous output From 1bb2a5f45ee68dfd0ad85a1284337b3ab177091c Mon Sep 17 00:00:00 2001 From: Austin Raney Date: Thu, 22 Aug 2024 09:39:52 -0400 Subject: [PATCH 4/5] chore: use canonical pandas import style --- python/ngen_cal/src/ngen/cal/calibration_cathment.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/python/ngen_cal/src/ngen/cal/calibration_cathment.py b/python/ngen_cal/src/ngen/cal/calibration_cathment.py index 601f2814..7b46c9a3 100644 --- a/python/ngen_cal/src/ngen/cal/calibration_cathment.py +++ b/python/ngen_cal/src/ngen/cal/calibration_cathment.py @@ -1,11 +1,10 @@ from __future__ import annotations -from pandas import DataFrame, read_csv # type: ignore +import pandas as pd import shutil from typing import TYPE_CHECKING if TYPE_CHECKING: - from pandas import DataFrame from pathlib import Path from geopandas import GeoSeries from datetime import datetime @@ -33,7 +32,7 @@ def __init__(self, workdir: Path, id: str, nexus, params: dict = {}): """ FormulatableCatchment.__init__(self=self, catchment_id=id, params=params, outflow=nexus) - Adjustable.__init__(self=self, df=DataFrame(params).rename(columns={'init': '0'})) + Adjustable.__init__(self=self, df=pd.DataFrame(params).rename(columns={'init': '0'})) #FIXME paramterize self._output_file = workdir/f'{self.id}.csv' self._workdir = workdir @@ -86,7 +85,7 @@ def evaluation_range(self) -> tuple[datetime, datetime] | None: return self._eval_range @property - def output(self) -> DataFrame: + def output(self) -> pd.DataFrame: """ The model output hydrograph for this catchment This re-reads the output file each call, as the output for given calibration catchment changes @@ -94,7 +93,7 @@ def output(self) -> DataFrame: """ try: #FIXME get the output variable from config - self._output = read_csv(self._output_file, usecols=["Time", self._output_var], parse_dates=['Time'], index_col='Time', dtype={self._output_var: 'float64'}) + self._output = pd.read_csv(self._output_file, usecols=["Time", self._output_var], parse_dates=['Time'], index_col='Time', dtype={self._output_var: 'float64'}) self._output.rename(columns={self._output_var:'sim_flow'}, inplace=True) #FIXME make sure units are correct here... #Assumes model catchment outputs are in m/hr, convert to m^3/s @@ -113,7 +112,7 @@ def output(self, df): self._output = df @property - def observed(self) -> DataFrame: + def observed(self) -> pd.DataFrame: """ The observed hydrograph for this catchment FIXME move output/observed to calibratable? """ From afb30b9f96ad2f4070e10d608e708a71294c9f96 Mon Sep 17 00:00:00 2001 From: Austin Raney Date: Thu, 22 Aug 2024 09:40:37 -0400 Subject: [PATCH 5/5] test: fix monkeypatch; fixes #166 --- python/ngen_cal/tests/test_calibration_catchment.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/python/ngen_cal/tests/test_calibration_catchment.py b/python/ngen_cal/tests/test_calibration_catchment.py index f0ad99e8..6e41b6fd 100644 --- a/python/ngen_cal/tests/test_calibration_catchment.py +++ b/python/ngen_cal/tests/test_calibration_catchment.py @@ -31,7 +31,10 @@ def test_output( """ import pandas as pd - monkeypatch.setattr(pd, "read_csv", lambda *args, **kwargs: FileNotFoundError()) + def file_not_found(*args, **kwargs): + raise FileNotFoundError + + monkeypatch.setattr(pd, "read_csv", file_not_found) output = catchment2.output assert output is None