Nicolas Pierson commited on
^^
Browse files
src/pages/3_Historical_Risk_Score_Fire.py
CHANGED
|
@@ -55,17 +55,27 @@ def load_all_data_wrapper_historical_fire_scores_region():
|
|
| 55 |
return asyncio.run(_load_all_data_async_internal_communes())
|
| 56 |
|
| 57 |
# --- Streamlit App Layout ---
|
| 58 |
-
st.set_page_config(page_title="Oasis", page_icon=":
|
| 59 |
|
| 60 |
-
st.header("Historical
|
| 61 |
-
st.subheader("An overview of real estate prices in France from 2015 to 2024")
|
| 62 |
|
| 63 |
-
st.write(
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
#
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
with st.spinner("Loading data and preparing maps..."):
|
| 71 |
(
|
|
@@ -84,67 +94,64 @@ with st.spinner("Loading data and preparing maps..."):
|
|
| 84 |
dataset_departements_housing_prices["code_departement"].astype(str).str.zfill(2)
|
| 85 |
)
|
| 86 |
|
| 87 |
-
|
| 88 |
-
"
|
| 89 |
-
options=dataset_departements_housing_prices["annee"].unique(),
|
| 90 |
-
format_func=lambda x: f"{x}",
|
| 91 |
-
)
|
| 92 |
-
|
| 93 |
-
top_departements = (
|
| 94 |
-
dataset_departements_housing_prices[
|
| 95 |
-
dataset_departements_housing_prices["annee"] == selected_year
|
| 96 |
-
]
|
| 97 |
-
.groupby("code_departement")["fire_score"]
|
| 98 |
.mean()
|
| 99 |
.reset_index()
|
| 100 |
.sort_values(by="fire_score", ascending=False)
|
| 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 |
-
st.dataframe(top_departements)
|
| 128 |
|
| 129 |
fig_france = display_choropleth_map_country(
|
| 130 |
dataset_departements_housing_prices,
|
| 131 |
departements_geojson,
|
| 132 |
metric_name="fire_score",
|
| 133 |
metric_description="Fire score",
|
| 134 |
-
title="Average Fire Score in French Departments
|
| 135 |
-
height=1400,
|
| 136 |
-
width=1400,
|
| 137 |
)
|
| 138 |
st.plotly_chart(fig_france, use_container_width=False)
|
| 139 |
-
st.write("Hover over the map to see detailed information for each department and year.")
|
| 140 |
-
st.write(
|
| 141 |
-
"Missing values are represented in light grey, while actual data is shown in a gradient from red (high prices) to green (low prices)."
|
| 142 |
-
)
|
| 143 |
-
st.write(
|
| 144 |
-
"Note: The color scale is customized to highlight missing values in light grey, while the actual data is represented using a reversed RdYlGn color scale, where red indicates higher prices and green indicates lower prices."
|
| 145 |
-
)
|
| 146 |
|
| 147 |
-
|
|
|
|
|
|
|
| 148 |
|
| 149 |
with st.spinner("Loading data and preparing maps..."):
|
| 150 |
(dataset_housing_prices, communes_geojson, insee_df) = (
|
|
@@ -172,6 +179,12 @@ selected_departement = st.selectbox(
|
|
| 172 |
else x,
|
| 173 |
)
|
| 174 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
fig_department = display_choropleth_map_for_department(
|
| 176 |
dataset_housing_prices,
|
| 177 |
selected_departement,
|
|
@@ -180,20 +193,7 @@ fig_department = display_choropleth_map_for_department(
|
|
| 180 |
dataset_housing_prices["fire_score"].max(),
|
| 181 |
metric_name="fire_score",
|
| 182 |
metric_description="Fire score",
|
| 183 |
-
title=f"Average Fire Score in Department {
|
| 184 |
-
height_graph=1400,
|
| 185 |
-
width_graph=1400,
|
| 186 |
)
|
| 187 |
|
| 188 |
-
st.subheader("Average Price per Square Meter in French Communes (2015-2024)")
|
| 189 |
st.plotly_chart(fig_department, use_container_width=False)
|
| 190 |
-
st.write("Hover over the map to see detailed information for each commune and year.")
|
| 191 |
-
st.write(
|
| 192 |
-
"Missing values are represented in light grey, while actual data is shown in a gradient from red (high prices) to green (low prices)."
|
| 193 |
-
)
|
| 194 |
-
st.write(
|
| 195 |
-
"Note: The color scale is customized to highlight missing values in light grey, while the actual data is represented using a reversed RdYlGn color scale, where red indicates higher prices and green indicates lower prices."
|
| 196 |
-
)
|
| 197 |
-
st.write(
|
| 198 |
-
"The map is animated by year, allowing you to see how the average price per square meter changes over time."
|
| 199 |
-
)
|
|
|
|
| 55 |
return asyncio.run(_load_all_data_async_internal_communes())
|
| 56 |
|
| 57 |
# --- Streamlit App Layout ---
|
| 58 |
+
st.set_page_config(page_title="Oasis", page_icon=":fire:", layout="wide")
|
| 59 |
|
| 60 |
+
st.header("Historical Fire Risk Scores (2015-2024)")
|
|
|
|
| 61 |
|
| 62 |
+
st.write("""
|
| 63 |
+
The following scores are calculated based on various factors, including historical fire incidents, vegetation types, and climatic conditions. The scores are designed to help assess the fire risk in different regions of France over the years.
|
| 64 |
+
|
| 65 |
+
| Score | Description |
|
| 66 |
+
|-------|-------------------|
|
| 67 |
+
| 0 | No risk |
|
| 68 |
+
| 1 | Low risk |
|
| 69 |
+
| 2 | Medium risk |
|
| 70 |
+
| 3 | High risk |
|
| 71 |
+
""")
|
| 72 |
|
| 73 |
+
################################################################################
|
| 74 |
+
|
| 75 |
+
st.subheader(
|
| 76 |
+
"Top 7 Departments with the Highest Average Fire Risk Score",
|
| 77 |
+
divider=True,
|
| 78 |
+
)
|
| 79 |
|
| 80 |
with st.spinner("Loading data and preparing maps..."):
|
| 81 |
(
|
|
|
|
| 94 |
dataset_departements_housing_prices["code_departement"].astype(str).str.zfill(2)
|
| 95 |
)
|
| 96 |
|
| 97 |
+
top_departements_fire_scores = (
|
| 98 |
+
dataset_departements_housing_prices.groupby("code_departement")["fire_score"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
.mean()
|
| 100 |
.reset_index()
|
| 101 |
.sort_values(by="fire_score", ascending=False)
|
| 102 |
+
.head(7)
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
# Format the department codes and names
|
| 106 |
+
top_departements_fire_scores = top_departements_fire_scores.assign(
|
| 107 |
+
**{
|
| 108 |
+
"Department Name": lambda x: x["code_departement"].apply(
|
| 109 |
+
lambda code: insee_df[
|
| 110 |
+
_format_department_code(insee_df)["code_departement"] == code
|
| 111 |
+
]["nom_departement"].values[0]
|
| 112 |
+
if code in _format_department_code(insee_df)["code_departement"].values
|
| 113 |
+
else "Unknown"
|
| 114 |
+
)
|
| 115 |
+
}
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
# format the fire score
|
| 119 |
+
top_departements_fire_scores["fire_score"] = top_departements_fire_scores[
|
| 120 |
+
"fire_score"
|
| 121 |
+
].map("{:,.2f}".format)
|
| 122 |
+
top_departements_fire_scores = top_departements_fire_scores.rename(
|
| 123 |
+
columns={
|
| 124 |
+
"code_departement": "Department Code",
|
| 125 |
+
"fire_score": "Average Fire Score",
|
| 126 |
+
"Department Name": "Department Name",
|
| 127 |
+
}
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
st.dataframe(
|
| 131 |
+
top_departements_fire_scores,
|
| 132 |
+
hide_index=True,
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
###############################################################################
|
| 136 |
+
|
| 137 |
+
st.subheader("Average Fire Risk Scores in French Departments", divider=True)
|
| 138 |
+
|
| 139 |
+
st.write(
|
| 140 |
+
"This map shows the average fire risk scores in French departments over the years."
|
| 141 |
)
|
|
|
|
| 142 |
|
| 143 |
fig_france = display_choropleth_map_country(
|
| 144 |
dataset_departements_housing_prices,
|
| 145 |
departements_geojson,
|
| 146 |
metric_name="fire_score",
|
| 147 |
metric_description="Fire score",
|
| 148 |
+
title="Average Fire Score in French Departments",
|
|
|
|
|
|
|
| 149 |
)
|
| 150 |
st.plotly_chart(fig_france, use_container_width=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
+
###############################################################################
|
| 153 |
+
|
| 154 |
+
st.subheader("Average Fire Risk Scores in French Communes", divider=True)
|
| 155 |
|
| 156 |
with st.spinner("Loading data and preparing maps..."):
|
| 157 |
(dataset_housing_prices, communes_geojson, insee_df) = (
|
|
|
|
| 179 |
else x,
|
| 180 |
)
|
| 181 |
|
| 182 |
+
selected_departement_label = (
|
| 183 |
+
f"{selected_departement} - {insee_df[insee_df['code_departement'] == selected_departement]['nom_departement'].values[0]}"
|
| 184 |
+
if selected_departement in insee_df["code_departement"].values
|
| 185 |
+
else selected_departement
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
fig_department = display_choropleth_map_for_department(
|
| 189 |
dataset_housing_prices,
|
| 190 |
selected_departement,
|
|
|
|
| 193 |
dataset_housing_prices["fire_score"].max(),
|
| 194 |
metric_name="fire_score",
|
| 195 |
metric_description="Fire score",
|
| 196 |
+
title=f"Average Fire Score in Department {selected_departement_label}",
|
|
|
|
|
|
|
| 197 |
)
|
| 198 |
|
|
|
|
| 199 |
st.plotly_chart(fig_department, use_container_width=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/pages/utils/graphs.py
CHANGED
|
@@ -88,7 +88,7 @@ def display_choropleth_map_for_department(
|
|
| 88 |
metric_name: str = "prixm2moyen",
|
| 89 |
metric_description: str = "Average Price per Square Meter",
|
| 90 |
title: str = "Average Price per Square Meter",
|
| 91 |
-
height_graph: int =
|
| 92 |
width_graph: int = 1400,
|
| 93 |
):
|
| 94 |
df_filtered = df[df["code_departement"] == department_code].copy()
|
|
@@ -143,8 +143,8 @@ def display_choropleth_map_for_department(
|
|
| 143 |
fig.update_traces(marker_line_width=0)
|
| 144 |
if fig.layout.updatemenus:
|
| 145 |
try:
|
| 146 |
-
fig.layout.updatemenus[0].buttons[0].args[1]["frame"]["duration"] =
|
| 147 |
-
fig.layout.updatemenus[0].buttons[0].args[1]["transition"]["duration"] =
|
| 148 |
except IndexError:
|
| 149 |
st.warning("Could not set animation speed.")
|
| 150 |
else:
|
|
|
|
| 88 |
metric_name: str = "prixm2moyen",
|
| 89 |
metric_description: str = "Average Price per Square Meter",
|
| 90 |
title: str = "Average Price per Square Meter",
|
| 91 |
+
height_graph: int = 1000,
|
| 92 |
width_graph: int = 1400,
|
| 93 |
):
|
| 94 |
df_filtered = df[df["code_departement"] == department_code].copy()
|
|
|
|
| 143 |
fig.update_traces(marker_line_width=0)
|
| 144 |
if fig.layout.updatemenus:
|
| 145 |
try:
|
| 146 |
+
fig.layout.updatemenus[0].buttons[0].args[1]["frame"]["duration"] = 750
|
| 147 |
+
fig.layout.updatemenus[0].buttons[0].args[1]["transition"]["duration"] = 250
|
| 148 |
except IndexError:
|
| 149 |
st.warning("Could not set animation speed.")
|
| 150 |
else:
|