Repository navigation
Expand file tree
/
Copy pathc_helper.pyx
More file actions
213 lines (189 loc) · 8.18 KB
/
Copy pathc_helper.pyx
File metadata and controls
213 lines (189 loc) · 8.18 KB
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
import numpy as np
cimport numpy as np
DTYPE = np.float64
ctypedef np.float64_t DTYPE_t
DTYPE_INT = np.int64
ctypedef np.int64_t DTYPE_INT_t
from libc.math cimport log
cdef extern from "math.h":
double sqrt(double m)
def c_mean_std(np.ndarray[DTYPE_t] values):
"""
More efficient method to get both mean and standard deviation
"""
cdef DTYPE_t v_mean, v_var
cdef DTYPE_INT_t idx
cdef DTYPE_INT_t v_len = values.shape[0]
v_mean = 0
for idx in range(v_len):
v_mean += values[idx]
v_mean /= v_len
v_var = 0
for idx in range(v_len):
v_var += (values[idx] - v_mean)**2
return v_mean, sqrt(v_var / v_len)
def c_new_mean_stds(np.ndarray[DTYPE_t] norm_signal not None,
np.ndarray[DTYPE_INT_t] new_segs not None):
cdef DTYPE_INT_t n_segs = new_segs.shape[0] - 1
cdef np.ndarray[DTYPE_t] means_arr = np.empty(n_segs, dtype=DTYPE)
cdef np.ndarray[DTYPE_t] stds_arr = np.empty(n_segs, dtype=DTYPE)
cdef DTYPE_t curr_sum, curr_var, seg_mean
cdef DTYPE_INT_t idx, seg_idx, seg_len
for idx in range(n_segs):
seg_len = new_segs[idx + 1] - new_segs[idx]
curr_sum = 0
for seg_idx in range(new_segs[idx], new_segs[idx + 1]):
curr_sum += norm_signal[seg_idx]
seg_mean = curr_sum / seg_len
means_arr[idx] = seg_mean
curr_var = 0
for seg_idx in range(new_segs[idx], new_segs[idx + 1]):
curr_var += (norm_signal[seg_idx] - seg_mean)**2
stds_arr[idx] = sqrt(curr_var / seg_len)
return means_arr, stds_arr
def c_new_means(np.ndarray[DTYPE_t] norm_signal not None,
np.ndarray[DTYPE_INT_t] new_segs not None):
cdef DTYPE_INT_t n_segs = new_segs.shape[0] - 1
cdef np.ndarray[DTYPE_t] means_arr = np.empty(n_segs, dtype=DTYPE)
cdef DTYPE_t curr_sum
cdef DTYPE_INT_t idx, seg_idx
for idx in range(n_segs):
curr_sum = 0
for seg_idx in range(new_segs[idx], new_segs[idx + 1]):
curr_sum += norm_signal[seg_idx]
means_arr[idx] = curr_sum / (new_segs[idx + 1] - new_segs[idx])
return means_arr
def c_apply_outlier_thresh(
np.ndarray[DTYPE_t] raw_signal, DTYPE_t lower_lim, DTYPE_t upper_lim):
cdef DTYPE_INT_t raw_size = raw_signal.shape[0]
cdef np.ndarray[DTYPE_t] clipped_signal = np.empty(raw_size, dtype=DTYPE)
cdef DTYPE_INT_t pos
cdef DTYPE_t pos_sig
for pos in range(raw_size):
pos_sig = raw_signal[pos]
if pos_sig > upper_lim:
clipped_signal[pos] = upper_lim
elif pos_sig < lower_lim:
clipped_signal[pos] = lower_lim
else:
clipped_signal[pos] = pos_sig
return clipped_signal
def c_valid_cpts_w_cap(
np.ndarray[DTYPE_t] raw_signal, DTYPE_INT_t min_base_obs,
DTYPE_INT_t running_stat_width, DTYPE_INT_t num_cpts):
cdef np.ndarray[DTYPE_t] raw_cumsum = np.cumsum(
np.concatenate([[0.0], raw_signal]))
# get difference between all neighboring running_stat_width regions
cdef np.ndarray[DTYPE_INT_t] candidate_poss = np.argsort(np.abs(
(2 * raw_cumsum[running_stat_width:-running_stat_width]) -
raw_cumsum[:-2*running_stat_width] -
raw_cumsum[2*running_stat_width:])).astype(DTYPE_INT)[::-1]
cdef np.ndarray[DTYPE_INT_t] cpts = np.empty(num_cpts, dtype=DTYPE_INT)
cpts[0] = candidate_poss[0] + running_stat_width
blacklist_pos = set(range(
candidate_poss[0] - min_base_obs + 1, candidate_poss[0] + min_base_obs))
cdef DTYPE_INT_t cand_pos
cdef DTYPE_INT_t num_cands = candidate_poss.shape[0] - (
2 * running_stat_width)
cdef DTYPE_INT_t cand_idx = 1
cdef DTYPE_INT_t added_cpts = 1
while added_cpts < num_cpts:
cand_pos = candidate_poss[cand_idx]
if cand_pos not in blacklist_pos:
cpts[added_cpts] = cand_pos + running_stat_width
added_cpts += 1
blacklist_pos.update(range(
cand_pos - min_base_obs + 1, cand_pos + min_base_obs))
cand_idx += 1
if cand_idx >= num_cands:
raise NotImplementedError, 'Fewer changepoints found than requested'
return cpts
def c_valid_cpts(np.ndarray[DTYPE_t] raw_signal, DTYPE_INT_t min_base_obs,
DTYPE_INT_t running_stat_width):
cdef np.ndarray[DTYPE_t] raw_cumsum = np.cumsum(
np.concatenate([[0.0], raw_signal]))
# get difference between all neighboring running_stat_width regions
cdef np.ndarray[DTYPE_INT_t] candidate_poss = np.argsort(np.abs(
(2 * raw_cumsum[running_stat_width:-running_stat_width]) -
raw_cumsum[:-2*running_stat_width] -
raw_cumsum[2*running_stat_width:])).astype(DTYPE_INT)[::-1]
cpts = [candidate_poss[0]]
blacklist_pos = set()
cdef DTYPE_INT_t pos
for pos in candidate_poss[1:]:
if pos not in blacklist_pos:
cpts.append(pos)
blacklist_pos.update(range(
pos-min_base_obs+1, pos+min_base_obs+1))
return np.array(cpts) + running_stat_width
def c_valid_cpts_w_cap_t_test(
np.ndarray[DTYPE_t] raw_signal, DTYPE_INT_t min_base_obs,
DTYPE_INT_t running_stat_width, DTYPE_INT_t num_cpts):
cdef DTYPE_INT_t pos, idx
cdef DTYPE_t pos_diff, m1, m2, var1, var2
cdef DTYPE_INT_t num_cands = raw_signal.shape[0] - (running_stat_width * 2)
# note these will not actually be t-scores, but will be a monotonic transform
# so the rank order will be the same
cdef np.ndarray[DTYPE_t] t_scores = np.empty(num_cands, dtype=DTYPE)
for pos in range(num_cands):
# compute means
m1 = 0
for idx in range(running_stat_width):
m1 += raw_signal[pos + idx]
m1 /= running_stat_width
m2 = 0
for idx in range(running_stat_width):
m2 += raw_signal[pos + running_stat_width + idx]
m2 /= running_stat_width
# compute sum of variances
var1 = 0
for idx in range(running_stat_width):
pos_diff = raw_signal[pos + idx] - m1
var1 += pos_diff * pos_diff
var2 = 0
for idx in range(running_stat_width):
pos_diff = raw_signal[pos + running_stat_width + idx] - m2
var2 += pos_diff * pos_diff
if var1 + var2 == 0:
t_scores[pos] = 0.0
elif m1 > m2:
t_scores[pos] = (m1 - m2) / sqrt(var1 + var2)
else:
t_scores[pos] = (m2 - m1) / sqrt(var1 + var2)
cdef np.ndarray[DTYPE_INT_t] candidate_poss = np.argsort(
t_scores).astype(DTYPE_INT)[::-1]
cdef np.ndarray[DTYPE_INT_t] cpts = np.empty(num_cpts, dtype=DTYPE_INT)
cpts[0] = candidate_poss[0] + running_stat_width
blacklist_pos = set(range(
candidate_poss[0] - min_base_obs + 1, candidate_poss[0] + min_base_obs))
cdef DTYPE_INT_t cand_pos
cdef DTYPE_INT_t cand_idx = 1
cdef DTYPE_INT_t added_cpts = 1
while added_cpts < num_cpts:
cand_pos = candidate_poss[cand_idx]
if cand_pos not in blacklist_pos:
cpts[added_cpts] = cand_pos + running_stat_width
added_cpts += 1
blacklist_pos.update(range(
cand_pos - min_base_obs + 1, cand_pos + min_base_obs))
cand_idx += 1
if cand_idx >= num_cands:
raise NotImplementedError, 'Fewer changepoints found than requested.'
return cpts
def c_calc_llh_ratio(
np.ndarray[DTYPE_t] reg_means,
np.ndarray[DTYPE_t] reg_ref_means, np.ndarray[DTYPE_t] reg_ref_vars,
np.ndarray[DTYPE_t] reg_alt_means, np.ndarray[DTYPE_t] reg_alt_vars):
cdef DTYPE_t ref_z_sum, ref_log_var_sum, alt_z_sum, alt_log_var_sum
ref_z_sum, ref_log_var_sum, alt_z_sum, alt_log_var_sum = 0.0, 0.0 ,0.0 ,0.0
cdef DTYPE_t ref_diff, alt_diff, log_lh_ratio
cdef DTYPE_INT_t idx
for idx in range(reg_means.shape[0]):
ref_diff = reg_means[idx] - reg_ref_means[idx]
ref_z_sum += (ref_diff * ref_diff) / reg_ref_vars[idx]
ref_log_var_sum += log(reg_ref_vars[idx])
alt_diff = reg_means[idx] - reg_alt_means[idx]
alt_z_sum += (alt_diff * alt_diff) / reg_alt_vars[idx]
alt_log_var_sum += log(reg_alt_vars[idx])
log_lh_ratio = alt_z_sum + alt_log_var_sum - ref_z_sum - ref_log_var_sum
return log_lh_ratio