diff --git a/ev2.py b/ev2.py index d71969a..c3eca12 100644 --- a/ev2.py +++ b/ev2.py @@ -9,7 +9,6 @@ from utilsv2.utils import toDateTime, toFloat, toInt OS = os.name - MAIN_MENU = { "1": "Adicionar novos dados", "2": "Aplicar filtros", @@ -140,7 +139,7 @@ def main(): case "3": print(filters) v = mongo.filter_query(cli, "quakes", filters, "test") - stats.calculate_stats(v, filters) + stats.stats(v) input() case "q": diff --git a/utilsv2/stats.py b/utilsv2/stats.py index 5711381..a626308 100644 --- a/utilsv2/stats.py +++ b/utilsv2/stats.py @@ -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), + ), + ) + )