Untitled
unknown
plain_text
2 years ago
1.9 kB
11
Indexable
def viterbi_bt(df_norm, cell):
# Positions
bins = sorted(list(df_norm.index))
# Set of states (copy numbers)
Q = range(max_copy_number+1)
# Initialization v[copy_number][bin] = 0
v = [ { bin : 0 for bin in bins } for c in range(max_copy_number + 1) ]
bt = [ { bin : None for bin in bins } for c in range(max_copy_number + 1) ]
for idx, bin in enumerate(bins):
norm_count = float(df_norm.loc[bin][cell])
# YOUR CODE HERE
if idx == 0:
for copy_number in Q:
v[copy_number][bin] = initialLogProb(copy_number) + emissionLogProb(copy_number, norm_count)
else:
for copy_number in Q:
max_ = -np.inf
prev_copy_number = None
for prev_q in Q:
temp = v[prev_q][bins[idx-1]] + transitionLogProb(prev_q, copy_number)
if max_ < temp:
max_ = temp
prev_copy_number = prev_q
v[copy_number][bin] = max_ + emissionLogProb(copy_number, norm_count)
bt[copy_number][bin] = prev_copy_number
return v, bt
bins = sorted(list(df_norm.index))
Q = range(max_copy_number+1)
V = {}
BT = {}
C = {}
max_joint_probability = {}
for cell in list(df_norm.columns[3:]):
V[cell], BT[cell] = viterbi_bt(df_norm, cell)
C[cell] = {}
max_prob = max_joint_prob(df_norm, V[cell])
final_c = None
final_bin = bins[-1]
for c in Q:
if V[cell][c][final_bin] == max_prob:
final_c = c
C[cell][final_bin] = final_c
for idx in range(len(bins)-2, -1, -1):
bin = bins[idx]
next_bin = bins[idx + 1]
next_bin_c = C[cell][next_bin]
C[cell][bin] = BT[cell][next_bin_c][next_bin]
max_joint_probability[cell] = max_prob
print(cell, "--", "max prob:", max_prob)Editor is loading...
Leave a Comment