Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# 4 Hidden States:
# 0--start, 1--downstream, 2--upstream, 3--end
numdists = 3 # Three-distribution Gaussian Mixtures
var = 7.5 / (numdists - 1)
means = [[], [], [], []]
for i in range(numdists):
means[3].append(i * 7.5 / ( numdists - 1 ) + 2.5)
means[2].append(i * 7.5 / ( numdists - 1 ))
means[1].append(-i * 7.5 / ( numdists - 1 ))
means[0].append(-i * 7.5 / ( numdists - 1 ) - 2.5)
states = []
for i, m in enumerate(means):
tmp = []
for j in m:
tmp.append(NormalDistribution(j, var))
mixture = GeneralMixtureModel(tmp)
states.append(State(mixture, name=str(i)))
hmm.add_states(*tuple(states))
# Transmission matrix
#A = [[0., 1., 0., 0.],
# [0., 0.5, 0.5, 0.],
# [0., 0., 0.5, 0.5],
# [1., 0., 0., 0.]]
hmm.add_transition(states[0], states[1], 1)
hmm.add_transition(states[1], states[1], 0.5)
hmm.add_transition(states[1], states[2], 0.5)
hmm.add_transition(states[2], states[2], 0.5)
hmm.add_transition(states[2], states[3], 0.5)
hmm.add_transition(states[3], states[0], 1)
#pi = [0.2, 0.3, 0.3, 0.2]
def load_segmentation_model(modeldata):
model = HiddenMarkovModel('model')
states = {}
for s in modeldata:
if len(s['emission']) == 1:
emission = NormalDistribution(*s['emission'][0][:2])
else:
weights = np.array([w for _, _, w in s['emission']])
dists = [NormalDistribution(mu, sigma)
for mu, sigma, _ in s['emission']]
emission = GeneralMixtureModel(dists, weights=weights)
state = State(emission, name=s['name'])
states[s['name']] = state
model.add_state(state)
if 'start_prob' in s:
model.add_transition(model.start, state, s['start_prob'])
for s in modeldata:
current = states[s['name']]
for nextstate, prob in s['transition']:
model.add_transition(current, states[nextstate], prob)
model.bake()
return model