-
Notifications
You must be signed in to change notification settings - Fork 1
/
statistics_swc.py
executable file
·396 lines (253 loc) · 6.53 KB
/
statistics_swc.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
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
import re
from math import sqrt
from random import randint
import sys
from collections import OrderedDict
from numpy import linalg as LA, array, dot
from math import acos
import numpy as np
from random import uniform, randrange
from math import cos, sin, pi, sqrt, radians, degrees
import collections
def total_length(dlist, dist): #soma_included
t_length=0
for dend in dlist:
t_length+=dist[dend]
return t_length
def total_area(dlist, area): #soma_included
t_area=0
for dend in dlist:
t_area+=area[dend]
return t_area
def path_length(dlist, path, dist):
plength=dict()
for dend in dlist:
d=0
for i in path[dend]:
d+=dist[i]
plength[dend]=d
return plength
def median_diameter(dlist, dend_add3d):
med_diam=dict()
for dend in dlist:
m=len(dend_add3d[dend])/2
med_diam[dend]=float(dend_add3d[dend][m][5])*2
return med_diam
def print_branch_order(dlist, bo):
bo_dict=dict()
for i in dlist:
bo_dict[i]=bo[i]
return sorted(bo_dict.items(), key=lambda x: x[0])
def bo_frequency(dlist, bo):
orders=[]
for dend in dlist:
orders.append(bo[dend])
bo_min=1 # min(orders)
bo_max=max(orders)
bo_freq={}
for i in range(bo_min, bo_max+1):
k=0
for order in orders:
if order==i:
k+=1
bo_freq[i]=k
return bo_freq, bo_max
def bo_dlength(dlist, bo, bo_max, dist):
bo_dlen={}
for i in range(1, bo_max+1):
k=0
add_length=0
for dend in dlist:
if i==bo[dend]:
k+=1
add_length+=dist[dend]
if k!=0:
bo_dlen[i]=add_length/k
return bo_dlen
def bo_plength(dlist, bo, bo_max, plength):
bo_plen={}
for i in range(1, bo_max+1):
k=0
add_length=0
for dend in dlist:
if i==bo[dend]:
k+=1
add_length+=plength[dend]
if k!=0:
bo_plen[i]=add_length/k
return bo_plen
def distance(x1,x2,y1,y2,z1,z2): #returns the euclidean distance between two 3d points
dist = sqrt((x2-x1)**2 + (y2-y1)**2 + (z2-z1)**2)
return dist
def sholl_intersections(points, parental_points, soma_index, radius, parameter):
sholl_list=dict()
for i in soma_index:
if i[6]==-1:
xr=i[2]
yr=i[3]
zr=i[4]
values=[]
for val in np.arange(0, 10000, radius):
values.append(val)
for u in range(len(values)-1):
previous_dist=values[u]
next_dist=values[u+1]
n_intersections=0
for i in points:
if points[i][1] in parameter:
x1=points[i][2]
y1=points[i][3]
z1=points[i][4]
mydist1=distance(xr,x1,yr,y1,zr,z1)
p=parental_points[i]
x2=points[p][2]
y2=points[p][3]
z2=points[p][4]
mydist2=distance(xr,x2,yr,y2,zr,z2)
if mydist1>next_dist and mydist2<next_dist:
n_intersections+=1
sholl_list[next_dist]=n_intersections
return sholl_list
def sholl_bp(bpoints, points, soma_index, radius):
sholl_list=dict()
for i in soma_index:
if i[6]==-1:
xr=i[2]
yr=i[3]
zr=i[4]
values=[]
for val in np.arange(0, 10000, radius):
values.append(val)
for val in range(len(values)-1):
oc=0
previous_dist=values[val]
next_dist=values[val+1]
for i in bpoints:
x=points[i][2]
y=points[i][3]
z=points[i][4]
mydist=distance(xr,x,yr,y,zr,z)
if mydist>previous_dist and mydist<next_dist:
oc+=1
sholl_list[next_dist]=oc
#sholl_list=remove_trailing_zeros(sholl_list, values, radius)
return sholl_list
def remove_trailing_zeros(sholl_list, values, radius):
k=len(sholl_list)
x=0
for i in values[:-1]:
if sholl_list[i+radius]==0:
x+=1
else:
x=0
new_sholl_dict=dict()
for i in range(k-x):
new_sholl_dict[values[i]+radius]=sholl_list[values[i]+radius]
return new_sholl_dict
def sholl_length(points, parental_points, soma_index, radius, parameter):
sholl_list=dict()
for i in soma_index:
if i[6]==-1:
xr=i[2]
yr=i[3]
zr=i[4]
values=[]
for val in np.arange(0, 10000, radius):
values.append(val)
for u in range(len(values)-1):
previous_dist=values[u]
next_dist=values[u+1]
sum_length=0
for i in points:
if points[i][1] in parameter:
x=points[i][2]
y=points[i][3]
z=points[i][4]
mydist=distance(xr,x,yr,y,zr,z)
if mydist>previous_dist and mydist<next_dist:
p=parental_points[i]
xp=points[p][2]
yp=points[p][3]
zp=points[p][4]
sum_length+=distance(x,xp,y,yp,z,zp)
sholl_list[next_dist]=sum_length
#sholl_list=remove_trailing_zeros(sholl_list, values, radius)
return sholl_list
def dist_angle_analysis(dlist, dend_add3d, soma_root, principal_axis):
dist_angle=[]
for dend in dlist:
point_list=[]
for i in range(len(dend_add3d[dend])):
x=dend_add3d[dend][i][2]
y=dend_add3d[dend][i][3]
z=dend_add3d[dend][i][4]
point=[x, y, z]
point_list.append([principal_axis, soma_root, point])
for i in point_list:
a=array(i[0], float)
b=array(i[1], float)
c=array(i[2], float)
ba = a-b
bc = c-b
quot_a = ba/LA.norm(ba)
quot_b = bc/LA.norm(bc)
dotp = dot(quot_a.T,quot_b)
degree = 180 - acos(dotp)*57.295779513082
dist = sqrt((x-soma_root[0])**2 + (y-soma_root[1])**2 + (z-soma_root[2])**2)
dist_angle.append([dist, degree])
return dist_angle
def dist_angle_frequency(dist_angle, radius):
dist_freq={}
angle_f={}
previous_val=0
for val in np.arange(0, 1000, radius):
angles_freq={}
angles=[]
count_dist=0
for i in range(len(dist_angle)):
if dist_angle[i][0]>previous_val and dist_angle[i][0]<val:
count_dist+=1
angles.append(dist_angle[i][1])
previous_a=0
for a in np.arange(5, 185, 5):
count_angle=0
for i in range(len(angles)):
if angles[i]>previous_a and angles[i]<a:
count_angle+=1
angles_freq[a]=count_angle
previous_a=a
dist_freq[val]=count_dist
angle_f[val]=angles_freq
previous_val=val
return dist_freq, angle_f
def axis(apical, dend_add3d, soma_index): #weighted linear regression
def calc_mean(l,d,sum_d):
ld=[]
for i in range(len(l)):
ld.append(l[i]*(d[i]/sum_d))
l_mean=np.mean(ld)
ld_weighted=[]
for i in ld:
ld_weighted.append(i-l_mean)
return ld_weighted
x=y=z=d=[]
x_soma=soma_index[0][2]
y_soma=soma_index[0][3]
z_soma=soma_index[0][4]
for dend in apical:
for i in dend_add3d[dend]:
x.append(i[2]-x_soma)
y.append(i[3]-y_soma)
z.append(i[4]-z_soma)
d.append(i[5])
sum_d=np.sum(d)
x_weighted=calc_mean(x,d,sum_d)
y_weighted=calc_mean(y,d,sum_d)
z_weighted=calc_mean(z,d,sum_d)
xyz_matrix=[]
for i in range(len(x_weighted)):
xyz_matrix.append([x_weighted[i], y_weighted[i], z_weighted[i]])
(u,s,v)=np.linalg.svd(xyz_matrix)
principal_axis=[v[0,0]+x_soma, v[1,0]+y_soma, v[2,0]+z_soma]
soma_root=[x_soma, y_soma, z_soma]
return principal_axis, soma_root