PyCTBN.PyCTBN.structure_graph package¶
Submodules¶
PyCTBN.PyCTBN.structure_graph.conditional_intensity_matrix module¶
-
class
PyCTBN.PyCTBN.structure_graph.conditional_intensity_matrix.
ConditionalIntensityMatrix
(state_residence_times: numpy.array = None, state_transition_matrix: numpy.array = None, cim: numpy.array = None)¶ Bases:
object
Abstracts the Conditional Intesity matrix of a node as aggregation of the state residence times vector and state transition matrix and the actual CIM matrix.
- Parameters
state_residence_times (numpy.array) – state residence times vector
state_transition_matrix (numpy.ndArray) – the transitions count matrix
- _cim
the actual cim of the node
-
property
cim
¶
-
compute_cim_coefficients
() → None¶ Compute the coefficients of the matrix _cim by using the following equality q_xx’ = M[x, x’] / T[x]. The class member
_cim
will contain the computed cim
-
property
state_residence_times
¶
-
property
state_transition_matrix
¶
PyCTBN.PyCTBN.structure_graph.network_generator module¶
-
class
PyCTBN.PyCTBN.structure_graph.network_generator.
NetworkGenerator
(labels, vals)¶ Bases:
object
Provides the methods to generate a network graph and the CIMs related to it Items in _labels, _vals and _indxs are related, and therefore respect the same order
- Parameters
_labels (List) – List of variables labels that will be part of the network
_vals (List) – List of cardinalities of the variables in network (defined in the same order as _labels)
_indxs (List) – List of the nodes indexes
_cims (Dict) – It contains, for each variable label (the key), the SetOfCims object related to it
_graph (NetworkGraph) – The NetworkGraph object representing the generated structure
-
property
cims
¶
-
property
dyn_str
¶
-
generate_cims
(min_val, max_val)¶ - For each node, generate the corresponding SetOfCims. The objective is to group the CIMs
(actually generated by private method __generate_cim) according to parents possibles states of every node. This method must obviously be executed after the graph has been generated.
- Parameters
min_val (float) – Minimum value allowed for the coefficients in the CIMs
max_val (float) – Maximum value allowed for the coefficients in the CIMs
-
generate_graph
(density, fixed: bool = False)¶ - Generate the edges according to specified density, and then instantiate the NetworkGraph object
to represent the network
- Parameters
density (float) – Probability of an edge between two nodes to exist
fixed (bool) – Specifies whether the required density is mandatory or it’s just a probability
-
property
graph
¶
-
property
variables
¶
PyCTBN.PyCTBN.structure_graph.network_graph module¶
-
class
PyCTBN.PyCTBN.structure_graph.network_graph.
NetworkGraph
(graph_struct: PyCTBN.PyCTBN.structure_graph.structure.Structure)¶ Bases:
object
Abstracts the infos contained in the Structure class in the form of a directed graph. Has the task of creating all the necessary filtering and indexing structures for parameters estimation
- Parameters
graph_struct (Structure) – the
Structure
object from which infos about the net will be extracted- _graph
directed graph
- _aggregated_info_about_nodes_parents
a structure that contains all the necessary infos about every parents of the node of which all the indexing and filtering structures will be constructed.
- _time_scalar_indexing_structure
the indexing structure for state res time estimation
- _transition_scalar_indexing_structure
the indexing structure for transition computation
- _time_filtering
the columns filtering structure used in the computation of the state res times
- _transition_filtering
the columns filtering structure used in the computation of the transition from one state to another
- _p_combs_structure
all the possible parents states combination for the node of interest
-
add_edges
(list_of_edges: List) → None¶ Add the edges to the
_graph
contained in the listlist_of_edges
.- Parameters
list_of_edges (List) – the list containing of tuples containing the edges
-
add_nodes
(list_of_nodes: List) → None¶ Adds the nodes to the
_graph
contained in the list of nodeslist_of_nodes
. Sets all the properties that identify a nodes (index, positional index, cardinality)- Parameters
list_of_nodes (List) – the nodes to add to
_graph
-
static
build_p_comb_structure_for_a_node
(parents_values: List) → numpy.ndarray¶ Builds the combinatorial structure that contains the combinations of all the values contained in
parents_values
.- Parameters
parents_values (List) – the cardinalities of the nodes
- Returns
A numpy matrix containing a grid of the combinations
- Return type
numpy.ndArray
-
static
build_time_columns_filtering_for_a_node
(node_indx: int, p_indxs: List) → numpy.ndarray¶ Builds the necessary structure to filter the desired columns indicated by
node_indx
andp_indxs
in the dataset. This structute will be used in the computation of the state res times. :param node_indx: the index of the node :type node_indx: int :param p_indxs: the indexes of the node’s parents :type p_indxs: List :return: The filtering structure for times estimation :rtype: numpy.ndArray
-
static
build_time_scalar_indexing_structure_for_a_node
(node_states: int, parents_vals: List) → numpy.ndarray¶ Builds an indexing structure for the computation of state residence times values.
- Parameters
node_states (int) – the node cardinality
parents_vals (List) – the caridinalites of the node’s parents
- Returns
The time indexing structure
- Return type
numpy.ndArray
-
static
build_transition_filtering_for_a_node
(node_indx: int, p_indxs: List, nodes_number: int) → numpy.ndarray¶ Builds the necessary structure to filter the desired columns indicated by
node_indx
andp_indxs
in the dataset. This structure will be used in the computation of the state transitions values. :param node_indx: the index of the node :type node_indx: int :param p_indxs: the indexes of the node’s parents :type p_indxs: List :param nodes_number: the total number of nodes in the dataset :type nodes_number: int :return: The filtering structure for transitions estimation :rtype: numpy.ndArray
-
static
build_transition_scalar_indexing_structure_for_a_node
(node_states_number: int, parents_vals: List) → numpy.ndarray¶ Builds an indexing structure for the computation of state transitions values.
- Parameters
node_states_number (int) – the node cardinality
parents_vals (List) – the caridinalites of the node’s parents
- Returns
The transition indexing structure
- Return type
numpy.ndArray
-
clear_indexing_filtering_structures
() → None¶ Initialize all the filtering/indexing structures.
-
property
edges
¶
-
fast_init
(node_id: str) → None¶ Initializes all the necessary structures for parameters estimation of the node identified by the label node_id
- Parameters
node_id (string) – the label of the node
-
get_node_indx
(node_id) → int¶
-
get_ordered_by_indx_set_of_parents
(node: str) → Tuple¶ Builds the aggregated structure that holds all the infos relative to the parent set of the node, namely (parents_labels, parents_indexes, parents_cardinalities).
- Parameters
node (string) – the label of the node
- Returns
a tuple containing all the parent set infos
- Return type
Tuple
-
get_parents_by_id
(node_id) → List¶ Returns a list of labels of the parents of the node
node_id
- Parameters
node_id (string) – the node label
- Returns
a List of labels of the parents
- Return type
List
-
get_positional_node_indx
(node_id) → int¶
-
get_states_number
(node_id) → int¶
-
has_edge
(edge: tuple) → bool¶ Check if the graph contains a specific edge
- Parameters:
edge: a tuple that rappresents the edge
- Returns:
bool
-
property
nodes
¶
-
property
nodes_indexes
¶
-
property
nodes_values
¶
-
property
p_combs
¶
-
remove_edges
(list_of_edges: List) → None¶ Remove the edges to the graph contained in the list list_of_edges.
- Parameters
list_of_edges (List) – The edges to remove from the graph
-
remove_node
(node_id: str) → None¶ Remove the node
node_id
from all the class members. Initialize all the filtering/indexing structures.
-
property
time_filtering
¶
-
property
time_scalar_indexing_strucure
¶
-
property
transition_filtering
¶
-
property
transition_scalar_indexing_structure
¶
PyCTBN.PyCTBN.structure_graph.sample_path module¶
-
class
PyCTBN.PyCTBN.structure_graph.sample_path.
SamplePath
(importer: PyCTBN.PyCTBN.utility.abstract_importer.AbstractImporter)¶ Bases:
object
Aggregates all the informations about the trajectories, the real structure of the sampled net and variables cardinalites. Has the task of creating the objects
Trajectory
andStructure
that will contain the mentioned data.- Parameters
importer (AbstractImporter) – the Importer object which contains the imported and processed data
- _trajectories
the
Trajectory
object that will contain all the concatenated trajectories- _structure
the
Structure
Object that will contain all the structural infos about the net- _total_variables_count
the number of variables in the net
-
build_structure
() → None¶ Builds the
Structure
object that aggregates all the infos about the net.
-
build_trajectories
() → None¶ Builds the Trajectory object that will contain all the trajectories. Clears all the unused dataframes in
_importer
Object
-
clear_memory
()¶
-
property
has_prior_net_structure
¶
-
property
structure
¶
-
property
total_variables_count
¶
-
property
trajectories
¶
PyCTBN.PyCTBN.structure_graph.set_of_cims module¶
-
class
PyCTBN.PyCTBN.structure_graph.set_of_cims.
SetOfCims
(node_id: str, parents_states_number: List, node_states_number: int, p_combs: numpy.ndarray, cims: numpy.ndarray = None)¶ Bases:
object
Aggregates all the CIMS of the node identified by the label _node_id.
- Parameters
node_id – the node label
parents_states_number (List) – the cardinalities of the parents
node_states_number (int) – the caridinality of the node
p_combs (numpy.ndArray) – the p_comb structure bound to this node
- _state_residence_time
matrix containing all the state residence time vectors for the node
- _transition_matrices
matrix containing all the transition matrices for the node
- _actual_cims
the cims of the node
-
property
actual_cims
¶
-
build_cims
(state_res_times: numpy.ndarray, transition_matrices: numpy.ndarray) → None¶ Build the
ConditionalIntensityMatrix
objects given the state residence times and transitions matrices. Compute the cim coefficients.The class member_actual_cims
will contain the computed cims.- Parameters
state_res_times (numpy.ndArray) – the state residence times matrix
transition_matrices (numpy.ndArray) – the transition matrices
-
build_times_and_transitions_structures
() → None¶ Initializes at the correct dimensions the state residence times matrix and the state transition matrices.
-
filter_cims_with_mask
(mask_arr: numpy.ndarray, comb: List) → numpy.ndarray¶ Filter the cims contained in the array
_actual_cims
given the boolean maskmask_arr
and the indexcomb
.- Parameters
mask_arr (numpy.array) – the boolean mask that indicates which parent to consider
comb (numpy.array) – the state/s of the filtered parents
- Returns
Array of
ConditionalIntensityMatrix
objects- Return type
numpy.array
-
get_cims_number
()¶
-
property
p_combs
¶
PyCTBN.PyCTBN.structure_graph.structure module¶
-
class
PyCTBN.PyCTBN.structure_graph.structure.
Structure
(nodes_labels_list: List, nodes_indexes_arr: numpy.ndarray, nodes_vals_arr: numpy.ndarray, edges_list: List, total_variables_number: int)¶ Bases:
object
Contains all the infos about the network structure(nodes labels, nodes caridinalites, edges, indexes)
- Parameters
nodes_labels_list (List) – the symbolic names of the variables
nodes_indexes_arr (numpy.ndArray) – the indexes of the nodes
nodes_vals_arr (numpy.ndArray) – the cardinalites of the nodes
edges_list (List) – the edges of the network
total_variables_number (int) – the total number of variables in the dataset
-
add_edge
(edge: tuple)¶
-
clean_structure_edges
()¶
-
contains_edge
(edge: tuple) → bool¶
-
property
edges
¶
-
get_node_id
(node_indx: int) → str¶ Given the
node_index
returns the node label.- Parameters
node_indx (int) – the node index
- Returns
the node label
- Return type
string
-
get_node_indx
(node_id: str) → int¶ Given the
node_index
returns the node label.- Parameters
node_id (string) – the node label
- Returns
the node index
- Return type
int
-
get_positional_node_indx
(node_id: str) → int¶
-
get_states_number
(node: str) → int¶ Given the node label
node
returns the cardinality of the node.- Parameters
node (string) – the node label
- Returns
the node cardinality
- Return type
int
-
property
nodes_indexes
¶
-
property
nodes_labels
¶
-
property
nodes_values
¶
-
remove_edge
(edge: tuple)¶
-
remove_node
(node_id: str) → None¶ Remove the node
node_id
from all the class members. The class member_total_variables_number
since it refers to the total number of variables in the dataset.
-
property
total_variables_number
¶
PyCTBN.PyCTBN.structure_graph.trajectory module¶
-
class
PyCTBN.PyCTBN.structure_graph.trajectory.
Trajectory
(list_of_columns: List, original_cols_number: int)¶ Bases:
object
Abstracts the infos about a complete set of trajectories, represented as a numpy array of doubles (the time deltas) and a numpy matrix of ints (the changes of states).
- Parameters
list_of_columns (List) – the list containing the times array and values matrix
original_cols_number (int) – total number of cols in the data
- _actual_trajectory
the trajectory containing also the duplicated/shifted values
- _times
the array containing the time deltas
-
property
complete_trajectory
¶
-
size
()¶
-
property
times
¶
-
property
trajectory
¶
PyCTBN.PyCTBN.structure_graph.trajectory_generator module¶
-
class
PyCTBN.PyCTBN.structure_graph.trajectory_generator.
TrajectoryGenerator
(importer: PyCTBN.PyCTBN.utility.abstract_importer.AbstractImporter = None, variables: pandas.core.frame.DataFrame = None, dyn_str: pandas.core.frame.DataFrame = None, dyn_cims: dict = None)¶ Bases:
object
Provides the methods to generate a trajectory basing on the network defined in the importer.
- Parameters
_importer (AbstractImporter) – the Importer object which contains the imported and processed data
_vnames (List) – List of the variables labels that belong to the network
_parents (Dict) – It contains, for each variable label (the key), the list of related parents labels
_cims (Dict) – It contains, for each variable label (the key), the SetOfCims object related to it
_generated_trajectory (pandas.DataFrame) – Result of the execution of CTBN_Sample, contains the output trajectory
-
CTBN_Sample
(t_end=- 1, max_tr=- 1)¶ - This method implements the generation of a trajectory, basing on the network structure and
on the coefficients defined in the CIMs. The variables are initialized with value 0, and the method takes care of adding the conventional last row made up of -1.
- Parameters
t_end (float) – If defined, the sampling ends when end time is reached
max_tr (int) – Parameter taken in consideration in case that t_end isn’t defined. It specifies the number of transitions to execute
-
multi_trajectory
(t_ends: list = None, max_trs: list = None)¶ Generate n trajectories in parallel, where n is the number of items in t_ends, if defined, or the number of items in max_trs otherwise
- Parameters
t_ends (list) – List of t_end values for the trajectories that will be generated
max_trs (list) – List of max_tr values for the trajectories that will be generated
-
worker
(t_end, max_tr, trajectories)¶ Single process that will be executed in parallel in order to generate one trajectory.
- Parameters
t_end (float) – If defined, the sampling ends when end time is reached
max_tr (int) – Parameter taken in consideration in case that t_end isn’t defined. It specifies the number of transitions to execute
trajectories (list) – Shared list that contains to which the generated trajectory is added