EMAN2
Classes | Public Member Functions | Private Attributes
EMAN::XYData Class Reference

XYData defines a 1D (x,y) data set. More...

#include <xydata.h>

Collaboration diagram for EMAN::XYData:
Collaboration graph
[legend]

List of all members.

Classes

struct  Pair

Public Member Functions

 XYData ()
virtual ~XYData ()
int read_file (const string &filename)
int write_file (const string &filename) const
float calc_correlation (XYData *xy, float minx, float maxx) const
void update ()
float get_yatx (float x, bool outzero=true)
float get_yatx_smooth (float x, int smoothing)
float get_x (size_t i) const
void set_x (size_t i, float x)
float get_y (size_t i) const
void set_y (size_t i, float y)
void insort (float x, float y)
void dedupx ()
vector< float > get_xlist () const
vector< float > get_ylist () const
vector< float > get_state () const
void set_state (vector< float >)
void set_xy_list (const vector< float > &xlist, const vector< float > &ylist)
size_t get_size () const
void set_size (size_t n)
float get_miny ()
float get_maxy ()
bool is_validx (float x) const

Private Attributes

vector< Pairdata
float ymin
float ymax
float mean_x_spacing

Detailed Description

XYData defines a 1D (x,y) data set.

Definition at line 49 of file xydata.h.


Constructor & Destructor Documentation

XYData::XYData ( )

Definition at line 52 of file xydata.cpp.

        : ymin(FLT_MAX), ymax(-FLT_MAX), mean_x_spacing(1.0)
{
}
virtual EMAN::XYData::~XYData ( ) [inline, virtual]

Definition at line 70 of file xydata.h.

                {
                }

Member Function Documentation

float XYData::calc_correlation ( XYData xy,
float  minx,
float  maxx 
) const

Definition at line 133 of file xydata.cpp.

References data, get_yatx(), is_validx(), LOGERR, sqrt(), update(), and x.

{
        size_t n = data.size();
        float x0 = data[0].x;
        float xn = data[n - 1].x;

        if (maxx <= minx || minx >= xn || maxx <= x0) {
                LOGERR("incorrect minx, maxx=%f,%f for this XYData range [%f,%f]",
                           minx, maxx, x0, xn);
                return 0;
        }

        float scc = 0;
        float norm1 = 0;
        float norm2 = 0;

        xy->update();
        for (size_t i = 0; i < n; i++) {
                float x = data[i].x;
                if (x >= minx && x <= maxx && xy->is_validx(x)) {
                        float selfy = data[i].y;
                        float xyy = xy->get_yatx(x);

                        scc += selfy * xyy;
                        norm1 += selfy * selfy;
                        norm2 += xyy * xyy;
                }
        }

        float result = scc / sqrt(norm1 * norm2);
        return result;
}
void XYData::dedupx ( )

Definition at line 248 of file xydata.cpp.

References data, x, and y.

                    {
        float acnt=1.0;
        for (std::vector<Pair>::iterator it = data.begin() ; it+1 < data.end(); ++it) {
                while (it+1<data.end() && it->x==(it+1)->x) {
//                      printf("%d\t%d\t%1.0f\t%f\t%f\n",it-data.begin(),data.end()-it,acnt,it->x,(it+1)->x);
                        it->y+=(it+1)->y;
                        acnt+=1.0;
                        data.erase(it+1);
                }
                if (acnt>1.0) {
                        it->y/=acnt;
                        acnt=1.0;
                }
        }
}
float EMAN::XYData::get_maxy ( ) [inline]

Definition at line 140 of file xydata.h.

References ymax.

                {
//                      update();
                        return ymax;
                }
float EMAN::XYData::get_miny ( ) [inline]

Definition at line 134 of file xydata.h.

References ymin.

                {
//                      update();
                        return ymin;
                }
size_t EMAN::XYData::get_size ( ) const [inline]
vector< float > XYData::get_state ( ) const

Definition at line 280 of file xydata.cpp.

References data.

                                       {
        vector<float> list;
        vector<Pair>::const_iterator cit;
        for(cit=data.begin(); cit!=data.end(); ++cit) {
                list.push_back( (*cit).x);
                list.push_back( (*cit).y);
        }

        return list;
        
}
float EMAN::XYData::get_x ( size_t  i) const [inline]

Definition at line 88 of file xydata.h.

References data.

Referenced by EMAN::SetSFProcessor::create_radial_func(), and EMAN::TestUtil::to_emobject().

                {
                        return data[i].x;
                }
vector< float > XYData::get_xlist ( ) const

Definition at line 305 of file xydata.cpp.

References data.

{
        vector<float> xlist;
        vector<Pair>::const_iterator cit;
        for(cit=data.begin(); cit!=data.end(); ++cit) {
                xlist.push_back( (*cit).x);
        }

        return xlist;
}
float EMAN::XYData::get_y ( size_t  i) const [inline]

Definition at line 99 of file xydata.h.

References data.

Referenced by EMAN::FSCFourierProcessor::process(), EMAN::SNRProcessor::process_inplace(), and EMAN::TestUtil::to_emobject().

                {
                        return data[i].y;
                }
float XYData::get_yatx ( float  x,
bool  outzero = true 
)

Definition at line 167 of file xydata.cpp.

References data, mean_x_spacing, nx, x, and y.

Referenced by EMAN::FourierWeightAverager::add_image(), calc_correlation(), EMAN::EMAN2Ctf::compute_1d(), EMAN::EMAN1Ctf::compute_1d(), EMAN::EMAN1Ctf::compute_2d_complex(), EMAN::SetSFProcessor::create_radial_func(), EMAN::MatchSFProcessor::create_radial_func(), and EMAN::FSCFourierProcessor::process().

{
        if (data.size()==0 || mean_x_spacing==0) return 0.0;

        int nx = (int) data.size();
        // Do the range checking up front before we get into trouble
        if (x<data[0].x) return outzero?0.0f:data[0].y;
        if (x>data[nx-1].x) return outzero?0.0f:data[nx-1].y;
        
        int s = (int) floor((x - data[0].x) / mean_x_spacing);
        if (s>nx-1) s=nx-1;

        // These deal with possibly nonuniform x values. A btree would be better, but this is simple
        while (s>0 && data[s].x > x) s--;
        while (s<(nx-1) && data[s + 1].x < x ) s++;
        if (s>nx-2) return outzero?0.0f:data[nx-1].y;
        
        float f = (x - data[s].x) / (data[s + 1].x - data[s].x);
        float y = data[s].y * (1 - f) + data[s + 1].y * f;
        return y;
}
float XYData::get_yatx_smooth ( float  x,
int  smoothing 
)

Definition at line 189 of file xydata.cpp.

References data, InvalidParameterException, mean_x_spacing, nx, x, and y.

{
        if (data.size()==0) return 0.0;
        if (data.size()==1) return data[0].y;
        if (smoothing!=1) throw InvalidParameterException("Only smoothing==1 (linear) currently supported");
        
        int nx = (int) data.size();
        
        int s = nx/2; 
        if (mean_x_spacing>0) s=(int) floor((x - data[0].x) / mean_x_spacing);
        if (s>nx-2) s=nx-2;
        else if (s<0) s=0;
        else {
                // These deal with possibly nonuniform x values. A btree would be better, but this is simple, and there usually won't be THAT many points
                while (s>0 && data[s].x > x) s--;
                while (s<(nx-2) && data[s + 1].x < x ) s++;
        }
        
        float f = 0,y=0;
        if (data[s + 1].x != data[s].x) {
                f= (x - data[s].x) / (data[s + 1].x - data[s].x);
                y = data[s].y * (1 - f) + data[s + 1].y * f;
        }
        else {
                int s2=s;
                while (data[s2].x==data[s].x) {
                        if (s2<nx-1) s2++;
                        if (s>0) s--;
                        if (s==0 &&s2==nx-1) return data[nx/2].y;
                }
                f= (x - data[s].x) / (data[s2].x - data[s].x);
                y = data[s].y * (1 - f) + data[s2].y * f;
                
        }
//      printf("%d %1.2f x %1.2f %1.2f %1.2f y %1.2f %1.2f\n",s,f,x,data[s].x,data[s+1].x,data[s].y,data[s+1].y);
        return y;
}
vector< float > XYData::get_ylist ( ) const

Definition at line 316 of file xydata.cpp.

References data.

{
        vector<float> ylist;
        vector<Pair>::const_iterator cit;
        for(cit=data.begin(); cit!=data.end(); ++cit) {
                ylist.push_back( (*cit).y);
        }

        return ylist;
}
void XYData::insort ( float  x,
float  y 
)

Definition at line 228 of file xydata.cpp.

References data, and update().

                                    {
        data.push_back(Pair(x,y));
        update();
        
        
        //      int nx = (int) data.size();
//      if (nx==0) { data.push_back(Pair(x,y)); return; }
//      
//      int s = (int) floor((x - data[0].x) / mean_x_spacing);
//      if (s>nx) s=nx;
//      else if (s<0) s=0;
//      else {
//      // These deal with possibly nonuniform x values. A btree would be better, but this is simple, and there usually won't be THAT many points
//              while (s>0 && data[s-1].x > x) s--;
//              while (s<nx && data[s].x <= x ) s++;
//      }
//      data.insert(data.begin()+s,Pair(x,y));
}       
bool EMAN::XYData::is_validx ( float  x) const [inline]

Definition at line 146 of file xydata.h.

References data.

Referenced by calc_correlation().

                {
                        if (x < data[0].x || x > data[data.size() - 1].x)
                        {
                                return false;
                        }
                        return true;
                }
int XYData::read_file ( const string &  filename)

Definition at line 81 of file xydata.cpp.

References data, FileAccessException, in, update(), x, and y.

Referenced by EMAN::FSCFourierProcessor::process(), and EMAN::SNRProcessor::process_inplace().

{
        FILE *in = fopen(filename.c_str(), "rb");
        if (!in) {
                throw FileAccessException(filename.c_str());
//              LOGERR("cannot open xydata file '%s'", filename.c_str());
//              return 1;
        }

        char buf[MAXPATHLEN];
        char tmp_str[MAXPATHLEN];

        while (fgets(buf, MAXPATHLEN, in)) {
                if (buf[0] != '#') {
                        float x = 0;
                        float y = 0;

                        if (sscanf(buf, " %f%[^.0-9-]%f", &x, tmp_str, &y) != 3) {
                                break;
                        }
                        data.push_back(Pair(x, y));
                }
        }

        fclose(in);
        in = 0;

        update();

        return 0;
}
void XYData::set_size ( size_t  n)

Definition at line 275 of file xydata.cpp.

References data.

{
        data.resize(n, Pair(0.0f, 0.0f));
}
void XYData::set_state ( vector< float >  list)

Definition at line 292 of file xydata.cpp.

References data, InvalidParameterException, and update().

                                          {
        if(list.size()%2==1) {
                throw InvalidParameterException("Invalid pickled data");
        }

        data.clear();
        for(unsigned int i=0; i<list.size(); i+=2) {
                data.push_back(Pair(list[i], list[i+1]));
        }

        update();
}
void EMAN::XYData::set_x ( size_t  i,
float  x 
) [inline]

Definition at line 93 of file xydata.h.

References data, and x.

Referenced by EMAN::MatchSFProcessor::create_radial_func(), and EMAN::FSCFourierProcessor::process().

                {
                        if (i>=data.size()) data.resize(i+1,Pair(0,0));
                        data[i].x = x;
                }
void XYData::set_xy_list ( const vector< float > &  xlist,
const vector< float > &  ylist 
)

Definition at line 264 of file xydata.cpp.

References data, and InvalidParameterException.

{
        if(xlist.size() != ylist.size()) {
                throw InvalidParameterException("xlist and ylist size does not match!");
        }

        for(unsigned int i=0; i<xlist.size(); ++i) {
                data.push_back(Pair(xlist[i], ylist[i]));
        }
}
void EMAN::XYData::set_y ( size_t  i,
float  y 
) [inline]

Definition at line 104 of file xydata.h.

References data, and y.

Referenced by EMAN::MatchSFProcessor::create_radial_func(), EMAN::FSCFourierProcessor::process(), and EMAN::SNRProcessor::process_inplace().

                {
                        if (i>=data.size()) data.resize(i+1,Pair(0,0));
                        data[i].y = y;
                }
void XYData::update ( )

Definition at line 57 of file xydata.cpp.

References data, mean_x_spacing, ymax, and ymin.

Referenced by calc_correlation(), insort(), EMAN::SNRProcessor::process_inplace(), read_file(), and set_state().

{
        if (data.size()==0) return;
        
        std::sort(data.begin(), data.end());

        ymin = FLT_MAX;
        ymax = -FLT_MAX;

        typedef vector < Pair >::const_iterator Ptype;
        for (Ptype p = data.begin(); p != data.end(); p++) {
                if (p->y > ymax) {
                        ymax = p->y;
                }
                if (p->y < ymin) {
                        ymin = p->y;
                }
        }

        size_t n = data.size();
        mean_x_spacing = (data[n - 1].x - data[0].x) / (float) n;
}
int XYData::write_file ( const string &  filename) const

Definition at line 113 of file xydata.cpp.

References data, and LOGERR.

{
        FILE *out = fopen(filename.c_str(), "wb");
        if (!out) {
                LOGERR("cannot open xydata file '%s' to write", filename.c_str());
                return 1;
        }

        typedef vector < Pair >::const_iterator Ptype;
        for (Ptype p = data.begin(); p != data.end(); p++) {
                fprintf(out, "%1.6g\t%1.6g\n", p->x, p->y);
        }

        fclose(out);
        out = 0;

        return 0;
}

Member Data Documentation

vector< Pair > EMAN::XYData::data [private]

Definition at line 159 of file xydata.h.

Referenced by get_yatx(), get_yatx_smooth(), and update().

float EMAN::XYData::ymax [private]

Definition at line 158 of file xydata.h.

Referenced by get_maxy(), and update().

float EMAN::XYData::ymin [private]

Definition at line 157 of file xydata.h.

Referenced by get_miny(), and update().


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