Skip to content

Instantly share code, notes, and snippets.

@TamerGezici
Created January 1, 2024 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TamerGezici/2975f23d44995b2c04aec7231017e22e to your computer and use it in GitHub Desktop.
Save TamerGezici/2975f23d44995b2c04aec7231017e22e to your computer and use it in GitHub Desktop.
Single run whole brain decoding example.
dbstop if error
clear all;
% Set defaults
cfg = decoding_defaults;
cfg.analysis = 'searchlight'; % standard alternatives: 'wholebrain', 'ROI' (pass ROIs in cfg.files.mask, see below)
cfg.searchlight.radius = 3; % use searchlight of radius 3 (by default in voxels), see more details below
% Set the output directory where data will be saved, e.g. 'c:\exp\results\buttonpress'
results_dir = fullfile('F:\fr-pr-3\A_block\MVPA\WB_MVPA_consecutive_repeat_switches_nomixed\r1_vs_r2_NEW');
higher_beta_loc = 'F:\fr-pr-3\A_block\GLM_results\MVPA_3A_consecutive_repeats_switches_nomixed\aamod_firstlevel_model_00001'; % where all subs' first level results are
segment_loc = 'C:\Users\user\Desktop\HCFprep\A_block\studyA_results\aamod_segment8_00001';
%subs = dir(fullfile(higher_beta_loc,'sub*'));
%subs = {subs(:).name};
subs = {'sub-03','sub-04','sub-06','sub-11','sub-20','sub-21','sub-22','sub-23','sub-24','sub-25','sub-26','sub-27','sub-28','sub-30','sub-33','sub-35','sub-36','sub-38','sub-40','sub-47','sub-48','sub-49','sub-50','sub-51','sub-52','sub-53','sub-54','sub-55'};
ntot = length(subs);
% Set the filename of your brain mask (or your ROI masks as cell matrix)
% for searchlight or wholebrain e.g. 'c:\exp\glm\model_button\mask.img' OR
% for ROI e.g. {'c:\exp\roi\roimaskleft.img', 'c:\exp\roi\roimaskright.img'}
% You can also use a mask file with multiple masks inside that are
% separated by different integer values (a "multi-mask")
% cfg.files.mask = load(fullfile(beta_loc, 'mask.nii'));
% don't remember the names? -> run display_regressor_names(higher_beta_loc)
combine_count = 2; % How many different types of regressors are we combining?
chunks_per_reg = 2;
chunk_count = chunks_per_reg*combine_count;
labelname1 = 'regexp:repeat_\w_1';% parity, value
labelname2 = 'regexp:repeat_\w_2';
labelvalue1 = 1; % value for labelname1
labelvalue2 = -1; % value for labelname2
% cfg.searchlight.unit = 'mm';
% cfg.searchlight.radius = 12; % if you use this, delete the other searchlight radius row at the top!
cfg.searchlight.spherical = 1;
cfg.verbose = 2; % you want all information to be printed on screen
% cfg.decoding.train.classification.model_parameters = '-s 0 -t 0 -c 1 -b 0 -q';
% Enable scaling min0max1 (otherwise libsvm can get VERY slow)
% if you dont need model parameters, and if you use libsvm, use:
cfg.scale.method = 'min0max1';
cfg.scale.estimation = 'all'; % scaling across all data is equivalent to no scaling (i.e. will yield the same results), it only changes the data range which allows libsvm to compute faster
% if you like to change the decoding software (default: libsvm):
% cfg.decoding.software = 'liblinear'; % for more, see decoding_toolbox\decoding_software\.
% Note: cfg.decoding.software and cfg.software are easy to confuse.
% cfg.decoding.software contains the decoding software (standard: libsvm)
% cfg.software contains the data reading software (standard: SPM/AFNI)
%% Decide whether you want to see the searchlight/ROI/... during decoding
cfg.plot_selected_voxels = 0; % 0: no plotting, 1: every step, 2: every second step, 100: every hundredth step...
%% Add additional output measures if you like
% See help decoding_transform_results for possible measures
cfg.results.output = {'accuracy_minus_chance'}; % 'accuracy_minus_chance' by default
for sub = 1:ntot
csub = subs{sub};
cfg.results.dir = fullfile(results_dir,csub);
beta_loc = fullfile(higher_beta_loc,csub,'stats'); % where individual's betas and mask are
if exist(cfg.results.dir)~=7;
mkdir(cfg.results.dir);
end
cd(cfg.results.dir);
regressor_names = design_from_spm(beta_loc);
% Extract all information for the cfg.files structure (labels will be [1 -1] if not changed above)
cfg = decoding_describe_data(cfg,{labelname1 labelname2},[labelvalue1, labelvalue2],regressor_names,beta_loc);
%cfg.files.chunk = ones(size(cfg.files.chunk,1),1);
%cfg.files.chunk = ones(size(cfg.files.chunk));
session_count = str2double(regexp(cfg.files.descr{end}, '\d', 'match', 'once')) ;
cfg.files.chunk = [1:chunk_count*session_count 1:chunk_count*session_count]';
cfg.design = make_design_cv(cfg);
check{1} = cfg.files.descr';
check{2} = cfg.files.label;
check{3} = cfg.files.chunk;
results = decoding(cfg);
end
% [h,p,ci]=ttest(results.accuracy_minus_chance.output);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment