EMAN2
Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Attributes
EMAN::CtfCWautoAverager Class Reference

CtfCWautoAverager averages the images with CTF correction with a Wiener filter. More...

#include <averager.h>

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

List of all members.

Public Member Functions

 CtfCWautoAverager ()
void add_image (EMData *image)
 To add an image to the Averager.
EMDatafinish ()
 Finish up the averaging and return the result.
string get_name () const
 Get the Averager's name.
string get_desc () const
void set_params (const Dict &new_params)
 Set the Averager parameters using a key/value dictionary.

Static Public Member Functions

static AveragerNEW ()

Static Public Attributes

static const string NAME = "ctfw.auto"

Protected Attributes

EMDatasnrsum
int nimg

Detailed Description

CtfCWautoAverager averages the images with CTF correction with a Wiener filter.

The Weiner filter is estimated directly from the data.

Definition at line 461 of file averager.h.


Constructor & Destructor Documentation

CtfCWautoAverager::CtfCWautoAverager ( )

Definition at line 716 of file averager.cpp.

Referenced by NEW().

        : nimg(0)
{

}

Member Function Documentation

void CtfCWautoAverager::add_image ( EMData image) [virtual]

To add an image to the Averager.

This image will be averaged in this function.

Parameters:
imageThe image to be averaged.

Implements EMAN::Averager.

Definition at line 723 of file averager.cpp.

References EMAN::EMData::add(), b, EMAN::Ctf::bfactor, EMAN::Ctf::compute_2d_complex(), copy(), EMAN::EMData::copy_head(), EMAN::Ctf::CTF_AMP, EMAN::Ctf::CTF_SNR, EMAN::EMData::do_fft(), EMAN::EMData::get_attr(), EMAN::EMData::get_data(), get_name(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), EMAN::EMUtil::is_same_size(), LOGERR, nimg, EMAN::EMData::process_inplace(), snrsum, and EMAN::EMData::to_zero().

{
        if (!image) {
                return;
        }



        EMData *fft=image->do_fft();

        if (nimg >= 1 && !EMUtil::is_same_size(fft, result)) {
                LOGERR("%s Averager can only process images of the same size", get_name().c_str());
                return;
        }

        nimg++;
        if (nimg == 1) {
                result = fft->copy_head();
                result->to_zero();
        }

        Ctf *ctf = (Ctf *)image->get_attr("ctf");
//string cc=ctf->to_string();
//FILE *out=fopen("ctf.txt","a");
//fprintf(out,"%s\n",cc.c_str());
//fclose(out);
        float b=ctf->bfactor;
        ctf->bfactor=100.0;                     // FIXME - this is a temporary fixed B-factor which does a (very) little sharpening

//      if (nimg==1) unlink("snr.hdf");

        EMData *snr = result -> copy();
        ctf->compute_2d_complex(snr,Ctf::CTF_SNR);
//      snr->write_image("snr.hdf",-1);
        EMData *ctfi = result-> copy();
        ctf->compute_2d_complex(ctfi,Ctf::CTF_AMP);

        ctf->bfactor=b; // return to its original value

        float *outd = result->get_data();
        float *ind = fft->get_data();
        float *snrd = snr->get_data();
        float *ctfd = ctfi->get_data();

        size_t sz=snr->get_xsize()*snr->get_ysize();
        for (size_t i = 0; i < sz; i+=2) {
                if (snrd[i]<0) snrd[i]=0.001;   // Used to be 0. See ctfcauto averager
                ctfd[i]=fabs(ctfd[i]);
                if (ctfd[i]<.05) ctfd[i]=0.05f;
//              {
//                      if (snrd[i]<=0) ctfd[i]=.05f;
//                      else ctfd[i]=snrd[i]*10.0f;
//              }
                outd[i]+=ind[i]*snrd[i]/ctfd[i];
                outd[i+1]+=ind[i+1]*snrd[i]/ctfd[i];
        }

        if (nimg==1) {
                snrsum=snr->copy_head();
                float *ssnrd=snrsum->get_data();
                // we're only using the real component, and we need to start with 1.0
                for (size_t i = 0; i < sz; i+=2) { ssnrd[i]=1.0; ssnrd[i+1]=0.0; }
        }
//      snr->write_image("snr.hdf",-1);
        snr->process_inplace("math.absvalue");
        snrsum->add(*snr);

        delete ctf;
        delete fft;
        delete snr;
        delete ctfi;
}
EMData * CtfCWautoAverager::finish ( ) [virtual]

Finish up the averaging and return the result.

Returns:
The averaged image.

Implements EMAN::Averager.

Definition at line 796 of file averager.cpp.

References EMAN::EMData::calc_radial_dist(), EMAN::EMData::do_ift(), EMAN::EMData::get_data(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), nimg, nx, ny, EMAN::Averager::result, EMAN::EMData::set_attr(), snrsum, and EMAN::EMData::update().

{
/*      EMData *tmp=result->do_ift();
        tmp->write_image("ctfcw.hdf",0);
        delete tmp;

        tmp=snrsum->do_ift();
        tmp->write_image("ctfcw.hdf",1);
        delete tmp;*/

//snrsum->write_image("snrsum.hdf",-1);
        //size_t sz=result->get_xsize()*result->get_ysize();
        int nx=result->get_xsize();
        int ny=result->get_ysize();     
        float *snrsd=snrsum->get_data();
        float *outd=result->get_data();

        int rm=(ny-2)*(ny-2)/4;
        for (int j=0; j<ny; j++) {
                for (int i=0; i<nx; i+=2) {
                        size_t ii=i+j*nx;
                        if ((j<ny/2 && i*i/4+j*j>rm) ||(j>=ny/2 && i*i/4+(ny-j)*(ny-j)>rm) || snrsd[ii]==0) { outd[ii]=outd[ii+1]=0; continue; }
                        outd[ii]/=snrsd[ii];            // snrsd contains total SNR
                        outd[ii+1]/=snrsd[ii];
                }
        }

        result->update();
        result->set_attr("ptcl_repr",nimg);
        result->set_attr("ctf_snr_total",snrsum->calc_radial_dist(snrsum->get_ysize()/2,0,1,false));
        result->set_attr("ctf_wiener_filtered",1);
        
        delete snrsum;
        EMData *ret=result->do_ift();
        delete result;
        result=NULL;
        return ret;
}
string EMAN::CtfCWautoAverager::get_desc ( ) const [inline, virtual]

Implements EMAN::Averager.

Definition at line 474 of file averager.h.

                {
                        return "Averaging with autmatic CTF correction. Does not require a structure factor, but only works with EMAN2's CTF model";
                }
string EMAN::CtfCWautoAverager::get_name ( ) const [inline, virtual]

Get the Averager's name.

Each Averager is identified by a unique name.

Returns:
The Averager's name.

Implements EMAN::Averager.

Definition at line 469 of file averager.h.

References NAME.

Referenced by add_image().

                {
                        return NAME;
                }
static Averager* EMAN::CtfCWautoAverager::NEW ( ) [inline, static]

Definition at line 479 of file averager.h.

References CtfCWautoAverager().

                {
                        return new CtfCWautoAverager();
                }
void EMAN::CtfCWautoAverager::set_params ( const Dict new_params) [inline, virtual]

Set the Averager parameters using a key/value dictionary.

Parameters:
new_paramsA dictionary containing the new parameters.

Reimplemented from EMAN::Averager.

Definition at line 484 of file averager.h.

References EMAN::Averager::params.

                {
                        params = new_params;
//                      outfile = params["outfile"];
                }

Member Data Documentation

const string CtfCWautoAverager::NAME = "ctfw.auto" [static]

Definition at line 490 of file averager.h.

Referenced by get_name().

Definition at line 494 of file averager.h.

Referenced by add_image(), and finish().

Definition at line 493 of file averager.h.

Referenced by add_image(), and finish().


The documentation for this class was generated from the following files: