data_analysis / app.py
ifw-arz's picture
Update app.py
36242ac
import pandas as pd
import streamlit as st
import time
import keyboard
import os
import psutil
from plot_data import *
from data_analysis import *
def main():
st.markdown("<h2 style='text-align: left; color: black; font-family:Arial;font-size:2.25rem;'>CSV Datenanalyse</h2>", unsafe_allow_html=True)
uploaded_file = st.file_uploader("CSV hochladen", type=["csv"])
if uploaded_file is not None:
try:
df = pd.read_csv(uploaded_file)
except pd.errors.EmptyDataError:
st.error("Fehler: Leere CSV Datei")
except Exception as e:
st.error(f"Error: {str(e)}")
tab1, tab2, tab3 = st.tabs(["Import", "Analyse", "Beenden"])
with tab1:
if uploaded_file is not None:
time_column = st.selectbox("Zeit auswählen", df.columns, index=0)
data_column = st.selectbox("Daten auswählen", df.columns)
try:
fig = plot_data_matplotlib(df, time_column, data_column)
st.pyplot(fig)
except:
pass
with tab2:
if uploaded_file is not None:
scatter = st.checkbox("Scatterplot", key="scatter")
fig = plot_data_zeitfaktor(df, time_column, data_column, scatter)
st.pyplot(fig)
st.subheader("Zu analysierenden Zeitbereich auswählen")
t0 = df[time_column].min()
tn = df[time_column].max()
col1, col2 = st.columns(2)
with col1:
t1 = st.slider("Linke Grenze", min_value=t0, max_value=tn, value=t0, step=0.1)
with col2:
t2 = st.slider("Rechte Grenze", min_value=t0, max_value=tn, value=tn, step=0.1)
if t1 > t2:
st.error("Fehler: Linke Grenze muss kleiner als rechte Grenze sein")
st.stop()
col12, col22 = st.columns([0.3, 0.7])
with col12:
st.checkbox("Mehrfach-Analyse", key="mehrfach")
with col22:
data_column_2 = st.selectbox("Zeiten Datensatz auswählen", df.columns, disabled=not(st.session_state.mehrfach))
try:
if not st.session_state.mehrfach:
result = aktualisierungsrate(df[time_column], df[data_column], t1, t2)
fig2 = plot_filtered(df, time_column, data_column, t1, t2, scatter)
st.pyplot(fig2)
st.subheader("Aktualisierungsrate")
st.write("Durchschnitt: ", result[0])
st.write("Minimal: ", result[1])
st.write("Maximal: ", result[2])
else:
result1 = aktualisierungsrate(df[time_column], df[data_column], t1, t2)
result2 = aktualisierungsrate(df[time_column], df[data_column_2], t1, t2)
fig2 = plot_filtered_mehrfach(df, time_column, data_column, data_column_2, t1, t2)
st.pyplot(fig2)
st.subheader("Aktualisierungsrate")
col1, col2 = st.columns(2)
with col1:
st.write(data_column)
st.write("Durchschnitt: ", result1[0])
st.write("Minimal: ", result1[1])
st.write("Maximal: ", result1[2])
with col2:
st.write(data_column_2)
st.write("Durchschnitt: ", result2[0])
st.write("Minimal: ", result2[1])
st.write("Maximal: ", result2[2])
st.subheader("Latenz")
latenzen, smaller_dataset = latenz(df[time_column], df[data_column], df[data_column_2], t1, t2)
col1, col2 = st.columns(2)
with col1:
st.write("Durchschnitt: ", latenzen.mean())
st.write("Minimal: ", latenzen.min())
st.write("Maximal: ", latenzen.max())
with col2:
st.write("Latenz berechnet anhand der Datenpunkte:")
st.dataframe(smaller_dataset, hide_index=True)
except:
pass
with tab3:
exit_app = st.button("App beenden")
if exit_app:
try:
time.sleep(3)
keyboard.press_and_release('ctrl+w')
pid = os.getpid()
p = psutil.Process(pid)
p.terminate()
except:
st.error("Beenden nicht möglich")
if __name__ == "__main__":
main()