16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542 | class indicator:
"""
Title
-----
Indicator data object
Description
-----------
Indicator objects contain a DataFrame object storing data for a single
indicator in a single local authority.
Examples
--------
>>> data = example_lgbf_data().merge(
>>> example_lgbf_metadata(),
>>> on = "Indicators_Information_Code",
>>> how = "inner"
>>> )
>>> data = data[(data["Indicators_Information_Code"] == "SW01") & (data["LA_Information_LocalAuthority"] == "Aberdeen City")]
>>>
>>> output = indicator(x = data)
>>> output
>>> output.data
>>> output.id()
>>> output.title()
>>> output.authority()
>>> output.category()
>>> output.plot(type = "indicator")
>>> output.plot(type = "numerator_denominator")
"""
def __init__(self, x: pd.DataFrame):
"""
Parameters
----------
x: pandas.core.frame.DataFrame
A DataFrame of data for single indicator dataset in single local
authority.
"""
self._data = deepcopy(x)
self._validate()
def __str__(self):
output = f"""
Indicator object
- id: {self.id()}
- title: {self.title()}
- authority: {self.authority()}
- category: {self.category()}
- Dimensions: {self.data.shape[1]} x {self.data.shape[0]}
"""
return output
def __repr__(self):
output = f"""
Indicator object
- id: {self.id()}
- title: {self.title()}
- authority: {self.authority()}
- category: {self.category()}
- Dimensions: {self.data.shape[1]} x {self.data.shape[0]}
"""
return output
def __setattr__(self, name, value):
old_value = getattr(self, name, None)
super().__setattr__(name, value)
if not name.startswith("_"):
try:
self._validate()
except Exception as e:
super().__setattr__(name, old_value)
raise e
def _validate(self):
assert isinstance(self.data, pd.DataFrame), "data is not DataFrame"
assert isinstance(self.id(), str), "id must be string"
assert isinstance(self.title(), str), "title must be string"
assert isinstance(self.authority(), str), "authority must be string"
assert isinstance(self.category(), str), "category must be string"
req_cols = indicator._required_indicator_cols()
assert set(req_cols).issubset(self.data.columns), (
"Missing columns. 'See _required_indicator_cols' for all required columns"
)
@property
def data(self):
return self._data
@data.setter
def data(self, value: pd.DataFrame):
"""
Title
-----
Set data attribute for indicator object.
Parameters
----------
value: pandas.core.frame.DataFrame
A DataFrame of indicator data
"""
assert isinstance(value, pd.DataFrame), "data is not DataFrame"
self._data = deepcopy(value)
@staticmethod
def _required_indicator_cols():
return [
"LA_Data_LGBF_Year",
"LA_Data_LA_IndicatorReal",
"LA_Data_LA_Numerator_real",
"LA_Data_LA_Den_Real",
"Scotland_Data_Scotland_Indicator_Real",
"Scotland_Data_Scotland_Num_Real",
"Scotland_Data_Scotland_Den_Real",
"FG_Data_FG_Avg_Indicator_Real",
"FG_Data_FG_Avg_Num_Real",
"FG_Data_FG_Avg_Den_Real",
"Indicators_Information_Unit",
"Indicators_Information_Title",
"Indicators_Information_Code",
"Indicators_Information_Numerator_Title",
"Indicators_Information_Denominator_Title",
"Indicators_Information_Category",
]
def id(self):
"""
Title
-----
Get indicator id.
"""
return self._assert_unique_col("Indicators_Information_Code")
def title(self):
"""
Title
-----
Get indicator title.
"""
return self._assert_unique_col("Indicators_Information_Title")
def authority(self):
"""
Title
-----
Get indicator authority.
"""
return self._assert_unique_col("LA_Information_LocalAuthority")
def category(self):
"""
Title
-----
Get indicator category.
"""
return self._assert_unique_col("Indicators_Information_Category")
def _assert_unique_col(self, col: str):
output = pd.unique(self.data[col]).tolist()
assert len(output) == 1, f"'{col}' must have only 1 unique value"
return output[0]
def plot(self, type: str, **kwargs):
"""
Title
-----
Plot indicator object.
Parameters
----------
type: str
Type of plot. Either "indicator" or "numerator_denominator"
**kwargs:
Passed to plotting methods.
Examples
----------
>>> x = indicator.example_indicator()
>>> x.plot(type = "indicator")
"""
match type:
case "indicator":
return self._indicator_plot(**kwargs)
case "numerator_denominator":
return self._numerator_denominator_plot(**kwargs)
case _:
raise Exception(f"{type} plot type not implemented")
def _indicator_plot(self, is_dark: bool = False, **kwargs):
data = self._indicator_plot_data()
unit = pd.unique(data["Indicators_Information_Unit"]).tolist()[0]
fig = px.line(data, x="Year", y="Metric", color="Category")
fig = fig.update_xaxes(type="category")
fig = fig.update_yaxes(
title_text=wrap_text(self.title()),
tickformat={"Percentage": ".1%", "Percentage points": ".1%"}.get(unit, ""),
tickprefix={"Pounds": "£"}.get(unit, ""),
ticksuffix={"Tonnes": "t", "Days": "days"}.get(unit, ""),
)
fig.update_layout(
legend=dict(orientation="h", yanchor="top", y=-0.5, xanchor="center", x=0.5)
)
fig = self._update_fig(fig=fig, is_dark=is_dark)
return fig
def _numerator_denominator_plot(self, is_dark: bool = False, **kwargs):
data = self._numerator_denominator_plot_data()
num_title = pd.unique(data["Indicators_Information_Numerator_Title"]).tolist()[
0
]
den_title = pd.unique(
data["Indicators_Information_Denominator_Title"]
).tolist()[0]
fig = make_subplots(specs=[[{"secondary_y": True}]])
fig = fig.add_trace(
go.Scatter(
x=data["LA_Data_LGBF_Year"],
y=data["LA_Data_LA_Numerator_real"],
name=num_title,
),
secondary_y=False,
)
fig = fig.add_trace(
go.Scatter(
x=data["LA_Data_LGBF_Year"],
y=data["LA_Data_LA_Den_Real"],
name=den_title,
),
secondary_y=True,
)
fig = fig.update_xaxes(title_text="Year", type="category")
fig = fig.update_yaxes(title_text=wrap_text(num_title), secondary_y=False)
fig = fig.update_yaxes(title_text=wrap_text(den_title), secondary_y=True)
fig = fig.update_layout(hovermode="x unified")
fig.update_layout(
legend=dict(orientation="h", yanchor="top", y=-0.5, xanchor="center", x=0.5)
)
fig = self._update_fig(fig=fig, is_dark=is_dark)
return fig
@staticmethod
def _update_fig(fig, is_dark: bool = False):
template = "plotly_dark" if is_dark else "plotly_white"
text_color = "#f8f9fa" if is_dark else "#212529"
hover_bg = "#343a40" if is_dark else "#ffffff"
fig = fig.update_xaxes(
tickfont=dict(color=text_color),
linecolor=text_color,
)
fig = fig.update_yaxes(
tickfont=dict(color=text_color),
linecolor=text_color,
)
fig = fig.update_layout(
paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)",
hovermode="x unified",
font=dict(color=text_color),
title=dict(font=dict(color=text_color)),
legend_title=dict(font=dict(color=text_color)),
hoverlabel=dict(
bgcolor=hover_bg,
font_color=text_color,
),
)
return fig
def _indicator_plot_data(self, **kwargs):
output = deepcopy(self.data)
req_cols = {
"LA_Data_LGBF_Year": "Year",
"Indicators_Information_Unit": "Indicators_Information_Unit",
"Indicators_Information_Title": "Indicators_Information_Title",
"LA_Data_LA_IndicatorReal": "Local Authority",
"Scotland_Data_Scotland_Indicator_Real": "Scotland",
"FG_Data_FG_Avg_Indicator_Real": "Family Group",
}
output = output.rename(columns=req_cols)[req_cols.values()]
output = output.melt(
id_vars=["Year", "Indicators_Information_Unit"],
value_vars=["Local Authority", "Family Group", "Scotland"],
var_name="Category",
value_name="Metric",
)
return output
def _numerator_denominator_plot_data(self, **kwargs):
output = deepcopy(self.data)
req_cols = [
"Indicators_Information_Code",
"LA_Information_LocalAuthority",
"LA_Data_LGBF_Year",
"LA_Data_LA_Numerator_real",
"LA_Data_LA_Den_Real",
"Indicators_Information_Numerator_Title",
"Indicators_Information_Denominator_Title",
]
return output[req_cols]
def summary(self, type: str, **kwargs):
"""
Title
-----
Summarise indicator object.
Parameters
----------
type: str
Type of summary. Options are "indicator".
**kwargs:
Passed to summary methods.
Examples
----------
>>> x = indicator.example_indicator()
>>> x.summary(type = "indicator")
"""
match type:
case "indicator":
return self._indicator_summary(**kwargs)
case "statistical_comparisons":
return self._statistical_comparisons(**kwargs)
case _:
raise Exception(f"{type} summary type not implemented")
def _indicator_summary(self):
cols = {
"Indicators_Information_Code": "Indicators Code",
"LA_Data_LGBF_Year": "Year",
"LA_Data_LA_IndicatorReal": "Indicator value",
"LA_Data_LA_Numerator_real": "Indicator numerator value",
"LA_Data_LA_Den_Real": "Indicator denominator value",
"Scotland_Data_Scotland_Indicator_Real": "Scotland indicator value",
"Scotland_Data_Scotland_Num_Real": "Scotland indicator numerator value",
"Scotland_Data_Scotland_Den_Real": "Scotland indicator denominator value",
"FG_Data_FG_Avg_Indicator_Real": "Family group indicator value",
"FG_Data_FG_Avg_Num_Real": "Family group indicator numerator value",
"FG_Data_FG_Avg_Den_Real": "Family group indicator denominator value",
}
return self.data.rename(columns=cols)[cols.values()]
def _statistical_comparisons(self, test: str = "mann_whitney"):
comparisons = [
("Indicator value", "Family group indicator value"),
("Indicator value", "Scotland indicator value"),
]
cols = {
"LA_Data_LGBF_Year": "Year",
"LA_Data_LA_IndicatorReal": "Indicator value",
"Scotland_Data_Scotland_Indicator_Real": "Scotland indicator value",
"FG_Data_FG_Avg_Indicator_Real": "Family group indicator value",
}
data = self.data.rename(columns=cols)[cols.values()]
match test:
case "mann_whitney":
output = [
self._mann_whitney_compare(data, x, y) for x, y in comparisons
]
return pd.DataFrame(output)
@staticmethod
def _mann_whitney_compare(data: pd.DataFrame, x: str, y: str):
stat, p_val = mannwhitneyu(data[x], data[y])
return {
"Comparison": f"{x} vs {y}",
"Median of x": np.median(data[x]),
"Median of y": np.median(data[y]),
"U-statistic": stat,
"P-value (unadjusted)": p_val,
}
@staticmethod
@module.ui
def mod_ui(object: indicator):
return ui.nav_panel(
object.title(),
ui.div(
ui.card_header(object.title()),
ui.output_ui("indicator_ui"),
min_height="500px",
),
)
@staticmethod
@module.server
def mod_server(input, output, session, object: indicator, is_dark):
@render_widget
def indicator_plot():
logger.info("Creating indicator plot")
return object.plot(type="indicator", is_dark=is_dark() == "dark")
@render_widget
def numerator_denominator_plot():
logger.info("Creating numerator denominator indicator plot")
return object.plot(
type="numerator_denominator", is_dark=is_dark() == "dark"
)
@render.data_frame
def indicator_table():
logger.info("Creating indicator summary")
return render.DataTable(
object.summary(type="indicator"), filters=True, width="100%"
)
@render.download(filename=f"{object.title()}.csv")
def download_indicator_table():
logger.info("Downloading indicator summary")
filtered_df = indicator_table.data_view()
yield filtered_df.to_csv(index=False)
@render.data_frame
def statistical_comparison_table():
logger.info("Creating statistical comparison summary")
return render.DataTable(
object.summary(type="statistical_comparisons"), width="100%"
)
@render.ui
def indicator_ui():
num_den_widget = None
col_widths = [12, 1]
if object._contains_num_den():
num_den_widget = output_widget("numerator_denominator_plot")
col_widths = [6, 6]
return ui.navset_tab(
ui.nav_panel(
"Plot",
ui.card_header(
ui.popover(
icon("question"),
"""
These interactive line plots present data for the
selected local authority and indicator, alongsie the
local authority family group and Scotland averages
(y-axis) against time (x-axis). The values used to
derive the indicator metric, if applicable, are also
presented.
""",
title="Notes",
placement="right",
)
),
ui.layout_columns(
output_widget("indicator_plot"),
num_den_widget,
col_widths=col_widths,
),
),
ui.nav_panel(
"Statistical comparisons",
ui.card_header(
ui.popover(
icon("question"),
"""
A Mann-Whitney U rank test was used to test the
hypothesis that distribution of values across the
time period was statistically different between the
indicator and family group or Scotland group
averages respectively. A P-value < 0.05 indicates
that the indicator values for the selected local
authority differs from other local authorities in
the family group or across Scotland respectively.
Please note it is unclear whether this data set
violates the assumption that indepdendent variables
are being tested therefore the results should be
interpreted with caution.
""",
title="Notes",
placement="right",
)
),
ui.output_data_frame("statistical_comparison_table"),
),
ui.nav_panel(
"Download",
ui.card_header(
ui.popover(
icon("question"),
"""
This interactive table presents all data
for the selected local authority and indicator,
alongsie the local authority family group and Scotland
averages. If applicable the numerator and denominator
values which were used to derive the indicator value
are presented.
""",
title="Notes",
placement="right",
)
),
ui.output_data_frame("indicator_table"),
ui.download_button(
"download_indicator_table",
"Download CSV",
),
),
)
def _contains_num_den(self):
return not (
pd.isna(self.data["LA_Data_LA_Numerator_real"]).all()
and pd.isna(self.data["LA_Data_LA_Den_Real"]).all()
)
@staticmethod
def example_indicator():
"""
Title
-----
Generate example indicator object
Returns
----------
indicator object.
Examples
----------
>>> x = indicator.example_indicator()
>>> print(x)
"""
metadata = example_lgbf_metadata()
data = example_lgbf_data()
output = data.merge(metadata, on="Indicators_Information_Code", how="inner")
output = output[output["Indicators_Information_Code"] == "SW01"]
output = output[output["LA_Information_LocalAuthority"] == "Aberdeen City"]
return indicator(x=output)
|