-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcirk_func.py
198 lines (154 loc) · 7.8 KB
/
cirk_func.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
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 26 17:30:27 2024
@author: regin
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#%%
# =============================================================================
# Functions
# =============================================================================
def apply_shocks(file_path, A_matrix, Y_matrix, sequencesA, sequencesY, indicatorimpact, indicatorintensity, threshold,sensitivity,indicator):
"""
Modify the A and Y matrix using an excel file with input parameters
Creates a dataframe (same shape as the original dataframe) containing the modified values for both the
A and Y matrix based on the input parameters.
does also include system to perform different parameters
Parameters:
File_path (string):
String containing the path to the excel file which includes the shocks
A_matrix (Dataframe_like)
Baseline A matrix.
Y_matrix (Dataframe_like)
Baseline Y matrix.
sequencesA (Lists)
List of lists containing the sequence of interventions implemented for each scenario
sequencesY (Lists)
List of lists containing the sequence of interventions implemented for each scenario
indicatorimpact (Dataframe_like)
Dataframe containing the environmental impact
indicatorintensity (Dataframe_like)
Dataframe environmental intensity
Threshold (float)
Float of the passable threshold for visualization
Sensitivity (float)
Extra input for changing the input of each intervention (Sensitivity = 1 when no sensitivity is applied)
indicator (string)
Enviromental impact of interest.
Returns: df_difference_output (Dictionary)
Difference of gross output
Returns: df_difference_impact (Dictionary)
Difference of environmental impact of interest
Raises:
IOError: An error occurred about the size of the model.
"""
# 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 results for each sequence of interventions
df_difference_output = {}
df_difference_impact = {}
# 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 sequence A and the interventions for the A matrix
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 sequence Y and the interventions for the Y matrix
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)
Results_output = pd.DataFrame()
Results_output["baseline"] = x_baseline
Results_output["changes"] = x_modify
Results_output["diff"] = Results_output["changes"] - Results_output["baseline"]
Difference_output = Results_output["diff"]
# Store the Results_output in the dictionary with the sequence index as the key
df_difference_output[f'intervention{seq_index}'] = Difference_output
df_difference_output = pd.DataFrame.from_dict(df_difference_output)
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)
df_difference_impact[f'intervention{seq_index}'] = F_diff_RE
F_diff_RE = pd.DataFrame(F_diff_RE, index=A_matrix.index)
F_relative_change1 = F_diff_RE/1000
# 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))
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} in kt", fontsize=12)
ax.set_xlabel('Regions')
ax.set_xticklabels(ax.get_xticklabels(), rotation=45, ha='right', fontsize=12)
# Show the plot
plt.show()
return df_difference_output, df_difference_impact
def insert_break_string(text, threshold):
"""
Add a line break for labels when longer than threshold. usefull when the labels are too long in graphs
Possible line of code, modified_labels = [insert_break_string(label) for label in labels]
Parameters:
text (Dataframe_like)
Dataframe containing results of the simulated data.
threshold (integer)
number of letters before break is applied
Returns: List
A list containing all labels with line break
Raises:
IOError: An error occurred accessing the bigtable.Table object.
"""
if len(text) <= threshold:
return text
space_index = text.find(' ', threshold)
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:]