holzdeko / Lufttemperatur.py
BorowyP
panel app
fd0472a
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 24 15:24:06 2023
@author: BorowyP
"""
import pandas as pd
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
from holoviews.operation.timeseries import rolling, rolling_outlier_std
hv.extension('bokeh')
#pn.extension('tabulator')
pn.extension(sizing_mode="stretch_width")
#pd.set_option("precision", 0)
PALETTE = ["#ff6f69", "#ffcc5c", "#88d8b0", ]
ACCENT_BASE_COLOR = PALETTE[0]
#import time
#start = time.time()
#print('load data')
import numpy as np
from math import pi
from bokeh.palettes import Category20c, Category20
from bokeh.plotting import figure
from bokeh.transform import cumsum
from bokeh.layouts import row
from bokeh.models.formatters import DatetimeTickFormatter
formatter = DatetimeTickFormatter(months='%b %Y') # wird in .hvplot benötigt für x-achse!
air_temp = pd.read_csv(r'df_air_temp.csv', sep=',',
header=0,
#skiprows=[1],
decimal=',',
#dtype={'temp': np.float64},
na_values=('#-INF', '#NAN'))
air_temp.index = pd.to_datetime(air_temp['Date'], format='%Y.%m.%d %H:%M:%S')
air_temp = air_temp.drop(['Date'], axis=1)
air_temp['temp'] = air_temp['temp'].astype(np.float64, copy=True, errors='ignore')
air_temp = air_temp.round(1)
air_date_slider = pn.widgets.DateRangeSlider(name='Date', start=air_temp.index.min(), end=air_temp.index.max())
Stndrt = pn.widgets.RadioButtonGroup(name='Standort', options=['Glienig', 'Groß Liebitz', 'Krausnick', 'Halbe', 'Spreeau', 'Hangelsberg'],button_type='success')
air_temp_inter = air_temp.interactive()
air_temp_inter = (
air_temp_inter[
(air_temp_inter.Standort == Stndrt) &
(air_temp_inter.index >= air_date_slider.param.value_start) &
(air_temp_inter.index <= air_date_slider.param.value_end)
])
def lin_reg(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,
'Temperatur' : 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=['Temperatur', 'Lineare Regression'],
ylabel='Lufttemperatur [°C]',
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(air_temp_inter):
y = air_temp_inter.temp
x = air_temp_inter['Unnamed: 0']
return pn.Column(lin_reg(x,y, air_temp_inter.index))
airtempplot = air_temp_inter.pipe(callback)
temp_glienig = pd.DataFrame({'Glienig': air_temp.loc[air_temp['Standort'] == 'Glienig']['temp']},
index = air_temp.loc[air_temp['Standort'] == 'Glienig'].index)
temp_grlieb = pd.DataFrame({'Groß Liebitz': air_temp.loc[air_temp['Standort'] == 'Groß Liebitz']['temp']},
index = air_temp.loc[air_temp['Standort'] == 'Groß Liebitz'].index)
temp_halbe = pd.DataFrame({'Halbe' : air_temp.loc[air_temp['Standort'] == 'Halbe']['temp']},
index= air_temp.loc[air_temp['Standort'] == 'Halbe'].index)
temp_hberg = pd.DataFrame({'Hangelsberg' : air_temp.loc[air_temp['Standort'] == 'Hangelsberg']['temp']},
index= air_temp.loc[air_temp['Standort'] == 'Hangelsberg'].index)
temp_krausnick = pd.DataFrame({'Krausnick' : air_temp.loc[air_temp['Standort'] == 'Krausnick']['temp']},
index= air_temp.loc[air_temp['Standort'] == 'Krausnick'].index)
temp_spreeau = pd.DataFrame({'Spreeau' : air_temp.loc[air_temp['Standort'] == 'Spreeau']['temp']},
index= air_temp.loc[air_temp['Standort'] == 'Spreeau'].index)
air_temp_hist = pd.concat([temp_glienig,temp_grlieb,temp_halbe,temp_hberg,temp_krausnick,temp_spreeau])
dfi_temp = air_temp_hist.interactive()
filtered = dfi_temp[
(dfi_temp.index >= air_date_slider.param.value_start) &
(dfi_temp.index <= air_date_slider.param.value_end)]
plot_air_temphist = filtered.hvplot(y=['Glienig',
'Groß Liebitz',
'Halbe',
'Hangelsberg',
'Krausnick',
'Spreeau'], kind='hist', responsive=True, min_height=300, xlabel='Lufttemperatur', alpha=0.5)
import os
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("## Luft Temperaturdaten"),
air_date_slider,
airtempplot.panel(),
plot_air_temphist,
#accent_base_color="#88d8b0",
#header_background="#88d8b0",
])
template.servable();