Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions RATapi/examples/convert_rascal_project/convert_rascal.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def convert_rascal(mat_filename="lipid_bilayer.mat"):

"""
project_path = pathlib.Path(__file__).parent / "R1monolayerVolumeModel.mat"
project = RAT.utils.convert.r1_to_project_class(project_path)
project = RAT.utils.convert.r1_to_project(project_path)

# change values if you like, including ones not supported by R1
project.parameters["Head Thickness"].prior_type = "gaussian"
Expand All @@ -35,10 +35,10 @@ def convert_rascal(mat_filename="lipid_bilayer.mat"):

# convert DSPC standard layers example to a struct and save as file
lipid_bilayer_project = RAT.examples.DSPC_standard_layers()[0]
RAT.utils.convert.project_class_to_r1(lipid_bilayer_project, filename=mat_filename)
RAT.utils.convert.project_to_r1(lipid_bilayer_project, filename=mat_filename)

# convert and return as a Python dictionary
struct = RAT.utils.convert.project_class_to_r1(lipid_bilayer_project, return_struct=True)
struct = RAT.utils.convert.project_to_r1(lipid_bilayer_project, return_struct=True)

return project, struct

Expand Down
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
10 changes: 5 additions & 5 deletions RATapi/utils/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from RATapi.utils.enums import Geometries, Languages, LayerModels


def r1_to_project_class(filename: Union[str, PathLike]) -> Project:
def r1_to_project(filename: Union[str, PathLike]) -> Project:
"""Read a RasCAL1 project struct as a Python `Project`.

Parameters
Expand Down Expand Up @@ -318,7 +318,7 @@ def fix_invalid_constraints(name: str, constrs: tuple[float, float], value: floa
return project


def project_class_to_r1(
def project_to_r1(
project: Project, filename: Union[str, PathLike] = "RAT_project", return_struct: bool = False
) -> Union[dict, None]:
"""Convert a RAT Project to a RasCAL1 project struct.
Expand Down Expand Up @@ -549,10 +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()
if eng is None:
loader = wrappers.MatlabWrapper.loader
if loader is None:
raise ImportError("matlabengine is not installed.")
eng = loader.result()
eng.workspace["problem"] = r1
eng.save(str(filename), "problem", nargout=0)
eng.exit()
return None
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
16 changes: 8 additions & 8 deletions tests/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import pytest

import RATapi
from RATapi.utils.convert import project_class_to_r1, r1_to_project_class
from RATapi.utils.convert import project_to_r1, r1_to_project

TEST_DIR_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "test_data")

Expand Down Expand Up @@ -58,9 +58,9 @@ def dspc_bilayer():
],
)
@pytest.mark.parametrize("path_type", [os.path.join, pathlib.Path])
def test_r1_to_project_class(file, project, path_type, request):
def test_r1_to_project(file, project, path_type, request):
"""Test that R1 to Project class conversion returns the expected Project."""
output_project = r1_to_project_class(path_type(TEST_DIR_PATH, file))
output_project = r1_to_project(path_type(TEST_DIR_PATH, file))
expected_project = request.getfixturevalue(project)

# assert statements have to be more careful due to R1 missing features
Expand All @@ -83,7 +83,7 @@ def test_r1_to_project_class(file, project, path_type, request):
def test_r1_involution(project, request, monkeypatch):
"""Test that converting a Project to an R1 struct and back returns the same project."""
original_project = request.getfixturevalue(project)
r1_struct = project_class_to_r1(original_project, return_struct=True)
r1_struct = project_to_r1(original_project, return_struct=True)

# rather than writing the struct to a file and reading the file, just directly
# hand the struct over
Expand All @@ -93,7 +93,7 @@ def mock_load(ignored_filename, **ignored_settings):

monkeypatch.setattr("RATapi.utils.convert.loadmat", mock_load, raising=True)

converted_project = r1_to_project_class(project)
converted_project = r1_to_project(project)

for class_list in RATapi.project.class_lists:
assert getattr(converted_project, class_list) == getattr(original_project, class_list)
Expand All @@ -105,7 +105,7 @@ def test_invalid_constraints():
match=r"The parameter (.+) has invalid constraints,"
" these have been adjusted to satisfy the current value of the parameter."
):
output_project = r1_to_project_class(pathlib.Path(TEST_DIR_PATH, "R1DoubleBilayerVolumeModel.mat"))
output_project = r1_to_project(pathlib.Path(TEST_DIR_PATH, "R1DoubleBilayerVolumeModel.mat"))

assert output_project.background_parameters[0].min == output_project.background_parameters[0].value

Expand All @@ -117,7 +117,7 @@ def test_matlab_save(path_type, request):
project = request.getfixturevalue("r1_default_project")
with tempfile.TemporaryDirectory() as temp:
matfile = path_type(temp, "testfile.mat")
project_class_to_r1(project, filename=matfile)
converted_project = r1_to_project_class(matfile)
project_to_r1(project, filename=matfile)
converted_project = r1_to_project(matfile)

assert project == converted_project
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
Loading