-
Notifications
You must be signed in to change notification settings - Fork 17
/
Neuron_trace.py
executable file
·1154 lines (984 loc) · 41.1 KB
/
Neuron_trace.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
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from curses import intrflush
from typing import List, Optional, Tuple, Union
import numpy as np
import re
import pandas as pd
import networkx as nx
from cloudvolume import CloudVolume, Skeleton
from io import StringIO
import os
from brainlit.utils.util import (
check_type,
check_size,
)
from sklearn.metrics import pairwise_distances_argmin_min
import warnings
class NeuronTrace:
"""Neuron Trace class to handle neuron traces as swcs and s3 skeletons
Arguments
---------
path : str
Path to either s3 bucket (url) or swc file (filepath).
seg_id : int
If s3 bucket path is provided, the segment number to pull, default None.
mip : int
If s3 bucket path is provided, the resolution to use for scaling, default None.
rounding : bool
If s3 is provided, specifies if it should be rounded, default True
read_offset : bool
If swc is provided, whether offset should be read from file, default False.
fill_missing: bool
Always passes directly into 'CloudVolume()' function to fill missing skeleton values with 0s, default True.
use_https : bool
Always passes directly into 'CloudVolume()' function to set use_https to desired value, default True.
Attributes
----------
path : str
Path to either s3 bucket (url) or swc file (filepath)
input_type : bool
Specifies whether input file is 'swc' or 'skel'
df : :class:`pandas.DataFrame`
Indices, coordinates, and parents of each node
args : tuple
Stores arguments for df - offset, color, cc, branch
seg_id : int
If s3 bucket path is provided, the segment number to pull
mip : None,int
If s3 bucket path is provided, the resolution to use for scaling
Example
----------
>>> swc_path = "./data/data_octree/consensus-swcs/2018-08-01_G-002_consensus.swc"
>>> s3_path = "s3://open-neurodata/brainlit/brain1_segments"
>>> seg_id = 11
>>> mip = 2
>>> swc_trace = NeuronTrace(swc_path)
>>> s3_trace = NeuronTrace(s3_path,seg_id,mip)
"""
def __init__(
self,
path: str,
seg_id: int = None,
mip: int = None,
rounding: bool = True,
read_offset: bool = False,
fill_missing: bool = True,
use_https: bool = False,
):
self.path = path
self.input_type = None
self.df = None
self.args = []
self.seg_id = seg_id
self.mip = mip
self.rounding = rounding
self.fill_missing = fill_missing
self.use_https = use_https
check_type(path, str)
check_type(seg_id, (type(None), int))
check_type(mip, (type(None), int))
check_type(read_offset, bool)
check_type(rounding, bool)
if (seg_id == None and type(mip) == int) or (
type(seg_id) == int and mip == None
):
raise ValueError(
"For 'swc' do not input mip or seg_id, and for 'skel', provide both mip and seg_id"
)
# first check if it is a skel
if seg_id != None and mip != None:
cv = CloudVolume(
path, mip=mip, fill_missing=fill_missing, use_https=use_https
)
skeleton = cv.skeleton.get(seg_id)
if type(skeleton) is Skeleton:
self.input_type = "skel"
# else, check if it is a swc by checking if file exists/extension is .swc
elif os.path.isfile(self.path) and os.path.splitext(path)[-1].lower() == ".swc":
self.input_type = "swc"
# if it is not a swc or skeleton, raise error
if self.input_type != "swc" and self.input_type != "skel":
raise ValueError("Did not input 'swc' filepath or 'skel' url")
# next, convert to a dataframe
if self.input_type == "swc" and read_offset == False:
df, offset, color, cc, branch = self._read_swc(self.path)
args = [offset, color, cc, branch]
self.df = df
self.args = args
elif self.input_type == "swc" and read_offset == True:
df, color, cc, branch = self._read_swc_offset(path)
args = [None, color, cc, branch]
self.df = df
self.args = args
elif self.input_type == "skel":
df = self._read_s3(path, seg_id, mip, rounding)
(self.path, seg_id, mip)
self.df = df
# public methods
def get_df_arguments(self) -> list:
"""Gets arguments for df - offset, color, cc, branch
Returns
-------
self.args : list
list of arguments for df, if found - offset, color, cc, branch
Example
-------
>>> swc_trace.get_df_arguments()
>>> [[73954.8686, 17489.532566, 34340.365689], [1.0, 1.0, 1.0], nan, nan]
"""
return self.args
def get_df(self) -> pd.DataFrame:
"""Gets the dataframe providing indices, coordinates, and parents of each node
Returns
-------
self.df : :class:`pandas.DataFrame`
dataframe providing indices, coordinates, and parents of each node
Example
-------
>>> swc_trace.get_df()
>>> sample structure x y z r parent
0 1 0 -52.589700 -1.448032 -1.228827 1.0 -1
1 2 0 -52.290940 -1.448032 -1.228827 1.0 1
2 3 0 -51.992181 -1.143616 -0.240423 1.0 2
3 4 0 -51.095903 -1.143616 -0.240423 1.0 3
4 5 0 -50.797144 -0.839201 -0.240423 1.0 4
... ... ... ... ... ... ... ...
148 149 0 45.702088 14.381594 -7.159252 1.0 148
149 150 0 46.000847 14.686010 -7.159252 1.0 149
150 151 0 46.897125 14.686010 -7.159252 1.0 150
151 152 0 47.494643 15.294842 -7.159252 1.0 151
152 153 6 48.092162 15.294842 -7.159252 1.0 152
53 rows × 7 columns
"""
return self.df
def get_skel(
self, benchmarking: bool = False, origin: np.ndarray = None
) -> Skeleton:
"""Gets a skeleton version of dataframe, if swc input is provided
Arguments
----------
origin : None, numpy array with shape (3,1) (default = None)
origin of coordinate frame in microns, (default: None assumes (0,0,0) origin)
benchmarking : bool
For swc files, specifies whether swc file is from benchmarking dataset, to obtain skeleton ID
Returns
--------
skel : cloudvolume.Skeleton
Skeleton object of given SWC file
Example
-------
>>> swc_trace.get_skel(benchmarking=True)
>>> Skeleton(segid=, vertices=(shape=153, float32), edges=(shape=152, uint32), radius=(153, float32), vertex_types=(153, uint8), vertex_color=(153, float32), space='physical' transform=[[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0]])
"""
check_type(origin, (type(None), np.ndarray))
check_type(benchmarking, bool)
if type(origin) == np.ndarray:
check_size(origin)
if self.input_type == "swc":
skel = self._swc2skeleton(self.path, benchmarking, origin)
return skel
elif self.input_type == "skel":
cv = CloudVolume(
self.path,
mip=self.mip,
fill_missing=self.fill_missing,
use_https=self.use_https,
)
skel = cv.skeleton.get(self.seg_id)
return skel
def get_df_voxel(
self, spacing: np.array, origin: np.array = np.array([0, 0, 0])
) -> pd.DataFrame:
"""Converts coordinates in pd.DataFrame from spatial units to voxel units
Arguments
----------
spacing : :class:`numpy.array`
Conversion factor (spatial units/voxel). Assumed to be np.array([x,y,z])
origin : :class:`numpy.array`
Origin of the spatial coordinate. Default is (0,0,0). Assumed to be
np.array([x,y,z])
Returns
-------
df_voxel : :class:`pandas.DataFrame`
Indicies, coordinates, and parents of each node in the swc. Coordinates
are in voxel units.
Example
-------
>>> swc_trace.get_df_voxel(spacing=np.asarray([2,2,2]))
>>> sample structure x y z r parent
0 1 0 -26 -1 -1 1.0 -1
1 2 0 -26 -1 -1 1.0 1
2 3 0 -26 -1 0 1.0 2
3 4 0 -26 -1 0 1.0 3
4 5 0 -25 0 0 1.0 4
... ... ... ... ... ... ... ...
148 149 0 23 7 -4 1.0 148
149 150 0 23 7 -4 1.0 149
150 151 0 23 7 -4 1.0 150
151 152 0 24 8 -4 1.0 151
152 153 6 24 8 -4 1.0 152
153 rows × 7 columns
"""
check_type(spacing, np.ndarray)
check_size(spacing)
check_type(origin, np.ndarray)
check_size(origin)
df_voxel = self._df_in_voxel(self.df, spacing, origin)
return df_voxel
def get_graph(
self, spacing: np.array = None, origin: np.array = None
) -> nx.classes.digraph.DiGraph:
"""Converts dataframe in either spatial or voxel coordinates into a directed graph.
Will convert to voxel coordinates if spacing is specified.
Arguments
----------
spacing : None, :class:`numpy.array` (default = None)
Conversion factor (spatial units/voxel). Assumed to be np.array([x,y,z]).
Provided if graph should convert to voxel coordinates first. Default is None.
origin : None, :class:`numpy.array` (default = None)
Origin of the spatial coordinate, if converting to voxels. Default is None.
Assumed to be np.array([x,y,z])
Returns
-------
G : :class:`networkx.classes.digraph.DiGraph`
Neuron from swc represented as directed graph. Coordinates x,y,z are
node attributes accessed by keys 'x','y','z' respectively.
Example
-------
>>> swc_trace.get_graph()
>>> <networkx.classes.digraph.DiGraph at 0x7f81a83937f0>
"""
check_type(spacing, (type(None), np.ndarray))
if type(spacing) == np.ndarray:
check_size(spacing)
check_type(origin, (type(None), np.ndarray))
if type(origin) == np.ndarray:
check_size(origin)
# if origin isn't specified but spacing is, set origin to np.array([0, 0, 0])
if type(spacing) == np.ndarray and origin is None:
origin = np.array([0, 0, 0])
# voxel conversion option
if type(spacing) == np.ndarray:
df_voxel = self._df_in_voxel(self.df, spacing, origin)
G = self._df_to_graph(df_voxel)
# no voxel conversion option
else:
G = self._df_to_graph(self.df)
return G
def get_paths(
self, spacing: np.array = None, origin: np.array = None
) -> List[np.array]:
"""Converts dataframe in either spatial or voxel coordinates into a list of paths.
Will convert to voxel coordinates if spacing is specified.
Arguments
----------
spacing : None, :class:`numpy.array` (default = None)
Conversion factor (spatial units/voxel). Assumed to be np.array([x,y,z]).
Provided if graph should convert to voxel coordinates first. Default is None.
origin : None, :class:`numpy.array`
Origin of the spatial coordinate, if converting to voxels. Default is None.
Assumed to be np.array([x,y,z])
Returns
-------
paths : list
List of Nx3 numpy.array. Rows of the array are 3D coordinates in voxel
units. Each array is one path.
Example
-------
>>> swc_trace.get_paths()[0][1:10]
>>> array([[-52, -1, -1],
[-51, -1, 0],
[-51, -1, 0],
[-50, 0, 0],
[-50, 0, 0],
[-49, 0, 0],
[-48, 0, 0],
[-46, 0, 0],
[-46, 0, 0]], dtype=object)
"""
check_type(spacing, (type(None), np.ndarray))
if type(spacing) == np.ndarray:
check_size(spacing)
check_type(origin, (type(None), np.ndarray))
if type(origin) == np.ndarray:
check_size(origin)
# if origin isn't specified but spacing is, set origin to np.array([0, 0, 0])
if type(spacing) == np.ndarray and origin is None:
origin = np.array([0, 0, 0])
# voxel conversion option
if type(spacing) == np.ndarray:
df_voxel = self._df_in_voxel(self.df, spacing, origin)
G = self._df_to_graph(df_voxel)
# no voxel conversion option
else:
G = self._df_to_graph(self.df)
paths = self._graph_to_paths(G)
return paths
def generate_df_subset(
self,
vox_in_img_list: list,
subneuron_start: int = None,
subneuron_end: int = None,
) -> pd.DataFrame:
"""Read a new subset dataframe in coordinates in img spacing.
Specify specific range of vertices from dataframe if desired
Arguments
----------
vox_in_img_list : list
List of voxels
subneuron_start : None, int (default = None)
Provides start index, if specified, to apply function to a portion of the dataframe
Default is None.
subneuron_end : None, int (default = None)
Provides end index, if specified, to apply function to a portion of the dataframe
Default is None.
Returns
-------
df : :class:`pandas.DataFrame`
Indicies, coordinates (in img spacing) and parents of each node.
Coordinates are in spatial units.
Example
-------
>>> #swc input, subneuron_start and subneuron_end specified
>>> subneuron_start = 5
>>> subneuron_end = 8
>>> #generate vox_in_img_list
>>> my_list = []
>>>for i in range(subneuron_end-subneuron_start):
my_list.append(10)
>>> vox_in_img_list_2 = list([my_list,my_list,my_list])
>>>swc_trace.generate_df_subset(vox_in_img_list_2,subneuron_start,subneuron_end)
>>> sample structure x y z r parent
5 6 0 10 10 10 1.0 5
6 7 0 10 10 10 1.0 6
7 8 0 10 10 10 1.0 7
"""
check_type(vox_in_img_list, list)
check_type(subneuron_start, (type(None), int))
check_type(subneuron_end, (type(None), int))
if (subneuron_start == None and type(subneuron_end) == int) or (
type(subneuron_start) == int and subneuron_end == None
):
raise ValueError(
"Provide both starting and ending vertices to use for the subneuron"
)
# no subneuron range specified
df = self.df
# subneuron range specified
if subneuron_start != None and subneuron_end != None:
subneuron_df = self.df[subneuron_start:subneuron_end]
df = subneuron_df
df_new = self._generate_df_subset(df, vox_in_img_list)
return df_new
def get_bfs_subgraph(
self,
node_id: int,
depth: int,
df: pd.DataFrame = None,
spacing: np.array = None,
origin: np.array = None,
) -> Tuple[nx.classes.digraph.DiGraph, nx.classes.digraph.DiGraph, List[np.array]]:
"""
Creates a spanning subgraph from a seed node and parent graph using BFS.
Arguments
----------
node_id : int
The id of the node to use as a seed.
If df is not None this become the node index.
depth : int
The max depth for BFS to traven in each direction.
df : None, DataFrame (default = None)
Dataframe storing indices.
In some cases indexing by row number is preferred.
spacing : None, :class:`numpy.array` (default = None)
Conversion factor (spatial units/voxel). Assumed to be np.array([x,y,z]).
Provided if graph should convert to voxel coordinates first. Default is None.
origin : :class:`numpy.array`
Origin of the spatial coordinate, if converting to voxels. Default is None.
Assumed to be np.array([x,y,z])
Returns
-------
G_sub : :class:`networkx.classes.digraph.DiGraph`
Subgraph
tree : DiGraph
The tree returned by BFS.
paths : list
List of Nx3 numpy.array. Rows of the array are 3D coordinates in voxel
units. Each array is one path.
Example
-------
>>> #swc input, specify node_id and depth
>>> swc_trace.get_bfs_subgraph(node_id=11,depth=2)
>>>(<networkx.classes.digraph.DiGraph at 0x7f7f2ce65670>,
<networkx.classes.digraph.DiGraph at 0x7f7f2ce65370>,
array([array([[4727, 4440, 3849],
[4732, 4442, 3850],
[4739, 4455, 3849]]),
array([[4732, 4442, 3850],
[4749, 4439, 3856]])], dtype=object))
"""
check_type(node_id, (list, int))
check_type(depth, int)
check_type(df, (type(None), pd.core.frame.DataFrame))
check_type(spacing, (type(None), np.ndarray))
if type(spacing) == np.ndarray:
check_size(spacing)
check_type(origin, (type(None), np.ndarray))
if type(origin) == np.ndarray:
check_size(origin)
# if origin isn't specified but spacing is, set origin to np.array([0, 0, 0])
if type(spacing) == np.ndarray and origin is None:
origin = np.array([0, 0, 0])
# voxel conversion option
if type(spacing) == np.ndarray:
df_voxel = self._df_in_voxel(self.df, spacing, origin)
G = self._df_to_graph(df_voxel)
# no voxel conversion option
else:
G = self._df_to_graph(self.df)
G_sub, tree = self._get_bfs_subgraph(G, node_id, depth, df)
paths = self._graph_to_paths(G_sub)
return G_sub, tree, paths
def get_sub_neuron(
self,
bounding_box: Union[tuple, list, None],
spacing: np.array = None,
origin: np.array = None,
) -> nx.classes.digraph.DiGraph:
"""Returns sub-neuron with node coordinates bounded by start and end
Arguments
----------
bounding_box : tuple or list or None
Defines a bounding box around a sub-region around the neuron. Length 2
tuple/list. First element is the coordinate of one corner (inclusive)
and second element is the coordinate of the opposite corner (exclusive).
Both coordinates are numpy.array([x,y,z])in voxel units.
spacing : None, :class:`numpy.array` (default = None)
Conversion factor (spatial units/voxel). Assumed to be np.array([x,y,z]).
Provided if graph should convert to voxel coordinates first. Default is None.
origin : :class:`numpy.array`
Origin of the spatial coordinate, if converting to voxels. Default is None.
Assumed to be np.array([x,y,z])
Returns
-------
G_sub : :class:`networkx.classes.digraph.DiGraph`
Neuron from swc represented as directed graph. Coordinates x,y,z are
node attributes accessed by keys 'x','y','z' respectively.
Example
-------
>>> bounding_box=[[1,2,4],[1,2,3]]
>>> #swc input, no spacing and origin
>>> swc_trace.get_sub_neuron(bounding_box)
>>> <networkx.classes.digraph.DiGraph at 0x7f81a95d1e50>
"""
check_type(bounding_box, (tuple, list))
if len(bounding_box) != 2:
raise ValueError("Bounding box must be length 2")
check_type(spacing, (type(None), np.ndarray))
check_type(spacing, (type(None), np.ndarray))
if type(spacing) == np.ndarray:
check_size(spacing)
check_type(origin, (type(None), np.ndarray))
if type(origin) == np.ndarray:
check_size(origin)
# if origin isn't specified but spacing is, set origin to np.array([0, 0, 0])
if type(spacing) == np.ndarray and origin is None:
origin = np.array([0, 0, 0])
# voxel conversion option
if type(spacing) == np.ndarray:
df_voxel = self._df_in_voxel(self.df, spacing, origin)
G = self._df_to_graph(df_voxel)
# no voxel conversion option
else:
G = self._df_to_graph(self.df)
G_sub = self._get_sub_neuron(G, bounding_box)
return G_sub
def get_sub_neuron_paths(
self,
bounding_box: Union[tuple, list, None],
spacing: np.array = None,
origin: np.array = None,
) -> List[np.array]:
"""Returns sub-neuron with node coordinates bounded by start and end
Arguments
----------
bounding_box : tuple or list or None
Defines a bounding box around a sub-region around the neuron. Length 2
tuple/list. First element is the coordinate of one corner (inclusive)
and second element is the coordinate of the opposite corner (exclusive).
Both coordinates are numpy.array([x,y,z])in voxel units.
spacing : None, :class:`numpy.array` (default = None)
Conversion factor (spatial units/voxel). Assumed to be np.array([x,y,z]).
Provided if graph should convert to voxel coordinates first. Default is None.
origin : :class:`numpy.array`
Origin of the spatial coordinate, if converting to voxels. Default is None.
Assumed to be np.array([x,y,z])
Returns
-------
paths : list
List of Nx3 numpy.array. Rows of the array are 3D coordinates in voxel
units. Each array is one path.
Example
-------
>>> bounding_box=[[1,2,4],[1,2,3]]
>>> #swc input, no spacing and origin
>>> swc_trace.get_sub_neuron_paths(bounding_box)
>>> array([], dtype=object)
"""
check_type(bounding_box, (tuple, list))
if len(bounding_box) != 2:
raise ValueError("Bounding box must be length 2")
check_type(spacing, (type(None), np.ndarray))
check_type(spacing, (type(None), np.ndarray))
if type(spacing) == np.ndarray:
check_size(spacing)
check_type(origin, (type(None), np.ndarray))
if type(origin) == np.ndarray:
check_size(origin)
# if origin isn't specified but spacing is, set origin to np.array([0, 0, 0])
if type(spacing) == np.ndarray and origin is None:
origin = np.array([0, 0, 0])
# voxel conversion option
if type(spacing) == np.ndarray:
df_voxel = self._df_in_voxel(self.df, spacing, origin)
G = self._df_to_graph(df_voxel)
# no voxel conversion option
else:
G = self._df_to_graph(self.df)
G_sub = self._get_sub_neuron(G, bounding_box)
paths = self._graph_to_paths(G_sub, round=self.rounding)
return paths
@staticmethod
def ssd(pts1: np.array, pts2: np.array) -> float:
"""Compute significant spatial distance metric between two traces as defined in APP1.
Args:
pts1 (np.array): array containing coordinates of points of trace 1. shape: npoints x ndims
pts2 (np.array): array containing coordinates of points of trace 1. shape: npoints x ndims
Returns:
[float]: significant spatial distance as defined by APP1
Example
-------
>>> pts1 = swc_trace.get_paths()[0][1:10]
>> pts2 = swc_trace.get_paths()[0][11:20]
>>> NeuronTrace.ssd(pts1,pts2)
>>>6.247937554557103
"""
check_type(pts1, np.ndarray)
check_type(pts2, np.ndarray)
_, dists1 = pairwise_distances_argmin_min(pts1, pts2)
dists1 = dists1[dists1 >= 2]
_, dists2 = pairwise_distances_argmin_min(pts2, pts1)
dists2 = dists2[dists2 >= 2]
# If there are is no significant distance between the 2 sets
if len(dists1) == 0 and len(dists2) == 0:
ssd = 0
# Else, calculate the mean
else:
dists = np.concatenate([dists1, dists2])
ssd = np.mean(dists)
return ssd
# private methods
def _read_swc(
self, path: str
) -> Tuple[pd.DataFrame, List[float], List[int], int, int]:
"""
Read a single swc file
Arguments:
path {string} -- path to file
raw {bool} -- whether you are passing the file directly
Returns:
df {pandas dataframe} -- indices, coordinates, and parents of each node
offset {list of floats} -- offset value of fragment
color {list of ints} -- color
cc {int} -- cc value, from file name
branch {int} -- branch number, from file name
"""
# check input
file = open(path, "r")
in_header = True
offset_found = False
header_length = -1
offset = np.nan
color = np.nan
cc = np.nan
branch = np.nan
while in_header:
line = file.readline().split()
if "OFFSET" in line:
offset_found = True
idx = line.index("OFFSET") + 1
offset = [float(line[i]) for i in np.arange(idx, idx + 3)]
elif "COLOR" in line:
idx = line.index("COLOR") + 1
line = line[idx]
line = line.split(",")
color = [float(line[i]) for i in np.arange(len(line))]
elif "NAME" in line:
idx = line.index("NAME") + 1
name = line[idx]
name = re.split(r"_|-|\.", name)
try:
idx = name.index("cc") + 1
cc = int(name[idx])
idx = name.index("branch") + 1
branch = int(name[idx])
except ValueError:
pass
elif line[0] != "#":
in_header = False
header_length += 1
if not offset_found:
warnings.warn("No offset information found in: " + path)
offset = [float(0) for i in range(3)]
# read coordinates
df = pd.read_table(
path,
names=["sample", "structure", "x", "y", "z", "r", "parent"],
skiprows=header_length,
delimiter="\s+",
)
return df, offset, color, cc, branch
def _read_swc_offset(self, path: str) -> Tuple[pd.DataFrame, List[int], int, int]:
df, offset, color, cc, branch = self._read_swc(path)
df["x"] = df["x"] + offset[0]
df["y"] = df["y"] + offset[1]
df["z"] = df["z"] + offset[2]
return df, color, cc, branch
def _read_s3(
self, s3_path: str, seg_id: int, mip: int, rounding: Optional[bool] = True
):
"""Read a s3 bucket path to a skeleton object
into a pandas dataframe.
Parameters
----------
s3_path : str
String representing the path to the s3 bucket
seg_id : int
The segement number to pull
mip : int
The resolution to use for scaling
rounding: bool, Optional
True is default, false if swc shouldn't be rounded
Returns
-------
df : :class:`pandas.DataFrame`
Indicies, coordinates, and parents of each node in the swc.
Coordinates are in spatial units.
"""
# TODO check header length
# check input
cv = CloudVolume(
s3_path, mip=mip, fill_missing=self.fill_missing, use_https=self.use_https
)
skeleton = cv.skeleton.get(seg_id)
swc_string = skeleton.to_swc()
string_io = StringIO(swc_string)
splitted_string = swc_string.split("\n")
in_h = True
h_len = -1
while in_h:
h_len += 1
line = splitted_string[h_len]
if len(line) == 0 or line[0] != "#":
in_h = False
df = pd.read_table(
string_io,
names=["sample", "structure", "x", "y", "z", "r", "parent"],
skiprows=h_len,
sep=" "
# delim_whitespace=True,
)
# round swc files when reading
if rounding == True:
res = cv.scales[mip]["resolution"]
df["x"] = np.round(df["x"] / res[0])
df["y"] = np.round(df["y"] / res[1])
df["z"] = np.round(df["z"] / res[2])
return df
def _generate_df_subset(self, swc_df, vox_in_img_list):
"""Read a new subset of swc dataframe in coordinates in img spacing.
Parameters
----------
swc_df : pd.DataFrame
DataFrame containing information from swc file
vox_in_img_list: list
List of voxels
Returns
-------
df : :class:`pandas.DataFrame`
Indicies, coordinates (in img spacing) and parents of each node in the swc.
Coordinates are in spatial units.
"""
# check input
df_new = swc_df.copy()
df_new["x"], df_new["y"], df_new["z"] = (
vox_in_img_list[:][0],
vox_in_img_list[:][1],
vox_in_img_list[:][2],
)
return df_new
def _space_to_voxel(
self,
spatial_coord: np.array,
spacing: np.array,
origin: np.array = np.array([0, 0, 0]),
) -> np.array:
"""Converts coordinate from spatial units to voxel units.
Parameters
----------
spatial_coord : :class:`numpy.array`
3D coordinate in spatial units. Assumed to be np.array[(x,y,z)]
spacing : :class:`numpy.array`
Conversion factor (spatial units/voxel). Assumed to be np.array([x,y,z])
origin : :class:`numpy.array`
Origin of the spatial coordinate. Default is (0,0,0). Assumed to be
np.array([x,y,z])
Returns
-------
voxel_coord : :class:`numpy.array`
Coordinate in voxel units. Assumed to be np.array([x,y,z])
"""
if np.any(spacing == 0):
raise ValueError(f"Zero detected in spacing: {spacing}")
voxel_coord = np.round(np.divide(spatial_coord - origin, spacing))
voxel_coord = voxel_coord.astype(np.int64)
return voxel_coord
def _df_in_voxel(
self,
df: pd.DataFrame,
spacing: np.array,
origin: np.array = np.array([0, 0, 0]),
) -> pd.DataFrame:
"""Converts coordinates in pd.DataFrame representing swc from spatial units
to voxel units
Parameters
----------
df : :class:`pandas.DataFrame`
Indicies, coordinates, and parents of each node in the swc. Coordinates
are in spatial units.
spacing : :class:`numpy.array`
Conversion factor (spatial units/voxel). Assumed to be np.array([x,y,z])
origin : :class:`numpy.array`
Origin of the spatial coordinate. Default is (0,0,0). Assumed to be
np.array([x,y,z])
Returns
-------
df_voxel : :class:`pandas.DataFrame`
Indicies, coordinates, and parents of each node in the swc. Coordinates
are in voxel units.
"""
x = []
y = []
z = []
df_voxel = df.copy()
for index, row in df_voxel.iterrows():
vox = self._space_to_voxel(row[["x", "y", "z"]].to_numpy(), spacing, origin)
x.append(vox[0])
y.append(vox[1])
z.append(vox[2])
df_voxel["x"] = x
df_voxel["y"] = y
df_voxel["z"] = z
return df_voxel
def _df_to_graph(self, df, round=False):
"""Converts dataframe form of neuron trace into a directed graph
Parameters
----------
df_voxel : :class:`pandas.DataFrame`
Indices, coordinates, and parents of each node in the swc.
round : boolean
Whether coordinates/distances should be rounded to integers.
Returns
-------
G : :class:`networkx.classes.digraph.DiGraph`
Neuron from swc represented as directed graph. Coordinates x,y,z are
node attributes accessed by keys 'x','y','z' respectively.
"""
G = nx.DiGraph()
# add nodes
for index, row in df.iterrows():
id = int(row["sample"])
coord = [row["x"], row["y"], row["z"]]
if round:
coord = [int(c) for c in coord]
G.add_node(id)
G.nodes[id]["x"] = coord[0]
G.nodes[id]["y"] = coord[1]
G.nodes[id]["z"] = coord[2]
# add edges
for index, row in df.iterrows():
child = int(row["sample"])
child_coord = [row["x"], row["y"], row["z"]]
parent = int(row["parent"])
if parent > min(df["parent"]):
parent_row = df[df["sample"] == parent]
parent_coord = [parent_row["x"], parent_row["y"], parent_row["z"]]
dist = np.linalg.norm(np.subtract(child_coord, parent_coord))
if round:
dist = int(dist)
G.add_edge(parent, child, distance=dist)
return G
def _get_sub_neuron(
self, G: nx.classes.digraph.DiGraph, bounding_box: Union[tuple, list, None]
) -> nx.classes.digraph.DiGraph:
"""Returns sub-neuron with node coordinates bounded by start and end
Parameters
----------
G : :class:`networkx.classes.digraph.DiGraph`
Neuron from swc represented as directed graph. Coordinates x,y,z are
node attributes accessed by keys 'x','y','z' respectively.
bounding_box : tuple or list or None
Defines a bounding box around a sub-region around the neuron. Length 2
tuple/list. First element is the coordinate of one corner (inclusive) and second element is the coordinate of the opposite corner (exclusive). Both coordinates are numpy.array([x,y,z])in voxel units.
Returns
-------
G_sub : :class:`networkx.classes.digraph.DiGraph`
Neuron from swc represented as directed graph. Coordinates x,y,z are
node attributes accessed by keys 'x','y','z' respectively.
"""
G_sub = G.copy() # make copy of input G
start = bounding_box[0]
end = bounding_box[1]
# remove nodes that are not neighbors of nodes bounded by start and end
for node in list(G_sub.nodes):
neighbors = list(G_sub.successors(node)) + list(G_sub.predecessors(node))
remove = True
for id in neighbors + [node]:
x = G_sub.nodes[id]["x"]
y = G_sub.nodes[id]["y"]
z = G_sub.nodes[id]["z"]
if x >= start[0] and y >= start[1] and z >= start[2]:
if x < end[0] and y < end[1] and z < end[2]:
remove = False