diff --git a/Packages.py b/Packages.py index ec7d83f23abc877b99e5eb07c3abc95a2280edba..fe21dcae4da468f573c90071dc6e646c642a9359 100644 --- a/Packages.py +++ b/Packages.py @@ -64,6 +64,8 @@ import joblib from hyperopt import fmin, hp, tpe, Trials, space_eval, STATUS_OK, anneal - - +import plotly.graph_objects as go +import plotly.express as px +import plotly.io as pio +import streamlit as st st.set_option('deprecation.showPyplotGlobalUse', False) diff --git a/Report/figures/Elbow.png b/Report/figures/Elbow.png new file mode 100644 index 0000000000000000000000000000000000000000..213645e6d6f619181e5ba2d285946ef481bac69a Binary files /dev/null and b/Report/figures/Elbow.png differ diff --git a/Report/figures/Influence_plot.png b/Report/figures/Influence_plot.png new file mode 100644 index 0000000000000000000000000000000000000000..03f8edf8afc00a7e54349750c3c1b795902c519c Binary files /dev/null and b/Report/figures/Influence_plot.png differ diff --git a/Report/figures/Spectra_Plot.png b/Report/figures/Spectra_Plot.png new file mode 100644 index 0000000000000000000000000000000000000000..b35f6dd6f5a24825054b640329f9dcb6f5747622 Binary files /dev/null and b/Report/figures/Spectra_Plot.png differ diff --git a/Report/figures/graphe.png b/Report/figures/graphe.png new file mode 100644 index 0000000000000000000000000000000000000000..3a7ad7924e3754459d2b9ab43f1515df6d201b19 Binary files /dev/null and b/Report/figures/graphe.png differ diff --git a/Report/figures/graphe_hotelling.png b/Report/figures/graphe_hotelling.png new file mode 100644 index 0000000000000000000000000000000000000000..31311510c2f3197203aee699a837ddb5c195992b Binary files /dev/null and b/Report/figures/graphe_hotelling.png differ diff --git a/Report/figures/graphe_loadings.png b/Report/figures/graphe_loadings.png new file mode 100644 index 0000000000000000000000000000000000000000..b349077cfcd03696e8e42abdc810bac81d1aad33 Binary files /dev/null and b/Report/figures/graphe_loadings.png differ diff --git a/Report/figures/scores_plot_2d_axis1_axis2.png b/Report/figures/scores_plot_2d_axis1_axis2.png new file mode 100644 index 0000000000000000000000000000000000000000..1814cded91b834b285496cfa0140e417e1d9a8a0 Binary files /dev/null and b/Report/figures/scores_plot_2d_axis1_axis2.png differ diff --git a/Report/figures/scores_plot_2d_axis1_axis3.png b/Report/figures/scores_plot_2d_axis1_axis3.png new file mode 100644 index 0000000000000000000000000000000000000000..606d628e5f24e88504d90067efa6cd1335fbfea7 Binary files /dev/null and b/Report/figures/scores_plot_2d_axis1_axis3.png differ diff --git a/Report/figures/scores_plot_2d_axis2_axis3.png b/Report/figures/scores_plot_2d_axis2_axis3.png new file mode 100644 index 0000000000000000000000000000000000000000..1b9207f30458a794c2b94a28927f87ae81841199 Binary files /dev/null and b/Report/figures/scores_plot_2d_axis2_axis3.png differ diff --git a/app.py b/app.py index 027393b8079257a75cae2893f29cd52e15db1c8a..3ae15c08841d69be3ba72b75a3737d00269f65e6 100644 --- a/app.py +++ b/app.py @@ -6,6 +6,16 @@ from Modules import * from Class_Mod.DATA_HANDLING import * +# HTML pour le bandeau "CEFE - CNRS" +bandeau_html = """ +<div style="width: 100%; background-color: #4682B4; padding: 10px; margin-bottom: 10px;"> + <h1 style="text-align: center; color: white;">CEFE - CNRS</h1> +</div> +""" + +# Injecter le code HTML du bandeau +st.markdown(bandeau_html, unsafe_allow_html=True) + # # TOC menu on the left show_pages( [Page("app.py", "Home"), @@ -18,6 +28,7 @@ hide_pages("Samples Selection") hide_pages("Models Creation") hide_pages("Predictions") + with st.sidebar: interface = st.selectbox(label="Interface", options=['simple', 'advanced'], key='interface') st.page_link('pages\\1-samples_selection.py') @@ -35,17 +46,35 @@ with st.sidebar: st.page_link('pages\\2-model_creation.py') st.page_link('pages\\3-prediction.py') + # Page header with st.container(): - st.subheader("Plateforme d'Analyses Chimiques pour l'Ecologie-PACE :goat:") - st.title("NIRS Utils") - st.write("Samples selection (PCA, [UMAP](https://umap-learn.readthedocs.io/en/latest/how_umap_works.html), ...), Predictive Modelling ([Pinard](https://github.com/GBeurier/pinard), [LWPLSR](https://doi.org/10.1002/cem.3209), ...), and Predictions using your data (CSV or DX files) and/or PACE NIRS Database.") - #st.image(img_general) - st.markdown("### We could add documentation here ###") + + # Centrer les boutons + st.markdown( + """ + <style> + .stButton>button { + display: block; + margin: 0 auto; + width: 200px; + height: 50px; + font-size: 16px; + } + </style> + """, + unsafe_allow_html=True + ) + header1, header2, header3 = st.columns(3) if header1.button("Samples Selection"): st.switch_page('pages\\1-samples_selection.py') if header2.button("Model Creation"): st.switch_page('pages\\2-model_creation.py') if header3.button("Predictions"): - st.switch_page('pages\\3-prediction.py') \ No newline at end of file + st.switch_page('pages\\3-prediction.py') + st.subheader("Plateforme d'Analyses Chimiques pour l'Ecologie-PACE :goat:") + st.title("NIRS Utils") + st.write("Samples selection (PCA, [UMAP](https://umap-learn.readthedocs.io/en/latest/how_umap_works.html), ...), Predictive Modelling ([Pinard](https://github.com/GBeurier/pinard), [LWPLSR](https://doi.org/10.1002/cem.3209), ...), and Predictions using your data (CSV or DX files) and/or PACE NIRS Database.") + #st.image(img_general) + st.markdown("### We could add documentation here ###") \ No newline at end of file diff --git a/pages/1-samples_selection.py b/pages/1-samples_selection.py index 08d8cb6e364ee4b7a55b00dbab272583e1ab2c4e..035240caa8996c5571ca58b0bc38c910b90d0b8e 100644 --- a/pages/1-samples_selection.py +++ b/pages/1-samples_selection.py @@ -3,6 +3,19 @@ st.set_page_config(page_title="NIRS Utils", page_icon=":goat:", layout="wide") from Modules import * from Class_Mod.DATA_HANDLING import * + + +# HTML pour le bandeau "CEFE - CNRS" +bandeau_html = """ +<div style="width: 100%; background-color: #4682B4; padding: 10px; margin-bottom: 10px;"> + <h1 style="text-align: center; color: white;">CEFE - CNRS</h1> +</div> +""" + + +# Injecter le code HTML du bandeau +st.markdown(bandeau_html, unsafe_allow_html=True) + st.session_state["interface"] = st.session_state.get('interface') if st.session_state["interface"] == 'simple': hide_pages("Predictions") @@ -54,13 +67,12 @@ if data_file: st.success("The data have been loaded successfully", icon="✅") os.unlink(tmp_path) - ## Visualize spectra if not spectra.empty: with col2: fig = plot_spectra(spectra) st.pyplot(fig) - + fig.savefig("./Report/figures/Spectra_Plot.png") ############################## Exploratory data analysis ############################### container2 = st.container(border=True) @@ -102,8 +114,13 @@ if not t.empty: if clus_method == cluster_methods[1]: ncluster = scores.number_input(min_value=2, max_value=30, value=3, label = 'Select the desired number of clusters') cl_model = Sk_Kmeans(tcr, max_clusters = 30) - fig2 = px.scatter(cl_model.inertia_.T, y = 'inertia') + fig2 = px.scatter(cl_model.inertia_.T, y = 'inertia') scores.plotly_chart(fig2) + img = pio.to_image(fig2, format="png") + with open("./Report/figures/Elbow.png", "wb") as f: + f.write(img) + + data, labels = cl_model.fit_optimal(nclusters = ncluster) elif clus_method == cluster_methods[2]: @@ -119,7 +136,7 @@ if not t.empty: # scores plot with clustering if list(labels) and meta_data.empty: fig = px.scatter_3d(tcr, x=axis1, y=axis2, z = axis3, color = labels) - + # scores plot with metadata elif len(list(labels)) == 0 and not meta_data.empty: filter = meta_data.columns[1:] @@ -151,26 +168,63 @@ if not t.empty: st.plotly_chart(fig) +## Export en 2d Axe1..Axe3 +if not t.empty: + if dim_red_method == dim_red_methods[1]: + + # nombre de clusters + num_clusters = len(np.unique(labels)) + + # Une couleur par cluster + custom_color_palette = px.colors.qualitative.Plotly[:num_clusters] + + # Graphique pour les dimensions (axis1, axis2) + fig_2d_axis1_axis2 = px.scatter(t, x=axis1, y=axis2, color=labels, color_discrete_sequence=custom_color_palette) + img_2d_axis1_axis2 = pio.to_image(fig_2d_axis1_axis2, format="png") + with open("./Report/figures/scores_plot_2d_axis1_axis2.png", "wb") as f: + f.write(img_2d_axis1_axis2) + + # Graphique pour les dimensions (axis1, axis3) + fig_2d_axis1_axis3 = px.scatter(t, x=axis1, y=axis3, color=labels, color_discrete_sequence=custom_color_palette) + img_2d_axis1_axis3 = pio.to_image(fig_2d_axis1_axis3, format="png") + with open("./Report/figures/scores_plot_2d_axis1_axis3.png", "wb") as f: + f.write(img_2d_axis1_axis3) + + # Graphique pour les dimensions (axis2, axis3) + fig_2d_axis2_axis3 = px.scatter(t, x=axis2, y=axis3, color=labels, color_discrete_sequence=custom_color_palette) + img_2d_axis2_axis3 = pio.to_image(fig_2d_axis2_axis3, format="png") + with open("./Report/figures/scores_plot_2d_axis2_axis3.png", "wb") as f: + f.write(img_2d_axis2_axis3) if not spectra.empty: if dim_red_method == dim_red_methods[1]: + with loadings: st.write('Loadings plot') p = dr_model.loadings_ - pp = pd.concat([p, pd.DataFrame(np.arange(p.shape[0]), index=p.index, columns=['wl'])], axis =1) + pp = pd.concat([p, pd.DataFrame(np.arange(p.shape[0]), index=p.index, columns=['wl'])], axis=1) df1 = pp.melt(id_vars="wl") - fig = px.line(df1, x = 'wl', y = 'value', color='variable') - fig.update_layout(legend=dict(x=1, y=0,font=dict(family="Courier", size=12, color="black"), - bordercolor="Black", borderwidth=2)) - st.plotly_chart(fig, use_container_width = True) + fig = px.line(df1, x='wl', y='value', color='variable', color_discrete_sequence=px.colors.qualitative.Plotly) + fig.update_layout(legend=dict(x=1, y=0, font=dict(family="Courier", size=12, color="black"), + bordercolor="black", borderwidth=2)) + st.plotly_chart(fig, use_container_width=True) + + # Export du graphique + img = pio.to_image(fig, format="png") + with open("./Report/figures/graphe_loadings.png", "wb") as f: + f.write(img) with influence: st.write('Influence plot') ax1 = st.selectbox("Component", options=dr_model.scores_.columns, index=3) leverage = dr_model.leverage_ residuals = dr_model.residuals_ - fig = px.scatter(x=leverage[ax1], y=residuals[ax1], color = leverage[ax1]*residuals[ax1]).update_layout(xaxis_title="Leverage",yaxis_title="Residuals") + fig = px.scatter(x=leverage[ax1], y=residuals[ax1], color=leverage[ax1]*residuals[ax1], color_continuous_scale='Blues') + fig.update_layout(xaxis_title="Leverage", yaxis_title="Residuals") st.plotly_chart(fig) + img = pio.to_image(fig, format="png") + with open("./Report/figures/Influence_plot.png", "wb") as f: + f.write(img) with hotelling: st.write('T²-Hotelling vs Q residuals plot') @@ -179,4 +233,5 @@ if not spectra.empty: hotelling = dr_model.hotelling_ fig = px.scatter(t, x=hotelling[ax2], y=residuals[ax2]).update_layout(xaxis_title="T²",yaxis_title="Residuals") - st.plotly_chart(fig) \ No newline at end of file + st.plotly_chart(fig) + fig.write_image("./Report/figures/graphe_hotelling.png", format="png") diff --git a/pages/2-model_creation.py b/pages/2-model_creation.py index 3f506ea752eda5ebf0460f6b53ca8a24015225ec..9fac8a2212a53a02f7b9ea0f26bc9cc033f39432 100644 --- a/pages/2-model_creation.py +++ b/pages/2-model_creation.py @@ -2,7 +2,15 @@ from Packages import * st.set_page_config(page_title="NIRS Utils", page_icon=":goat:", layout="wide") from Modules import * from Class_Mod.DATA_HANDLING import * - +# HTML pour le bandeau "CEFE - CNRS" +bandeau_html = """ +<div style="width: 100%; background-color: #4682B4; padding: 10px; margin-bottom: 10px;"> + <h1 style="text-align: center; color: white;">CEFE - CNRS</h1> +</div> +""" + +# Injecter le code HTML du bandeau +st.markdown(bandeau_html, unsafe_allow_html=True) st.session_state["interface"] = st.session_state.get('interface') if st.session_state["interface"] == 'simple': diff --git a/pages/3-prediction.py b/pages/3-prediction.py index 65130fd1dfcdde9f491dc7f8eaee4e19817ddc55..16fe404830d28abe22a98d822d72b391da909583 100644 --- a/pages/3-prediction.py +++ b/pages/3-prediction.py @@ -2,7 +2,15 @@ from Packages import * st.set_page_config(page_title="NIRS Utils", page_icon=":goat:", layout="wide") from Modules import * from Class_Mod.DATA_HANDLING import * +# HTML pour le bandeau "CEFE - CNRS" +bandeau_html = """ +<div style="width: 100%; background-color: #4682B4; padding: 10px; margin-bottom: 10px;"> + <h1 style="text-align: center; color: white;">CEFE - CNRS</h1> +</div> +""" +# Injecter le code HTML du bandeau +st.markdown(bandeau_html, unsafe_allow_html=True) st.session_state["interface"] = st.session_state.get('interface')