Skip to content

Commit e3ef5e8

Browse files
authored
Merge pull request #89 from jdebacker/warnings
PR to reduce warnings/notifications
2 parents b620d43 + a56e3e1 commit e3ef5e8

9 files changed

+109
-67
lines changed

examples/run_og_zaf.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import json
66
import time
77
import copy
8-
import importlib.resources
8+
from importlib.resources import files
99
import matplotlib.pyplot as plt
1010
from ogzaf.calibrate import Calibration
1111
from ogcore.parameters import Specifications
@@ -44,9 +44,11 @@ def main():
4444
output_base=base_dir,
4545
)
4646
# Update parameters for baseline from default json file
47-
with importlib.resources.open_text(
48-
"ogzaf", "ogzaf_default_parameters.json"
49-
) as file:
47+
with (
48+
files("ogzaf")
49+
.joinpath("ogzaf_default_parameters.json")
50+
.open("r") as file
51+
):
5052
defaults = json.load(file)
5153
p.update_specifications(defaults)
5254
# Update parameters from calibrate.py Calibration class

examples/run_og_zaf_education.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import time
88
import copy
99
import numpy as np
10+
from importlib.resources import files
1011
import matplotlib.pyplot as plt
1112
from ogzaf.calibrate import Calibration
1213
from ogcore.parameters import Specifications
@@ -47,15 +48,13 @@ def main():
4748
output_base=base_dir,
4849
)
4950
# Update parameters for baseline from default json file
50-
p.update_specifications(
51-
json.load(
52-
open(
53-
os.path.join(
54-
CUR_DIR, "..", "ogzaf", "ogzaf_default_parameters.json"
55-
)
56-
)
57-
)
58-
)
51+
with (
52+
files("ogzaf")
53+
.joinpath("ogzaf_default_parameters.json")
54+
.open("r") as file
55+
):
56+
defaults = json.load(file)
57+
p.update_specifications(defaults)
5958
# move closure rule out to 50 years since educaation phases in over 20 years
6059
p.tG1 = 50
6160

examples/run_og_zaf_multiple_industry.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import json
77
import time
88
import copy
9-
import importlib.resources
9+
from importlib.resources import files
1010
import matplotlib.pyplot as plt
1111
from ogcore.parameters import Specifications
1212
from ogcore import output_tables as ot
@@ -43,9 +43,11 @@ def main():
4343
output_base=base_dir,
4444
)
4545
# Update parameters for baseline from default json file
46-
with importlib.resources.open_text(
47-
"ogzaf", "ogzaf_default_parameters_multisector.json"
48-
) as file:
46+
with (
47+
files("ogzaf")
48+
.joinpath("ogzaf_default_parameters.json")
49+
.open("r") as file
50+
):
4951
defaults = json.load(file)
5052
p.update_specifications(defaults)
5153

examples/run_og_zaf_multiple_industry_cit_cut.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# function call
33
import multiprocessing
44
from distributed import Client
5-
import importlib.resources
5+
from importlib.resources import files
66
import os
77
import json
88
import time
@@ -42,9 +42,11 @@ def main():
4242
output_base=base_dir,
4343
)
4444
# Update parameters for baseline from default json file
45-
with importlib.resources.open_text(
46-
"ogzaf", "ogzaf_default_parameters.json"
47-
) as file:
45+
with (
46+
files("ogzaf")
47+
.joinpath("ogzaf_default_parameters.json")
48+
.open("r") as file
49+
):
4850
defaults = json.load(file)
4951
p.update_specifications(defaults)
5052
# Update parameters from calibrate.py Calibration class for multiple industries

examples/run_og_zaf_multiple_industry_energy_tax.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# function call
33
import multiprocessing
44
from distributed import Client
5-
import importlib.resources
5+
from importlib.resources import files
66
import os
77
import json
88
import time
@@ -46,11 +46,14 @@ def main():
4646
output_base=base_dir,
4747
)
4848
# Update parameters for baseline from default json file
49-
with importlib.resources.open_text(
50-
"ogzaf", "ogzaf_default_parameters.json"
51-
) as file:
49+
with (
50+
files("ogzaf")
51+
.joinpath("ogzaf_default_parameters.json")
52+
.open("r") as file
53+
):
5254
defaults = json.load(file)
5355
p.update_specifications(defaults)
56+
p.update_specifications(defaults)
5457
# Update parameters from calibrate.py Calibration class for multiple industries
5558
p.M = 4
5659
p.I = 5

ogzaf/input_output.py

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,43 @@
1111
SAM_path = "https://www.wider.unu.edu/sites/default/files/Data/SASAM-2015-Data-Resource.xlsx"
1212
SAM_path_alt = "https://raw.githubusercontent.com/EAPD-DRB/SAM-files/main/Data/ZAF/SASAM-2015-Data-Resource.xlsx"
1313

14-
if is_connected():
15-
try:
16-
SAM = pd.read_excel(
17-
SAM_path,
18-
sheet_name="Micro SAM 2015",
19-
skiprows=6,
20-
index_col=0,
21-
storage_options=storage_options,
22-
)
23-
print("Successfully read SAM from WIDER.")
24-
except Exception as e:
25-
print(f"Failed to read from WIDER: {e}")
14+
15+
def read_SAM():
16+
if is_connected():
2617
try:
27-
# Attempt to read from the GitHub repository
2818
SAM = pd.read_excel(
29-
SAM_path_alt,
19+
SAM_path,
3020
sheet_name="Micro SAM 2015",
3121
skiprows=6,
3222
index_col=0,
3323
storage_options=storage_options,
3424
)
35-
print("Successfully read SAM from GitHub repository.")
25+
print("Successfully read SAM from WIDER.")
3626
except Exception as e:
37-
print(f"Failed to read from the GitHub repository: {e}")
38-
SAM = None
39-
# If both attempts fail, SAM will be None
40-
if SAM is None:
41-
print("Failed to read SAM from both sources.")
42-
else:
43-
SAM = None
44-
print("No internet connection. SAM cannot be read.")
27+
print(f"Failed to read from WIDER: {e}")
28+
try:
29+
# Attempt to read from the GitHub repository
30+
SAM = pd.read_excel(
31+
SAM_path_alt,
32+
sheet_name="Micro SAM 2015",
33+
skiprows=6,
34+
index_col=0,
35+
storage_options=storage_options,
36+
)
37+
print("Successfully read SAM from GitHub repository.")
38+
except Exception as e:
39+
print(f"Failed to read from the GitHub repository: {e}")
40+
SAM = None
41+
# If both attempts fail, SAM will be None
42+
if SAM is None:
43+
print("Failed to read SAM from both sources.")
44+
else: # pragma: no cover
45+
SAM = None
46+
print("No internet connection. SAM cannot be read.")
47+
return SAM
4548

4649

47-
def get_alpha_c(sam=SAM, cons_dict=CONS_DICT):
50+
def get_alpha_c(sam=None, cons_dict=CONS_DICT):
4851
"""
4952
Calibrate the alpha_c vector, showing the shares of household
5053
expenditures for each consumption category
@@ -56,6 +59,8 @@ def get_alpha_c(sam=SAM, cons_dict=CONS_DICT):
5659
Returns:
5760
alpha_c (dict): Dictionary of shares of household expenditures
5861
"""
62+
if sam is None:
63+
sam = read_SAM()
5964
alpha_c = {}
6065
overall_sum = 0
6166
for key, value in cons_dict.items():
@@ -72,7 +77,7 @@ def get_alpha_c(sam=SAM, cons_dict=CONS_DICT):
7277
return alpha_c
7378

7479

75-
def get_io_matrix(sam=SAM, cons_dict=CONS_DICT, prod_dict=PROD_DICT):
80+
def get_io_matrix(sam=None, cons_dict=CONS_DICT, prod_dict=PROD_DICT):
7681
"""
7782
Calibrate the io_matrix array. This array relates the share of each
7883
production category in each consumption category
@@ -85,6 +90,8 @@ def get_io_matrix(sam=SAM, cons_dict=CONS_DICT, prod_dict=PROD_DICT):
8590
Returns:
8691
io_df (pd.DataFrame): Dataframe of io_matrix
8792
"""
93+
if sam is None:
94+
sam = read_SAM()
8895
# Create initial matrix as dataframe of 0's to fill in
8996
io_dict = {}
9097
for key in prod_dict.keys():

setup.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@
1818
"Issue Tracker": "https://github.com/EAPD-DRB/OG-ZAF/issues",
1919
},
2020
packages=["ogzaf"],
21-
package_data={"ogzaf": ["ogzaf_default_parameters.json", "data/*"]},
21+
package_data={
22+
"ogzaf": [
23+
"ogzaf_default_parameters.json",
24+
"ogzaf_default_parameters_multisector.json",
25+
"data/*",
26+
]
27+
},
2228
include_packages=True,
2329
python_requires=">=3.7.7, <3.13",
2430
install_requires=[

tests/test_input_output.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@
2525
}
2626

2727

28+
def test_read_sam():
29+
"""
30+
Test of read_SAM() function
31+
"""
32+
test_df = io.read_SAM()
33+
34+
assert isinstance(test_df, pd.DataFrame)
35+
36+
2837
@pytest.mark.parametrize(
2938
"sam_df, cons_dict",
3039
[

tests/test_macro_params.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,36 @@
22
Tests of macro_params.py module
33
"""
44

5+
import pytest
56
from ogzaf import macro_params
67

78

8-
def test_get_macro_params():
9-
test_dict = macro_params.get_macro_params()
9+
@pytest.mark.parametrize(
10+
"update_from_api",
11+
[True, False],
12+
ids=["update_from_api=True", "update_from_api=False"],
13+
)
14+
def test_get_macro_params(update_from_api):
15+
test_dict = macro_params.get_macro_params(update_from_api=update_from_api)
1016

1117
assert isinstance(test_dict, dict)
12-
assert (
13-
list(test_dict.keys()).sort()
14-
== [
15-
"r_gov_shift",
16-
"r_gov_scale",
17-
"alpha_T",
18-
"alpha_G",
19-
"initial_debt_ratio",
20-
"g_y_annual",
21-
"gamma",
22-
"zeta_D",
23-
"initial_foreign_debt_ratio",
24-
].sort()
25-
)
18+
if update_from_api:
19+
assert (
20+
list(test_dict.keys()).sort()
21+
== [
22+
"r_gov_shift",
23+
"r_gov_scale",
24+
"alpha_T",
25+
"alpha_G",
26+
"initial_debt_ratio",
27+
"g_y_annual",
28+
"gamma",
29+
"zeta_D",
30+
"initial_foreign_debt_ratio",
31+
].sort()
32+
)
33+
else:
34+
assert (
35+
list(test_dict.keys()).sort()
36+
== ["r_gov_shift", "r_gov_scale"].sort()
37+
)

0 commit comments

Comments
 (0)