#include <xydata.h>
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_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) |
vector< float > | get_xlist () const |
vector< float > | get_ylist () const |
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< Pair > | data |
float | ymin |
float | ymax |
float | mean_x_spacing |
Classes | |
struct | Pair |
Definition at line 49 of file xydata.h.
XYData::XYData | ( | ) |
Definition at line 52 of file xydata.cpp.
00053 : ymin(FLT_MAX), ymax(-FLT_MAX), mean_x_spacing(1.0) 00054 { 00055 }
virtual EMAN::XYData::~XYData | ( | ) | [inline, virtual] |
float XYData::calc_correlation | ( | XYData * | xy, | |
float | minx, | |||
float | maxx | |||
) | const |
Definition at line 130 of file xydata.cpp.
References data, get_yatx(), is_validx(), LOGERR, sqrt(), update(), x, and y.
00131 { 00132 size_t n = data.size(); 00133 float x0 = data[0].x; 00134 float xn = data[n - 1].x; 00135 00136 if (maxx <= minx || minx >= xn || maxx <= x0) { 00137 LOGERR("incorrect minx, maxx=%f,%f for this XYData range [%f,%f]", 00138 minx, maxx, x0, xn); 00139 return 0; 00140 } 00141 00142 float scc = 0; 00143 float norm1 = 0; 00144 float norm2 = 0; 00145 00146 xy->update(); 00147 for (size_t i = 0; i < n; i++) { 00148 float x = data[i].x; 00149 if (x >= minx && x <= maxx && xy->is_validx(x)) { 00150 float selfy = data[i].y; 00151 float xyy = xy->get_yatx(x); 00152 00153 scc += selfy * xyy; 00154 norm1 += selfy * selfy; 00155 norm2 += xyy * xyy; 00156 } 00157 } 00158 00159 float result = scc / sqrt(norm1 * norm2); 00160 return result; 00161 }
float EMAN::XYData::get_maxy | ( | ) | [inline] |
float EMAN::XYData::get_miny | ( | ) | [inline] |
size_t EMAN::XYData::get_size | ( | ) | const [inline] |
Definition at line 113 of file xydata.h.
References data.
Referenced by EMAN::SNRProcessor::process_inplace().
00114 { 00115 return data.size(); 00116 }
float EMAN::XYData::get_x | ( | size_t | i | ) | const [inline] |
vector< float > XYData::get_xlist | ( | ) | const |
Definition at line 202 of file xydata.cpp.
References data.
00203 { 00204 vector<float> xlist; 00205 vector<Pair>::const_iterator cit; 00206 for(cit=data.begin(); cit!=data.end(); ++cit) { 00207 xlist.push_back( (*cit).x); 00208 } 00209 00210 return xlist; 00211 }
float EMAN::XYData::get_y | ( | size_t | i | ) | const [inline] |
Definition at line 96 of file xydata.h.
References data.
Referenced by EMAN::SNRProcessor::process_inplace().
00097 { 00098 return data[i].y; 00099 }
float XYData::get_yatx | ( | float | x, | |
bool | outzero = true | |||
) |
Definition at line 164 of file xydata.cpp.
References data, mean_x_spacing, nx, and y.
Referenced by calc_correlation(), EMAN::EMAN2Ctf::compute_1d(), EMAN::EMAN1Ctf::compute_1d(), EMAN::EMAN2Ctf::compute_2d_complex(), EMAN::EMAN1Ctf::compute_2d_complex(), EMAN::SetSFProcessor::create_radial_func(), and EMAN::MatchSFProcessor::create_radial_func().
00165 { 00166 if (data.size()==0 || mean_x_spacing==0) return 0.0; 00167 00168 int nx = (int) data.size(); 00169 // Do the range checking up front before we get into trouble 00170 if (x<data[0].x) return outzero?0.0f:data[0].y; 00171 if (x>data[nx-1].x) return outzero?0.0f:data[nx-1].y; 00172 00173 int s = (int) floor((x - data[0].x) / mean_x_spacing); 00174 if (s>nx-1) s=nx-1; 00175 00176 // These deal with possibly nonuniform x values. A btree would be better, but this is simple 00177 while (s>0 && data[s].x > x) s--; 00178 while (s<(nx-1) && data[s + 1].x < x ) s++; 00179 if (s>nx-2) return outzero?0.0f:data[nx-1].y; 00180 00181 float f = (x - data[s].x) / (data[s + 1].x - data[s].x); 00182 float y = data[s].y * (1 - f) + data[s + 1].y * f; 00183 return y; 00184 }
vector< float > XYData::get_ylist | ( | ) | const |
Definition at line 213 of file xydata.cpp.
References data.
00214 { 00215 vector<float> ylist; 00216 vector<Pair>::const_iterator cit; 00217 for(cit=data.begin(); cit!=data.end(); ++cit) { 00218 ylist.push_back( (*cit).y); 00219 } 00220 00221 return ylist; 00222 }
bool EMAN::XYData::is_validx | ( | float | x | ) | const [inline] |
int XYData::read_file | ( | const string & | filename | ) |
Definition at line 79 of file xydata.cpp.
References data, in, LOGERR, update(), x, and y.
Referenced by EMAN::SNRProcessor::process_inplace().
00080 { 00081 FILE *in = fopen(filename.c_str(), "rb"); 00082 if (!in) { 00083 LOGERR("cannot open xydata file '%s'", filename.c_str()); 00084 return 1; 00085 } 00086 00087 char buf[MAXPATHLEN]; 00088 char tmp_str[MAXPATHLEN]; 00089 00090 while (fgets(buf, MAXPATHLEN, in)) { 00091 if (buf[0] != '#') { 00092 float x = 0; 00093 float y = 0; 00094 00095 if (sscanf(buf, " %f%[^.0-9-]%f", &x, tmp_str, &y) != 3) { 00096 break; 00097 } 00098 data.push_back(Pair(x, y)); 00099 } 00100 } 00101 00102 fclose(in); 00103 in = 0; 00104 00105 update(); 00106 00107 return 0; 00108 }
void XYData::set_size | ( | size_t | n | ) |
Definition at line 197 of file xydata.cpp.
References data.
00198 { 00199 data.resize(n, Pair(0.0f, 0.0f)); 00200 }
void EMAN::XYData::set_x | ( | size_t | i, | |
float | x | |||
) | [inline] |
void XYData::set_xy_list | ( | const vector< float > & | xlist, | |
const vector< float > & | ylist | |||
) |
Definition at line 186 of file xydata.cpp.
References data, and InvalidParameterException.
00187 { 00188 if(xlist.size() != ylist.size()) { 00189 throw InvalidParameterException("xlist and ylist size does not match!"); 00190 } 00191 00192 for(unsigned int i=0; i<xlist.size(); ++i) { 00193 data.push_back(Pair(xlist[i], ylist[i])); 00194 } 00195 }
void EMAN::XYData::set_y | ( | size_t | i, | |
float | y | |||
) | [inline] |
Definition at line 101 of file xydata.h.
References data.
Referenced by EMAN::MatchSFProcessor::create_radial_func(), and EMAN::SNRProcessor::process_inplace().
void XYData::update | ( | ) |
Definition at line 57 of file xydata.cpp.
References data, mean_x_spacing, ymax, and ymin.
Referenced by calc_correlation(), EMAN::SNRProcessor::process_inplace(), and read_file().
00058 { 00059 std::sort(data.begin(), data.end()); 00060 00061 ymin = FLT_MAX; 00062 ymax = -FLT_MAX; 00063 00064 typedef vector < Pair >::const_iterator Ptype; 00065 for (Ptype p = data.begin(); p != data.end(); p++) { 00066 if (p->y > ymax) { 00067 ymax = p->y; 00068 } 00069 if (p->y < ymin) { 00070 ymin = p->y; 00071 } 00072 } 00073 00074 size_t n = data.size(); 00075 mean_x_spacing = (data[n - 1].x - data[0].x) / (float) n; 00076 }
int XYData::write_file | ( | const string & | filename | ) | const |
Definition at line 110 of file xydata.cpp.
00111 { 00112 FILE *out = fopen(filename.c_str(), "wb"); 00113 if (!out) { 00114 LOGERR("cannot open xydata file '%s' to write", filename.c_str()); 00115 return 1; 00116 } 00117 00118 typedef vector < Pair >::const_iterator Ptype; 00119 for (Ptype p = data.begin(); p != data.end(); p++) { 00120 fprintf(out, "%1.6g\t%1.6g\n", p->x, p->y); 00121 } 00122 00123 fclose(out); 00124 out = 0; 00125 00126 return 0; 00127 }
vector< Pair > EMAN::XYData::data [private] |
Definition at line 142 of file xydata.h.
Referenced by calc_correlation(), get_size(), get_x(), get_xlist(), get_y(), get_yatx(), get_ylist(), is_validx(), read_file(), set_size(), set_x(), set_xy_list(), set_y(), update(), and write_file().
float EMAN::XYData::mean_x_spacing [private] |
float EMAN::XYData::ymax [private] |
float EMAN::XYData::ymin [private] |