-
Notifications
You must be signed in to change notification settings - Fork 0
/
ML_mapping.py
444 lines (405 loc) · 20.2 KB
/
ML_mapping.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
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
"""
This Python module uses the code from https://github.com/UKPLab/sentence-transformers to match lists of words to
existing classifications in the world of Industrial Ecology.
author: maxime.agez@polymtl.ca
"""
import pandas as pd
import json
import pkg_resources
from sentence_transformers import (SentenceTransformer, util)
class Mapping:
def __init__(self, reference_classification, transformer_model, number_of_guesses):
"""
:param reference_classification: [string] The reference classification that is used for matching.
Available choices:
- openIO-Canada
- exiobase
- USEEIO 2.0
- GTAP 10
- IOCC
- NACE Rev.1.1
- NACE Rev.2
- CPA 2008
- CPA 2.1
- NAPCS 2017
- NAPCS 2022
- NAICS 2017
- NAICS 2022
- ISIC Rev.4
- CPC 2.1
- COICOP 2018
- ecoinvent 3.8 technosphere
- ecoinvent 3.9 technosphere
- ecoinvent 3.8 elementary flows
- ecoinvent 3.9 elementary flows
- IMPACT World+ 2.0
- USEtox 2
- EF 3.0
- EF 3.1
:param transformer_model: [string] The name of the machine learning model to be used for matching. THe different
available models are described here: https://www.sbert.net/docs/pretrained_models.html
:param number_of_guesses: [integer] The amount of suggestions made by the ML algorithm that will be displayed.
"""
self.reference_classification = reference_classification
self.model = SentenceTransformer(transformer_model)
self.number_of_guesses = number_of_guesses
# define attributes
self.mapping = None
self.sorted_scores = None
self.indices = None
self.inputs = None
self.input_embeddings = None
self.reference_embeddings = None
self.iocc_sectors = None
self.exio_sectors = None
self.useeio_sectors = None
self.gtap_sectors = None
self.nace_1_1_sectors = None
self.nace_2_sectors = None
self.cpa_2008_sectors = None
self.cpa_2_1_sectors = None
self.napcs_2017_sectors = None
self.napcs_2022_sectors = None
self.naics_2017_sectors = None
self.naics_2022_sectors = None
self.isic_4_sectors = None
self.cpc_2_1_sectors = None
self.coicop_sectors = None
self.ecoinvent_3_8_technosphere = None
self.ecoinvent_3_9_technosphere = None
self.ecoinvent_3_8_flows = None
self.ecoinvent_3_9_flows = None
self.iw_flows = None
self.usetox_flows = None
self.ef_3_0_flows = None
self.ef_3_1_flows = None
if self.reference_classification in ['IOCC', 'openIO-Canada']:
self.match_to_iocc()
elif self.reference_classification in ['NACE Rev.1.1']:
self.match_to_nace_1_1()
elif self.reference_classification in ['NACE Rev.2']:
self.match_to_nace_2()
elif self.reference_classification in ['CPA 2008']:
self.match_to_cpa_2008()
elif self.reference_classification in ['CPA 2.1']:
self.match_to_cpa_2_1()
elif self.reference_classification in ['exiobase']:
self.match_to_exio()
elif self.reference_classification in ['USEEIO 2.0']:
self.match_to_useeio()
elif self.reference_classification in ['GTAP 10']:
self.match_to_gtap()
elif self.reference_classification in ['NAPCS 2017']:
self.match_to_napcs_2017()
elif self.reference_classification in ['NAPCS 2022']:
self.match_to_napcs_2022()
elif self.reference_classification in ['NAICS 2017']:
self.match_to_naics_2017()
elif self.reference_classification in ['NAICS 2022']:
self.match_to_naics_2022()
elif self.reference_classification in ['ISIC Rev.4']:
self.match_to_isic_4()
elif self.reference_classification in ['CPC 2.1']:
self.match_to_cpc_2_1()
elif self.reference_classification in ['COICOP 2018']:
self.match_to_coicop()
elif self.reference_classification in ['ecoinvent 3.8 technosphere']:
self.match_to_ei38_techno()
elif self.reference_classification in ['ecoinvent 3.9 technosphere']:
self.match_to_ei39_techno()
elif self.reference_classification in ['ecoinvent 3.8 elementary flows']:
self.match_to_ei38_flows()
elif self.reference_classification in ['ecoinvent 3.9 elementary flows']:
self.match_to_ei39_flows()
elif self.reference_classification in ['IMPACT World+ 2.0']:
self.match_to_iw()
elif self.reference_classification in ['USEtox 2']:
self.match_to_usetox()
elif self.reference_classification in ['EF 3.0']:
self.match_to_ef_3_0()
elif self.reference_classification in ['EF 3.1']:
self.match_to_ef_3_1()
def match_inputs(self, inputs):
"""
Loads the list of inputs to the machine learning model.
:param inputs: a [list] of words to-be-matched with the reference classification
:return:
"""
self.inputs = inputs
self.input_embeddings = self.model.encode(self.inputs)
def match_to_iocc(self):
"""
Method maps list of inputs to the Detailed level of the IOCC classification, notably used in the Input-Output
database OpenIO-Canada.
:return:
"""
with open(pkg_resources.resource_filename(__name__, '/Data/IOCC_sectors.json'), 'r') as f:
self.iocc_sectors = json.load(f)
self.reference_embeddings = self.model.encode(self.iocc_sectors)
def match_to_nace_1_1(self):
"""
Method maps list of inputs to the NACE Rev. 1.1 classification.
:return:
"""
with open(pkg_resources.resource_filename(__name__, '/Data/NACE_1_1_sectors.json'), 'r') as f:
self.nace_1_1_sectors = json.load(f)
self.reference_embeddings = self.model.encode([i[1] for i in self.nace_1_1_sectors])
def match_to_nace_2(self):
"""
Method maps list of inputs to the NACE Rev. 2 classification.
:return:
"""
with open(pkg_resources.resource_filename(__name__, '/Data/NACE_2_sectors.json'), 'r') as f:
self.nace_2_sectors = json.load(f)
self.reference_embeddings = self.model.encode([i[1] for i in self.nace_2_sectors])
def match_to_cpa_2008(self):
"""
Method maps list of inputs to the CPA 2008 classification.
:return:
"""
with open(pkg_resources.resource_filename(__name__, '/Data/CPA_2008_sectors.json'), 'r') as f:
self.cpa_2008_sectors = json.load(f)
self.reference_embeddings = self.model.encode([i[1] for i in self.cpa_2008_sectors])
def match_to_cpa_2_1(self):
"""
Method maps list of inputs to the CPA 2.1 classification.
:return:
"""
with open(pkg_resources.resource_filename(__name__, '/Data/CPA_2_1_sectors.json'), 'r') as f:
self.cpa_2_1_sectors = json.load(f)
self.reference_embeddings = self.model.encode([i[1] for i in self.cpa_2_1_sectors])
def match_to_exio(self):
"""
Method maps list of inputs to the exiobase classification, the latter is inspired by NACE.
:return:
"""
with open(pkg_resources.resource_filename(__name__, '/Data/exiobase_sectors.json'), 'r') as f:
self.exio_sectors = json.load(f)
self.reference_embeddings = self.model.encode(self.exio_sectors)
def match_to_useeio(self):
"""
Method maps list of inputs to the USEEIO 2.0 classification.
:return:
"""
with open(pkg_resources.resource_filename(__name__, '/Data/USEEIO_sectors.json'), 'r') as f:
self.useeio_sectors = json.load(f)
self.reference_embeddings = self.model.encode(self.useeio_sectors)
def match_to_gtap(self):
"""
Method maps list of inputs to the classification of the GTAP database.
:return:
"""
with open(pkg_resources.resource_filename(__name__, '/Data/GTAP_sectors.json'), 'r') as f:
self.gtap_sectors = json.load(f)
self.reference_embeddings = self.model.encode(self.gtap_sectors)
def match_to_napcs_2017(self):
"""
Method maps list of inputs to the NAPCS 2017 classification.
:return:
"""
with open(pkg_resources.resource_filename(__name__, '/Data/NAPCS_2017_sectors.json'), 'r') as f:
self.napcs_2017_sectors = json.load(f)
self.reference_embeddings = self.model.encode([i[1] for i in self.napcs_2017_sectors])
def match_to_napcs_2022(self):
"""
Method maps list of inputs to the NAPCS 2022 classification.
:return:
"""
with open(pkg_resources.resource_filename(__name__, '/Data/NAPCS_2022_sectors.json'), 'r') as f:
self.napcs_2022_sectors = json.load(f)
self.reference_embeddings = self.model.encode([i[1] for i in self.napcs_2022_sectors])
def match_to_naics_2017(self):
"""
Method maps list of inputs to the NAICS 2017 classification.
:return:
"""
with open(pkg_resources.resource_filename(__name__, '/Data/NAICS_2017_sectors.json'), 'r') as f:
self.naics_2017_sectors = json.load(f)
self.reference_embeddings = self.model.encode([i[1] for i in self.naics_2017_sectors])
def match_to_naics_2022(self):
"""
Method maps list of inputs to the NAICS 2022 classification.
:return:
"""
with open(pkg_resources.resource_filename(__name__, '/Data/NAICS_2022_sectors.json'), 'r') as f:
self.naics_2022_sectors = json.load(f)
self.reference_embeddings = self.model.encode([i[1] for i in self.naics_2022_sectors])
def match_to_isic_4(self):
"""
Method maps list of inputs to the ISIC Rev.4 classification.
:return:
"""
with open(pkg_resources.resource_filename(__name__, '/Data/ISIC_4_sectors.json'), 'r') as f:
self.isic_4_sectors = json.load(f)
self.reference_embeddings = self.model.encode([i[1] for i in self.isic_4_sectors])
def match_to_cpc_2_1(self):
"""
Method maps list of inputs to the CPC 2.1 classification.
:return:
"""
with open(pkg_resources.resource_filename(__name__, '/Data/CPC_2_1_sectors.json'), 'r') as f:
self.cpc_2_1_sectors = json.load(f)
self.reference_embeddings = self.model.encode([i[1] for i in self.cpc_2_1_sectors])
def match_to_coicop(self):
"""
Method maps list of inputs to the COICOP classification.
:return:
"""
with open(pkg_resources.resource_filename(__name__, '/Data/COICOP_2018_sectors.json'), 'r') as f:
self.coicop_sectors = json.load(f)
self.reference_embeddings = self.model.encode([i[1] for i in self.coicop_sectors])
def match_to_iw(self):
"""
Method maps list of inputs to the classification used by the IMPACT World+ LCIA methodology.
:return:
"""
with open(pkg_resources.resource_filename(__name__, '/Data/IW_2.0_flows.json'), 'r') as f:
self.iw_flows = json.load(f)
self.reference_embeddings = self.model.encode(self.iw_flows)
def match_to_ei38_techno(self):
"""
Method maps list of inputs to the product classification used by the ecoinvent 3.8 database.
:return:
"""
with open(pkg_resources.resource_filename(__name__, '/Data/ecoinvent_3_8_sectors.json'), 'r') as f:
self.ecoinvent_3_8_technosphere = json.load(f)
self.reference_embeddings = self.model.encode(self.ecoinvent_3_8_technosphere)
def match_to_ei39_techno(self):
"""
Method maps list of inputs to the product classification used by the ecoinvent 3.9 database.
:return:
"""
with open(pkg_resources.resource_filename(__name__, '/Data/ecoinvent_3_9_sectors.json'), 'r') as f:
self.ecoinvent_3_9_technosphere = json.load(f)
self.reference_embeddings = self.model.encode(self.ecoinvent_3_9_technosphere)
def match_to_ei38_flows(self):
"""
Method maps list of inputs to the elementary flow classification used by the ecoinvent 3.8 database.
:return:
"""
with open(pkg_resources.resource_filename(__name__, '/Data/ecoinvent_3_8_flows.json'), 'r') as f:
self.ecoinvent_3_8_flows = json.load(f)
self.reference_embeddings = self.model.encode(self.ecoinvent_3_8_flows)
def match_to_ei39_flows(self):
"""
Method maps list of inputs to the elementary flow classification used by the ecoinvent 3.9 database.
:return:
"""
with open(pkg_resources.resource_filename(__name__, '/Data/ecoinvent_3_9_flows.json'), 'r') as f:
self.ecoinvent_3_9_flows = json.load(f)
self.reference_embeddings = self.model.encode(self.ecoinvent_3_9_flows)
def match_to_usetox(self):
"""
Method maps list of inputs to the classification used by the USEtox database.
:return:
"""
with open(pkg_resources.resource_filename(__name__, '/Data/USEtox_flows.json'), 'r') as f:
self.usetox_flows = json.load(f)
self.reference_embeddings = self.model.encode(self.usetox_flows)
def match_to_ef_3_0(self):
"""
Method maps list of inputs to the classification used by the EF 3.0 LCIA method.
:return:
"""
with open(pkg_resources.resource_filename(__name__, '/Data/EF_3_0_flows.json'), 'r') as f:
self.ef_3_0_flows = json.load(f)
self.reference_embeddings = self.model.encode(self.ef_3_0_flows)
def match_to_ef_3_1(self):
"""
Method maps list of inputs to the classification used by the EF 3.1 LCIA method.
:return:
"""
with open(pkg_resources.resource_filename(__name__, '/Data/EF_3_1_flows.json'), 'r') as f:
self.ef_3_1_flows = json.load(f)
self.reference_embeddings = self.model.encode(self.ef_3_1_flows)
def calculate_scores(self):
"""
Calculates similarity scores.
:return: a sorted list of the similarity scores and the associated indices
"""
scores = util.pytorch_cos_sim(self.input_embeddings, self.reference_embeddings)
self.sorted_scores, self.indices = scores.sort(dim=1, descending=True)
def format_results(self):
"""
Formats the results in a dataframe.
:return: self.mapping, the final mapping that the user is after
"""
if self.reference_classification in ['IOCC', 'openIO-Canada']:
reference_list = self.iocc_sectors
elif self.reference_classification in ['NACE Rev.1.1']:
reference_list = self.nace_1_1_sectors
elif self.reference_classification in ['NACE Rev.2']:
reference_list = self.nace_2_sectors
elif self.reference_classification in ['CPA 2008']:
reference_list = self.cpa_2008_sectors
elif self.reference_classification in ['CPA 2.1']:
reference_list = self.cpa_2_1_sectors
elif self.reference_classification in ['exiobase']:
reference_list = self.exio_sectors
elif self.reference_classification in ['USEEIO 2.0']:
reference_list = self.useeio_sectors
elif self.reference_classification in ['GTAP 10']:
reference_list = self.gtap_sectors
elif self.reference_classification in ['NAPCS 2017']:
reference_list = self.napcs_2017_sectors
elif self.reference_classification in ['NAPCS 2022']:
reference_list = self.napcs_2022_sectors
elif self.reference_classification in ['NAICS 2017']:
reference_list = self.naics_2017_sectors
elif self.reference_classification in ['NAICS 2022']:
reference_list = self.naics_2022_sectors
elif self.reference_classification in ['ISIC Rev.4']:
reference_list = self.isic_4_sectors
elif self.reference_classification in ['CPC 2.1']:
reference_list = self.cpc_2_1_sectors
elif self.reference_classification in ['COICOP 2018']:
reference_list = self.coicop_sectors
elif self.reference_classification in ['ecoinvent 3.8 technosphere']:
reference_list = self.ecoinvent_3_8_technosphere
elif self.reference_classification in ['ecoinvent 3.9 technosphere']:
reference_list = self.ecoinvent_3_9_technosphere
elif self.reference_classification in ['ecoinvent 3.8 elementary flows']:
reference_list = self.ecoinvent_3_8_flows
elif self.reference_classification in ['ecoinvent 3.9 elementary flows']:
reference_list = self.ecoinvent_3_9_flows
elif self.reference_classification in ['IMPACT World+ 2.0']:
reference_list = self.iw_flows
elif self.reference_classification in ['USEtox 2']:
reference_list = self.usetox_flows
elif self.reference_classification in ['EF 3.0']:
reference_list = self.ef_3_0_flows
elif self.reference_classification in ['EF 3.1']:
reference_list = self.ef_3_1_flows
if self.reference_classification in ['IOCC', 'openIO-Canada', 'exiobase', 'USEEIO 2.0', 'GTAP 10',
'ecoinvent 3.8 elementary flows', 'ecoinvent 3.9 elementary flows',
'ecoinvent 3.8 technosphere', 'ecoinvent 3.9 technosphere',
'IMPACT World+ 2.0', 'USEtox 2', 'EF 3.0', 'EF 3.1']:
self.mapping = pd.DataFrame(None, ['order', 'sector', 'similarity'])
for i, product in enumerate(self.inputs):
for j in range(0, self.number_of_guesses):
self.mapping = pd.concat([self.mapping,
pd.DataFrame([product,
j + 1,
reference_list[self.indices[i][j].cpu().numpy()],
self.sorted_scores[i][j].cpu().numpy().tolist()],
['product', 'order', 'sector', 'similarity'])],
axis=1)
self.mapping = self.mapping.T.set_index(['product', 'order'])
return self.mapping
elif self.reference_classification in ['NACE Rev.1.1', 'NACE Rev.2', 'CPA 2008', 'CPA 2.1', 'NAPCS 2017',
'NAPCS 2022', 'NAICS 2017', 'NAICS 2022', 'ISIC Rev.4', 'CPC 2.1',
'COICOP 2018']:
self.mapping = pd.DataFrame(None, ['order', 'sector', 'similarity'])
for i, product in enumerate(self.inputs):
for j in range(0, self.number_of_guesses):
self.mapping = pd.concat([self.mapping,
pd.DataFrame([product,
j + 1,
reference_list[self.indices[i][j].cpu().numpy()][0],
reference_list[self.indices[i][j].cpu().numpy()][1],
self.sorted_scores[i][j].cpu().numpy().tolist()],
['product', 'order', 'code sector', 'sector', 'similarity'])],
axis=1)
self.mapping = self.mapping.T.set_index(['product', 'order'])
self.mapping = self.mapping.T.reindex(['code sector', 'sector', 'similarity']).T
return self.mapping