-
Notifications
You must be signed in to change notification settings - Fork 2
/
_emission.pyx
332 lines (300 loc) · 12.8 KB
/
_emission.pyx
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
from libc.math cimport exp, log
import numpy as np
cimport numpy as np
cimport cython
from .track import TrackTable
np.import_array()
ctypedef np.float64_t dtype_t
ctypedef np.int32_t itype_t
cdef dtype_t _NINF = -np.inf
cdef dtype_t _MINDBL = -1e20
def canFast(obs):
return isinstance(obs, TrackTable) or (
isinstance(obs, np.ndarray) and (obs.dtype == np.int32 or
obs.dtype == np.uint16 or
obs.dtype == np.uint8))
@cython.boundscheck(False)
def fastAllLogProbs(obs, logProbs, outProbs, normalize, segRatios):
if isinstance(obs, TrackTable):
obs = obs.getNumPyArray()
assert isinstance(obs, np.ndarray)
assert isinstance(logProbs, np.ndarray)
assert isinstance(outProbs, np.ndarray)
assert len(obs.shape) == 2
assert len(logProbs.shape) == 3
assert logProbs.dtype == np.float
assert outProbs.dtype == np.float
assert outProbs.shape[0] == obs.shape[0]
assert logProbs.shape[0] == obs.shape[1]
cdef itype_t nObs = obs.shape[0]
cdef itype_t nTracks = obs.shape[1]
cdef itype_t nStates = logProbs.shape[1]
if obs.dtype == np.int32:
_fastAllLogProbs32(nObs, nTracks, nStates, obs, logProbs, outProbs,
normalize, segRatios)
elif obs.dtype == np.uint16:
_fastAllLogProbsU16(nObs, nTracks, nStates, obs, logProbs, outProbs,
normalize, segRatios)
elif obs.dtype == np.uint8:
_fastAllLogProbsU8(nObs, nTracks, nStates, obs, logProbs, outProbs,
normalize, segRatios)
else:
print obs.dtype
assert False
@cython.boundscheck(False)
def _fastAllLogProbsU8(itype_t nObs, itype_t nTracks, itype_t nStates,
np.ndarray[np.uint8_t, ndim=2] obs,
np.ndarray[dtype_t, ndim=3] logProbs,
np.ndarray[dtype_t, ndim=2] outProbs,
dtype_t normalize,
np.ndarray[dtype_t, ndim=1] segRatios):
cdef itype_t i, j, k
cdef dtype_t minDbl = _MINDBL
cdef dtype_t maxProb = _MINDBL
cdef itype_t hasRatio = 0
if segRatios is not None:
hasRatio = 1
with nogil:
for i in xrange(nObs):
for j in xrange(nStates):
outProbs[i,j] = 0.0
for k in xrange(nTracks):
outProbs[i, j] += logProbs[k, j, obs[i, k]]
outProbs[i, j] *= normalize
if hasRatio == 1:
outProbs[i, j] *= segRatios[i]
if outProbs[i, j] > maxProb:
maxProb = outProbs[i, j]
# no state that can emit symbol so we give every state 0
# NOTE: should print a warning message since this implies
# that data impossible with model.
if maxProb == minDbl:
for j in xrange(nStates):
outProbs[i, j] = 0.0
@cython.boundscheck(False)
def _fastAllLogProbsU16(itype_t nObs, itype_t nTracks, itype_t nStates,
np.ndarray[np.uint16_t, ndim=2] obs,
np.ndarray[dtype_t, ndim=3] logProbs,
np.ndarray[dtype_t, ndim=2] outProbs,
dtype_t normalize,
np.ndarray[dtype_t, ndim=1] segRatios):
cdef itype_t i, j, k
cdef dtype_t minDbl = _MINDBL
cdef dtype_t maxProb = _MINDBL
cdef itype_t hasRatio = 0
if segRatios is not None:
hasRatio = 1
with nogil:
for i in xrange(nObs):
for j in xrange(nStates):
outProbs[i,j] = 0.0
for k in xrange(nTracks):
outProbs[i, j] += logProbs[k, j, obs[i, k]]
outProbs[i, j] *= normalize
if hasRatio == 1:
outProbs[i, j] *= segRatios[i]
if outProbs[i, j] > maxProb:
maxProb = outProbs[i, j]
# no state that can emit symbol so we give every state 0
# NOTE: should print a warning message since this implies
# that data impossible with model.
if maxProb == minDbl:
for j in xrange(nStates):
outProbs[i, j] = 0.0
@cython.boundscheck(False)
def _fastAllLogProbs32(itype_t nObs, itype_t nTracks, itype_t nStates,
np.ndarray[np.int32_t, ndim=2] obs,
np.ndarray[dtype_t, ndim=3] logProbs,
np.ndarray[dtype_t, ndim=2] outProbs,
dtype_t normalize,
np.ndarray[dtype_t, ndim=1] segRatios):
cdef itype_t i, j, k
cdef dtype_t minDbl = _MINDBL
cdef dtype_t maxProb = _MINDBL
cdef itype_t hasRatio = 0
if segRatios is not None:
hasRatio = 1
with nogil:
for i in xrange(nObs):
for j in xrange(nStates):
outProbs[i,j] = 0.0
for k in xrange(nTracks):
outProbs[i, j] += logProbs[k, j, obs[i, k]]
outProbs[i, j] *= normalize
if hasRatio == 1:
outProbs[i, j] *= segRatios[i]
if outProbs[i, j] > maxProb:
maxProb = outProbs[i, j]
# no state that can emit symbol so we give every state 0
# NOTE: should print a warning message since this implies
# that data impossible with model.
if maxProb == minDbl:
for j in xrange(nStates):
outProbs[i, j] = 0.0
@cython.boundscheck(False)
def fastAccumulateStats(obs, obsStats, posteriors, segRatios):
if isinstance(obs, TrackTable):
obs = obs.getNumPyArray()
assert isinstance(obs, np.ndarray)
assert len(obs.shape) == 2
cdef itype_t nObs = obs.shape[0]
cdef itype_t nTracks = obs.shape[1]
cdef itype_t nStates = obsStats[0].shape[0]
if obs.dtype == np.int32:
_fastAccumulateStats32(nObs, nTracks, nStates, obs, obsStats,
posteriors, segRatios)
elif obs.dtype == np.uint16:
_fastAccumulateStatsU16(nObs, nTracks, nStates, obs, obsStats,
posteriors, segRatios)
elif obs.dtype == np.uint8:
_fastAccumulateStatsU8(nObs, nTracks, nStates, obs, obsStats,
posteriors, segRatios)
else:
assert False
@cython.boundscheck(False)
def _fastAccumulateStatsU8(itype_t nObs, itype_t nTracks, itype_t nStates,
np.ndarray[np.uint8_t, ndim=2] obs,
np.ndarray[dtype_t, ndim=3] obsStats,
np.ndarray[dtype_t, ndim=2] posteriors,
np.ndarray[dtype_t, ndim=1] segRatios):
cdef itype_t i, track, state, obsVal
cdef dtype_t segProb
cdef itype_t hasRatio = 0
if segRatios is not None:
hasRatio = 1
with nogil:
for i in xrange(nObs):
for track in xrange(nTracks):
obsVal = obs[i,track]
for state in xrange(nStates):
segProb = posteriors[i, state]
if hasRatio == 1:
segProb *= segRatios[i]
obsStats[track, state, obsVal] += segProb
@cython.boundscheck(False)
def _fastAccumulateStatsU16(itype_t nObs, itype_t nTracks, itype_t nStates,
np.ndarray[np.uint16_t, ndim=2] obs,
np.ndarray[dtype_t, ndim=3] obsStats,
np.ndarray[dtype_t, ndim=2] posteriors,
np.ndarray[dtype_t, ndim=1] segRatios):
cdef itype_t i, track, state, obsVal
cdef dtype_t segProb
cdef itype_t hasRatio = 0
if segRatios is not None:
hasRatio = 1
with nogil:
for i in xrange(nObs):
for track in xrange(nTracks):
obsVal = obs[i,track]
for state in xrange(nStates):
segProb = posteriors[i, state]
if hasRatio == 1:
segProb *= segRatios[i]
obsStats[track, state, obsVal] += segProb
@cython.boundscheck(False)
def _fastAccumulateStats32(itype_t nObs, itype_t nTracks, itype_t nStates,
np.ndarray[np.int32_t, ndim=2] obs,
np.ndarray[dtype_t, ndim=3] obsStats,
np.ndarray[dtype_t, ndim=2] posteriors,
np.ndarray[dtype_t, ndim=1] segRatios):
cdef itype_t i, track, state, obsVal
cdef dtype_t segProb
cdef itype_t hasRatio = 0
if segRatios is not None:
hasRatio = 1
with nogil:
for i in xrange(nObs):
for track in xrange(nTracks):
obsVal = obs[i,track]
for state in xrange(nStates):
segProb = posteriors[i, state]
if hasRatio == 1:
segProb *= segRatios[i]
obsStats[track, state, obsVal] += segProb
@cython.boundscheck(False)
def fastUpdateCounts(bedInterval, trackTable, obsStats, segRatios):
assert isinstance(trackTable, TrackTable)
obs = trackTable.getNumPyArray()
assert isinstance(obs, np.ndarray)
assert len(obs.shape) == 2
cdef itype_t tableStart = trackTable.getStart()
cdef itype_t start = bedInterval[1]
cdef itype_t end = bedInterval[2]
cdef itype_t symbol = bedInterval[3]
cdef itype_t nObs = obs.shape[0]
cdef itype_t nTracks = obs.shape[1]
cdef itype_t nStates = obsStats[0].shape[0]
if obs.dtype == np.int32:
_fastUpdateCounts32(nObs, nTracks, nStates, start, end, symbol,
tableStart, obs, obsStats, segRatios)
elif obs.dtype == np.uint16:
_fastUpdateCountsU16(nObs, nTracks, nStates, start, end, symbol,
tableStart, obs, obsStats, segRatios)
elif obs.dtype == np.uint8:
_fastUpdateCountsU8(nObs, nTracks, nStates, start, end, symbol,
tableStart, obs, obsStats, segRatios)
else:
assert False
@cython.boundscheck(False)
def _fastUpdateCounts32(itype_t nObs, itype_t nTracks, itype_t nStates,
itype_t start, itype_t end, itype_t symbol,
itype_t tableStart,
np.ndarray[np.int32_t, ndim=2] obs,
np.ndarray[dtype_t, ndim=3] obsStats,
np.ndarray[dtype_t, ndim=1] segRatios):
cdef itype_t pos, tablePos, track
cdef dtype_t val
cdef itype_t hasRatio = 0
if segRatios is not None:
hasRatio = 1
with nogil:
for pos in xrange(start, end):
# note that pos must be in table-relative coordinates
# (ie as from getOverlapinTableCoords())
val = 1.0
if hasRatio == 1:
val = segRatios[pos]
for track in xrange(nTracks):
obsStats[track, symbol, obs[pos, track]] += val
@cython.boundscheck(False)
def _fastUpdateCountsU16(itype_t nObs, itype_t nTracks, itype_t nStates,
itype_t start, itype_t end, itype_t symbol,
itype_t tableStart,
np.ndarray[np.uint16_t, ndim=2] obs,
np.ndarray[dtype_t, ndim=3] obsStats,
np.ndarray[dtype_t, ndim=1] segRatios):
cdef itype_t pos, tablePos, track
cdef dtype_t val
cdef itype_t hasRatio = 0
if segRatios is not None:
hasRatio = 1
with nogil:
for pos in xrange(start, end):
# note that pos must be in table-relative coordinates
# (ie as from getOverlapinTableCoords())
val = 1.0
if hasRatio == 1:
val = segRatios[pos]
for track in xrange(nTracks):
obsStats[track, symbol, obs[pos, track]] += val
@cython.boundscheck(False)
def _fastUpdateCountsU8(itype_t nObs, itype_t nTracks, itype_t nStates,
itype_t start, itype_t end, itype_t symbol,
itype_t tableStart,
np.ndarray[np.uint8_t, ndim=2] obs,
np.ndarray[dtype_t, ndim=3] obsStats,
np.ndarray[dtype_t, ndim=1] segRatios):
cdef itype_t pos, tablePos, track
cdef dtype_t val
cdef itype_t hasRatio = 0
if segRatios is not None:
hasRatio = 1
with nogil:
for pos in xrange(start, end):
# note that pos must be in table-relative coordinates
# (ie as from getOverlapinTableCoords())
val = 1.0
if hasRatio == 1:
val = segRatios[pos]
for track in xrange(nTracks):
obsStats[track, symbol, obs[pos, track]] += val