help > Help with Batch scripting using 3d nii files
Showing 1-6 of 6 posts89 Subscribers
replyDisplay: Results per page:
Jul 23, 2012  06:07 PM
Help with Batch scripting using 3d nii files
Hi,

I'm sure this is an incredibly basic question, so hopefully it has an equally easy answer:

I'm trying to write a batch script that will find my 3d nii files (I have 140 time points for about 175 patients) and then run the pre-processing steps on them. In the example Batch script, the program calls a 4d nii file using the following code:

>> BATCH.New.functionals{1}{1}='mypath/subject1/session1/foo4d.nii'

The problem is, my data is organised as 3d nii files. How do I amend the line so that Conn will grab all 140 scans for each patient?

As a side point, I'd also like to write a 'for' loop which will go through and grab each subject/session in turn? Is it possible, or do I need to just copy the lines and go through, changing each one to point to the specific subject/session???

Thanks for your time

Mac
reply reply with quote
Jul 24, 2012  06:07 PM
RE: Help with Batch scripting using 3d nii files
Hi Mac,

To enter (multiple) 3d nii files for each subject/session in the batch.New.functionals field you can simply either enter them as a char array (a matrix with one row per time point; e.g. batch.New.functionals{nsub}{nses}=strvcat('file1.img','file2.img','file3.img') ) or as a cell array (a list with one element per time point; e.g. batch.New.functionals{nsub}{nses}={'file1.img','file2.img','file3.img'})

And yes, I would definitely loop over all subjects/sessions data first to fill the batch.New.functionals field (but note that you only need to invoque the conn_batch command once) e.g.

  SubjectFolder={'/data/Subject01','/data/Subject02','/data/Subject03','/data/Subject04'};
  SessionSubFolder={'Session01','Session02'};
  for nsub=1:numel(SubjectFolder)
    for nses=1:numel(SessionSubFolder)
       files=dir(fullfile(SubjectFolder{nsub},SessionSubFolder{nses},'*.img'));
       batch.New.functionals{nsub}{nses}={files(:).name};
    end
   end
   % ... other batch definitions
   conn_batch(batch);

(assumming, for example, that you have a list of subject folders -SubjectFolder- and another list of subfolders -SessionSubFolder- within each of the previous subject folders where each subject/session data resides, and that the filenames are sorted appropriately within these folders)
 
Let me know if this clarifies
Best
Alfonso 

  
Originally posted by Mac Shine:
Hi,

I'm sure this is an incredibly basic question, so hopefully it has an equally easy answer:

I'm trying to write a batch script that will find my 3d nii files (I have 140 time points for about 175 patients) and then run the pre-processing steps on them. In the example Batch script, the program calls a 4d nii file using the following code:

>> BATCH.New.functionals{1}{1}='mypath/subject1/session1/foo4d.nii'

The problem is, my data is organised as 3d nii files. How do I amend the line so that Conn will grab all 140 scans for each patient?

As a side point, I'd also like to write a 'for' loop which will go through and grab each subject/session in turn? Is it possible, or do I need to just copy the lines and go through, changing each one to point to the specific subject/session???

Thanks for your time

Mac
reply reply with quote
Jul 24, 2012  08:07 PM
RE: Help with Batch scripting using 3d nii files
Hi Alfredo

Thanks for getting back to me so quickly.

To clarify your assumption, we only have one session per subject and they are in their own folder. The data structure is:

/data/patients/x001/functional
                             /structural
/data/patients/x002/functional
                             /structural
etc.

In that case, should I delete the lines related to "SessionSubFolder" (i.e. - lines 2, 4 and 7) as well as the other references to Sessions?


The other problem I'm having is that conn doesn't seem to want to recognise my .nii files. I know that they exist, as I can navigate to them and view them in SPM Display, however whenever I try to run the batch, I get the following error message:

??? Error using ==> spm_vol>subfunc at 111
File "/Volumes/My Passport/fMRI/FOG/RS T1s/FNFoff/Freezers/x002/functional/vol001.nii" does not exist.


Any ideas where I'm going wrong?

Mac
reply reply with quote
Jul 25, 2012  12:07 PM
RE: Help with Batch scripting using 3d nii files
Hi Mac,

For your data structure something like the following should work:


nsubjects=175;
SubjectFolder=arrayfun(@(x)['/data/patients/x',num2str(x,'%03d')],1:nsubjects,'uni',0);
SessionSubFolder={'functional'};
for nsub=1:numel(SubjectFolder)
    for nses=1:numel(SessionSubFolder)
        files=dir(fullfile(SubjectFolder{nsub},SessionSubFolder{nses},'vol*.img'));
        batch.New.functionals{nsub}{nses}={files(:).name};
        fprintf('Subject %d, Session %d: %d functional files in %s\n',nsub,nses,numel(files),fullfile(SubjectFolder{nsub},SessionSubFolder{nses}));
    end
endAnd regarding your question about the spm_vol error message, that error indicates simply that the file do not exist at the target location, typically this is the effect of some typo in the path or filename (perhaps the 3d files are named vol*.img instead of vol*.nii?), if there is some typo you should be able to type in the command line   dir('/Volumes/My Passport/fMRI/FOG/RS T1s/FNFoff/Freezers/x002/functional/vol001.nii')and see a similar error, then go back and edit this line until you find the source of the problem.Let me know if this worksBestAlfonsoOriginally posted by Mac Shine:
 
Hi Alfredo

Thanks for getting back to me so quickly.

To clarify your assumption, we only have one session per subject and they are in their own folder. The data structure is:

/data/patients/x001/functional
                             /structural
/data/patients/x002/functional
                             /structural
etc.

In that case, should I delete the lines related to "SessionSubFolder" (i.e. - lines 2, 4 and 7) as well as the other references to Sessions?


The other problem I'm having is that conn doesn't seem to want to recognise my .nii files. I know that they exist, as I can navigate to them and view them in SPM Display, however whenever I try to run the batch, I get the following error message:

??? Error using ==> spm_vol>subfunc at 111
File "/Volumes/My Passport/fMRI/FOG/RS T1s/FNFoff/Freezers/x002/functional/vol001.nii" does not exist.


Any ideas where I'm going wrong?

Mac
reply reply with quote
Jul 25, 2012  09:07 PM
RE: Help with Batch scripting using 3d nii files
Hi Alfredo

That worked perfectly for the functional scans. Is there a similar code that would work for the structural scans? When I try what I think would work, MATLAB just sighs and gives up!

Mac
reply reply with quote
Jul 26, 2012  08:07 PM
RE: Help with Batch scripting using 3d nii files
Hi Mac

No problem, something along these lines should work:

nsubjects=175;
SubjectFolder=arrayfun(@(x)['/data/patients/x',num2str(x,'%03d')],1:nsubjects,'uni',0);
SessionSubFolder={'functional'};
StructuralFolder='structural';
for nsub=1:numel(SubjectFolder)
    for nses=1:numel(SessionSubFolder)
        files=conn_dir(fullfile(SubjectFolder{nsub},SessionSubFolder{nses},'vol*.img'));
        batch.New.functionals{nsub}{nses}=files;
        fprintf('Subject %d, Session %d: %d functional files in %s\n',nsub,nses,size(files,1),fullfile(SubjectFolder{nsub},SessionSubFolder{nses}));
    end
    files=conn_dir(fullfile(SubjectFolder{nsub},StructuralFolder,'anat*.img'));
    if size(files,1)~=1, error('no anatomical file found'); end
    batch.New.structurals{nsub}=files;
end
(please change the reference to 'anat*.img' to something appropriate for the filename of your structural volumes)
Hope this helps

Best
Alfonso


Originally posted by Mac Shine:
Hi Alfredo

That worked perfectly for the functional scans. Is there a similar code that would work for the structural scans? When I try what I think would work, MATLAB just sighs and gives up!

Mac
reply reply with quote
reply