-
Notifications
You must be signed in to change notification settings - Fork 61
feat: configurability for cycle detection #261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ParticularlyPythonicBS
wants to merge
2
commits into
unstable
Choose a base branch
from
feat/220_cycle_dection_configurability
base: unstable
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+239
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| from pathlib import Path | ||
| from unittest.mock import MagicMock | ||
|
|
||
| import networkx as nx | ||
| import pytest | ||
|
|
||
| from temoa.core.config import TemoaConfig | ||
| from temoa.model_checking.commodity_graph import visualize_graph | ||
| from temoa.types.core_types import Period, Region | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_config() -> MagicMock: | ||
| config = MagicMock(spec=TemoaConfig) | ||
| config.plot_commodity_network = True | ||
| config.output_path = Path('./') | ||
| config.cycle_count_limit = 100 | ||
| config.cycle_length_limit = 1 | ||
| return config | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def cycle_graph() -> nx.MultiDiGraph[str]: | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| dg: nx.MultiDiGraph[str] = nx.MultiDiGraph() | ||
| # Create two cycles: (A->B->A) length 2, (C->D->E->C) length 3 | ||
| dg.add_edge('A', 'B') | ||
| dg.add_edge('B', 'A') | ||
| dg.add_edge('C', 'D') | ||
| dg.add_edge('D', 'E') | ||
| dg.add_edge('E', 'C') | ||
| # Add some other nodes/edges to make it look like a real graph | ||
| dg.add_node('A', layer=1, sector='S1') | ||
| dg.add_node('B', layer=2, sector='S1') | ||
| dg.add_node('C', layer=1, sector='S2') | ||
| dg.add_node('D', layer=2, sector='S2') | ||
| dg.add_node('E', layer=3, sector='S2') | ||
| return dg | ||
|
|
||
|
|
||
| def test_cycle_limits_logging( | ||
| mock_config: MagicMock, | ||
| cycle_graph: nx.MultiDiGraph[str], | ||
| caplog: pytest.LogCaptureFixture, | ||
| monkeypatch: pytest.MonkeyPatch, | ||
| ) -> None: | ||
| """Test that cycles are logged according to length limit.""" | ||
| mock_config.cycle_length_limit = 3 | ||
|
|
||
| with caplog.at_level(logging.INFO): | ||
| # We need to mock generate_commodity_graph to return our controlled graph | ||
| import temoa.model_checking.commodity_graph as cg | ||
|
|
||
| monkeypatch.setattr( | ||
| cg, 'generate_commodity_graph', MagicMock(return_value=(cycle_graph, {})) | ||
| ) | ||
| monkeypatch.setattr(cg, 'generate_technology_graph', MagicMock()) | ||
| monkeypatch.setattr(cg, 'nx_to_vis', MagicMock(return_value='path')) | ||
|
|
||
| visualize_graph( | ||
| region=Region('R1'), | ||
| period=Period(2020), | ||
| network_data=MagicMock(), | ||
| demand_orphans=[], | ||
| other_orphans=[], | ||
| driven_techs=[], | ||
| config=mock_config, | ||
| ) | ||
|
|
||
| # Should only log cycles of length >= 3 | ||
| # (C->D->E->C) is length 3 | ||
| # (A->B->A) is length 2, should be skipped | ||
| assert any( | ||
| 'Cycle detected' in record.message | ||
| and 'C' in record.message | ||
| and 'D' in record.message | ||
| and 'E' in record.message | ||
| for record in caplog.records | ||
| ) | ||
| assert not any( | ||
| 'Cycle detected' in record.message and 'A' in record.message and 'B' in record.message | ||
| for record in caplog.records | ||
| ) | ||
|
|
||
|
|
||
| def test_cycle_count_limit( | ||
| mock_config: MagicMock, | ||
| cycle_graph: nx.MultiDiGraph[str], | ||
| caplog: pytest.LogCaptureFixture, | ||
| monkeypatch: pytest.MonkeyPatch, | ||
| ) -> None: | ||
| """Test that cycle detection stops after cycle_count_limit.""" | ||
| mock_config.cycle_count_limit = 1 | ||
| mock_config.cycle_length_limit = 1 | ||
|
|
||
| with caplog.at_level(logging.INFO): | ||
| import temoa.model_checking.commodity_graph as cg | ||
|
|
||
| monkeypatch.setattr( | ||
| cg, 'generate_commodity_graph', MagicMock(return_value=(cycle_graph, {})) | ||
| ) | ||
| monkeypatch.setattr(cg, 'generate_technology_graph', MagicMock()) | ||
| monkeypatch.setattr(cg, 'nx_to_vis', MagicMock(return_value='path')) | ||
|
|
||
| visualize_graph( | ||
| region=Region('R1'), | ||
| period=Period(2020), | ||
| network_data=MagicMock(), | ||
| demand_orphans=[], | ||
| other_orphans=[], | ||
| driven_techs=[], | ||
| config=mock_config, | ||
| ) | ||
|
|
||
| # Should only log 1 cycle and then the warning | ||
| # nx.simple_cycles might return them in different order depending on version/impl | ||
| # but it should only log ONE of them. | ||
| cycle_logs = [record.message for record in caplog.records if 'Cycle detected' in record.message] | ||
| assert len(cycle_logs) == 1 | ||
| assert 'Cycle detection reached limit of 1 cycles. Stopping.' in caplog.text | ||
|
|
||
|
|
||
| def test_cycle_count_limit_zero( | ||
| mock_config: MagicMock, | ||
| cycle_graph: nx.MultiDiGraph[str], | ||
| caplog: pytest.LogCaptureFixture, | ||
| monkeypatch: pytest.MonkeyPatch, | ||
| ) -> None: | ||
| """Test that cycle_count_limit=0 logs an error and stops immediately.""" | ||
| mock_config.cycle_count_limit = 0 | ||
|
|
||
| with caplog.at_level(logging.INFO): | ||
| import temoa.model_checking.commodity_graph as cg | ||
|
|
||
| monkeypatch.setattr( | ||
| cg, 'generate_commodity_graph', MagicMock(return_value=(cycle_graph, {})) | ||
| ) | ||
| monkeypatch.setattr(cg, 'generate_technology_graph', MagicMock()) | ||
| monkeypatch.setattr(cg, 'nx_to_vis', MagicMock(return_value='path')) | ||
|
|
||
| visualize_graph( | ||
| region=Region('R1'), | ||
| period=Period(2020), | ||
| network_data=MagicMock(), | ||
| demand_orphans=[], | ||
| other_orphans=[], | ||
| driven_techs=[], | ||
| config=mock_config, | ||
| ) | ||
|
|
||
| assert any( | ||
| 'Cycles detected but cycle_count_limit is 0. Stopping.' in record.message | ||
| for record in caplog.records | ||
| ) | ||
| assert not any('Cycle detected:' in record.message for record in caplog.records) | ||
|
|
||
|
|
||
| def test_cycle_unbounded( | ||
| mock_config: MagicMock, | ||
| cycle_graph: nx.MultiDiGraph[str], | ||
| caplog: pytest.LogCaptureFixture, | ||
| monkeypatch: pytest.MonkeyPatch, | ||
| ) -> None: | ||
| """Test that cycle_count_limit=-1 allows all cycles.""" | ||
| mock_config.cycle_count_limit = -1 | ||
|
|
||
| with caplog.at_level(logging.INFO): | ||
| import temoa.model_checking.commodity_graph as cg | ||
|
|
||
| monkeypatch.setattr( | ||
| cg, 'generate_commodity_graph', MagicMock(return_value=(cycle_graph, {})) | ||
| ) | ||
| monkeypatch.setattr(cg, 'generate_technology_graph', MagicMock()) | ||
| monkeypatch.setattr(cg, 'nx_to_vis', MagicMock(return_value='path')) | ||
|
|
||
| visualize_graph( | ||
| region=Region('R1'), | ||
| period=Period(2020), | ||
| network_data=MagicMock(), | ||
| demand_orphans=[], | ||
| other_orphans=[], | ||
| driven_techs=[], | ||
| config=mock_config, | ||
| ) | ||
|
|
||
| assert any( | ||
| 'Cycle detected' in record.message and 'C' in record.message for record in caplog.records | ||
| ) | ||
| assert any( | ||
| 'Cycle detected' in record.message and 'A' in record.message for record in caplog.records | ||
| ) | ||
| assert not any('Stopping' in record.message for record in caplog.records) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.