Fix nas estatísticas

This commit is contained in:
2026-01-04 19:26:19 -01:00
parent c275bc7065
commit 9276ff0c47
2 changed files with 50 additions and 22 deletions

View File

@@ -1,4 +1,5 @@
import time
from datetime import datetime
import numpy as np
from pymongo import MongoClient
@@ -10,35 +11,52 @@ def print_filters(filters):
_res += f"{k}: {v}\n"
def calculate_stats(data: list, filters):
_stats_txt = "Estatísticas\n"
_stats_txt += f"Número de eventos: {len(data)}\n"
_res = calc_mag(data)
_stats_txt += f"Magnitudes:\n\tMédia: {_res[0]} \u00b1 {_res[1]}\n\tMediana: {_res[2]}\n\tValor Mínimo: {_res[3]}\n\tValor Máximo: {_res[4]}\n"
_res = calc_depth(data)
_stats_txt += f"\nProfundidade:\n\tMédia: {_res[0]} \u00b1 {_res[1]}\n\tMediana: {_res[2]}\n\tValor Mínimo: {_res[3]}\n\tValor Máximo: {_res[4]}\n"
def pprint(v):
return f"\tMédia: {v[0]} \u00b1 {v[1]}; Mediana: {v[2]}; Máximo: {v[4]}; Mínimo: {v[3]}"
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 = []
_stats += f"{currMonth.strftime('%Y-%m')}:\n\tMagnitude: {pprint(m)}\n\tProfundidade: {pprint(d)}\n"
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"
fname = f"stats-{time.time_ns()}.txt"
with open(fname, "wb") as fp:
fp.write(_stats_txt.encode("utf-8"))
fp.write(_stats.encode("utf-8"))
print(_stats_txt)
def get_data(client: MongoClient):
pass
# print(_stats)
def calc_depth(data):
if len(data) == 0:
return 0
depths = np.array([v["Depth"] for v in data], dtype=float)
return (
np.average(depths),
np.std(depths),
np.median(depths),
np.min(depths),
np.max(depths),
return list(
map(
float,
(
round(np.average(depths), 3),
round(np.std(depths), 3),
round(np.median(depths), 3),
np.min(depths),
np.max(depths),
),
)
)
@@ -46,4 +64,15 @@ def calc_mag(data):
if len(data) == 0:
return 0
mags = np.array([v["Magnitudes"]["L"]["Magnitude"] for v in data], dtype=float)
return (np.average(mags), np.std(mags), np.median(mags), np.min(mags), np.max(mags))
return list(
map(
float,
(
round(np.average(mags), 3),
round(np.std(mags), 3),
round(np.median(mags), 3),
np.min(mags),
np.max(mags),
),
)
)