0001
0002
0003
0004
0005
0006
0007
0008 GET_FILE_INFO = 0;
0009 LOAD_RAW_DATA = 0;
0010 COMPUTE_ICA = 0;
0011 SAVE_ICA_DATA = 0;
0012 LOAD_ICA_DATA = 0;
0013 COMPUTE_COMPONENTS = 0;
0014 WRITE_COMPONENTS = 0;
0015 WRITE_Z = 0;
0016
0017 rootdir = '/data1/prime_long/';
0018 outdir = './testimages/';
0019
0020 sub_id = '25jan01JP';
0021 run_id = 1;
0022
0023 if GET_FILE_INFO
0024
0025 P = spm_get('Files',fullfile(rootdir,sub_id,'epi', sprintf('run%d',run_id)),'sn*.img');
0026
0027 nvol = size(P,1);
0028
0029
0030 disp(sprintf('Getting info for %d files', nvol))
0031 V = spm_vol(P);
0032 end
0033
0034 if LOAD_RAW_DATA
0035
0036 clear indata
0037 disp(sprintf('Loading data'))
0038 indata = zeros(nvol,prod(V(1).dim(1:3)));
0039
0040 for ivol = 1:nvol
0041 fid = fopen(deblank(P(ivol,:)),'rb','l');
0042 indata(ivol,:) = fread(fid,inf,'int16')';
0043 fclose(fid);
0044 end
0045 end
0046
0047 if COMPUTE_ICA
0048
0049 disp('Running ICA algorithm')
0050 [weights sphere] = runica(indata);
0051 end
0052
0053 if SAVE_ICA_DATA
0054 disp('Saving data')
0055 save(sprintf('%s_ica.mat',sub_id), 'weights','sphere')
0056 end
0057
0058 if LOAD_ICA_DATA
0059 disp('Loading ICA data')
0060 load(sprintf('%s_ica.mat',sub_id))
0061 end
0062
0063 if COMPUTE_COMPONENTS
0064 disp('Computing components')
0065 components = weights * indata;
0066 end
0067
0068 if WRITE_COMPONENTS
0069 disp('Writing components')
0070 for ivol = 1:nvol
0071 outname = fullfile(outdir,sprintf('%s_comp%04d',sub_id,ivol));
0072 Vo(ivol) = V(ivol);
0073 Vo(ivol).fname = outname;
0074 Vo(ivol).descrip = sprintf('ICA component: %d', ivol);
0075
0076 tmp = components(ivol,:);
0077 if WRITE_Z
0078 tmp = (tmp-mean(tmp))/std(tmp);
0079 max_z(ivol) = max(tmp);
0080 min_z(ivol) = min(tmp);
0081 end
0082 Vo(ivol) = spm_write_vol(Vo(ivol), reshape(tmp,Vo(ivol).dim(1:3)));
0083 end
0084 end