BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
Py::Fmt2 Namespace Reference

Description

Utility functions for writing Python code snippets.

Functions

std::string printAxis (const IAxis *axis, const std::string &unit)
 Prints an axis. More...
 
std::string printParameterDistribution (const ParameterDistribution &par_distr, const std::string &distVarName, const std::string &units)
 
std::string printRangedDistribution (const IRangedDistribution &distr)
 
std::string representShape2D (const std::string &indent, const IShape2D *ishape, bool mask_value, std::function< std::string(double)> printValueFunc)
 Returns fixed Python code snippet that defines the function "simulate". More...
 

Function Documentation

◆ printAxis()

std::string Py::Fmt2::printAxis ( const IAxis axis,
const std::string &  unit 
)

Prints an axis.

Definition at line 93 of file PyFmt2.cpp.

94 {
95  std::ostringstream result;
96  if (const auto* a = dynamic_cast<const FixedBinAxis*>(axis); a)
97  result << "ba.FixedBinAxis(" << Py::Fmt::printString(a->axisName()) << ", " << a->size()
98  << ", " << Py::Fmt::printValue(a->min(), unit) << ", "
99  << Py::Fmt::printValue(a->max(), unit) << ")";
100  else if (const auto* a = dynamic_cast<const PointwiseAxis*>(axis); a) {
101  result << "numpy.asarray([";
102  const std::vector<double>& points = a->binCenters();
103  for (auto iter = points.begin(); iter != points.end() - 1; ++iter)
104  result << Py::Fmt::printValue(*iter, unit) << ",";
105  result << Py::Fmt::printValue(points.back(), unit) << "])\n";
106  } else
107  throw std::runtime_error("printAxis not implemented for current axis type");
108  return result.str();
109 }
Axis with fixed bin size.
Definition: FixedBinAxis.h:23
Axis containing arbitrary (non-equidistant) coordinate values. Lower boundary of the first bin and up...
Definition: PointwiseAxis.h:33
std::string printString(const std::string &value)
Definition: PyFmt.cpp:144
std::string printValue(double value, const std::string &units)
Definition: PyFmt.cpp:123

References Py::Fmt::printString(), and Py::Fmt::printValue().

Here is the call graph for this function:

◆ printParameterDistribution()

std::string Py::Fmt2::printParameterDistribution ( const ParameterDistribution par_distr,
const std::string &  distVarName,
const std::string &  units 
)

Definition at line 111 of file PyFmt2.cpp.

113 {
114  std::ostringstream result;
115 
116  result << "ba.ParameterDistribution(ba." << par_distr.whichParameterAsPyEnum() << ", "
117  << distVarName << ", " << par_distr.nDraws() << ", "
118  << Py::Fmt::printDouble(par_distr.sigmaFactor())
119  << Py::Fmt::printRealLimitsArg(par_distr.getLimits(), units) << ")";
120 
121  return result.str();
122 }
std::string whichParameterAsPyEnum() const
double sigmaFactor() const
get the sigma factor
size_t nDraws() const
get number of samples for this distribution
RealLimits getLimits() const
std::string printDouble(double input)
Definition: PyFmt.cpp:46
std::string printRealLimitsArg(const RealLimits &limits, const std::string &units)
Prints RealLimits in the form of argument (in the context of ParameterDistribution and similar)....
Definition: PyFmtLimits.cpp:59

References ParameterDistribution::getLimits(), ParameterDistribution::nDraws(), Py::Fmt::printDouble(), Py::Fmt::printRealLimitsArg(), ParameterDistribution::sigmaFactor(), and ParameterDistribution::whichParameterAsPyEnum().

Here is the call graph for this function:

◆ printRangedDistribution()

std::string Py::Fmt2::printRangedDistribution ( const IRangedDistribution distr)

Definition at line 124 of file PyFmt2.cpp.

125 {
126  std::ostringstream result;
127  result << Py::Fmt::indent() << "distribution = ba.";
128  result << distr.name();
129  result << "(" << distr.nSamples() << ", " << Py::Fmt::printDouble(distr.sigmaFactor());
130  if (!distr.limits().isLimitless())
131  result << Py::Fmt::printRealLimitsArg(distr.limits());
132  result << ")";
133  return result.str();
134 }
double sigmaFactor() const
Returns sigma factor to use during sampling.
virtual std::string name() const =0
Returns distribution name for python-formatted text.
RealLimits limits() const
Returns current limits of the distribution.
size_t nSamples() const
Returns number of samples to generate.
bool isLimitless() const
Definition: RealLimits.cpp:192
std::string indent(size_t width)
Returns a string of blanks with given width. By default the width equals standard offset in python fi...
Definition: PyFmt.cpp:203

References Py::Fmt::indent(), RealLimits::isLimitless(), IRangedDistribution::limits(), IRangedDistribution::name(), IRangedDistribution::nSamples(), Py::Fmt::printDouble(), Py::Fmt::printRealLimitsArg(), and IRangedDistribution::sigmaFactor().

Here is the call graph for this function:

◆ representShape2D()

std::string Py::Fmt2::representShape2D ( const std::string &  indent,
const IShape2D ishape,
bool  mask_value,
std::function< std::string(double)>  printValueFunc 
)

Returns fixed Python code snippet that defines the function "simulate".

Definition at line 38 of file PyFmt2.cpp.

40 {
41  std::ostringstream result;
42  result << std::setprecision(12);
43 
44  if (const auto* shape = dynamic_cast<const Polygon*>(ishape)) {
45  std::vector<double> xpos, ypos;
46  shape->getPoints(xpos, ypos);
47  result << indent << "points = [";
48  for (size_t i = 0; i < xpos.size(); ++i) {
49  result << "[" << printValueFunc(xpos[i]) << ", " << printValueFunc(ypos[i]) << "]";
50  if (i != xpos.size() - 1)
51  result << ", ";
52  }
53  result << "]\n";
54  result << indent << "simulation.addMask("
55  << "ba.Polygon(points), " << Py::Fmt::printBool(mask_value) << ")\n";
56  } else if (dynamic_cast<const InfinitePlane*>(ishape))
57  result << indent << "simulation.maskAll()\n";
58  else if (const auto* shape = dynamic_cast<const Ellipse*>(ishape)) {
59  result << indent << "simulation.addMask(";
60  result << "ba.Ellipse(" << printValueFunc(shape->getCenterX()) << ", "
61  << printValueFunc(shape->getCenterY()) << ", " << printValueFunc(shape->radiusX())
62  << ", " << printValueFunc(shape->radiusY());
63  if (shape->getTheta() != 0.0)
64  result << ", " << Py::Fmt::printDegrees(shape->getTheta());
65  result << "), " << Py::Fmt::printBool(mask_value) << ")\n";
66  }
67 
68  else if (const auto* shape = dynamic_cast<const Rectangle*>(ishape)) {
69  result << indent << "simulation.addMask(";
70  result << "ba.Rectangle(" << printValueFunc(shape->getXlow()) << ", "
71  << printValueFunc(shape->getYlow()) << ", " << printValueFunc(shape->getXup())
72  << ", " << printValueFunc(shape->getYup()) << "), " << Py::Fmt::printBool(mask_value)
73  << ")\n";
74  }
75 
76  else if (const auto* shape = dynamic_cast<const VerticalLine*>(ishape)) {
77  result << indent << "simulation.addMask(";
78  result << "ba.VerticalLine(" << printValueFunc(shape->getXpos()) << "), "
79  << Py::Fmt::printBool(mask_value) << ")\n";
80  }
81 
82  else if (const auto* shape = dynamic_cast<const HorizontalLine*>(ishape)) {
83  result << indent << "simulation.addMask(";
84  result << "ba.HorizontalLine(" << printValueFunc(shape->getYpos()) << "), "
85  << Py::Fmt::printBool(mask_value) << ")\n";
86  } else
87  throw std::runtime_error("representShape2D(const IShape2D*) -> Error. Unknown shape");
88 
89  return result.str();
90 }
An ellipse, for use in detector masks.
Definition: Ellipse.h:23
An infinite horizontal line.
Definition: Line.h:55
The infinite plane is used for masking the entire detector.
Definition: InfinitePlane.h:28
A polygon, for use in detector masks.
Definition: Polygon.h:30
A rectangle, for use in detector masks.
Definition: Rectangle.h:25
An infinite vertical line.
Definition: Line.h:38
std::string printDegrees(double input)
Definition: PyFmt.cpp:116
std::string printBool(double value)
Definition: PyFmt.cpp:41

References Py::Fmt::indent(), Py::Fmt::printBool(), and Py::Fmt::printDegrees().

Here is the call graph for this function: