<jupyter_start><jupyter_text>SPM12 scripting in a Jupyter notebook with an Octave kernel<jupyter_code>%-Welcome
spm('asciiwelcome');
[vspm,rspm] = spm('Ver');
tlkt = ver(spm_check_version);
fprintf('%s, version %s\n',vspm,rspm);
fprintf('%s, version %s\n',tlkt.Name,version);<jupyter_output>___  ____  __  __                                            
/ __)(  _ \(  \/  )                                           
\__ \ )___/ )    (   Statistical Parametric Mapping           
(___/(__)  (_/\/\_)  SPM12 - https://www.fil.ion.ucl.ac.uk/spm/

SPM12, version 7771
Octave, version 4.2.2<jupyter_text>STEP 1: Initialize directory structure for data inputs and outputs<jupyter_code>% Preprocessed and analysed data will be in "results/" folder
data_dir = fullfile(getenv('HOME'),'ds000157-download');
spm_dir = fullfile(getenv('HOME'),'spm12');
results_dir = fullfile(getenv('HOME'),'results');
stats_dir = [results_dir filesep 'stats'];
processing_dir = [results_dir filesep 'preproc_data'];
if ~exist(stats_dir,'dir')
    mkdir(stats_dir)
end
if ~exist(processing_dir,'dir')
    mkdir(processing_dir)
end
% Initialize subject data; extract; copy to preprocessing folder
sub = '01';
s_raw_fn = fullfile(data_dir, ['sub-' sub], 'anat', ['sub-' sub '_T1w.nii.gz']);
if exist(s_raw_fn, 'file')
    out_fns = gunzip(s_raw_fn);
    s_fn = out_fns{1};
else
    s_fn = strrep(s_raw_fn, '.gz', '');
end
f_raw_fn = fullfile(data_dir, ['sub-' sub], 'func', ['sub-' sub '_task-passiveimageviewing_bold.nii.gz']);
if exist(f_raw_fn, 'file')
    out_fns = gunzip(f_raw_fn);
    f_fn = out_fns{1};
else
    f_fn = strrep(f_raw_fn, '.gz', '');
end
% Create preprocessing subfolders
anat_dir = fullfile(processing_dir, ['sub-' sub], 'anat'); 
func_dir = fullfile(processing_dir, ['sub-' sub], 'func'); 
if ~exist(anat_dir, 'dir')
    mkdir(anat_dir)
end
if ~exist(func_dir, 'dir')
    mkdir(func_dir)
end
% Copy extracted files to preprocessing subfolders
anat_fn = fullfile(processing_dir, ['sub-' sub], 'anat', ['sub-' sub '_T1w.nii']);
func_fn = fullfile(processing_dir, ['sub-' sub], 'func', ['sub-' sub '_task-passiveimageviewing_bold.nii']);
if ~exist(anat_fn, 'file')
    copyfile(s_fn, anat_dir)
end
if ~exist(func_fn, 'file')
    copyfile(f_fn, func_dir)
end
% Initialize processing variables
fwhm = 6;  % mm<jupyter_output>ans = 1
ans = 1
ans = 1
ans = 1
ans = 1
ans = 1<jupyter_text>STEP 2: Quick inspection of data<jupyter_code>%% QUICK INSPECTION OF ANATOMICAL DATA
[p_anat, frm1, rg1, dim1] = fmrwhy_util_readNifti(anat_fn);
anat_3Dimg = p_anat.nii.img;

[Ni, Nj, Nk] = size(anat_3Dimg);
subplot(131); imagesc(rot90(squeeze(anat_3Dimg(round(Ni/2),:,:)))); colormap gray; axis image
subplot(132); imagesc(rot90(squeeze(anat_3Dimg(:,round(Nj/2),:)))); colormap gray; axis image
subplot(133); imagesc(rot90(squeeze(anat_3Dimg(:,:,round(Nk/2))))); colormap gray; axis image
%% QUICK INSPECTION OF FUNCTIONAL DATA

[p_func, frm1, rg1, dim1] = fmrwhy_util_readNifti(func_fn);
func_4Dimg = p_func.nii.img;

[Ni, Nj, Nk, Nt] = size(func_4Dimg);
subplot(131); imagesc(rot90(squeeze(func_4Dimg(round(Ni/2),:,:,1)))); colormap gray; axis image
subplot(132); imagesc(rot90(squeeze(func_4Dimg(:,round(Nj/2),:,1)))); colormap gray; axis image
subplot(133); imagesc(rot90(squeeze(func_4Dimg(:,:,round(Nk/2),1)))); colormap gray; axis image<jupyter_output><empty_output><jupyter_text>STEP 3: Preprocessing<jupyter_code>% Preprocess structural and functional images (if not already)
% Calls function `spm_standardPreproc_jsh`
[d, f, e] = fileparts(anat_fn);
[d1, f1, e1] = fileparts(func_fn);
if exist([d filesep 'rc1' f e], 'file')
    disp('...preproc already done, saving variables...')
    preproc_data = struct;
    % Structural filenames
    preproc_data.forward_transformation = [d filesep 'y_' f e];
    preproc_data.inverse_transformation = [d filesep 'iy_' f e];
    preproc_data.gm_fn = [d filesep 'c1' f e];
    preproc_data.wm_fn = [d filesep 'c2' f e];
    preproc_data.csf_fn = [d filesep 'c3' f e];
    preproc_data.bone_fn = [d filesep 'c4' f e];
    preproc_data.soft_fn = [d filesep 'c5' f e];
    preproc_data.air_fn = [d filesep 'c6' f e];
    preproc_data.rstructural_fn = [d filesep 'r' f e];
    preproc_data.rgm_fn = [d filesep 'rc1' f e];
    preproc_data.rwm_fn = [d filesep 'rc2' f e];
    preproc_data.rcsf_fn = [d filesep 'rc3' f e];
    preproc_data.rbone_fn = [d filesep 'rc4' f e];
    preproc_data.rsoft_fn = [d filesep 'rc5' f e];
    preproc_data.rair_fn = [d filesep 'rc6' f e];
    % Functional filenames
    preproc_data.rfunctional_fn = [d1 filesep 'r' f1 e1];
    preproc_data.srfunctional_fn = [d1 filesep 'sr' f1 e1];
    preproc_data.mp_fn = [d1 filesep 'rp_' f1 '.txt'];
    preproc_data.MP = load(preproc_data.mp_fn);
else
    disp('...running preprocessing batch jobs...')
    preproc_data = spm_standardPreproc_jsh(func_fn, anat_fn, fwhm, spm_dir);
end<jupyter_output>...running preprocessing batch jobs...
Step 1 -- Realign all volumes to first functional volume


------------------------------------------------------------------------
13-Apr-2020 13:38:57 - Running job #1
------------------------------------------------------------------------
13-Apr-2020 13:38:57 - Running 'Realign: Estimate & Reslice'

SPM12: spm_realign (v7141)                         13:38:57 - 13/04/2020
========================================================================
Completed                               :          13:40:13 - 13/04/2020

SPM12: spm_reslice (v7141)                         13:40:13 - 13/04/2020
========================================================================
Completed                               :          13:40:58 - 13/04/2020
13-Apr-2020 13:40:58 - Done    'Realign: Estimate & Reslice'
13-Apr-2020 13:40:58 - Done

Step 1 - Done!
Step 2 -- Coregister structural image to first dynamic image


-------------------------------------------------[...]<jupyter_text>STEP 4: Create 1st level statistical design<jupyter_code>% Set up statistical design parameters, based on task data
sess_params = struct;
sess_params.timing_units = 'secs';
sess_params.timing_RT = 1.6;
sess_params.cond_name = 'Pictures';
sess_params.cond_onset = [0; 40.1; 77.2; 111.3; 143.3; 179.4; 218.5; 251.5; 299.6; 334.7; 374.8; 411.9; 445.9; 478.0; 514.1; 553.2];
sess_params.cond_duration = [24.1000; 24.06; 24.07; 24.06; 24.06; 24.07; 24.04; 24.06; 24.07; 24.10; 24.06; 24.06; 24.09; 24.09; 24.06; 24.07];
% Call script to set up design
spm_specify1stlevel_jsh(stats_dir, preproc_data.srfunctional_fn, preproc_data.mp_fn, sess_params)
% Display/explore design matrix 
load([stats_dir filesep 'SPM.mat']);
spm_DesRep('fMRIDesMtx',SPM,1,1)
saveas(gcf, [processing_dir filesep 'des_mat.png'])<jupyter_output>Can not initialise pmod value(s): job is not a struct.
Can not initialise regress value(s): job is not a struct.
Can not initialise fact value(s): job is not a struct.


------------------------------------------------------------------------
13-Apr-2020 13:54:02 - Running job #1
------------------------------------------------------------------------
13-Apr-2020 13:54:03 - Running 'fMRI model specification'

SPM12: spm_fMRI_design (v7739)                     13:54:03 - 13/04/2020
========================================================================
Saving fMRI design                      :               ...SPM.mat saved

SPM12: spm_fmri_spm_ui (v7738)                     13:54:03 - 13/04/2020
========================================================================
Mapping files                           :                        ...done
Calculating globals                     :                        ...done
Saving SPM configuration                :               ...SPM.mat saved
Sa[...]<jupyter_text>STEP 5: Estimate the model<jupyter_code>spm_estimateModel_jsh(stats_dir)<jupyter_output>------------------------------------------------------------------------
13-Apr-2020 13:56:30 - Running job #1
------------------------------------------------------------------------
13-Apr-2020 13:56:31 - Running 'Model estimation'

SPM12: spm_spm (v7738)                             13:56:31 - 13/04/2020
========================================================================

SPM12: spm_est_non_sphericity (v7577)              13:56:34 - 13/04/2020
========================================================================
Chunk   1/1                             :                  ...processing13-Apr-2020 13:56:35 - Failed  'Model estimation'
file2mat.c not compiled - see Makefile
In file "/home/jovyan/spm12/@file_array/private/file2mat.m" (v7147), function "file2mat" at line 16.
In file "/home/jovyan/spm12/@file_array/subsref.m" (v7439), function "subsref>multifile2mat" at line 179.
In file "/home/jovyan/spm12/@file_array/subsref.m" (v7439), function "subsref>subfun" at line 99.
In f[...]<jupyter_text>STEP 6: Setup the task contrast<jupyter_code>[Ntt, Nregr] = size(SPM.xX.X);
contrast_params = struct;
contrast_params.weights = zeros(1, Nregr); 
contrast_params.weights(1) = 1;
contrast_params.name = 'Picture viewing';
spm_setupTaskContrast_jsh(stats_dir, contrast_params)<jupyter_output>------------------------------------------------------------------------
13-Apr-2020 13:56:59 - Running job #1
------------------------------------------------------------------------
13-Apr-2020 13:57:00 - Running 'Contrast Manager'

SPM12: spm_contrasts.m                             13:57:00 - 13/04/2020
========================================================================
Contrasts folder                        :     /home/jovyan/results/stats
13-Apr-2020 13:57:00 - Failed  'Contrast Manager'
This model has not been estimated.
In file "/home/jovyan/spm12/config/spm_run_con.m" (v7738), function "spm_run_con" at line 34.

The following modules did not run:
Failed: Contrast Manager

error: Job execution failed. The full log of this run can be found in MATLAB command window, starting with the lines (look for the line showing the exact #job as displayed in this error message)
------------------ 
Running job #1
------------------<jupyter_text>STEP 7: Generate results<jupyter_code>spm_runResults_jsh(stats_dir)<jupyter_output>------------------------------------------------------------------------
13-Apr-2020 13:57:34 - Running job #1
------------------------------------------------------------------------
13-Apr-2020 13:57:34 - Running 'Results Report'

    * - SPM12: spm_getSPM  ---------------------------------------------

        This model has not been estimated.
         
        /home/jovyan/results/stats/SPM.mat

        -----------------------------------------  13:57:34 - 13/04/2020

13-Apr-2020 13:57:34 - Failed  'Results Report'
matrix cannot be indexed with .
In file "/home/jovyan/spm12/spm_list.m" (v7725), function "spm_list" at line 172.
In file "/home/jovyan/spm12/config/spm_run_results.m" (v7268), function "spm_run_results" at line 80.

The following modules did not run:
Failed: Results Report

error: Job execution failed. The full log of this run can be found in MATLAB command window, starting with the lines (look for the line showing the exact #job as displayed in this error message)
-[...]
{'fraction_non_alphanumeric': 0.24248927038626608, 'fraction_numerical': 0.06071173104434907, 'mean_word_length': 2.6409505208333335, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 28, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '276983', 'license': 'MIT', 'n_tokens_mistral': 4253, 'n_tokens_neox': 3738, 'n_words': 902, 'path': 'spm12_scripting_tutorial.ipynb', 'repo_name': 'ubik60/linanalys2', 'size': '11184'}