-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtechnoMatcher.py
178 lines (138 loc) · 5.99 KB
/
technoMatcher.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
# -*- coding: utf-8 -*-
"""
technoMatcher
Based on reformat_20191014, technoMatcher22 34 and 36, and bioMatcher
Created on Sun May 3 09:14:29 2020
@author: xicotencatlbm
"""
#%%Import modules
from eiVersions import cd3_1, cd3_2, cd3_3, cd3_4, cd3_5, cd3_6
from pathlib import Path
import pandas as pd
#%% Fetch the customised dictionary with manual correspondences.
sourceFile = 'processDataCMLCA.xlsx'
file = Path.cwd()/'data'/sourceFile
# Create general dictionary with manual correspondences from process data file.
mcd_ge = pd.read_excel(file, sheet_name='manualDict_general',
header=1,
index_col=[0,1]).T.to_dict()
# cd3_1.update(mcd_ge)
# note that the mcd_ge doesn't output geography. Then, how does it get matched?
# maybe geography isn't useful at all. Anyway, the output excel does provide that information, so maybe don't input it but read it at the output?
# There are no codes in the output, though.
#%% Build the customised dictionary with special correspondences
#I'm here
sp = pd.read_excel(file, sheet_name='customDict',
header = 1,
index_col=[0,1,2]).T.to_dict()
#temp df
cf3_3 = 'ecoinvent_correspondence_file_eiv3.2_to_eiv3.3_final.xlsx'
file = Path.cwd()/'data/correspondenceFiles'/cf3_3
from eiVersions import df3_1
df3_1 = df3_1[['activityID', 'activityName', 'geography', 'product name']
].set_index(['activityID', 'product name']).T.to_dict()
df3_3 = pd.read_excel(file, sheet_name='cut-off', header = 2,
usecols = 'O,P,Q,X',
).rename(columns={'activityID.1': 'activityID',
'product name.1': 'product name',
'activityName.1': 'activityName',
'geography.1': 'geography'}
).set_index(['activityID', 'product name']).T.to_dict()
cf3_4 = 'correspondence_file_eiv3.3_to_eiv3.4_20170921_final.xlsx'
file = Path.cwd()/'data/correspondenceFiles'/cf3_4
df3_4 = pd.read_excel(file, sheet_name='cut-off', header = 2,
usecols = 'O,P,Q,X',
).rename(columns={'activityID.1': 'activityID',
'product name.1': 'product name',
'activityName.1': 'activityName',
'geography.1': 'geography'}
).set_index(['activityID', 'product name']).T.to_dict()
cf3_5 = 'correspondence_file_eiv3.4_to_eiv3.5_20181008.xlsx'
file = Path.cwd()/'data/correspondenceFiles'/cf3_5
df3_5 = pd.read_excel(file, sheet_name='cut-off', header = 2,
usecols = 'O,P,Q,X',
).rename(columns={'activityID.1': 'activityID',
'product name.1': 'product name',
'activityName.1': 'activityName',
'geography.1': 'geography'}
).set_index(['activityID', 'product name']).T.to_dict()
# cd3_1 = {}
# cd3_3 = {}
#%% Dictionary of correspondences according to database version.
version = {'2_2': 'The matching values will be deducted from the key.',
'3_1': cd3_1,
'3_2': cd3_2,
'3_3': cd3_3,
'3_4': cd3_4,
'3_5': cd3_5,
'3_6': cd3_6
}
for from_database, code, subid in sp:
if from_database == '3_2':
df = df3_3
if from_database == '2_2':
df = df3_1
if from_database == '3_3':
df = df3_4
if from_database == '3_4':
df = df3_5
entry = sp[from_database, code, subid]
activityID = sp[from_database, code, subid]['activityID']
productName = sp[from_database, code, subid]['product name']
to_database = sp[from_database, code, subid]['database']
activityName = df[activityID, productName]['activityName']
geography = df[activityID, productName]['geography']
complement = {'activityName': activityName,
'geography': geography}
entry.update(complement)
version[to_database].update({(code,subid): entry})
#%% Dictionary of correspondences according to database version.
# version = {'2_2': 'The matching values will be deducted from the key.',
# '3_1': cd3_1,
# '3_2': cd3_2,
# '3_3': cd3_3,
# '3_4': cd3_4,
# '3_5': cd3_5,
# '3_6': cd3_6}
# #%%
def technoMatch(CMLCAstring, unit, target):
if target not in list(version):
print('The target ecoinvent version is invalid.')
return None
subkeys= ['activityID', 'product name']
key = (CMLCAstring, unit)
if target == '2_2':
current = '2_2'
try:
product, geography = tuple(CMLCAstring.strip(']').rsplit('[',1))
match = {'activityName': product,
'geography': geography,
'product name': product,
'unit': unit}
except ValueError:
match = None
else:
current = '3_1'
while current != target:
try:
key = tuple(map(version[current][key].get, subkeys))
except KeyError:
pass
i = list(version).index(current) + 1
current = list(version)[i]
if target != '2_2':
match = version[target].get(key)
return match
#%%
test = [('corrugated board, mixed fibre, single wall, at plant[RER]','kg'),
('transport, lorry >16t, fleet average[RER]','tkm'),
('corrugated board, recycling fibre, double wall, at plant[RER]','kg'),
('chlorine, liquid, production mix, at plant[RER]','kg'),
('transport, lorry 16-32t, EURO3[RER]','tkm')]
shortList = []
for v in test:
try:
a = technoMatch(v[0], v[1], '3_4')['activityID'], technoMatch(v[0], v[1], '3_4')['product name']
shortList.append(a)
except:
pass