danielrosehill commited on
Commit
f25efaa
·
1 Parent(s): 18f002e
Files changed (1) hide show
  1. app.py +125 -44
app.py CHANGED
@@ -68,7 +68,7 @@ def filter_data(countries, categories, min_value=None, max_value=None):
68
  return filtered_df
69
 
70
  def create_bar_chart(countries, categories):
71
- """Create a bar chart showing value factors by country and category"""
72
  filtered_df = filter_data(countries, categories)
73
 
74
  if filtered_df.empty:
@@ -80,16 +80,25 @@ def create_bar_chart(countries, categories):
80
  )
81
  return fig
82
 
83
- # Group by country and topic (category)
84
- grouped = filtered_df.groupby(['country', 'topic'])['value'].mean().reset_index()
 
 
 
 
 
 
 
 
85
  fig = px.bar(
86
  grouped,
87
  x='country',
88
  y='value',
89
- color='topic',
90
- title="Value Factors by Country and Category",
91
- labels={'value': "Value Factor (USD)", 'country': "Country", 'topic': "Category"},
92
- barmode='group'
 
93
  )
94
 
95
  fig.update_layout(xaxis_tickangle=-45, height=600)
@@ -130,7 +139,7 @@ def create_map_visualization(countries, categories):
130
  return fig
131
 
132
  def create_comparison_chart(countries, categories):
133
- """Create a comparison chart showing categories across selected countries"""
134
  filtered_df = filter_data(countries, categories)
135
 
136
  if filtered_df.empty:
@@ -142,23 +151,32 @@ def create_comparison_chart(countries, categories):
142
  )
143
  return fig
144
 
145
- # Group by topic (category) and country
146
- grouped = filtered_df.groupby(['topic', 'country'])['value'].mean().reset_index()
 
 
 
 
 
 
 
 
147
  fig = px.bar(
148
  grouped,
149
- x='topic',
150
  y='value',
151
  color='country',
152
- title="Category Comparison Across Countries",
153
- labels={'value': "Value Factor (USD)", 'topic': "Category"},
154
- barmode='group'
 
155
  )
156
 
157
  fig.update_layout(xaxis_tickangle=-45, height=600)
158
  return fig
159
 
160
  def create_box_plot(countries, categories):
161
- """Create a box plot showing distribution of value factors"""
162
  filtered_df = filter_data(countries, categories)
163
 
164
  if filtered_df.empty:
@@ -170,13 +188,21 @@ def create_box_plot(countries, categories):
170
  )
171
  return fig
172
 
 
 
 
 
 
 
 
173
  fig = px.box(
174
  filtered_df,
175
- x='topic',
176
  y='value',
177
  color='country',
178
- title="Distribution of Value Factors",
179
- labels={'value': "Value Factor (USD)", 'topic': "Category"}
 
180
  )
181
 
182
  fig.update_layout(xaxis_tickangle=-45, height=600)
@@ -223,50 +249,84 @@ with gr.Blocks(title="GVFD Navigator", theme=gr.themes.Soft()) as demo:
223
  Explore environmental and social impact value factors by country from the IFVI Global Value Factor Database.
224
 
225
  This visualization tool allows you to:
226
- - Filter by country and impact category
 
227
  - View interactive charts and maps
228
  - Compare value factors across regions
229
  - Explore detailed data tables
230
 
 
 
 
231
  **Data Source**: [IFVI Global Value Factor Database V2](https://huggingface.co/datasets/danielrosehill/Global-Value-Factor-Database-Refactor-V2)
232
  """)
233
 
234
  with gr.Tabs() as exploration_mode:
235
- with gr.Tab("Explore by Country"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
236
  gr.Markdown("""
237
- ### Select a Country
238
- Choose one or more countries to see all their environmental value factors across different categories.
239
  """)
240
- country_selector = gr.Dropdown(
241
  choices=get_countries(),
242
  multiselect=True,
243
  label="Select Country/Countries",
244
  info="Start typing to search..."
245
  )
246
- country_category_filter = gr.CheckboxGroup(
247
- choices=get_categories(),
248
- label="Filter by Categories (Optional)",
249
- info="Leave empty to show all categories for selected countries"
 
 
 
 
 
 
 
 
250
  )
251
- country_refresh_btn = gr.Button("Show Country Data", variant="primary", size="lg")
252
 
253
- with gr.Tab("Explore by Category"):
254
  gr.Markdown("""
255
- ### Select a Category
256
- Choose an environmental impact category to compare value factors across different countries.
257
  """)
258
- category_selector = gr.Dropdown(
259
- choices=get_categories(),
260
  multiselect=True,
261
- label="Select Category/Categories"
 
262
  )
263
- category_country_filter = gr.Dropdown(
 
 
 
 
 
 
 
264
  choices=get_countries(),
265
  multiselect=True,
266
- label="Filter by Countries (Optional)",
267
- info="Leave empty to show all countries for selected categories"
268
  )
269
- category_refresh_btn = gr.Button("Show Category Data", variant="primary", size="lg")
270
 
271
  with gr.Row():
272
  with gr.Column():
@@ -442,17 +502,38 @@ with gr.Blocks(title="GVFD Navigator", theme=gr.themes.Soft()) as demo:
442
  get_data_table(countries, categories)
443
  )
444
 
445
- # Handler for country exploration mode
446
- country_refresh_btn.click(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
447
  fn=update_all,
448
- inputs=[country_selector, country_category_filter],
449
  outputs=[bar_chart, map_chart, comparison_chart, box_plot, stats_output, data_table]
450
  )
451
 
452
- # Handler for category exploration mode
453
- category_refresh_btn.click(
454
  fn=update_all,
455
- inputs=[category_country_filter, category_selector],
456
  outputs=[bar_chart, map_chart, comparison_chart, box_plot, stats_output, data_table]
457
  )
458
 
 
68
  return filtered_df
69
 
70
  def create_bar_chart(countries, categories):
71
+ """Create a bar chart showing value factors by country and specific impact category"""
72
  filtered_df = filter_data(countries, categories)
73
 
74
  if filtered_df.empty:
 
80
  )
81
  return fig
82
 
83
+ # Create a composite key for proper comparison level: category + location + impact
84
+ filtered_df['impact_category'] = (
85
+ filtered_df['category'].astype(str) + ' (' +
86
+ filtered_df['location'].astype(str) + ', ' +
87
+ filtered_df['impact'].astype(str) + ')'
88
+ )
89
+
90
+ # Group by country and the composite impact category
91
+ grouped = filtered_df.groupby(['country', 'impact_category', 'topic'])['value'].mean().reset_index()
92
+
93
  fig = px.bar(
94
  grouped,
95
  x='country',
96
  y='value',
97
+ color='impact_category',
98
+ title="Value Factors by Country and Specific Impact Category",
99
+ labels={'value': "Value Factor (USD)", 'country': "Country", 'impact_category': "Impact Category"},
100
+ barmode='group',
101
+ hover_data=['topic']
102
  )
103
 
104
  fig.update_layout(xaxis_tickangle=-45, height=600)
 
139
  return fig
140
 
141
  def create_comparison_chart(countries, categories):
142
+ """Create a comparison chart showing specific impact categories across selected countries"""
143
  filtered_df = filter_data(countries, categories)
144
 
145
  if filtered_df.empty:
 
151
  )
152
  return fig
153
 
154
+ # Create a composite key for proper comparison level: category + location + impact
155
+ filtered_df['impact_category'] = (
156
+ filtered_df['category'].astype(str) + ' (' +
157
+ filtered_df['location'].astype(str) + ', ' +
158
+ filtered_df['impact'].astype(str) + ')'
159
+ )
160
+
161
+ # Group by the composite impact category and country
162
+ grouped = filtered_df.groupby(['impact_category', 'country', 'topic'])['value'].mean().reset_index()
163
+
164
  fig = px.bar(
165
  grouped,
166
+ x='impact_category',
167
  y='value',
168
  color='country',
169
+ title="Specific Impact Category Comparison Across Countries",
170
+ labels={'value': "Value Factor (USD)", 'impact_category': "Impact Category"},
171
+ barmode='group',
172
+ hover_data=['topic']
173
  )
174
 
175
  fig.update_layout(xaxis_tickangle=-45, height=600)
176
  return fig
177
 
178
  def create_box_plot(countries, categories):
179
+ """Create a box plot showing distribution of value factors by specific impact categories"""
180
  filtered_df = filter_data(countries, categories)
181
 
182
  if filtered_df.empty:
 
188
  )
189
  return fig
190
 
191
+ # Create a composite key for proper comparison level: category + location + impact
192
+ filtered_df['impact_category'] = (
193
+ filtered_df['category'].astype(str) + ' (' +
194
+ filtered_df['location'].astype(str) + ', ' +
195
+ filtered_df['impact'].astype(str) + ')'
196
+ )
197
+
198
  fig = px.box(
199
  filtered_df,
200
+ x='impact_category',
201
  y='value',
202
  color='country',
203
+ title="Distribution of Value Factors by Specific Impact Category",
204
+ labels={'value': "Value Factor (USD)", 'impact_category': "Impact Category"},
205
+ hover_data=['topic']
206
  )
207
 
208
  fig.update_layout(xaxis_tickangle=-45, height=600)
 
249
  Explore environmental and social impact value factors by country from the IFVI Global Value Factor Database.
250
 
251
  This visualization tool allows you to:
252
+ - Filter by country and impact topic (Air Pollution, Water Pollution, etc.)
253
+ - Compare **specific impact categories** (e.g., PM2.5 in Urban areas for Primary Health)
254
  - View interactive charts and maps
255
  - Compare value factors across regions
256
  - Explore detailed data tables
257
 
258
+ **Important**: Value factors are comparable at the **category + location + impact** level within each topic.
259
+ For example, within "Air Pollution", individual measurements like "PM2.5 (Urban, Primary Health)" are comparable across countries.
260
+
261
  **Data Source**: [IFVI Global Value Factor Database V2](https://huggingface.co/datasets/danielrosehill/Global-Value-Factor-Database-Refactor-V2)
262
  """)
263
 
264
  with gr.Tabs() as exploration_mode:
265
+ with gr.Tab("Air Pollution"):
266
+ gr.Markdown("""
267
+ ### Air Pollution Value Factors
268
+ Explore specific air pollution impact categories (e.g., PM2.5, NOx, SOx) across countries.
269
+ Each category shows values by location (Urban, Rural, etc.) and impact type (Primary Health, etc.).
270
+ """)
271
+ air_country_selector = gr.Dropdown(
272
+ choices=get_countries(),
273
+ multiselect=True,
274
+ label="Select Country/Countries",
275
+ info="Start typing to search..."
276
+ )
277
+ air_refresh_btn = gr.Button("Show Air Pollution Data", variant="primary", size="lg")
278
+
279
+ with gr.Tab("Water Pollution"):
280
  gr.Markdown("""
281
+ ### Water Pollution Value Factors
282
+ Explore specific water pollution impact categories across countries.
283
  """)
284
+ water_poll_country_selector = gr.Dropdown(
285
  choices=get_countries(),
286
  multiselect=True,
287
  label="Select Country/Countries",
288
  info="Start typing to search..."
289
  )
290
+ water_poll_refresh_btn = gr.Button("Show Water Pollution Data", variant="primary", size="lg")
291
+
292
+ with gr.Tab("Land Use"):
293
+ gr.Markdown("""
294
+ ### Land Use and Conservation Value Factors
295
+ Explore land use and conservation impact categories across countries.
296
+ """)
297
+ land_country_selector = gr.Dropdown(
298
+ choices=get_countries(),
299
+ multiselect=True,
300
+ label="Select Country/Countries",
301
+ info="Start typing to search..."
302
  )
303
+ land_refresh_btn = gr.Button("Show Land Use Data", variant="primary", size="lg")
304
 
305
+ with gr.Tab("Waste"):
306
  gr.Markdown("""
307
+ ### Waste Generation Value Factors
308
+ Explore waste generation impact categories across countries.
309
  """)
310
+ waste_country_selector = gr.Dropdown(
311
+ choices=get_countries(),
312
  multiselect=True,
313
+ label="Select Country/Countries",
314
+ info="Start typing to search..."
315
  )
316
+ waste_refresh_btn = gr.Button("Show Waste Data", variant="primary", size="lg")
317
+
318
+ with gr.Tab("Water Consumption"):
319
+ gr.Markdown("""
320
+ ### Water Consumption Value Factors
321
+ Explore water consumption impact categories across countries.
322
+ """)
323
+ water_cons_country_selector = gr.Dropdown(
324
  choices=get_countries(),
325
  multiselect=True,
326
+ label="Select Country/Countries",
327
+ info="Start typing to search..."
328
  )
329
+ water_cons_refresh_btn = gr.Button("Show Water Consumption Data", variant="primary", size="lg")
330
 
331
  with gr.Row():
332
  with gr.Column():
 
502
  get_data_table(countries, categories)
503
  )
504
 
505
+ # Handler for Air Pollution tab
506
+ air_refresh_btn.click(
507
+ fn=update_all,
508
+ inputs=[air_country_selector, gr.State(["Air Pollution"])],
509
+ outputs=[bar_chart, map_chart, comparison_chart, box_plot, stats_output, data_table]
510
+ )
511
+
512
+ # Handler for Water Pollution tab
513
+ water_poll_refresh_btn.click(
514
+ fn=update_all,
515
+ inputs=[water_poll_country_selector, gr.State(["Water Pollution"])],
516
+ outputs=[bar_chart, map_chart, comparison_chart, box_plot, stats_output, data_table]
517
+ )
518
+
519
+ # Handler for Land Use tab
520
+ land_refresh_btn.click(
521
+ fn=update_all,
522
+ inputs=[land_country_selector, gr.State(["Land Use and Conservation"])],
523
+ outputs=[bar_chart, map_chart, comparison_chart, box_plot, stats_output, data_table]
524
+ )
525
+
526
+ # Handler for Waste tab
527
+ waste_refresh_btn.click(
528
  fn=update_all,
529
+ inputs=[waste_country_selector, gr.State(["Waste Generation"])],
530
  outputs=[bar_chart, map_chart, comparison_chart, box_plot, stats_output, data_table]
531
  )
532
 
533
+ # Handler for Water Consumption tab
534
+ water_cons_refresh_btn.click(
535
  fn=update_all,
536
+ inputs=[water_cons_country_selector, gr.State(["Water Consumption"])],
537
  outputs=[bar_chart, map_chart, comparison_chart, box_plot, stats_output, data_table]
538
  )
539