Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion RATapi/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ def make_problem(project: RATapi.Project) -> ProblemDefinition:
problem.numberOfContrasts = len(project.contrasts)
problem.geometry = project.geometry
problem.useImaginary = project.absorption
problem.repeatLayers = [[0, 1]] * len(project.contrasts) # This is marked as "to do" in RAT
problem.repeatLayers = [1] * len(project.contrasts)
problem.contrastBackgroundParams = contrast_background_params
problem.contrastBackgroundTypes = contrast_background_types
problem.contrastBackgroundActions = [contrast.background_action for contrast in project.contrasts]
Expand Down
4 changes: 2 additions & 2 deletions RATapi/outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ class BayesResults(Results):
def make_results(
procedure: Procedures,
output_results: RATapi.rat_core.OutputResult,
bayes_results: Optional[RATapi.rat_core.BayesResults] = None,
bayes_results: Optional[RATapi.rat_core.OutputBayesResult] = None,
) -> Union[Results, BayesResults]:
"""Initialise a python Results or BayesResults object using the outputs from a RAT calculation.

Expand All @@ -419,7 +419,7 @@ def make_results(
The procedure used by the calculation.
output_results : RATapi.rat_core.OutputResult
The C++ output results from the calculation.
bayes_results : Optional[RATapi.rat_core.BayesResults]
bayes_results : Optional[RATapi.rat_core.OutputBayesResult]
The optional extra C++ Bayesian output results from a Bayesian calculation.

Returns
Expand Down
3 changes: 2 additions & 1 deletion RATapi/utils/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,9 +549,10 @@ def convert_parameters(
# scipy.io.savemat doesn't do cells properly:
# https://github.com/scipy/scipy/issues/3756
# rather than fiddling we just use matlab
eng = wrappers.start_matlab().result()
eng = wrappers.start_matlab()
if eng is None:
raise ImportError("matlabengine is not installed.")
eng = eng.result()
eng.workspace["problem"] = r1
eng.save(str(filename), "problem", nargout=0)
eng.exit()
Expand Down
6 changes: 3 additions & 3 deletions RATapi/utils/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import RATapi
import RATapi.inputs
import RATapi.outputs
from RATapi.rat_core import PlotEventData, makeSLDProfileXY
from RATapi.rat_core import PlotEventData, makeSLDProfile


def plot_errorbars(ax: Axes, x: np.ndarray, y: np.ndarray, err: np.ndarray, one_sided: bool, color: str):
Expand Down Expand Up @@ -154,11 +154,11 @@ def plot_ref_sld_helper(
layer = data.resampledLayers[i][j]
if layers.shape[1] == 4:
layer = np.delete(layer, 2, 1)
new_profile = makeSLDProfileXY(
new_profile = makeSLDProfile(
layers[0, 1], # Bulk In
layers[-1, 1], # Bulk Out
data.subRoughs[i], # roughness
layer,
data.subRoughs[i], # roughness
1,
)

Expand Down
6 changes: 3 additions & 3 deletions cpp/includes/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,12 @@ struct DreamOutput
py::array_t<real_T> CR;
};

const std::string docsBayesResults = R"(The Python binding for the C++ bayesResults struct.
const std::string docsOutputBayesResult = R"(The Python binding for the C++ bayesResults struct.
The results of a Bayesian RAT calculation.

Parameters
----------
predictionIntervals : RATapi.rat_core.orePredictionIntervals
predictionIntervals : RATapi.rat_core.PredictionIntervals
The prediction intervals.
confidenceIntervals : RATapi.rat_core.ConfidenceIntervals
The 65% and 95% confidence intervals for the best fit results.
Expand All @@ -282,7 +282,7 @@ chain : np.ndarray
The ``i``'th column of the array contains the chain for parameter ``fitNames[i]``.
)";

struct BayesResults
struct OutputBayesResult
{
PredictionIntervals predictionIntervals;
ConfidenceIntervals confidenceIntervals;
Expand Down
64 changes: 31 additions & 33 deletions cpp/rat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ setup_pybind11(cfg)
#include "RAT/RATMain_initialize.h"
#include "RAT/RATMain_terminate.h"
#include "RAT/RATMain_types.h"
#include "RAT/makeSLDProfileXY.h"
#include "RAT/makeSLDProfile.h"
#include "RAT/dylib.hpp"
#include "RAT/events/eventManager.h"
#include "includes/defines.h"
Expand Down Expand Up @@ -263,7 +263,7 @@ RAT::b_ProblemDefinition createProblemDefinitionStruct(const ProblemDefinition&
problem_struct.numberOfContrasts = problem.numberOfContrasts;
stringToRatBoundedArray(problem.geometry, problem_struct.geometry.data, problem_struct.geometry.size);
problem_struct.useImaginary = problem.useImaginary;
problem_struct.repeatLayers = customCaller("Problem.repeatLayers", pyListToRatCellWrap2, problem.repeatLayers);
problem_struct.repeatLayers = customCaller("Problem.repeatLayers", pyArrayToRatRowArray1d, problem.repeatLayers);
problem_struct.contrastBackgroundParams = customCaller("Problem.contrastBackgroundParams", pyListToRatCellWrap3, problem.contrastBackgroundParams);
problem_struct.contrastBackgroundTypes = customCaller("Problem.contrastBackgroundTypes", pyListToRatCellWrap02d, problem.contrastBackgroundTypes);
problem_struct.contrastBackgroundActions = customCaller("Problem.contrastBackgroundActions", pyListToRatCellWrap02d, problem.contrastBackgroundActions);
Expand Down Expand Up @@ -460,7 +460,7 @@ ProblemDefinition problemDefinitionFromStruct(const RAT::b_ProblemDefinition pro
problem_def.numberOfContrasts = problem.numberOfContrasts;
stringFromRatBoundedArray(problem.geometry.data, problem.geometry.size, problem_def.geometry);
problem_def.useImaginary = problem.useImaginary;
problem_def.repeatLayers = pyListFromRatCellWrap2(problem.repeatLayers);
problem_def.repeatLayers = pyArrayFromRatArray1d<coder::array<real_T, 2U>>(problem.repeatLayers);
problem_def.contrastBackgroundParams = pyListFromBoundedCellWrap<coder::array<RAT::cell_wrap_3, 2U>>(problem.contrastBackgroundParams);
problem_def.contrastBackgroundTypes = pyListFromRatCellWrap02d(problem.contrastBackgroundTypes);
problem_def.contrastBackgroundActions = pyListFromRatCellWrap02d(problem.contrastBackgroundActions);
Expand Down Expand Up @@ -510,9 +510,9 @@ ProblemDefinition problemDefinitionFromStruct(const RAT::b_ProblemDefinition pro
return problem_def;
}

BayesResults bayesResultsFromStruct(const RAT::BayesResults results)
OutputBayesResult OutputBayesResultsFromStruct(const RAT::BayesResults results)
{
BayesResults bayesResults;
OutputBayesResult bayesResults;

bayesResults.chain = pyArrayFromRatArray2d(results.chain);

Expand Down Expand Up @@ -575,7 +575,7 @@ out_problem_def : Rat.rat_core.ProblemDefinition
The project input with the updated fit values.
results : Rat.rat_core.OutputResult
The results from a RAT calculation.
bayes_result : Rat.rat_core.BayesResults
bayes_result : Rat.rat_core.OutputBayesResult
The extra results if RAT calculation is Bayesian.
)";

Expand All @@ -593,21 +593,21 @@ py::tuple RATMain(const ProblemDefinition& problem_def, const Control& control)
out_problem_def.customFiles = problem_def.customFiles.attr("copy")();
return py::make_tuple(out_problem_def,
OutputResultFromStruct(results),
bayesResultsFromStruct(bayesResults));
OutputBayesResultsFromStruct(bayesResults));
}

const std::string docsMakeSLDProfileXY = R"(Creates the profiles for the SLD plots
const std::string docsMakeSLDProfile = R"(Creates the profiles for the SLD plots

Parameters
----------
bulk_in : float
Bulk in value for contrast.
bulk_out : float
Bulk out value for contrast.
ssub : float
Substrate roughness.
layers : np.ndarray[np.float]
Array of parameters for each layer in the contrast.
ssub : float
Substrate roughness.
number_of_repeats : int, default: 1
Number of times the layers are repeated.

Expand All @@ -617,22 +617,20 @@ sld_profile : np.ndarray[np.float]
Computed SLD profile
)";

py::array_t<real_T> makeSLDProfileXY(real_T bulk_in,
real_T bulk_out,
real_T ssub,
const py::array_t<real_T> &layers,
int number_of_repeats=DEFAULT_NREPEATS)
py::array_t<real_T> makeSLDProfile(real_T bulk_in,
real_T bulk_out,
const py::array_t<real_T> &layers,
real_T ssub,
int number_of_repeats=DEFAULT_NREPEATS)
{
coder::array<real_T, 2U> out;
coder::array<real_T, 2U> layers_array = pyArrayToRatArray2d(layers);
py::buffer_info buffer_info = layers.request();
RAT::makeSLDProfileXY(bulk_in,
bulk_out,
ssub,
layers_array,
buffer_info.shape[0],
number_of_repeats,
out);
RAT::makeSLDProfile(bulk_in,
bulk_out,
layers_array,
ssub,
number_of_repeats,
out);

return pyArrayFromRatArray2d(out);

Expand Down Expand Up @@ -787,14 +785,14 @@ PYBIND11_MODULE(rat_core, m) {
.def_readwrite("R_stat", &DreamOutput::R_stat)
.def_readwrite("CR", &DreamOutput::CR);

py::class_<BayesResults>(m, "BayesResults", docsBayesResults.c_str())
py::class_<OutputBayesResult>(m, "OutputBayesResult", docsOutputBayesResult.c_str())
.def(py::init<>())
.def_readwrite("predictionIntervals", &BayesResults::predictionIntervals)
.def_readwrite("confidenceIntervals", &BayesResults::confidenceIntervals)
.def_readwrite("dreamParams", &BayesResults::dreamParams)
.def_readwrite("dreamOutput", &BayesResults::dreamOutput)
.def_readwrite("nestedSamplerOutput", &BayesResults::nestedSamplerOutput)
.def_readwrite("chain", &BayesResults::chain);
.def_readwrite("predictionIntervals", &OutputBayesResult::predictionIntervals)
.def_readwrite("confidenceIntervals", &OutputBayesResult::confidenceIntervals)
.def_readwrite("dreamParams", &OutputBayesResult::dreamParams)
.def_readwrite("dreamOutput", &OutputBayesResult::dreamOutput)
.def_readwrite("nestedSamplerOutput", &OutputBayesResult::nestedSamplerOutput)
.def_readwrite("chain", &OutputBayesResult::chain);

py::class_<Calculation>(m, "Calculation", docsCalculation.c_str())
.def(py::init<>())
Expand Down Expand Up @@ -1047,7 +1045,7 @@ PYBIND11_MODULE(rat_core, m) {
p.numberOfContrasts = t[6].cast<real_T>();
p.geometry = t[7].cast<std::string>();
p.useImaginary = t[8].cast<bool>();
p.repeatLayers = t[9].cast<py::list>();
p.repeatLayers = t[9].cast<py::array_t<real_T>>();
p.contrastBackgroundParams = t[10].cast<py::list>();
p.contrastBackgroundTypes = t[11].cast<py::list>();
p.contrastBackgroundActions = t[12].cast<py::list>();
Expand Down Expand Up @@ -1099,6 +1097,6 @@ PYBIND11_MODULE(rat_core, m) {

m.def("RATMain", &RATMain, docsRATMain.c_str(), py::arg("problem_def"), py::arg("control"));

m.def("makeSLDProfileXY", &makeSLDProfileXY, docsMakeSLDProfileXY.c_str(),
py::arg("bulk_in"), py::arg("bulk_out"), py::arg("ssub"), py::arg("layers"), py::arg("number_of_repeats") = DEFAULT_NREPEATS);
m.def("makeSLDProfile", &makeSLDProfile, docsMakeSLDProfile.c_str(),
py::arg("bulk_in"), py::arg("bulk_out"), py::arg("layers"), py::arg("ssub"), py::arg("number_of_repeats") = DEFAULT_NREPEATS);
}
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2253,7 +2253,7 @@ def dream_bayes():

This optimisation used the parameters: nSamples=1, nChains=1.
"""
bayes = RATapi.rat_core.BayesResults()
bayes = RATapi.rat_core.OutputBayesResult()
bayes.predictionIntervals = RATapi.rat_core.PredictionIntervals()
bayes.predictionIntervals.reflectivity = [
np.array(
Expand Down
6 changes: 3 additions & 3 deletions tests/test_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def standard_layers_problem(test_names, test_checks):
problem.simulationLimits = [[1.0, 1.0]]
problem.numberOfContrasts = 1
problem.numberOfLayers = 1
problem.repeatLayers = [[0, 1]]
problem.repeatLayers = [1]
problem.layersDetails = [[2, 3, 4, float("NaN"), 2]]
problem.contrastLayers = [[1]]
problem.numberOfDomainContrasts = 0
Expand Down Expand Up @@ -242,7 +242,7 @@ def domains_problem(test_names, test_checks):
problem.simulationLimits = [[1.0, 1.0]]
problem.numberOfContrasts = 1
problem.numberOfLayers = 1
problem.repeatLayers = [[0, 1]]
problem.repeatLayers = [1]
problem.layersDetails = [[2, 3, 4, float("NaN"), 2]]
problem.contrastLayers = [[2, 1]]
problem.numberOfDomainContrasts = 2
Expand Down Expand Up @@ -311,7 +311,7 @@ def custom_xy_problem(test_names, test_checks):
problem.data = [np.empty([0, 6])]
problem.dataLimits = [[0.0, 0.0]]
problem.simulationLimits = [[0.005, 0.7]]
problem.repeatLayers = [[0, 1]]
problem.repeatLayers = [1]
problem.layersDetails = []
problem.contrastLayers = [[]]
problem.numberOfContrasts = 1
Expand Down
18 changes: 9 additions & 9 deletions tests/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,31 +140,31 @@ def test_ref_sld_bayes(fig, bayes_fig, bayes):
assert any(isinstance(comp, PolyCollection) for comp in components)


@patch("RATapi.utils.plotting.makeSLDProfileXY")
@patch("RATapi.utils.plotting.makeSLDProfile")
def test_sld_profile_function_call(mock: MagicMock) -> None:
"""Tests the makeSLDProfileXY function called with
"""Tests the makeSLDProfile function called with
correct args.
"""
RATplot.plot_ref_sld_helper(data())

assert mock.call_count == 3
assert mock.call_args_list[0].args[0] == 2.07e-06
assert mock.call_args_list[0].args[1] == 6.28e-06
assert mock.call_args_list[0].args[2] == 0.0
assert mock.call_args_list[0].args[3] == 0.0
assert mock.call_args_list[0].args[4] == 1

assert mock.call_args_list[1].args[0] == 2.07e-06
assert mock.call_args_list[1].args[1] == 1.83e-06
assert mock.call_args_list[1].args[2] == 0.0
assert mock.call_args_list[1].args[3] == 0.0
assert mock.call_args_list[1].args[4] == 1

assert mock.call_args_list[2].args[0] == 2.07e-06
assert mock.call_args_list[2].args[1] == -5.87e-07
assert mock.call_args_list[2].args[2] == 0.0
assert mock.call_args_list[2].args[3] == 0.0
assert mock.call_args_list[2].args[4] == 1


@patch("RATapi.utils.plotting.makeSLDProfileXY")
@patch("RATapi.utils.plotting.makeSLDProfile")
def test_live_plot(mock: MagicMock) -> None:
plot_data = data()

Expand All @@ -177,17 +177,17 @@ def test_live_plot(mock: MagicMock) -> None:
assert mock.call_count == 3
assert mock.call_args_list[0].args[0] == 2.07e-06
assert mock.call_args_list[0].args[1] == 6.28e-06
assert mock.call_args_list[0].args[2] == 0.0
assert mock.call_args_list[0].args[3] == 0.0
assert mock.call_args_list[0].args[4] == 1

assert mock.call_args_list[1].args[0] == 2.07e-06
assert mock.call_args_list[1].args[1] == 1.83e-06
assert mock.call_args_list[1].args[2] == 0.0
assert mock.call_args_list[1].args[3] == 0.0
assert mock.call_args_list[1].args[4] == 1

assert mock.call_args_list[2].args[0] == 2.07e-06
assert mock.call_args_list[2].args[1] == -5.87e-07
assert mock.call_args_list[2].args[2] == 0.0
assert mock.call_args_list[2].args[3] == 0.0
assert mock.call_args_list[2].args[4] == 1


Expand Down
4 changes: 2 additions & 2 deletions tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def reflectivity_calculation_problem():
problem.simulationLimits = [[0.011403, 0.59342], [0.011403, 0.59342]]
problem.numberOfContrasts = 2.0
problem.numberOfLayers = 6.0
problem.repeatLayers = [[0.0, 1.0], [0.0, 1.0]]
problem.repeatLayers = [1.0, 1.0]
problem.layersDetails = [
np.array([2.0]),
np.array([4.0]),
Expand Down Expand Up @@ -237,7 +237,7 @@ def dream_problem():
problem.simulationLimits = [[0.011403, 0.59342], [0.011403, 0.59342]]
problem.numberOfContrasts = 2.0
problem.numberOfLayers = 6.0
problem.repeatLayers = [[0.0, 1.0], [0.0, 1.0]]
problem.repeatLayers = [1.0, 1.0]
problem.layersDetails = [
np.array([2.0]),
np.array([4.0]),
Expand Down