14 #include "msdevstudio/MSconfig.h"
18 #include <boost/python.hpp>
33 using namespace boost::python;
41 class_ < PyFunctionRep >
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"
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" ) )
56 .def ( init < FunctionBase *, PyDataRep * >
59 .def ( init < FunctionBase * >
62 .def (
"addTo", &PyFunctionRep::addTo,
63 "addTo ( Display ) -> None\n"
65 "Adds the Function to a Display. taking the Display's selected\n"
66 "DataRep as its target." )
68 .def (
"parmNames", &PyFunctionRep::parmNames,
69 return_value_policy < copy_const_reference > (),
70 "parmNames ( None ) -> list\n"
72 "Returns a list of parameter names." )
74 .def (
"parameters", &PyFunctionRep::parameters,
75 return_value_policy < copy_const_reference > (),
76 "parameters ( None ) -> list\n"
78 "Returns a list of the function's parameter values." )
80 .def (
"principleErrors", &PyFunctionRep::principleErrors,
81 return_value_policy < copy_const_reference > (),
82 "principleErrors ( None ) -> list\n"
84 "Returns the errors on the parameters." )
86 .def (
"errors", &PyFunctionRep::principleErrors,
87 return_value_policy < copy_const_reference > (),
88 "errors ( None ) - > list\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." )
94 .def (
"fit", &PyFunctionRep::fitFunction,
95 "fit ( None ) -> boolean\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." )
102 .def (
"setParameters", &PyFunctionRep::setParameters,
103 "setParameters ( list ) -> None\n"
105 "Sets the function's parameter values." )
107 .def (
"valueAt", &PyFunctionRep::operator(),
108 "valueAt ( x ) -> value\n"
110 "Returns the function's value at given coordinate." )
112 .def (
"chiSquared", &PyFunctionRep::objectiveValue,
113 "chiSquare ( None ) -> value\n"
115 "Returns the Chi-Squared." )
117 .def (
"objectiveValue", &PyFunctionRep::objectiveValue,
118 "objectiveValue ( None ) -> value\n"
120 "Returns the objective Value that the fitter minimizes.\n"
121 "Typically it is the Chi-Squared." )
123 .def (
"degreesOfFreedom", &PyFunctionRep::degreesOfFreedom,
124 "degressOfFreedom ( None ) -> value\n"
126 "Returns the number of degrees of freedom a fitter would have." )
133 .def (
"setFixedFlags", &PyFunctionRep::setFixedFlags,
134 "setFixedFlags ( list ) -> None\n"
136 "Set which parameters should be held fixed during fitting." )
138 .def (
"setFitter", &PyFunctionRep::setFitter,
139 "setFitter ( string ) -> None\n"
141 "Sets the fitter by name from fitter factory." )
143 .def (
"getFitterName", &PyFunctionRep::getFitterName,
144 return_value_policy < copy_const_reference > (),
145 "getFitterName ( None ) -> string\n"
147 "Returns the current fitter name." )
149 .def (
"createResidualsDisplay",
150 &PyFunctionRep::createResidualsDisplay,
151 return_value_policy < manage_new_object > (),
152 "createResidualsDisplay ( None ) -> Display\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." )
158 .def (
"setFitRange",
159 &PyFunctionRep::setFitRange,
160 "setFitRange ( low, high ) -> None\n"
162 "Sets the range of the coordinate axis that is used for fitting." )
164 .def (
"setFitRangeEnabled",
165 &PyFunctionRep::setFitRangeEnabled,
166 "setFitRange ( boolean ) -> None\n"
168 "Enabled use of the fit range" )
176 using namespace hippodraw;
178 PyFunctionRep::PyFunctionRep (
const std::string & name,
PyDataRep * rep )
183 DataRep * datarep = rep -> getDataRep ();
184 m_rep = controller -> createFunctionRep ( name, datarep );
199 DataRep * datarep = rep -> getDataRep ();
200 m_rep = controller -> createFunctionRep (
function, datarep );
216 m_rep = controller -> createFunctionRep (
function, 0 );
235 m_target = display->
display ();
239 m_target -> setActivePlot ( -1,
true );
241 catch (
const std::runtime_error & e ) {
248 const vector < std::string > & PyFunctionRep::parmNames ()
const
251 const vector < std::string > &
vec = m_rep->parmNames();
258 const vector < double > & PyFunctionRep::parameters ()
const
261 const vector < double > &
vec = m_rep->parameters ();
267 const vector < double > & PyFunctionRep::principleErrors ()
const
270 const vector < double > &
vec = m_rep -> principleErrors();
276 void PyFunctionRep::setParameters (
const std::vector<double> & params )
279 m_rep->setParameters(params);
283 bool PyFunctionRep::fitFunction ()
287 bool ok = controller -> fitFunction ( m_target, m_rep );
295 operator () (
double x )
304 function = m_rep->getFunction();
307 return function ->operator() ( x );
323 const DataRep * datarep = m_target -> getDataRep ( 0 );
332 const vector < vector < double > > &
338 const vector < vector < double > > & covariance
339 = controller -> getCovarianceMatrix ( m_target );
362 setFixedFlags (
const std::vector < int > & flags )
366 m_rep->setFixedFlags ( flags );
373 setFitter (
const std::string & name )
376 controller -> setFitter ( m_rep, name );
381 getFitterName ( )
const
383 return m_rep -> getFitterName ();
388 createResidualsDisplay ()
const
392 = controller -> createResidualsDisplay ( m_target, m_rep );
400 setFitRange (
double low,
double high )
402 const Range range ( low, high );
404 m_rep -> setCutRange ( range );
410 setFitRangeEnabled (
bool yes )
412 m_rep -> setCutRange ( yes );