Spaces:
Running
Running
# -*- coding: utf-8 -*- | |
""" | |
Created on Fri Mar 3 12:12:35 2023 | |
@author: BorowyP | |
""" | |
import pandas as pd | |
import numpy as np | |
import hvplot.pandas # Adds .hvplot and .interactive methods to Pandas dataframes | |
import panel as pn # Panel is a simple, flexible and enterprise-ready data app framework | |
import holoviews as hv | |
pn.extension(sizing_mode="stretch_width") | |
PALETTE = ["#ff6f69", "#ffcc5c", "#88d8b0", ] | |
from bokeh.models.formatters import DatetimeTickFormatter | |
formatter = DatetimeTickFormatter(months='%b %Y') # wird in .hvplot benötigt für x-achse! | |
air_hum = pd.read_csv(r'df_air_hum.csv', sep=',', | |
header=0, | |
#skiprows=[1], | |
decimal=',', | |
na_values=('#-INF', '#NAN')) | |
air_hum.index = pd.to_datetime(air_hum['Date'], format='%Y.%m.%d %H:%M:%S') | |
#air_hum.index.names = ['Date'] | |
air_hum = air_hum.drop(['Date'], axis=1) | |
air_hum['hum'] = air_hum['hum'].astype(np.float64, copy=True, errors='ignore') | |
air_hum = air_hum.round(1) | |
air_date_slider = pn.widgets.DateRangeSlider(name='Date', start=air_hum.index.min(), end=air_hum.index.max()) | |
Stndrt = pn.widgets.RadioButtonGroup(name='Standort', options=['Glienig', 'Groß Liebitz', 'Krausnick', 'Halbe', 'Spreeau', 'Hangelsberg'],button_type='success') | |
air_date_slider | |
air_hum_inter = air_hum.interactive() | |
air_hum_inter = ( | |
air_hum_inter[ | |
(air_hum_inter.Standort == Stndrt) & | |
(air_hum_inter.index >= air_date_slider.param.value_start) & | |
(air_hum_inter.index <= air_date_slider.param.value_end) | |
]) | |
def lin_reg_hum(dfx,dfy, date): | |
# Formel für regres-gerade: y= alpha + b * x | |
# https://www.crashkurs-statistik.de/einfache-lineare-regression/ | |
lin_df = pd.DataFrame({'Date' : date, | |
'Luftfeuchte' : dfy, | |
'x-x.mean' : dfx-dfx.mean(), | |
'y-y.mean' : dfy-dfy.mean(), | |
'(x-x.mean) * (y-y.mean)': (dfx-dfx.mean()) * (dfy-dfy.mean()), | |
'(x-x.mean)²' : (dfx-dfx.mean()) * (dfx-dfx.mean()) | |
}) | |
b = lin_df['(x-x.mean) * (y-y.mean)'].sum()/ lin_df['(x-x.mean)²'].sum() | |
alpha = dfy.mean() - b * dfx.mean() | |
lin_df['Lineare Regression'] = round(alpha + b * dfx,2) | |
lin_plot = lin_df.hvplot(x='Date', | |
xlabel='Datum', | |
title=Stndrt, | |
y=['Luftfeuchte', 'Lineare Regression'], | |
ylabel='rel Luftfeuchte [%]', | |
color=PALETTE, | |
line_width=0.5, | |
xformatter=formatter) | |
SQE = ((lin_df['Lineare Regression']-dfy.mean())*(lin_df['Lineare Regression']-dfy.mean())).sum() | |
SQT = (lin_df['y-y.mean'] * lin_df['y-y.mean']).sum() | |
R_Wert = round(SQE/SQT,2) | |
mean = round(dfy.mean(),2) | |
median = dfy.median() | |
maxm = dfy.max() | |
minm = dfy.min() | |
anz = dfy.count() | |
monitor_df = pd.DataFrame({'Standort' : [Stndrt.value], | |
'von' : [air_date_slider.value[0]], | |
'bis' : [air_date_slider.value[1]], | |
'Mittelwert' : [mean], | |
'Median' : [median], | |
'Maximum' : [maxm], | |
'Minimum' : [minm], | |
'Anzahl' : [anz], | |
'R²' : [R_Wert] | |
}) | |
return pn.Column(lin_plot, monitor_df) | |
def callback_hum(air_hum_inter): | |
y = air_hum_inter.hum | |
x = air_hum_inter['Unnamed: 0'] | |
return pn.Column(lin_reg_hum(x,y, air_hum_inter.index)) | |
airhumplot = air_hum_inter.pipe(callback_hum) | |
airhumplot | |
hum_glienig = pd.DataFrame({'Glienig': air_hum.loc[air_hum['Standort'] == 'Glienig']['hum']}, | |
index = air_hum.loc[air_hum['Standort'] == 'Glienig'].index) | |
hum_grlieb = pd.DataFrame({'Groß Liebitz': air_hum.loc[air_hum['Standort'] == 'Groß Liebitz']['hum']}, | |
index = air_hum.loc[air_hum['Standort'] == 'Groß Liebitz'].index) | |
hum_halbe = pd.DataFrame({'Halbe' : air_hum.loc[air_hum['Standort'] == 'Halbe']['hum']}, | |
index= air_hum.loc[air_hum['Standort'] == 'Halbe'].index) | |
hum_hberg = pd.DataFrame({'Hangelsberg' : air_hum.loc[air_hum['Standort'] == 'Hangelsberg']['hum']}, | |
index= air_hum.loc[air_hum['Standort'] == 'Hangelsberg'].index) | |
hum_kraunick = pd.DataFrame({'Krausnick' : air_hum.loc[air_hum['Standort'] == 'Krausnick']['hum']}, | |
index= air_hum.loc[air_hum['Standort'] == 'Krausnick'].index) | |
hum_spreeau = pd.DataFrame({'Spreeau' : air_hum.loc[air_hum['Standort'] == 'Spreeau']['hum']}, | |
index= air_hum.loc[air_hum['Standort'] == 'Spreeau'].index) | |
air_hum_hist = pd.concat([hum_glienig,hum_grlieb,hum_halbe,hum_hberg,hum_kraunick,hum_spreeau]) | |
dfi_hum = air_hum_hist.interactive() | |
filtered = dfi_hum[ | |
(dfi_hum.index >= air_date_slider.param.value_start) & | |
(dfi_hum.index <= air_date_slider.param.value_end)] | |
plot_air_humhist = filtered.hvplot(y=['Glienig', | |
'Groß Liebitz', | |
'Halbe', | |
'Hangelsberg', | |
'Krausnick', | |
'Spreeau'], | |
kind='hist', | |
responsive=True, | |
min_height=300, | |
xlabel='rel Luftfeuchte[%]', | |
alpha=0.5) | |
plot_air_humhist | |
hd_logo = pn.pane.PNG('HD_Logo.png', width=100) | |
hd_logo | |
lfe_logo = pn.pane.PNG('LFE_Logo.png', width=100) | |
fnr_logo = pn.pane.PNG('fnr_logo.png', width=100) | |
template = pn.template.FastListTemplate( | |
title='Holzdeko Dashboard', | |
sidebar=[hd_logo, | |
pn.pane.Markdown("## Einstellungen"), | |
'Standort',Stndrt, | |
lfe_logo, | |
fnr_logo | |
], | |
main=[pn.pane.Markdown("## Relative Luftfeuchte"), | |
air_date_slider, | |
airhumplot.panel(), | |
plot_air_humhist # LUFT | |
#accent_base_color="#88d8b0", | |
#header_background="#88d8b0", | |
]) | |
template.servable(); | |
#print('fertig!') | |
# To launch this dashboard as a web server, we can simply run | |
# cd C:\Users\BorowyP\Desktop\Dashboard-Preasi\soil_air\ | |
# panel serve 20230303_air_hum_docker.ipynb --autoreload |