Files
prog-team-proj/utilsv2/stats.py

84 lines
2.3 KiB
Python
Raw Permalink Normal View History

2026-01-04 18:51:36 -01:00
import time
2026-01-04 19:26:19 -01:00
from datetime import datetime
2026-01-04 18:51:36 -01:00
import numpy as np
def print_filters(filters):
_res = ""
for k, v in filters.items():
_res += f"{k}: {v}\n"
2026-01-04 19:26:19 -01:00
def pprint(v):
2026-01-05 14:47:49 -01:00
return f"\tMédia: {v[0]} \u00b1 {v[1]}; 1o Quartil: {v[3]}; Mediana: {v[2]}; 3o Quartil: {v[4]}; Máximo: {v[5]}; Mínimo: {v[6]}"
2026-01-04 18:51:36 -01:00
2026-01-04 19:26:19 -01:00
def stats(data):
_stats = f"===Estatística==\nNúmero total de eventos: {len(data)}\n"
aux = []
currMonth: datetime = data[0]["DateTime"]
idx = 0
while idx < len(data):
if data[idx]["DateTime"].month == currMonth.month:
aux.append(data[idx])
idx += 1
else:
m = calc_mag(aux)
d = calc_depth(aux)
aux = []
2026-01-05 14:47:49 -01:00
_stats += f"{currMonth.strftime('%Y-%m')}:\n\tMagnitude: {pprint(m)}\n\tProfundidade: {pprint(d)}\n\n"
2026-01-04 19:26:19 -01:00
currMonth = data[idx]["DateTime"]
m = calc_mag(aux)
d = calc_depth(aux)
_stats += f"{currMonth.strftime('%Y-%m')}:\nMagnitude: {pprint(m)}\nProfundidade: {pprint(d)}\n"
2026-01-04 18:51:36 -01:00
2026-01-04 19:26:19 -01:00
fname = f"stats-{time.time_ns()}.txt"
with open(fname, "wb") as fp:
fp.write(_stats.encode("utf-8"))
2026-01-04 18:51:36 -01:00
2026-01-04 19:26:19 -01:00
# print(_stats)
2026-01-04 18:51:36 -01:00
def calc_depth(data):
if len(data) == 0:
return 0
depths = np.array([v["Depth"] for v in data], dtype=float)
2026-01-05 14:47:49 -01:00
quantile = np.quantile(depths, [0.25, 0.75])
2026-01-04 19:26:19 -01:00
return list(
map(
float,
(
round(np.average(depths), 3),
round(np.std(depths), 3),
round(np.median(depths), 3),
2026-01-05 14:47:49 -01:00
round(quantile[0], 3),
round(quantile[1], 3),
2026-01-04 19:26:19 -01:00
np.min(depths),
np.max(depths),
),
)
2026-01-04 18:51:36 -01:00
)
def calc_mag(data):
if len(data) == 0:
return 0
mags = np.array([v["Magnitudes"]["L"]["Magnitude"] for v in data], dtype=float)
2026-01-05 14:47:49 -01:00
quantile = np.quantile(mags, [0.25, 0.75])
2026-01-04 19:26:19 -01:00
return list(
map(
float,
(
round(np.average(mags), 3),
round(np.std(mags), 3),
round(np.median(mags), 3),
2026-01-05 14:47:49 -01:00
round(quantile[0], 3),
round(quantile[1], 3),
2026-01-04 19:26:19 -01:00
np.min(mags),
np.max(mags),
),
)
)