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
20 changes: 3 additions & 17 deletions RATapi/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import RATapi
import RATapi.controls
import RATapi.wrappers
from RATapi.rat_core import Checks, Control, Limits, NameStore, ProblemDefinition
from RATapi.rat_core import Checks, Control, NameStore, ProblemDefinition
from RATapi.utils.enums import Calculations, Languages, LayerModels, TypeOptions

parameter_field = {
Expand Down Expand Up @@ -113,7 +113,7 @@ def __len__(self):
return len(self.files)


def make_input(project: RATapi.Project, controls: RATapi.Controls) -> tuple[ProblemDefinition, Limits, Control]:
def make_input(project: RATapi.Project, controls: RATapi.Controls) -> tuple[ProblemDefinition, Control]:
"""Construct the inputs required for the compiled RAT code using the data defined in the input project and controls.

Parameters
Expand All @@ -127,28 +127,14 @@ def make_input(project: RATapi.Project, controls: RATapi.Controls) -> tuple[Prob
-------
problem : RAT.rat_core.ProblemDefinition
The problem input used in the compiled RAT code.
limits : RAT.rat_core.Limits
A list of min/max values for each parameter defined in the project.
cpp_controls : RAT.rat_core.Control
The controls object used in the compiled RAT code.

"""
limits = Limits()

for class_list in RATapi.project.parameter_class_lists:
setattr(
limits,
parameter_field[class_list],
[[element.min, element.max] for element in getattr(project, class_list)],
)

if project.model == LayerModels.CustomXY:
controls.calcSldDuringFit = True

problem = make_problem(project)
cpp_controls = make_controls(controls)

return problem, limits, cpp_controls
return problem, cpp_controls


def make_problem(project: RATapi.Project) -> ProblemDefinition:
Expand Down
17 changes: 7 additions & 10 deletions RATapi/outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,13 @@ class Results:
The background for each contrast defined over the simulation range.
resolutions : list
The resolution for each contrast defined over the simulation range.
layerSlds : list
The array of layer parameter values for each contrast.
sldProfiles : list
The SLD profiles for each contrast.
layers : list
The array of layer parameter values for each contrast.
resampledLayers : list
If resampling is used, the SLD for each contrast after resampling has been performed.
If resampling is used, the array of layer parameter values for each contrast after resampling has been
performed.
calculationResults : CalculationResults
The chi-squared fit results from the final calculation and fit.
contrastParams : ContrastParams
Expand All @@ -164,8 +165,8 @@ class Results:
shiftedData: list
backgrounds: list
resolutions: list
layerSlds: list
sldProfiles: list
layers: list
resampledLayers: list
calculationResults: CalculationResults
contrastParams: ContrastParams
Expand Down Expand Up @@ -323,8 +324,6 @@ class DreamOutput(RATResult):
The runtime of the DREAM algorithm in seconds.
iteration : float
The number of iterations performed.
modelOutput : float
Unused. Will always be 0.
AR : np.ndarray
A two-column array where ``DreamOutput.AR[i, 0]`` is an iteration number
and ``DreamOutput.AR[i, 1]`` is the average acceptance rate of chain step
Expand All @@ -344,7 +343,6 @@ class DreamOutput(RATResult):
outlierChains: np.ndarray
runtime: float
iteration: float
modelOutput: float
AR: np.ndarray
R_stat: np.ndarray
CR: np.ndarray
Expand Down Expand Up @@ -483,7 +481,6 @@ def make_results(
outlierChains=bayes_results.dreamOutput.outlierChains,
runtime=bayes_results.dreamOutput.runtime,
iteration=bayes_results.dreamOutput.iteration,
modelOutput=bayes_results.dreamOutput.modelOutput,
AR=bayes_results.dreamOutput.AR,
R_stat=bayes_results.dreamOutput.R_stat,
CR=bayes_results.dreamOutput.CR,
Expand All @@ -502,8 +499,8 @@ def make_results(
shiftedData=output_results.shiftedData,
backgrounds=output_results.backgrounds,
resolutions=output_results.resolutions,
layerSlds=output_results.layerSlds,
sldProfiles=output_results.sldProfiles,
layers=output_results.layers,
resampledLayers=output_results.resampledLayers,
calculationResults=calculation_results,
contrastParams=contrast_params,
Expand All @@ -524,8 +521,8 @@ def make_results(
shiftedData=output_results.shiftedData,
backgrounds=output_results.backgrounds,
resolutions=output_results.resolutions,
layerSlds=output_results.layerSlds,
sldProfiles=output_results.sldProfiles,
layers=output_results.layers,
resampledLayers=output_results.resampledLayers,
calculationResults=calculation_results,
contrastParams=contrast_params,
Expand Down
3 changes: 1 addition & 2 deletions RATapi/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def run(project, controls):

horizontal_line = "\u2500" * 107 + "\n"
display_on = controls.display != Display.Off
problem_definition, limits, cpp_controls = make_input(project, controls)
problem_definition, cpp_controls = make_input(project, controls)

if display_on:
print("Starting RAT " + horizontal_line)
Expand All @@ -118,7 +118,6 @@ def run(project, controls):
with ProgressBar(display=display_on), TextOutput(display=display_on):
problem_definition, output_results, bayes_results = RATapi.rat_core.RATMain(
problem_definition,
limits,
cpp_controls,
)
end = time.time()
Expand Down
2 changes: 1 addition & 1 deletion cpp/RAT
Submodule RAT updated 205 files
42 changes: 4 additions & 38 deletions cpp/includes/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,6 @@ runtime : float
The runtime of the DREAM algorithm in seconds.
iteration : float
The number of iterations performed.
modelOutput : float
Unused. Will always be 0.
AR : np.ndarray[np.float]
A two-column array where ``DreamOutput.AR[i, 0]`` is an iteration number
and ``DreamOutput.AR[i, 1]`` is the average acceptance rate of chain step
Expand All @@ -259,7 +257,6 @@ struct DreamOutput
py::array_t<real_T> outlierChains;
real_T runtime;
real_T iteration;
real_T modelOutput;
py::array_t<real_T> AR;
py::array_t<real_T> R_stat;
py::array_t<real_T> CR;
Expand Down Expand Up @@ -354,12 +351,12 @@ backgrounds : list
The background for each contrast defined over the simulation range.
resolutions : list
The resolution for each contrast defined over the simulation range.
layerSlds : list
The array of layer parameter values for each contrast.
sldProfiles : list
The SLD profiles for each contrast.
layers : list
The array of layer parameter values for each contrast.
resampledLayers : list
If resampling is used, the SLD for each contrast after resampling has been performed.
If resampling is used, the array of layer parameter values for each contrast after resampling has been performed.
calculationResults : RATapi.rat_core.Calculation
The chi-squared fit results from the final calculation and fit.
contrastParams : RATapi.rat_core.ContrastParams
Expand All @@ -377,46 +374,15 @@ struct OutputResult {
py::list shiftedData;
py::list backgrounds;
py::list resolutions;
py::list layerSlds;
py::list sldProfiles;
py::list layers;
py::list resampledLayers;
Calculation calculationResults {};
ContrastParams contrastParams {};
py::array_t<real_T> fitParams;
py::list fitNames;
};

const std::string docsLimits = R"(The Python binding for the C++ limit struct which contains
Min and max values for each parameter defined in the project.

Parameters
----------
params : np.ndarray[np.float]
Limits for params in the problem definition.
backgroundParams : np.ndarray[np.float]
Limits for backgroundParams in the problem definition.
scalefactors : np.ndarray[np.float]
Limits for scalefactors in the problem definition.
bulkIns : np.ndarray[np.float]
Limits for bulkIns in the problem definition.
bulkOuts : np.ndarray[np.float]
Limits for bulkOuts in the problem definition.
resolutionParams : np.ndarray[np.float]
Limits for resolutionParams in the problem definition.
domainRatios : np.ndarray[np.float]
Limits for domainRatios in the problem definition.
)";

struct Limits {
py::array_t<real_T> params;
py::array_t<real_T> backgroundParams;
py::array_t<real_T> scalefactors;
py::array_t<real_T> bulkIns;
py::array_t<real_T> bulkOuts;
py::array_t<real_T> resolutionParams;
py::array_t<real_T> domainRatios;
};

const std::string docsNameStore = R"(The Python binding for the C++ names struct which
contains names of all parameters in the project.

Expand Down
77 changes: 12 additions & 65 deletions cpp/rat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,21 +301,6 @@ RAT::b_ProblemDefinition createProblemDefinitionStruct(const ProblemDefinition&
}


RAT::ProblemLimits createProblemLimitsStruct(const Limits& limits)
{
RAT::ProblemLimits limits_struct;
limits_struct.params = customCaller("Limits.params", pyArrayToRatArray2d, limits.params);
limits_struct.backgroundParams = customCaller("Limits.backgroundParams", pyArrayToRatArray2d, limits.backgroundParams);
limits_struct.scalefactors = customCaller("Limits.scalefactors", pyArrayToRatArray2d, limits.scalefactors);
limits_struct.bulkIns = customCaller("Limits.bulkIns", pyArrayToRatArray2d, limits.bulkIns);
limits_struct.bulkOuts = customCaller("Limits.bulkOuts", pyArrayToRatArray2d, limits.bulkOuts);
limits_struct.resolutionParams = customCaller("Limits.resolutionParams", pyArrayToRatArray2d, limits.resolutionParams);
limits_struct.domainRatios = customCaller("Limits.domainRatios", pyArrayToRatArray2d, limits.domainRatios);

return limits_struct;
}


RAT::Controls createControlsStruct(const Control& control)
{
RAT::Controls control_struct;
Expand Down Expand Up @@ -391,26 +376,26 @@ OutputResult OutputResultFromStruct(const RAT::Results result)
output_result.resolutions.append(array);
}

for (int32_T idx0{0}; idx0 < result.layerSlds.size(0); idx0++) {
for (int32_T idx0{0}; idx0 < result.sldProfiles.size(0); idx0++) {
py::list inner_list;
for (int32_T idx1{0}; idx1 < result.layerSlds.size(1); idx1++) {
auto tmp = result.layerSlds[idx0 + result.layerSlds.size(0) * idx1];
for (int32_T idx1{0}; idx1 < result.sldProfiles.size(1); idx1++) {
auto tmp = result.sldProfiles[idx0 + result.sldProfiles.size(0) * idx1];
auto array = py::array_t<real_T, py::array::f_style>({tmp.f1.size(0), tmp.f1.size(1)});
std::memcpy(array.request().ptr, tmp.f1.data(), array.nbytes());
inner_list.append(array);
}
output_result.layerSlds.append(inner_list);
output_result.sldProfiles.append(inner_list);
}

for (int32_T idx0{0}; idx0 < result.sldProfiles.size(0); idx0++) {
for (int32_T idx0{0}; idx0 < result.layers.size(0); idx0++) {
py::list inner_list;
for (int32_T idx1{0}; idx1 < result.sldProfiles.size(1); idx1++) {
auto tmp = result.sldProfiles[idx0 + result.sldProfiles.size(0) * idx1];
for (int32_T idx1{0}; idx1 < result.layers.size(1); idx1++) {
auto tmp = result.layers[idx0 + result.layers.size(0) * idx1];
auto array = py::array_t<real_T, py::array::f_style>({tmp.f1.size(0), tmp.f1.size(1)});
std::memcpy(array.request().ptr, tmp.f1.data(), array.nbytes());
inner_list.append(array);
}
output_result.sldProfiles.append(inner_list);
output_result.layers.append(inner_list);
}

for (int32_T idx0{0}; idx0 < result.resampledLayers.size(0); idx0++) {
Expand Down Expand Up @@ -548,7 +533,6 @@ BayesResults bayesResultsFromStruct(const RAT::BayesResults results)
bayesResults.dreamOutput.outlierChains = pyArray2dFromBoundedArray<coder::bounded_array<real_T, 2000U, 2U>>(results.dreamOutput.outlierChains);
bayesResults.dreamOutput.runtime = results.dreamOutput.runtime;
bayesResults.dreamOutput.iteration = results.dreamOutput.iteration;
bayesResults.dreamOutput.modelOutput = results.dreamOutput.modelOutput;
bayesResults.dreamOutput.R_stat = pyArrayFromRatArray2d(results.dreamOutput.R_stat);
bayesResults.dreamOutput.CR = pyArrayFromRatArray2d(results.dreamOutput.CR);
bayesResults.dreamOutput.AR = pyArray2dFromBoundedArray<coder::bounded_array<real_T, 2000U, 2U>>(results.dreamOutput.AR);
Expand Down Expand Up @@ -582,8 +566,6 @@ Parameters
----------
problem_def : Rat.rat_core.ProblemDefinition
The project input for the RAT calculation.
limits : RATapi.rat_core.Limits
Min and max values for each parameter defined in the problem definition.
control : RATapi.rat_core.Control
The controls object for the RAT calculation.

Expand All @@ -597,16 +579,15 @@ bayes_result : Rat.rat_core.BayesResults
The extra results if RAT calculation is Bayesian.
)";

py::tuple RATMain(const ProblemDefinition& problem_def, const Limits& limits, const Control& control)
py::tuple RATMain(const ProblemDefinition& problem_def, const Control& control)
{
RAT::b_ProblemDefinition problem_def_struct = createProblemDefinitionStruct(problem_def);
RAT::ProblemLimits limits_struct = createProblemLimitsStruct(limits);
RAT::Controls control_struct = createControlsStruct(control);
// Output
RAT::Results results;
RAT::BayesResults bayesResults;
// Call the entry-point
RAT::RATMain(&problem_def_struct, &limits_struct, &control_struct, &results, &bayesResults);
RAT::RATMain(&problem_def_struct, &control_struct, &results, &bayesResults);
// Copy result to output
auto out_problem_def = problemDefinitionFromStruct(problem_def_struct);
out_problem_def.customFiles = problem_def.customFiles.attr("copy")();
Expand Down Expand Up @@ -802,7 +783,6 @@ PYBIND11_MODULE(rat_core, m) {
.def_readwrite("outlierChains", &DreamOutput::outlierChains)
.def_readwrite("runtime", &DreamOutput::runtime)
.def_readwrite("iteration", &DreamOutput::iteration)
.def_readwrite("modelOutput", &DreamOutput::modelOutput)
.def_readwrite("AR", &DreamOutput::AR)
.def_readwrite("R_stat", &DreamOutput::R_stat)
.def_readwrite("CR", &DreamOutput::CR);
Expand Down Expand Up @@ -836,8 +816,8 @@ PYBIND11_MODULE(rat_core, m) {
.def_readwrite("shiftedData", &OutputResult::shiftedData)
.def_readwrite("backgrounds", &OutputResult::backgrounds)
.def_readwrite("resolutions", &OutputResult::resolutions)
.def_readwrite("layerSlds", &OutputResult::layerSlds)
.def_readwrite("sldProfiles", &OutputResult::sldProfiles)
.def_readwrite("layers", &OutputResult::layers)
.def_readwrite("resampledLayers", &OutputResult::resampledLayers)
.def_readwrite("calculationResults", &OutputResult::calculationResults)
.def_readwrite("contrastParams", &OutputResult::contrastParams)
Expand Down Expand Up @@ -912,39 +892,6 @@ PYBIND11_MODULE(rat_core, m) {
return chk;
}));

py::class_<Limits>(m, "Limits", docsLimits.c_str())
.def(py::init<>())
.def_readwrite("params", &Limits::params)
.def_readwrite("backgroundParams", &Limits::backgroundParams)
.def_readwrite("scalefactors", &Limits::scalefactors)
.def_readwrite("bulkIns", &Limits::bulkIns)
.def_readwrite("bulkOuts", &Limits::bulkOuts)
.def_readwrite("resolutionParams", &Limits::resolutionParams)
.def_readwrite("domainRatios", &Limits::domainRatios)
.def(py::pickle(
[](const Limits &lim) { // __getstate__
/* Return a tuple that fully encodes the state of the object */
return py::make_tuple(lim.params, lim.backgroundParams, lim.scalefactors, lim.bulkIns, lim.bulkOuts,
lim.resolutionParams, lim.domainRatios);
},
[](py::tuple t) { // __setstate__
if (t.size() != 7)
throw std::runtime_error("Encountered invalid state unpickling Limits object!");

/* Create a new C++ instance */
Limits lim;

lim.params = t[0].cast<py::array_t<real_T>>();
lim.backgroundParams = t[1].cast<py::array_t<real_T>>();
lim.scalefactors = t[2].cast<py::array_t<real_T>>();
lim.bulkIns = t[3].cast<py::array_t<real_T>>();
lim.bulkOuts = t[4].cast<py::array_t<real_T>>();
lim.resolutionParams = t[5].cast<py::array_t<real_T>>();
lim.domainRatios = t[6].cast<py::array_t<real_T>>();

return lim;
}));

py::class_<Control>(m, "Control", docsControl.c_str())
.def(py::init<>())
.def_readwrite("parallel", &Control::parallel)
Expand Down Expand Up @@ -1150,7 +1097,7 @@ PYBIND11_MODULE(rat_core, m) {
return p;
}));

m.def("RATMain", &RATMain, docsRATMain.c_str(), py::arg("problem_def"), py::arg("limits"), py::arg("control"));
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);
Expand Down
Loading