openasce.discovery package¶
- class openasce.discovery.CausalGraph(names=[], bn=None, w: ndarray = None)[source]¶
Bases:
objectCausal Graph Class
Represent the casual graph
Constructor
- Parameters
names – the node names
bn – basic causal graph
w – the connection matrix for causal graph
- DEFAULT_COLUMN_NAME_PREFIX = 'x'¶
- __init__(names=[], bn=None, w: ndarray = None)[source]¶
Constructor
- Parameters
names – the node names
bn – basic causal graph
w – the connection matrix for causal graph
- add_edge(parent: Union[int, str], child: Union[int, str], max_parents=None) bool[source]¶
Adds edge if respects max parents constraint and does not create a cycle
- Parameters
parent (int) – id of parent
child (int) – id of child
max_parents (int) – None means no constraints
- Returns
True if actually added the edge and False means no way to add the edge
- calculate_parameter(data: ndarray, rd: Dict[int, int] = None)[source]¶
Calculate the edge weight in the graph
- Parameters
data – samples
rd – r[i] = r_i
- compute_r(data: ndarray) dict[source]¶
Compute the number of the value for each node
- Parameters
data (np array) – (nsamples, nfeatures)
- Returns
r (dict): r[i] = r_i
- copy(cg) None[source]¶
Copies the structure of cg inside self and erases everything else
- Parameters
cg (CausalGraph) – model
- is_cyclic() bool[source]¶
Returns True if a cycle is found else False.
Iterates over the nodes to find all the parents’ parents, etc. A cycle is found if a node belongs to its own parent’s set.
- load(file_name: str) None[source]¶
Loads structure from file. See save method
- Parameters
file_name – the path of the file to be loaded
- merge(g1, g2, p1=1, p2=1, max_parents: int = None, mut_rate: float = 0.0) None[source]¶
Pick up edges from both g1 and g2 according to some random policy
- Parameters
g1 (CausalGraph) –
g1 –
p1 (float in [0,1]) – proba of an edge in g1 being in self
p2 (float in [0,1]) – proba of an edge in g2 being in self p1 + p2 = 1
max_parents (int) –
- mutate(mut_rate: float = 0) None[source]¶
Introduces new edges with a probability mut_rate
- Parameters
mut_rate (float in [0,1]) – proba of mutation
- names_init(names: List[str]) None[source]¶
Initialize the graph with feature names
initialize the names_to_index and index_to_names attributes initialize parents[i] = set() (no edges for the moment)
- Parameters
names (list of string) – the names of the nodes
- Returns
None
- num_save(file_name: str) None[source]¶
Saves the graph in number format
- Example
parent1, child1 parent2, child2
- Parameters
file_name – saved file path
- random_init(max_parents: int = None) None[source]¶
Add edges randomly
For each node, pick a random number of the desired number of parents. Then, for each candidate, pick another random number. In average, the node will have the desired number of parents.
- Parameters
max_parents – maximal number of one node’s parents
- random_merge(g1, g2, p1, p2) None[source]¶
Creates graph from edges both in g1 and g2. Adds edges according to proba p1 and p2
- Parameters
g1 (CausalGraph) –
g1 –
p1 (float in [0,1]) – proba of an edge in g1 being in self
p2 (float in [0,1]) – proba of an edge in g2 being in self
- remove_extra_parents(max_parents: int = None) None[source]¶
Removes extra edges if does not respect max parents constraint
- Parameters
max_parents – the maximal number of the node’s parents
- save(file_path: str) None[source]¶
Saves the graph in the desired format
- Example
parent1, child1 parent2, child2
- Parameters
file_path – saved file path
- class openasce.discovery.CausalRegressionDiscovery[source]¶
Bases:
DiscoveryExecute the causal discovery by notears method
Attributes:
Constructor
Arguments:
Returns:
- fit(*, X: Union[ndarray, Callable], **kwargs)[source]¶
Feed the sample data
- Parameters
X (num of samples, features or callable returning np.ndarray) – samples
Returns:
- get_result() Tuple[CausalGraph, float][source]¶
Get the causal graph sample data
- Parameters
X (num of samples, features or callable returning np.ndarray) – samples
Returns:
- class openasce.discovery.CausalSearchDiscovery[source]¶
Bases:
DiscoveryExecute the causal inference by search method
Attributes:
Constructor
Arguments:
Returns:
- fit(*, X: Union[ndarray, Callable], **kwargs) None[source]¶
Feed the sample data
- Parameters
X (num of samples, features or callable returning np.ndarray) – samples
Returns:
- get_result() Tuple[CausalGraph, float][source]¶
Get the causal graph sample data
- Parameters
X (num of samples, features or callable returning np.ndarray) – samples
Returns:
Subpackages¶
- openasce.discovery.regression_discovery package
- Submodules
- openasce.discovery.regression_discovery.lbfgsb_optimizer module
- openasce.discovery.regression_discovery.locally_connected module
- openasce.discovery.regression_discovery.notears_mlp module
- openasce.discovery.regression_discovery.regression_discovery module
- openasce.discovery.regression_discovery.regression_strategy module
- openasce.discovery.regression_discovery.trace_expm module
- openasce.discovery.search_discovery package
Submodules¶
openasce.discovery.causal_graph module¶
- class openasce.discovery.causal_graph.CausalGraph(names=[], bn=None, w: ndarray = None)[source]¶
Bases:
objectCausal Graph Class
Represent the casual graph
Constructor
- Parameters
names – the node names
bn – basic causal graph
w – the connection matrix for causal graph
- DEFAULT_COLUMN_NAME_PREFIX = 'x'¶
- __init__(names=[], bn=None, w: ndarray = None)[source]¶
Constructor
- Parameters
names – the node names
bn – basic causal graph
w – the connection matrix for causal graph
- add_edge(parent: Union[int, str], child: Union[int, str], max_parents=None) bool[source]¶
Adds edge if respects max parents constraint and does not create a cycle
- Parameters
parent (int) – id of parent
child (int) – id of child
max_parents (int) – None means no constraints
- Returns
True if actually added the edge and False means no way to add the edge
- calculate_parameter(data: ndarray, rd: Dict[int, int] = None)[source]¶
Calculate the edge weight in the graph
- Parameters
data – samples
rd – r[i] = r_i
- compute_r(data: ndarray) dict[source]¶
Compute the number of the value for each node
- Parameters
data (np array) – (nsamples, nfeatures)
- Returns
r (dict): r[i] = r_i
- copy(cg) None[source]¶
Copies the structure of cg inside self and erases everything else
- Parameters
cg (CausalGraph) – model
- is_cyclic() bool[source]¶
Returns True if a cycle is found else False.
Iterates over the nodes to find all the parents’ parents, etc. A cycle is found if a node belongs to its own parent’s set.
- load(file_name: str) None[source]¶
Loads structure from file. See save method
- Parameters
file_name – the path of the file to be loaded
- merge(g1, g2, p1=1, p2=1, max_parents: int = None, mut_rate: float = 0.0) None[source]¶
Pick up edges from both g1 and g2 according to some random policy
- Parameters
g1 (CausalGraph) –
g1 –
p1 (float in [0,1]) – proba of an edge in g1 being in self
p2 (float in [0,1]) – proba of an edge in g2 being in self p1 + p2 = 1
max_parents (int) –
- mutate(mut_rate: float = 0) None[source]¶
Introduces new edges with a probability mut_rate
- Parameters
mut_rate (float in [0,1]) – proba of mutation
- names_init(names: List[str]) None[source]¶
Initialize the graph with feature names
initialize the names_to_index and index_to_names attributes initialize parents[i] = set() (no edges for the moment)
- Parameters
names (list of string) – the names of the nodes
- Returns
None
- num_save(file_name: str) None[source]¶
Saves the graph in number format
- Example
parent1, child1 parent2, child2
- Parameters
file_name – saved file path
- random_init(max_parents: int = None) None[source]¶
Add edges randomly
For each node, pick a random number of the desired number of parents. Then, for each candidate, pick another random number. In average, the node will have the desired number of parents.
- Parameters
max_parents – maximal number of one node’s parents
- random_merge(g1, g2, p1, p2) None[source]¶
Creates graph from edges both in g1 and g2. Adds edges according to proba p1 and p2
- Parameters
g1 (CausalGraph) –
g1 –
p1 (float in [0,1]) – proba of an edge in g1 being in self
p2 (float in [0,1]) – proba of an edge in g2 being in self
- remove_extra_parents(max_parents: int = None) None[source]¶
Removes extra edges if does not respect max parents constraint
- Parameters
max_parents – the maximal number of the node’s parents
- save(file_path: str) None[source]¶
Saves the graph in the desired format
- Example
parent1, child1 parent2, child2
- Parameters
file_path – saved file path
openasce.discovery.discovery module¶
- class openasce.discovery.discovery.Discovery[source]¶
Bases:
RuntimeDiscovery Class
Base class of the causal discovery
- node_names¶
the name of graph node, which should be set before fit
- Type
List[str]
- fit(*, X: Union[ndarray, Callable], **kwargs) None[source]¶
Feed the sample data and search the causal relation on them
- Parameters
X – Features of the samples.
- Returns
None
- property node_names¶
openasce.discovery.graph_node_form module¶
- class openasce.discovery.graph_node_form.GraphNodeForm(input_data: List[List[float]], columns: List[str])[source]¶
Bases:
object- SCORE_COLUMN_NAME = 'node_score_value'¶
- property columns¶
- property data¶
- get_score_deviation(addition)[source]¶
multiply ext’s score column to local score column for same key column’s value
- Parameters
addition – Another GraphNodeForm used to calculate the deviation
- Returns
Calculation result
- get_score_value(target_key: str, target_value: int)[source]¶
multiply ext’s score column to local score column for same key column’s value
- Parameters
target_key – the column name
target_value – the column value
Returns:
- multiply_score_column(key: str, ext) None[source]¶
multiply ext’s score column to local score column for same key column’s value
- Parameters
key – the column name
ext (GraphNodeForm) – another GraphNodeForm
- Returns
None
- property score_column_index¶
- set_flag_zero(key: str, value_list: List[int]) None[source]¶
set score column to 0 if the value of key column is not in input value_list
- Parameters
key – the column name
value_list – the values need to be set
- Returns
None
- set_groupby_sum(key: str)[source]¶
multiply ext’s score column to local score column for same key column’s value
- Parameters
key – the column name
Returns:
- property size¶