RATapi 0.0.0.dev10__cp313-cp313-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. ratapi/__init__.py +29 -0
  2. ratapi/classlist.py +620 -0
  3. ratapi/controls.py +258 -0
  4. ratapi/eventManager.dll +0 -0
  5. ratapi/events.py +88 -0
  6. ratapi/examples/__init__.py +25 -0
  7. ratapi/examples/absorption/__init__.py +1 -0
  8. ratapi/examples/absorption/absorption.py +172 -0
  9. ratapi/examples/absorption/volume_thiol_bilayer.py +141 -0
  10. ratapi/examples/convert_rascal_project/Model_IIb.py +87 -0
  11. ratapi/examples/convert_rascal_project/__init__.py +1 -0
  12. ratapi/examples/convert_rascal_project/convert_rascal.py +49 -0
  13. ratapi/examples/data/D2O_spin_down.dat +100 -0
  14. ratapi/examples/data/D2O_spin_up.dat +101 -0
  15. ratapi/examples/data/DSPC_D2O.dat +82 -0
  16. ratapi/examples/data/DSPC_SMW.dat +82 -0
  17. ratapi/examples/data/H2O_spin_down.dat +102 -0
  18. ratapi/examples/data/H2O_spin_up.dat +102 -0
  19. ratapi/examples/data/__init__.py +1 -0
  20. ratapi/examples/data/c_PLP0016596.dat +146 -0
  21. ratapi/examples/data/c_PLP0016601.dat +97 -0
  22. ratapi/examples/data/c_PLP0016607.dat +104 -0
  23. ratapi/examples/data/d2o_background_data.dat +82 -0
  24. ratapi/examples/domains/__init__.py +1 -0
  25. ratapi/examples/domains/alloy_domains.py +31 -0
  26. ratapi/examples/domains/domains_XY_model.py +72 -0
  27. ratapi/examples/domains/domains_custom_XY.py +86 -0
  28. ratapi/examples/domains/domains_custom_layers.py +60 -0
  29. ratapi/examples/domains/domains_standard_layers.py +92 -0
  30. ratapi/examples/languages/__init__.py +1 -0
  31. ratapi/examples/languages/custom_bilayer.py +69 -0
  32. ratapi/examples/languages/run_custom_file_languages.py +41 -0
  33. ratapi/examples/languages/setup_problem.py +130 -0
  34. ratapi/examples/normal_reflectivity/DSPC_custom_XY.py +149 -0
  35. ratapi/examples/normal_reflectivity/DSPC_custom_layers.py +130 -0
  36. ratapi/examples/normal_reflectivity/DSPC_data_background.py +220 -0
  37. ratapi/examples/normal_reflectivity/DSPC_function_background.py +219 -0
  38. ratapi/examples/normal_reflectivity/DSPC_standard_layers.py +210 -0
  39. ratapi/examples/normal_reflectivity/__init__.py +1 -0
  40. ratapi/examples/normal_reflectivity/background_function.py +16 -0
  41. ratapi/examples/normal_reflectivity/custom_XY_DSPC.py +138 -0
  42. ratapi/examples/normal_reflectivity/custom_bilayer_DSPC.py +87 -0
  43. ratapi/inputs.py +588 -0
  44. ratapi/models.py +677 -0
  45. ratapi/outputs.py +821 -0
  46. ratapi/project.py +1074 -0
  47. ratapi/rat_core.cp313-win_amd64.pyd +0 -0
  48. ratapi/run.py +140 -0
  49. ratapi/utils/__init__.py +1 -0
  50. ratapi/utils/convert.py +564 -0
  51. ratapi/utils/custom_errors.py +40 -0
  52. ratapi/utils/enums.py +204 -0
  53. ratapi/utils/orso.py +247 -0
  54. ratapi/utils/plotting.py +1231 -0
  55. ratapi/wrappers.py +144 -0
  56. ratapi-0.0.0.dev10.dist-info/METADATA +61 -0
  57. ratapi-0.0.0.dev10.dist-info/RECORD +59 -0
  58. ratapi-0.0.0.dev10.dist-info/WHEEL +5 -0
  59. ratapi-0.0.0.dev10.dist-info/top_level.txt +1 -0
ratapi/inputs.py ADDED
@@ -0,0 +1,588 @@
1
+ """Converts python models to the necessary inputs for the compiled RAT code."""
2
+
3
+ import importlib
4
+ import os
5
+ import pathlib
6
+ from collections.abc import Callable
7
+
8
+ import numpy as np
9
+
10
+ import ratapi
11
+ import ratapi.wrappers
12
+ from ratapi.rat_core import Checks, Control, NameStore, ProblemDefinition
13
+ from ratapi.utils.enums import Calculations, Languages, LayerModels, TypeOptions
14
+
15
+ parameter_field = {
16
+ "parameters": "params",
17
+ "bulk_in": "bulkIns",
18
+ "bulk_out": "bulkOuts",
19
+ "scalefactors": "scalefactors",
20
+ "domain_ratios": "domainRatios",
21
+ "background_parameters": "backgroundParams",
22
+ "resolution_parameters": "resolutionParams",
23
+ }
24
+
25
+
26
+ def get_python_handle(file_name: str, function_name: str, path: str | pathlib.Path = "") -> Callable:
27
+ """Get the function handle from a function defined in a python module located anywhere within the filesystem.
28
+
29
+ Parameters
30
+ ----------
31
+ file_name : str
32
+ The name of the file containing the function of interest.
33
+ function_name : str
34
+ The name of the function we wish to obtain the handle for within the module.
35
+ path : str
36
+ The path to the file containing the function (default is "", which represent the working directory).
37
+
38
+ Returns
39
+ -------
40
+ handle : Callable
41
+ The handle of the function defined in the python module file.
42
+
43
+ """
44
+ spec = importlib.util.spec_from_file_location(pathlib.Path(file_name).stem, os.path.join(path, file_name))
45
+ custom_module = importlib.util.module_from_spec(spec)
46
+ spec.loader.exec_module(custom_module)
47
+ handle = getattr(custom_module, function_name)
48
+ return handle
49
+
50
+
51
+ class FileHandles:
52
+ """Class to defer creation of custom file handles.
53
+
54
+ Parameters
55
+ ----------
56
+ files : ClassList[CustomFile]
57
+ A list of custom file models.
58
+
59
+ """
60
+
61
+ def __init__(self, files=None):
62
+ self.index = 0
63
+ self.files = [] if files is None else [file.model_dump() for file in files]
64
+
65
+ def __iter__(self):
66
+ self.index = 0
67
+ return self
68
+
69
+ def get_handle(self, index: int):
70
+ """Return file handle for a given custom file.
71
+
72
+ Parameters
73
+ ----------
74
+ index : int
75
+ The index of the custom file.
76
+
77
+ """
78
+ custom_file = self.files[index]
79
+ full_path = os.path.join(custom_file["path"], custom_file["filename"])
80
+ if custom_file["language"] == Languages.Python:
81
+ file_handle = get_python_handle(custom_file["filename"], custom_file["function_name"], custom_file["path"])
82
+ elif custom_file["language"] == Languages.Matlab:
83
+ file_handle = ratapi.wrappers.MatlabWrapper(full_path).getHandle()
84
+ elif custom_file["language"] == Languages.Cpp:
85
+ file_handle = ratapi.wrappers.DylibWrapper(full_path, custom_file["function_name"]).getHandle()
86
+
87
+ return file_handle
88
+
89
+ def copy(self) -> "FileHandles":
90
+ """Create a copy of the FileHandles object.
91
+
92
+ Returns
93
+ -------
94
+ FileHandles
95
+ The copy of this FileHandles object.
96
+
97
+ """
98
+ handles = FileHandles()
99
+ handles.files = [file.copy() for file in self.files]
100
+
101
+ return handles
102
+
103
+ def __next__(self):
104
+ if self.index < len(self.files):
105
+ custom_file = self.get_handle(self.index)
106
+ self.index += 1
107
+ return custom_file
108
+ else:
109
+ raise StopIteration
110
+
111
+ def __len__(self):
112
+ return len(self.files)
113
+
114
+
115
+ def make_input(project: ratapi.Project, controls: ratapi.Controls) -> tuple[ProblemDefinition, Control]:
116
+ """Construct the inputs required for the compiled RAT code using the data defined in the input project and controls.
117
+
118
+ Parameters
119
+ ----------
120
+ project : RAT.Project
121
+ The project model, which defines the physical system under study.
122
+ controls : RAT.Controls
123
+ The controls model, which defines algorithmic properties.
124
+
125
+ Returns
126
+ -------
127
+ problem : RAT.rat_core.ProblemDefinition
128
+ The problem input used in the compiled RAT code.
129
+ cpp_controls : RAT.rat_core.Control
130
+ The controls object used in the compiled RAT code.
131
+
132
+ """
133
+ problem = make_problem(project)
134
+ cpp_controls = make_controls(controls)
135
+
136
+ return problem, cpp_controls
137
+
138
+
139
+ def make_problem(project: ratapi.Project) -> ProblemDefinition:
140
+ """Construct the problem input required for the compiled RAT code.
141
+
142
+ Parameters
143
+ ----------
144
+ project : RAT.Project
145
+ The project model, which defines the physical system under study.
146
+
147
+ Returns
148
+ -------
149
+ problem : RAT.rat_core.ProblemDefinition
150
+ The problem input used in the compiled RAT code.
151
+
152
+ """
153
+ prior_id = {"uniform": 1, "gaussian": 2, "jeffreys": 3}
154
+
155
+ # Ensure all contrast fields are properly defined
156
+ for contrast in project.contrasts:
157
+ contrast_fields = ["data", "background", "bulk_in", "bulk_out", "scalefactor", "resolution"]
158
+
159
+ if project.calculation == Calculations.Domains:
160
+ contrast_fields.append("domain_ratio")
161
+
162
+ for field in contrast_fields:
163
+ if getattr(contrast, field) == "":
164
+ raise ValueError(
165
+ f'In the input project, the "{field}" field of contrast "{contrast.name}" does not have a '
166
+ f"value defined. A value must be supplied before running the project."
167
+ )
168
+
169
+ # Ensure backgrounds and resolutions have a source defined
170
+ background = project.backgrounds[contrast.background]
171
+ resolution = project.resolutions[contrast.resolution]
172
+ if background.source == "":
173
+ raise ValueError(
174
+ f"All backgrounds must have a source defined. For a {background.type} type background, "
175
+ f"the source must be defined in "
176
+ f'"{ratapi.project.values_defined_in[f"backgrounds.{background.type}.source"]}"'
177
+ )
178
+ if resolution.source == "" and resolution.type != TypeOptions.Data:
179
+ raise ValueError(
180
+ f"Constant resolutions must have a source defined. The source must be defined in "
181
+ f'"{ratapi.project.values_defined_in[f"resolutions.{resolution.type}.source"]}"'
182
+ )
183
+
184
+ # Set contrast parameters according to model type
185
+ if project.model == LayerModels.StandardLayers:
186
+ if project.calculation == Calculations.Domains:
187
+ contrast_models = [
188
+ [project.domain_contrasts.index(domain_contrast, True) for domain_contrast in contrast.model]
189
+ for contrast in project.contrasts
190
+ ]
191
+ else:
192
+ contrast_models = [
193
+ [project.layers.index(layer, True) for layer in contrast.model] for contrast in project.contrasts
194
+ ]
195
+ else:
196
+ contrast_models = [[]] * len(project.contrasts)
197
+
198
+ # Set contrast parameters according to model type
199
+ if project.model == LayerModels.StandardLayers:
200
+ contrast_custom_files = [float("NaN")] * len(project.contrasts)
201
+ else:
202
+ contrast_custom_files = [project.custom_files.index(contrast.model[0], True) for contrast in project.contrasts]
203
+
204
+ # Get details of defined layers
205
+ layer_details = get_layer_details(project)
206
+
207
+ contrast_background_params = []
208
+ contrast_background_types = []
209
+ all_data = []
210
+ data_limits = []
211
+ simulation_limits = []
212
+ contrast_resolution_params = []
213
+ contrast_resolution_types = []
214
+
215
+ # Set data, background and resolution for each contrast
216
+ for contrast in project.contrasts:
217
+ # Set data
218
+ data_index = project.data.index(contrast.data)
219
+ data = project.data[data_index].data
220
+ data_range = project.data[data_index].data_range
221
+ simulation_range = project.data[data_index].simulation_range
222
+
223
+ if data_range:
224
+ data_limits.append(data_range)
225
+ else:
226
+ data_limits.append([0.0, 0.0])
227
+
228
+ if simulation_range:
229
+ simulation_limits.append(simulation_range)
230
+ else:
231
+ simulation_limits.append([0.0, 0.0])
232
+
233
+ # Set background parameters
234
+ background = project.backgrounds[contrast.background]
235
+ contrast_background_types.append(background.type)
236
+ contrast_background_param = []
237
+
238
+ if background.type == TypeOptions.Data:
239
+ contrast_background_param.append(project.data.index(background.source, True))
240
+ if background.value_1 != "":
241
+ contrast_background_param.append(project.background_parameters.index(background.value_1))
242
+ # If we are using a data background, we add the background data to the contrast data
243
+ data = append_data_background(data, project.data[background.source].data)
244
+
245
+ elif background.type == TypeOptions.Function:
246
+ contrast_background_param.append(project.custom_files.index(background.source, True))
247
+ contrast_background_param.extend(
248
+ [
249
+ project.background_parameters.index(value, True)
250
+ for value in [
251
+ background.value_1,
252
+ background.value_2,
253
+ background.value_3,
254
+ background.value_4,
255
+ background.value_5,
256
+ ]
257
+ if value != ""
258
+ ]
259
+ )
260
+
261
+ else:
262
+ contrast_background_param.append(project.background_parameters.index(background.source, True))
263
+
264
+ contrast_background_params.append(contrast_background_param)
265
+
266
+ # Set resolution parameters
267
+ resolution = project.resolutions[contrast.resolution]
268
+ contrast_resolution_types.append(resolution.type)
269
+ contrast_resolution_param = []
270
+ if resolution.type == TypeOptions.Function:
271
+ contrast_resolution_param.append(project.custom_files.index(resolution.source, True))
272
+ contrast_resolution_param.extend(
273
+ [
274
+ project.resolution_parameters.index(value, True)
275
+ for value in [
276
+ resolution.value_1,
277
+ resolution.value_2,
278
+ resolution.value_3,
279
+ resolution.value_4,
280
+ resolution.value_5,
281
+ ]
282
+ if value != ""
283
+ ]
284
+ )
285
+
286
+ elif resolution.type == TypeOptions.Constant:
287
+ contrast_resolution_param.append(project.resolution_parameters.index(resolution.source, True))
288
+
289
+ contrast_resolution_params.append(contrast_resolution_param)
290
+
291
+ # Contrast data has exactly six columns to include background data if relevant
292
+ all_data.append(np.column_stack((data, np.zeros((data.shape[0], 6 - data.shape[1])))))
293
+
294
+ problem = ProblemDefinition()
295
+
296
+ problem.TF = project.calculation
297
+ problem.resample = make_resample(project)
298
+ problem.data = all_data
299
+ problem.dataPresent = make_data_present(project)
300
+ problem.dataLimits = data_limits
301
+ problem.simulationLimits = simulation_limits
302
+ problem.numberOfContrasts = len(project.contrasts)
303
+ problem.geometry = project.geometry
304
+ problem.useImaginary = project.absorption
305
+ problem.repeatLayers = [contrast.repeat_layers for contrast in project.contrasts]
306
+ problem.contrastBackgroundParams = contrast_background_params
307
+ problem.contrastBackgroundTypes = contrast_background_types
308
+ problem.contrastBackgroundActions = [contrast.background_action for contrast in project.contrasts]
309
+ problem.contrastScalefactors = [
310
+ project.scalefactors.index(contrast.scalefactor, True) for contrast in project.contrasts
311
+ ]
312
+ problem.contrastBulkIns = [project.bulk_in.index(contrast.bulk_in, True) for contrast in project.contrasts]
313
+ problem.contrastBulkOuts = [project.bulk_out.index(contrast.bulk_out, True) for contrast in project.contrasts]
314
+
315
+ problem.contrastResolutionParams = contrast_resolution_params
316
+ problem.contrastResolutionTypes = contrast_resolution_types
317
+
318
+ problem.backgroundParams = [param.value for param in project.background_parameters]
319
+ problem.scalefactors = [param.value for param in project.scalefactors]
320
+ problem.bulkIns = [param.value for param in project.bulk_in]
321
+ problem.bulkOuts = [param.value for param in project.bulk_out]
322
+ problem.resolutionParams = [param.value for param in project.resolution_parameters]
323
+ problem.params = [param.value for param in project.parameters]
324
+ problem.numberOfLayers = len(project.layers)
325
+ problem.contrastLayers = [contrast_model if contrast_model else [] for contrast_model in contrast_models]
326
+ problem.layersDetails = layer_details if project.model == LayerModels.StandardLayers else []
327
+ problem.customFiles = FileHandles(project.custom_files)
328
+ problem.modelType = project.model
329
+ problem.contrastCustomFiles = contrast_custom_files
330
+
331
+ problem.contrastDomainRatios = [
332
+ project.domain_ratios.index(contrast.domain_ratio, True) if hasattr(contrast, "domain_ratio") else 0
333
+ for contrast in project.contrasts
334
+ ]
335
+
336
+ problem.domainRatios = [param.value for param in project.domain_ratios]
337
+ problem.numberOfDomainContrasts = len(project.domain_contrasts)
338
+
339
+ domain_contrast_models = [
340
+ [project.layers.index(layer, True) for layer in domain_contrast.model]
341
+ for domain_contrast in project.domain_contrasts
342
+ ]
343
+
344
+ problem.domainContrastLayers = [
345
+ domain_contrast_model if domain_contrast_model else [] for domain_contrast_model in domain_contrast_models
346
+ ]
347
+ problem.fitParams = [
348
+ param.value
349
+ for class_list in ratapi.project.parameter_class_lists
350
+ for param in getattr(project, class_list)
351
+ if param.fit
352
+ ]
353
+ problem.fitLimits = [
354
+ [param.min, param.max]
355
+ for class_list in ratapi.project.parameter_class_lists
356
+ for param in getattr(project, class_list)
357
+ if param.fit
358
+ ]
359
+ problem.priorNames = [
360
+ param.name for class_list in ratapi.project.parameter_class_lists for param in getattr(project, class_list)
361
+ ]
362
+ problem.priorValues = [
363
+ [prior_id[param.prior_type], param.mu, param.sigma]
364
+ for class_list in ratapi.project.parameter_class_lists
365
+ for param in getattr(project, class_list)
366
+ ]
367
+
368
+ # Names
369
+ problem.names = NameStore()
370
+ for class_list in ratapi.project.parameter_class_lists:
371
+ setattr(problem.names, parameter_field[class_list], [param.name for param in getattr(project, class_list)])
372
+ problem.names.contrasts = [contrast.name for contrast in project.contrasts]
373
+
374
+ # Checks
375
+ problem.checks = Checks()
376
+ for class_list in ratapi.project.parameter_class_lists:
377
+ setattr(
378
+ problem.checks, parameter_field[class_list], [int(element.fit) for element in getattr(project, class_list)]
379
+ )
380
+
381
+ check_indices(problem)
382
+
383
+ return problem
384
+
385
+
386
+ def get_layer_details(project: ratapi.Project) -> list[int]:
387
+ """Get parameter indices for all layers defined in the project."""
388
+ hydrate_id = {"bulk in": 1, "bulk out": 2}
389
+ layer_details = []
390
+
391
+ # Get the thickness, SLD, roughness fields from the appropriate model
392
+ if project.absorption:
393
+ layer_fields = list(ratapi.models.AbsorptionLayer.model_fields.keys())[1:-2]
394
+ else:
395
+ layer_fields = list(ratapi.models.Layer.model_fields.keys())[1:-2]
396
+
397
+ for layer in project.layers:
398
+ for field in layer_fields:
399
+ if getattr(layer, field) == "":
400
+ raise ValueError(
401
+ f'In the input project, the "{field}" field of layer {layer.name} does not have a value '
402
+ f"defined. A value must be supplied before running the project."
403
+ )
404
+
405
+ layer_params = [project.parameters.index(getattr(layer, attribute), True) for attribute in list(layer_fields)]
406
+
407
+ layer_params.append(project.parameters.index(layer.hydration, True) if layer.hydration else float("NaN"))
408
+ layer_params.append(hydrate_id[layer.hydrate_with])
409
+
410
+ layer_details.append(layer_params)
411
+
412
+ return layer_details
413
+
414
+
415
+ def make_resample(project: ratapi.Project) -> list[int]:
416
+ """Construct the "resample" field of the problem input required for the compiled RAT code.
417
+
418
+ Parameters
419
+ ----------
420
+ project : RAT.Project
421
+ The project model, which defines the physical system under study.
422
+
423
+ Returns
424
+ -------
425
+ list[int]
426
+ The "resample" field of the problem input used in the compiled RAT code.
427
+
428
+ """
429
+ return [contrast.resample for contrast in project.contrasts]
430
+
431
+
432
+ def make_data_present(project: ratapi.Project) -> list[int]:
433
+ """Construct the "dataPresent" field of the problem input required for the compiled RAT code.
434
+
435
+ Parameters
436
+ ----------
437
+ project : RAT.Project
438
+ The project model, which defines the physical system under study.
439
+
440
+ Returns
441
+ -------
442
+ list[int]
443
+ The "dataPresent" field of the problem input used in the compiled RAT code.
444
+
445
+ """
446
+ return [1 if project.data[contrast.data].data.size != 0 else 0 for contrast in project.contrasts]
447
+
448
+
449
+ def check_indices(problem: ProblemDefinition) -> None:
450
+ """Check the indices given in a problem's contrasts lie within the range of the corresponding parameter lists.
451
+
452
+ Parameters
453
+ ----------
454
+ problem : RAT.rat_core.ProblemDefinition
455
+ The problem input used in the compiled RAT code.
456
+
457
+ """
458
+ index_list = {
459
+ "scalefactors": "contrastScalefactors",
460
+ "bulkIns": "contrastBulkIns",
461
+ "bulkOuts": "contrastBulkOuts",
462
+ "domainRatios": "contrastDomainRatios",
463
+ }
464
+
465
+ # Check the indices -- note we have switched to 1-based indexing at this point
466
+ for params in index_list:
467
+ param_list = getattr(problem, params)
468
+ if len(param_list) > 0:
469
+ elements = [
470
+ element
471
+ for element in getattr(problem, index_list[params])
472
+ if (element != -1) and not (0 < element <= len(param_list))
473
+ ]
474
+ if elements:
475
+ raise IndexError(
476
+ f'The problem field "{index_list[params]}" contains: {", ".join(str(i) for i in elements)}'
477
+ f', which lie{"s" * (len(elements) == 1)} outside of the range of "{params}"',
478
+ )
479
+
480
+ # backgroundParams has a different structure, so is handled separately:
481
+ # it is of type list[list[int]], where each list[int] is the indices for
482
+ # source, value_1, value_2, value_3, value_4, value_5 where they are defined
483
+ # e.g. for a data background with offset it is [source value_1], for a function
484
+ # with 3 values it is [source value_1 value_2 value_3], etc.
485
+
486
+ source_param_lists = {
487
+ "constant": "backgroundParams",
488
+ "data": "data",
489
+ "function": "customFiles",
490
+ }
491
+
492
+ for i, background_data in enumerate(problem.contrastBackgroundParams):
493
+ background_type = problem.contrastBackgroundTypes[i]
494
+
495
+ # check source param is in range of the relevant parameter list
496
+ param_list = getattr(problem, source_param_lists[background_type])
497
+ source_index = background_data[0]
498
+ if not 0 < source_index <= len(param_list):
499
+ raise IndexError(
500
+ f'Entry {i} of contrastBackgroundParams has type "{background_type}" '
501
+ f"and source index {source_index}, "
502
+ f'which is outside the range of "{source_param_lists[background_type]}".'
503
+ )
504
+
505
+ # check value params are in range for background params
506
+ if len(background_data) > 1:
507
+ elements = [element for element in background_data[1:] if not 0 < element <= len(problem.backgroundParams)]
508
+ if elements:
509
+ raise IndexError(
510
+ f"Entry {i} of contrastBackgroundParams contains: {', '.join(str(i) for i in elements)}"
511
+ f', which lie{"s" * (len(elements) == 1)} outside of the range of "backgroundParams"',
512
+ )
513
+
514
+
515
+ def append_data_background(data: np.array, background: np.array) -> np.array:
516
+ """Append background data to contrast data.
517
+
518
+ Parameters
519
+ ----------
520
+ data : np.array
521
+ The contrast data to which we are appending a background.
522
+ background : np.array
523
+ The background data to append to the contrast.
524
+
525
+ Returns
526
+ -------
527
+ np.array
528
+ The contrast data with background data appended as two additional columns.
529
+
530
+ """
531
+ if not np.allclose(data[:, 0], background[:, 0]):
532
+ raise ValueError("The q-values of the data and background must be equal.")
533
+
534
+ return np.hstack((data, np.zeros((data.shape[0], 4 - data.shape[1])), background[:, 1:]))
535
+
536
+
537
+ def make_controls(input_controls: ratapi.Controls) -> Control:
538
+ """Convert the controls object to the format required by the compiled RAT code.
539
+
540
+ Parameters
541
+ ----------
542
+ input_controls : RAT.Controls
543
+ The controls model, which defines algorithmic properties.
544
+
545
+ Returns
546
+ -------
547
+ controls : RAT.rat_core.Control
548
+ The controls object used in the compiled RAT code.
549
+
550
+ """
551
+ controls = Control()
552
+
553
+ controls.procedure = input_controls.procedure
554
+ controls.parallel = input_controls.parallel
555
+ controls.numSimulationPoints = input_controls.numSimulationPoints
556
+ controls.resampleMinAngle = input_controls.resampleMinAngle
557
+ controls.resampleNPoints = input_controls.resampleNPoints
558
+ controls.display = input_controls.display
559
+ # Simplex
560
+ controls.xTolerance = input_controls.xTolerance
561
+ controls.funcTolerance = input_controls.funcTolerance
562
+ controls.maxFuncEvals = input_controls.maxFuncEvals
563
+ controls.maxIterations = input_controls.maxIterations
564
+ controls.updateFreq = input_controls.updateFreq
565
+ controls.updatePlotFreq = input_controls.updatePlotFreq
566
+ # DE
567
+ controls.populationSize = input_controls.populationSize
568
+ controls.fWeight = input_controls.fWeight
569
+ controls.crossoverProbability = input_controls.crossoverProbability
570
+ controls.strategy = int(input_controls.strategy) # RAT core expects strategy as an integer
571
+ controls.targetValue = input_controls.targetValue
572
+ controls.numGenerations = input_controls.numGenerations
573
+ # NS
574
+ controls.nLive = input_controls.nLive
575
+ controls.nMCMC = input_controls.nMCMC
576
+ controls.propScale = input_controls.propScale
577
+ controls.nsTolerance = input_controls.nsTolerance
578
+ # Dream
579
+ controls.nSamples = input_controls.nSamples
580
+ controls.nChains = input_controls.nChains
581
+ controls.jumpProbability = input_controls.jumpProbability
582
+ controls.pUnitGamma = input_controls.pUnitGamma
583
+ controls.boundHandling = input_controls.boundHandling
584
+ controls.adaptPCR = input_controls.adaptPCR
585
+
586
+ controls.IPCFilePath = input_controls._IPCFilePath
587
+
588
+ return controls