Misc v2
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -2,6 +2,8 @@
|
|||||||
*.json
|
*.json
|
||||||
*.csv
|
*.csv
|
||||||
|
|
||||||
|
stats-*.txt
|
||||||
|
|
||||||
|
|
||||||
# ---> Python
|
# ---> Python
|
||||||
# Byte-compiled / optimized / DLL files
|
# Byte-compiled / optimized / DLL files
|
||||||
|
|||||||
43
ev2.py
43
ev2.py
@@ -1,11 +1,9 @@
|
|||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
from enum import Enum
|
import time
|
||||||
|
|
||||||
from utilsv2 import mongo, stats
|
from utilsv2 import mongo, parser, stats, utils
|
||||||
from utilsv2.log import logger
|
from utilsv2.log import logger
|
||||||
from utilsv2.parser import parse
|
|
||||||
from utilsv2.utils import toDateTime, toFloat, toInt
|
|
||||||
|
|
||||||
OS = os.name
|
OS = os.name
|
||||||
|
|
||||||
@@ -55,8 +53,8 @@ def filter_menu(old_fiters):
|
|||||||
|
|
||||||
d1, d2 = aux.split(",", maxsplit=1)
|
d1, d2 = aux.split(",", maxsplit=1)
|
||||||
|
|
||||||
d1 = toDateTime(d1)
|
d1 = utils.toDateTime(d1)
|
||||||
d2 = toDateTime(d2)
|
d2 = utils.toDateTime(d2)
|
||||||
filters["DateTime"] = {}
|
filters["DateTime"] = {}
|
||||||
|
|
||||||
if d1 != -1:
|
if d1 != -1:
|
||||||
@@ -73,8 +71,8 @@ def filter_menu(old_fiters):
|
|||||||
|
|
||||||
d1, d2 = aux.split(",", maxsplit=1)
|
d1, d2 = aux.split(",", maxsplit=1)
|
||||||
|
|
||||||
d1 = toFloat(d1)
|
d1 = utils.toFloat(d1)
|
||||||
d2 = toFloat(d2)
|
d2 = utils.toFloat(d2)
|
||||||
filters["Magnitudes.L.Magnitude"] = {}
|
filters["Magnitudes.L.Magnitude"] = {}
|
||||||
|
|
||||||
if d1 != -1:
|
if d1 != -1:
|
||||||
@@ -82,15 +80,17 @@ def filter_menu(old_fiters):
|
|||||||
|
|
||||||
if d2 != -1:
|
if d2 != -1:
|
||||||
filters["Magnitudes.L.Magnitude"]["$lte"] = d2
|
filters["Magnitudes.L.Magnitude"]["$lte"] = d2
|
||||||
case "2":
|
|
||||||
|
# Profundidades
|
||||||
|
case "3":
|
||||||
clear_screen()
|
clear_screen()
|
||||||
print("Inserir profundidades de corte, separadas por uma vírgula(,)")
|
print("Inserir profundidades de corte, separadas por uma vírgula(,)")
|
||||||
aux = input()
|
aux = input()
|
||||||
|
|
||||||
d1, d2 = aux.split(",", maxsplit=1)
|
d1, d2 = aux.split(",", maxsplit=1)
|
||||||
|
|
||||||
d1 = toFloat(d1)
|
d1 = utils.toFloat(d1)
|
||||||
d2 = toFloat(d2)
|
d2 = utils.toFloat(d2)
|
||||||
filters["Depth"] = {}
|
filters["Depth"] = {}
|
||||||
|
|
||||||
if d1 != -1:
|
if d1 != -1:
|
||||||
@@ -99,12 +99,14 @@ def filter_menu(old_fiters):
|
|||||||
if d2 != -1:
|
if d2 != -1:
|
||||||
filters["Depth"]["$lte"] = d2
|
filters["Depth"]["$lte"] = d2
|
||||||
|
|
||||||
|
# GAP
|
||||||
case "4":
|
case "4":
|
||||||
clear_screen()
|
clear_screen()
|
||||||
print("Inserir GAP")
|
print("Inserir GAP")
|
||||||
aux = input()
|
aux = input()
|
||||||
|
|
||||||
gap = toInt(aux)
|
gap = utils.toInt(aux)
|
||||||
|
filters["GAP"] = {}
|
||||||
if aux:
|
if aux:
|
||||||
filters["GAP"]["$lte"] = gap
|
filters["GAP"]["$lte"] = gap
|
||||||
|
|
||||||
@@ -113,6 +115,7 @@ def filter_menu(old_fiters):
|
|||||||
|
|
||||||
case "7":
|
case "7":
|
||||||
print(filters)
|
print(filters)
|
||||||
|
time.sleep(2.0)
|
||||||
|
|
||||||
case "q":
|
case "q":
|
||||||
return filters
|
return filters
|
||||||
@@ -133,14 +136,26 @@ def main():
|
|||||||
|
|
||||||
match usrIn:
|
match usrIn:
|
||||||
case "1":
|
case "1":
|
||||||
parse("falsos.txt")
|
aux = input("Ficheiro a ler:")
|
||||||
|
if utils.fileExists(aux):
|
||||||
|
logger.info(f"Parsing the file {aux}")
|
||||||
|
ev, st = parser.parse(aux)
|
||||||
|
|
||||||
|
mongo.add_events(cli, "quakes", ev, "main")
|
||||||
|
mongo.add_stations(cli, "stations", st, "main")
|
||||||
|
|
||||||
|
else:
|
||||||
|
print(f"Could not open the file {aux}")
|
||||||
|
logger.error(f"Could not open the file {aux}")
|
||||||
|
time.sleep(2.0)
|
||||||
|
|
||||||
case "2":
|
case "2":
|
||||||
filters = filter_menu(filters)
|
filters = filter_menu(filters)
|
||||||
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.stats(v)
|
stats.stats(v)
|
||||||
input()
|
time.sleep(2.0)
|
||||||
|
|
||||||
case "q":
|
case "q":
|
||||||
break
|
break
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ def add_events(
|
|||||||
_res = coll.insert_many(data)
|
_res = coll.insert_many(data)
|
||||||
|
|
||||||
if _res.acknowledged:
|
if _res.acknowledged:
|
||||||
logger.info(f"Added {len(_res.inserted_ids)} events.")
|
logger.info(f"Added {len(_res.inserted_ids)} events to {db}.{collection}")
|
||||||
else:
|
else:
|
||||||
logger.info("Could not add events to the database.")
|
logger.info("Could not add events to the database.")
|
||||||
|
|
||||||
@@ -45,7 +45,7 @@ def add_stations(
|
|||||||
_res = coll.insert_many(data)
|
_res = coll.insert_many(data)
|
||||||
|
|
||||||
if _res.acknowledged:
|
if _res.acknowledged:
|
||||||
logger.info(f"Added {len(_res.inserted_ids)} events.")
|
logger.info(f"Added {len(_res.inserted_ids)} stations to {db}.{collection}")
|
||||||
else:
|
else:
|
||||||
logger.info("Could not add events to the database.")
|
logger.info("Could not add events to the database.")
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ def print_filters(filters):
|
|||||||
|
|
||||||
|
|
||||||
def pprint(v):
|
def pprint(v):
|
||||||
return f"\tMédia: {v[0]} \u00b1 {v[1]}; Mediana: {v[2]}; Máximo: {v[4]}; Mínimo: {v[3]}"
|
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]}"
|
||||||
|
|
||||||
|
|
||||||
def stats(data):
|
def stats(data):
|
||||||
@@ -27,7 +27,7 @@ def stats(data):
|
|||||||
m = calc_mag(aux)
|
m = calc_mag(aux)
|
||||||
d = calc_depth(aux)
|
d = calc_depth(aux)
|
||||||
aux = []
|
aux = []
|
||||||
_stats += f"{currMonth.strftime('%Y-%m')}:\n\tMagnitude: {pprint(m)}\n\tProfundidade: {pprint(d)}\n"
|
_stats += f"{currMonth.strftime('%Y-%m')}:\n\tMagnitude: {pprint(m)}\n\tProfundidade: {pprint(d)}\n\n"
|
||||||
currMonth = data[idx]["DateTime"]
|
currMonth = data[idx]["DateTime"]
|
||||||
|
|
||||||
m = calc_mag(aux)
|
m = calc_mag(aux)
|
||||||
@@ -45,6 +45,7 @@ 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)
|
||||||
|
quantile = np.quantile(depths, [0.25, 0.75])
|
||||||
return list(
|
return list(
|
||||||
map(
|
map(
|
||||||
float,
|
float,
|
||||||
@@ -52,6 +53,8 @@ def calc_depth(data):
|
|||||||
round(np.average(depths), 3),
|
round(np.average(depths), 3),
|
||||||
round(np.std(depths), 3),
|
round(np.std(depths), 3),
|
||||||
round(np.median(depths), 3),
|
round(np.median(depths), 3),
|
||||||
|
round(quantile[0], 3),
|
||||||
|
round(quantile[1], 3),
|
||||||
np.min(depths),
|
np.min(depths),
|
||||||
np.max(depths),
|
np.max(depths),
|
||||||
),
|
),
|
||||||
@@ -63,6 +66,7 @@ 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)
|
||||||
|
quantile = np.quantile(mags, [0.25, 0.75])
|
||||||
return list(
|
return list(
|
||||||
map(
|
map(
|
||||||
float,
|
float,
|
||||||
@@ -70,6 +74,8 @@ def calc_mag(data):
|
|||||||
round(np.average(mags), 3),
|
round(np.average(mags), 3),
|
||||||
round(np.std(mags), 3),
|
round(np.std(mags), 3),
|
||||||
round(np.median(mags), 3),
|
round(np.median(mags), 3),
|
||||||
|
round(quantile[0], 3),
|
||||||
|
round(quantile[1], 3),
|
||||||
np.min(mags),
|
np.min(mags),
|
||||||
np.max(mags),
|
np.max(mags),
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import os
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
|
|
||||||
@@ -34,3 +35,14 @@ def toInt(v: str) -> int | None:
|
|||||||
|
|
||||||
def print_ym(dt: datetime) -> str:
|
def print_ym(dt: datetime) -> str:
|
||||||
return dt.strftime("%Y-%m")
|
return dt.strftime("%Y-%m")
|
||||||
|
|
||||||
|
|
||||||
|
def fileExists(fname: str) -> bool:
|
||||||
|
files = set(os.listdir())
|
||||||
|
|
||||||
|
if fname not in files:
|
||||||
|
return False
|
||||||
|
|
||||||
|
if not os.path.isfile(fname):
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|||||||
Reference in New Issue
Block a user