-
Notifications
You must be signed in to change notification settings - Fork 0
/
FTS-analysis.py
276 lines (231 loc) · 11.4 KB
/
FTS-analysis.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
# ---
# jupyter:
# jupytext:
# formats: ipynb,py:percent
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.9.1
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---
# %% [markdown] slideshow={"slide_type": "slide"}
# # Analysis of FTS financial flows to the CCCM sector
#
# ### Limitations
# - FTS data comes from agency and donor reporting and can vary in quality.
# - Many FTS flows combine funds for CCCM with other sectors, meaning that some figures may overestimate CCCM specific funding. These limitations are documented in the accompanying notes.
# - 2020 flows may be incomplete, subject to reporting delays.
# %%
import requests
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.ticker as tkr
import openpyxl
import matplotlib #imported for heatchart
import geopandas as gpd
import geoviews as gv
from cartopy import crs
import holoviews as hv
# style seaborn
sns.set_theme()
sns.set_context("talk")
sns.set_style("white")
# %%
# read and clean data
old = pd.read_excel('FTS2005-2019.xlsx', sheet_name='2005-201')
new = pd.read_excel('FTS2020.xlsx', sheet_name='Results - Incoming')
full = new.append(old, ignore_index=True)
def splitagencies(x):
if x['Destination Organization']=='International Organization for Migration':
return 'IOM'
else:
return 'Other agencies'
full['Agency'] = full.apply(splitagencies, axis=1)
# %% slideshow={"slide_type": "slide"}
# plot total funding per year
p = full.groupby(['Destination Usage year']).agg({'Amount (USD)':['sum']}).reset_index()
p.columns=['Year','Total']
fig, ax = plt.subplots(figsize = (18,9))
g = sns.barplot(data=p,x="Year", y="Total", color="#2a87c8").set_title("Total CCCM Funding")
ax.yaxis.set_major_formatter(tkr.FuncFormatter(lambda y, p: f'${y/1000000:,.0f}m'))
ax.set(xlabel='Year', ylabel='Funding (US$)')
sns.despine()
plt.savefig('outputs/one.svg')
# %% [markdown]
# On average, CCCM funding increased approximately 56% year-on-year since 2005
# The CCCM sector has received approximately US$1,065m since 2005
# %%
# print funding
print(f"Total CCCM Funding: ${full['Amount (USD)'].sum():,d}")
# %% slideshow={"slide_type": "slide"}
#plot per country - 2005-2020
fig, ax = plt.subplots(figsize = (18,16))
df = full
df2 = df.groupby(['Destination Location']).agg({'Amount (USD)':['sum']})
df3 = df2.reset_index()
df3.columns=['Country','Funding']
df3 = df3.sort_values(['Funding']).reset_index(drop=True)
g = sns.barplot(data=df3, y="Country", x="Funding", color="#2a87c8").set_title("Funding per Country - 2005-2020")
ax.xaxis.set_major_formatter(tkr.FuncFormatter(lambda y, p: f'${y/1000000:,.0f}m'))
ax.set(xlabel='Funding (US$)', ylabel='Country')
sns.despine()
plt.savefig('outputs/four.svg')
# %% [markdown]
# ### Limitations:
# - Yemen funding in is misleading as it includes funding for all sectors by the government of Saudi Arabia
# - CAR data is inaccurate as many flows were mistakenly tagged as CCCM
# - Jordan data is inaccurate
# %% slideshow={"slide_type": "slide"}
# map countries funded between 2005-2020
mapdata = full.groupby(['Destination Location']).agg({'Amount (USD)':['sum']})
mapdata = mapdata.reset_index()
mapdata.columns=['Destination Location','Funding']
gv.extension('bokeh', 'matplotlib',logo=False)
gv.output(backend='bokeh', fig='png', logo=False, size=800)
geometries = gpd.read_file('./world.geojson')
geometries.rename(columns={"NAME":"Destination Location"}, inplace=True)
geometries['Destination Location'] = geometries['Destination Location'].replace('Democratic Republic of the Congo','Congo, The Democratic Republic of the')
mapdf = gpd.GeoDataFrame(pd.merge(geometries, mapdata))
basemap = gv.Polygons(geometries, crs=crs.GOOGLE_MERCATOR).opts(line_color='w', line_alpha=0.3, color='#A6A6A6', xaxis=None, yaxis=None, width=200, height=100)
map = gv.Polygons(mapdf,label='Trainings', crs=crs.GOOGLE_MERCATOR).opts(show_frame=False, width=200, height=100, xaxis=None, yaxis=None, color='#2a87c8', line_color='w', line_alpha=0.3, title="43 countries received CCCM funding between 2005-2020")
t = basemap * map
hv.save(t, 'outputs/mapall.png', fmt='png')
t
# todo: find a way to export the map as an svg. rendering as bokeh stopped working for some reason
# %% slideshow={"slide_type": "slide"}
#plot per country - 2019
fig, ax = plt.subplots(figsize = (18,9))
df = full[full['Destination Usage year']==2019]
df2 = df.groupby(['Destination Location']).agg({'Amount (USD)':['sum']})
df3 = df2.reset_index()
df3.columns=['Country','Funding']
df3 = df3.sort_values(['Funding']).reset_index(drop=True)
g = sns.barplot(data=df3, y="Country", x="Funding", color="#2a87c8").set_title("Funding per Country - 2019")
ax.xaxis.set_major_formatter(tkr.FuncFormatter(lambda y, p: f'${y/1000000:,.0f}m'))
ax.set(xlabel='Funding (US$)', ylabel='Country')
sns.despine()
plt.savefig('outputs/five.svg')
# %% [markdown]
# 16 countries received CCCM funding in 2020 compared to 14 in 2009
# Funding ended for Cameroon and Haiti and started for Honduras, Mozambique, Niger and Philippines
# %% slideshow={"slide_type": "slide"}
#plot per country - 2020
fig, ax = plt.subplots(figsize = (18,9))
df = full[full['Destination Usage year']==2020]
df2 = df.groupby(['Destination Location']).agg({'Amount (USD)':['sum']})
df3 = df2.reset_index()
df3.columns=['Country','Funding']
df3 = df3.sort_values(['Funding']).reset_index(drop=True)
g = sns.barplot(data=df3, y="Country", x="Funding", color="#2a87c8").set_title("Funding per Country - 2020")
ax.xaxis.set_major_formatter(tkr.FuncFormatter(lambda y, p: f'${y/1000000:,.0f}m'))
ax.set(xlabel='Funding (US$)', ylabel='Country')
sns.despine()
plt.savefig('outputs/six.svg')
# %% slideshow={"slide_type": "slide"}
# map countries funded in 2020
mapdata2020 = full[full['Destination Usage year']==2020].groupby(['Destination Location']).agg({'Amount (USD)':['sum']})
mapdata2020 = mapdata2020.reset_index()
mapdata2020.columns=['Destination Location','Funding']
geometries['Destination Location'] = geometries['Destination Location'].replace('Democratic Republic of the Congo','Congo, The Democratic Republic of the')
mapdf2020 = gpd.GeoDataFrame(pd.merge(geometries, mapdata2020))
map2020 = gv.Polygons(mapdf2020,label='Trainings', crs=crs.GOOGLE_MERCATOR).opts(show_frame=False, width=200, height=100, xaxis=None, yaxis=None, color='#2a87c8', line_color='w', line_alpha=0.3, title="16 countries received CCCM funding in 2020")
t2 = basemap * map2020
hv.save(t2, 'outputs/map2020.png', fmt='png')
t2
# %% slideshow={"slide_type": "slide"}
# full.groupby(['Destination Location']).agg({'Amount (USD)':['sum']})
hmdf = full[['Destination Usage year','Destination Location','Amount (USD)']].groupby(['Destination Usage year','Destination Location']).agg({'Amount (USD)':['sum']}).reset_index()
hmdf.columns=['Year','Country','Funding']
hmdf = hmdf.astype({'Funding': 'int64','Country': 'category','Year':'int64'})
hmdf = hmdf.pivot('Country','Year','Funding').fillna(0).astype('int64')
norm = matplotlib.colors.Normalize(0,20000000)
colors = [[norm(0), "white"],
[norm(500000), "#eff3ff"],
[norm(2500000), "#bdd7e7"],
[norm(5000000), "#6baed6"],
[norm(20000000), "#2171b5"]]
cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", colors)
f, ax = plt.subplots(figsize=(27, 18))
s= sns.heatmap(hmdf, annot=False, cmap=cmap, fmt=".1f", linewidths=.5, ax=ax)
cbar = s.collections[0].colorbar
cbar.set_ticks([0, 25000000, 50000000, 75000000, 113593430])
cbar.set_ticklabels(['$0', '$25m','$50m','$75m','$113m'])
plt.savefig('outputs/seven.svg')
# %% [markdown]
# The longest consecutively funding countries for CCCM are South Sudan (9 years) and Yemen (12 years)
# %% slideshow={"slide_type": "slide"}
# plot the numbr of CCCM agencies per year
agencies = full.groupby(['Destination Usage year']).agg({'Destination Organization':['nunique']}).reset_index()
agencies.columns = ['Year','Number of organizations']
fig, ax = plt.subplots(figsize = (18,9))
g = sns.barplot(data=agencies,x="Year", y="Number of organizations", color="#2a87c8").set_title("Number of CCCM Organizations")
ax.set(xlabel='Year', ylabel='Number of organizations')
sns.despine()
plt.savefig('outputs/eight.svg')
# %% [markdown]
# Approximately 117 agencies have received CCCM funding since 2005
# %% [markdown]
# ###
# %% slideshow={"slide_type": "slide"}
donors = full.groupby(['Destination Usage year']).agg({'Source Organization':['nunique']}).reset_index()
donors.columns = ['Year','Number of donors']
fig, ax = plt.subplots(figsize = (18,9))
g = sns.barplot(data=donors,x="Year", y="Number of donors", color="#2a87c8").set_title("Number of CCCM donors")
ax.set(xlabel='Year', ylabel='Number of donors')
sns.despine()
plt.savefig('outputs/nine.svg')
# %% [markdown]
# 2020 seen the largest increase in donors to CCCM since 2009
# %%
# number of orgs in 2020
f2020 = full[full['Destination Usage year'] == 2020]
len(f2020['Source Organization'].unique())
# %% slideshow={"slide_type": "slide"}
donorsall = full.groupby(['Source Organization']).agg({'Amount (USD)':['sum']}).reset_index()
donorsall.columns = ['Donor', 'Funding']
donorsall = donorsall.sort_values(['Funding']).reset_index(drop=True)
fig, ax = plt.subplots(figsize = (18,16))
g = sns.barplot(data=donorsall, y="Donor", x="Funding", color="#2a87c8").set_title("Funding per Donor - 2005-2020")
ax.xaxis.set_major_formatter(tkr.FuncFormatter(lambda y, p: f'${y/1000000:,.0f}m'))
ax.set(xlabel='Funding (US$)', ylabel='Donor')
sns.despine()
plt.savefig('outputs/ten.svg')
#how much funding was from ECHO and USA
#donorsall[donorsall['Donor']=='United States of America, Government of'].agg({'Funding':['sum']})
#donorsall[donorsall['Donor']=='European Commission\'s Humanitarian Aid and Civil Protection Department'].agg({'Funding':['sum']})
# %% [markdown]
# Since 2005, funding from the US government and ECHO account for over 42% of all funding received for CCCM
# %% slideshow={"slide_type": "slide"}
# 2019 donors
f2019 = full[full['Destination Usage year']==2019]
donors2019 = f2019.groupby(['Source Organization']).agg({'Amount (USD)':['sum']}).reset_index()
donors2019.columns = ['Donor', 'Funding']
donors2019 = donors2019.sort_values(['Funding']).reset_index(drop=True)
fig, ax = plt.subplots(figsize = (18,8))
g = sns.barplot(data=donors2019, y="Donor", x="Funding", color="#2a87c8").set_title("Funding per Donor - 2019")
ax.xaxis.set_major_formatter(tkr.FuncFormatter(lambda y, p: f'${y/1000000:,.0f}m'))
ax.set(xlabel='Funding (US$)', ylabel='Donor')
sns.despine()
plt.savefig('outputs/eleven.svg')
# %% slideshow={"slide_type": "slide"}
# 2020 donors
f2020 = full[full['Destination Usage year']==2020]
donors2020 = f2020.groupby(['Source Organization']).agg({'Amount (USD)':['sum']}).reset_index()
donors2020.columns = ['Donor', 'Funding']
donors2020 = donors2020.sort_values(['Funding']).reset_index(drop=True)
fig, ax = plt.subplots(figsize = (18,10))
g = sns.barplot(data=donors2020, y="Donor", x="Funding", color="#2a87c8").set_title("Funding per Donor - 2020")
ax.xaxis.set_major_formatter(tkr.FuncFormatter(lambda y, p: f'${y/1000000:,.0f}m'))
ax.set(xlabel='Funding (US$)', ylabel='Donor')
sns.despine()
plt.savefig('outputs/twelve.svg')
# %% [markdown]
# 6 additional donors funded CCCM in 2020 compared to 2019
# %%
#print the donor count
print(f"Number of donors in 2020: {len(donors2020['Donor'].unique())} \nNumber of donors in 2019: {len(donors2019['Donor'].unique())}")