Skip to content

Commit 31032b5

Browse files
authored
Ignore unused custom files and update to dev17 (#214)
1 parent 4a9767a commit 31032b5

4 files changed

Lines changed: 57 additions & 7 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ build-backend = 'setuptools.build_meta'
88

99
[project]
1010
name = "ratapi"
11-
version = "0.0.0.dev16"
11+
version = "0.0.0.dev17"
1212
description = "Python extension for the Reflectivity Analysis Toolbox (RAT)"
1313
readme = "README.md"
1414
requires-python = ">=3.10"

ratapi/inputs.py

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,38 @@ def get_python_handle(file_name: str, function_name: str, path: str | pathlib.Pa
4949
return handle
5050

5151

52+
def get_used_custom_files(project):
53+
"""Get custom files referenced in the project.
54+
55+
Parameters
56+
----------
57+
project : RAT.Project
58+
The project model, which defines the physical system under study.
59+
60+
Returns
61+
-------
62+
files : ClassList[CustomFile]
63+
A list of custom file models used in the project.
64+
65+
"""
66+
used_custom_files = {}
67+
files = {file.name: file for file in project.custom_files}
68+
if project.model != "standard layers":
69+
for contrast in project.contrasts:
70+
if contrast.model:
71+
used_custom_files[contrast.model[0]] = files[contrast.model[0]]
72+
73+
for background in project.backgrounds:
74+
if background.type == "function":
75+
used_custom_files[background.source] = files[background.source]
76+
77+
for resolution in project.resolutions:
78+
if resolution.type == "function":
79+
used_custom_files[resolution.source] = files[resolution.source]
80+
81+
return ratapi.ClassList(list(used_custom_files.values()))
82+
83+
5284
class FileHandles:
5385
"""Class to defer creation of custom file handles.
5486
@@ -206,10 +238,11 @@ def make_problem(project: ratapi.Project, validate_range: bool = False) -> Probl
206238
contrast_models = [[]] * len(project.contrasts)
207239

208240
# Set contrast parameters according to model type
241+
used_custom_files = get_used_custom_files(project)
209242
if project.model == LayerModels.StandardLayers:
210243
contrast_custom_files = [float("NaN")] * len(project.contrasts)
211244
else:
212-
contrast_custom_files = [project.custom_files.index(contrast.model[0], True) for contrast in project.contrasts]
245+
contrast_custom_files = [used_custom_files.index(contrast.model[0], True) for contrast in project.contrasts]
213246

214247
# Get details of defined layers
215248
layer_details = get_layer_details(project)
@@ -253,7 +286,7 @@ def make_problem(project: ratapi.Project, validate_range: bool = False) -> Probl
253286
data = append_data_background(data, project.data[background.source].data)
254287

255288
elif background.type == TypeOptions.Function:
256-
contrast_background_param.append(project.custom_files.index(background.source, True))
289+
contrast_background_param.append(used_custom_files.index(background.source, True))
257290
contrast_background_param.extend(
258291
[
259292
project.background_parameters.index(value, True)
@@ -278,7 +311,7 @@ def make_problem(project: ratapi.Project, validate_range: bool = False) -> Probl
278311
contrast_resolution_types.append(resolution.type)
279312
contrast_resolution_param = []
280313
if resolution.type == TypeOptions.Function:
281-
contrast_resolution_param.append(project.custom_files.index(resolution.source, True))
314+
contrast_resolution_param.append(used_custom_files.index(resolution.source, True))
282315
contrast_resolution_param.extend(
283316
[
284317
project.resolution_parameters.index(value, True)
@@ -334,7 +367,7 @@ def make_problem(project: ratapi.Project, validate_range: bool = False) -> Probl
334367
problem.numberOfLayers = len(project.layers)
335368
problem.contrastLayers = [contrast_model if contrast_model else [] for contrast_model in contrast_models]
336369
problem.layersDetails = layer_details if project.model == LayerModels.StandardLayers else []
337-
problem.customFiles = FileHandles(project.custom_files)
370+
problem.customFiles = FileHandles(used_custom_files)
338371
problem.modelType = project.model
339372
problem.contrastCustomFiles = contrast_custom_files
340373

ratapi/wrappers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def __init__(self, filename: str) -> None:
5151
raise ImportError(self.loader_error_message) from None
5252

5353
self.engine = self.loader.result()
54-
path = pathlib.Path(filename)
54+
path = pathlib.Path(filename).resolve()
5555
self.engine.cd(str(path.parent), nargout=0)
5656
self.function_name = path.stem
5757

tests/test_inputs.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import ratapi
1212
import ratapi.wrappers
13-
from ratapi.inputs import FileHandles, check_indices, make_controls, make_input, make_problem
13+
from ratapi.inputs import FileHandles, check_indices, get_used_custom_files, make_controls, make_input, make_problem
1414
from ratapi.rat_core import Checks, Control, NameStore, ProblemDefinition
1515
from ratapi.utils.enums import (
1616
BackgroundActions,
@@ -466,6 +466,23 @@ def test_make_input(test_project, test_problem, test_controls, request) -> None:
466466
check_controls_equal(controls, test_controls)
467467

468468

469+
def test_get_used_custom_files(custom_xy_project):
470+
"""Test unused custom files are removed."""
471+
472+
used_custom_files = get_used_custom_files(custom_xy_project)
473+
assert len(used_custom_files) == len(custom_xy_project.custom_files)
474+
assert used_custom_files[0] == custom_xy_project.custom_files[0]
475+
476+
custom_xy_project.custom_files.append(name="Test Custom File2", filename="matlab_test.m", language="matlab")
477+
used_custom_files = get_used_custom_files(custom_xy_project)
478+
assert len(used_custom_files) == 1
479+
assert used_custom_files[0].name == custom_xy_project.custom_files[0].name
480+
481+
custom_xy_project.backgrounds.append(name="b2", type="function", source="Test Custom File2")
482+
used_custom_files = get_used_custom_files(custom_xy_project)
483+
assert len(used_custom_files) == len(custom_xy_project.custom_files)
484+
485+
469486
@pytest.mark.parametrize(
470487
["test_project", "test_problem"],
471488
[

0 commit comments

Comments
 (0)