pyreason

Package Contents

Classes

Output

Filter

Program

GraphmlParser

Fact

Rule

Example text:

Threshold

A class representing a threshold for a clause in a rule.

Functions

reset()

Resets certain variables to None to be able to do pr.reason() multiple times in a program

reset_rules()

Resets rules to none

load_graphml(→ None)

Loads graph from GraphMl file path into program

load_graph(→ None)

Load a networkx DiGraph into pyreason

load_inconsistent_predicate_list(→ None)

Load IPL from YAML file path into program

add_rule(→ None)

Add a rule to pyreason from text format. This format is not as modular as the YAML format.

add_rules_from_file(→ None)

Add a set of rules from a text file

add_fact(→ None)

Add a PyReason fact to the program.

add_annotation_function(→ None)

Function to add annotation functions to PyReason. The added functions can be used in rules

reason([timesteps, convergence_threshold, ...])

Function to start the main reasoning process. Graph and rules must already be loaded.

save_rule_trace(interpretation[, folder])

Saves the trace of the program. This includes every change that has occurred to the interpretation. If atom_trace was set to true

get_rule_trace(→ Tuple[pandas.DataFrame, pandas.DataFrame])

Returns the trace of the program as 2 pandas dataframes (one for nodes, one for edges).

filter_and_sort_nodes(interpretation, labels[, bound, ...])

Filters and sorts the node changes in the interpretation and returns as a list of Pandas dataframes that are easy to access

filter_and_sort_edges(interpretation, labels[, bound, ...])

Filters and sorts the edge changes in the interpretation and returns as a list of Pandas dataframes that are easy to access

Attributes

package_path

cache_path

cache_status_path

settings

cache_status

graph_path

package_path[source]
cache_path[source]
cache_status_path[source]
class Output(timestamp)[source]
save_rule_trace(interpretation, folder='./')[source]
get_rule_trace(interpretation)[source]
class Filter(tmax)[source]
filter_and_sort_nodes(interpretation, labels, bound, sort_by='lower', descending=True)[source]
filter_and_sort_edges(interpretation, labels, bound, sort_by='lower', descending=True)[source]
class Program(graph, facts_node, facts_edge, rules, ipl, annotation_functions, reverse_graph, atom_trace, save_graph_attributes_to_rule_trace, canonical, inconsistency_check, store_interpretation_changes, parallel_computing, update_mode)[source]
available_labels_node = '[]'
available_labels_edge = '[]'
specific_node_labels = '[]'
specific_edge_labels = '[]'
reason(tmax, convergence_threshold, convergence_bound_threshold, verbose=True)[source]
reason_again(tmax, convergence_threshold, convergence_bound_threshold, facts_node, facts_edge, verbose=True)[source]
class GraphmlParser[source]
parse_graph(graph_path, reverse)[source]
load_graph(graph)[source]
parse_graph_attributes(static_facts)[source]
class Fact(name: str, component: str | Tuple[str, str], attribute: str, bound: pyreason.scripts.numba_wrapper.numba_types.interval_type.Interval | List[float], start_time: int, end_time: int, static: bool = False)[source]
class Rule(rule_text: str, name: str, infer_edges: bool = False, set_static: bool = False, immediate_rule: bool = False, custom_thresholds=None)[source]
Example text:

‘pred1(x,y) : [0.2, 1] <- pred2(a, b) : [1,1], pred3(b, c)’

1. It is not possible to have weights for different clauses. Weights are 1 by default with bias 0 TODO: Add weights as a parameter

class Threshold(quantifier, quantifier_type, thresh)[source]

A class representing a threshold for a clause in a rule.

quantifier

The comparison operator, e.g., ‘greater_equal’, ‘less’, etc.

Type:

str

quantifier_type

A tuple indicating the type of quantifier, e.g., (‘number’, ‘total’).

Type:

tuple

thresh

The numerical threshold value to compare against.

Type:

int

to_tuple()[source]

Converts the Threshold instance into a tuple compatible with numba types.

to_tuple()[source]

Converts the Threshold instance into a tuple compatible with numba types.

Returns:

A tuple representation of the Threshold instance.

Return type:

tuple

settings[source]
reset()[source]

Resets certain variables to None to be able to do pr.reason() multiple times in a program without memory blowing up

reset_rules()[source]

Resets rules to none

load_graphml(path: str) None[source]

Loads graph from GraphMl file path into program

Parameters:

path – Path for the GraphMl file

load_graph(graph: networkx.DiGraph) None[source]

Load a networkx DiGraph into pyreason

Parameters:

graph (nx.DiGraph) – Networkx DiGraph object to load into pyreason

Returns:

None

load_inconsistent_predicate_list(path: str) None[source]

Load IPL from YAML file path into program

Parameters:

path – Path for the YAML IPL file

add_rule(pr_rule: pyreason.scripts.rules.rule.Rule) None[source]

Add a rule to pyreason from text format. This format is not as modular as the YAML format.

add_rules_from_file(file_path: str, infer_edges: bool = False) None[source]

Add a set of rules from a text file

Parameters:
  • file_path (str) – Path to the text file containing rules

  • infer_edges (bool) – Whether to infer edges on these rules if an edge doesn’t exist between head variables and the body of the rule is satisfied

Returns:

None

add_fact(pyreason_fact: pyreason.scripts.facts.fact.Fact) None[source]

Add a PyReason fact to the program.

Parameters:

pyreason_fact – PyReason fact created using pr.Fact(…)

Returns:

None

add_annotation_function(function: Callable) None[source]

Function to add annotation functions to PyReason. The added functions can be used in rules

Parameters:

function (Callable) – Function to be added. This has to be under a numba njit decorator. function has signature: two parameters as input – annotations, weights

Returns:

None

reason(timesteps: int = -1, convergence_threshold: int = -1, convergence_bound_threshold: float = -1, again: bool = False, node_facts: List[Type[pyreason.scripts.numba_wrapper.numba_types.fact_node_type.Fact]] = None, edge_facts: List[Type[pyreason.scripts.numba_wrapper.numba_types.fact_edge_type.Fact]] = None)[source]

Function to start the main reasoning process. Graph and rules must already be loaded.

Parameters:
  • timesteps – Max number of timesteps to run. -1 specifies run till convergence. If reasoning again, this is the number of timesteps to reason for extra (no zero timestep), defaults to -1

  • convergence_threshold – Maximim number of interpretations that have changed between timesteps or fixed point operations until considered convergent. Program will end at convergence. -1 => no changes, perfect convergence, defaults to -1

  • convergence_bound_threshold – Maximum change in any interpretation (bounds) between timesteps or fixed point operations until considered convergent, defaults to -1

  • again – Whether to reason again on an existing interpretation, defaults to False

  • node_facts – New node facts to use during the next reasoning process. Other facts from file will be discarded, defaults to None

  • edge_facts – New edge facts to use during the next reasoning process. Other facts from file will be discarded, defaults to None

Returns:

The final interpretation after reasoning.

save_rule_trace(interpretation, folder: str = './')[source]

Saves the trace of the program. This includes every change that has occurred to the interpretation. If atom_trace was set to true this gives us full explainability of why interpretations changed

Parameters:
  • interpretation – the output of pyreason.reason(), the final interpretation

  • folder – the folder in which to save the result, defaults to ‘./’

get_rule_trace(interpretation) Tuple[pandas.DataFrame, pandas.DataFrame][source]

Returns the trace of the program as 2 pandas dataframes (one for nodes, one for edges). This includes every change that has occurred to the interpretation. If atom_trace was set to true this gives us full explainability of why interpretations changed

Parameters:

interpretation – the output of pyreason.reason(), the final interpretation

:returns two pandas dataframes (nodes, edges) representing the changes that occurred during reasoning

filter_and_sort_nodes(interpretation, labels: List[str], bound: pyreason.scripts.numba_wrapper.numba_types.interval_type.Interval = interval.closed(0, 1), sort_by: str = 'lower', descending: bool = True)[source]

Filters and sorts the node changes in the interpretation and returns as a list of Pandas dataframes that are easy to access

Parameters:
  • interpretation – the output of pyreason.reason(), the final interpretation

  • labels – A list of strings, labels that are in the interpretation that are to be filtered

  • bound – The bound that will filter any interpretation that is not in it. the default does not filter anything, defaults to interval.closed(0,1)

  • sort_by – String that is either ‘lower’ or ‘upper’, sorts by the lower/upper bound, defaults to ‘lower’

  • descending – A bool that sorts by descending/ascending order, defaults to True

Returns:

A list of Pandas dataframes that contain the filtered and sorted interpretations that are easy to access

filter_and_sort_edges(interpretation, labels: List[str], bound: pyreason.scripts.numba_wrapper.numba_types.interval_type.Interval = interval.closed(0, 1), sort_by: str = 'lower', descending: bool = True)[source]

Filters and sorts the edge changes in the interpretation and returns as a list of Pandas dataframes that are easy to access

Parameters:
  • interpretation – the output of pyreason.reason(), the final interpretation

  • labels – A list of strings, labels that are in the interpretation that are to be filtered

  • bound – The bound that will filter any interpretation that is not in it. the default does not filter anything, defaults to interval.closed(0,1)

  • sort_by – String that is either ‘lower’ or ‘upper’, sorts by the lower/upper bound, defaults to ‘lower’

  • descending – A bool that sorts by descending/ascending order, defaults to True

Returns:

A list of Pandas dataframes that contain the filtered and sorted interpretations that are easy to access

cache_status[source]
graph_path[source]