diff --git a/Dashboards/visualisation/dash_hist4_pxbar.py b/Dashboards/visualisation/dash_hist4_pxbar.py new file mode 100644 index 0000000..4986a8c --- /dev/null +++ b/Dashboards/visualisation/dash_hist4_pxbar.py @@ -0,0 +1,138 @@ +import pandas as pd +import numpy as np +import base64 + +import sys +from pathlib import Path + +import plotly.express as px + +from dash import Dash, dcc, html +from dash.dependencies import Output, Input + +# Ajoute le dossier parent à sys.path +chemin_dossier_parent = Path(__file__).parent.parent +sys.path.append(str(chemin_dossier_parent)) +from my_data.db_connect import get_session +from my_data.datasets import get_environment_data, get_lichen_data, get_lichen_species_data, get_lichen_ecology + +# image file path +image_path = 'C:/Users/Galinette/Documents/GitHub/lichensgo/Dashboards/visualisation/xanthoria parietina.jpg' +encoded_image = base64.b64encode(open(image_path, 'rb').read()) + +# Initialize Dash app +app = Dash(__name__) + +# Load datasets +environment_df = get_environment_data() +lichen_df = get_lichen_data() +lichen_species_df = get_lichen_species_data() +lichen_ecology_df = get_lichen_ecology() + +print("\n Lichen ecology info:") +print(lichen_ecology_df.head()) + +print("\n Taxon") +print(lichen_ecology_df.loc[20,"taxon"]) + +# group by species' type + add a column with the count occurence +df_grouped=( + lichen_df + .groupby("species_id", as_index=False) + .agg(count_col=pd.NamedAgg(column="species_id", aggfunc="count")) +) + +# concatenate dataframe "df_grouped" with the lichen species' names +df_grouped_species=pd.concat([df_grouped, lichen_species_df.loc[:,"name"]], axis=1) + +# sort based on occurence +# (note): update_xaxes(categoryorder="total descending") does not work +df_grouped_species=( + df_grouped_species + .sort_values(by="count_col", ascending=False, ignore_index=True) +) + +# Define app layout +app.layout = html.Div([ + html.H1("Select the Lichen's species"), + dcc.Dropdown(id="dropdown_Lichen", + options=lichen_species_df["name"].unique(), + value="Xanthoria parietina" + ), + html.H2("Ecology information of the selected lichen:"), + html.H4(id="info_taxon"), + html.H4(id="info_pH"), + html.H4(id="info_aridity"), + html.Img(src='data:image/png;base64,{}'.format(encoded_image.decode()), + style={ + 'width': '10%', + 'height': '10%', + }), + dcc.Graph(id="hist4") +]) + +# Define callback to update graph +@app.callback( + Output("info_taxon", "children"), + Output("info_pH", "children"), + Output("info_aridity", "children"), + #Output("image", "src"), + Output("hist4", "figure"), + Input("dropdown_Lichen", "value") +) +def hist4_interactive(Lichen_selected): + + # index in "df_grouped_species" corresponding to the selected species + idx=df_grouped_species["name"].loc[lambda x: x==Lichen_selected].index + + # adjust the color based on the selected species + color_discrete_sequence=['#ec7c34']*len(df_grouped_species) + color_discrete_sequence[int(idx[0])]='#609cd4' + + hist4=px.bar( + df_grouped_species, + x="count_col", + y="name", + orientation="h", + color="name", + color_discrete_sequence=color_discrete_sequence, + title="Espèces les plus observées par les observateurs Lichens GO" + ) + + # remove the legend + hist4.update(layout_showlegend=False) + + # update the layout + hist4.update_layout( + title_font=dict(color="grey",size=24), + title={"x": .5,"y": .95,"xanchor": "center"}, + plot_bgcolor='white', + paper_bgcolor="white", + width=1100, + height=800, + ) + + # update axes + hist4.update_xaxes( + title="Count", + showline=True, + linecolor='black', + gridcolor="black", + ) + + hist4.update_yaxes( + title="", + showline=True, + linecolor='black', + ) + + info_taxon =f" Taxon : {lichen_ecology_df.loc[int(idx[0]),"taxon"]}" + info_pH =f" pH : {lichen_ecology_df.loc[int(idx[0]),"pH"]}" + info_aridity =f" Aridity : {lichen_ecology_df.loc[int(idx[0]),"aridity"]}" + + return info_taxon, info_pH, info_aridity, hist4 + +# Run the app +if __name__ == "__main__": + app.run(debug=True,host="127.0.0.1",port=8050) + #app.run() \ No newline at end of file diff --git a/Dashboards/visualisation/hist3_pxbar.py b/Dashboards/visualisation/hist3_pxbar.py new file mode 100644 index 0000000..e4194cf --- /dev/null +++ b/Dashboards/visualisation/hist3_pxbar.py @@ -0,0 +1,171 @@ +import pandas as pd +import sys +from pathlib import Path +import plotly +import plotly.express as px + +# Ajoute le dossier parent à sys.path +chemin_dossier_parent = Path(__file__).parent.parent +sys.path.append(str(chemin_dossier_parent)) +from my_data.db_connect import get_session +from my_data.datasets import get_environment_data, get_lichen_data, get_lichen_species_data, get_tree_data, get_observation_data, get_table_data + +session = get_session() + +# Récupération des datasets +environment_df = get_environment_data() +lichen_df = get_lichen_data() +lichen_species_df = get_lichen_species_data() +observation_df = get_observation_data() +table_df = get_table_data() +tree_df = get_tree_data() + +# Affichage des datasets +# print("\nEnvironment Data") +# print(environment_df.head()) + +# print("\nLichen Data") +# print(lichen_df.head()) + +# print("\nLichen Species Data") +# print(lichen_species_df.head()) + +# print("\nObservation Data") +# print(observation_df.head()) + +print("\nTable Data") +print(table_df.head()) + +# print("\nTree Data") +# print(tree_df.head()) + +# Select one site based on latitude, longitude, date et user id +# Filtering with query method +user = 2 +date = "2022-01-20" +lat = 45.822677 +long = 1.242670 +obs = 500 + +site = observation_df.query("user_id == @user" + and "date_obs == @date" + and "localisation_lat == @lat" + and "localisation_long == @long") + +# Tree data of the selected site +site_tree = tree_df.query("observation_id == @obs") + +print("\nTree Data of the selected site") +print(site_tree) + +# Lichen data of the selected site +site_lichen = lichen_df.query("observation_id == @obs") + +print("\nLichen Data of the selected site") +print(site_lichen) + +# Table data of the selected site +lichen_species = site_lichen["id"].unique() + +# For each lichen observed per site, count the total number of quadrat +quadrat = [] + +for i in lichen_species: + site_lichen_table = table_df[table_df["lichen_id"] == i] + + print("\nTable Data lichen") + print(site_lichen_table) + + # Sum of the non-empty quadrat + sum_sq1 = site_lichen_table['sq1'].sum() + sum_sq2 = site_lichen_table['sq2'].sum() + sum_sq3 = site_lichen_table['sq3'].sum() + sum_sq4 = site_lichen_table['sq4'].sum() + sum_sq5 = site_lichen_table['sq5'].sum() + + sum_quadrat = sum_sq1 + sum_sq2 + sum_sq3 + sum_sq4 + sum_sq5 + + # Letters to check + letters_to_check = ['N', 'E', 'S', 'O'] + + # Count based on the orientation + count_dict = {} + for letter in sum_quadrat: + if letter in count_dict: + count_dict[letter] += 1 + else: + count_dict[letter] = 1 + + # Complete with 0 for any missing orientation + result_dict = [] + for letter in letters_to_check: + if letter not in count_dict: + result_dict.append(0) + else: + result_dict.append(count_dict[letter]) + + # print("\nNon-empty quadrat") + # print(sum_quadrat) + + # Append the results to the quadrat output + quadrat.append({"id": i, + "sum_quadrat":len(sum_quadrat), + "N":result_dict[0], + "E":result_dict[1], + "S":result_dict[2], + "O":result_dict[3]}) + +# Convert the quadrat list to a DataFrame +quadrat_df = pd.DataFrame(quadrat) + +print("\nLichen Data of the selected site with orientation") +print(quadrat_df) + +# Merge the DataFrame +site_lichen_quadrat = pd.merge(site_lichen, quadrat_df) + +### Histogram 3 with Plotly express bar: ### +# Espèces observées sur le site sélectionné + +# concatenate dataframe "site_lichen_quadrat" with the lichen species' names +idx = site_lichen_quadrat["species_id"] +df = lichen_species_df.loc[idx-1,"name"].reset_index(drop=True) + +site_lichen_quadrat_species=pd.concat([site_lichen_quadrat, df], axis=1) + +# sort based on occurence +# (note): update_xaxes(categoryorder="total descending") does not work +site_lichen_quadrat_species=( + site_lichen_quadrat_species + .sort_values(by="sum_quadrat", ignore_index=True) +) + +print("\n Final table for histogram") +print(site_lichen_quadrat_species) + +### Design bar plot ### + +hist3=px.bar( + site_lichen_quadrat_species, + x=["N", "E", "S", "O"], + y="name", + orientation="h", + # width=1500, + # height=800, + title="Espèces observées sur le site sélectionné" +) + +# remove the legend +#hist3.update(layout_showlegend=False) + +# update the title +hist3.update_layout( + title_font=dict(color="grey",size=20), + title={"x": .5,"y": .9,"xanchor": "center"}, +) + +# update axes +hist3.update_xaxes(title="Count",showgrid=False) +hist3.update_yaxes(title="") + +hist3.show() \ No newline at end of file diff --git a/Dashboards/visualisation/hist4_pxbar.py b/Dashboards/visualisation/hist4_pxbar.py new file mode 100644 index 0000000..bb42883 --- /dev/null +++ b/Dashboards/visualisation/hist4_pxbar.py @@ -0,0 +1,104 @@ +import pandas as pd +import sys +from pathlib import Path +import plotly +import plotly.express as px + +# Ajoute le dossier parent à sys.path +chemin_dossier_parent = Path(__file__).parent.parent +sys.path.append(str(chemin_dossier_parent)) +from my_data.db_connect import get_session +from my_data.datasets import get_environment_data, get_lichen_data, get_lichen_species_data + +session = get_session() + +# Récupération des datasets +environment_df = get_environment_data() +lichen_df = get_lichen_data() +lichen_species_df = get_lichen_species_data() + +# Affichage des datasets > test dataset +print("\nEnvironment Data") +print(environment_df.head()) + +print("\nLichen Data") +print(lichen_df.head()) + +print("\nLichen Species Data") +print(lichen_species_df.head()) + +### Histogram 4 with Plotly express bar: ### +# Espèces les plus observées par les observateurs Lichens GO + +# group by species' type + add a column with the count occurence +df_grouped=( + lichen_df + .groupby("species_id", as_index=False) + .agg(count_col=pd.NamedAgg(column="species_id", aggfunc="count")) +) + +# concatenate dataframe "df_grouped" with the lichen species' names +df_grouped_species=pd.concat([df_grouped, lichen_species_df.loc[:,"name"]], axis=1) + +# sort based on occurence +# (note): update_xaxes(categoryorder="total descending") does not work +df_grouped_species=( + df_grouped_species + .sort_values(by="count_col", ascending=False, ignore_index=True) +) + +print("df_grouped_species:\n") +print(df_grouped_species) + +### Design bar plot ### + +# TODO: the user's selection should be interactive -> to modify in the final Dash +user_selection_species=lichen_species_df.loc[30,"name"] +print("Species selected by the user:",user_selection_species) + +# index in "df_grouped_species" corresponding to the selected species +idx=df_grouped_species["name"].loc[lambda x: x==user_selection_species].index +#print("idx is:",idx) + +# adjust the color based on the selected species +color_discrete_sequence=['#ec7c34']*len(df_grouped_species) +color_discrete_sequence[int(idx[0])]='#609cd4' + +hist4=px.bar( + df_grouped_species, + x="count_col", + y="name", + orientation="h", + color="name", + color_discrete_sequence=color_discrete_sequence, + # width=1500, + # height=800, + title="Espèces les plus observées par les observateurs Lichens GO" +) + +# remove the legend +hist4.update(layout_showlegend=False) + +# update the title +hist4.update_layout( + title_font=dict(color="grey",size=24), + title={"x": .5,"y": .95,"xanchor": "center"}, + plot_bgcolor='white', + paper_bgcolor="white" +) + +# update axes +hist4.update_xaxes( + title="Count", + showline=True, + linecolor='black', + gridcolor="black", +) + +hist4.update_yaxes( + title="", + showline=True, + linecolor='black', +) + +hist4.show() \ No newline at end of file diff --git a/Dashboards/visualisation/xanthoria parietina.jpg b/Dashboards/visualisation/xanthoria parietina.jpg new file mode 100644 index 0000000..ed45daf Binary files /dev/null and b/Dashboards/visualisation/xanthoria parietina.jpg differ