Skip to content

Refactor: Split graphsAndPaths monolith into focused graphs package#72

Merged
marota merged 2 commits intomasterfrom
claude/refactor-graph-module-fV6j5
Apr 13, 2026
Merged

Refactor: Split graphsAndPaths monolith into focused graphs package#72
marota merged 2 commits intomasterfrom
claude/refactor-graph-module-fV6j5

Conversation

@marota
Copy link
Copy Markdown
Owner

@marota marota commented Apr 13, 2026

Summary

Refactors the 2295-line alphaDeesp/core/graphsAndPaths.py monolith into a well-organized alphaDeesp/core/graphs/ package with single-responsibility modules. The refactor preserves all behavior byte-for-byte while improving code organization, testability, and maintainability. A backward-compatible shim in graphsAndPaths.py ensures zero breaking changes for existing imports.

Key Changes

  • New package structure: Created alphaDeesp/core/graphs/ with 8 focused modules:

    • power_flow_graph.py — Base class for grid state visualization
    • overflow_graph.py — Colored overflow-redispatch graph (1284 lines, bulk of original logic)
    • structured_overload_graph.py — Extracts constrained paths, loop paths, and hubs
    • constrained_path.py — Main path carrying overloaded lines
    • graph_utils.py — Pure graph helpers (delete_color_edges, nodepath_to_edgepath, etc.)
    • shortest_paths.py — Shortest-path algorithms with mandatory/promoted-edge constraints
    • null_flow.py — Double/un-double null-flow edges
    • constants.py — Shared constants (voltage colors)
    • __init__.py — Public API re-exports
  • Backward compatibility shim: graphsAndPaths.py reduced to 49 lines, re-exporting all 16 public symbols from the new package. All existing imports continue to work unchanged.

  • Explicit public API: Added __all__ to clearly define the 16 public names (classes, functions, constants) the package commits to exporting.

  • Comprehensive test coverage: Added test_graphs_package.py with 443 lines of tests covering:

    • Structural invariants (package layout, symbol locations, import acyclicity)
    • PowerFlowGraph construction behavior
    • Structured overload graph path extraction
    • Shortest-path algorithm correctness
  • Documentation: Added docs/refactor-graphs-package.md explaining motivation, design principles, and migration guidance.

Implementation Details

  • Applied Single Responsibility Principle: each module has one clear purpose
  • Used strangler-fig/façade pattern: legacy import path preserved via shim
  • Maintained identical behavior: no algorithmic changes, only code organization
  • Ensured import acyclicity: graph classes depend on utilities, not vice versa
  • Locked structural invariants with tests to prevent accidental regressions during future maintenance

https://claude.ai/code/session_01KWk5cYHneZjkrcT2LRnxCi

claude added 2 commits April 13, 2026 15:06
Split the 2295-line graphsAndPaths monolith into 9 single-responsibility
sub-modules under alphaDeesp/core/graphs/ (constants, graph_utils,
null_flow, shortest_paths, power_flow_graph, constrained_path,
structured_overload_graph, overflow_graph, __init__), so each class and
helper group can be read, tested and evolved on its own. graphsAndPaths.py
is kept as a thin re-export shim to preserve every existing import path.
Behaviour is preserved byte-for-byte: class/function bodies are moved, not
rewritten. The 109 tests in test_graphs_and_paths_unit.py pass unchanged.
Add docs/refactor-graphs-package.md documenting the motivation, design
principles, new layout, dependency graph, backwards-compatibility
contract, migration guide and verification strategy of the
graphsAndPaths -> alphaDeesp.core.graphs split.

Add alphaDeesp/tests/test_graphs_package.py (35 tests) covering the
refactor's structural invariants (shim parity, public API stability,
sub-module boundaries, import acyclicity) plus behavioural coverage for
three areas that were under-tested in the legacy unit file:
PowerFlowGraph construction, Structured_Overload_Distribution_Graph
end-to-end on a small hand-built graph, and
shortest_path_mandatory_and_promoted. Together with the 109 pre-existing
unit tests in test_graphs_and_paths_unit.py (which also still pass
unchanged) this brings the graphs package up to 144 tests.
@marota marota merged commit f420386 into master Apr 13, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants