-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquality.py
More file actions
214 lines (184 loc) · 7.72 KB
/
Copy pathquality.py
File metadata and controls
214 lines (184 loc) · 7.72 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
from __future__ import annotations
import pandas as pd
from .tracker import AuditReport
_DEFAULT_DIST_COLS = [
"RR", # precipitation (mm)
"TN", # min temperature (°C)
"TX", # max temperature (°C)
"TM", # mean temperature (°C)
"TAMPLI", # diurnal temperature range (°C)
"FFM", # mean wind speed (m/s)
"FXY", # max wind gust (m/s)
"PMERM", # mean sea-level pressure (hPa)
"UN", # min relative humidity (%)
"UX", # max relative humidity (%)
"INST", # sunshine duration (h)
"GLOT", # global radiation (J/cm²)
"temp_amplitude", # engineered feature
]
def check_completeness(df: pd.DataFrame, report: AuditReport) -> float:
total_cells = df.shape[0] * df.shape[1]
null_cells = int(df.isnull().sum().sum())
completeness = round(1 - null_cells / total_cells, 4)
col_completeness = (1 - df.isnull().mean()).sort_values()
report.add("overall_completeness", completeness, "quality_score",
"Non-null cells / total cells (1.0 = fully complete)")
for col in df.columns:
rate = round(float(1 - df[col].isnull().mean()), 4)
report.add(f"completeness__{col}", rate, "completeness_per_column", col)
print(
f"Overall completeness : {completeness:.2%}"
f" ({total_cells - null_cells:,} / {total_cells:,} cells non-null)"
)
print("\nLeast complete columns:")
for col, rate in col_completeness.head(5).items():
bar = "░" * int((1 - rate) * 30)
print(f" {rate:>6.1%} {bar} {col}")
print("\nMost complete columns:")
for col, rate in col_completeness.tail(5).items():
print(f" {rate:>6.1%} {col}")
return completeness
def check_distributions(
df: pd.DataFrame,
report: AuditReport,
dist_cols: list[str] | None = None,
) -> pd.DataFrame:
if dist_cols is None:
dist_cols = _DEFAULT_DIST_COLS
stat_rows = []
for col in dist_cols:
if col not in df.columns:
continue
s = df[col].dropna()
if s.empty:
continue
row = {
"column": col,
"n": len(s),
"mean": round(float(s.mean()), 3),
"std": round(float(s.std()), 3),
"min": round(float(s.min()), 3),
"p25": round(float(s.quantile(0.25)), 3),
"median": round(float(s.median()), 3),
"p75": round(float(s.quantile(0.75)), 3),
"max": round(float(s.max()), 3),
}
stat_rows.append(row)
for stat, val in row.items():
if stat == "column":
continue
report.add(f"dist__{col}__{stat}", val, "numeric_distribution", col)
stats_df = pd.DataFrame(stat_rows).set_index("column") if stat_rows else pd.DataFrame()
if not stats_df.empty:
print(stats_df.to_string())
return stats_df
def check_outliers(
df: pd.DataFrame, report: AuditReport
) -> list[tuple[str, int, float]]:
outlier_cols = [
c
for c in df.select_dtypes(include="number").columns
if c not in ("year", "month", "NUM_POSTE", "AAAAMMJJ")
]
outlier_rows: list[tuple[str, int, float]] = []
for col in outlier_cols:
s = df[col].dropna()
if len(s) < 10 or s.std() == 0:
continue
z = (s - s.mean()) / s.std()
n_out = int((z.abs() > 3).sum())
rate = round(n_out / len(df), 4)
outlier_rows.append((col, n_out, rate))
report.add(
f"outlier_rate__{col}",
rate,
"outlier_analysis",
f"{n_out} values |z|>3 out of {len(df)} rows",
)
outlier_rows.sort(key=lambda x: -x[2])
print(f'{"Column":<40} {"N outliers":>10} {"Rate":>7}')
print("─" * 65)
for col, n, rate in outlier_rows:
bar = "█" * min(int(rate * 300), 30)
print(f"{col:<40} {n:>10,} {rate:>6.2%} {bar}")
return outlier_rows
def check_validity(df: pd.DataFrame, report: AuditReport) -> dict[str, int]:
checks: dict[str, int] = {}
# Temperature consistency
if "TN" in df.columns and "TX" in df.columns:
both = df[["TN", "TX"]].dropna()
checks["tn_le_tx"] = int((both["TN"] <= both["TX"]).all())
# Precipitation non-negative
if "RR" in df.columns:
checks["rr_non_negative"] = int((df["RR"].dropna() >= 0).all())
# Wind speed non-negative
for wcol in ["FFM", "FXY", "FXI"]:
if wcol in df.columns:
checks[f"{wcol.lower()}_non_negative"] = int((df[wcol].dropna() >= 0).all())
# Humidity in [0, 100]
for hcol in ["UN", "UX", "UM"]:
if hcol in df.columns:
checks[f"{hcol.lower()}_in_0_100"] = int(
df[hcol].dropna().between(0, 100).all()
)
# Sane temperature range [-60, 60]
if "TM" in df.columns:
checks["tm_range_sane"] = int(
(df["TM"].dropna().between(-60, 60)).all()
)
# Pressure range [900, 1100] hPa
if "PMERM" in df.columns:
checks["pmerm_range_sane"] = int(
(df["PMERM"].dropna().between(900, 1100)).all()
)
# Date range
if "date" in df.columns:
checks["date_range_sane"] = int(
(df["date"] >= "1950-01-01").all() and (df["date"] <= "2026-12-31").all()
)
# No fully null rows
checks["no_fully_null_rows"] = int(df.isnull().all(axis=1).sum() == 0)
all_passed = sum(checks.values())
for k, v in checks.items():
report.add(f"validity__{k}", v, "validity_checks", "1=pass 0=fail")
report.add("validity_checks_passed", all_passed, "validity_checks",
f"{all_passed}/{len(checks)} checks passed")
report.add("validity_checks_total", len(checks), "validity_checks",
"Total validity rules evaluated")
print("Validity checks:")
for k, v in checks.items():
print(f' {"✓" if v else "✗"} {k}')
print(f"\n{all_passed}/{len(checks)} checks passed")
return checks
def check_effectiveness(df: pd.DataFrame, report: AuditReport) -> dict:
total_corrections = sum(
report.get(m)
for m in [
"values_fixed_negative",
"values_fixed_humidity_bounds",
"values_fixed_temp_consistency",
]
)
total_recovered = report.get("values_recovered_interpolation")
total_cells_final = df.shape[0] * df.shape[1]
correction_rate = round(total_corrections / max(total_cells_final, 1), 6)
initial_null = report.get("initial_null_total")
recovery_vs_null = round(total_recovered / max(initial_null, 1), 4)
report.add_many(
[
{"metric": "total_corrections", "value": total_corrections, "category": "pipeline_effectiveness", "reason": "Negative + humidity-bounds + temperature-consistency fixes"},
{"metric": "total_recovered_cells", "value": total_recovered, "category": "pipeline_effectiveness", "reason": "Cells recovered by linear interpolation within station"},
{"metric": "correction_rate_per_cell", "value": correction_rate, "category": "pipeline_effectiveness", "reason": "Corrections / total final cells — data noise density"},
{"metric": "recovery_vs_initial_null", "value": recovery_vs_null, "category": "pipeline_effectiveness", "reason": "Recovered / initial null count — interpolation coverage"},
]
)
print(f"Total corrections : {total_corrections:,}")
print(f"Total recovered : {total_recovered:,}")
print(f"Correction rate : {correction_rate:.4%} of all cells")
print(f"Recovery vs nulls : {recovery_vs_null:.1%} of initial nulls recovered by interpolation")
return {
"total_corrections": total_corrections,
"total_recovered": total_recovered,
"correction_rate": correction_rate,
"recovery_vs_null": recovery_vs_null,
}