-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_monetary.py
308 lines (243 loc) · 12 KB
/
function_monetary.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
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 17 16:56:27 2024
@author: regin
"""
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 14 11:53:04 2024
@author: regin
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#%%
current_pop = 16655799
project_pop = 20610000
Populationgrowth = project_pop / current_pop
unit = "kilotonne"
#%%Input of the HIOT
# data input and modify
hiot_path = "C:/Industrial_ecology/Thesis/IOT_2011_ixi/"
Z = pd.read_csv(f'{hiot_path}Z.txt', sep='\t', index_col=[0, 1], header=[0, 1])
#Z = pd.DataFrame(Z.values, columns = Z.columns, index = Z.columns)
# A = pd.read_csv(f'{path}A.txt', sep='\t', index_col=[0, 1], header=[0, 1])
#Z = Z.droplevel([2,3], axis=1).droplevel([2,3], axis=0)
Y = pd.read_csv(f'{hiot_path}Y.txt' , sep='\t', index_col=[0, 1], header=[0, 1])
#Y = pd.DataFrame(Y.values, columns = Y.columns, index = Z.columns)
#x = Z.sum(axis = 1) + Y.sum(axis = 1)
#Y = Y.droplevel([2,3], axis=1)
Y.NL = Y.NL * Populationgrowth
x = Z.sum(axis = 1) + Y.sum(axis = 1)
#modify the final demand to project to 2050
x_out = x.copy()
x_out[x_out!=0] = 1/x_out[x_out!=0]
inv_diag_x = np.diag(x_out)
A = Z @ inv_diag_x
A = pd.DataFrame(A.values, columns = Z.columns, index = Z.columns)
I = np.eye(A.shape[0])
L = np.linalg.inv(I-A)
Y_modify = Y.copy()
x_pop = L@ Y_modify.sum(1)
#%%
labels = Y.index.get_level_values(1).tolist()
def insert_break_after_40(text):
"""Inserts a line break after the first space encountered beyond the 40th character."""
if len(text) <= 40:
return text
space_index = text.find(' ', 40)
if space_index == -1:
return text # No space found after the 40th character, return as is.
return text[:space_index] + '\n' + text[space_index+1:]
# Modify the labels list
modified_labels = [insert_break_after_40(label) for label in labels]
#%% import extensions and slice the data you want to use
F_sat = pd.read_csv(f'{hiot_path}satellite/F.txt' , sep='\t', index_col=[0], header=[0, 1])
F_imp = pd.read_csv(f'{hiot_path}impacts/F.txt' , sep='\t', index_col=[0], header=[0, 1])
# indicator = "Domestic Extraction Used - Metal Ores - Bauxite and aluminium ores"
# indicator ="Domestic Extraction Used - Metal Ores - Iron ores"
# indicator = "GHG emissions (GWP100) | Problem oriented approach: baseline (CML, 2001) | GWP100 (IPCC, 2007)"
# indicator = "GHG emissions (GWP100) | Problem oriented approach: baseline (CML, 2001) | GWP100 (IPCC, 2007)"
indicator = "Carbon dioxide (CO2) Fuel combustion"
# unit = "kg"
# indicator ="CO2 - combustion - air"
# unit = "kg"
# F_indicator = F_sat.loc[indicator]
# f_indicator = F_indicator @ inv_diag_x
F_indicator = F_imp.loc[indicator]
f_indicator = F_indicator @ inv_diag_x
# F_indicator = pd.DataFrame(f_indicator * x_pop, index = F_sat.columns) # calcualte impact adjusted to pupulation growht
#%%
# file_path = 'C:/Industrial_ecology/Thesis/Circularinterventions/Code/Input_circular_interventions/shocks_full.xlsx'
# sheet_name = 'z' # Replace with the name of your sheet
# Full_shocks_A = pd.read_excel(file_path, sheet_name=sheet_name)
# print(Full_shocks_A)
#%%
def apply_shocks_A_multiple_sequencesreg(file_path, A_matrix, Y_matrix, sequencesA, sequencesY, indicatorimpact, indicatorintensity, threshold,sensitivity):
# Read the Excel file
Full_shocks_A = pd.read_excel(file_path, sheet_name='z')
Full_shocks_Y = pd.read_excel(file_path, sheet_name='Y')
# Dictionary to store diffcheckers for each sequence
diffcheckers = {}
diffcheckerimpact = {}
# Ensure sequenceA and sequenceY are the same length
max_length = max(len(sequencesA), len(sequencesY))
sequencesA.extend([[]] * (max_length - len(sequencesA)))
sequencesY.extend([[]] * (max_length - len(sequencesY)))
# Process sequences together
for seq_index, (seqA, seqY) in enumerate(zip(sequencesA, sequencesY)):
# Create copies of the original DataFrames to modify
A_modify = A_matrix.copy()
Y_modify = Y_matrix.copy()
# Process sequenceA
for seq in seqA:
if seq < len(Full_shocks_A):
row = Full_shocks_A.iloc[seq]
country_row = row['row region']
sector_row = row['row sector']
country_column = row['column region']
sector_column = row['column sector']
value = row['value']
typechange = row["type"]
print(seq)
if typechange == "Percentage":
A_modify.loc[(country_row, sector_row), (country_column, sector_column)] *= ((1 + value)*sensitivity)
else:
A_modify.loc[(country_row, sector_row), (country_column, sector_column)] += (value * sensitivity)
else:
print(f"Index {seq} is out of range for the DataFrame.")
# Process sequenceY
for seq in seqY:
if seq < len(Full_shocks_Y):
row = Full_shocks_Y.iloc[seq]
country_row = row['row region']
sector_row = row['row sector']
country_column = row['column region']
demand_column = row['demand category']
value = row['value']
typechange = row["type"]
print(seq)
if typechange == "Percentage":
Y_modify.loc[(country_row, sector_row), (country_column, demand_column)] *= ((1 + value)*sensitivity)
else:
Y_modify.loc[(country_row, sector_row), (country_column, demand_column)] += (value * sensitivity)
else:
print(f"Index {seq} is out of range for the DataFrame.")
# Groupby to check results
I = np.eye(A_matrix.shape[0])
x_modify = np.linalg.inv(I - A_modify) @ Y_modify.sum(axis=1)
x_baseline = np.linalg.inv(I - A_matrix) @ Y_matrix.sum(axis=1)
diffchecker = pd.DataFrame()
diffchecker["baseline"] = x_baseline
diffchecker["changes"] = x_modify
diffchecker["diff"] = diffchecker["changes"] - diffchecker["baseline"]
diffcheckerdif = diffchecker["diff"]
# Store the diffchecker in the dictionary with the sequence index as the key
diffcheckers[f'intervention_A_{seq_index}_Y_{seq_index}'] = diffcheckerdif
diffcheckerfull = pd.DataFrame.from_dict(diffcheckers)
L_ct = np.linalg.inv(I - A_modify.values)
x_ct = L_ct @ Y_modify.values.sum(axis=1)
RE_ct = indicatorintensity * x_ct
F_diff_RE = (RE_ct - indicatorimpact)
diffcheckerimpact[f'intervention_A_{seq_index}_Y_{seq_index}'] = F_diff_RE
#diffcheckerimpact = pd.DataFrame.from_dict(diffcheckerimpact)
F_diff_RE = pd.DataFrame(F_diff_RE, index=A_matrix.index)
F_relative_change1 = F_diff_RE# /1000000 # uncomment this when working with co2 as it is given in kg
# Filter the DataFrame to include only values above the threshold
filtered_df = F_relative_change1[np.abs(F_relative_change1) > threshold].dropna()
# Calculate the sum of values below the threshold
below_threshold_sum = F_relative_change1[np.abs(F_relative_change1) <= threshold].sum().sum()
# Add the below-threshold sum as a new row
filtered_df.loc[('Below Threshold', 'Sum of below threshold'), :] = below_threshold_sum
# Choose a color palette (using Set1)
colors = plt.get_cmap('Set1').colors
plt.rcParams.update({'font.size': 18})
# Plot the filtered DataFrame with adjusted size and legend placement
ax = filtered_df.unstack().plot(kind="bar", stacked=True, legend=False, figsize=(10, 6), color=colors)
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5), fontsize=12)
ax.grid(True)
ax.set_title(f'Filtered differences for scenario {seq_index}\n in {indicator} (threshold = {threshold})', fontsize=12)
ax.set_ylabel(f"{indicator} ({unit})", fontsize=11)
ax.set_xlabel('Regions')
ax.set_xticklabels(ax.get_xticklabels(), rotation=45, ha='right', fontsize=12)
# Show the plot
plt.show()
return diffcheckerfull,diffcheckerimpact
#%%
# Example usage
file_path = 'C:/Industrial_ecology/Thesis/Circularinterventions/Code/Input_circular_interventions/shocks_full.xlsx'
sequencesA=[
list(range(0, 32)), # Sequence 1
list(range(32, 53)),
list(range(0, 53))# Sequence 2
]
sequencesY =[
list(range(0, 2)), # Sequence 1
list(range(2,3)),
list(range(0, 3))# Sequence 2
]
indicatorimpact = F_indicator.values
indicatorintensity = f_indicator
threshold = 30000
sensitivity = 1
A1 = A.copy() # Replace this with your actual DataFrame
diffcheckers,diffcheckerimpact = apply_shocks_A_multiple_sequencesreg(file_path, A, Y, sequencesA, sequencesY, indicatorimpact, indicatorintensity ,threshold,sensitivity)
sensitivity = 1.05
diffcheckersmin, diffcheckerminimpact = apply_shocks_A_multiple_sequencesreg(file_path, A, Y, sequencesA, sequencesY, indicatorimpact, indicatorintensity ,threshold,sensitivity)
sensitivity = 1.02
diffcheckersplus,diffcheckerplusimpact = apply_shocks_A_multiple_sequencesreg(file_path, A, Y, sequencesA, sequencesY, indicatorimpact, indicatorintensity ,threshold,sensitivity)
Full_shocks_A = pd.read_excel(file_path, sheet_name='z')
#%%
diffcheckerimpact = pd.DataFrame.from_dict(diffcheckerimpact)
diffcheckerimpact.index = A.index
diffcheckerminimpact = pd.DataFrame.from_dict(diffcheckerminimpact)
diffcheckerminimpact.index = A.index
diffcheckerplusimpact = pd.DataFrame.from_dict(diffcheckerplusimpact)
diffcheckerplusimpact.index = A.index
differencesensitivity= diffcheckerminimpact-diffcheckerimpact
differencesensitivity.plot()
differencesensitivity1= diffcheckerplusimpact-diffcheckerimpact
difplot = pd.DataFrame()
difplot["increase_5%"] = differencesensitivity.intervention_A_2_Y_2.values
difplot["increase_2%"] = differencesensitivity1.intervention_A_2_Y_2.values
#%%
modified_labels = [insert_break_after_40(label) for label in labels]
regions_labels = Y.index.get_level_values(0).tolist()
index2 = pd.MultiIndex.from_arrays([regions_labels, modified_labels], names=['regions', 'sectors'])
threshold = 0
unit = "kt"
F_diff_RE = difplot.values#differencesensitivity.intervention_A_2_Y_2.values
F_diff_RE = pd.DataFrame(F_diff_RE, index2)# A.index)
F_relative_change1 = F_diff_RE
# Filter the DataFrame to include only values above the threshold
filtered_df = F_relative_change1[np.abs(F_relative_change1) > threshold].dropna()
# Calculate the sum of values below the threshold
below_threshold_sum = F_relative_change1[np.abs(F_relative_change1) <= threshold].sum().sum()
# Add the below-threshold sum as a new row
filtered_df.loc[('Below Threshold', 'Sum of below threshold'), :] = below_threshold_sum
# Choose a color palette (using Set1)
colors = plt.get_cmap('Set1').colors
plt.rcParams.update({'font.size': 18})
# Plot the filtered DataFrame with adjusted size and legend placement
ax = filtered_df.unstack().plot(kind="bar", stacked=True, legend=False, figsize=(10, 6), color=colors)
#ax.legend(loc='center left', bbox_to_anchor=(1, 0.5), fontsize=12)
ax.grid(True)
ax.set_title(f'Filtered differences sensitivity\n {indicator} (threshold = {threshold})', fontsize=12)
ax.set_ylabel(f"{indicator}\n ({unit})", fontsize=11)
ax.set_xlabel('Regions')
ax.set_xticklabels(ax.get_xticklabels(), rotation=45, ha='right', fontsize=12)
# Show the plot
plt.show()
# %% export data
# labelresource = indicator[40:]
# labelresource = labelresource.replace(" ", "_")
# outputpath = "C:/Industrial_ecology/Thesis/Circularinterventions/Code/Input_circular_interventions/output_visuals/"
# diffcheckerimpact.to_csv(f'{outputpath}{labelresource}_mon_impact.csv', index=True)
# difplot.to_csv(f'{outputpath}{labelresource}_mon_sens.csv', index=True)
#%%
# diffcheckerimpact = diffcheckerimpact
# difplot = difplot
# labelresource = "CO2"
# outputpath = "C:/Industrial_ecology/Thesis/Circularinterventions/Code/Input_circular_interventions/output_visuals/"
# diffcheckerimpact.to_csv(f'{outputpath}{labelresource}_mon_impact.csv', index=True)
# difplot.to_csv(f'{outputpath}{labelresource}_mon_sens.csv', index=True)