Skip to content
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

Update scipy_wrapper #2159

Merged
merged 2 commits into from Jun 26, 2023
Merged

Update scipy_wrapper #2159

merged 2 commits into from Jun 26, 2023

Conversation

SteveDiamond
Copy link
Collaborator

Description

Updates the scipy_wrapper as suggested in scipy/scipy#18749
Issue link (if applicable): #2158

Type of change

  • New feature (backwards compatible)
  • New feature (breaking API changes)
  • Bug fix
  • Other (Documentation, CI, ...)

Contribution checklist

  • Add our license to new files.
  • Check that your code adheres to our coding style.
  • Write unittests.
  • Run the unittests and check that they’re passing.
  • Run the benchmarks to make sure your change doesn’t introduce a regression.

Co-authored-by: Isaac Virshup <ivirshup@gmail.com>
@github-actions
Copy link

Benchmarks that have stayed the same:

   before           after         ratio
 [8f3e2b95]       [e9d68aa0]
      21.7±0s          21.9±0s     1.01  simple_LP_benchmarks.SimpleLPBenchmark.time_compile_problem
      10.2±0s          10.3±0s     1.01  svm_l1_regularization.SVMWithL1Regularization.time_compile_problem
      28.0±0s          28.1±0s     1.00  cvar_benchmark.CVaRBenchmark.time_compile_problem
      8.54±0s          8.54±0s     1.00  semidefinite_programming.SemidefiniteProgramming.time_compile_problem
      3.86±0s          3.84±0s     1.00  simple_LP_benchmarks.SimpleFullyParametrizedLPBenchmark.time_compile_problem
      7.77±0s          7.72±0s     0.99  optimal_advertising.OptimalAdvertising.time_compile_problem
      8.81±0s          8.72±0s     0.99  huber_regression.HuberRegression.time_compile_problem
      42.1±0s          41.5±0s     0.99  sdp_segfault_1132_benchmark.SDPSegfault1132Benchmark.time_compile_problem
      1.72±0s          1.68±0s     0.98  simple_LP_benchmarks.SimpleScalarParametrizedLPBenchmark.time_compile_problem

@sonarcloud
Copy link

sonarcloud bot commented Jun 26, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

100.0% 100.0% Coverage
0.0% 0.0% Duplication

Copy link
Collaborator

@phschiele phschiele left a comment

Choose a reason for hiding this comment

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

LGTM. Let me summarize the understanding that I've gathered as of now (this might be obvious to some readers).

  1. Returning NotImplemented from a binary operation (instead of raising NotImplementedError) is a signal that a certain operation is not implemented, but a fallback may be available (e.g., if a.__eq__(b) is not implemented, b.__eq__(a) might still be available). Only if all recovery attempts fail, an exception is being raised.
  2. scipy_wrapper ensures that all binary operations fall back to the cvxpy implementation when other is a cvxpy expression.
  3. This might not always be necessary, though, as SciPy may already provide this signal in their implementation, e.g., here.

@phschiele phschiele mentioned this pull request Jun 26, 2023
@PTNobel
Copy link
Collaborator

PTNobel commented Jun 26, 2023

  1. Returning NotImplemented from a binary operation (instead of raising NotImplementedError) is a signal that a certain operation is not implemented, but a fallback may be available (e.g., if a.eq(b) is not implemented, b.eq(a) might still be available). Only if all recovery attempts fail, an exception is being raised.

Correct.

  1. This might not always be necessary, though, as SciPy may already provide this signal in their implementation, e.g., here.

Do we know how recent that code was? I wonder if we even need this hotfix nowadays...It was added in 2013 per git blame, and it seems this originated as: f8b8e7e#diff-5b5c7742b5ff35de17876c5a718a7f6f1aeb774037c2e73d3c39bb7e937607c3
which doesn't seem relevant to this file...

In any case, I wonder if there is still a need. Especially on the __div__, __add__, __sub__, __mul__ arguments.

Testing with

class C:
    pass
    
getattr(sp.dok_matrix(np.zeros((5, 5))), func)(C())

only errored for __ge__, __le__, __lt__, __gt__ and returned NotImplemented for the rest.

@phschiele
Copy link
Collaborator

  1. Returning NotImplemented from a binary operation (instead of raising NotImplementedError) is a signal that a certain operation is not implemented, but a fallback may be available (e.g., if a.eq(b) is not implemented, b.eq(a) might still be available). Only if all recovery attempts fail, an exception is being raised.

Correct.

  1. This might not always be necessary, though, as SciPy may already provide this signal in their implementation, e.g., here.

Do we know how recent that code was? I wonder if we even need this hotfix nowadays...It was added in 2013 per git blame, and it seems this originated as: f8b8e7e#diff-5b5c7742b5ff35de17876c5a718a7f6f1aeb774037c2e73d3c39bb7e937607c3 which doesn't seem relevant to this file...

In any case, I wonder if there is still a need. Especially on the __div__, __add__, __sub__, __mul__ arguments.

Testing with

class C:
    pass
    
getattr(sp.dok_matrix(np.zeros((5, 5))), func)(C())

only errored for __ge__, __le__, __lt__, __gt__ and returned NotImplemented for the rest.

Good point. Indeed, I have checked that, based on our tests, we do not need any patch with recent SciPy versions. However, because I neither know that our tests cover all cases, nor checked with older SciPy versions, I was hesitant to suggest removing the patching entirely.

@SteveDiamond SteveDiamond merged commit bd2c711 into master Jun 26, 2023
51 checks passed
@SteveDiamond SteveDiamond deleted the steven-sp-wrapper branch June 26, 2023 18:47
@PTNobel
Copy link
Collaborator

PTNobel commented Jun 26, 2023

However, because I neither know that our tests cover all cases, nor checked with older SciPy versions, I was hesitant to suggest removing the patching entirely.

Could we deprecate the patch in 1.4 and see if it is still needed? We could raise a warning and asks people to contact us if they hit it.

phschiele added a commit that referenced this pull request Jun 26, 2023
* update spwrapper

* Update cvxpy/interface/scipy_wrapper.py

Co-authored-by: Isaac Virshup <ivirshup@gmail.com>

---------

Co-authored-by: Philipp Schiele <44360364+phschiele@users.noreply.github.com>
Co-authored-by: Isaac Virshup <ivirshup@gmail.com>
@rileyjmurray
Copy link
Collaborator

@PTNobel I'd support deprecating in 1.4.

phschiele added a commit that referenced this pull request Jun 26, 2023
* Introduce ruff linter (#2049)

* Set up ruff

* Add isort

* Update action

* Use suggested action

* Push error to check action

* Remove pre-commit action

* Remove demonstration

* Update pragmas

* Update webdocs

* Update solver imports

* Update pyproject.toml

* Fix scip timelimit no solution (#2084)

* Add condition timelimit and no solution

* Add timelimit example

* Add unit test timelimit and no solution

* attempt

* Update timelimit

* try to fix windows

* remove failing test

---------

Co-authored-by: Steven Diamond <steven@gridmatic.com>

* Adds devcontainer support (#2088)

* Adds minimal devcontainer setup

* Adds docs

* Correct categorization of ``tr_inv`` atom. (#2123)

This resolves GitHub issue #2092.

* Fixes to PowCone3D and updates to test_conic_solvers.py (#2131)

Update solver_test_helpers.py to include a cone program with SDP and power cone constraints.

Correct a bug in PowCone3D.residual, which should have been declared as a property but was not.

---------

Co-authored-by: Steven Diamond <steven@gridmatic.com>

* Fix rare bugs in curvature calculation: non-psd diagonal matrices and starting vectors in eigsh (#2086)

* explicitly generate the starting vector in our calls to eigsh; generate the vector based on a fixed random stream.

* Add special casing for diagonal matrices 

---------

Co-authored-by: Riley Murray <rileyjmurray@users.noreply.github.com>

* Map imprecise status to optimal_inaccurate (#2134)

* Merge 2126

* Fix conv and add convolve (#2047)

* make conv more like np.convolve

* more tests

* make backwards compatible

* add convolve, deprecate conv

* added convolve, deprecated conv

* update docs

* Merge 2080

* CBC interface: add ability to configure the underlying CLP solver (#1821)

* update CBC our interface (which passes through cylp) so that the user can configure the underlying CLP solver.

* write tests to verify that changing CLP tolerance settings will change CLP's behavior predictably.

* write tests to verify that changing logLevel affects the amount of material logged to console. (This was hard!)

* remove deprecated old test code

* replace TestCBC's use of unittest.skipUnless with appropriately modified pytest.mark.skipif.

---------

Co-authored-by: Riley Murray <rileyjmurray@users.noreply.github.com>

* remove github banner (#2160)

* Update scipy_wrapper (#2159)

* update spwrapper

* Update cvxpy/interface/scipy_wrapper.py

Co-authored-by: Isaac Virshup <ivirshup@gmail.com>

---------

Co-authored-by: Philipp Schiele <44360364+phschiele@users.noreply.github.com>
Co-authored-by: Isaac Virshup <ivirshup@gmail.com>

* PR benchmark against base branch (#2161)

---------

Co-authored-by: Paul <93285862+Paulnkk@users.noreply.github.com>
Co-authored-by: Steven Diamond <steven@gridmatic.com>
Co-authored-by: Riley Murray <rileyjmurray@users.noreply.github.com>
Co-authored-by: Riskfolio <dany.cajas.n@uni.pe>
Co-authored-by: wujianjack <wujian@shanshu.ai>
Co-authored-by: Steven Diamond <diamond@cs.stanford.edu>
Co-authored-by: Allen Lawrence <allenlawrence94@gmail.com>
Co-authored-by: bkurtz <bkurtz@users.noreply.github.com>
Co-authored-by: Isaac Virshup <ivirshup@gmail.com>
@PTNobel PTNobel mentioned this pull request Jul 8, 2023
9 tasks
Paulnkk pushed a commit to Paulnkk/cvxpy that referenced this pull request Jul 15, 2023
* update spwrapper

* Update cvxpy/interface/scipy_wrapper.py

Co-authored-by: Isaac Virshup <ivirshup@gmail.com>

---------

Co-authored-by: Philipp Schiele <44360364+phschiele@users.noreply.github.com>
Co-authored-by: Isaac Virshup <ivirshup@gmail.com>
@Paulnkk Paulnkk mentioned this pull request Jul 15, 2023
Paulnkk pushed a commit to Paulnkk/cvxpy that referenced this pull request Aug 1, 2023
* update spwrapper

* Update cvxpy/interface/scipy_wrapper.py

Co-authored-by: Isaac Virshup <ivirshup@gmail.com>

---------

Co-authored-by: Philipp Schiele <44360364+phschiele@users.noreply.github.com>
Co-authored-by: Isaac Virshup <ivirshup@gmail.com>
@Paulnkk Paulnkk mentioned this pull request Aug 1, 2023
SteveDiamond added a commit that referenced this pull request Aug 7, 2023
* Introduce ruff linter (#2049)

* Fix scip timelimit no solution (#2084)

* Add condition timelimit and no solution

* Add timelimit example

* Add unit test timelimit and no solution

* attempt

* Update timelimit

* try to fix windows

* remove failing test

---------

Co-authored-by: Steven Diamond <steven@gridmatic.com>

* Adds devcontainer support (#2088)

* Adds minimal devcontainer setup

* Adds docs

* Fix conv and add convolve (#2047)

* make conv more like np.convolve

* more tests

* make backwards compatible

* add convolve, deprecate conv

* added convolve, deprecated conv

* update docs

* Cherry-pick #2080

* Fix scip timelimit no solution (#2084)

* Add condition timelimit and no solution

* Add timelimit example

* Add unit test timelimit and no solution

* attempt

* Update timelimit

* try to fix windows

* remove failing test

---------

Co-authored-by: Steven Diamond <steven@gridmatic.com>

* Fixes to PowCone3D and updates to test_conic_solvers.py (#2131)

Update solver_test_helpers.py to include a cone program with SDP and power cone constraints.

Correct a bug in PowCone3D.residual, which should have been declared as a property but was not.

---------

Co-authored-by: Steven Diamond <steven@gridmatic.com>

* Revert "Fixes to PowCone3D and updates to test_conic_solvers.py (#2131)"

This reverts commit e68445e.

* Fix rare bugs in curvature calculation: non-psd diagonal matrices and starting vectors in eigsh (#2086)

* explicitly generate the starting vector in our calls to eigsh; generate the vector based on a fixed random stream.

* Add special casing for diagonal matrices

---------

Co-authored-by: Riley Murray <rileyjmurray@users.noreply.github.com>

* Fix bug with calling Mosek simplex solver (#2126)

* got new mosek simplex test passing

* revert clarabel intf change

* CBC interface: add ability to configure the underlying CLP solver (#1821)

* update CBC our interface (which passes through cylp) so that the user can configure the underlying CLP solver.

* write tests to verify that changing CLP tolerance settings will change CLP's behavior predictably.

* write tests to verify that changing logLevel affects the amount of material logged to console. (This was hard!)

* remove deprecated old test code

* replace TestCBC's use of unittest.skipUnless with appropriately modified pytest.mark.skipif.

---------

Co-authored-by: Riley Murray <rileyjmurray@users.noreply.github.com>

* remove github banner (#2160)

* Update scipy_wrapper (#2159)

* update spwrapper

* Update cvxpy/interface/scipy_wrapper.py

Co-authored-by: Isaac Virshup <ivirshup@gmail.com>

---------

Co-authored-by: Philipp Schiele <44360364+phschiele@users.noreply.github.com>
Co-authored-by: Isaac Virshup <ivirshup@gmail.com>

* Fixes to PowCone3D and updates to test_conic_solvers.py (#2131)

Update solver_test_helpers.py to include a cone program with SDP and power cone constraints.

Correct a bug in PowCone3D.residual, which should have been declared as a property but was not.

---------

Co-authored-by: Steven Diamond <steven@gridmatic.com>

* Explicit import of  power cone 3d

* Only port bug fix for mosek

* Only port bug fix for mosek

* Only port bug fix for mosek

---------

Co-authored-by: Philipp Schiele <44360364+phschiele@users.noreply.github.com>
Co-authored-by: Steven Diamond <steven@gridmatic.com>
Co-authored-by: Steven Diamond <diamond@cs.stanford.edu>
Co-authored-by: Allen Lawrence <allenlawrence94@gmail.com>
Co-authored-by: Riskfolio <dany.cajas.n@uni.pe>
Co-authored-by: Riley Murray <rileyjmurray@users.noreply.github.com>
Co-authored-by: bkurtz <bkurtz@users.noreply.github.com>
Co-authored-by: Isaac Virshup <ivirshup@gmail.com>
@PTNobel PTNobel mentioned this pull request Feb 21, 2024
9 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants