Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable Single Regression in RSNRegression #170

Merged
merged 2 commits into from
Apr 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions global/scripts/RSNregression.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function RSNregression(InputFile, InputVNFile, GroupMaps, Method, ParamsFile, VAWeightsName, OutputBeta, varargin)
optional = myargparse(varargin, {'VolInputFile' 'VolInputVNFile' 'VolCiftiTemplate' 'OldBias' 'OldVolBias' 'GoodBCFile' 'VolGoodBCFile' 'SpectraParams' 'OutputZ' 'OutputVolBeta' 'OutputVolZ' 'SurfString' 'ScaleFactor' 'WRSmoothingSigma'});
function RSNregression(InputFile, InputVNFile, Method, ParamsFile, OutputBeta, varargin)
optional = myargparse(varargin, {'GroupMaps' 'VAWeightsName' 'VolInputFile' 'VolInputVNFile' 'VolCiftiTemplate' 'OldBias' 'OldVolBias' 'GoodBCFile' 'VolGoodBCFile' 'SpectraParams' 'OutputZ' 'OutputVolBeta' 'OutputVolZ' 'SurfString' 'ScaleFactor' 'WRSmoothingSigma'});

%InputFile - text file containing filenames of timeseries to concatenate
%InputVNFile - text file containing filenames of the variance maps of each input
Expand Down Expand Up @@ -135,9 +135,11 @@ function RSNregression(InputFile, InputVNFile, GroupMaps, Method, ParamsFile, VA
clear vnvolsum;
end

GroupMapcii = ciftiopen(GroupMaps, wbcommand);
weightscii = ciftiopen(VAWeightsName, wbcommand); %normalized vertex areas, voxels are all 1s
AreaWeights = weightscii.cdata;
if strcmp(Method,'weighted') || strcmp(Method,'dual')
GroupMapcii = ciftiopen(optional.GroupMaps, wbcommand);
weightscii = ciftiopen(optional.VAWeightsName, wbcommand); %normalized vertex areas, voxels are all 1s
AreaWeights = weightscii.cdata;
end
switch Method
case 'weighted'
if strcmp(optional.WRSmoothingSigma, '')
Expand Down Expand Up @@ -174,15 +176,21 @@ function RSNregression(InputFile, InputVNFile, GroupMaps, Method, ParamsFile, VA
[betaICA, NODEts] = weightedDualRegression(normalise(betaICAone), inputConcat, AreaWeights);
case 'dual'
[betaICA, NODEts] = weightedDualRegression(GroupMapcii.cdata, inputConcat, AreaWeights);
case 'single'
SpectraArray = textscan(optional.SpectraParams, '%s', 'Delimiter', {'@'});
InputSpectraTS = SpectraArray{1}{1};
NODEts=ciftiopen(InputSpectraTS, wbcommand);
NODEts=NODEts.cdata';
betaICA = ((pinv(normalise(NODEts)) * demean(inputConcat')))';
otherwise
error(['unrecognized method: "' Method '", use "weighted" or "dual"']);
error(['unrecognized method: "' Method '", use "weighted", "dual", or "single"']);
end

NODEtsnorm = normalise(NODEts);

%outputs
%Save Timeseries and Spectra if Desired
if ~strcmp(optional.SpectraParams, '')
if ~strcmp(optional.SpectraParams, '') && ~strcmp(Method,'single')
SpectraArray = textscan(optional.SpectraParams, '%s', 'Delimiter', {'@'});
nTPsForSpectra = min(str2double(SpectraArray{1}{1}), size(NODEts, 1));
OutputSpectraTS = SpectraArray{1}{2};
Expand Down
39 changes: 30 additions & 9 deletions global/scripts/RSNregression.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ PARAMETERs are [ ] = optional; < > = user supplied value
#arguments to opts_Add*: switch, variable to set, name for inside of <> in help text, description, [default value other than empty string if AddOptional], [compatibility flag, ...]
opts_AddMandatory '--study-folder' 'StudyFolder' 'path' "folder containing all subjects"
opts_AddMandatory '--subject' 'Subject' 'subject ID' ""
opts_AddMandatory '--group-maps' 'GroupMaps' 'file' "the group template maps to regress"
opts_AddOptional '--group-maps' 'GroupMaps' 'file' "the group template spatial maps for weighted or dual regression"
opts_AddOptional '--timeseries' 'Timeseries' 'file' "the timeseries for single regression"
opts_AddMandatory '--subject-timeseries' 'InputList' 'fmri@fmri@fmri...' "the timeseries fmri names to concatenate"
opts_AddOptional '--surf-reg-name' 'RegName' 'name' "the registration string corresponding to the input files"
opts_AddMandatory '--low-res' 'LowResMesh' 'meshnum' "mesh resolution, like '32' for 32k_fs_LR"
opts_AddMandatory '--proc-string' 'ProcString' 'string' "part of filename describing processing, like '_hp2000_clean'"
opts_AddMandatory '--method' 'Method' 'regression method' "'weighted' or 'dual' - weighted regression finds locations in the subject that don't match the template well and downweights them; dual is simpler, both methods use vertex area information"
opts_AddMandatory '--method' 'Method' 'regression method' "'weighted', 'dual', or 'single' - weighted regression finds locations in the subject that don't match the template well and downweights them; dual is simpler, both methods use vertex area information. single temporal regression requires a prior run of weighted or dual regression and is intended for adding an additional output space"
opts_AddOptional '--weighted-smoothing-sigma' 'WRSmoothingSigma' 'number' "default 14 for human data - when using --method=weighted, the smoothing sigma, in mm, to apply to the 'alignment quality' weighting map" '14'
opts_AddOptional '--low-ica-dims' 'LowICADims' 'num@num@num...' "when using --method=weighted, the low ICA dimensionality files to use for determining weighting"
opts_AddOptional '--low-ica-template-name' 'ICATemplateName' 'filename' "filename template where 'REPLACEDIM' will be replaced by each of the --low-ica-dims values in turn to form the low-dim inputs"
Expand All @@ -55,6 +56,7 @@ opts_AddOptional '--matlab-run-mode' 'MatlabMode' '0, 1, or 2' "defaults to $g_m
0 = compiled MATLAB (not implemented)
1 = interpreted MATLAB
2 = Octave" "$g_matlab_default_mode"

opts_ParseArguments "$@"

if ((pipedirguessed))
Expand Down Expand Up @@ -160,6 +162,13 @@ case "$Method" in
(dual)
MethodStr="DR"
;;
(single)
if [ -e ${Timeseries} ] ; then
MethodStr="SR"
else
log_Err_Abort "single method requires a prior run of weighted or dual"
fi
;;
(*)
log_Err_Abort "unknown method string: '$Method'"
;;
Expand All @@ -183,10 +192,13 @@ then
fi
fi
SpectraParams=""
if ((nTPsForSpectra > 0))
if [[ nTPsForSpectra -gt 0 ]] && [[ ! ${Method} == "single" ]]
then
#these files are temporary anyway
SpectraParams="$nTPsForSpectra@$DownSampleMNIFolder/${Subject}.${OutString}_${MethodStr}${RegString}_ts.${LowResMesh}k_fs_LR.txt@$DownSampleMNIFolder/${Subject}.${OutString}_${MethodStr}${RegString}_spectra.${LowResMesh}k_fs_LR.txt"
elif [[ ${Method} == "single" ]]
then
SpectraParams="${Timeseries}"
fi
#the msmall script should do this for itself instead
#OutputNorm=""
Expand All @@ -198,18 +210,27 @@ fi

#fix the _va_norm file being surface-only
#extract the all-voxels ROI file and use it in -from-template
tempfile="$(mktemp --tmpdir XXXXXX.roi.nii.gz)"
tempfiles_add "$tempfile" "$tempfile.junk.nii.gz" "$tempfile.91k.dscalar.nii"
wb_command -cifti-separate "$GroupMaps" COLUMN -volume-all "$tempfile.junk.nii.gz" -roi "$tempfile" -crop
wb_command -cifti-create-dense-from-template "$GroupMaps" "$tempfile.91k.dscalar.nii" -cifti "$VANormOnlySurf" -volume-all "$tempfile" -from-cropped
if [[ ! ${Method} == "single" ]]
then
tempfile="$(mktemp --tmpdir XXXXXX.roi.nii.gz)"
tempfiles_add "$tempfile" "$tempfile.junk.nii.gz" "$tempfile.91k.dscalar.nii"
wb_command -cifti-separate "$GroupMaps" COLUMN -volume-all "$tempfile.junk.nii.gz" -roi "$tempfile" -crop
wb_command -cifti-create-dense-from-template "$GroupMaps" "$tempfile.91k.dscalar.nii" -cifti "$VANormOnlySurf" -volume-all "$tempfile" -from-cropped
fi

case "$MatlabMode" in
(0)
log_Err_Abort "FIXME: compiled matlab support not yet implemented"
;;
(1 | 2)
#SurfString and WRSmoothingSigma are optional to the matlab function (needed only for weighted), but we will always have it, so always providing it is simpler here
matlab_args="'$tempname.input.txt', '$tempname.inputvn.txt', '$GroupMaps', '$Method', '$tempname.params.txt', '$tempfile.91k.dscalar.nii', '$OutBeta', 'SurfString', '$SurfString', 'WRSmoothingSigma', '$WRSmoothingSigma'"
matlab_args="'$tempname.input.txt', '$tempname.inputvn.txt', '$Method', '$tempname.params.txt', '$OutBeta', 'SurfString', '$SurfString', 'WRSmoothingSigma', '$WRSmoothingSigma'"

if [[ ! ${Method} == "single" ]]
then
matlab_args+=", 'GroupMaps', '$GroupMaps'"
matlab_args+=", 'VAWeightsName', '$tempfile.91k.dscalar.nii'"
fi
if ((DoZ))
then
matlab_args+=", 'OutputZ', '$OutputZ'"
Expand Down Expand Up @@ -247,7 +268,7 @@ case "$MatlabMode" in
;;
esac

if [[ "$SpectraParams" != "" ]]
if [[ "$SpectraParams" != "" ]] && [[ ! ${Method} == "single" ]]
then
wb_command -file-information "$GroupMaps" -only-map-names > "$tempname.mapnames.txt"
TR=$(wb_command -file-information "$SpectraTRFile" -only-step-interval)
Expand Down