imaginglss.utils.npyquery module

npyquery, Query mini-language for numpy arrays.

We reuse the python expression parser to construct the AST. This is inspired by TinyDB, and may have been inspired by other python database implementations as well.

Override Column.apply for customized access to objects (e.g.
Pandas dataframe or imaginglss’s ColumnStore )

Simple manipulation of the AST is allowed. For example, node.assume(a, b) allows one to replace ‘a’ with ‘b’ in the expression.

Examples

>>> d = dtype([
        ('BlackholeMass', 'f4'), 
        ('Position', ('f4', 3))])
>>> data = numpy.zeros(4, dtype=d)
>>> data['BlackholeMass'][:] = numpy.arange(4)
>>> data['Position'][:] = numpy.arange(12).reshape(3, 1)
>>> query  = (Column('BlackholeMass') > 2.0)
>>> query &= (Column('BlackholeMass') > 4.0)
>>> query &= (Column('Position')[2] > 1.0)
>>> print(query.apply(data))
>>> query = query.assume(Column('BlackholeMass'), 1.0)
>>> print(query.apply(data))
class imaginglss.utils.npyquery.AssumptionVisitor(node, literal)[source]

Bases: imaginglss.utils.npyquery.Visitor

visit_node(node)[source]
class imaginglss.utils.npyquery.Column(name)[source]

Bases: imaginglss.utils.npyquery.Node

Represents accessing a column from the data array

apply(array, s)[source]
names
class imaginglss.utils.npyquery.Expr(operator, function, operands)[source]

Bases: imaginglss.utils.npyquery.Node

Represents an expression.

e.g. comparing a column with a number.

An expression can have multiple operands.

>>> for o in expr.operands:
>>>   .....
>>> print expr[0], expr[1]
flatten(operands)[source]

Flattens operands of associative operators.

e.g. (a + b) + (c + d) becomes a + b + c + d

operands
class imaginglss.utils.npyquery.GetItem(obj, index)[source]

Bases: imaginglss.utils.npyquery.Node

Represents accesing an item in a vector column.

Astonishing that pandas can’t do this.

obj
class imaginglss.utils.npyquery.Literal(value)[source]

Bases: imaginglss.utils.npyquery.Node

Represents a literal constant.

imaginglss.utils.npyquery.Max(*args)[source]
imaginglss.utils.npyquery.Min(*args)[source]
class imaginglss.utils.npyquery.Node[source]

Bases: object

A node in the query expression.

Parameters:

array : ndarray or alike

applying the query node to the array and returns items satisfying the array.

T
apply(array)[source]
assume(node, literal)[source]

Replace all subexpression ‘node’ with ‘literal’.

cos()[source]
equals(other)[source]
log()[source]
log10()[source]
max()[source]
min()[source]
names

returns a list of column names used in this node.

This function is recursive

sin()[source]
tan()[source]
class imaginglss.utils.npyquery.QueryVisitor(array, s)[source]

Bases: imaginglss.utils.npyquery.Visitor

visit_column(node)[source]
visit_expr(node)[source]
visit_getitem(node)[source]
visit_literal(node)[source]
visit_transpose(node)[source]
class imaginglss.utils.npyquery.Transpose(node)[source]

Bases: imaginglss.utils.npyquery.Node

Represents a transpose. (.T attribute)

obj
class imaginglss.utils.npyquery.Visitor[source]

Bases: object

reg(attr, type)[source]
visit(node)[source]
visit_node(node)[source]
imaginglss.utils.npyquery.repr_slice(s)[source]
imaginglss.utils.npyquery.test()[source]