thouis / numpy-trac-migration

numpy Trac to github issues migration
2 stars 3 forks source link

Keyword argument support for vectorize. (Trac #2100) #5896

Open numpy-gitbot opened 12 years ago

numpy-gitbot commented 12 years ago

Original ticket http://projects.scipy.org/numpy/ticket/2100 on 2012-04-06 by atmention:mforbes, assigned to unknown.

In a few place, people have requested being able to use keyword arguments with vectorize. For example: http://thread.gmane.org/gmane.comp.python.scientific.user/29362/focus=29371

Here is a simple patch that defines a decorator kwvectorize simply wrapping vectorize to provide simple support for kwargs. It simply looks at the order of arguments defined in the original function, and then reorders any kwargs provided appropriately, calling vectorize with only positional arguments.

numpy-gitbot commented 12 years ago

atmention:mforbes wrote on 2012-04-06

It seems to me that this could easily be rolled into vectorize with only a slight performance hit, but it should be reviewed first by someone familiar with the subtleties of the current vectorize implementation.

numpy-gitbot commented 12 years ago

atmention:mforbes wrote on 2012-04-14

Here is a patch that is integrated into the main vectorize class. It includes the following:

API change:

  1. vectorize now supports vectorization over keyword arguments.
  2. Added new new optional kwargs: argspec:: Allows the user to override automatic detection (for wrapped functions for example) exclude:: A set of variables that will be excluded from vectorization.
  3. If pyfunc.original_function is defined, then this is used for getting documentation and the argspec.

Additional changes:

  1. I need the argspec, so I added _get_argspec() that calls inspect.getargspec() and falls back to _get_nargs(). Maybe these could be rolled together.
  2. I added the patch for issue #1156 with a regression test so that it can be closed too if accepted.
  3. Original __call__ is now in _vectorize_call. This is called directly unless __call__ is passed keyword arguments, in which case the new code is executed which assembles the arguments into their proper positions and then calls _vectorize_call.
  4. exclude is implemented by creating a wrapper function with only the un-excluded positional arguments. This is explicitly passed to _vectorize_call instead of self.thefunc. The wrapper explicitly calls the original function with everything packed as a dictionary of keyword arguments for simplicity.

Added documentation, as well as some tests. All of the vectorize code is now covered by tests.

numpy-gitbot commented 12 years ago

Attachment added by atmention:mforbes on 2012-04-14: 0001-MMF-added-kwvectorize-wrapper-for-vectorize-with-kwa.patch

numpy-gitbot commented 12 years ago

Attachment added by atmention:mforbes on 2012-04-14: 0002-New-kwarg-and-exclude-support-integrated-into-vector.patch

numpy-gitbot commented 12 years ago

Attachment added by atmention:mforbes on 2012-04-14: 0003-Added-patch-suggested-for-ticket-1156-and-finished-c.patch

numpy-gitbot commented 12 years ago

Attachment added by atmention:mforbes on 2012-04-14: 0004-Added-some-comments-pointing-out-the-issue-numbers-f.patch

numpy-gitbot commented 12 years ago

atmention:rgommers wrote on 2012-04-16

Hi Michael, looks good from a quick glance at the patches. Could you send this as a pull request on github? That will make testing and code review quite a bit easier.

Noticed a few minor things so far:

numpy-gitbot commented 12 years ago

atmention:mforbes wrote on 2012-04-19

Okay. I fixed these and added a pull request for the branches with changes (https://github.com/numpy/numpy/pull/256).

numpy-gitbot commented 12 years ago

atmention:mforbes wrote on 2012-05-10

Completed patch now at pull request https://github.com/numpy/numpy/pull/275. This supersedes PR 256.