Skip to content

Commit 8123089

Browse files
authored
Merge pull request #14 from cpp-lln-lab/remi-refactor_roifroml_atlas
[ENH] refactor code for creation of ROIs from atlas
2 parents 1acf17d + a53b52a commit 8123089

12 files changed

+123
-85
lines changed
32 Bytes
Binary file not shown.

demos/atlas/create_roi_from_atlas.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
opt.roi.atlas = 'wang';
2+
opt.roi.name = {'V1v', 'V1d'};
3+
opt.roi.dir = fullfile(pwd, 'derivatives', 'cpp_roi', 'group');
4+
5+
spm_mkdir(opt.roi.dir);
6+
7+
hemi = {'lh', 'rh'};
8+
9+
for iHemi = 1:numel(hemi)
10+
11+
for iROI = 1:numel(opt.roi.name)
12+
13+
roiName = opt.roi.name{iROI};
14+
15+
imageName = extractRoiFromAtlas(opt.roi.dir, opt.roi.atlas, roiName, hemi{iHemi});
16+
17+
end
18+
19+
end

demos/roi/label_and_extract_clusters.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
% where each cluster has one label.
77
%
88

9+
run ../../initCppRoi;
10+
911
gunzip(fullfile('inputs', '*.gz'));
1012

1113
zMap = fullfile(pwd, 'inputs', 'visual motion_association-test_z_FDR_0.01.nii');

demos/roi/other_demo.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
% (C) Copyright 2021 CPP ROI developers
22

3+
run ../../initCppRoi;
4+
35
gunzip(fullfile('inputs', '*.gz'));
46
zMap = fullfile(pwd, 'inputs', 'visual motion_association-test_z_FDR_0.01.nii');
57

demos/roi/roi_script.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
clear;
1111
clc;
1212

13+
run ../../initCppRoi;
14+
1315
%% ASSUMPTION
1416
%
1517
% This assumes that the 2 immages are in the same space (MNI, individual)

src/atlas/extractRoiFromAtlas.m

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,37 @@
11
% (C) Copyright 2021 CPP ROI developers
22

3-
function [imageName, imageVolume] = extractRoiFromAtlas(atlas, label, hemisphere)
3+
function roiImage = extractRoiFromAtlas(roiDir, atlas, roiName, hemisphere)
44

5-
[imageName, imageVolume] = extractRoiFromLabel(sourceImage, label, hemisphere);
5+
if strcmp(atlas, 'wang')
66

7+
[maxProbaFiles, roiLabels] = getRetinoProbaAtlas();
8+
9+
if strcmp(hemisphere, 'lh')
10+
sourceImage = maxProbaFiles(1, :);
11+
else
12+
sourceImage = maxProbaFiles(2, :);
13+
end
14+
15+
end
16+
17+
roiIdx = strcmp(roiName, roiLabels.ROI);
18+
label = roiLabels.label(roiIdx);
19+
20+
labelStruct = struct('ROI', [hemisphere roiName], ...
21+
'label', label);
22+
23+
roiImage = extractRoiByLabel(sourceImage, labelStruct);
24+
25+
nameStructure = struct( ...
26+
'space', 'MNI', ...
27+
'hemi', hemisphere, ...
28+
'desc', atlas, ...
29+
'label', roiName, ...
30+
'type', 'mask', ...
31+
'ext', '.nii');
32+
newName = createFilename(nameStructure);
33+
34+
movefile(roiImage, fullfile(roiDir, newName));
35+
36+
roiImage = fullfile(roiDir, newName);
737
end

src/atlas/getRetinoProbaAtlas.m

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -15,54 +15,13 @@
1515
% PMID: 25452571
1616
% Probabilistic Maps of Visual Topography in Human Cortex
1717
%
18-
% If the data is not present in the ``cpp_spm/atlas/ProbAtlas_v4`` of the repo, it will be
19-
% downloaded and unzipped
2018

21-
README_URL = 'http://scholar.princeton.edu/sites/default/files/napl/files/readme.txt';
22-
ATLAS_URL = 'http://scholar.princeton.edu/sites/default/files/napl/files/probatlas_v4.zip';
19+
unzipAtlas('wang');
2320

24-
ATLAS_FOLDER = fullfile( ...
25-
fileparts(mfilename('fullpath')), ...
26-
'..', '..', 'atlas');
21+
atlasDir = returnAtlasDir('wang');
2722

28-
if ~exist(fullfile(ATLAS_FOLDER, 'ProbAtlas_v4'), 'dir')
23+
maxProbaFiles = spm_select('FPList', fullfile(atlasDir, 'subj_vol_all'), '^.*_dseg.nii$');
2924

30-
mkdir(ATLAS_FOLDER);
31-
32-
try
33-
urlwrite(ATLAS_URL, 'probatlas_v4.zip');
34-
unzip('probatlas_v4.zip', ATLAS_FOLDER);
35-
catch
36-
system(sprintf('curl -L %s -o probatlas_v4.zip', ATLAS_URL));
37-
unzip('probatlas_v4.zip', ATLAS_FOLDER);
38-
end
39-
40-
end
41-
42-
% urlwrite(README_URL, fullfile(ATLAS_FOLDER, 'ProbAtlas_v4', 'README.txt'));
43-
% urlread(README_URL)
44-
45-
maxProbaFiles = spm_select('FPListRec', ...
46-
fullfile(ATLAS_FOLDER, 'ProbAtlas_v4'), ...
47-
'^max.*.nii$');
48-
49-
if size(maxProbaFiles, 1) < 2
50-
51-
gunzip(fullfile(pwd, 'atlas', 'ProbAtlas_v4', 'subj_vol_all', 'max*.nii.gz'));
52-
53-
maxProbaFiles = spm_select('FPListRec', ...
54-
fullfile(ATLAS_FOLDER, 'ProbAtlas_v4'), ...
55-
'^max.*.nii$');
56-
57-
end
58-
59-
if size(maxProbaFiles, 1) < 2
60-
error('no atlas present');
61-
end
62-
63-
% the label file was created manually to be easier to load into SPM
64-
roiLabels = spm_load(spm_select('FPListRec', ...
65-
fullfile(ATLAS_FOLDER), ...
66-
'^ROI_labels_ProbAtlas_v4.csv$'));
25+
roiLabels = getRoiLabelLookUpTable('wang');
6726

6827
end

src/atlas/getRoiLabelLookUpTable.m

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,19 @@
77
return
88
end
99

10+
atlasDir = returnAtlasDir(atlas);
11+
1012
switch lower(atlas)
1113

1214
case 'wang'
1315

14-
atlasDir = fullfile(fileparts(mfilename('fullpath')), ...
15-
'..', ...
16-
'..', ...
17-
'atlas');
18-
19-
roiLabelLUT = spm_load(fullfile(atlasDir, ...
20-
'visual_topography_probability_atlas', ...
21-
'LUT.csv'));
16+
roiLabelLUT = spm_load(fullfile(atlasDir, 'LUT.csv'));
2217

2318
case 'anatomy_toobox'
2419

2520
anat_tb_URL = 'https://www.fz-juelich.de/SharedDocs/Downloads/INM/INM-1/DE/Toolbox/Toolbox_22c.html';
2621

27-
spmDir = spm('dir');
28-
29-
roiLabelLUT = fullfile(spmDir, 'toolbox', 'Anatomy', 'Anatomy_v22c_MPM.txt');
22+
roiLabelLUT = fullfile(atlasDir, 'Anatomy_v22c_MPM.txt');
3023

3124
if ~exist(roiLabelLUT, 'file')
3225
error('Did you install the spm Anatomy toolbox?\n\nDownload it from: %s', ...

src/atlas/returnAtlasDir.m

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
% (C) Copyright 2021 CPP ROI developers
2+
3+
function atlasDir = returnAtlasDir(atlas)
4+
5+
atlasDir = fullfile(fileparts(mfilename('fullpath')), '..', '..', 'atlas');
6+
7+
if nargin > 0
8+
9+
switch atlas
10+
11+
case 'wang'
12+
atlasDir = fullfile(atlasDir, 'visual_topography_probability_atlas');
13+
14+
case 'anatomy_toobox'
15+
atlasDir = fullfile(spm('dir'), 'toolbox', 'Anatomy');
16+
17+
end
18+
19+
end
20+
21+
atlasDir = spm_file(atlasDir, 'cpath');
22+
23+
end

src/atlas/unzipAtlas.m

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
% (C) Copyright 2021 CPP ROI developers
22

3-
function unzipAtlas()
3+
function unzipAtlas(atlas)
44

5-
atlasDir = fullfile(fileparts(mfilename('fullpath')), '..', '..', 'atlas');
5+
atlasDir = returnAtlasDir();
66

7-
file = fullfile(atlasDir, 'visual_topography_probability_atlas.zip');
7+
if strcmp(atlas, 'wang')
88

9-
unzip(file, fileparts(file));
9+
file = fullfile(atlasDir, 'visual_topography_probability_atlas.zip');
1010

11-
labelImages = spm_select('FPList', ...
12-
fullfile(atlasDir, ...
13-
'visual_topography_probability_atlas', ...
14-
'subj_vol_all'), ...
15-
'^maxprob_vol_.*h.nii.gz$');
11+
if ~exist(fullfile(atlasDir, 'visual_topography_probability_atlas'), 'dir')
1612

17-
gunzip(cellstr(labelImages));
13+
unzip(file, fileparts(file));
14+
15+
labelImages = spm_select('FPList', ...
16+
fullfile(atlasDir, ...
17+
'visual_topography_probability_atlas', ...
18+
'subj_vol_all'), ...
19+
'^.*_dseg.nii.gz$');
20+
21+
gunzip(cellstr(labelImages));
22+
23+
end
24+
25+
end
1826

1927
end

src/roi/createRoi.m

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,12 @@
158158
roiImage = specification.mask1;
159159
sphere = specification.mask2;
160160
end
161-
161+
162162
% check that input image has at least enough voxels to include
163163
maskVol = spm_read_vols(spm_vol(roiImage));
164164
totalNbVoxels = sum(maskVol(:));
165165
if sphere.maxNbVoxels > totalNbVoxels
166-
error('Number of voxels requested greater than the total number of voxels in this mask');
166+
error('Number of voxels requested greater than the total number of voxels in this mask');
167167
end
168168

169169
specification = struct( ...
@@ -174,33 +174,33 @@
174174
hdr = spm_vol(roiImage);
175175
dim = diag(hdr.mat);
176176
radiusStep = min(abs(dim(1:3)));
177-
177+
178178
% determine maximum radius to expand to
179179
maxRadius = hdr.dim .* dim(1:3)';
180180
maxRadius = max(abs(maxRadius));
181181

182-
fprintf(1, '\n Expansion:')
183-
182+
fprintf(1, '\n Expansion:');
183+
184184
while true
185185
mask = createRoi('intersection', specification);
186186
mask.roi.radius = specification.mask2.radius;
187-
187+
188188
fprintf(1, '\n radius: %0.2f mm; roi size: %i voxels', ...
189-
mask.roi.radius, ...
190-
mask.roi.size)
191-
189+
mask.roi.radius, ...
190+
mask.roi.size);
191+
192192
if mask.roi.size > sphere.maxNbVoxels
193193
break
194194
end
195-
195+
196196
if mask.roi.radius > maxRadius
197-
error('sphere expanded beyond the dimension of the mask.')
197+
error('sphere expanded beyond the dimension of the mask.');
198198
end
199-
199+
200200
specification.mask2.radius = specification.mask2.radius + radiusStep;
201201
end
202-
203-
fprintf(1, '\n')
202+
203+
fprintf(1, '\n');
204204

205205
mask.xyz = sphere.location;
206206

@@ -310,12 +310,12 @@
310310
fullfile(outputDir, tempFile));
311311

312312
outputFile = fullfile(outputDir, roiName);
313-
313+
314314
mars_rois2img(fullfile(outputDir, tempFile), ...
315315
outputFile, ...
316316
spm_vol(volumeDefiningImage));
317317
delete(fullfile(outputDir, tempFile));
318-
318+
319319
% delete label files
320320
delete(fullfile(outputDir, '*_mask_labels.mat'));
321321

tests/test_unit_convertToValidCamelCase.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
% (C) Copyright 2021 CPP BIDS SPM-pipeline developers
1+
% (C) Copyright 2021 CPP ROI developers
22

33
function test_suite = test_unit_convertToValidCamelCase %#ok<*STOUT>
44
try % assignment of 'localfunctions' is necessary in Matlab >= 2016

0 commit comments

Comments
 (0)