Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

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

AlignerNEW ()

Static Public Attributes

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.align2d The Transform storing the starting guess. If unspecified the identity matrix is used
step The initial increment used for stepping on the gradient. default=0.1
precision The precision which, if achieved, can stop the iterative refinement before reaching the maximum iterations. Default is 0.02
maxiter The maximum number of iterations. default=12
maxshift 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
stepscale If 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
verbose This 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

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().

01285                 {
01286                         return align(this_img, to_img, "sqeuclidean", Dict());
01287                 }

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_img The image to be compared.
to_img 'this_img" is aligned with 'to_img'.
cmp_name The comparison method to compare the two images.
cmp_params The parameter dictionary for comparison method.
Returns:
The aligned image.

Implements EMAN::Aligner.

Definition at line 1818 of file aligner.cpp.

References EMAN::EMData::get_attr(), EMAN::Transform::get_params(), EMAN::EMData::get_xsize(), EMAN::Dict::has_key(), EMAN::Cmp::params, EMAN::EMData::process(), EMAN::EMData::set_attr(), EMAN::Dict::set_default(), status, t, and x.

01820 {
01821 
01822         if (!to) {
01823                 return 0;
01824         }
01825 
01826         EMData *result;
01827         int mode = params.set_default("mode", 0);
01828         float saz = 0.0;
01829         float sdx = 0.0;
01830         float sdy = 0.0;
01831         float sscale = 1.0;
01832         bool mirror = false;
01833         Transform* t;
01834         if (params.has_key("xform.align2d") ) {
01835                 t = params["xform.align2d"];
01836                 Dict params = t->get_params("2d");
01837                 saz = params["alpha"];
01838                 sdx = params["tx"];
01839                 sdy = params["ty"];
01840                 mirror = params["mirror"];
01841                 sscale = params["scale"];
01842         } else {
01843                 t = new Transform(); // is the identity
01844         }
01845 
01846         // We do this to prevent the GSL routine from crashing on an invalid alignment
01847         if ((float)(this_img->get_attr("sigma"))==0.0 || (float)(to->get_attr("sigma"))==0.0) {
01848                 result = this_img->process("xform",Dict("transform",t));
01849                 result->set_attr("xform.align2d",t);
01850                 delete t;
01851                 return result;
01852         }
01853         
01854         float step = params.set_default("step",0.1f);
01855         float stepscale = params.set_default("stepscale",0.0f);
01856 
01857         int np = 3;
01858         if (stepscale!=0.0) np++;
01859         Dict gsl_params;
01860         gsl_params["this"] = this_img;
01861         gsl_params["with"] = to;
01862         gsl_params["snr"]  = params["snr"];
01863         gsl_params["mirror"] = mirror;
01864         if (params.has_key("mask")) gsl_params["mask"]=params["mask"];
01865 
01866         const gsl_multimin_fdfminimizer_type *T = gsl_multimin_fdfminimizer_vector_bfgs;
01867         
01868         gsl_vector *x = gsl_vector_alloc(np);
01869         gsl_vector_set(x, 0, sdx);
01870         gsl_vector_set(x, 1, sdy);
01871         gsl_vector_set(x, 2, saz);
01872         if (stepscale!=0.0) gsl_vector_set(x,3,1.0);
01873         
01874         Cmp *c = 0;
01875 
01876         gsl_multimin_function_fdf minex_func;
01877         if (mode == 2) {
01878                 minex_func.f = &refalifnfast;
01879         }
01880         else {
01881                 c = Factory < Cmp >::get(cmp_name, cmp_params);
01882                 gsl_params["cmp"] = (void *) c;
01883                 minex_func.f = &refalifn;
01884         }
01885 
01886         minex_func.df = &refalidf;
01887         minex_func.fdf = &refalifdf;
01888         minex_func.n = np;
01889         minex_func.params = (void *) &gsl_params;
01890 
01891         gsl_multimin_fdfminimizer *s = gsl_multimin_fdfminimizer_alloc(T, np);
01892         gsl_multimin_fdfminimizer_set(s, &minex_func, x, step, 0.001f);
01893 
01894         int rval = GSL_CONTINUE;
01895         int status = GSL_SUCCESS;
01896         int iter = 1;
01897 
01898         float precision = params.set_default("precision",0.02f);
01899         int maxiter = params.set_default("maxiter",12);
01900         int verbose = params.set_default("verbose",0);
01901 
01902 //      printf("Refine sx=%1.2f sy=%1.2f sa=%1.2f prec=%1.4f maxit=%d\n",stepx,stepy,stepaz,precision,maxiter);
01903 //      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));
01904 
01905         while (rval == GSL_CONTINUE && iter < maxiter) {
01906                 iter++;
01907                 status = gsl_multimin_fdfminimizer_iterate(s);
01908                 if (status) {
01909                         break;
01910                 }
01911                 rval = gsl_multimin_test_gradient (s->gradient, precision);
01912 //              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]);
01913         }
01914 
01915         int maxshift = params.set_default("maxshift",-1);
01916 
01917         if (maxshift <= 0) {
01918                 maxshift = this_img->get_xsize() / 4;
01919         }
01920         float fmaxshift = static_cast<float>(maxshift);
01921         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))  )
01922         {
01923                 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));
01924                 Transform  tsoln(Dict("type","2d","alpha",(float)gsl_vector_get(s->x, 2)));
01925                 tsoln.set_mirror(mirror);
01926                 tsoln.set_trans((float)gsl_vector_get(s->x, 0),(float)gsl_vector_get(s->x, 1));
01927                 if (stepscale!=0.0) tsoln.set_scale((float)gsl_vector_get(s->x, 3));
01928                 result = this_img->process("xform",Dict("transform",&tsoln));
01929                 result->set_attr("xform.align2d",&tsoln);
01930         } else { // The refine aligner failed - this shift went beyond the max shift
01931                 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));
01932                 result = this_img->process("xform",Dict("transform",t));
01933                 result->set_attr("xform.align2d",t);
01934         }
01935 
01936         delete t;
01937         t = 0;
01938 
01939         gsl_vector_free(x);
01940         gsl_multimin_fdfminimizer_free(s);
01941 
01942         if (c != 0) delete c;
01943         return result;
01944 }

virtual string EMAN::RefineAlignerCG::get_desc  )  const [inline, virtual]
 

Implements EMAN::Aligner.

Definition at line 1294 of file aligner.h.

01295                 {
01296                         return "Refines a preliminary 2D alignment using a simplex algorithm. Subpixel precision.";
01297                 }

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.

01290                 {
01291                         return NAME;
01292                 }

virtual TypeDict EMAN::RefineAlignerCG::get_param_types  )  const [inline, virtual]
 

Implements EMAN::Aligner.

Definition at line 1304 of file aligner.h.

References EMAN::TypeDict::put().

01305                 {
01306                         TypeDict d;
01307 
01308                         d.put("mode", EMObject::INT, "Currently unused");
01309                         d.put("xform.align2d", EMObject::TRANSFORM, "The Transform storing the starting guess. If unspecified the identity matrix is used");
01310                         d.put("step", EMObject::FLOAT, "The x increment used to create the starting simplex. Default is 0.1");
01311                         d.put("precision", EMObject::FLOAT, "The precision which, if achieved, can stop the iterative refinement before reaching the maximum iterations. Default is 0.02.");
01312                         d.put("maxiter", EMObject::INT,"The maximum number of iterations that can be performed by the Simplex minimizer. default=12");
01313                         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.");
01314                         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.");
01315                         d.put("mask", EMObject::EMDATA, "A mask to be applied to the image being aligned prior to each similarity comparison.");
01316                         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");
01317                         return d;
01318                 }

Aligner* EMAN::RefineAlignerCG::NEW  )  [inline, static]
 

Definition at line 1299 of file aligner.h.

01300                 {
01301                         return new RefineAlignerCG();
01302                 }


Member Data Documentation

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

Definition at line 79 of file aligner.cpp.


The documentation for this class was generated from the following files:
Generated on Tue Jun 11 13:47:48 2013 for EMAN2 by  doxygen 1.3.9.1