EMAN::SqEuclideanCmp Class Reference

Squared Euclidean distance normalized by n between 'this' and 'with'. More...

#include <cmp.h>

Inheritance diagram for EMAN::SqEuclideanCmp:

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

Collaboration graph
[legend]
List of all members.

Public Member Functions

 SqEuclideanCmp ()
float cmp (EMData *image, EMData *with) const
 To compare 'image' with another image passed in through its parameters.
string get_name () const
 Get the Cmp's name.
string get_desc () const
TypeDict get_param_types () const
 Get Cmp parameter information in a dictionary.

Static Public Member Functions

static CmpNEW ()

Static Public Attributes

static const string NAME = "sqeuclidean"

Detailed Description

Squared Euclidean distance normalized by n between 'this' and 'with'.

Definition at line 192 of file cmp.h.


Constructor & Destructor Documentation

EMAN::SqEuclideanCmp::SqEuclideanCmp (  )  [inline]

Definition at line 195 of file cmp.h.

Referenced by NEW().

00195 {}


Member Function Documentation

float SqEuclideanCmp::cmp ( EMData image,
EMData with 
) const [virtual]

To compare 'image' with another image passed in through its parameters.

An optional transformation may be used to transform the 2 images.

Parameters:
image The first image to be compared.
with The second image to be comppared.
Returns:
The comparison result. Smaller better by default

Implements EMAN::Cmp.

Definition at line 151 of file cmp.cpp.

References dm, ENTERFUNC, EXITFUNC, EMAN::EMData::get_attr(), EMAN::EMData::get_const_data(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), EMAN::EMData::get_zsize(), EMAN::Util::goodf(), EMAN::EMData::has_attr(), EMAN::Dict::has_key(), EMAN::EMData::is_complex(), EMAN::EMData::is_fftodd(), nx, ny, EMAN::Cmp::params, EMAN::EMData::process(), EMAN::EMData::set_attr(), EMAN::Dict::set_default(), and EMAN::Cmp::validate_input_args().

00152 {
00153         ENTERFUNC;
00154         EMData *with = withorig;
00155         validate_input_args(image, with);
00156 
00157         int zeromask = params.set_default("zeromask",0);
00158         int normto = params.set_default("normto",0);
00159 
00160         if (normto) {
00161                 if (zeromask) with = withorig->process("normalize.toimage",Dict("to",image));
00162                 else with = withorig->process("normalize.toimage",Dict("to",image,"ignore_zero",0));
00163                 with->set_attr("deleteme",1);
00164                 if ((float)(with->get_attr("norm_mult"))<=0) {          // This means the normalization inverted the image, a clear probablity of noise bias, so we undo the normalization
00165                         delete with;
00166                         with=withorig;
00167                 }
00168         }
00169 
00170         const float *const y_data = with->get_const_data();
00171         const float *const x_data = image->get_const_data();
00172         double result = 0.;
00173         float n = 0.0f;
00174         if(image->is_complex() && with->is_complex()) {
00175         // Implemented by PAP  01/09/06 - please do not change.  If in doubts, write/call me.
00176                 int nx  = with->get_xsize();
00177                 int ny  = with->get_ysize();
00178                 int nz  = with->get_zsize();
00179                 nx = (nx - 2 + with->is_fftodd()); // nx is the real-space size of the input image
00180                 int lsd2 = (nx + 2 - nx%2) ; // Extended x-dimension of the complex image
00181 
00182                 int ixb = 2*((nx+1)%2);
00183                 int iyb = ny%2;
00184                 //
00185                 if(nz == 1) {
00186                 //  it looks like it could work in 3D, but it is not, really.
00187                 for ( int iz = 0; iz <= nz-1; iz++) {
00188                         double part = 0.;
00189                         for ( int iy = 0; iy <= ny-1; iy++) {
00190                                 for ( int ix = 2; ix <= lsd2 - 1 - ixb; ix++) {
00191                                                 size_t ii = ix + (iy  + iz * ny)* lsd2;
00192                                                 part += (x_data[ii] - y_data[ii])*double(x_data[ii] - y_data[ii]);
00193                                 }
00194                         }
00195                         for ( int iy = 1; iy <= ny/2-1 + iyb; iy++) {
00196                                 size_t ii = (iy  + iz * ny)* lsd2;
00197                                 part += (x_data[ii] - y_data[ii])*double(x_data[ii] - y_data[ii]);
00198                                 part += (x_data[ii+1] - y_data[ii+1])*double(x_data[ii+1] - y_data[ii+1]);
00199                         }
00200                         if(nx%2 == 0) {
00201                                 for ( int iy = 1; iy <= ny/2-1 + iyb; iy++) {
00202                                         size_t ii = lsd2 - 2 + (iy  + iz * ny)* lsd2;
00203                                         part += (x_data[ii] - y_data[ii])*double(x_data[ii] - y_data[ii]);
00204                                         part += (x_data[ii+1] - y_data[ii+1])*double(x_data[ii+1] - y_data[ii+1]);
00205                                 }
00206 
00207                         }
00208                         part *= 2;
00209                         part += (x_data[0] - y_data[0])*double(x_data[0] - y_data[0]);
00210                         if(ny%2 == 0) {
00211                                 int ii = (ny/2  + iz * ny)* lsd2;
00212                                 part += (x_data[ii] - y_data[ii])*double(x_data[ii] - y_data[ii]);
00213                         }
00214                         if(nx%2 == 0) {
00215                                 int ii = lsd2 - 2 + (0  + iz * ny)* lsd2;
00216                                 part += (x_data[ii] - y_data[ii])*double(x_data[ii] - y_data[ii]);
00217                                 if(ny%2 == 0) {
00218                                         int ii = lsd2 - 2 +(ny/2  + iz * ny)* lsd2;
00219                                         part += (x_data[ii] - y_data[ii])*double(x_data[ii] - y_data[ii]);
00220                                 }
00221                         }
00222                         result += part;
00223                 }
00224                 n = (float)nx*(float)ny*(float)nz*(float)nx*(float)ny*(float)nz;
00225 
00226                 }else{ //This 3D code is incorrect, but it is the best I can do now 01/09/06 PAP
00227                 int ky, kz;
00228                 int ny2 = ny/2; int nz2 = nz/2;
00229                 for ( int iz = 0; iz <= nz-1; iz++) {
00230                         if(iz>nz2) kz=iz-nz; else kz=iz;
00231                         for ( int iy = 0; iy <= ny-1; iy++) {
00232                                 if(iy>ny2) ky=iy-ny; else ky=iy;
00233                                 for ( int ix = 0; ix <= lsd2-1; ix++) {
00234                                 // Skip Friedel related values
00235                                 if(ix>0 || (kz>=0 && (ky>=0 || kz!=0))) {
00236                                                 size_t ii = ix + (iy  + iz * ny)* lsd2;
00237                                                 result += (x_data[ii] - y_data[ii])*double(x_data[ii] - y_data[ii]);
00238                                         }
00239                                 }
00240                         }
00241                 }
00242                 n = ((float)nx*(float)ny*(float)nz*(float)nx*(float)ny*(float)nz)/2.0f;
00243                 }
00244         } else {                // real space
00245                 size_t totsize = image->get_xsize()*image->get_ysize()*image->get_zsize();
00246                 if (params.has_key("mask")) {
00247                   EMData* mask;
00248                   mask = params["mask"];
00249                   const float *const dm = mask->get_const_data();
00250                   for (size_t i = 0; i < totsize; i++) {
00251                            if (dm[i] > 0.5) {
00252                                 double temp = x_data[i]- y_data[i];
00253                                 result += temp*temp;
00254                                 n++;
00255                            }
00256                   }
00257                 } 
00258                 else if (zeromask) {
00259                         n=0;
00260                         for (size_t i = 0; i < totsize; i++) {
00261                                 if (x_data[i]==0 || y_data[i]==0) continue;
00262                                 double temp = x_data[i]- y_data[i];
00263                                 result += temp*temp;
00264                                 n++;
00265                         }
00266                         
00267                 }
00268                 else {
00269                   for (size_t i = 0; i < totsize; i++) {
00270                                 double temp = x_data[i]- y_data[i];
00271                                 result += temp*temp;
00272                    }
00273                    n = (float)totsize;
00274                 }
00275         }
00276         result/=n;
00277 
00278         EXITFUNC;
00279         if (with->has_attr("deleteme")) delete with;
00280         float ret = (float)result;
00281         if (!Util::goodf(&ret)) return FLT_MAX;
00282         return ret;
00283 }

string EMAN::SqEuclideanCmp::get_desc (  )  const [inline, virtual]

Implements EMAN::Cmp.

Definition at line 204 of file cmp.h.

00205                 {
00206                         return "Squared Euclidean distance (sum(a - b)^2)/n.";
00207                 }

string EMAN::SqEuclideanCmp::get_name (  )  const [inline, virtual]

Get the Cmp's name.

Each Cmp is identified by a unique name.

Returns:
The Cmp's name.

Implements EMAN::Cmp.

Definition at line 199 of file cmp.h.

References NAME.

00200                 {
00201                         return NAME;
00202                 }

TypeDict EMAN::SqEuclideanCmp::get_param_types (  )  const [inline, virtual]

Get Cmp parameter information in a dictionary.

Each parameter has one record in the dictionary. Each record contains its name, data-type, and description.

Returns:
A dictionary containing the parameter info.

Implements EMAN::Cmp.

Definition at line 214 of file cmp.h.

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

00215                 {
00216                         TypeDict d;
00217                         d.put("mask", EMObject::EMDATA, "image mask");
00218                         d.put("zeromask", EMObject::INT, "If set, zero pixels in either image will be excluded from the statistics");
00219                         d.put("normto",EMObject::INT,"If set, 'with' is normalized to 'this' before computing the distance");
00220                         return d;
00221                 }

static Cmp* EMAN::SqEuclideanCmp::NEW (  )  [inline, static]

Definition at line 209 of file cmp.h.

References SqEuclideanCmp().

00210                 {
00211                         return new SqEuclideanCmp();
00212                 }


Member Data Documentation

const string SqEuclideanCmp::NAME = "sqeuclidean" [static]

Definition at line 223 of file cmp.h.

Referenced by get_name().


The documentation for this class was generated from the following files:
Generated on Mon Jul 19 12:42:25 2010 for EMAN2 by  doxygen 1.4.7