-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_analysis_compute.py
More file actions
153 lines (117 loc) · 4.83 KB
/
Copy pathtest_analysis_compute.py
File metadata and controls
153 lines (117 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
"""tests.test_analysis_compute — 统计分析计算函数单测。"""
from __future__ import annotations
import numpy as np
import pandas as pd
import pytest
from lab_analysis.analysis._compute import (
classify_inflammation,
correlation_matrix_calc,
cv_stability_analysis,
descriptive_stats,
linear_regression_trend,
moving_average_analysis,
zscore_outlier_detection,
)
class TestClassifyInflammation:
def test_none_returns_unknown(self):
assert classify_inflammation(None) == "未知"
def test_high_returns_acute(self):
assert classify_inflammation(5.0) == "急性期"
def test_low_returns_remission(self):
assert classify_inflammation(0.5) == "缓解期"
def test_mid_returns_transition(self):
assert classify_inflammation(2.0) == "过渡期"
def test_boundary_acute(self):
assert classify_inflammation(3.0) == "过渡期"
def test_boundary_remission(self):
assert classify_inflammation(1.0) == "过渡期"
class TestLinearRegressionTrend:
def test_insufficient_data(self):
result = linear_regression_trend(pd.Series([1.0]))
assert result["trend"] == "数据不足"
def test_upward_trend(self):
s = pd.Series([1.0, 2.0, 3.0, 4.0, 5.0])
result = linear_regression_trend(s)
assert result["trend"] == "上升"
assert result["slope"] > 0
def test_downward_trend(self):
s = pd.Series([5.0, 4.0, 3.0, 2.0, 1.0])
result = linear_regression_trend(s)
assert result["trend"] == "下降"
def test_flat_trend(self):
s = pd.Series([3.0, 3.0, 3.0, 3.0])
result = linear_regression_trend(s)
assert result["trend"] == "平稳"
def test_has_n_points(self):
s = pd.Series([1.0, 2.0, 3.0])
result = linear_regression_trend(s)
assert result["n_points"] == 3
class TestCorrelationMatrixCalc:
def test_returns_correlations(self):
df = pd.DataFrame({"A": [1, 2, 3], "B": [2, 4, 6], "C": [6, 5, 4]})
result = correlation_matrix_calc(df, ["A", "B", "C"])
assert "A~B" in result
assert abs(result["A~B"] - 1.0) < 0.01
def test_skips_metrics_not_in_df(self):
df = pd.DataFrame({"A": [1, 2, 3]})
result = correlation_matrix_calc(df, ["A", "NONEXISTENT"])
assert result == {}
def test_returns_empty_for_single_metric(self):
df = pd.DataFrame({"A": [1, 2, 3]})
result = correlation_matrix_calc(df, ["A"])
assert result == {}
class TestDescriptiveStats:
def test_empty_series(self):
result = descriptive_stats(pd.Series([], dtype=float))
assert result["count"] == 0
def test_basic_stats(self):
result = descriptive_stats(pd.Series([1.0, 2.0, 3.0]))
assert result["count"] == 3
assert result["mean"] == 2.0
assert result["min"] == 1.0
assert result["max"] == 3.0
def test_single_value_std_zero(self):
result = descriptive_stats(pd.Series([5.0]))
assert result["std"] == 0
def test_cv(self):
result = descriptive_stats(pd.Series([2.0, 4.0, 6.0]))
assert result["cv"] is not None
assert result["cv"] > 0
class TestMovingAverageAnalysis:
def test_empty_for_window_one(self):
df = pd.DataFrame({"hs-CRP": [1, 2, 3]})
assert moving_average_analysis(df, window=1) == {}
def test_insufficient_data_skipped(self):
df = pd.DataFrame({"hs-CRP": [1]})
result = moving_average_analysis(df, window=3)
assert result == {}
def test_returns_ma_for_key_metrics(self):
df = pd.DataFrame({"hs-CRP": [1.0, 2.0, 3.0, 4.0, 5.0]})
result = moving_average_analysis(df, window=3)
assert "hs-CRP" in result
assert "moving_avg" in result["hs-CRP"]
def test_skips_missing_metric(self):
df = pd.DataFrame({"OTHER": [1, 2, 3]})
result = moving_average_analysis(df)
assert result == {}
class TestCvStabilityAnalysis:
def test_insufficient_data_skipped(self):
df = pd.DataFrame({"WBC": [5.0, 6.0]})
result = cv_stability_analysis(df)
assert result == {}
def test_stable_metric(self):
df = pd.DataFrame({"WBC": [5.0, 5.1, 5.0, 5.1, 5.0]})
result = cv_stability_analysis(df)
assert "WBC" in result
assert result["WBC"]["risk_level"] == "低"
class TestZscoreOutlierDetection:
def test_insufficient_data_skipped(self):
df = pd.DataFrame({"WBC": [5.0, 6.0]})
result = zscore_outlier_detection(df)
assert result == {}
def test_detects_outlier(self):
values = [5.0] * 20 + [999.0]
df = pd.DataFrame({"WBC": values, "report_date": pd.date_range("2026-01-01", periods=21)})
result = zscore_outlier_detection(df)
assert "WBC" in result
assert result["WBC"]["outliers_severe"]["count"] >= 1