neurolang.relational_algebra#
Module containing the intermediate representation implementation of relational algebra. It includes, expressions representing the operations, a solver, to execute the expressions on RelationalAlgebraSet objects. and different static optimisers.
- class neurolang.relational_algebra.BinaryRelationalAlgebraOperation(relation_left, relation_right)#
- classmethod apply(*args)#
Builds a new expression using a tuple of its parameters
- property type#
Get the expression’s type.
For parameterized classes (e.g., FunctionApplication[bool]), the type is stored as a class attribute under _nl_class_type. For non-parameterized classes, it is stored as an instance attribute under _type, defaulting to Unknown. Class-level access (e.g., PM.type for a class PM that inherits from PatternMatcher[int]) also works by reading _nl_class_type from the class hierarchy.
This property avoids instance-attribute shadowing: calling self.type = X in __init__ stores via the setter under _type, and change_type (which changes __class__ to a parameterized class) makes the type available from the class attribute — no need to pop anything from __dict__.
- unapply()#
Returns a tuple of parameters used to build the expression.
- class neurolang.relational_algebra.ColumnInt#
Refer to a relational algebra set’s column by its index.
- class neurolang.relational_algebra.ColumnStr#
Refer to a named relational algebra set’s column by its name.
- class neurolang.relational_algebra.ConcatenateConstantColumn(relation, column_name, column_value)#
Add a column with a repeated constant value to a relation.
- Attributes:
- relationConstant[RelationalAlgebraSet]
Relation to which the column will be added.
- column_nameConstant[ColumnStr] or Symbol[ColumnStr]
Name of the newly added column.
- column_valueConstant
Constant value repeated in the new column.
- classmethod apply(*args)#
Builds a new expression using a tuple of its parameters
- property type#
Get the expression’s type.
For parameterized classes (e.g., FunctionApplication[bool]), the type is stored as a class attribute under _nl_class_type. For non-parameterized classes, it is stored as an instance attribute under _type, defaulting to Unknown. Class-level access (e.g., PM.type for a class PM that inherits from PatternMatcher[int]) also works by reading _nl_class_type from the class hierarchy.
This property avoids instance-attribute shadowing: calling self.type = X in __init__ stores via the setter under _type, and change_type (which changes __class__ to a parameterized class) makes the type available from the class attribute — no need to pop anything from __dict__.
- unapply()#
Returns a tuple of parameters used to build the expression.
- class neurolang.relational_algebra.Destroy(relation, src_column, dst_column)#
Operation to map a column of a collection of elements into a new column with all collections concatenated
- Attributes:
- relationExpression[AbstractSet]
Relation on which the projections are applied.
- src_columnConstant[ColumnStr]
Column to destroy in the operation.
- dst_columnConstant[ColumnStr]
Column to map onto the new set.
Notes
The concept of set destruction is formally defined in chapter 20 of [1].
[1]Abiteboul, S., Hull, R. & Vianu, V. “Foundations of databases”. ( Addison Wesley, 1995).
- classmethod apply(*args)#
Builds a new expression using a tuple of its parameters
- property type#
Get the expression’s type.
For parameterized classes (e.g., FunctionApplication[bool]), the type is stored as a class attribute under _nl_class_type. For non-parameterized classes, it is stored as an instance attribute under _type, defaulting to Unknown. Class-level access (e.g., PM.type for a class PM that inherits from PatternMatcher[int]) also works by reading _nl_class_type from the class hierarchy.
This property avoids instance-attribute shadowing: calling self.type = X in __init__ stores via the setter under _type, and change_type (which changes __class__ to a parameterized class) makes the type available from the class attribute — no need to pop anything from __dict__.
- unapply()#
Returns a tuple of parameters used to build the expression.
- class neurolang.relational_algebra.Difference(relation_left, relation_right)#
- classmethod apply(*args)#
Builds a new expression using a tuple of its parameters
- property type#
Get the expression’s type.
For parameterized classes (e.g., FunctionApplication[bool]), the type is stored as a class attribute under _nl_class_type. For non-parameterized classes, it is stored as an instance attribute under _type, defaulting to Unknown. Class-level access (e.g., PM.type for a class PM that inherits from PatternMatcher[int]) also works by reading _nl_class_type from the class hierarchy.
This property avoids instance-attribute shadowing: calling self.type = X in __init__ stores via the setter under _type, and change_type (which changes __class__ to a parameterized class) makes the type available from the class attribute — no need to pop anything from __dict__.
- unapply()#
Returns a tuple of parameters used to build the expression.
- class neurolang.relational_algebra.EliminateTrivialProjections(*args, **kwargs)#
- match(expression)#
Find the action for a given expression by going through the
patterns.Goes through the triplets in in
patternsand calls the action specified by the first satisfied triplet.
- pattern_match(pattern, expression)#
Return
Trueifpatternmatchesexpression.Patterns are of the following form:
...matches everything.Expressionclass matches ifexpressionis instance of the given class for anytype.Expression[T]matches ifexpressionis instance of the given class withexpression.typea subtype ofT.Expression[T](Pattern_0, Pattern_1, ....., Pattern_N)matches ifexpressionis instance of the given class withexpression.typea subtype ofTand recursively matches the construction parameters of the instance on the types given asPattern_0,Pattern_1, etc.(Pattern_0, ....., Pattern_N)matches when expression is atupleof length N + 1 whereexpression[0]matchesPattern_0,expression[1]matchesPattern_1, etc.instancean instance of a python class not subclassingExpressionmatches wheninstance == expression
- property patterns#
Property holding an iterator of triplets
(pattern, guard, action).pattern: is an Expression class, or instance where construction parameters and the type can be replaced by an ellipsis … to signal a wildcard. Seepattern_match()for more details.guard: is a function mapping an Expression instance to a boolean orNone.action: is the method receiving the matchingexpressioninstance to be executed upon pattern and match beingTrue.
- class neurolang.relational_algebra.EquiJoin(relation_left, columns_left, relation_right, columns_right)#
- classmethod apply(*args)#
Builds a new expression using a tuple of its parameters
- property type#
Get the expression’s type.
For parameterized classes (e.g., FunctionApplication[bool]), the type is stored as a class attribute under _nl_class_type. For non-parameterized classes, it is stored as an instance attribute under _type, defaulting to Unknown. Class-level access (e.g., PM.type for a class PM that inherits from PatternMatcher[int]) also works by reading _nl_class_type from the class hierarchy.
This property avoids instance-attribute shadowing: calling self.type = X in __init__ stores via the setter under _type, and change_type (which changes __class__ to a parameterized class) makes the type available from the class attribute — no need to pop anything from __dict__.
- unapply()#
Returns a tuple of parameters used to build the expression.
- class neurolang.relational_algebra.ExtendedProjection(relation, projection_list)#
General operation defining string-based relational algebra projections allowing flexible computations on a relation’s columns.
- Attributes:
- relationExpression[AbstractSet]
Relation on which the projections are applied.
- projection_listTuple[FunctionApplicationListMember]
List of projections to apply.
Notes
The concept of extended projection is formally defined in section 5.2.5 of [1].
[1]Garcia-Molina, Hector, Jeffrey D. Ullman, and Jennifer Widom. “Database systems: the complete book.” (2009).
- classmethod apply(*args)#
Builds a new expression using a tuple of its parameters
- property type#
Get the expression’s type.
For parameterized classes (e.g., FunctionApplication[bool]), the type is stored as a class attribute under _nl_class_type. For non-parameterized classes, it is stored as an instance attribute under _type, defaulting to Unknown. Class-level access (e.g., PM.type for a class PM that inherits from PatternMatcher[int]) also works by reading _nl_class_type from the class hierarchy.
This property avoids instance-attribute shadowing: calling self.type = X in __init__ stores via the setter under _type, and change_type (which changes __class__ to a parameterized class) makes the type available from the class attribute — no need to pop anything from __dict__.
- unapply()#
Returns a tuple of parameters used to build the expression.
- class neurolang.relational_algebra.FullOuterNaturalJoin(relation_left, relation_right)#
- classmethod apply(*args)#
Builds a new expression using a tuple of its parameters
- property type#
Get the expression’s type.
For parameterized classes (e.g., FunctionApplication[bool]), the type is stored as a class attribute under _nl_class_type. For non-parameterized classes, it is stored as an instance attribute under _type, defaulting to Unknown. Class-level access (e.g., PM.type for a class PM that inherits from PatternMatcher[int]) also works by reading _nl_class_type from the class hierarchy.
This property avoids instance-attribute shadowing: calling self.type = X in __init__ stores via the setter under _type, and change_type (which changes __class__ to a parameterized class) makes the type available from the class attribute — no need to pop anything from __dict__.
- unapply()#
Returns a tuple of parameters used to build the expression.
- class neurolang.relational_algebra.FunctionApplicationListMember(fun_exp, dst_column)#
Representation of a function application to a column. Can be used to represent either the application of an extended projection, or the application of an aggregate function, to a column.
- Attributes:
- fun_expUnion[Constant[str], FunctionApplication]
Constant string representation of an extended projection operation, or FunctionApplication representation of an aggregate function.
- dst_columnConstant[ColumnStr] or Symbol[ColumnStr]
Constant column string of the destination column.
Notes
In the case of an extended projection operation, as described in [1], a function application list member can either be:
a single attribute (column) name in the relation, resulting in a normal non-extended projection,
an expression x -> y where x and y are both attribute (column) names, x effectively being rename as y,
or an expression E -> z where E is an expression involving attributes of the relation, arithmetic operators, and string operators, and z is a new name for the attribute that results from the calculation implied by E. For example, a + b -> x represents the sum of the attributes a and b, renamed x.
[1]Garcia-Molina, Hector, Jeffrey D. Ullman, and Jennifer Widom. “Database systems: the complete book.” (2009).
- classmethod apply(*args)#
Builds a new expression using a tuple of its parameters
- property type#
Get the expression’s type.
For parameterized classes (e.g., FunctionApplication[bool]), the type is stored as a class attribute under _nl_class_type. For non-parameterized classes, it is stored as an instance attribute under _type, defaulting to Unknown. Class-level access (e.g., PM.type for a class PM that inherits from PatternMatcher[int]) also works by reading _nl_class_type from the class hierarchy.
This property avoids instance-attribute shadowing: calling self.type = X in __init__ stores via the setter under _type, and change_type (which changes __class__ to a parameterized class) makes the type available from the class attribute — no need to pop anything from __dict__.
- unapply()#
Returns a tuple of parameters used to build the expression.
- class neurolang.relational_algebra.GroupByAggregation(relation, groupby, aggregate_functions)#
General representation of a groupby operation with aggregate functions.
- Attributes:
- relationExpression[AbstractSet]
Relation on which the groupby is applied.
- groupbyTuple[Union[Constant[ColumnStr], Symbol[ColumnStr]]]
The list of columns on which to group.
- aggregate_functionsTuple[FunctionApplicationListMember]
List of aggregate functions to apply.
- classmethod apply(*args)#
Builds a new expression using a tuple of its parameters
- property type#
Get the expression’s type.
For parameterized classes (e.g., FunctionApplication[bool]), the type is stored as a class attribute under _nl_class_type. For non-parameterized classes, it is stored as an instance attribute under _type, defaulting to Unknown. Class-level access (e.g., PM.type for a class PM that inherits from PatternMatcher[int]) also works by reading _nl_class_type from the class hierarchy.
This property avoids instance-attribute shadowing: calling self.type = X in __init__ stores via the setter under _type, and change_type (which changes __class__ to a parameterized class) makes the type available from the class attribute — no need to pop anything from __dict__.
- unapply()#
Returns a tuple of parameters used to build the expression.
- class neurolang.relational_algebra.Intersection(relation_left, relation_right)#
- classmethod apply(*args)#
Builds a new expression using a tuple of its parameters
- property type#
Get the expression’s type.
For parameterized classes (e.g., FunctionApplication[bool]), the type is stored as a class attribute under _nl_class_type. For non-parameterized classes, it is stored as an instance attribute under _type, defaulting to Unknown. Class-level access (e.g., PM.type for a class PM that inherits from PatternMatcher[int]) also works by reading _nl_class_type from the class hierarchy.
This property avoids instance-attribute shadowing: calling self.type = X in __init__ stores via the setter under _type, and change_type (which changes __class__ to a parameterized class) makes the type available from the class attribute — no need to pop anything from __dict__.
- unapply()#
Returns a tuple of parameters used to build the expression.
- class neurolang.relational_algebra.LeftNaturalJoin(relation_left, relation_right)#
- classmethod apply(*args)#
Builds a new expression using a tuple of its parameters
- property type#
Get the expression’s type.
For parameterized classes (e.g., FunctionApplication[bool]), the type is stored as a class attribute under _nl_class_type. For non-parameterized classes, it is stored as an instance attribute under _type, defaulting to Unknown. Class-level access (e.g., PM.type for a class PM that inherits from PatternMatcher[int]) also works by reading _nl_class_type from the class hierarchy.
This property avoids instance-attribute shadowing: calling self.type = X in __init__ stores via the setter under _type, and change_type (which changes __class__ to a parameterized class) makes the type available from the class attribute — no need to pop anything from __dict__.
- unapply()#
Returns a tuple of parameters used to build the expression.
- class neurolang.relational_algebra.Materialise(relation)#
Mark a relation to be computed as a set when the plan containing it is evaluated, rather than while it is built. It evaluates to its relation. Each occurrence in a plan is evaluated: a backend may cache or persist the set at this node.
Planning walkers can keep it behind a symbol rather than walking into it, as for a constant set.
- classmethod apply(*args)#
Builds a new expression using a tuple of its parameters
- property type#
Get the expression’s type.
For parameterized classes (e.g., FunctionApplication[bool]), the type is stored as a class attribute under _nl_class_type. For non-parameterized classes, it is stored as an instance attribute under _type, defaulting to Unknown. Class-level access (e.g., PM.type for a class PM that inherits from PatternMatcher[int]) also works by reading _nl_class_type from the class hierarchy.
This property avoids instance-attribute shadowing: calling self.type = X in __init__ stores via the setter under _type, and change_type (which changes __class__ to a parameterized class) makes the type available from the class attribute — no need to pop anything from __dict__.
- unapply()#
Returns a tuple of parameters used to build the expression.
- class neurolang.relational_algebra.NAryRelationalAlgebraOperation#
- classmethod apply(*args)#
Builds a new expression using a tuple of its parameters
- property type#
Get the expression’s type.
For parameterized classes (e.g., FunctionApplication[bool]), the type is stored as a class attribute under _nl_class_type. For non-parameterized classes, it is stored as an instance attribute under _type, defaulting to Unknown. Class-level access (e.g., PM.type for a class PM that inherits from PatternMatcher[int]) also works by reading _nl_class_type from the class hierarchy.
This property avoids instance-attribute shadowing: calling self.type = X in __init__ stores via the setter under _type, and change_type (which changes __class__ to a parameterized class) makes the type available from the class attribute — no need to pop anything from __dict__.
- unapply()#
Returns a tuple of parameters used to build the expression.
- class neurolang.relational_algebra.NameColumns(relation, column_names)#
Give names to the columns of a relational algebra set.
All columns must be named at once. Each column name must either be a Constant[ColumnStr] or a Symbol[ColumnStr] pointing to a symbolic column name resolved when the expression is compiled.
- classmethod apply(*args)#
Builds a new expression using a tuple of its parameters
- property type#
Get the expression’s type.
For parameterized classes (e.g., FunctionApplication[bool]), the type is stored as a class attribute under _nl_class_type. For non-parameterized classes, it is stored as an instance attribute under _type, defaulting to Unknown. Class-level access (e.g., PM.type for a class PM that inherits from PatternMatcher[int]) also works by reading _nl_class_type from the class hierarchy.
This property avoids instance-attribute shadowing: calling self.type = X in __init__ stores via the setter under _type, and change_type (which changes __class__ to a parameterized class) makes the type available from the class attribute — no need to pop anything from __dict__.
- unapply()#
Returns a tuple of parameters used to build the expression.
- class neurolang.relational_algebra.NamedRelationalAlgebraFrozenSet(columns, iterable=None)#
- isdisjoint(other)#
Return True if two sets have a null intersection.
- property might_have_duplicates#
Sets the flag manually on whether the set might have duplicates. Careful, if you set it to False and it’s not, then the semantics of the queries will break.
- Returns:
- bool
returns whether the set might have duplicates.
- projection(*columns)#
Projects the set on the given list of columns.
Examples
>>> ras = RelationalAlgebraFrozenSet( [(i % 2, i, i * 2) for i in range(5)]) >>> ras 0 1 2 0 0 0 0 1 1 1 2 2 0 2 4 3 1 3 6 4 0 4 8 >>> ras.projection(1) 0 0 0 1 1 2 2 3 3 4 4
- row_count_estimate()#
Return the number of rows of the container.
Duplicates are not dropped, so this is an upper bound of
len(self)obtained without any computation.- Returns:
- int
Number of rows of the underlying
pandas.DataFrame.
- selection(select_criteria)#
Select the elements based on the given selet_criteria. The select_criteria may be a callable function, a string expression or a Dict of columns -> value
- Parameters:
- select_criteriaUnion[Callable, RelationalAlgebraStringExpression,
- Dict[int, Union[int, Callable]]]
selection criteria
- Returns:
- RelationalAlgebraFrozenSet
A RelationalAlgebraFrozenSet with elements matching the criteria.
- Parameters:
select_criteria (Callable | RelationalAlgebraStringExpression | Dict[int, int | Callable])
- selection_columns(select_criteria)#
Select the elements where column pairs indicated as key, value items of the select_criteria are equal. The select_criteria must be a Dict of Int -> Int.
- Parameters:
- select_criteriaDict [Int, Int]
selection criteria
- Returns:
- RelationalAlgebraFrozenSet
A RelationalAlgebraFrozenSet with elements matching the criteria.
- Parameters:
Examples
Select the elements where col0 == col1 and col1 == col2
>>> ras = RelationalAlgebraFrozenSet( ... [(i % 2, i, i * 2) for i in range(5)]) >>> ras.selection_columns({0:1, 1: 2}) 0 1 2 0 0 0 0
- class neurolang.relational_algebra.NaryNaturalJoin(relations)#
N-ary natural join holding N operands in a flat tuple.
Unlike a left-deep tree of NaturalJoin nodes, this preserves the full set of operands so that join-reordering optimisers can reorder them globally without rebuilding pairwise trees.
- classmethod apply(*args)#
Builds a new expression using a tuple of its parameters
- property type#
Get the expression’s type.
For parameterized classes (e.g., FunctionApplication[bool]), the type is stored as a class attribute under _nl_class_type. For non-parameterized classes, it is stored as an instance attribute under _type, defaulting to Unknown. Class-level access (e.g., PM.type for a class PM that inherits from PatternMatcher[int]) also works by reading _nl_class_type from the class hierarchy.
This property avoids instance-attribute shadowing: calling self.type = X in __init__ stores via the setter under _type, and change_type (which changes __class__ to a parameterized class) makes the type available from the class attribute — no need to pop anything from __dict__.
- unapply()#
Returns a tuple of parameters used to build the expression.
- class neurolang.relational_algebra.NaturalJoin(relation_left, relation_right)#
- classmethod apply(*args)#
Builds a new expression using a tuple of its parameters
- property type#
Get the expression’s type.
For parameterized classes (e.g., FunctionApplication[bool]), the type is stored as a class attribute under _nl_class_type. For non-parameterized classes, it is stored as an instance attribute under _type, defaulting to Unknown. Class-level access (e.g., PM.type for a class PM that inherits from PatternMatcher[int]) also works by reading _nl_class_type from the class hierarchy.
This property avoids instance-attribute shadowing: calling self.type = X in __init__ stores via the setter under _type, and change_type (which changes __class__ to a parameterized class) makes the type available from the class attribute — no need to pop anything from __dict__.
- unapply()#
Returns a tuple of parameters used to build the expression.
- class neurolang.relational_algebra.NumberColumns(relation, column_names)#
Converts a named relational algebra set to an unnamed one
All columns must be numbered at once. Each column name must either be a Constant[ColumnStr] and the numbering will be the ordering in the column_names attribute.
- classmethod apply(*args)#
Builds a new expression using a tuple of its parameters
- property type#
Get the expression’s type.
For parameterized classes (e.g., FunctionApplication[bool]), the type is stored as a class attribute under _nl_class_type. For non-parameterized classes, it is stored as an instance attribute under _type, defaulting to Unknown. Class-level access (e.g., PM.type for a class PM that inherits from PatternMatcher[int]) also works by reading _nl_class_type from the class hierarchy.
This property avoids instance-attribute shadowing: calling self.type = X in __init__ stores via the setter under _type, and change_type (which changes __class__ to a parameterized class) makes the type available from the class attribute — no need to pop anything from __dict__.
- unapply()#
Returns a tuple of parameters used to build the expression.
- class neurolang.relational_algebra.Product(relations)#
- classmethod apply(*args)#
Builds a new expression using a tuple of its parameters
- property type#
Get the expression’s type.
For parameterized classes (e.g., FunctionApplication[bool]), the type is stored as a class attribute under _nl_class_type. For non-parameterized classes, it is stored as an instance attribute under _type, defaulting to Unknown. Class-level access (e.g., PM.type for a class PM that inherits from PatternMatcher[int]) also works by reading _nl_class_type from the class hierarchy.
This property avoids instance-attribute shadowing: calling self.type = X in __init__ stores via the setter under _type, and change_type (which changes __class__ to a parameterized class) makes the type available from the class attribute — no need to pop anything from __dict__.
- unapply()#
Returns a tuple of parameters used to build the expression.
- class neurolang.relational_algebra.Projection(relation, attributes)#
- classmethod apply(*args)#
Builds a new expression using a tuple of its parameters
- property type#
Get the expression’s type.
For parameterized classes (e.g., FunctionApplication[bool]), the type is stored as a class attribute under _nl_class_type. For non-parameterized classes, it is stored as an instance attribute under _type, defaulting to Unknown. Class-level access (e.g., PM.type for a class PM that inherits from PatternMatcher[int]) also works by reading _nl_class_type from the class hierarchy.
This property avoids instance-attribute shadowing: calling self.type = X in __init__ stores via the setter under _type, and change_type (which changes __class__ to a parameterized class) makes the type available from the class attribute — no need to pop anything from __dict__.
- unapply()#
Returns a tuple of parameters used to build the expression.
- class neurolang.relational_algebra.PushInSelections(*args, **kwargs)#
- match(expression)#
Find the action for a given expression by going through the
patterns.Goes through the triplets in in
patternsand calls the action specified by the first satisfied triplet.
- pattern_match(pattern, expression)#
Return
Trueifpatternmatchesexpression.Patterns are of the following form:
...matches everything.Expressionclass matches ifexpressionis instance of the given class for anytype.Expression[T]matches ifexpressionis instance of the given class withexpression.typea subtype ofT.Expression[T](Pattern_0, Pattern_1, ....., Pattern_N)matches ifexpressionis instance of the given class withexpression.typea subtype ofTand recursively matches the construction parameters of the instance on the types given asPattern_0,Pattern_1, etc.(Pattern_0, ....., Pattern_N)matches when expression is atupleof length N + 1 whereexpression[0]matchesPattern_0,expression[1]matchesPattern_1, etc.instancean instance of a python class not subclassingExpressionmatches wheninstance == expression
- property patterns#
Property holding an iterator of triplets
(pattern, guard, action).pattern: is an Expression class, or instance where construction parameters and the type can be replaced by an ellipsis … to signal a wildcard. Seepattern_match()for more details.guard: is a function mapping an Expression instance to a boolean orNone.action: is the method receiving the matchingexpressioninstance to be executed upon pattern and match beingTrue.
- class neurolang.relational_algebra.RelationalAlgebraColumnInt#
- class neurolang.relational_algebra.RelationalAlgebraColumnStr#
- class neurolang.relational_algebra.RelationalAlgebraOperation#
- classmethod apply(*args)#
Builds a new expression using a tuple of its parameters
- property type#
Get the expression’s type.
For parameterized classes (e.g., FunctionApplication[bool]), the type is stored as a class attribute under _nl_class_type. For non-parameterized classes, it is stored as an instance attribute under _type, defaulting to Unknown. Class-level access (e.g., PM.type for a class PM that inherits from PatternMatcher[int]) also works by reading _nl_class_type from the class hierarchy.
This property avoids instance-attribute shadowing: calling self.type = X in __init__ stores via the setter under _type, and change_type (which changes __class__ to a parameterized class) makes the type available from the class attribute — no need to pop anything from __dict__.
- unapply()#
Returns a tuple of parameters used to build the expression.
- class neurolang.relational_algebra.RelationalAlgebraOptimiser(*args, **kwargs)#
Optimise relational algebra expressions by rewriting them.
Rewrites equi-selections over products into equijoins, pushes selections and projections down, simplifies renames and products, and reorders joins.
- Pipeline order within each convergence pass:
Selection pushdown, projection elimination, rename and product simplification.
PushProjectionsDown — push projections through NaryNaturalJoin operands to reduce join sizes.
FlattenAndReorderJoins — flatten join trees into an NaryNaturalJoin and greedily reorder them to avoid cross products.
- match(expression)#
Find the action for a given expression by going through the
patterns.Goes through the triplets in in
patternsand calls the action specified by the first satisfied triplet.
- pattern_match(pattern, expression)#
Return
Trueifpatternmatchesexpression.Patterns are of the following form:
...matches everything.Expressionclass matches ifexpressionis instance of the given class for anytype.Expression[T]matches ifexpressionis instance of the given class withexpression.typea subtype ofT.Expression[T](Pattern_0, Pattern_1, ....., Pattern_N)matches ifexpressionis instance of the given class withexpression.typea subtype ofTand recursively matches the construction parameters of the instance on the types given asPattern_0,Pattern_1, etc.(Pattern_0, ....., Pattern_N)matches when expression is atupleof length N + 1 whereexpression[0]matchesPattern_0,expression[1]matchesPattern_1, etc.instancean instance of a python class not subclassingExpressionmatches wheninstance == expression
- property patterns#
Property holding an iterator of triplets
(pattern, guard, action).pattern: is an Expression class, or instance where construction parameters and the type can be replaced by an ellipsis … to signal a wildcard. Seepattern_match()for more details.guard: is a function mapping an Expression instance to a boolean orNone.action: is the method receiving the matchingexpressioninstance to be executed upon pattern and match beingTrue.
- class neurolang.relational_algebra.RelationalAlgebraSet(iterable=None)#
- add(value)#
Add an element.
- clear()#
This is slow (creates N new iterators!) but effective.
- discard(value)#
Remove an element. Do not raise an exception if absent.
- isdisjoint(other)#
Return True if two sets have a null intersection.
- property might_have_duplicates#
Sets the flag manually on whether the set might have duplicates. Careful, if you set it to False and it’s not, then the semantics of the queries will break.
- Returns:
- bool
returns whether the set might have duplicates.
- pop()#
Return the popped value. Raise KeyError if empty.
- projection(*columns)#
Projects the set on the given list of columns.
Examples
>>> ras = RelationalAlgebraFrozenSet( [(i % 2, i, i * 2) for i in range(5)]) >>> ras 0 1 2 0 0 0 0 1 1 1 2 2 0 2 4 3 1 3 6 4 0 4 8 >>> ras.projection(1) 0 0 0 1 1 2 2 3 3 4 4
- remove(value)#
Remove an element. If not a member, raise a KeyError.
- row_count_estimate()#
Return the number of rows of the container.
Duplicates are not dropped, so this is an upper bound of
len(self)obtained without any computation.- Returns:
- int
Number of rows of the underlying
pandas.DataFrame.
- selection(select_criteria)#
Select the elements based on the given selet_criteria. The select_criteria may be a callable function, a string expression or a Dict of columns -> value
- Parameters:
- select_criteriaUnion[Callable, RelationalAlgebraStringExpression,
- Dict[int, Union[int, Callable]]]
selection criteria
- Returns:
- RelationalAlgebraFrozenSet
A RelationalAlgebraFrozenSet with elements matching the criteria.
- Parameters:
select_criteria (Callable | RelationalAlgebraStringExpression | Dict[int, int | Callable])
- selection_columns(select_criteria)#
Select the elements where column pairs indicated as key, value items of the select_criteria are equal. The select_criteria must be a Dict of Int -> Int.
- Parameters:
- select_criteriaDict [Int, Int]
selection criteria
- Returns:
- RelationalAlgebraFrozenSet
A RelationalAlgebraFrozenSet with elements matching the criteria.
- Parameters:
Examples
Select the elements where col0 == col1 and col1 == col2
>>> ras = RelationalAlgebraFrozenSet( ... [(i % 2, i, i * 2) for i in range(5)]) >>> ras.selection_columns({0:1, 1: 2}) 0 1 2 0 0 0 0
- class neurolang.relational_algebra.RelationalAlgebraSolver(symbol_table=None)#
Mixing that walks through relational algebra expressions and executes the operations.
Relations are expected to be represented as objects with the same interface as
RelationalAlgebraSet.- match(expression)#
Find the action for a given expression by going through the
patterns.Goes through the triplets in in
patternsand calls the action specified by the first satisfied triplet.
- pattern_match(pattern, expression)#
Return
Trueifpatternmatchesexpression.Patterns are of the following form:
...matches everything.Expressionclass matches ifexpressionis instance of the given class for anytype.Expression[T]matches ifexpressionis instance of the given class withexpression.typea subtype ofT.Expression[T](Pattern_0, Pattern_1, ....., Pattern_N)matches ifexpressionis instance of the given class withexpression.typea subtype ofTand recursively matches the construction parameters of the instance on the types given asPattern_0,Pattern_1, etc.(Pattern_0, ....., Pattern_N)matches when expression is atupleof length N + 1 whereexpression[0]matchesPattern_0,expression[1]matchesPattern_1, etc.instancean instance of a python class not subclassingExpressionmatches wheninstance == expression
- property patterns#
Property holding an iterator of triplets
(pattern, guard, action).pattern: is an Expression class, or instance where construction parameters and the type can be replaced by an ellipsis … to signal a wildcard. Seepattern_match()for more details.guard: is a function mapping an Expression instance to a boolean orNone.action: is the method receiving the matchingexpressioninstance to be executed upon pattern and match beingTrue.
- class neurolang.relational_algebra.RelationalAlgebraStringExpression#
- class neurolang.relational_algebra.RenameColumn(relation, src, dst)#
- classmethod apply(*args)#
Builds a new expression using a tuple of its parameters
- property type#
Get the expression’s type.
For parameterized classes (e.g., FunctionApplication[bool]), the type is stored as a class attribute under _nl_class_type. For non-parameterized classes, it is stored as an instance attribute under _type, defaulting to Unknown. Class-level access (e.g., PM.type for a class PM that inherits from PatternMatcher[int]) also works by reading _nl_class_type from the class hierarchy.
This property avoids instance-attribute shadowing: calling self.type = X in __init__ stores via the setter under _type, and change_type (which changes __class__ to a parameterized class) makes the type available from the class attribute — no need to pop anything from __dict__.
- unapply()#
Returns a tuple of parameters used to build the expression.
- class neurolang.relational_algebra.RenameColumns(relation, renames)#
Convenient operation for renaming multiple columns at the same time.
- Attributes:
- relationNamedRelationalAlgebraFrozenSet
The relation whose columns shall be renamed.
- renamestuple of pairs of Constant[ColumnStr] or Symbol[ColumnStr]
The renamings that should happen, represented as tuples (src, dst).
- classmethod apply(*args)#
Builds a new expression using a tuple of its parameters
- property type#
Get the expression’s type.
For parameterized classes (e.g., FunctionApplication[bool]), the type is stored as a class attribute under _nl_class_type. For non-parameterized classes, it is stored as an instance attribute under _type, defaulting to Unknown. Class-level access (e.g., PM.type for a class PM that inherits from PatternMatcher[int]) also works by reading _nl_class_type from the class hierarchy.
This property avoids instance-attribute shadowing: calling self.type = X in __init__ stores via the setter under _type, and change_type (which changes __class__ to a parameterized class) makes the type available from the class attribute — no need to pop anything from __dict__.
- unapply()#
Returns a tuple of parameters used to build the expression.
- class neurolang.relational_algebra.RenameOptimizations(*args, **kwargs)#
- match(expression)#
Find the action for a given expression by going through the
patterns.Goes through the triplets in in
patternsand calls the action specified by the first satisfied triplet.
- pattern_match(pattern, expression)#
Return
Trueifpatternmatchesexpression.Patterns are of the following form:
...matches everything.Expressionclass matches ifexpressionis instance of the given class for anytype.Expression[T]matches ifexpressionis instance of the given class withexpression.typea subtype ofT.Expression[T](Pattern_0, Pattern_1, ....., Pattern_N)matches ifexpressionis instance of the given class withexpression.typea subtype ofTand recursively matches the construction parameters of the instance on the types given asPattern_0,Pattern_1, etc.(Pattern_0, ....., Pattern_N)matches when expression is atupleof length N + 1 whereexpression[0]matchesPattern_0,expression[1]matchesPattern_1, etc.instancean instance of a python class not subclassingExpressionmatches wheninstance == expression
- property patterns#
Property holding an iterator of triplets
(pattern, guard, action).pattern: is an Expression class, or instance where construction parameters and the type can be replaced by an ellipsis … to signal a wildcard. Seepattern_match()for more details.guard: is a function mapping an Expression instance to a boolean orNone.action: is the method receiving the matchingexpressioninstance to be executed upon pattern and match beingTrue.
- class neurolang.relational_algebra.ReplaceNull(relation, column, value)#
- classmethod apply(*args)#
Builds a new expression using a tuple of its parameters
- property type#
Get the expression’s type.
For parameterized classes (e.g., FunctionApplication[bool]), the type is stored as a class attribute under _nl_class_type. For non-parameterized classes, it is stored as an instance attribute under _type, defaulting to Unknown. Class-level access (e.g., PM.type for a class PM that inherits from PatternMatcher[int]) also works by reading _nl_class_type from the class hierarchy.
This property avoids instance-attribute shadowing: calling self.type = X in __init__ stores via the setter under _type, and change_type (which changes __class__ to a parameterized class) makes the type available from the class attribute — no need to pop anything from __dict__.
- unapply()#
Returns a tuple of parameters used to build the expression.
- class neurolang.relational_algebra.Selection(relation, formula)#
- classmethod apply(*args)#
Builds a new expression using a tuple of its parameters
- property type#
Get the expression’s type.
For parameterized classes (e.g., FunctionApplication[bool]), the type is stored as a class attribute under _nl_class_type. For non-parameterized classes, it is stored as an instance attribute under _type, defaulting to Unknown. Class-level access (e.g., PM.type for a class PM that inherits from PatternMatcher[int]) also works by reading _nl_class_type from the class hierarchy.
This property avoids instance-attribute shadowing: calling self.type = X in __init__ stores via the setter under _type, and change_type (which changes __class__ to a parameterized class) makes the type available from the class attribute — no need to pop anything from __dict__.
- unapply()#
Returns a tuple of parameters used to build the expression.
- class neurolang.relational_algebra.SimplifyExtendedProjectionsWithConstants(*args, **kwargs)#
- match(expression)#
Find the action for a given expression by going through the
patterns.Goes through the triplets in in
patternsand calls the action specified by the first satisfied triplet.
- pattern_match(pattern, expression)#
Return
Trueifpatternmatchesexpression.Patterns are of the following form:
...matches everything.Expressionclass matches ifexpressionis instance of the given class for anytype.Expression[T]matches ifexpressionis instance of the given class withexpression.typea subtype ofT.Expression[T](Pattern_0, Pattern_1, ....., Pattern_N)matches ifexpressionis instance of the given class withexpression.typea subtype ofTand recursively matches the construction parameters of the instance on the types given asPattern_0,Pattern_1, etc.(Pattern_0, ....., Pattern_N)matches when expression is atupleof length N + 1 whereexpression[0]matchesPattern_0,expression[1]matchesPattern_1, etc.instancean instance of a python class not subclassingExpressionmatches wheninstance == expression
- property patterns#
Property holding an iterator of triplets
(pattern, guard, action).pattern: is an Expression class, or instance where construction parameters and the type can be replaced by an ellipsis … to signal a wildcard. Seepattern_match()for more details.guard: is a function mapping an Expression instance to a boolean orNone.action: is the method receiving the matchingexpressioninstance to be executed upon pattern and match beingTrue.
- class neurolang.relational_algebra.UnaryRelationalAlgebraOperation#
- classmethod apply(*args)#
Builds a new expression using a tuple of its parameters
- property type#
Get the expression’s type.
For parameterized classes (e.g., FunctionApplication[bool]), the type is stored as a class attribute under _nl_class_type. For non-parameterized classes, it is stored as an instance attribute under _type, defaulting to Unknown. Class-level access (e.g., PM.type for a class PM that inherits from PatternMatcher[int]) also works by reading _nl_class_type from the class hierarchy.
This property avoids instance-attribute shadowing: calling self.type = X in __init__ stores via the setter under _type, and change_type (which changes __class__ to a parameterized class) makes the type available from the class attribute — no need to pop anything from __dict__.
- unapply()#
Returns a tuple of parameters used to build the expression.
- class neurolang.relational_algebra.Union(relation_left, relation_right)#
- classmethod apply(*args)#
Builds a new expression using a tuple of its parameters
- property type#
Get the expression’s type.
For parameterized classes (e.g., FunctionApplication[bool]), the type is stored as a class attribute under _nl_class_type. For non-parameterized classes, it is stored as an instance attribute under _type, defaulting to Unknown. Class-level access (e.g., PM.type for a class PM that inherits from PatternMatcher[int]) also works by reading _nl_class_type from the class hierarchy.
This property avoids instance-attribute shadowing: calling self.type = X in __init__ stores via the setter under _type, and change_type (which changes __class__ to a parameterized class) makes the type available from the class attribute — no need to pop anything from __dict__.
- unapply()#
Returns a tuple of parameters used to build the expression.