-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualisation_tools.py
334 lines (270 loc) · 13.3 KB
/
visualisation_tools.py
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
# This File contains methods for visualising/mapping predicted data.
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as tck
import cartopy.crs as ccrs
import xarray as xr
import cartopy.feature as cf
import seaborn as sns
filepath = 'data.csv'
sns.set(style="white", context="talk")
def prediction_formatting(filepath):
""" Returns a dataframe with columns for map plotting. """
df = pd.read_csv(filepath)
df['2040_change'] = df['2040_mean']/df['maize_a_2010'] - 1
df['2025_change'] = df['2025_mean']/df['maize_a_2010'] - 1
df['2020_change'] = df['2020_mean']/df['maize_a_2010'] - 1
return df
def change_worldmap(df_raw, year='2040'):
""" Returns plot of world yield changes from 2010 to a given year (2020, 2025 or 2040).s"""
# create dataframe of relevant variables
df = df_raw[['lon','lat', year+'_change','iso3']]
# calculate mean changes for global, LIFDC and US
global_change = df[ year+'_change'].mean()
lifdc_df = df[df['iso3'].isin(['AFG', 'BGD', 'BEN', 'BGD', 'BDI', 'CMR', 'CAF',
'TCD', 'COG', 'CIV', 'PRK', 'COD', 'DJI', 'ERI', 'ETH', 'GMB', 'GHA',
'GNB', 'HTI', 'IND', 'KEN', 'KGZ', 'LSO', 'LBR', 'MDG', 'MWI', 'MLI',
'MRT', 'MOZ', 'NPL', 'NIC', 'NER', 'RWA', 'STP', 'SEN', 'SLE', 'SOM',
'SLP', 'SSD', 'SDN', 'SYR', 'TJK', 'TGO', 'UGA', 'TZA', 'UZB', 'VNM',
'YEM', 'ZWE'])]
lifdc_change = lifdc_df[year+'_change'].mean()
us_df = df[df['iso3'] == 'USA']
us_change = us_df[ year+'_change'].mean()
# text for box
t1 = 'Yield change:'
t2 = 'USA= {:.2%}'.format(us_change)
t3 = 'LIFDC = {:.2%}'.format(lifdc_change)
t4 = 'Global= {:.2%}'.format(global_change)
# convert dataframe to data array
df_values = df[['lon','lat', year+'_change']]
df_pv = df_values.pivot(index='lat', columns='lon')
df_pv = df_pv.droplevel(0, axis=1)
da = xr.DataArray(data=df_pv)
# plot
plt.figure(figsize=(12,5))
ax = plt.subplot(projection=ccrs.PlateCarree())
da.plot.pcolormesh('lon', 'lat', ax=ax, vmax=1, extend='both', cmap='RdBu_r',
cbar_kwargs={'fraction': 0.019,'pad': 0.10, 'format': tck.PercentFormatter(xmax=1.0) }) #'label': '%'
gl = ax.gridlines(draw_labels=True)
gl.xlabel_style = {'size': 12}
gl.ylabel_style = {'size': 12}
ax.coastlines(resolution='50m')
ax.set_extent([-160, 180, -60, 85])
ax.set_title('Maize Yield Change ' + year + '\n', size='x-large')
ax.set_xlabel('Longitude')
ax.set_ylabel('Latitude')
ax.set_aspect("equal")
plt.text(-170,-50, t1 + '\n' + t2 + '\n' + t3 + '\n'+ t4, fontsize=10,
bbox=dict(facecolor='white', edgecolor='grey', pad=10.0))
plt.show()
def mean_worldmap(df_raw, year='2040'):
""" Returns worldmap of mean yield predictions for given year (2020, 2025 or 2040). """
# create dataframe of relevant variables
df = df_raw[['lon','lat', year+'_mean','iso3']]
df['yield'] = df[year+'_mean']/1000
# calculate mean changes for global, LIFDC and US
global_change = df['yield'].mean()
lifdc_df = df[df['iso3'].isin(['AFG', 'BGD', 'BEN', 'BGD', 'BDI', 'CMR', 'CAF',
'TCD', 'COG', 'CIV', 'PRK', 'COD', 'DJI', 'ERI', 'ETH', 'GMB', 'GHA',
'GNB', 'HTI', 'IND', 'KEN', 'KGZ', 'LSO', 'LBR', 'MDG', 'MWI', 'MLI',
'MRT', 'MOZ', 'NPL', 'NIC', 'NER', 'RWA', 'STP', 'SEN', 'SLE', 'SOM',
'SLP', 'SSD', 'SDN', 'SYR', 'TJK', 'TGO', 'UGA', 'TZA', 'UZB', 'VNM',
'YEM', 'ZWE'])]
lifdc_change = lifdc_df['yield'].mean()
us_df = df[df['iso3'] == 'USA']
us_change = us_df['yield'].mean()
# text for box
t1 = 'Yield:'
t2 = 'USA= {:.2f} (ton/ha)'.format(us_change)
t3 = 'LIFDC = {:.2f} (ton/ha)'.format(lifdc_change)
t4 = 'Global= {:.2f} (ton/ha)'.format(global_change)
# convert dataframe to data array
df_values = df[['lon','lat', year+'_mean']]
df_pv = df_values.pivot(index='lon', columns='lat')
df_pv = df_pv.droplevel(0, axis=1)
da = xr.DataArray(data=df_pv)
# plot
plt.figure(figsize=(12,5))
ax = plt.subplot(projection=ccrs.PlateCarree())
da.plot.pcolormesh('lon', 'lat', ax=ax, cmap='magma_r',
cbar_kwargs={'fraction': 0.01, 'pad': 0.10) #'label': '%'
ax.gridlines(draw_labels=True)
ax.coastlines(resolution='50m')
ax.set_extent([-160, 180, -60, 85])
ax.set_title('Maize Yield ' + year + '\n', size='x-large')
ax.set_xlabel('Longitude')
ax.set_ylabel('Latitude')
ax.set_aspect("equal")
plt.text(-170,-50, t1 + '\n' + t2 + '\n' + t3 + '\n'+ t4, fontsize=10,
bbox=dict(facecolor='white', edgecolor='grey', pad=10.0))
plt.show()
def std_worldmap(df_raw, year='2040'):
""" Returns world map of standard deviations in yield predictions for 2020, 2025 or 2040. """
# create dataframe of relevant variables
df = df_raw[['lon','lat', year+'_std','iso3']]
df['yield'] = df[year+'_std']/1000
# calculate mean changes for global, LIFDC and US
global_change = df['yield'].mean()
lifdc_df = df[df['iso3'].isin(['AFG', 'BGD', 'BEN', 'BGD', 'BDI', 'CMR', 'CAF',
'TCD', 'COG', 'CIV', 'PRK', 'COD', 'DJI', 'ERI', 'ETH', 'GMB', 'GHA',
'GNB', 'HTI', 'IND', 'KEN', 'KGZ', 'LSO', 'LBR', 'MDG', 'MWI', 'MLI',
'MRT', 'MOZ', 'NPL', 'NIC', 'NER', 'RWA', 'STP', 'SEN', 'SLE', 'SOM',
'SLP', 'SSD', 'SDN', 'SYR', 'TJK', 'TGO', 'UGA', 'TZA', 'UZB', 'VNM',
'YEM', 'ZWE'])]
lifdc_change = lifdc_df['yield'].mean()
us_df = df[df['iso3'] == 'USA']
us_change = us_df['yield'].mean()
# text for box
t1 = 'Yield standard deviation:'
t2 = 'USA= {:.2f} ton/ha'.format(us_change)
t3 = 'LIFDC = {:.2f} ton/ha'.format(lifdc_change)
t4 = 'Global= {:.2f} ton/ha'.format(global_change)
# convert dataframe to data array
df_values = df[['lon','lat','yield']]
df_pv = df_values.pivot(index='lon', columns='lat')
df_pv = df_pv.droplevel(0, axis=1)
da = xr.DataArray(data=df_pv)
# plot
plt.figure(figsize=(12,5))
ax = plt.subplot(projection=ccrs.PlateCarree())
da.plot.pcolormesh('lon', 'lat', ax=ax, cbar_kwargs={'fraction': 0.019, 'pad': 0.05},
cmap= 'magma_r')
gl = ax.gridlines(draw_labels=True)
gl.xlabel_style = {'size': 12}
gl.ylabel_style = {'size': 12}
ax.coastlines(resolution='50m')
ax.set_extent([-160, 180, -60, 85])
ax.set_title('Maize Yield Standard Deviation ' + year + '\n', size='x-large')
ax.set_xlabel('Longitude')
ax.set_ylabel('Latitude')
ax.set_aspect("equal")
plt.text(-170,-50, t1 + '\n' + t2 + '\n' + t3 + '\n'+ t4, fontsize=10,
bbox=dict(facecolor='white', edgecolor='grey', pad=10.0))
plt.show()
def change_countrymap(df_raw, year='2040', iso3='USA'):
""" Produces map of a given country's change in yield from 2010 to 2020, 2025 or 2040. """
coords = {'USA': [-135, -65, 22, 50], 'CHN': [71, 140, 10, 50],
'BRA': [-80, -30, -45, 8]}
# create dataframe of relevant variables
df = df_raw[['lon','lat', year+'_change','iso3']]
country_df = df[df['iso3'] == iso3]
country_change = country_df[year+'_change'].mean()
# convert dataframe to data array
df_values = country_df[['lon','lat', year+'_change']]
df_pv = df_values.pivot(index='lat', columns='lon')
df_pv = df_pv.droplevel(0, axis=1)
da = xr.DataArray(data=df_pv)
# plot
plt.figure(figsize=(12,5))
ax = plt.subplot(projection=ccrs.PlateCarree())
da.plot.pcolormesh('lon', 'lat', ax=ax, vmin=-1, vmax=1, extend='both', cmap='RdBu_r',
cbar_kwargs={'fraction': 0.019, 'pad': 0.10,'format': tck.PercentFormatter(xmax=1.0)})
ax.gridlines(draw_labels=True)
ax.coastlines(resolution='50m')
ax.add_feature(cf.BORDERS)
ax.set_extent(coords[iso3])
ax.set_title('Maize Yield Change ' + year + '\n', size='xx-large')
ax.set_aspect("equal")
t1 = 'National yield change: {:.2%}'.format(country_change)
plt.text((coords[iso3])[0]+5, (coords[iso3])[2]+5, t1, fontsize=12,
bbox=dict(facecolor='white', edgecolor='grey', pad=10.0))
plt.show()
def mean_countrymap(df_raw, year='2040', iso3='USA'):
""" Produces map of a given country's mean yield predictions for 2020, 2025 or 2040. """
coords = {'USA': [-135, -65, 22, 50], 'CHN': [71, 140, 10, 50],
'BRA': [-80, -30, -45, 8]}
# create dataframe of relevant variables
df = df_raw[['lon','lat', year+'_mean','iso3']]
df['yield'] = df[year+'_mean']/1000
country_df = df[df['iso3'] == iso3]
country_change = country_df['yield'].mean()
# convert dataframe to data array
df_values = country_df[['lon','lat', 'yield']]
df_pv = df_values.pivot(index='lat', columns='lon')
df_pv = df_pv.droplevel(0, axis=1)
da = xr.DataArray(data=df_pv)
# plot
plt.figure(figsize=(12,5))
ax = plt.subplot(projection=ccrs.PlateCarree())
da.plot.pcolormesh('lon', 'lat', ax=ax, cmap= 'magma_r',
cbar_kwargs={'fraction': 0.019, 'pad': 0.10})
ax.gridlines(draw_labels=True)
ax.coastlines(resolution='50m')
ax.add_feature(cf.BORDERS)
ax.set_extent(coords[iso3])
ax.set_title('Maize Yield ' + year + '\n', size='x-large')
ax.set_aspect("equal")
t1 = 'National yield: {:.2f} ton/ha'.format(country_change)
plt.text((coords[iso3])[0]+5, (coords[iso3])[2]+5, t1, fontsize=10,
bbox=dict(facecolor='white', edgecolor='grey', pad=10.0))
plt.show()
def std_countrymap(df_raw, year='2040', iso3='USA'):
""" Produces map of a given country's standard deviations in yield predictions for 2020, 2025 or 2040. """
coords = {'USA': [-135, -65, 22, 50], 'CHN': [71, 140, 10, 50],
'BRA': [-80, -30, -45, 8]}
# create dataframe of relevant variables
df = df_raw[['lon','lat', year+'_std','iso3']]
df['yield'] = df[year+'_std']/1000
country_df = df[df['iso3'] == iso3]
country_change = country_df['yield'].mean()
# convert dataframe to data array
df_values = country_df[['lon','lat', 'yield']]
df_pv = df_values.pivot(index='lat', columns='lon')
df_pv = df_pv.droplevel(0, axis=1)
da = xr.DataArray(data=df_pv)
# plot
plt.figure(figsize=(12,5))
ax = plt.subplot(projection=ccrs.PlateCarree())
da.plot.pcolormesh('lon', 'lat', ax=ax, cmap= 'magma_r',
cbar_kwargs={'fraction': 0.019, 'pad': 0.10})
ax.gridlines(draw_labels=True)
ax.coastlines(resolution='50m')
ax.add_feature(cf.BORDERS)
ax.set_extent(coords[iso3])
ax.set_title('Maize Yield Standard Deviation ' + year + '\n', size='x-large')
ax.set_aspect("equal")
t1 = 'National standard deviation: {:.2f} ton/ha'.format(country_change)
plt.text((coords[iso3])[0]+5, (coords[iso3])[2]+5, t1, fontsize=10,
bbox=dict(facecolor='white', edgecolor='grey', pad=10.0))
plt.show()
def yield_vs_time(filepath):
""" Returns violin graph of yield distribution as a function of time. """
# Seperate data into classes
df= pd.read_csv(filepath)
df_2020 = pd.concat([df['2p6_2019_predict'], df['2p6_2020_predict'], df['2p6_2021_predict'],
df['4p5_2019_predict'], df['4p5_2020_predict'], df['4p5_2021_predict'],
df['6p0_2019_predict'], df['6p0_2020_predict'], df['6p0_2021_predict'],
df['8p5_2019_predict'], df['8p5_2020_predict'], df['8p5_2021_predict']],
ignore_index=True, axis=0)
df_2025 = pd.concat([df['2p6_2024_predict'], df['2p6_2025_predict'], df['2p6_2026_predict'],
df['4p5_2024_predict'], df['4p5_2025_predict'], df['4p5_2026_predict'],
df['6p0_2024_predict'], df['6p0_2025_predict'], df['6p0_2026_predict'],
df['8p5_2024_predict'], df['8p5_2025_predict'], df['8p5_2026_predict']],
ignore_index=True, axis=0)
df_2040 = pd.concat([df['2p6_2039_predict'], df['2p6_2040_predict'], df['2p6_2042_predict'],
df['4p5_2039_predict'], df['4p5_2040_predict'], df['4p5_2042_predict'],
df['6p0_2039_predict'], df['6p0_2040_predict'], df['6p0_2042_predict'],
df['8p5_2039_predict'], df['8p5_2040_predict'], df['8p5_2042_predict']],
ignore_index=True, axis=0)
df_violin = pd.concat([df['maize_a_2010'], df_2020, df_2025, df_2040], axis=1)
df_violin.columns = ['2010', '2020', '2025', '2040']
df_violin = df_violin.div(1000)
# Plot
plt.figure()
plt.title('Yield as a function of time \n')
plt.xlabel('Year')
plt.ylabel('Yield (ton/ha) \n')
plt.grid(which= 'major', axis='y')
sns.violinplot(data=df_violin, palette='Blues', inner="quart")
plt.show()
def feature_importance():
""" Dummy function to create feature importance graph. """
# fake dataframe
df = pd.DataFrame({'Feature1': 80, 'Feature2': 50, 'Feature3': 44,
'Feature4': 13, 'Feature5': 2, 'Feature6': 0.09})
# df = pd.read_csv(filepath)
# names = df['Features'].values
# importance = df['Importance'].values
sns.barplot(data= df, palette="rocket", orient='h')
plt.title('Feature Importance')
plt.xlabel('Importance (%)')