Skip to content

Conversation

andyfaff
Copy link
Contributor

@andyfaff andyfaff commented Nov 9, 2024

No description provided.

@github-actions github-actions bot added scipy.optimize maintenance Items related to regular maintenance tasks labels Nov 9, 2024
@andyfaff andyfaff mentioned this pull request Nov 9, 2024
50 tasks
Copy link
Contributor

@mdhaber mdhaber left a comment

Choose a reason for hiding this comment

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

I'll work on that PR to allow additional information at the end of the rng description.

@mdhaber mdhaber changed the title MAINT: SPEC-007 quadratic_programming MAINT: SPEC-007 quadratic_assignment Nov 9, 2024
@andyfaff
Copy link
Contributor Author

andyfaff commented Nov 9, 2024

I'll work on that PR to allow additional information at the end of the rng description.

quadratic_assignment is different. It's rng documentation is '2nd level' as part of the '1st level' options section. Does the decorator docstring patching cover that possibility? (I'm fine with leaving as-is)

@mdhaber
Copy link
Contributor

mdhaber commented Nov 9, 2024

Oh, I didn't notice before that rng is already the name of the argument. The reason for the two release cycles after rng is available as a keyword but before warnings are emitted is that downstream packages need to have access to to rng in their minimum supported SciPy. Since rng is already the keyword, we can actually begin to emit deprecation/future warnings immediately, if you'd like. Maybe take a look at the logic in the decorator and see what applies here?

Or I can, if you'd prefer.

@mdhaber
Copy link
Contributor

mdhaber commented Nov 9, 2024

Oh. And as you say, it's not even an argument; it's a key in a dictionary. Yeah, this would all have to be custom. Another option is to just not change this function. It is not tightly connected with anything else and probably not super widely used, so I would be content just leaving it alone.

@andyfaff
Copy link
Contributor Author

andyfaff commented Nov 10, 2024

I'll change the code to start emitting Warnings as per the SPEC-007 (rng is now available for all releases). The SPEC doesn't cover the case if rng is the keyword but it accepts RandomState

I propose:

  • FutureWarning if rng is None and np.random.seed has been set.
  • FutureWarning is rng is an int.
  • DeprecationWarning if rng has been given a RandomState.

@mdhaber
Copy link
Contributor

mdhaber commented Nov 10, 2024

Yup, that sounds about right!

A,
B,
method=self.method,
options={"rng": np.random.default_rng(123), "maximize": True}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Some of these needed a specific number for the seed.

@andyfaff
Copy link
Contributor Author

andyfaff commented Nov 10, 2024

The failing tests are interesting. They're all from FutureWarnings from np.random.seed having been set, with rng not having been specified.
No global seed is now set in test_quadratic_assignment, it must be being set somewhere else.

If there is a global seed set, then this will cause complications as tests that just use 'rng'=None will all emit FutureWarning. This will be more of a problem down the line with the other SPEC-007 items mature.

@andyfaff
Copy link
Contributor Author

I've had a quick search of the codebase. A global random seed is set in scipy/conftest.py. I think this is presumably set for (doc)testing purposes?

Perhaps there are other instances used elsewhere that are propagated into individual tests?

@andyfaff
Copy link
Contributor Author

There is certainly an overabundance of setting np.random.seed in the codebase. This is a bad thing.

@andyfaff
Copy link
Contributor Author

Alternatively it might be that examining np.random.mtrand._rand._bit_generator._seed_seq is None is not a good way for seeing if the global seed has been set.

@mdhaber
Copy link
Contributor

mdhaber commented Nov 11, 2024

I'm not sure about that part. Maybe we can unset it in relevant files? Or we just can't test that particular behavior in CI.

@ev-br
Copy link
Member

ev-br commented Nov 11, 2024

A global random seed is set in scipy/conftest.py. I think this is presumably set for (doc)testing purposes?

Indeed it is. This is historic, cf https://github.com/scipy/scipy/blob/main/scipy/conftest.py#L427.
Can probably be removed now that docstring examples have transitioned to using default_rng.

Copy link
Contributor

@mdhaber mdhaber left a comment

Choose a reason for hiding this comment

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

Looks close - just a few comments (that I'm willing to implement if you're not interested but will allow me to) and fixing the warning in the doc example.

Comment on lines 71 to 73
with assert_warns(FutureWarning):
res = quadratic_assignment(A, B, method=self.method,
options={"rng": 0, "maximize": False})
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we match the message and add a test for the other FutureWarning and the DeprecationWarning?

Co-authored-by: Matt Haberland <mhaberla@calpoly.edu>
@andyfaff
Copy link
Contributor Author

andyfaff commented Nov 12, 2024

Feel free to push to the branch with the changes you'd like to see.

andyfaff and others added 3 commits November 13, 2024 12:32
Co-authored-by: Matt Haberland <mhaberla@calpoly.edu>
@andyfaff
Copy link
Contributor Author

I've made the changes and have added extra tests to check that the warnings are emitted.

Copy link
Contributor

@mdhaber mdhaber left a comment

Choose a reason for hiding this comment

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

Will commit and merge if tests are happy.

@mdhaber mdhaber merged commit 3946a43 into scipy:main Nov 13, 2024
34 checks passed
@mdhaber mdhaber added the needs-release-note a maintainer should add a release note written by a reviewer/author to the wiki label Nov 13, 2024
@j-bowhay j-bowhay added this to the 1.15.0 milestone Nov 13, 2024
@lucascolley lucascolley changed the title MAINT: SPEC-007 quadratic_assignment API: optimize.quadratic_assignment: transition to rng (SPEC 7) Nov 13, 2024
@mdhaber mdhaber removed the needs-release-note a maintainer should add a release note written by a reviewer/author to the wiki label Nov 17, 2024
@andyfaff andyfaff deleted the qap branch March 21, 2025 09:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance Items related to regular maintenance tasks scipy.optimize
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants