GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — BUG: accept zeros on numpy.random dirichlet function by pcralmeida · Pull Request #23440 · numpy/numpy

BUG: accept zeros on numpy.random dirichlet function #23440

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.

Sign up for GitHub

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

Merged
seberg merged 5 commits into numpy:main from pcralmeida:dirichlet
Apr 11, 2023

Uh oh!

There was an error while loading. Please reload this page.

Merged

BUG: accept zeros on numpy.random dirichlet function #23440

Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit Hold shift + click to select a range
28e1bb4
fix(mtrand): change dirichlet alpha value error
pcralmeida Mar 21, 2023
cd8f481
fix(test_matlib): add spaces on test
pcralmeida Mar 23, 2023
195874c
fix(dirichlet): add verification on generator.pyx
pcralmeida Mar 25, 2023
72a5647
feat(mtrand.pyx): revert changes
pcralmeida Apr 6, 2023
4497e43
fix(test_randomstate): alter method tocrng
pcralmeida Apr 6, 2023
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

6 changes: 3 additions & 3 deletions 6 numpy/random/mtrand.pyx
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -4425,7 +4425,7 @@ cdef class RandomState:
Raises
------
ValueError
If any value in ``alpha`` is less than or equal to zero
If any value in ``alpha`` is less than zero

See Also
--------
Expand Down Expand Up @@ -4506,8 +4506,8 @@ cdef class RandomState:
alpha_arr = <np.ndarray>np.PyArray_FROMANY(
alpha, np.NPY_DOUBLE, 1, 1,
np.NPY_ARRAY_ALIGNED | np.NPY_ARRAY_C_CONTIGUOUS)
if np.any(np.less_equal(alpha_arr, 0)):
raise ValueError('alpha <= 0')
if np.any(np.less(alpha_arr, 0)):
raise ValueError('alpha < 0')
Comment thread Outdated

@seberg seberg Mar 24, 2023

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Please limit this change to generator.pyx. I am not sure I mind this much (since it is an error being thrown), but the backward compat guarantees on RandomState this file are very strong.

So, the change here is not correct, but the identical change on the generator would be.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@pcralmeida pcralmeida Mar 25, 2023

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I've added the changes to generator.pyx as intended, and also passed the test to the numpy/random submodule. However, the test would raise the alpha = 0 value error from mtrand.pyx, so I maintained the changes that were made. The changes now seem OK, but I will wait for your feedback.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@seberg seberg Mar 26, 2023

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

The value-error is desired in mtrand (strict compatibility). The tests just need to be different. We should make sure that generator is tested, not mtrand (i.e. not np.random.diriclet but np.random.default_rng().dirichlet. Those tests live in a different file.

In a sense, we want to nudge users towards preferring np.random.default_rng() anyway, so there is probably not much reason to improve mtrand.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@pcralmeida pcralmeida Apr 6, 2023

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Hey, sorry for the long wait. I've read your comment and I've reverted the changes in mtrand. Also fixed the test having np.random.default_rng() in account. Hope these changes are good now.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
alpha_data = <double*>np.PyArray_DATA(alpha_arr)

if size is None:
Expand Down
4 changes: 4 additions & 0 deletions 4 numpy/tests/test_matlib.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np
import numpy.matlib
from numpy.testing import assert_array_equal, assert_
from numpy.random import dirichlet

def test_empty():
x = numpy.matlib.empty((2,))
Expand Down Expand Up @@ -56,3 +57,6 @@ def test_repmat():
y = np.array([[0, 1, 2, 3, 0, 1, 2, 3],
[0, 1, 2, 3, 0, 1, 2, 3]])
assert_array_equal(x, y)

def test_dirichlet():
dirichlet([5, 9, 0, 8])
Comment thread Outdated

@MatteoRaso MatteoRaso Mar 24, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Suggested change
def test_dirichlet():
dirichlet([5, 9, 0, 8])
def test_dirichlet():
y = dirichlet([5, 9, 0, 8])
assert y[2] == 0

We don't just want to show that the function doesn't throw an error, we also want to show that the function returns the right result when 0 is given as input.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

seberg reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@seberg seberg Mar 24, 2023

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Matteo is right, we should check this, thanks. However, this isn't the right file. The correct tests are in the numpy/random submodule.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.