-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmc_NearestNetworkNode.m
More file actions
144 lines (114 loc) · 3.84 KB
/
mc_NearestNetworkNode.m
File metadata and controls
144 lines (114 loc) · 3.84 KB
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
function [ labels ] = mc_NearestNetworkNode(voxlist, radius, refimage)
%MC_NEARESTNETWORKNODE A function to map unlabeled network nodes to
%network of their nearest neighbor
%
% Input Args
% voxlist - nROI*3 matrix of coordinates in MNI
% space
% radius - Radius of sphere to search in MNI distance
% refimage- OPTIONAL - Path to a reference image's hdr
% If not specified, defaults to Yeo map at
% /net/data4/MAS/ROIS/Yeo/YeoPlus.hdr
% Output Args
%
% labels - 1*nRoi list of values taken from
% nearest neighbor
% NOTES
%% Get the space
if ~exist('refimage','var')
refimage='/net/data4/MAS/ROIS/Yeo/YeoPlus.hdr';
end
v2m=spm_get_space(refimage);
m2v=inv(v2m);
VoxSize=spm_imatrix(v2m);
VoxSize=VoxSize(7:9);
VoxSize=abs(VoxSize);
minSize=min(VoxSize);
%% Convert radius to voxels in smallest direction
VoxRad=ceil(radius/minSize);
offset=buildsphere(VoxRad);
%% Loop over stuff
for iNode = 1:size(voxlist,1)
labels(iNode) = spherecheck(voxlist(iNode,:),offset,v2m,m2v,refimage);
end
function network_win=spherecheck(mni_anchor,offset,v2m,m2v,refimage)
% This function does the heavy lifting. Given an anchor point in mni and offset, it finds the winning network
vox_anchor = mni2vox(mni_anchor,m2v);
sphere_vox = offset + repmat(vox_anchor,size(offset,1),1);
sphere_vox = prune_vox(sphere_vox,refimage); % Prune to only include voxels that are in the bounding box
if size(sphere_vox,1) == 0
network_win=0;
return
end
sphere_mni = vox2mni(sphere_vox,v2m);
distances = calc_mni_dist_mni(sphere_mni,mni_anchor);
networks = mc_network_lookup(refimage,sphere_mni);
networks = networks(:,4);
if nnz(networks) == 0
network_win=0;
return
end
nnz_networks = networks(find(networks),:);
nnz_sphere_mni = sphere_mni(find(networks),:);
nnz_distances = distances(find(networks),:);
[mindist, min_nnz_distances_idx] = min(nnz_distances);
network_win = nnz_networks(min_nnz_distances_idx);
end
function pruned = prune_vox(voxlist,refimage)
hdr=spm_vol(refimage);
pass1=voxlist(:,1)>=1 & voxlist(:,1)<=hdr.dim(1);
pass2=voxlist(:,2)>=1 & voxlist(:,2)<=hdr.dim(2);
pass3=voxlist(:,3)>=1 & voxlist(:,3)<=hdr.dim(3);
pass=pass1 & pass2 & pass3;
pruned=voxlist(pass,:);
end
function mnidistance = calc_mni_dist_vox(vox1,vox2,v2m)
mni1=vox2mni(vox1,v2m);
mni2=vox2mni(vox2,v2m);
mnidistance=calc_dist_generic(mni1,mni2);
end
function mnidistance = calc_mni_dist_mni(mni1,mni2)
mnidistance=calc_dist_generic(mni1,mni2);
end
function voxdistance = calc_vox_dist_vox(vox1,vox2)
voxdistance=calc_dist_generic(vox1,vox2);
end
function voxdistance = calc_vox_dist_mni(mni1,mni2,m2v)
vox1=mni2vox(mni1,m2v);
vox2=mni2vox(mni2,m2v);
voxdistance=calc_dist_generic(vox1,vox2);
end
function distance = calc_dist_generic(coord1,coord2)
% Calculate the distance between one or more points and a reference point in arbitrary space
coord2=repmat(coord2,size(coord1,1),1);
distance = sqrt(sum((coord1-coord2).^2,2));
end
function vox = mni2vox(mni,m2v)
vox = m2v*[mni repmat(1,size(mni,1),1)]';
vox = vox(1:3,:)';
end
function mni = vox2mni(vox,v2m)
mni = v2m*[vox repmat(1,size(vox,1),1)]';
mni = mni(1:3,:)';
end
function results = buildsphere(R)
% Borrowed from SOM_MakeSphereROI.m from Robert Welsh
Rbox = round(R);
if R < 1
results = [0; 0; 0];
return
end
Xs = [-Rbox:Rbox];
Ys = [-Rbox:Rbox];
Zs = [-Rbox:Rbox];
XGrid = repmat(Xs,[length(Ys) 1]);
YGrid = repmat(Ys',[1 length(Xs)]);
results = [];
% Now loop on the Z's and find out if in the radius.
for iZ = 1:length(Zs)
rDist = sqrt(XGrid(:).^2 + YGrid(:).^2 + Zs(iZ)^2);
RIDX = find(R>=rDist);
results = [results; XGrid(RIDX) YGrid(RIDX) Zs(iZ)*ones(length(RIDX),1)];
end
end
end