JiscMail Logo
Email discussion lists for the UK Education and Research communities

Help for SPM Archives


SPM Archives

SPM Archives


SPM@JISCMAIL.AC.UK


View:

Message:

[

First

|

Previous

|

Next

|

Last

]

By Topic:

[

First

|

Previous

|

Next

|

Last

]

By Author:

[

First

|

Previous

|

Next

|

Last

]

Font:

Proportional Font

LISTSERV Archives

LISTSERV Archives

SPM Home

SPM Home

SPM  August 2021

SPM August 2021

Options

Subscribe or Unsubscribe

Subscribe or Unsubscribe

Log In

Log In

Get Password

Get Password

Subject:

Re: Improve efficiency of first-level model script

From:

"Lally, Clare" <[log in to unmask]>

Reply-To:

Lally, Clare

Date:

Tue, 31 Aug 2021 08:34:39 +0000

Content-Type:

text/plain

Parts/Attachments:

Parts/Attachments

text/plain (1 lines)

Dear Guillaume,



Thank you for your response! That's right about the data being over 7000 scans. The design is less neat... I am using open-access pre-processed data from the Naturalistic Neuroimaging Database, where participants were scanned whilst they watched a feature-length movie. The events that I am modelling are instances of particular movie characters speaking, so the 'trials' are unequal sizes and distributed differently across the run.



I will try running one participant at a time, and I will try to keep note of when the script crashes.



I remember you mentioned this issue with disabling masking before, I use a mask when I conduct the next stage of my analyses (multivariate) but it sounds like I need to do something at this stage to. My SPM script produces the binary mask.nii and the output is sensible. Can I apply this mask at this stage? Alternatively I could exclude any values that are zero exactly (as all values outside of the brain are zero) although I worry I'd accidentally exclude values inside the brain as well. Which would you advise and please could I have some guidance on how to include this in the code? Thank you!!!



All the best,

Clare



On 27/08/2021, 10:49, "Flandin, Guillaume" <[log in to unmask]> wrote:



    Dear Clare,

    

    A two hour recording in a single session is long. Do I understand it

    correctly that there is on average one trial every 30 seconds and that

    they are spread across 24 conditions? With a TR of 1s, this means that

    you have over 7000 scans?

    

    While it should not happen, I presume that the error is due to out of

    memory when handling covariance matrices. Do you know at which stage do

    the crashes occur? Not a satisfactory answer but it might help if you

    close and restart MATLAB in between the analysis of each subject (there

    is also a 'pack' command in MATLAB but I don't know how well it would

    work in your case).

    https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.mathworks.com%2Fhelp%2Fmatlab%2Fref%2Fpack.html&amp;data=04%7C01%7Cc.lally%40UCL.AC.UK%7Cd8a34ee2e3b045b4769508d9693ff293%7C1faf88fea9984c5b93c9210a11d9a5c2%7C0%7C0%7C637656545633386684%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=Mq8Pg5dyMIqdhzeRbHkLrIb6fVET9k4mEPl5X1tDS7M%3D&amp;reserved=0

    

    One thing I notice in your script is that you disabled masking:

      jobs{1}.stats{1}.fmri_spec.mthresh = -Inf;

      jobs{1}.stats{1}.fmri_spec.mask = {''};

    From the comment in your code, I understand that the data have already

    been mean-centred during preprocessing hence the change but make sure

    outside of the brain is not considered during the GLM analysis otherwise

    provide an explicit mask.

    

    Best regards,

    Guillaume.

    

    

    On 26/08/2021 11:26, Lally, Clare wrote:

    > Hi everyone, 

    > 

    >  

    > 

    > Please could I have some advice on how to make my first level model

    > estimation batch script more computationally efficient? I am working

    > with a large dataset, in which participants have 2 hours of functional

    > data in a single run. In my model, I have 242 events distributed across

    > 24 conditions. 

    > 

    >  

    > 

    > I have been running the script below on a 3.6 GHz 16 GB iMac, but it is

    > taking over 8 hours per participant. Also, the script routinely crashes

    > with the generic ‘MATLAB has encountered an error and needs to close’,

    > which means I have to restart the script from the current participant.

    > However, it does run the whole way through otherwise. 

    > 

    >  

    > 

    > I suspect the script runs slow and crashes because the code is producing

    > things that don’t need to be created in the first place, or that can be

    > removed from memory once they have served their purpose. 

    > 

    >  

    > 

    > I’d be very grateful for any advice on how to check this or what to

    > change. Thank you! 

    > 

    >  

    > 

    > Clare 

    > 

    >  

    > 

    >  

    > 

    > %% Univariate matrix items vs. baseline - estimate the model 

    > 

    >   

    > 

    > % Specify whether to conduct model estimation (time-consuming) 

    > 

    > ESTIMATE_GLM = 1; % Set this to zero to check the script without

    > estimating  

    > 

    >   

    > 

    > % Scanning/timing details 

    > 

    > TR = 1; 

    > 

    > timings = readtable(fullfile(timings_path, 'Contrasts_file.csv')); 

    > 

    > runs = unique(timings.Run); 

    > 

    > conditions = unique(timings.ConditionName); 

    > 

    > runs_n = length(runs); 

    > 

    > conditions_n = length(conditions); 

    > 

    > onsets_n = length(timings.Onset); 

    > 

    >   

    > 

    > % Start for loop for first level analyses here 

    > 

    > for p = 1:numel(ppt) 

    > 

    >   

    > 

    >     % Progress message 

    > 

    >     disp(['Processing ', char(ppt(p)), ' ...']) 

    > 

    >     

    > 

    >     % Define locations for data and outputs 

    > 

    >     func_path = fullfile(data_path, char(ppt(p)), 'func'); 

    > 

    >     funcs = dir(fullfile(data_path, char(ppt(p)), 'func',

    > [char(ppt(p)) '*_bold_no_blur_no_censor.nii'])); 

    > 

    >     funcs = funcs.name; 

    > 

    >     numScans = numel(spm_vol(fullfile(func_path,funcs))); 

    > 

    >     anat = fullfile(data_path, char(ppt(p)), 'anat',

    > [char(ppt(p)) '_t1w_mni_alignment.nii']); 

    > 

    >     output_path = fullfile(data_path, char(ppt(p)), output_folder); 

    > 

    >     

    > 

    >     % Begin creating jobs structure 

    > 

    >     jobs{1}.stats{1}.fmri_spec.dir = cellstr(output_path); 

    > 

    >     jobs{1}.stats{1}.fmri_spec.timing.units = 'secs'; 

    > 

    >     jobs{1}.stats{1}.fmri_spec.timing.RT = TR; 

    > 

    >     jobs{1}.stats{1}.fmri_spec.timing.fmri_t = 16; 

    > 

    >     jobs{1}.stats{1}.fmri_spec.timing.fmri_t0 = 1; 

    > 

    >     

    > 

    >     % Create multiple conditions .mat file for each run    

    > 

    >     for r = 1:size(runs_n, 1) 

    > 

    >             % Create empty structures for onsets and durations 

    > 

    >             names = conditions'; 

    > 

    >             onsets = cell(1, size(conditions,1)); 

    > 

    >             durations = cell(1, size(conditions,1)); 

    > 

    >           

    > 

    >         % For each condition     

    > 

    >         for c = 1:size(conditions,1) 

    > 

    >              

    > 

    >             % For each onset of an event 

    > 

    >             for x = 1:onsets_n 

    > 

    >                 if isequal(timings.ConditionName(x), conditions(c)) &&

    > timings.Run(x) == r 

    > 

    >                     onsets{c} = double([onsets{c} timings.Onset(x)]); 

    > 

    >                     durations{c} = double([durations{c}

    > timings.Duration(x)]); 

    > 

    >   

    > 

    >                 end 

    > 

    >             end 

    > 

    >         end 

    > 

    >   

    > 

    >         % Save the structure as a .mat file for each run per participant 

    > 

    >         save (fullfile(output_path, [mat_prefix, char(ppt(p)), '_run',

    > num2str(runs_n(r))]), ... 

    > 

    >         'names', 'onsets', 'durations') 

    > 

    >     

    > 

    >         % Get frames for each run using spm_select - add run-related

    > file indexing to the line below if needed  

    > 

    >         files = spm_select('ExtFPList', func_path, funcs, 1:numScans); 

    > 

    >          

    > 

    >         % Fill in session information within jobs structure 

    > 

    >         jobs{1}.stats{1}.fmri_spec.sess(r).scans = cellstr(files); 

    > 

    >         jobs{1}.stats{1}.fmri_spec.sess(r).cond = struct('name',

    > {}, 'onset', {}, 'duration', {}, 'tmod', {}, 'pmod', {}); 

    > 

    >         jobs{1}.stats{1}.fmri_spec.sess(r).multi =

    > cellstr(fullfile(output_path, [mat_prefix, char(ppt(p)), '_run',

    > num2str(runs_n(r)), '.mat'])); 

    > 

    >         jobs{1}.stats{1}.fmri_spec.sess(r).regress = struct('name',

    > {}, 'val', {}); 

    > 

    >         jobs{1}.stats{1}.fmri_spec.sess(r).multi_reg = {''}; 

    > 

    >         jobs{1}.stats{1}.fmri_spec.sess(r).hpf = 128; 

    > 

    >     end 

    > 

    >     

    > 

    >     % Fill in the rest of the jobs fields 

    > 

    >     jobs{1}.stats{1}.fmri_spec.fact = struct('name', {}, 'levels', {}); 

    > 

    >     jobs{1}.stats{1}.fmri_spec.bases.hrf = struct('derivs', [0 0]); 

    > 

    >     jobs{1}.stats{1}.fmri_spec.volt = 1; 

    > 

    >     jobs{1}.stats{1}.fmri_spec.global = 'None'; 

    > 

    >     jobs{1}.stats{1}.fmri_spec.mthresh = -Inf; % For mean-centred

    > functional images - needed for NNDb 

    > 

    >     jobs{1}.stats{1}.fmri_spec.mask = {''}; 

    > 

    >     jobs{1}.stats{1}.fmri_spec.cvi = 'AR(1)'; 

    > 

    >     

    > 

    >     % Navigate to output directory, specify and estimate GLM 

    > 

    >     cd(output_path); 

    > 

    >     spm_jobman('run', jobs) 

    > 

    >     

    > 

    >     if ESTIMATE_GLM == 1 

    > 

    >         load SPM; 

    > 

    >         spm_spm(SPM); 

    > 

    >     end 

    > 

    > end 

    > 

    > 

    > 

    > Clare Lally (Post-doctoral Research Fellow)

    > UCL Vocal Communication Laboratory

    > <https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.carolynmcgettigan.com%2F&amp;data=04%7C01%7Cc.lally%40UCL.AC.UK%7Cd8a34ee2e3b045b4769508d9693ff293%7C1faf88fea9984c5b93c9210a11d9a5c2%7C0%7C0%7C637656545633386684%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=z%2Fz9sxRW2bKcTI7fTg4kvvcjWoC0qPtA8huj8cToHT0%3D&amp;reserved=0>

    > UCL SHaPS

    > Chandler House

    > 2 Wakefield Street

    > London WC1N 1PF

    > 

    > Personal website

    > <https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fclarelally.wordpress.com%2F&amp;data=04%7C01%7Cc.lally%40UCL.AC.UK%7Cd8a34ee2e3b045b4769508d9693ff293%7C1faf88fea9984c5b93c9210a11d9a5c2%7C0%7C0%7C637656545633396677%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=VO6xlnXVXy6dSIJvS0MpuqvzjuPiZCuzb0F699kRqpQ%3D&amp;reserved=0> | Twitter:

    > @Clare_Lally <https://twitter.com/Clare_Lally>

    > 

    

    -- 

    Guillaume Flandin, PhD

    Wellcome Centre for Human Neuroimaging

    UCL Queen Square Institute of Neurology

    London WC1N 3BG

    



Top of Message | Previous Page | Permalink

JiscMail Tools


RSS Feeds and Sharing


Advanced Options


Archives

May 2024
April 2024
March 2024
February 2024
January 2024
December 2023
November 2023
October 2023
September 2023
August 2023
July 2023
June 2023
May 2023
April 2023
March 2023
February 2023
January 2023
December 2022
November 2022
October 2022
September 2022
August 2022
July 2022
June 2022
May 2022
April 2022
March 2022
February 2022
January 2022
December 2021
November 2021
October 2021
September 2021
August 2021
July 2021
June 2021
May 2021
April 2021
March 2021
February 2021
January 2021
December 2020
November 2020
October 2020
September 2020
August 2020
July 2020
June 2020
May 2020
April 2020
March 2020
February 2020
January 2020
December 2019
November 2019
October 2019
September 2019
August 2019
July 2019
June 2019
May 2019
April 2019
March 2019
February 2019
January 2019
December 2018
November 2018
October 2018
September 2018
August 2018
July 2018
June 2018
May 2018
April 2018
March 2018
February 2018
January 2018
December 2017
November 2017
October 2017
September 2017
August 2017
July 2017
June 2017
May 2017
April 2017
March 2017
February 2017
January 2017
December 2016
November 2016
October 2016
September 2016
August 2016
July 2016
June 2016
May 2016
April 2016
March 2016
February 2016
January 2016
December 2015
November 2015
October 2015
September 2015
August 2015
July 2015
June 2015
May 2015
April 2015
March 2015
February 2015
January 2015
December 2014
November 2014
October 2014
September 2014
August 2014
July 2014
June 2014
May 2014
April 2014
March 2014
February 2014
January 2014
December 2013
November 2013
October 2013
September 2013
August 2013
July 2013
June 2013
May 2013
April 2013
March 2013
February 2013
January 2013
December 2012
November 2012
October 2012
September 2012
August 2012
July 2012
June 2012
May 2012
April 2012
March 2012
February 2012
January 2012
December 2011
November 2011
October 2011
September 2011
August 2011
July 2011
June 2011
May 2011
April 2011
March 2011
February 2011
January 2011
December 2010
November 2010
October 2010
September 2010
August 2010
July 2010
June 2010
May 2010
April 2010
March 2010
February 2010
January 2010
December 2009
November 2009
October 2009
September 2009
August 2009
July 2009
June 2009
May 2009
April 2009
March 2009
February 2009
January 2009
December 2008
November 2008
October 2008
September 2008
August 2008
July 2008
June 2008
May 2008
April 2008
March 2008
February 2008
January 2008
December 2007
November 2007
October 2007
September 2007
August 2007
July 2007
June 2007
May 2007
April 2007
March 2007
February 2007
January 2007
2006
2005
2004
2003
2002
2001
2000
1999
1998


JiscMail is a Jisc service.

View our service policies at https://www.jiscmail.ac.uk/policyandsecurity/ and Jisc's privacy policy at https://www.jisc.ac.uk/website/privacy-notice

For help and support help@jisc.ac.uk

Secured by F-Secure Anti-Virus CataList Email List Search Powered by the LISTSERV Email List Manager