Home > rest_20090422 > rest_ROIList_gui.m

rest_ROIList_gui

PURPOSE ^

List ROI definition list and edit for REST's functional connectivity by Xiao-Wei Song

SYNOPSIS ^

function varargout = rest_ROIList_gui(varargin)

DESCRIPTION ^

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="Dawnwei.Song@gmail.com">Xiaowei Song</a>; <a href="ycg.yan@gmail.com">Chaogan Yan</a> 
   Revised by YAN Chao-Gan 080610: NIFTI compatible
   Last Modified by Yan Chao-Gan 080808: also support NIFTI images.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

DOWNLOAD ^

rest_ROIList_gui.m

SOURCE CODE ^

0001 function varargout = rest_ROIList_gui(varargin)
0002 %List ROI definition list and edit for REST's functional connectivity by Xiao-Wei Song
0003 %------------------------------------------------------------------------------------------------------------------------------
0004 %    Copyright(c) 2007~2010
0005 %    State Key Laboratory of Cognitive Neuroscience and Learning in Beijing Normal University
0006 %    Written by Xiao-Wei Song
0007 %    http://resting-fmri.sourceforge.net
0008 %------------------------------------------------------------------------------------------------------------------------------
0009 % Begin initialization code - DO NOT EDIT
0010 %     Mail to Authors:  <a href="Dawnwei.Song@gmail.com">Xiaowei Song</a>; <a href="ycg.yan@gmail.com">Chaogan Yan</a>
0011 %   Revised by YAN Chao-Gan 080610: NIFTI compatible
0012 %   Last Modified by Yan Chao-Gan 080808: also support NIFTI images.
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 % End initialization code - DO NOT EDIT
0030 
0031 
0032 % --- Executes just before rest_ROIList_gui is made visible.
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         %Calling convention: varargin{1} was discarded
0039         % x=roi_list('ListROI', './setRoi/listTest.rest_roi')
0040         % handles.ROISetFile =varargin{2};
0041         % handles.ROISetList =ReadROIDefineFile(handles);
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 % Update handles structure
0052 guidata(hObject, handles);
0053 UpdateDisplay(handles);
0054 % UIWAIT makes rest_ROIList_gui wait for user response (see UIRESUME)
0055 try
0056     uiwait(handles.figListROI);
0057 catch
0058     uiresume(handles.figListROI);
0059 end
0060 
0061 
0062 % --- Outputs from this function are returned to the command line.
0063 function varargout = rest_ROIList_gui_OutputFcn(hObject, eventdata, handles) 
0064 % varargout  cell array for returning output args (see VARARGOUT);
0065 % hObject    handle to figure
0066 % eventdata  reserved - to be defined in a future version of MATLAB
0067 % handles    structure with handles and user data (see GUIDATA)
0068 
0069 % Get default command line output from handles structure
0070 % uiwait(handles.figListROI);
0071 % varargout{1} = handles.ROISetFile; %handles.output;
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 % --- Executes on button press in btnDelete.
0105 function btnDelete_Callback(hObject, eventdata, handles)
0106 % hObject    handle to btnDelete (see GCBO) % handles    structure with handles and user data (see GUIDATA)
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 % --- Executes on button press in btnAdd.
0126 function btnAdd_Callback(hObject, eventdata, handles)
0127 % hObject    handle to btnAdd (see GCBO)
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 % --- Executes on button press in btnView.
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 % --- Executes on button press in btnDone.
0143 function btnDone_Callback(hObject, eventdata, handles)
0144 % hObject    handle to btnDone (see GCBO)
0145     %Remove the empty data  at the first position from the list
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     % delete(handles.figListROI);
0155 
0156 
0157 function Result =ReadROIDefineFile(handles)
0158     %Check first
0159     [pathstr, name, ext, versn] = fileparts(mfilename('fullpath'));
0160     theROIDir =fullfile(pathstr,'SetROI');
0161     % Result =handles.ROISetFile; %fullfile(theROIDir, AROIDefineFile);
0162     Result ={};
0163     if 7==exist(theROIDir,'dir') && 2==exist(handles.ROISetFile, 'file'),
0164         %Read the list from ROI definition file
0165         % Result =textread(handles.ROISetFile,'%s\n', 'whitespace', '');
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%The definition dir and file doesn't exist
0174         if 7~=exist(theROIDir,'dir'),
0175             %I initialize the list file and the dir
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     %Linux compatible
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 % --- Executes on selection change in pmnuMisc.
0212 function pmnuMisc_Callback(hObject, eventdata, handles)
0213 % hObject    handle to pmnuMisc (see GCBO)
0214     
0215     switch get(handles.pmnuMisc, 'Value'),
0216     case {1, 2, 4, 8},    %Misc or '---'
0217         %Do nothing
0218     case 3, %'Retrieve averaged time course from selected ROI definition'
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, %'Export List of ROI definitions'
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, %'Import List of ROI definitions'
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            %Save before setting selected to the first of imported lines
0242            guidata(hObject, handles);
0243            set(handles.pmnuMisc, 'Value', size(handles.ROISetList,1) -size(theLines,1) +1);
0244         end
0245     
0246     case 7, %'Clear Current List'
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 % --- Executes during object creation, after setting all properties.
0262 function pmnuMisc_CreateFcn(hObject, eventdata, handles)
0263 % hObject    handle to pmnuMisc (see GCBO)
0264 % eventdata  reserved - to be defined in a future version of MATLAB
0265 % handles    empty - handles not created until after all CreateFcns called
0266 
0267 % Hint: popupmenu controls usually have a white background on Windows.
0268 %       See ISPC and COMPUTER.
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             %The ROI definition is a Ball definition
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 ,    % not canceled and legal
0282                 %Get the 4D brain
0283                 [the4DBrain, VoxelSize, ImgFileList, Header] =rest_to4d(pathname);                
0284                 %Generate the mask
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,    % Make sure the Definition file exist
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 ,    % not canceled and legal
0302                     %Get the 4D brain
0303                     [the4DBrain, VoxelSize, ImgFileList, Header] =rest_to4d(pathname);    
0304                     [nDim1, nDim2, nDim3, nDim4]=size(the4DBrain);
0305                     %The ROI definition is a mask file
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         %Retrieve the TimeCourse
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    %The Averaged Time Course within the ROI now comes out! 20070903
0327         rest_waitbar;
0328         
0329         %Save ROI time course to a txt file
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 % --- Executes when user attempts to close figListROI.
0343 function figListROI_CloseRequestFcn(hObject, eventdata, handles)
0344     btnDone_Callback(hObject, eventdata, handles)
0345 % Hint: delete(hObject) closes the figure
0346 %delete(hObject);
0347 
0348

Generated on Wed 29-Apr-2009 01:06:38 by m2html © 2005