Skip to content

Feature/defensive checks and tests#7

Merged
FBumann merged 4 commits intomasterfrom
feature/defensive-checks-and-tests
Nov 26, 2025
Merged

Feature/defensive checks and tests#7
FBumann merged 4 commits intomasterfrom
feature/defensive-checks-and-tests

Conversation

@FBumann
Copy link
Owner

@FBumann FBumann commented Nov 26, 2025

Closes # (if applicable).

Changes proposed in this Pull Request

Checklist

  • Code changes are sufficiently documented; i.e. new functions contain docstrings and further explanations may be given in doc.
  • Unit tests for new features were added (if applicable).
  • A note for the release notes doc/release_notes.rst of the upcoming release is included.
  • I consent to the release of this PR's code under the MIT license.

Summary by CodeRabbit

  • New Features

    • Added optional section header control for quadratic constraints file output.
  • Bug Fixes

    • Fixed quadratic constraint label validation and duplicate detection.
    • Corrected MOSEK quadratic coefficient handling to match solver conventions.
    • Eliminated redundant assignment in solver output processing.
  • Tests

    • Expanded quadratic constraint solver testing across multiple solvers and I/O APIs with new integration test fixtures.
  • Documentation

    • Enhanced quadratic matrix construction documentation and notebook examples.

✏️ Tip: You can customize this high-level summary in your review settings.

  1. linopy/matrices.py:185-238 - Uniqueness check for QC labels

  Added seen_labels tracking in both qc_sense and qc_rhs properties with assertions to detect duplicate quadratic constraint labels across different constraint objects.

  2. linopy/matrices.py:241-305 - Document Q-matrix assembly assumptions

  Extended the docstring for Qc property to document the assumption that flat_qcons stores each quadratic term exactly once, and the symmetrization logic applied.

  3. linopy/matrices.py:307-357 - Debug assertions for label consistency

  Added a skipped_rows counter in qc_linear with an assertion that fails if any linear terms are skipped due to missing variable or constraint labels.

  4. linopy/constants.py:203-213 - Expose qc_dual in Result repr

  Updated Result.__repr__ to show the count of qc_duals when present (e.g., "Solution: 2 primals, 1 duals, 3 qc_duals").

  5. examples/quadratic-constraints.ipynb - Strip notebook outputs

  Cleared all execution outputs and reset execution counts to None for all code cells.

  6. linopy/io.py:462-470 - Guard for missing label metadata

  Added an explicit check with a clear ValueError if a label from the flat representation is not found in the constraint metadata.

  Also fixed:

  - linopy/solvers.py:1213 - Removed the double assignment typo (solution = solution = ...).
@coderabbitai
Copy link

coderabbitai bot commented Nov 26, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

This PR enhances quadratic constraint handling through validation additions, testing infrastructure improvements, and bug fixes. It includes duplicate QC label validation, Solution representation updates, file export enhancements, MOSEK convention alignment, and a comprehensive test suite for multi-solver QC solving.

Changes

Cohort / File(s) Summary
Quadratic Constraint Validation
linopy/matrices.py
Added runtime validation to detect duplicate QC labels globally; enhanced Qc docstring with construction notes; introduced skipped\_rows counter in qc_linear to catch unmapped linear terms and report index mismatches.
Solution Representation
linopy/constants.py
Modified Solution __repr__ to compute and display qc\_dual count from self.solution.qc_dual when present, appending it after primal count with conditional suffix.
File I/O and Export
linopy/io.py
Added optional write_section_header parameter to quadratic_constraints_to_file; introduced label validation raising ValueError for missing metadata; updated MOSEK QC handling to pass full Q data without division by 2, aligning with MOSEK convention.
Solver Logic
linopy/solvers.py
Fixed duplicate variable assignment: solution = solution = maybe_adjust_objective_sign(...)solution = maybe_adjust_objective_sign(...).
Testing Infrastructure
test/test_quadratic_constraint.py
Added QC solver availability detection with configurable qc_solver_params; introduced six pytest fixtures (qc_circle_model, qc_multidim_model, qc_mixed_model, qc_cross_terms_model, qc_geq_model, qc_equality_model) and new TestQuadraticConstraintSolving class with parameterized solver tests covering constraint satisfaction and objective validation across multiple QC types and solvers.
Documentation
examples/quadratic-constraints.ipynb
Reformatted cell sources from single strings to multi-line arrays; removed prior execution outputs and metadata, preparing notebook for fresh execution.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

  • linopy/io.py: MOSEK QC convention change (no division by 2) requires verification against MOSEK API expectations and testing coverage.
  • test/test_quadratic_constraint.py: Large test suite addition; verify fixture models are correctly constructed and assertions appropriately cover solver edge cases and nonconvexity handling.
  • linopy/matrices.py: Duplicate label validation logic; ensure all code paths populate labels before checks and error messages are actionable.
  • linopy/solvers.py: Bug fix is straightforward but verify maybe_adjust_objective_sign side effects and return value handling.

Possibly related PRs

Poem

🐰 Quadratic constraints now leap with care,
Duplicate labels caught mid-air!
MOSEK conventions aligned just right,
Tests bloom in parametric light. ✨
Validation guards each QC's flight!

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feature/defensive-checks-and-tests

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c5c3914 and 206c768.

📒 Files selected for processing (6)
  • examples/quadratic-constraints.ipynb (9 hunks)
  • linopy/constants.py (1 hunks)
  • linopy/io.py (4 hunks)
  • linopy/matrices.py (5 hunks)
  • linopy/solvers.py (1 hunks)
  • test/test_quadratic_constraint.py (2 hunks)

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@FBumann FBumann merged commit bb31430 into master Nov 26, 2025
1 check was pending
@coderabbitai coderabbitai bot mentioned this pull request Nov 26, 2025
4 tasks
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