-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcvnreadsurfacemetric.m
141 lines (127 loc) · 3.88 KB
/
cvnreadsurfacemetric.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
function [result,metricfiles] = cvnreadsurfacemetric(subject, hemi, metricname, surftype, surfsuffix, varargin)
% result = cvnreadsurfacemetric(subject, hemi, metricname, surftype, surfsuffix, 'param',value,...)
%
% Read a surface metric file (eg: curv, sulc, sapv) for given hemispheres,
% surface type, etc...
%
% Inputs:
% subject: Freesurfer subject
% hemi: 'lh','rh', or {'lh','rh'} (default={'lh','rh'})
% metricname 'curv', 'sulc', 'sapv', ....
% surftype: '',sphere|inflated|white|pial|layerA1 ...
% surfsuffix: DENSE|DENSETRUNCpt|orig ("orig"=<hemi>.sphere)
%
% Outputs:
% result: Vx1 metric values or struct('data',<L+R>x1,'numlh',L,'numrh',R);
%
% Optional inputs: 'paramname','value',...
% surfdir: optional directory to search (default =
% <freesurfer>/<subj>/surf)
%
% Examples:
% >> curv = cvnreadsurfacemetric('C0051', {'lh','rh'}, 'curv', '', 'DENSETRUNCpt')
% curv =
% data: [867225x1 double]
% numlh: 399735
% numrh: 467490
%
% >> sapv1 = cvnreadsurfacemetric('C0051', {'lh','rh'}, 'sapv', 'layerA1', 'DENSETRUNCpt')
% sapv1 =
% data: [867225x1 double]
% numlh: 399735
% numrh: 467490
%%%%%%%%%%%%%%%%%%%%
%default options
options=struct(...
'surfdir',[],...
'verbose',false);
%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%
%parse options
input_opts=mergestruct(varargin{:});
fn=fieldnames(input_opts);
for f = 1:numel(fn)
opt=input_opts.(fn{f});
if(~(isnumeric(opt) && isempty(opt)))
options.(fn{f})=input_opts.(fn{f});
end
end
%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
surfdir=[];
if(~isempty(options.surfdir) && exist(options.surfdir,'dir'))
surfdir=options.surfdir;
else
freesurfdir=cvnpath('freesurfer');
surfdir=sprintf('%s/%s/surf',freesurfdir,subject);
end
assert(exist(surfdir,'dir')>0,'Missing surf directory: %s',surfdir);
if(isempty(hemi))
hemi={'lh','rh'};
elseif(ischar(hemi))
hemi={hemi};
end
hemi=lower(hemi);
if(isequal(hemi,{'rh','lh'}))
hemi={'lh','rh'};
end
if(strcmpi(surfsuffix,'orig'))
suffix_file='';
else
suffix_file=surfsuffix;
end
surftype_file='';
if(~isempty(surftype))
surftype_file=sprintf('_%s_',surftype);
end
iscurvfile=zeros(numel(hemi),1);
metricpattern_all={sprintf('%s/%%s.%s%s%s',surfdir,metricname,surftype_file,suffix_file)
sprintf('%s/%%s.%s%s%s.mgz',surfdir,metricname,surftype_file,suffix_file)
sprintf('%s/%%s%s%s.%s',surfdir,surftype_file,suffix_file,metricname)};
metricpattern=[];
for m = 1:numel(metricpattern_all)
iscurvfile(:)=false;
for h = 1:numel(hemi)
surffile=sprintf(metricpattern_all{m},hemi{h});
iscurvfile(h)=exist(surffile,'file')>0;
end
if(all(iscurvfile))
metricpattern=metricpattern_all{m};
break;
end
end
result=[];
metricfiles={};
if(isempty(metricpattern))
if(~isempty(surftype))
%if no file was found, try with blank surftype (many such as curv,
% sulc, don't have a specific surftype)
[result,metricfiles] = cvnreadsurfacemetric(subject, hemi, metricname, '', surfsuffix, varargin{:},'verbose',false);
return;
end
if(options.verbose)
wstr=repmat('\n%s',1,numel(metricpattern_all));
warning(['No metric file found: ' wstr],metricpattern_all{:});
end
return;
else
hvert={};
hresult={};
for h = 1:numel(hemi)
metricfile=sprintf(metricpattern,hemi{h});
[~,~,ext]=fileparts(metricfile);
if(strcmpi(ext,'.mgz'))
val=load_mgh(metricfile);
else
[val,~]=read_curv(metricfile);
end
hvert{h}=numel(val);
hresult{h}=val(:);
metricfiles{h}=metricfile;
end
if(isequal(hemi,{'lh','rh'}))
result=struct('data',cat(1,hresult{:}),'numlh',hvert{1},'numrh',hvert{2});
else
result=cat(1,hresult{:});
end
end