def array2nii(im, A, fnii, descrip='', trnsp=(), flip=(), storage_as=[]):
    '''Store the numpy array 'im' to a NIfTI file 'fnii'.
    ----
    Arguments:
        'im':       image to be stored in NIfTI
        'A':        affine transformation
        'fnii':     output NIfTI file name.
        'descrip':  the description given to the file
        'trsnp':    transpose/permute the dimensions.
                    In NIfTI it has to be in this order: [x,y,z,t,...])
        'flip':     flip tupple for flipping the direction of x,y,z axes. 
                    (1: no flip, -1: flip)
        'storage_as': uses the flip and displacement as given by the following
                    NifTI dictionary, obtained using
                    nimpa.getnii(filepath, output='all').
    '''

    if not len(trnsp) in [0,3,4] and not len(flip) in [0,3]:
        raise ValueError('e> number of flip and/or transpose elements is incorrect.')

    #---------------------------------------------------------------------------
    #> TRANSLATIONS and FLIPS
    #> get the same geometry as the input NIfTI file in the form of dictionary,
    #>>as obtained from getnii(..., output='all')

    #> permute the axis order in the image array
    if isinstance(storage_as, dict) and 'transpose' in storage_as \
            and 'flip' in storage_as:

        trnsp = (storage_as['transpose'].index(0),
                 storage_as['transpose'].index(1),
                 storage_as['transpose'].index(2))

        flip = storage_as['flip']
    

    if trnsp==():
        im = im.transpose()
    #> check if the image is 4D (dynamic) and modify as needed
    elif len(trnsp)==3 and im.ndim==4:
        trnsp = tuple([t+1 for t in trnsp] + [0])
        im = im.transpose(trnsp)
    else:
        im = im.transpose(trnsp)
    

    #> perform flip of x,y,z axes after transposition into proper NIfTI order
    if flip!=() and len(flip)==3:
        im = im[::-flip[0], ::-flip[1], ::-flip[2], ...]
    #---------------------------------------------------------------------------

    nii = nib.Nifti1Image(im, A)
    hdr = nii.header
    hdr.set_sform(None, code='scanner')
    hdr['cal_max'] = np.max(im) #np.percentile(im, 90) #
    hdr['cal_min'] = np.min(im)
    hdr['descrip'] = descrip
    nib.save(nii, fnii)
def array2nii ( im , A , fnii , descrip = '' , trnsp = ( ) , flip = ( ) , storage_as = [ ] ) : if not len ( trnsp ) in [ 0 , 3 , 4 ] and not len ( flip ) in [ 0 , 3 ] : raise ValueError ( 'e> number of flip and/or transpose elements is incorrect.' ) if isinstance ( storage_as , dict ) and 'transpose' in storage_as and 'flip' in storage_as : trnsp = ( storage_as [ 'transpose' ] . index ( 0 ) , storage_as [ 'transpose' ] . index ( 1 ) , storage_as [ 'transpose' ] . index ( 2 ) ) flip = storage_as [ 'flip' ] if trnsp == ( ) : im = im . transpose ( ) elif len ( trnsp ) == 3 and im . ndim == 4 : trnsp = tuple ( [ t + 1 for t in trnsp ] + [ 0 ] ) im = im . transpose ( trnsp ) else : im = im . transpose ( trnsp ) if flip != ( ) and len ( flip ) == 3 : im = im [ : : - flip [ 0 ] , : : - flip [ 1 ] , : : - flip [ 2 ] , ... ] nii = nib . Nifti1Image ( im , A ) hdr = nii . header hdr . set_sform ( None , code = 'scanner' ) hdr [ 'cal_max' ] = np . max ( im ) hdr [ 'cal_min' ] = np . min ( im ) hdr [ 'descrip' ] = descrip nib . save ( nii , fnii )
Store the numpy array 'im' to a NIfTI file 'fnii'.
    ----
    Arguments:
        'im':       image to be stored in NIfTI
        'A':        affine transformation
        'fnii':     output NIfTI file name.
        'descrip':  the description given to the file
        'trsnp':    transpose/permute the dimensions.
                    In NIfTI it has to be in this order: [x,y,z,t,...])
        'flip':     flip tupple for flipping the direction of x,y,z axes. 
                    (1: no flip, -1: flip)
        'storage_as': uses the flip and displacement as given by the following
                    NifTI dictionary, obtained using
                    nimpa.getnii(filepath, output='all').