-
Notifications
You must be signed in to change notification settings - Fork 0
/
codelists.py
176 lines (165 loc) · 8.72 KB
/
codelists.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
from lxml import etree
import requests
import re
import json
# Set version to 2.03 and language to English
VERSION = "2.03"
LANGUAGE = "en"
STANDARD = "activity"
# A function to convert xpath names to Datastore field names
def convertNameToCanonical(xpath):
retval = xpath.replace("//iati-activities/", "")
retval = retval.replace("//iati-activity/", "")
retval = retval.replace("//iati-organisations/", "")
retval = retval.replace("//iati-organisation/", "")
retval = retval.replace("//@", "")
retval = retval.replace("/text()", "")
if retval.startswith("@"):
retval = retval[1:]
retval = retval.replace("/@", "@")
retval = retval.replace("-", "_")
retval = retval.replace(":", "_")
retval = retval.replace("/", "@")
retval = retval.replace("@", "_")
return retval
# A function to remove attributes from xpath
def stripAttributePath(xpath):
split_xpath = xpath.split("/")
if split_xpath[-1].startswith("@"):
split_xpath = split_xpath[0:-1]
return "/".join(split_xpath)
# A function to fetch parent xpaths
def parentPath(xpath, generations=1):
split_xpath = xpath.split("/")
split_xpath = split_xpath[0:(-1 * generations)]
return "/".join(split_xpath)
def main(file_path):
# Pull in codelist mapping in XML
mapping_list = list()
mapping_url = "https://raw.githubusercontent.com/IATI/IATI-Codelists/version-{}/mapping.xml".format(VERSION)
mapping_content = requests.get(mapping_url)
mapping_xml = etree.fromstring(mapping_content.text)
mappings = mapping_xml.xpath("/mappings/mapping")
for mapping in mappings:
path = mapping.xpath("./path/text()")[0]
if (
STANDARD == "activity" and (
path.startswith("//iati-activity") or path.startswith("//iati-activities") or path.startswith("//@xml:lang")
)
) or (
STANDARD == "organisation" and (
path.startswith("//iati-organisation") or path.startswith("//iati-organisations") or path.startswith("//@xml:lang")
)
):
codelist = mapping.xpath("./codelist/@ref")[0]
condition_nodes = mapping.xpath("./condition/text()")
if len(condition_nodes) == 0:
mapping_dict = {
"path": path,
"codelist": codelist,
"datastore_name": convertNameToCanonical(path)
}
if path == "//iati-activities/@version":
mapping_dict["datastore_name"] = "dataset_version" # Special case
mapping_list.append(mapping_dict)
else:
condition = condition_nodes[0]
parent_condition_path = stripAttributePath(path)
if condition.startswith(".."):
parent_condition_path = parentPath(parent_condition_path)
conditions = condition.split(" or ")
for condition in conditions:
condition_attribute = re.findall("@\\w+", condition)[0]
condition_value_match = re.findall("= '.+'", condition)
if len(condition_value_match) > 0:
condition_value = condition_value_match[0].replace("'", "").replace("= ", "")
else:
condition_value = ""
condition_path = "{}/{}".format(parent_condition_path, condition_attribute)
mapping_dict = {
"path": path,
"codelist": codelist,
"datastore_name": convertNameToCanonical(path),
"condition_datastore_name": convertNameToCanonical(condition_path),
"condition_value": condition_value
}
mapping_list.append(mapping_dict)
# Find unique codelists and download
cl_list = list()
unique_codelists = list(set([m["codelist"] for m in mapping_list]))
for codelist in unique_codelists:
codelist_url = "https://cdn.iatistandard.org/prod-iati-website/reference_downloads/{}/codelists/downloads/clv3/json/{}/{}.json".format(
VERSION.replace(".",""),
LANGUAGE,
codelist
)
codelist_json = requests.get(codelist_url).json()["data"]
for cl_d in codelist_json:
cl_d["codelist"] = codelist
if "name" not in cl_d.keys():
cl_d["name"] = cl_d["code"]
cl_list.append(cl_d)
# Apply codelists to data
with open(file_path, "r") as sample_data_file:
sample_data = json.load(sample_data_file)["response"]["docs"]
for i in range(0, len(sample_data)):
row = sample_data[i]
field_names = list(row.keys())
for field_name in field_names:
new_field_name = "{}_recode".format(field_name)
value_codes = row[field_name]
if field_name in [mapping["datastore_name"] for mapping in mapping_list]:
mapping_subset = [mapping for mapping in mapping_list if mapping["datastore_name"] == field_name]
if len(mapping_subset) == 1 and "condition_datastore_name" not in mapping_subset[0].keys():
# Unconditional
codelist_subset = [codelist_item for codelist_item in cl_list if codelist_item["codelist"] == mapping_subset[0]["codelist"]]
codelist_subset.append({"code": "", "name": ""})
value_names = list()
value_codes_type = type(value_codes)
if value_codes_type is not list:
value_codes = [value_codes]
for value_code in value_codes:
value_name_comp = [cl_item["name"] for cl_item in codelist_subset if cl_item["code"] == value_code]
if value_name_comp:
value_names.append(value_name_comp[0])
else:
value_names.append(value_code)
print("Warning: {} is not in codelist {}".format(value_code, mapping_subset[0]["codelist"]))
if value_codes_type is list:
sample_data[i][new_field_name] = value_names
else:
sample_data[i][new_field_name] = value_names[0]
else:
# Conditional
conditional_datastore_name = mapping_subset[0]["condition_datastore_name"]
try:
cond_compare_values = row[conditional_datastore_name]
except KeyError:
cond_compare_values = [""] * len(row[field_name])
if type(cond_compare_values) is not list:
cond_compare_values = [cond_compare_values]
original_type = type(row[field_name])
if original_type is not list:
row[field_name] = [row[field_name]]
for j in range(0, len(cond_compare_values)):
cond_compare_value = cond_compare_values[j]
mapping_subset = [mapping for mapping in mapping_subset if mapping["condition_value"] == cond_compare_value]
if len(mapping_subset) > 0: # No codelist for this value if 0
codelist_subset = [codelist_item for codelist_item in cl_list if codelist_item["codelist"] == mapping_subset[0]["codelist"]]
codelist_subset.append({"code": "", "name": ""})
value_code = row[field_name][j]
value_name_comp = [cl_item["name"] for cl_item in codelist_subset if cl_item["code"] == value_code]
if value_name_comp:
value_name = value_name_comp[0]
else:
value_name = value_code
print("Warning: {} is not in codelist {}".format(value_code, mapping_subset[0]["codelist"]))
if original_type is list:
if new_field_name not in sample_data[i].keys():
sample_data[i][new_field_name] = sample_data[i][field_name][:]
sample_data[i][new_field_name][j] = value_name
else:
sample_data[i][new_field_name] = value_name
return sample_data
if __name__ == '__main__':
main("../data/sample.json")