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

BUG: Fix random.choice when probability is not C contiguous #13716

Merged
merged 4 commits into from Jun 6, 2019

Conversation

jeremiedbb
Copy link
Contributor

Fixes #13713

ping @glemaitre

def test_choice_probas_not_contiguous(self):
p = np.repeat(np.array([[0.1, 0, 0.3, 0.6, 0]]).T, 3, axis=1)
random.choice(5, 3, p=p[:, 1])

Copy link
Member

Choose a reason for hiding this comment

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

test should also try np.random.Generator().choice(...) which would also fail.

Copy link
Member

Choose a reason for hiding this comment

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

... and np.random.multinomial, and np.random.Generator().multinomial

Copy link
Contributor

Choose a reason for hiding this comment

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

This is in the wrong file. Should be test_randomstate.py

Copy link
Contributor

Choose a reason for hiding this comment

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

And again in test_generator_mt19937.py

p = <np.ndarray>np.PyArray_FROM_OTF(p, np.NPY_DOUBLE, np.NPY_ALIGNED)
p = <np.ndarray>np.PyArray_FROM_OTF(
p, np.NPY_DOUBLE, np.NPY_ALIGNED | np.NPY_ARRAY_C_CONTIGUOUS
)
Copy link
Member

Choose a reason for hiding this comment

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

would be better to change the kahan_sum function rather than forcing a copy here. There are more places that need testing/fixing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

something like passing a memoryview instead of a pointer to kahan_sum ?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it has to go here (and in Generator) since p has to be converted from other iteable containers anyway, The downside of having two very similar files.

If this also appears in multinomial, then it would be best to create a new function in common.pyx, that would do the conversion and the checks (or at least as many as can be done).

@mattip
Copy link
Member

mattip commented Jun 4, 2019

@bashtage: any thoughts?

@bashtage
Copy link
Contributor

bashtage commented Jun 4, 2019

There are 3 required casts in each file. The two probabilities and alpha in Dirichlet. The other OFT are fine without the copy since they are all run through broadcast which takes care of the stride.

Here is a PR on randomgen that shows the locations:

bashtage/randomgen#142

@charris charris changed the title [MRG] Fix random.choice when proba is not C contiguous BUG: Fix random.choice when probabily is not C contiguous Jun 5, 2019
@jeremiedbb jeremiedbb changed the title BUG: Fix random.choice when probabily is not C contiguous BUG: Fix random.choice when probability is not C contiguous Jun 5, 2019
@jeremiedbb
Copy link
Contributor Author

thanks for the locations @bashtage
I added the fix and the corresponding tests to multinomial and dirichlet.

@bashtage
Copy link
Contributor

bashtage commented Jun 5, 2019

In tests of Generator you have to use random.bit_generator.seed not random.seed. This is a change in the new API . Other than this, LGTM.

@jeremiedbb
Copy link
Contributor Author

I just saw that, I naively copy-pasted. I'm on it :)

@jeremiedbb
Copy link
Contributor Author

just a question: why didn't that fail in randomgen ? you used the old api for the generator tests.

@bashtage
Copy link
Contributor

bashtage commented Jun 5, 2019

The APIs are not perfectly in-sync since randomgen has legacy (some users), and there is no need to break their code if they wish to upgrade. NumPy gets to start with a clean sheet.

@jeremiedbb
Copy link
Contributor Author

I see, thanks.

@charris
Copy link
Member

charris commented Jun 5, 2019

Note to self: backport will need to ignore new random generator.

@bashtage
Copy link
Contributor

bashtage commented Jun 5, 2019

@charris Backport isn't needed since this is a new bug that was only introduced in #13163

@charris
Copy link
Member

charris commented Jun 5, 2019

@bashtage Thanks for the heads up.

@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Jun 5, 2019
@bashtage
Copy link
Contributor

bashtage commented Jun 5, 2019

Note to self: backport will need to ignore new random generator.

This is probably only about 70% true. There is a lot of derived code that is effectively common with 1.16.

@mattip mattip merged commit d429f0f into numpy:master Jun 6, 2019
@mattip
Copy link
Member

mattip commented Jun 6, 2019

Thanks @jeremiedbb, reviewers.

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.

Wrong value for the sum of p in np.random.choice
4 participants