RATapi 0.0.0.dev0__cp312-cp312-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 (64) hide show
  1. RATapi/__init__.py +8 -0
  2. RATapi/classlist.py +387 -0
  3. RATapi/controls.py +143 -0
  4. RATapi/eventManager.dll +0 -0
  5. RATapi/events.py +70 -0
  6. RATapi/examples/__init__.py +0 -0
  7. RATapi/examples/absorption/__init__.py +0 -0
  8. RATapi/examples/absorption/absorption.py +148 -0
  9. RATapi/examples/absorption/volume_thiol_bilayer.py +138 -0
  10. RATapi/examples/data/D2O_spin_down.dat +100 -0
  11. RATapi/examples/data/D2O_spin_up.dat +101 -0
  12. RATapi/examples/data/DSPC_D2O.dat +82 -0
  13. RATapi/examples/data/DSPC_SMW.dat +82 -0
  14. RATapi/examples/data/H2O_spin_down.dat +102 -0
  15. RATapi/examples/data/H2O_spin_up.dat +102 -0
  16. RATapi/examples/data/__init__.py +0 -0
  17. RATapi/examples/data/c_PLP0016596.dat +146 -0
  18. RATapi/examples/data/c_PLP0016601.dat +97 -0
  19. RATapi/examples/data/c_PLP0016607.dat +104 -0
  20. RATapi/examples/domains/__init__.py +0 -0
  21. RATapi/examples/domains/alloy_domains.py +26 -0
  22. RATapi/examples/domains/domains_XY_model.py +70 -0
  23. RATapi/examples/domains/domains_custom_XY.py +69 -0
  24. RATapi/examples/domains/domains_custom_layers.py +46 -0
  25. RATapi/examples/domains/domains_standard_layers.py +75 -0
  26. RATapi/examples/languages/__init__.py +0 -0
  27. RATapi/examples/languages/custom_bilayer.py +66 -0
  28. RATapi/examples/languages/run_custom_file_languages.py +41 -0
  29. RATapi/examples/languages/setup_problem.py +130 -0
  30. RATapi/examples/non_polarised/DSPC_custom_XY.py +140 -0
  31. RATapi/examples/non_polarised/DSPC_custom_layers.py +121 -0
  32. RATapi/examples/non_polarised/DSPC_standard_layers.py +358 -0
  33. RATapi/examples/non_polarised/__init__.py +0 -0
  34. RATapi/examples/non_polarised/custom_XY_DSPC.py +137 -0
  35. RATapi/examples/non_polarised/custom_bilayer_DSPC.py +85 -0
  36. RATapi/inputs.py +471 -0
  37. RATapi/models.py +288 -0
  38. RATapi/outputs.py +218 -0
  39. RATapi/project.py +659 -0
  40. RATapi/rat_core.cp312-win_amd64.pyd +0 -0
  41. RATapi/run.py +35 -0
  42. RATapi/utils/__init__.py +0 -0
  43. RATapi/utils/custom_errors.py +42 -0
  44. RATapi/utils/enums.py +101 -0
  45. RATapi/utils/plotting.py +211 -0
  46. RATapi/wrappers.py +115 -0
  47. RATapi-0.0.0.dev0.dist-info/METADATA +57 -0
  48. RATapi-0.0.0.dev0.dist-info/RECORD +64 -0
  49. RATapi-0.0.0.dev0.dist-info/WHEEL +5 -0
  50. RATapi-0.0.0.dev0.dist-info/top_level.txt +2 -0
  51. tests/__init__.py +0 -0
  52. tests/conftest.py +4728 -0
  53. tests/test_classlist.py +882 -0
  54. tests/test_controls.py +863 -0
  55. tests/test_custom_errors.py +49 -0
  56. tests/test_events.py +61 -0
  57. tests/test_inputs.py +908 -0
  58. tests/test_models.py +308 -0
  59. tests/test_outputs.py +31 -0
  60. tests/test_plotting.py +167 -0
  61. tests/test_project.py +1387 -0
  62. tests/test_run.py +570 -0
  63. tests/test_wrappers.py +57 -0
  64. tests/utils.py +171 -0
RATapi/inputs.py ADDED
@@ -0,0 +1,471 @@
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 typing import Callable, Union
7
+
8
+ import RATapi
9
+ import RATapi.controls
10
+ import RATapi.wrappers
11
+ from RATapi.rat_core import Cells, Checks, Control, Limits, Priors, ProblemDefinition
12
+ from RATapi.utils.enums import Calculations, Languages, LayerModels, TypeOptions
13
+
14
+
15
+ def make_input(
16
+ project: RATapi.Project, controls: RATapi.Controls
17
+ ) -> tuple[ProblemDefinition, Cells, Limits, Priors, Control]:
18
+ """Constructs the inputs required for the compiled RAT code using the data defined in the input project and
19
+ controls.
20
+
21
+ Parameters
22
+ ----------
23
+ project : RAT.Project
24
+ The project model, which defines the physical system under study.
25
+ controls : RAT.Controls
26
+ The controls model, which defines algorithmic properties.
27
+
28
+ Returns
29
+ -------
30
+ problem : RAT.rat_core.ProblemDefinition
31
+ The problem input used in the compiled RAT code.
32
+ cells : RAT.rat_core.Cells
33
+ The set of inputs that are defined in MATLAB as cell arrays.
34
+ limits : RAT.rat_core.Limits
35
+ A list of min/max values for each parameter defined in the project.
36
+ priors : RAT.rat_core.Priors
37
+ The priors defined for each parameter in the project.
38
+ cpp_controls : RAT.rat_core.Control
39
+ The controls object used in the compiled RAT code.
40
+
41
+ """
42
+ parameter_field = {
43
+ "parameters": "param",
44
+ "bulk_in": "bulkIn",
45
+ "bulk_out": "bulkOut",
46
+ "scalefactors": "scalefactor",
47
+ "domain_ratios": "domainRatio",
48
+ "background_parameters": "backgroundParam",
49
+ "resolution_parameters": "resolutionParam",
50
+ }
51
+ checks_field = {
52
+ "parameters": "fitParam",
53
+ "bulk_in": "fitBulkIn",
54
+ "bulk_out": "fitBulkOut",
55
+ "scalefactors": "fitScalefactor",
56
+ "domain_ratios": "fitDomainRatio",
57
+ "background_parameters": "fitBackgroundParam",
58
+ "resolution_parameters": "fitResolutionParam",
59
+ }
60
+
61
+ prior_id = {"uniform": 1, "gaussian": 2, "jeffreys": 3}
62
+
63
+ problem = make_problem(project)
64
+ cells = make_cells(project)
65
+
66
+ checks = Checks()
67
+ limits = Limits()
68
+ priors = Priors()
69
+
70
+ for class_list in RATapi.project.parameter_class_lists:
71
+ setattr(checks, checks_field[class_list], [int(element.fit) for element in getattr(project, class_list)])
72
+ setattr(
73
+ limits,
74
+ parameter_field[class_list],
75
+ [[element.min, element.max] for element in getattr(project, class_list)],
76
+ )
77
+ setattr(
78
+ priors,
79
+ parameter_field[class_list],
80
+ [[element.name, element.prior_type, element.mu, element.sigma] for element in getattr(project, class_list)],
81
+ )
82
+
83
+ # Use dummy values for qzshifts
84
+ checks.fitQzshift = []
85
+ limits.qzshift = []
86
+ priors.qzshift = []
87
+
88
+ priors.priorNames = [
89
+ param.name for class_list in RATapi.project.parameter_class_lists for param in getattr(project, class_list)
90
+ ]
91
+ priors.priorValues = [
92
+ [prior_id[param.prior_type], param.mu, param.sigma]
93
+ for class_list in RATapi.project.parameter_class_lists
94
+ for param in getattr(project, class_list)
95
+ ]
96
+
97
+ if project.model == LayerModels.CustomXY:
98
+ controls.calcSldDuringFit = True
99
+
100
+ cpp_controls = make_controls(controls, checks)
101
+
102
+ return problem, cells, limits, priors, cpp_controls
103
+
104
+
105
+ def make_problem(project: RATapi.Project) -> ProblemDefinition:
106
+ """Constructs the problem input required for the compiled RAT code.
107
+
108
+ Parameters
109
+ ----------
110
+ project : RAT.Project
111
+ The project model, which defines the physical system under study.
112
+
113
+ Returns
114
+ -------
115
+ problem : RAT.rat_core.ProblemDefinition
116
+ The problem input used in the compiled RAT code.
117
+
118
+ """
119
+ action_id = {"add": 1, "subtract": 2}
120
+
121
+ # Set contrast parameters according to model type
122
+ if project.model == LayerModels.StandardLayers:
123
+ contrast_custom_files = [float("NaN")] * len(project.contrasts)
124
+ else:
125
+ contrast_custom_files = [project.custom_files.index(contrast.model[0], True) for contrast in project.contrasts]
126
+
127
+ # Set background parameters, with -1 used to indicate a data background
128
+ contrast_background_params = []
129
+
130
+ for contrast in project.contrasts:
131
+ background = project.backgrounds[project.backgrounds.index(contrast.background)]
132
+ if background.type == TypeOptions.Data:
133
+ contrast_background_params.append(-1)
134
+ else:
135
+ contrast_background_params.append(project.background_parameters.index(background.value_1, True))
136
+
137
+ # Set resolution parameters, with -1 used to indicate a data resolution
138
+ contrast_resolution_params = []
139
+
140
+ for contrast in project.contrasts:
141
+ resolution = project.resolutions[project.resolutions.index(contrast.resolution)]
142
+ if resolution.type == TypeOptions.Data:
143
+ contrast_resolution_params.append(-1)
144
+ else:
145
+ contrast_resolution_params.append(project.resolution_parameters.index(resolution.value_1, True))
146
+
147
+ problem = ProblemDefinition()
148
+
149
+ problem.TF = project.calculation
150
+ problem.modelType = project.model
151
+ problem.geometry = project.geometry
152
+ problem.useImaginary = project.absorption
153
+ problem.params = [param.value for param in project.parameters]
154
+ problem.bulkIn = [param.value for param in project.bulk_in]
155
+ problem.bulkOut = [param.value for param in project.bulk_out]
156
+ problem.qzshifts = [0.0]
157
+ problem.scalefactors = [param.value for param in project.scalefactors]
158
+ problem.domainRatio = [param.value for param in project.domain_ratios]
159
+ problem.backgroundParams = [param.value for param in project.background_parameters]
160
+ problem.resolutionParams = [param.value for param in project.resolution_parameters]
161
+ problem.contrastBulkIns = [project.bulk_in.index(contrast.bulk_in, True) for contrast in project.contrasts]
162
+ problem.contrastBulkOuts = [project.bulk_out.index(contrast.bulk_out, True) for contrast in project.contrasts]
163
+ problem.contrastQzshifts = [0] * len(project.contrasts) # This is marked as "to do" in RAT
164
+ problem.contrastScalefactors = [
165
+ project.scalefactors.index(contrast.scalefactor, True) for contrast in project.contrasts
166
+ ]
167
+ problem.contrastDomainRatios = [
168
+ project.domain_ratios.index(contrast.domain_ratio, True) if hasattr(contrast, "domain_ratio") else 0
169
+ for contrast in project.contrasts
170
+ ]
171
+ problem.contrastBackgroundParams = contrast_background_params
172
+ problem.contrastBackgroundActions = [action_id[contrast.background_action] for contrast in project.contrasts]
173
+ problem.contrastResolutionParams = contrast_resolution_params
174
+ problem.contrastCustomFiles = contrast_custom_files
175
+ problem.resample = make_resample(project)
176
+ problem.dataPresent = make_data_present(project)
177
+ problem.oilChiDataPresent = [0] * len(project.contrasts)
178
+ problem.numberOfContrasts = len(project.contrasts)
179
+ problem.numberOfLayers = len(project.layers)
180
+ problem.numberOfDomainContrasts = len(project.domain_contrasts)
181
+ problem.fitParams = [
182
+ param.value
183
+ for class_list in RATapi.project.parameter_class_lists
184
+ for param in getattr(project, class_list)
185
+ if param.fit
186
+ ]
187
+ problem.fitLimits = [
188
+ [param.min, param.max]
189
+ for class_list in RATapi.project.parameter_class_lists
190
+ for param in getattr(project, class_list)
191
+ if param.fit
192
+ ]
193
+ problem.otherParams = [
194
+ param.value
195
+ for class_list in RATapi.project.parameter_class_lists
196
+ for param in getattr(project, class_list)
197
+ if not param.fit
198
+ ]
199
+ problem.otherLimits = [
200
+ [param.min, param.max]
201
+ for class_list in RATapi.project.parameter_class_lists
202
+ for param in getattr(project, class_list)
203
+ if not param.fit
204
+ ]
205
+
206
+ check_indices(problem)
207
+
208
+ return problem
209
+
210
+
211
+ def make_resample(project: RATapi.Project) -> list[int]:
212
+ """Constructs the "resample" field of the problem input required for the compiled RAT code.
213
+
214
+ Parameters
215
+ ----------
216
+ project : RAT.Project
217
+ The project model, which defines the physical system under study.
218
+
219
+ Returns
220
+ -------
221
+ : list[int]
222
+ The "resample" field of the problem input used in the compiled RAT code.
223
+
224
+ """
225
+ return [contrast.resample for contrast in project.contrasts]
226
+
227
+
228
+ def make_data_present(project: RATapi.Project) -> list[int]:
229
+ """Constructs the "dataPresent" field of the problem input required for the compiled RAT code.
230
+
231
+ Parameters
232
+ ----------
233
+ project : RAT.Project
234
+ The project model, which defines the physical system under study.
235
+
236
+ Returns
237
+ -------
238
+ : list[int]
239
+ The "dataPresent" field of the problem input used in the compiled RAT code.
240
+
241
+ """
242
+ return [
243
+ 1 if project.data[project.data.index(contrast.data)].data.size != 0 else 0 for contrast in project.contrasts
244
+ ]
245
+
246
+
247
+ def check_indices(problem: ProblemDefinition) -> None:
248
+ """Checks the indices in contrast lists in a ProblemDefinition object lie within the range of the corresponding
249
+ parameter lists.
250
+
251
+ Parameters
252
+ ----------
253
+ problem : RAT.rat_core.ProblemDefinition
254
+ The problem input used in the compiled RAT code.
255
+
256
+ """
257
+ index_list = {
258
+ "bulkIn": "contrastBulkIns",
259
+ "bulkOut": "contrastBulkOuts",
260
+ "scalefactors": "contrastScalefactors",
261
+ "domainRatio": "contrastDomainRatios",
262
+ "backgroundParams": "contrastBackgroundParams",
263
+ "resolutionParams": "contrastResolutionParams",
264
+ }
265
+
266
+ # Check the indices -- note we have switched to 1-based indexing at this point
267
+ for params in index_list:
268
+ param_list = getattr(problem, params)
269
+ if len(param_list) > 0 and not all(
270
+ (element > 0 or element == -1) and element <= len(param_list)
271
+ for element in getattr(problem, index_list[params])
272
+ ):
273
+ elements = [
274
+ element
275
+ for element in getattr(problem, index_list[params])
276
+ if not ((element > 0 or element == -1) and element <= len(param_list))
277
+ ]
278
+ raise IndexError(
279
+ f'The problem field "{index_list[params]}" contains: {", ".join(str(i) for i in elements)}'
280
+ f', which lie outside of the range of "{params}"',
281
+ )
282
+
283
+
284
+ def make_cells(project: RATapi.Project) -> Cells:
285
+ """Constructs the cells input required for the compiled RAT code.
286
+
287
+ Note that the order of the inputs (i.e, f1 to f20) has been hard--coded into the compiled RAT code.
288
+
289
+ Parameters
290
+ ----------
291
+ project : RAT.Project
292
+ The project model, which defines the physical system under study.
293
+
294
+ Returns
295
+ -------
296
+ cells : RAT.rat_core.Cells
297
+ The set of inputs that are defined in MATLAB as cell arrays.
298
+
299
+ """
300
+ hydrate_id = {"bulk in": 1, "bulk out": 2}
301
+
302
+ # Set contrast parameters according to model type
303
+ if project.model == LayerModels.StandardLayers:
304
+ if project.calculation == Calculations.Domains:
305
+ contrast_models = [
306
+ [project.domain_contrasts.index(domain_contrast, True) for domain_contrast in contrast.model]
307
+ for contrast in project.contrasts
308
+ ]
309
+ else:
310
+ contrast_models = [
311
+ [project.layers.index(layer, True) for layer in contrast.model] for contrast in project.contrasts
312
+ ]
313
+ else:
314
+ contrast_models = [[]] * len(project.contrasts)
315
+
316
+ # Get details of defined layers
317
+ layer_details = []
318
+ for layer in project.layers:
319
+ layer_params = [
320
+ project.parameters.index(getattr(layer, attribute), True)
321
+ for attribute in list(layer.model_fields.keys())[1:-2]
322
+ ]
323
+ layer_params.append(project.parameters.index(layer.hydration, True) if layer.hydration else float("NaN"))
324
+ layer_params.append(hydrate_id[layer.hydrate_with])
325
+
326
+ layer_details.append(layer_params)
327
+
328
+ # Find contrast data in project.data classlist
329
+ all_data = []
330
+ data_limits = []
331
+ simulation_limits = []
332
+
333
+ for contrast in project.contrasts:
334
+ data_index = project.data.index(contrast.data)
335
+ all_data.append(project.data[data_index].data)
336
+ data_range = project.data[data_index].data_range
337
+ simulation_range = project.data[data_index].simulation_range
338
+
339
+ if data_range:
340
+ data_limits.append(data_range)
341
+ else:
342
+ data_limits.append([0.0, 0.0])
343
+
344
+ if simulation_range:
345
+ simulation_limits.append(simulation_range)
346
+ else:
347
+ simulation_limits.append([0.0, 0.0])
348
+
349
+ file_handles = []
350
+ for custom_file in project.custom_files:
351
+ full_path = os.path.join(custom_file.path, custom_file.filename)
352
+ if custom_file.language == Languages.Python:
353
+ file_handles.append(get_python_handle(custom_file.filename, custom_file.function_name, custom_file.path))
354
+ elif custom_file.language == Languages.Matlab:
355
+ file_handles.append(RATapi.wrappers.MatlabWrapper(full_path).getHandle())
356
+ elif custom_file.language == Languages.Cpp:
357
+ file_handles.append(RATapi.wrappers.DylibWrapper(full_path, custom_file.function_name).getHandle())
358
+
359
+ # Populate the set of cells
360
+ cells = Cells()
361
+ cells.f1 = [[0, 1]] * len(project.contrasts) # This is marked as "to do" in RAT
362
+ cells.f2 = all_data
363
+ cells.f3 = data_limits
364
+ cells.f4 = simulation_limits
365
+ cells.f5 = [contrast_model if contrast_model else [0] for contrast_model in contrast_models]
366
+ cells.f6 = layer_details if project.model == LayerModels.StandardLayers else [[0]]
367
+ cells.f7 = [param.name for param in project.parameters]
368
+ cells.f8 = [param.name for param in project.background_parameters]
369
+ cells.f9 = [param.name for param in project.scalefactors]
370
+ cells.f10 = [] # Placeholder for qzshifts
371
+ cells.f11 = [param.name for param in project.bulk_in]
372
+ cells.f12 = [param.name for param in project.bulk_out]
373
+ cells.f13 = [param.name for param in project.resolution_parameters]
374
+ cells.f14 = file_handles
375
+ cells.f15 = [param.type for param in project.backgrounds]
376
+ cells.f16 = [param.type for param in project.resolutions]
377
+
378
+ cells.f17 = [[[]]] * len(project.contrasts) # Placeholder for oil chi data
379
+ cells.f18 = [[0, 1]] * len(project.domain_contrasts) # This is marked as "to do" in RAT
380
+
381
+ domain_contrast_models = [
382
+ [project.layers.index(layer, True) for layer in domain_contrast.model]
383
+ for domain_contrast in project.domain_contrasts
384
+ ]
385
+ cells.f19 = [
386
+ domain_contrast_model if domain_contrast_model else 0 for domain_contrast_model in domain_contrast_models
387
+ ]
388
+
389
+ cells.f20 = [param.name for param in project.domain_ratios]
390
+
391
+ return cells
392
+
393
+
394
+ def get_python_handle(file_name: str, function_name: str, path: Union[str, pathlib.Path] = "") -> Callable:
395
+ """Get the function handle from a function defined in a python module located anywhere within the filesystem.
396
+
397
+ Parameters
398
+ ----------
399
+ file_name : str
400
+ The name of the file containing the function of interest.
401
+ function_name : str
402
+ The name of the function we wish to obtain the handle for within the module.
403
+ path : str
404
+ The path to the file containing the function (default is "", which represent the working directory).
405
+
406
+ Returns
407
+ -------
408
+ handle : Callable
409
+ The handle of the function defined in the python module file.
410
+
411
+ """
412
+ spec = importlib.util.spec_from_file_location(pathlib.Path(file_name).stem, os.path.join(path, file_name))
413
+ custom_module = importlib.util.module_from_spec(spec)
414
+ spec.loader.exec_module(custom_module)
415
+ handle = getattr(custom_module, function_name)
416
+ return handle
417
+
418
+
419
+ def make_controls(input_controls: RATapi.Controls, checks: Checks) -> Control:
420
+ """Converts the controls object to the format required by the compiled RAT code.
421
+
422
+ Parameters
423
+ ----------
424
+ input_controls : RAT.Controls
425
+ The controls model, which defines algorithmic properties.
426
+ checks : Rat.rat_core.Checks
427
+ States whether or not to fit each parameter defined in the project.
428
+
429
+ Returns
430
+ -------
431
+ controls : RAT.rat_core.Control
432
+ The controls object used in the compiled RAT code.
433
+
434
+ """
435
+ controls = Control()
436
+
437
+ controls.procedure = input_controls.procedure
438
+ controls.parallel = input_controls.parallel
439
+ controls.calcSldDuringFit = input_controls.calcSldDuringFit
440
+ controls.resampleParams = input_controls.resampleParams
441
+ controls.display = input_controls.display
442
+ # Simplex
443
+ controls.xTolerance = input_controls.xTolerance
444
+ controls.funcTolerance = input_controls.funcTolerance
445
+ controls.maxFuncEvals = input_controls.maxFuncEvals
446
+ controls.maxIterations = input_controls.maxIterations
447
+ controls.updateFreq = input_controls.updateFreq
448
+ controls.updatePlotFreq = input_controls.updatePlotFreq
449
+ # DE
450
+ controls.populationSize = input_controls.populationSize
451
+ controls.fWeight = input_controls.fWeight
452
+ controls.crossoverProbability = input_controls.crossoverProbability
453
+ controls.strategy = input_controls.strategy.value # Required for the IntEnum
454
+ controls.targetValue = input_controls.targetValue
455
+ controls.numGenerations = input_controls.numGenerations
456
+ # NS
457
+ controls.nLive = input_controls.nLive
458
+ controls.nMCMC = input_controls.nMCMC
459
+ controls.propScale = input_controls.propScale
460
+ controls.nsTolerance = input_controls.nsTolerance
461
+ # Dream
462
+ controls.nSamples = input_controls.nSamples
463
+ controls.nChains = input_controls.nChains
464
+ controls.jumpProbability = input_controls.jumpProbability
465
+ controls.pUnitGamma = input_controls.pUnitGamma
466
+ controls.boundHandling = input_controls.boundHandling
467
+ controls.adaptPCR = input_controls.adaptPCR
468
+ # Checks
469
+ controls.checks = checks
470
+
471
+ return controls