Bsoft  2.1.0
Bernard's software package
ps_model.cpp
Go to the documentation of this file.
1 
9 #include "ps_model.h"
10 #include "ps_plot.h"
11 #include "ps_views.h"
12 #include "model_views.h"
13 #include "model_compare.h"
14 #include "model_util.h"
15 #include "linked_list.h"
16 #include "utilities.h"
17 
18 // Declaration of global variables
19 extern int verbose; // Level of output to the screen
20 
28 int ps_model_views(Bstring& filename, Bmodel* model, int combined)
29 {
30  int i(1), left(50), bottom(200), width(500), height(250), nm(1);
31  double x, y;
32  Euler euler;
33 
34  Bmodel* mp;
35  Bcomponent* comp = model->comp;
36  if ( !comp ) return -1;
37 
38  if ( !combined )
39  for ( nm=0, mp = model; mp; mp = mp->next ) nm++;
40 
41  Bstring title("Model views");
42 
43  ofstream* fps = ps_open_and_init(filename, title, nm, 600, 800);
44 
45  if ( combined ) {
46  *fps << "%%Page: " << i << " " << i << endl;
47  *fps << "/Helvetica findfont 12 scalefont setfont" << endl;
48  *fps << "50 755 moveto (" << title << ": " << model->id << ") show" << endl;
49  *fps << "/Data [" << endl << "%x y fom sel" << endl;
50  for ( mp = model; mp; mp = mp->next ) {
51  for ( comp = mp->comp; comp; comp = comp->next ) if ( comp->sel ) {
52  euler = Euler(comp->view);
53  x = euler.phi()*fabs(sin(euler.theta())) + M_PI;
54  y = euler.theta();
55  *fps << setprecision(4) << x << " " << y << " " << comp->fom << " " << comp->sel << endl;
56  }
57  }
58  *fps << "] def" << endl;
59  ps_phi_theta_plot(fps, left, bottom, width, height, 4);
60  *fps << "showpage" << endl;
61  } else {
62  for ( i=1, mp = model; mp; mp = mp->next, i++ ) {
63  *fps << "%%Page: " << i << " " << i << endl;
64  *fps << "/Helvetica findfont 12 scalefont setfont" << endl;
65  *fps << "50 755 moveto (" << title << ": " << mp->id << ") show" << endl;
66  *fps << "/Data [" << endl << "%x y fom sel" << endl;
67  for ( comp = mp->comp; comp; comp = comp->next ) if ( comp->sel ) {
68  euler = Euler(comp->view);
69  x = euler.phi()*fabs(sin(euler.theta())) + M_PI;
70  y = euler.theta();
71  *fps << setprecision(4) << x << " " << y << " " << comp->fom << " " << comp->sel << endl;
72  }
73  *fps << "] def" << endl;
74  ps_phi_theta_plot(fps, left, bottom, width, height, 4);
75  *fps << "showpage" << endl;
76  }
77  }
78 
79  ps_close(fps);
80 
81  return 0;
82 }
83 
92 int ps_model_symmetry_views(Bstring& filename, Bmodel* model, Bstring& symmetry_string, int combined)
93 {
94  int i(1), nm(1);
95  Bmodel* mp;
96 
97  if ( !model->comp ) return -1;
98 
99  if ( !combined )
100  for ( nm=0, mp = model; mp; mp = mp->next ) nm++;
101 
102  Bstring title("Model views");
103 
104  ofstream* fps = ps_open_and_init(filename, title, nm, 600, 800);
105 
106  View* view;
107 
108  if ( combined ) {
109  *fps << "%%Page: " << i << " " << i << endl;
110  *fps << "/Helvetica findfont 12 scalefont setfont" << endl;
111  *fps << "50 755 moveto (" << title << ": " << model->id << ") show" << endl;
112  *fps << "/Data [" << endl << "%x y fom sel" << endl;
113  view = views_from_models(model);
114  ps_views(fps, symmetry_string, view, 2);
115  kill_list((char *) view, sizeof(View));
116  *fps << "showpage" << endl;
117  } else {
118  for ( i=1, mp = model; mp; mp = mp->next, i++ ) {
119  *fps << "%%Page: " << i << " " << i << endl;
120  *fps << "/Helvetica findfont 12 scalefont setfont" << endl;
121  *fps << "50 755 moveto (" << title << ": " << mp->id << ") show" << endl;
122  *fps << "/Data [" << endl << "%x y fom sel" << endl;
123  view = views_from_model(model);
124  ps_views(fps, symmetry_string, view, 2);
125  kill_list((char *) view, sizeof(View));
126  *fps << "showpage" << endl;
127  }
128  }
129 
130  ps_close(fps);
131 
132  return 0;
133 }
134 
142 {
143  if ( !model ) return 0;
144 
145  int nmod, i, j, nh(100), max(0);
146  Bmodel* mp;
147  Bcomponent* comp;
148 
149  for ( nmod=0, mp = model; mp; mp = mp->next ) if ( mp->sel ) nmod++;
150 
151  if ( verbose & VERB_PROCESS ) {
152  if ( nmod < 2 )
153  cout << "Generating file " << filename << " with a FOM histogram" << endl << endl;
154  else
155  cout << "Generating file " << filename << " with " << nmod << " FOM histograms" << endl << endl;
156  }
157 
158  Bstring title("FOM histogram");
159  Bplot* plot = new Bplot(nmod, nh+1, nmod+1);
160  plot->title(title);
161 
162  for ( i=0; i<=nh; i++ ) (*plot)[i] = i*1.0/nh;
163 
164  for ( j=0, mp = model; mp; mp = mp->next ) if ( mp->sel ) {
165  j += plot->rows();
166  for ( comp = mp->comp; comp; comp = comp->next ) if ( comp->sel ) {
167  i = (long) (comp->fom*nh + 0.5);
168  if ( i < 0 ) i = 0;
169  if ( i > nh ) i = nh;
170  (*plot)[j+i] += 1;
171  }
172  }
173 
174  for ( i=plot->rows(); i<plot->rows()*plot->columns(); i++ )
175  if ( max < (*plot)[i] ) max = (int) (*plot)[i];
176  max = 10*(max/10 + 1);
177 
178  for ( i=0; i<nmod; i++ ) {
179  plot->page(i).title(title);
180  plot->page(i).columns(2);
181  plot->page(i).column(0).number(0);
182  plot->page(i).column(1).number(i+1);
183  plot->page(i).column(0).label("FOM");
184  plot->page(i).column(1).label("Count");
185  plot->page(i).column(0).axis(1);
186  plot->page(i).column(1).axis(3);
187  plot->page(i).column(1).type(1);
188  plot->page(i).column(1).element_size(0.5);
189  plot->page(i).axis(1).max(1);
190  plot->page(i).axis(3).max(max);
191  plot->page(i).axis(1).label("FOM");
192  plot->page(i).axis(3).label("Count");
193  }
194 
195  ps_plot(filename, plot);
196 
197  delete plot;
198 
199  return 0;
200 }
201 
220 int ps_model_occupancy(Bmodel* model, double cutoff, int bins, int nfit,
221  vector<double>& distrib, vector<double>& prob, double R, Bstring& filename)
222 {
223  if ( filename.length() < 1 ) return -1;
224 
225  Bmodel* mp;
226  Bcomponent* comp;
227 
228  long ncomp = model_maxnum_components(model);
229 
230  long i, j;
231  long pages(3);
232  long width(600);
233  long height(800);
234  long npoint(ncomp+1);
235  long nfunc(3+nfit);
236 // double x_min(0), x_max(ncomp);
237 // double y_min(0), y_max(0);
238  double y_max(0);
239  double hmin(1e6), hmax(-1e6);
240  double scale, shift;
241  Bstring title("Occupancy Analysis");
242  Bstring label, txt;
243 
244  // Page 1
245  if ( verbose & VERB_DEBUG )
246  cout << "DEBUG ps_model_occupancy: first page " << endl;
247 
248  Bplot* plot = new Bplot(1, npoint, nfunc+1);
249  plot->title(title);
250 
251  title = filename + ": Occupancy";
252 
253  plot->page(0).title(title);
254  plot->page(0).columns(nfunc+1);
255  for ( i=0; i<=nfunc; i++ ) plot->page(0).column(i).number(i);
256  plot->page(0).column(0).label("Occupancy");
257  plot->page(0).column(0).axis(1);
258  plot->page(0).column(1).label("Count");
259  plot->page(0).column(1).axis(3);
260  plot->page(0).column(1).type(1);
261  plot->page(0).column(2).label("StDev");
262  plot->page(0).column(2).axis(3);
263  plot->page(0).column(2).type(3);
264  for ( i=3; i<=nfunc; i++ ) {
265  label = Bstring(i-3, "Fit%d");
266  plot->page(0).column(i).type(2);
267  plot->page(0).column(i).label(label);
268  plot->page(0).column(i).axis(3);
269  plot->page(0).column(i).element_size(0.5);
270  }
271  plot->page(0).column(3).label("FitSum");
272  plot->page(0).column(4).color(1,0,0);
273  if ( nfit > 1 ) plot->page(0).column(5).color(0,1,0);
274  if ( nfit > 2 ) plot->page(0).column(6).color(0,0,1);
275  if ( nfit > 3 ) plot->page(0).column(7).color(1,1,0);
276 
277  plot->page(0).axis(1).min(0);
278  plot->page(0).axis(1).max(ncomp);
279  plot->page(0).axis(1).label("Occupancy");
280  plot->page(0).axis(3).min(0);
281  plot->page(0).axis(3).max(1);
282  plot->page(0).axis(3).label("Models (fraction)");
283 
284  if ( cutoff > 0 ) {
285  txt = Bstring(cutoff, "Coverage cutoff: %g");
286  plot->page(0).add_text(txt);
287  }
288 
289  if ( nfit > 1 ) {
290  txt = "Weights: ";
291  for ( i=0; i<nfit; i++ ) txt += Bstring(prob[i], " %f");
292  plot->page(0).add_text(txt);
293  txt = "Probability: ";
294  for ( i=0; i<nfit; i++ ) txt += Bstring(prob[i+nfit], " %f");
295  plot->page(0).add_text(txt);
296  }
297 
298  txt = Bstring(R, "R = %g");
299  plot->page(0).add_text(txt);
300 
301  for ( i=0; i<npoint; i++) (*plot)[i] = i;
302  for ( i=0, j=npoint; i<npoint*nfunc; i++, j++ ) (*plot)[j] = distrib[i];
303 
304  ofstream* fps = ps_open_and_init(filename, title, pages, width, height);
305 
306  ps_graph(fps, plot, 1);
307 
308  delete plot;
309 
310  // Page 2
311  if ( verbose & VERB_DEBUG )
312  cout << "DEBUG ps_model_occupancy: second page " << endl;
313 
314  for ( hmin=1e6, hmax=-1e6, mp = model; mp; mp = mp->next ) {
315  for ( comp = mp->comp; comp; comp = comp->next ) {
316  if ( comp->density > hmax ) hmax = comp->density;
317  if ( comp->density < hmin ) hmin = comp->density;
318  }
319  }
320 
321  if ( hmin == hmax ) {
322  cerr << "Warning: The component densities vary only from " << hmin << " to " << hmax << endl;
323  hmax = hmin + 1;
324  }
325 
326  scale = (bins - 1)/(hmax - hmin);
327 
328  plot = new Bplot(1, bins, 2);
329 
330  title = filename + ": Average density";
331 
332  plot->page(0).title(title);
333  plot->page(0).columns(2);
334  for ( i=0; i<2; i++ ) plot->page(0).column(i).number(i);
335  plot->page(0).column(0).label("Density");
336  plot->page(0).column(0).axis(1);
337  plot->page(0).column(1).label("Components");
338  plot->page(0).column(1).axis(3);
339  plot->page(0).column(1).type(1);
340  plot->page(0).column(1).element_size(0.5);
341 
342  if ( verbose & VERB_DEBUG )
343  cout << "DEBUG ps_model_occupancy: bins=" << bins << " scale=" << scale << endl;
344 
345  for ( mp = model; mp; mp = mp->next ) {
346  for ( comp = mp->comp; comp; comp = comp->next ) {
347  i = (long) (scale*(comp->density - hmin) + 0.5);
348  if ( i < bins ) (*plot)[i+bins] += 1;
349  }
350  }
351 
352  for ( i=0, j=bins, y_max = 0; i<bins; i++, j++ ) {
353  (*plot)[i] = i/scale + hmin;
354  if ( y_max < (*plot)[j] ) y_max = (*plot)[j];
355  }
356  y_max *= 1.05;
357 
358  plot->page(0).axis(1).min(hmin);
359  plot->page(0).axis(1).max(hmax);
360  plot->page(0).axis(1).label("Density");
361  plot->page(0).axis(3).min(0);
362  plot->page(0).axis(3).max(y_max);
363  plot->page(0).axis(3).label("Components");
364 
365  ps_graph(fps, plot, 1);
366 
367  delete plot;
368 
369  // Page 3
370  if ( verbose & VERB_DEBUG )
371  cout << "DEBUG ps_model_occupancy: third page " << endl;
372 
373  for ( hmin=1e6, hmax=-1e6, mp = model; mp; mp = mp->next ) {
374  for ( comp = mp->comp; comp; comp = comp->next, i++ ) {
375  shift = comp->loc.distance(comp->vec);
376  if ( shift > hmax ) hmax = shift;
377  if ( shift < hmin ) hmin = shift;
378  }
379  }
380 
381  scale = (bins - 1)/(hmax - hmin);
382 
383  plot = new Bplot(1, bins, 2);
384 
385  title = filename + ": Shifts";
386 
387  plot->page(0).title(title);
388  plot->page(0).columns(2);
389  for ( i=0; i<2; i++ ) plot->page(0).column(i).number(i);
390  plot->page(0).column(0).label("Density");
391  plot->page(0).column(0).axis(1);
392  plot->page(0).column(1).label("Components");
393  plot->page(0).column(1).axis(3);
394  plot->page(0).column(1).type(1);
395  plot->page(0).column(1).element_size(0.5);
396 
397  for ( mp = model; mp; mp = mp->next ) {
398  for ( comp = mp->comp; comp; comp = comp->next ) {
399  if ( comp->sel ) { // count shifts of occupied locations only
400  shift = comp->loc.distance(comp->vec);
401  i = (long) (scale*(shift - hmin) + 0.5);
402  if ( i < bins ) (*plot)[i+bins] += 1;
403  }
404  }
405  }
406 
407  for ( i=0, j=bins, y_max = 0; i<bins; i++, j++ ) {
408  (*plot)[i] = i/scale + hmin;
409  if ( y_max < (*plot)[j] ) y_max = (*plot)[j];
410  }
411  y_max *= 1.05;
412 
413  plot->page(0).axis(1).min(hmin);
414  plot->page(0).axis(1).max(hmax);
415  plot->page(0).axis(1).label("Shift (A)");
416  plot->page(0).axis(3).min(0);
417  plot->page(0).axis(3).max(y_max);
418  plot->page(0).axis(3).label("Components");
419 
420  ps_graph(fps, plot, 1);
421 
422  delete plot;
423 
424  ps_close(fps);
425 
426  return 0;
427 }
428 
Bcomponent::next
Bcomponent * next
Definition: rwmodel.h:52
ps_model_symmetry_views
int ps_model_symmetry_views(Bstring &filename, Bmodel *model, Bstring &symmetry_string, int combined)
Postscript plot of the distribution of model views.
Definition: ps_model.cpp:92
Bcomponent
Definition: rwmodel.h:51
ps_plot
int ps_plot(Bstring filename, Bplot *plot)
Generates postscript graphs.
Definition: ps_plot.cpp:129
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
kill_list
long kill_list(char *list, unsigned long size)
Frees all the items in a linked list.
Definition: linked_list.cpp:214
Bcomponent::loc
Vector3< float > loc
Definition: rwmodel.h:58
Bstring
Definition: Bstring.h:29
Bplot::rows
void rows(long n)
Definition: Bplot.h:212
Euler::theta
double theta()
Definition: Euler.h:57
Bcolumn::color
double * color()
Definition: Bplot.h:76
Bcolumn::element_size
double element_size()
Definition: Bplot.h:74
Bmodel::next
Bmodel * next
Definition: rwmodel.h:120
Bplot
Definition: Bplot.h:146
View
Definition: View.h:26
Bcolumn::type
long type()
Definition: Bplot.h:66
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
Bcolumn::axis
long axis()
Definition: Bplot.h:68
model_compare.h
Functions to compare models and components.
verbose
int verbose
ps_plot.h
Header file for postscript output functions.
Bpage::axis
Baxis & axis(long id)
Definition: Bplot.h:117
Vector3::distance
double distance(const Vector3 &v)
Definition: Vector3.h:227
Bpage::title
Bstring & title()
Definition: Bplot.h:100
Bmodel::comp
Bcomponent * comp
Definition: rwmodel.h:132
ps_model_views
int ps_model_views(Bstring &filename, Bmodel *model, int combined)
Postscript plot of the distribution of model views.
Definition: ps_model.cpp:28
Bcomponent::vec
Vector3< float > vec
Definition: rwmodel.h:60
ps_close
int ps_close(ofstream *fps)
Closes a postscript file.
Definition: ps_plot.cpp:105
views_from_model
View * views_from_model(Bmodel *model)
Returns a list of component views.
Definition: model_views.cpp:29
ps_open_and_init
ofstream * ps_open_and_init(Bstring filename, Bstring title, int npages, int width, int height)
Opens and initializes a postscript file.
Definition: ps_plot.cpp:32
Bmodel::id
Bstring id
Definition: rwmodel.h:121
model_maxnum_components
long model_maxnum_components(Bmodel *model)
Calculates the largest number of components in a model.
Definition: model_compare.cpp:43
Bpage::add_text
void add_text(Bstring &s)
Definition: Bplot.h:122
ps_phi_theta_plot
int ps_phi_theta_plot(ofstream *fps, int left, int bottom, int width, int height, int ncol)
Generates postscript plot of projected phi and theta.
Definition: ps_views.cpp:334
Bpage::column
Bcolumn & column(long i)
Definition: Bplot.h:110
Bmodel::sel
int sel
Definition: rwmodel.h:129
Bplot::title
Bstring & title()
Definition: Bplot.h:194
Baxis::min
double min()
Definition: Bplot.h:30
Bmodel
Definition: rwmodel.h:119
ps_views
int ps_views(Bstring &filename, Bstring &symmetry_string, View *view, int flags)
Generates postscript plot of views on the unit sphere.
Definition: ps_views.cpp:34
Euler::phi
double phi()
Definition: Euler.h:58
ps_graph
int ps_graph(ofstream *fps, Bplot *plot, int page_number)
Generates postscript graphs.
Definition: ps_plot.cpp:247
Baxis::label
Bstring & label()
Definition: Bplot.h:28
M_PI
#define M_PI
Definition: utilities.h:63
utilities.h
Header file for general utilities.
Bstring::length
long length() const
Definition: Bstring.cpp:263
Bcomponent::view
View view
Definition: rwmodel.h:59
Bcomponent::fom
float fom
Definition: rwmodel.h:65
model_views.h
Library routines used for analysing model component views.
model
Bmodel * model
Definition: bshow.cpp:34
linked_list.h
Header file for generalized linked list functions.
views_from_models
View * views_from_models(Bmodel *model)
Returns a list of component views.
Definition: model_views.cpp:53
Bplot::columns
void columns(long n)
Definition: Bplot.h:214
ps_model_fom_histogram
int ps_model_fom_histogram(Bstring &filename, Bmodel *model)
Postscript plot of the model component FOM histogram.
Definition: ps_model.cpp:141
model_util.h
Library routines used for model processing.
Bcolumn::label
Bstring & label()
Definition: Bplot.h:63
Bplot::page
Bpage & page(long i)
Definition: Bplot.h:216
ps_model_occupancy
int ps_model_occupancy(Bmodel *model, double cutoff, int bins, int nfit, vector< double > &distrib, vector< double > &prob, double R, Bstring &filename)
Generates a postscript plot for a model occupancy analysis.
Definition: ps_model.cpp:220
Bcolumn::number
long number()
Definition: Bplot.h:61
ps_model.h
Header file for postscript tools dealing with models.
Baxis::max
double max()
Definition: Bplot.h:32
Bcomponent::density
float density
Definition: rwmodel.h:64
Euler
Definition: Euler.h:28
ps_views.h
Header file for postscript tools dealing with views.
Bpage::columns
void columns(long ncol)
Definition: Bplot.h:105
Bcomponent::sel
int sel
Definition: rwmodel.h:66