-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataBucketAlpha.py
1215 lines (1082 loc) · 44.7 KB
/
DataBucketAlpha.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
-- Start STREAMLIT server --
cd my/path/to/streamlit/directoy/
streamlit run DataBucketAlpha.py
or
streamlit run DataBucketAlpha.py --server.port 80
-- For HTTPS support: --
https://docs.streamlit.io/develop/concepts/configuration/https-support
-- Create by hand the cerificate for SSL --
openssl req -newkey rsa:2048 -nodes -keyout key.pem -x509 -days 365 -out cert.pem
-- ./.streamlit/config.toml configuration fo SSL --
[server]
sslCertFile = 'cert.pem'
sslKeyFile = 'key.pem'
-- For Dockerfile (and streamlit) requirements.txt --
streamlit
pandas
matplotlib
streamlit-folium
streamlit-extras
extra-streamlit-components
seaborn
sklearn
plotly
lime
numpy
pygwalker
streamlit_pandas_profiling
ydata_profiling
pivottablejs
dtale
mplcyberpunk
uuid
opencv-python
streamlit-extras
mitosheet
pydeck
streamlit-aggrid
faker
lxml
fastparquet
st_aggrid (sttreamlit-aggrid>= 0.3.4.post3)
h5netcdf
-- For Dockerfile --
FROM python:3.11.5
------ or try to use a previous lite version of Python ------
FROM python:3.8-slim
WORKDIR /app
COPY . /app
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
CMD ["streamlit","run","DataBucketAlpha.py"]
-- For Docker BUILD and RUN --
docker build -t databucketalpha:v1 .
docker images
docker run -p 8501:8501 databucketalpha:v1
"""
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
import folium
from streamlit_folium import st_folium, folium_static
import seaborn as sns
from sklearn.ensemble import RandomForestRegressor
from lime.lime_tabular import LimeTabularExplainer
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
import pygwalker as pyw
import streamlit.components.v1 as components
from ydata_profiling import ProfileReport
from streamlit_pandas_profiling import st_profile_report
from pivottablejs import pivot_ui
import dtale
import numpy
#https://github.com/dhaitz/mplcyberpunk
import mplcyberpunk
import ssl
import requests
import time
import os, sys
import shutil
import uuid
import datetime as dt
import cv2
from PIL import Image
import streamlit.components.v1 as components
from streamlit_extras.dataframe_explorer import dataframe_explorer
from streamlit_extras.jupyterlite import jupyterlite
from datetime import datetime, timedelta
from mitosheet.streamlit.v1 import spreadsheet
from mitosheet.streamlit.v1.spreadsheet import _get_mito_backend
import pydeck as pdk
import inspect
from faker import providers
from faker import Faker
#import fastparquet
PROVIDER_MODULES = {"": ""}
PROVIDER_MODULES.update(
{x[0]: x[1] for x in inspect.getmembers(providers, inspect.ismodule)}
)
PROVIDER_FUNCTIONS = {"": {"": ""}}
for k in PROVIDER_MODULES.keys():
try:
PROVIDER_FUNCTIONS.update(
{
k: {
x[0]: x[1]
for x in inspect.getmembers(
PROVIDER_MODULES[k].Provider, inspect.isfunction
)
if x[0] != "__init__"
and not x[0].startswith("_")
and "BaseProvider" not in str(x[1])
}
}
)
except AttributeError:
PROVIDER_FUNCTIONS.update(
{
k: {
x[0]: x[1]
for x in inspect.getmembers(PROVIDER_MODULES[k], inspect.isfunction)
if x[0] != "__init__"
and not x[0].startswith("_")
and "BaseProvider" not in str(x[1])
}
}
)
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.set_page_config(
page_title="Data Bucket Alpha",
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)
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
#st.markdown('<style>body{background-color: Blue;}</style>',unsafe_allow_html=True)
#STOP HIDE the TOP an burger menu!
st.sidebar.title("Navigation Options 🧭")
# Display different functionalities
choice = st.sidebar.selectbox('Select....', options=["Home", "Load a Dataset", "Merge Datasets", "Load ODVs to MERGE in a Dataset", "Data Editor", "Dataframe Explorer", "Dataframe Analysis", "PLOT Service", "Map Service", "Model Prediction", "Aggrid Builder", "Pygwalker", "ydata_profiling", "pivottablejs", "Dtale", "MitoSheet", "Jupyterlite", "Fake Data Service", "Reset all"], index=0)
def mapservice():
st.title(':blue[Map Service] 🗺️')
if 'df' in st.session_state:
df= st.session_state['df']
cols_list = df.columns.tolist()
optionLatituide = st.selectbox(
"Choose Latitude column",
(cols_list),
index=None,
)
optionLongitude = st.selectbox(
"Choose Longitude column",
(cols_list),
index=None,
)
optionName = st.selectbox(
"Choose station name column",
(cols_list),
index=None,
)
onMapBasic = st.toggle('Map this!')
onMap = st.toggle('Map this using Folium!')
onMapPydeck = st.toggle('Map this using Pydeck!')
if onMap and optionLatituide and optionLongitude:
df_extr=df[[optionLatituide,optionLongitude,optionName]].copy()
DF_Mysite_filtered=df_extr.drop_duplicates()
DF_Mysite_filtered.columns = ['latitude', 'longitude','name']
m = folium.Map(location=[DF_Mysite_filtered.latitude.mean(), DF_Mysite_filtered.longitude.mean()], zoom_start=7, control_scale=True)
#Loop through each row in the dataframe
for i,row in DF_Mysite_filtered.iterrows():
#Setup the content of the popup
iframe = folium.IFrame('Dataset name:' + str(row[2]), width=500, height=50)
popup = folium.Popup(iframe, max_width=500)
#Initialise the popup using the iframe
popup = folium.Popup(iframe, min_width=300, max_width=300)
#Add each row to the map
folium.Marker(location=[row[0],row[1]],
popup = popup, c='Well Name').add_to(m)
st_data = st_folium(m, width=800, height=500)
if (onMapPydeck or onMapBasic) and optionLatituide and optionLongitude:
if onMapPydeck:
df_extr=df[[optionLongitude,optionLatituide]].copy()
DF_Mysite_filtered=df_extr.drop_duplicates()
DF_Mysite_filtered.columns = ['lon','lat']
st.pydeck_chart(pdk.Deck(
map_style=None,
initial_view_state=pdk.ViewState(
latitude=DF_Mysite_filtered.lat.mean(),
longitude=DF_Mysite_filtered.lon.mean(),
zoom=11,
pitch=50,
),
layers=[
pdk.Layer(
'HexagonLayer',
data=DF_Mysite_filtered,
get_position='[lon, lat]',
radius=200,
elevation_scale=4,
elevation_range=[0, 1000],
pickable=True,
extruded=True,
),
pdk.Layer(
'ScatterplotLayer',
data=DF_Mysite_filtered,
get_position='[lon, lat]',
get_color='[200, 30, 0, 160]',
get_radius=200,
),
],
))
if onMapBasic:
df_extr=df[[optionLatituide,optionLongitude]].copy()
DF_Mysite_filtered=df_extr.drop_duplicates()
DF_Mysite_filtered.columns = ['lat','lon']
st.map(DF_Mysite_filtered)
else:
st.write('Please LOAD DATA')
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:
if file.name.split(".")[-1] == "csv":
df = pd.read_csv(file,sep=separaval)
elif file.name.split(".")[-1] == 'json':
df = pd.read_json(file, lines=True)
elif file.name.split(".")[-1] == 'xml':
df = pd.read_xml(file)
elif file.name.split(".")[-1] == 'parquet':
df = pd.read_parquet(file, engine='fastparquet')
elif file.name.split(".")[-1] == 'nc':
import xarray as xr
ds = xr.open_dataset(file)
df = ds.to_dataframe()
st.session_state['df'] = df
st.dataframe(df)
idrnd = uuid.uuid4()
savename=str(time.strftime("%Y%m%d%H%M%S")+'-'+str(idrnd))
csv, json, parquet, xml = st.columns(4)
csv.download_button(
label="Download data as CSV", data=df.to_csv(index=False), file_name="dw_data"+savename+".csv",
mime="text/csv"
)
json.download_button(
label="Download data as JSON", data=df.to_json(), file_name="dw_data"+savename+".json"
)
parquet.download_button(
label="Download data as Parquet",
data=df.to_parquet(index=False),
file_name="dw_data"+savename+".parquet",
)
else:
st.dataframe(st.session_state['df'])
return st.session_state['df']
def resetall():
keys = list(st.session_state.keys())
for key in keys:
st.session_state.pop(key)
st.write("The cache has been cleaned!")
def clean_data():
if 'df' in st.session_state:
df= st.session_state['df']
count_rows_with_nan = df.isna().any(axis=1).sum()
st.session_state['null_count']=count_rows_with_nan
st.subheader("No. of rows containing null value",)
# Display Null count
st.metric("Null value", count_rows_with_nan, int(len(df)-count_rows_with_nan))
field = st.selectbox('Select Field to Analyze for Outliers', df.columns)
# Plot and display in Streamlit
if field:
st.dataframe(detect_outliers(df, field))
def detect_outliers(df, field):
Q1 = df[field].quantile(0.25)
Q3 = df[field].quantile(0.75)
IQR = Q3 - Q1
lower_bound = Q1 - 1.5 * IQR
upper_bound = Q3 + 1.5 * IQR
return df[(df[field] < lower_bound) | (df[field] > upper_bound)]
def plot_data_with_outliers(df, field):
outliers = detect_outliers(df, field)
plt.style.use("cyberpunk")
mplcyberpunk.add_glow_effects()
mplcyberpunk.add_gradient_fill(alpha_gradientglow=0.5)
fig, ax = plt.subplots()
ax.boxplot(df[field])
ax.scatter(outliers.index, outliers[field], color='red', label='Outliers')
ax.set_title(f"Outliers in {field}")
ax.legend()
return fig
def data_page():
'''
Display different graphs to understand the data
'''
if 'df' in st.session_state and st.session_state['df'] is not None:
#st.title('PLOT Service')
st.title(':blue[PLOT Service] 📊')
df= st.session_state['df']
CorrGraph = st.toggle('Correlation graph')
if CorrGraph:
fig, ax = plt.subplots(figsize=(10,10))
# Create the heatmap
sns.heatmap(df.corr(), annot=True, cmap="coolwarm", fmt=".2f", linewidths=.5)
# Add a legend
ax.legend()
# Display the plot
st.pyplot(fig)
#st.divider()
#st.write("🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺🔺")
st.write("🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩")
#st.write("🔻🔻🔻🔻🔻🔻🔻🔻🔻🔻🔻🔻🔻🔻🔻🔻🔻🔻🔻🔻🔻")
selected_columns = st.multiselect('Select columns to plot:', df.columns)
plotstyle = st.selectbox(
'Select PLOT STYLE (only for line charts)',
("default",
"classic",
"Solarize_Light2",
"bmh",
"dark_background",
"ggplot",
"grayscale",
"seaborn-v0_8",
"seaborn-v0_8-bright",
"seaborn-v0_8-pastel",
"cyberpunk"),
key="placeholder",
)
plotLineChart = st.toggle('PLOT ALL PARAMS IN A SINGLE LINE CHART')
ScatterGraph = st.toggle('PLOT ALL PARAMS IN a Scatter graph')
AreaGraph = st.toggle('PLOT ALL PARAMS IN an Area graph')
BarGraph = st.toggle('PLOT ALL PARAMS IN a Bar graph')
singleplotLineChart = st.toggle('FOR EACH PARAM A SINGLE LINE CHART PLOT')
if selected_columns is not None:
# Create a figure
if plotstyle=='':
plt.style.use("cyberpunk")
mplcyberpunk.add_glow_effects()
mplcyberpunk.add_gradient_fill(alpha_gradientglow=0.5)
else:
plt.style.use(plotstyle)
if plotLineChart:
fig, ax = plt.subplots()
# Plot the data
for column in selected_columns:
ax.plot(df[column], label=column)
#st.write(df[column])
# Set the axis labels
ax.set_xlabel('X Axis')
ax.set_ylabel('Y Axis')
# Set the title
ax.set_title('Line Chart')
# Add a legend
ax.legend()
# Display the plot
st.pyplot(fig)
if ScatterGraph:
dftemp = pd.DataFrame()
dftemp=pd.concat([df[selected_columns]])
st.scatter_chart(dftemp)
if AreaGraph:
dftemp = pd.DataFrame()
dftemp=pd.concat([df[selected_columns]])
st.area_chart(dftemp)
if BarGraph:
dftemp = pd.DataFrame()
dftemp=pd.concat([df[selected_columns]])
st.bar_chart(dftemp)
if singleplotLineChart:
# Plot the data
for column in selected_columns:
fig, ax = plt.subplots()
ax.plot(df[column], label=column)
# Set the axis labels
ax.set_xlabel('X Axis')
ax.set_ylabel('Y Axis')
# Set the title
ax.set_title('Line Chart')
# Add a legend
ax.legend()
# Display the plot
st.pyplot(fig)
else:
st.write('Please LOAD DATA')
def reset_data():
#if 'df' not in st.session_state:
# print('Dataframe not found')
#else:
# del st.session_state['df']
keys = list(st.session_state.keys())
for key in keys:
st.session_state.pop(key)
st.write("The cache has been cleaned!")
load_data()
def reset_dataOdv():
if 'dfodv' not in st.session_state:
print('Dataframe not found')
else:
del st.session_state['dfodv']
load_Odvs()
def random_forest_feature_imp():
if 'df' in st.session_state:
df=st.session_state['df']
X_train, y_train, X_test, y_test=create_test_train_data(df.columns[:-1])
model = RandomForestRegressor()
model.fit(X_train, y_train)
plt.style.use("cyberpunk")
mplcyberpunk.add_glow_effects()
mplcyberpunk.add_gradient_fill(alpha_gradientglow=0.5)
fig = plt.figure(figsize = (10, 5))
# creating the bar plot
plt.bar(df.columns[:-1], model.feature_importances_, color ='maroon',
width = 0.4)
# Print the feature importances
st.pyplot(fig)
def random_forest_model():
# Train a random forest model
if 'df' in st.session_state:
df=st.session_state['df']
model = RandomForestRegressor()
selected_imp_columns_rf = st.multiselect('Select columns for training Random Forest:', df.columns[:-1], key='rf')
if selected_imp_columns_rf !=[]:
X_train, y_train, X_test, y_test=create_test_train_data(selected_imp_columns_rf)
model.fit(X_train, y_train)
y_pred= model.predict(X_test)
st.metric(" Mean Squared Error",np.round(mean_squared_error(y_test, y_pred),2))
st.metric(" R Square",np.round(r2_score(y_test, y_pred),2))
plt.style.use("cyberpunk")
mplcyberpunk.add_glow_effects()
mplcyberpunk.add_gradient_fill(alpha_gradientglow=0.5)
fig1 = plt.figure(figsize = (10, 5))
df_pred= pd.DataFrame(data={'predictions': y_pred, 'actual': y_test})
plt.plot(df_pred['actual'], label='Actual')
plt.plot(df_pred['predictions'], label='predictions')
plt.legend()
st.pyplot(fig1)
def linear_regression_model():
if 'df' in st.session_state:
df=st.session_state['df']
selected_imp_columns_lr = st.multiselect('Select columns for training Linear Regression:', df.columns[:-1], key='lr')
if selected_imp_columns_lr !=[]:
X_train, y_train, X_test, y_test=create_test_train_data(selected_imp_columns_lr)
model =LinearRegression()
model.fit(X_train, y_train)
y_pred=model.predict(X_test)
st.metric(" Mean Squared Error",np.round(mean_squared_error(y_test, y_pred),2))
st.metric(" R Square",np.round(r2_score(y_test, y_pred),2))
plt.style.use("cyberpunk")
mplcyberpunk.add_glow_effects()
mplcyberpunk.add_gradient_fill(alpha_gradientglow=0.5)
fig1 = plt.figure(figsize = (10, 5))
df_pred= pd.DataFrame(data={'predictions': y_pred, 'actual': y_test})
plt.plot(df_pred['actual'], label='Actual')
plt.plot(df_pred['predictions'], label='predictions')
plt.legend()
st.pyplot(fig1)
def create_test_train_data(selected_imp_columns):
if 'df' in st.session_state:
df= st.session_state['df']
df_len=len(df)
y_col=df.columns[-1]
#st.write("in test train",selected_imp_columns)
#df_col=selected_imp_columns
train_len=int(.7*df_len)
X_train=df.loc[:train_len, selected_imp_columns].copy()
y_train=df.loc[:train_len,y_col].copy()
X_test=df.loc[train_len:, selected_imp_columns].copy()
y_test=df.loc[train_len:,y_col].copy()
#st.write(X_test, y_test)
return X_train, y_train, X_test, y_test
def lime_feature_imp():
if 'df' in st.session_state:
df= st.session_state['df']
X_train, y_train, X_test, y_test=create_test_train_data(df.columns[:-1])
model = RandomForestRegressor()
model.fit(X_train, y_train)
explainer=LimeTabularExplainer(training_data=np.array(X_train),
mode="regression",
feature_names=list(X_train.columns),
training_labels=np.array(y_train),
random_state=0)
exp=explainer.explain_instance(X_test.iloc[0], model.predict, num_features=len(X_train.columns))
st.pyplot(exp.as_pyplot_figure())
def model_selection():
if 'df' in st.session_state:
#st.write(selected_imp_columns[0])
st.title(':blue[Model Prediction]')
#linear, RF, xgboost=st.tabs(['Linear Regression', 'Random Forest', 'XGBoost'])
linear, RF=st.tabs(['Linear Regression', 'Random Forest'])
with linear:
feature_imp, pred= st.columns(2)
with feature_imp:
st.subheader("Lime Feature Importances")
lime_feature_imp()
with pred:
st.subheader("Prediction with Linear Regression")
linear_regression_model()
with RF:
feature_imp, pred= st.columns(2)
with feature_imp:
st.subheader("Random Forest Feature Importance")
random_forest_feature_imp()
with pred:
st.subheader("Prediction with Random Forest")
random_forest_model()
else:
st.write('Please LOAD DATA')
def loadDtale():
st.title(':blue[Dtale]')
st.link_button("How to use", "https://www.youtube.com/watch?v=LRv6b1KDujI")
if 'df' in st.session_state:
df= st.session_state['df']
d = dtale.show(df)
d.open_browser()
clean_data()
st.write('A new web page has been opened for Dtale')
else:
st.write('Please LOAD DATA')
def loadpivottablejs():
#st.write('PivoTableJS')
st.title(':blue[PivoTableJS]')
st.link_button("How to use", "https://pivottable.js.org/examples/index.html")
if 'df' in st.session_state:
df= st.session_state['df']
t = pivot_ui(df)
with open(t.src) as t:
components.html(t.read(), width=1200, height=1000, scrolling=True)
clean_data()
else:
st.write('Please LOAD DATA')
def loadydata_profiling():
#st.write('Ydata Profiling')
st.title(':blue[Ydata Profiling]')
st.link_button("How to use", "https://docs.profiling.ydata.ai/latest/getting-started/concepts/")
if 'df' in st.session_state:
df= st.session_state['df']
st.dataframe(df)
pr = ProfileReport(df, title="Report")
st_profile_report(pr)
clean_data()
else:
st.write('Please LOAD DATA')
def loadDataEditor():
if 'df' in st.session_state:
execute=0
df= st.session_state['df']
#st.write('Data Editor')
st.title(':blue[Data Editor] 📝')
selected_column = st.selectbox('Select column to delete:', df.columns)
st.write('')
if st.button("Delete Column"):
execute=1
if execute==1:
df.drop(columns=[selected_column], axis=1, inplace=True)
execute=0
st.data_editor(df, num_rows="dynamic")
#clean_data()
else:
st.write('Please LOAD DATA')
def dataframexplorer():
if 'df' in st.session_state:
df= st.session_state['df']
#st.write('Dataframe Explorer')
st.title(':blue[Dataframe Explorer] 🕵️♂️')
filtered_df = dataframe_explorer(df, case=False)
st.dataframe(filtered_df, use_container_width=True)
else:
st.write('Please LOAD DATA')
#--START MITO SHEET
def clear_mito_backend_cache():
_get_mito_backend.clear()
# Function to cache the last execution time - so we can clear periodically
@st.cache_resource
def get_cached_time():
# Initialize with a dictionary to store the last execution time
return {"last_executed_time": None}
def try_clear_cache():
# How often to clear the cache
CLEAR_DELTA = timedelta(hours=12)
current_time = datetime.now()
cached_time = get_cached_time()
# Check if the current time is different from the cached last execution time
if cached_time["last_executed_time"] is None or cached_time["last_executed_time"] + CLEAR_DELTA < current_time:
clear_mito_backend_cache()
cached_time["last_executed_time"] = current_time
def mito():
#st.write('MITO Sheet')
st.title(':blue[MITO Sheet]')
st.link_button("How to use", "https://docs.trymito.io/")
if 'df' in st.session_state:
df= st.session_state['df']
new_dfs, code = spreadsheet(df)
code = code if code else "# Edit the spreadsheet above to generate code"
st.code(code)
try_clear_cache()
else:
st.write('Please LOAD DATA')
#--END MITO SHEET
def jupyt():
jupyterlite(600, 800)
#--START DATAFRAME ANALYSIS
# Gets additional value such as min / median / max etc.
def column_summary_plus(df):
result_df = []
# Loop through each column in the DataFrame
for column in df.columns:
print(f"Start processing {column} col with {df[column].dtype} dtype")
# Get column dtype
col_dtype = df[column].dtype
# Get distinct values and their counts
value_counts = df[column].value_counts()
distinct_values = value_counts.index.tolist()
# Get number of distinct values
num_distinct_values = len(distinct_values)
# Get min and max values
sorted_values = sorted(distinct_values)
min_value = sorted_values[0] if sorted_values else None
max_value = sorted_values[-1] if sorted_values else None
# Get median value
non_distinct_val_list = sorted(df[column].dropna().tolist())
len_non_d_list = len(non_distinct_val_list)
if len(non_distinct_val_list) == 0:
median = None
else:
median = non_distinct_val_list[len_non_d_list//2]
# Get average value if value is number
if np.issubdtype(df[column].dtype, np.number):
if len(non_distinct_val_list) > 0:
average = sum(non_distinct_val_list)/len_non_d_list
non_zero_val_list = [v for v in non_distinct_val_list if v > 0]
average_non_zero = sum(non_zero_val_list)/len_non_d_list
else:
average = None
average_non_zero = None
else:
average = None
average_non_zero = None
# Check if null values are present
null_present = 1 if df[column].isnull().any() else 0
# Get number of nulls and non-nulls
num_nulls = df[column].isnull().sum()
num_non_nulls = df[column].notnull().sum()
# Distinct_values only take top 10 distinct values count
top_10_d_v = value_counts.head(10).index.tolist()
top_10_c = value_counts.head(10).tolist()
top_10_d_v_dict = dict(zip(top_10_d_v,top_10_c))
# Append the information to the result DataFrame
result_df.append({'col_name': column,
'col_dtype': col_dtype,
'num_distinct_values': num_distinct_values,
'min_value': min_value,
'max_value': max_value,
'median_no_na': median,
'average_no_na': average,
'average_non_zero': average_non_zero,
'null_present': null_present,
'nulls_num': num_nulls,
'non_nulls_num': num_non_nulls,
'distinct_values': top_10_d_v_dict
})
result_df_out= pd.DataFrame(result_df)
return result_df_out
def dataframe_analysis():
#st.write('MITO Sheet')
st.title(':blue[Dataframe Analysis] 🎛️')
if 'df' in st.session_state:
df= st.session_state['df']
#summary_df = column_summary(df)
#st.write(summary_df)
summary_df_plus = column_summary_plus(df)
st.write(summary_df_plus)
numerical_columns = df.select_dtypes(include=[np.number]).columns
# Perform univariate analysis on numerical columns
for column in numerical_columns:
# For continuous variables
if len(df[column].unique()) > 10: # Assuming if unique values > 10, consider it continuous
fig1=plt.figure(figsize=(8, 6))
sns.histplot(df[column], kde=True, color = "darkgreen", ec="black")
plt.title(f'Histogram of {column}')
plt.xlabel(column)
plt.ylabel('Frequency')
st.pyplot(fig1)
else: # For discrete or ordinal variables
fig1=plt.figure(figsize=(8, 6))
ax = sns.countplot(x=column, data=df)
plt.title(f'Count of {column}')
plt.xlabel(column)
plt.ylabel('Count')
# Annotate each bar with its count
for p in ax.patches:
ax.annotate(format(p.get_height(), '.0f'),
(p.get_x() + p.get_width() / 2., p.get_height()),
ha = 'center', va = 'center',
xytext = (0, 5),
textcoords = 'offset points')
st.pyplot(fig1)
else:
st.write('Please LOAD DATA')
#--END DATAFRAME ANALYSIS
def loadPgwalker():
#st.write('Pgwalker')
st.title(':blue[Pygwalker]')
st.link_button("How to use", "https://www.youtube.com/watch?v=u0A-bcQHfmA")
if 'df' in st.session_state:
df= st.session_state['df']
# Generate the HTML using Pygwalker
pyg_html = pyw.to_html(df)
components.html(pyg_html,width=900, height=1000, scrolling=True)
clean_data()
else:
st.write('Please LOAD DATA')
def gob():
st.title(':blue[Aggrid Builder]')
if 'df' in st.session_state:
df= st.session_state['df']
from st_aggrid import AgGrid, GridOptionsBuilder
grid_builder = GridOptionsBuilder.from_dataframe(df)
grid_builder.configure_selection(selection_mode="multiple", use_checkbox=False)
#grid_builder.configure_pagination(enabled=False, paginationAutoPageSize=False, paginationPageSize=3)
grid_builder.configure_pagination(enabled=True, paginationAutoPageSize=False, paginationPageSize=10)
grid_builder.configure_side_bar(filters_panel=True, columns_panel=False)
grid_options = grid_builder.build()
AgGrid(data=df, gridOptions=grid_options, custom_css={
"#gridToolBar": {
"padding-bottom": "0px !important",
}
})
#clean_data()
else:
st.write('Please LOAD DATA')
def fromfile(fn,dirname):
counterHeader = 1
try:
f = open(dirname+'/'+fn,'r')
while True:
l=f.readline()
if l.find('//')==-1:
break
counterHeader += 1
except IOError:
sys.exit(-1)
data = pd.read_csv(dirname+'/'+fn,sep='\t',index_col=False, na_values=numpy.nan, skiprows = counterHeader-1)
data.columns = [c.replace(' ', '_') for c in data.columns]
data['Cruise'].fillna(method='ffill', inplace = True)
data['Station'].fillna(method='ffill', inplace = True)
data['Type'].fillna(method='ffill', inplace = True)
data['YYYY-MM-DDThh:mm:ss.sss'].fillna(method='ffill', inplace = True)
data['Longitude_[degrees_east]'].fillna(method='ffill', inplace = True)
data['Latitude_[degrees_north]'].fillna(method='ffill', inplace = True)
data['LOCAL_CDI_ID'].fillna(method='ffill', inplace = True)
data['EDMO_code'].fillna(method='ffill', inplace = True)
data['Bot._Depth_[m]'].fillna(method='ffill', inplace = True)
return data
def load_Odvs():
'''
loads the selected file into a dataframe
stores the selected file and dataframe in st.session_state
'''
st.title(':blue[Merge ODVs in a dataset] 📒')
st.write("You can load several ODVs to merge in a single dataframe")
files = st.file_uploader("Upload ODV files", type=['txt'], accept_multiple_files=True)
if 'dfodv' in st.session_state and st.session_state['dfodv'] is not None:
st.write('Working...')
else:
counterDummy=1
idrnd = uuid.uuid4()
dirname=str(time.strftime("%Y%m%d%H%M%S")+'-'+str(idrnd))
os.mkdir(dirname)
actualDir=os.getcwd()
for uploaded_file in files:
with open(os.path.join(dirname,uploaded_file.name),"wb") as f:
f.write(uploaded_file.getbuffer())
counterDummy=1
li=[]
for u in os.listdir(dirname):
data=fromfile(u,dirname)
li.append(data)
counterDummy += 1
if counterDummy >1:
dfodv = pd.concat(li)
st.session_state['dfodv'] = dfodv
st.dataframe(dfodv)
shutil.rmtree(dirname)
#--START MERGER
def extractmultiple(file_to_extract,separaval):
if file_to_extract.name.split(".")[-1] == "csv":
extracted_data = pd.read_csv(file_to_extract,sep=separaval)
elif file_to_extract.name.split(".")[-1] == 'json':
extracted_data = pd.read_json(file_to_extract, lines=True)
elif file_to_extract.name.split(".")[-1] == 'xml':
extracted_data = pd.read_xml(file_to_extract)
elif file_to_extract.name.split(".")[-1] == 'parquet':
extracted_data = pd.read_parquet(file_to_extract, engine='fastparquet')
elif file_to_extract.name.split(".")[-1] == 'nc':
import xarray as xr
ds = xr.open_dataset(file_to_extract)
extracted_data = ds.to_dataframe()
return extracted_data
def mergerdataset():
'''
loads the selected file into a dataframe
stores the selected file and dataframe in st.session_state
'''
st.title(':blue[Merge Datasets (csv,json,xml,parquet,netcdf)] ♨️')
st.caption("""
Load and Merge (multiple upload)""")
uploaded_files = st.file_uploader("Choose a file", accept_multiple_files=True)
dataframes = []
sepa = st.radio(
"If CSV specify the separator",
["COMMA", "TAB", "COLON", "SEMICOLON"])
if uploaded_files:
if sepa == 'COMMA':
separaval=','
if sepa == 'TAB':
sepa='\t'
if sepa == 'COLON':
separaval=':'
if sepa == 'SEMICOLON':
separaval=';'
for file in uploaded_files:
file.seek(0)
df = extractmultiple(file,separaval)
dataframes.append(df)
if len(dataframes) >= 1:
merged_df = pd.concat(dataframes, ignore_index=True, join='outer')