Home > rest_20090422 > reho_gui.m

reho_gui

PURPOSE ^

Regional homogenius GUI by Xiaowei Song

SYNOPSIS ^

function varargout = reho_gui(varargin)

DESCRIPTION ^

Regional homogenius GUI by Xiaowei 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
 $mail     =dawnwei.song@gmail.com
 $Version =1.0
 $Date    =20070421
-----------------------------------------------------------
     <a href="Dawnwei.Song@gmail.com">Mail to Author</a>: Xiaowei Song
    Version=1.3;
    Release=20090420;
   Modified by GUIDE v2.5 03-Nov-2007 20:10:43
   Revised by Yan Chao-Gan 080808: also support NIFTI images.
   Last Revised by YAN Chao-Gan, 090420. Designate the input file path and output file path for the revised reho.m

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

DOWNLOAD ^

reho_gui.m

SOURCE CODE ^

0001 function varargout = reho_gui(varargin)
0002 %Regional homogenius GUI by Xiaowei 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 % $mail     =dawnwei.song@gmail.com
0009 % $Version =1.0
0010 % $Date    =20070421
0011 %-----------------------------------------------------------
0012 %     <a href="Dawnwei.Song@gmail.com">Mail to Author</a>: Xiaowei Song
0013 %    Version=1.3;
0014 %    Release=20090420;
0015 %   Modified by GUIDE v2.5 03-Nov-2007 20:10:43
0016 %   Revised by Yan Chao-Gan 080808: also support NIFTI images.
0017 %   Last Revised by YAN Chao-Gan, 090420. Designate the input file path and output file path for the revised reho.m
0018 
0019 % Begin initialization code - DO NOT EDIT
0020 gui_Singleton = 1;
0021 gui_State = struct('gui_Name',       mfilename, ...
0022                    'gui_Singleton',  gui_Singleton, ...
0023                    'gui_OpeningFcn', @reho_gui_OpeningFcn, ...
0024                    'gui_OutputFcn',  @reho_gui_OutputFcn, ...
0025                    'gui_LayoutFcn',  [] , ...
0026                    'gui_Callback',   []);
0027 if nargin && ischar(varargin{1})
0028     gui_State.gui_Callback = str2func(varargin{1});
0029 end
0030 
0031 if nargout
0032     [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
0033 else
0034     gui_mainfcn(gui_State, varargin{:});
0035 end
0036 % End initialization code - DO NOT EDIT
0037 
0038 
0039 % --- Executes just before reho_gui is made visible.
0040 function reho_gui_OpeningFcn(hObject, eventdata, handles, varargin)    
0041     %Matlab -Linux compatible, Initialize controls' default properties, dawnsong , 20070507
0042     InitControlProperties(hObject, handles);
0043     %Matlab -v6 compatible, create some frames instead of panels
0044     InitFrames(hObject,handles);
0045         
0046     [pathstr, name, ext, versn] = fileparts(mfilename('fullpath'));    
0047     %the {hdr/img} directories to be processed , count of volumns(i.e. time series' point number) corresponding to the dir
0048     handles.Cfg.DataDirs ={}; %{[pathstr '\SampleData'], 10} ;
0049     handles.Cfg.MaskFile = 'Default';                 %the  user defined mask file
0050     handles.Cfg.ClusterCount =27 ;            % the cluster type, 27 or 19 or 7
0051     handles.Cfg.OutputDir =pwd;                % pwd is the default dir for reho map result
0052     handles.Cfg.WantMeanRehoMap ='Yes';        %Calcute the mean ReHo map default
0053     handles.Filter.BandLow  =0.01;            %Config about Band pass filter, dawnsong 20070429
0054     handles.Filter.BandHigh =0.08;
0055     handles.Filter.UseFilter       ='No';
0056     handles.Filter.Retrend        ='Yes';        % by default, do re-trend after linear filtering after removing linear trend and disable changing this value, 20070530 ZangYF decide
0057     handles.Filter.SamplePeriod=2;            %by default, set TR=2s
0058     handles.Detrend.BeforeFilter ='No';% ZangYF, 20070530 decide
0059     handles.Detrend.AfterFilter  ='No';% ZangYF, 20070530 decide
0060     handles.Log.SelfPath =pathstr;            % 20070507, dawnsong, just for writing log to file for further investigation
0061     handles.Log.Filename =GetLogFilename('', '');
0062     %Performance record, use elapsed time to describe it, 20070507
0063     handles.Performance =0;            
0064     
0065     guidata(hObject, handles);
0066     UpdateDisplay(handles);
0067     movegui(handles.figRehoMain, 'center');
0068     set(handles.figRehoMain,'Name','Regional Homogeneity');
0069     
0070     % Choose default command line output for reho_gui
0071     handles.output = hObject;        
0072     guidata(hObject, handles);% Update handles structure
0073 
0074     % UIWAIT makes reho_gui wait for user response (see UIRESUME)
0075     % uiwait(handles.figRehoMain);
0076 
0077 % --- Outputs from this function are returned to the command line.
0078 function varargout = reho_gui_OutputFcn(hObject, eventdata, handles)
0079     % Get default command line output from handles structure
0080     varargout{1} = handles.output;
0081 
0082     
0083     
0084 function edtDataDirectory_Callback(hObject, eventdata, handles)
0085     theDir =get(hObject, 'String');    
0086     restGui_SetDataDir(hObject,theDir, handles);        
0087 
0088 function edtDataDirectory_CreateFcn(hObject, eventdata, handles)
0089     if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
0090         set(hObject,'BackgroundColor','white');
0091     end
0092 
0093     % --- Executes on button press in btnSelectDataDir.
0094 function btnSelectDataDir_Callback(hObject, eventdata, handles)
0095     if prod(size(handles.Cfg.DataDirs))>0 && size(handles.Cfg.DataDirs, 1)>0,
0096         theDir =handles.Cfg.DataDirs{1,1};
0097     else
0098         theDir =pwd;
0099     end
0100     theDir =uigetdir(theDir, 'Please select the data directory to compute ReHo map: ');
0101     if ischar(theDir),
0102         restGui_SetDataDir(hObject, theDir,handles);    
0103     end
0104     
0105 function RecursiveAddDataDir(hObject, eventdata, handles)
0106     if prod(size(handles.Cfg.DataDirs))>0 && size(handles.Cfg.DataDirs, 1)>0,
0107         theDir =handles.Cfg.DataDirs{1,1};
0108     else
0109         theDir =pwd;
0110     end
0111     theDir =uigetdir(theDir, 'Please select the parent data directory of many sub-folders containing EPI data to compute ReHo map: ');
0112     if ischar(theDir),
0113         %Make the warning dlg off! 20071201
0114         setappdata(0, 'ReHo_DoingRecursiveDir', 1);
0115         theOldColor =get(handles.listDataDirs, 'BackgroundColor');
0116         set(handles.listDataDirs, 'BackgroundColor', [ 0.7373    0.9804    0.4784]);
0117         try
0118             rest_RecursiveDir(theDir, 'reho_gui(''restGui_SetDataDir'',gcbo, ''%s'', guidata(gcbo) )');
0119         catch
0120             rest_misc( 'DisplayLastException');
0121         end    
0122         set(handles.listDataDirs, 'BackgroundColor', theOldColor);
0123         rmappdata(0, 'ReHo_DoingRecursiveDir');
0124     end
0125     
0126     
0127 function restGui_SetDataDir(hObject,ADir, handles)    
0128     theOldWarnings =warning('off', 'all');
0129     if ~isequal(ADir , 0) && rest_misc('GetMatlabVersion')>=7.3,        
0130         ADir =strtrim(ADir);
0131     end    
0132     % if (~isequal(ADir , 0)) &&( (size(handles.Cfg.DataDirs, 1)==0)||(0==seqmatch({ADir} ,handles.Cfg.DataDirs( : , 1) ) ) ) ,20070519
0133     if (~isequal(ADir , 0)) &&( (size(handles.Cfg.DataDirs, 1)==0)||(0==length(strmatch(ADir,handles.Cfg.DataDirs( : , 1),'exact' ) ) ))
0134         handles.Cfg.DataDirs =[ {ADir , 0}; handles.Cfg.DataDirs];%update the dir
0135         theVolumnCount =CheckDataDir(handles.Cfg.DataDirs{1,1} );    
0136         if (theVolumnCount<=0),
0137             if isappdata(0, 'ReHo_DoingRecursiveDir') && getappdata(0, 'ReHo_DoingRecursiveDir'), 
0138             else
0139                 fprintf('There is no data or non-data files in this directory:\n%s\nPlease re-select\n\n', ADir);
0140                 errordlg( sprintf('There is no data or non-data files in this directory:\n\n%s\n\nPlease re-select', handles.Cfg.DataDirs{1,1} )); 
0141             end
0142             handles.Cfg.DataDirs(1,:)=[];
0143             if size(handles.Cfg.DataDirs, 1)==0
0144                 handles.Cfg.DataDirs=[];
0145             end    %handles.Cfg.DataDirs = handles.Cfg.DataDirs( 2:end, :);%update the dir
0146         else
0147             handles.Cfg.DataDirs{1,2} =theVolumnCount;
0148         end    
0149     
0150         guidata(hObject, handles);
0151         UpdateDisplay(handles);
0152     end
0153     warning(theOldWarnings);
0154     
0155 %% Update All the uiControls' display on the GUI
0156 function UpdateDisplay(handles)
0157     if size(handles.Cfg.DataDirs,1)>0    
0158         theOldIndex =get(handles.listDataDirs, 'Value');
0159         %set(handles.listDataDirs, 'String',  handles.Cfg.DataDirs(: ,1) , 'Value', 1);
0160         set(handles.listDataDirs, 'String',  GetInputDirDisplayList(handles) , 'Value', 1);
0161         theCount =size(handles.Cfg.DataDirs,1);
0162         if (theOldIndex > 0) && (theOldIndex <= theCount)
0163             set(handles.listDataDirs, 'Value', theOldIndex);
0164         end    
0165         set(handles.edtDataDirectory,'String', handles.Cfg.DataDirs{1,1});
0166         theResultFilename=get(handles.edtPrefix, 'String');
0167         theResultFilename=[theResultFilename '_' GetDirName(handles.Cfg.DataDirs{1,1})];
0168         set(handles.txtResultFilename, 'String', [theResultFilename  '.{hdr/img}']);
0169     else
0170         set(handles.listDataDirs, 'String', '' , 'Value', 0);
0171         set(handles.txtResultFilename, 'String', 'Result: Prefix_DirectoryName.{hdr/img}');
0172     end
0173     % set(handles.pnlParametersInput,'Title', ...            %show the first dir's Volumn count in the panel's title
0174          % ['Input Parameters (Volumn count= '...
0175           % num2str( cell2mat(handles.Cfg.DataDirs(1,2)) )...
0176           % ' in 'handles.Cfg.DataDirs(1,1) ' )']);
0177     set(handles.edtOutputDir ,'String', handles.Cfg.OutputDir);    
0178     if isequal(handles.Cfg.MaskFile, '')
0179         set(handles.edtMaskfile, 'String', 'Don''t use any Mask');
0180     else
0181         set(handles.edtMaskfile, 'String', handles.Cfg.MaskFile);    
0182     end
0183     %Set detrend dawnsong 20070820
0184     if strcmpi(handles.Detrend.BeforeFilter, 'Yes')
0185         %Update filter and detrend button's state according to Option: detrend/Filter 20070820
0186         set(handles.btnDetrend, 'Enable', 'on');
0187     else
0188         %Update filter and detrend button's state according to Option: detrend/Filter 20070820
0189         set(handles.btnDetrend, 'Enable', 'off');
0190     end
0191     %Set filter, dawnsong 20070430
0192     if strcmpi(handles.Filter.UseFilter, 'Yes')
0193         set(handles.ckboxFilter, 'Value', 1);        
0194         set(handles.ckboxRetrend, 'Enable', 'off');        
0195         set(handles.edtBandLow, 'Enable', 'on', 'String', num2str(handles.Filter.BandLow));
0196         set(handles.edtBandHigh, 'Enable', 'on', 'String', num2str(handles.Filter.BandHigh));
0197         set(handles.edtSamplePeriod, 'Enable', 'on', 'String', num2str(handles.Filter.SamplePeriod));
0198         %Update filter and detrend button's state according to Option: detrend/Filter 20070820
0199         set(handles.btnBandPass, 'Enable', 'on');        
0200     else
0201         set(handles.ckboxFilter, 'Value', 0);        
0202         set(handles.ckboxRetrend,'Enable', 'off');
0203         set(handles.edtBandLow, 'Enable', 'off', 'String', num2str(handles.Filter.BandLow));
0204         set(handles.edtBandHigh, 'Enable', 'off', 'String', num2str(handles.Filter.BandHigh));
0205         set(handles.edtSamplePeriod, 'Enable', 'off', 'String', num2str(handles.Filter.SamplePeriod));        
0206         %Update filter and detrend button's state according to Option: detrend/Filter 20070820
0207         set(handles.btnBandPass, 'Enable', 'off');        
0208     end
0209     %Set mean calculation, dawnsong 20070504
0210     set(handles.ckboxDivideMean, 'Value', strcmpi(handles.Cfg.WantMeanRehoMap, 'Yes'));        
0211     set(handles.ckboxRemoveTrendBefore, 'Value', strcmpi(handles.Detrend.BeforeFilter, 'Yes'));    
0212     set(handles.ckboxRemoveTrendAfter, 'Value', strcmpi(handles.Detrend.AfterFilter, 'Yes'));        
0213         
0214 
0215     
0216 %% check the Data dir to make sure that there are only {hdr,img}
0217 function Result=GetInputDirDisplayList(handles)
0218     Result ={};
0219     for x=size(handles.Cfg.DataDirs, 1):-1:1
0220         Result =[{sprintf('%d# %s',handles.Cfg.DataDirs{x, 2},handles.Cfg.DataDirs{x, 1})} ;Result];
0221     end
0222 
0223 % in this dir
0224 function [nVolumn]=CheckDataDir(ADataDir)
0225     theFilenames = dir(ADataDir);
0226     theHdrFiles=dir(fullfile(ADataDir,'*.hdr'));
0227     theImgFiles=dir(fullfile(ADataDir,'*.img'));
0228     % if (length(theFilenames)-length(theHdrFiles)-length(theImgFiles))>2
0229         % nVolumn =-1;
0230         % errordlg(sprintf(['There should not be any file other than *.{hdr,img} .' ...
0231                     % 'Please re-examin the DataDir\n\n%s '] ...
0232                     % , ADataDir));
0233         % return;
0234     % end
0235     if ~length(theHdrFiles)==length(theImgFiles)
0236         nVolumn =-1;
0237         fprintf('%s, *.{hdr,img} should be pairwise. Please re-examin them.\n', ADataDir);
0238         errordlg('*.{hdr,img} should be pairwise. Please re-examin them.'); 
0239         return;
0240     end        
0241     count = 3; nVolumn = 0;        
0242     for count = 3:size(struct2cell(theFilenames),2)                
0243         if    (length(theFilenames(count).name)>4) && ...
0244             strcmpi(theFilenames(count).name(end-3:end) , '.hdr') 
0245             if strcmpi(theFilenames(count).name(1:end-4) ...                %hdr
0246                             , theFilenames(count+1).name(1:end-4) )     %img
0247                 nVolumn = nVolumn + 1;  
0248             else
0249                 %error('*.{hdr,img} should be pairwise. Please re-examin them.');
0250                 nVolumn =-1;
0251                 fprintf('%s, *.{hdr,img} should be pairwise. Please re-examin them.', ADataDir);
0252                 errordlg('*.{hdr,img} should be pairwise. Please re-examin them.'); 
0253                 break;
0254             end
0255         end            
0256     end
0257     
0258     
0259 %% --- Executes on button press in btnComputeReho.
0260 function btnComputeReho_Callback(hObject, eventdata, handles)
0261     if (size(handles.Cfg.DataDirs, 1)==0) %check legal parameter set first
0262         errordlg('No Data found! Please re-config'); 
0263         return;
0264     end
0265     if (exist('reho.m','file')==2)
0266         %write log 20070507
0267         handles.Log.Filename =GetLogFilename(handles.Cfg.OutputDir, get(handles.edtPrefix, 'String'));
0268         Log2File(handles);
0269         handles.Performance =cputime; %Write down the Start time , 20070903
0270         %start computation
0271         theOldDir =pwd;
0272         theOldColor=get(hObject,'BackgroundColor');        
0273         set(hObject,'Enable','off', 'BackgroundColor', 'red');
0274         drawnow;
0275         try
0276             %%Remove the linear trend first, and create a new directory, then do filtering
0277             if strcmpi(handles.Filter.UseFilter, 'Yes') && strcmpi(handles.Detrend.BeforeFilter, 'Yes'),
0278                 Detrend(hObject, handles);
0279                 %20070614, Bug fix, Update the data structure manually
0280                 handles =guidata(hObject);    % I have to read it again, because I change it for further processing
0281             end    
0282             
0283             %%Filter all the data and create a new directory, then compute the ReHo value, dawnsong 20070429
0284             %Band pass filter
0285             if strcmpi(handles.Filter.UseFilter, 'Yes')
0286                 BandPass(hObject, handles);    
0287                 %20070614, Bug fix, Update the data structure manually
0288                 handles =guidata(hObject);    % I have to read it again, because I change it for further processing
0289             end    
0290                         
0291             %%Remove the linear trend after filtering, and create a new directory, then do ReHo
0292             if strcmpi(handles.Filter.UseFilter, 'Yes') && strcmpi(handles.Detrend.AfterFilter, 'Yes'),
0293                 Detrend(hObject, handles);
0294                 %20070614, Bug fix, Update the data structure manually
0295                 handles =guidata(hObject);    % I have to read it again, because I change it for further processing
0296             end
0297             
0298             %compute the ReHo brain
0299             if size(handles.Cfg.DataDirs, 1)>1, 
0300                 rest_waitbar(0,'ReHo Batch Compution, wait ...','ReHo','Parent');
0301             end
0302             for x=1:size(handles.Cfg.DataDirs, 1)    
0303                 %Update display
0304                 set(handles.listDataDirs, 'Value', x);
0305                 drawnow;
0306                 if size(handles.Cfg.DataDirs, 1)>1, 
0307                     rest_waitbar((x-1)/size(handles.Cfg.DataDirs, 1)+0.01, ...
0308                                 handles.Cfg.DataDirs{x, 1}, ...
0309                                 'ReHo Computing','Parent');
0310                 end
0311                 
0312                 cd(handles.Cfg.DataDirs{x, 1});%Change current dir to the data dir
0313                 fprintf('\nReHo :"%s"\n', handles.Cfg.DataDirs{x, 1});        
0314                 
0315                 theOutputDir=get(handles.edtOutputDir, 'String');
0316                 thePrefix =get(handles.edtPrefix, 'String');
0317                 theDstFile=fullfile(theOutputDir,[thePrefix '_' ...
0318                                             GetDirName(handles.Cfg.DataDirs{x, 1}) ] );
0319                 
0320                 reho(   handles.Cfg.DataDirs{x, 1}, ...
0321                         handles.Cfg.ClusterCount, ...
0322                         handles.Cfg.MaskFile, ...
0323                         theDstFile);      % Revised by YAN Chao-Gan, 090420. Designate the input file path and output file path for the revised reho.m
0324                 
0325                 %20070504, divide ReHo brain by the mean within the mask
0326                 if strcmpi(handles.Cfg.WantMeanRehoMap, 'Yes')
0327                     theOrigReHoMap =theDstFile;                
0328                     theMeanReHoMap =fullfile(theOutputDir,['m' thePrefix '_' ...
0329                                                 GetDirName(handles.Cfg.DataDirs{x, 1}) ] );
0330                     theMaskFile =handles.Cfg.MaskFile;
0331                     rest_DivideMeanWithinMask(theOrigReHoMap, theMeanReHoMap, theMaskFile);
0332                 end
0333             end    
0334             handles.Performance =cputime -handles.Performance; %Write down the End time , 20070903
0335             LogPerformance(handles);
0336         catch    
0337             rest_misc( 'DisplayLastException');
0338             errordlg(sprintf('Exception occured: \n\n%s' , lasterr)); 
0339         end
0340         rest_waitbar;
0341         cd(theOldDir);
0342         set(hObject,'Enable','on', 'BackgroundColor', theOldColor);
0343         drawnow;
0344     else
0345         errordlg('No reho.m ! Please re-install'); 
0346     end
0347 
0348 %%
0349 function edtMaskfile_Callback(hObject, eventdata, handles)
0350     theMaskfile =get(hObject, 'String');
0351     if ~isequal(theMaskfile , 0) && rest_misc('GetMatlabVersion')>=7.3,
0352         theMaskfile =strtrim(theMaskfile);
0353     end    
0354     if exist(theMaskfile, 'file')
0355         handles.Cfg.MaskFile =theMaskfile;
0356         guidata(hObject, handles);
0357     else
0358         errordlg(sprintf('The mask file "%s" does not exist!\n Please re-check it.', theMaskfile));
0359     end
0360     
0361 % --- Executes during object creation, after setting all properties.
0362 function edtMaskfile_CreateFcn(hObject, eventdata, handles)
0363     if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
0364         set(hObject,'BackgroundColor','white');
0365     end
0366 
0367 
0368 %% --- Executes on button press in btnSelectMask.
0369 function btnSelectMask_Callback(hObject, eventdata, handles)
0370     [filename, pathname] = uigetfile({'*.img;*.mat', 'All Mask files (*.img; *.mat)'; ...
0371                                                 '*.mat','MAT masks (*.mat)'; ...
0372                                                 '*.img', 'ANALYZE or NIFTI masks(*.img)'}, ...
0373                                                 'Pick a user''s  mask');
0374     if ~(filename==0)
0375         handles.Cfg.MaskFile =[pathname filename];
0376         guidata(hObject,handles);
0377     elseif ~( exist(handles.Cfg.MaskFile, 'file')==2)
0378         set(handles.rbtnDefaultMask, 'Value',[1]);        
0379         set(handles.rbtnUserMask, 'Value',[0]);        
0380         set(handles.edtMaskfile, 'Enable','off');
0381         set(handles.btnSelectMask, 'Enable','off');            
0382         handles.Cfg.MaskFile ='Default';        
0383         guidata(hObject, handles);
0384     end    
0385     UpdateDisplay(handles);
0386 
0387 %% --- Select Default mask
0388 function rbtnDefaultMask_Callback(hObject, eventdata, handles)    
0389     set(handles.btnSelectMask, 'Enable','off');        
0390     handles.Cfg.MaskFile ='Default';
0391     guidata(hObject, handles);
0392     set(handles.rbtnDefaultMask,'Value',1);
0393     set(handles.rbtnNullMask,'Value',0);
0394     set(handles.rbtnUserMask,'Value',0);
0395     
0396 % --- Executes on button press in rbtnNullMask.
0397 function rbtnNullMask_Callback(hObject, eventdata, handles)
0398     set(handles.edtMaskfile, 'Enable','off', 'String','Don''t use any Mask');
0399     set(handles.btnSelectMask, 'Enable','off');
0400     drawnow;
0401     handles.Cfg.MaskFile ='';
0402     guidata(hObject, handles);
0403     set(handles.rbtnDefaultMask,'Value',0);
0404     set(handles.rbtnNullMask,'Value',1);
0405     set(handles.rbtnUserMask,'Value',0);
0406         
0407 %% --- Select user defined mask
0408 function rbtnUserMask_Callback(hObject, eventdata, handles)
0409     set(handles.edtMaskfile,'Enable','on', 'String',handles.Cfg.MaskFile);
0410     set(handles.btnSelectMask, 'Enable','on');
0411     set(handles.rbtnDefaultMask,'Value',0);
0412     set(handles.rbtnNullMask,'Value',0);
0413     set(handles.rbtnUserMask,'Value',1);
0414     drawnow;
0415 % --- Executes on button press in rbtn27voxels.
0416 function rbtn27voxels_Callback(hObject, eventdata, handles)
0417     handles.Cfg.ClusterCount =27;
0418     guidata(hObject, handles);
0419     set(handles.rbtn7voxels,'Value',0);
0420     set(handles.rbtn19voxels,'Value',0);
0421     set(handles.rbtn27voxels,'Value',1);
0422 
0423 % --- Executes on button press in rbtn19voxels.
0424 function rbtn19voxels_Callback(hObject, eventdata, handles)
0425     handles.Cfg.ClusterCount =19;
0426     guidata(hObject, handles);
0427     set(handles.rbtn7voxels,'Value',0);
0428     set(handles.rbtn19voxels,'Value',1);
0429     set(handles.rbtn27voxels,'Value',0);
0430     
0431 % --- Executes on button press in rbtn7voxels.
0432 function rbtn7voxels_Callback(hObject, eventdata, handles)
0433     handles.Cfg.ClusterCount =7;
0434     guidata(hObject, handles);    
0435     set(handles.rbtn7voxels,'Value',1);
0436     set(handles.rbtn19voxels,'Value',0);
0437     set(handles.rbtn27voxels,'Value',0);
0438 
0439 %% Double-click to show the volumn count of the selected dir
0440 function listDataDirs_Callback(hObject, eventdata, handles)
0441     theIndex =get(hObject, 'Value');
0442     if isempty(theIndex) || theIndex<1,
0443         msgbox(sprintf('Nothing added.\n\nYou must add some diretories containing only paired {hdr/img} files first'), ...
0444                     'REST' ,'help');
0445         return;
0446     end
0447     if strcmp(get(handles.figRehoMain, 'SelectionType'), 'open') %when double click
0448         msgbox(sprintf('%s \t\nhas\t %d\t volumes\n\nTotal: %d Data Directories' , ... 
0449                     handles.Cfg.DataDirs{theIndex, 1} , ...
0450                     handles.Cfg.DataDirs{theIndex, 2} , ...
0451                     size(handles.Cfg.DataDirs,1)), ...
0452                     'Volume count in selected dir' ,'help');
0453     end
0454     
0455 function listDataDirs_KeyPressFcn(hObject, eventdata, handles)
0456     %Delete the selected item when 'Del' is pressed
0457     key =get(handles.figRehoMain, 'currentkey');
0458     if seqmatch({key},{'delete', 'backspace'})
0459         DeleteSelectedDataDir(hObject, eventdata, handles);
0460     end
0461     
0462     
0463 function DeleteSelectedDataDir(hObject, eventdata, handles)    
0464     theIndex =get(handles.listDataDirs, 'Value');
0465     if prod(size(handles.Cfg.DataDirs))==0 ...
0466         || size(handles.Cfg.DataDirs, 1)==0 ...
0467         || theIndex>size(handles.Cfg.DataDirs, 1),
0468         return;
0469     end
0470     theDir     =handles.Cfg.DataDirs{theIndex, 1};
0471     theVolumnCount=handles.Cfg.DataDirs{theIndex, 2};
0472     tmpMsg=sprintf('Delete\n\n "%s" \nVolumn Count :%d ?', theDir, theVolumnCount);
0473     if strcmp(questdlg(tmpMsg, 'Delete confirmation'), 'Yes')
0474         if theIndex>1,
0475             set(handles.listDataDirs, 'Value', theIndex-1);
0476         end
0477         handles.Cfg.DataDirs(theIndex, :)=[];
0478         if size(handles.Cfg.DataDirs, 1)==0
0479             handles.Cfg.DataDirs={};
0480         end    
0481         guidata(hObject, handles);
0482         UpdateDisplay(handles);
0483     end
0484 
0485 function ClearDataDirectories(hObject, eventdata, handles)    
0486     if prod(size(handles.Cfg.DataDirs))==0 ...
0487         || size(handles.Cfg.DataDirs, 1)==0,        
0488         return;
0489     end
0490     tmpMsg=sprintf('Attention!\n\n\nDelete all data directories?');
0491     if strcmpi(questdlg(tmpMsg, 'Clear confirmation'), 'Yes'),        
0492         handles.Cfg.DataDirs(:)=[];
0493         if prod(size(handles.Cfg.DataDirs))==0,
0494             handles.Cfg.DataDirs={};
0495         end    
0496         guidata(hObject, handles);
0497         UpdateDisplay(handles);
0498     end
0499     
0500 function listDataDirs_CreateFcn(hObject, eventdata, handles)
0501     if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
0502         set(hObject,'BackgroundColor','white');
0503     end
0504 
0505 function edtPrefix_Callback(hObject, eventdata, handles)
0506 %nothing need to do, because I get the prefix when I need. Look at line 229 "thePrefix =get(handles.edtPrefix, 'String');"
0507     UpdateDisplay(handles);
0508 
0509 function edtPrefix_CreateFcn(hObject, eventdata, handles)
0510     if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
0511         set(hObject,'BackgroundColor','white');
0512     end
0513 
0514 function edtOutputDir_Callback(hObject, eventdata, handles)
0515     theDir =get(hObject, 'String');    
0516     SetOutputDir(hObject,handles, theDir);    
0517     
0518 
0519 function edtOutputDir_CreateFcn(hObject, eventdata, handles)
0520     if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
0521         set(hObject,'BackgroundColor','white');
0522     end
0523 
0524 function btnSelectOutputDir_Callback(hObject, eventdata, handles)
0525     theDir =handles.Cfg.OutputDir;
0526     theDir =uigetdir(theDir, 'Please select the data directory to compute ReHo map: ');
0527     if ~isequal(theDir, 0)
0528         SetOutputDir(hObject,handles, theDir);    
0529     end    
0530     
0531 function SetOutputDir(hObject, handles, ADir)
0532     if 7==exist(ADir,'dir')
0533         handles.Cfg.OutputDir =ADir;
0534         guidata(hObject, handles);
0535         UpdateDisplay(handles);
0536     end
0537 
0538 function Result=GetDirName(ADir)
0539     if isempty(ADir), Result=ADir; return; end
0540     theDir =ADir;
0541     if strcmp(theDir(end),filesep)==1
0542         theDir=theDir(1:end-1);
0543     end    
0544     [tmp,Result]=fileparts(theDir);
0545     
0546 %Matlab -v6 compatible, create some frames instead of panels
0547     
0548 % --- Executes on button press in ckboxFilter.
0549 function ckboxFilter_Callback(hObject, eventdata, handles)
0550     if get(hObject,'Value')
0551         handles.Filter.UseFilter ='Yes';
0552     else    
0553         handles.Filter.UseFilter ='No';
0554     end    
0555     guidata(hObject, handles);
0556     UpdateDisplay(handles);
0557 
0558 function edtBandLow_Callback(hObject, eventdata, handles)
0559     handles.Filter.BandLow =str2double(get(hObject,'String'));
0560     guidata(hObject, handles);
0561 
0562 function edtBandLow_CreateFcn(hObject, eventdata, handles)
0563 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
0564     set(hObject,'BackgroundColor','white');
0565 end
0566 
0567 function edtBandHigh_Callback(hObject, eventdata, handles)
0568     handles.Filter.BandHigh =str2double(get(hObject,'String'));
0569     guidata(hObject, handles);
0570     
0571 function edtBandHigh_CreateFcn(hObject, eventdata, handles)
0572 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
0573     set(hObject,'BackgroundColor','white');
0574 end
0575 function ckboxRetrend_Callback(hObject, eventdata, handles)
0576     if get(hObject,'Value')
0577         handles.Filter.Retrend ='Yes';
0578     else    
0579         handles.Filter.Retrend ='No';
0580     end    
0581     guidata(hObject, handles);
0582     UpdateDisplay(handles);
0583     
0584 function edtSamplePeriod_Callback(hObject, eventdata, handles)
0585     handles.Filter.SamplePeriod =str2double(get(hObject,'String'));
0586     guidata(hObject, handles);
0587     
0588 function edtSamplePeriod_CreateFcn(hObject, eventdata, handles)
0589     if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
0590         set(hObject,'BackgroundColor','white');
0591     end
0592 
0593 
0594 
0595 %Divide ReHo brain by the mean within the mask, output: mRehoMap.{hdr/img}
0596 function ckboxDivideMean_Callback(hObject, eventdata, handles)
0597     if get(hObject,'Value')
0598         handles.Cfg.WantMeanRehoMap ='Yes';
0599     else    
0600         handles.Cfg.WantMeanRehoMap ='No';
0601     end
0602     guidata(hObject, handles);
0603     UpdateDisplay(handles);
0604     
0605 % ---Manaul Operations, Divide one rehomap by its global mean
0606 function btnDivideMean_Callback(hObject, eventdata, handles)
0607     theOldColor=get(hObject,'BackgroundColor');        
0608     set(hObject,'Enable','off', 'BackgroundColor', 'red');
0609     drawnow;
0610     try
0611         [filename, pathname] = uigetfile({'*.img', 'ReHo files (*.img)'}, ...
0612                                                     'Pick one ReHo map');
0613         if (filename~=0)% not canceled
0614             if strcmpi(filename(end-3:end), '.img')%revise filename to remove extension
0615                 filename = filename(1:end-4);
0616             end
0617             if ~strcmpi(pathname(end), filesep)%revise dir name to remove the last \ or /
0618                 pathname = [pathname filesep];
0619             end
0620             theOrigReHoMap =[pathname filename];
0621             theMeanReHoMap =[pathname 'm' filename];
0622             theMaskFile =handles.Cfg.MaskFile;
0623             rest_DivideMeanWithinMask(theOrigReHoMap, theMeanReHoMap, theMaskFile);
0624             msgbox(sprintf('ReHo brain "%s.{hdr/img}" \ndivide its mean within mask successfully.\t\n\nSave to "%s.{hdr/img}"\n' , ... 
0625                     theOrigReHoMap, theMeanReHoMap), ...                
0626                     'Divide mean within mask successfully' ,'help');
0627         end    
0628     catch
0629         rest_misc( 'DisplayLastException');
0630     end
0631     set(hObject,'Enable','on', 'BackgroundColor', theOldColor);
0632     drawnow;
0633 % manually band pass filter
0634 function btnBandPass_Callback(hObject, eventdata, handles)
0635     theOldColor=get(hObject,'BackgroundColor');        
0636     set(hObject,'Enable','off', 'BackgroundColor', 'red');
0637     drawnow;
0638     try
0639         %Band pass filter
0640         if strcmpi(handles.Filter.UseFilter, 'Yes')
0641             BandPass(hObject, handles);
0642             msgbox('Ideal Band Pass filter Over.',...
0643                     'Filter successfully' ,'help');
0644         else
0645             errordlg(sprintf('You didn''t select option "Band Pass". \n\nPlease slect first.'));
0646         end
0647         UpdateDisplay(handles);        
0648     catch
0649         rest_misc( 'DisplayLastException');
0650     end    
0651     rest_waitbar;
0652     set(hObject,'Enable','on', 'BackgroundColor', theOldColor);
0653     drawnow;
0654     
0655 %%Create GUI
0656 
0657 %%Create Frames/Lines
0658 function InitFrames(hObject,handles);
0659     offsetY =80; %dawnsong, 20070504, add for the divide by the mask mean, the Y of Edit "OutPut Diectory"
0660     % for Matlab 6.5 compatible, draw a panel
0661     uicontrol(handles.figRehoMain, 'Style','Frame','Position',[2 offsetY+151 433 1]);
0662     uicontrol(handles.figRehoMain, 'Style','Frame','Position',[2 offsetY+241 433 1]);
0663     uicontrol(handles.figRehoMain, 'Style','Frame','Position',[90 offsetY+152 1 90]);    
0664     uicontrol(handles.figRehoMain, 'Style','Frame','Position',[2 offsetY+152 1 233]);    
0665     uicontrol(handles.figRehoMain, 'Style','Frame','Position',[435 offsetY+152 1 233]);    
0666     uicontrol(handles.figRehoMain, 'Style','Frame','Position',[2 offsetY+385 433 1]);    
0667     uicontrol(handles.figRehoMain,'Style','Text','Position',[152 offsetY+380 140 14],...
0668         'String','Input Parameters');
0669     uicontrol(handles.figRehoMain,'Style','Text','Position',[8 offsetY+238 40 14],...
0670         'String','Cluster');    
0671     uicontrol(handles.figRehoMain,'Style','Text','Position',[208 offsetY+238 40 14],...
0672         'String','Mask');    
0673     
0674     uicontrol(handles.figRehoMain, 'Style','Frame','Position',[2 offsetY+94 433 1]);    
0675     uicontrol(handles.figRehoMain, 'Style','Frame','Position',[2 offsetY-8 433 1]);    
0676     uicontrol(handles.figRehoMain, 'Style','Frame','Position',[2 offsetY-8 1 102]);    
0677     uicontrol(handles.figRehoMain, 'Style','Frame','Position',[435 offsetY-8 1 102]);    
0678     uicontrol(handles.figRehoMain,'Style','Text','Position',[152 offsetY+88 160 14],...
0679         'String','Output Parameters (ReHo map)');
0680         
0681     uicontrol(handles.figRehoMain, 'Style','Frame','Position',[2 offsetY+400 433 1]);    
0682     uicontrol(handles.figRehoMain, 'Style','Frame','Position',[435 offsetY+400 1 50]);    
0683     uicontrol(handles.figRehoMain, 'Style','Frame','Position',[2 offsetY+400 1 50]);    
0684     uicontrol(handles.figRehoMain, 'Style','Frame','Position',[2 offsetY+450 433 1]);    
0685     uicontrol(handles.figRehoMain,'Style','Text','Position',[142 offsetY+445 180 14],...
0686         'String','Option: Ideal Band Pass Filter');
0687     
0688     uicontrol(handles.figRehoMain, 'Style','Frame','Position',[2 offsetY+465 433 1]);    
0689     uicontrol(handles.figRehoMain, 'Style','Frame','Position',[435 offsetY+465 1 50]);    
0690     uicontrol(handles.figRehoMain, 'Style','Frame','Position',[2 offsetY+465 1 50]);    
0691     uicontrol(handles.figRehoMain, 'Style','Frame','Position',[2 offsetY+515 433 1]);    
0692     uicontrol(handles.figRehoMain,'Style','Text','Position',[142 offsetY+505 180 14],...
0693         'String','Option: Remove Linear Trend');
0694     
0695     
0696     %20070506, Add manual operation button groups like SPM
0697     uicontrol(handles.figRehoMain, 'Style','Frame','Position',[2 offsetY-25 433 1]);    
0698     uicontrol(handles.figRehoMain,'Style','Text','Position',[152 offsetY-30 140 14],...
0699         'String','Manual Operations');
0700     
0701 
0702 function InitControlProperties(hObject, handles)
0703     %for Linux compatible 20070507 dawnsong
0704     % --- FIGURE -------------------------------------
0705     set(handles.figRehoMain,...
0706         'Units', 'pixels', ...
0707         'Position', [10 5 440 600], ...
0708         'Name', 'reho_gui', ...
0709         'MenuBar', 'none', ...
0710         'NumberTitle', 'off', ...        
0711         'Color', get(0,'DefaultUicontrolBackgroundColor'));
0712 
0713     % --- STATIC TEXTS -------------------------------------
0714     set(handles.text2,    ...
0715         'Style', 'text', ...
0716         'Units', 'pixels', ...
0717         'Position', [16 186 91 40], ...
0718         'FontSize', 24, ...
0719         'FontWeight', 'bold', ...
0720         'String', 'ReHo');
0721 
0722     set(handles.text4,    ...
0723         'Style', 'text', ...
0724         'Units', 'pixels', ...
0725         'Position', [8 109 80 21], ...
0726         'String', 'Directory:');
0727 
0728     set(handles.txtInputDir,    ...
0729         'Style', 'text', ...
0730         'Units', 'pixels', ...
0731         'Position', [7 336 80 21], ...
0732         'String', 'Data Directory:');
0733 
0734     set(handles.text5,    ...
0735         'Style', 'text', ...
0736         'Units', 'pixels', ...
0737         'Position', [9 140 80 21], ...
0738         'String', 'Prefix:');
0739 
0740     set(handles.txtResultFilename,    ...
0741         'Style', 'text', ...
0742         'Units', 'pixels', ...
0743         'Position', [227 148 200 16], ...
0744         'HorizontalAlignment','left'         , ...
0745         'String', 'Result: Prefix_DirectoryName.{hdr/img}');
0746 
0747     set(handles.text7,    ...
0748         'Style', 'text', ...
0749         'Units', 'pixels', ...
0750         'Position', [144 476 25 51], ...
0751         'FontSize', 28, ...        
0752         'String', '~');
0753 
0754     set(handles.text8,    ...
0755         'Style', 'text', ...
0756         'Units', 'pixels', ...
0757         'Position', [230 491 45 16], ...
0758         'String', 'TR: (s)');
0759 
0760     % --- PUSHBUTTONS -------------------------------------
0761     set(handles.btnSelectOutputDir,    ...
0762         'Style', 'pushbutton', ...
0763         'Units', 'pixels', ...
0764         'Position', [396 107 30 25], ...
0765         'FontSize', 18, ...
0766         'String', '...');
0767 
0768     set(handles.btnSelectDataDir,    ...
0769         'Style', 'pushbutton', ...
0770         'Units', 'pixels', ...
0771         'Position', [396 334 30 25], ...
0772         'FontSize', 18, ...
0773         'String', '...', ...
0774         'CData', zeros(1,0));
0775 
0776     set(handles.btnSelectMask,    ...
0777         'Style', 'pushbutton', ...
0778         'Units', 'pixels', ...
0779         'Position', [396 240 30 25], ...
0780         'FontSize', 18, ...
0781         'String', '...', ...
0782         'Enable', 'off', ...
0783         'CData', zeros(1,0));
0784 
0785     set(handles.btnComputeReho,    ...
0786         'Style', 'pushbutton', ...
0787         'Units', 'pixels', ...
0788         'Position', [316 186 110 33], ...
0789         'FontSize', 12, ...
0790         'FontWeight', 'bold', ...
0791         'String', 'Do all');
0792 
0793     
0794     set(handles.btnDetrend , ...
0795         'Style', 'pushbutton', ...
0796         'Units', 'pixels', ...
0797         'Position', [337 553 90 25], ...
0798         'FontSize', 10, ...
0799         'Enable', 'off', ...   %Update filter and detrend button's state according to Option: detrend/Filter 20070820
0800         'String', 'Detrend');
0801     set(handles.btnBandPass , ...
0802         'Style', 'pushbutton', ...
0803         'Units', 'pixels', ...
0804         'Position', [337 490 90 25], ...
0805         'FontSize', 10, ...
0806         'Enable', 'off', ...  %Update filter and detrend button's state according to Option: detrend/Filter 20070820
0807         'String', 'Filter');
0808     set(handles.btnHelp,    ...
0809         'Style', 'pushbutton', ...
0810         'Units', 'pixels', ...
0811         'Position', [10 10 90 33], ...
0812         'FontSize', 10, ...
0813         'String', 'Help');    
0814     set(handles.btnDivideMean,    ...
0815         'Style', 'pushbutton', ...
0816         'Units', 'pixels', ...
0817         'Position', [110 10 90 33], ...
0818         'FontSize', 10, ...
0819         'String', 'Divide Mean');
0820     set(handles.btnSliceViewer,    ...
0821         'Style', 'pushbutton', ...
0822         'Units', 'pixels', ...
0823         'Position', [210 10 90 33], ...
0824         'FontSize', 10, ...
0825         'String', 'Slice Viewer');    
0826     set(handles.btnWaveGraph,    ...
0827         'Style', 'pushbutton', ...
0828         'Units', 'pixels', ...
0829         'Position', [314 10 110 33], ...
0830         'FontSize', 10, ...
0831         'String', 'Power Spectrum');
0832         
0833         
0834     % --- RADIO BUTTONS -------------------------------------
0835     set(handles.rbtn7voxels,    ...
0836         'Style', 'radiobutton', ...
0837         'Units', 'pixels', ...
0838         'Position', [10 298 70 16], ...
0839         'String', '7 voxels');
0840 
0841     set(handles.rbtn19voxels,    ...
0842         'Style', 'radiobutton', ...
0843         'Units', 'pixels', ...
0844         'Position', [10 273 70 16], ...
0845         'String', '19 voxels');
0846 
0847     set(handles.rbtn27voxels,    ...
0848         'Style', 'radiobutton', ...
0849         'Units', 'pixels', ...
0850         'Position', [10 248 70 16], ...
0851         'String', '27 voxels');
0852 
0853     set(handles.rbtnDefaultMask,    ...
0854         'Style', 'radiobutton', ...
0855         'Units', 'pixels', ...
0856         'Position', [110 297 158 16], ...
0857         'String', 'Default mask');
0858 
0859     set(handles.rbtnUserMask,    ...
0860         'Style', 'radiobutton', ...
0861         'Units', 'pixels', ...
0862         'Position', [110 271 148 16], ...
0863         'String', 'User''s defined mask');
0864 
0865     set(handles.rbtnNullMask,    ...
0866         'Style', 'radiobutton', ...
0867         'Units', 'pixels', ...
0868         'Position', [277 298 82 16], ...
0869         'String', 'No mask');
0870 
0871     % --- CHECKBOXES -------------------------------------
0872     set(handles.ckboxFilter, ...
0873         'Style', 'checkbox', ...
0874         'Units', 'pixels', ...
0875         'Position', [14 491 80 22], ...
0876         'String', 'Band (Hz)');
0877     set(handles.ckboxRetrend, ...        
0878         'Style', 'checkbox', ...
0879         'Units', 'pixels', ...
0880         'Visible', 'off', ...
0881         'Position', [366 490 60 22], ...
0882         'String', 'Retrend');
0883     set(handles.ckboxDivideMean,    ...
0884         'Style', 'checkbox', ...
0885         'Units', 'pixels', ...
0886         'Position', [12 82 430 19], ...
0887         'String', 'Divide ReHo brain by the mean within the mask (mPrefix_DirectoryName.{hdr/img})');
0888     set(handles.ckboxRemoveTrendBefore, ...
0889         'Style', 'checkbox', ...
0890         'Units', 'pixels', ...
0891         'Position', [13 555 140 21],...
0892         'String', 'detrend');        %'String', 'detrend BEFORE Filter');
0893     set(handles.ckboxRemoveTrendAfter, ...
0894         'Style', 'checkbox', ...
0895         'Units', 'pixels', ...
0896         'Position', [171 555 140 21],...
0897         'Visible', 'off', ...
0898         'String', 'detrend AFTER Filter');
0899 
0900         
0901     % --- EDIT TEXTS -------------------------------------
0902     set(handles.edtOutputDir,    ...
0903         'Style', 'edit', ...
0904         'Units', 'pixels', ...
0905         'Position', [94 109 300 23], ...
0906         'BackgroundColor', [1 1 1], ...
0907         'String', 'Edit Text');
0908 
0909     set(handles.edtDataDirectory,    ...
0910         'Style', 'edit', ...
0911         'Units', 'pixels', ...
0912         'Position', [94 336 300 22], ...
0913         'BackgroundColor', [1 1 1], ...
0914         'String', '');
0915 
0916     set(handles.edtMaskfile,    ...
0917         'Style', 'edit', ...
0918         'Units', 'pixels', ...
0919         'Position', [94 240 300 23], ...
0920         'BackgroundColor', [1 1 1], ...
0921         'String', 'Edit Text', ...
0922         'Enable', 'off');
0923 
0924     set(handles.edtPrefix,    ...
0925         'Style', 'edit', ...
0926         'Units', 'pixels', ...
0927         'Position', [94 142 115 22], ...
0928         'BackgroundColor', [1 1 1], ...
0929         'String', 'RehoMap');
0930 
0931     set(handles.edtBandLow,    ...
0932         'Style', 'edit', ...
0933         'Units', 'pixels', ...
0934         'Position', [94 491 50 22], ...
0935         'BackgroundColor', [1 1 1], ...
0936         'String', '0.01', ...
0937         'Enable', 'off');
0938 
0939     set(handles.edtBandHigh,    ...
0940         'Style', 'edit', ...
0941         'Units', 'pixels', ...
0942         'Position', [171 491 50 22], ...
0943         'BackgroundColor', [1 1 1], ...
0944         'String', '0.08', ...
0945         'Enable', 'off');
0946 
0947     set(handles.edtSamplePeriod,    ...
0948         'Style', 'edit', ...
0949         'Units', 'pixels', ...
0950         'Position', [276 491 50 22], ...
0951         'BackgroundColor', [1 1 1], ...
0952         'String', '2', ...
0953         'Enable', 'off');
0954 
0955     % --- LISTBOXES -------------------------------------
0956     set(handles.listDataDirs,    ...
0957         'Style', 'listbox', ...
0958         'Units', 'pixels', ...
0959         'Position', [14 363 413 98], ...
0960         'BackgroundColor', [1 1 1], ...
0961         'String', '');
0962 
0963     %20071103, Add context menu to Input Data Directories to add¡¢delete¡¢export¡¢import¡­¡­
0964     handles.hContextMenu =uicontextmenu;
0965     set(handles.listDataDirs, 'UIContextMenu', handles.hContextMenu);    
0966     uimenu(handles.hContextMenu, 'Label', 'Add a directory', 'Callback', get(handles.btnSelectDataDir, 'Callback'));    
0967     uimenu(handles.hContextMenu, 'Label', 'Remove selected directory', 'Callback', 'reho_gui(''DeleteSelectedDataDir'',gcbo,[], guidata(gcbo))');
0968     uimenu(handles.hContextMenu, 'Label', 'Add recursively all sub-folders of a directory', 'Callback', 'reho_gui(''RecursiveAddDataDir'',gcbo,[], guidata(gcbo))');
0969     uimenu(handles.hContextMenu, 'Label', '=============================');    
0970     uimenu(handles.hContextMenu, 'Label', 'Clear all data directories', 'Callback', 'reho_gui(''ClearDataDirectories'',gcbo,[], guidata(gcbo))');
0971     
0972         
0973     % Save handles structure
0974     guidata(hObject,handles);
0975 
0976 %% Log options to a log file for further investigation, 20070507
0977 function Log2File(handles)
0978     constLineSep= '-------------------------------------------------------------------------------';
0979     [theVer, theRelease] =rest_misc( 'GetRestVersion');
0980     theMsgVersion = sprintf('REST Version:%s, Release %s\r\n%s\r\n', theVer, theRelease, constLineSep);    
0981     theMsgHead = sprintf('ReHo computation log %s\r\n%s\r\n', rest_misc( 'GetDateTimeStr'), constLineSep);
0982     theMsg =sprintf('%s\r\n%s\r\n\r\n%s', theMsgVersion, theMsgHead, constLineSep);
0983     theMsg =sprintf('%s\r\nRemove Linear Trend options:\r\n%s\r\n\r\n%s',theMsg,...
0984                     LogRemoveLinearTrend(handles), constLineSep);
0985     theMsg =sprintf('%s\r\nIdeal Band Pass filter options:\r\n%s\r\n\r\n%s',theMsg,...
0986                     LogBandPassFilter(handles), constLineSep);
0987     theMsg =sprintf('%s\r\nReHo input parameters:\r\n%s\r\n\r\n%s', theMsg, ...
0988                     LogReHoInputParameters(handles), constLineSep);
0989     theMsg =sprintf('%s\r\nReHo output parameters:\r\n%s\r\n\r\n%s', theMsg, ...
0990                     LogReHoOutputParameters(handles), constLineSep);
0991     
0992     fid = fopen(handles.Log.Filename,'w');
0993     if fid~=-1
0994         fprintf(fid,'%s',theMsg);
0995         fclose(fid);
0996     else
0997         errordlg(sprintf('Error to open log file:\n\n%s', handles.Log.Filename));
0998     end
0999 
1000 
1001     
1002 %Log the total elapsed time by once "Do all"
1003 function LogPerformance(handles)    
1004     theMsg =sprintf('\r\n\r\nTotal elapsed time for Regional Homogeneity Computing: %g  seconds\r\n',handles.Performance);
1005     fid = fopen(handles.Log.Filename,'r+');
1006     fseek(fid, 0, 'eof');
1007     if fid~=-1
1008         fprintf(fid,'%s',theMsg);
1009         fclose(fid);
1010     else
1011         errordlg(sprintf('Error to open log file:\n\n%s', handles.Log.Filename));
1012     end
1013 
1014     
1015 function ResultLogString=LogRemoveLinearTrend(handles)
1016     ResultLogString ='';
1017     ResultLogString =sprintf('%s\tremove linear trend BEFORE filter: %s\r\n',ResultLogString, handles.Detrend.BeforeFilter);
1018     %ResultLogString =sprintf('%s\tremove linear trend AFTER filter: %s\r\n',ResultLogString, handles.Detrend.AfterFilter);
1019     
1020 function ResultLogString=LogBandPassFilter(handles)
1021     ResultLogString ='';        
1022     ResultLogString =sprintf('%s\tUse Filter: %s\r\n',ResultLogString, handles.Filter.UseFilter);
1023     ResultLogString =sprintf('%s\tBand Low: %g\r\n', ResultLogString, handles.Filter.BandLow);
1024     ResultLogString =sprintf('%s\tBand High: %g\r\n',ResultLogString, handles.Filter.BandHigh);
1025     ResultLogString =sprintf('%s\tSample Period(i.e. TR): %g\r\n',ResultLogString, handles.Filter.SamplePeriod);
1026     
1027 function ResultLogString=LogReHoInputParameters(handles)
1028     ResultLogString ='';
1029     constLineSep= '-------------------------------------------------------------------------------';
1030     theDataDirString= '';
1031     theDataDirCells =get(handles.listDataDirs, 'string');
1032     for x=1:length(theDataDirCells)
1033         theDataDirString =sprintf('%s\r\n\t%s', theDataDirString, theDataDirCells{x});
1034     end
1035     theDirType ='';
1036     if strcmpi(handles.Detrend.BeforeFilter, 'Yes')
1037         theDirType =sprintf(' %s after Detrend processing', theDirType);
1038     end
1039     if strcmpi(handles.Detrend.BeforeFilter, 'Yes') && ...
1040         strcmpi(handles.Filter.UseFilter, 'Yes'),
1041         theDirType =sprintf(' %s and ', theDirType);
1042     end
1043     if strcmpi(handles.Filter.UseFilter, 'Yes')
1044         theDirType =sprintf(' %s after Filter processing', theDirType);
1045     end
1046     ResultLogString =sprintf('%s\tInput Data Directories( %s): \r\n\t%s%s\r\n\t%s\r\n',ResultLogString,...
1047                             theDirType, ...
1048                             constLineSep, ...
1049                             theDataDirString, ...
1050                             constLineSep);
1051     ResultLogString =sprintf('%s\tMask file: %s\r\n', ResultLogString, handles.Cfg.MaskFile);
1052     ResultLogString =sprintf('%s\tCluster Count: %g\r\n',ResultLogString, handles.Cfg.ClusterCount);    
1053         
1054 function ResultLogString=LogReHoOutputParameters(handles)
1055     ResultLogString ='';
1056     ResultLogString =sprintf('%s\tPrefix to the Data directories: %s\r\n',ResultLogString, get(handles.edtPrefix, 'String'));
1057     ResultLogString =sprintf('\tOutput Prefix: %s\r\n', get(handles.edtPrefix, 'String'));
1058     ResultLogString =sprintf('%s\tOutput Data Directories: %s\r\n',ResultLogString, handles.Cfg.OutputDir);
1059     ResultLogString =sprintf('%s\tWant mean ReHo map computation: %s \r\n',ResultLogString, handles.Cfg.WantMeanRehoMap);
1060     
1061 
1062     
1063     
1064 %compose the log filename
1065 function ResultLogFileName=GetLogFilename(ALogDirectory, APrefix)
1066     if isempty(ALogDirectory)
1067         [pathstr, name, ext, versn] = fileparts(mfilename('fullpath'));    
1068         ALogDirectory =pathstr;
1069     end
1070     if ~strcmp(ALogDirectory(end), filesep)
1071         ALogDirectory =[ALogDirectory filesep];
1072     end
1073     ResultLogFileName=sprintf('%s%s_%sReHo.log', ...
1074         ALogDirectory, ...
1075         APrefix, ...
1076         rest_misc( 'GetDateTimeStr'));
1077 
1078 
1079 function btnSliceViewer_Callback(hObject, eventdata, handles)
1080 %Display a brain image like MRIcro
1081     theOldColor=get(hObject,'BackgroundColor');        
1082     set(hObject,'Enable','off', 'BackgroundColor', 'red');
1083     drawnow;
1084     try
1085         rest_sliceviewer;
1086         % [filename, pathname] = uigetfile({'*.img', 'ANALYZE or NIFTI files (*.img)'}, ...
1087                                                         % 'Pick one brain map');
1088         % if any(filename~=0) && ischar(filename) && length(filename)>4 ,    % not canceled and legal
1089             % if ~strcmpi(pathname(end), filesep)%revise pathname to add \ or /
1090                 % pathname = [pathname filesep];
1091             % end
1092             % theBrainMap =[pathname filename];
1093             % rest_sliceviewer('ShowImage', theBrainMap);
1094         % end
1095     catch
1096         rest_misc( 'DisplayLastException');
1097     end    
1098     set(hObject,'Enable','on', 'BackgroundColor', theOldColor);
1099     drawnow;
1100 
1101 
1102 function btnWaveGraph_Callback(hObject, eventdata, handles)
1103     %Display a brain image like MRIcro, and show specific voxel's time course and its freq domain's fluctuation
1104     theOldColor=get(hObject,'BackgroundColor');        
1105     set(hObject,'Enable','off', 'BackgroundColor', 'red');
1106     drawnow;
1107     try
1108         % if strcmpi(handles.Filter.UseFilter, 'Yes'),
1109         % else
1110             % msgbox(sprintf('You didn''t select option "Band Pass" before show Wave Graph. \n\nYou may enable it first and then you can set Band parameter and TR.'));
1111         % end
1112         [filename, pathname] = uigetfile({'*.img', 'ANALYZE or NIFTI files (*.img)'}, ...
1113                                                         'Pick one functional EPI brain map in the dataset''s directory');
1114         if any(filename~=0) && ischar(filename) && length(filename)>4 ,    % not canceled and legal
1115             if ~strcmpi(pathname(end), filesep)%revise pathname to remove extension
1116                 pathname = [pathname filesep];
1117             end
1118             theBrainMap     =[pathname filename];            
1119             theViewer =rest_sliceviewer('ShowImage', theBrainMap);
1120             
1121             %Set the ALFF figure to show corresponding voxel's time-course and its freq amplitude
1122             theDataSetDir     =pathname;
1123             theVoxelPosition=rest_sliceviewer('GetPosition', theViewer);
1124             theSamplePeriod =handles.Filter.SamplePeriod;
1125             theBandRange    =[handles.Filter.BandLow, handles.Filter.BandHigh];                        
1126             rest_powerspectrum('ShowFluctuation', theDataSetDir, theVoxelPosition, ...
1127                             theSamplePeriod, theBandRange);
1128                             
1129             %Update the Callback
1130             theCallback     ='';
1131             cmdDataSetDir    =sprintf('theDataSetDir= ''%s'';', theDataSetDir);
1132             cmdBrainMap     =sprintf('theVoxelPosition=rest_sliceviewer(''GetPosition'', %g);', theViewer);
1133             cmdSamplePeriod =sprintf('theSamplePeriod= %g;', handles.Filter.SamplePeriod);
1134             cmdBandRange    =sprintf('theBandRange= [%g, %g];', handles.Filter.BandLow, handles.Filter.BandHigh);
1135             cmdUpdateWaveGraph    ='rest_powerspectrum(''ShowFluctuation'', theDataSetDir, theVoxelPosition, theSamplePeriod, theBandRange);';
1136             theCallback    =sprintf('%s\n%s\n%s\n%s\n%s\n',cmdDataSetDir, ...
1137                                 cmdBrainMap, cmdSamplePeriod, cmdBandRange, ...
1138                                 cmdUpdateWaveGraph);
1139             cmdClearVar ='clear theDataSetDir theVoxelPosition theSamplePeriod theBandRange;';
1140             rest_sliceviewer('UpdateCallback', theViewer, [theCallback cmdClearVar], 'ALFF Analysis');
1141             
1142             % Update some Message
1143             theMsg =sprintf('TR( s): %g\nBand( Hz): %g~%g', ...
1144                             theSamplePeriod, theBandRange(1), theBandRange(2) );
1145             rest_sliceviewer('SetMessage', theViewer, theMsg);                
1146         end        
1147     catch
1148         rest_misc( 'DisplayLastException');
1149     end    
1150     set(hObject,'Enable','on', 'BackgroundColor', theOldColor);
1151     drawnow;
1152     rest_waitbar;
1153 
1154 
1155 function ckboxRemoveTrendBefore_Callback(hObject, eventdata, handles)
1156     if get(hObject,'Value')
1157         handles.Detrend.BeforeFilter ='Yes';
1158     else    
1159         handles.Detrend.BeforeFilter ='No';
1160     end    
1161     guidata(hObject, handles);
1162     UpdateDisplay(handles);
1163 
1164 function ckboxRemoveTrendAfter_Callback(hObject, eventdata, handles)
1165     if get(hObject,'Value')
1166         handles.Detrend.AfterFilter ='Yes';
1167     else    
1168         handles.Detrend.AfterFilter ='No';
1169     end    
1170     guidata(hObject, handles);
1171     UpdateDisplay(handles);
1172 
1173 
1174 % --- Executes on button press in btnDetrend.
1175 function btnDetrend_Callback(hObject, eventdata, handles)
1176     theOldColor=get(hObject,'BackgroundColor');        
1177     set(hObject,'Enable','off', 'BackgroundColor', 'red');
1178     drawnow;
1179     try
1180         Detrend(hObject,handles);
1181         msgbox('Remove the Linear Trend Over.',...
1182                 'Detrend successfully' ,'help');
1183     catch
1184         rest_misc( 'DisplayLastException');
1185     end
1186     rest_waitbar;    
1187     set(hObject,'Enable','on', 'BackgroundColor', theOldColor);
1188     drawnow;
1189     
1190 function Detrend(hObject,handles)
1191     for x=1:size(handles.Cfg.DataDirs, 1)    
1192         %Update display
1193         set(handles.listDataDirs, 'Value', x);
1194         drawnow;
1195         if size(handles.Cfg.DataDirs, 1)>1,
1196             rest_waitbar((x-1)/size(handles.Cfg.DataDirs, 1)+0.01, ...
1197                     handles.Cfg.DataDirs{x, 1}, ...
1198                     'Removing the Linear Trend','Parent');
1199         end
1200         
1201         rest_detrend(handles.Cfg.DataDirs{x, 1}, '_detrend');
1202         
1203         %Revise the data directories
1204         handles.Cfg.DataDirs{x, 1}=[handles.Cfg.DataDirs{x, 1} , '_detrend'];
1205         guidata(hObject, handles);    % Save Dir names
1206     end    
1207     UpdateDisplay(handles);
1208 
1209 function BandPass(hObject, handles)
1210     for x=1:size(handles.Cfg.DataDirs, 1)    
1211         %Update display
1212         set(handles.listDataDirs, 'Value', x);
1213         drawnow;
1214         if size(handles.Cfg.DataDirs, 1)>1,
1215             rest_waitbar((x-1)/size(handles.Cfg.DataDirs, 1)+0.01, ...
1216                     handles.Cfg.DataDirs{x, 1}, ...
1217                     'Band Pass filter','Parent');
1218         end
1219                 
1220         rest_bandpass(handles.Cfg.DataDirs{x, 1}, ...
1221                       handles.Filter.SamplePeriod, ...                                  
1222                       handles.Filter.BandHigh, ...
1223                       handles.Filter.BandLow, ...
1224                       handles.Filter.Retrend, ...
1225                       handles.Cfg.MaskFile);
1226         %build the postfix for filtering
1227         thePostfix ='_filtered';                    
1228         %Revise the data directories
1229         handles.Cfg.DataDirs{x, 1}=[handles.Cfg.DataDirs{x, 1} , thePostfix];
1230         guidata(hObject, handles);    % Save Dir names
1231     end
1232     UpdateDisplay(handles);
1233 
1234 function btnHelp_Callback(hObject, eventdata, handles)
1235     web('http://resting-fmri.sourceforge.net');
1236     %web (sprintf('%s/man/English/ReHo/index.html', rest_misc( 'WhereIsREST')), '-helpbrowser');
1237

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