Fix nas estatísticas
This commit is contained in:
3
ev2.py
3
ev2.py
@@ -9,7 +9,6 @@ from utilsv2.utils import toDateTime, toFloat, toInt
|
|||||||
|
|
||||||
OS = os.name
|
OS = os.name
|
||||||
|
|
||||||
|
|
||||||
MAIN_MENU = {
|
MAIN_MENU = {
|
||||||
"1": "Adicionar novos dados",
|
"1": "Adicionar novos dados",
|
||||||
"2": "Aplicar filtros",
|
"2": "Aplicar filtros",
|
||||||
@@ -140,7 +139,7 @@ def main():
|
|||||||
case "3":
|
case "3":
|
||||||
print(filters)
|
print(filters)
|
||||||
v = mongo.filter_query(cli, "quakes", filters, "test")
|
v = mongo.filter_query(cli, "quakes", filters, "test")
|
||||||
stats.calculate_stats(v, filters)
|
stats.stats(v)
|
||||||
input()
|
input()
|
||||||
|
|
||||||
case "q":
|
case "q":
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import time
|
import time
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from pymongo import MongoClient
|
from pymongo import MongoClient
|
||||||
@@ -10,35 +11,52 @@ def print_filters(filters):
|
|||||||
_res += f"{k}: {v}\n"
|
_res += f"{k}: {v}\n"
|
||||||
|
|
||||||
|
|
||||||
def calculate_stats(data: list, filters):
|
def pprint(v):
|
||||||
_stats_txt = "Estatísticas\n"
|
return f"\tMédia: {v[0]} \u00b1 {v[1]}; Mediana: {v[2]}; Máximo: {v[4]}; Mínimo: {v[3]}"
|
||||||
_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"
|
def stats(data):
|
||||||
_res = calc_depth(data)
|
_stats = f"===Estatística==\nNúmero total de eventos: {len(data)}\n"
|
||||||
_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"
|
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"
|
fname = f"stats-{time.time_ns()}.txt"
|
||||||
with open(fname, "wb") as fp:
|
with open(fname, "wb") as fp:
|
||||||
fp.write(_stats_txt.encode("utf-8"))
|
fp.write(_stats.encode("utf-8"))
|
||||||
|
|
||||||
print(_stats_txt)
|
# print(_stats)
|
||||||
|
|
||||||
|
|
||||||
def get_data(client: MongoClient):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def calc_depth(data):
|
def calc_depth(data):
|
||||||
if len(data) == 0:
|
if len(data) == 0:
|
||||||
return 0
|
return 0
|
||||||
depths = np.array([v["Depth"] for v in data], dtype=float)
|
depths = np.array([v["Depth"] for v in data], dtype=float)
|
||||||
return (
|
return list(
|
||||||
np.average(depths),
|
map(
|
||||||
np.std(depths),
|
float,
|
||||||
np.median(depths),
|
(
|
||||||
np.min(depths),
|
round(np.average(depths), 3),
|
||||||
np.max(depths),
|
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:
|
if len(data) == 0:
|
||||||
return 0
|
return 0
|
||||||
mags = np.array([v["Magnitudes"]["L"]["Magnitude"] for v in data], dtype=float)
|
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),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user