EMAN2
Public Member Functions | Static Public Member Functions | Static Public Attributes
EMAN::RefineAlignerCG Class Reference

Conjugate gradient refine alignment. More...

#include <aligner.h>

Inheritance diagram for EMAN::RefineAlignerCG:
Inheritance graph
[legend]
Collaboration diagram for EMAN::RefineAlignerCG:
Collaboration graph
[legend]

List of all members.

Public Member Functions

virtual EMDataalign (EMData *this_img, EMData *to_img, const string &cmp_name="dot", const Dict &cmp_params=Dict()) const
 To align 'this_img' with another image passed in through its parameters.
virtual EMDataalign (EMData *this_img, EMData *to_img) const
virtual string get_name () const
 Get the Aligner's name.
virtual string get_desc () const
virtual TypeDict get_param_types () const

Static Public Member Functions

static AlignerNEW ()

Static Public Attributes

static const string NAME = "refinecg"

Detailed Description

Conjugate gradient refine alignment.

Refines a preliminary 2D alignment to subpixel precision. Faster than 'refine', but requires better local minimum

Parameters:
xform.align2dThe Transform storing the starting guess. If unspecified the identity matrix is used
stepThe initial increment used for stepping on the gradient. default=0.1
precisionThe precision which, if achieved, can stop the iterative refinement before reaching the maximum iterations. Default is 0.02
maxiterThe maximum number of iterations. default=12
maxshiftMaximum translation in pixels in any direction. If the solution yields a shift beyond this value in any direction, then the refinement is judged a failure and the original alignment is used as the solution
stepscaleIf set to any non-zero value, scale will be included in the alignment, and this will be the initial step. Images should be edgenormalized. If the scale goes beyond +-30% alignment will fail
verboseThis will cause debugging information to be printed on the screen for the iterative refinement. Larger numbers -> more info. default=0

Definition at line 1278 of file aligner.h.


Member Function Documentation

EMData * RefineAlignerCG::align ( EMData this_img,
EMData to_img,
const string &  cmp_name = "dot",
const Dict cmp_params = Dict() 
) const [virtual]

To align 'this_img' with another image passed in through its parameters.

The alignment uses a user-given comparison method to compare the two images. If none is given, a default one is used.

Parameters:
this_imgThe image to be compared.
to_img'this_img" is aligned with 'to_img'.
cmp_nameThe comparison method to compare the two images.
cmp_paramsThe parameter dictionary for comparison method.
Returns:
The aligned image.

Implements EMAN::Aligner.

Definition at line 1823 of file aligner.cpp.

References EMAN::EMData::get_attr(), EMAN::Transform::get_params(), EMAN::EMData::get_xsize(), EMAN::Dict::has_key(), EMAN::Aligner::params, EMAN::EMData::process(), refalidf(), refalifdf(), refalifn(), refalifnfast(), EMAN::EMData::set_attr(), EMAN::Dict::set_default(), EMAN::Transform::set_mirror(), EMAN::Transform::set_scale(), EMAN::Transform::set_trans(), status, t, and x.

Referenced by align().

{

        if (!to) {
                return 0;
        }

        EMData *result;
        int mode = params.set_default("mode", 0);
        float saz = 0.0;
        float sdx = 0.0;
        float sdy = 0.0;
        float sscale = 1.0;
        bool mirror = false;
        Transform* t;
        if (params.has_key("xform.align2d") ) {
                t = params["xform.align2d"];
                Dict params = t->get_params("2d");
                saz = params["alpha"];
                sdx = params["tx"];
                sdy = params["ty"];
                mirror = params["mirror"];
                sscale = params["scale"];
        } else {
                t = new Transform(); // is the identity
        }

        // We do this to prevent the GSL routine from crashing on an invalid alignment
        if ((float)(this_img->get_attr("sigma"))==0.0 || (float)(to->get_attr("sigma"))==0.0) {
                result = this_img->process("xform",Dict("transform",t));
                result->set_attr("xform.align2d",t);
                delete t;
                return result;
        }
        
        float step = params.set_default("step",0.1f);
        float stepscale = params.set_default("stepscale",0.0f);

        int np = 3;
        if (stepscale!=0.0) np++;
        Dict gsl_params;
        gsl_params["this"] = this_img;
        gsl_params["with"] = to;
        gsl_params["snr"]  = params["snr"];
        gsl_params["mirror"] = mirror;
        if (params.has_key("mask")) gsl_params["mask"]=params["mask"];

        const gsl_multimin_fdfminimizer_type *T = gsl_multimin_fdfminimizer_vector_bfgs;
        
        gsl_vector *x = gsl_vector_alloc(np);
        gsl_vector_set(x, 0, sdx);
        gsl_vector_set(x, 1, sdy);
        gsl_vector_set(x, 2, saz);
        if (stepscale!=0.0) gsl_vector_set(x,3,1.0);
        
        Cmp *c = 0;

        gsl_multimin_function_fdf minex_func;
        if (mode == 2) {
                minex_func.f = &refalifnfast;
        }
        else {
                c = Factory < Cmp >::get(cmp_name, cmp_params);
                gsl_params["cmp"] = (void *) c;
                minex_func.f = &refalifn;
        }

        minex_func.df = &refalidf;
        minex_func.fdf = &refalifdf;
        minex_func.n = np;
        minex_func.params = (void *) &gsl_params;

        gsl_multimin_fdfminimizer *s = gsl_multimin_fdfminimizer_alloc(T, np);
        gsl_multimin_fdfminimizer_set(s, &minex_func, x, step, 0.001f);

        int rval = GSL_CONTINUE;
        int status = GSL_SUCCESS;
        int iter = 1;

        float precision = params.set_default("precision",0.02f);
        int maxiter = params.set_default("maxiter",12);
        int verbose = params.set_default("verbose",0);

//      printf("Refine sx=%1.2f sy=%1.2f sa=%1.2f prec=%1.4f maxit=%d\n",stepx,stepy,stepaz,precision,maxiter);
//      printf("%1.2f %1.2f %1.1f  ->",(float)gsl_vector_get(s->x, 0),(float)gsl_vector_get(s->x, 1),(float)gsl_vector_get(s->x, 2));

        while (rval == GSL_CONTINUE && iter < maxiter) {
                iter++;
                status = gsl_multimin_fdfminimizer_iterate(s);
                if (status) {
                        break;
                }
                rval = gsl_multimin_test_gradient (s->gradient, precision);
//              if (verbose>2) printf("GSL %d. %1.3f %1.3f %1.3f   %1.3f\n",iter,gsl_vector_get(s->x,0),gsl_vector_get(s->x,1),gsl_vector_get(s->x,2),s->gradient[0]);
        }

        int maxshift = params.set_default("maxshift",-1);

        if (maxshift <= 0) {
                maxshift = this_img->get_xsize() / 4;
        }
        float fmaxshift = static_cast<float>(maxshift);
        if ( fmaxshift >= fabs((float)gsl_vector_get(s->x, 0)) && fmaxshift >= fabs((float)gsl_vector_get(s->x, 1)) && (stepscale==0 || (((float)gsl_vector_get(s->x, 3))<1.3 && ((float)gsl_vector_get(s->x, 3))<0.7))  )
        {
                if (verbose>0) printf(" Refine good (%d) %1.2f %1.2f %1.1f\n",iter,(float)gsl_vector_get(s->x, 0),(float)gsl_vector_get(s->x, 1),(float)gsl_vector_get(s->x, 2));
                Transform  tsoln(Dict("type","2d","alpha",(float)gsl_vector_get(s->x, 2)));
                tsoln.set_mirror(mirror);
                tsoln.set_trans((float)gsl_vector_get(s->x, 0),(float)gsl_vector_get(s->x, 1));
                if (stepscale!=0.0) tsoln.set_scale((float)gsl_vector_get(s->x, 3));
                result = this_img->process("xform",Dict("transform",&tsoln));
                result->set_attr("xform.align2d",&tsoln);
        } else { // The refine aligner failed - this shift went beyond the max shift
                if (verbose>1) printf(" Refine Failed %1.2f %1.2f %1.1f\n",(float)gsl_vector_get(s->x, 0),(float)gsl_vector_get(s->x, 1),(float)gsl_vector_get(s->x, 2));
                result = this_img->process("xform",Dict("transform",t));
                result->set_attr("xform.align2d",t);
        }

        delete t;
        t = 0;

        gsl_vector_free(x);
        gsl_multimin_fdfminimizer_free(s);

        if (c != 0) delete c;
        return result;
}
virtual EMData* EMAN::RefineAlignerCG::align ( EMData this_img,
EMData to_img 
) const [inline, virtual]

Implements EMAN::Aligner.

Definition at line 1284 of file aligner.h.

References align().

                {
                        return align(this_img, to_img, "sqeuclidean", Dict());
                }
virtual string EMAN::RefineAlignerCG::get_desc ( ) const [inline, virtual]

Implements EMAN::Aligner.

Definition at line 1294 of file aligner.h.

                {
                        return "Refines a preliminary 2D alignment using a simplex algorithm. Subpixel precision.";
                }
virtual string EMAN::RefineAlignerCG::get_name ( ) const [inline, virtual]

Get the Aligner's name.

Each Aligner is identified by a unique name.

Returns:
The Aligner's name.

Implements EMAN::Aligner.

Definition at line 1289 of file aligner.h.

References NAME.

                {
                        return NAME;
                }
virtual TypeDict EMAN::RefineAlignerCG::get_param_types ( ) const [inline, virtual]

Implements EMAN::Aligner.

Definition at line 1304 of file aligner.h.

References EMAN::EMObject::EMDATA, EMAN::EMObject::FLOAT, EMAN::EMObject::INT, EMAN::TypeDict::put(), and EMAN::EMObject::TRANSFORM.

                {
                        TypeDict d;

                        d.put("mode", EMObject::INT, "Currently unused");
                        d.put("xform.align2d", EMObject::TRANSFORM, "The Transform storing the starting guess. If unspecified the identity matrix is used");
                        d.put("step", EMObject::FLOAT, "The x increment used to create the starting simplex. Default is 0.1");
                        d.put("precision", EMObject::FLOAT, "The precision which, if achieved, can stop the iterative refinement before reaching the maximum iterations. Default is 0.02.");
                        d.put("maxiter", EMObject::INT,"The maximum number of iterations that can be performed by the Simplex minimizer. default=12");
                        d.put("maxshift", EMObject::INT,"Maximum translation in pixels in any direction. If the solution yields a shift beyond this value in any direction, then the refinement is judged a failure and the original alignment is used as the solution.");
                        d.put("stepscale", EMObject::FLOAT, "If set to any non-zero value, scale will be included in the alignment. Images should be edgenormalized. If the scale goes beyond +-30% alignment will fail.");
                        d.put("mask", EMObject::EMDATA, "A mask to be applied to the image being aligned prior to each similarity comparison.");
                        d.put("verbose", EMObject::INT, "This will cause debugging information to be printed on the screen for the iterative refinement. Larger numbers -> more info. default=0");
                        return d;
                }
static Aligner* EMAN::RefineAlignerCG::NEW ( ) [inline, static]

Definition at line 1299 of file aligner.h.

                {
                        return new RefineAlignerCG();
                }

Member Data Documentation

const string RefineAlignerCG::NAME = "refinecg" [static]

Definition at line 1320 of file aligner.h.

Referenced by get_name().


The documentation for this class was generated from the following files: