-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrest_ROIList_gui.m
executable file
·383 lines (333 loc) · 14.9 KB
/
rest_ROIList_gui.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
function varargout = rest_ROIList_gui(varargin)
%List ROI definition list and edit for REST's functional connectivity by Xiao-Wei Song
%------------------------------------------------------------------------------------------------------------------------------
% Copyright(c) 2007~2010
% State Key Laboratory of Cognitive Neuroscience and Learning in Beijing Normal University
% Written by Xiao-Wei Song
% http://resting-fmri.sourceforge.net
%------------------------------------------------------------------------------------------------------------------------------
% Begin initialization code - DO NOT EDIT
% Mail to Authors: <a href="[email protected]">Xiaowei Song</a>; <a href="[email protected]">Chaogan Yan</a>
% Revised by YAN Chao-Gan 080610: NIFTI compatible
% Revised by Yan Chao-Gan 080808: also support NIFTI images.
% Last Modified by DONG Zhang-Ye and Yan Chao-Gan 110504: Add "Add multiple user-defined mask files" function.
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @rest_ROIList_gui_OpeningFcn, ...
'gui_OutputFcn', @rest_ROIList_gui_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before rest_ROIList_gui is made visible.
function rest_ROIList_gui_OpeningFcn(hObject, eventdata, handles, varargin)
InitControls(hObject, handles);
set(handles.listROI, 'String', '');
if ~isempty(varargin),
%Calling convention: varargin{1} was discarded
% x=roi_list('ListROI', './setRoi/listTest.rest_roi')
% handles.ROISetFile =varargin{2};
% handles.ROISetList =ReadROIDefineFile(handles);
handles.ROISetList =varargin{1};
if ~isempty(handles.ROISetList) && ( isempty(handles.ROISetList{1}) || all(isspace(handles.ROISetList{1})) ),
handles.ROISetList(1) =[];
end
else
error('There must be 2 parameters at least!');
end
% Make Display correct in linux - YAN Chao-Gan 111025 Added.
if ~ispc
ZoomFactor=0.85;
ObjectNames = fieldnames(handles);
for i=1:length(ObjectNames);
eval(['IsFontSizeProp=isprop(handles.',ObjectNames{i},',''FontSize'');']);
if IsFontSizeProp
eval(['PCFontSize=get(handles.',ObjectNames{i},',''FontSize'');']);
FontSize=PCFontSize*ZoomFactor;
eval(['set(handles.',ObjectNames{i},',''FontSize'',',num2str(FontSize),');']);
end
end
end
% Update handles structure
guidata(hObject, handles);
UpdateDisplay(handles);
% UIWAIT makes rest_ROIList_gui wait for user response (see UIRESUME)
try
uiwait(handles.figListROI);
catch
uiresume(handles.figListROI);
end
% --- Outputs from this function are returned to the command line.
function varargout = rest_ROIList_gui_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
% uiwait(handles.figListROI);
% varargout{1} = handles.ROISetFile; %handles.output;
varargout{1} = handles.ROISetList;
delete(handles.figListROI);
function listROI_Callback(hObject, eventdata, handles)
function listROI_KeyPressFcn(hObject, eventdata, handles)
key =get(handles.figListROI, 'currentkey');
if seqmatch({key},{'delete', 'backspace'})
theIndex =get(hObject, 'Value');
if theIndex> size(handles.ROISetList, 1), return; end
theDef =handles.ROISetList{theIndex};
tmpMsg=sprintf('Delete\n\n "%s"?', theDef);
if strcmp(questdlg(tmpMsg, 'Delete confirmation'), 'Yes')
if theIndex>1,
set(hObject, 'Value', theIndex-1);
end
handles.ROISetList(theIndex, :)=[];
if size(handles.ROISetList, 1)==0
handles.ROISetList={};
end
guidata(hObject, handles);
UpdateDisplay(handles);
end
end
function listROI_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in btnDelete.
function btnDelete_Callback(hObject, eventdata, handles)
% hObject handle to btnDelete (see GCBO) % handles structure with handles and user data (see GUIDATA)
theIndex =get(handles.listROI, 'Value');
if theIndex> size(handles.ROISetList, 1), return; end
theDef =handles.ROISetList{theIndex};
tmpMsg=sprintf('Delete\n\n "%s"?', theDef);
if strcmp(questdlg(tmpMsg, 'Delete confirmation'), 'Yes')
if theIndex>1,
set(handles.listROI, 'Value', theIndex-1);
guidata(hObject, handles);
end
handles.ROISetList(theIndex, :)=[];
if size(handles.ROISetList, 1)==0
handles.ROISetList={};
end
guidata(hObject, handles);
UpdateDisplay(handles);
end
% --- Executes on button press in btnAdd.
function btnAdd_Callback(hObject, eventdata, handles)
% hObject handle to btnAdd (see GCBO)
theROIDefinition=rest_SetROI;
if ~isempty(theROIDefinition),
handles.ROISetList =[handles.ROISetList; {theROIDefinition}];
set(handles.listROI, 'Value',size(handles.ROISetList, 1));
guidata(hObject, handles);
UpdateDisplay(handles);
end
% --- Executes on button press in btnView.
function btnView_Callback(hObject, eventdata, handles)
theIndex =get(handles.listROI, 'Value');
AROIDef =handles.ROISetList{theIndex};
rest_misc( 'ViewROI', AROIDef);
% --- Executes on button press in btnDone.
function btnDone_Callback(hObject, eventdata, handles)
% hObject handle to btnDone (see GCBO)
%Remove the empty data at the first position from the list
try
if isempty(handles.ROISetList{1}) || all(isspace(handles.ROISetList{1})),
handles.ROISetList(1) =[];
guidata(hObject, handles);
end
catch
end
uiresume(handles.figListROI);
% delete(handles.figListROI);
function Result =ReadROIDefineFile(handles)
%Check first
[pathstr, name, ext] = fileparts(mfilename('fullpath'));
theROIDir =fullfile(pathstr,'SetROI');
% Result =handles.ROISetFile; %fullfile(theROIDir, AROIDefineFile);
Result ={};
if 7==exist(theROIDir,'dir') && 2==exist(handles.ROISetFile, 'file'),
%Read the list from ROI definition file
% Result =textread(handles.ROISetFile,'%s\n', 'whitespace', '');
h =fopen(handles.ROISetFile, 'r');
while 1,
tline = fgetl(h);
if ~ischar(tline), break; end
Result =[Result; {tline}];
end
fclose(h);
else%The definition dir and file doesn't exist
if 7~=exist(theROIDir,'dir'),
%I initialize the list file and the dir
mkdir(theROIDir);
end
h=fopen(handles.ROISetFile, 'w');
fclose(h);
return;
end
function UpdateDisplay(handles)
set(handles.figListROI, 'Name', 'ROI Definition List');
set(handles.listROI, 'String', handles.ROISetList);
function InitControls(hObject, handles)
set(handles.pmnuMisc, 'String', {
'Misc',
'==========================================================',
'Add multiple user-defined mask files', %Multiple Files 20110330 DONG,update some words 20110504,DONG and Yan Chao-Gan
'Retrieve averaged time course from Selected ROI definition',
'**********************************************************' ,
'Export List of ROI definitions',
'Import List of ROI definitions',
'Clear Current List',
'**********************************************************'}, ...
'Position',[6 43 410 21],...
'Value',1);
%Linux compatible
set(handles.listROI,'Position',[5 74 411 208]);
set(handles.btnDelete,'Position',[269 10 69 24]);
set(handles.btnAdd, 'Position',[348 10 69 24]);
set(handles.btnView,'Position',[85 10 69 24],'String','View ROI');
set(handles.btnDone,'Position',[6 10 69 24],'String','Done');
guidata(hObject, handles)
% --- Executes on selection change in pmnuMisc.
function pmnuMisc_Callback(hObject, eventdata, handles)
% hObject handle to pmnuMisc (see GCBO)
switch get(handles.pmnuMisc, 'Value'),
case {1, 2, 5, 9}, %Misc or '---'
%Do nothing
case 3 %Multiple Files 20110330 DONG
[filename, pathname] = uigetfile({'*.img;*.nii;*.nii.gz','Brain Image Files (*.img;*.nii;*.nii.gz)';'*.*', 'All Files (*.*)';},'Pick the statistical map','MultiSelect','on');
theLines={};
if ~isempty(filename)
if ~iscell(filename)
theLines ={[pathname,filename]};
else
for i=1:size(filename,2)
theLines =[ theLines;{[pathname,filename{1,i}]}];
end
end
guidata(hObject,handles);
end
handles.ROISetList =[handles.ROISetList; theLines];
%Save before setting selected to the first of imported lines
guidata(hObject, handles);
set(handles.pmnuMisc, 'Value', size(handles.ROISetList,1) -size(theLines,1) +1);%Multiple Files 20110330 DONG
case 4, %'Retrieve averaged time course from selected ROI definition'
if prod(size(handles.ROISetList))>0 && size(handles.ROISetList, 1)>0,
theIndex =get(handles.listROI, 'Value');
AROIDef =handles.ROISetList{theIndex};
RetrieveTimeCourseFromROIDefinition(AROIDef);
end
case 6, %'Export List of ROI definitions'
if prod(size(handles.ROISetList))>0 && size(handles.ROISetList, 1)>0,
[filename, pathname] = uiputfile('*.txt','Save current ROI definition''s list to a text file: ');
if isequal(filename,0) | isequal(pathname,0),
else
theFilename =fullfile(pathname,filename);
rest_misc( 'ExportCells2Txt', handles.ROISetList, theFilename);
end
end
case 7, %'Import List of ROI definitions'
[filename, pathname] = uigetfile('*.txt','Load current ROI definition''s list from a text file: ');
if isequal(filename,0) | isequal(pathname,0),
else
theFilename =fullfile(pathname,filename);
theLines=rest_misc( 'ImportLinesFromTxt', theFilename);
handles.ROISetList =[handles.ROISetList; theLines];
%Save before setting selected to the first of imported lines
guidata(hObject, handles);
set(handles.pmnuMisc, 'Value', size(handles.ROISetList,1) -size(theLines,1) +1);
end
case 8, %'Clear Current List'
if prod(size(handles.ROISetList))>0 && size(handles.ROISetList, 1)>0,
tmpMsg=sprintf('Clear All %d ROI Definitions in the list?', size(handles.ROISetList, 1));
if strcmp(questdlg(tmpMsg, 'Clear confirmation'), 'Yes')
handles.ROISetList={};
end
end
otherwise
end
set(handles.pmnuMisc, 'Value', 1);
guidata(hObject, handles);
UpdateDisplay(handles);
% --- Executes during object creation, after setting all properties.
function pmnuMisc_CreateFcn(hObject, eventdata, handles)
% hObject handle to pmnuMisc (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function RetrieveTimeCourseFromROIDefinition(AROIDef)
try
theROIFilename ='ROITimeCourse_%s.txt';
if rest_SphereROI( 'IsBallDefinition', AROIDef),
%The ROI definition is a Ball definition
[filename, pathname] = uigetfile({'*.img;*.nii;*.nii.gz','Brain Image Files (*.img;*.nii;*.nii.gz)';'*.*', 'All Files (*.*)';}, ...
'Pick one functional brain map(EPI)');
if any(filename~=0) && ischar(filename) && length(filename)>4 , % not canceled and legal
%Get the 4D brain
[the4DBrain, VoxelSize, ImgFileList, Header,nVolumn] =rest_to4d(pathname);
%Generate the mask
BrainSize =size(the4DBrain);
BrainSize =BrainSize(1:3);
[AROICenter, AROIRadius] =rest_SphereROI('STR2ROIBALL', AROIDef);
maskROI =rest_SphereROI( 'BallDefinition2Mask' , AROIDef, BrainSize, VoxelSize, Header);
theROIFilename =sprintf(theROIFilename, sprintf('%g_%g_%g_%g', AROICenter(1),AROICenter(2),AROICenter(3), AROIRadius));
end
elseif exist(AROIDef,'file')==2, % Make sure the Definition file exist
[pathstr, name, ext] = fileparts(AROIDef);
if strcmpi(ext, '.txt'),
warndlg(sprintf('%s\n\n is already a ROI time course definition!', AROIDef));
return;
elseif strcmpi(ext, '.img'),
[filename, pathname] = uigetfile({'*.img;*.nii;*.nii.gz','Brain Image Files (*.img;*.nii;*.nii.gz)';'*.*', 'All Files (*.*)';}, ...
'Pick one functional brain map(EPI)');
if any(filename~=0) && ischar(filename) && length(filename)>4 , % not canceled and legal
%Get the 4D brain
[the4DBrain, VoxelSize, ImgFileList, Header,nVolumn] =rest_to4d(pathname);
[nDim1, nDim2, nDim3, nDim4]=size(the4DBrain);
%The ROI definition is a mask file
maskROI =rest_loadmask(nDim1, nDim2, nDim3, AROIDef);
theROIFilename =sprintf(theROIFilename, name);
end
else
error(sprintf('REST doesn''t support the selected ROI definition now, Please check: \n%s', AROIDef));
end
else
error(sprintf('Wrong ROI definition, Please check: \n%s', AROIDef));
end
%Retrieve the TimeCourse
maskROI = (0~=maskROI);
theROITimeCourse =zeros(size(the4DBrain, 4), 1);
for t=1:size(the4DBrain, 4),
theTimePoint = squeeze(the4DBrain(:,:,:, t));
theTimePoint = theTimePoint(maskROI);
if ~isempty(theTimePoint),
theROITimeCourse(t) =mean(theTimePoint);
end
end %The Averaged Time Course within the ROI now comes out! 20070903
rest_waitbar;
%Save ROI time course to a txt file
[theROIFilename, pathname] = uiputfile(theROIFilename,'Save current ROI averaged time course to a text file: ');
if isequal(theROIFilename,0) | isequal(pathname,0),
else
theFilename =fullfile(pathname,theROIFilename);
save(theFilename, 'theROITimeCourse', '-ASCII', '-DOUBLE','-TABS')
end
catch
rest_misc( 'DisplayLastException');
end
% --- Executes when user attempts to close figListROI.
function figListROI_CloseRequestFcn(hObject, eventdata, handles)
btnDone_Callback(hObject, eventdata, handles)
% Hint: delete(hObject) closes the figure
%delete(hObject);