0001 function varargout = rest_ROIList_gui(varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 gui_Singleton = 1;
0014 gui_State = struct('gui_Name', mfilename, ...
0015 'gui_Singleton', gui_Singleton, ...
0016 'gui_OpeningFcn', @rest_ROIList_gui_OpeningFcn, ...
0017 'gui_OutputFcn', @rest_ROIList_gui_OutputFcn, ...
0018 'gui_LayoutFcn', [] , ...
0019 'gui_Callback', []);
0020 if nargin && ischar(varargin{1})
0021 gui_State.gui_Callback = str2func(varargin{1});
0022 end
0023
0024 if nargout
0025 [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
0026 else
0027 gui_mainfcn(gui_State, varargin{:});
0028 end
0029
0030
0031
0032
0033 function rest_ROIList_gui_OpeningFcn(hObject, eventdata, handles, varargin)
0034 InitControls(hObject, handles);
0035
0036 set(handles.listROI, 'String', '');
0037 if ~isempty(varargin),
0038
0039
0040
0041
0042 handles.ROISetList =varargin{1};
0043 if ~isempty(handles.ROISetList) && ( isempty(handles.ROISetList{1}) || all(isspace(handles.ROISetList{1})) ),
0044 handles.ROISetList(1) =[];
0045 end
0046 else
0047 error('There must be 2 parameters at least!');
0048 end
0049
0050
0051
0052 guidata(hObject, handles);
0053 UpdateDisplay(handles);
0054
0055 try
0056 uiwait(handles.figListROI);
0057 catch
0058 uiresume(handles.figListROI);
0059 end
0060
0061
0062
0063 function varargout = rest_ROIList_gui_OutputFcn(hObject, eventdata, handles)
0064
0065
0066
0067
0068
0069
0070
0071
0072 varargout{1} = handles.ROISetList;
0073 delete(handles.figListROI);
0074
0075
0076 function listROI_Callback(hObject, eventdata, handles)
0077
0078 function listROI_KeyPressFcn(hObject, eventdata, handles)
0079 key =get(handles.figListROI, 'currentkey');
0080 if seqmatch({key},{'delete', 'backspace'})
0081 theIndex =get(hObject, 'Value');
0082 if theIndex> size(handles.ROISetList, 1), return; end
0083 theDef =handles.ROISetList{theIndex};
0084 tmpMsg=sprintf('Delete\n\n "%s"?', theDef);
0085 if strcmp(questdlg(tmpMsg, 'Delete confirmation'), 'Yes')
0086 if theIndex>1,
0087 set(hObject, 'Value', theIndex-1);
0088 end
0089 handles.ROISetList(theIndex, :)=[];
0090 if size(handles.ROISetList, 1)==0
0091 handles.ROISetList={};
0092 end
0093 guidata(hObject, handles);
0094 UpdateDisplay(handles);
0095 end
0096 end
0097
0098 function listROI_CreateFcn(hObject, eventdata, handles)
0099 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
0100 set(hObject,'BackgroundColor','white');
0101 end
0102
0103
0104
0105 function btnDelete_Callback(hObject, eventdata, handles)
0106
0107 theIndex =get(handles.listROI, 'Value');
0108 if theIndex> size(handles.ROISetList, 1), return; end
0109 theDef =handles.ROISetList{theIndex};
0110 tmpMsg=sprintf('Delete\n\n "%s"?', theDef);
0111 if strcmp(questdlg(tmpMsg, 'Delete confirmation'), 'Yes')
0112 if theIndex>1,
0113 set(handles.listROI, 'Value', theIndex-1);
0114 guidata(hObject, handles);
0115 end
0116 handles.ROISetList(theIndex, :)=[];
0117 if size(handles.ROISetList, 1)==0
0118 handles.ROISetList={};
0119 end
0120 guidata(hObject, handles);
0121 UpdateDisplay(handles);
0122 end
0123
0124
0125
0126 function btnAdd_Callback(hObject, eventdata, handles)
0127
0128 theROIDefinition=rest_SetROI;
0129 if ~isempty(theROIDefinition),
0130 handles.ROISetList =[handles.ROISetList; {theROIDefinition}];
0131 set(handles.listROI, 'Value',size(handles.ROISetList, 1));
0132 guidata(hObject, handles);
0133 UpdateDisplay(handles);
0134 end
0135
0136
0137 function btnView_Callback(hObject, eventdata, handles)
0138 theIndex =get(handles.listROI, 'Value');
0139 AROIDef =handles.ROISetList{theIndex};
0140 rest_misc( 'ViewROI', AROIDef);
0141
0142
0143 function btnDone_Callback(hObject, eventdata, handles)
0144
0145
0146 try
0147 if isempty(handles.ROISetList{1}) || all(isspace(handles.ROISetList{1})),
0148 handles.ROISetList(1) =[];
0149 guidata(hObject, handles);
0150 end
0151 catch
0152 end
0153 uiresume(handles.figListROI);
0154
0155
0156
0157 function Result =ReadROIDefineFile(handles)
0158
0159 [pathstr, name, ext, versn] = fileparts(mfilename('fullpath'));
0160 theROIDir =fullfile(pathstr,'SetROI');
0161
0162 Result ={};
0163 if 7==exist(theROIDir,'dir') && 2==exist(handles.ROISetFile, 'file'),
0164
0165
0166 h =fopen(handles.ROISetFile, 'r');
0167 while 1,
0168 tline = fgetl(h);
0169 if ~ischar(tline), break; end
0170 Result =[Result; {tline}];
0171 end
0172 fclose(h);
0173 else
0174 if 7~=exist(theROIDir,'dir'),
0175
0176 mkdir(theROIDir);
0177 end
0178 h=fopen(handles.ROISetFile, 'w');
0179 fclose(h);
0180 return;
0181 end
0182 function UpdateDisplay(handles)
0183 set(handles.figListROI, 'Name', 'ROI Definition List');
0184 set(handles.listROI, 'String', handles.ROISetList);
0185
0186
0187
0188
0189 function InitControls(hObject, handles)
0190 set(handles.pmnuMisc, 'String', {
0191 'Misc',
0192 '==========================================================',
0193 'Retrieve averaged time course from Selected ROI definition',
0194 '**********************************************************' ,
0195 'Export List of ROI definitions',
0196 'Import List of ROI definitions',
0197 'Clear Current List',
0198 '**********************************************************'}, ...
0199 'Position',[6 43 410 21],...
0200 'Value',1);
0201
0202
0203 set(handles.listROI,'Position',[5 74 411 208]);
0204 set(handles.btnDelete,'Position',[269 10 69 24]);
0205 set(handles.btnAdd, 'Position',[348 10 69 24]);
0206 set(handles.btnView,'Position',[85 10 69 24],'String','View ROI');
0207 set(handles.btnDone,'Position',[6 10 69 24],'String','Done');
0208
0209 guidata(hObject, handles)
0210
0211
0212 function pmnuMisc_Callback(hObject, eventdata, handles)
0213
0214
0215 switch get(handles.pmnuMisc, 'Value'),
0216 case {1, 2, 4, 8},
0217
0218 case 3,
0219 if prod(size(handles.ROISetList))>0 && size(handles.ROISetList, 1)>0,
0220 theIndex =get(handles.listROI, 'Value');
0221 AROIDef =handles.ROISetList{theIndex};
0222 RetrieveTimeCourseFromROIDefinition(AROIDef);
0223 end
0224 case 5,
0225 if prod(size(handles.ROISetList))>0 && size(handles.ROISetList, 1)>0,
0226 [filename, pathname] = uiputfile('*.txt','Save current ROI definition''s list to a text file: ');
0227 if isequal(filename,0) | isequal(pathname,0),
0228 else
0229 theFilename =fullfile(pathname,filename);
0230 rest_misc( 'ExportCells2Txt', handles.ROISetList, theFilename);
0231 end
0232 end
0233
0234 case 6,
0235 [filename, pathname] = uigetfile('*.txt','Load current ROI definition''s list from a text file: ');
0236 if isequal(filename,0) | isequal(pathname,0),
0237 else
0238 theFilename =fullfile(pathname,filename);
0239 theLines=rest_misc( 'ImportLinesFromTxt', theFilename);
0240 handles.ROISetList =[handles.ROISetList; theLines];
0241
0242 guidata(hObject, handles);
0243 set(handles.pmnuMisc, 'Value', size(handles.ROISetList,1) -size(theLines,1) +1);
0244 end
0245
0246 case 7,
0247 if prod(size(handles.ROISetList))>0 && size(handles.ROISetList, 1)>0,
0248 tmpMsg=sprintf('Clear All %d ROI Definitions in the list?', size(handles.ROISetList, 1));
0249 if strcmp(questdlg(tmpMsg, 'Clear confirmation'), 'Yes')
0250 handles.ROISetList={};
0251 end
0252 end
0253
0254 otherwise
0255 end
0256
0257 set(handles.pmnuMisc, 'Value', 1);
0258 guidata(hObject, handles);
0259 UpdateDisplay(handles);
0260
0261
0262 function pmnuMisc_CreateFcn(hObject, eventdata, handles)
0263
0264
0265
0266
0267
0268
0269 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
0270 set(hObject,'BackgroundColor','white');
0271 end
0272
0273
0274 function RetrieveTimeCourseFromROIDefinition(AROIDef)
0275 try
0276 theROIFilename ='ROITimeCourse_%s.txt';
0277 if rest_SphereROI( 'IsBallDefinition', AROIDef),
0278
0279 [filename, pathname] = uigetfile({'*.img', 'ANALYZE or NIFTI files (*.img)'}, ...
0280 'Pick one functional brain map(EPI)');
0281 if any(filename~=0) && ischar(filename) && length(filename)>4 ,
0282
0283 [the4DBrain, VoxelSize, ImgFileList, Header] =rest_to4d(pathname);
0284
0285 BrainSize =size(the4DBrain);
0286 BrainSize =BrainSize(1:3);
0287
0288 [AROICenter, AROIRadius] =rest_SphereROI('STR2ROIBALL', AROIDef);
0289 maskROI =rest_SphereROI( 'BallDefinition2Mask' , AROIDef, BrainSize, VoxelSize, Header);
0290 theROIFilename =sprintf(theROIFilename, sprintf('%g_%g_%g_%g', AROICenter(1),AROICenter(2),AROICenter(3), AROIRadius));
0291 end
0292
0293 elseif exist(AROIDef,'file')==2,
0294 [pathstr, name, ext, versn] = fileparts(AROIDef);
0295 if strcmpi(ext, '.txt'),
0296 warndlg(sprintf('%s\n\n is already a ROI time course definition!', AROIDef));
0297 return;
0298 elseif strcmpi(ext, '.img'),
0299 [filename, pathname] = uigetfile({'*.img', 'ANALYZE or NIFTI files (*.img)'}, ...
0300 'Pick one functional brain map(EPI)');
0301 if any(filename~=0) && ischar(filename) && length(filename)>4 ,
0302
0303 [the4DBrain, VoxelSize, ImgFileList, Header] =rest_to4d(pathname);
0304 [nDim1, nDim2, nDim3, nDim4]=size(the4DBrain);
0305
0306 maskROI =rest_loadmask(nDim1, nDim2, nDim3, AROIDef);
0307 theROIFilename =sprintf(theROIFilename, name);
0308 end
0309
0310 else
0311 error(sprintf('REST doesn''t support the selected ROI definition now, Please check: \n%s', AROIDef));
0312 end
0313 else
0314 error(sprintf('Wrong ROI definition, Please check: \n%s', AROIDef));
0315 end
0316
0317
0318 maskROI = (0~=maskROI);
0319 theROITimeCourse =zeros(size(the4DBrain, 4), 1);
0320 for t=1:size(the4DBrain, 4),
0321 theTimePoint = squeeze(the4DBrain(:,:,:, t));
0322 theTimePoint = theTimePoint(maskROI);
0323 if ~isempty(theTimePoint),
0324 theROITimeCourse(t) =mean(theTimePoint);
0325 end
0326 end
0327 rest_waitbar;
0328
0329
0330 [theROIFilename, pathname] = uiputfile(theROIFilename,'Save current ROI averaged time course to a text file: ');
0331 if isequal(theROIFilename,0) | isequal(pathname,0),
0332 else
0333 theFilename =fullfile(pathname,theROIFilename);
0334 save(theFilename, 'theROITimeCourse', '-ASCII', '-DOUBLE','-TABS')
0335 end
0336 catch
0337 rest_misc( 'DisplayLastException');
0338 end
0339
0340
0341
0342
0343 function figListROI_CloseRequestFcn(hObject, eventdata, handles)
0344 btnDone_Callback(hObject, eventdata, handles)
0345
0346
0347
0348