Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changes/2003.maintenance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve model parameter overwrite function with an explicit `flat_dict` statement to avoid being flooded by warnings like `WARNING::model_parameter(l519)::overwrite_parameters::Parameter MSTS-05 not found in model MSTS-12, cannot overwrite it.` (note the error in the warning).
20 changes: 14 additions & 6 deletions src/simtools/model/model_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,17 +490,17 @@ def _get_key_for_parameter_changes(self, site, array_element_name, changes_data)

return None

def overwrite_parameters(self, changes):
def overwrite_parameters(self, changes, flat_dict=False):
"""
Change the value of multiple existing parameters in the model.

This function does not modify the DB, it affects only the current instance.

Allows for two types of 'changes' dictionary:

- simple: '{parameter_name: new_value, ...}'
- model repository style:
'{parameter_name: {"value": new_value, "version": new_version}, ...}'
- simple (flat_dict=True): '{parameter_name: new_value, ...}'
- model repository style (flat_dict=False):
'{array_element: {parameter_name: {"value": new_value, "version": new_version}, ...}}'

Parameters
----------
Expand All @@ -509,11 +509,19 @@ def overwrite_parameters(self, changes):
"""
if not changes:
return
key_for_changes = self._get_key_for_parameter_changes(self.site, self.name, changes)
changes = changes.get(key_for_changes, {}) if key_for_changes else changes
if not flat_dict:
key_for_changes = self._get_key_for_parameter_changes(self.site, self.name, changes)
changes = changes.get(key_for_changes, {})
if not changes:
return

if flat_dict:
self._logger.debug(f"Overwriting parameters with changes: {changes}")
else:
self._logger.debug(
f"Overwriting parameters for {key_for_changes} with changes: {changes}"
)

for par_name, par_value in changes.items():
if par_name not in self.parameters:
self._logger.warning(
Expand Down
2 changes: 1 addition & 1 deletion src/simtools/ray_tracing/psf_parameter_optimisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ def _run_ray_tracing_simulation(tel_model, site_model, args_dict, pars):
if pars is None:
raise ValueError("No best parameters found")

tel_model.overwrite_parameters(pars)
tel_model.overwrite_parameters(pars, flat_dict=True)
ray = RayTracing(
telescope_model=tel_model,
site_model=site_model,
Expand Down
2 changes: 1 addition & 1 deletion src/simtools/visualization/plot_psf.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ def create_psf_vs_offaxis_plot(tel_model, site_model, args_dict, best_pars, outp
logger.info(f"Creating {psf_label_cm} vs off-axis angle plot with best parameters...")

# Apply best parameters to telescope model
tel_model.overwrite_parameters(best_pars)
tel_model.overwrite_parameters(best_pars, flat_dict=True)

# Create off-axis angle array
max_offset = args_dict.get("max_offset", MAX_OFFSET_DEFAULT)
Expand Down
10 changes: 5 additions & 5 deletions tests/resources/info_test_model_parameter_changes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ changes:
LSTS-01:
effective_focal_length:
version: '3.0.0'
value: '2923.7 0 0 2 0'
value: [2923.7, 0., 0., 2, 0.]
# unchanged parameter for other LSTSs (test that no changes are applied)
LSTS-design:
effective_focal_length:
version: '3.0.0'
value: '2923.7 0 0 0 0'
value: [2923.7, 0., 0., 0., 0.]
# no changes expected for MSTs
MSTS-05:
effective_focal_length:
version: '3.0.0'
value: '1644.51 0 0 0 0'
value: [1644.51, 0., 0., 0., 0.]
# change parameters for all SSTSs except SSTS-22
SSTS-design:
effective_focal_length:
version: '3.0.0'
value: '315.191 0 0 0 0'
value: [315.191, 0., 0., 0., 0.]
SSTS-22:
effective_focal_length:
version: '3.0.0'
value: '215.191 0 0 0 0'
value: [215.191, 0., 0., 0., 0.]
6 changes: 3 additions & 3 deletions tests/unit_tests/model/test_model_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def test_overwrite_parameters(telescope_model_lst, mocker):
telescope_copy = copy.deepcopy(telescope_model_lst)
mock_change = mocker.patch.object(TelescopeModel, "overwrite_model_parameter")
telescope_copy.overwrite_parameters(
{"camera_pixels": {"value": 9999}, "mirror_focal_length": {"value": 55}}
{"camera_pixels": {"value": 9999}, "mirror_focal_length": {"value": 55}}, flat_dict=True
)
mock_change.assert_any_call("camera_pixels", 9999, None)
mock_change.assert_any_call("mirror_focal_length", 55, None)
Expand Down Expand Up @@ -509,7 +509,7 @@ def test_overwrite_parameters_with_version_dict(telescope_model_lst):

changes = {"num_gains": {"value": 4, "version": "2.0.0"}}

tel_model.overwrite_parameters(changes)
tel_model.overwrite_parameters(changes, flat_dict=True)

assert tel_model.parameters["num_gains"]["value"] == 4
assert tel_model.parameters["num_gains"]["parameter_version"] == "2.0.0"
Expand All @@ -522,7 +522,7 @@ def test_overwrite_parameters_with_simple_value(telescope_model_lst):
# Simple value (not a dict with 'value' or 'version' keys)
changes = {"num_gains": 5}

tel_model.overwrite_parameters(changes)
tel_model.overwrite_parameters(changes, flat_dict=True)

assert tel_model.parameters["num_gains"]["value"] == 5

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def test__run_ray_tracing_simulation(
mock_telescope_model, mock_site_model, mock_args_dict, pars
)
assert psf_diameter == pytest.approx(expected_psf_diameter)
mock_telescope_model.overwrite_parameters.assert_called_once_with(pars)
mock_telescope_model.overwrite_parameters.assert_called_once_with(pars, flat_dict=True)


@pytest.mark.parametrize(
Expand Down
10 changes: 5 additions & 5 deletions tests/unit_tests/visualization/test_plot_psf.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,11 @@ def test_create_psf_vs_offaxis_plot(sample_parameters, tmp_path):
plt.close("all")

# Verify telescope parameters were applied and simulation was run
mock_telescope_model.overwrite_parameters.assert_called_once_with(sample_parameters)
assert mock_ray.simulate.call_count == 1
assert mock_ray.analyze.call_count == 1
assert mock_ray.plot.call_count == 2
assert mock_save_figure.call_count == 2

mock_telescope_model.overwrite_parameters.assert_called_once_with(
sample_parameters, flat_dict=True
)
assert mock_save_figure.call_count >= 1 # At least one save call


def test_plot_psf_histogram_returns_none_when_not_configured(tmp_path):
Expand Down