-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbranchingProbabilities.py
194 lines (107 loc) · 4.32 KB
/
branchingProbabilities.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/python3
import numpy as np
verbose = False
class calcProbabilities:
def __init__(self,sysname,tm_path,remove=False):
# Load transition matrix
self.tm = np.load(tm_path)
# nstates
self.nmin = len(self.tm[0])
############################
### equi. Probabilities ###
############################
# population of each node by summing over all incoming flow
pop_m = np.sum(self.tm, axis=0)
# probabiliy
peq_m = pop_m/np.sum(pop_m)
############################
### Branching Prob. ###
############################
# norm matrix so that rows sum to 0 -> right stochastic matrix
rm = np.zeros((self.nmin,self.nmin))
# if mts == True delete transitions that are not bidirectional and delete disconnected states
if remove == False:
T = self.tm
if remove == True:
print("Processing Transition Matrix")
T = np.zeros((self.nmin,self.nmin))
for i in range(self.nmin):
for j in range(self.nmin):
t = self.tm[i,j]
if i == j:
T[i,j] = t
else:
if self.tm[j,i] != 0:
T[i,j] = t
print("Removing non bidirectional transition states")
i = 0
s = 1
while i < self.nmin:
l = np.count_nonzero(T[i])
if l == 1:
T = np.delete(T,i,axis=0)
T = np.delete(T,i,axis=1)
self.nmin -= 1
if verbose == True:
print("removed state {}".format(s))
if l != 1:
i += 1
s += 1
print("Done")
"""
for i in range(self.nmin):
row = T[i]
n = np.sum(row)
rm[i] = row/n
"""
i = 0
delEntries = []
print("---------------------------------------------------------")
print("Start Normalization")
rm = np.zeros((self.nmin,self.nmin))
print("Dimension Probability Matrix: ", rm.shape)
while i < self.nmin:
if i == 0:
rm = np.zeros((self.nmin,self.nmin))
if verbose == True:
print("Dimension Probability Matrix: ", rm.shape)
row = T[i]
n = np.sum(row)
if n == 0:
if verbose == True:
print("state {} has no outgoing transitions".format(i))
print("delete entry and reinitialize")
T = np.delete(T,i,0)
T = np.delete(T,i,1)
delEntries.append(i)
i = 0
self.nmin = self.nmin - 1
# update prob eq list
peq_m = peq_m[:-1]
else:
rm[i] = row/n
#print(np.sum(rm[i]))
i += 1
print("End Normalization")
print("Dimension Probability Matrix: ", rm.shape)
# write everyting
self.writeProbMatrix(rm,sysname + '_branchingProbabilities.txt')
self.writeProbabilities(peq_m,sysname + '_equiProb.txt')
############################
### Helper Functions ###
############################
def writeProbMatrix(self,T,outname):
with open(outname,'w') as f:
for i in range(self.nmin):
row = str(T[i,0])
for j in range(1,self.nmin):
row += '\t '+str(T[i,j])
row += '\n'
f.write(row)
def writeProbabilities(self,P,outname):
with open(outname,'w') as f:
prob = str(P[0])
for i in range(1,self.nmin):
prob += '\t' + str(P[i])
prob += '\n'
f.write(prob)