Exemple #1
0
    def set_quad(self, rad):
        if self.rad == rad:
            print("Using previous quadurature schem")
        else:
            tic = time.time()
            # set slicing interpolation parameters
            self.slice_params = {'quad_type': 'sk97'}
            self.slice_interp = {'N': self.N, 'kern': 'lanczos', 'kernsize': 6, 'rad': rad, 'zeropad': 0, 'dopremult': True} #, 'onlyRs': True}
            # set slicing quadrature
            usFactor_R = 1.0
            quad_R = quadrature.quad_schemes[('dir', self.slice_params['quad_type'])]
            const_quad_rad = 0.45 # adjust quadrature
            degree_R, resolution_R = quad_R.compute_degree(self.N, const_quad_rad, usFactor_R)
            # print(np.rad2deg(resolution_R))
            slice_quad = {}
            slice_quad['resolution'] = max(0.5*quadrature.compute_max_angle(self.N, rad), resolution_R)
            # slice_quad['resolution'] = np.rad2deg(resolution_R)
            slice_quad['dir'], slice_quad['W'] = quad_R.get_quad_points(degree_R, self.is_sym)
            self.slice_quad = slice_quad
            self.quad_domain_R = quadrature.FixedSphereDomain(slice_quad['dir'], slice_quad['resolution'], sym=self.is_sym)
            # generate slicing operators (points on sphere)
            self.slice_ops = self.quad_domain_R.compute_operator(self.slice_interp)
            self.N_R = len(self.quad_domain_R)
            # print("  Slice Ops: %d, resolution: %.2f degree, generated in: %.4f seconds" \
            #     % (self.N_R, np.rad2deg(self.quad_domain_R.resolution), time.time()-tic))

            tic = time.time()
            # set inplane interpolation parameters
            self.inplane_interp = {'N': self.N, 'kern': 'lanczos', 'kernsize': 6, 'rad': rad, 'zeropad': 0, 'dopremult': True} #, 'onlyRs': True}
            # set inplane quadrature
            usFactor_I = 1.0
            maxAngle = quadrature.compute_max_angle(self.N, const_quad_rad, usFactor_I)
            degree_I = np.uint32(np.ceil(2.0 * np.pi / maxAngle))
            resolution_I = max(0.5*quadrature.compute_max_angle(self.N, rad), 2.0*np.pi / degree_I)
            inplane_quad = {}
            inplane_quad['resolution'] = resolution_I
            inplane_quad['thetas'] = np.linspace(0, 2.0*np.pi, degree_I, endpoint=False)
            # inplane_quad['thetas'] += inplane_quad['thetas'][1]/2.0
            inplane_quad['W'] = np.require((2.0*np.pi/float(degree_I))*np.ones((degree_I,)), dtype=np.float32)
            self.inplane_quad = inplane_quad
            self.quad_domain_I = quadrature.FixedCircleDomain(inplane_quad['thetas'],
                                                            inplane_quad['resolution'])
            # generate inplane operators
            self.inplane_ops = self.quad_domain_I.compute_operator(self.inplane_interp)
            self.N_I = len(self.quad_domain_I)
            # print("  Inplane Ops: %d, resolution: %.2f degree, generated in: %.4f seconds." \
            #     % (self.N_I, np.rad2deg(self.quad_domain_I.resolution), time.time()-tic))

            transform_change = self.cryodata.interp_params['kern'] != self.inplane_interp['kern'] or \
                            self.cryodata.interp_params['kernsize'] != self.inplane_interp['kernsize'] or \
                            self.cryodata.interp_params['zeropad'] != self.inplane_interp['zeropad'] or \
                            self.cryodata.interp_params['dopremult'] != self.inplane_interp['dopremult']
            if transform_change:
                self.cryodata.set_transform(self.inplane_interp)

            self.rad = rad
Exemple #2
0
def get_quadrature(N=124, const_rad=0.8):

    is_sym = None
    slice_params = {'quad_type': 'sk97'}

    tic = time.time()
    # set slicing quadrature
    usFactor_R = 1.0
    quad_R = quadrature.quad_schemes[('dir', slice_params['quad_type'])]
    degree_R, resolution_R = quad_R.compute_degree(N, const_rad, usFactor_R)
    # print(np.rad2deg(resolution_R))
    slice_quad = {}
    slice_quad['resolution'] = max(
        0.5 * quadrature.compute_max_angle(N, const_rad), resolution_R)
    # slice_quad['resolution'] = np.rad2deg(resolution_R)
    slice_quad['dir'], slice_quad['W'] = quad_R.get_quad_points(
        degree_R, is_sym)
    slice_quad = slice_quad
    quad_domain_R = quadrature.FixedSphereDomain(slice_quad['dir'],
                                                 slice_quad['resolution'],
                                                 sym=is_sym)
    N_R = len(quad_domain_R)
    print("  Slice Ops: %d, resolution: %.2f degree, generated in: %.4f seconds" \
                % (N_R, np.rad2deg(quad_domain_R.resolution), time.time()-tic))

    tic = time.time()
    # set inplane quadrature
    usFactor_I = 1.0
    maxAngle = quadrature.compute_max_angle(N, const_rad, usFactor_I)
    degree_I = np.uint32(np.ceil(2.0 * np.pi / maxAngle))
    resolution_I = max(0.5 * quadrature.compute_max_angle(N, const_rad),
                       2.0 * np.pi / degree_I)
    inplane_quad = {}
    inplane_quad['resolution'] = resolution_I
    inplane_quad['thetas'] = np.linspace(0,
                                         2.0 * np.pi,
                                         degree_I,
                                         endpoint=False)
    # inplane_quad['thetas'] += inplane_quad['thetas'][1]/2.0
    inplane_quad['W'] = np.require((2.0 * np.pi / float(degree_I)) * np.ones(
        (degree_I, )),
                                   dtype=np.float32)
    inplane_quad = inplane_quad
    quad_domain_I = quadrature.FixedCircleDomain(inplane_quad['thetas'],
                                                 inplane_quad['resolution'])
    # generate inplane operators
    N_I = len(quad_domain_I)
    print("  Inplane Ops: %d, resolution: %.2f degree, generated in: %.4f seconds." \
        % (N_I, np.rad2deg(quad_domain_I.resolution), time.time()-tic))

    return quad_domain_R, quad_domain_I
Exemple #3
0
    def set_inplane_quad(self,rad):
        # Get (and generate if needed) the quadrature scheme for inplane rotation 
        params = self.params

        tic = time.time()
        
        degree_I = params.get('quad_degree_I','auto')
        usFactor_I = params.get('quad_undersample_I',params.get('quad_undersample',1.0))

        kern_I = params.get('interp_kernel_I',params.get('interp_kernel',None))
        kernsize_I = params.get('interp_kernel_size_I',params.get('interp_kernel_size',None))
        zeropad_I = params.get('interp_zeropad_I',params.get('interp_zeropad',0))
        dopremult_I = params.get('interp_premult_I',params.get('interp_premult',True))

        maxAngle = quadrature.compute_max_angle(self.N,rad,usFactor_I)
        if degree_I == 'auto':
            degree_I = np.uint32(np.ceil(2.0 * np.pi / maxAngle))
        resolution_I = max(0.5*quadrature.compute_max_angle(self.N,rad), 2.0*np.pi / degree_I)

        inplane_params = { 'degree':degree_I }
        interp_params_I = { 'N':self.N, 'kern':kern_I, 'kernsize':kernsize_I, 'rad':rad, 'zeropad':zeropad_I, 'dopremult':dopremult_I }
        
        domain_change_I = self.inplane_params != inplane_params
        interp_change_I = self.inplane_interp != interp_params_I
        transform_change = self.inplane_interp is None or \
                        self.inplane_interp['kern'] != interp_params_I['kern'] or \
                        self.inplane_interp['kernsize'] != interp_params_I['kernsize'] or \
                        self.inplane_interp['zeropad'] != interp_params_I['zeropad']

        if domain_change_I:
            inplane_quad = {}
            inplane_quad['resolution'] = resolution_I
            inplane_quad['thetas'] = np.linspace(0, 2.0*np.pi, degree_I, endpoint=False)
            inplane_quad['thetas'] += inplane_quad['thetas'][1]/2.0
            inplane_quad['W'] = np.require((2.0*np.pi/float(degree_I))*np.ones((degree_I,)),dtype=np.float32)

            self.quad_domain_I = quadrature.FixedCircleDomain(inplane_quad['thetas'],
                                                              inplane_quad['resolution'])
            
            self.N_I = len(self.quad_domain_I)
            self.sampler_I.setup(params, self.N_D, self.N_D_Train, self.quad_domain_I)
            self.inplane_quad = inplane_quad
            self.inplane_params = inplane_params

        if domain_change_I or interp_change_I:
            print("  Inplane Ops: %d, " % self.N_I); sys.stdout.flush()
            if self.N_I < self.otf_thresh_I:
                self.using_precomp_inplane = True
                print("generated in", end=''); sys.stdout.flush()
                self.inplane_ops = self.quad_domain_I.compute_operator(interp_params_I)
                print(" {0} secs.".format(time.time() - tic))
            else:
                self.using_precomp_inplane = False
                print("generating OTF.")
                self.inplane_ops = None
            self.inplane_interp = interp_params_I

                    
        if transform_change:
            if dopremult_I:
                premult = cryoops.compute_premultiplier(self.N + 2*int(interp_params_I['zeropad']*(self.N/2)),
                                                        interp_params_I['kern'],interp_params_I['kernsize'])
                premult = premult.reshape((-1,1)) * premult.reshape((1,-1))
            else:
                premult = None
            self.fspace_stack.set_transform(premult,interp_params_I['zeropad'])
Exemple #4
0
    def set_slice_quad(self,rad):
        # Get (and generate if needed) the quadrature scheme for slicing
        params = self.params

        tic = time.time()

        N = self.N
        degree_R = params.get('quad_degree_R','auto')
        quad_scheme_R = params.get('quad_type_R','sk97')
        sym = get_symmetryop(params.get('symmetry',None)) if params.get('perfect_symmetry',True) else None
        usFactor_R = params.get('quad_undersample_R',params.get('quad_undersample',1.0))

        kern_R = params.get('interp_kernel_R',params.get('interp_kernel',None))
        kernsize_R = params.get('interp_kernel_size_R',params.get('interp_kernel_size',None))
        zeropad_R = params.get('interp_zeropad_R',params.get('interp_zeropad',0))
        dopremult_R = params.get('interp_premult_R',params.get('interp_premult',True))

        quad_R = quadrature.quad_schemes[('dir',quad_scheme_R)]

        if degree_R == 'auto':
            degree_R,resolution_R = quad_R.compute_degree(N,rad,usFactor_R)
        resolution_R = max(0.5*quadrature.compute_max_angle(self.N,rad), resolution_R)

        slice_params = { 'quad_type':quad_scheme_R, 'degree':degree_R, 
                         'sym':sym }
        interp_params_R = { 'N':self.N, 'kern':kern_R, 'kernsize':kernsize_R, 'rad':rad, 'zeropad':zeropad_R, 'dopremult':dopremult_R }
        
        domain_change_R = slice_params != self.slice_params
        interp_change_R = self.slice_interp != interp_params_R
        transform_change = self.slice_interp is None or \
                        self.slice_interp['kern'] != interp_params_R['kern'] or \
                        self.slice_interp['kernsize'] != interp_params_R['kernsize'] or \
                        self.slice_interp['zeropad'] != interp_params_R['zeropad']

        if domain_change_R:
            slice_quad = {}

            slice_quad['resolution'] = resolution_R
            slice_quad['degree'] = degree_R
            slice_quad['symop'] = sym

            slice_quad['dir'],slice_quad['W'] = quad_R.get_quad_points(degree_R,slice_quad['symop'])
            slice_quad['W'] = np.require(slice_quad['W'], dtype=np.float32)

            self.quad_domain_R = quadrature.FixedSphereDomain(slice_quad['dir'],
                                                              slice_quad['resolution'],\
                                                              sym=sym)
            self.N_R = len(self.quad_domain_R)
            self.sampler_R.setup(params, self.N_D, self.N_D_Train, self.quad_domain_R)
            
            self.slice_quad = slice_quad
            self.slice_params = slice_params

        if domain_change_R or interp_change_R:
            symorder = 1 if self.slice_quad['symop'] is None else self.slice_quad['symop'].get_order()
            print("  Slice Ops: %d, " % self.N_R); sys.stdout.flush()
            if self.N_R*symorder < self.otf_thresh_R:
                self.using_precomp_slicing = True
                print("generated in", end=''); sys.stdout.flush()
                self.slice_ops = self.quad_domain_R.compute_operator(interp_params_R)
                print(" {0} secs.".format(time.time() - tic))

                Gsz = (self.N_R,self.N_T)
                self.G = np.empty(Gsz, dtype=self.G_datatype)
                self.slices = np.empty(np.prod(Gsz), dtype=np.complex64)
            else:
                self.using_precomp_slicing = False
                print("generating OTF.")
                self.slice_ops = None
                self.G = np.empty((self.N,self.N,self.N),dtype=self.G_datatype)
                self.slices = None
            self.slice_interp = interp_params_R

        if transform_change:
            if dopremult_R:
                premult = cryoops.compute_premultiplier(self.N + 2*int(interp_params_R['zeropad']*(self.N/2)), 
                                                        interp_params_R['kern'],interp_params_R['kernsize'])
                premult = premult.reshape((-1,1,1)) * premult.reshape((1,-1,1)) * premult.reshape((1,1,-1))
            else:
                premult = None
            self.slice_premult = premult
            self.slice_zeropad = interp_params_R['zeropad']
            assert interp_params_R['zeropad'] == 0,'Zero padding for slicing not yet implemented'
Exemple #5
0
    def set_proj_quad(self,rad):
        # Get (and generate if needed) the quadrature scheme for slicing
        params = self.params

        tic = time.time()

        N = self.N

        quad_scheme_R = params.get('quad_type_R','sk97')
        quad_R = quadrature.quad_schemes[('dir',quad_scheme_R)]

        degree_R = params.get('quad_degree_R','auto')
        degree_I = params.get('quad_degree_I','auto')

        usFactor_R = params.get('quad_undersample_R',params.get('quad_undersample',1.0))
        usFactor_I = params.get('quad_undersample_I',params.get('quad_undersample',1.0))

        kern_R = params.get('interp_kernel_R',params.get('interp_kernel',None))
        kernsize_R = params.get('interp_kernel_size_R',params.get('interp_kernel_size',None))
        zeropad_R = params.get('interp_zeropad_R',params.get('interp_zeropad',0))
        dopremult_R = params.get('interp_premult_R',params.get('interp_premult',True))

        sym = get_symmetryop(params.get('symmetry',None)) if params.get('perfect_symmetry',True) else None

        maxAngle = quadrature.compute_max_angle(self.N,rad,usFactor_I)
        if degree_I == 'auto':
            degree_I = np.uint32(np.ceil(2.0 * np.pi / maxAngle))

        if degree_R == 'auto':
            degree_R,resolution_R = quad_R.compute_degree(N,rad,usFactor_R)

        resolution_R = max(0.5*quadrature.compute_max_angle(self.N,rad), resolution_R)
        resolution_I = max(0.5*quadrature.compute_max_angle(self.N,rad), 2.0*np.pi / degree_I)

        slice_params = { 'quad_type':quad_scheme_R, 'degree':degree_R, 
                         'sym':sym }
        inplane_params = { 'degree':degree_I }
        proj_params = { 'quad_type_R':quad_scheme_R, 'degree_R':degree_R, 
                         'sym':sym, 'degree_I':degree_I }
        interp_params_RI = { 'N':self.N, 'kern':kern_R, 'kernsize':kernsize_R, 'rad':rad, 'zeropad':zeropad_R, 'dopremult':dopremult_R }
        interp_change_RI = self.proj_interp != interp_params_RI
        
        transform_change = self.slice_interp is None or \
                        self.slice_interp['kern'] != interp_params_RI['kern'] or \
                        self.slice_interp['kernsize'] != interp_params_RI['kernsize'] or \
                        self.slice_interp['zeropad'] != interp_params_RI['zeropad']

        domain_change_R = self.slice_params != slice_params
        domain_change_I = self.inplane_params != inplane_params
        domain_change_RI = self.proj_params != proj_params  
        
        if domain_change_RI:
            proj_quad = {}

            proj_quad['resolution'] = max(resolution_R,resolution_I)
            proj_quad['degree_R'] = degree_R
            proj_quad['degree_I'] = degree_I
            proj_quad['symop'] = sym

            proj_quad['dir'],proj_quad['W_R'] = quad_R.get_quad_points(degree_R,proj_quad['symop'])
            proj_quad['W_R'] = np.require(proj_quad['W_R'], dtype=np.float32)

            proj_quad['thetas'] = np.linspace(0, 2.0*np.pi, degree_I, endpoint=False)
            proj_quad['thetas'] += proj_quad['thetas'][1]/2.0
            proj_quad['W_I'] = np.require((2.0*np.pi/float(degree_I))*np.ones((degree_I,)),dtype=np.float32)

            self.quad_domain_RI = quadrature.FixedSO3Domain( proj_quad['dir'],
                                                            -proj_quad['thetas'],
                                                             proj_quad['resolution'],
                                                             sym=sym)
            self.N_RI = len(self.quad_domain_RI)
            self.proj_quad = proj_quad
            self.proj_params = proj_params


            if domain_change_R:
                self.quad_domain_R = quadrature.FixedSphereDomain(proj_quad['dir'],
                                                                  proj_quad['resolution'],
                                                                  sym=sym)
                self.N_R = len(self.quad_domain_R)
                self.sampler_R.setup(params, self.N_D, self.N_D_Train, self.quad_domain_R)
                self.slice_params = slice_params

            if domain_change_I:
                self.quad_domain_I = quadrature.FixedCircleDomain(proj_quad['thetas'],
                                                                  proj_quad['resolution'])
                self.N_I = len(self.quad_domain_I)
                self.sampler_I.setup(params, self.N_D, self.N_D_Train, self.quad_domain_I)
                self.inplane_params = inplane_params

        if domain_change_RI or interp_change_RI:
            symorder = 1 if self.proj_quad['symop'] is None else self.proj_quad['symop'].get_order()
            print("  Projection Ops: %d (%d slice, %d inplane), " % (self.N_RI, self.N_R, self.N_I)); sys.stdout.flush()
            if self.N_RI*symorder < self.otf_thresh_RI:
                self.using_precomp_slicing = True
                print("generated in", end=''); sys.stdout.flush()
                self.slice_ops = self.quad_domain_RI.compute_operator(interp_params_RI)
                print(" {0} secs.".format(time.time() - tic))

                Gsz = (self.N_RI,self.N_T)
                self.G = np.empty(Gsz, dtype=self.G_datatype)
                self.slices = np.empty(np.prod(Gsz), dtype=np.complex64)
            else:
                self.using_precomp_slicing = False
                print("generating OTF.")
                self.slice_ops = None
                self.G = np.empty((N,N,N),dtype=self.G_datatype)
                self.slices = None
            self.using_precomp_inplane = False
            self.inplane_ops = None
            
            self.proj_interp = interp_params_RI

        if transform_change:
            if dopremult_R:
                premult = cryoops.compute_premultiplier(self.N + 2*int(interp_params_RI['zeropad']*(self.N/2)), 
                                                        interp_params_RI['kern'],interp_params_RI['kernsize'])
                premult = premult.reshape((-1,1,1)) * premult.reshape((1,-1,1)) * premult.reshape((1,1,-1))
            else:
                premult = None
            self.slice_premult = premult
            self.slice_zeropad = interp_params_RI['zeropad']
            assert interp_params_RI['zeropad'] == 0,'Zero padding for slicing not yet implemented'