forked from pockerman/hidden_markov_modeling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhmm_helpers.py
152 lines (106 loc) · 4.16 KB
/
hmm_helpers.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
"""
Helpers for HMM
"""
import json
from pomegranate import*
from helpers import INFO
from exceptions import Error
class HMMCallback(object):
def __init__(self, callback):
self._callback = callback
self.model = None
def on_epoch_end(self, logs):
for key in logs.keys():
print("{0} {1}: {2}".format(INFO, key, logs[key]))
def on_training_begin(self):
pass
def on_training_end(self, logs):
pass
def save_hmm(hmm_model, configuration, win_interval_length):
json_str = hmm_model.to_json()
import json
with open(configuration["HMM"]["save_hmm_filename"]+
"_"+str(win_interval_length)+".json", 'w') as jsonfile:
json.dump(json_str, jsonfile)
def build_hmm(hmm_file):
with open(hmm_file) as json_file:
hmm_json_map = json.load(json_file)
hmm_json_map = json.loads(hmm_json_map)
hmm = HiddenMarkovModel(name=hmm_json_map["name"],
start=None, end=None)
# reade in the states
states = hmm_json_map["states"]
distribution_ties = hmm_json_map.get("distribution ties",None)
hmm, hmm_states = build_states(hmm=hmm,
states=states,
distribution_ties=distribution_ties)
hmm.start = hmm_states[hmm_json_map['start_index']]
hmm.end = hmm_states[hmm_json_map['end_index']]
# Add all the edges to the model
for start, end, probability, pseudocount, group in hmm_json_map['edges']:
hmm.add_transition(hmm_states[start], hmm_states[end], probability,
pseudocount, group)
hmm.bake(verbose=True)
return hmm
def build_states(hmm, states, distribution_ties):
state_objs = []
for state in states:
state_obj = build_state(state_map=state)
if state_obj is not None:
state_objs.append(state_obj)
if distribution_ties is not None:
for i, j in distribution_ties:
# Tie appropriate states together
states[i].tie(states[j])
hmm.add_states(state_objs)
return hmm, state_objs
def build_state(state_map):
name = state_map["name"]
weight = state_map["weight"]
dist_map = state_map["distribution"]
if dist_map is not None:
# the state has IndependentComponentsDistribution
# as a distribution. In this case we have more
# than one parameters unless we deal with a GMM
# that wraps the components
if "name" in dist_map and \
dist_map["name"] == "IndependentComponentsDistribution":
parameters = dist_map["parameters"]
dist_param = parameters[0]
components = []
for param in dist_param:
if param["class"] == "GeneralMixtureModel":
# get the dists list for the GMM
distributions = param["distributions"]
dist_list = []
for dist in distributions:
distribution = Distribution.from_json(json.dumps(dist))
dist_list.append(distribution)
weights = param["weights"]
gmm = GeneralMixtureModel(dist_list, weights=weights)
components.append(gmm)
elif param["class"] == "Distribution":
distribution = Distribution.from_json(json.dumps(param))
components.append(distribution)
# now that we collected the independent components
# construct the state
return State(IndependentComponentsDistribution(components),
name=name, weight=weight )
elif "class" in dist_map and \
dist_map["class"] == "GeneralMixtureModel":
# get the distributions list for this GMM
distributions = dist_map["distributions"]
dist_list = []
for dist in distributions:
distribution = Distribution.from_json(json.dumps(dist))
dist_list.append(distribution)
weights = dist_map["weights"]
gmm = GeneralMixtureModel(dist_list, weights=weights)
return State(gmm, name=name, weight=weight )
elif "class" in dist_map and \
dist_map["class"] == "Distribution":
distribution = Distribution.from_json(json.dumps(dist_map))
return State(distribution, name=name, weight=weight )
else:
#this means that the state has
return State(None, name=name, weight=weight)