Mais coisas, estatisticas

This commit is contained in:
2026-01-04 18:51:36 -01:00
parent 1d1826ebe5
commit c275bc7065
6 changed files with 262 additions and 29 deletions

17
utilsv2/graphs.py Normal file
View File

@@ -0,0 +1,17 @@
from matplotlib import pyplot as plt
class Plotter:
pass
def plot_bars(self):
pass
def plot_lin(self):
pass
def plot_box(self):
pass
def adjust_x(self):
pass

View File

@@ -14,7 +14,7 @@ except ModuleNotFoundError:
logger = logging.getLogger(__name__)
def connect(uri) -> MongoClient:
def connect(uri: str) -> MongoClient:
try:
client = MongoClient(uri)
logger.info("Connected to the DB")
@@ -26,10 +26,9 @@ def connect(uri) -> MongoClient:
def add_events(
client: MongoClient, collection: str, data: list[dict[str, Any]]
client: MongoClient, collection: str, data: list[dict[str, Any]], db: str = "main"
) -> None:
db = client["main"]
coll = db[collection]
coll: Collection = client[db][collection]
_res = coll.insert_many(data)
@@ -40,10 +39,9 @@ def add_events(
def add_stations(
client: MongoClient, collection: str, data: list[dict[str, Any]]
client: MongoClient, collection: str, data: list[dict[str, Any]], db: str = "main"
) -> None:
db = client["main"]
coll = db[collection]
coll: Collection = client[db][collection]
_res = coll.insert_many(data)
@@ -62,19 +60,28 @@ def close(client: MongoClient) -> None:
logger.info("Closed the DB.")
def query_all(cli: MongoClient, collection: str) -> Any:
coll: Collection = cli.main[collection]
def query_all(client: MongoClient, collection: str, db: str = "main") -> Any:
coll: Collection = client[db][collection]
result = coll.find({})
for doc in result:
print(doc)
return list(result)
def filter_query(cli: MongoClient, collection: str, filter_by):
coll: Collection = cli.main[collection]
def filter_query(
client: MongoClient, collection: str, filter_by: dict[str, Any], db: str = "main"
):
coll: Collection = client[db][collection]
res = coll.find({""})
res = coll.find(
filter_by, {"DateTime": 1, "Magnitudes": 1, "Depth": 1, "GAP": 1}
).sort({"DateTime": 1})
if not res._empty:
res = list(res)
logger.info(f"Retrieved {len(res)} elements.")
return res
if __name__ == "__main__":

49
utilsv2/stats.py Normal file
View File

@@ -0,0 +1,49 @@
import time
import numpy as np
from pymongo import MongoClient
def print_filters(filters):
_res = ""
for k, v in filters.items():
_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"
fname = f"stats-{time.time_ns()}.txt"
with open(fname, "wb") as fp:
fp.write(_stats_txt.encode("utf-8"))
print(_stats_txt)
def get_data(client: MongoClient):
pass
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),
)
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))

View File

@@ -1,2 +1,32 @@
from datetime import datetime
def is_empty(_str: str) -> bool:
return len(_str.strip(" ")) == 0
def toDateTime(dt: str) -> datetime | int:
if len(dt) == 0:
return -1
try:
return datetime.strptime(dt, "%Y-%m-%d")
except ValueError:
return -1
def toFloat(v: str) -> float:
if len(v) == 0:
return -1.0
try:
return float(v)
except ValueError:
return -1.0
def toInt(v: str) -> int | None:
if len(v) == 0:
return None
try:
return int(v)
except ValueError:
return None