Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

outcomes.html 28 KB

You have to be logged in to leave a comment. Sign In
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
  6. <meta name="generator" content="pdoc 0.7.2" />
  7. <title>src.outcomes API documentation</title>
  8. <meta name="description" content="" />
  9. <link href='https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.0/normalize.min.css' rel='stylesheet'>
  10. <link href='https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/8.0.0/sanitize.min.css' rel='stylesheet'>
  11. <link href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/github.min.css" rel="stylesheet">
  12. <style>.flex{display:flex !important}body{line-height:1.5em}#content{padding:20px}#sidebar{padding:30px;overflow:hidden}.http-server-breadcrumbs{font-size:130%;margin:0 0 15px 0}#footer{font-size:.75em;padding:5px 30px;border-top:1px solid #ddd;text-align:right}#footer p{margin:0 0 0 1em;display:inline-block}#footer p:last-child{margin-right:30px}h1,h2,h3,h4,h5{font-weight:300}h1{font-size:2.5em;line-height:1.1em}h2{font-size:1.75em;margin:1em 0 .50em 0}h3{font-size:1.4em;margin:25px 0 10px 0}h4{margin:0;font-size:105%}a{color:#058;text-decoration:none;transition:color .3s ease-in-out}a:hover{color:#e82}.title code{font-weight:bold}h2[id^="header-"]{margin-top:2em}.ident{color:#900}pre code{background:#f8f8f8;font-size:.8em;line-height:1.4em}code{background:#f2f2f1;padding:1px 4px;overflow-wrap:break-word}h1 code{background:transparent}pre{background:#f8f8f8;border:0;border-top:1px solid #ccc;border-bottom:1px solid #ccc;margin:1em 0;padding:1ex}#http-server-module-list{display:flex;flex-flow:column}#http-server-module-list div{display:flex}#http-server-module-list dt{min-width:10%}#http-server-module-list p{margin-top:0}.toc ul,#index{list-style-type:none;margin:0;padding:0}#index code{background:transparent}#index h3{border-bottom:1px solid #ddd}#index ul{padding:0}#index h4{font-weight:bold}#index h4 + ul{margin-bottom:.6em}@media (min-width:200ex){#index .two-column{column-count:2}}@media (min-width:300ex){#index .two-column{column-count:3}}dl{margin-bottom:2em}dl dl:last-child{margin-bottom:4em}dd{margin:0 0 1em 3em}#header-classes + dl > dd{margin-bottom:3em}dd dd{margin-left:2em}dd p{margin:10px 0}.name{background:#eee;font-weight:bold;font-size:.85em;padding:5px 10px;display:inline-block;min-width:40%}.name:hover{background:#e0e0e0}.name > span:first-child{white-space:nowrap}.name.class > span:nth-child(2){margin-left:.4em}.inherited{color:#999;border-left:5px solid #eee;padding-left:1em}.inheritance em{font-style:normal;font-weight:bold}.desc h2{font-weight:400;font-size:1.25em}.desc h3{font-size:1em}.desc dt code{background:inherit}.source summary,.git-link-div{color:#666;text-align:right;font-weight:400;font-size:.8em;text-transform:uppercase}.source summary > *{white-space:nowrap;cursor:pointer}.git-link{color:inherit;margin-left:1em}.source pre{max-height:500px;overflow:auto;margin:0}.source pre code{font-size:12px;overflow:visible}.hlist{list-style:none}.hlist li{display:inline}.hlist li:after{content:',\2002'}.hlist li:last-child:after{content:none}.hlist .hlist{display:inline;padding-left:1em}img{max-width:100%}.admonition{padding:.1em .5em;margin-bottom:1em}.admonition-title{font-weight:bold}.admonition.note,.admonition.info,.admonition.important{background:#aef}.admonition.todo,.admonition.versionadded,.admonition.tip,.admonition.hint{background:#dfd}.admonition.warning,.admonition.versionchanged,.admonition.deprecated{background:#fd4}.admonition.error,.admonition.danger,.admonition.caution{background:lightpink}</style>
  13. <style media="screen and (min-width: 700px)">@media screen and (min-width:700px){#sidebar{width:30%}#content{width:70%;max-width:100ch;padding:3em 4em;border-left:1px solid #ddd}pre code{font-size:1em}.item .name{font-size:1em}main{display:flex;flex-direction:row-reverse;justify-content:flex-end}.toc ul ul,#index ul{padding-left:1.5em}.toc > ul > li{margin-top:.5em}}</style>
  14. <style media="print">@media print{#sidebar h1{page-break-before:always}.source{display:none}}@media print{*{background:transparent !important;color:#000 !important;box-shadow:none !important;text-shadow:none !important}a[href]:after{content:" (" attr(href) ")";font-size:90%}a[href][title]:after{content:none}abbr[title]:after{content:" (" attr(title) ")"}.ir a:after,a[href^="javascript:"]:after,a[href^="#"]:after{content:""}pre,blockquote{border:1px solid #999;page-break-inside:avoid}thead{display:table-header-group}tr,img{page-break-inside:avoid}img{max-width:100% !important}@page{margin:0.5cm}p,h2,h3{orphans:3;widows:3}h1,h2,h3,h4,h5,h6{page-break-after:avoid}}</style>
  15. </head>
  16. <body>
  17. <main>
  18. <article id="content">
  19. <header>
  20. <h1 class="title">Module <code>src.outcomes</code></h1>
  21. </header>
  22. <section id="section-intro">
  23. <details class="source">
  24. <summary>
  25. <span>Expand source code</span>
  26. </summary>
  27. <pre><code class="python">import numpy as np
  28. import pandas as pd
  29. pd.options.mode.chained_assignment = None # default=&#39;warn&#39; - caution: this turns off setting with copy warning
  30. from viz import *
  31. def add_rule_based_label(df):
  32. df[&#39;Y_peak_time_frac&#39;] = df[&#39;Y_peak_idx&#39;].values / df[&#39;lifetime&#39;].values
  33. df[&#39;y_z_score&#39;] = (df[&#39;Y_max&#39;].values - df[&#39;Y_mean&#39;].values) / df[&#39;Y_std&#39;].values
  34. X_max_around_Y_peak = []
  35. X_max_after_Y_peak = []
  36. for i in range(len(df)):
  37. pt = df[&#39;Y_peak_idx&#39;].values[i]
  38. lt = df[&#39;lifetime&#39;].values[i]
  39. left_bf = np.int(0.2 * lt) + 1 # look at a window with length = 30%*lifetime
  40. right_bf = np.int(0.1 * lt) + 1
  41. arr_around = df[&#39;X&#39;].iloc[i][max(0, pt - left_bf): min(pt + right_bf, lt)]
  42. arr_after = df[&#39;X&#39;].iloc[i][min(pt + right_bf, lt - 1):]
  43. X_max_around_Y_peak.append(max(arr_around))
  44. if len(arr_after) &gt; 0:
  45. X_max_after_Y_peak.append(max(arr_after))
  46. else:
  47. X_max_after_Y_peak.append(max(arr_around))
  48. df[&#39;X_max_around_Y_peak&#39;] = X_max_around_Y_peak
  49. df[&#39;X_max_after_Y_peak&#39;] = X_max_after_Y_peak
  50. df[&#39;X_max_diff&#39;] = df[&#39;X_max_around_Y_peak&#39;] - df[&#39;X_max_after_Y_peak&#39;]
  51. def rule_based_model(track):
  52. # three rules:
  53. # if aux peaks too early -- negative
  54. # elif:
  55. # if y_consec_sig or y_conservative_thresh or (cla drops around aux peak, and aux max is greater than
  56. # mean + 2.6*std), then positive
  57. # else: negative
  58. if track[&#39;Y_peak_time_frac&#39;] &lt; 0.2:
  59. return 0
  60. if track[&#39;y_consec_sig&#39;] or track[&#39;y_conservative_thresh&#39;]:
  61. return 1
  62. # if track[&#39;X_max_diff&#39;] &gt; 260 and track[&#39;y_z_score&#39;] &gt; 2.6:
  63. # return 1
  64. if track[&#39;X_max_diff&#39;] &gt; 260 and track[&#39;Y_max&#39;] &gt; 560:
  65. return 1
  66. return 0
  67. df[&#39;y_rule_based&#39;] = np.array([rule_based_model(df.iloc[i]) for i in range(len(df))])
  68. return df
  69. def add_outcomes(df, LABELS=None, thresh=3.25, p_thresh=0.05, aux_peak=642.375, aux_thresh=973):
  70. &#39;&#39;&#39;Add binary outcome of whether spike happened and info on whether events were questionable
  71. &#39;&#39;&#39;
  72. df[&#39;y_score&#39;] = df[&#39;Y_max&#39;].values - (df[&#39;Y_mean&#39;].values + thresh * df[&#39;Y_std&#39;].values)
  73. df[&#39;y_thresh&#39;] = (df[&#39;y_score&#39;].values &gt; 0).astype(np.int) # Y_max was big
  74. df[&#39;y&#39;] = df[&#39;Y_max&#39;] &gt; aux_peak
  75. # outcomes based on significant p-values
  76. num_sigs = [np.array(df[&#39;Y_pvals&#39;].iloc[i]) &lt; p_thresh for i in range(df.shape[0])]
  77. df[&#39;y_num_sig&#39;] = np.array([num_sigs[i].sum() for i in range(df.shape[0])]).astype(np.int)
  78. df[&#39;y_single_sig&#39;] = np.array([num_sigs[i].sum() &gt; 0 for i in range(df.shape[0])]).astype(np.int)
  79. df[&#39;y_double_sig&#39;] = np.array([num_sigs[i].sum() &gt; 1 for i in range(df.shape[0])]).astype(np.int)
  80. df[&#39;y_conservative_thresh&#39;] = (df[&#39;Y_max&#39;].values &gt; aux_thresh).astype(np.int)
  81. y_consec_sig = []
  82. y_sig_min_diff = []
  83. for i in range(df.shape[0]):
  84. idxs_sig = np.where(num_sigs[i] == 1)[0] # indices of significance
  85. if len(idxs_sig) &gt; 1:
  86. y_sig_min_diff.append(np.min(np.diff(idxs_sig)))
  87. else:
  88. y_sig_min_diff.append(np.nan)
  89. # find whether there were consecutive sig. indices
  90. if len(idxs_sig) &gt; 1 and np.min(np.diff(idxs_sig)) == 1:
  91. y_consec_sig.append(1)
  92. else:
  93. y_consec_sig.append(0)
  94. df[&#39;y_consec_sig&#39;] = y_consec_sig
  95. df[&#39;y_sig_min_diff&#39;] = y_sig_min_diff
  96. df[&#39;y_consec_thresh&#39;] = np.logical_or(df[&#39;y_consec_sig&#39;], df[&#39;y_conservative_thresh&#39;])
  97. def add_hotspots(df, num_sigs, outcome_def=&#39;consec_sig&#39;):
  98. &#39;&#39;&#39;Identify hotspots as any track which over its time course has multiple events
  99. events must meet the event definition, then for a time not meet it, then meet it again
  100. Example: two consecutive significant p-values, then non-significant p-value, then 2 more consecutive p-values
  101. &#39;&#39;&#39;
  102. if outcome_def == &#39;consec_sig&#39;:
  103. hotspots = np.zeros(df.shape[0]).astype(np.int)
  104. for i in range(df.shape[0]):
  105. idxs_sig = np.where(num_sigs[i] == 1)[0] # indices of significance
  106. if idxs_sig.size &lt; 5:
  107. hotspots[i] = 0
  108. else:
  109. diffs = np.diff(idxs_sig)
  110. consecs = np.where(diffs == 1)[0] # diffs==1 means there were consecutive sigs
  111. consec_diffs = np.diff(consecs)
  112. if consec_diffs.shape[0] &gt; 0 and np.max(
  113. consec_diffs) &gt; 2: # there were greated than 2 non-consec sigs between the consec sigs
  114. hotspots[i] = 1
  115. else:
  116. hotspots[i] = 0
  117. df[&#39;sig_idxs&#39;] = num_sigs
  118. df[&#39;hotspots&#39;] = hotspots == 1
  119. return df
  120. df = add_hotspots(df, num_sigs)
  121. if LABELS is not None:
  122. df[&#39;y_consec_thresh&#39;][df.pid.isin(LABELS[&#39;pos&#39;])] = 1 # add manual pos labels
  123. df[&#39;y_consec_thresh&#39;][df.pid.isin(LABELS[&#39;neg&#39;])] = 0 # add manual neg labels
  124. df[&#39;hotspots&#39;][df.pid.isin(LABELS[&#39;hotspots&#39;])] = True # add manual hotspot labels
  125. df = add_rule_based_label(df)
  126. return df
  127. def add_sig_mean(df, resp_tracks=[&#39;Y&#39;]):
  128. &#34;&#34;&#34;add response of regression problem: mean auxilin strength among significant observations
  129. &#34;&#34;&#34;
  130. for track in resp_tracks:
  131. sig_mean = []
  132. for i in range(len(df)):
  133. r = df.iloc[i]
  134. sigs = np.array(r[f&#39;{track}_pvals&#39;]) &lt; 0.05
  135. if sum(sigs)&gt;0:
  136. sig_mean.append(np.mean(np.array(r[track])[sigs]))
  137. else:
  138. sig_mean.append(0)
  139. df[f&#39;{track}_sig_mean&#39;] = sig_mean
  140. df[f&#39;{track}_sig_mean_normalized&#39;] = sig_mean
  141. for cell in set(df[&#39;cell_num&#39;]):
  142. cell_idx = np.where(df[&#39;cell_num&#39;].values == cell)[0]
  143. y = df[f&#39;{track}_sig_mean&#39;].values[cell_idx]
  144. df[f&#39;{track}_sig_mean_normalized&#39;].values[cell_idx] = (y - np.mean(y))/np.std(y)
  145. return df
  146. def add_aux_dyn_outcome(df, p_thresh=0.05, clath_thresh=1500, dyn_thresh=2000,
  147. dyn_cons_thresh=5, clath_sig_frac=0.5, clath_consec_thresh_frac=0.15):
  148. &#34;&#34;&#34;add response of regression problem: mean auxilin strength among significant observations
  149. &#34;&#34;&#34;
  150. # look for clathrin significance
  151. num_sigs = [np.array(df[&#39;X_pvals&#39;].iloc[i]) &lt; p_thresh for i in range(df.shape[0])]
  152. x_consec_sig = []
  153. x_frac_sig = []
  154. lifetime_steps = np.array([len(df[&#39;X&#39;].iloc[i]) for i in range(df.shape[0])]) # get lifetimes
  155. for i in range(df.shape[0]):
  156. l = lifetime_steps[i]
  157. sigs = num_sigs[i]
  158. x_frac_sig.append(np.mean(sigs) &gt;= clath_sig_frac)
  159. cons = 0
  160. consec_flag = False
  161. for j in range(len(sigs)):
  162. if sigs[j] == 1:
  163. cons += 1
  164. else:
  165. cons = 0
  166. if cons &gt;= max(l * clath_consec_thresh_frac, 5):
  167. consec_flag = True
  168. break
  169. if consec_flag:
  170. x_consec_sig.append(1)
  171. else:
  172. x_consec_sig.append(0)
  173. # outcomes based on significant p-values
  174. df[&#39;clath_conservative_thresh&#39;] = (df[&#39;X_max&#39;].values &gt; clath_thresh).astype(np.int)
  175. df[&#39;clath_sig&#39;] = np.logical_and(x_consec_sig, x_frac_sig)
  176. df[&#39;successful&#39;] = np.logical_and(df[&#39;y_consec_thresh&#39;], df[&#39;clath_conservative_thresh&#39;])
  177. df[&#39;successful_dynamin&#39;] = df[&#39;successful&#39;]
  178. df[&#39;successful_full&#39;] = np.logical_and(df[&#39;clath_sig&#39;], df[&#39;successful_dynamin&#39;])
  179. # look for dynamin peak
  180. if &#39;Z&#39; in df.keys():
  181. num_sigs = [np.array(df[&#39;Z_pvals&#39;].iloc[i]) &lt; p_thresh for i in range(df.shape[0])]
  182. z_consec_sig = []
  183. for i in range(df.shape[0]):
  184. sigs = num_sigs[i]
  185. cons = 0
  186. consec_flag = False
  187. for j in range(len(sigs)):
  188. if sigs[j] == 1:
  189. cons += 1
  190. else:
  191. cons = 0
  192. if cons &gt;= dyn_cons_thresh:
  193. consec_flag = True
  194. break
  195. if consec_flag:
  196. z_consec_sig.append(1)
  197. else:
  198. z_consec_sig.append(0)
  199. df[&#39;z_consec_sig&#39;] = z_consec_sig
  200. df[&#39;Z_max&#39;] = [np.max(df.iloc[i][&#39;Z&#39;]) for i in range(df.shape[0])]
  201. df[&#39;z_thresh&#39;] = df[&#39;Z_max&#39;] &gt; dyn_thresh
  202. df[&#39;z_consec_thresh&#39;] = np.logical_and(df[&#39;z_consec_sig&#39;], df[&#39;z_thresh&#39;])
  203. df[&#39;Y_peak_idx&#39;] = np.nan_to_num(np.array([np.argmax(y) for y in df.Y]))
  204. df[&#39;Z_peak_idx&#39;] = np.nan_to_num(np.array([np.argmax(z) for z in df.Z]))
  205. df[&#39;z_peaked_first&#39;] = df[&#39;Z_peak_idx&#39;] &lt; df[&#39;Y_peak_idx&#39;]
  206. df[&#39;z_peak&#39;] = np.logical_and(df[&#39;z_consec_thresh&#39;], df[&#39;z_peaked_first&#39;])
  207. # peaks must happen at end of track
  208. df[&#39;z_peak&#39;] = np.logical_and(df[&#39;z_peak&#39;], df[&#39;Z_peak_idx&#39;] &gt; lifetime_steps / 2)
  209. df[&#39;successful_dynamin&#39;] = np.logical_or(
  210. df[&#39;successful&#39;],
  211. np.logical_and(df[&#39;clath_conservative_thresh&#39;], df[&#39;z_peak&#39;])
  212. )
  213. df[&#39;successful_full&#39;] = np.logical_and(df[&#39;clath_sig&#39;], df[&#39;successful_dynamin&#39;])
  214. return df</code></pre>
  215. </details>
  216. </section>
  217. <section>
  218. </section>
  219. <section>
  220. </section>
  221. <section>
  222. <h2 class="section-title" id="header-functions">Functions</h2>
  223. <dl>
  224. <dt id="src.outcomes.add_aux_dyn_outcome"><code class="name flex">
  225. <span>def <span class="ident">add_aux_dyn_outcome</span></span>(<span>df, p_thresh=0.05, clath_thresh=1500, dyn_thresh=2000, dyn_cons_thresh=5, clath_sig_frac=0.5, clath_consec_thresh_frac=0.15)</span>
  226. </code></dt>
  227. <dd>
  228. <section class="desc"><p>add response of regression problem: mean auxilin strength among significant observations</p></section>
  229. <details class="source">
  230. <summary>
  231. <span>Expand source code</span>
  232. </summary>
  233. <pre><code class="python">def add_aux_dyn_outcome(df, p_thresh=0.05, clath_thresh=1500, dyn_thresh=2000,
  234. dyn_cons_thresh=5, clath_sig_frac=0.5, clath_consec_thresh_frac=0.15):
  235. &#34;&#34;&#34;add response of regression problem: mean auxilin strength among significant observations
  236. &#34;&#34;&#34;
  237. # look for clathrin significance
  238. num_sigs = [np.array(df[&#39;X_pvals&#39;].iloc[i]) &lt; p_thresh for i in range(df.shape[0])]
  239. x_consec_sig = []
  240. x_frac_sig = []
  241. lifetime_steps = np.array([len(df[&#39;X&#39;].iloc[i]) for i in range(df.shape[0])]) # get lifetimes
  242. for i in range(df.shape[0]):
  243. l = lifetime_steps[i]
  244. sigs = num_sigs[i]
  245. x_frac_sig.append(np.mean(sigs) &gt;= clath_sig_frac)
  246. cons = 0
  247. consec_flag = False
  248. for j in range(len(sigs)):
  249. if sigs[j] == 1:
  250. cons += 1
  251. else:
  252. cons = 0
  253. if cons &gt;= max(l * clath_consec_thresh_frac, 5):
  254. consec_flag = True
  255. break
  256. if consec_flag:
  257. x_consec_sig.append(1)
  258. else:
  259. x_consec_sig.append(0)
  260. # outcomes based on significant p-values
  261. df[&#39;clath_conservative_thresh&#39;] = (df[&#39;X_max&#39;].values &gt; clath_thresh).astype(np.int)
  262. df[&#39;clath_sig&#39;] = np.logical_and(x_consec_sig, x_frac_sig)
  263. df[&#39;successful&#39;] = np.logical_and(df[&#39;y_consec_thresh&#39;], df[&#39;clath_conservative_thresh&#39;])
  264. df[&#39;successful_dynamin&#39;] = df[&#39;successful&#39;]
  265. df[&#39;successful_full&#39;] = np.logical_and(df[&#39;clath_sig&#39;], df[&#39;successful_dynamin&#39;])
  266. # look for dynamin peak
  267. if &#39;Z&#39; in df.keys():
  268. num_sigs = [np.array(df[&#39;Z_pvals&#39;].iloc[i]) &lt; p_thresh for i in range(df.shape[0])]
  269. z_consec_sig = []
  270. for i in range(df.shape[0]):
  271. sigs = num_sigs[i]
  272. cons = 0
  273. consec_flag = False
  274. for j in range(len(sigs)):
  275. if sigs[j] == 1:
  276. cons += 1
  277. else:
  278. cons = 0
  279. if cons &gt;= dyn_cons_thresh:
  280. consec_flag = True
  281. break
  282. if consec_flag:
  283. z_consec_sig.append(1)
  284. else:
  285. z_consec_sig.append(0)
  286. df[&#39;z_consec_sig&#39;] = z_consec_sig
  287. df[&#39;Z_max&#39;] = [np.max(df.iloc[i][&#39;Z&#39;]) for i in range(df.shape[0])]
  288. df[&#39;z_thresh&#39;] = df[&#39;Z_max&#39;] &gt; dyn_thresh
  289. df[&#39;z_consec_thresh&#39;] = np.logical_and(df[&#39;z_consec_sig&#39;], df[&#39;z_thresh&#39;])
  290. df[&#39;Y_peak_idx&#39;] = np.nan_to_num(np.array([np.argmax(y) for y in df.Y]))
  291. df[&#39;Z_peak_idx&#39;] = np.nan_to_num(np.array([np.argmax(z) for z in df.Z]))
  292. df[&#39;z_peaked_first&#39;] = df[&#39;Z_peak_idx&#39;] &lt; df[&#39;Y_peak_idx&#39;]
  293. df[&#39;z_peak&#39;] = np.logical_and(df[&#39;z_consec_thresh&#39;], df[&#39;z_peaked_first&#39;])
  294. # peaks must happen at end of track
  295. df[&#39;z_peak&#39;] = np.logical_and(df[&#39;z_peak&#39;], df[&#39;Z_peak_idx&#39;] &gt; lifetime_steps / 2)
  296. df[&#39;successful_dynamin&#39;] = np.logical_or(
  297. df[&#39;successful&#39;],
  298. np.logical_and(df[&#39;clath_conservative_thresh&#39;], df[&#39;z_peak&#39;])
  299. )
  300. df[&#39;successful_full&#39;] = np.logical_and(df[&#39;clath_sig&#39;], df[&#39;successful_dynamin&#39;])
  301. return df</code></pre>
  302. </details>
  303. </dd>
  304. <dt id="src.outcomes.add_outcomes"><code class="name flex">
  305. <span>def <span class="ident">add_outcomes</span></span>(<span>df, LABELS=None, thresh=3.25, p_thresh=0.05, aux_peak=642.375, aux_thresh=973)</span>
  306. </code></dt>
  307. <dd>
  308. <section class="desc"><p>Add binary outcome of whether spike happened and info on whether events were questionable</p></section>
  309. <details class="source">
  310. <summary>
  311. <span>Expand source code</span>
  312. </summary>
  313. <pre><code class="python">def add_outcomes(df, LABELS=None, thresh=3.25, p_thresh=0.05, aux_peak=642.375, aux_thresh=973):
  314. &#39;&#39;&#39;Add binary outcome of whether spike happened and info on whether events were questionable
  315. &#39;&#39;&#39;
  316. df[&#39;y_score&#39;] = df[&#39;Y_max&#39;].values - (df[&#39;Y_mean&#39;].values + thresh * df[&#39;Y_std&#39;].values)
  317. df[&#39;y_thresh&#39;] = (df[&#39;y_score&#39;].values &gt; 0).astype(np.int) # Y_max was big
  318. df[&#39;y&#39;] = df[&#39;Y_max&#39;] &gt; aux_peak
  319. # outcomes based on significant p-values
  320. num_sigs = [np.array(df[&#39;Y_pvals&#39;].iloc[i]) &lt; p_thresh for i in range(df.shape[0])]
  321. df[&#39;y_num_sig&#39;] = np.array([num_sigs[i].sum() for i in range(df.shape[0])]).astype(np.int)
  322. df[&#39;y_single_sig&#39;] = np.array([num_sigs[i].sum() &gt; 0 for i in range(df.shape[0])]).astype(np.int)
  323. df[&#39;y_double_sig&#39;] = np.array([num_sigs[i].sum() &gt; 1 for i in range(df.shape[0])]).astype(np.int)
  324. df[&#39;y_conservative_thresh&#39;] = (df[&#39;Y_max&#39;].values &gt; aux_thresh).astype(np.int)
  325. y_consec_sig = []
  326. y_sig_min_diff = []
  327. for i in range(df.shape[0]):
  328. idxs_sig = np.where(num_sigs[i] == 1)[0] # indices of significance
  329. if len(idxs_sig) &gt; 1:
  330. y_sig_min_diff.append(np.min(np.diff(idxs_sig)))
  331. else:
  332. y_sig_min_diff.append(np.nan)
  333. # find whether there were consecutive sig. indices
  334. if len(idxs_sig) &gt; 1 and np.min(np.diff(idxs_sig)) == 1:
  335. y_consec_sig.append(1)
  336. else:
  337. y_consec_sig.append(0)
  338. df[&#39;y_consec_sig&#39;] = y_consec_sig
  339. df[&#39;y_sig_min_diff&#39;] = y_sig_min_diff
  340. df[&#39;y_consec_thresh&#39;] = np.logical_or(df[&#39;y_consec_sig&#39;], df[&#39;y_conservative_thresh&#39;])
  341. def add_hotspots(df, num_sigs, outcome_def=&#39;consec_sig&#39;):
  342. &#39;&#39;&#39;Identify hotspots as any track which over its time course has multiple events
  343. events must meet the event definition, then for a time not meet it, then meet it again
  344. Example: two consecutive significant p-values, then non-significant p-value, then 2 more consecutive p-values
  345. &#39;&#39;&#39;
  346. if outcome_def == &#39;consec_sig&#39;:
  347. hotspots = np.zeros(df.shape[0]).astype(np.int)
  348. for i in range(df.shape[0]):
  349. idxs_sig = np.where(num_sigs[i] == 1)[0] # indices of significance
  350. if idxs_sig.size &lt; 5:
  351. hotspots[i] = 0
  352. else:
  353. diffs = np.diff(idxs_sig)
  354. consecs = np.where(diffs == 1)[0] # diffs==1 means there were consecutive sigs
  355. consec_diffs = np.diff(consecs)
  356. if consec_diffs.shape[0] &gt; 0 and np.max(
  357. consec_diffs) &gt; 2: # there were greated than 2 non-consec sigs between the consec sigs
  358. hotspots[i] = 1
  359. else:
  360. hotspots[i] = 0
  361. df[&#39;sig_idxs&#39;] = num_sigs
  362. df[&#39;hotspots&#39;] = hotspots == 1
  363. return df
  364. df = add_hotspots(df, num_sigs)
  365. if LABELS is not None:
  366. df[&#39;y_consec_thresh&#39;][df.pid.isin(LABELS[&#39;pos&#39;])] = 1 # add manual pos labels
  367. df[&#39;y_consec_thresh&#39;][df.pid.isin(LABELS[&#39;neg&#39;])] = 0 # add manual neg labels
  368. df[&#39;hotspots&#39;][df.pid.isin(LABELS[&#39;hotspots&#39;])] = True # add manual hotspot labels
  369. df = add_rule_based_label(df)
  370. return df</code></pre>
  371. </details>
  372. </dd>
  373. <dt id="src.outcomes.add_rule_based_label"><code class="name flex">
  374. <span>def <span class="ident">add_rule_based_label</span></span>(<span>df)</span>
  375. </code></dt>
  376. <dd>
  377. <section class="desc"></section>
  378. <details class="source">
  379. <summary>
  380. <span>Expand source code</span>
  381. </summary>
  382. <pre><code class="python">def add_rule_based_label(df):
  383. df[&#39;Y_peak_time_frac&#39;] = df[&#39;Y_peak_idx&#39;].values / df[&#39;lifetime&#39;].values
  384. df[&#39;y_z_score&#39;] = (df[&#39;Y_max&#39;].values - df[&#39;Y_mean&#39;].values) / df[&#39;Y_std&#39;].values
  385. X_max_around_Y_peak = []
  386. X_max_after_Y_peak = []
  387. for i in range(len(df)):
  388. pt = df[&#39;Y_peak_idx&#39;].values[i]
  389. lt = df[&#39;lifetime&#39;].values[i]
  390. left_bf = np.int(0.2 * lt) + 1 # look at a window with length = 30%*lifetime
  391. right_bf = np.int(0.1 * lt) + 1
  392. arr_around = df[&#39;X&#39;].iloc[i][max(0, pt - left_bf): min(pt + right_bf, lt)]
  393. arr_after = df[&#39;X&#39;].iloc[i][min(pt + right_bf, lt - 1):]
  394. X_max_around_Y_peak.append(max(arr_around))
  395. if len(arr_after) &gt; 0:
  396. X_max_after_Y_peak.append(max(arr_after))
  397. else:
  398. X_max_after_Y_peak.append(max(arr_around))
  399. df[&#39;X_max_around_Y_peak&#39;] = X_max_around_Y_peak
  400. df[&#39;X_max_after_Y_peak&#39;] = X_max_after_Y_peak
  401. df[&#39;X_max_diff&#39;] = df[&#39;X_max_around_Y_peak&#39;] - df[&#39;X_max_after_Y_peak&#39;]
  402. def rule_based_model(track):
  403. # three rules:
  404. # if aux peaks too early -- negative
  405. # elif:
  406. # if y_consec_sig or y_conservative_thresh or (cla drops around aux peak, and aux max is greater than
  407. # mean + 2.6*std), then positive
  408. # else: negative
  409. if track[&#39;Y_peak_time_frac&#39;] &lt; 0.2:
  410. return 0
  411. if track[&#39;y_consec_sig&#39;] or track[&#39;y_conservative_thresh&#39;]:
  412. return 1
  413. # if track[&#39;X_max_diff&#39;] &gt; 260 and track[&#39;y_z_score&#39;] &gt; 2.6:
  414. # return 1
  415. if track[&#39;X_max_diff&#39;] &gt; 260 and track[&#39;Y_max&#39;] &gt; 560:
  416. return 1
  417. return 0
  418. df[&#39;y_rule_based&#39;] = np.array([rule_based_model(df.iloc[i]) for i in range(len(df))])
  419. return df</code></pre>
  420. </details>
  421. </dd>
  422. <dt id="src.outcomes.add_sig_mean"><code class="name flex">
  423. <span>def <span class="ident">add_sig_mean</span></span>(<span>df, resp_tracks=['Y'])</span>
  424. </code></dt>
  425. <dd>
  426. <section class="desc"><p>add response of regression problem: mean auxilin strength among significant observations</p></section>
  427. <details class="source">
  428. <summary>
  429. <span>Expand source code</span>
  430. </summary>
  431. <pre><code class="python">def add_sig_mean(df, resp_tracks=[&#39;Y&#39;]):
  432. &#34;&#34;&#34;add response of regression problem: mean auxilin strength among significant observations
  433. &#34;&#34;&#34;
  434. for track in resp_tracks:
  435. sig_mean = []
  436. for i in range(len(df)):
  437. r = df.iloc[i]
  438. sigs = np.array(r[f&#39;{track}_pvals&#39;]) &lt; 0.05
  439. if sum(sigs)&gt;0:
  440. sig_mean.append(np.mean(np.array(r[track])[sigs]))
  441. else:
  442. sig_mean.append(0)
  443. df[f&#39;{track}_sig_mean&#39;] = sig_mean
  444. df[f&#39;{track}_sig_mean_normalized&#39;] = sig_mean
  445. for cell in set(df[&#39;cell_num&#39;]):
  446. cell_idx = np.where(df[&#39;cell_num&#39;].values == cell)[0]
  447. y = df[f&#39;{track}_sig_mean&#39;].values[cell_idx]
  448. df[f&#39;{track}_sig_mean_normalized&#39;].values[cell_idx] = (y - np.mean(y))/np.std(y)
  449. return df</code></pre>
  450. </details>
  451. </dd>
  452. </dl>
  453. </section>
  454. <section>
  455. </section>
  456. </article>
  457. <nav id="sidebar">
  458. <h1>Index</h1>
  459. <div class="toc">
  460. <ul></ul>
  461. </div>
  462. <ul id="index">
  463. <li><h3>Super-module</h3>
  464. <ul>
  465. <li><code><a title="src" href="index.html">src</a></code></li>
  466. </ul>
  467. </li>
  468. <li><h3><a href="#header-functions">Functions</a></h3>
  469. <ul class="">
  470. <li><code><a title="src.outcomes.add_aux_dyn_outcome" href="#src.outcomes.add_aux_dyn_outcome">add_aux_dyn_outcome</a></code></li>
  471. <li><code><a title="src.outcomes.add_outcomes" href="#src.outcomes.add_outcomes">add_outcomes</a></code></li>
  472. <li><code><a title="src.outcomes.add_rule_based_label" href="#src.outcomes.add_rule_based_label">add_rule_based_label</a></code></li>
  473. <li><code><a title="src.outcomes.add_sig_mean" href="#src.outcomes.add_sig_mean">add_sig_mean</a></code></li>
  474. </ul>
  475. </li>
  476. </ul>
  477. </nav>
  478. </main>
  479. <footer id="footer">
  480. <p>Generated by <a href="https://pdoc3.github.io/pdoc"><cite>pdoc</cite> 0.7.2</a>.</p>
  481. </footer>
  482. <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
  483. <script>hljs.initHighlightingOnLoad()</script>
  484. </body>
  485. </html>
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...