def pre_recon_processing(self):
     if (not self.options.phasecorr_data):
         print('No Phasecorr Data') 
         return None   #something else#
     else:
         print('Processing FSE phase corrections...')
     #fetch param_dict from phase correction fid
     if (self.inputAcq.platform=="Varian"):
         phasecorrAcq = vrf.VarianAcquisition(self.options.phasecorr_data)
     else:
         print("Not ready for Bruker acquisition...")
     #fetch fid data from phase correction
     phase_fid_data = read_correction_fid(phasecorrAcq)
     #specify phase correction petable
     if (not self.options.phasecorr_table):
         print('No phase correction petable specified')
         default_table = os.path.join(self.options.phasecorr_data,
                                      'phasecorrtable_nf%d_ni%d'%(vrf.get_dict_value(phasecorrAcq.param_dict,'nf',6),
                                                                  vrf.get_dict_value(phasecorrAcq.param_dict,'ni',504)))
         phasecorr_petable_name = vrf.get_dict_value(phasecorrAcq.param_dict,'petable',default_table)   #fetch default table
     else:
         phasecorr_petable_name = self.options.phasecorr_table
     #petable checks
     t1_array = vrf.parse_petable_file(phasecorr_petable_name,'t1')
     t2_array = vrf.parse_petable_file(phasecorr_petable_name,'t2')
     nfid = int(phasecorrAcq.nfid)
     etl = int(vrf.get_dict_value(phasecorrAcq.param_dict,'etl',6))
     nf = phasecorrAcq.nf
     ni_perchan = phasecorrAcq.ni*phasecorrAcq.nfid
     if any((t1_array[1::etl]!=0)&(t2_array[1::etl]!=0)):
         print('PE table format appears to be wrong...should have zeros on echo 2!!')
     #parse zeros from table
     ifid,itrain = nonzero( all( (reshape(t1_array,(nfid,ni_perchan*(nf//etl)//nfid,nf))==0)*
                                 (reshape(t2_array,(nfid,ni_perchan*(nf//etl)//nfid,nf))==0) ,axis=-1) )
     #"moments" from table
     M = moment_terms_from_petable(t1_array,t2_array,etl)
     M.shape = (M.shape[0],nfid,ni_perchan*(nf//etl)//nfid,etl)
     phase_fid_data.shape=(nfid,phase_fid_data.shape[1],ni_perchan*(nf//etl)//nfid,etl,phase_fid_data.shape[-1])
     #generate read shifts
     for imouse in range(phase_fid_data.shape[1]):
         self.maxroind = int( argmax(abs(mean( phase_fid_data[ifid,imouse,itrain,1,:] ,axis=0))) )
     for imouse in range(phase_fid_data.shape[1]):
         if (not self.options.no_even_odd_echo_roshift):
             even_odd_pixel_shift = ROshift(mean(mean(phase_fid_data[ifid,imouse,itrain,0::2,:],axis=0),axis=0),
                                            mean(mean(phase_fid_data[ifid,imouse,itrain,1::2,:],axis=0),axis=0))
             phase_fid_data[:,imouse,:,0::2,:] = apply_RO_shift(phase_fid_data[:,imouse,:,0::2,:], even_odd_pixel_shift/2.0) 
             phase_fid_data[:,imouse,:,1::2,:] = apply_RO_shift(phase_fid_data[:,imouse,:,1::2,:],-even_odd_pixel_shift/2.0) 
             self.even_odd_pixel_shift[imouse] = even_odd_pixel_shift
         elif (imouse==0): print('Not performing even_odd_roshift')   
         if (not self.options.no_even_odd_echo_phaseshift):
             self.ephase_corr[imouse,:] = Echo_phasecorr(mean(phase_fid_data[ifid,imouse,itrain,:,:],axis=0),alpha=self.alpha)
         elif (imouse==0): print('Not performing even_odd_echo_phaseshift')    
         if (not self.options.no_moment_terms_from_petable):
             self.pMfit[imouse,:] = Phase_shift_cal(phase_fid_data[:,imouse,:,:,:],M,ifid,itrain)
         elif (imouse==0): print('Not calculating moment_terms_from_petable')
def split_kspace(inputAcq,kspace,petable_name,imouse,options): 
    nv = int(vrf.get_dict_value(inputAcq.param_dict,'nv',504))
    nv2 = int(vrf.get_dict_value(inputAcq.param_dict,'nv2',504))
    t1 = vrf.parse_petable_file(petable_name,'t1')
    t2 = vrf.parse_petable_file(petable_name,'t2')
    kspace1 = petable_orderedpair_reordering(kspace[0:kspace.shape[-3]//2,:,:],('t1','t2'),petable_name=petable_name,matrix=(nv,nv2),
                                             index_start=0,index_end=(kspace.shape[-2]*kspace.shape[-3]//2))
    kspace1 = fov_adjustment(kspace1,options,inputAcq,imouse)
    print("Not performing ppe FOV adjustment")
    kspace2 = petable_orderedpair_reordering(kspace[kspace.shape[-3]//2:kspace.shape[-3],:,:],('t1','t2'),petable_name=petable_name,matrix=(nv,nv2),
                                             index_start=(kspace.shape[-2]*(kspace.shape[-3]//2)),index_end=(kspace.shape[-2]*kspace.shape[-3]))
    kspace2 = fov_adjustment(kspace2,options,inputAcq,imouse)
    return kspace1,kspace2    
def read_correction_fid(inputAcq):
    nro = int(vrf.get_dict_value(inputAcq.param_dict,'np',1)/2)
    etl = int(vrf.get_dict_value(inputAcq.param_dict,'etl',1))
    nfid = int(vrf.get_dict_value(inputAcq.param_dict,'nfid',80)) # added this LSN, Oct2017
    nf = int(vrf.get_dict_value(inputAcq.param_dict,'nf',80)) # added this, LSN, Oct2017
    ni_perchan = inputAcq.ni*inputAcq.nfid
    # added / in inputAcq.nf//etl
    raw_data = zeros((inputAcq.nfid,inputAcq.nmice,ni_perchan*(inputAcq.nf//etl)//inputAcq.nfid,inputAcq.nf,nro),complex)
    for imouse in range (inputAcq.nmice):
        for j in range((ni_perchan*(inputAcq.nf//etl))):
            fid_data,data_error = inputAcq.getdatafids(j*inputAcq.nf,(j+1)*inputAcq.nf,rcvrnum=imouse)
            raw_data[int(j/(ni_perchan*(inputAcq.nf/etl)//inputAcq.nfid)),imouse,int(j%(ni_perchan*(inputAcq.nf/etl)//inputAcq.nfid)),:,:] = fid_data[:,:].copy() # added int designations here, LSN Oct 2017
    return raw_data 
 def __init__(self,inputAcq,options,outputfile,noshift_ppe=True,noshift_ppe2=False):
     if (inputAcq.platform!="Varian"):
         print("fse3dmice_recon is for Varian acquisitions only!!")
         raise SystemExit
     self.options = options
     self.inputAcq = inputAcq
     self.outputfile = outputfile
     if (self.options.petable==''):
         self.petable_name = vrf.get_dict_value(inputAcq.param_dict,'petable','petable')
     else:
         self.petable_name = self.options.petable
     self.kspace=None
     #create all arrays
     self.alpha = self.options.echoamp_alpha #coefficient for amplitude correction filter
     self.even_odd_pixel_shift = zeros(inputAcq.nmice,float)
     self.ephase_corr = ones((inputAcq.nmice,vrf.get_dict_value(inputAcq.param_dict,'etl',6)),complex)
     self.pMfit = zeros((inputAcq.nmice,5),float)
     self.maxroind=None 
     self.options.noshift_ppe=noshift_ppe
     self.options.noshift_ppe2=noshift_ppe2
 def gen_kspace(self,imouse=0):
     #grabs some useful numbers
     etl = int(vrf.get_dict_value(self.inputAcq.param_dict,'etl',6))
     nf = self.inputAcq.nf
     ni_perchan = self.inputAcq.ni*self.inputAcq.nfid
     nro = int(vrf.get_dict_value(self.inputAcq.param_dict,'np',630*2)/2)
     nrcvrs = self.inputAcq.nrcvrs
     t1_array = vrf.parse_petable_file(self.petable_name,'t1')
     t2_array = vrf.parse_petable_file(self.petable_name,'t2')
     #generate image moments to multiply with pMfit values
     M2 = moment_terms_from_petable(t1_array,t2_array,etl)
     #read in image k-space data
     kspace = zeros((nf*ni_perchan//etl,etl,nro),complex)
     for k in range(nf*ni_perchan//etl):
         fid_data,data_error = self.inputAcq.getdatafids(k*etl,(k+1)*etl,rcvrnum=imouse)
         kspace[k,:,:] = fid_data.copy()
     #apply read correction
     roramp = exp(1.j*2*pi*0.5*self.even_odd_pixel_shift[imouse]*(append(arange(nro/2),arange(-nro/2,0,1)))/nro) # original
     for k in range(kspace.shape[1]):
         kspace[:,k,:] = ifft(roramp**((-1)**(k%2))*self.ephase_corr[imouse,k]*
                                 fft(kspace[:,k,:],axis=-1),axis=-1)
     #apply phase correctionmaxroind
     phasecorr = exp(-1.j*sum(self.pMfit[imouse,:,newaxis,newaxis]*M2,axis=0))
     kspace = phasecorr[:,:,newaxis]*kspace 
     kspace1,kspace2 = split_kspace(self.inputAcq,kspace,self.petable_name,imouse,self.options)
     del kspace                       
     if (not self.options.no_Echo_shift_apply):
         kspace1,kspace2 = Echo_shift_apply(kspace1,kspace2,self.petable_name,self.inputAcq,self.maxroind)
     else: print('Not performing Echo_shift')  
     #average two acquisitions
     self.kspace = 0.5*(kspace1 + kspace2)
     del kspace1
     del kspace2
     #filter if requested
     if self.options.fermi_ellipse:
         self.kspace = fermi_ellipse_filter(self.kspace)
     elif self.options.fermi_pecirc:
         self.kspace = fermi_pecircle_filter(self.kspace)
def Echo_shift_apply(kspace1,kspace2,petable_name,inputAcq,maxroind):   
    t1_array_petable = vrf.parse_petable_file(petable_name,'t1')
    t2_array_petable = vrf.parse_petable_file(petable_name,'t2')
    nv = kspace1.shape[-2] 
    nv2 = kspace1.shape[-3] 
    etl = int(vrf.get_dict_value(inputAcq.param_dict,'etl',6))
    nfid = inputAcq.nfid
    ni_perchan = inputAcq.ni*inputAcq.nfid
    nf = inputAcq.nf
    #defining petable1 petable2
    index1 = 0
    index2 = t1_array_petable.shape[0]//2
    index3 = t1_array_petable.shape[0]
    t1_array_petable1 = t1_array_petable[index1:index2]
    t1_array_petable2 = t1_array_petable[index2:index3]
    t2_array_petable1 = t2_array_petable[index1:index2]
    t2_array_petable2 = t2_array_petable[index2:index3]
    petable1_pemat=zeros((nv2,nv),int)
    petable1_pemat[t2_array_petable1+nv2//2-1,t1_array_petable1+nv//2-1]=1+arange(len(t1_array_petable1))%etl
    petable2_pemat=zeros((nv2,nv),int)
    petable2_pemat[t2_array_petable2+nv2//2-1,t1_array_petable2+nv//2-1]=1+arange(len(t1_array_petable2))%etl
    del t1_array_petable1
    del t1_array_petable2
    del t2_array_petable1
    del t2_array_petable2
    ROI1_centre = (petable1_pemat==petable1_pemat[nv2//2-1,nv//2-1])
    ROI2_centre = (petable2_pemat==petable2_pemat[nv2//2-1,nv//2-1])
    kspace1_centre = ROI1_centre[:,:,newaxis]*kspace1
    kspace2_centre = ROI2_centre[:,:,newaxis]*kspace2
    del ROI1_centre
    del ROI2_centre
    kspace1 = kspace1-kspace1_centre
    kspace2 = kspace2-kspace2_centre
    eopixshift_2 = Echo_shift((mean(kspace1_centre[:,:,maxroind-5:maxroind+5],axis=-1)),    
                     (mean(kspace2_centre[:,:,maxroind-5:maxroind+5],axis=-1)))
    eoramp_2 = exp(-1.j*2*pi*0.5*eopixshift_2[0]*append(arange(nv2//2),arange(-nv2//2,0,1))//nv2)[:,newaxis]* \
               exp(-1.j*2*pi*0.5*eopixshift_2[1]*append(arange(nv//2),arange(-nv//2,0,1))//nv)[newaxis,:]
    for j in range(kspace1_centre.shape[2]):
        kspace1_centre[:,:,j] = ifft2(eoramp_2*fft2(kspace1_centre[:,:,j]))
        kspace2_centre[:,:,j] = ifft2(eoramp_2**(-1)*fft2(kspace2_centre[:,:,j])) 
    kspace1 = kspace1 + kspace1_centre
    kspace2 = kspace2 + kspace2_centre
    del kspace1_centre
    del kspace2_centre
    return kspace1,kspace2