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

EMAN::CtfAverager Class Reference

CtfAverager is the base Averager class for CTF correction or SNR weighting. More...

#include <averager.h>

Inheritance diagram for EMAN::CtfAverager:

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

Collaboration graph
[legend]
List of all members.

Public Member Functions

 CtfAverager ()
void add_image (EMData *image)
 To add an image to the Averager.
EMDatafinish ()
 Finish up the averaging and return the result.
vector< float > get_snr () const

Protected Attributes

XYDatasf
EMDatacurves
bool need_snr
const char * outfile

Private Attributes

vector< float > snr
EMDataimage0_fft
EMDataimage0_copy
vector< vector< float > > ctf
vector< vector< float > > ctfn
float * snri
float * snrn
float * tdr
float * tdi
float * tn
float filter
int nimg
int nx
int ny
int nz

Detailed Description

CtfAverager is the base Averager class for CTF correction or SNR weighting.

Definition at line 288 of file averager.h.


Constructor & Destructor Documentation

CtfAverager::CtfAverager  ) 
 

Definition at line 834 of file averager.cpp.

References nx, and ny.

00834                          :
00835         sf(0), curves(0), need_snr(false), outfile(0),
00836         image0_fft(0), image0_copy(0), snri(0), snrn(0),
00837         tdr(0), tdi(0), tn(0),
00838         filter(0), nimg(0), nx(0), ny(0), nz(0)
00839 {
00840 
00841 }


Member Function Documentation

void CtfAverager::add_image EMData image  )  [virtual]
 

To add an image to the Averager.

This image will be averaged in this function.

Parameters:
image The image to be averaged.

Implements EMAN::Averager.

Definition at line 843 of file averager.cpp.

References EMAN::EMData::ap2ri(), EMAN::Ctf::apix, EMAN::Ctf::compute_1d(), EMAN::EMData::copy_head(), ctf, ctfn, curves, EMAN::EMData::do_fft(), EMAN::EMObject::f, EMAN::Util::fast_floor(), filter, EMAN::Dict::get(), EMAN::EMData::get_attr_dict(), EMAN::EMData::get_ctf(), EMAN::EMData::get_data(), EMAN::Averager::get_name(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), EMAN::EMData::get_zsize(), EMAN::EMData::has_ctff(), image0_copy, image0_fft, EMAN::EMUtil::is_same_size(), LOGERR, LOGWARN, nimg, nx, ny, EMAN::EMData::set_size(), snri, snrn, sqrt(), tdi, tdr, tn, EMAN::EMData::to_zero(), EMAN::EMData::update(), x, and y.

00844 {
00845         if (!image) {
00846                 return;
00847         }
00848 
00849         if (nimg >= 1 && !EMUtil::is_same_size(image, result)) {
00850                 LOGERR("%sAverager can only process same-size Image",
00851                                                          get_name().c_str());
00852                 return;
00853         }
00854 
00855         if (image->get_zsize() != 1) {
00856                 LOGERR("%sAverager: Only 2D images are currently supported",
00857                                                          get_name().c_str());
00858         }
00859 
00860         string alg_name = get_name();
00861 
00862         if (alg_name == "CtfCW" || alg_name == "CtfCWauto") {
00863                 if (image->get_ctf() != 0 && !image->has_ctff()) {
00864                         LOGERR("%sAverager: Attempted CTF Correction with no ctf parameters",
00865                                                                  get_name().c_str());
00866                 }
00867         }
00868         else {
00869                 if (image->get_ctf() != 0) {
00870                         LOGERR("%sAverager: Attempted CTF Correction with no ctf parameters",
00871                                                                  get_name().c_str());
00872                 }
00873         }
00874 
00875         nimg++;
00876 
00877 
00878         if (nimg == 1) {
00879                 image0_fft = image->do_fft();
00880 
00881                 nx = image0_fft->get_xsize();
00882                 ny = image0_fft->get_ysize();
00883                 nz = image0_fft->get_zsize();
00884 
00885                 result = new EMData();
00886                 result->set_size(nx - 2, ny, nz);
00887 
00888 
00889                 if (alg_name == "Weighting" && curves) {
00890                         if (!sf) {
00891                                 LOGWARN("CTF curve in file will contain relative, not absolute SNR!");
00892                         }
00893                         curves->set_size(Ctf::CTFOS * ny / 2, 3, 1);
00894                         curves->to_zero();
00895                 }
00896 
00897 
00898                 if (alg_name == "CtfC") {
00899                         filter = params["filter"];
00900                         if (filter == 0) {
00901                                 filter = 22.0f;
00902                         }
00903                         float apix_y = image->get_attr_dict().get("apix_y");
00904                         float ds = 1.0f / (apix_y * ny * Ctf::CTFOS);
00905                         filter = 1.0f / (filter * ds);
00906                 }
00907 
00908                 if (alg_name == "CtfCWauto") {
00909                         int nxy2 = nx * ny/2;
00910 
00911                         snri = new float[ny / 2];
00912                         snrn = new float[ny / 2];
00913                         tdr = new float[nxy2];
00914                         tdi = new float[nxy2];
00915                         tn = new float[nxy2];
00916 
00917                         for (int i = 0; i < ny / 2; i++) {
00918                                 snri[i] = 0;
00919                                 snrn[i] = 0;
00920                         }
00921 
00922                         for (int i = 0; i < nxy2; i++) {
00923                                 tdr[i] = 1;
00924                                 tdi[i] = 1;
00925                                 tn[i] = 1;
00926                         }
00927                 }
00928 
00929                 image0_copy = image0_fft->copy_head();
00930                 image0_copy->ap2ri();
00931                 image0_copy->to_zero();
00932         }
00933 
00934         Ctf::CtfType curve_type = Ctf::CTF_AMP;
00935         if (alg_name == "CtfCWauto") {
00936                 curve_type = Ctf::CTF_AMP;
00937         }
00938 
00939         float *src = image->get_data();
00940         image->ap2ri();
00941         Ctf *image_ctf = image->get_ctf();
00942         int ny2 = image->get_ysize();
00943 
00944         vector<float> ctf1 = image_ctf->compute_1d(ny2,1.0f/(image_ctf->apix*image->get_ysize()), curve_type);
00945 
00946         if (ctf1.size() == 0) {
00947                 LOGERR("Unexpected CTF correction problem");
00948         }
00949 
00950         ctf.push_back(ctf1);
00951 
00952         vector<float> ctfn1;
00953         if (sf) {
00954                 ctfn1 = image_ctf->compute_1d(ny2,1.0f/(image_ctf->apix*image->get_ysize()), Ctf::CTF_SNR, sf);
00955         }
00956         else {
00957                 ctfn1 = image_ctf->compute_1d(ny2,1.0f/(image_ctf->apix*image->get_ysize()), Ctf::CTF_SNR);
00958         }
00959 
00960         ctfn.push_back(ctfn1);
00961 
00962         if (alg_name == "CtfCWauto") {
00963                 int j = 0;
00964                 for (int y = 0; y < ny; y++) {
00965                         for (int x = 0; x < nx / 2; x++, j += 2) {
00966 #ifdef  _WIN32
00967                                 float r = (float)_hypot((float)x, (float)(y - ny / 2.0f));
00968 #else
00969                                 float r = (float)hypot((float)x, (float)(y - ny / 2.0f));
00970 #endif  //_WIN32
00971                                 int l = static_cast < int >(Util::fast_floor(r));
00972 
00973                                 if (l >= 0 && l < ny / 2) {
00974                                         int k = y*nx/2 + x;
00975                                         tdr[k] *= src[j];
00976                                         tdi[k] *= src[j + 1];
00977 #ifdef  _WIN32
00978                                         tn[k] *= (float)_hypot(src[j], src[j + 1]);
00979 #else
00980                                         tn[k] *= (float)hypot(src[j], src[j + 1]);
00981 #endif  //_WIN32
00982                                 }
00983                         }
00984                 }
00985         }
00986 
00987 
00988         float *tmp_data = image0_copy->get_data();
00989 
00990         int j = 0;
00991         for (int y = 0; y < ny; y++) {
00992                 for (int x = 0; x < nx / 2; x++, j += 2) {
00993                         float r = Ctf::CTFOS * sqrt(x * x + (y - ny / 2.0f) * (y - ny / 2.0f));
00994                         int l = static_cast < int >(Util::fast_floor(r));
00995                         r -= l;
00996 
00997                         float f = 0;
00998                         if (l <= Ctf::CTFOS * ny / 2 - 2) {
00999                                 f = (ctf1[l] * (1 - r) + ctf1[l + 1] * r);
01000                         }
01001                         tmp_data[j] += src[j] * f;
01002                         tmp_data[j + 1] += src[j + 1] * f;
01003                 }
01004         }
01005 
01006         EMData *image_fft = image->do_fft();
01007         image_fft->update();
01008         if(image_ctf) {delete image_ctf; image_ctf=0;}
01009 }

EMData * CtfAverager::finish  )  [virtual]
 

Finish up the averaging and return the result.

Returns:
The averaged image.

Implements EMAN::Averager.

Definition at line 1011 of file averager.cpp.

References ctf, ctfn, curves, EMAN::EMData::do_ift(), EMAN::Util::fast_floor(), filter, EMAN::EMData::get_data(), EMAN::Averager::get_name(), image0_copy, nimg, nx, ny, outfile, EMAN::Util::save_data(), EMAN::EMData::set_attr(), snr, snri, snrn, tdi, tdr, tn, EMAN::EMData::update(), x, and y.

01012 {
01013         int j = 0;
01014         for (int y = 0; y < ny; y++) {
01015                 for (int x = 0; x < nx / 2; x++, j += 2) {
01016 #ifdef  _WIN32
01017                         float r = (float) _hypot(x, y - ny / 2.0f);
01018 #else
01019                         float r = (float) hypot(x, y - ny / 2.0f);
01020 #endif
01021                         int l = static_cast < int >(Util::fast_floor(r));
01022                         if (l >= 0 && l < ny / 2) {
01023                                 int k = y*nx/2 + x;
01024                                 snri[l] += (tdr[k] + tdi[k]/tn[k]);
01025                                 snrn[l] += 1;
01026                         }
01027                 }
01028         }
01029 
01030         for (int i = 0; i < ny / 2; i++) {
01031                 snri[i] *= nimg / snrn[i];
01032         }
01033 
01034         if(strcmp(outfile, "") != 0) {
01035                 Util::save_data(0, 1, snri, ny / 2, outfile);
01036         }
01037 
01038 
01039         float *cd = 0;
01040         if (curves) {
01041                 cd = curves->get_data();
01042         }
01043 
01044         for (int i = 0; i < Ctf::CTFOS * ny / 2; i++) {
01045                 float ctf0 = 0;
01046                 for (int j = 0; j < nimg; j++) {
01047                         ctf0 += ctfn[j][i];
01048                         if (ctf[j][i] == 0) {
01049                                 ctf[j][i] = 1.0e-12f;
01050                         }
01051 
01052                         if (curves) {
01053                                 cd[i] += ctf[j][i] * ctfn[j][i];
01054                                 cd[i + Ctf::CTFOS * ny / 2] += ctfn[j][i];
01055                                 cd[i + 2 * Ctf::CTFOS * ny / 2] += ctfn[j][i];
01056                         }
01057                 }
01058 
01059                 string alg_name = get_name();
01060 
01061                 if (alg_name == "CtfCW" && need_snr) {
01062                         snr[i] = ctf0;
01063                 }
01064 
01065                 float ctf1 = ctf0;
01066                 if (alg_name == "CtfCWauto") {
01067                         ctf1 = snri[i / Ctf::CTFOS];
01068                 }
01069 
01070                 if (ctf1 <= 0.0001f) {
01071                         ctf1 = 0.1f;
01072                 }
01073 
01074                 if (alg_name == "CtfC") {
01075                         for (int j = 0; j < nimg; j++) {
01076                                 ctf[j][i] = exp(-i * i / (filter * filter)) * ctfn[j][i] / (fabs(ctf[j][i]) * ctf1);
01077                         }
01078                 }
01079                 else if (alg_name == "Weighting") {
01080                         for (int j = 0; j < nimg; j++) {
01081                                 ctf[j][i] = ctfn[j][i] / ctf1;
01082                         }
01083                 }
01084                 else if (alg_name == "CtfCW") {
01085                         for (int j = 0; j < nimg; j++) {
01086                                 ctf[j][i] = (ctf1 / (ctf1 + 1)) * ctfn[j][i] / (ctf[j][i] * ctf1);
01087                         }
01088                 }
01089                 else if (alg_name == "CtfCWauto") {
01090                         for (int j = 0; j < nimg; j++) {
01091                                 ctf[j][i] = ctf1 * ctfn[j][i] / (fabs(ctf[j][i]) * ctf0);
01092                         }
01093                 }
01094         }
01095 
01096 
01097         if (curves) {
01098                 for (int i = 0; i < Ctf::CTFOS * ny / 2; i++) {
01099                         cd[i] /= cd[i + Ctf::CTFOS * ny / 2];
01100                 }
01101                 curves->update();
01102         }
01103 
01104         image0_copy->update();
01105 
01106         float *result_data = result->get_data();
01107         EMData *tmp_ift = image0_copy->do_ift();
01108         float *tmp_ift_data = tmp_ift->get_data();
01109         memcpy(result_data, tmp_ift_data, (nx - 2) * ny * sizeof(float));
01110 
01111         tmp_ift->update();
01112         result->update();
01113         result->set_attr("ptcl_repr",nimg);
01114 
01115         if( image0_copy )
01116         {
01117                 delete image0_copy;
01118                 image0_copy = 0;
01119         }
01120 
01121         if (snri) {
01122                 delete[]snri;
01123                 snri = 0;
01124         }
01125 
01126         if (snrn) {
01127                 delete[]snrn;
01128                 snrn = 0;
01129         }
01130 
01131         if( snri )
01132         {
01133                 delete [] snri;
01134                 snri = 0;
01135         }
01136         if( snrn )
01137         {
01138                 delete [] snrn;
01139                 snrn = 0;
01140         }
01141         if( tdr )
01142         {
01143                 delete [] tdr;
01144                 tdr = 0;
01145         }
01146         if( tdi )
01147         {
01148                 delete [] tdi;
01149                 tdi = 0;
01150         }
01151         if( tn )
01152         {
01153                 delete [] tn;
01154                 tn = 0;
01155         }
01156 
01157         return result;
01158 }

vector< float > EMAN::CtfAverager::get_snr  )  const [inline]
 

Definition at line 295 of file averager.h.

00296                 {
00297                         return snr;
00298                 }


Member Data Documentation

vector<vector<float> > EMAN::CtfAverager::ctf [private]
 

Definition at line 310 of file averager.h.

Referenced by add_image(), and finish().

vector<vector<float> > EMAN::CtfAverager::ctfn [private]
 

Definition at line 311 of file averager.h.

Referenced by add_image(), and finish().

EMData* EMAN::CtfAverager::curves [protected]
 

Definition at line 302 of file averager.h.

Referenced by add_image(), and finish().

float EMAN::CtfAverager::filter [private]
 

Definition at line 319 of file averager.h.

Referenced by add_image(), and finish().

EMData* EMAN::CtfAverager::image0_copy [private]
 

Definition at line 308 of file averager.h.

Referenced by add_image(), and finish().

EMData* EMAN::CtfAverager::image0_fft [private]
 

Definition at line 307 of file averager.h.

Referenced by add_image().

bool EMAN::CtfAverager::need_snr [protected]
 

Definition at line 303 of file averager.h.

int EMAN::CtfAverager::nimg [private]
 

Definition at line 320 of file averager.h.

Referenced by add_image(), and finish().

int EMAN::CtfAverager::nx [private]
 

Definition at line 321 of file averager.h.

int EMAN::CtfAverager::ny [private]
 

Definition at line 322 of file averager.h.

int EMAN::CtfAverager::nz [private]
 

Definition at line 323 of file averager.h.

const char* EMAN::CtfAverager::outfile [protected]
 

Definition at line 304 of file averager.h.

Referenced by finish().

XYData* EMAN::CtfAverager::sf [protected]
 

Definition at line 301 of file averager.h.

vector< float > EMAN::CtfAverager::snr [mutable, private]
 

Definition at line 306 of file averager.h.

Referenced by finish().

float* EMAN::CtfAverager::snri [private]
 

Definition at line 313 of file averager.h.

Referenced by add_image(), and finish().

float* EMAN::CtfAverager::snrn [private]
 

Definition at line 314 of file averager.h.

Referenced by add_image(), and finish().

float* EMAN::CtfAverager::tdi [private]
 

Definition at line 316 of file averager.h.

Referenced by add_image(), and finish().

float* EMAN::CtfAverager::tdr [private]
 

Definition at line 315 of file averager.h.

Referenced by add_image(), and finish().

float* EMAN::CtfAverager::tn [private]
 

Definition at line 317 of file averager.h.

Referenced by add_image(), and finish().


The documentation for this class was generated from the following files:
Generated on Thu Dec 9 13:47:14 2010 for EMAN2 by  doxygen 1.3.9.1