Files
dataset/softwares/test/meteorologia_aeroportos/_diag_outliers.py
2026-05-31 10:32:59 -03:00

41 lines
1.5 KiB
Python

"""Diagnóstico de outliers no banco SQLite."""
import sys; sys.path.insert(0, ".")
from pathlib import Path
import sqlite3
import pandas as pd
conn = sqlite3.connect("dados/met.db")
LIMITS = {"T": (-25, 55), "Td": (-30, 40), "UR": (0, 100),
"QNH": (940, 1060), "WS": (0, 200), "WG": (0, 250),
"WD": (0, 360), "VIS": (0, 9999), "TETO": (0, 9999), "PREC": (0, 500)}
print("=== Outliers por variável (fora dos limites físicos) ===\n")
total = conn.execute("SELECT COUNT(*) FROM observations").fetchone()[0]
print(f"Total observações: {total:,}\n")
for col, (lo, hi) in LIMITS.items():
rows = conn.execute(f"""
SELECT aerodrome, dt, {col}
FROM observations
WHERE {col} IS NOT NULL AND ({col} < {lo} OR {col} > {hi})
ORDER BY ABS({col}) DESC LIMIT 10
""").fetchall()
if rows:
print(f"--- {col} (limite: {lo} a {hi}) ---")
for r in rows:
print(f" {r[0]} {r[1]} {col}={r[2]}")
cnt = conn.execute(f"SELECT COUNT(*) FROM observations WHERE {col} < {lo} OR {col} > {hi}").fetchone()[0]
print(f" Total fora do limite: {cnt:,} ({cnt/total*100:.2f}%)\n")
# Estatísticas básicas
print("\n=== Estatísticas básicas ===\n")
cols = ", ".join([f"MIN({c}) as min_{c}, MAX({c}) as max_{c}, AVG({c}) as avg_{c}" for c in LIMITS])
row = conn.execute(f"SELECT {cols} FROM observations").fetchone()
keys = [f"{fn}_{c}" for c in LIMITS for fn in ("min","max","avg")]
for k, v in zip(keys, row):
if v is not None:
print(f" {k}: {v:.2f}")
conn.close()