@@ -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+
5284class 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
0 commit comments