Skip to content

Commit 400b228

Browse files
add tests for BPAP analysis (#21)
* add tests for BPAP analysis
1 parent 2642bee commit 400b228

File tree

5 files changed

+504
-12
lines changed

5 files changed

+504
-12
lines changed

bluecellulab/analysis/analysis.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def __init__(self, cell: Cell) -> None:
248248
self.cell = cell
249249
self.dt = 0.025
250250
self.stim_start = 1000
251-
self.stim_duration = 3
251+
self.stim_duration = 5
252252
self.basal_cmap = sns.color_palette("crest", as_cmap=True)
253253
self.apical_cmap = sns.color_palette("YlOrBr_r", as_cmap=True)
254254

@@ -301,7 +301,11 @@ def amplitudes(self, recs) -> list[float]:
301301
for rec in recs.values()
302302
]
303303
features_results = efel.get_feature_values(traces, [efel_feature_name])
304-
amps = [feat_res[efel_feature_name][0] for feat_res in features_results]
304+
amps = [
305+
feat_res[efel_feature_name][0]
306+
for feat_res in features_results
307+
if feat_res[efel_feature_name] is not None
308+
]
305309

306310
return amps
307311

@@ -320,7 +324,7 @@ def distances_to_soma(self, recs) -> list[float]:
320324

321325
def get_amplitudes_and_distances(self):
322326
soma_rec, dend_rec, apic_rec = self.get_recordings()
323-
soma_amp = self.amplitudes({"soma": soma_rec})[0]
327+
soma_amp = self.amplitudes({"soma": soma_rec})
324328
dend_amps = None
325329
dend_dist = None
326330
apic_amps = None
@@ -334,20 +338,21 @@ def get_amplitudes_and_distances(self):
334338

335339
return soma_amp, dend_amps, dend_dist, apic_amps, apic_dist
336340

337-
def fit(self, soma_amp, dend_amps, dend_dist, apic_amps, apic_dist):
341+
@staticmethod
342+
def fit(soma_amp, dend_amps, dend_dist, apic_amps, apic_dist):
338343
"""Fit the amplitudes vs distances to an exponential decay function."""
339344
from scipy.optimize import curve_fit
340345

341346
popt_dend = None
342347
if dend_amps and dend_dist:
343348
dist = [0] + dend_dist # add soma distance
344-
amps = [soma_amp] + dend_amps # add soma amplitude
349+
amps = soma_amp + dend_amps # add soma amplitude
345350
popt_dend, _ = curve_fit(exp_decay, dist, amps)
346351

347352
popt_apic = None
348353
if apic_amps and apic_dist:
349354
dist = [0] + apic_dist # add soma distance
350-
amps = [soma_amp] + apic_amps # add soma amplitude
355+
amps = soma_amp + apic_amps # add soma amplitude
351356
popt_apic, _ = curve_fit(exp_decay, dist, amps)
352357

353358
return popt_dend, popt_apic
@@ -357,10 +362,18 @@ def validate(self, soma_amp, dend_amps, dend_dist, apic_amps, apic_dist):
357362
validated = True
358363
notes = ""
359364
popt_dend, popt_apic = self.fit(soma_amp, dend_amps, dend_dist, apic_amps, apic_dist)
365+
logging.warning(popt_dend)
366+
if dend_amps is not None:
367+
plt.cla()
368+
plt.plot([0], soma_amp, '.')
369+
plt.plot(dend_dist, dend_amps, '.')
370+
x = np.linspace(0, max(dend_dist), 100)
371+
plt.plot(x, exp_decay(x, *popt_dend), color='darkgreen', linestyle='--', label='Basal Dendritic Fit')
372+
plt.savefig("bad_dendritic_fit.png")
360373
if popt_dend is None:
361374
logger.debug("No dendritic recordings found.")
362375
notes += "No dendritic recordings found.\n"
363-
elif popt_dend[1] <= 0:
376+
elif popt_dend[1] <= 0 or popt_dend[0] <= 0:
364377
logger.debug("Dendritic fit is not decaying.")
365378
validated = False
366379
notes += "Dendritic fit is not decaying.\n"
@@ -369,7 +382,7 @@ def validate(self, soma_amp, dend_amps, dend_dist, apic_amps, apic_dist):
369382
if popt_apic is None:
370383
logger.debug("No apical recordings found.")
371384
notes += "No apical recordings found.\n"
372-
elif popt_apic[1] <= 0:
385+
elif popt_apic[1] <= 0 or popt_apic[0] <= 0:
373386
logger.debug("Apical fit is not decaying.")
374387
validated = False
375388
notes += "Apical fit is not decaying.\n"
@@ -395,7 +408,7 @@ def plot_amp_vs_dist(
395408

396409
outpath = pathlib.Path(output_dir) / output_fname
397410
fig, ax1 = plt.subplots(figsize=(10, 6))
398-
ax1.scatter([0], [soma_amp], marker="^", color='black', label='Soma')
411+
ax1.scatter([0], soma_amp, marker="^", color='black', label='Soma')
399412
if dend_amps and dend_dist:
400413
ax1.scatter(
401414
dend_dist,
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
{
2+
"soma_amp": [
3+
111.34521528672875
4+
],
5+
"dend_amps": [
6+
98.23094942121509,
7+
68.62311468267298,
8+
62.62445979426846,
9+
62.24338283567193,
10+
66.32030696047326,
11+
63.522811861695004,
12+
63.45726979131787,
13+
70.89295894734948,
14+
56.40111148498258,
15+
57.24623368533049,
16+
40.64141777594544,
17+
53.95040221390064,
18+
52.96498423826803,
19+
51.77910968569723,
20+
25.48964917778366,
21+
9.59068977316555,
22+
9.936317272682018,
23+
46.15179156409582,
24+
23.047487845117388,
25+
10.919267449128355,
26+
12.758847706343673,
27+
23.656029558256762,
28+
8.36783639154369,
29+
8.915576947165093
30+
],
31+
"dend_dist": [
32+
4.132154495613162,
33+
22.94928443789968,
34+
50.87825971384081,
35+
50.87825976187439,
36+
28.591305994698544,
37+
55.540305007384525,
38+
55.540303052041935,
39+
18.72565188608846,
40+
51.271298393796826,
41+
35.667618263231226,
42+
83.38964576111702,
43+
45.176057024384555,
44+
50.48827399214656,
45+
51.94445272486609,
46+
115.69091757824029,
47+
189.43046150177435,
48+
184.22346702641576,
49+
72.32763697482358,
50+
129.09827686720868,
51+
188.56691047813882,
52+
177.6367370238564,
53+
129.70443239186386,
54+
207.31989211502642,
55+
200.1518994137304
56+
],
57+
"apic_amps": [
58+
110.80678737727112,
59+
101.21605439589935,
60+
86.45127709396556,
61+
88.08080280533231,
62+
32.7487731134306,
63+
30.986378303959974,
64+
3.0848502343884405,
65+
3.900853119844811,
66+
2.254931704930655,
67+
2.272720870639091,
68+
90.52441288641674,
69+
10.036675413651054,
70+
1.5206839474543443,
71+
1.01022333338382,
72+
0.8088054914310447,
73+
0.8016925870685299,
74+
0.7580296129067392,
75+
1.3303465457294124,
76+
8.806995839146538,
77+
0.9537378681873889,
78+
0.4053703970757567,
79+
0.5674652641521476,
80+
0.3398361661102314,
81+
0.3348109129922108,
82+
0.8841741534492655,
83+
0.5218629233019385,
84+
0.31311758758579344,
85+
0.3085072452276165,
86+
0.48578883821971885,
87+
0.2736550609218682,
88+
0.1999756427979804,
89+
0.09507803246007995,
90+
0.09382282144618159,
91+
102.68144816715075,
92+
59.08911390270051,
93+
8.162679258219498,
94+
0.6806335333181153,
95+
1.1937861091823123,
96+
6.231441926240805,
97+
0.7930112074815128,
98+
0.6449669328471828,
99+
55.742076904640925,
100+
7.5470182725754995,
101+
0.8099454420506902,
102+
0.4000616811359379,
103+
0.4154747059981929,
104+
0.776037805549052,
105+
0.3605311915651441,
106+
0.35693542370228215,
107+
14.155905821221253,
108+
1.1821315251824274,
109+
2.3449694843495337,
110+
0.45782044274096734,
111+
0.6538964325382892,
112+
0.43222484450811294,
113+
0.4265208374952465,
114+
97.10645056031572,
115+
54.27656594573131,
116+
75.49916274085979,
117+
68.06435369617161,
118+
36.78649185764703,
119+
15.828082927586678,
120+
11.44315916560727,
121+
10.662191215976264,
122+
10.347023270475447,
123+
10.322089940494863,
124+
12.37575657525089,
125+
2.6748736119722736,
126+
3.914318698322276,
127+
2.232545245145687,
128+
1.873447305432748,
129+
0.7171774885574251,
130+
1.108737132922485,
131+
39.04549747264967,
132+
34.371652960319295,
133+
22.869037635674644,
134+
19.887804173247375
135+
],
136+
"apic_dist": [
137+
4.765188446744128,
138+
14.31071445662771,
139+
21.36621485709223,
140+
35.669389444568615,
141+
96.48755269431791,
142+
118.80448505001037,
143+
250.35918452963003,
144+
225.02715903706326,
145+
274.9754184084895,
146+
273.8306957945262,
147+
49.42517496857583,
148+
128.1849816736902,
149+
187.7829910390483,
150+
204.337994632125,
151+
224.20399765973622,
152+
224.2040012334495,
153+
224.20397922118696,
154+
204.338001241116,
155+
131.49597496512618,
156+
194.40497903674185,
157+
237.44798440332858,
158+
210.9599860997319,
159+
240.75899152627198,
160+
240.7589868431039,
161+
194.40497832503735,
162+
210.95997928408332,
163+
240.75898751465706,
164+
240.7589993689498,
165+
210.95997465947454,
166+
240.7589761131518,
167+
247.3809739706042,
168+
287.1129671133686,
169+
287.11297269192727,
170+
36.996716900747586,
171+
68.14638288352256,
172+
131.05539235941643,
173+
233.69639775538496,
174+
210.51938200182275,
175+
140.9883765736489,
176+
230.38535799054807,
177+
237.0073678575123,
178+
68.1463847439706,
179+
131.05539290678556,
180+
217.14138949526074,
181+
263.4953791597165,
182+
260.1843840825309,
183+
217.14138943285212,
184+
263.49538192269904,
185+
263.49537944873003,
186+
104.56738755398104,
187+
200.58639647917846,
188+
160.85439903655225,
189+
230.38540159510495,
190+
203.89740912109608,
191+
230.3854198507865,
192+
230.38540311446172,
193+
18.379717313542777,
194+
74.127322831481,
195+
32.48306590224313,
196+
54.04714058470337,
197+
93.87530030243069,
198+
138.65220567371856,
199+
163.35909185885941,
200+
167.55586001212822,
201+
176.83381652748454,
202+
177.28905382870164,
203+
155.3762498517425,
204+
237.71057185209986,
205+
205.05573687710864,
206+
238.49243715184906,
207+
242.5732752622551,
208+
297.779499133758,
209+
274.9278696890993,
210+
100.91244622167407,
211+
64.24869681239623,
212+
120.55932216820668,
213+
140.4253262674191
214+
]
215+
}

0 commit comments

Comments
 (0)