Skip to content

Commit

Permalink
TEMPORARY Debug test_plot_simulated on macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
khaeru committed Jan 30, 2025
1 parent 4c205a6 commit 50f6fde
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pytest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ jobs:
run: |
pytest message_ix_models \
-m "not (ece_db or snapshot)" \
--color=yes --durations=20 -rA --verbose \
--color=yes --durations=20 -rA --verbose --exitfirst \
--cov-report=term-missing --cov-report=xml \
--numprocesses=auto \
--local-cache --jvmargs="-Xmx6G"
Expand Down
3 changes: 3 additions & 0 deletions message_ix_models/model/transport/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,15 @@ def from_context(
# Handle arguments
options = options or dict()

log.debug(f".transport.Config.from_context: {context.model.regions = }")
try:
# Identify the node codelist used in `scenario`
regions = identify_nodes(scenario) if scenario else context.model.regions
except (AttributeError, ValueError):
pass
else:
if scenario:
log.debug(f".transport.Config.from_context: {scenario.set('node') = }")
if context.model.regions != regions:
log.info(
f"Override Context.model.regions={context.model.regions!r} with "
Expand Down
21 changes: 20 additions & 1 deletion message_ix_models/model/transport/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,17 @@ def built_transport(
options = options or dict()

# Retrieve (maybe generate) the bare RES with the same settings
log.info(f"built_transport: {context.regions = }")
res = bare_res(request, context, solved)
log.info(f"built_transport: {res.set('node') = }")
log.info(f"built_transport: {res.url = }")

# Derive the name for the transport scenario
model_name = res.model.replace("-GLOBIOM", "-Transport")

try:
scenario = Scenario(context.get_platform(), model_name, "baseline")
log.info(f"built_transport: {scenario.url = }")
except ValueError:
log.info(f"Create '{model_name}/baseline' for testing")

Expand All @@ -132,6 +136,7 @@ def built_transport(

with log_cm:
scenario = res.clone(model=model_name)
log.info(f"built_transport: {scenario.url = }")
build.main(context, scenario, options, fast=True)
else:
# Loaded existing Scenario; ensure config files are loaded on `context`
Expand All @@ -142,7 +147,16 @@ def built_transport(
scenario.solve(solve_options=dict(lpmethod=4))

log.info(f"Clone to '{model_name}/{request.node.name}'")
return scenario.clone(scenario=request.node.name, keep_solution=solved)
log.info(f"built_transport: {scenario.url = }")
result = scenario.clone(scenario=request.node.name, keep_solution=solved)
log.info(f"built_transport: {result.url = }")
log.info(f"built_transport: {result.set('node') = }")
result2 = scenario.clone(
model=model_name, scenario=request.node.name, keep_solution=solved
)
log.info(f"built_transport: {result2.url = }")
log.info(f"built_transport: {result2.set('node') = }")
return result


def simulated_solution(request, context) -> Reporter:
Expand All @@ -154,7 +168,10 @@ def simulated_solution(request, context) -> Reporter:
from .report import callback

# Build the base model
log.debug(f"simulated_solution: {context.regions = }")
scenario = built_transport(request, context, solved=False)
log.debug(f"simulated_solution: {scenario.url = }")
log.debug(f"simulated_solution: {scenario.set('node') = }")

# Info about the built model
info = ScenarioInfo(scenario)
Expand Down Expand Up @@ -194,4 +211,6 @@ def simulated_solution(request, context) -> Reporter:
with silence_log("genno", logging.CRITICAL):
message_ix_models.report.prepare_reporter(context, reporter=rep)

log.debug(f"simulated_solution: {context.regions = }")

return rep
2 changes: 2 additions & 0 deletions message_ix_models/report/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ def prepare_reporter(
"""
log.info("Prepare reporter")

log.debug(f".report.prepare_reporter: {context.regions = }")

if reporter:
# Existing `Reporter` provided
rep = reporter
Expand Down
3 changes: 3 additions & 0 deletions message_ix_models/testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ def bare_res(request, context: Context, solved: bool = False) -> message_ix.Scen
from message_ix_models.model import bare

# Model name: standard "MESSAGEix-GLOBIOM R12 YB" plus a suffix
log.info(f"bare_res: {context.model.regions = }")
model_name = bare.name(context, unique=True)

mp = context.get_platform()
Expand All @@ -213,6 +214,8 @@ def bare_res(request, context: Context, solved: bool = False) -> message_ix.Scen
context.scenario_info.update(model=model_name, scenario="baseline")
base = bare.create_res(context)

log.info(f"bare_res: {base.set('node') = }")

if solved and not base.has_solution():
log.info("Solve")
base.solve(solve_options=dict(lpmethod=4), quiet=True)
Expand Down
1 change: 1 addition & 0 deletions message_ix_models/tests/model/transport/test_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ def test_simulated_solution(request, test_context, regions="R12", years="B"):
def test_plot_simulated(request, test_context, plot_name, regions="R12", years="B"):
"""Plots are generated correctly using simulated data."""
test_context.update(regions=regions, years=years)
log.debug(f"test_plot_simulated: {test_context.regions = }")
rep = simulated_solution(request, test_context)

# print(rep.describe(f"plot {plot_name}")) # DEBUG
Expand Down

0 comments on commit 50f6fde

Please sign in to comment.