-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebqc.py
359 lines (313 loc) · 12.8 KB
/
webqc.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# -*- coding: utf-8 -*-
'''
streamlit run webqc.py
python=3.11.5
pandas==2.0.3
altair==5.2.0
streamlit_extras==0.4.2
'''
import streamlit as st
import pandas as pd
#import numpy as np
import altair as alt
import math
# Uncomment the following line if use away from streamlit.app
from streamlit_extras.dataframe_explorer import dataframe_explorer
st.set_page_config(
page_title="WebQC",
page_icon="🗃️",
layout="wide",
initial_sidebar_state="expanded"
)
#START HIDE the TOP an burger menu!
st.markdown("""
<style>
[data-testid="stDecoration"] {
display: none;
}
.reportview-container {
margin-top: -2em;
}
#MainMenu {visibility: hidden;}
.stDeployButton {display:none;}
footer {visibility: hidden;}
#stDecoration {display:none;}
</style>""",
unsafe_allow_html=True)
ms = st.session_state
if "themes" not in ms:
ms.themes = {"current_theme": "dark",
"refreshed": True,
"light": {"theme.base": "dark",
"button_face": "🌜"},
"dark": {"theme.base": "light",
"button_face": "🌞"},
}
if ms.themes["current_theme"]=="dark":
hide_streamlit_style = """
<style>
#root > div:nth-child(1) > div > div > div > div > section > div {padding-top: 0.5rem;}
.stSelectbox div[data-baseweb="select"] > div:first-child {
background-color: Chocolate;
border-color: #ff0000;
}
body
</style>
<style>
#MainMenu {visibility: hidden;}
footer {visibility: hidden;}
</style>
"""
else:
hide_streamlit_style = """
<style>
#root > div:nth-child(1) > div > div > div > div > section > div {padding-top: 0.5rem;}
.stSelectbox div[data-baseweb="select"] > div:first-child {
background-color: gray;
border-color: #000000;
}
body
</style>
<style>
#MainMenu {visibility: hidden;}
footer {visibility: hidden;}
</style>
"""
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
#STOP HIDE the TOP an burger menu!
def ChangeTheme():
previous_theme = ms.themes["current_theme"]
tdict = ms.themes["light"] if ms.themes["current_theme"] == "light" else ms.themes["dark"]
for vkey, vval in tdict.items():
if vkey.startswith("theme"): st._config.set_option(vkey, vval)
ms.themes["refreshed"] = False
if previous_theme == "dark": ms.themes["current_theme"] = "light"
elif previous_theme == "light": ms.themes["current_theme"] = "dark"
st.sidebar.title("Web QC")
st.sidebar.title("Navigation Options")
# Display different functionalities
choice = st.sidebar.selectbox('Select....', options=["Load Data", "Edit Dataframe", "Quality Control"], index=0)
btn_face = ms.themes["light"]["button_face"] if ms.themes["current_theme"] == "light" else ms.themes["dark"]["button_face"]
st.sidebar.write (':blue[\nChange Theme:]')
st.sidebar.button(btn_face, on_click=ChangeTheme)
if ms.themes["refreshed"] == False:
ms.themes["refreshed"] = True
st.rerun()
def function_cleaner(x):
x = x.replace(' ', '_')
x = x.replace(':', '_')
x = x.replace('[', '_')
x = x.replace(']', '_')
x = x.replace('.', '_')
x = x.replace('/', '_')
x = x.replace('#', '_')
x = x.replace(',', '_')
x = x.replace(';', '_')
x = x.replace(':', '_')
x = x.replace('^', '_')
x = x.replace('\'', '_')
x = x.replace('"', '_')
x = x.replace('(', '_')
x = x.replace(')', '_')
x = x.replace('&', '_')
print('change '+str(x))
return x
def load_data_2_dataframe(file,separaval):
'''
Input Parameters
----------
file : the file needs to be of type CSV
Description
-------
Loads the data from the CSV file into a pandas dataframe
Returns
-------
session_state containing dataframe df
'''
if 'df' not in st.session_state:
df = pd.read_csv(file,sep=separaval)
for tmpcol in df.columns:
cleanCol=function_cleaner(tmpcol)
df.rename(columns={tmpcol: cleanCol}, inplace=True)
st.session_state['df'] = df
#st.data_editor(df, num_rows="dynamic")
st.data_editor(df)
else:
#st.data_editor(st.session_state['df'], num_rows="dynamic")
st.data_editor(st.session_state['df'])
return st.session_state['df']
def reset_data():
if 'df' not in st.session_state:
print('Dataframe not found')
else:
del st.session_state['df']
load_data()
def qc():
st.title(':blue[Assign QC Flags] 📈')
if 'df' in st.session_state:
seedata = st.toggle('See dataframe', key="2")
df= st.session_state['df']
#df = dataframe_explorer(df, case=False)
if "WebQCIndex" not in df.columns:
df['WebQCIndex'] = range(1, len(df) + 1)
st.write('To manage the QC, a column labelled WebQCIndex has been added to the dataframe')
if seedata:
st.write(df)
colsGrid = st.columns(4)
param_column=['']
param_column.extend(df.columns)
qc_column=['']
qc_column.extend(df.columns)
with colsGrid[0]:
selected_param_column = st.selectbox('Select param column to check:', param_column)
with colsGrid[1]:
selected_qc_column = st.selectbox('Select QC flag column:', qc_column)
if selected_param_column != '' and selected_qc_column != '':
#myX=selected_index_column
myY=selected_param_column
myZ=selected_qc_column
point_selector = alt.selection_point("point_selection")
interval_selector = alt.selection_interval("interval_selection")
chartFilter = st.toggle('Use Chart with filters (WARNING: anomaly behaviour with great datasets)', key="5")
if chartFilter:
chart = (
#alt.Chart(df)
# Uncomment the following line if use away from streamlit.app
alt.Chart(dataframe_explorer(df, case=False))
.mark_circle()
.encode(
x="WebQCIndex",
y=myY,
#size=myZ+":N",
color=myZ+":N",
tooltip=["WebQCIndex", myY, myZ],
fillOpacity=alt.condition(point_selector, alt.value(1), alt.value(0.3))
)
.add_params(point_selector, interval_selector)
.properties(width=1100, height=900)
)
else:
chart = (
alt.Chart(df)
.mark_circle()
.encode(
x="WebQCIndex",
y=myY,
#size=myZ+":N",
color=myZ+":N",
tooltip=["WebQCIndex", myY, myZ],
fillOpacity=alt.condition(point_selector, alt.value(1), alt.value(0.3))
)
.add_params(point_selector, interval_selector)
.properties(width=1100, height=900)
)
event = st.altair_chart(chart, theme="streamlit", key="alt_chart", on_select="rerun")
downloaddata = st.toggle('Download data CSV format', key="1")
if downloaddata:
import time
import uuid
idrnd = uuid.uuid4()
savename=str(time.strftime("%Y%m%d%H%M%S")+'-'+str(idrnd))
st.download_button(
label="Download Saved data data as CSV", data=df.to_csv(index=False), file_name="Saved_data"+savename+".csv",
mime="text/csv"
)
if str(event)!='{\'selection\': {\'interval\': {}}}':
with colsGrid[2]:
myFlags=[0,1,2,3,4,5,6,7,8,9]
selected_flag = st.selectbox('Select QC FLAG:', myFlags)
with colsGrid[3]:
st.write('')
st.write('')
if st.button("Assign QC FLAG"):
x = event.get("selection").get("interval_selection").get("WebQCIndex")
y = event.get("selection").get("interval_selection").get(myY)
if str(event)!='{\'selection\': {\'interval\': {}}}':
count=0
for u in x:
if count==0:
bottomId = math.floor(u)
#bottomId = u
count+=1
else:
topId = math.ceil(u)
#topId = u
count=0
for e in y:
if count==0:
#bottomParam = math.ceil(e)
#bottomParam = math.floor(e)
bottomParam = e
count+=1
else:
topParam = e
#topParam = math.ceil(e)
for i in range(bottomId, topId):
#print(str(df[myZ][i])+' to '+str(selected_flag))
if df[myY][i] >= bottomParam and df[myY][i] <= topParam:
df[myZ][i]=selected_flag
#print(str(df[myZ][i])+' to '+str(selected_flag))
#df.loc[df[myZ], i] = selected_flag
st.rerun()
else:
st.write('Please LOAD DATA')
def load_data():
'''
loads the selected file into a dataframe
stores the selected file and dataframe in st.session_state
'''
st.title(':blue[Load CSV dataset] 📂')
# check if the dataframe df in st.session_state and is not blank
if 'df' in st.session_state and st.session_state['df'] is not None:
df=load_data_2_dataframe(st.session_state['selected_file'])
else:
file = st.file_uploader("Upload a file", type=['csv'])
separa = st.radio(
"Specify the separator",
["COMMA", "TAB", "COLON", "SEMICOLON"])
if file is not None:
if separa == 'COMMA':
separaval=','
if separa == 'TAB':
separaval='\t'
if separa == 'COLON':
separaval=':'
if separa == 'SEMICOLON':
separaval=';'
st.session_state['selected_file'] = file
df=load_data_2_dataframe(st.session_state['selected_file'],separaval)
def loadDataEditor():
if 'df' in st.session_state:
df= st.session_state['df']
#st.write('Data Editor')
st.title(':blue[Data Editor] 📝')
#df = dataframe_explorer(df, case=False)
cols = st.columns(2)
with cols[0]:
selected_column = st.selectbox('Select column name to delete:', df.columns,index=None, placeholder="Select ...",)
#st.write('')
if st.button("Delete Column"):
if selected_column is not None:
df.drop(columns=[selected_column], axis=1, inplace=True)
with cols[1]:
#st.write('Insert column name')
title = st.text_input(
"Insert column name to create a new one 👇",
"",
key="placeholder",
)
if st.button("Create Column"):
if title != '':
df[title] = ''
#st.data_editor(df, num_rows="dynamic")
# Uncomment the following line if use away from streamlit.app
st.data_editor(dataframe_explorer(df, case=False), num_rows="dynamic")
#clean_data()
else:
st.write('Please LOAD DATA')
if choice == "Load Data":
reset_data()
elif choice == "Edit Dataframe":
loadDataEditor()
elif choice == "Quality Control":
qc()