Home > rest_20090422 > fc_gui.m

fc_gui

PURPOSE ^

Functional Connectivity GUI by Xiaowei Song

SYNOPSIS ^

function varargout = fc_gui(varargin)

DESCRIPTION ^

Functional Connectivity 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.2
 $Date    =20080808
-----------------------------------------------------------
     <a href="Dawnwei.Song@gmail.com">Mail to Author</a>: Xiaowei Song
    Version=1.2;
    Release=20080808;
 Modified by GUIDE v2.5 02-Oct-2007 10:20:30
 Revised by YAN Chao-Gan 080808, in order to process multiple subjects;
 with different covaribles in batch mode.
 Last Revised by YAN Chao-Gan 080903, in order to fix the bug that with no Covariables

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

DOWNLOAD ^

fc_gui.m

SOURCE CODE ^

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

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