Skip to content
Open
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
3 changes: 2 additions & 1 deletion spopt/locate/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2183,7 +2183,8 @@ def to_dataframes(self, include_iterations: bool = True) -> dict[str, pd.DataFra
dataframes["coverage"] = pd.DataFrame(coverage_data)

covered_flow_sum = sum(
coverage.get("coverage", 0) for coverage in self.flow_coverage.values()
coverage.get("covered_volume", 0)
for coverage in self.flow_coverage.values()
)
total_flow = sum(self.flows.values())
coverage_pct = covered_flow_sum / total_flow * 100

Copilot AI Apr 11, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

coverage_pct = covered_flow_sum / total_flow * 100 will raise ZeroDivisionError when flows are loaded but their total volume sums to 0 (allowed today since add_flow doesn’t validate volume > 0). Other parts of this module guard with if total_flow > 0 else 0 (e.g., _get_flow_details). Please add the same guard here so to_dataframes() can export summaries for zero-total-volume inputs without crashing.

Suggested change
coverage_pct = covered_flow_sum / total_flow * 100
coverage_pct = covered_flow_sum / total_flow * 100 if total_flow > 0 else 0

Copilot uses AI. Check for mistakes.
Expand Down
12 changes: 12 additions & 0 deletions spopt/tests/test_locate/test_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,18 @@ def test_dataframe_export(self, setup_solved_model):
assert "destination" in dfs["coverage"].columns
assert "covered_proportion" in dfs["coverage"].columns

covered = dfs["coverage"]["covered_volume"].sum()
total = dfs["coverage"]["flow_volume"].sum()
expected_pct = (covered / total) * 100 if total > 0 else 0
summary_pct = float(
dfs["summary"]
.loc[dfs["summary"]["Metric"] == "Coverage %", "Value"]
.iloc[0]
.replace("Flow Coverage: ", "")
.replace("%", "")
)
assert summary_pct == pytest.approx(expected_pct, abs=0.01)

def test_solver_details(self, setup_solved_model):
model = setup_solved_model
details = model.get_solver_details()
Expand Down
Loading