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

Description

Utility functions for writing Python code snippets.

Functions

std::string indent (size_t width=4u)
 Returns a string of blanks with given width. By default the width equals standard offset in python files. More...
 
bool isHexagonal (double length1, double length2, double angle)
 
bool isSquare (double length1, double length2, double angle)
 
std::string printArguments (const std::vector< std::pair< double, std::string >> &arguments)
 Takes pairs of value/unit and concatenates them for an argument list. Each pair's content will be processed by printValue(), so the meaning of the content is the same as in printValue(). More...
 
std::string printArguments (const std::vector< std::pair< std::variant< double, int >, std::string >> &arguments)
 Convenience overload for printing arguments containing a mixture of int and double. More...
 
std::string printBool (double value)
 
std::string printDegrees (double input)
 
std::string printDouble (double input)
 
std::string printFunction (const std::string &name, const std::vector< std::pair< double, std::string >> &arguments)
 Print a function in the form "<name>(<arguments>)". arguments will be processed by printArguments(), see there for details. More...
 
std::string printFunction (const std::string &name, double value, const std::string &unit)
 Convenience overload for printing a function with one argument. More...
 
std::string printFunction (const std::string &name, double value1, const std::string &unit1, double value2, const std::string &unit2)
 Convenience overload for printing a function with two arguments. More...
 
std::string printImportedSymbols (const std::string &code)
 
std::string printInt (int value)
 
std::string printKvector (const R3 value)
 
std::string printLightDouble (double input)
 prints double as an integer, if possible within standard accuracy More...
 
std::string printNm (double input)
 
std::string printNm2 (double input)
 
std::string printRealLimits (const RealLimits &limits, const std::string &units)
 
std::string printRealLimitsArg (const RealLimits &limits, const std::string &units)
 Prints RealLimits in the form of argument (in the context of ParameterDistribution and similar). Default RealLimits will not be printed, any other will be printed as ", ba.RealLimits.limited(1*deg, 2*deg)". More...
 
std::string printScientificDouble (double input)
 
std::string printString (const std::string &value)
 
std::string printValue (double value, const std::string &units)
 
std::string printValue (std::variant< double, int > value, const std::string &units)
 

Function Documentation

◆ indent()

◆ isHexagonal()

bool Py::Fmt::isHexagonal ( double  length1,
double  length2,
double  angle 
)

Definition at line 190 of file PyFmt.cpp.

191 {
192  return length1 == length2 && BaseUtils::algo::almostEqual(angle, M_TWOPI / 3.0);
193 }
#define M_TWOPI
Definition: Constants.h:54
bool almostEqual(double a, double b)
Returns true if two doubles agree within machine epsilon.
Definition: Algorithms.h:33

References BaseUtils::algo::almostEqual(), and M_TWOPI.

Here is the call graph for this function:

◆ isSquare()

bool Py::Fmt::isSquare ( double  length1,
double  length2,
double  angle 
)

Definition at line 185 of file PyFmt.cpp.

186 {
187  return length1 == length2 && BaseUtils::algo::almostEqual(angle, M_PI_2);
188 }
#define M_PI_2
Definition: Constants.h:45

References BaseUtils::algo::almostEqual(), and M_PI_2.

Here is the call graph for this function:

◆ printArguments() [1/2]

std::string Py::Fmt::printArguments ( const std::vector< std::pair< double, std::string >> &  arguments)

Takes pairs of value/unit and concatenates them for an argument list. Each pair's content will be processed by printValue(), so the meaning of the content is the same as in printValue().

Definition at line 151 of file PyFmt.cpp.

152 {
153  std::vector<std::string> args;
154  for (const auto& argument : arguments)
155  args.push_back(Py::Fmt::printValue(argument.first, argument.second));
156  return BaseUtils::String::join(args, ", ");
157 }
std::string join(const std::vector< std::string > &joinable, const std::string &joint)
Returns string obtain by joining vector elements.
Definition: StringUtils.cpp:45
std::string printValue(double value, const std::string &units)
Definition: PyFmt.cpp:123

References BaseUtils::String::join(), and printValue().

Referenced by printFunction().

Here is the call graph for this function:

◆ printArguments() [2/2]

std::string Py::Fmt::printArguments ( const std::vector< std::pair< std::variant< double, int >, std::string >> &  arguments)

Convenience overload for printing arguments containing a mixture of int and double.

Definition at line 160 of file PyFmt.cpp.

161 {
162  std::vector<std::string> args;
163  for (const auto& argument : arguments)
164  args.push_back(Py::Fmt::printValue(argument.first, argument.second));
165  return BaseUtils::String::join(args, ", ");
166 }

References BaseUtils::String::join(), and printValue().

Here is the call graph for this function:

◆ printBool()

std::string Py::Fmt::printBool ( double  value)

Definition at line 41 of file PyFmt.cpp.

42 {
43  return value ? "True" : "False";
44 }

Referenced by Py::Fmt2::representShape2D().

◆ printDegrees()

std::string Py::Fmt::printDegrees ( double  input)

Definition at line 116 of file PyFmt.cpp.

117 {
118  std::ostringstream inter;
119  inter << printLightDouble(Units::rad2deg(input)) << "*deg";
120  return inter.str();
121 }
std::string printLightDouble(double input)
prints double as an integer, if possible within standard accuracy
Definition: PyFmt.cpp:61
double rad2deg(double angle)
Definition: Units.h:54

References printLightDouble(), and Units::rad2deg().

Referenced by SampleToPython::defineInterferences(), SampleToPython::defineLattices2D(), printValue(), and Py::Fmt2::representShape2D().

Here is the call graph for this function:

◆ printDouble()

std::string Py::Fmt::printDouble ( double  input)

Definition at line 46 of file PyFmt.cpp.

47 {
48  std::ostringstream inter;
49  inter << std::setprecision(12);
50  if (std::abs(input) < std::numeric_limits<double>::epsilon()) {
51  inter << "0.0";
52  return inter.str();
53  }
54  inter << input;
55  if (inter.str().find('e') == std::string::npos && inter.str().find('.') == std::string::npos)
56  inter << ".0";
57  return inter.str();
58 }

Referenced by SampleToPython::defineInterferences(), SampleToPython::defineMaterials(), SampleToPython::defineParticleLayouts(), printKvector(), Py::Fmt2::printParameterDistribution(), Py::Fmt2::printRangedDistribution(), and printValue().

◆ printFunction() [1/3]

std::string Py::Fmt::printFunction ( const std::string &  name,
const std::vector< std::pair< double, std::string >> &  arguments 
)

Print a function in the form "<name>(<arguments>)". arguments will be processed by printArguments(), see there for details.

Definition at line 168 of file PyFmt.cpp.

170 {
171  return name + "(" + printArguments(arguments) + ")";
172 }
std::string printArguments(const std::vector< std::pair< std::variant< double, int >, std::string >> &arguments)
Convenience overload for printing arguments containing a mixture of int and double.
Definition: PyFmt.cpp:160

References printArguments().

Referenced by printFunction(), IProfile1D::pythonConstructor(), IProfile2D::pythonConstructor(), LayerRoughness::pythonConstructor(), IFormFactor::pythonConstructor(), Profile1DVoigt::pythonConstructor(), Profile2DVoigt::pythonConstructor(), DistributionGate::pythonConstructor(), DistributionLorentz::pythonConstructor(), DistributionGaussian::pythonConstructor(), DistributionLogNormal::pythonConstructor(), DistributionCosine::pythonConstructor(), and DistributionTrapezoid::pythonConstructor().

Here is the call graph for this function:

◆ printFunction() [2/3]

std::string Py::Fmt::printFunction ( const std::string &  name,
double  value,
const std::string &  unit 
)

Convenience overload for printing a function with one argument.

Definition at line 174 of file PyFmt.cpp.

175 {
176  return printFunction(name, {{value, unit}});
177 }
std::string printFunction(const std::string &name, double value1, const std::string &unit1, double value2, const std::string &unit2)
Convenience overload for printing a function with two arguments.
Definition: PyFmt.cpp:179

References printFunction().

Here is the call graph for this function:

◆ printFunction() [3/3]

std::string Py::Fmt::printFunction ( const std::string &  name,
double  value1,
const std::string &  unit1,
double  value2,
const std::string &  unit2 
)

Convenience overload for printing a function with two arguments.

Definition at line 179 of file PyFmt.cpp.

181 {
182  return printFunction(name, {{value1, unit1}, {value2, unit2}});
183 }

References printFunction().

Here is the call graph for this function:

◆ printImportedSymbols()

std::string Py::Fmt::printImportedSymbols ( const std::string &  code)

Definition at line 24 of file PyFmt.cpp.

25 {
26  std::vector<std::string> to_declare;
27  for (const std::string key : {"angstrom", "deg", "nm", "nm2", "micrometer"})
28  if (code.find("*" + key) != std::string::npos)
29  to_declare.push_back(key);
30  for (const std::string key : {"R3"})
31  if (code.find(key) != std::string::npos)
32  to_declare.push_back(key);
33  return "from bornagain import " + BaseUtils::String::join(to_declare, ", ") + "\n";
34 }

References BaseUtils::String::join().

Here is the call graph for this function:

◆ printInt()

std::string Py::Fmt::printInt ( int  value)

Definition at line 36 of file PyFmt.cpp.

37 {
38  return std::to_string(value);
39 }

Referenced by printValue().

◆ printKvector()

std::string Py::Fmt::printKvector ( const R3  value)

Definition at line 195 of file PyFmt.cpp.

196 {
197  std::ostringstream result;
198  result << "R3(" << printDouble(value.x()) << ", " << printDouble(value.y()) << ", "
199  << printDouble(value.z()) << ")";
200  return result.str();
201 }
std::string printDouble(double input)
Definition: PyFmt.cpp:46

References printDouble().

Here is the call graph for this function:

◆ printLightDouble()

std::string Py::Fmt::printLightDouble ( double  input)

prints double as an integer, if possible within standard accuracy

Definition at line 61 of file PyFmt.cpp.

62 {
63  std::ostringstream inter;
64  int ival = std::lround(input);
65  if (std::abs(input - ival) < 1e-11)
66  inter << ival;
67  else {
68  inter << std::setprecision(12);
69  if (std::abs(input) < std::numeric_limits<double>::epsilon())
70  return "0.0";
71  inter << input;
72  if (inter.str().find('e') == std::string::npos
73  && inter.str().find('.') == std::string::npos)
74  inter << ".0";
75  }
76  return inter.str();
77 }

Referenced by printDegrees(), printNm(), and printNm2().

◆ printNm()

std::string Py::Fmt::printNm ( double  input)

Definition at line 79 of file PyFmt.cpp.

80 {
81  std::ostringstream inter;
82  inter << std::setprecision(12);
83  inter << printLightDouble(input) << "*nm";
84  return inter.str();
85 }

References printLightDouble().

Referenced by SampleToPython::defineInterferences(), SampleToPython::defineLattices2D(), SampleToPython::defineLattices3D(), SampleToPython::defineLayers(), and printValue().

Here is the call graph for this function:

◆ printNm2()

std::string Py::Fmt::printNm2 ( double  input)

Definition at line 87 of file PyFmt.cpp.

88 {
89  std::ostringstream inter;
90  inter << std::setprecision(12);
91  inter << printLightDouble(input) << "*nm2";
92  return inter.str();
93 }

References printLightDouble().

Referenced by SampleToPython::defineInterferences().

Here is the call graph for this function:

◆ printRealLimits()

std::string Py::Fmt::printRealLimits ( const RealLimits limits,
const std::string &  units 
)

Definition at line 22 of file PyFmtLimits.cpp.

23 {
24  std::ostringstream result;
25 
26  if (limits.isLimitless())
27  result << "RealLimits()";
28 
29  else if (limits.isPositive())
30  result << "RealLimits.positive()";
31 
32 
33  else if (limits.isNonnegative())
34  result << "RealLimits.nonnegative()";
35 
36 
37  else if (limits.isLowerLimited()) {
38  result << "RealLimits.lowerLimited(" << Py::Fmt::printValue(limits.lowerLimit(), units)
39  << ")";
40  }
41 
42  else if (limits.isUpperLimited()) {
43  result << "RealLimits.upperLimited(" << Py::Fmt::printValue(limits.upperLimit(), units)
44  << ")";
45  }
46 
47  else if (limits.isLimited()) {
48  result << "RealLimits.limited(" << Py::Fmt::printValue(limits.lowerLimit(), units) << ", "
49  << Py::Fmt::printValue(limits.upperLimit(), units) << ")";
50  }
51 
52  return result.str();
53 }
bool isLimited() const
Definition: RealLimits.cpp:218
bool isLowerLimited() const
Definition: RealLimits.cpp:208
bool isPositive() const
Definition: RealLimits.cpp:197
double upperLimit() const
Returns upper limit.
Definition: RealLimits.cpp:71
double lowerLimit() const
Returns lower limit.
Definition: RealLimits.cpp:49
bool isNonnegative() const
Definition: RealLimits.cpp:203
bool isLimitless() const
Definition: RealLimits.cpp:192
bool isUpperLimited() const
Definition: RealLimits.cpp:213

References RealLimits::isLimited(), RealLimits::isLimitless(), RealLimits::isLowerLimited(), RealLimits::isNonnegative(), RealLimits::isPositive(), RealLimits::isUpperLimited(), RealLimits::lowerLimit(), printValue(), and RealLimits::upperLimit().

Referenced by printRealLimitsArg().

Here is the call graph for this function:

◆ printRealLimitsArg()

std::string Py::Fmt::printRealLimitsArg ( const RealLimits limits,
const std::string &  units 
)

Prints RealLimits in the form of argument (in the context of ParameterDistribution and similar). Default RealLimits will not be printed, any other will be printed as ", ba.RealLimits.limited(1*deg, 2*deg)".

Definition at line 59 of file PyFmtLimits.cpp.

60 {
61  return limits.isLimitless() ? "" : ", ba." + printRealLimits(limits, units);
62 }
std::string printRealLimits(const RealLimits &limits, const std::string &units)
Definition: PyFmtLimits.cpp:22

References RealLimits::isLimitless(), and printRealLimits().

Referenced by Py::Fmt2::printParameterDistribution(), and Py::Fmt2::printRangedDistribution().

Here is the call graph for this function:

◆ printScientificDouble()

std::string Py::Fmt::printScientificDouble ( double  input)

Definition at line 96 of file PyFmt.cpp.

97 {
98  std::ostringstream inter;
99  inter << std::scientific;
100  inter << input;
101 
102  std::string::size_type pos = inter.str().find('e');
103  if (pos == std::string::npos)
104  return inter.str();
105 
106  std::string part1 = inter.str().substr(0, pos);
107  std::string part2 = inter.str().substr(pos, std::string::npos);
108 
109  part1.erase(part1.find_last_not_of('0') + 1, std::string::npos);
110  if (part1.back() == '.')
111  part1 += "0";
112 
113  return part1 + part2;
114 }
std::string scientific(T value, int n=10)
Returns scientific string representing given value of any numeric type.
Definition: StringUtils.h:75

References BaseUtils::String::scientific().

Referenced by SampleToPython::defineMultiLayers().

Here is the call graph for this function:

◆ printString()

std::string Py::Fmt::printString ( const std::string &  value)

Definition at line 144 of file PyFmt.cpp.

145 {
146  std::ostringstream result;
147  result << "\"" << value << "\"";
148  return result.str();
149 }

Referenced by Py::Fmt2::printAxis().

◆ printValue() [1/2]

std::string Py::Fmt::printValue ( double  value,
const std::string &  units 
)

Definition at line 123 of file PyFmt.cpp.

124 {
125  if (units == "rad")
126  return printDegrees(value);
127  if (units == "nm")
128  return printNm(value);
129  if (units.empty())
130  return printDouble(value);
131  ASSERT(0);
132 }
#define ASSERT(condition)
Definition: Assert.h:45
std::string printDegrees(double input)
Definition: PyFmt.cpp:116
std::string printNm(double input)
Definition: PyFmt.cpp:79

References ASSERT, printDegrees(), printDouble(), and printNm().

Referenced by printArguments(), Py::Fmt2::printAxis(), printRealLimits(), and printValue().

Here is the call graph for this function:

◆ printValue() [2/2]

std::string Py::Fmt::printValue ( std::variant< double, int >  value,
const std::string &  units 
)

Definition at line 134 of file PyFmt.cpp.

135 {
136  if (std::holds_alternative<int>(value)) {
137  ASSERT(units.empty()); // int with units is not supported. Implement when necessary.
138  return printInt(std::get<int>(value));
139  }
140 
141  return printValue(std::get<double>(value), units);
142 }
std::string printValue(std::variant< double, int > value, const std::string &units)
Definition: PyFmt.cpp:134
std::string printInt(int value)
Definition: PyFmt.cpp:36

References ASSERT, printInt(), and printValue().

Here is the call graph for this function: