I have a list of 300000 lists (fiber tracks), where each track is a list of (x,y,z) tuples/coordinates:
tracks=
[[(1,2,3),(3,2,4),...]
[(4,2,1),(5,7,3),...]
...
]
I also have a group of masks, where each mask is defined as a list of (x,y,z) tuples/coordinates:
mask_coords_list=
[[(1,2,3),(8,13,4),...]
[(6,2,2),(5,7,3),...]
...
]
I am trying to find, for all possible pairs of masks:
- the number of tracks that intersect each mask-mask pair (to create a connectivity matrix)
- the subset of tracks that intersect each mask, in order to add 1 to each (x,y,z) coordinate for each track in the subset (to create a "density" image)
I'm currently doing part 1 like so:
def mask_connectivity_matrix(tracks,masks,masks_coords_list):
connect_mat=zeros((len(masks),len(masks)))
for track in tracks:
cur=[]
for count,mask_coords in enumerate(masks_coords_list):
if any(set(track) & set(mask_coords)):
cur.append(count)
for x,y in list(itertools.combinations(cur,2)):
connect_mat[x,y] += 1
and part 2 like so:
def mask_tracks(tracks,masks,masks_coords_list):
vox_tracks_img=zeros((xdim,ydim,zdim,len(masks)))
for track in tracks:
for count,mask in enumerate(masks_coords_list):
if any(set(track) & set(mask)):
for x,y,z in track:
vox_tracks_img[x,y,z,count] += 1
Using sets to find intersections has sped this process up significantly but both portions still take over an hour when I have a list of 70 or more masks. Is there a more efficient way to do this than iterating for each track?