-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
91 lines (73 loc) · 2.74 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
## Weibull distribution plot in Shiny
## Author: Pawel Cwiek [aut,cre]
## License: GPL-3
from numpy import arange
import math
import matplotlib.pyplot as plt
import pandas as pd
from pathlib import Path
from shiny import App, Inputs, Outputs, Session, reactive, render, ui
from shiny.ui import head_content, tags
# function weibull return a probability for given parameters:
# c scale factor
# k shape factor
# ws wind speed
def weibull(c, k, ws):
return((k/c)*((ws/c)**(k-1)) * math.exp(-(ws/c)**k))
def mean_wind_speed(c,k):
return c* math.gamma(1+1/k)
app_ui = ui.page_fluid(
head_content(
tags.style((Path(__file__).parent / "www/style.css").read_text())
),
ui.row(
ui.column(4,
ui.h1("Weibull Distribution"),
ui.h3("Create Weibull distribution graph"),
ui.input_numeric("c", label = ui.h5("Scale factor c"), value = 7.0, step = 0.1, min = 0),
ui.input_numeric("k", label = ui.h5("Shape factor k"), value = 2.0, step = 0.1, min = 0),
ui.input_slider("range", label = ui.h5("Range of wind speeds"),
min = 0, max = 25, value = [0,25]),
tags.style((Path(__file__).parent / "www/ion.rangeSlider.css").read_text())),
ui.column(4,
ui.output_plot("wbplot", width='800px', height='600px'),
ui.output_text("text1"), offset = 1)
),
)
def server(input: Inputs, output: Outputs, session: Session):
mean_ws=reactive.Value(0)
@reactive.Effect
@reactive.event(input.c)
def _():
mean_ws.set(mean_wind_speed(input.c(), input.k()))
@reactive.Effect
@reactive.event(input.k)
def _():
mean_ws.set(mean_wind_speed(input.c(), input.k()))
@output
@render.plot
def wbplot():
c=input.c()
k=input.k()
min=input.range()[0]
max=input.range()[1]
ddist = pd.DataFrame(data=arange(0,30,0.1), columns=['wind_speed'])
ddist['probability'] = ddist['wind_speed'].apply(lambda x: weibull(c, k, x))
fig, ax = plt.subplots()
ax.plot(ddist.wind_speed, ddist.probability, color='#A52A2A',
label='Weibull distribution\nc={:.2f}\nk={:.2f}'.format(c,k) )
ax.set_title("Weibull distribution")
ax.set_xlabel("Wind speed (m/s)")
ax.set_ylabel("Probability")
ax.legend()
plt.xlim(min, max)
plt.ylim(0, 0.3)
plt.xticks(range(min,max,5))
plt.yticks(arange(0, 0.30, 0.05))
return fig
@output
@render.text
def text1():
mean_ws_p=mean_ws.get()
return f"Mean wind speed {mean_ws_p:.2f} m/s"
app = App(app_ui, server)