pyreason
pyreason.scriptspyreason.scripts.annotation_functionspyreason.scripts.componentspyreason.scripts.factspyreason.scripts.interpretationpyreason.scripts.intervalpyreason.scripts.numba_wrapperpyreason.scripts.programpyreason.scripts.rulespyreason.scripts.thresholdpyreason.scripts.utilspyreason.scripts.argspyreason.scripts.diffuse
Package Contents
Classes
Example text: |
|
A class representing a threshold for a clause in a rule. |
Functions
|
Resets certain variables to None to be able to do pr.reason() multiple times in a program |
Resets rules to none |
|
|
Loads graph from GraphMl file path into program |
|
Load a networkx DiGraph into pyreason |
|
Load IPL from YAML file path into program |
|
Add a rule to pyreason from text format. This format is not as modular as the YAML format. |
|
Add a set of rules from a text file |
|
Add a PyReason fact to the program. |
|
Function to add annotation functions to PyReason. The added functions can be used in rules |
|
Function to start the main reasoning process. Graph and rules must already be loaded. |
|
Saves the trace of the program. This includes every change that has occurred to the interpretation. If atom_trace was set to true |
|
Returns the trace of the program as 2 pandas dataframes (one for nodes, one for edges). |
|
Filters and sorts the node changes in the interpretation and returns as a list of Pandas dataframes that are easy to access |
|
Filters and sorts the edge changes in the interpretation and returns as a list of Pandas dataframes that are easy to access |
Attributes
- 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 = '[]'
- 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
- reset()[source]
Resets certain variables to None to be able to do pr.reason() multiple times in a program without memory blowing up
- 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