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

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

CmpNEW ()

Static Public Attributes

const string NAME = "sqeuclidean"

Detailed Description

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

Definition at line 229 of file cmp.h.


Constructor & Destructor Documentation

EMAN::SqEuclideanCmp::SqEuclideanCmp  )  [inline]
 

Definition at line 232 of file cmp.h.

00232 {}


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 269 of file cmp.cpp.

References dm, 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::EMData::process(), EMAN::EMData::set_attr(), EMAN::Dict::set_default(), and EMAN::Cmp::validate_input_args().

00270 {
00271         ENTERFUNC;
00272         EMData *with = withorig;
00273         validate_input_args(image, with);
00274 
00275         int zeromask = params.set_default("zeromask",0);
00276         int normto = params.set_default("normto",0);
00277 
00278         if (normto) {
00279                 if (zeromask) with = withorig->process("normalize.toimage",Dict("to",image));
00280                 else with = withorig->process("normalize.toimage",Dict("to",image,"ignore_zero",0));
00281                 with->set_attr("deleteme",1);
00282                 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
00283                         delete with;
00284                         with=withorig;
00285                 }
00286         }
00287 
00288         const float *const y_data = with->get_const_data();
00289         const float *const x_data = image->get_const_data();
00290         double result = 0.;
00291         float n = 0.0f;
00292         if(image->is_complex() && with->is_complex()) {
00293         // Implemented by PAP  01/09/06 - please do not change.  If in doubts, write/call me.
00294                 int nx  = with->get_xsize();
00295                 int ny  = with->get_ysize();
00296                 int nz  = with->get_zsize();
00297                 nx = (nx - 2 + with->is_fftodd()); // nx is the real-space size of the input image
00298                 int lsd2 = (nx + 2 - nx%2) ; // Extended x-dimension of the complex image
00299 
00300                 int ixb = 2*((nx+1)%2);
00301                 int iyb = ny%2;
00302                 //
00303                 if(nz == 1) {
00304                 //  it looks like it could work in 3D, but it is not, really.
00305                 for ( int iz = 0; iz <= nz-1; iz++) {
00306                         double part = 0.;
00307                         for ( int iy = 0; iy <= ny-1; iy++) {
00308                                 for ( int ix = 2; ix <= lsd2 - 1 - ixb; ix++) {
00309                                                 size_t ii = ix + (iy  + iz * ny)* lsd2;
00310                                                 part += (x_data[ii] - y_data[ii])*double(x_data[ii] - y_data[ii]);
00311                                 }
00312                         }
00313                         for ( int iy = 1; iy <= ny/2-1 + iyb; iy++) {
00314                                 size_t ii = (iy  + iz * ny)* lsd2;
00315                                 part += (x_data[ii] - y_data[ii])*double(x_data[ii] - y_data[ii]);
00316                                 part += (x_data[ii+1] - y_data[ii+1])*double(x_data[ii+1] - y_data[ii+1]);
00317                         }
00318                         if(nx%2 == 0) {
00319                                 for ( int iy = 1; iy <= ny/2-1 + iyb; iy++) {
00320                                         size_t ii = lsd2 - 2 + (iy  + iz * ny)* lsd2;
00321                                         part += (x_data[ii] - y_data[ii])*double(x_data[ii] - y_data[ii]);
00322                                         part += (x_data[ii+1] - y_data[ii+1])*double(x_data[ii+1] - y_data[ii+1]);
00323                                 }
00324 
00325                         }
00326                         part *= 2;
00327                         part += (x_data[0] - y_data[0])*double(x_data[0] - y_data[0]);
00328                         if(ny%2 == 0) {
00329                                 int ii = (ny/2  + iz * ny)* lsd2;
00330                                 part += (x_data[ii] - y_data[ii])*double(x_data[ii] - y_data[ii]);
00331                         }
00332                         if(nx%2 == 0) {
00333                                 int ii = lsd2 - 2 + (0  + iz * ny)* lsd2;
00334                                 part += (x_data[ii] - y_data[ii])*double(x_data[ii] - y_data[ii]);
00335                                 if(ny%2 == 0) {
00336                                         int ii = lsd2 - 2 +(ny/2  + iz * ny)* lsd2;
00337                                         part += (x_data[ii] - y_data[ii])*double(x_data[ii] - y_data[ii]);
00338                                 }
00339                         }
00340                         result += part;
00341                 }
00342                 n = (float)nx*(float)ny*(float)nz*(float)nx*(float)ny*(float)nz;
00343 
00344                 }else{ //This 3D code is incorrect, but it is the best I can do now 01/09/06 PAP
00345                 int ky, kz;
00346                 int ny2 = ny/2; int nz2 = nz/2;
00347                 for ( int iz = 0; iz <= nz-1; iz++) {
00348                         if(iz>nz2) kz=iz-nz; else kz=iz;
00349                         for ( int iy = 0; iy <= ny-1; iy++) {
00350                                 if(iy>ny2) ky=iy-ny; else ky=iy;
00351                                 for ( int ix = 0; ix <= lsd2-1; ix++) {
00352                                 // Skip Friedel related values
00353                                 if(ix>0 || (kz>=0 && (ky>=0 || kz!=0))) {
00354                                                 size_t ii = ix + (iy  + iz * ny)* (size_t)lsd2;
00355                                                 result += (x_data[ii] - y_data[ii])*double(x_data[ii] - y_data[ii]);
00356                                         }
00357                                 }
00358                         }
00359                 }
00360                 n = ((float)nx*(float)ny*(float)nz*(float)nx*(float)ny*(float)nz)/2.0f;
00361                 }
00362         } else {                // real space
00363                 size_t totsize = (size_t)image->get_xsize()*image->get_ysize()*image->get_zsize();
00364                 if (params.has_key("mask")) {
00365                   EMData* mask;
00366                   mask = params["mask"];
00367                   const float *const dm = mask->get_const_data();
00368                   for (size_t i = 0; i < totsize; i++) {
00369                            if (dm[i] > 0.5) {
00370                                 double temp = x_data[i]- y_data[i];
00371                                 result += temp*temp;
00372                                 n++;
00373                            }
00374                   }
00375                 } 
00376                 else if (zeromask) {
00377                         n=0;
00378                         for (size_t i = 0; i < totsize; i++) {
00379                                 if (x_data[i]==0 || y_data[i]==0) continue;
00380                                 double temp = x_data[i]- y_data[i];
00381                                 result += temp*temp;
00382                                 n++;
00383                         }
00384                         
00385                 }
00386                 else {
00387                   for (size_t i = 0; i < totsize; i++) {
00388                                 double temp = x_data[i]- y_data[i];
00389                                 result += temp*temp;
00390                    }
00391                    n = (float)totsize;
00392                 }
00393         }
00394         result/=n;
00395 
00396         EXITFUNC;
00397         if (with->has_attr("deleteme")) delete with;
00398         float ret = (float)result;
00399         if (!Util::goodf(&ret)) return FLT_MAX;
00400         return ret;
00401 }

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

Implements EMAN::Cmp.

Definition at line 241 of file cmp.h.

00242                 {
00243                         return "Squared Euclidean distance (sum(a - b)^2)/n.";
00244                 }

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 236 of file cmp.h.

00237                 {
00238                         return NAME;
00239                 }

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 251 of file cmp.h.

References EMAN::TypeDict::put().

00252                 {
00253                         TypeDict d;
00254                         d.put("mask", EMObject::EMDATA, "image mask");
00255                         d.put("zeromask", EMObject::INT, "If set, zero pixels in either image will be excluded from the statistics");
00256                         d.put("normto",EMObject::INT,"If set, 'with' is normalized to 'this' before computing the distance");
00257                         return d;
00258                 }

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

Definition at line 246 of file cmp.h.

00247                 {
00248                         return new SqEuclideanCmp();
00249                 }


Member Data Documentation

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

Definition at line 50 of file cmp.cpp.


The documentation for this class was generated from the following files:
Generated on Tue Jul 12 13:51:17 2011 for EMAN2 by  doxygen 1.3.9.1