-
Notifications
You must be signed in to change notification settings - Fork 0
/
ikneed.py
232 lines (204 loc) · 6 KB
/
ikneed.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import base64
import streamlit as st
import pandas as pd
import plotly.graph_objects as go
from kneed import DataGenerator, KneeLocator
st.set_page_config(
page_title="ikneed",
page_icon="📈",
layout="centered",
initial_sidebar_state="expanded",
)
# Intro
"""
## ikneed
Interactively find the point of maximum curvature in your data with the
[kneed](https://github.com/arvkevi/kneed) Python package. This app lets
you explore the effects of parameter tuning on the identified knee point. All
of the parameters from the `KneeLocator` class are interactive in the sidebar.
The figure will update when you change the parameters.
[Source code for ikneed](https://github.com/arvkevi/ikneed)
"""
@st.cache()
def find_knee(x, y, S, curve, direction, online, interp_method, polynomial_degree):
kl = KneeLocator(
x=x,
y=y,
S=S,
curve=curve,
direction=direction,
online=online,
interp_method=interp_method,
polynomial_degree=polynomial_degree,
)
return kl
def get_table_download_link(df):
"""Generates a link allowing the data in a given panda dataframe to be downloaded
in: dataframe
out: href string
"""
csv = df.to_csv(index=False, sep="\t")
b64 = base64.b64encode(
csv.encode()
).decode() # some strings <-> bytes conversions necessary here
href = f'<a href="data:file/csv;base64,{b64}" download="kneed_parameters.tsv">Download as .tsv</a>'
return href
def plot_figure(x, y, kl, all_knees):
fig = go.Figure()
fig.add_trace(
go.Scatter(
x=x,
y=y,
mode="lines",
line=dict(color="cornflowerblue", width=6),
name="input data",
)
)
if all_knees:
fig.add_trace(
go.Scatter(
x=sorted(list(kl.all_knees)),
y=list(kl.all_knees_y),
mode="markers",
marker=dict(
color="orange",
size=12,
line=dict(width=1, color="DarkSlateGrey"),
),
marker_symbol="circle",
name="potential knee",
)
)
fig.add_trace(
go.Scatter(
x=[kl.knee],
y=[kl.knee_y],
mode="markers",
marker=dict(
color="orangered",
size=16,
line=dict(width=1, color="DarkSlateGrey"),
),
marker_symbol="x",
name="knee point",
)
)
fig.update_layout(
title="Knee/Elbow(s) in Your Data",
title_x=0.5,
xaxis_title="x",
yaxis_title="y",
)
fig.update_layout(
xaxis=dict(
showline=True,
showgrid=False,
showticklabels=True,
linecolor="rgb(204, 204, 204)",
linewidth=4,
ticks="outside",
tickfont=dict(
family="Arial",
size=18,
color="rgb(82, 82, 82)",
),
),
yaxis=dict(
showline=True,
showgrid=False,
showticklabels=True,
linecolor="rgb(204, 204, 204)",
linewidth=4,
ticks="outside",
tickfont=dict(
family="Arial",
size=18,
color="rgb(82, 82, 82)",
),
),
showlegend=True,
plot_bgcolor="white",
)
return fig
def main():
"""
The main function
"""
default_x, default_y = DataGenerator.concave_increasing()
# default_x, default_y = DataGenerator.bumpy()
x_str = st.sidebar.text_area(
"x (comma or new-line separated numbers)",
value=(",").join(map(str, default_x)),
)
y_str = st.sidebar.text_area(
"y (comma or new-line separated numbers",
value=(",").join(map(str, default_y)),
)
# KneeLocator parameters
S = st.sidebar.number_input(
"S (sensitivity)", min_value=0.0, max_value=None, value=1.0, step=1.0
)
curve = st.sidebar.radio(
"curve",
["concave", "convex"],
)
direction = st.sidebar.radio("direction", ["increasing", "decreasing"])
online = st.sidebar.checkbox("online")
interp_method = st.sidebar.radio("interp_method", ["interp1d", "polynomial"])
# parse x and y
try:
x = [float(_) for _ in x_str.split(",")]
except ValueError:
x = [float(_) for _ in x_str.strip().split("\n")]
try:
y = [float(_) for _ in y_str.split(",")]
except ValueError:
y = [float(_) for _ in y_str.strip().split("\n")]
polynomial_degree = 7
if interp_method == "polynomial":
polynomial_degree = st.sidebar.number_input(
"polynomial_degree", min_value=1, max_value=99, value=7
)
kl = find_knee(x, y, S, curve, direction, online, interp_method, polynomial_degree)
if interp_method == "polynomial":
y = kl.Ds_y
df = pd.DataFrame.from_dict(
{
"knee": [kl.knee],
"S": [S],
"curve": [curve],
"direction": [direction],
"online": [online],
"interp_method": [interp_method],
"polynomial_degree": [polynomial_degree],
"x": [x_str],
"y": [y_str],
},
)
all_knees = st.checkbox("Show all knees/elbows")
# plot the figure
st.write(plot_figure(x, y, kl, all_knees))
"""
Knee point found using each of the parameter sets:
"""
# dataframe knee logger w/ parameters
# export dataframe
st.dataframe(data=df)
st.markdown(get_table_download_link(df), unsafe_allow_html=True)
"""
All the parameters of the KneeLocator class from kneed:
```python
kl = KneeLocator(
x=x,
y=y,
S=S,
curve=curve,
direction=direction,
online=online,
interp_method=interp_method,
polynomial_degree=polynomial_degree,
)
```
"""
if __name__ == "__main__":
main()