-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssd_patterns.m
245 lines (213 loc) · 10.3 KB
/
ssd_patterns.m
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
%% Create interpolated SSD patterns and plot raw points (Figures 2b, S3, & S4)
repo_path = 'Path_to\Manuscript_repository';
addpath('Path_to\wjn_toolbox_tsbinns');
addpath(genpath('Path_to\spm12'));
addpath(genpath('Path_to\leaddbs'));
folderpath_analysis = 'Path_to\Project\Analysis\Results\BIDS_01_Berlin_Neurophys\sub-multi\ses-multi';
%% MED OFF and ON averaged (Figures 2b & S4)
fbands = ["alpha", "low_beta", "high_beta"];
regions = ["cortex", "STN"];
patterns_table = readtable(fullfile(folderpath_analysis, 'ssd_topographies_cortex_STN_MedOffOn.csv'));
for region_i = 1:length(regions)
region = regions(region_i);
region_table = patterns_table(ci(region, patterns_table.ch_regions), :);
% define meshes to plot and interpolate to
if strcmp(region, 'cortex')
interp_mesh = fullfile(meshes_path, 'Automated Anatomical Labeling 3 (Rolls 2020).nii');
cortex_mesh = load(fullfile(meshes_path, 'CortexHiRes.mat'));
plot_mesh.vertices = cortex_mesh.Vertices_rh;
plot_mesh.faces = cortex_mesh.Faces_rh;
elseif strcmp(region, 'STN')
interp_mesh = fullfile(meshes_path, 'STN_rh.nii');
plot_mesh = export(gifti(fullfile(meshes_path, 'STN_rh.surf.gii')));
else
error('Regions to interpolate to must be cortex or STN.')
end
for fband_i = 1:length(fbands)
fband = fbands(fband_i);
fband_table = region_table(ci(fband, region_table.band_names), :);
coords = [];
patterns = [];
% extract values from table
for idx = 1:size(fband_table, 1)
coords(idx,:) = eval(fband_table.ch_coords{idx});
patterns(idx,1) = fband_table.ssd_topographies(idx);
end
% pin coordinates to surface (if cortex)
if strcmp(region, 'cortex')
cortex_surf = export(gifti(fullfile(meshes_path, 'Automated Anatomical Labeling 3 (Rolls 2020).surf.gii')));
for idx = 1 : size(coords, 1)
[mind(1, idx), i(1, idx)] = min(wjn_distance(cortex_surf.vertices, [abs(coords(idx, 1)), coords(idx, 2:3)]));
coords(idx, :) = cortex_surf.vertices(i(idx), :) * 0.975;
end
end
% interpolate patterns to mesh
heatmap_fname = sprintf('SSD_%s_%s_Med.nii', region, fband);
wjn_heatmap(heatmap_fname, coords, patterns - min(patterns), interp_mesh, [4, 4, 4])
% (above saves file in current directory, so move to figures directory)
movefile(heatmap_fname, sprintf('..\\figures\\%s', heatmap_fname));
movefile("s"+heatmap_fname, sprintf('..\\figures\\s%s', heatmap_fname));
movefile("z_s"+heatmap_fname, sprintf('..\\figures\\z_s%s', heatmap_fname));
% plot locations of raw points
figure
hold on
if strcmp(region, 'cortex')
mesh = wjn_plot_surface(plot_mesh);
mesh.FaceAlpha = 0.4;
hold on
wjn_plot_colored_spheres(coords, patterns, 2)
else
mesh = wjn_plot_surface(plot_mesh, [], 10);
mesh.FaceAlpha = 0.2;
mesh.EdgeColor = 'w';
mesh.EdgeAlpha = 0.5;
hold on
wjn_plot_colored_spheres(coords, patterns, 0.25)
view(5, 23)
end
camlight
exportgraphics(gcf, fullfile(repo_path, 'figures', sprintf('SSD_%s_%s_Med.png', region, fband)), 'Resolution', 1000);
end
end
%% MED OFF and ON separately (Figure S3a)
fbands = ["high_beta"];
regions = ["cortex"];
meds = ["off", "on"];
patterns_table = readtable(fullfile(folderpath_analysis, 'ssd_topographies_cortex_STN_MedOffOn.csv'));
for med_i = 1:length(meds)
med = meds(med_i);
med_table = patterns_table(ci(med, patterns_table.med), :);
for region_i = 1:length(regions)
region = regions(region_i);
region_table = med_table(ci(region, med_table.ch_regions), :);
% define meshes to plot and interpolate to
if strcmp(region, 'cortex')
interp_mesh = fullfile(meshes_path, 'Automated Anatomical Labeling 3 (Rolls 2020).nii');
cortex_mesh = load(fullfile(meshes_path, 'CortexHiRes.mat'));
plot_mesh.vertices = cortex_mesh.Vertices_rh;
plot_mesh.faces = cortex_mesh.Faces_rh;
elseif strcmp(region, 'STN')
interp_mesh = fullfile(meshes_path, 'STN_rh.nii');
plot_mesh = export(gifti(fullfile(meshes_path, 'STN_rh.surf.gii')));
else
error('Regions to interpolate to must be cortex or STN.')
end
for fband_i = 1:length(fbands)
fband = fbands(fband_i);
fband_table = region_table(ci(fband, region_table.band_names), :);
coords = [];
patterns = [];
% extract values from table
for idx = 1:size(fband_table, 1)
coords(idx,:) = eval(fband_table.ch_coords{idx});
patterns(idx,1) = fband_table.ssd_topographies(idx);
end
% pin coordinates to surface (if cortex)
if strcmp(region, 'cortex')
cortex_surf = export(gifti(fullfile(meshes_path, 'Automated Anatomical Labeling 3 (Rolls 2020).surf.gii')));
for idx = 1 : size(coords, 1)
[mind(1, idx), i(1, idx)] = min(wjn_distance(cortex_surf.vertices, [abs(coords(idx, 1)), coords(idx, 2:3)]));
coords(idx, :) = cortex_surf.vertices(i(idx), :) * 0.975;
end
end
% interpolate patterns to mesh (and save as nifti file)
heatmap_fname = sprintf('SSD_%s_%s_Med%s.nii', region, fband, med);
wjn_heatmap(heatmap_fname, coords, patterns - min(patterns), interp_mesh, [4, 4, 4])
% (above saves file in current directory, so move to figures directory)
movefile(heatmap_fname, sprintf('..\\figures\\%s', heatmap_fname));
movefile("s"+heatmap_fname, sprintf('..\\figures\\s%s', heatmap_fname));
movefile("z_s"+heatmap_fname, sprintf('..\\figures\\z_s%s', heatmap_fname));
% plot locations of raw points
figure
hold on
if strcmp(region, 'cortex')
mesh = wjn_plot_surface(plot_mesh);
mesh.FaceAlpha = 0.4;
hold on
wjn_plot_colored_spheres(coords, patterns, 2)
else
mesh = wjn_plot_surface(plot_mesh, [], 10);
mesh.FaceAlpha = 0.2;
mesh.EdgeColor = 'w';
mesh.EdgeAlpha = 0.5;
hold on
wjn_plot_colored_spheres(coords, patterns, 0.25)
view(5, 23)
end
camlight
exportgraphics(gcf, fullfile(coherence_path, 'figures', sprintf('SSD_%s_%s_Med%s.png', region, fband, med)), 'Resolution', 1000);
end
end
end
%% STIM OFF and ON separately (Figure S3b)
fbands = ["high_beta"];
regions = ["cortex"];
stims = ["off", "on"];
patterns_table = readtable(fullfile(folderpath_analysis, 'ssd_topographies_cortex_STN_StimOffOn.csv'));
for stim_i = 1:length(stims)
stim = stims(stim_i);
stim_table = patterns_table(ci(stim, patterns_table.stim), :);
for region_i = 1:length(regions)
region = regions(region_i);
region_table = stim_table(ci(region, stim_table.ch_regions), :);
% define meshes to plot and interpolate to
if strcmp(region, 'cortex')
interp_mesh = fullfile(meshes_path, 'Automated Anatomical Labeling 3 (Rolls 2020).nii');
cortex_mesh = load(fullfile(meshes_path, 'CortexHiRes.mat'));
plot_mesh.vertices = cortex_mesh.Vertices_rh;
plot_mesh.faces = cortex_mesh.Faces_rh;
elseif strcmp(region, 'STN')
interp_mesh = fullfile(meshes_path, 'STN_rh.nii');
plot_mesh = export(gifti(fullfile(meshes_path, 'STN_rh.surf.gii')));
else
error('Regions to interpolate to must be cortex or STN.')
end
for fband_i = 1:length(fbands)
fband = fbands(fband_i);
fband_table = region_table(ci(fband, region_table.band_names), :);
coords = [];
patterns = [];
% extract values from table
for idx = 1:size(fband_table, 1)
coords(idx,:) = eval(fband_table.ch_coords{idx});
patterns(idx,1) = fband_table.ssd_topographies(idx);
end
% pin coordinates to surface (if cortex)
if strcmp(region, 'cortex')
cortex_surf = export(gifti(fullfile(meshes_path, 'Automated Anatomical Labeling 3 (Rolls 2020).surf.gii')));
for idx = 1 : size(coords, 1)
[mind(1, idx), i(1, idx)] = min(wjn_distance(cortex_surf.vertices, [abs(coords(idx, 1)), coords(idx, 2:3)]));
coords(idx, :) = cortex_surf.vertices(i(idx), :) * 0.975;
end
end
% interpolate patterns to mesh (and save as nifti file)
heatmap_fname = sprintf('SSD_%s_%s_Stim%s.nii', region, fband, stim);
wjn_heatmap(heatmap_fname, coords, patterns - min(patterns), interp_mesh, [4, 4, 4])
% (above saves file in current directory, so move to figures directory)
movefile(heatmap_fname, sprintf('..\\figures\\%s', heatmap_fname));
movefile("s"+heatmap_fname, sprintf('..\\figures\\s%s', heatmap_fname));
movefile("z_s"+heatmap_fname, sprintf('..\\figures\\z_s%s', heatmap_fname));
% plot locations of raw points
figure
hold on
if strcmp(region, 'cortex')
mesh = wjn_plot_surface(plot_mesh);
mesh.FaceAlpha = 0.4;
hold on
wjn_plot_colored_spheres(coords, patterns, 2)
else
mesh = wjn_plot_surface(plot_mesh, [], 10);
mesh.FaceAlpha = 0.2;
mesh.EdgeColor = 'w';
mesh.EdgeAlpha = 0.5;
hold on
wjn_plot_colored_spheres(coords, patterns, 0.25)
view(5, 23)
end
camlight
exportgraphics(gcf, fullfile(coherence_path, 'figures', sprintf('SSD_%s_%s_Stim%s.png', region, fband, stim)), 'Resolution', 1000);
end
end
end
%% Plot interpolated patterns
% To plot interpolated patterns, use SurfIce