0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 marsbar('on')
0012
0013
0014
0015 root_dir = spm_get(-1, '', 'Root directory of example data');
0016 sesses = {'sess1','sess2','sess3'};
0017
0018
0019 roi_dir = fullfile(root_dir, 'rois');
0020
0021
0022 if isempty(which('marsbar'))
0023 error('Need MarsBaR on the path');
0024 end
0025 v = str2num(marsbar('ver'));
0026 if v < 0.35
0027 error('Batch script only works for MarsBaR >= 0.35');
0028 end
0029 marsbar('on');
0030
0031
0032
0033 spm_ver = spm('ver');
0034 sdirname = [spm_ver '_ana'];
0035 if strcmp(spm_ver, 'SPM99')
0036 conf_design_name = 'SPMcfg.mat';
0037 else
0038 spm_defaults;
0039 conf_design_name = 'SPM.mat';
0040 end
0041
0042
0043 spm('defaults', 'fmri');
0044
0045
0046 mars_sdir = 'Mars_ana';
0047
0048
0049 sess2_model_dir = fullfile(root_dir, 'sess2', sdirname);
0050 sess2_model = mardo(fullfile(sess2_model_dir, 'SPM.mat'));
0051 if ~is_spm_estimated(sess2_model)
0052 error(['Session 2 model has not been estimated. ' ...
0053 'You may need to run the run_preprocess script']);
0054 end
0055
0056
0057 con_name = 'stim_hrf';
0058 t_con = get_contrast_by_name(sess2_model, con_name);
0059 if isempty(t_con)
0060 error(['Cannot find the contrast ' con_name ...
0061 ' in the design; has it been estimated?']);
0062 end
0063
0064 if isstruct(t_con.Vspm)
0065 t_con_fname = t_con.Vspm.fname;
0066 else
0067 t_con_fname = t_con.Vspm;
0068 end
0069 t_img_name = fullfile(sess2_model_dir, t_con_fname);
0070 if ~exist(t_img_name)
0071 error(['Cannot find t image ' t_img_name ...
0072 '; has it been estimated?']);
0073 end
0074
0075
0076 p_thresh = 0.05;
0077 erdf = error_df(sess2_model);
0078 t_thresh = spm_invTcdf(1-p_thresh, erdf);
0079
0080
0081 V = spm_vol(t_img_name);
0082 img = spm_read_vols(V);
0083 tmp = find(img(:) > t_thresh);
0084 img = img(tmp);
0085 XYZ = mars_utils('e2xyz', tmp, V.dim(1:3));
0086
0087
0088 cluster_nos = spm_clusters(XYZ);
0089 [mx max_index] = max(img);
0090 max_cluster = cluster_nos(max_index);
0091 cluster_XYZ = XYZ(:, cluster_nos == max_cluster);
0092
0093
0094 act_roi = maroi_pointlist(struct('XYZ', cluster_XYZ, ...
0095 'mat', V.mat), 'vox');
0096
0097
0098 box_limits = [-20 20; -66 -106; -20 7]';
0099 box_centre = mean(box_limits);
0100 box_widths = abs(diff(box_limits));
0101 box_roi = maroi_box(struct('centre', box_centre, ...
0102 'widths', box_widths));
0103
0104
0105 trim_stim = box_roi & act_roi;
0106
0107
0108 trim_stim = label(trim_stim, 'batch_trim_stim');
0109
0110
0111 saveroi(trim_stim, fullfile(roi_dir, 'batch_trim_stim_roi.mat'));
0112
0113
0114 save_as_image(trim_stim, fullfile(root_dir, 'batch_trim_stim.img'));
0115
0116
0117
0118 bg_L_name = fullfile(roi_dir, 'MNI_Putamen_L_roi.mat');
0119 bg_R_name = fullfile(roi_dir, 'MNI_Putamen_R_roi.mat');
0120 roi_array{1} = trim_stim;
0121 roi_array{2} = maroi(bg_L_name);
0122 roi_array{3} = maroi(bg_R_name);
0123
0124
0125 pwd_orig = pwd;
0126 sesses = {'sess1', 'sess3'};
0127
0128
0129
0130 event_session_no = 1;
0131 event_type_no = 1;
0132 event_spec = [event_session_no; event_type_no];
0133 event_duration = 0;
0134
0135 clear model_file
0136 for roi_no = 1:length(roi_array)
0137 roi = roi_array{roi_no};
0138 for ss = 1:length(sesses)
0139
0140
0141
0142
0143
0144 if roi_no == 1
0145 model_file{ss} = configure_er_model(root_dir, sesses{ss}, mars_sdir);
0146 end
0147
0148 D = mardo(model_file{ss});
0149
0150
0151 Y = get_marsy(roi, D, 'mean');
0152
0153
0154 E = estimate(D, Y);
0155
0156
0157 [E Ic] = add_contrasts(E, 'stim_hrf', 'T', [1 0 0]);
0158
0159
0160 stat_struct(ss) = compute_contrasts(E, Ic);
0161
0162
0163 [this_tc dt] = event_fitted(E, event_spec, event_duration);
0164
0165
0166 tc(:, ss) = this_tc / block_means(E) * 100;
0167
0168 end
0169
0170
0171
0172
0173 vals = [ [1 3]; [stat_struct(:).con]; [stat_struct(:).stat]; ];
0174 fprintf('Statistics for %s\n', label(roi));
0175 fprintf('Session %d; contrast value %5.4f; t stat %5.4f\n', vals);
0176
0177
0178 figure
0179 secs = [0:length(tc) - 1] * dt;
0180 plot(secs, tc)
0181 title(['Time courses for ' label(roi)], 'Interpreter', 'none');
0182 xlabel('Seconds')
0183 ylabel('% signal change');
0184 legend(sesses)
0185
0186 end