Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gauge viz #1

Merged
merged 4 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 59 additions & 59 deletions Dashboards/demo_streamlit.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,74 @@
# Source : https://discuss.streamlit.io/t/develop-a-dashboard-app-with-streamlit-using-plotly/37148/4
# run with : streamlit run Dashboards/demo_streamlit.py

# Finalement > prendre les données issue de la vue
lichen_ecology = df.get_lichen_ecology()
lichen_frequency = df.get_lichen_frequency()

# Debug data
# st.dataframe(df)
# Calcul somme des fréquences
def calc_frequences(df):
df_agg = df.groupby("main_lichenspecies").agg({
"id": "first",
"frequency": "sum"
}).reset_index()

# Calculer les proportions des espèces selon leur tolérance à l'anthropisation
artificialisation_proportions = lichen_ecology['poleotolerance'].value_counts(normalize=True) * 100
return df_agg

calc_freq = calc_frequences(lichen_frequency)
calc_freq = calc_freq[["main_lichenspecies", "frequency"]]

def deg_artif(id_site: int, species_name : str):
# Calcul filtrable
freq = lichen_frequency[lichen_frequency["id"] == id_site]["frequency"].values[0]
freq_g = calc_freq[calc_freq["main_lichenspecies"] == species_name]["frequency"].values[0]

return round(freq / freq_g * 100, 2)


# Sélection du site
id_site = st.selectbox(
"Sur quel site voulez-vous ?",
lichen_frequency["id"],
index=None,
placeholder="site n°",
)

# Sélection des espèces
species_name = st.selectbox(
"Sur quel espèce voulez-vous ?",
calc_freq["main_lichenspecies"],
index=None,
placeholder="Je sélectionne l'espèce...",
)

# Affichage des éléments
if id_site and species_name != None:
pass
else:
id_site = 460
species_name = "Physcia aipolia/stellaris"
# le calcul
artificialisation_proportions = deg_artif(id_site, species_name)

# # Dataviz charts
fig1 = go.Figure(go.Indicator(
domain = {'x': [0, 1], 'y': [0, 1]},
value = artificialisation_proportions["intermediate"],
value = artificialisation_proportions,
mode = "gauge+number",
title = {'text': "Degré d'artificialisation"},
gauge = {'axis': {'range': [0, 100], 'dtick': 25},
'bar': {'color': "#000000"},
'steps' : [
{'range': [0, 25], 'color': "#E3D7FF"},
{'range': [25, 50], 'color': "#AFA2FF"},
{'range': [50, 75], 'color': "#7A89C2"},
{'range': [75, 100], 'color': "#72788D"}
# {'range': [0, 25], 'color': "#E3D7FF"},
# {'range': [25, 50], 'color': "#AFA2FF"},
# {'range': [50, 75], 'color': "#7A89C2"},
# {'range': [75, 100], 'color': "#72788D"}
{'range': [0, 25], 'color': "green"},
{'range': [25, 50], 'color': "yellow"},
{'range': [50, 75], 'color': "orange"},
{'range': [75, 100], 'color': "red"}
],
'threshold' : {'line': {'color': "#000000", 'width': 4}, 'thickness': 0.75, 'value': artificialisation_proportions["intermediate"]}
'threshold' : {'line': {'color': "#000000", 'width': 4}, 'thickness': 0.75, 'value': artificialisation_proportions}
}))

x_values = [10, 8, 6, 5, 4, 3, 2, 1]
Expand Down Expand Up @@ -60,61 +105,16 @@
hand_length = np.sqrt(2) / 4
hand_angle = np.pi * (1 - (max(min_value, min(max_value, current_value)) - min_value) / (max_value - min_value))

## Version 3 non retenue du code python
# fig3 = go.Figure(
# data=[
# go.Pie(
# values=[0.5] + (np.ones(n_quadrants) / 2 / n_quadrants).tolist(),
# rotation=90,
# hole=0.5,
# marker_colors=quadrant_colors,
# textinfo="text",
# hoverinfo="skip",
# ),
# ],
# layout=go.Layout(
# showlegend=False,
# margin=dict(b=0,t=10,l=10,r=10),
# width=450,
# height=450,
# paper_bgcolor=plot_bgcolor,
# annotations=[
# go.layout.Annotation(
# text=f"<b>Degrés d'artificialisation:</b><br>{current_value} %",
# x=0.5, xanchor="center", xref="paper",
# y=0.25, yanchor="bottom", yref="paper",
# showarrow=False,
# )
# ],
# shapes=[
# go.layout.Shape(
# type="circle",
# x0=0.48, x1=0.52,
# y0=0.48, y1=0.52,
# fillcolor="#333",
# line_color="#333",
# ),
# go.layout.Shape(
# type="line",
# x0=0.5, x1=0.5 + hand_length * np.cos(hand_angle),
# y0=0.5, y1=0.5 + hand_length * np.sin(hand_angle),
# line=dict(color="#333", width=4)
# )
# ]
# )
# )

# Display streamlit
st.title("Dataviz POC")
tab1, tab2, tab3= st.tabs(["Gauge", "Histogram", "df debug"])
tab1, tab2, tab3= st.tabs(["Gauge", "Histogram", "df Fréquences"])
with tab1:
st.write("**Mode de calcul**`df['poleotolerance'].value_counts(normalize=True) * 100`")
st.write(f"Degrés d'artificialisation sur le site n°**{id_site}** pour l'espèce **{species_name}**")
st.plotly_chart(fig1)

with tab2:
st.plotly_chart(fig2)

with tab3:
st.write("Debug du dataset")
# st.dataframe(df)

st.write("les données de fréquences")
st.write(calc_freq)
19 changes: 15 additions & 4 deletions Dashboards/my_data/datasets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pandas as pd
from sqlalchemy import text
from my_data.db_connect import get_session
from my_data.model import Tree, TreeSpecies, Observation, Lichen, LichenSpecies, Environment, Table, LichenEcology
from my_data.model import Tree, TreeSpecies, Observation, Lichen, LichenSpecies, Environment, Table, LichenEcology, LichenFrequency

session = get_session()

Expand Down Expand Up @@ -100,7 +101,6 @@ def get_tree_species():
})
return pd.DataFrame(tree_species_data)

# Nouvelles données
def get_lichen_ecology():
data = session.query(LichenEcology).all()
lichen_ecology_data = []
Expand All @@ -111,6 +111,17 @@ def get_lichen_ecology():
"pH": lichen_ecology.pH,
"aridity": lichen_ecology.aridity,
"eutrophication": lichen_ecology.eutrophication,
"poleotolerance" : lichen_ecology.poleotolerance
"poleotolerance": lichen_ecology.poleotolerance
})
return pd.DataFrame(lichen_ecology_data)
return pd.DataFrame(lichen_ecology_data)

def get_lichen_frequency():
data = session.query(LichenFrequency).all()
lichen_frequency_data = []
for lichen_frequency in data:
lichen_frequency_data.append({
"id": lichen_frequency.id_site,
"main_lichenspecies": lichen_frequency.main_lichenspecies,
"frequency": lichen_frequency.frequency
})
return pd.DataFrame(lichen_frequency_data)
14 changes: 12 additions & 2 deletions Dashboards/my_data/model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from sqlalchemy import Column, BigInteger, Integer, String, ForeignKey, ARRAY, Boolean, Date, Text, Float
from sqlalchemy import Column, BigInteger, Integer, String, ForeignKey, ARRAY, Boolean, Date, Text, Float, text
from sqlalchemy.orm import declarative_base, relationship
from my_data.db_connect import engine

Base = declarative_base()

Expand Down Expand Up @@ -97,4 +98,13 @@ class LichenEcology(Base):
aridity = Column(String(255))
eutrophication = Column(String(255))
poleotolerance = Column(String(255))


# Table pour les fréquences
class LichenFrequency(Base):
__tablename__ = 'lichen_frequency'
__table_args__ = {'autoload_with': engine}

id = Column(BigInteger, primary_key=True, autoincrement=True)
id_site = Column(BigInteger)
main_lichenspecies = Column(String(255))
frequency = Column(BigInteger)
58 changes: 58 additions & 0 deletions Dashboards/my_data/sql_stuff.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
-- Listing des modifications faites sur la base de données
-- POC requête pour afficher les fréquences 17/07/2024
SELECT
o.id AS id_site,
ls.name AS main_lichenspecies,
COUNT(l.id) AS frequency
FROM
main_observation o
JOIN main_tree t ON o.id = t.observation_id
JOIN main_lichen l ON o.id = l.observation_id
JOIN main_lichenspecies ls ON l.species_id = ls.id

GROUP BY
o.id, ls.name

ORDER BY o.id, frequency DESC;

-- Pour la commandline PSQL
SELECT o.id AS id_site, ls.name AS main_lichenspecies, COUNT(l.id) AS frequency FROM main_observation o JOIN main_tree t ON o.id = t.observation_id JOIN main_lichen l ON o.id = l.observation_id JOIN main_lichenspecies ls ON l.species_id = ls.id GROUP BY o.id, ls.name ORDER BY o.id, frequency DESC;

-- Créer une table view récupérable dans le modèle intégrée dans la base de données le 17/07/2024
CREATE VIEW lichen_frequency AS
SELECT
o.id AS id_site,
ls.name AS main_lichenspecies,
COUNT(l.id) AS frequency
FROM
main_observation o
JOIN main_tree t ON o.id = t.observation_id
JOIN main_lichen l ON o.id = l.observation_id
JOIN main_lichenspecies ls ON l.species_id = ls.id
GROUP BY
o.id, ls.name
ORDER BY
o.id, frequency DESC;

-- Pour la commandline PSQL
CREATE VIEW lichen_frequency AS SELECT o.id AS id_site, ls.name AS main_lichenspecies, COUNT(l.id) AS frequency FROM main_observation o JOIN main_tree t ON o.id = t.observation_id JOIN main_lichen l ON o.id = l.observation_id JOIN main_lichenspecies ls ON l.species_id = ls.id GROUP BY o.id, ls.name ORDER BY o.id, frequency DESC;


-- Update SQL command
CREATE VIEW lichen_frequency AS
SELECT
ROW_NUMBER() OVER (ORDER BY o.id, ls.name) AS id,
o.id AS id_site,
ls.name AS main_lichenspecies,
COUNT(l.id) AS frequency
FROM
main_observation o
JOIN main_tree t ON o.id = t.observation_id
JOIN main_lichen l ON o.id = l.observation_id
JOIN main_lichenspecies ls ON l.species_id = ls.id
GROUP BY
o.id, ls.name
ORDER BY
o.id, frequency DESC;

CREATE VIEW lichen_frequency AS SELECT ROW_NUMBER() OVER (ORDER BY o.id, ls.name) AS id, o.id AS id_site, ls.name AS main_lichenspecies, COUNT(l.id) AS frequency FROM main_observation o JOIN main_tree t ON o.id = t.observation_id JOIN main_lichen l ON o.id = l.observation_id JOIN main_lichenspecies ls ON l.species_id = ls.id GROUP BY o.id, ls.name ORDER BY o.id, frequency DESC;
37 changes: 20 additions & 17 deletions Dashboards/tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
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_observation_data, get_table_data, get_tree_data, get_tree_species, get_lichen_ecology
from my_data.datasets import get_environment_data, get_lichen_data, get_lichen_species_data, get_observation_data, get_table_data, get_tree_data, get_tree_species, get_lichen_ecology, get_lichen_frequency

session = get_session()

Expand All @@ -19,28 +19,31 @@
tree_df = get_tree_data()
tree_species_df = get_tree_species()
lichen_ecology_df = get_lichen_ecology()
lichen_frequency_df = get_lichen_frequency()

# Affichage des datasets > test dataset
print("\nEnvironment Data")
print(environment_df.head())
# print("\nEnvironment Data")
# print(environment_df.head())

print("\nLichen Data")
print(lichen_df.head())
# print("\nLichen Data")
# print(lichen_df.head())

print("\nLichen Species Data")
print(lichen_species_df.head())
# print("\nLichen Species Data")
# print(lichen_species_df.head())

print("\nObservation Data")
print(observation_df.head())
# print("\nObservation Data")
# print(observation_df.head())

print("\nTable Data")
print(table_df.head())
# print("\nTable Data")
# print(table_df.head())

print("\nTree Data")
print(tree_df.head())
# print("\nTree Data")
# print(tree_df.head())

print("\nTree Species Data")
print(tree_species_df.head())
# print("\nTree Species Data")
# print(tree_species_df.head())

print("\nLichen Ecology Data")
print(lichen_ecology_df)
# print("\nLichen Ecology Data")
# print(lichen_ecology_df)

# Correction de l'appel de méthode
Loading