PyFunctionRep.cxx
Go to the documentation of this file.
1 
12 // for dll interface warning
13 #ifdef _MSC_VER
14 #include "msdevstudio/MSconfig.h"
15 #endif
16 
17 // include first to avoid _POSIX_C_SOURCE warning.
18 #include <boost/python.hpp>
19 
20 #include "PyFunctionRep.h"
21 
22 #include "PyApp.h"
23 #include "PyDataRep.h"
24 #include "QtDisplay.h"
25 
27 #include "datareps/FunctionRep.h"
28 #include "functions/FunctionBase.h"
30 #include "plotters/PlotterBase.h"
31 
32 using std::vector;
33 using namespace boost::python;
34 
35 namespace hippodraw {
36 namespace Python {
37 
38 void
40 {
41  class_ < PyFunctionRep >
42  ( "Function",
43  "This class wraps a FunctionBase object with a DataRep. This allows\n"
44  "it to be drawn in a Display. It also provides interface to member\n"
45  "functions of FunctionBase, although the user could obtain a\n"
46  "reference to the FunctionBase itself to do so.",
47  init < const std::string &, PyDataRep * >
48  ( "Function ( string, DataRep ) -> Function\n"
49  "Function ( FunctionBase, DataRep ) -> Function\n"
50  "\n"
51  "The first form creates a function using the string to find the\n"
52  "FunctionBase in the FunctionFactory. The second form creates a\n"
53  "using an existing FunctionBase object. Both forms use the DataRep\n"
54  "as target for fitting and drawing\n" ) )
55 
56  .def ( init < FunctionBase *, PyDataRep * >
57  ( ) )
58 
59  .def ( init < FunctionBase * >
60  ( ) )
61 
62  .def ( "addTo", &PyFunctionRep::addTo,
63  "addTo ( Display ) -> None\n"
64  "\n"
65  "Adds the Function to a Display. taking the Display's selected\n"
66  "DataRep as its target." )
67 
68  .def ( "parmNames", &PyFunctionRep::parmNames,
69  return_value_policy < copy_const_reference > (),
70  "parmNames ( None ) -> list\n"
71  "\n"
72  "Returns a list of parameter names." )
73 
74  .def ( "parameters", &PyFunctionRep::parameters,
75  return_value_policy < copy_const_reference > (),
76  "parameters ( None ) -> list\n"
77  "\n"
78  "Returns a list of the function's parameter values." )
79 
80  .def ( "principleErrors", &PyFunctionRep::principleErrors,
81  return_value_policy < copy_const_reference > (),
82  "principleErrors ( None ) -> list\n"
83  "\n"
84  "Returns the errors on the parameters." )
85 
86  .def ( "errors", &PyFunctionRep::principleErrors,
87  return_value_policy < copy_const_reference > (),
88  "errors ( None ) - > list\n"
89  "\n"
90  "Returns the errors on the parameters. The errors are calculated\n"
91  "by a fitter, thus the values returned are only valid after\n"
92  "having done a fit." )
93 
94  .def ( "fit", &PyFunctionRep::fitFunction,
95  "fit ( None ) -> boolean\n"
96  "\n"
97  "Attempts to fit the the function to the target DataRep.\n"
98  "Uses the currently selected fitter, unless one was explicitly\n"
99  "set. Note the fit is always done to linear sum if more than one\n"
100  "function is on the data." )
101 
102  .def ( "setParameters", &PyFunctionRep::setParameters,
103  "setParameters ( list ) -> None\n"
104  "\n"
105  "Sets the function's parameter values." )
106 
107  .def ( "valueAt", &PyFunctionRep::operator(),
108  "valueAt ( x ) -> value\n"
109  "\n"
110  "Returns the function's value at given coordinate." )
111 
112  .def ( "chiSquared", &PyFunctionRep::objectiveValue,
113  "chiSquare ( None ) -> value\n"
114  "\n"
115  "Returns the Chi-Squared." )
116 
117  .def ( "objectiveValue", &PyFunctionRep::objectiveValue,
118  "objectiveValue ( None ) -> value\n"
119  "\n"
120  "Returns the objective Value that the fitter minimizes.\n"
121  "Typically it is the Chi-Squared." )
122 
123  .def ( "degreesOfFreedom", &PyFunctionRep::degreesOfFreedom,
124  "degressOfFreedom ( None ) -> value\n"
125  "\n"
126  "Returns the number of degrees of freedom a fitter would have." )
127 
128 // .def ( "covarianceMatrix",
129 // &PyFunctionRep::covarianceMatrix,
130 // return_value_policy < copy_const_reference > (),
131 // "Returns the covariance matrix" )
132 
133  .def ( "setFixedFlags", &PyFunctionRep::setFixedFlags,
134  "setFixedFlags ( list ) -> None\n"
135  "\n"
136  "Set which parameters should be held fixed during fitting." )
137 
138  .def ( "setFitter", &PyFunctionRep::setFitter,
139  "setFitter ( string ) -> None\n"
140  "\n"
141  "Sets the fitter by name from fitter factory." )
142 
143  .def ( "getFitterName", &PyFunctionRep::getFitterName,
144  return_value_policy < copy_const_reference > (),
145  "getFitterName ( None ) -> string\n"
146  "\n"
147  "Returns the current fitter name." )
148 
149  .def ( "createResidualsDisplay",
150  &PyFunctionRep::createResidualsDisplay,
151  return_value_policy < manage_new_object > (),
152  "createResidualsDisplay ( None ) -> Display\n"
153  "\n"
154  "Returns residuals Display object. The residuals display is an\n"
155  "XY plot showing the difference between the function values and\n"
156  "the target DataRep values." )
157 
158  .def ( "setFitRange",
159  &PyFunctionRep::setFitRange,
160  "setFitRange ( low, high ) -> None\n"
161  "\n"
162  "Sets the range of the coordinate axis that is used for fitting." )
163 
164  .def ( "setFitRangeEnabled",
165  &PyFunctionRep::setFitRangeEnabled,
166  "setFitRange ( boolean ) -> None\n"
167  "\n"
168  "Enabled use of the fit range" )
169 
170  ;
171 }
172 
173 } // namespace Python
174 } // namespace hippodraw
175 
176 using namespace hippodraw;
177 
178 PyFunctionRep::PyFunctionRep ( const std::string & name, PyDataRep * rep )
179 {
180  PyApp::lock ();
181  try {
182  FunctionController * controller = FunctionController::instance ();
183  DataRep * datarep = rep -> getDataRep ();
184  m_rep = controller -> createFunctionRep ( name, datarep );
185  m_target = 0;
186  }
187  catch ( const FactoryException & e ) {
188  PyApp::unlock ();
189  throw e;
190  }
191  PyApp::unlock ();
192 }
193 
194 PyFunctionRep::PyFunctionRep ( FunctionBase * function, PyDataRep * rep )
195 {
196  PyApp::lock ();
197  try {
198  FunctionController * controller = FunctionController::instance ();
199  DataRep * datarep = rep -> getDataRep ();
200  m_rep = controller -> createFunctionRep ( function, datarep );
201  m_target = 0;
202  }
203  catch ( const FactoryException & e ) {
204  PyApp::unlock ();
205  throw e;
206  }
207  PyApp::unlock ();
208 }
209 
210 PyFunctionRep::
211 PyFunctionRep ( FunctionBase * function )
212 {
213  PyApp::lock ();
214  try {
215  FunctionController * controller = FunctionController::instance ();
216  m_rep = controller -> createFunctionRep ( function, 0 );
217  m_target = 0;
218  }
219  catch ( const FactoryException & e ) {
220  PyApp::unlock ();
221  throw e;
222  }
223  PyApp::unlock ();
224 }
225 
226 DataRep * PyFunctionRep::getRep () const
227 {
228  return m_rep;
229 }
230 
231 void PyFunctionRep::addTo ( QtDisplay * display )
232 {
233  PyApp::lock();
234  FunctionController * controller = FunctionController::instance ();
235  m_target = display->display ();
236 
237  try {
238  controller->addFunction ( m_target, m_rep );
239  m_target -> setActivePlot ( -1, true );
240  }
241  catch ( const std::runtime_error & e ) {
242  PyApp::unlock ();
243  throw e;
244  }
245  PyApp::unlock ();
246 }
247 
248 const vector < std::string > & PyFunctionRep::parmNames () const
249 {
250  PyApp::lock();
251  const vector < std::string > & vec = m_rep->parmNames();
252  PyApp::unlock ();
253 
254  return vec;
255 }
256 
257 
258 const vector < double > & PyFunctionRep::parameters () const
259 {
260  PyApp::lock();
261  const vector < double > & vec = m_rep->parameters ();
262  PyApp::unlock ();
263 
264  return vec;
265 }
266 
267 const vector < double > & PyFunctionRep::principleErrors () const
268 {
269  PyApp::lock();
270  const vector < double > & vec = m_rep -> principleErrors();
271  PyApp::unlock ();
272 
273  return vec;
274 }
275 
276 void PyFunctionRep::setParameters ( const std::vector<double> & params )
277 {
278  PyApp::lock();
279  m_rep->setParameters(params);
280  PyApp::unlock ();
281 }
282 
283 bool PyFunctionRep::fitFunction ()
284 {
285  PyApp::lock();
286  FunctionController * controller = FunctionController::instance ();
287  bool ok = controller -> fitFunction ( m_target, m_rep );
288  PyApp::unlock ();
289 
290  return ok;
291 }
292 
293 double
294 PyFunctionRep::
295 operator () ( double x )
296 {
297  FunctionController * controller = FunctionController::instance ();
298  FunctionRep * rep = controller->getComposite ( m_target, m_rep );
299  FunctionBase * function = 0;
300  if ( rep != 0 ) {
301  function = rep->getFunction ();
302  }
303  else {
304  function = m_rep->getFunction();
305  }
306 
307  return function ->operator() ( x );
308  }
309 
315 double
316 PyFunctionRep::
317 objectiveValue()
318 {
319  PyApp::lock();
320  FunctionController * controller = FunctionController::instance ();
321  double value;
322  if (m_target) {
323  const DataRep * datarep = m_target -> getDataRep ( 0 );
324  value = controller->getObjectiveValue( m_target, datarep );
325  } else {
326  value = 0;
327  }
328  PyApp::unlock ();
329 
330  return value;
331 }
332 const vector < vector < double > > &
333 PyFunctionRep::
334 covarianceMatrix ( )
335 {
336  PyApp::lock();
337  FunctionController * controller = FunctionController::instance ();
338  const vector < vector < double > > & covariance
339  = controller -> getCovarianceMatrix ( m_target );
340  PyApp::unlock ();
341 
342  return covariance;
343 }
344 
345 int
346 PyFunctionRep::
347 degreesOfFreedom ()
348 {
349  PyApp::lock();
350  FunctionController * controller = FunctionController::instance ();
351  int value = 0;
352  if (m_target) {
353  value = controller->getDegreesOfFreedom(m_target);
354  }
355  PyApp::unlock ();
356 
357  return value;
358 }
359 
360 void
361 PyFunctionRep::
362 setFixedFlags ( const std::vector < int > & flags )
363 {
364  PyApp::lock();
365 
366  m_rep->setFixedFlags ( flags );
367 
368  PyApp::unlock ();
369 }
370 
371 void
372 PyFunctionRep::
373 setFitter ( const std::string & name )
374 {
375  FunctionController * controller = FunctionController::instance ();
376  controller -> setFitter ( m_rep, name );
377 }
378 
379 const std::string &
380 PyFunctionRep::
381 getFitterName ( ) const
382 {
383  return m_rep -> getFitterName ();
384 }
385 
386 QtDisplay *
387 PyFunctionRep::
388 createResidualsDisplay () const
389 {
390  FunctionController * controller = FunctionController::instance();
391  PlotterBase * plotter
392  = controller -> createResidualsDisplay ( m_target, m_rep );
393  QtDisplay * display = new QtDisplay ( plotter );
394 
395  return display;
396 }
397 
398 void
399 PyFunctionRep::
400 setFitRange ( double low, double high )
401 {
402  const Range range ( low, high );
403 
404  m_rep -> setCutRange ( range );
405 }
406 
408 void
409 PyFunctionRep::
410 setFitRangeEnabled ( bool yes )
411 {
412  m_rep -> setCutRange ( yes );
413 }

Generated for HippoDraw Class Library by doxygen