neurolang.frontend.query_resolution_datalog module

Query Builder Datalog

Complements QueryBuilderBase with query capabilities, as well as Region and Neurosynth capabilities

class neurolang.frontend.query_resolution_datalog.QueryBuilderDatalog(program_ir: ~neurolang.datalog.basic_representation.DatalogProgram, chase_class: ~typing.Type[~neurolang.datalog.chase.Chase] = <class 'neurolang.datalog.chase.Chase'>)

Bases: RegionMixin, NeuroSynthMixin, QueryBuilderBase

Complements QueryBuilderBase with query capabilities, as well as Region and Neurosynth capabilities

Attributes:
current_program

Returns the list of expressions that have currently been

environment

Dynamic context that can be used to create symbols to write a Datalog program.

functions

Returns the list of symbols corresponding to callables

region_names

Returns the list of symbol names with Region type

region_set_names

Returns the list of symbol names with set_type

scope

Dynamic context that can be used to create symbols to write a Datalog program.

symbol_table

Projector to the program_ir’s symbol_table

symbols

Iterator through the symbol’s names

types

Returns a list of the types of the symbols currently

Methods

add_atlas_set(name, atlas_labels, spatial_image)

Creates an atlas set: 1- for each region specified by a label and name in atlas_labels, creates associated ExplicitVBR and symbols Tuple[region_name: str, Region] 2- groups regions in an AbstractSet[Tuple[str, Region]] symbol with specified name

add_constraint(antecedent, consequent)

Creates an right implication of the consequent by the antecedent and adds the rule to the current program: antecedent -> consequent

add_region(region[, name])

Adds region fe.Symbol to symbol_table

add_region_set(region_set[, name])

Creates an AbstractSet fe.Symbol containing the elements specified in the iterable with a List[Tuple[Region]] format

add_symbol(value[, name, type_])

Creates a symbol with given value and adds it to the current symbol_table.

add_tuple_set(iterable[, type_, name])

Creates an AbstractSet fe.Symbol containing the elements specified in the iterable with a List[Tuple[Any, ...]] format (see examples).

all(quantified_variable, body)

Universal predicate on the body.

create_region(spatial_image[, label, ...])

Creates an ExplicitVBR out of the voxels of a dense spatial_image with specified label

del_symbol(name)

Deletes the symbol with parameter name from the symbol_table

execute_datalog_program(code)

Execute a Datalog program in classical syntax.

exists(quantified_variable, body)

Existential predicate on the body.

get_symbol(symbol_name)

Retrieves symbol via its name, either providing a fe.Expression with the correct name or the name itself

load_neurosynth_mni_peaks_reported(data_dir)

Load the coordinates for the peaks reported by studies in the Neurosynth dataset.

load_neurosynth_study_ids(data_dir[, name, ...])

Load all study ids (PMIDs) that are part of the Neurosynth database.

load_neurosynth_term_study_associations(data_dir)

Load TF-IDF values for each (term, study) association within the Neurosynth database.

make_implicit_regions_explicit(affine, dim)

Raises NotImplementedError for now

new_region_symbol([name])

Returns symbol with type Region

new_symbol([type_, name])

Creates a symbol and associated expression, optionally specifying it's type and/or name

predicate_parameter_names(predicate_name)

Get the names of the parameters for the given predicate

query(*args)

Performs an inferential query on the database.

reset_program()

Clears current symbol table

solve_all()

Returns a dictionary of "predicate_name": "Content" for all elements in the solution of the Datalog program.

sphere(center, radius[, name])

Creates a Region symbol associated with the spherical volume described by its center and volume

magic_sets_rewrite_program

add_constraint(antecedent: Expression, consequent: Expression) Expression

Creates an right implication of the consequent by the antecedent and adds the rule to the current program:

antecedent -> consequent

Parameters:
antecedentfe.Expression

see description, will be processed to a logic form before creating the right implication rule

consequentfe.Expression

see description, will be processed to a logic form before creating the right implication rule

Returns:
fe.Expression

see description

add_tuple_set(iterable: ~typing.Iterable, type_: ~typing.Type = <class 'neurolang.type_system.Unknown'>, name: str | None = None) Symbol

Creates an AbstractSet fe.Symbol containing the elements specified in the iterable with a List[Tuple[Any, …]] format (see examples). Typically used to crate extensional facts from existing databases

Parameters:
iterableIterable

typically a list of tuples of values, other formats will be interpreted as the latter

type_Type, optional

type of elements for the tuples, if not specified will be inferred from the first element, by default Unknown

namestr, optional

name for the AbstractSet symbol, by default None

Returns:
fe.Symbol

see description

Examples

>>> p_ir = DatalogProgram()
>>> nl = QueryBuilderDatalog(program_ir=p_ir)
>>> nl.add_tuple_set([(1, 2), (3, 4)], name="l1")
l1: typing.AbstractSet[typing.Tuple[int, int]] =             [(1, 2), (3, 4)]
>>> nl.add_tuple_set([[1, 2, 3], (3, 4)], name="l2")
l2: typing.AbstractSet[typing.Tuple[int, int, float]] =             [(1, 2, 3.0), (3, 4, nan)]
>>> nl.add_tuple_set((1, 2, 3), name="l3")
l3: typing.AbstractSet[typing.Tuple[int]] =             [(1,), (2,), (3,)]
property current_program: List[Expression]

Returns the list of expressions that have currently been declared in the program

Returns:
List[fe.Expression]

see description

execute_datalog_program(code: str) None | bool | RelationalAlgebraFrozenSet

Execute a Datalog program in classical syntax. If the program contains a query, in the form ans(x) :- R(x), then this query is executed against the program and the result is returned. Otherwise returns None.

Parameters:
codestring

Datalog program.

Examples

>>> p_ir = DatalogProgram()
>>> nl = QueryBuilderDatalog(program_ir=p_ir)
>>> nl.add_tuple_set([(1, 2), (2, 2)], name="l")
l: typing.AbstractSet[typing.Tuple[int, int]] = [(1, 2), (2, 2)]
>>> prog = '''
...        l2(x, y) :- l(x, y), (x == y)
...        ans(x) :- l2(x, y)
...        '''
>>> with nl.environment as e:
...     q = nl.execute_datalog_program(prog)
>>> q
... q: typing.AbstractSet[typing.Tuple[int]] = [(2,)]
magic_sets_rewrite_program(query_expression)
predicate_parameter_names(predicate_name: str | Symbol | Expression) Tuple[str]

Get the names of the parameters for the given predicate

Parameters:
predicate_nameUnion[str, fe.Symbol, fe.Expression]

predicate to obtain the names from

Returns:
tuple[str]

parameter names

query(*args) bool | RelationalAlgebraFrozenSet | Symbol

Performs an inferential query on the database. There are three modalities 1. If there is only one argument, the query returns True or False depending on wether the query could be inferred. 2. If there are two arguments and the first is a tuple of fe.Symbol, it returns the set of results meeting the query in the second argument. 3. If the first argument is a predicate (e.g. Q(x)) it performs the query, adds it to the engine memory, and returns the corresponding symbol. See example for 3 modalities

Returns:
Union[bool, RelationalAlgebraFrozenSet, fe.Symbol]

read the descrpition.

reset_program() None

Clears current symbol table

solve_all() Dict[str, NamedRelationalAlgebraFrozenSet]

Returns a dictionary of “predicate_name”: “Content” for all elements in the solution of the Datalog program.

Returns:
Dict[str, NamedRelationalAlgebraFrozenSet]

extensional and intentional facts that have been derived through the current program