def sagd_init(data_dir, model_file, use_angular_correlation=False): data_params = { 'dataset_name': "1AON", 'inpath': os.path.join(data_dir, 'imgdata.mrc'), 'ctfpath': os.path.join(data_dir, 'defocus.txt'), 'microscope_params': { 'akv': 200, 'wgh': 0.07, 'cs': 2.0 }, 'resolution': 2.8, 'sigma': 'noise_std', 'sigma_out': 'data_std', 'minisize': 20, 'test_imgs': 20, 'partition': 0, 'num_partitions': 0, 'random_seed': 1, # 'symmetry': 'C7' } # Setup dataset print("Loading dataset %s" % data_dir) cryodata, _ = dataset_loading_test(data_params) # mleDC, _, mleDC_est_std = cryodata.get_dc_estimate() # modelscale = (np.abs(mleDC) + 2*mleDC_est_std)/cryodata.N modelscale = 1.0 if model_file is not None: print("Loading density map %s" % model_file) M = readMRC(model_file) else: print("Generating random initial density map ...") M = cryoem.generate_phantom_density(cryodata.N, 0.95 * cryodata.N / 2.0, \ 5 * cryodata.N / 128.0, 30, seed=0) M *= modelscale / M.sum() slice_interp = { 'kern': 'lanczos', 'kernsize': 4, 'zeropad': 0, 'dopremult': True } # fM = SimpleKernel.get_fft(M, slice_interp) M_totalmass = 5000 M *= M_totalmass / M.sum() N = M.shape[0] kernel = 'lanczos' ksize = 6 premult = cryoops.compute_premultiplier(N, kernel, ksize) V = density.real_to_fspace( premult.reshape((1, 1, -1)) * premult.reshape( (1, -1, 1)) * premult.reshape((-1, 1, 1)) * M) M = V.real**2 + V.imag**2 freqs_3d = geometry.gencoords_base(N, 3) / (N * data_params['resolution']) freq_radius_3d = np.sqrt((freqs_3d**2).sum(axis=1)) mask_3d_outlier = np.require(np.float_(freq_radius_3d > 0.015).reshape( (N, N, N)), dtype=density.real_t) fM = M * mask_3d_outlier cparams = { 'use_angular_correlation': use_angular_correlation, 'likelihood': 'UnknownRSLikelihood()', 'kernel': 'multicpu', 'prior_name': "'Null'", 'sparsity_lambda': 0.9, 'prior': 'NullPrior()', # 'prior_name': "'CAR'", # 'prior': 'CARPrior()', # 'car_type': 'gauss0.5', # 'car_tau': 75.0, 'iteration': 0, 'pixel_size': cryodata.pixel_size, 'max_frequency': 0.02, 'num_batches': cryodata.N_batches, 'interp_kernel_R': 'lanczos', 'interp_kernel_size_R': 4, 'interp_zeropad_R': 0.0, 'interp_premult_R': True, 'interp_kernel_I': 'lanczos', 'interp_kernel_size_I': 8, 'interp_zeropad_I': 0.0, # 1.0, 'interp_premult_I': True, 'sigma': cryodata.noise_var, 'modelscale': modelscale, # 'symmetry': 'C7' } is_params = { # importance sampling # Ignore the first 50 iterations entirely 'is_prior_prob': max(0.05, 2**(-0.005 * max(0, cparams['iteration'] - 50))), 'is_temperature': max(1.0, 2**(750.0 / max(1, cparams['iteration'] - 50))), 'is_ess_scale': 10, 'is_fisher_chirality_flip': cparams['iteration'] < 2500, 'is_on_R': True, 'is_global_prob_R': 0.9, 'is_on_I': True, 'is_global_prob_I': 1e-10, 'is_on_S': True, 'is_global_prob_S': 0.9, 'is_gaussian_sigmascale_S': 0.67, } cparams.update(is_params) return cryodata, (M, fM), cparams
def __init__(self, expbase, cmdparams=None): """cryodata is a CryoData instance. expbase is a path to the base of folder where this experiment's files will be stored. The folder above expbase will also be searched for .params files. These will be loaded first.""" BackgroundWorker.__init__(self) # Create a background thread which handles IO self.io_queue = Queue() self.io_thread = Thread(target=self.ioworker) self.io_thread.daemon = True self.io_thread.start() # General setup ---------------------------------------------------- self.expbase = expbase self.outbase = None # Paramter setup --------------------------------------------------- # search above expbase for params files _,_,filenames = os.walk(opj(expbase,'../')).next() self.paramfiles = [opj(opj(expbase,'../'), fname) \ for fname in filenames if fname.endswith('.params')] # search expbase for params files _,_,filenames = os.walk(opj(expbase)).next() self.paramfiles += [opj(expbase,fname) \ for fname in filenames if fname.endswith('.params')] if 'local.params' in filenames: self.paramfiles += [opj(expbase,'local.params')] # load parameter files self.params = Params(self.paramfiles) self.cparams = None if cmdparams is not None: # Set parameter specified on the command line for k,v in cmdparams.iteritems(): self.params[k] = v # Dataset setup ------------------------------------------------------- self.imgpath = self.params['inpath'] psize = self.params['resolution'] if not isinstance(self.imgpath,list): imgstk = MRCImageStack(self.imgpath,psize) else: imgstk = CombinedImageStack([MRCImageStack(cimgpath,psize) for cimgpath in self.imgpath]) if self.params.get('float_images',True): imgstk.float_images() self.ctfpath = self.params['ctfpath'] mscope_params = self.params['microscope_params'] if not isinstance(self.ctfpath,list): ctfstk = CTFStack(self.ctfpath,mscope_params) else: ctfstk = CombinedCTFStack([CTFStack(cctfpath,mscope_params) for cctfpath in self.ctfpath]) self.cryodata = CryoDataset(imgstk,ctfstk) self.cryodata.compute_noise_statistics() if self.params.get('window_images',True): imgstk.window_images() minibatch_size = self.params['minisize'] testset_size = self.params['test_imgs'] partition = self.params.get('partition',0) num_partitions = self.params.get('num_partitions',1) seed = self.params['random_seed'] if isinstance(partition,str): partition = eval(partition) if isinstance(num_partitions,str): num_partitions = eval(num_partitions) if isinstance(seed,str): seed = eval(seed) self.cryodata.divide_dataset(minibatch_size,testset_size,partition,num_partitions,seed) self.cryodata.set_datasign(self.params.get('datasign','auto')) if self.params.get('normalize_data',True): self.cryodata.normalize_dataset() self.voxel_size = self.cryodata.pixel_size # Iterations setup ------------------------------------------------- self.iteration = 0 self.tic_epoch = None self.num_data_evals = 0 self.eval_params() outdir = self.cparams.get('outdir',None) if outdir is None: if self.cparams.get('num_partitions',1) > 1: outdir = 'partition{0}'.format(self.cparams['partition']) else: outdir = '' self.outbase = opj(self.expbase,outdir) if not os.path.isdir(self.outbase): os.makedirs(self.outbase) # Output setup ----------------------------------------------------- self.ostream = OutputStream(opj(self.outbase,'stdout')) self.ostream(80*"=") self.ostream("Experiment: " + expbase + \ " Kernel: " + self.params['kernel']) self.ostream("Started on " + socket.gethostname() + \ " At: " + time.strftime('%B %d %Y: %I:%M:%S %p')) self.ostream("Git SHA1: " + gitutil.git_get_SHA1()) self.ostream(80*"=") gitutil.git_info_dump(opj(self.outbase, 'gitinfo')) self.startdatetime = datetime.now() # for diagnostics and parameters self.diagout = Output(opj(self.outbase, 'diag'),runningout=False) # for stats (per image etc) self.statout = Output(opj(self.outbase, 'stat'),runningout=True) # for likelihoods of individual images self.likeout = Output(opj(self.outbase, 'like'),runningout=False) self.img_likes = n.empty(self.cryodata.N_D) self.img_likes[:] = n.inf # optimization state vars ------------------------------------------ init_model = self.cparams.get('init_model',None) if init_model is not None: filename = init_model if filename.upper().endswith('.MRC'): M = readMRC(filename) else: with open(filename) as fp: M = cPickle.load(fp) if type(M)==list: M = M[-1]['M'] if M.shape != 3*(self.cryodata.N,): M = cryoem.resize_ndarray(M,3*(self.cryodata.N,),axes=(0,1,2)) else: init_seed = self.cparams.get('init_random_seed',0) + self.cparams.get('partition',0) print "Randomly generating initial density (init_random_seed = {0})...".format(init_seed), ; sys.stdout.flush() tic = time.time() M = cryoem.generate_phantom_density(self.cryodata.N, 0.95*self.cryodata.N/2.0, \ 5*self.cryodata.N/128.0, 30, seed=init_seed) print "done in {0}s".format(time.time() - tic) tic = time.time() print "Windowing and aligning initial density...", ; sys.stdout.flush() # window the initial density wfunc = self.cparams.get('init_window','circle') cryoem.window(M,wfunc) # Center and orient the initial density cryoem.align_density(M) print "done in {0:.2f}s".format(time.time() - tic) # apply the symmetry operator init_sym = get_symmetryop(self.cparams.get('init_symmetry',self.cparams.get('symmetry',None))) if init_sym is not None: tic = time.time() print "Applying symmetry operator...", ; sys.stdout.flush() M = init_sym.apply(M) print "done in {0:.2f}s".format(time.time() - tic) tic = time.time() print "Scaling initial model...", ; sys.stdout.flush() modelscale = self.cparams.get('modelscale','auto') mleDC, _, mleDC_est_std = self.cryodata.get_dc_estimate() if modelscale == 'auto': # Err on the side of a weaker prior by using a larger value for modelscale modelscale = (n.abs(mleDC) + 2*mleDC_est_std)/self.cryodata.N print "estimated modelscale = {0:.3g}...".format(modelscale), ; sys.stdout.flush() self.params['modelscale'] = modelscale self.cparams['modelscale'] = modelscale M *= modelscale/M.sum() print "done in {0:.2f}s".format(time.time() - tic) if mleDC_est_std/n.abs(mleDC) > 0.05: print " WARNING: the DC component estimate has a high relative variance, it may be inaccurate!" if ((modelscale*self.cryodata.N - n.abs(mleDC)) / mleDC_est_std) > 3: print " WARNING: the selected modelscale value is more than 3 std devs different than the estimated one. Be sure this is correct." self.M = n.require(M,dtype=density.real_t) self.fM = density.real_to_fspace(M) self.dM = density.zeros_like(self.M) self.step = eval(self.cparams['optim_algo']) self.step.setup(self.cparams, self.diagout, self.statout, self.ostream) # Objective function setup -------------------------------------------- param_type = self.cparams.get('parameterization','real') cplx_param = param_type in ['complex','complex_coeff','complex_herm_coeff'] self.like_func = eval_objective(self.cparams['likelihood']) self.prior_func = eval_objective(self.cparams['prior']) if self.cparams.get('penalty',None) is not None: self.penalty_func = eval_objective(self.cparams['penalty']) prior_func = SumObjectives(self.prior_func.fspace, \ [self.penalty_func,self.prior_func], None) else: prior_func = self.prior_func self.obj = SumObjectives(cplx_param, [self.like_func,prior_func], [None,None]) self.obj.setup(self.cparams, self.diagout, self.statout, self.ostream) self.obj.set_dataset(self.cryodata) self.obj_wrapper = ObjectiveWrapper(param_type) self.last_save = time.time() self.logpost_history = FiniteRunningSum() self.like_history = FiniteRunningSum() # Importance Samplers ------------------------------------------------- self.is_sym = get_symmetryop(self.cparams.get('is_symmetry',self.cparams.get('symmetry',None))) self.sampler_R = FixedFisherImportanceSampler('_R',self.is_sym) self.sampler_I = FixedFisherImportanceSampler('_I') self.sampler_S = FixedGaussianImportanceSampler('_S') self.like_func.set_samplers(sampler_R=self.sampler_R,sampler_I=self.sampler_I,sampler_S=self.sampler_S)
def load_kernel(data_dir, model_file, use_angular_correlation=False, sample_shifts=False): data_params = { 'dataset_name': "1AON", 'inpath': os.path.join(data_dir, 'imgdata.mrc'), 'ctfpath': os.path.join(data_dir, 'defocus.txt'), 'microscope_params': {'akv': 200, 'wgh': 0.07, 'cs': 2.0}, 'resolution': 2.8, 'sigma': 'noise_std', 'sigma_out': 'data_std', 'minisize': 20, 'test_imgs': 20, 'partition': 0, 'num_partitions': 0, 'random_seed': 1, # 'symmetry': 'C7' } print("Loading dataset %s" % data_dir) cryodata, _ = dataset_loading_test(data_params) # mleDC, _, mleDC_est_std = cryodata.get_dc_estimate() # modelscale = (np.abs(mleDC) + 2*mleDC_est_std)/cryodata.N modelscale = 1.0 if model_file is not None: print("Loading density map %s" % model_file) M = mrc.readMRC(model_file) else: print("Generating random initial density map ...") M = cryoem.generate_phantom_density(cryodata.N, 0.95 * cryodata.N / 2.0, \ 5 * cryodata.N / 128.0, 30, seed=0) M *= modelscale/M.sum() slice_interp = {'kern': 'lanczos', 'kernsize': 4, 'zeropad': 0, 'dopremult': True} fM = M minibatch = cryodata.get_next_minibatch(shuffle_minibatches=False) is_sym = get_symmetryop(data_params.get('symmetry',None)) sampler_R = FixedFisherImportanceSampler('_R', is_sym) sampler_I = FixedFisherImportanceSampler('_I') sampler_S = None cparams = { 'use_angular_correlation': use_angular_correlation, 'iteration': 0, 'pixel_size': cryodata.pixel_size, 'max_frequency': 0.02, 'interp_kernel_R': 'lanczos', 'interp_kernel_size_R': 4, 'interp_zeropad_R': 0, 'interp_premult_R': True, 'interp_kernel_I': 'lanczos', 'interp_kernel_size_I': 4, 'interp_zeropad_I': 0.0, 'interp_premult_I': True, 'quad_shiftsigma': 10, 'quad_shiftextent': 60, 'sigma': cryodata.noise_var, # 'symmetry': 'C7' } kernel = UnknownRSThreadedCPUKernel() kernel.setup(cparams, None, None, None) kernel.set_samplers(sampler_R, sampler_I, sampler_S) kernel.set_dataset(cryodata) kernel.precomp_slices = None kernel.set_data(cparams, minibatch) kernel.using_precomp_slicing = False kernel.using_precomp_inplane = False kernel.M = M kernel.fM = fM return kernel