-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathgen_synthetic.py
191 lines (138 loc) · 5.17 KB
/
gen_synthetic.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
from __future__ import division
import numpy as np
from util import make_mask, make_mask_prediction
def generate_data(tran, emit, T, miss=0., nmasks=1):
""" Generate synthetic data from finite HMM with continuous emissions.
A `miss` fraction of data is marked as missing. `nmasks` sets of
missing data are created.
tran : K x K discrete transition matrix.
emit : K x 1 vector of continuous emission distributions.
T : Number of observations to generate.
miss : Fraction of observations that are missing. Tries to pick evenly
over all states.
nmasks : Number of missing masks to generate.
"""
# doesn't matter for now because we're only dealing with one sequence of
# observations
K = tran.shape[0]
curr_st = 0
obs = []
sts = [0]
obs.append(emit[0].rvs()[0])
for i in xrange(T-1):
# transition
# pass in probability of 1
curr_st = np.random.choice(K, p=tran[curr_st,:])
sts.append(curr_st)
# emit an observation
point = emit[curr_st].rvs()[0]
obs.append(point)
obs = np.array(obs)
sts = np.array(sts)
masks = None
if miss > 0.:
masks = list()
for i in xrange(nmasks):
masks.append(make_mask(sts, miss))
# Backwards compatability with old tests
if len(masks) == 1:
masks = masks[0]
return obs, sts, masks
def generate_data_smoothing(tran, emit, T, miss=0., left=0, nmasks=1):
""" Generate synthetic data from finite HMM with continuous emissions.
A `miss` fraction of data to the right of index `left` is marked as
missing. `nmasks` sets of missing data are created.
tran : K x K transition matrix (row normalized).
emit : 1-d array-like of size K containing continuous emissions.
T : Length of resulting observation sequence.
miss : Fraction of observations that are missing.
left : Index of leftmost observation that can be missing.
nmasks : Number of missing masks to generate.
"""
# doesn't matter for now because we're only dealing with one sequence of
# observations
K = tran.shape[0]
curr_st = 0
obs = []
sts = [0]
obs.append(emit[0].rvs()[0])
for i in xrange(T-1):
# transition
# pass in probability of 1
curr_st = np.random.choice(K, p=tran[curr_st,:])
sts.append(curr_st)
# emit an observation
point = emit[curr_st].rvs()[0]
obs.append(point)
obs = np.array(obs)
sts = np.array(sts)
masks = None
if miss > 0.:
masks = list()
for i in xrange(nmasks):
masks.append(make_mask(sts, miss, left))
# Backwards compatability with old tests
if len(masks) == 1:
masks = masks[0]
return obs, sts, masks
def generate_data_prediction(tran, emit, T, miss=0., nmasks=1):
""" Generate synthetic data from finite HMM with continuous emissions.
A `miss` fraction of data at the end of the observation sequence is
marked as missing. `nmasks` sets of missing data are created.
tran : K x K transition matrix (row normalized).
emit : 1-d array-like of size K containing continuous emissions.
T : Length of resulting observation sequence.
miss : Fraction of observations that are missing.
nmasks : Number of missing masks to generate.
"""
# doesn't matter for now because we're only dealing with one sequence of
# observations
K = tran.shape[0]
curr_st = 0
obs = []
sts = [0]
obs.append(emit[0].rvs()[0])
for i in xrange(T-1):
# transition
# pass in probability of 1
curr_st = np.random.choice(K, p=tran[curr_st,:])
sts.append(curr_st)
# emit an observation
point = emit[curr_st].rvs()[0]
obs.append(point)
obs = np.array(obs)
sts = np.array(sts)
masks = None
if miss > 0:
masks = list()
for i in xrange(nmasks):
masks.append(make_mask_prediction(sts, miss))
if len(masks) == 1:
masks = masks[0]
return obs, sts, masks
def generate_data_mmap(tran, emit, T):
""" This writes large sequences of observations to disk using mmap
Uses numpy's memmap here isntead of mmap which is better for
storing numpy arrays
"""
fpo = np.memmap('obs.dat', dtype='float64', mode='w+', shape=(T, tran.shape[0]))
fps = np.memmap('sts.dat', dtype='int32', mode='w+', shape=(T, 1))
states = np.arange(tran.shape[0])
curr_st = 0
fps[0,:] = 0
fpo[0,:] = emit[0].rvs()[0]
for i in xrange(T - 1):
# transition
# pass in probability of 1
curr_st = np.random.choice(states, p=tran[curr_st,:])
fps[i,:] = curr_st
# emit an observation
fpo[i,:] = emit[curr_st].rvs()[0]
# Delete the file pointers to flush the changes to memory. Here we're only
# using the filenames obs.dat and sts.dat
del fps
del fpo
def read_data_mmap(N, T, size):
fp = np.memmap('obs.dat', dtype='float64', mode='r', shape=(T, N))
for i in xrange(T//size):
yield np.array(fp[i*size:(i+1)*size,:])