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 1272 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 1278 of file aligner.h.

References align().

01279                 {
01280                         return align(this_img, to_img, "sqeuclidean", Dict());
01281                 }

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 1802 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.

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

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

Implements EMAN::Aligner.

Definition at line 1288 of file aligner.h.

01289                 {
01290                         return "Refines a preliminary 2D alignment using a simplex algorithm. Subpixel precision.";
01291                 }

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 1283 of file aligner.h.

01284                 {
01285                         return NAME;
01286                 }

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

Implements EMAN::Aligner.

Definition at line 1298 of file aligner.h.

References EMAN::TypeDict::put().

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

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

Definition at line 1293 of file aligner.h.

01294                 {
01295                         return new RefineAlignerCG();
01296                 }


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 Mon Aug 13 13:41:31 2012 for EMAN2 by  doxygen 1.3.9.1