Slice Timing Correction

From BrainImagingCenter
Revision as of 16:31, 16 July 2011 by Jed.dobson (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

To use, cut-and-paste the following into a file and run via the Python interpreter. Running the script without any arguments will produce some help output. Please contact the author, Andy Connolly, for additional assistance.


#!/usr/bin/env python 

import os, sys, math


if __name__=='__main__':
    h="\n\
    \n\
    Usage at command line:\n\
\n\
       python philips_interleaved_timing.py tr slices <opt: filename>\n\
\n\
       example:\n\
        python philips_interleaved_timing.py 3000 44 slice_times.1D\n\
\n\
       where tr is length of tr in msec\n\
             slices is the number of slices per dynamic scan\n\
             filename is the name of the file to save the times in\n\
             (without filename the values will only go to standard output)\n\
\n\
    Explanation:\n\
\n\
    Creates a tpattern file for use in conjunction with AFNI 3dTshift for slice time correction for \n\
    the philips 'interleaved' slice-timing option, which is different from usual interleaved \n\
    protocols, such as alternating in the positive direction (i.e. alt+z)\n\
    Note this only works when the interleaved slices are acquired using the 'equidistant'\n\
    time-spacing option as well.  There is no way to calculate the time step for \n\
    acquisitions taken using the 'minimum' or 'default' time-spacing option.\n\
    However, in the case that you are using the maximum coverage/ max number of slices for your TR,\n\
    then you need not worry about whether you've used minimum or equidistant spacing...\n\
    \n\
    Author: Andy Connolly, andrew.c.connolly@dartmouth.edu\n\
    date: Sept. 3, 2009\n\
    '''"

    printfile=False
    if len(sys.argv) < 3:
        print h
        exit()
    if len(sys.argv) > 3:
        filename=sys.argv[3]
        printfile=True
    tr = float(sys.argv[1])
    print 'tr in ms = '+str(tr)
    n = float(sys.argv[2])
    print 'number of slices = '+str(n)
    
    times=list(range(int(n)))
    sliceorder=[]
    opposite_times=list(range(int(n)))
    step=tr/n
    print 'time step = '+str(step)
    t=0
    space=int(round(math.sqrt(n)))
    ceil=int(math.ceil(math.sqrt(n)))
    print 'slice step = ' + str(space)

    

    
    for start_idx in range(space):
        for i in range(ceil):
            if start_idx+(i*space)<len(times):
                times[start_idx+(i*space)]=str(round(t,3))
                opposite_times[start_idx+(i * space)]=str(tr - round(t,3))
                sliceorder.append(start_idx+(i * space) + 1)
                t=t+step


    print times
    print 'Slice order = '+str(sliceorder)
    if printfile:
        f = file(filename,'w')
        #g = file('opposite_times_for_testing.1D','w')
        s = ''
        gs = ''
        for t in times:
            s = s+str(t)+'\n'
        for t in opposite_times:
            gs = gs + str(t) + '\n'
        f.write(s)
        f.close()
        #g.write(gs)