Skip to content

refac: clean up piecewise module#641

Merged
FBumann merged 26 commits intoPyPSA:feat/piecewise-api-refactorfrom
FBumann:refac/piecewise-cleanup
Apr 1, 2026
Merged

refac: clean up piecewise module#641
FBumann merged 26 commits intoPyPSA:feat/piecewise-api-refactorfrom
FBumann:refac/piecewise-cleanup

Conversation

@FBumann
Copy link
Copy Markdown
Collaborator

@FBumann FBumann commented Apr 1, 2026

Summary

  • Remove dead code (_add_pwl_sos2_core, _add_pwl_incremental_core, _compute_combined_mask, unused constants)
  • Inline _add_dpwl_sos2_core into _add_disjunctive
  • Split _add_continuous into _add_sos2 and _add_incremental for readability
  • Extract helpers: _stack_along_link, _strip_nan, _rename_to_segments, _breakpoints_from_slopes
  • Rename for clarity: _extra_coords_var_coords_from, _validate_xy_points_validate_breakpoint_shapes
  • Remove skip_nan_check parameter (NaN is always handled via masking)
  • Fix mypy errors, consistent return values, deduplicate code paths
  • Rename test classes to match current function names

Test plan

  • pytest test/test_piecewise_constraints.py — 94 passed
  • mypy . --exclude build — clean
  • Coverage: 80% → 91%

🤖 Generated with Claude Code

FBumann and others added 24 commits April 1, 2026 14:41
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Move method validation to add_piecewise_constraints entry point and
avoid calling _has_trailing_nan_only multiple times on the same data.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
_coerce_breaks already returns DataArray inputs unchanged.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
… set

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
NaN breakpoints are always handled automatically via masking.
The skip_nan_check flag added API surface for minimal value —
it only asserted no NaN (misleading name) and skipped mask
computation (negligible performance gain).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Remnants of the old LP method that was removed.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Both SOS2 and incremental branches now consistently return the
link constraint, making the return value predictable for callers.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Extract the SOS2 and incremental formulations into separate functions.
Add _stack_along_link helper to deduplicate the expand+concat pattern.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
TestPiecewiseEnvelope -> TestTangentLines
TestSolverEnvelope -> TestSolverTangentLines

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Rename x_points/y_points to bp_a/bp_b to reflect N-variable context.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…ttern

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Move the ~50 line slopes-to-points conversion out of breakpoints()
into _breakpoints_from_slopes, keeping breakpoints() as a clean
validation-then-dispatch function.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Cover error paths and edge cases: non-1D input, slopes mode with
DataArray y0, non-numeric breakpoint coords, segment dim mismatch,
disjunctive >2 pairs, disjunctive interior NaN, expression name
fallback, incremental NaN masking, and scalar coord handling.

Coverage: 92% -> 97%

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@FBumann FBumann force-pushed the refac/piecewise-cleanup branch from 4d97ad8 to 30b0e68 Compare April 1, 2026 13:18
- Use `X | Y` instead of `(X, Y)` in isinstance (UP038)
- Remove unused `dim` variable in _add_continuous (F841)
- Fix docstring formatting (D213)
- Remove unnecessary type: ignore comment

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@FBumann FBumann force-pushed the refac/piecewise-cleanup branch from 9918397 to b110248 Compare April 1, 2026 13:21
@FBumann FBumann merged commit e23f934 into PyPSA:feat/piecewise-api-refactor Apr 1, 2026
1 of 2 checks 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.

1 participant