mais coisas de estatistica
This commit is contained in:
@@ -7,13 +7,30 @@ import sys
|
||||
from datetime import datetime
|
||||
|
||||
import pandas as pd
|
||||
|
||||
from utils import parser, crud, stats, utils
|
||||
from utils import crud, parser, stats, utils
|
||||
|
||||
HEADER = """=== Terramotos ==="""
|
||||
|
||||
EVENT_COLS = ["Data", "Latitude", "Longitude", "Profundidade", "Tipo Evento", "Gap", "Magnitudes", "Regiao", "Sentido"]
|
||||
STATION_COLS = ["Estacao", "Hora", "Min", "Seg", "Componente", "Distancia Epicentro", "Tipo Onda"]
|
||||
EVENT_COLS = [
|
||||
"Data",
|
||||
"Latitude",
|
||||
"Longitude",
|
||||
"Profundidade",
|
||||
"Tipo Evento",
|
||||
"Gap",
|
||||
"Magnitudes",
|
||||
"Regiao",
|
||||
"Sentido",
|
||||
]
|
||||
STATION_COLS = [
|
||||
"Estacao",
|
||||
"Hora",
|
||||
"Min",
|
||||
"Seg",
|
||||
"Componente",
|
||||
"Distancia Epicentro",
|
||||
"Tipo Onda",
|
||||
]
|
||||
|
||||
MENU = """[1] Criar a base de dados
|
||||
[3] Apagar um evento
|
||||
@@ -89,7 +106,6 @@ def main():
|
||||
else:
|
||||
retInfo = "Base de dados não encontrada!"
|
||||
|
||||
|
||||
case "4":
|
||||
if db is not None:
|
||||
crud.read_ids(db)
|
||||
@@ -152,7 +168,7 @@ def main():
|
||||
|
||||
case "8":
|
||||
if db is not None:
|
||||
stats.stat_menu(db)
|
||||
stats.stats(db)
|
||||
else:
|
||||
retInfo = "Base de dados não encontrada!"
|
||||
|
||||
@@ -198,6 +214,7 @@ def _file_exists(name: str) -> bool:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def _event_exists(df, eid) -> bool:
|
||||
allEvents = set(df["ID"])
|
||||
return eid in allEvents
|
||||
@@ -210,13 +227,17 @@ def _get_usr_input(msg:str, asType=str):
|
||||
return None
|
||||
return asType(usrIn)
|
||||
|
||||
|
||||
def _prettify_event(df):
|
||||
preambleInfo = df.drop_duplicates(subset="ID", keep="first")
|
||||
stations = df[["Estacao", "Componente", "Tipo Onda", "Amplitude"]]
|
||||
info = df.drop_duplicates(subset="Data", keep="first")
|
||||
data = datetime.fromisoformat(info.Data.values[0]).strftime("%c")
|
||||
print(f"Região: {info["Regiao"].values[0]}\nData: {data}\nLatitude: {info.Lat.values[0]}\nLongitude: {info.Long.values[0]}"
|
||||
+ f"\nProfundidade: {info.Prof.values[0]}\nTipo de evento: {info['Tipo Ev'].values[0]}\n")
|
||||
print(
|
||||
f"Região: {info['Regiao'].values[0]}\nData: {data}\nLatitude: {info.Lat.values[0]}\nLongitude: {info.Long.values[0]}"
|
||||
+ f"\nProfundidade: {info.Prof.values[0]}\nTipo de evento: {info['Tipo Ev'].values[0]}\n"
|
||||
)
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import collections
|
||||
import datetime
|
||||
|
||||
import numpy as np
|
||||
import stats
|
||||
from matplotlib import pyplot as plt
|
||||
|
||||
|
||||
224
utils/stats.py
224
utils/stats.py
@@ -1,210 +1,56 @@
|
||||
# pyright: basic
|
||||
|
||||
import datetime
|
||||
import os
|
||||
import sys
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import utils
|
||||
|
||||
STAT_HEADER = """=== Terramotos ===
|
||||
== Estatísticas ==
|
||||
|
||||
def stats(df: pd.DataFrame) -> None:
|
||||
"""Estatisticas para a DataFrame
|
||||
:param df: DataFrame em questão"""
|
||||
|
||||
mags = mags_avg_std(df)
|
||||
depth = depth_avg_std(df)
|
||||
|
||||
median_mags = median_mags(df)
|
||||
|
||||
|
||||
def mags_avg_std(data: pd.DataFrame) -> tuple[np.floating, np.floating]:
|
||||
"""Media e desvio-padrao das magnitudes
|
||||
:param data: Dataframe com dados a filtrar
|
||||
:returns: Tuple com a media e desvio-padrao
|
||||
"""
|
||||
filtered_data: pd.DataFrame = filter_mags(data)
|
||||
vals = filtered_data["MagL"].to_numpy()
|
||||
return (np.average(vals), np.std(vals))
|
||||
|
||||
STAT_MENU = """[1] Média
|
||||
[2] Variância
|
||||
[3] Desvio padrão
|
||||
[4] Máximo
|
||||
[5] Mínimo
|
||||
[6] Moda
|
||||
|
||||
[Q] Voltar ao menu principal
|
||||
def depth_avg_std(data: pd.DataFrame) -> tuple[np.floating, np.floating]:
|
||||
"""Media e desvio-padrao das profundidades
|
||||
:param data: Dataframe com dados a filtrar
|
||||
:returns: Tuple com a media e desvio-padrao
|
||||
"""
|
||||
|
||||
FILTER_CHOICES = """[1] Magnitudes
|
||||
[2] Distância
|
||||
[3] Profundidade
|
||||
|
||||
"""
|
||||
|
||||
CHOICE = {"1": "Magnitudes", "2": "Distancia", "3": "Prof"}
|
||||
filtered_data: pd.DataFrame = filter_depth(data)
|
||||
vals = np.average(filtered_data["Profundidade"].to_numpy())
|
||||
return (np.average(vals), np.std(vals))
|
||||
|
||||
|
||||
def filter_submenu(type: str):
|
||||
os.system("cls" if sys.platform == "windows" else "clear")
|
||||
print(f"{STAT_HEADER}\n = {type} = ")
|
||||
print(FILTER_CHOICES)
|
||||
def median_mags(data: pd.DataFrame):
|
||||
filtered_data: pd.DataFrame = filter_mags(data)
|
||||
vals = sorted(filtered_data["MagL"].to_numpy())
|
||||
|
||||
choice = input("Qual dos valores: ")
|
||||
quartil = len(vals) // 4
|
||||
|
||||
try:
|
||||
usrChoice = CHOICE[choice]
|
||||
return usrChoice
|
||||
except KeyError:
|
||||
return None
|
||||
return (
|
||||
filtered_data[quartil, :]["MagL"],
|
||||
filtered_data[quartil * 2, :]["MagL"],
|
||||
filtered_data[quartil * 3, :]["MagL"],
|
||||
)
|
||||
|
||||
|
||||
def stat_menu(df: pd.DataFrame):
|
||||
inStats = True
|
||||
while inStats:
|
||||
os.system("cls" if sys.platform == "windows" else "clear")
|
||||
print(STAT_HEADER + "\n" + STAT_MENU)
|
||||
usrIn = input("Opção: ").lower()
|
||||
|
||||
match usrIn:
|
||||
case "1":
|
||||
c = filter_submenu("Média")
|
||||
|
||||
if c is not None:
|
||||
retValue = average(df, c)
|
||||
if retValue:
|
||||
print(f"A média de {c} é {retValue}")
|
||||
else:
|
||||
print("Um erro aconteceu. Nada a apresentar de momento.")
|
||||
else:
|
||||
continue
|
||||
|
||||
case "2":
|
||||
c = filter_submenu("Variância")
|
||||
|
||||
if c is not None:
|
||||
retValue = variance(df, c)
|
||||
if retValue:
|
||||
print(f"A variância dos dados de {c} é {retValue}")
|
||||
else:
|
||||
print("Um erro aconteceu. Nada a apresentar de momento.")
|
||||
else:
|
||||
continue
|
||||
|
||||
case "3":
|
||||
c = filter_submenu("Desvio Padrão")
|
||||
|
||||
if c is not None:
|
||||
retValue = std_dev(df, c)
|
||||
if retValue:
|
||||
print(f"O desvio padrão de {c} é {retValue}")
|
||||
else:
|
||||
print("Um erro aconteceu. Nada a apresentar de momento.")
|
||||
else:
|
||||
continue
|
||||
|
||||
case "4":
|
||||
c = filter_submenu("Máximo")
|
||||
|
||||
if c is not None:
|
||||
retValue = max_v(df, c)
|
||||
print(f"O valor máximo em {c} é {retValue}")
|
||||
else:
|
||||
continue
|
||||
|
||||
case "5":
|
||||
c = filter_submenu("Mínimo")
|
||||
|
||||
if c is not None:
|
||||
retValue = min_v(df, c)
|
||||
print(f"O valor mínimo em {c} é {retValue}")
|
||||
else:
|
||||
continue
|
||||
|
||||
case "6":
|
||||
c = filter_submenu("Mínimo")
|
||||
|
||||
if c is not None:
|
||||
retValue = moda(df, c)
|
||||
print(f"O valor moda em {c} é {retValue}")
|
||||
else:
|
||||
continue
|
||||
|
||||
case "q":
|
||||
inStats = False
|
||||
continue
|
||||
|
||||
case _:
|
||||
pass
|
||||
input("Clica `Enter` para continuar")
|
||||
|
||||
|
||||
def average(df: pd.DataFrame, filter_by):
|
||||
events = df.drop_duplicates(subset="ID", keep="first")
|
||||
values = events[filter_by].to_numpy()
|
||||
|
||||
if filter_by == "Magnitudes":
|
||||
values = _unpack_mags(values)
|
||||
try:
|
||||
return np.average(values)
|
||||
except:
|
||||
return None
|
||||
|
||||
|
||||
def variance(df, filter_by):
|
||||
events = df.drop_duplicates(subset="ID", keep="first")
|
||||
values = events[filter_by].to_numpy()
|
||||
|
||||
if filter_by == "Magnitudes":
|
||||
values = _unpack_mags(values)
|
||||
|
||||
try:
|
||||
return np.var(values)
|
||||
except:
|
||||
return None
|
||||
|
||||
|
||||
def std_dev(df, filter_by):
|
||||
events = df.drop_duplicates(subset="ID", keep="first")
|
||||
values = events[filter_by].to_numpy()
|
||||
|
||||
if filter_by == "Magnitudes":
|
||||
values = _unpack_mags(values)
|
||||
|
||||
try:
|
||||
return np.std(values)
|
||||
except:
|
||||
return None
|
||||
|
||||
|
||||
def max_v(df, filter_by):
|
||||
events = df.drop_duplicates(subset="ID", keep="first")
|
||||
values = events[filter_by].to_numpy()
|
||||
|
||||
if filter_by == "Magnitudes":
|
||||
values = _unpack_mags(values)
|
||||
|
||||
return np.max(values)
|
||||
|
||||
|
||||
def min_v(df, filter_by):
|
||||
events = df.drop_duplicates(subset="ID", keep="first")
|
||||
values = events[filter_by].to_numpy()
|
||||
|
||||
if filter_by == "Magnitudes":
|
||||
values = _unpack_mags(values)
|
||||
|
||||
return np.min(values)
|
||||
|
||||
|
||||
def moda(df, filter_by):
|
||||
events = df.drop_duplicates(subset="ID", keep="first")
|
||||
values = events[filter_by].to_numpy()
|
||||
|
||||
if filter_by == "Magnitudes":
|
||||
values = _unpack_mags(values)
|
||||
|
||||
uniques, count = np.unique(values, return_counts=True)
|
||||
uniques_list = list(zip(uniques, count))
|
||||
|
||||
return sorted(uniques_list, reverse=True, key=lambda x: x[1])[0][0]
|
||||
|
||||
|
||||
def _unpack_mags(arr: np.ndarray):
|
||||
newVals = np.empty(0)
|
||||
for v in arr:
|
||||
for m in v:
|
||||
newVals = np.append(newVals, float(m["Magnitude"]))
|
||||
return newVals
|
||||
|
||||
|
||||
def filter_mags(data, more_than=None, less_than=None):
|
||||
def filter_mags(data, more_than=None, less_than=None) -> pd.DataFrame:
|
||||
"""Filters by magnitudes a DataFrame into a new Dataframe
|
||||
|
||||
:param data: Raw pandas DataFrame
|
||||
|
||||
Reference in New Issue
Block a user