Esempio n. 1
0
    def get_sigma2_map(self,nu,model,rotavg=True):
        N = self.cryodata.N
        N_D = float(self.cryodata.N_D_Train)
        num_batches = float(self.cryodata.num_batches)
        base_sigma2 = self.cryodata.get_noise_std()**2

        mean_sigma2 = self.error_history.get_mean().reshape((N,N))
        mean_mask = self.mask_history.get_mean().reshape((N,N))
        mask_w = self.mask_history.get_wsum() * (N_D / num_batches)
        
        if rotavg:
            mean_sigma2 = cryoem.rotational_average(mean_sigma2,normalize=True,doexpand=True)
            mean_mask = cryoem.rotational_average(mean_mask,normalize=False,doexpand=True)

        obsw = mask_w * mean_mask
        map_sigma2 = (mean_sigma2 * obsw + nu * base_sigma2) / (obsw + nu)

        assert np.all(np.isfinite(map_sigma2))

        if model == 'coloured':
            map_sigma2 = map_sigma2
        elif model == 'white':
            map_sigma2 = np.mean(map_sigma2)
        else:
            assert False, 'model must be one of white or coloured'

        return map_sigma2
Esempio n. 2
0
    def get_envelope_map(self,
                         sigma2,
                         rho,
                         env_lb=None,
                         env_ub=None,
                         minFreq=None,
                         bfactor=None,
                         rotavg=True):
        N = self.cryodata.N
        N_D = float(self.cryodata.N_D_Train)
        num_batches = float(self.cryodata.num_batches)
        psize = self.params['pixel_size']

        mean_corr = self.correlation_history.get_mean().reshape((N, N))
        mean_power = self.power_history.get_mean().reshape((N, N))
        mean_mask = self.mask_history.get_mean().reshape((N, N))
        mask_w = self.mask_history.get_wsum() * (N_D / num_batches)

        if rotavg:
            mean_corr = cryoem.rotational_average(mean_corr,
                                                  normalize=True,
                                                  doexpand=True)
            mean_power = cryoem.rotational_average(mean_power,
                                                   normalize=True,
                                                   doexpand=True)
            mean_mask = cryoem.rotational_average(mean_mask,
                                                  normalize=False,
                                                  doexpand=True)

        if isinstance(sigma2, n.ndarray):
            sigma2 = sigma2.reshape((N, N))

        if bfactor is not None:
            coords = gencoords(N, 2).reshape((N**2, 2))
            freqs = n.sqrt(n.sum(coords**2, axis=1)) / (psize * N)
            prior_envelope = ctf.envelope_function(freqs, bfactor).reshape(
                (N, N))
        else:
            prior_envelope = 1.0

        obsw = (mask_w * mean_mask / sigma2)
        exp_env = (mean_corr * obsw +
                   prior_envelope * rho) / (mean_power * obsw + rho)

        if minFreq is not None:
            # Only consider envelope parameters for frequencies above a threshold
            minRad = minFreq * 2.0 * psize

            _, _, minRadMask = gencoords(N, 2, minRad, True)

            exp_env[minRadMask.reshape((N, N))] = 1.0

        if env_lb is not None or env_ub is not None:
            n.clip(exp_env, env_lb, env_ub, out=exp_env)

        return exp_env
Esempio n. 3
0
    def get_envelope_mle(self,rotavg=False):
        N = self.cryodata.N

        mean_corr = self.correlation_history.get_mean()
        mean_power = self.power_history.get_mean()
        mean_mask = self.mask_history.get_mean()
        
        if rotavg:
            mean_corr = cryoem.rotational_average(mean_corr.reshape((N,N)),doexpand=True)
            mean_power = cryoem.rotational_average(mean_power.reshape((N,N)),doexpand=True)
            mean_mask = cryoem.rotational_average(mean_mask.reshape((N,N)),doexpand=True)

        obs_mask = mean_mask > 0
        exp_env = np.ones_like(mean_corr)
        exp_env[obs_mask] = (mean_corr[obs_mask] / mean_power[obs_mask])

        return exp_env.reshape((N,N))
Esempio n. 4
0
def rot_power_spectra(fM,powerLen = None,resolution = None):
    if resolution == None:
        resolution = 1

    powerfM = fM.real**2 + fM.imag**2 
    raps = cryoem.rotational_average(powerfM,powerLen)
    radius = np.linspace(0,len(raps)-1,len(raps))

    return (radius,raps)
Esempio n. 5
0
def rot_power_spectra(fM,powerLen = None,resolution = None):
    if resolution == None:
        resolution = 1

    powerfM = fM.real**2 + fM.imag**2 
    raps = c.rotational_average(powerfM,powerLen)
    radius = n.linspace(0,len(raps)-1,len(raps))

    return (radius,raps)
Esempio n. 6
0
    def get_envelope_map(self,sigma2,rho,env_lb=None,env_ub=None,minFreq=None,bfactor=None,rotavg=True):
        N = self.cryodata.N
        N_D = float(self.cryodata.N_D_Train)
        num_batches = float(self.cryodata.num_batches)
        psize = self.params['pixel_size']

        mean_corr = self.correlation_history.get_mean().reshape((N,N))
        mean_power = self.power_history.get_mean().reshape((N,N))
        mean_mask = self.mask_history.get_mean().reshape((N,N))
        mask_w = self.mask_history.get_wsum() * (N_D / num_batches)
        
        if rotavg:
            mean_corr = cryoem.rotational_average(mean_corr,normalize=True,doexpand=True)
            mean_power = cryoem.rotational_average(mean_power,normalize=True,doexpand=True)
            mean_mask = cryoem.rotational_average(mean_mask,normalize=False,doexpand=True)

        if isinstance(sigma2,np.ndarray):
            sigma2 = sigma2.reshape((N,N))

        if bfactor is not None:
            coords = gencoords(N,2).reshape((N**2,2))
            freqs = np.sqrt(np.sum(coords**2,axis=1))/(psize*N)
            prior_envelope = ctf.envelope_function(freqs,bfactor).reshape((N,N))
        else:
            prior_envelope = 1.0

        obsw = (mask_w * mean_mask / sigma2)
        exp_env = (mean_corr * obsw + prior_envelope*rho) / (mean_power * obsw + rho)
        
        if minFreq is not None:
            # Only consider envelope parameters for frequencies above a threshold
            minRad = minFreq*2.0*psize
    
            _, _, minRadMask = gencoords(N, 2, minRad, True)
            
            exp_env[minRadMask.reshape((N,N))] = 1.0
        
        if env_lb is not None or env_ub is not None:
            np.clip(exp_env,env_lb,env_ub,out=exp_env)

        return exp_env
Esempio n. 7
0
    def show_noise_plot(self,cfig):
        cdiag = self.diag
        cparams = cdiag['params']

        vox_size = cparams['voxel_size']
        name = cparams['name']
        maxfreq = cparams['max_frequency']
        N = self.M.shape[0]
        rad_cutoff = cparams.get('rad_cutoff', 1.0)
        rad = min(rad_cutoff,maxfreq*2.0*vox_size)

        startI = int((1-rad)*N/2)+1
        endI = N/2 + int(rad*N/2)+1
        imextent = [startI-(N+1.0)/2,endI-(N+1.0)/2,startI-(N+1.0)/2,endI-(N+1.0)/2]
        imextent = [e/(2.0*vox_size)/(N/2) for e in imextent]
        sigma_est = cparams['sigma']
        sigma_mle = np.sqrt(cdiag['sigma2_mle'])
        train_sigma_est = np.sqrt(cdiag['train_sigma2_est']).reshape((N,N))
        test_sigma_est = np.sqrt(cdiag['test_sigma2_est']).reshape((N,N))
        showsigma = isinstance(sigma_est,np.ndarray)
        vmin = min([np.min(sigma_est),sigma_mle[startI:endI,startI:endI].min()])
        vmax = max([np.max(sigma_est),sigma_mle[startI:endI,startI:endI].max()])
        
        imshow_kws = { 'interpolation':'nearest', \
                       'vmin':vmin, 'vmax':vmax, 'extent':imextent, \
                       'norm':LogNorm(vmin=vmin, vmax=vmax) }

        cbaxs = []
        plt.figure(cfig.number)
        plt.clf()

        plt.subplot(2,1,1)
        raps = np.sqrt(cryoem.rotational_average(train_sigma_est**2))
        fs = np.linspace(0,(len(raps)-1)/(N/2.0)/(2.0*vox_size),len(raps))
        plt.plot(fs,raps,label='Training RMSE')

        raps = np.sqrt(cryoem.rotational_average(test_sigma_est**2))
        fs = np.linspace(0,(len(raps)-1)/(N/2.0)/(2.0*vox_size),len(raps))
        plt.plot(fs,raps,label='Testing RMSE')
        plt.legend()
        plt.grid()

        if showsigma:
            cbaxs.append(plt.subplot(2,2,3))
        else:
            cbaxs.append(plt.subplot(2,1,2))
        im = plt.imshow(sigma_mle[startI:endI,startI:endI], **imshow_kws)
        plt.title('Freq RMSE (MLE)')

        if showsigma:
            sigma_est = sigma_est.reshape((N,N))
            cbaxs.append(plt.subplot(2,2,4))
            im = plt.imshow(sigma_est[startI:endI,startI:endI], **imshow_kws)
            plt.title('Coloured Noise Std Dev')

        plt.subplot(2,1,1)
        if showsigma:
            raps = np.sqrt(cryoem.rotational_average(sigma_est**2))
            fs = np.linspace(0,(len(raps)-1)/(N/2.0)/(2.0*vox_size),len(raps))
        else:
            raps = [sigma_est,sigma_est]
            fs = [fs[0],fs[-1]]
        plt.plot(fs,raps,label='Noise Std Dev')
        plt.xlim((0,rad/(2.0*vox_size)))
        plt.yscale('log',basey=2)
        plt.legend()
 
        plt.title(name + ' Noise Levels')

        plt.colorbar(im, ax=cbaxs, ticks=LogLocator(base=2), format=LogFormatterMathtext(base=2))
Esempio n. 8
0
    def show_envelope_plot(self,cfig):
        cdiag = self.diag
        cparams = cdiag['params']

        resolution = cparams['pixel_size']
        name = cparams['name']
        maxfreq = cparams['max_frequency']
        N = self.M.shape[0]
        rad_cutoff = cparams.get('rad_cutoff', 1.0)
        rad = min(rad_cutoff,maxfreq*2.0*resolution)
    
        envelope_mle = cdiag['envelope_mle']
        vmin = envelope_mle.min()
        vmax = envelope_mle.max()
        exp_envelope = cparams.get('exp_envelope',None)
        
        have_exp = exp_envelope is not None
        if have_exp:
            vmin = exp_envelope.min()
            vmax = exp_envelope.max()
        
        startI = int((1-rad)*N/2)+1
        endI = N/2 + int(rad*N/2)+1
        imextent = [startI-(N+1.0)/2,endI-(N+1.0)/2,startI-(N+1.0)/2,endI-(N+1.0)/2]
        imextent = [e/(2.0*resolution)/(N/2) for e in imextent]

        cbaxs = []

        plt.figure(cfig.number)
        plt.clf()

        cbaxs.append(plt.subplot(2,1+have_exp,1))
        im = plt.imshow(envelope_mle[startI:endI,startI:endI], interpolation='nearest',
                        vmin=vmin, vmax=vmax, extent=imextent)
        plt.title('ML')
        
        if have_exp:
            cbaxs.append(plt.subplot(2,2,2))
            plt.imshow(exp_envelope[startI:endI,startI:endI], interpolation='nearest',
                       vmin=vmin, vmax=vmax, extent=imextent)
            plt.title('MAP')
        plt.colorbar(im,ax=cbaxs)
        
        env_max = 1.0
        env_min = 0.0
        bfactor = cparams.get('learn_like_envelope_bfactor',500.0)
        have_bfactor = bfactor is not None 
        if have_bfactor:
            plt.subplot(2,1,2)
            (fs,bfactor_env) = get_env_func(N, resolution=resolution, 
                                            bfactor=bfactor)
    
            plt.plot(fs,bfactor_env,label='Prior (bfactor {0})'.format(bfactor),linewidth=2)
            env_max = max(env_max,bfactor_env.max())
            env_min = min(env_min,bfactor_env.min())
        else:
            fs = np.linspace(0,1.0/(2.0*resolution),N/2)
            bfactor_env = 1.0

        ra_mle_envelope = cryoem.rotational_average(envelope_mle,maxRadius=N/2)
        plt.plot(fs[0:int(rad*N/2)],ra_mle_envelope[0:int(rad*N/2)],label='ML')
        env_max = max(env_max,ra_mle_envelope.max())
        env_min = min(env_min,ra_mle_envelope.min())
        if have_exp:
            ra_exp_envelope = cryoem.rotational_average(exp_envelope,maxRadius=N/2) 
            plt.plot(fs[0:N/2],ra_exp_envelope[0:N/2],label='MAP',linewidth=2)
            env_max = max(env_max,ra_exp_envelope.max())
            env_min = min(env_min,ra_exp_envelope.min())
        plt.legend()
        plt.grid()

        plt.plot((rad/(2.0*resolution))*np.ones((2,)), np.array([env_min,env_max]))
            
        plt.suptitle(name + ' Envelope')
Esempio n. 9
0
    def show_noise_plot(self,cfig):
        cdiag = self.diag
        cparams = cdiag['params']

        vox_size = cparams['voxel_size']
        name = cparams['name']
        maxfreq = cparams['max_frequency']
        N = self.M.shape[0]
        rad_cutoff = cparams.get('rad_cutoff', 1.0)
        rad = min(rad_cutoff,maxfreq*2.0*vox_size)

        startI = int((1-rad)*N/2)+1
        endI = N/2 + int(rad*N/2)+1
        imextent = [startI-(N+1.0)/2,endI-(N+1.0)/2,startI-(N+1.0)/2,endI-(N+1.0)/2]
        imextent = [e/(2.0*vox_size)/(N/2) for e in imextent]
        sigma_est = cparams['sigma']
        sigma_mle = n.sqrt(cdiag['sigma2_mle'])
        train_sigma_est = n.sqrt(cdiag['train_sigma2_est']).reshape((N,N))
        test_sigma_est = n.sqrt(cdiag['test_sigma2_est']).reshape((N,N))
        showsigma = isinstance(sigma_est,n.ndarray)
        vmin = min([n.min(sigma_est),sigma_mle[startI:endI,startI:endI].min()])
        vmax = max([n.max(sigma_est),sigma_mle[startI:endI,startI:endI].max()])
        
        imshow_kws = { 'interpolation':'nearest', \
                       'vmin':vmin, 'vmax':vmax, 'extent':imextent, \
                       'norm':LogNorm(vmin=vmin, vmax=vmax) }

        cbaxs = []
        plt.figure(cfig.number)
        plt.clf()

        plt.subplot(2,1,1)
        raps = n.sqrt(c.rotational_average(train_sigma_est**2))
        fs = n.linspace(0,(len(raps)-1)/(N/2.0)/(2.0*vox_size),len(raps))
        plt.plot(fs,raps,label='Training RMSE')

        raps = n.sqrt(c.rotational_average(test_sigma_est**2))
        fs = n.linspace(0,(len(raps)-1)/(N/2.0)/(2.0*vox_size),len(raps))
        plt.plot(fs,raps,label='Testing RMSE')
        plt.legend()
        plt.grid()

        if showsigma:
            cbaxs.append(plt.subplot(2,2,3))
        else:
            cbaxs.append(plt.subplot(2,1,2))
        im = plt.imshow(sigma_mle[startI:endI,startI:endI], **imshow_kws)
        plt.title('Freq RMSE (MLE)')

        if showsigma:
            sigma_est = sigma_est.reshape((N,N))
            cbaxs.append(plt.subplot(2,2,4))
            im = plt.imshow(sigma_est[startI:endI,startI:endI], **imshow_kws)
            plt.title('Coloured Noise Std Dev')

        plt.subplot(2,1,1)
        if showsigma:
            raps = n.sqrt(c.rotational_average(sigma_est**2))
            fs = n.linspace(0,(len(raps)-1)/(N/2.0)/(2.0*vox_size),len(raps))
        else:
            raps = [sigma_est,sigma_est]
            fs = [fs[0],fs[-1]]
        plt.plot(fs,raps,label='Noise Std Dev')
        plt.xlim((0,rad/(2.0*vox_size)))
        plt.yscale('log',basey=2)
        plt.legend()
 
        plt.title(name + ' Noise Levels')

        plt.colorbar(im, ax=cbaxs, ticks=LogLocator(base=2), format=LogFormatterMathtext(base=2))
Esempio n. 10
0
    def show_envelope_plot(self,cfig):
        cdiag = self.diag
        cparams = cdiag['params']

        resolution = cparams['pixel_size']
        name = cparams['name']
        maxfreq = cparams['max_frequency']
        N = self.M.shape[0]
        rad_cutoff = cparams.get('rad_cutoff', 1.0)
        rad = min(rad_cutoff,maxfreq*2.0*resolution)
    
        envelope_mle = cdiag['envelope_mle']
        vmin = envelope_mle.min()
        vmax = envelope_mle.max()
        exp_envelope = cparams.get('exp_envelope',None)
        
        have_exp = exp_envelope is not None
        if have_exp:
            vmin = exp_envelope.min()
            vmax = exp_envelope.max()
        
        startI = int((1-rad)*N/2)+1
        endI = N/2 + int(rad*N/2)+1
        imextent = [startI-(N+1.0)/2,endI-(N+1.0)/2,startI-(N+1.0)/2,endI-(N+1.0)/2]
        imextent = [e/(2.0*resolution)/(N/2) for e in imextent]

        cbaxs = []

        plt.figure(cfig.number)
        plt.clf()

        cbaxs.append(plt.subplot(2,1+have_exp,1))
        im = plt.imshow(envelope_mle[startI:endI,startI:endI], interpolation='nearest',
                        vmin=vmin, vmax=vmax, extent=imextent)
        plt.title('ML')
        
        if have_exp:
            cbaxs.append(plt.subplot(2,2,2))
            plt.imshow(exp_envelope[startI:endI,startI:endI], interpolation='nearest',
                       vmin=vmin, vmax=vmax, extent=imextent)
            plt.title('MAP')
        plt.colorbar(im,ax=cbaxs)
        
        env_max = 1.0
        env_min = 0.0
        bfactor = cparams.get('learn_like_envelope_bfactor',500.0)
        have_bfactor = bfactor is not None 
        if have_bfactor:
            plt.subplot(2,1,2)
            (fs,bfactor_env) = get_env_func(N, resolution=resolution, 
                                            bfactor=bfactor)
    
            plt.plot(fs,bfactor_env,label='Prior (bfactor {0})'.format(bfactor),linewidth=2)
            env_max = max(env_max,bfactor_env.max())
            env_min = min(env_min,bfactor_env.min())
        else:
            fs = n.linspace(0,1.0/(2.0*resolution),N/2)
            bfactor_env = 1.0

        ra_mle_envelope = c.rotational_average(envelope_mle,maxRadius=N/2)
        plt.plot(fs[0:int(rad*N/2)],ra_mle_envelope[0:int(rad*N/2)],label='ML')
        env_max = max(env_max,ra_mle_envelope.max())
        env_min = min(env_min,ra_mle_envelope.min())
        if have_exp:
            ra_exp_envelope = c.rotational_average(exp_envelope,maxRadius=N/2) 
            plt.plot(fs[0:N/2],ra_exp_envelope[0:N/2],label='MAP',linewidth=2)
            env_max = max(env_max,ra_exp_envelope.max())
            env_min = min(env_min,ra_exp_envelope.min())
        plt.legend()
        plt.grid()

        plt.plot((rad/(2.0*resolution))*n.ones((2,)), n.array([env_min,env_max]))
            
        plt.suptitle(name + ' Envelope')