Skip to content

ENH: Add axis argument to random.permutation and random.shuffle - #13829

Merged
mattip merged 1 commit into
numpy:masterfrom
kianasun:add-permutation-axis
Sep 16, 2019
Merged

mattip merged 1 commit into
numpy:masterfrom
kianasun:add-permutation-axis

Conversation

@kianasun

Copy link
Copy Markdown
Contributor

The random.permutation function now can only shuffle the first axis of a multi-dimensional array. I add an argument for the function and allow it to shuffle along a given axis.

@wkschwartz wkschwartz left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Needs documentation and possibly input validation/error handling for the new argument.

Comment thread numpy/random/generator.pyx Resolved
@kianasun
kianasun force-pushed the add-permutation-axis branch 2 times, most recently from 51d28d2 to c91cd0f Compare June 25, 2019 16:12
Comment thread numpy/random/tests/test_random.py Resolved
@charris charris changed the title ENH: Improve permutation function on a given axis MAINT: Improve permutation function on a given axis Jun 25, 2019
@kianasun
kianasun force-pushed the add-permutation-axis branch from c91cd0f to 4ff95a7 Compare June 25, 2019 17:40
Comment thread numpy/random/mtrand.pyx Resolved
@wkschwartz

Copy link
Copy Markdown

Once that print statement is removed, I think this is ready for review by core maintainers. @kianasun, the docs say that the next step is for you to e-mail the NumPy mailing list to request a code review.

@kianasun
kianasun force-pushed the add-permutation-axis branch from 4ff95a7 to 8008eb2 Compare June 25, 2019 17:49
@kianasun

Copy link
Copy Markdown
Contributor Author

@wkschwartz OK. I will send an email after all checks are done. Thank you!

@wkschwartz

Copy link
Copy Markdown

Should this PR be extended to address #11583, and there by make the two functions have more similar signatures?

@bashtage bashtage left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You can't add any features to mtrand.pyx.

Comment thread numpy/random/mtrand.pyx Resolved
@bashtage

Copy link
Copy Markdown
Contributor

It would be nice to mirror the api in shuffle as well since permutation and shuffle are closely related. Shuffle could be implemented using two calls to numpy.swapaxes

@kianasun
kianasun force-pushed the add-permutation-axis branch 2 times, most recently from 9f57893 to 3deeea9 Compare June 26, 2019 18:27
@kianasun

Copy link
Copy Markdown
Contributor Author

Hi @bashtage , I have updated the code :)

Comment thread numpy/random/generator.pyx Resolved
Comment thread numpy/random/generator.pyx Resolved
Comment thread numpy/random/generator.pyx Resolved
Comment thread numpy/random/generator.pyx Resolved
@bashtage

Copy link
Copy Markdown
Contributor

Some small issues but this needs to go to the mailing list since the API is a bit tricky since shuffle has 3 distinct paths/uses and only one needs axis.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The tests immediately above this cover a variety of input types. Should the axis argument be paired in combination with them?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I am not sure. If combine them, we might need to test errors for different input types. For example lambda x: np.array([]) which has one dimension, if we set axis=1, do we need to assert AxisError?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, axis=1 should raise AxisError on a 1d array

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In that case, will it be better to combine them since we need to raise errors for some but not all types?

@kianasun
kianasun force-pushed the add-permutation-axis branch 2 times, most recently from aeff0bb to 9bb7b23 Compare July 3, 2019 17:37
@kianasun

kianasun commented Jul 3, 2019

Copy link
Copy Markdown
Contributor Author

Hi @bashtage @wkschwartz, I have updated the code as recommended (except a few questions I asked). By the way, do I need to change anything to pass the check Shippable? I am not sure why it is failed. Thanks.

@bashtage

bashtage commented Jul 4, 2019

Copy link
Copy Markdown
Contributor

@kianasun You should propose this enhancement on the NumPy mailing list.

@kianasun

kianasun commented Jul 8, 2019

Copy link
Copy Markdown
Contributor Author

Hi @bashtage, I sent an email to Numpy-discussion last week.

@mattip

mattip commented Sep 12, 2019

Copy link
Copy Markdown
Member

@kianasun there are conflicts and changes have been requested.

@kianasun
kianasun force-pushed the add-permutation-axis branch from 9bb7b23 to 443d410 Compare September 12, 2019 23:30
@kianasun

Copy link
Copy Markdown
Contributor Author

@mattip updated.

@mattip

mattip commented Sep 13, 2019

Copy link
Copy Markdown
Member

resent a message to the mailing list since the one from July had no responses.

Comment thread numpy/random/generator.pyx Resolved
@kianasun
kianasun force-pushed the add-permutation-axis branch from 443d410 to d3858b0 Compare September 13, 2019 23:49
Comment thread numpy/random/generator.pyx Resolved
@kianasun
kianasun force-pushed the add-permutation-axis branch from d3858b0 to 53398fd Compare September 14, 2019 00:06
@mattip

mattip commented Sep 14, 2019

Copy link
Copy Markdown
Member

The mailing list discussion was positive. I will merge this soon if there are no more comments.

Comment thread numpy/random/generator.pyx Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This doesn't make any sense to me - the message it prints out will just be wrong if a.ndim isn't 1.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@eric-wieser It seems for untyped path, the function only shuffle on 0 axis?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@eric-wieser But you are right, the error message is not correct. Should we raise another error?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think on this path, you should use:

if axis != 0:
    x = np.swapaxes(x, ...)

That way, if swapaxes is supported on a duck-array, it will just work. If it isn't supported, then the duckarray will be able to report itself that its not supported.

By adding the !=0 check, you ensure that duckarrays don't need to implement swapaxes to support axis=0.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@eric-wieser I see. If swapaxes works, we need to add another swapaxes after the blockwith self.lock ... ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Correct

Comment thread numpy/random/generator.pyx Resolved
@eric-wieser

Copy link
Copy Markdown
Member

This should be ENH not MAINT, and needs a release note.

@mattip mattip added the 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes label Sep 14, 2019
@mattip mattip changed the title MAINT: Improve permutation function on a given axis ENH: Improve permutation function on a given axis Sep 14, 2019
@kianasun
kianasun force-pushed the add-permutation-axis branch from 53398fd to 887119b Compare September 14, 2019 20:02
Comment thread numpy/random/generator.pyx Resolved
Comment thread numpy/random/generator.pyx Resolved
@kianasun
kianasun force-pushed the add-permutation-axis branch from 887119b to 247282b Compare September 14, 2019 22:24
Comment thread numpy/random/generator.pyx Resolved
@kianasun
kianasun force-pushed the add-permutation-axis branch from 247282b to c99a166 Compare September 14, 2019 23:56
@mattip mattip changed the title ENH: Improve permutation function on a given axis ENH: Add axis argument to random.permutation and random.shuffle Sep 16, 2019
@mattip
mattip merged commit f869492 into numpy:master Sep 16, 2019
@mattip

mattip commented Sep 16, 2019

Copy link
Copy Markdown
Member

Thanks @kianasun and reviewers

@WarrenWeckesser

Copy link
Copy Markdown
Member

FYI: There's a small bug in the new code. I proposed a fix in #14662.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants