From 829d32027cf881267b0ea044c4c00546c4e5eafd Mon Sep 17 00:00:00 2001 From: "Joachim Wuttke (h)" <j.wuttke@fz-juelich.de> Date: Sat, 8 Aug 2020 21:56:29 +0200 Subject: [PATCH] split PythonFormatting -> pyfmt, pyfmt2 --- Core/Beam/FootprintGauss.cpp | 8 +- Core/Beam/FootprintSquare.cpp | 8 +- Core/Computation/LayoutStrategyBuilder.h | 6 +- Core/Computation/MultiLayerFuncs.h | 6 +- .../Computation/ParticleLayoutComputation.cpp | 2 +- Core/Computation/ProcessedLayout.cpp | 4 +- Core/Computation/ProcessedSample.cpp | 6 +- Core/Computation/ProcessedSample.h | 2 +- .../RoughMultiLayerComputation.cpp | 2 +- Core/Computation/SpecularComputation.cpp | 2 +- Core/Computation/SpecularStrategyBuilder.h | 6 +- Core/Detector/DetectorFunctions.cpp | 2 +- Core/Detector/IDetector.cpp | 2 +- Core/Detector/IDetector.h | 2 +- Core/Detector/ScanResolution.cpp | 8 +- Core/Detector/SimulationArea.h | 6 +- Core/Detector/SimulationAreaIterator.h | 6 +- Core/Export/SampleToPython.cpp | 111 ++++++----- Core/Export/SimulationToPython.cpp | 158 ++++++++------- Core/Fitting/FitObjective.cpp | 2 +- Core/Instrument/AngularSpecScan.cpp | 22 +-- Core/Instrument/QSpecScan.cpp | 10 +- Core/Intensity/ArrayUtils.h | 6 +- Core/Intensity/AxisNames.h | 6 +- Core/Intensity/CumulativeValue.h | 6 +- Core/Intensity/IntensityDataFunctions.cpp | 2 +- Core/Intensity/UnitConverter1D.cpp | 2 +- Core/Multilayer/IFresnelMap.h | 2 +- Core/Multilayer/ISpecularStrategy.h | 2 +- Core/Multilayer/KzComputation.cpp | 2 +- Core/Multilayer/MultiLayer.cpp | 2 +- Core/Multilayer/MultiLayerUtils.h | 6 +- Core/Multilayer/ScalarFresnelMap.cpp | 2 +- Core/Multilayer/Slice.h | 6 +- Core/Multilayer/SlicedFormFactorList.h | 6 +- .../SpecularMagneticOldStrategy.cpp | 2 +- Core/Multilayer/SpecularMagneticStrategy.cpp | 2 +- Core/Multilayer/SpecularScalarStrategy.cpp | 2 +- Core/Parametrization/IterationStrategy.cpp | 2 +- Core/Parametrization/RangedDistributions.cpp | 7 +- Core/PyIO/PythonFormatting.cpp | 184 +++--------------- Core/PyIO/PythonFormatting.h | 31 +-- Core/Simulation/Simulation.cpp | 2 +- Core/Tools/PyFmt.cpp | 156 +++++++++++++++ Core/Tools/PyFmt.h | 51 +++++ GUI/coregui/Models/GUIDomainSampleVisitor.cpp | 2 +- .../IntensityDataWidgets/PlotEventInfo.cpp | 4 +- .../SaveProjectionsAssistant.cpp | 6 +- Tests/Functional/Python/PyEmbedded/Tests.cpp | 8 +- .../Core/Detector/SpecularDetector1DTest.cpp | 2 +- .../Core/Detector/SphericalDetectorTest.cpp | 2 +- .../ExportToPython/PythonFormattingTest.cpp | 76 ++++---- .../UnitTests/Core/Sample/MultiLayerTest.cpp | 2 +- .../Core/Sample/MultilayerAveragingTest.cpp | 2 +- 54 files changed, 516 insertions(+), 458 deletions(-) create mode 100644 Core/Tools/PyFmt.cpp create mode 100644 Core/Tools/PyFmt.h diff --git a/Core/Beam/FootprintGauss.cpp b/Core/Beam/FootprintGauss.cpp index 9a866d698f6..2f91175b13f 100644 --- a/Core/Beam/FootprintGauss.cpp +++ b/Core/Beam/FootprintGauss.cpp @@ -14,7 +14,7 @@ #include "Core/Beam/FootprintGauss.h" #include "Core/Basics/MathConstants.h" -#include "Core/PyIO/PythonFormatting.h" +#include "Core/Tools/PyFmt.h" #include "Core/Tools/MathFunctions.h" FootprintGauss::FootprintGauss(double width_ratio) : IFootprintFactor(width_ratio) @@ -40,10 +40,10 @@ double FootprintGauss::calculate(double alpha) const std::string FootprintGauss::print() const { std::stringstream result; - result << "\n" << PythonFormatting::indent() << "# Defining footprint:\n"; - result << PythonFormatting::indent() << "footprint = "; + result << "\n" << pyfmt::indent() << "# Defining footprint:\n"; + result << pyfmt::indent() << "footprint = "; result << "ba.FootprintGauss"; - result << "(" << PythonFormatting::printDouble(widthRatio()) << ")"; + result << "(" << pyfmt::printDouble(widthRatio()) << ")"; return result.str(); } diff --git a/Core/Beam/FootprintSquare.cpp b/Core/Beam/FootprintSquare.cpp index 6e05954cacd..e5fc4cd7ad3 100644 --- a/Core/Beam/FootprintSquare.cpp +++ b/Core/Beam/FootprintSquare.cpp @@ -14,7 +14,7 @@ #include "Core/Beam/FootprintSquare.h" #include "Core/Basics/MathConstants.h" -#include "Core/PyIO/PythonFormatting.h" +#include "Core/Tools/PyFmt.h" #include <algorithm> #include <stdexcept> @@ -41,10 +41,10 @@ double FootprintSquare::calculate(double alpha) const std::string FootprintSquare::print() const { std::stringstream result; - result << "\n" << PythonFormatting::indent() << "# Defining footprint:\n"; - result << PythonFormatting::indent() << "footprint = "; + result << "\n" << pyfmt::indent() << "# Defining footprint:\n"; + result << pyfmt::indent() << "footprint = "; result << "ba.FootprintSquare"; - result << "(" << PythonFormatting::printDouble(widthRatio()) << ")"; + result << "(" << pyfmt::printDouble(widthRatio()) << ")"; return result.str(); } diff --git a/Core/Computation/LayoutStrategyBuilder.h b/Core/Computation/LayoutStrategyBuilder.h index c81d514d688..4d9b93d3590 100644 --- a/Core/Computation/LayoutStrategyBuilder.h +++ b/Core/Computation/LayoutStrategyBuilder.h @@ -12,8 +12,8 @@ // // ************************************************************************** // -#ifndef BORNAGAIN_CORE_MULTILAYER_LAYOUTSTRATEGYBUILDER_H -#define BORNAGAIN_CORE_MULTILAYER_LAYOUTSTRATEGYBUILDER_H +#ifndef BORNAGAIN_CORE_COMPUTATION_LAYOUTSTRATEGYBUILDER_H +#define BORNAGAIN_CORE_COMPUTATION_LAYOUTSTRATEGYBUILDER_H #include "Core/Parametrization/SimulationOptions.h" #include <memory> @@ -45,4 +45,4 @@ private: std::unique_ptr<IInterferenceFunctionStrategy> mP_strategy; }; -#endif // BORNAGAIN_CORE_MULTILAYER_LAYOUTSTRATEGYBUILDER_H +#endif // BORNAGAIN_CORE_COMPUTATION_LAYOUTSTRATEGYBUILDER_H diff --git a/Core/Computation/MultiLayerFuncs.h b/Core/Computation/MultiLayerFuncs.h index 354428b961e..3c387f5dd36 100644 --- a/Core/Computation/MultiLayerFuncs.h +++ b/Core/Computation/MultiLayerFuncs.h @@ -12,8 +12,8 @@ // // ************************************************************************** // -#ifndef BORNAGAIN_CORE_MULTILAYER_MULTILAYERFUNCS_H -#define BORNAGAIN_CORE_MULTILAYER_MULTILAYERFUNCS_H +#ifndef BORNAGAIN_CORE_COMPUTATION_MULTILAYERFUNCS_H +#define BORNAGAIN_CORE_COMPUTATION_MULTILAYERFUNCS_H #include "Core/Basics/Complex.h" #include "Wrap/WinDllMacros.h" @@ -34,4 +34,4 @@ BA_CORE_API_ std::pair<double, double> DefaultMaterialProfileLimits(const MultiL //! Generate z values (equidistant) for use in MaterialProfile BA_CORE_API_ std::vector<double> GenerateZValues(int n_points, double z_min, double z_max); -#endif // BORNAGAIN_CORE_MULTILAYER_MULTILAYERFUNCS_H +#endif // BORNAGAIN_CORE_COMPUTATION_MULTILAYERFUNCS_H diff --git a/Core/Computation/ParticleLayoutComputation.cpp b/Core/Computation/ParticleLayoutComputation.cpp index b5ccadeef7d..0e7f3b7b65b 100644 --- a/Core/Computation/ParticleLayoutComputation.cpp +++ b/Core/Computation/ParticleLayoutComputation.cpp @@ -13,9 +13,9 @@ // ************************************************************************** // #include "Core/Computation/ParticleLayoutComputation.h" +#include "Core/Computation/LayoutStrategyBuilder.h" #include "Core/Computation/ProcessedLayout.h" #include "Core/Multilayer/IInterferenceFunctionStrategy.h" -#include "Core/Computation/LayoutStrategyBuilder.h" #include "Core/SimulationElement/SimulationElement.h" ParticleLayoutComputation::ParticleLayoutComputation(const ProcessedLayout* p_layout, diff --git a/Core/Computation/ProcessedLayout.cpp b/Core/Computation/ProcessedLayout.cpp index 627b56806df..923e6664b2d 100644 --- a/Core/Computation/ProcessedLayout.cpp +++ b/Core/Computation/ProcessedLayout.cpp @@ -15,13 +15,13 @@ #include "Core/Computation/ProcessedLayout.h" #include "Core/Aggregate/IInterferenceFunction.h" #include "Core/Aggregate/ILayout.h" -#include "Core/Multilayer/Slice.h" #include "Core/Multilayer/FormFactorBAPol.h" #include "Core/Multilayer/FormFactorCoherentSum.h" #include "Core/Multilayer/FormFactorDWBA.h" #include "Core/Multilayer/FormFactorDWBAPol.h" -#include "Core/Particle/IParticle.h" +#include "Core/Multilayer/Slice.h" #include "Core/Multilayer/SlicedFormFactorList.h" +#include "Core/Particle/IParticle.h" namespace { diff --git a/Core/Computation/ProcessedSample.cpp b/Core/Computation/ProcessedSample.cpp index a1649db53f5..b4b148545e2 100644 --- a/Core/Computation/ProcessedSample.cpp +++ b/Core/Computation/ProcessedSample.cpp @@ -13,15 +13,15 @@ // ************************************************************************** // #include "Core/Computation/ProcessedSample.h" -#include "Core/Multilayer/MultiLayerUtils.h" #include "Core/Computation/ProcessedLayout.h" -#include "Core/Multilayer/Slice.h" +#include "Core/Computation/SpecularStrategyBuilder.h" #include "Core/Multilayer/Layer.h" #include "Core/Multilayer/LayerRoughness.h" #include "Core/Multilayer/MatrixFresnelMap.h" #include "Core/Multilayer/MultiLayer.h" +#include "Core/Multilayer/MultiLayerUtils.h" #include "Core/Multilayer/ScalarFresnelMap.h" -#include "Core/Computation/SpecularStrategyBuilder.h" +#include "Core/Multilayer/Slice.h" #include "Core/Parametrization/SimulationOptions.h" #include "Core/Particle/HomogeneousRegion.h" diff --git a/Core/Computation/ProcessedSample.h b/Core/Computation/ProcessedSample.h index 1aafab3cd8d..7e2fe152504 100644 --- a/Core/Computation/ProcessedSample.h +++ b/Core/Computation/ProcessedSample.h @@ -15,8 +15,8 @@ #ifndef BORNAGAIN_CORE_COMPUTATION_PROCESSEDSAMPLE_H #define BORNAGAIN_CORE_COMPUTATION_PROCESSEDSAMPLE_H -#include "Core/Multilayer/Slice.h" #include "Core/Material/Material.h" +#include "Core/Multilayer/Slice.h" #include "Core/Vector/Vectors3D.h" #include <map> #include <memory> diff --git a/Core/Computation/RoughMultiLayerComputation.cpp b/Core/Computation/RoughMultiLayerComputation.cpp index 8d942537e28..9ca589dd1b0 100644 --- a/Core/Computation/RoughMultiLayerComputation.cpp +++ b/Core/Computation/RoughMultiLayerComputation.cpp @@ -15,13 +15,13 @@ #include "Core/Computation/RoughMultiLayerComputation.h" #include "Core/Basics/MathConstants.h" #include "Core/Computation/ProcessedSample.h" -#include "Core/Multilayer/Slice.h" #include "Core/Multilayer/IFresnelMap.h" #include "Core/Multilayer/ILayerRTCoefficients.h" #include "Core/Multilayer/Layer.h" #include "Core/Multilayer/LayerInterface.h" #include "Core/Multilayer/LayerRoughness.h" #include "Core/Multilayer/MultiLayer.h" +#include "Core/Multilayer/Slice.h" #include "Core/SimulationElement/SimulationElement.h" #include <cerf> diff --git a/Core/Computation/SpecularComputation.cpp b/Core/Computation/SpecularComputation.cpp index a4ffd51b284..5853c97524f 100644 --- a/Core/Computation/SpecularComputation.cpp +++ b/Core/Computation/SpecularComputation.cpp @@ -15,8 +15,8 @@ #include "Core/Computation/SpecularComputation.h" #include "Core/Computation/ProcessedSample.h" #include "Core/Computation/ProgressHandler.h" -#include "Core/Multilayer/MultiLayer.h" #include "Core/Computation/SpecularStrategyBuilder.h" +#include "Core/Multilayer/MultiLayer.h" #include "Core/SimulationElement/SpecularSimulationElement.h" static_assert(std::is_copy_constructible<SpecularComputation>::value == false, diff --git a/Core/Computation/SpecularStrategyBuilder.h b/Core/Computation/SpecularStrategyBuilder.h index 94eba427338..a05258a78ac 100644 --- a/Core/Computation/SpecularStrategyBuilder.h +++ b/Core/Computation/SpecularStrategyBuilder.h @@ -12,8 +12,8 @@ // // ************************************************************************** // -#ifndef BORNAGAIN_CORE_MULTILAYER_SPECULARSTRATEGYBUILDER_H -#define BORNAGAIN_CORE_MULTILAYER_SPECULARSTRATEGYBUILDER_H +#ifndef BORNAGAIN_CORE_COMPUTATION_SPECULARSTRATEGYBUILDER_H +#define BORNAGAIN_CORE_COMPUTATION_SPECULARSTRATEGYBUILDER_H #include "Core/Multilayer/ISpecularStrategy.h" #include "Core/Multilayer/MultiLayer.h" @@ -26,4 +26,4 @@ public: static std::unique_ptr<ISpecularStrategy> build(const MultiLayer& sample, const bool magnetic); }; -#endif // BORNAGAIN_CORE_MULTILAYER_SPECULARSTRATEGYBUILDER_H +#endif // BORNAGAIN_CORE_COMPUTATION_SPECULARSTRATEGYBUILDER_H diff --git a/Core/Detector/DetectorFunctions.cpp b/Core/Detector/DetectorFunctions.cpp index c0a48feaadd..0f21813b2a3 100644 --- a/Core/Detector/DetectorFunctions.cpp +++ b/Core/Detector/DetectorFunctions.cpp @@ -13,9 +13,9 @@ // ************************************************************************** // #include "Core/Detector/DetectorFunctions.h" -#include "Core/Intensity/OutputData.h" #include "Core/Detector/SimulationArea.h" #include "Core/Detector/SimulationAreaIterator.h" +#include "Core/Intensity/OutputData.h" #include "Fit/Tools/StringUtils.h" #include <algorithm> #include <cctype> diff --git a/Core/Detector/IDetector.cpp b/Core/Detector/IDetector.cpp index b2e5e76edac..ebfc2e1a561 100644 --- a/Core/Detector/IDetector.cpp +++ b/Core/Detector/IDetector.cpp @@ -17,8 +17,8 @@ #include "Core/Detector/DetectorMask.h" #include "Core/Detector/IDetectorResolution.h" #include "Core/Detector/RegionOfInterest.h" -#include "Core/Intensity/OutputData.h" #include "Core/Detector/SimulationArea.h" +#include "Core/Intensity/OutputData.h" #include "Core/SimulationElement/SimulationElement.h" IDetector::IDetector() diff --git a/Core/Detector/IDetector.h b/Core/Detector/IDetector.h index baf6c535b7d..fc62b1dd8d4 100644 --- a/Core/Detector/IDetector.h +++ b/Core/Detector/IDetector.h @@ -18,8 +18,8 @@ #include "Core/Basics/ICloneable.h" #include "Core/Binning/IAxis.h" #include "Core/Detector/DetectionProperties.h" -#include "Core/Intensity/IUnitConverter.h" #include "Core/Detector/SimulationAreaIterator.h" +#include "Core/Intensity/IUnitConverter.h" #include "Core/Parametrization/INode.h" #include "Core/Tools/SafePointerVector.h" diff --git a/Core/Detector/ScanResolution.cpp b/Core/Detector/ScanResolution.cpp index 1dd8959496d..8f3a66d27b5 100644 --- a/Core/Detector/ScanResolution.cpp +++ b/Core/Detector/ScanResolution.cpp @@ -14,7 +14,7 @@ #include "Core/Detector/ScanResolution.h" #include "Core/Parametrization/RangedDistributions.h" -#include "Core/PyIO/PythonFormatting.h" +#include "Core/Tools/PyFmt.h" namespace { @@ -45,7 +45,7 @@ public: protected: std::string name() const override { return relative_resolution; } - std::string printStdDevs() const override { return PythonFormatting::printDouble(m_reldev); } + std::string printStdDevs() const override { return pyfmt::printDouble(m_reldev); } private: double m_reldev; //!< deltas for computing resolutions @@ -72,7 +72,7 @@ public: protected: std::string name() const override { return absolute_resolution; } - std::string printStdDevs() const override { return PythonFormatting::printDouble(m_stddev); } + std::string printStdDevs() const override { return pyfmt::printDouble(m_stddev); } private: double m_stddev; //!< deltas for computing resolutions @@ -196,7 +196,7 @@ std::string ScanResolution::print() const { std::stringstream result; result << *m_distr << "\n"; - result << PythonFormatting::indent() << "resolution = "; + result << pyfmt::indent() << "resolution = "; result << "ba." << name(); result << "(" << "distribution" diff --git a/Core/Detector/SimulationArea.h b/Core/Detector/SimulationArea.h index bdd88a8cd18..ded320387e3 100644 --- a/Core/Detector/SimulationArea.h +++ b/Core/Detector/SimulationArea.h @@ -12,8 +12,8 @@ // // ************************************************************************** // -#ifndef BORNAGAIN_CORE_INTENSITY_SIMULATIONAREA_H -#define BORNAGAIN_CORE_INTENSITY_SIMULATIONAREA_H +#ifndef BORNAGAIN_CORE_DETECTOR_SIMULATIONAREA_H +#define BORNAGAIN_CORE_DETECTOR_SIMULATIONAREA_H #include "Core/Detector/SimulationAreaIterator.h" #include "Wrap/WinDllMacros.h" @@ -67,4 +67,4 @@ public: virtual bool isMasked(size_t) const; }; -#endif // BORNAGAIN_CORE_INTENSITY_SIMULATIONAREA_H +#endif // BORNAGAIN_CORE_DETECTOR_SIMULATIONAREA_H diff --git a/Core/Detector/SimulationAreaIterator.h b/Core/Detector/SimulationAreaIterator.h index bfba706edca..5f6dcd45688 100644 --- a/Core/Detector/SimulationAreaIterator.h +++ b/Core/Detector/SimulationAreaIterator.h @@ -12,8 +12,8 @@ // // ************************************************************************** // -#ifndef BORNAGAIN_CORE_INTENSITY_SIMULATIONAREAITERATOR_H -#define BORNAGAIN_CORE_INTENSITY_SIMULATIONAREAITERATOR_H +#ifndef BORNAGAIN_CORE_DETECTOR_SIMULATIONAREAITERATOR_H +#define BORNAGAIN_CORE_DETECTOR_SIMULATIONAREAITERATOR_H #include "Wrap/WinDllMacros.h" #include <cstdlib> @@ -58,4 +58,4 @@ inline bool SimulationAreaIterator::operator!=(const SimulationAreaIterator& rig return !(*this == right); } -#endif // BORNAGAIN_CORE_INTENSITY_SIMULATIONAREAITERATOR_H +#endif // BORNAGAIN_CORE_DETECTOR_SIMULATIONAREAITERATOR_H diff --git a/Core/Export/SampleToPython.cpp b/Core/Export/SampleToPython.cpp index 41371541006..640540e203e 100644 --- a/Core/Export/SampleToPython.cpp +++ b/Core/Export/SampleToPython.cpp @@ -31,6 +31,7 @@ #include "Core/Particle/ParticleDistribution.h" #include "Core/PyIO/PythonFormatting.h" #include "Core/Scattering/IFormFactor.h" +#include "Core/Tools/PyFmt.h" #include "Core/includeIncludes/InterferenceFunctions.h" #include <iomanip> #include <map> @@ -84,7 +85,7 @@ SampleToPython::~SampleToPython() = default; std::string SampleToPython::defineGetSample() const { - return "def " + PythonFormatting::getSampleFunctionName() + "():\n" + defineMaterials() + defineLayers() + return "def " + pyfmt::getSampleFunctionName() + "():\n" + defineMaterials() + defineLayers() + defineFormFactors() + defineParticles() + defineCoreShellParticles() + defineParticleCompositions() + defineLattices() + defineCrystals() + defineMesoCrystals() + defineParticleDistributions() + defineInterferenceFunctions() @@ -118,16 +119,16 @@ std::string SampleToPython::defineMaterials() const if (p_material->isScalarMaterial()) { result << indent() << m_label->labelMaterial(p_material) << " = ba." << factory_name->second << "(\"" << p_material->getName() << "\", " - << PythonFormatting::printDouble(material_data.real()) << ", " << PythonFormatting::printDouble(material_data.imag()) - << ")\n"; + << pyfmt::printDouble(material_data.real()) << ", " + << pyfmt::printDouble(material_data.imag()) << ")\n"; } else { kvector_t magnetic_field = p_material->magnetization(); result << indent() << "magnetic_field = kvector_t(" << magnetic_field.x() << ", " << magnetic_field.y() << ", " << magnetic_field.z() << ")\n"; result << indent() << m_label->labelMaterial(p_material) << " = ba." << factory_name->second << "(\"" << p_material->getName(); - result << "\", " << PythonFormatting::printDouble(material_data.real()) << ", " - << PythonFormatting::printDouble(material_data.imag()) << ", " + result << "\", " << pyfmt::printDouble(material_data.real()) << ", " + << pyfmt::printDouble(material_data.imag()) << ", " << "magnetic_field)\n"; } } @@ -167,7 +168,7 @@ std::string SampleToPython::defineFormFactors() const for (auto it = themap->begin(); it != themap->end(); ++it) { const IFormFactor* p_ff = it->first; result << indent() << it->second << " = ba.FormFactor" << p_ff->getName() << "(" - << PythonFormatting::argumentList(p_ff) << ")\n"; + << pyfmt2::argumentList(p_ff) << ")\n"; } return result.str(); } @@ -237,13 +238,13 @@ std::string SampleToPython::defineParticleDistributions() const // building distribution functions std::string s_distr = "distr_" + std::to_string(index); result << indent() << s_distr << " = " - << PythonFormatting::printDistribution(*par_distr.getDistribution(), units) << "\n"; + << pyfmt2::printDistribution(*par_distr.getDistribution(), units) << "\n"; // building parameter distribution std::string s_par_distr = "par_distr_" + std::to_string(index); result << indent() << s_par_distr << " = " - << PythonFormatting::printParameterDistribution(par_distr, s_distr, units) << "\n"; + << pyfmt2::printParameterDistribution(par_distr, s_distr, units) << "\n"; // linked parameters std::vector<std::string> linked_pars = par_distr.getLinkedParameterNames(); @@ -302,12 +303,12 @@ std::string SampleToPython::defineLattices() const kvector_t bas_b = p_lattice->getBasisVectorB(); kvector_t bas_c = p_lattice->getBasisVectorC(); result << indent() << lattice_name << " = ba.Lattice(\n"; - result << indent() << indent() << "ba.kvector_t(" << PythonFormatting::printNm(bas_a.x()) << ", " - << PythonFormatting::printNm(bas_a.y()) << ", " << PythonFormatting::printNm(bas_a.z()) << "),\n"; - result << indent() << indent() << "ba.kvector_t(" << PythonFormatting::printNm(bas_b.x()) << ", " - << PythonFormatting::printNm(bas_b.y()) << ", " << PythonFormatting::printNm(bas_b.z()) << "),\n"; - result << indent() << indent() << "ba.kvector_t(" << PythonFormatting::printNm(bas_c.x()) << ", " - << PythonFormatting::printNm(bas_c.y()) << ", " << PythonFormatting::printNm(bas_c.z()) << "))\n"; + result << indent() << indent() << "ba.kvector_t(" << pyfmt::printNm(bas_a.x()) << ", " + << pyfmt::printNm(bas_a.y()) << ", " << pyfmt::printNm(bas_a.z()) << "),\n"; + result << indent() << indent() << "ba.kvector_t(" << pyfmt::printNm(bas_b.x()) << ", " + << pyfmt::printNm(bas_b.y()) << ", " << pyfmt::printNm(bas_b.z()) << "),\n"; + result << indent() << indent() << "ba.kvector_t(" << pyfmt::printNm(bas_c.x()) << ", " + << pyfmt::printNm(bas_c.y()) << ", " << pyfmt::printNm(bas_c.z()) << "))\n"; } return result.str(); } @@ -375,48 +376,48 @@ std::string SampleToPython::defineInterferenceFunctions() const dynamic_cast<const InterferenceFunction1DLattice*>(interference)) { const Lattice1DParameters latticeParameters = p_lattice_1d->getLatticeParameters(); result << indent() << it->second << " = ba.InterferenceFunction1DLattice(" - << PythonFormatting::printNm(latticeParameters.m_length) << ", " - << PythonFormatting::printDegrees(latticeParameters.m_xi) << ")\n"; + << pyfmt::printNm(latticeParameters.m_length) << ", " + << pyfmt::printDegrees(latticeParameters.m_xi) << ")\n"; auto pdf = INodeUtils::OnlyChildOfType<IFTDecayFunction1D>(*p_lattice_1d); if (pdf->decayLength() != 0.0) result << indent() << it->second << "_pdf = ba." << pdf->getName() << "(" - << PythonFormatting::argumentList(pdf) << ")\n" + << pyfmt2::argumentList(pdf) << ")\n" << indent() << it->second << ".setDecayFunction(" << it->second << "_pdf)\n"; } else if (auto p_para_radial = dynamic_cast<const InterferenceFunctionRadialParaCrystal*>(interference)) { result << indent() << it->second << " = ba.InterferenceFunctionRadialParaCrystal(" - << PythonFormatting::printNm(p_para_radial->peakDistance()) << ", " - << PythonFormatting::printNm(p_para_radial->dampingLength()) << ")\n"; + << pyfmt::printNm(p_para_radial->peakDistance()) << ", " + << pyfmt::printNm(p_para_radial->dampingLength()) << ")\n"; if (p_para_radial->kappa() != 0.0) result << indent() << it->second << ".setKappa(" - << PythonFormatting::printDouble(p_para_radial->kappa()) << ")\n"; + << pyfmt::printDouble(p_para_radial->kappa()) << ")\n"; if (p_para_radial->domainSize() != 0.0) result << indent() << it->second << ".setDomainSize(" - << PythonFormatting::printDouble(p_para_radial->domainSize()) << ")\n"; + << pyfmt::printDouble(p_para_radial->domainSize()) << ")\n"; auto pdf = INodeUtils::OnlyChildOfType<IFTDistribution1D>(*p_para_radial); if (pdf->omega() != 0.0) result << indent() << it->second << "_pdf = ba." << pdf->getName() << "(" - << PythonFormatting::argumentList(pdf) << ")\n" + << pyfmt2::argumentList(pdf) << ")\n" << indent() << it->second << ".setProbabilityDistribution(" << it->second << "_pdf)\n"; } else if (auto p_lattice_2d = dynamic_cast<const InterferenceFunction2DLattice*>(interference)) { const Lattice2D& lattice = p_lattice_2d->lattice(); result << indent() << it->second << " = ba.InterferenceFunction2DLattice(" - << PythonFormatting::printNm(lattice.length1()) << ", " << PythonFormatting::printNm(lattice.length2()) << ", " - << PythonFormatting::printDegrees(lattice.latticeAngle()) << ", " - << PythonFormatting::printDegrees(lattice.rotationAngle()) << ")\n"; + << pyfmt::printNm(lattice.length1()) << ", " << pyfmt::printNm(lattice.length2()) + << ", " << pyfmt::printDegrees(lattice.latticeAngle()) << ", " + << pyfmt::printDegrees(lattice.rotationAngle()) << ")\n"; auto pdf = INodeUtils::OnlyChildOfType<IFTDecayFunction2D>(*p_lattice_2d); result << indent() << it->second << "_pdf = ba." << pdf->getName() << "(" - << PythonFormatting::argumentList(pdf) << ")\n" + << pyfmt2::argumentList(pdf) << ")\n" << indent() << it->second << ".setDecayFunction(" << it->second << "_pdf)\n"; if (p_lattice_2d->integrationOverXi() == true) @@ -425,9 +426,9 @@ std::string SampleToPython::defineInterferenceFunctions() const dynamic_cast<const InterferenceFunctionFinite2DLattice*>(interference)) { const Lattice2D& lattice = p_lattice_2d->lattice(); result << indent() << it->second << " = ba.InterferenceFunctionFinite2DLattice(" - << PythonFormatting::printNm(lattice.length1()) << ", " << PythonFormatting::printNm(lattice.length2()) << ", " - << PythonFormatting::printDegrees(lattice.latticeAngle()) << ", " - << PythonFormatting::printDegrees(lattice.rotationAngle()) << ", " + << pyfmt::printNm(lattice.length1()) << ", " << pyfmt::printNm(lattice.length2()) + << ", " << pyfmt::printDegrees(lattice.latticeAngle()) << ", " + << pyfmt::printDegrees(lattice.rotationAngle()) << ", " << p_lattice_2d->numberUnitCells1() << ", " << p_lattice_2d->numberUnitCells2() << ")\n"; @@ -438,14 +439,15 @@ std::string SampleToPython::defineInterferenceFunctions() const std::vector<double> domainSize = p_para_2d->domainSizes(); const Lattice2D& lattice = p_para_2d->lattice(); result << indent() << it->second << " = ba.InterferenceFunction2DParaCrystal(" - << PythonFormatting::printNm(lattice.length1()) << ", " << PythonFormatting::printNm(lattice.length2()) << ", " - << PythonFormatting::printDegrees(lattice.latticeAngle()) << ", " - << PythonFormatting::printDegrees(lattice.rotationAngle()) << ", " - << PythonFormatting::printNm(p_para_2d->dampingLength()) << ")\n"; + << pyfmt::printNm(lattice.length1()) << ", " << pyfmt::printNm(lattice.length2()) + << ", " << pyfmt::printDegrees(lattice.latticeAngle()) << ", " + << pyfmt::printDegrees(lattice.rotationAngle()) << ", " + << pyfmt::printNm(p_para_2d->dampingLength()) << ")\n"; if (domainSize[0] != 0.0 || domainSize[1] != 0.0) - result << indent() << it->second << ".setDomainSizes(" << PythonFormatting::printNm(domainSize[0]) - << ", " << PythonFormatting::printNm(domainSize[1]) << ")\n"; + result << indent() << it->second << ".setDomainSizes(" + << pyfmt::printNm(domainSize[0]) << ", " << pyfmt::printNm(domainSize[1]) + << ")\n"; if (p_para_2d->integrationOverXi() == true) result << indent() << it->second << ".setIntegrationOverXi(True)\n"; @@ -455,19 +457,19 @@ std::string SampleToPython::defineInterferenceFunctions() const const IFTDistribution2D* pdf = pdf_vector[0]; result << indent() << it->second << "_pdf_1 = ba." << pdf->getName() << "(" - << PythonFormatting::argumentList(pdf) << ")\n"; + << pyfmt2::argumentList(pdf) << ")\n"; pdf = pdf_vector[1]; result << indent() << it->second << "_pdf_2 = ba." << pdf->getName() << "(" - << PythonFormatting::argumentList(pdf) << ")\n"; + << pyfmt2::argumentList(pdf) << ")\n"; result << indent() << it->second << ".setProbabilityDistributions(" << it->second << "_pdf_1, " << it->second << "_pdf_2)\n"; } else if (auto p_lattice_hd = dynamic_cast<const InterferenceFunctionHardDisk*>(interference)) { result << indent() << it->second << " = ba.InterferenceFunctionHardDisk(" - << PythonFormatting::printNm(p_lattice_hd->radius()) << ", " - << PythonFormatting::printDouble(p_lattice_hd->density()) << ")\n"; + << pyfmt::printNm(p_lattice_hd->radius()) << ", " + << pyfmt::printDouble(p_lattice_hd->density()) << ")\n"; } else throw Exceptions::NotImplementedException( "Bug: ExportToPython::defineInterferenceFunctions() called with unexpected " @@ -475,7 +477,7 @@ std::string SampleToPython::defineInterferenceFunctions() const + interference->getName()); if (interference->positionVariance() > 0.0) { result << indent() << it->second << ".setPositionVariance(" - << PythonFormatting::printNm2(interference->positionVariance()) << ")\n"; + << pyfmt::printNm2(interference->positionVariance()) << ")\n"; } } return result.str(); @@ -498,8 +500,8 @@ std::string SampleToPython::defineParticleLayouts() const for (auto p_particle : particles) { double abundance = p_particle->abundance(); result << indent() << it->second << ".addParticle(" - << m_label->labelParticle(p_particle) << ", " << PythonFormatting::printDouble(abundance) - << ")\n"; + << m_label->labelParticle(p_particle) << ", " + << pyfmt::printDouble(abundance) << ")\n"; } if (auto p_iff = INodeUtils::OnlyChildOfType<IInterferenceFunction>(*particleLayout)) result << indent() << it->second << ".setInterferenceFunction(" @@ -521,8 +523,8 @@ std::string SampleToPython::defineRoughnesses() const result << std::setprecision(12); result << "\n" << indent() << "# Defining Roughness Parameters\n"; for (auto it = themap->begin(); it != themap->end(); ++it) - result << indent() << it->second << " = ba.LayerRoughness(" << PythonFormatting::argumentList(it->first) - << ")\n"; + result << indent() << it->second << " = ba.LayerRoughness(" + << pyfmt2::argumentList(it->first) << ")\n"; return result.str(); } @@ -561,9 +563,9 @@ std::string SampleToPython::defineMultiLayers() const if (external_field.mag() > 0.0) { std::string field_name = it->second + "_external_field"; result << indent() << field_name << " = kvector_t(" - << PythonFormatting::printScientificDouble(external_field.x()) << ", " - << PythonFormatting::printScientificDouble(external_field.y()) << ", " - << PythonFormatting::printScientificDouble(external_field.z()) << ")\n"; + << pyfmt::printScientificDouble(external_field.x()) << ", " + << pyfmt::printScientificDouble(external_field.y()) << ", " + << pyfmt::printScientificDouble(external_field.z()) << ")\n"; result << indent() << it->second << ".setExternalField(" << field_name << ")\n"; } size_t numberOfLayers = it->first->numberOfLayers(); @@ -603,25 +605,26 @@ void SampleToPython::setRotationInformation(const IParticle* p_particle, std::st case Transform3D::EULER: { double alpha, beta, gamma; p_particle->rotation()->getTransform3D().calculateEulerAngles(&alpha, &beta, &gamma); - result << indent() << name << "_rotation = ba.RotationEuler(" << PythonFormatting::printDegrees(alpha) - << ", " << PythonFormatting::printDegrees(beta) << ", " << PythonFormatting::printDegrees(gamma) << ")\n"; + result << indent() << name << "_rotation = ba.RotationEuler(" + << pyfmt::printDegrees(alpha) << ", " << pyfmt::printDegrees(beta) << ", " + << pyfmt::printDegrees(gamma) << ")\n"; break; } case Transform3D::XAXIS: { double alpha = p_particle->rotation()->getTransform3D().calculateRotateXAngle(); - result << indent() << name << "_rotation = ba.RotationX(" << PythonFormatting::printDegrees(alpha) + result << indent() << name << "_rotation = ba.RotationX(" << pyfmt::printDegrees(alpha) << ")\n"; break; } case Transform3D::YAXIS: { double alpha = p_particle->rotation()->getTransform3D().calculateRotateYAngle(); - result << indent() << name << "_rotation = ba.RotationY(" << PythonFormatting::printDegrees(alpha) + result << indent() << name << "_rotation = ba.RotationY(" << pyfmt::printDegrees(alpha) << ")\n"; break; } case Transform3D::ZAXIS: { double alpha = p_particle->rotation()->getTransform3D().calculateRotateZAngle(); - result << indent() << name << "_rotation = ba.RotationZ(" << PythonFormatting::printDegrees(alpha) + result << indent() << name << "_rotation = ba.RotationZ(" << pyfmt::printDegrees(alpha) << ")\n"; break; } @@ -637,8 +640,8 @@ void SampleToPython::setPositionInformation(const IParticle* p_particle, std::st bool has_position_info = (pos != kvector_t()); if (has_position_info) { - result << indent() << name << "_position = kvector_t(" << PythonFormatting::printNm(pos.x()) << ", " - << PythonFormatting::printNm(pos.y()) << ", " << PythonFormatting::printNm(pos.z()) << ")\n"; + result << indent() << name << "_position = kvector_t(" << pyfmt::printNm(pos.x()) << ", " + << pyfmt::printNm(pos.y()) << ", " << pyfmt::printNm(pos.z()) << ")\n"; result << indent() << name << ".setPosition(" << name << "_position)\n"; } diff --git a/Core/Export/SimulationToPython.cpp b/Core/Export/SimulationToPython.cpp index 13b41a5ecc7..25ee7fc4f4c 100644 --- a/Core/Export/SimulationToPython.cpp +++ b/Core/Export/SimulationToPython.cpp @@ -30,13 +30,14 @@ #include "Core/Simulation/GISASSimulation.h" #include "Core/Simulation/OffSpecSimulation.h" #include "Core/Simulation/SpecularSimulation.h" +#include "Core/Tools/PyFmt.h" #include <iomanip> namespace { const std::string defineSimulate = "def run_simulation():\n" " sample = " - + PythonFormatting::getSampleFunctionName() + + pyfmt::getSampleFunctionName() + "()\n" " simulation = get_simulation()\n" " simulation.setSample(sample)\n" @@ -48,9 +49,9 @@ const std::string defineSimulate = "def run_simulation():\n" std::function<std::string(double)> printFunc(const IDetector* detector) { if (detector->defaultAxesUnits() == AxesUnits::MM) - return PythonFormatting::printDouble; + return pyfmt::printDouble; if (detector->defaultAxesUnits() == AxesUnits::RADIANS) - return PythonFormatting::printDegrees; + return pyfmt::printDegrees; throw Exceptions::RuntimeErrorException( "SimulationToPython::defineMasks() -> Error. Unknown detector units."); } @@ -67,7 +68,7 @@ std::string SimulationToPython::generateSimulationCode(const Simulation& simulat SampleToPython sampleGenerator; - return PythonFormatting::scriptPreamble() + sampleGenerator.generateSampleCode(*simulation.sample()) + return pyfmt::scriptPreamble() + sampleGenerator.generateSampleCode(*simulation.sample()) + defineGetSimulation(&simulation) + defineSimulate + defineMain(mainType); } @@ -86,14 +87,14 @@ std::string SimulationToPython::defineGetSimulation(const Simulation* simulation throw std::runtime_error("SimulationToPython::defineGetSimulation() -> Error. " "Wrong simulation type"); - result << PythonFormatting::indent() << "return simulation\n\n\n"; + result << pyfmt::indent() << "return simulation\n\n\n"; return result.str(); } std::string SimulationToPython::defineGISASSimulation(const GISASSimulation* simulation) const { std::ostringstream result; - result << PythonFormatting::indent() << "simulation = ba.GISASSimulation()\n"; + result << pyfmt::indent() << "simulation = ba.GISASSimulation()\n"; result << defineDetector(simulation); result << defineDetectorResolutionFunction(simulation); result << defineDetectorPolarizationAnalysis(simulation); @@ -108,7 +109,7 @@ std::string SimulationToPython::defineGISASSimulation(const GISASSimulation* sim std::string SimulationToPython::defineOffSpecSimulation(const OffSpecSimulation* simulation) const { std::ostringstream result; - result << PythonFormatting::indent() << "simulation = ba.OffSpecSimulation()\n"; + result << pyfmt::indent() << "simulation = ba.OffSpecSimulation()\n"; result << defineDetector(simulation); result << defineDetectorResolutionFunction(simulation); result << defineDetectorPolarizationAnalysis(simulation); @@ -123,7 +124,7 @@ std::string SimulationToPython::defineOffSpecSimulation(const OffSpecSimulation* std::string SimulationToPython::defineSpecularSimulation(const SpecularSimulation* simulation) const { std::ostringstream result; - result << PythonFormatting::indent() << "simulation = ba.SpecularSimulation()\n"; + result << pyfmt::indent() << "simulation = ba.SpecularSimulation()\n"; result << defineDetectorPolarizationAnalysis(simulation); result << defineSpecularScan(*simulation); result << defineParameterDistributions(simulation); @@ -143,65 +144,69 @@ std::string SimulationToPython::defineDetector(const Simulation* simulation) con result << std::setprecision(12); if (auto detector = dynamic_cast<const SphericalDetector*>(iDetector)) { - result << PythonFormatting::indent() << "simulation.setDetectorParameters("; + result << pyfmt::indent() << "simulation.setDetectorParameters("; for (size_t index = 0; index < detector->dimension(); ++index) { if (index != 0) result << ", "; result << detector->getAxis(index).size() << ", " - << PythonFormatting::printDegrees(detector->getAxis(index).getMin()) << ", " - << PythonFormatting::printDegrees(detector->getAxis(index).getMax()); + << pyfmt::printDegrees(detector->getAxis(index).getMin()) << ", " + << pyfmt::printDegrees(detector->getAxis(index).getMax()); } result << ")\n"; } else if (auto detector = dynamic_cast<const RectangularDetector*>(iDetector)) { - result << PythonFormatting::indent() << "\n"; - result << PythonFormatting::indent() << "detector = ba.RectangularDetector(" << detector->getNbinsX() << ", " - << PythonFormatting::printDouble(detector->getWidth()) << ", " << detector->getNbinsY() << ", " - << PythonFormatting::printDouble(detector->getHeight()) << ")\n"; + result << pyfmt::indent() << "\n"; + result << pyfmt::indent() << "detector = ba.RectangularDetector(" << detector->getNbinsX() + << ", " << pyfmt::printDouble(detector->getWidth()) << ", " << detector->getNbinsY() + << ", " << pyfmt::printDouble(detector->getHeight()) << ")\n"; if (detector->getDetectorArrangment() == RectangularDetector::GENERIC) { - result << PythonFormatting::indent() << "detector.setPosition(" - << PythonFormatting::printKvector(detector->getNormalVector()) << ", " - << PythonFormatting::printDouble(detector->getU0()) << ", " << PythonFormatting::printDouble(detector->getV0()); - if (!PythonFormatting::isDefaultDirection(detector->getDirectionVector())) - result << ", " << PythonFormatting::printKvector(detector->getDirectionVector()); + result << pyfmt::indent() << "detector.setPosition(" + << pyfmt::printKvector(detector->getNormalVector()) << ", " + << pyfmt::printDouble(detector->getU0()) << ", " + << pyfmt::printDouble(detector->getV0()); + if (!pyfmt::isDefaultDirection(detector->getDirectionVector())) + result << ", " << pyfmt::printKvector(detector->getDirectionVector()); result << ")\n"; } else if (detector->getDetectorArrangment() == RectangularDetector::PERPENDICULAR_TO_SAMPLE) { - result << PythonFormatting::indent() << "detector.setPerpendicularToSampleX(" - << PythonFormatting::printDouble(detector->getDistance()) << ", " << PythonFormatting::printDouble(detector->getU0()) - << ", " << PythonFormatting::printDouble(detector->getV0()) << ")\n"; + result << pyfmt::indent() << "detector.setPerpendicularToSampleX(" + << pyfmt::printDouble(detector->getDistance()) << ", " + << pyfmt::printDouble(detector->getU0()) << ", " + << pyfmt::printDouble(detector->getV0()) << ")\n"; } else if (detector->getDetectorArrangment() == RectangularDetector::PERPENDICULAR_TO_DIRECT_BEAM) { - result << PythonFormatting::indent() << "detector.setPerpendicularToDirectBeam(" - << PythonFormatting::printDouble(detector->getDistance()) << ", " << PythonFormatting::printDouble(detector->getU0()) - << ", " << PythonFormatting::printDouble(detector->getV0()) << ")\n"; + result << pyfmt::indent() << "detector.setPerpendicularToDirectBeam(" + << pyfmt::printDouble(detector->getDistance()) << ", " + << pyfmt::printDouble(detector->getU0()) << ", " + << pyfmt::printDouble(detector->getV0()) << ")\n"; } else if (detector->getDetectorArrangment() == RectangularDetector::PERPENDICULAR_TO_REFLECTED_BEAM) { - result << PythonFormatting::indent() << "detector.setPerpendicularToReflectedBeam(" - << PythonFormatting::printDouble(detector->getDistance()) << ", " << PythonFormatting::printDouble(detector->getU0()) - << ", " << PythonFormatting::printDouble(detector->getV0()) << ")\n"; + result << pyfmt::indent() << "detector.setPerpendicularToReflectedBeam(" + << pyfmt::printDouble(detector->getDistance()) << ", " + << pyfmt::printDouble(detector->getU0()) << ", " + << pyfmt::printDouble(detector->getV0()) << ")\n"; } else if (detector->getDetectorArrangment() == RectangularDetector::PERPENDICULAR_TO_REFLECTED_BEAM_DPOS) { - result << PythonFormatting::indent() << "detector.setPerpendicularToReflectedBeam(" - << PythonFormatting::printDouble(detector->getDistance()) << ")\n"; - result << PythonFormatting::indent() << "detector.setDirectBeamPosition(" - << PythonFormatting::printDouble(detector->getDirectBeamU0()) << ", " - << PythonFormatting::printDouble(detector->getDirectBeamV0()) << ")\n"; + result << pyfmt::indent() << "detector.setPerpendicularToReflectedBeam(" + << pyfmt::printDouble(detector->getDistance()) << ")\n"; + result << pyfmt::indent() << "detector.setDirectBeamPosition(" + << pyfmt::printDouble(detector->getDirectBeamU0()) << ", " + << pyfmt::printDouble(detector->getDirectBeamV0()) << ")\n"; } else throw Exceptions::RuntimeErrorException( "SimulationToPython::defineDetector() -> Error. Unknown alignment."); - result << PythonFormatting::indent() << "simulation.setDetector(detector)\n"; + result << pyfmt::indent() << "simulation.setDetector(detector)\n"; } else throw Exceptions::RuntimeErrorException("SimulationToPython::defineDetector() -> Error. " "Unknown detector"); if (iDetector->regionOfInterest()) { - result << PythonFormatting::indent() << "simulation.setRegionOfInterest(" + result << pyfmt::indent() << "simulation.setRegionOfInterest(" << printFunc(iDetector)(iDetector->regionOfInterest()->getXlow()) << ", " << printFunc(iDetector)(iDetector->regionOfInterest()->getYlow()) << ", " << printFunc(iDetector)(iDetector->regionOfInterest()->getXup()) << ", " << printFunc(iDetector)(iDetector->regionOfInterest()->getYup()) << ")\n"; } - result << PythonFormatting::indent() << "\n"; + result << pyfmt::indent() << "\n"; return result.str(); } @@ -214,7 +219,7 @@ std::string SimulationToPython::defineDetectorResolutionFunction(const Simulatio if (auto* p_convfunc = dynamic_cast<const ConvolutionDetectorResolution*>(p_resfunc)) { if (auto* resfunc = dynamic_cast<const ResolutionFunction2DGaussian*>( p_convfunc->getResolutionFunction2D())) { - result << PythonFormatting::indent() << "simulation.setDetectorResolutionFunction("; + result << pyfmt::indent() << "simulation.setDetectorResolutionFunction("; result << "ba.ResolutionFunction2DGaussian("; result << printFunc(detector)(resfunc->getSigmaX()) << ", "; result << printFunc(detector)(resfunc->getSigmaY()) << "))\n"; @@ -242,12 +247,13 @@ SimulationToPython::defineDetectorPolarizationAnalysis(const Simulation* simulat if (analyzer_direction.mag() > 0.0) { std::string direction_name = "analyzer_direction"; - result << PythonFormatting::indent() << direction_name << " = kvector_t(" - << PythonFormatting::printDouble(analyzer_direction.x()) << ", " << PythonFormatting::printDouble(analyzer_direction.y()) - << ", " << PythonFormatting::printDouble(analyzer_direction.z()) << ")\n"; - result << PythonFormatting::indent() << "simulation.setAnalyzerProperties(" << direction_name << ", " - << PythonFormatting::printDouble(analyzer_efficiency) << ", " - << PythonFormatting::printDouble(analyzer_total_transmission) << ")\n"; + result << pyfmt::indent() << direction_name << " = kvector_t(" + << pyfmt::printDouble(analyzer_direction.x()) << ", " + << pyfmt::printDouble(analyzer_direction.y()) << ", " + << pyfmt::printDouble(analyzer_direction.z()) << ")\n"; + result << pyfmt::indent() << "simulation.setAnalyzerProperties(" << direction_name << ", " + << pyfmt::printDouble(analyzer_efficiency) << ", " + << pyfmt::printDouble(analyzer_total_transmission) << ")\n"; } return result.str(); } @@ -257,8 +263,9 @@ std::string SimulationToPython::defineGISASBeam(const GISASSimulation& simulatio std::ostringstream result; const Beam& beam = simulation.getInstrument().getBeam(); - result << PythonFormatting::indent() << "simulation.setBeamParameters(" << PythonFormatting::printNm(beam.getWavelength()) << ", " - << PythonFormatting::printDegrees(beam.getAlpha()) << ", " << PythonFormatting::printDegrees(beam.getPhi()) << ")\n"; + result << pyfmt::indent() << "simulation.setBeamParameters(" + << pyfmt::printNm(beam.getWavelength()) << ", " << pyfmt::printDegrees(beam.getAlpha()) + << ", " << pyfmt::printDegrees(beam.getPhi()) << ")\n"; result << defineBeamPolarization(beam); result << defineBeamIntensity(beam); @@ -271,12 +278,12 @@ std::string SimulationToPython::defineOffSpecBeam(const OffSpecSimulation& simul std::ostringstream result; const Beam& beam = simulation.getInstrument().getBeam(); - const std::string axis_def = PythonFormatting::indent() + "alpha_i_axis = "; - result << axis_def - << PythonFormatting::printAxis(*simulation.beamAxis(), "rad", axis_def.size()) << "\n"; + const std::string axis_def = pyfmt::indent() + "alpha_i_axis = "; + result << axis_def << pyfmt2::printAxis(*simulation.beamAxis(), "rad", axis_def.size()) << "\n"; - result << PythonFormatting::indent() << "simulation.setBeamParameters(" << PythonFormatting::printNm(beam.getWavelength()) << ", " - << "alpha_i_axis, " << PythonFormatting::printDegrees(beam.getPhi()) << ")\n"; + result << pyfmt::indent() << "simulation.setBeamParameters(" + << pyfmt::printNm(beam.getWavelength()) << ", " + << "alpha_i_axis, " << pyfmt::printDegrees(beam.getPhi()) << ")\n"; result << defineBeamPolarization(beam); result << defineBeamIntensity(beam); @@ -292,7 +299,7 @@ std::string SimulationToPython::defineSpecularScan(const SpecularSimulation& sim "does not contain any scan"); result << *scan << "\n"; - result << PythonFormatting::indent() << "simulation.setScan(scan)\n"; + result << pyfmt::indent() << "simulation.setScan(scan)\n"; result << defineBeamIntensity(simulation.getInstrument().getBeam()); result << "\n"; return result.str(); @@ -304,10 +311,12 @@ std::string SimulationToPython::defineBeamPolarization(const Beam& beam) const auto bloch_vector = beam.getBlochVector(); if (bloch_vector.mag() > 0.0) { std::string beam_polarization = "beam_polarization"; - result << PythonFormatting::indent() << beam_polarization << " = kvector_t(" << PythonFormatting::printDouble(bloch_vector.x()) - << ", " << PythonFormatting::printDouble(bloch_vector.y()) << ", " << PythonFormatting::printDouble(bloch_vector.z()) + result << pyfmt::indent() << beam_polarization << " = kvector_t(" + << pyfmt::printDouble(bloch_vector.x()) << ", " + << pyfmt::printDouble(bloch_vector.y()) << ", " + << pyfmt::printDouble(bloch_vector.z()) << ")\n"; + result << pyfmt::indent() << "simulation.setBeamPolarization(" << beam_polarization << ")\n"; - result << PythonFormatting::indent() << "simulation.setBeamPolarization(" << beam_polarization << ")\n"; } return result.str(); } @@ -317,8 +326,8 @@ std::string SimulationToPython::defineBeamIntensity(const Beam& beam) const std::ostringstream result; double beam_intensity = beam.getIntensity(); if (beam_intensity > 0.0) - result << PythonFormatting::indent() << "simulation.setBeamIntensity(" - << PythonFormatting::printScientificDouble(beam_intensity) << ")\n"; + result << pyfmt::indent() << "simulation.setBeamIntensity(" + << pyfmt::printScientificDouble(beam_intensity) << ")\n"; return result.str(); } @@ -338,12 +347,14 @@ std::string SimulationToPython::defineParameterDistributions(const Simulation* s double sigma_factor = distributions[i].getSigmaFactor(); std::string s_distr = "distr_" + std::to_string(i + 1); - result << PythonFormatting::indent() << s_distr << " = " - << PythonFormatting::printDistribution(*distributions[i].getDistribution(), mainParUnits) << "\n"; - - result << PythonFormatting::indent() << "simulation.addParameterDistribution(\"" << main_par_name << "\", " - << s_distr << ", " << nbr_samples << ", " << PythonFormatting::printDouble(sigma_factor) - << PythonFormatting::printRealLimitsArg(distributions[i].getLimits(), mainParUnits) << ")\n"; + result << pyfmt::indent() << s_distr << " = " + << pyfmt2::printDistribution(*distributions[i].getDistribution(), mainParUnits) + << "\n"; + + result << pyfmt::indent() << "simulation.addParameterDistribution(\"" << main_par_name + << "\", " << s_distr << ", " << nbr_samples << ", " + << pyfmt::printDouble(sigma_factor) + << pyfmt2::printRealLimitsArg(distributions[i].getLimits(), mainParUnits) << ")\n"; } return result.str(); } @@ -360,7 +371,8 @@ std::string SimulationToPython::defineMasks(const Simulation* simulation) const for (size_t i_mask = 0; i_mask < detectorMask->numberOfMasks(); ++i_mask) { bool mask_value(false); const IShape2D* shape = detectorMask->getMaskShape(i_mask, mask_value); - result << PythonFormatting::representShape2D(PythonFormatting::indent(), shape, mask_value, printFunc(detector)); + result << pyfmt2::representShape2D(pyfmt::indent(), shape, mask_value, + printFunc(detector)); } result << "\n"; } @@ -374,15 +386,15 @@ std::string SimulationToPython::defineSimulationOptions(const Simulation* simula const SimulationOptions& options = simulation->getOptions(); if (options.getHardwareConcurrency() != options.getNumberOfThreads()) - result << PythonFormatting::indent() << "simulation.getOptions().setNumberOfThreads(" + result << pyfmt::indent() << "simulation.getOptions().setNumberOfThreads(" << options.getNumberOfThreads() << ")\n"; if (options.isIntegrate()) - result << PythonFormatting::indent() << "simulation.getOptions().setMonteCarloIntegration(True, " + result << pyfmt::indent() << "simulation.getOptions().setMonteCarloIntegration(True, " << options.getMcPoints() << ")\n"; if (options.useAvgMaterials()) - result << PythonFormatting::indent() << "simulation.getOptions().setUseAvgMaterials(True)\n"; + result << pyfmt::indent() << "simulation.getOptions().setUseAvgMaterials(True)\n"; if (options.includeSpecular()) - result << PythonFormatting::indent() << "simulation.getOptions().setIncludeSpecular(True)\n"; + result << pyfmt::indent() << "simulation.getOptions().setIncludeSpecular(True)\n"; return result.str(); } @@ -393,13 +405,13 @@ std::string SimulationToPython::defineBackground(const Simulation* simulation) c auto p_bg = simulation->background(); if (auto p_constant_bg = dynamic_cast<const ConstantBackground*>(p_bg)) { if (p_constant_bg->backgroundValue() > 0.0) { - result << PythonFormatting::indent() << "background = ba.ConstantBackground(" - << PythonFormatting::printScientificDouble(p_constant_bg->backgroundValue()) << ")\n"; - result << PythonFormatting::indent() << "simulation.setBackground(background)\n"; + result << pyfmt::indent() << "background = ba.ConstantBackground(" + << pyfmt::printScientificDouble(p_constant_bg->backgroundValue()) << ")\n"; + result << pyfmt::indent() << "simulation.setBackground(background)\n"; } } else if (dynamic_cast<const PoissonNoiseBackground*>(p_bg)) { - result << PythonFormatting::indent() << "background = ba.PoissonNoiseBackground()\n"; - result << PythonFormatting::indent() << "simulation.setBackground(background)\n"; + result << pyfmt::indent() << "background = ba.PoissonNoiseBackground()\n"; + result << pyfmt::indent() << "simulation.setBackground(background)\n"; } return result.str(); } diff --git a/Core/Fitting/FitObjective.cpp b/Core/Fitting/FitObjective.cpp index 0fd7bad153a..36505066561 100644 --- a/Core/Fitting/FitObjective.cpp +++ b/Core/Fitting/FitObjective.cpp @@ -17,8 +17,8 @@ #include "Core/Fitting/ObjectiveMetric.h" #include "Core/Fitting/ObjectiveMetricUtils.h" #include "Core/Fitting/PyFittingCallbacks.h" -#include "Core/Intensity/ArrayUtils.h" #include "Core/Instrument/ChiSquaredModule.h" +#include "Core/Intensity/ArrayUtils.h" #include "Core/Simulation/Simulation.h" #include "Fit/Kernel/MinimizerResult.h" #include "Fit/Kernel/Parameters.h" diff --git a/Core/Instrument/AngularSpecScan.cpp b/Core/Instrument/AngularSpecScan.cpp index 12b66293b00..b39d02b1bb9 100644 --- a/Core/Instrument/AngularSpecScan.cpp +++ b/Core/Instrument/AngularSpecScan.cpp @@ -21,6 +21,7 @@ #include "Core/Parametrization/RangedDistributions.h" #include "Core/PyIO/PythonFormatting.h" #include "Core/SimulationElement/SpecularSimulationElement.h" +#include "Core/Tools/PyFmt.h" #include "Fit/Tools/RealLimits.h" namespace @@ -238,27 +239,26 @@ AngularSpecScan::createIntensities(const std::vector<SpecularSimulationElement>& std::string AngularSpecScan::print() const { std::stringstream result; - result << "\n" << PythonFormatting::indent() << "# Defining specular scan:\n"; - const std::string axis_def = PythonFormatting::indent() + "axis = "; - result << axis_def << PythonFormatting::printAxis(*coordinateAxis(), "rad", axis_def.size()) - << "\n"; + result << "\n" << pyfmt::indent() << "# Defining specular scan:\n"; + const std::string axis_def = pyfmt::indent() + "axis = "; + result << axis_def << pyfmt2::printAxis(*coordinateAxis(), "rad", axis_def.size()) << "\n"; - result << PythonFormatting::indent() << "scan = "; - result << "ba.AngularSpecScan(" << PythonFormatting::printDouble(m_wl) << ", axis)\n"; + result << pyfmt::indent() << "scan = "; + result << "ba.AngularSpecScan(" << pyfmt::printDouble(m_wl) << ", axis)\n"; if (m_footprint) { result << *m_footprint << "\n"; - result << PythonFormatting::indent() << "scan.setFootprintFactor(footprint)\n"; + result << pyfmt::indent() << "scan.setFootprintFactor(footprint)\n"; } if (!m_inc_resolution->empty()) { - result << "\n" << PythonFormatting::indent() << "# Defining angular resolution\n"; + result << "\n" << pyfmt::indent() << "# Defining angular resolution\n"; result << *m_inc_resolution << "\n"; - result << PythonFormatting::indent() << "scan.setAngleResolution(resolution)\n"; + result << pyfmt::indent() << "scan.setAngleResolution(resolution)\n"; } if (!m_wl_resolution->empty()) { - result << "\n" << PythonFormatting::indent() << "# Defining wavelength resolution\n"; + result << "\n" << pyfmt::indent() << "# Defining wavelength resolution\n"; result << *m_wl_resolution << "\n"; - result << PythonFormatting::indent() << "scan.setWavelengthResolution(resolution)\n"; + result << pyfmt::indent() << "scan.setWavelengthResolution(resolution)\n"; } return result.str(); } diff --git a/Core/Instrument/QSpecScan.cpp b/Core/Instrument/QSpecScan.cpp index 258b5caa30b..ee7b91f0019 100644 --- a/Core/Instrument/QSpecScan.cpp +++ b/Core/Instrument/QSpecScan.cpp @@ -20,6 +20,7 @@ #include "Core/Parametrization/RangedDistributions.h" #include "Core/PyIO/PythonFormatting.h" #include "Core/SimulationElement/SpecularSimulationElement.h" +#include "Core/Tools/PyFmt.h" #include "Fit/Tools/RealLimits.h" namespace @@ -111,15 +112,14 @@ QSpecScan::createIntensities(const std::vector<SpecularSimulationElement>& sim_e std::string QSpecScan::print() const { std::stringstream result; - const std::string axis_def = PythonFormatting::indent() + "axis = "; - result << axis_def << PythonFormatting::printAxis(*coordinateAxis(), "", axis_def.size()) - << "\n"; + const std::string axis_def = pyfmt::indent() + "axis = "; + result << axis_def << pyfmt2::printAxis(*coordinateAxis(), "", axis_def.size()) << "\n"; - result << PythonFormatting::indent() << "scan = ba.QSpecScan(axis)"; + result << pyfmt::indent() << "scan = ba.QSpecScan(axis)"; if (!m_resolution->empty()) { result << "\n"; result << *m_resolution << "\n"; - result << PythonFormatting::indent() << "scan.setQResolution(resolution)"; + result << pyfmt::indent() << "scan.setQResolution(resolution)"; } return result.str(); } diff --git a/Core/Intensity/ArrayUtils.h b/Core/Intensity/ArrayUtils.h index 5905ec6b09e..64ed3932a19 100644 --- a/Core/Intensity/ArrayUtils.h +++ b/Core/Intensity/ArrayUtils.h @@ -12,8 +12,8 @@ // // ************************************************************************** // -#ifndef BORNAGAIN_CORE_INSTRUMENT_ARRAYUTILS_H -#define BORNAGAIN_CORE_INSTRUMENT_ARRAYUTILS_H +#ifndef BORNAGAIN_CORE_INTENSITY_ARRAYUTILS_H +#define BORNAGAIN_CORE_INTENSITY_ARRAYUTILS_H #include "Core/Intensity/OutputData.h" #include "Wrap/WinDllMacros.h" @@ -170,4 +170,4 @@ template <class T> decltype(auto) ArrayUtils::createVector2D(const T& data) return result; } -#endif // BORNAGAIN_CORE_INSTRUMENT_ARRAYUTILS_H +#endif // BORNAGAIN_CORE_INTENSITY_ARRAYUTILS_H diff --git a/Core/Intensity/AxisNames.h b/Core/Intensity/AxisNames.h index 1b9cd31a314..aa96f7022a9 100644 --- a/Core/Intensity/AxisNames.h +++ b/Core/Intensity/AxisNames.h @@ -12,8 +12,8 @@ // // ************************************************************************** // -#ifndef BORNAGAIN_CORE_INSTRUMENT_AXISNAMES_H -#define BORNAGAIN_CORE_INSTRUMENT_AXISNAMES_H +#ifndef BORNAGAIN_CORE_INTENSITY_AXISNAMES_H +#define BORNAGAIN_CORE_INTENSITY_AXISNAMES_H #include "Core/Intensity/IUnitConverter.h" #include <map> @@ -35,4 +35,4 @@ BA_CORE_API_ std::map<AxesUnits, std::string> InitSpecAxisQ(); BA_CORE_API_ std::map<AxesUnits, std::string> InitSampleDepthAxis(); } // namespace AxisNames -#endif // BORNAGAIN_CORE_INSTRUMENT_AXISNAMES_H +#endif // BORNAGAIN_CORE_INTENSITY_AXISNAMES_H diff --git a/Core/Intensity/CumulativeValue.h b/Core/Intensity/CumulativeValue.h index 45ac50f1787..7e67ce17b33 100644 --- a/Core/Intensity/CumulativeValue.h +++ b/Core/Intensity/CumulativeValue.h @@ -12,8 +12,8 @@ // // ************************************************************************** // -#ifndef BORNAGAIN_CORE_INSTRUMENT_CUMULATIVEVALUE_H -#define BORNAGAIN_CORE_INSTRUMENT_CUMULATIVEVALUE_H +#ifndef BORNAGAIN_CORE_INTENSITY_CUMULATIVEVALUE_H +#define BORNAGAIN_CORE_INTENSITY_CUMULATIVEVALUE_H #include "Wrap/WinDllMacros.h" @@ -45,4 +45,4 @@ private: BA_CORE_API_ bool operator<(const CumulativeValue& lhs, const CumulativeValue& rhs); BA_CORE_API_ bool operator>(const CumulativeValue& lhs, const CumulativeValue& rhs); -#endif // BORNAGAIN_CORE_INSTRUMENT_CUMULATIVEVALUE_H +#endif // BORNAGAIN_CORE_INTENSITY_CUMULATIVEVALUE_H diff --git a/Core/Intensity/IntensityDataFunctions.cpp b/Core/Intensity/IntensityDataFunctions.cpp index 7383b1d66c0..79e8583cf2f 100644 --- a/Core/Intensity/IntensityDataFunctions.cpp +++ b/Core/Intensity/IntensityDataFunctions.cpp @@ -15,10 +15,10 @@ #include "Core/Intensity/IntensityDataFunctions.h" #include "Core/Detector/ConvolutionDetectorResolution.h" #include "Core/Detector/IDetector.h" -#include "Core/Intensity/ArrayUtils.h" #include "Core/Instrument/FourierTransform.h" #include "Core/Instrument/Instrument.h" #include "Core/Instrument/SimulationResult.h" +#include "Core/Intensity/ArrayUtils.h" #include "Core/Intensity/IHistogram.h" #include "Fit/TestEngine/Numeric.h" #include <iostream> diff --git a/Core/Intensity/UnitConverter1D.cpp b/Core/Intensity/UnitConverter1D.cpp index 1d0161fefb2..5accb49720b 100644 --- a/Core/Intensity/UnitConverter1D.cpp +++ b/Core/Intensity/UnitConverter1D.cpp @@ -18,8 +18,8 @@ #include "Core/Binning/FixedBinAxis.h" #include "Core/Binning/PointwiseAxis.h" #include "Core/Instrument/AngularSpecScan.h" -#include "Core/Intensity/AxisNames.h" #include "Core/Instrument/QSpecScan.h" +#include "Core/Intensity/AxisNames.h" #include "Core/Intensity/OutputData.h" #include "Core/Parametrization/Units.h" diff --git a/Core/Multilayer/IFresnelMap.h b/Core/Multilayer/IFresnelMap.h index 08f4df4cbda..b0f6020c038 100644 --- a/Core/Multilayer/IFresnelMap.h +++ b/Core/Multilayer/IFresnelMap.h @@ -15,9 +15,9 @@ #ifndef BORNAGAIN_CORE_MULTILAYER_IFRESNELMAP_H #define BORNAGAIN_CORE_MULTILAYER_IFRESNELMAP_H -#include "Core/Multilayer/Slice.h" #include "Core/Multilayer/ILayerRTCoefficients.h" #include "Core/Multilayer/ISpecularStrategy.h" +#include "Core/Multilayer/Slice.h" #include "Core/Vector/Vectors3D.h" #include "Wrap/WinDllMacros.h" #include <memory> diff --git a/Core/Multilayer/ISpecularStrategy.h b/Core/Multilayer/ISpecularStrategy.h index 55cb9f91c03..7f8c2f568e9 100644 --- a/Core/Multilayer/ISpecularStrategy.h +++ b/Core/Multilayer/ISpecularStrategy.h @@ -15,8 +15,8 @@ #ifndef BORNAGAIN_CORE_MULTILAYER_ISPECULARSTRATEGY_H #define BORNAGAIN_CORE_MULTILAYER_ISPECULARSTRATEGY_H -#include "Core/Multilayer/Slice.h" #include "Core/Multilayer/ILayerRTCoefficients.h" +#include "Core/Multilayer/Slice.h" //! Interface for the Fresnel computations, both in the scalar and magnetic case //! diff --git a/Core/Multilayer/KzComputation.cpp b/Core/Multilayer/KzComputation.cpp index 77a20821747..bb531ee1cfd 100644 --- a/Core/Multilayer/KzComputation.cpp +++ b/Core/Multilayer/KzComputation.cpp @@ -13,9 +13,9 @@ // ************************************************************************** // #include "Core/Multilayer/KzComputation.h" -#include "Core/Multilayer/Slice.h" #include "Core/Multilayer/Layer.h" #include "Core/Multilayer/MultiLayer.h" +#include "Core/Multilayer/Slice.h" #include "Core/Parametrization/Units.h" namespace diff --git a/Core/Multilayer/MultiLayer.cpp b/Core/Multilayer/MultiLayer.cpp index b28d33897e6..e4acd6d916d 100644 --- a/Core/Multilayer/MultiLayer.cpp +++ b/Core/Multilayer/MultiLayer.cpp @@ -15,12 +15,12 @@ #include "Core/Multilayer/MultiLayer.h" #include "Core/Aggregate/ILayout.h" #include "Core/Basics/Exceptions.h" -#include "Core/Multilayer/MultiLayerUtils.h" #include "Core/Material/MaterialUtils.h" #include "Core/Multilayer/Layer.h" #include "Core/Multilayer/LayerFillLimits.h" #include "Core/Multilayer/LayerInterface.h" #include "Core/Multilayer/LayerRoughness.h" +#include "Core/Multilayer/MultiLayerUtils.h" #include "Core/Parametrization/ParameterPool.h" #include "Core/Parametrization/RealParameter.h" diff --git a/Core/Multilayer/MultiLayerUtils.h b/Core/Multilayer/MultiLayerUtils.h index cf8afb3eba5..e60b79f9b7b 100644 --- a/Core/Multilayer/MultiLayerUtils.h +++ b/Core/Multilayer/MultiLayerUtils.h @@ -12,8 +12,8 @@ // // ************************************************************************** // -#ifndef BORNAGAIN_CORE_COMPUTATION_MULTILAYERUTILS_H -#define BORNAGAIN_CORE_COMPUTATION_MULTILAYERUTILS_H +#ifndef BORNAGAIN_CORE_MULTILAYER_MULTILAYERUTILS_H +#define BORNAGAIN_CORE_MULTILAYER_MULTILAYERUTILS_H #include "Wrap/WinDllMacros.h" #include <cstddef> @@ -51,4 +51,4 @@ BA_CORE_API_ std::vector<ZLimits> ParticleRegions(const MultiLayer& multilayer, BA_CORE_API_ bool hasRoughness(const MultiLayer& sample); } // namespace MultiLayerUtils -#endif // BORNAGAIN_CORE_COMPUTATION_MULTILAYERUTILS_H +#endif // BORNAGAIN_CORE_MULTILAYER_MULTILAYERUTILS_H diff --git a/Core/Multilayer/ScalarFresnelMap.cpp b/Core/Multilayer/ScalarFresnelMap.cpp index 8e27ac7a813..244f151f617 100644 --- a/Core/Multilayer/ScalarFresnelMap.cpp +++ b/Core/Multilayer/ScalarFresnelMap.cpp @@ -13,8 +13,8 @@ // ************************************************************************** // #include "Core/Multilayer/ScalarFresnelMap.h" -#include "Core/Multilayer/Slice.h" #include "Core/Multilayer/ScalarRTCoefficients.h" +#include "Core/Multilayer/Slice.h" #include "Core/SimulationElement/SimulationElement.h" #include "Core/Vector/Vectors3D.h" #include <functional> diff --git a/Core/Multilayer/Slice.h b/Core/Multilayer/Slice.h index 98f3de025b8..c5cd17d057f 100644 --- a/Core/Multilayer/Slice.h +++ b/Core/Multilayer/Slice.h @@ -12,8 +12,8 @@ // // ************************************************************************** // -#ifndef BORNAGAIN_CORE_COMPUTATION_SLICE_H -#define BORNAGAIN_CORE_COMPUTATION_SLICE_H +#ifndef BORNAGAIN_CORE_MULTILAYER_SLICE_H +#define BORNAGAIN_CORE_MULTILAYER_SLICE_H #include "Core/Material/Material.h" #include <memory> @@ -64,4 +64,4 @@ private: std::unique_ptr<LayerRoughness> mP_top_roughness; }; -#endif // BORNAGAIN_CORE_COMPUTATION_SLICE_H +#endif // BORNAGAIN_CORE_MULTILAYER_SLICE_H diff --git a/Core/Multilayer/SlicedFormFactorList.h b/Core/Multilayer/SlicedFormFactorList.h index 1247e8a641d..e303b2a2ea5 100644 --- a/Core/Multilayer/SlicedFormFactorList.h +++ b/Core/Multilayer/SlicedFormFactorList.h @@ -12,8 +12,8 @@ // // ************************************************************************** // -#ifndef BORNAGAIN_CORE_PARTICLE_SLICEDFORMFACTORLIST_H -#define BORNAGAIN_CORE_PARTICLE_SLICEDFORMFACTORLIST_H +#ifndef BORNAGAIN_CORE_MULTILAYER_SLICEDFORMFACTORLIST_H +#define BORNAGAIN_CORE_MULTILAYER_SLICEDFORMFACTORLIST_H #include "Core/Particle/HomogeneousRegion.h" #include "Core/Scattering/IFormFactor.h" @@ -52,4 +52,4 @@ private: std::map<size_t, std::vector<HomogeneousRegion>> m_region_map; }; -#endif // BORNAGAIN_CORE_PARTICLE_SLICEDFORMFACTORLIST_H +#endif // BORNAGAIN_CORE_MULTILAYER_SLICEDFORMFACTORLIST_H diff --git a/Core/Multilayer/SpecularMagneticOldStrategy.cpp b/Core/Multilayer/SpecularMagneticOldStrategy.cpp index fb6d096edde..2727c0903e1 100644 --- a/Core/Multilayer/SpecularMagneticOldStrategy.cpp +++ b/Core/Multilayer/SpecularMagneticOldStrategy.cpp @@ -13,10 +13,10 @@ // ************************************************************************** // #include "Core/Multilayer/SpecularMagneticOldStrategy.h" -#include "Core/Multilayer/Slice.h" #include "Core/Multilayer/Layer.h" #include "Core/Multilayer/LayerInterface.h" #include "Core/Multilayer/MultiLayer.h" +#include "Core/Multilayer/Slice.h" #include "Core/Vector/WavevectorInfo.h" #include <Eigen/LU> diff --git a/Core/Multilayer/SpecularMagneticStrategy.cpp b/Core/Multilayer/SpecularMagneticStrategy.cpp index 1b42dd65ad5..c66b7c84075 100644 --- a/Core/Multilayer/SpecularMagneticStrategy.cpp +++ b/Core/Multilayer/SpecularMagneticStrategy.cpp @@ -14,8 +14,8 @@ #include "Core/Multilayer/SpecularMagneticStrategy.h" #include "Core/Basics/PhysicalConstants.h" -#include "Core/Multilayer/Slice.h" #include "Core/Multilayer/KzComputation.h" +#include "Core/Multilayer/Slice.h" namespace { diff --git a/Core/Multilayer/SpecularScalarStrategy.cpp b/Core/Multilayer/SpecularScalarStrategy.cpp index ee81d8a5607..15b00afa1e5 100644 --- a/Core/Multilayer/SpecularScalarStrategy.cpp +++ b/Core/Multilayer/SpecularScalarStrategy.cpp @@ -13,10 +13,10 @@ // ************************************************************************** // #include "Core/Multilayer/SpecularScalarStrategy.h" -#include "Core/Multilayer/Slice.h" #include "Core/Multilayer/KzComputation.h" #include "Core/Multilayer/Layer.h" #include "Core/Multilayer/LayerRoughness.h" +#include "Core/Multilayer/Slice.h" #include <Eigen/Dense> #include <stdexcept> diff --git a/Core/Parametrization/IterationStrategy.cpp b/Core/Parametrization/IterationStrategy.cpp index 8c44a8fb4e8..48aeb7793ea 100644 --- a/Core/Parametrization/IterationStrategy.cpp +++ b/Core/Parametrization/IterationStrategy.cpp @@ -14,8 +14,8 @@ #include "Core/Parametrization/IterationStrategy.h" #include "Core/Basics/Assert.h" -#include "Core/Parametrization/NodeIterator.h" #include "Core/Parametrization/INode.h" +#include "Core/Parametrization/NodeIterator.h" PreorderStrategy::PreorderStrategy() {} diff --git a/Core/Parametrization/RangedDistributions.cpp b/Core/Parametrization/RangedDistributions.cpp index 5648316ed65..6cadc4bf82b 100644 --- a/Core/Parametrization/RangedDistributions.cpp +++ b/Core/Parametrization/RangedDistributions.cpp @@ -16,6 +16,7 @@ #include "Core/Parametrization/Distributions.h" #include "Core/Parametrization/ParameterSample.h" #include "Core/PyIO/PythonFormatting.h" +#include "Core/Tools/PyFmt.h" #include <limits> namespace @@ -88,10 +89,10 @@ std::unique_ptr<IDistribution1D> RangedDistribution::distribution(double mean, d std::string RangedDistribution::print() const { std::stringstream result; - result << PythonFormatting::indent() << "distribution = " << name(); - result << "(" << m_n_samples << ", " << PythonFormatting::printDouble(m_sigma_factor); + result << pyfmt::indent() << "distribution = " << name(); + result << "(" << m_n_samples << ", " << pyfmt::printDouble(m_sigma_factor); if (!m_limits.isLimitless()) - result << PythonFormatting::printRealLimitsArg(m_limits); + result << pyfmt2::printRealLimitsArg(m_limits); result << ")"; return result.str(); } diff --git a/Core/PyIO/PythonFormatting.cpp b/Core/PyIO/PythonFormatting.cpp index 06d6da52d84..cfa64321322 100644 --- a/Core/PyIO/PythonFormatting.cpp +++ b/Core/PyIO/PythonFormatting.cpp @@ -3,7 +3,7 @@ // BornAgain: simulate and fit scattering at grazing incidence // //! @file Core/PyIO/PythonFormatting.cpp -//! @brief Implements functions from PythonFormatting namespace. +//! @brief Implements functions from namespace pyfmt2. //! //! @homepage http://www.bornagainproject.org //! @license GNU General Public License v3 or higher (see COPYING) @@ -27,149 +27,11 @@ #include "Core/Parametrization/ParameterPool.h" #include "Core/Parametrization/RealParameter.h" #include "Core/Parametrization/Units.h" +#include "Core/Tools/PyFmt.h" #include "Fit/TestEngine/Numeric.h" #include "Fit/Tools/StringUtils.h" #include <iomanip> -namespace pyfmt -{ - -std::string scriptPreamble() -{ - const std::string result = "import numpy\n" - "import bornagain as ba\n" - "from bornagain import deg, angstrom, nm, nm2, kvector_t\n\n\n"; - - return result; -} - -std::string getSampleFunctionName() -{ - return "get_sample"; -} - -std::string printBool(double value) -{ - return value ? "True" : "False"; -} - -std::string printDouble(double input) -{ - std::ostringstream inter; - inter << std::setprecision(12); - if (std::abs(input) < std::numeric_limits<double>::epsilon()) { - inter << "0.0"; - return inter.str(); - } - inter << input; - if (inter.str().find('e') == std::string::npos && inter.str().find('.') == std::string::npos) - inter << ".0"; - return inter.str(); -} - -std::string printNm(double input) -{ - std::ostringstream inter; - inter << std::setprecision(12); - inter << printDouble(input) << "*nm"; - return inter.str(); -} - -std::string printNm2(double input) -{ - std::ostringstream inter; - inter << std::setprecision(12); - inter << printDouble(input) << "*nm2"; - return inter.str(); -} - -// 1.000000e+07 -> 1.0e+07 -std::string printScientificDouble(double input) -{ - std::ostringstream inter; - inter << std::scientific; - inter << input; - - std::string::size_type pos = inter.str().find('e'); - if (pos == std::string::npos) - return inter.str(); - - std::string part1 = inter.str().substr(0, pos); - std::string part2 = inter.str().substr(pos, std::string::npos); - - part1.erase(part1.find_last_not_of('0') + 1, std::string::npos); - if (part1.back() == '.') - part1 += "0"; - - return part1 + part2; -} - -std::string printDegrees(double input) -{ - std::ostringstream inter; - inter << std::setprecision(11) << Units::rad2deg(input); - if (inter.str().find('e') == std::string::npos && inter.str().find('.') == std::string::npos) - inter << ".0"; - inter << "*deg"; - return inter.str(); -} - -std::string printValue(double value, const std::string& units) -{ - if (units == "rad") - return printDegrees(value); - else if (units == "nm") - return printNm(value); - else if (units == "") - return printDouble(value); - else - throw std::runtime_error("PythonFormatting::printValue() -> Error. Unknown units '" + units - + "'"); -} - -std::string printString(const std::string& value) -{ - std::ostringstream result; - result << "\"" << value << "\""; - return result.str(); -} - -bool isSquare(double length1, double length2, double angle) -{ - return length1 == length2 && Numeric::AreAlmostEqual(angle, M_PI_2); -} - -bool isHexagonal(double length1, double length2, double angle) -{ - return length1 == length2 && Numeric::AreAlmostEqual(angle, M_TWOPI / 3.0); -} - -std::string printKvector(const kvector_t value) -{ - std::ostringstream result; - result << "kvector_t(" << printDouble(value.x()) << ", " << printDouble(value.y()) << ", " - << printDouble(value.z()) << ")"; - return result.str(); -} - -//! returns true if it is (0, -1, 0) vector -bool isDefaultDirection(const kvector_t direction) -{ - return Numeric::AreAlmostEqual(direction.x(), 0.0) - && Numeric::AreAlmostEqual(direction.y(), -1.0) - && Numeric::AreAlmostEqual(direction.z(), 0.0); -} - -std::string indent(size_t width) -{ - return std::string(width, ' '); -} - -} // namespace pyfmt - - - - namespace pyfmt2 { @@ -192,7 +54,7 @@ std::string representShape2D(const std::string& indent, const IShape2D* ishape, } result << "]\n"; result << indent << "simulation.addMask(" - << "ba.Polygon(points), " << printBool(mask_value) << ")\n"; + << "ba.Polygon(points), " << pyfmt::printBool(mask_value) << ")\n"; } else if (dynamic_cast<const InfinitePlane*>(ishape)) { result << indent << "simulation.maskAll()\n"; @@ -203,28 +65,28 @@ std::string representShape2D(const std::string& indent, const IShape2D* ishape, << printValueFunc(shape->getCenterY()) << ", " << printValueFunc(shape->getRadiusX()) << ", " << printValueFunc(shape->getRadiusY()); if (shape->getTheta() != 0.0) - result << ", " << printDegrees(shape->getTheta()); - result << "), " << printBool(mask_value) << ")\n"; + result << ", " << pyfmt::printDegrees(shape->getTheta()); + result << "), " << pyfmt::printBool(mask_value) << ")\n"; } else if (const Rectangle* shape = dynamic_cast<const Rectangle*>(ishape)) { result << indent << "simulation.addMask("; result << "ba.Rectangle(" << printValueFunc(shape->getXlow()) << ", " << printValueFunc(shape->getYlow()) << ", " << printValueFunc(shape->getXup()) - << ", " << printValueFunc(shape->getYup()) << "), " << printBool(mask_value) + << ", " << printValueFunc(shape->getYup()) << "), " << pyfmt::printBool(mask_value) << ")\n"; } else if (const VerticalLine* shape = dynamic_cast<const VerticalLine*>(ishape)) { result << indent << "simulation.addMask("; result << "ba.VerticalLine(" << printValueFunc(shape->getXpos()) << "), " - << printBool(mask_value) << ")\n"; + << pyfmt::printBool(mask_value) << ")\n"; } else if (const HorizontalLine* shape = dynamic_cast<const HorizontalLine*>(ishape)) { result << indent << "simulation.addMask("; result << "ba.HorizontalLine(" << printValueFunc(shape->getYpos()) << "), " - << printBool(mask_value) << ")\n"; + << pyfmt::printBool(mask_value) << ")\n"; } else throw std::runtime_error("representShape2D(const IShape2D*) -> Error. Unknown shape"); @@ -237,8 +99,8 @@ std::string representShape2D(const std::string& indent, const IShape2D* ishape, std::string valueTimesUnit(const RealParameter* par) { if (par->unit() == "rad") - return printDegrees(par->value()); - return printDouble(par->value()) + (par->unit() == "" ? "" : ("*" + par->unit())); + return pyfmt::printDegrees(par->value()); + return pyfmt::printDouble(par->value()) + (par->unit() == "" ? "" : ("*" + par->unit())); } //! Returns comma-separated list of parameter values, including unit multiplicator (like "* nm"). @@ -281,16 +143,18 @@ std::string printRealLimits(const RealLimits& limits, const std::string& units) } else if (limits.isLowerLimited()) { - result << "RealLimits.lowerLimited(" << printValue(limits.lowerLimit(), units) << ")"; + result << "RealLimits.lowerLimited(" << pyfmt::printValue(limits.lowerLimit(), units) + << ")"; } else if (limits.isUpperLimited()) { - result << "RealLimits.upperLimited(" << printValue(limits.upperLimit(), units) << ")"; + result << "RealLimits.upperLimited(" << pyfmt::printValue(limits.upperLimit(), units) + << ")"; } else if (limits.isLimited()) { - result << "RealLimits.limited(" << printValue(limits.lowerLimit(), units) << ", " - << printValue(limits.upperLimit(), units) << ")"; + result << "RealLimits.limited(" << pyfmt::printValue(limits.lowerLimit(), units) << ", " + << pyfmt::printValue(limits.upperLimit(), units) << ")"; } return result.str(); @@ -318,7 +182,7 @@ std::string printParameterDistribution(const ParameterDistribution& par_distr, result << "ba.ParameterDistribution(" << "\"" << par_distr.getMainParameterName() << "\"" << ", " << distVarName << ", " << par_distr.getNbrSamples() << ", " - << printDouble(par_distr.getSigmaFactor()) + << pyfmt::printDouble(par_distr.getSigmaFactor()) << printRealLimitsArg(par_distr.getLimits(), units) << ")"; return result.str(); @@ -329,9 +193,9 @@ std::string printAxis(const IAxis& axis, const std::string& units, size_t offset std::ostringstream result; if (auto fixedAxis = dynamic_cast<const FixedBinAxis*>(&axis)) { - result << "ba.FixedBinAxis(" << printString(fixedAxis->getName()) << ", " - << fixedAxis->size() << ", " << printValue(fixedAxis->getMin(), units) << ", " - << printValue(fixedAxis->getMax(), units) << ")"; + result << "ba.FixedBinAxis(" << pyfmt::printString(fixedAxis->getName()) << ", " + << fixedAxis->size() << ", " << pyfmt::printValue(fixedAxis->getMin(), units) << ", " + << pyfmt::printValue(fixedAxis->getMax(), units) << ")"; } else if (auto pointwise_axis = dynamic_cast<const PointwiseAxis*>(&axis)) { const std::string py_def_call = "numpy.asarray(["; const size_t total_offset = offset + py_def_call.size(); @@ -339,12 +203,12 @@ std::string printAxis(const IAxis& axis, const std::string& units, size_t offset std::vector<double> points = pointwise_axis->getBinCenters(); for (auto iter = points.begin(); iter != points.end() - 1; ++iter) { - result << printValue(*iter, units) << ",\n"; - result << indent(total_offset); + result << pyfmt::printValue(*iter, units) << ",\n"; + result << pyfmt::indent(total_offset); } - result << printValue(points.back(), units) << "])"; + result << pyfmt::printValue(points.back(), units) << "])"; } else { - throw std::runtime_error("PythonFormatting::printAxis() -> Error. Unsupported axis"); + throw std::runtime_error("pyfmt2::printAxis() -> Error. Unsupported axis"); } return result.str(); diff --git a/Core/PyIO/PythonFormatting.h b/Core/PyIO/PythonFormatting.h index b20ad739a06..616a0ecb713 100644 --- a/Core/PyIO/PythonFormatting.h +++ b/Core/PyIO/PythonFormatting.h @@ -3,7 +3,7 @@ // BornAgain: simulate and fit scattering at grazing incidence // //! @file Core/PyIO/PythonFormatting.h -//! @brief Defines PythonFormatting namespace. +//! @brief Defines namespace pyfmt2. //! //! @homepage http://www.bornagainproject.org //! @license GNU General Public License v3 or higher (see COPYING) @@ -15,7 +15,7 @@ #ifndef BORNAGAIN_CORE_PYIO_PYTHONFORMATTING_H #define BORNAGAIN_CORE_PYIO_PYTHONFORMATTING_H -#include "Core/Vector/Vectors3D.h" +#include "Wrap/WinDllMacros.h" #include <functional> #include <string> @@ -29,33 +29,6 @@ class IAxis; //! Utility functions for writing Python code snippets. -namespace pyfmt -{ - -BA_CORE_API_ std::string scriptPreamble(); -BA_CORE_API_ std::string getSampleFunctionName(); - -BA_CORE_API_ std::string printInt(int value); -BA_CORE_API_ std::string printBool(double value); -BA_CORE_API_ std::string printDouble(double input); -BA_CORE_API_ std::string printNm(double input); -BA_CORE_API_ std::string printNm2(double input); -BA_CORE_API_ std::string printScientificDouble(double input); -BA_CORE_API_ std::string printDegrees(double input); -BA_CORE_API_ std::string printValue(double value, const std::string& units = ""); -BA_CORE_API_ std::string printString(const std::string& value); - -BA_CORE_API_ bool isSquare(double length1, double length2, double angle); -BA_CORE_API_ bool isHexagonal(double length1, double length2, double angle); -BA_CORE_API_ std::string printKvector(const kvector_t value); -BA_CORE_API_ bool isDefaultDirection(const kvector_t direction); - -//! Returns a string of blanks with given width. By default -//! the width equals standard offset in python files. -BA_CORE_API_ std::string indent(size_t width = 4u); - -} // namespace pyfmt - namespace pyfmt2 { diff --git a/Core/Simulation/Simulation.cpp b/Core/Simulation/Simulation.cpp index a767b4c79f3..aa18fe0ab5c 100644 --- a/Core/Simulation/Simulation.cpp +++ b/Core/Simulation/Simulation.cpp @@ -15,10 +15,10 @@ #include "Core/Simulation/Simulation.h" #include "Core/Computation/IBackground.h" #include "Core/Computation/IComputation.h" -#include "Core/Multilayer/MultiLayerUtils.h" #include "Core/Detector/DetectorFunctions.h" #include "Core/Multilayer/IMultiLayerBuilder.h" #include "Core/Multilayer/MultiLayer.h" +#include "Core/Multilayer/MultiLayerUtils.h" #include "Core/Parametrization/ParameterPool.h" #include "Core/Parametrization/ParameterSample.h" #include "Core/Simulation/MPISimulation.h" diff --git a/Core/Tools/PyFmt.cpp b/Core/Tools/PyFmt.cpp new file mode 100644 index 00000000000..44aadad3ac4 --- /dev/null +++ b/Core/Tools/PyFmt.cpp @@ -0,0 +1,156 @@ +// ************************************************************************** // +// +// BornAgain: simulate and fit scattering at grazing incidence +// +//! @file Core/PyIO/PythonFormatting.cpp +//! @brief Implements functions from namespace pyfmt. +//! +//! @homepage http://www.bornagainproject.org +//! @license GNU General Public License v3 or higher (see COPYING) +//! @copyright Forschungszentrum Jülich GmbH 2018 +//! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS) +// +// ************************************************************************** // + +#include "Core/Tools/PyFmt.h" +#include "Core/Basics/MathConstants.h" +#include "Fit/TestEngine/Numeric.h" +#include "Fit/Tools/StringUtils.h" +#include "Core/Parametrization/Units.h" // printDegrees +#include <iomanip> + +namespace pyfmt +{ + +std::string scriptPreamble() +{ + const std::string result = "import numpy\n" + "import bornagain as ba\n" + "from bornagain import deg, angstrom, nm, nm2, kvector_t\n\n\n"; + + return result; +} + +std::string getSampleFunctionName() +{ + return "get_sample"; +} + +std::string printBool(double value) +{ + return value ? "True" : "False"; +} + +std::string printDouble(double input) +{ + std::ostringstream inter; + inter << std::setprecision(12); + if (std::abs(input) < std::numeric_limits<double>::epsilon()) { + inter << "0.0"; + return inter.str(); + } + inter << input; + if (inter.str().find('e') == std::string::npos && inter.str().find('.') == std::string::npos) + inter << ".0"; + return inter.str(); +} + +std::string printNm(double input) +{ + std::ostringstream inter; + inter << std::setprecision(12); + inter << printDouble(input) << "*nm"; + return inter.str(); +} + +std::string printNm2(double input) +{ + std::ostringstream inter; + inter << std::setprecision(12); + inter << printDouble(input) << "*nm2"; + return inter.str(); +} + +// 1.000000e+07 -> 1.0e+07 +std::string printScientificDouble(double input) +{ + std::ostringstream inter; + inter << std::scientific; + inter << input; + + std::string::size_type pos = inter.str().find('e'); + if (pos == std::string::npos) + return inter.str(); + + std::string part1 = inter.str().substr(0, pos); + std::string part2 = inter.str().substr(pos, std::string::npos); + + part1.erase(part1.find_last_not_of('0') + 1, std::string::npos); + if (part1.back() == '.') + part1 += "0"; + + return part1 + part2; +} + +std::string printDegrees(double input) +{ + std::ostringstream inter; + inter << std::setprecision(11) << Units::rad2deg(input); + if (inter.str().find('e') == std::string::npos && inter.str().find('.') == std::string::npos) + inter << ".0"; + inter << "*deg"; + return inter.str(); +} + +std::string printValue(double value, const std::string& units) +{ + if (units == "rad") + return printDegrees(value); + else if (units == "nm") + return printNm(value); + else if (units == "") + return printDouble(value); + else + throw std::runtime_error("pyfmt::printValue() -> Error. Unknown units '" + units + + "'"); +} + +std::string printString(const std::string& value) +{ + std::ostringstream result; + result << "\"" << value << "\""; + return result.str(); +} + +bool isSquare(double length1, double length2, double angle) +{ + return length1 == length2 && Numeric::AreAlmostEqual(angle, M_PI_2); +} + +bool isHexagonal(double length1, double length2, double angle) +{ + return length1 == length2 && Numeric::AreAlmostEqual(angle, M_TWOPI / 3.0); +} + +std::string printKvector(const kvector_t value) +{ + std::ostringstream result; + result << "kvector_t(" << printDouble(value.x()) << ", " << printDouble(value.y()) << ", " + << printDouble(value.z()) << ")"; + return result.str(); +} + +//! returns true if it is (0, -1, 0) vector +bool isDefaultDirection(const kvector_t direction) +{ + return Numeric::AreAlmostEqual(direction.x(), 0.0) + && Numeric::AreAlmostEqual(direction.y(), -1.0) + && Numeric::AreAlmostEqual(direction.z(), 0.0); +} + +std::string indent(size_t width) +{ + return std::string(width, ' '); +} + +} // namespace pyfmt diff --git a/Core/Tools/PyFmt.h b/Core/Tools/PyFmt.h new file mode 100644 index 00000000000..6b182881282 --- /dev/null +++ b/Core/Tools/PyFmt.h @@ -0,0 +1,51 @@ +// ************************************************************************** // +// +// BornAgain: simulate and fit scattering at grazing incidence +// +//! @file Core/PyIO/PythonFormatting.h +//! @brief Defines namespace pyfmt. +//! +//! @homepage http://www.bornagainproject.org +//! @license GNU General Public License v3 or higher (see COPYING) +//! @copyright Forschungszentrum Jülich GmbH 2018 +//! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS) +// +// ************************************************************************** // + +#ifndef BORNAGAIN_CORE_TOOLS_PYFMT_H +#define BORNAGAIN_CORE_TOOLS_PYFMT_H + +#include "Wrap/WinDllMacros.h" +#include "Core/Vector/Vectors3D.h" +#include <string> + +//! Utility functions for writing Python code snippets. + +namespace pyfmt +{ + +BA_CORE_API_ std::string scriptPreamble(); +BA_CORE_API_ std::string getSampleFunctionName(); + +BA_CORE_API_ std::string printInt(int value); +BA_CORE_API_ std::string printBool(double value); +BA_CORE_API_ std::string printDouble(double input); +BA_CORE_API_ std::string printNm(double input); +BA_CORE_API_ std::string printNm2(double input); +BA_CORE_API_ std::string printScientificDouble(double input); +BA_CORE_API_ std::string printDegrees(double input); +BA_CORE_API_ std::string printValue(double value, const std::string& units = ""); +BA_CORE_API_ std::string printString(const std::string& value); + +BA_CORE_API_ bool isSquare(double length1, double length2, double angle); +BA_CORE_API_ bool isHexagonal(double length1, double length2, double angle); +BA_CORE_API_ std::string printKvector(const kvector_t value); +BA_CORE_API_ bool isDefaultDirection(const kvector_t direction); + +//! Returns a string of blanks with given width. By default +//! the width equals standard offset in python files. +BA_CORE_API_ std::string indent(size_t width = 4u); + +} // namespace pyfmt + +#endif // BORNAGAIN_CORE_TOOLS_PYFMT_H diff --git a/GUI/coregui/Models/GUIDomainSampleVisitor.cpp b/GUI/coregui/Models/GUIDomainSampleVisitor.cpp index 233ba7673bf..a3ea26c64c0 100644 --- a/GUI/coregui/Models/GUIDomainSampleVisitor.cpp +++ b/GUI/coregui/Models/GUIDomainSampleVisitor.cpp @@ -14,10 +14,10 @@ #include "GUI/coregui/Models/GUIDomainSampleVisitor.h" #include "Core/Aggregate/ParticleLayout.h" -#include "Core/Multilayer/MultiLayerUtils.h" #include "Core/Material/Material.h" #include "Core/Multilayer/Layer.h" #include "Core/Multilayer/MultiLayer.h" +#include "Core/Multilayer/MultiLayerUtils.h" #include "Core/Parametrization/Units.h" #include "Core/Particle/Crystal.h" #include "Core/Particle/MesoCrystal.h" diff --git a/GUI/coregui/Views/IntensityDataWidgets/PlotEventInfo.cpp b/GUI/coregui/Views/IntensityDataWidgets/PlotEventInfo.cpp index 3259da1eeec..75b9c9c7200 100644 --- a/GUI/coregui/Views/IntensityDataWidgets/PlotEventInfo.cpp +++ b/GUI/coregui/Views/IntensityDataWidgets/PlotEventInfo.cpp @@ -13,7 +13,7 @@ // ************************************************************************** // #include "GUI/coregui/Views/IntensityDataWidgets/PlotEventInfo.h" -#include "Core/PyIO/PythonFormatting.h" +#include "Core/Tools/PyFmt.h" #include "GUI/coregui/utils/GUIHelpers.h" PlotEventInfo::PlotEventInfo(PLOT_TYPE type) @@ -43,6 +43,6 @@ QString PlotEventInfo::statusString() const QString PlotEventInfo::valueToString() const { return m_info_type == PLOT_TYPE::Plot1D || m_log_valued_axis - ? QString::fromStdString(PythonFormatting::printScientificDouble(m_value)) + ? QString::fromStdString(pyfmt::printScientificDouble(m_value)) : QString::number(m_value, 'f', 2); } diff --git a/GUI/coregui/Views/IntensityDataWidgets/SaveProjectionsAssistant.cpp b/GUI/coregui/Views/IntensityDataWidgets/SaveProjectionsAssistant.cpp index f631314c8fd..495d6185a00 100644 --- a/GUI/coregui/Views/IntensityDataWidgets/SaveProjectionsAssistant.cpp +++ b/GUI/coregui/Views/IntensityDataWidgets/SaveProjectionsAssistant.cpp @@ -15,7 +15,7 @@ #include "GUI/coregui/Views/IntensityDataWidgets/SaveProjectionsAssistant.h" #include "Core/Intensity/Histogram1D.h" #include "Core/Intensity/Histogram2D.h" -#include "Core/PyIO/PythonFormatting.h" +#include "Core/Tools/PyFmt.h" #include "GUI/coregui/Models/IntensityDataItem.h" #include "GUI/coregui/Models/MaskItems.h" #include "GUI/coregui/Models/ProjectionItems.h" @@ -31,13 +31,13 @@ const int bin_values_colwidth = 20; QString to_scientific_str(double value) { - auto str = PythonFormatting::printScientificDouble(value); + auto str = pyfmt::printScientificDouble(value); return QString("%1").arg(QString::fromStdString(str), -bin_values_colwidth); } QString to_double_str(double value) { - auto str = PythonFormatting::printDouble(value); + auto str = pyfmt::printDouble(value); return QString("%1").arg(QString::fromStdString(str), -bin_centers_colwidth); } } // unnamed namespace diff --git a/Tests/Functional/Python/PyEmbedded/Tests.cpp b/Tests/Functional/Python/PyEmbedded/Tests.cpp index 272c63130d1..2e042601257 100644 --- a/Tests/Functional/Python/PyEmbedded/Tests.cpp +++ b/Tests/Functional/Python/PyEmbedded/Tests.cpp @@ -17,8 +17,8 @@ #include "Core/Multilayer/MultiLayer.h" #include "Core/PyIO/PyEmbeddedUtils.h" #include "Core/PyIO/PyImport.h" -#include "Core/PyIO/PythonFormatting.h" #include "Core/StandardSamples/SampleBuilderFactory.h" +#include "Core/Tools/PyFmt.h" #include "Core/Tools/PythonCore.h" #include "Core/Tools/SysUtils.h" #include "Tests/GTestWrapper/google_test.h" @@ -375,10 +375,10 @@ TEST_F(PyEmbedded, ExportToPythonAndBack) auto code = ExportToPython::generateSampleCode(*sample); std::stringstream snippet; - snippet << PythonFormatting::scriptPreamble() << code; + snippet << pyfmt::scriptPreamble() << code; - auto multilayer = PyImport::createFromPython( - snippet.str(), PythonFormatting::getSampleFunctionName(), BABuild::buildLibDir()); + auto multilayer = PyImport::createFromPython(snippet.str(), pyfmt::getSampleFunctionName(), + BABuild::buildLibDir()); auto new_code = ExportToPython::generateSampleCode(*multilayer); EXPECT_TRUE(code == new_code); diff --git a/Tests/UnitTests/Core/Detector/SpecularDetector1DTest.cpp b/Tests/UnitTests/Core/Detector/SpecularDetector1DTest.cpp index 0105f71b749..4504912466c 100644 --- a/Tests/UnitTests/Core/Detector/SpecularDetector1DTest.cpp +++ b/Tests/UnitTests/Core/Detector/SpecularDetector1DTest.cpp @@ -1,8 +1,8 @@ #include "Core/Detector/SpecularDetector1D.h" #include "Core/Beam/Beam.h" #include "Core/Binning/FixedBinAxis.h" -#include "Core/Intensity/OutputData.h" #include "Core/Detector/SimulationArea.h" +#include "Core/Intensity/OutputData.h" #include "Core/Parametrization/Units.h" #include "Tests/GTestWrapper/google_test.h" #include <memory> diff --git a/Tests/UnitTests/Core/Detector/SphericalDetectorTest.cpp b/Tests/UnitTests/Core/Detector/SphericalDetectorTest.cpp index df2e715bf57..11c23f8f91a 100644 --- a/Tests/UnitTests/Core/Detector/SphericalDetectorTest.cpp +++ b/Tests/UnitTests/Core/Detector/SphericalDetectorTest.cpp @@ -6,8 +6,8 @@ #include "Core/Detector/DetectorFunctions.h" #include "Core/Detector/RegionOfInterest.h" #include "Core/Detector/ResolutionFunction2DGaussian.h" -#include "Core/Intensity/OutputData.h" #include "Core/Detector/SimulationArea.h" +#include "Core/Intensity/OutputData.h" #include "Core/Mask/Polygon.h" #include "Core/Mask/Rectangle.h" #include "Core/Parametrization/Units.h" diff --git a/Tests/UnitTests/Core/ExportToPython/PythonFormattingTest.cpp b/Tests/UnitTests/Core/ExportToPython/PythonFormattingTest.cpp index cb732799f17..7430076ae52 100644 --- a/Tests/UnitTests/Core/ExportToPython/PythonFormattingTest.cpp +++ b/Tests/UnitTests/Core/ExportToPython/PythonFormattingTest.cpp @@ -4,6 +4,7 @@ #include "Core/Parametrization/Distributions.h" #include "Core/Parametrization/ParameterDistribution.h" #include "Core/Parametrization/Units.h" +#include "Core/Tools/PyFmt.h" #include "Fit/Tools/RealLimits.h" #include "Tests/GTestWrapper/google_test.h" @@ -13,63 +14,61 @@ class PythonFormattingTest : public ::testing::Test TEST_F(PythonFormattingTest, ValueTimesUnits) { - EXPECT_EQ("2.0*nm", PythonFormatting::printValue(2.0, "nm")); - EXPECT_EQ("2.0*deg", PythonFormatting::printValue(2.0 * Units::deg, "rad")); - EXPECT_EQ("2.0", PythonFormatting::printValue(2.0, "")); + EXPECT_EQ("2.0*nm", pyfmt::printValue(2.0, "nm")); + EXPECT_EQ("2.0*deg", pyfmt::printValue(2.0 * Units::deg, "rad")); + EXPECT_EQ("2.0", pyfmt::printValue(2.0, "")); } TEST_F(PythonFormattingTest, RealLimits) { - EXPECT_EQ("RealLimits.positive()", PythonFormatting::printRealLimits(RealLimits::positive())); + EXPECT_EQ("RealLimits.positive()", pyfmt2::printRealLimits(RealLimits::positive())); - EXPECT_EQ("RealLimits.nonnegative()", - PythonFormatting::printRealLimits(RealLimits::nonnegative())); + EXPECT_EQ("RealLimits.nonnegative()", pyfmt2::printRealLimits(RealLimits::nonnegative())); EXPECT_EQ("RealLimits.lowerLimited(1.0)", - PythonFormatting::printRealLimits(RealLimits::lowerLimited(1.0))); + pyfmt2::printRealLimits(RealLimits::lowerLimited(1.0))); EXPECT_EQ("RealLimits.lowerLimited(1.0*nm)", - PythonFormatting::printRealLimits(RealLimits::lowerLimited(1.0), "nm")); + pyfmt2::printRealLimits(RealLimits::lowerLimited(1.0), "nm")); EXPECT_EQ("RealLimits.lowerLimited(1.0*deg)", - PythonFormatting::printRealLimits(RealLimits::lowerLimited(1.0 * Units::deg), "rad")); + pyfmt2::printRealLimits(RealLimits::lowerLimited(1.0 * Units::deg), "rad")); EXPECT_EQ("RealLimits.upperLimited(1.0)", - PythonFormatting::printRealLimits(RealLimits::upperLimited(1.0))); + pyfmt2::printRealLimits(RealLimits::upperLimited(1.0))); EXPECT_EQ("RealLimits.upperLimited(1.0*nm)", - PythonFormatting::printRealLimits(RealLimits::upperLimited(1.0), "nm")); + pyfmt2::printRealLimits(RealLimits::upperLimited(1.0), "nm")); EXPECT_EQ("RealLimits.upperLimited(1.0*deg)", - PythonFormatting::printRealLimits(RealLimits::upperLimited(1.0 * Units::deg), "rad")); + pyfmt2::printRealLimits(RealLimits::upperLimited(1.0 * Units::deg), "rad")); EXPECT_EQ("RealLimits.limited(1.0, 2.0)", - PythonFormatting::printRealLimits(RealLimits::limited(1.0, 2.0))); + pyfmt2::printRealLimits(RealLimits::limited(1.0, 2.0))); EXPECT_EQ("RealLimits.limited(1.0*nm, 2.0*nm)", - PythonFormatting::printRealLimits(RealLimits::limited(1.0, 2.0), "nm")); - EXPECT_EQ("RealLimits.limited(1.0*deg, 2.0*deg)", - PythonFormatting::printRealLimits( - RealLimits::limited(1.0 * Units::deg, 2.0 * Units::deg), "rad")); + pyfmt2::printRealLimits(RealLimits::limited(1.0, 2.0), "nm")); + EXPECT_EQ( + "RealLimits.limited(1.0*deg, 2.0*deg)", + pyfmt2::printRealLimits(RealLimits::limited(1.0 * Units::deg, 2.0 * Units::deg), "rad")); // testing printing of real limits in the context of ParameterDistribution and similar - EXPECT_EQ(", ba.RealLimits.limited(1.0*deg, 2.0*deg)", - PythonFormatting::printRealLimitsArg( - RealLimits::limited(1.0 * Units::deg, 2.0 * Units::deg), "rad")); + EXPECT_EQ( + ", ba.RealLimits.limited(1.0*deg, 2.0*deg)", + pyfmt2::printRealLimitsArg(RealLimits::limited(1.0 * Units::deg, 2.0 * Units::deg), "rad")); - EXPECT_EQ("", PythonFormatting::printRealLimitsArg(RealLimits::limitless())); + EXPECT_EQ("", pyfmt2::printRealLimitsArg(RealLimits::limitless())); } TEST_F(PythonFormattingTest, printDistribution) { - EXPECT_EQ(PythonFormatting::printDistribution(DistributionGate(1.0, 2.0)), + EXPECT_EQ(pyfmt2::printDistribution(DistributionGate(1.0, 2.0)), "ba.DistributionGate(1.0, 2.0)"); - EXPECT_EQ(PythonFormatting::printDistribution(DistributionGate(1.0, 2.0), "nm"), + EXPECT_EQ(pyfmt2::printDistribution(DistributionGate(1.0, 2.0), "nm"), "ba.DistributionGate(1.0*nm, 2.0*nm)"); - EXPECT_EQ(PythonFormatting::printDistribution( - DistributionGate(1.0 * Units::deg, 2.0 * Units::deg), "rad"), - "ba.DistributionGate(1.0*deg, 2.0*deg)"); - EXPECT_EQ( - PythonFormatting::printDistribution(DistributionLogNormal(1.0 * Units::deg, 0.01), "rad"), - "ba.DistributionLogNormal(1.0*deg, 0.01)"); + pyfmt2::printDistribution(DistributionGate(1.0 * Units::deg, 2.0 * Units::deg), "rad"), + "ba.DistributionGate(1.0*deg, 2.0*deg)"); + + EXPECT_EQ(pyfmt2::printDistribution(DistributionLogNormal(1.0 * Units::deg, 0.01), "rad"), + "ba.DistributionLogNormal(1.0*deg, 0.01)"); } TEST_F(PythonFormattingTest, printParameterDistribution) @@ -79,25 +78,25 @@ TEST_F(PythonFormattingTest, printParameterDistribution) ParameterDistribution dist("ParName", gate, 5, 2.0); // No RealLimits defined - EXPECT_EQ(PythonFormatting::printParameterDistribution(dist, "distr_1"), + EXPECT_EQ(pyfmt2::printParameterDistribution(dist, "distr_1"), "ba.ParameterDistribution(\"ParName\", distr_1, 5, 2.0)"); // RealLimits defined, units unknown ParameterDistribution dist2("ParName", gate, 5, 2.0, RealLimits::limited(1.0, 2.0)); - EXPECT_EQ(PythonFormatting::printParameterDistribution(dist2, "distr_1"), + EXPECT_EQ(pyfmt2::printParameterDistribution(dist2, "distr_1"), "ba.ParameterDistribution(\"ParName\", distr_1, " "5, 2.0, ba.RealLimits.limited(1.0, 2.0))"); // RealLimits defined, units explicetely set ParameterDistribution dist3("ParName", gate, 5, 2.0, RealLimits::limited(1.0, 2.0)); - EXPECT_EQ(PythonFormatting::printParameterDistribution(dist3, "distr_1", "nm"), + EXPECT_EQ(pyfmt2::printParameterDistribution(dist3, "distr_1", "nm"), "ba.ParameterDistribution(\"ParName\", distr_1, " "5, 2.0, ba.RealLimits.limited(1.0*nm, 2.0*nm))"); // RealLimits defined, checking that method guess radians units correctly ParameterDistribution dist4("/Particle/ZRotation/Angle", gate, 5, 2.0, RealLimits::limited(1.0 * Units::deg, 2.0 * Units::deg)); - EXPECT_EQ(PythonFormatting::printParameterDistribution(dist4, "distr_1", "rad"), + EXPECT_EQ(pyfmt2::printParameterDistribution(dist4, "distr_1", "rad"), "ba.ParameterDistribution(\"/Particle/ZRotation/Angle\", " "distr_1, 5, 2.0, ba.RealLimits.limited(1.0*deg, 2.0*deg))"); } @@ -105,16 +104,15 @@ TEST_F(PythonFormattingTest, printParameterDistribution) TEST_F(PythonFormattingTest, printAxis) { FixedBinAxis axis1("axis0", 10, -1.0, 2.0); - EXPECT_EQ(PythonFormatting::printAxis(axis1, "", 0), - "ba.FixedBinAxis(\"axis0\", 10, -1.0, 2.0)"); + EXPECT_EQ(pyfmt2::printAxis(axis1, "", 0), "ba.FixedBinAxis(\"axis0\", 10, -1.0, 2.0)"); FixedBinAxis axis2("axis0", 10, -1.0 * Units::deg, 2.0 * Units::deg); - EXPECT_EQ(PythonFormatting::printAxis(axis2, "rad", 0), + EXPECT_EQ(pyfmt2::printAxis(axis2, "rad", 0), "ba.FixedBinAxis(\"axis0\", 10, -1.0*deg, 2.0*deg)"); PointwiseAxis axis3("axis3", std::vector<double>{1.0 * Units::deg, 2.0 * Units::deg, 3.0 * Units::deg}); - EXPECT_EQ(PythonFormatting::printAxis(axis3, "rad", 0), "numpy.asarray([1.0*deg,\n" - " 2.0*deg,\n" - " 3.0*deg])"); + EXPECT_EQ(pyfmt2::printAxis(axis3, "rad", 0), "numpy.asarray([1.0*deg,\n" + " 2.0*deg,\n" + " 3.0*deg])"); } diff --git a/Tests/UnitTests/Core/Sample/MultiLayerTest.cpp b/Tests/UnitTests/Core/Sample/MultiLayerTest.cpp index 9bf722a589f..860a10ec43b 100644 --- a/Tests/UnitTests/Core/Sample/MultiLayerTest.cpp +++ b/Tests/UnitTests/Core/Sample/MultiLayerTest.cpp @@ -1,11 +1,11 @@ #include "Core/Multilayer/MultiLayer.h" #include "Core/Aggregate/ParticleLayout.h" #include "Core/Basics/MathConstants.h" -#include "Core/Multilayer/MultiLayerUtils.h" #include "Core/Material/MaterialFactoryFuncs.h" #include "Core/Multilayer/Layer.h" #include "Core/Multilayer/LayerInterface.h" #include "Core/Multilayer/LayerRoughness.h" +#include "Core/Multilayer/MultiLayerUtils.h" #include "Core/Parametrization/Units.h" #include "Tests/GTestWrapper/google_test.h" diff --git a/Tests/UnitTests/Core/Sample/MultilayerAveragingTest.cpp b/Tests/UnitTests/Core/Sample/MultilayerAveragingTest.cpp index 48aeb2561a4..eea8d338fb7 100644 --- a/Tests/UnitTests/Core/Sample/MultilayerAveragingTest.cpp +++ b/Tests/UnitTests/Core/Sample/MultilayerAveragingTest.cpp @@ -2,11 +2,11 @@ #include "Core/Aggregate/ParticleLayout.h" #include "Core/Basics/MathConstants.h" #include "Core/Computation/ProcessedSample.h" -#include "Core/Multilayer/Slice.h" #include "Core/HardParticle/FormFactorCylinder.h" #include "Core/Material/MaterialFactoryFuncs.h" #include "Core/Multilayer/Layer.h" #include "Core/Multilayer/MultiLayer.h" +#include "Core/Multilayer/Slice.h" #include "Core/Parametrization/SimulationOptions.h" #include "Core/Particle/Particle.h" #include "Tests/GTestWrapper/google_test.h" -- GitLab