-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbrainLoad.m
123 lines (117 loc) · 4.47 KB
/
brainLoad.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
function [fileNames,X,bid,initSz] = brainLoad(folder,mask)
% This function loads in all of the .nii or .nii.gz files within a folder,
% constraining the portion of the images being extracted to a mask if
% supplied. The way files are loaded is by first trying to use matlab's
% image processing toolbox (niftiread.m). If that fails, then we try to
% load using the freesurfer load_nifti.m function. If that fails, then we
% try to load using SPM. Make sure one of these sets of files is in your
% matlab path!
%
% Inputs:
% folder : leave empty for UI folder selection, otherwise it is a string
% path to your folder containing .nii or .nii.gz files
%
% mask: same as above for folder. Note, if you pass this in as empty, you
% will trigger the file selection UI. Just hit cancel if you do not intend
% to use a mask.
%
% Outputs:
% fileNames: the name of the file, which forms every col of the output X
% X: each row is a voxel, each col is a file (i.e., subject)
% bid: voxel identities from the whole brain image that we used as a mask
% (short for brain IDs)
% initSz: the initial size of the brain data before it is vectorized
%
% Example calls:
% [fileNames,X,bid,initSz] = brainLoad([],[])
% [fileNames,X,bid,initSz] = brainLoad(['C:\Users\alext\toAnalyze'],['C:\Users\alext\Downloads\MNI152_2mm.nii.gz'])
%
% % Alex Teghipco // [email protected]
% permitted extensions
exta = {'*.nii.gz','*.nii'};
% define file separator based on system
if ispc
s = '\';
elseif isunix || ismac
s = '/';
else
error('Cannot identify your system...pc or mac or unix?')
end
% ask user where to pull nifti files from and mask
if isempty(folder)
disp('Please select a folder that contains the nifti files you want to import. It must not have any other nifti files!')
folder = uigetdir([pwd],'Please select your folder with .nii or .nii.gz files');
if folder == 0
error('You must select a folder! Looks like you hit cancel')
end
end
if isempty(mask)
disp('Please select your brain mask nifti file. All nonzero values in the mask will be treated as areas of the brain you want to keep.')
[mask1,mask2] = uigetfile(exta,'Please select your brain mask. All nonzero values in the mask will be treated as areas of the brain you want to keep.',pwd);
mask = [mask2 mask1];
end
% load in your mask
try
%disp('Trying to load using matlabs niftiread.m')
tmpM = niftiread(mask);
bid = find(tmpM ~= 0);
catch
try
%disp('Did not work! Trying to load using freesurfers load_nifti.m')
tmp = load_nifti(mask);
tmpM = tmp.vol;
bid = find(tmpM ~= 0);
catch
try
%disp('Did not work! Trying to load using spm load_nifti.m')
tmp1 = spm_vol(mask);
tmpM = spm_read_vols(tmp1);
bid = find(tmpM ~= 0);
catch
bid = [];
disp(['No loadable mask supplied Proceeding without the mask...if this is not expected behavior please make sure spm_read_vols or load_nifti or niftiread is in your path' ])
end
end
end
% combine files of both kinds of extensions: nii or nii.gz
files = [];
for i = 1:length(exta)
files = [files; dir(fullfile([folder], [exta{i}]))];
end
% now loop through and load
for i = 1:length(files)
disp(['Working on file ' num2str(i) ' of ' num2str(length(files))]);
disp(['File is: ' files(i).name])
try
%disp('Trying to load using matlabs niftiread.m')
tmp = niftiread([files(i).folder s files(i).name]);
initSz = size(tmp);
catch
try
%disp('Did not work! Trying to load using freesurfers load_nifti.m')
tmp = load_nifti([files(i).folder s files(i).name]);
tmp = tmp.vol;
initSz = size(tmp);
catch
try
%disp('Did not work! Trying to load using spm load_nifti.m')
tmp1 = spm_vol([files(i).folder s files(i).name]);
tmp = spm_read_vols(tmp1);
initSz = size(tmp);
catch
disp(['Could not load in file! ' files(i).folder s files(i).name])
error('Double check that nifti is unzipped if necessary based on the software you are trying to use')
end
end
end
if ~isempty(bid)
try
X(:,i) = tmp(bid);
catch
error('Check your mask, it seems like it might be a different shape than your files that you are trying to load in.')
end
else
X(:,i) = tmp(:);
end
end
fileNames = {files.name};