openasce.inference package

class openasce.inference.GraphInferModel(*, graph: CausalGraph = None, column_names: List[str] = None, treatment_name: str = None, label_name: str = None, num_iteration=20)[source]

Bases: InferenceModel

The inference using the causal graph

graph

The causal graph. If not set, the class will try to find it out if discovery is available.

column_names

all names of sample

treatment_name

treatment column name in column_names

label_name

target column name in column_names

Parameters
  • graph – causal graph

  • column_names – all names of column

  • treatment_name – the name of treatment column

  • label_name – the name of target name

__annotations__ = {}
__doc__ = 'The inference using the causal graph\n\n    Attributes:\n        graph: The causal graph. If not set, the class will try to find it out if discovery is available.\n        column_names: all names of sample\n        treatment_name: treatment column name in column_names\n        label_name: target column name in column_names\n    '
__init__(*, graph: CausalGraph = None, column_names: List[str] = None, treatment_name: str = None, label_name: str = None, num_iteration=20) None[source]
Parameters
  • graph – causal graph

  • column_names – all names of column

  • treatment_name – the name of treatment column

  • label_name – the name of target name

__module__ = 'openasce.inference.graph_inference'
_do_lbp(*, do_condition: Dict, condition: Dict)[source]

compute the under the treatment

Argument:

do_condition: treatment node and its value condition: node and its value need to compute the outcome

_strict_to_condition(condition, p_r)[source]
_update_multipy(form_a: GraphNodeForm, form_b: GraphNodeForm)[source]
_update_normalize(node: GraphNodeForm)[source]
property column_names

All nodes’ name. Note: should include the treatment node and label node.

property data

Return the sample data

property discovery: Discovery
estimate(*, X: Iterable[ndarray] = None, Y: Iterable[ndarray] = None, T: Iterable[ndarray] = None, **kwargs) None[source]

Feed the sample data and estimate the outcome on the samples

Parameters
  • X – Features of the samples.

  • Y – Ignore in causal graph inference

  • T – Ignore in causal graph inference

  • kwargs – {‘treat_value’: treat_value, ‘label_value’:label_value}

Returns:

fit(*, X: Iterable[ndarray], Y: Iterable[ndarray] = None, T: Iterable[ndarray] = None, **kwargs) None[source]

Feed the sample data to train the graph

Parameters
  • X – All features of the samples including the treatment and the label node.

  • Y – Ignore in causal graph inference

  • T – Ignore in causal graph inference.

Returns:

get_result()[source]

Get the estimated result

The sub-class should implement this routine and runtime invokes it.

Arguments:

Returns

The estimation result.

property graph
property label_name
output()[source]

Output the estimated result to files

The sub-class should implement this routine and runtime invokes it.

Arguments:

Returns:

property treatment_name
class openasce.inference.TModel(hidden_units: Dict, lr: float = 0.1, name: str = 't_model')[source]

Bases: InferenceModel

T_model based on NN

Initialize.

Parameters
  • hidden_units (dict) – list of positive integer, the layer number and units in each layer.

  • lr (float) – learning rate

__annotations__ = {}
__doc__ = '\n    T_model based on NN\n    '
__init__(hidden_units: Dict, lr: float = 0.1, name: str = 't_model') None[source]

Initialize.

Parameters
  • hidden_units (dict) – list of positive integer, the layer number and units in each layer.

  • lr (float) – learning rate

__module__ = 'openasce.inference.t_model'
_call(*, x: Tensor, y: Tensor, t: Tensor, training: bool) Union[None, Dict[str, Tensor]][source]
Parameters
  • x – one batch of features

  • y – one batch of labels, shape: [batch_size], outcome labels

  • t – one batch of treatments

  • training – True means training and False for predict

Returns

None for training and Dict for predict

_generator(**kwargs)[source]

main loop

_predict_loop()[source]

main loop for prediction

_train_loop(*, num_epochs, **kwargs)[source]

main loop for train

estimate(X: Iterable[Tensor] = None, T: Iterable[Tensor] = None, *, Z: Iterable[Tuple[Tensor, Tensor, Tensor]] = None, **kwargs) None[source]

Feed the sample data Estimate the effect on the samples, and get_result method can be used to get the result of prediction :param X: Features of the samples. :param T: Treatments of the samples. :param Z: The iterable object returning (a batch of X, a batch of Y, a batch of T)

Returns

None

fit(X: Iterable[Tensor] = None, Y: Iterable[Tensor] = None, T: Iterable[Tensor] = None, *, Z: Iterable[Tuple[Tensor, Tensor, Tensor]] = None, num_epochs: int = 1, **kwargs) None[source]

Feed the sample data and train the model on the samples.

Parameters
  • X – Features of the samples.

  • Y – Outcomes of the samples.

  • T – Treatments of the samples.

  • Z – The iterable object returning (a batch of X, a batch of Y, a batch of T)

  • num_epochs – number of the train epoch

Returns

None

forward(x: Tensor, t: Tensor, training: bool) Dict[str, Tensor][source]
get_optimizer(lr: float = 0.01)[source]

Build the optimizer.

Args:

Returns

An optimizer.

loss(predictions: Dict, labels: List[Tensor])[source]
property trainable_variables

Subpackages

Submodules

openasce.inference.graph_inference module

class openasce.inference.graph_inference.GraphInferModel(*, graph: CausalGraph = None, column_names: List[str] = None, treatment_name: str = None, label_name: str = None, num_iteration=20)[source]

Bases: InferenceModel

The inference using the causal graph

graph

The causal graph. If not set, the class will try to find it out if discovery is available.

column_names

all names of sample

treatment_name

treatment column name in column_names

label_name

target column name in column_names

Parameters
  • graph – causal graph

  • column_names – all names of column

  • treatment_name – the name of treatment column

  • label_name – the name of target name

__annotations__ = {}
__doc__ = 'The inference using the causal graph\n\n    Attributes:\n        graph: The causal graph. If not set, the class will try to find it out if discovery is available.\n        column_names: all names of sample\n        treatment_name: treatment column name in column_names\n        label_name: target column name in column_names\n    '
__init__(*, graph: CausalGraph = None, column_names: List[str] = None, treatment_name: str = None, label_name: str = None, num_iteration=20) None[source]
Parameters
  • graph – causal graph

  • column_names – all names of column

  • treatment_name – the name of treatment column

  • label_name – the name of target name

__module__ = 'openasce.inference.graph_inference'
_do_lbp(*, do_condition: Dict, condition: Dict)[source]

compute the under the treatment

Argument:

do_condition: treatment node and its value condition: node and its value need to compute the outcome

_strict_to_condition(condition, p_r)[source]
_update_multipy(form_a: GraphNodeForm, form_b: GraphNodeForm)[source]
_update_normalize(node: GraphNodeForm)[source]
property column_names

All nodes’ name. Note: should include the treatment node and label node.

property data

Return the sample data

property discovery: Discovery
estimate(*, X: Iterable[ndarray] = None, Y: Iterable[ndarray] = None, T: Iterable[ndarray] = None, **kwargs) None[source]

Feed the sample data and estimate the outcome on the samples

Parameters
  • X – Features of the samples.

  • Y – Ignore in causal graph inference

  • T – Ignore in causal graph inference

  • kwargs – {‘treat_value’: treat_value, ‘label_value’:label_value}

Returns:

fit(*, X: Iterable[ndarray], Y: Iterable[ndarray] = None, T: Iterable[ndarray] = None, **kwargs) None[source]

Feed the sample data to train the graph

Parameters
  • X – All features of the samples including the treatment and the label node.

  • Y – Ignore in causal graph inference

  • T – Ignore in causal graph inference.

Returns:

get_result()[source]

Get the estimated result

The sub-class should implement this routine and runtime invokes it.

Arguments:

Returns

The estimation result.

property graph
property label_name
output()[source]

Output the estimated result to files

The sub-class should implement this routine and runtime invokes it.

Arguments:

Returns:

property treatment_name

openasce.inference.graph_inference_test module

openasce.inference.inference_model module

class openasce.inference.inference_model.InferenceModel[source]

Bases: Runtime

Inference Class

Base class of the causal inference

Attributes:

CONDITION_DICT_NAME = 'condition'
LABEL_VALUE = 'label_value'
TREATMENT_VALUE = 'treatment_value'
__annotations__ = {}
__doc__ = 'Inference Class\n\n    Base class of the causal inference\n\n    Attributes:\n\n    '
__init__() None[source]
__module__ = 'openasce.inference.inference_model'
_wrap_fit()[source]
property data

Return the sample data

estimate(*, X: Iterable[ndarray], T: Iterable[ndarray], **kwargs) None[source]

Feed the sample data and estimate the effect on the samples

Parameters
  • X – Features of the samples.

  • T – Treatments of the samples.

Returns

None

fit(*, X: Iterable[ndarray], Y: Iterable[ndarray], T: Iterable[ndarray], **kwargs) None[source]

Feed the sample data and train the model used to effect on the samples.

Parameters
  • X – Features of the samples.

  • Y – Outcomes of the samples.

  • T – Treatments of the samples.

Returns

None

get_result() Any[source]

Get the estimated result

The sub-class should implement this routine and runtime invokes it.

Returns

The estimation result.

output(output_path: str) None[source]

Output the estimated result to files

The sub-class should implement this routine and runtime invokes it.

Parameters

output_path – The path of output file.

Returns

None

openasce.inference.t_model module

class openasce.inference.t_model.TModel(hidden_units: Dict, lr: float = 0.1, name: str = 't_model')[source]

Bases: InferenceModel

T_model based on NN

Initialize.

Parameters
  • hidden_units (dict) – list of positive integer, the layer number and units in each layer.

  • lr (float) – learning rate

__annotations__ = {}
__doc__ = '\n    T_model based on NN\n    '
__init__(hidden_units: Dict, lr: float = 0.1, name: str = 't_model') None[source]

Initialize.

Parameters
  • hidden_units (dict) – list of positive integer, the layer number and units in each layer.

  • lr (float) – learning rate

__module__ = 'openasce.inference.t_model'
_call(*, x: Tensor, y: Tensor, t: Tensor, training: bool) Union[None, Dict[str, Tensor]][source]
Parameters
  • x – one batch of features

  • y – one batch of labels, shape: [batch_size], outcome labels

  • t – one batch of treatments

  • training – True means training and False for predict

Returns

None for training and Dict for predict

_generator(**kwargs)[source]

main loop

_predict_loop()[source]

main loop for prediction

_train_loop(*, num_epochs, **kwargs)[source]

main loop for train

estimate(X: Iterable[Tensor] = None, T: Iterable[Tensor] = None, *, Z: Iterable[Tuple[Tensor, Tensor, Tensor]] = None, **kwargs) None[source]

Feed the sample data Estimate the effect on the samples, and get_result method can be used to get the result of prediction :param X: Features of the samples. :param T: Treatments of the samples. :param Z: The iterable object returning (a batch of X, a batch of Y, a batch of T)

Returns

None

fit(X: Iterable[Tensor] = None, Y: Iterable[Tensor] = None, T: Iterable[Tensor] = None, *, Z: Iterable[Tuple[Tensor, Tensor, Tensor]] = None, num_epochs: int = 1, **kwargs) None[source]

Feed the sample data and train the model on the samples.

Parameters
  • X – Features of the samples.

  • Y – Outcomes of the samples.

  • T – Treatments of the samples.

  • Z – The iterable object returning (a batch of X, a batch of Y, a batch of T)

  • num_epochs – number of the train epoch

Returns

None

forward(x: Tensor, t: Tensor, training: bool) Dict[str, Tensor][source]
get_optimizer(lr: float = 0.01)[source]

Build the optimizer.

Args:

Returns

An optimizer.

loss(predictions: Dict, labels: List[Tensor])[source]
property trainable_variables