EMAN2
Classes | Public Member Functions | Private Attributes | List of all members
EMAN::XYData Class Reference

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

#include <xydata.h>

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)
 
void make_gauss (int n, float xmax, float width)
 
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 46 of file xydata.h.

Constructor & Destructor Documentation

◆ XYData()

XYData::XYData ( )

Definition at line 48 of file xydata.cpp.

49 : ymin(FLT_MAX), ymax(-FLT_MAX), mean_x_spacing(1.0)
50{
51}
float mean_x_spacing
Definition: xydata.h:159
float ymin
Definition: xydata.h:157
float ymax
Definition: xydata.h:158

◆ ~XYData()

virtual EMAN::XYData::~XYData ( )
inlinevirtual

Definition at line 67 of file xydata.h.

68 {
69 }

Member Function Documentation

◆ calc_correlation()

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

Definition at line 129 of file xydata.cpp.

130{
131 size_t n = data.size();
132 float x0 = data[0].x;
133 float xn = data[n - 1].x;
134
135 if (maxx <= minx || minx >= xn || maxx <= x0) {
136 LOGERR("incorrect minx, maxx=%f,%f for this XYData range [%f,%f]",
137 minx, maxx, x0, xn);
138 return 0;
139 }
140
141 float scc = 0;
142 float norm1 = 0;
143 float norm2 = 0;
144
145 xy->update();
146 for (size_t i = 0; i < n; i++) {
147 float x = data[i].x;
148 if (x >= minx && x <= maxx && xy->is_validx(x)) {
149 float selfy = data[i].y;
150 float xyy = xy->get_yatx(x);
151
152 scc += selfy * xyy;
153 norm1 += selfy * selfy;
154 norm2 += xyy * xyy;
155 }
156 }
157
158 float result = scc / sqrt(norm1 * norm2);
159 return result;
160}
void update()
Definition: xydata.cpp:53
vector< Pair > data
Definition: xydata.h:156
float get_yatx(float x, bool outzero=true)
Definition: xydata.cpp:172
bool is_validx(float x) const
Definition: xydata.h:146
EMData * sqrt() const
return square root of current image
#define LOGERR
Definition: log.h:51
#define x(i)
Definition: projector.cpp:1517

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

◆ dedupx()

void XYData::dedupx ( )

Definition at line 253 of file xydata.cpp.

253 {
254 float acnt=1.0;
255 for (std::vector<Pair>::iterator it = data.begin() ; it+1 < data.end(); ++it) {
256 while (it+1<data.end() && it->x==(it+1)->x) {
257// printf("%d\t%d\t%1.0f\t%f\t%f\n",it-data.begin(),data.end()-it,acnt,it->x,(it+1)->x);
258 it->y+=(it+1)->y;
259 acnt+=1.0;
260 data.erase(it+1);
261 }
262 if (acnt>1.0) {
263 it->y/=acnt;
264 acnt=1.0;
265 }
266 }
267}
#define y(i, j)
Definition: projector.cpp:1516

References data, and y.

◆ get_maxy()

float EMAN::XYData::get_maxy ( )
inline

Definition at line 140 of file xydata.h.

141 {
142// update();
143 return ymax;
144 }

References ymax.

◆ get_miny()

float EMAN::XYData::get_miny ( )
inline

Definition at line 134 of file xydata.h.

135 {
136// update();
137 return ymin;
138 }

References ymin.

◆ get_size()

size_t EMAN::XYData::get_size ( ) const
inline

◆ get_state()

vector< float > XYData::get_state ( ) const

Definition at line 285 of file xydata.cpp.

285 {
286 vector<float> list;
287 vector<Pair>::const_iterator cit;
288 for(cit=data.begin(); cit!=data.end(); ++cit) {
289 list.push_back( (*cit).x);
290 list.push_back( (*cit).y);
291 }
292
293 return list;
294
295}

References data.

Referenced by EMAN::SetIsoPowProcessor::process_inplace().

◆ get_x()

float EMAN::XYData::get_x ( size_t  i) const
inline

Definition at line 84 of file xydata.h.

85 {
86 return data[i].x;
87 }

References data.

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

◆ get_xlist()

vector< float > XYData::get_xlist ( ) const

Definition at line 310 of file xydata.cpp.

311{
312 vector<float> xlist;
313 vector<Pair>::const_iterator cit;
314 for(cit=data.begin(); cit!=data.end(); ++cit) {
315 xlist.push_back( (*cit).x);
316 }
317
318 return xlist;
319}

References data.

◆ get_y()

float EMAN::XYData::get_y ( size_t  i) const
inline

Definition at line 95 of file xydata.h.

96 {
97 if (i>=data.size()) throw InvalidValueException(i, "Attempt to access XYData out of range");
98 return data[i].y;
99 }
#define InvalidValueException(val, desc)
Definition: exception.h:285

References data, and InvalidValueException.

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

◆ get_yatx()

float XYData::get_yatx ( float  x,
bool  outzero = true 
)

Definition at line 172 of file xydata.cpp.

173{
174 if (data.size()==0 || mean_x_spacing==0) return 0.0;
175
176 int nx = (int) data.size();
177 // Do the range checking up front before we get into trouble
178 if (x<data[0].x) return outzero?0.0f:data[0].y;
179 if (x>data[nx-1].x) return outzero?0.0f:data[nx-1].y;
180
181 int s = (int) floor((x - data[0].x) / mean_x_spacing);
182 if (s>nx-1) s=nx-1;
183
184 // These deal with possibly nonuniform x values. A btree would be better, but this is simple
185 while (s>0 && data[s].x > x) s--;
186 while (s<(nx-1) && data[s + 1].x < x ) s++;
187 if (s>nx-2) return outzero?0.0f:data[nx-1].y;
188
189 float f = (x - data[s].x) / (data[s + 1].x - data[s].x);
190 float y = data[s].y * (1 - f) + data[s + 1].y * f;
191 return y;
192}

References data, mean_x_spacing, x, and y.

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

◆ get_yatx_smooth()

float XYData::get_yatx_smooth ( float  x,
int  smoothing 
)

Definition at line 194 of file xydata.cpp.

195{
196 if (data.size()==0) return 0.0;
197 if (data.size()==1) return data[0].y;
198 if (smoothing!=1) throw InvalidParameterException("Only smoothing==1 (linear) currently supported");
199
200 int nx = (int) data.size();
201
202 int s = nx/2;
203 if (mean_x_spacing>0) s=(int) floor((x - data[0].x) / mean_x_spacing);
204 if (s>nx-2) s=nx-2;
205 else if (s<0) s=0;
206 else {
207 // 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
208 while (s>0 && data[s].x > x) s--;
209 while (s<(nx-2) && data[s + 1].x < x ) s++;
210 }
211
212 float f = 0,y=0;
213 if (data[s + 1].x != data[s].x) {
214 f= (x - data[s].x) / (data[s + 1].x - data[s].x);
215 y = data[s].y * (1 - f) + data[s + 1].y * f;
216 }
217 else {
218 int s2=s;
219 while (data[s2].x==data[s].x) {
220 if (s2<nx-1) s2++;
221 if (s>0) s--;
222 if (s==0 &&s2==nx-1) return data[nx/2].y;
223 }
224 f= (x - data[s].x) / (data[s2].x - data[s].x);
225 y = data[s].y * (1 - f) + data[s2].y * f;
226
227 }
228// 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);
229 return y;
230}
#define InvalidParameterException(desc)
Definition: exception.h:361

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

◆ get_ylist()

vector< float > XYData::get_ylist ( ) const

Definition at line 321 of file xydata.cpp.

322{
323 vector<float> ylist;
324 vector<Pair>::const_iterator cit;
325 for(cit=data.begin(); cit!=data.end(); ++cit) {
326 ylist.push_back( (*cit).y);
327 }
328
329 return ylist;
330}

References data.

◆ insort()

void XYData::insort ( float  x,
float  y 
)

Definition at line 233 of file xydata.cpp.

233 {
234 data.push_back(Pair(x,y));
235 update();
236
237
238 // int nx = (int) data.size();
239// if (nx==0) { data.push_back(Pair(x,y)); return; }
240//
241// int s = (int) floor((x - data[0].x) / mean_x_spacing);
242// if (s>nx) s=nx;
243// else if (s<0) s=0;
244// else {
245// // 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
246// while (s>0 && data[s-1].x > x) s--;
247// while (s<nx && data[s].x <= x ) s++;
248// }
249// data.insert(data.begin()+s,Pair(x,y));
250}

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

◆ is_validx()

bool EMAN::XYData::is_validx ( float  x) const
inline

Definition at line 146 of file xydata.h.

147 {
148 if (x < data[0].x || x > data[data.size() - 1].x)
149 {
150 return false;
151 }
152 return true;
153 }

References data, and x.

Referenced by calc_correlation().

◆ make_gauss()

void XYData::make_gauss ( int  n,
float  xmax,
float  width 
)

Definition at line 162 of file xydata.cpp.

163{
164 set_size(n);
165 for (int i=0; i<n; i++) {
166 float x=i*xmax/n;
167 data[i].x=x;
168 data[i].y=exp(-x*x/(width*width));
169 }
170}
void set_size(size_t n)
Definition: xydata.cpp:280

References data, set_size(), and x.

Referenced by EMAN::GaussSegmentProcessor::process().

◆ read_file()

int XYData::read_file ( const string &  filename)

Definition at line 77 of file xydata.cpp.

78{
79 FILE *in = fopen(filename.c_str(), "rb");
80 if (!in) {
81 throw FileAccessException(filename.c_str());
82// LOGERR("cannot open xydata file '%s'", filename.c_str());
83// return 1;
84 }
85
86 char buf[MAXPATHLEN];
87 char tmp_str[MAXPATHLEN];
88
89 while (fgets(buf, MAXPATHLEN, in)) {
90 if (buf[0] != '#') {
91 float x = 0;
92 float y = 0;
93
94 if (sscanf(buf, " %f%[^.0-9-]%f", &x, tmp_str, &y) != 3) {
95 break;
96 }
97 data.push_back(Pair(x, y));
98 }
99 }
100
101 fclose(in);
102 in = 0;
103
104 update();
105
106 return 0;
107}
#define FileAccessException(filename)
Definition: exception.h:187

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

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

◆ set_size()

void XYData::set_size ( size_t  n)

Definition at line 280 of file xydata.cpp.

281{
282 data.resize(n, Pair(0.0f, 0.0f));
283}

References data.

Referenced by make_gauss().

◆ set_state()

void XYData::set_state ( vector< float >  list)

Definition at line 297 of file xydata.cpp.

297 {
298 if(list.size()%2==1) {
299 throw InvalidParameterException("Invalid pickled data");
300 }
301
302 data.clear();
303 for(unsigned int i=0; i<list.size(); i+=2) {
304 data.push_back(Pair(list[i], list[i+1]));
305 }
306
307 update();
308}

References data, InvalidParameterException, and update().

Referenced by EMAN::SetIsoPowProcessor::process_inplace().

◆ set_x()

void EMAN::XYData::set_x ( size_t  i,
float  x 
)
inline

Definition at line 89 of file xydata.h.

90 {
91 if (i>=data.size()) data.resize(i+1,Pair(0,0));
92 data[i].x = x;
93 }

References data, and x.

Referenced by EMAN::FSCFourierProcessor::process().

◆ set_xy_list()

void XYData::set_xy_list ( const vector< float > &  xlist,
const vector< float > &  ylist 
)

Definition at line 269 of file xydata.cpp.

270{
271 if(xlist.size() != ylist.size()) {
272 throw InvalidParameterException("xlist and ylist size does not match!");
273 }
274
275 for(unsigned int i=0; i<xlist.size(); ++i) {
276 data.push_back(Pair(xlist[i], ylist[i]));
277 }
278}

References data, and InvalidParameterException.

◆ set_y()

void EMAN::XYData::set_y ( size_t  i,
float  y 
)
inline

Definition at line 101 of file xydata.h.

102 {
103 if (i>=data.size()) data.resize(i+1,Pair(0,0));
104 data[i].y = y;
105 }

References data, and y.

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

◆ update()

void XYData::update ( )

Definition at line 53 of file xydata.cpp.

54{
55 if (data.size()==0) return;
56
57 std::sort(data.begin(), data.end());
58
59 ymin = FLT_MAX;
60 ymax = -FLT_MAX;
61
62 typedef vector < Pair >::const_iterator Ptype;
63 for (Ptype p = data.begin(); p != data.end(); p++) {
64 if (p->y > ymax) {
65 ymax = p->y;
66 }
67 if (p->y < ymin) {
68 ymin = p->y;
69 }
70 }
71
72 size_t n = data.size();
73 mean_x_spacing = (data[n - 1].x - data[0].x) / (float) n;
74}

References data, mean_x_spacing, ymax, and ymin.

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

◆ write_file()

int XYData::write_file ( const string &  filename) const

Definition at line 109 of file xydata.cpp.

110{
111 FILE *out = fopen(filename.c_str(), "wb");
112 if (!out) {
113 LOGERR("cannot open xydata file '%s' to write", filename.c_str());
114 return 1;
115 }
116
117 typedef vector < Pair >::const_iterator Ptype;
118 for (Ptype p = data.begin(); p != data.end(); p++) {
119 fprintf(out, "%1.6g\t%1.6g\n", p->x, p->y);
120 }
121
122 fclose(out);
123 out = 0;
124
125 return 0;
126}

References data, and LOGERR.

Member Data Documentation

◆ data

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

◆ mean_x_spacing

float EMAN::XYData::mean_x_spacing
private

Definition at line 159 of file xydata.h.

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

◆ ymax

float EMAN::XYData::ymax
private

Definition at line 158 of file xydata.h.

Referenced by get_maxy(), and update().

◆ ymin

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: