ElvarThorS commited on
Commit
1263e08
·
1 Parent(s): 46d9bb2

Added plots to appropriate areas

Browse files
Files changed (2) hide show
  1. app/app.py +135 -33
  2. app/data_processing/point_scoring.py +1 -1
app/app.py CHANGED
@@ -8,6 +8,7 @@ from datetime import datetime
8
 
9
  import seaborn as sns
10
  import matplotlib.pyplot as plt
 
11
 
12
  from pandas.core.frame import functools
13
  # Load data and compute static values
@@ -78,6 +79,40 @@ with ui.layout_columns(col_widths=[8, 4]):
78
  def totalScore():
79
  score = scores()
80
  return f"{round(float(score["total_score"]), 2)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
 
82
  with ui.nav_panel("Income"):
83
  "Income Score"
@@ -85,6 +120,37 @@ with ui.layout_columns(col_widths=[8, 4]):
85
  def incomeScore():
86
  score = scores()
87
  return f"{round(float(score["income_score"]), 2)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
 
89
  with ui.nav_panel("Age"):
90
  "Age Score"
@@ -93,45 +159,81 @@ with ui.layout_columns(col_widths=[8, 4]):
93
  score = scores()
94
  return f"{round(float(score["age_score"]), 2)}"
95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  with ui.nav_panel("Density"):
97
  "Density"
98
  @render.text
99
  def sensityScoer():
100
  score = scores()
101
- return f"{round(float(score["density_score"] * 1000000), 2)} Person / Kilometer"
102
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
 
104
- @render.plot(alt="A b ar chart of income distribution.")
105
- def income_plot():
106
- print("Generating income distribution bar chart")
107
-
108
- # Get selected stop coordinates
109
- x, y = stop.get()
110
- station_coord = (y, x)
111
-
112
- # Fetch income distribution data from the Data_provider instance
113
- income_data = initBackend.get_station_score(station_coord, radius=input.rad())['income_data'] # Assume this returns a dictionary
114
-
115
- # Example structure: {1: 150, 2: 200, 3: 180, ...}
116
- income_brackets = list(income_data.keys())
117
- populations = list(income_data.values())
118
-
119
- # Create a Matplotlib figure
120
- fig, ax = plt.subplots(figsize=(8, 4))
121
-
122
- # Create the bar chart
123
- ax.bar(income_brackets, populations, color='lightcoral')
124
-
125
- # Customize the plot
126
- ax.set_title("Population by Income Bracket")
127
- ax.set_xlabel("Income Bracket")
128
- ax.set_ylabel("Population")
129
- ax.set_xticks(income_brackets)
130
- ax.set_xticklabels(income_brackets, rotation=45, ha="right")
131
-
132
- # Return the figure for rendering in Shiny
133
- return fig
134
-
135
 
136
 
137
 
 
8
 
9
  import seaborn as sns
10
  import matplotlib.pyplot as plt
11
+ import matplotlib.cm as cm
12
 
13
  from pandas.core.frame import functools
14
  # Load data and compute static values
 
79
  def totalScore():
80
  score = scores()
81
  return f"{round(float(score["total_score"]), 2)}"
82
+
83
+ @render.plot(alt="A pie chart of score contributions from age, income, and density.")
84
+ def contribution_pie_chart():
85
+ print("Generating pie chart of contributions")
86
+
87
+ # Get score components
88
+ score = scores()
89
+ age_contribution = score["age_score"]
90
+ income_contribution = score["income_score"]
91
+ density_contribution = score["density_score"]
92
+
93
+ # Data for the pie chart
94
+ contributions = [age_contribution, income_contribution, density_contribution]
95
+ labels = ["Age", "Income", "Density"]
96
+ colors = ["#FD4D86", "#36DEC2", "#704CB0"] # Custom colors for the segments
97
+
98
+ # Create a Matplotlib figure
99
+ fig, ax = plt.subplots(figsize=(6, 6))
100
+
101
+ # Create the pie chart
102
+ ax.pie(
103
+ contributions,
104
+ labels=labels,
105
+ autopct='%1.1f%%',
106
+ startangle=90,
107
+ colors=colors,
108
+ textprops={'fontsize': 12}
109
+ )
110
+
111
+ # Add a title
112
+ ax.set_title("Score Contributions", fontsize=16)
113
+
114
+ # Return the figure for rendering in Shiny
115
+ return fig
116
 
117
  with ui.nav_panel("Income"):
118
  "Income Score"
 
120
  def incomeScore():
121
  score = scores()
122
  return f"{round(float(score["income_score"]), 2)}"
123
+ @render.plot(alt="A chart of income distribution.")
124
+ def income_plot():
125
+ print("Generating income distribution bar chart")
126
+
127
+ # Get selected stop coordinates
128
+ x, y = stop.get()
129
+ station_coord = (y, x)
130
+
131
+ # Fetch income distribution data from the Data_provider instance
132
+ income_data = initBackend.get_station_score(station_coord, radius=input.rad())['income_data'] # Assume this returns a dictionary
133
+
134
+ # Example structure: {1: 150, 2: 200, 3: 180, ...}
135
+ income_brackets = list(income_data.keys())
136
+ populations = list(income_data.values())
137
+
138
+ # Create a Matplotlib figure
139
+ fig, ax = plt.subplots(figsize=(8, 4))
140
+
141
+ # Create the bar chart
142
+ ax.bar(income_brackets, populations, color='#36DEC2')
143
+
144
+ # Customize the plot
145
+ ax.set_title("Population by Income Bracket")
146
+ ax.set_xlabel("Income Bracket")
147
+ ax.set_ylabel("Population")
148
+ ax.set_xticks(income_brackets)
149
+ ax.set_xticklabels(income_brackets, rotation=45, ha="right")
150
+
151
+ # Return the figure for rendering in Shiny
152
+ return fig
153
+
154
 
155
  with ui.nav_panel("Age"):
156
  "Age Score"
 
159
  score = scores()
160
  return f"{round(float(score["age_score"]), 2)}"
161
 
162
+ @render.plot(alt="A bar chart of age distribution.")
163
+ def age_plot():
164
+ print("Generating age distribution bar chart")
165
+
166
+ # Get selected stop coordinates
167
+ x, y = stop.get()
168
+ station_coord = (y, x)
169
+
170
+ # Fetch age distribution data from the Data_provider instance
171
+ age_data = initBackend.get_station_score(station_coord, radius=input.rad())['age_data'] # Assume this returns a dictionary
172
+
173
+ # Example structure: {'0-4 ára': 120, '5-9 ára': 140, ...}
174
+ age_brackets = list(age_data.keys())
175
+ populations = list(age_data.values())
176
+
177
+ # Create a Matplotlib figure
178
+ fig, ax = plt.subplots(figsize=(8, 4))
179
+
180
+ # Create the bar chart with custom colors
181
+ ax.bar(age_brackets, populations, color='#FD4D86')
182
+
183
+ # Customize the plot
184
+ ax.set_title("Population by Age Bracket", fontsize=14)
185
+ ax.set_xlabel("Age Bracket", fontsize=12)
186
+ ax.set_ylabel("Population", fontsize=12)
187
+ ax.set_xticks(range(len(age_brackets)))
188
+ ax.set_xticklabels(age_brackets, rotation=45, ha="right", fontsize=10)
189
+
190
+ # Return the figure for rendering in Shiny
191
+ return fig
192
+
193
  with ui.nav_panel("Density"):
194
  "Density"
195
  @render.text
196
  def sensityScoer():
197
  score = scores()
198
+ return f"{round(float(score["density_score"]), 2)}"
199
+ @render.plot(alt="A bar chart of density scores for all areas within the radius.")
200
+ def density_plot():
201
+ print("Generating density score bar chart")
202
+
203
+ # Get selected stop coordinates
204
+ x, y = stop.get()
205
+ station_coord = (y, x)
206
+
207
+ # Fetch small area contributions from the Data_provider instance
208
+ small_area_contributions = initBackend.get_station_score(
209
+ station_coord,
210
+ radius=input.rad(),
211
+ w_density=input.w_density(),
212
+ w_income=input.w_income(),
213
+ w_age=input.w_age()
214
+ )['small_area_contributions']
215
+
216
+ # Extract density scores for each small area
217
+ area_ids = [area_id for area_id in small_area_contributions.keys()]
218
+ density_scores = [area_data['density_score'] for area_data in small_area_contributions.values()]
219
+
220
+ # Create a Matplotlib figure
221
+ fig, ax = plt.subplots(figsize=(8, 4))
222
+
223
+ # Create the bar chart
224
+ ax.bar(area_ids, density_scores, color='#704CB0')
225
+
226
+ # Customize the plot
227
+ ax.set_title("Density Scores of Small Areas", fontsize=14)
228
+ ax.set_xlabel("Small Area ID", fontsize=12)
229
+ ax.set_ylabel("Density Score", fontsize=12)
230
+ ax.set_xticks(range(len(area_ids)))
231
+ ax.set_xticklabels(area_ids, rotation=45, ha="right", fontsize=10)
232
+
233
+ # Return the figure for rendering in Shiny
234
+ return fig
235
 
236
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
237
 
238
 
239
 
app/data_processing/point_scoring.py CHANGED
@@ -72,7 +72,7 @@ def score_current(station_coord, df_features, cov_smsv, w_density, w_income, w_a
72
  aggregated_income_distribution[income_group] = proportion
73
 
74
  # Calculate density score
75
- density_contribution = smsv_info["density"].iloc[0] * w_density * smsv["small_zone_percentage"] * 100
76
 
77
  # Calculate age score
78
  age_contribution = get_age_score(age_dist) * w_age * smsv["small_zone_percentage"]
 
72
  aggregated_income_distribution[income_group] = proportion
73
 
74
  # Calculate density score
75
+ density_contribution = smsv_info["density"].iloc[0] * w_density * smsv["small_zone_percentage"] * 200
76
 
77
  # Calculate age score
78
  age_contribution = get_age_score(age_dist) * w_age * smsv["small_zone_percentage"]