Skip to content

Commit 905a7e8

Browse files
authored
Merge pull request #95 from EnergySystemsModellingLab/update_notebooks
Update notebook, tests and linkchecker
2 parents e9f7e10 + bbb30ae commit 905a7e8

6 files changed

Lines changed: 15 additions & 61 deletions

File tree

‎.lycheeignore‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Stack Exchange blocks Lychee's automated requests with HTTP 403.
2+
https://stackoverflow\.com/.*
3+
https://unix\.stackexchange\.com/.*
4+
https://apple\.stackexchange\.com/.*

‎data/README.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
This is a placeholder directory for input/output data for example analyses.
1+
This is a placeholder directory for output data for example analyses.

‎muse2_data_analysis/helpers.py‎

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,7 @@
66

77
DATA_DIR = Path(__file__).parent.parent.absolute() / "data"
88
EXAMPLE_NAME = "muse1_default"
9-
_INPUT_DIR = DATA_DIR / f"{EXAMPLE_NAME}_input"
10-
_OUTPUT_DIR = DATA_DIR / f"{EXAMPLE_NAME}_output"
11-
12-
13-
def get_example_input_dir() -> Path:
14-
"""Get the directory for example input data.
15-
16-
If the folder does not exist, we create it by extracting an example from muse2.
17-
"""
18-
if _INPUT_DIR.exists():
19-
return _INPUT_DIR
20-
21-
run_muse2("example", "extract", EXAMPLE_NAME, str(_INPUT_DIR))
22-
return _INPUT_DIR
9+
_OUTPUT_DIR = DATA_DIR / f"{EXAMPLE_NAME}"
2310

2411

2512
def get_example_output_dir() -> Path:
@@ -30,9 +17,6 @@ def get_example_output_dir() -> Path:
3017
if _OUTPUT_DIR.exists():
3118
return _OUTPUT_DIR
3219

33-
# Ensure INPUT_DIR exists
34-
get_example_input_dir()
35-
3620
# Run the example model
37-
run_muse2("run", str(_INPUT_DIR), "--output-dir", str(_OUTPUT_DIR))
21+
run_muse2("example", "run", EXAMPLE_NAME, "--output-dir", str(_OUTPUT_DIR))
3822
return _OUTPUT_DIR

‎notebooks/capacity.ipynb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"assets = pd.read_csv(OUTPUT_DIR / \"assets.csv\")\n",
5757
"asset_capacities = pd.read_csv(OUTPUT_DIR / \"asset_capacities.csv\")\n",
5858
"\n",
59-
"merged = asset_capacities.merge(assets, on=[\"asset_id\", \"group_id\"])\n",
59+
"merged = asset_capacities.merge(assets, on=\"asset_id\")\n",
6060
"agg_capacities = merged.groupby([\"milestone_year\", \"agent_id\", \"process_id\"])[\n",
6161
" \"capacity\"\n",
6262
"].sum()\n",

‎notebooks/prices.ipynb‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
"\n",
2424
"from muse2_data_analysis.helpers import get_example_output_dir\n",
2525
"\n",
26-
"INPUT_DIR = get_example_output_dir()\n",
27-
"prices = pd.read_csv(INPUT_DIR / \"commodity_prices.csv\")\n",
26+
"OUTPUT_DIR = get_example_output_dir()\n",
27+
"prices = pd.read_csv(OUTPUT_DIR / \"commodity_prices.csv\")\n",
2828
"prices.head()"
2929
]
3030
},

‎tests/test_helpers.py‎

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,6 @@
66
from muse2_data_analysis import helpers
77

88

9-
@patch("muse2_data_analysis.helpers.run_muse2")
10-
def test_get_example_input_dir_returns_existing_directory(
11-
run_muse2_mock: Mock, tmp_path: Path
12-
) -> None:
13-
"""Check that get_example_input_dir returns an existing input directory."""
14-
input_dir = tmp_path / "input"
15-
input_dir.mkdir()
16-
17-
with patch.object(helpers, "_INPUT_DIR", input_dir):
18-
assert helpers.get_example_input_dir() == input_dir
19-
20-
run_muse2_mock.assert_not_called()
21-
22-
23-
@patch("muse2_data_analysis.helpers.run_muse2")
24-
def test_get_example_input_dir_extracts_example_when_missing(
25-
run_muse2_mock: Mock, tmp_path: Path
26-
) -> None:
27-
"""Check that get_example_input_dir extracts the example when missing."""
28-
input_dir = tmp_path / "input"
29-
30-
with patch.object(helpers, "_INPUT_DIR", input_dir):
31-
assert helpers.get_example_input_dir() == input_dir
32-
33-
run_muse2_mock.assert_called_once_with(
34-
"example", "extract", helpers.EXAMPLE_NAME, str(input_dir)
35-
)
36-
37-
389
@patch("muse2_data_analysis.helpers.run_muse2")
3910
def test_get_example_output_dir_returns_existing_directory(
4011
run_muse2_mock: Mock, tmp_path: Path
@@ -50,20 +21,15 @@ def test_get_example_output_dir_returns_existing_directory(
5021

5122

5223
@patch("muse2_data_analysis.helpers.run_muse2")
53-
@patch("muse2_data_analysis.helpers.get_example_input_dir")
5424
def test_get_example_output_dir_runs_example_when_missing(
55-
get_example_input_dir_mock: Mock, run_muse2_mock: Mock, tmp_path: Path
25+
run_muse2_mock: Mock, tmp_path: Path
5626
) -> None:
57-
"""Check that get_example_output_dir ensures input exists and runs the model."""
58-
input_dir = tmp_path / "input"
27+
"""Check that get_example_output_dir runs the example when output is missing."""
5928
output_dir = tmp_path / "output"
60-
get_example_input_dir_mock.return_value = input_dir
6129

62-
with patch.object(helpers, "_INPUT_DIR", input_dir):
63-
with patch.object(helpers, "_OUTPUT_DIR", output_dir):
64-
assert helpers.get_example_output_dir() == output_dir
30+
with patch.object(helpers, "_OUTPUT_DIR", output_dir):
31+
assert helpers.get_example_output_dir() == output_dir
6532

66-
get_example_input_dir_mock.assert_called_once_with()
6733
run_muse2_mock.assert_called_once_with(
68-
"run", str(input_dir), "--output-dir", str(output_dir)
34+
"example", "run", helpers.EXAMPLE_NAME, "--output-dir", str(output_dir)
6935
)

0 commit comments

Comments
 (0)