-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhdx_signals.py
237 lines (203 loc) · 8.94 KB
/
hdx_signals.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
#!/usr/bin/python
"""
Generic Blob into HDX Pipeline:
------------
TODO
- Add summary about this dataset pipeline
"""
import logging
import os
from datetime import datetime, timezone
import pandas as pd
from hdx.data.dataset import Dataset
from hdx.data.showcase import Showcase
from slugify import slugify
logger = logging.getLogger(__name__)
class HDXSignals:
def __init__(self, configuration, retriever, folder, errors):
self.configuration = configuration
self.retriever = retriever
self.folder = folder
self.manual_url = None
self.dataset_data = {}
self.errors = errors
self.created_date = None
self.start_date = None
self.latest_date = None
def get_data(self, state):
try:
url = os.environ["BLOB_URL"]
account = os.environ["STORAGE_ACCOUNT"]
container = os.environ["CONTAINER"]
key = os.environ["KEY"]
except Exception:
url = self.configuration["url"]
account = self.configuration["account"]
container = self.configuration["container"]
key = self.configuration["key"]
alerts_filename = self.configuration["alerts_filename"]
locations_filename = self.configuration["locations_filename"]
metadata_filename = self.configuration["metadata_filename"]
dataset_name = self.configuration["dataset_names"]["HDX-SIGNALS"]
alerts_file = self.retriever.download_file(
url=url,
account=account,
container=container,
key=key,
blob=alerts_filename)
data_df_alerts = pd.read_csv(alerts_file, sep=",", escapechar='\\', keep_default_na=False).replace('[“”]', '', regex=True)
data_df_alerts['date'] = pd.to_datetime(data_df_alerts['date'])
# Find the minimum and maximum dates
self.start_date = data_df_alerts['date'].min()
self.latest_date = data_df_alerts['date'].max()
locations_file = self.retriever.download_file(
url=url,
account=account,
container=container,
key=key,
blob=locations_filename)
data_df_locations = pd.read_csv(locations_file, sep=",", escapechar='\\', keep_default_na=False).replace('[“”]', '', regex=True)
metadata_file = self.retriever.download_file(
url=url,
account=account,
container=container,
key=key,
blob=metadata_filename)
metadata_dict = pd.read_table(metadata_file, sep=",")
colnames = ['iso3', 'acled_conflict', 'idmc_displacement_conflict',
'idmc_displacement_disaster', 'ipc_food_insecurity', 'jrc_agricultural_hotspots']
data_df_locations_subset = data_df_locations[colnames]
data_df_locations_subset.rename(columns={'iso3': 'Alpha-3 code'}, inplace=True)
lat_lon_file = pd.read_csv("metadata/countries_codes_and_coordinates.csv", sep=",")
latitude = []
longitude = []
for iso3 in data_df_locations_subset['Alpha-3 code']:
try:
lat = lat_lon_file.loc[lat_lon_file['Alpha-3 code'] == iso3, 'Latitude (average)'].iloc[0]
lon = lat_lon_file.loc[lat_lon_file['Alpha-3 code'] == iso3, 'Longitude (average)'].iloc[0]
except Exception:
lat = "NA"
lon = "NA"
latitude.append(lat)
longitude.append(lon)
data_df_locations_subset['Latitude'] = latitude
data_df_locations_subset['Longitude'] = longitude
data_df_alerts.to_csv("metadata/signals.csv", sep=';', encoding='utf-8', index=False)
data_df_locations_subset.to_csv("metadata/location_metadata.csv", sep=';', encoding='utf-8', index=False)
self.dataset_data[dataset_name] = [data_df_alerts.apply(lambda x: x.to_dict(), axis=1),
data_df_locations.apply(lambda x: x.to_dict(), axis=1),
metadata_dict.apply(lambda x: x.to_dict(), axis=1)]
self.created_date = datetime.fromtimestamp((os.path.getctime(alerts_file)), tz=timezone.utc)
if self.created_date > state.get(dataset_name, state["DEFAULT"]):
if self.dataset_data:
return [{"name": dataset_name}]
else:
return None
def generate_dataset_and_showcase(self, dataset_name):
# Setting metadata and configurations
name = self.configuration["dataset_names"]["HDX-SIGNALS"]
title = self.configuration["title"]
update_frequency = self.configuration["update_frequency"]
dataset = Dataset({"name": slugify(name), "title": title})
rows = self.dataset_data[dataset_name][0]
dataset.set_maintainer(self.configuration["maintainer_id"])
dataset.set_organization(self.configuration["organization_id"])
dataset.set_expected_update_frequency(update_frequency)
dataset.set_subnational(False)
dataset.add_other_location("world")
dataset["notes"] = self.configuration["notes"]
filename = "hdx_signals.csv"
resource_data = {"name": filename,
"description": self.configuration["description_alerts_file"]}
tags = sorted([t for t in self.configuration["allowed_tags"]])
dataset.add_tags(tags)
# Setting time period
start_date = self.start_date
ongoing = False
if not start_date:
logger.error(f"Start date missing for {dataset_name}")
return None, None
dataset.set_time_period(start_date, self.latest_date, ongoing)
headers = rows[0].keys()
date_headers = [h for h in headers if "date" in h.lower() and type(rows[0][h]) == int]
for row in rows:
for date_header in date_headers:
row_date = row[date_header]
if not row_date:
continue
if len(str(row_date)) > 9:
row_date = row_date / 1000
row_date = datetime.utcfromtimestamp(row_date)
row_date = row_date.strftime("%Y-%m-%d")
row[date_header] = row_date
rows
dataset.generate_resource_from_rows(
self.folder,
filename,
rows,
resource_data,
list(rows[0].keys()),
encoding='utf-8'
)
second_filename = "hdx_signals_location_metadata.csv"
resource_data = {"name": second_filename,
"description": self.configuration["description_locations_file"]}
rows = self.dataset_data[dataset_name][1]
headers = rows[0].keys()
date_headers = [h for h in headers if "date" in h.lower() and type(rows[0][h]) == int]
for row in rows:
for date_header in date_headers:
row_date = row[date_header]
if not row_date:
continue
if len(str(row_date)) > 9:
row_date = row_date / 1000
row_date = datetime.utcfromtimestamp(row_date)
row_date = row_date.strftime("%Y-%m-%d")
row[date_header] = row_date
rows
dataset.generate_resource_from_rows(
self.folder,
second_filename,
rows,
resource_data,
list(rows[0].keys()),
encoding='utf-8'
)
resource_data = {"name": "hdx_signals_data_dictionary.csv",
"description": self.configuration["description_metadata_file"]}
rows = self.dataset_data[dataset_name][2]
headers = rows[0].keys()
date_headers = [h for h in headers if "date" in h.lower() and type(rows[0][h]) == int]
for row in rows:
for date_header in date_headers:
row_date = row[date_header]
if not row_date:
continue
if len(str(row_date)) > 9:
row_date = row_date / 1000
row_date = datetime.utcfromtimestamp(row_date)
row_date = row_date.strftime("%Y-%m-%d")
row[date_header] = row_date
rows
dataset.generate_resource_from_rows(
self.folder,
"hdx_signals_data_dictionary.csv",
rows,
resource_data,
list(rows[0].keys()),
encoding='utf-8'
)
if not self.configuration["visualization_link"]:
return dataset, None
showcase = Showcase(
{
"name": f"{slugify(dataset_name)}-showcase",
"title": f"{dataset['title']} Showcase",
"notes": dataset["notes"],
"url": self.configuration["visualization_link"],
"image_url": "https://raw.githubusercontent.com/OCHA-DAP/hdx-signals-alerts/main/config/HDXSignalsLogo_V2.png",
}
)
showcase.add_tags(tags)
return dataset, showcase