Update correlation_explorer_app.py
Browse files- correlation_explorer_app.py +19 -9
correlation_explorer_app.py
CHANGED
|
@@ -13,7 +13,7 @@ df_global = pd.read_csv("merged_global.csv")
|
|
| 13 |
df_hemi = pd.read_csv("hemispheric_merged.csv")
|
| 14 |
# Merge by Year and Month
|
| 15 |
df = pd.merge(df_global, df_hemi, on=["year", "month"], suffixes=("", "_hemi"))
|
| 16 |
-
#
|
| 17 |
def get_season(month):
|
| 18 |
return {
|
| 19 |
12: "DJF", 1: "DJF", 2: "DJF",
|
|
@@ -80,11 +80,13 @@ app.layout = dbc.Container([
|
|
| 80 |
dbc.Row([
|
| 81 |
dbc.Col([
|
| 82 |
html.Label("X-axis Variable"),
|
| 83 |
-
|
|
|
|
| 84 |
], md=6),
|
| 85 |
dbc.Col([
|
| 86 |
html.Label("Y-axis Variable"),
|
| 87 |
-
|
|
|
|
| 88 |
], md=6)
|
| 89 |
], className="mb-3"),
|
| 90 |
|
|
@@ -122,7 +124,7 @@ app.layout = dbc.Container([
|
|
| 122 |
]),
|
| 123 |
html.P("Switch views (Global, Northern, Southern Hemisphere) and select indicators and years to compare."),
|
| 124 |
html.P("The scatter plot shows how the selected variables change together, with a regression trendline and R² value."),
|
| 125 |
-
html.P("Pearson's r (
|
| 126 |
html.P("The correlation heatmap reveals how all indicators relate within your selected range.")
|
| 127 |
])
|
| 128 |
], className="mb-4"),
|
|
@@ -156,11 +158,16 @@ def update_variable_options(scope):
|
|
| 156 |
Input('scope-selector', 'value'),
|
| 157 |
Input('theme-toggle', 'value')
|
| 158 |
)
|
| 159 |
-
def update_visuals(x_var, y_var, year_range,
|
|
|
|
| 160 |
vars_dict = get_variables(scope)
|
| 161 |
dff = df[(df["year"] >= year_range[0]) & (df["year"] <= year_range[1])]
|
| 162 |
-
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
dff = dff.groupby(['year', 'Season']).mean(numeric_only=True).reset_index()
|
| 165 |
else:
|
| 166 |
dff = dff.copy()
|
|
@@ -187,7 +194,7 @@ def update_visuals(x_var, y_var, year_range, mode, scope, dark_mode):
|
|
| 187 |
)
|
| 188 |
fig.update_traces(
|
| 189 |
hovertemplate=f"<b>Year</b>: %{{customdata[0]}}<br><b>Month/Season</b>: %{{customdata[1]}}<br><b>{vars_dict[x_var]}</b>: %{{x:.3f}}<br><b>{vars_dict[y_var]}</b>: %{{y:.3f}}",
|
| 190 |
-
customdata=dff[["year", "Season"]] if
|
| 191 |
)
|
| 192 |
# Add R² if OLS exists
|
| 193 |
try:
|
|
@@ -218,4 +225,7 @@ def update_visuals(x_var, y_var, year_range, mode, scope, dark_mode):
|
|
| 218 |
server = app.server # Required for gunicorn
|
| 219 |
# 5. Run app
|
| 220 |
if __name__ == '__main__':
|
| 221 |
-
app.run_server(debug=True, host='0.0.0.0', port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
df_hemi = pd.read_csv("hemispheric_merged.csv")
|
| 14 |
# Merge by Year and Month
|
| 15 |
df = pd.merge(df_global, df_hemi, on=["year", "month"], suffixes=("", "_hemi"))
|
| 16 |
+
# For the seaosnal analysis categorizing the months
|
| 17 |
def get_season(month):
|
| 18 |
return {
|
| 19 |
12: "DJF", 1: "DJF", 2: "DJF",
|
|
|
|
| 80 |
dbc.Row([
|
| 81 |
dbc.Col([
|
| 82 |
html.Label("X-axis Variable"),
|
| 83 |
+
html.P("Select the variable for the X-axis"),
|
| 84 |
+
dcc.Dropdown(id='x-axis-dropdown', placeholder="Choose a variable for X-axis")
|
| 85 |
], md=6),
|
| 86 |
dbc.Col([
|
| 87 |
html.Label("Y-axis Variable"),
|
| 88 |
+
html.P("Select the variable for the Y-axis"),
|
| 89 |
+
dcc.Dropdown(id='y-axis-dropdown', placeholder="Choose a variable for Y-axis")
|
| 90 |
], md=6)
|
| 91 |
], className="mb-3"),
|
| 92 |
|
|
|
|
| 124 |
]),
|
| 125 |
html.P("Switch views (Global, Northern, Southern Hemisphere) and select indicators and years to compare."),
|
| 126 |
html.P("The scatter plot shows how the selected variables change together, with a regression trendline and R² value."),
|
| 127 |
+
html.P("Pearson's r (shown in the heatmap and above the scatter) helps you evaluate correlation strength."),
|
| 128 |
html.P("The correlation heatmap reveals how all indicators relate within your selected range.")
|
| 129 |
])
|
| 130 |
], className="mb-4"),
|
|
|
|
| 158 |
Input('scope-selector', 'value'),
|
| 159 |
Input('theme-toggle', 'value')
|
| 160 |
)
|
| 161 |
+
def update_visuals(x_var, y_var, year_range,view_mode, scope, dark_mode):
|
| 162 |
+
|
| 163 |
vars_dict = get_variables(scope)
|
| 164 |
dff = df[(df["year"] >= year_range[0]) & (df["year"] <= year_range[1])]
|
| 165 |
+
"""
|
| 166 |
+
Updating both charts based on user inputs
|
| 167 |
+
Supports theme switching and monthly/seasonal toggling.
|
| 168 |
+
"""
|
| 169 |
+
# If the view mode switched one the seasonal the month columns are not needed
|
| 170 |
+
if view_mode == "Seasonal":
|
| 171 |
dff = dff.groupby(['year', 'Season']).mean(numeric_only=True).reset_index()
|
| 172 |
else:
|
| 173 |
dff = dff.copy()
|
|
|
|
| 194 |
)
|
| 195 |
fig.update_traces(
|
| 196 |
hovertemplate=f"<b>Year</b>: %{{customdata[0]}}<br><b>Month/Season</b>: %{{customdata[1]}}<br><b>{vars_dict[x_var]}</b>: %{{x:.3f}}<br><b>{vars_dict[y_var]}</b>: %{{y:.3f}}",
|
| 197 |
+
customdata=dff[["year", "Season"]] if view_mode == "Seasonal" else dff[["year", "month"]]
|
| 198 |
)
|
| 199 |
# Add R² if OLS exists
|
| 200 |
try:
|
|
|
|
| 225 |
server = app.server # Required for gunicorn
|
| 226 |
# 5. Run app
|
| 227 |
if __name__ == '__main__':
|
| 228 |
+
app.run_server(debug=True, host='0.0.0.0', port=7860)
|
| 229 |
+
dbc.Row([
|
| 230 |
+
dbc.Col(html.Footer("Created by Irem R. as part of Bachelor Thesis at Riga Technical University — 2025", className="text-center text-muted"), width=12)
|
| 231 |
+
], className="mt-4")
|