Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .lycheeignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Stack Exchange blocks Lychee's automated requests with HTTP 403.
https://stackoverflow\.com/.*
https://unix\.stackexchange\.com/.*
https://apple\.stackexchange\.com/.*
2 changes: 1 addition & 1 deletion data/README.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
This is a placeholder directory for input/output data for example analyses.
This is a placeholder directory for output data for example analyses.
20 changes: 2 additions & 18 deletions muse2_data_analysis/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,7 @@

DATA_DIR = Path(__file__).parent.parent.absolute() / "data"
EXAMPLE_NAME = "muse1_default"
_INPUT_DIR = DATA_DIR / f"{EXAMPLE_NAME}_input"
_OUTPUT_DIR = DATA_DIR / f"{EXAMPLE_NAME}_output"


def get_example_input_dir() -> Path:
"""Get the directory for example input data.

If the folder does not exist, we create it by extracting an example from muse2.
"""
if _INPUT_DIR.exists():
return _INPUT_DIR

run_muse2("example", "extract", EXAMPLE_NAME, str(_INPUT_DIR))
return _INPUT_DIR
_OUTPUT_DIR = DATA_DIR / f"{EXAMPLE_NAME}"

Comment thread
tsmbland marked this conversation as resolved.

def get_example_output_dir() -> Path:
Expand All @@ -30,9 +17,6 @@ def get_example_output_dir() -> Path:
if _OUTPUT_DIR.exists():
return _OUTPUT_DIR

# Ensure INPUT_DIR exists
get_example_input_dir()

# Run the example model
run_muse2("run", str(_INPUT_DIR), "--output-dir", str(_OUTPUT_DIR))
run_muse2("example", "run", EXAMPLE_NAME, "--output-dir", str(_OUTPUT_DIR))
return _OUTPUT_DIR
2 changes: 1 addition & 1 deletion notebooks/capacity.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"assets = pd.read_csv(OUTPUT_DIR / \"assets.csv\")\n",
"asset_capacities = pd.read_csv(OUTPUT_DIR / \"asset_capacities.csv\")\n",
"\n",
"merged = asset_capacities.merge(assets, on=[\"asset_id\", \"group_id\"])\n",
"merged = asset_capacities.merge(assets, on=\"asset_id\")\n",
Comment thread
tsmbland marked this conversation as resolved.
"agg_capacities = merged.groupby([\"milestone_year\", \"agent_id\", \"process_id\"])[\n",
" \"capacity\"\n",
"].sum()\n",
Expand Down
4 changes: 2 additions & 2 deletions notebooks/prices.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"\n",
"from muse2_data_analysis.helpers import get_example_output_dir\n",
"\n",
"INPUT_DIR = get_example_output_dir()\n",
"prices = pd.read_csv(INPUT_DIR / \"commodity_prices.csv\")\n",
"OUTPUT_DIR = get_example_output_dir()\n",
"prices = pd.read_csv(OUTPUT_DIR / \"commodity_prices.csv\")\n",
"prices.head()"
]
},
Expand Down
44 changes: 5 additions & 39 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,6 @@
from muse2_data_analysis import helpers


@patch("muse2_data_analysis.helpers.run_muse2")
def test_get_example_input_dir_returns_existing_directory(
run_muse2_mock: Mock, tmp_path: Path
) -> None:
"""Check that get_example_input_dir returns an existing input directory."""
input_dir = tmp_path / "input"
input_dir.mkdir()

with patch.object(helpers, "_INPUT_DIR", input_dir):
assert helpers.get_example_input_dir() == input_dir

run_muse2_mock.assert_not_called()


@patch("muse2_data_analysis.helpers.run_muse2")
def test_get_example_input_dir_extracts_example_when_missing(
run_muse2_mock: Mock, tmp_path: Path
) -> None:
"""Check that get_example_input_dir extracts the example when missing."""
input_dir = tmp_path / "input"

with patch.object(helpers, "_INPUT_DIR", input_dir):
assert helpers.get_example_input_dir() == input_dir

run_muse2_mock.assert_called_once_with(
"example", "extract", helpers.EXAMPLE_NAME, str(input_dir)
)


@patch("muse2_data_analysis.helpers.run_muse2")
def test_get_example_output_dir_returns_existing_directory(
run_muse2_mock: Mock, tmp_path: Path
Expand All @@ -50,20 +21,15 @@ def test_get_example_output_dir_returns_existing_directory(


@patch("muse2_data_analysis.helpers.run_muse2")
@patch("muse2_data_analysis.helpers.get_example_input_dir")
def test_get_example_output_dir_runs_example_when_missing(
get_example_input_dir_mock: Mock, run_muse2_mock: Mock, tmp_path: Path
run_muse2_mock: Mock, tmp_path: Path
) -> None:
"""Check that get_example_output_dir ensures input exists and runs the model."""
input_dir = tmp_path / "input"
"""Check that get_example_output_dir runs the example when output is missing."""
output_dir = tmp_path / "output"
get_example_input_dir_mock.return_value = input_dir

with patch.object(helpers, "_INPUT_DIR", input_dir):
with patch.object(helpers, "_OUTPUT_DIR", output_dir):
assert helpers.get_example_output_dir() == output_dir
with patch.object(helpers, "_OUTPUT_DIR", output_dir):
assert helpers.get_example_output_dir() == output_dir

get_example_input_dir_mock.assert_called_once_with()
run_muse2_mock.assert_called_once_with(
"run", str(input_dir), "--output-dir", str(output_dir)
"example", "run", helpers.EXAMPLE_NAME, "--output-dir", str(output_dir)
)
Loading