Created
April 1, 2019 21:21
-
-
Save danlurie/af4304a6f33a604b78063a5447811f62 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function [outputBnormFiles] = backNormROI(structuralImage,outputSpaceImage,toBackNormROIs,outputPrefix,outputDirectory,varargin) | |
% INPUTS: | |
% structuralDirectory - directory for structural - generate norm files here | |
% structuralImage - image that will be normalized | |
% meanEpiFile - image for the mean of the functionals | |
% preprocessingDirectory - make a directory for the backNormROIs | |
% toBackNormROIs - images that will be backnormalized | |
% assume MNI spaces | |
% outputPrefix - append this prefix to the backNormROIs | |
% | |
% OPTIONAL INPUTS: | |
% restart = 1/0 default is 0, this will delete any existing ROIs | |
% | |
% FLOW OF SCRIPTS | |
% 1. Normalize the structural image | |
% generate norm and inverse norm parameters | |
% | |
% 2. Back-normalize the Atlas ROIs into structural space | |
% | |
% 3. Binarize the Atlas ROI in structural space | |
% | |
% 4. Resample the ROI from Structural into EPI space | |
% | |
% OPTIONAL INPUTS | |
if length(varargin)>=1 | |
restart = varargin{1}; | |
else | |
restart = 0; | |
end | |
% Number of ROIs to be back-normalized | |
numROIs = length(toBackNormROIs); | |
outputBnormFiles = cell(1,numROIs); | |
% ROIs saved in sub directory within structural directory | |
[structuralDirectory,~,~] = fileparts(structuralImage); | |
structuralDirectory = [structuralDirectory '/']; | |
%% Step 1: estimate the noramlization of the structural image | |
% get the inverse normalization estimate as well | |
inverseDeformPrefix = 'iy_'; | |
inverseDeformationFndr = dir([structuralDirectory inverseDeformPrefix '*']); | |
if isempty(inverseDeformationFndr) | |
fprintf('Running Segmentation\n'); | |
clear matlabbatch | |
spm_jobman('initcfg'); | |
toSegmentStructFile = dir([structuralDirectory 'ons*.nii']); | |
if isempty(toSegmentStructFile) | |
toSegmentStructFile = dir([structuralDirectory 'sub*.nii']); | |
end | |
matlabbatch{1}.spm.spatial.preproc.channel(1).vols = {[structuralDirectory toSegmentStructFile(1).name]}; | |
% mean bias corrected image is written out | |
matlabbatch{1}.spm.spatial.preproc.channel.write = [0 1]; | |
% change defaults to generate [inverse forward] normalization | |
matlabbatch{1}.spm.spatial.preproc.warp.write = [1 1]; | |
spm_jobman('run',matlabbatch); | |
inverseDeformationFndr = dir([structuralDirectory inverseDeformPrefix '*']); | |
end | |
inverseDeformationFile = [structuralDirectory inverseDeformationFndr(1).name]; | |
%% STEP 2: Apply inverse normalization | |
for roiIdx = 1:numROIs | |
toBnormFile = toBackNormROIs{roiIdx}; | |
[~,fileName,ext]=fileparts(toBnormFile); | |
outputFile = [outputDirectory outputPrefix fileName ext]; | |
outputBnormFiles{roiIdx} = outputFile; | |
if restart && exist(outputFile,'file')==2 | |
delete(outputFile); | |
end | |
if exist(outputFile,'file')~=2 | |
clear matlabbatch | |
spm_jobman('initcfg'); | |
matlabbatch{1}.spm.util.defs.comp{1}.inv.comp{1}.def = {inverseDeformationFile}; | |
matlabbatch{1}.spm.util.defs.comp{1}.inv.space = {structuralImage}; | |
matlabbatch{1}.spm.util.defs.out{1}.push.fnames = {toBnormFile}; | |
matlabbatch{1}.spm.util.defs.out{1}.push.weight = {''}; | |
matlabbatch{1}.spm.util.defs.out{1}.push.savedir.saveusr = {outputDirectory}; | |
matlabbatch{1}.spm.util.defs.out{1}.push.fov.file = {outputSpaceImage}; | |
matlabbatch{1}.spm.util.defs.out{1}.push.preserve = 0; | |
matlabbatch{1}.spm.util.defs.out{1}.push.fwhm = [0 0 0]; | |
spm_jobman('run',matlabbatch); | |
fprintf('Done\n') | |
movefile([outputDirectory 'w' fileName ext],outputFile); | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment