1
0
Fork 0

Add all necessary structures to place Cims values

parallel_struct_est
philpMartin 4 years ago
parent a20ed4e0eb
commit 4356eee147
  1. 22
      main_package/classes/amalgamated_cims.py
  2. 8
      main_package/classes/conditional_intensity_matrix.py
  3. 31
      main_package/classes/network_graph.py
  4. 11
      main_package/classes/sample_path.py
  5. 35
      main_package/classes/set_of_cims.py
  6. 10
      main_package/classes/structure.py

@ -1,20 +1,24 @@
import conditional_intensity_matrix as cim import set_of_cims as socim
import numpy as np import numpy as np
class AmalgamatedCims: class AmalgamatedCims:
def __init__(self, states_number,list_of_keys, list_of_matrices_dims): def __init__(self, states_number,list_of_keys, nodes_value, list_of_vars_order):
self.actual_cims = {} self.actual_cims = {}
self.init_cims_structure(list_of_keys, list_of_matrices_dims) self.init_cims_structure(list_of_keys, nodes_value, list_of_vars_order)
self.states_per_variable = states_number self.states_per_variable = states_number
def init_cims_structure(self, keys, dims): def init_cims_structure(self, keys, nodes_val, list_of_vars_order):
for key, dim in (keys, dims): for key, vars_order in (keys, list_of_vars_order):
self.actual_cims[key] = np.empty(dim, dtype=cim.ConditionalIntensityMatrix) self.actual_cims[key] = socim.SetOfCims(key, vars_order, nodes_val)
for key in self.actual_cims.keys():
for indx in range(len(self.actual_cims[key])):
self.actual_cims[key][indx] = cim.ConditionalIntensityMatrix(self.states_per_variable)
def compute_matrix_indx(self, row, col): def compute_matrix_indx(self, row, col):
return self.state_per_variable * row + col return self.state_per_variable * row + col
def get_vars_order(self, node):
return self.actual_cims[node][1]
def update_state_transition_for_matrix(self, node, dict_of_indxs, element_indx):
self.actual_cims[node]

@ -5,5 +5,9 @@ class ConditionalIntensityMatrix:
def __init__(self, dimension): def __init__(self, dimension):
self.state_residence_times = np.zeros(shape=(1, dimension)) self.state_residence_times = np.zeros(shape=(1, dimension))
self.state_transition_matrix = np.zeros(shape=(dimension, dimension)) self.state_transition_matrix = np.zeros(shape=(dimension, dimension), dtype=int)
self.cim = np.zeros(shape=(dimension, dimension)) self.cim = np.zeros(shape=(dimension, dimension), dtype=float)
def update_state_transition_count(self, positions_list):
self.state_transition_matrix[positions_list[0]][positions_list[1]] = self.state_transition_matrix[positions_list[0]][positions_list[1]] + 1

@ -1,4 +1,4 @@
import numpy as np
import sample_path as sp import sample_path as sp
import networkx as nx import networkx as nx
import os import os
@ -21,11 +21,29 @@ class NetworkGraph():
def init_graph(self): def init_graph(self):
self.sample_path.build_trajectories() self.sample_path.build_trajectories()
self.sample_path.build_structure() self.sample_path.build_structure()
self.add_edges(self.sample_path.structure.list_of_edges()) self.add_nodes(self.sample_path.structure.list_of_node_ids())
self.add_edges(self.sample_path.structure.list_of_edge_ids())
def add_nodes(self, list_of_node_ids):
for indx, id in enumerate(list_of_node_ids):
#print(indx, id)
self.graph.add_node(id)
nx.set_node_attributes(self.graph, {id:indx}, 'indx')
#for node in list(self.graph.nodes):
#print(node)
def add_edges(self, list_of_edges): def add_edges(self, list_of_edges):
self.graph.add_edges_from(list_of_edges) self.graph.add_edges_from(list_of_edges)
def get_parents_by_id(self, node_id):
return list(self.graph.predecessors(node_id))
def get_node(self, node_id):
to_find = nd.Node(node_id)
for node in self.graph.nodes:
if node == to_find:
return node
@ -39,4 +57,13 @@ s1 = sp.SamplePath(path)
g1 = NetworkGraph(s1) g1 = NetworkGraph(s1)
g1.init_graph() g1.init_graph()
print(g1.graph.number_of_nodes())
print(g1.graph.number_of_edges())
print(nx.get_node_attributes(g1.graph, 'indx')['X'])
for node in g1.get_parents_by_id('X'):
print(g1.sample_path.structure.get_node_indx(node))
print(node)

@ -35,16 +35,5 @@ class SamplePath:
def get_number_trajectories(self): def get_number_trajectories(self):
return len(self.trajectories) return len(self.trajectories)
"""os.getcwd()
os.chdir('..')
path = os.getcwd() + '/data'
print(path)
sp = SamplePath(path)
sp.build_trajectories()
sp.build_structure()
print(sp.trajectories[7].actual_trajectory)
print(sp.importer.df_samples_list[7])
print(sp.get_number_trajectories())
print(list(sp.structure.list_of_edges()))"""

@ -0,0 +1,35 @@
import numpy as np
import conditional_intensity_matrix as cim
class SetOfCims:
def __init__(self, node_id, ordered_parent_set, value_type):
self.node_id = node_id
self.ordered_parent_set = ordered_parent_set
self.value = value_type
self.actual_cims = None
def build_actual_cims_structure(self):
cims_number = self.value**len(self.ordered_parent_set)
self.actual_cims = np.empty(cims_number, dtype=cim.ConditionalIntensityMatrix)
for indx, matrix in enumerate(self.actual_cims):
self.actual_cims[indx] = cim.ConditionalIntensityMatrix(self.value)
def indexes_converter(self, dict_of_indexes): # Si aspetta oggetti del tipo {X:1, Y:1, Z:0}
literal_index = ""
for node in self.ordered_parent_set:
literal_index = literal_index + str(dict_of_indexes[node])
return int(literal_index, self.value)
sofc = SetOfCims('W', ['X','Y', 'Z'], 2)
#sofc.build_actual_cims_structure(sofc.ordered_parent_set, sofc.value)
sofc.build_actual_cims_structure()
print(sofc.actual_cims)
print(sofc.indexes_converter({'X':1, 'Y':1, 'Z':0}))

@ -6,11 +6,19 @@ class Structure:
self.structure_frame = structure self.structure_frame = structure
self.variables_frame = variables self.variables_frame = variables
def list_of_edges(self): def list_of_edge_ids(self):
edges_list = [] edges_list = []
for indx, row in self.structure_frame.iterrows(): for indx, row in self.structure_frame.iterrows():
row_tuple = (row.From, row.To) row_tuple = (row.From, row.To)
edges_list.append(row_tuple) edges_list.append(row_tuple)
return edges_list return edges_list
def list_of_node_ids(self):
#print(self.variables_frame)
return self.variables_frame['Name']
def get_node_id(self, node_indx):
return self.variables_frame['Name'][node_indx]
def get_node_indx(self, node_id):
return list(self.variables_frame['Name']).index(node_id)