Skip to content

Commit

Permalink
move tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-B98 committed Jan 7, 2025
1 parent 6927135 commit b5f65db
Show file tree
Hide file tree
Showing 15 changed files with 40 additions and 114 deletions.
4 changes: 1 addition & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,4 @@ repos:
rev: v1.8.0
hooks:
- id: mypy
additional_dependencies:
- pandera[mypy]
- types-PyYAML
exclude: "^tests/.*"
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ def test_preprocessor(self):
},
)

_, df = self.preprocessor.process([Dataset(DatasetType.DEMOGRAPHICS, demo_df)])[
0
]
_, df = self.preprocessor.process([Dataset(DatasetType.DEMOGRAPHICS, demo_df)])[0]

pd.testing.assert_series_equal(
df["weight"],
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ def setUp(self) -> None:
def test_abs_creatinine_aki(self):
creatinine_df = pd.DataFrame(
data={"creat": [1] * 24 + [1.3] * 23 + [6] * 23},
index=pd.period_range(
start="2023-01-01 00:00:00", end="2023-01-03 21:00:00", freq="h"
),
index=pd.period_range(start="2023-01-01 00:00:00", end="2023-01-03 21:00:00", freq="h"),
)

_type, df = self.probe.probe(
Expand All @@ -40,9 +38,7 @@ def test_abs_creatinine_aki(self):
pd.Series(
data=[0.0] * 24 + [1.0] * 23 + [3.0] * 23,
name="abs_creatinine_stage",
index=pd.period_range(
start="2023-01-01 00:00:00", end="2023-01-03 21:00:00", freq="h"
),
index=pd.period_range(start="2023-01-01 00:00:00", end="2023-01-03 21:00:00", freq="h"),
),
check_index=False,
)
Expand Down Expand Up @@ -80,9 +76,7 @@ def setUp(self) -> None:
def test_rel_creatinine_aki(self):
creatinine_df = pd.DataFrame(
data={"creat": [1] * 24 + [1.5] * 23 + [3] * 23 + [9] * 23},
index=pd.period_range(
start="2023-01-01 00:00:00", end="2023-01-04 20:00:00", freq="h"
),
index=pd.period_range(start="2023-01-01 00:00:00", end="2023-01-04 20:00:00", freq="h"),
)

_type, df = self.probe.probe(
Expand All @@ -97,9 +91,7 @@ def test_rel_creatinine_aki(self):
pd.Series(
data=[0.0] * 24 + [1.0] * 23 + [2.0] * 23 + [3.0] * 23,
name="rel_creatinine_stage",
index=pd.period_range(
start="2023-01-01 00:00:00", end="2023-01-04 20:00:00", freq="h"
),
index=pd.period_range(start="2023-01-01 00:00:00", end="2023-01-04 20:00:00", freq="h"),
),
check_index=False,
)
Expand Down Expand Up @@ -131,158 +123,121 @@ def test_validation_data(self):

class TestBaselineCreatinine(TestCase):
def test_rolling_min_baseline(self):
probe = AbstractCreatinineProbe(
baseline_timeframe="1d", method=CreatinineBaselineMethod.ROLLING_MIN
)
probe = AbstractCreatinineProbe(baseline_timeframe="1d", method=CreatinineBaselineMethod.ROLLING_MIN)
series = pd.Series(
data=[1.0] * 47 + [1.5] * 23 + [2.0] * 23,
name="creat",
index=pd.period_range(
start="2023-01-01 00:00:00", end="2023-01-04 20:00:00", freq="h"
),
index=pd.period_range(start="2023-01-01 00:00:00", end="2023-01-04 20:00:00", freq="h"),
)

self._test_helper(probe, series)

def test_rolling_first_baseline(self):
probe = AbstractCreatinineProbe(
baseline_timeframe="1d", method=CreatinineBaselineMethod.ROLLING_FIRST
)
probe = AbstractCreatinineProbe(baseline_timeframe="1d", method=CreatinineBaselineMethod.ROLLING_FIRST)

series = pd.Series(
data=[1.0] * 47 + [1.5] * 23 + [2.0] * 23,
name="creat",
index=pd.period_range(
start="2023-01-01 00:00:00", end="2023-01-04 20:00:00", freq="h"
),
index=pd.period_range(start="2023-01-01 00:00:00", end="2023-01-04 20:00:00", freq="h"),
)

self._test_helper(probe, series)

def test_rolling_mean_baseline(self):
probe = AbstractCreatinineProbe(
baseline_timeframe="1d", method=CreatinineBaselineMethod.ROLLING_MEAN
)
probe = AbstractCreatinineProbe(baseline_timeframe="1d", method=CreatinineBaselineMethod.ROLLING_MEAN)
series = pd.Series(
data=[1] * 24 + [1.5] * 23 + [2] * 23 + [3] * 23,
name="creat",
index=pd.period_range(
start="2023-01-01 00:00:00", end="2023-01-04 20:00:00", freq="h"
),
index=pd.period_range(start="2023-01-01 00:00:00", end="2023-01-04 20:00:00", freq="h"),
)
series = series.rolling(24).mean() # rolling window of 24 hours
series = series.bfill() # backwards fill
self._test_helper(probe, series)

def test_fixed_min_baseline(self):
probe = AbstractCreatinineProbe(
baseline_timeframe="1d", method=CreatinineBaselineMethod.FIXED_MIN
)
probe = AbstractCreatinineProbe(baseline_timeframe="1d", method=CreatinineBaselineMethod.FIXED_MIN)

series = pd.Series(
data=[1.0] * 93,
name="creat",
index=pd.period_range(
start="2023-01-01 00:00:00", end="2023-01-04 20:00:00", freq="h"
),
index=pd.period_range(start="2023-01-01 00:00:00", end="2023-01-04 20:00:00", freq="h"),
)

self._test_helper(probe, series)

def test_fixed_mean_baseline(self):
probe = AbstractCreatinineProbe(
baseline_timeframe="2d", method=CreatinineBaselineMethod.FIXED_MEAN
)
probe = AbstractCreatinineProbe(baseline_timeframe="2d", method=CreatinineBaselineMethod.FIXED_MEAN)
data_list = [1] * 24 + [1.5] * 23 + [2] * 23 + [3] * 23
mean_value = np.mean(data_list[:49])
series = pd.Series(
data=[mean_value] * 93,
name="creat",
index=pd.period_range(
start="2023-01-01 00:00:00", end="2023-01-04 20:00:00", freq="h"
),
index=pd.period_range(start="2023-01-01 00:00:00", end="2023-01-04 20:00:00", freq="h"),
)

self._test_helper(probe, series)

def test_overall_first_baseline(self):
probe = AbstractCreatinineProbe(
method=CreatinineBaselineMethod.OVERALL_FIRST)
probe = AbstractCreatinineProbe(method=CreatinineBaselineMethod.OVERALL_FIRST)

series = pd.Series(
data=[1.0] * 93,
name="creat",
index=pd.period_range(
start="2023-01-01 00:00:00", end="2023-01-04 20:00:00", freq="h"
),
index=pd.period_range(start="2023-01-01 00:00:00", end="2023-01-04 20:00:00", freq="h"),
)

self._test_helper(probe, series)

def test_overall_min_baseline(self):
probe = AbstractCreatinineProbe(
method=CreatinineBaselineMethod.OVERALL_MIN)
probe = AbstractCreatinineProbe(method=CreatinineBaselineMethod.OVERALL_MIN)

series = pd.Series(
data=[1.0] * 93,
name="creat",
index=pd.period_range(
start="2023-01-01 00:00:00", end="2023-01-04 20:00:00", freq="h"
),
index=pd.period_range(start="2023-01-01 00:00:00", end="2023-01-04 20:00:00", freq="h"),
)

self._test_helper(probe, series)

def test_overall_mean_baseline(self):
probe = AbstractCreatinineProbe(
method=CreatinineBaselineMethod.OVERALL_MEAN)
probe = AbstractCreatinineProbe(method=CreatinineBaselineMethod.OVERALL_MEAN)

data = [1] * 24 + [1.5] * 23 + [2] * 23 + [3] * 23
mean_value = np.mean(data)
series = pd.Series(
data=[mean_value] * 93,
name="creat",
index=pd.period_range(
start="2023-01-01 00:00:00", end="2023-01-04 20:00:00", freq="h"
),
index=pd.period_range(start="2023-01-01 00:00:00", end="2023-01-04 20:00:00", freq="h"),
)

self._test_helper(probe, series)

def test_constant_baseline(self):
probe = AbstractCreatinineProbe(
method=CreatinineBaselineMethod.CONSTANT)
probe = AbstractCreatinineProbe(method=CreatinineBaselineMethod.CONSTANT)

series = pd.Series(
data=[1.0] * 93,
name="creat",
index=pd.period_range(
start="2023-01-01 00:00:00", end="2023-01-04 20:00:00", freq="h"
),
index=pd.period_range(start="2023-01-01 00:00:00", end="2023-01-04 20:00:00", freq="h"),
)

self._test_helper(probe, series)

def test_calculated_baseline(self):
probe = AbstractCreatinineProbe(
method=CreatinineBaselineMethod.CALCULATED)
probe = AbstractCreatinineProbe(method=CreatinineBaselineMethod.CALCULATED)

series = pd.Series(
data=[2.9159636295463067] * 93,
name="creat",
index=pd.period_range(
start="2023-01-01 00:00:00", end="2023-01-04 20:00:00", freq="h"
),
index=pd.period_range(start="2023-01-01 00:00:00", end="2023-01-04 20:00:00", freq="h"),
)

self._test_helper(probe, series)

def _test_helper(self, probe, series):
creatinine_df = pd.DataFrame(
data={"creat": [1] * 24 + [1.5] * 23 + [2] * 23 + [3] * 23},
index=pd.period_range(
start="2023-01-01 00:00:00", end="2023-01-04 20:00:00", freq="h"
),
index=pd.period_range(start="2023-01-01 00:00:00", end="2023-01-04 20:00:00", freq="h"),
)

patient_df = pd.Series(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ def test_rrt_probe(self):
data={
"rrt_status": [0] * 24 + [1] * 23 + [0] * 23 + [1] * 23,
},
index=pd.period_range(
start="2023-01-01 00:00:00", end="2023-01-04 20:00:00", freq="h"
),
index=pd.period_range(start="2023-01-01 00:00:00", end="2023-01-04 20:00:00", freq="h"),
)

_, df = self.probe.probe([Dataset(DatasetType.RRT, rrt_df)])[0]
Expand All @@ -29,9 +27,7 @@ def test_rrt_probe(self):
pd.Series(
data=[0] * 24 + [3] * 23 + [0] * 23 + [3] * 23,
name="rrt_stage",
index=pd.period_range(
start="2023-01-01 00:00:00", end="2023-01-04 20:00:00", freq="h"
),
index=pd.period_range(start="2023-01-01 00:00:00", end="2023-01-04 20:00:00", freq="h"),
dtype=float,
),
check_index=False,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ def test_anuria(self):
pd.testing.assert_series_equal(
df["urineoutput_stage"],
pd.Series(
data=[np.nan, np.nan, np.nan, np.nan,
np.nan, 1, 1, 1, 1, 1, 1, 3],
data=[np.nan, np.nan, np.nan, np.nan, np.nan, 1, 1, 1, 1, 1, 1, 3],
name="urineoutput_stage",
index=pd.DatetimeIndex(
data=[
Expand Down Expand Up @@ -187,9 +186,7 @@ def test_validation_data(self):
),
Dataset(
DatasetType.DEMOGRAPHICS,
self.validation_data_unlabelled[["weight"]]
.groupby("stay_id")
.first(),
self.validation_data_unlabelled[["weight"]].groupby("stay_id").first(),
),
],
probes=[UrineOutputProbe()],
Expand All @@ -206,9 +203,7 @@ def test_validation_data(self):
def test_aki_strict(self):
urine_output_df = pd.DataFrame(
data={"urineoutput": [100] + [25] * 24},
index=pd.period_range(
start="2023-01-01 00:00:00", end="2023-01-02 00:00:00", freq="h"
),
index=pd.period_range(start="2023-01-01 00:00:00", end="2023-01-02 00:00:00", freq="h"),
)

demographics = pd.Series(data={"weight": 100})
Expand All @@ -225,9 +220,7 @@ def test_aki_strict(self):
pd.Series(
data=[np.nan] * 5 + [0] + [1] * 6 + [2] * 12 + [3],
name="urineoutput_stage",
index=pd.period_range(
start="2023-01-01 00:00:00", end="2023-01-02 00:00:00", freq="h"
),
index=pd.period_range(start="2023-01-01 00:00:00", end="2023-01-02 00:00:00", freq="h"),
dtype=float,
),
check_index=False,
Expand All @@ -236,9 +229,7 @@ def test_aki_strict(self):
def test_nan_values(self):
urine_output_df = pd.DataFrame(
data={"urineoutput": [100] * 6 + [np.nan] * 6 + [100] * 6},
index=pd.period_range(
start="2023-01-01 00:00:00", end="2023-01-01 17:00:00", freq="h"
),
index=pd.period_range(start="2023-01-01 00:00:00", end="2023-01-01 17:00:00", freq="h"),
)

demographics = pd.Series(data={"weight": 100})
Expand All @@ -262,9 +253,7 @@ def test_nan_values(self):
pd.Series(
data=[np.nan] * 5 + [0] + [np.nan] * 11 + [0],
name="urineoutput_stage",
index=pd.period_range(
start="2023-01-01 00:00:00", end="2023-01-01 17:00:00", freq="h"
),
index=pd.period_range(start="2023-01-01 00:00:00", end="2023-01-01 17:00:00", freq="h"),
dtype=float,
),
check_index=False,
Expand All @@ -275,9 +264,7 @@ def test_nan_values(self):
pd.Series(
data=[np.nan] * 5 + [0] + [np.nan] * 11 + [0],
name="urineoutput_stage",
index=pd.period_range(
start="2023-01-01 00:00:00", end="2023-01-01 17:00:00", freq="h"
),
index=pd.period_range(start="2023-01-01 00:00:00", end="2023-01-01 17:00:00", freq="h"),
dtype=float,
),
check_index=False,
Expand Down
File renamed without changes.
16 changes: 4 additions & 12 deletions test/test_analyser.py → tests/test_analyser.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ def test_validation_data(self):
data={
"rrt_status": [0] * 24 + [1] * 23 + [0] * 23 + [1] * 22 + [-1],
},
index=pd.period_range(
start="2023-01-01 00:00:00", end="2023-01-04 20:00:00", freq="h"
),
index=pd.period_range(start="2023-01-01 00:00:00", end="2023-01-04 20:00:00", freq="h"),
)

with self.assertRaises(ValueError):
Expand Down Expand Up @@ -84,18 +82,12 @@ def test_creatinine_settings(self):
DatasetType.URINEOUTPUT,
self.validation_data_unlabelled[["urineoutput"]],
),
Dataset(
DatasetType.CREATININE, self.validation_data_unlabelled[["creat"]]
),
Dataset(DatasetType.CREATININE, self.validation_data_unlabelled[["creat"]]),
Dataset(
DatasetType.DEMOGRAPHICS,
self.validation_data_unlabelled[["weight"]]
.groupby("stay_id")
.first(),
),
Dataset(
DatasetType.RRT, self.validation_data_unlabelled[["rrt_status"]]
self.validation_data_unlabelled[["weight"]].groupby("stay_id").first(),
),
Dataset(DatasetType.RRT, self.validation_data_unlabelled[["rrt_status"]]),
],
preprocessors=[],
).process_stays()
Expand Down

0 comments on commit b5f65db

Please sign in to comment.