for anyone doing ROI analysis and extracting values from individual subject data, hopefully this script will save you valuable time.
the script Rick R. proposed in the previous post didn't quite work for me, but he graciously spent some high quality time with me today and below is the script we came up with, which works beautifully. Thanks Rick!
why use this script?
Scenario 1: you've extracted time series from each subject for a bunch of ROI's and you want to plot group averaged histograms for each condition in each ROI.
Scenario 2: you extracted time series from a group-averaged IRF, and you want to add error bars to your plots
Scenario X: i'm sure you can think of other uses.
the script assumes you already have 3dROIstats output for each subject in your group, and that output contains data for multiple ROI's (you can modify the script for any # ROI's, # subjects, # conditions). for each subject, 3dROIstats gives you the Mean across the voxels in an ROI for each timepoint in your timeseries. the goal here is, to do the following for each ROI: 1) for each subject, calculate the mean of the timepoints for each condition (one mean per condition); 2) for each condition, calculate the mean across subjects; 3) calculate the stdev & sem across subjects.
after reading in the 3dROIstats file for each subject the final output (as the script appears here) will be one file per ROI (4 ROI's) with 3 columns and 4 rows: the 3 columns representing mean, stdev & sem across subjects; each of represents a different condition.
of course, you will need to modify it to fit your experiment. feel free to post with any questions. i hope this helps at least one other afnirian.
# first run 3dROIstats for each subj
# for each subject, calculate mean for each condition for each ROI, then average these means across subjects and calculate stdev & SEM across subjects.
# step 1: for each subject's ROIstats file, loop through each column that contains an ROI Mean and calculate a condition mean across timepoints of each condition (in this example, condition 1 has 10 timepoints [rows 1..11], condition 2 has 10 timepoints [rows 16..26], etc.)). write out a separate 1D file for the mean of each condition for each ROI.
set roi_names = (ROI.1 ROI.2 ROI.3 ROI.4)
set cols = (2 5 8 11)
foreach index(1 2 3 4)
set col = $cols[$index]
set roi = $roi_names[$index]
foreach subj (S1 S2)
3dTstat -mean -prefix m.11.$subj.1D ROIstats.$subj.1D"{1..11}[$col]'"
3dTstat -mean -prefix m.26.$subj.1D ROIstats.$subj.1D"{16..26}[$col]'"
3dTstat -mean -prefix m.41.$subj.1D ROIstats.$subj.1D"{31..41}[$col]'"
3dTstat -mean -prefix m.56.$subj.1D ROIstats.$subj.1D"{46..56}[$col]'"
end
# step 2: concatenate and append means into one file for each ROI: each row is a condition mean and each column is a subject. Remove the the output of step 1.
1dcat m.11.*.1D >! mean.$roi.1D
1dcat m.26.*.1D >> mean.$roi.1D
1dcat m.41.*.1D >> mean.$roi.1D
1dcat m.56.*.1D >> mean.$roi.1D
rm m.??.*.1D
# step 3: calculate mean and standard deviation across subjects for each condition in each ROI. write output to 1D file (one output file per ROI: Column 1 = mean; Column 2 = stdev; each row = one condition)
3dTstat -mean -stdevNOD -prefix mean.stdev.$roi.1D mean.$roi.1D
#step 4: calculate SEM across subjects for each condition in each ROI
1deval \
-a mean.$roi.1D'[0]' \
-b mean.$roi.1D'[1]' \
-expr 'sem(a,b)' \
>! sem.$roi.1D
# step 5: append to output of step 3 as column 3.
1dcat mean.stdev.$roi.1D sem.$roi.1D >! mean.stdev.sem.$roi.1D
end
# list final output files. Each file is for one ROI, and has three columns (mean, stdev, sem) and one row for each condition.
echo mean.stdev.sem.*.1D