Old engine for Continuous Time Bayesian Networks. Superseded by reCTBN. 🐍
https://github.com/madlabunimib/PyCTBN
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
91 lines
2.9 KiB
91 lines
2.9 KiB
4 years ago
|
import sys
|
||
|
sys.path.append('../')
|
||
|
|
||
4 years ago
|
import typing as ty
|
||
4 years ago
|
|
||
4 years ago
|
import numpy as np
|
||
4 years ago
|
|
||
4 years ago
|
|
||
4 years ago
|
class Structure:
|
||
4 years ago
|
"""
|
||
4 years ago
|
Contains all the infos about the network structure(nodes names, nodes caridinalites, edges...)
|
||
4 years ago
|
|
||
4 years ago
|
:nodes_labels_list: the symbolic names of the variables
|
||
|
:nodes_indexes_arr: the indexes of the nodes
|
||
|
:nodes_vals_arr: the cardinalites of the nodes
|
||
|
:edges_list: the edges of the network
|
||
|
:total_variables_number: the total number of variables in the net
|
||
4 years ago
|
"""
|
||
4 years ago
|
|
||
4 years ago
|
def __init__(self, nodes_label_list: ty.List, node_indexes_arr: np.ndarray, nodes_vals_arr: np.ndarray,
|
||
4 years ago
|
edges_list: ty.List, total_variables_number: int):
|
||
|
self._nodes_labels_list = nodes_label_list
|
||
|
self._nodes_indexes_arr = node_indexes_arr
|
||
|
self._nodes_vals_arr = nodes_vals_arr
|
||
|
self._edges_list = edges_list
|
||
|
self._total_variables_number = total_variables_number
|
||
|
|
||
|
@property
|
||
|
def edges(self):
|
||
4 years ago
|
#records = self.structure_frame.to_records(index=False)
|
||
|
#edges_list = list(records)
|
||
4 years ago
|
return self._edges_list
|
||
4 years ago
|
|
||
4 years ago
|
@property
|
||
|
def nodes_labels(self):
|
||
|
return self._nodes_labels_list
|
||
4 years ago
|
|
||
4 years ago
|
@property
|
||
4 years ago
|
def nodes_indexes(self) -> np.ndarray:
|
||
4 years ago
|
return self._nodes_indexes_arr
|
||
4 years ago
|
|
||
4 years ago
|
@property
|
||
4 years ago
|
def nodes_values(self) -> np.ndarray:
|
||
4 years ago
|
return self._nodes_vals_arr
|
||
4 years ago
|
|
||
4 years ago
|
@property
|
||
|
def total_variables_number(self):
|
||
|
return self._total_variables_number
|
||
4 years ago
|
|
||
4 years ago
|
def get_node_id(self, node_indx: int) -> str:
|
||
4 years ago
|
return self._nodes_labels_list[node_indx]
|
||
4 years ago
|
|
||
4 years ago
|
def clean_structure_edges(self):
|
||
|
self._edges_list = list()
|
||
|
|
||
|
def add_edge(self,edge: tuple):
|
||
|
self._edges_list.append(tuple)
|
||
|
print(self._edges_list)
|
||
|
|
||
|
def remove_edge(self,edge: tuple):
|
||
|
self._edges_list.remove(tuple)
|
||
|
|
||
|
def contains_edge(self,edge:tuple) -> bool:
|
||
|
return edge in self._edges_list
|
||
|
|
||
4 years ago
|
def get_node_indx(self, node_id: str) -> int:
|
||
4 years ago
|
pos_indx = self._nodes_labels_list.index(node_id)
|
||
|
return self._nodes_indexes_arr[pos_indx]
|
||
4 years ago
|
|
||
4 years ago
|
def get_positional_node_indx(self, node_id: str) -> int:
|
||
4 years ago
|
return self._nodes_labels_list.index(node_id)
|
||
4 years ago
|
|
||
4 years ago
|
def get_states_number(self, node: str) -> int:
|
||
4 years ago
|
pos_indx = self._nodes_labels_list.index(node)
|
||
|
return self._nodes_vals_arr[pos_indx]
|
||
4 years ago
|
|
||
4 years ago
|
def __repr__(self):
|
||
4 years ago
|
return "Variables:\n" + str(self._nodes_labels_list) +"\nValues:\n"+ str(self._nodes_vals_arr) +\
|
||
|
"\nEdges: \n" + str(self._edges_list)
|
||
4 years ago
|
|
||
|
def __eq__(self, other):
|
||
|
"""Overrides the default implementation"""
|
||
|
if isinstance(other, Structure):
|
||
4 years ago
|
return set(self._nodes_labels_list) == set(other._nodes_labels_list) and \
|
||
|
np.array_equal(self._nodes_vals_arr, other._nodes_vals_arr) and \
|
||
|
np.array_equal(self._nodes_indexes_arr, other._nodes_indexes_arr) and \
|
||
4 years ago
|
self._edges_list == other._edges_list
|
||
4 years ago
|
|
||
4 years ago
|
return NotImplemented
|
||
4 years ago
|
|