-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStreamlit_Mapping.py
More file actions
793 lines (727 loc) · 42.5 KB
/
Copy pathStreamlit_Mapping.py
File metadata and controls
793 lines (727 loc) · 42.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
import streamlit as st
import pandas as pd
import geopandas as gpd
import map
import numpy as np
import plotly.express as px
import base64
import re
# Experimental
# import deepseek
def make_map_itl(itl_level, itlmapping, _itl3_shapes_df, nat=False):
map_df = _itl3_shapes_df.rename(columns={'ITL325CD': 'itl3'})
# Merge up from ITL3 level to target level
map_df = map_df.merge(itlmapping, how='left', on='itl3')
if itl_level != 'itl3':
map_df = map_df.groupby([itl_level, f'{itl_level}name']).geometry.apply(lambda x: x.union_all()).reset_index()
map_df = gpd.GeoDataFrame(map_df, geometry='geometry', crs=_itl3_shapes_df.crs)
if nat:
excluded_itl1 = ["TLN", "TLM", "TLL"]
remaining_itl1 = itlmapping[~itlmapping['itl1'].isin(excluded_itl1)]['itl1'].unique().tolist()
# Merge geometries of the remaining ITL1 regions
merged_geom = map_df[map_df[itl_level].isin(remaining_itl1)].geometry.union_all()
# Remove individual ITL1 regions and add the merged one
map_df = map_df[~map_df[itl_level].isin(remaining_itl1)]
# Create merged row with correct format
merged_row = gpd.GeoDataFrame({
'itl1': ['TLB'], # Assign 'TLB' as the new ITL1 code
'itl1name': ['England'], # Provide a name for the merged region
'geometry': [merged_geom]
}, crs=map_df.crs)
map_df = pd.concat([map_df, merged_row], ignore_index=True)
map_df['geometry'] = map_df['geometry'].simplify(0.0001, preserve_topology=True)
map_df = map_df.rename(columns={f'{itl_level}name': 'region'})
return map_df
def make_map_authorities(authority_level, mcamapping, _la_shapes_df):
map_df = _la_shapes_df.rename(columns={'LAD24CD': 'la'})
if authority_level != 'la': # If MCA data is entered
# Merge all data with MCA mapping
mapped_df = map_df.merge(mcamapping, how='left', on='la')
# Split into MCA and non-MCA dataframes
mca_df = mapped_df[mapped_df['mca'].notna()].copy()
non_mca_df = mapped_df[mapped_df['mca'].isna()].copy()
# Process MCA regions
mca_regions = mca_df.groupby(['mca', 'mcaname']).geometry.apply(lambda x: x.union_all()).reset_index()
mca_regions = gpd.GeoDataFrame(mca_regions, geometry='geometry', crs=_la_shapes_df.crs)
mca_regions['region_type'] = 'mca'
# Process non-MCA regions
non_mca_geometry = non_mca_df.geometry.union_all()
non_mca_regions = gpd.GeoDataFrame({
'mca': ['non_mca_all'],
'mcaname': ['Non-MCA Regions'],
'geometry': [non_mca_geometry],
'region_type': ['non_mca']
}, geometry='geometry', crs=_la_shapes_df.crs)
# Merge MCA and non-MCA regions
map_df = pd.concat([mca_regions, non_mca_regions], ignore_index=True)
map_df = map_df.rename(columns={'mcaname': 'region'})
else:
# Merge the mca mapping to the map df so the region names can be displayed on the map
map_df = map_df.merge(mcamapping[['la', 'laname']], how='left', on='la')
map_df = map_df.rename(columns={'laname': 'region'})
map_df = gpd.GeoDataFrame(map_df, geometry='geometry', crs=_la_shapes_df.crs)
map_df['geometry'] = map_df['geometry'].simplify(0.0001, preserve_topology=True)
return map_df
# Generate the colour scale
def generate_colour_scale(colours, n=256):
# Interpolate between colours
colour_scale = []
for i in range(len(colours) - 1):
start = np.array(px.colors.hex_to_rgb(colours[i]))
end = np.array(px.colors.hex_to_rgb(colours[i + 1]))
steps = np.linspace(0, 1, n // (len(colours) - 1))
interpolated = (1 - steps)[:, None] * start + steps[:, None] * end
colour_scale.extend([f"rgb({int(r)},{int(g)},{int(b)})" for r, g, b in interpolated])
return colour_scale[::-1]
# Determine itl code level
def assign_itl_level(code):
length = len(code)
if length == 3:
return 'ITL1'
elif length == 4:
return 'ITL2'
elif length == 5:
return 'ITL3'
else:
return ''
# Determine authority code level
def assign_ca_level(code):
if code[:3] == 'E47' or code[:3] == 'E61':
return 'MCA'
elif len(code) == 9:
return 'LA'
else:
return ''
# Convert image to base 64 (streamlit only displays base 64), these codes are put in styles.css
def get_image_as_base64(file_path):
with open(file_path, "rb") as file:
data = base64.b64encode(file.read()).decode("utf-8")
return data
# Load css styling
@st.cache_data(show_spinner=False)
def load_css(filepath):
with open(filepath) as f:
st.html(f"<style>{f.read()}</style>")
# Select ITL or authority, get the respective map file, construct the map figures
@st.cache_data(show_spinner=False)
def get_figures(df, mcamapping, _la_shapes_df, itlmapping, _itl3_shapes_df, colorscale=None, show_missing_values=False, units='%', dp=2, thresholds=[], map_height=550, index=0, ):
if df.iloc[0, 0][:2] == 'TL':
geo_level = assign_itl_level(df.iloc[0, 0]).lower()
nat = False
if 'TLB' in list(df.iloc[:, 0]):
nat = True
map_df = make_map_itl(geo_level, itlmapping, _itl3_shapes_df, nat)
elif len(df.iloc[0, 0]) == 9:
geo_level = assign_ca_level(df.iloc[0, 0]).lower()
map_df = make_map_authorities(geo_level, mcamapping, _la_shapes_df)
else:
return [], []
df = df.rename(columns={df.columns[0]: geo_level})
mapnames = list(df.set_index(geo_level).columns)
fig = map.make_choropleths(df.set_index(geo_level), map_df, geo_level, colorscale, show_missing_values, units, dp, thresholds, map_height, index)
return fig, mapnames
# Preload files
@st.cache_data(show_spinner=False)
def load_files():
mcamapping = pd.read_csv('src/mcamapping.csv')
la_shapes_df = gpd.read_file('src/Local_Authority_Districts_December_2024_Boundaries_UK_BUC_-2087974657986281540.geojson')
itlmapping = pd.read_csv('src/itlmapping-updated.csv')
itl3_shapes_df = gpd.read_file('src/International_Territorial_Level_3_Updated.geojson')
return mcamapping, la_shapes_df, itlmapping, itl3_shapes_df
def main():
st.set_page_config(layout="wide", page_title="UK Colour Mapping")
st.sidebar.html("<a href='https://lab.productivity.ac.uk' alt='The Productivity Lab'></a>")
st.logo("static/logo.png", link="https://lab.productivity.ac.uk/", icon_image=None)
st.sidebar.markdown("---") # This creates a basic horizontal line (divider)
mcamapping, la_shapes_df, itlmapping, itl3_shapes_df = load_files()
query_params = {k.lower(): v.lower() for k, v in st.query_params.items()}
if 'dvo' in st.session_state:
dvo = st.session_state.dvo
else:
dvo = False
if 'fig' in st.session_state:
fig = st.session_state.fig
else:
fig = []
if 'mapname' in st.session_state:
mapname = st.session_state.mapname
else:
mapname = []
if 'index' in st.session_state:
index = st.session_state.index
else:
index = 0
if 'df' in st.session_state:
df = st.session_state.df
else:
df = pd.DataFrame()
if 'levels' in st.session_state:
levels = st.session_state.levels
else:
levels = []
if 'level' in st.session_state:
level = st.session_state.level
else:
level = ''
if 'selected_button' not in st.session_state: # Selected button from the example datasets
st.session_state.selected_button = None
if 'dataset_info' not in st.session_state:
st.session_state.dataset_info = None
if 'link' not in st.session_state:
st.session_state.link = None
if 'insights' not in st.session_state:
st.session_state.insights = ''
# Load CSS from assets
load_css('assets/styles.css')
# Intro to tool above tool itself
with st.expander(label="**About this tool**", expanded=False):
st.markdown(
"""
### Intro
###### Developed by the [TPI Productivity Lab](https://www.productivity.ac.uk/the-productivity-lab/), this tool allows for the quick creation of custom choropleth maps of regions in the United Kingdom, allowing for visual comparisons of different metrics across different geographic areas.
##### This tool can produce custom maps in 3 simple steps:
- **Construct your custom data file**: First construct a CSV file containing your data alongside relevant region codes. A step by step guide is provided on how to do this [here](https://www.lab.productivity.ac.uk/tools/map-tool).
- **Upload your data**: Click *Browse Files* below, locate your file, and then press *Upload File*.
- **Customise your map**: Use the options on the sidebar to alter the colour, view, and units.
##### If you want to see some examples of what this tool can do, select one of the pre-existing data sets below.
### Customisation Options
#### Map navigation
- **Select map**: shows the list of maps produced from the data selected. The names of the maps are the respective column names in the data file.
- **Change title**: rename the map you are currently working on. (Character limit: 70)
- **Select geography level**: if your map has 2 or more different types of region codes in the first column, these types will show up in this menu allowing you to choose which type to use.
#### Formatting
- **Select units**: choose the units you wish to use from this menu. The selected unit will format the hover data and colourscale/key.
- **Select decimal places**: choose the number of decimal places you would like your data to be rounded to. The selected number of decimal places will format the hover data and colourscale/key.
- **Hide the rest of the UK**: enabling this will remove any regions missing data in the map.
#### Colour options
- **Use discrete colouring**: enable this to use solid colouring within specified bounds. Here you will be given the option to classify your data into coloured categories based on the bounds you select. Precise data will be inflated to account for the small increments.
- **Number of colours**: choose the number of colours used to define the colour scale/colour categories. (minimum 2, maximum 6)
- **Pick colours**: pick a colour from the colour selection interface or enter a hex code for a specific colour in your scale/categorisations. If using discrete colouring, you can also specify a range in which this colour categorises data on the map.
"""
)
# Placeholders for the maps
figure = st.empty()
figure_loading = st.empty()
# Save space for the map while it is switching states
if fig:
figure.markdown(
"""
<div style="border: 0px #ccc; height: 550px; width: 1000px; display: flex; align-items: center; justify-content: center; background-color: #ffffff;">
<p style="color: #aaa;"></p>
</div>
""",
unsafe_allow_html=True
)
def reset_insights():
st.session_state.insights = ''
# Define callback functions that return data
def button1_click():
st.session_state.selected_button = "Subregional productivity data - local authoritites 2022"
st.session_state.dataset_info = "This dataset contains information about subregional productivity"
st.session_state.link = "https://www.ons.gov.uk/employmentandlabourmarket/peopleinwork/labourproductivity/articles/regionalandsubregionalproductivityintheuk/june2023"
df = pd.read_csv("examples/LA_example.csv")
if not df.empty:
fig = True
mapname = df.columns[1:].tolist()
levels = []
st.session_state.levels = levels
st.session_state.level = 'LA'
st.session_state.fig = fig
st.session_state.mapname = mapname
st.session_state.df = df
def button2_click():
st.session_state.selected_button = "2025 ITL1 Scorecard Data"
st.session_state.dataset_info = "This dataset includes scorecards for all 12 ITL1 regions in the United Kingdom. These scorecards indicate for each ITL1 region how well the region is performing as compared to the median of all ITL1 regions in the UK, for a broad set of indicators. These indicators are considered to be drivers of productivity and are classified according to five broad categories: Business performance & characteristics; Skills & training; Policy & institutions; Health & wellbeing; Investment, infrastructure & connectivity."
st.session_state.link = "https://lab.productivity.ac.uk/data/productivity-datasets/ITl1-scorecards/"
df = pd.read_csv("examples/ITL1_Scorecard_map_tool_data.csv")
if not df.empty:
fig = True
mapname = df.columns[1:].tolist()
levels = []
st.session_state.levels = levels
st.session_state.level = 'ITL1'
st.session_state.fig = fig
st.session_state.mapname = mapname
st.session_state.df = df
def button3_click():
st.session_state.selected_button = "2025 MCA Scorecard data"
st.session_state.dataset_info = "This dataset includes scorecards for all Mayoral Combined Authorities (MCA) in the United Kingdom. These scorecards indicate how well each MCA area is performing compared to the UK weighted mean of all ITL1 regions in the UK for a broad set of indicators, including productivity performance and drivers of productivity according to the categories: Business Performance; Skills & Training; Health & Wellbeing, and, Investment, infrastructure & Connectivity."
st.session_state.link = "https://lab.productivity.ac.uk/data/productivity-datasets/MCA-scorecards/"
df = pd.read_csv("examples/MCA_Scorecard_map_tool_data.csv")
if not df.empty:
fig = True
mapname = df.columns[1:].tolist()
levels = ['MCA', 'ITL3', 'National']
st.session_state.levels = levels
st.session_state.level = 'MCA'
st.session_state.fig = fig
st.session_state.mapname = mapname
st.session_state.df = df
def button4_click():
st.session_state.selected_button = "2025 ITL3 Scorecard data"
st.session_state.dataset_info = "The TPI UK ITL3 scorecards are produced to assess the United Kingdom's subregional productivity performance through a range of productivity indicators and drivers. These scorecards include data for 179 regions, defined by the International Territorial Level 3 (ITL3). In addition data is available for 12 aggregate ITL1 geographies, covering the whole of the United Kingdom. Data is available for three indicators of productivity, and 12 productivity drivers."
st.session_state.link = "https://lab.productivity.ac.uk/data/productivity-datasets/ITL3-scorecards/"
df = pd.read_csv("examples/ITL3_Scorecard_map_tool_data.csv")
if not df.empty:
fig = True
mapname = df.columns[1:].tolist()
levels = ['ITL3', 'ITL1', 'National']
st.session_state.levels = levels
st.session_state.level = 'ITL3'
st.session_state.fig = fig
st.session_state.mapname = mapname
st.session_state.df = df
def button5_click():
st.session_state.selected_button = "TPI MCA Digitalisation and Innovation Indicators"
st.session_state.dataset_info = "This dataset contains two new indicators produced by the TPI Productivity Lab in collaboration with The Data City to examine disparities in the adoption of innovation practices and the concentration of digital firms within Mayoral Combined Authorities (MCA) in the UK."
st.session_state.link = "https://lab.productivity.ac.uk/data/productivity-datasets/MCA-digitalisation-innovation-indicators/"
df = pd.read_csv("examples/MCA_digitalisation_innovation.csv")
if not df.empty:
fig = True
mapname = df.columns[1:].tolist()
levels = []
st.session_state.levels = levels
st.session_state.level = 'MCA'
st.session_state.fig = fig
st.session_state.mapname = mapname
st.session_state.df = df
def button6_click():
st.session_state.selected_button = "2025 ITL2 Regional and Global Trade"
st.session_state.dataset_info = "This dataset contains data about regional and global trade"
st.session_state.link = "https://www.google.com"
df = pd.read_csv("examples/ITL2_example.csv")
if not df.empty:
fig = True
mapname = df.columns[1:].tolist()
levels = []
st.session_state.levels = levels
st.session_state.level = 'ITL2'
st.session_state.fig = fig
st.session_state.mapname = mapname
st.session_state.df = df
def button7_click():
st.session_state.selected_button = "Subnational trade balance data 2022"
st.session_state.dataset_info = "This dataset contains trade balances for goods and services for EU, non-EU and US trade"
st.session_state.link = "https://www.ons.gov.uk/businessindustryandtrade/internationaltrade/bulletins/internationaltradeinuknationsregionsandcities/2022"
df = pd.read_csv("examples/ITL_tradebalance.csv")
if not df.empty:
fig = True
mapname = df.columns[1:].tolist()
levels = ['ITL1', 'ITL2', 'ITL3']
st.session_state.levels = levels
st.session_state.level = levels[0]
st.session_state.fig = fig
st.session_state.mapname = mapname
st.session_state.df = df
def button8_click():
st.session_state.selected_button = "2025 UK Measures of National Health and Well-being"
st.session_state.dataset_info = "This dataset contains measures of health and wellbeing on the national and ITL1 level. These measures are taken from the ONS 'UK Measures of National Well-being Dashboard' where regional data is available."
st.session_state.link = "https://www.ons.gov.uk/peoplepopulationandcommunity/wellbeing/articles/ukmeasuresofnationalwellbeing/dashboard"
df = pd.read_csv("examples/ITL1_Wellbeing.csv")
if not df.empty:
fig = True
mapname = df.columns[1:].tolist()
levels = ['ITL1', 'National']
st.session_state.levels = levels
st.session_state.level = levels[0]
st.session_state.fig = fig
st.session_state.mapname = mapname
st.session_state.df = df
def button9_click():
st.session_state.selected_button = "2024 UK local authority and regional greenhouse gas emissions"
st.session_state.dataset_info = "This data set contains greenhouse gas emissions measured in carbon dioxide equivalent. These are accredited official statistics from the Department of Energy Security and Net Zero which cover local authorities and has been aggregated to the different International Territorial Levels."
st.session_state.link = "https://www.gov.uk/government/statistics/uk-local-authority-and-regional-greenhouse-gas-emissions-statistics-2005-to-2022"
df = pd.read_csv("examples/all_emissions_2022.csv")
if not df.empty:
fig = True
mapname = df.columns[1:].tolist()
levels = ['ITL3', 'ITL2', 'ITL1', 'LA']
st.session_state.levels = levels
st.session_state.level = levels[0]
st.session_state.fig = fig
st.session_state.mapname = mapname
st.session_state.df = df
# In your main UI code:
with st.expander(label="Pre-existing datasets from **The Productivity Institute Data Lab**", expanded=True):
col1, col2, col3, col4 = st.columns(4)
# Set button types
button1_type = 'primary' if st.session_state.selected_button == "Subregional productivity data - local authoritites 2023" else 'secondary'
button2_type = 'primary' if st.session_state.selected_button == "2025 ITL1 Scorecard Data" else 'secondary'
button3_type = 'primary' if st.session_state.selected_button == "2025 MCA Scorecard data" else 'secondary'
button4_type = 'primary' if st.session_state.selected_button == "2025 ITL3 Scorecard data" else 'secondary'
button5_type = 'primary' if st.session_state.selected_button == "TPI MCA Digitalisation and Innovation Indicators" else 'secondary'
button6_type = 'primary' if st.session_state.selected_button == "2025 ITL2 Regional and Global Trade" else 'secondary'
button7_type = 'primary' if st.session_state.selected_button == "Subnational trade balance data 2022" else 'secondary'
button8_type = 'primary' if st.session_state.selected_button == "2025 UK Measures of National Health and Well-being" else 'secondary'
button9_type = 'primary' if st.session_state.selected_button == "2024 UK local authority and regional greenhouse gas emissions" else 'secondary'
with col1:
if st.button(label='', key='Example_button1', type=button1_type, on_click=button1_click):
st.rerun()
if st.button(label='', key='Example_button5', type=button5_type, on_click=button5_click):
st.rerun()
if st.button(label='', key='Example_button9', type=button9_type, on_click=button9_click):
st.rerun()
with col2:
if st.button(label='', key='Example_button2', type=button2_type, on_click=button2_click):
st.rerun()
if st.button(label='', key='Example_button6', type=button6_type, on_click=button6_click):
st.rerun()
with col3:
if st.button(label='', key='Example_button3', type=button3_type, on_click=button3_click):
st.rerun()
if st.button(label='', key='Example_button7', type=button7_type, on_click=button7_click):
st.rerun()
with col4:
if st.button(label='', key='Example_button4', type=button4_type, on_click=button4_click):
st.rerun()
if st.button(label='', key='Example_button8', type=button8_type, on_click=button8_click):
st.rerun()
if st.session_state.selected_button:
st.markdown(f"### Currently selected: {st.session_state.selected_button}")
st.write(st.session_state.dataset_info)
st.markdown(f"[Click here for more information]({st.session_state.link})")
upload_file = st.file_uploader("Upload a file", type=["csv"])
# Button to confirm the selection
if st.button("Upload File"):
if upload_file:
st.session_state.selected_button = None # So no example datasets are selected if the user inputs their own data
try:
df = pd.read_csv(upload_file, encoding="utf-8", on_bad_lines='skip', low_memory=False)
if df.empty:
st.error("Uploaded CSV is empty")
else:
st.success(f"Successfully loaded {upload_file.name}")
except UnicodeDecodeError:
st.error("Encoding error: Ensure the file is UTF-8 encoded")
except pd.errors.ParserError:
st.error("Parsing error: check if the csv format is valid")
except Exception as e:
st.error(f"An unexpected error occured: {e}")
# Find the levels in the first column
levels = df.iloc[:, 0]
levels['ITL_Level'] = df[df.columns[0]].apply(assign_itl_level)
levels['CA_Level'] = df[df.columns[0]].apply(assign_ca_level)
levels = pd.concat([levels['ITL_Level'], levels['CA_Level']]).drop_duplicates().tolist()
if 'TLB' in list(df[df.columns[0]]):
levels.append('National')
if '' in levels:
levels.remove('')
if len(levels) > 1:
st.session_state.levels = levels
level = levels[0]
st.session_state.level = levels[0]
st.session_state.index = 0
# reset_insights()
if df.iloc[0, 0][:2] == 'TL' or len(df.iloc[0, 0]) == 9:
fig = True
mapname = df.columns[1:].tolist()
else:
fig = False
mapname = []
st.error("Region code not recognised.")
else:
st.error("No file uploaded yet.")
rerun = False
# If there are maps to switch between then display the select box
if mapname:
if index >= len(mapname) or index < 0:
index = 0
st.session_state.index = mapname.index(st.sidebar.selectbox("Select map", options=mapname, index=index, on_change=reset_insights))
# Text box to change current map title
new_title = st.sidebar.text_input('Change title',value=mapname[st.session_state.index])
# Validate input: must not be empty, must be the characters in the regex, cannot be longer than 70 characters
valid_input = True
if not df.empty:
if not new_title:
st.sidebar.error("Cannot accept empty title.")
valid_input = False
if valid_input:
df = df.rename(columns={df.columns[st.session_state.index + 1]: new_title})
if mapname != list(df.columns[1:]):
st.session_state.df = df
st.session_state.mapname = list(df.columns[1:])
mapname = list(df.columns[1:])
rerun = True
else:
# Otherwise show empty select box
st.sidebar.selectbox("Select map", options=mapname)
# If there is more than one geography level in the data then allow the user to select
if len(levels) > 1:
level = st.sidebar.selectbox("Select geography level", options=levels, index=levels.index(level), on_change=reset_insights)
if 'ITL' == level[:3]:
level_to_length = {'ITL1': 3, 'ITL2': 4, 'ITL3': 5}
st.session_state.df = df
df = df.loc[(df[df.columns[0]].str.len() == level_to_length[level]) & (df[df.columns[0]] != 'TLB')].copy()
elif level == 'National':
st.session_state.df = df
df = df[df[df.columns[0]].isin(['TLB', 'TLL', 'TLM', 'TLN'])]
else:
st.session_state.df = df
if level == 'MCA':
df = df.loc[df[df.columns[0]].str[:3].isin(['E47', 'E61'])].copy()
else:
df = df.loc[df[df.columns[0]].str.len() == 9].copy()
else:
st.session_state.df = df
st.session_state.levels = []
# Sidebar updates after upload
st.sidebar.markdown("---") # This creates a basic horizontal line (divider)
unit_options = ['None', '%', '£', '$', '€']
if 'unit' in query_params:
if query_params['unit'] in unit_options:
map_units = unit_options.index(query_params['unit'])
else:
map_units = 0
else:
map_units = 0
unit = st.sidebar.selectbox("Select units", options=unit_options, index=map_units)
dp_options = list(range(6))
if 'dp' in query_params:
try:
map_dp = min(max(float(query_params['dp']), 0), 5)
except:
map_dp = 0
else:
map_dp = 0
dp = st.sidebar.select_slider("Select decimal places", options=dp_options, value=map_dp)
show_missing_values = st.sidebar.toggle(label='Hide the rest of the UK', value=False)
if 'size' in query_params:
try:
map_size = min(max(float(query_params['size']), 0.25), 2)
except:
map_size = 1
else:
map_size = 1
map_height = st.sidebar.slider("Adjust map size", min_value=0.25, max_value=float(2), value=float(map_size), step=0.01) * 550
st.sidebar.markdown("---") # This creates a basic horizontal line (divider)
# Colour change options
discrete_colours = st.sidebar.toggle(label='Use discrete colouring')
num_colours = st.sidebar.slider("Number of Colours", min_value=2, max_value=6, value=5)
if not df.empty and 'index' in st.session_state and mapname:
df[mapname[st.session_state.index]] = (
df[mapname[st.session_state.index]]
.astype(str)
.str.replace(r"[£$€,]", "", regex=True)
)
df[mapname[st.session_state.index]] = pd.to_numeric(df[mapname[st.session_state.index]], errors="coerce")
# Colour pickers
colours = []
# Create two columns in the sidebar using container
with st.sidebar.container():
if discrete_colours:
# Create evenly spaced thresholds
if not df.empty and mapname:
min_val = df[mapname[st.session_state.index]].min()
max_val = df[mapname[st.session_state.index]].max()
thresholds = np.linspace(min_val, float(max_val), num_colours+1)
else:
thresholds = np.linspace(0, 100, num_colours+1)
min_val = 0
max_val = 100
if any(np.isnan(x) for x in thresholds):
thresholds = np.linspace(0, 100, num_colours+1)
min_val = 0
max_val = 100
thresholds = [round(x, 5) for x in thresholds]
colour_column1, colour_column2, colour_column3, colour_column4, colour_column5 = st.columns(5) # Create two columns
step = float(10**-(dp+1))
for i in range(1, num_colours + 1):
if i > 3:
if i == 4:
if num_colours != i:
with colour_column4:
colour = st.color_picker(f"-", "#47be6d", label_visibility='hidden')
with colour_column5:
thresholds[i] = st.number_input('<', float(thresholds[i-1] + step/10), float(thresholds[i+1] - step/10), float(thresholds[i]), step=step, key=f'+input{i}', label_visibility="hidden", format=f'%.{dp}f')
else:
with colour_column4:
colour = st.color_picker(f"-", "#47be6d", label_visibility='hidden')
with colour_column5:
thresholds[i] = st.number_input('<', float(thresholds[i-1] + step/10), value=float(thresholds[i]), step=step, key=f'+input{i}', label_visibility="hidden", format=f'%.{dp}f')
elif i == 5:
if num_colours != i:
with colour_column1:
st.text_input('-', f'{float(thresholds[i-1] + step/10):.{dp}f}', label_visibility='hidden', disabled=True, key=f'text_input_{i}')
with colour_column2:
colour = st.color_picker(f"-", "#f4e625", label_visibility='hidden')
with colour_column3:
thresholds[i] = st.number_input('<', float(thresholds[i-1] + step/10), float(thresholds[i+1] - step/10), float(thresholds[i]), step=step, key=f'+input{i}', label_visibility="hidden", format=f'%.{dp}f')
else:
with colour_column1:
st.text_input('-', f'{float(thresholds[i-1] + step/10):.{dp}f}', label_visibility='hidden', disabled=True, key=f'text_input_{i}')
with colour_column2:
colour = st.color_picker(f"-", "#f4e625", label_visibility='hidden')
with colour_column3:
thresholds[i] = st.number_input('<', float(thresholds[i-1] + step/10), value=float(thresholds[i]), step=step, key=f'+input{i}', label_visibility="hidden", format=f'%.{dp}f')
else:
if num_colours != i:
with colour_column4:
colour = st.color_picker(f"-", "#ffffff", label_visibility='hidden')
with colour_column5:
thresholds[i] = st.number_input('<', float(thresholds[i-1] + step/10), float(thresholds[i+1] - step/10), float(thresholds[i]), step=step, key=f'+input{i}', label_visibility="hidden", format=f'%.{dp}f')
else:
with colour_column4:
colour = st.color_picker(f"-", "#ffffff", label_visibility='hidden')
with colour_column5:
thresholds[i] = st.number_input('<', float(thresholds[i-1] + step/10), value=float(thresholds[i]), step=step, key=f'+input{i}', label_visibility="hidden", format=f'%.{dp}f')
else:
if i == 1:
with colour_column1:
thresholds[i-1] = st.number_input('<', max_value=float(thresholds[i] - step/10), value=float(thresholds[i-1]), step=step, key=f'-input{i}', label_visibility="hidden", format=f'%.{dp}f')
with colour_column2:
colour = st.color_picker(f"-", "#440255", label_visibility='hidden')
with colour_column3:
thresholds[i] = st.number_input('<', float(thresholds[i-1] + step/10), float(thresholds[i+1] - step/10), float(thresholds[i]), step=step, key=f'+input{i}', label_visibility="hidden", format=f'%.{dp}f')
elif i == 2:
if num_colours != i:
with colour_column4:
colour = st.color_picker(f"-", "#39538b", label_visibility='hidden')
with colour_column5:
thresholds[i] = st.number_input('<', float(thresholds[i-1] + step/10), float(thresholds[i+1] - step/10), float(thresholds[i]), step=step, key=f'+input{i}', label_visibility="hidden", format=f'%.{dp}f')
else:
with colour_column4:
colour = st.color_picker(f"-", "#39538b", label_visibility='hidden')
with colour_column5:
thresholds[i] = st.number_input('<', float(thresholds[i-1] + step/10), value=float(thresholds[i]), step=step, key=f'+input{i}', label_visibility="hidden", format=f'%.{dp}f')
elif i == 3:
if num_colours != i:
with colour_column1:
st.text_input('-', f'{float(thresholds[i-1] + step/10):.{dp}f}', label_visibility='hidden', disabled=True, key=f'text_input_{i}')
with colour_column2:
colour = st.color_picker(f"-", "#26828e", label_visibility='hidden')
with colour_column3:
thresholds[i] = st.number_input('<', float(thresholds[i-1] + step/10), float(thresholds[i+1] - step/10), float(thresholds[i]), step=step, key=f'+input{i}', label_visibility="hidden", format=f'%.{dp}f')
else:
with colour_column1:
st.text_input('-', f'{float(thresholds[i-1] + step/10):.{dp}f}', label_visibility='hidden', disabled=True, key=f'text_input_{i}')
with colour_column2:
colour = st.color_picker(f"-", "#26828e", label_visibility='hidden')
with colour_column3:
thresholds[i] = st.number_input('<', float(thresholds[i-1] + step/10), value=float(thresholds[i]), step=step, key=f'+input{i}', label_visibility="hidden", format=f'%.{dp}f')
colours.append(colour)
custom_colour_scale = colours
else:
thresholds=[]
colour_column1, colour_column2 = st.columns(2) # Create two columns
for i in range(num_colours):
if i > 2:
with colour_column2: # Use the second column for colours after index 2
if i == 3:
colour = st.color_picker(f"Pick Colour {i+1}", "#47be6d")
elif i == 4:
colour = st.color_picker(f"Pick Colour {i+1}", "#f4e625")
else:
colour = st.color_picker(f"Pick Colour {i+1}", "#ffffff")
colours.append(colour)
else:
with colour_column1: # Use the first column for the first 3 colours
if i == 0:
colour = st.color_picker(f"Pick Colour {i+1}", "#440255")
elif i == 1:
colour = st.color_picker(f"Pick Colour {i+1}", "#39538b")
elif i == 2:
colour = st.color_picker(f"Pick Colour {i+1}", "#26828e")
colours.append(colour)
custom_colour_scale = generate_colour_scale(colours)
st.sidebar.markdown("---") # This creates a basic horizontal line (divider)
if rerun:
st.rerun()
# Labeling options
if fig:
# Save session state variables and load figure
with figure_loading.container():
with st.spinner('Loading map...'):
st.session_state.fig, st.session_state.mapname = get_figures(df, mcamapping, la_shapes_df, itlmapping, itl3_shapes_df, custom_colour_scale, show_missing_values, unit, dp, thresholds, map_height, st.session_state.index)
figure.plotly_chart(st.session_state.fig, use_container_width=True,
config = {
'toImageButtonOptions': {
'filename': f"TPI_UK_Colour_Map_{st.session_state.mapname[st.session_state.index].replace(' ','_')}",
'scale': 2
}
}
)
map_index = st.session_state.index
st.session_state.index = index
if 'preset' in query_params.keys() and not dvo:
if query_params['preset'] == '2022_la_prod':
button1_click()
st.session_state.dvo = True
st.rerun()
if query_params['preset'] == '2024_itl1_scorecard':
button2_click()
st.session_state.dvo = True
st.rerun()
if query_params['preset'] == '2024_mca_scorecard':
button3_click()
st.session_state.dvo = True
st.rerun()
if query_params['preset'] == '2024_itl3_scorecard':
button4_click()
st.session_state.dvo = True
st.rerun()
if query_params['preset'] == 'dig_inv_indicators':
button5_click()
st.session_state.dvo = True
st.rerun()
if query_params['preset'] == '2025_itl2_trade':
button6_click()
st.session_state.dvo = True
st.rerun()
if query_params['preset'] == '2022_subnat_trade_balance':
button7_click()
st.session_state.dvo = True
st.rerun()
if query_params['preset'] == '2025_health_wellbeing':
button8_click()
st.session_state.dvo = True
st.rerun()
if query_params['preset'] == '2024_la_reg_emissions':
button9_click()
st.session_state.dvo = True
st.rerun()
if query_params['preset'] == 'ln_average_growth':
df = pd.read_csv("examples/ITL3_LN_Average_Growth.csv")
fig = True
mapname = df.columns[1:].tolist()
levels = []
st.session_state.levels = levels
st.session_state.level = 'ITL3'
st.session_state.fig = fig
st.session_state.mapname = mapname
st.session_state.df = df
st.session_state.dvo = True
st.rerun()
# Experimental
# with st.expander('Insights from AI', expanded=False):
# with st.spinner('Loading insights...'):
# if level[:3].lower() == 'itl':
# mapping = pd.read_csv('src/itlmapping.csv')[[level.lower(), f'{level.lower()}name']].set_index(level.lower()).drop_duplicates()
# elif level == 'National':
# mapping = pd.DataFrame({'itl1': ['TLB', 'TLL', 'TLM', 'TLN'],
# 'itl1name': ['England', 'Wales', 'Scotland', 'Northern Ireland']}).set_index('itl1')
# level = 'ITL1'
# else:
# mapping = pd.read_csv('src/mcamapping.csv')[[level.lower(), f'{level.lower()}name']].set_index(level.lower()).drop_duplicates()
# insights_df = df[[df.columns[0], st.session_state.mapname[map_index]]].set_index(df.columns[0])
# insights_df = insights_df.join(mapping).set_index(f'{level.lower()}name', drop=True)[st.session_state.mapname[map_index]].to_dict()
# if len(st.session_state.insights) == 0:
# insight = deepseek.get_insight('map of the UK', st.session_state.mapname[map_index], insights_df)
# st.session_state.insights = insight
# else:
# insight = st.session_state.insights
# st.write(insight)
# filename = f"{mapname[st.session_state.index]}.png"
# # Save the figure as PNG
# pio.write_image(fig[st.session_state.index], filename, format="png", engine="kaleido", scale=3)
# # Provide a download link for the PNG file
# with open(filename, "rb") as file:
# st.download_button(
# label="Download Plot as PNG",
# data=file,
# file_name=filename,
# mime="image/png"
# )
# print(btn)
if __name__ == '__main__':
pd.options.mode.copy_on_write = True
main()