numexpr icon indicating copy to clipboard operation
numexpr copied to clipboard

Convinient API for NumExpr.run

Open vallsv opened this issue 6 years ago • 3 comments

Hi,

I would like to know what you think about improving the arguments of NumExpr.run in order to retrieve argument names from a dictionary?

Basically the same API as numexpr.evaluate

>>> numexpr.evaluate('where((a==1) | (b > 0), 1, 0)', local_dict={'a': 0, 'b':0})
array(0, dtype=int32)

Here is what i have to do in order to have the same:

>>> expr = numexpr.NumExpr('where((a==1) | (b > 0), 1, 0)')
>>> args = {'a': 0, 'b': 0}
>>> func_args = [args[k] for k in expr.input_names]
>>> expr.run(*func_args)
array(0, dtype=int32)

It could be nice to support a dictionary, or the keyword arguments.

expr.run(a=0, b=0)
# or
expr.run({'a'=0, 'b'=0})
# or
expr.run(local_dict={'a'=0, 'b'=0})

vallsv avatar Sep 28 '19 16:09 vallsv

Have you looked into the 3.0 branch?

https://github.com/pydata/numexpr/tree/numexpr-3.0

It has support for kwargs because it is Python >3.5 only, so one can use keyword arguments or **local_dict. In fact I think that's currently the only way to use it, as I can't makeup my mind on whether I should have legacy support for local_dict or not. E.g.:

import numexpr3 as ne3
local_dict = {'a': 0, 'b':0}
ne3.NumExpr('result=2*a + b')(**local_dict)

It's still in alpha and has regressions (no bytes/unicode support, no reductions) but you can install it via pip.

robbmcleod avatar Sep 30 '19 17:09 robbmcleod

Thanks a lot.

Then i guess you are far from a release?

As the API is not the same (this object was not callable in numexpr2) you can do what you want. And if you support kwargs, i guess it's better to avoid special cases like an argument named local_dict. My 2 cents.

Are you interest about a backport of this API to the current version of NumExpr? It could help for compatibility.

BTW do you know it exists a pre-release tag on pypi, which avoid people to install it except they use --pre with pip. Do you plan to use it at one point, or you think about publishing the release with this package name numexpr3?

vallsv avatar Oct 01 '19 07:10 vallsv

Development of numexpr3 is stalled now due to lack of time. So I don't plan on changing the name at present. But if we did find time to work on it, I would probably want it to be nearly backward compatible.

Backporting to the current NumExpr is difficult without dropping support for Python 2.7, as keyword arguments are way less flexible in 2.7 compared to 3.4+. I know NumPy has already dropped support for 2.7, but I've been delaying on that. I'm inclined at present to continue to support 2.7 until 2020 (which is not that long from now), so I could think about making this change now and there we could do a pre-release for NumExpr 2.8.0.

robbmcleod avatar Oct 04 '19 14:10 robbmcleod

Message to comment on stale issues. If none provided, will not mark issues stale

github-actions[bot] avatar Feb 19 '24 01:02 github-actions[bot]