shunwang / numexpr

Automatically exported from code.google.com/p/numexpr
MIT License
0 stars 0 forks source link

Support Indexing #60

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Would be great if indexing would be supported, also values of a dictionary 
cannot be passed, e.g.:

import numexpr as ne

A = np.random.random((100,100,100))

ne.evaluate("(A[...,0:-1] + A[...,1:])/2.")
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

xxxxx/<ipython console> in <module>()

/usr/lib/pymodules/python2.7/numexpr/necompiler.pyc in evaluate(ex, local_dict, 
global_dict, **kwargs)
    627     if expr_key not in _names_cache:
    628         context = getContext(kwargs)
--> 629         _names_cache[expr_key] = getExprNames(ex, context)
    630     names, ex_uses_vml = _names_cache[expr_key]
    631     # Get the arguments based on the names.

/usr/lib/pymodules/python2.7/numexpr/necompiler.pyc in getExprNames(text, 
context)
    584 
    585 def getExprNames(text, context):
--> 586     ex = stringToExpression(text, {}, context)
    587     ast = expressionToAST(ex)
    588     input_order = getInputOrder(ast, None)

/usr/lib/pymodules/python2.7/numexpr/necompiler.pyc in stringToExpression(s, 
types, context)
    219         names.update(expressions.functions)
    220         # now build the expression

--> 221         ex = eval(c, names)
    222         if expressions.isConstant(ex):
    223             ex = expressions.ConstantNode(ex, expressions.getKind(ex))

xxxxx/<expr> in <module>()

TypeError: 'VariableNode' object is not subscriptable

Original issue reported on code.google.com by maahn...@googlemail.com on 22 Sep 2011 at 12:02

GoogleCodeExporter commented 9 years ago
FWIW, you can do: ne.evaluate("(a + b)/2.", {'a': A[...,0:-1], 'b': A[...,1:]})

Original comment by gdemen...@gmail.com on 27 Sep 2011 at 12:51

GoogleCodeExporter commented 9 years ago
I agree that support for indexing would be very useful, especially since a 
single cell can be evaluated to a boolean result.  This would particularly 
benefit the PyTable project, as mentioned in this PyTables issue:
https://github.com/PyTables/PyTables/issues/127#issuecomment-3803520

Original comment by aaronqui...@gmail.com on 3 Feb 2012 at 8:34

GoogleCodeExporter commented 9 years ago
concur

in my case (and I am not yet a user of numexpr -- just came around it while 
contributing to its package maintenance in Debian etc) -- I would benefit if 
numexpr supported fancy indexing.  In my task I need to perform lots of simple 
operations (sum) across given set of columns, e.g. if X is an array, I need to 
do something like

ixs = [[1,2,10], [1,2,20]....]
[np.sum(X[:, ixs], axis=1) for ixs in ixs]

so far the quickest "pure-Python" way I have is to use a coo sparse matrix with 
entries for those columns and then doing matrix product between X and that 
sparse "index" matrix.
Not sure if my actual case could be anyhow addressed by numexpr, but having a 
generic ability to evaluate something like 

"X[:, ixs] + 2*Y[ixs]"

could indeed be useful. Thanks

Original comment by yarikop...@gmail.com on 4 Mar 2012 at 4:48

GoogleCodeExporter commented 9 years ago
This would imply a major rewrite of numexpr, and having other solutions that 
does that (like Numba), I don't think it is realistic to implement this in 
numexpr itself.  So closing this.

Original comment by fal...@gmail.com on 29 Apr 2013 at 10:29