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