From c684962425d7dce6cf65fdc01f3649b23db21c9a Mon Sep 17 00:00:00 2001 From: PONS Date: Thu, 5 Feb 2026 13:40:57 +0100 Subject: [PATCH] Clear factory before loading --- pyaml/accelerator.py | 2 ++ tests/test_arrays.py | 10 +++------- tests/test_bpm.py | 9 --------- tests/test_bpm_controlsystem.py | 7 ------- tests/test_chromaticity_monitor.py | 5 ----- tests/test_errors.py | 6 ------ tests/test_ident_models.py | 4 ---- tests/test_load_quad.py | 2 -- tests/test_ranges_cfm_deviceaccess.py | 23 +++++++++++++---------- tests/test_rf.py | 7 ------- tests/test_serialized_magnets.py | 6 +++--- tests/test_tune.py | 3 --- tests/test_tune_hardware.py | 6 +++--- tests/test_tune_monitor.py | 9 +++------ tests/test_tuning_dispersion.py | 7 ------- tests/test_tuning_orbit_correction.py | 5 ----- tests/test_tuning_orm.py | 5 ----- tests/test_tuning_tools.py | 3 --- 18 files changed, 27 insertions(+), 92 deletions(-) diff --git a/pyaml/accelerator.py b/pyaml/accelerator.py index 1f5fe5c4..0300f354 100644 --- a/pyaml/accelerator.py +++ b/pyaml/accelerator.py @@ -153,6 +153,8 @@ def from_dict(config_dict: dict, ignore_external=False) -> "Accelerator": if ignore_external: # control systems are external, so remove controls field config_dict.pop("controls", None) + # Ensure factory is clean before building a new accelerator + Factory.clear() return Factory.depth_first_build(config_dict, ignore_external) @staticmethod diff --git a/tests/test_arrays.py b/tests/test_arrays.py index eefd7281..37b70365 100644 --- a/tests/test_arrays.py +++ b/tests/test_arrays.py @@ -9,12 +9,10 @@ from pyaml.arrays.bpm_array import BPMArray from pyaml.arrays.cfm_magnet import CombinedFunctionMagnet from pyaml.arrays.cfm_magnet import ConfigModel as CombinedFunctionMagnetConfigModel -from pyaml.arrays.cfm_magnet_array import CombinedFunctionMagnetArray from pyaml.arrays.element_array import ElementArray from pyaml.arrays.magnet import ConfigModel as MagnetArrayConfigModel from pyaml.arrays.magnet import Magnet from pyaml.arrays.magnet_array import MagnetArray -from pyaml.configuration.factory import Factory @pytest.mark.parametrize( @@ -205,8 +203,6 @@ def test_arrays(install_test_package): bpmsLive = BPMArray("", sr.live.get_all_bpms()) bpmsLive.positions.get() - Factory.clear() - # Test dynamic arrays sr: Accelerator = Accelerator.load( @@ -244,7 +240,6 @@ def test_arrays(install_test_package): v = sr.design.get_cfm_magnets("emptyCFM").strengths.get() # Ensure good attach assert np.shape(v) == (0,) - Factory.clear() @pytest.mark.parametrize( "sr_file", @@ -253,7 +248,9 @@ def test_arrays(install_test_package): ], ) def test_serialized_magnets_arrays(sr_file): - sr: Accelerator = Accelerator.load(sr_file, use_fast_loader=True, ignore_external=True) + sr: Accelerator = Accelerator.load( + sr_file, use_fast_loader=True, ignore_external=True + ) the_serie = sr.design.get_serialized_magnets("series") strength = the_serie.strengths.get() assert len(strength) == 5 @@ -263,4 +260,3 @@ def test_serialized_magnets_arrays(sr_file): assert len(hardwares) == 5 print(hardwares) the_serie.hardwares.set([10]) - diff --git a/tests/test_bpm.py b/tests/test_bpm.py index be334f57..914d179f 100644 --- a/tests/test_bpm.py +++ b/tests/test_bpm.py @@ -2,7 +2,6 @@ import pytest from pyaml.accelerator import Accelerator -from pyaml.configuration.factory import Factory def test_simulator_bpm_tilt(): @@ -13,8 +12,6 @@ def test_simulator_bpm_tilt(): bpm.tilt.set(0.01) assert bpm.tilt.get() == 0.01 - Factory.clear() - def test_simulator_bpm_offset(): sr: Accelerator = Accelerator.load("tests/config/bpms.yaml", ignore_external=True) @@ -28,8 +25,6 @@ def test_simulator_bpm_offset(): assert bpm.offset.get()[1] == 0.2 assert np.allclose(bpm.positions.get(), np.array([0.0, 0.0])) - Factory.clear() - @pytest.mark.parametrize( "install_test_package", @@ -45,8 +40,6 @@ def test_simulator_bpm_position(install_test_package): assert np.allclose(bpm.positions.get(), np.array([0.0, 0.0])) assert np.allclose(bpm_simple.positions.get(), np.array([0.0, 0.0])) - Factory.clear() - def test_simulator_bpm_position_with_bad_corrector_strength(): sr: Accelerator = Accelerator.load("tests/config/bpms.yaml", ignore_external=True) @@ -60,5 +53,3 @@ def test_simulator_bpm_position_with_bad_corrector_strength(): for bpm in [bpm1, bpm_simple, bpm3]: assert bpm.positions.get()[0] != 0.0 assert bpm.positions.get()[1] != 0.0 - - Factory.clear() diff --git a/tests/test_bpm_controlsystem.py b/tests/test_bpm_controlsystem.py index 3e5ed269..83c64b98 100644 --- a/tests/test_bpm_controlsystem.py +++ b/tests/test_bpm_controlsystem.py @@ -2,7 +2,6 @@ import pytest from pyaml.accelerator import Accelerator -from pyaml.configuration.factory import Factory @pytest.mark.parametrize( @@ -19,8 +18,6 @@ def test_controlsystem_bpm_tilt(install_test_package): bpm.tilt.set(0.01) assert bpm.tilt.get() == 0.01 - Factory.clear() - @pytest.mark.parametrize( "install_test_package", @@ -38,8 +35,6 @@ def test_controlsystem_bpm_offset(install_test_package): assert bpm.offset.get()[1] == 0.2 assert np.allclose(bpm.positions.get(), np.array([0.0, 0.0])) - Factory.clear() - @pytest.mark.parametrize( "install_test_package", @@ -65,5 +60,3 @@ def test_controlsystem_bpm_position_indexed(install_test_package): bpm = sr.live.get_bpm("BPM_C01-04") assert np.allclose(bpm.positions.get(), np.array([0.0, 1.0])) - - Factory.clear() diff --git a/tests/test_chromaticity_monitor.py b/tests/test_chromaticity_monitor.py index 745c7db3..e24d99f5 100644 --- a/tests/test_chromaticity_monitor.py +++ b/tests/test_chromaticity_monitor.py @@ -2,7 +2,6 @@ import pytest from pyaml.accelerator import Accelerator -from pyaml.configuration.factory import Factory def test_simulator_chromaticity_monitor(): @@ -20,8 +19,6 @@ def test_simulator_chromaticity_monitor(): == sr.design.get_lattice().get_chrom()[1] ) - Factory.clear() - @pytest.mark.parametrize( "install_test_package", @@ -44,5 +41,3 @@ def test_controlsystem_chromaticity_monitor(install_test_package): ksi = np.abs(chromaticity_monitor.chromaticity.get()) assert abs(ksi[0]) < 1e-17 assert abs(ksi[1]) < 1e-17 - - Factory.clear() diff --git a/tests/test_errors.py b/tests/test_errors.py index 2c7be4a8..a71d28b4 100644 --- a/tests/test_errors.py +++ b/tests/test_errors.py @@ -3,7 +3,6 @@ from pyaml.accelerator import Accelerator from pyaml.arrays.magnet_array import MagnetArray from pyaml.common.exception import PyAMLConfigException, PyAMLException -from pyaml.configuration.factory import Factory @pytest.mark.parametrize( @@ -15,18 +14,15 @@ def test_tune(install_test_package): with pytest.raises(PyAMLConfigException) as exc: ml: Accelerator = Accelerator.load("tests/config/bad_conf_duplicate_1.yaml") assert "MagnetArray HCORR : duplicate name SH1A-C02-H @index 2" in str(exc) - Factory.clear() with pytest.raises(PyAMLConfigException) as exc: ml: Accelerator = Accelerator.load("tests/config/bad_conf_duplicate_2.yaml") assert "BPMArray BPM : duplicate name BPM_C04-06 @index 3" in str(exc) - Factory.clear() with pytest.raises(PyAMLConfigException) as exc: ml: Accelerator = Accelerator.load("tests/config/bad_conf_duplicate_3.yaml") assert "element BPM_C04-06 already defined" in str(exc) assert "line 58, column 3" in str(exc) - Factory.clear() sr: Accelerator = Accelerator.load("tests/config/EBSTune.yaml") m1 = sr.live.get_magnet("QF1E-C04") @@ -45,5 +41,3 @@ def test_tune(install_test_package): with pytest.raises(PyAMLException) as exc: m2 = sr.design.get_bpm("QF1A-C05XX") assert "BPM QF1A-C05XX not defined" in str(exc) - - Factory.clear() diff --git a/tests/test_ident_models.py b/tests/test_ident_models.py index 4b0d9dcc..03582404 100644 --- a/tests/test_ident_models.py +++ b/tests/test_ident_models.py @@ -3,9 +3,7 @@ from pyaml.accelerator import Accelerator from pyaml.configuration.factory import Factory -from pyaml.magnet.cfm_magnet import CombinedFunctionMagnet from pyaml.magnet.hcorrector import HCorrector -from pyaml.magnet.model import MagnetModel from pyaml.magnet.vcorrector import VCorrector @@ -44,5 +42,3 @@ def test_cfm_magnets(magnet_file, install_test_package): assert np.abs(o[1] - 3.39661431e-07) < 1e-10 assert np.abs(o[2] + 1.59928207e-06) < 1e-10 assert np.abs(o[3] + 1.74771216e-05) < 1e-10 - - Factory.clear() diff --git a/tests/test_load_quad.py b/tests/test_load_quad.py index fd5a735b..22d9f3c1 100644 --- a/tests/test_load_quad.py +++ b/tests/test_load_quad.py @@ -16,8 +16,6 @@ ) from pyaml.magnet.cfm_magnet import CombinedFunctionMagnet from pyaml.magnet.hcorrector import HCorrector -from pyaml.magnet.identity_model import IdentityMagnetModel -from pyaml.magnet.quadrupole import ConfigModel as QuadrupoleConfigModel from pyaml.magnet.quadrupole import Quadrupole # TODO: Generate JSON pydantic schema for MetaConfigurator diff --git a/tests/test_ranges_cfm_deviceaccess.py b/tests/test_ranges_cfm_deviceaccess.py index 76b506b7..887558e7 100644 --- a/tests/test_ranges_cfm_deviceaccess.py +++ b/tests/test_ranges_cfm_deviceaccess.py @@ -3,7 +3,6 @@ from pyaml import PyAMLException from pyaml.accelerator import Accelerator -from pyaml.configuration.factory import Factory def _in_range(vmin, vmax) -> float: @@ -21,7 +20,9 @@ def _out_of_range(vmin, vmax) -> float: return float(vmax) + 0.1 if vmin is not None: return float(vmin) - 0.1 - raise RuntimeError("Unbounded range [None, None], cannot build an out-of-range value.") + raise RuntimeError( + "Unbounded range [None, None], cannot build an out-of-range value." + ) @pytest.mark.parametrize( @@ -34,7 +35,9 @@ def _out_of_range(vmin, vmax) -> float: ], indirect=["install_test_package"], ) -def test_cfm_ranges_from_yaml_are_propagated_and_enforced(magnet_file, install_test_package): +def test_cfm_ranges_from_yaml_are_propagated_and_enforced( + magnet_file, install_test_package +): sr: Accelerator = Accelerator.load(magnet_file) sr.design.get_lattice().disable_6d() @@ -55,11 +58,13 @@ def test_cfm_ranges_from_yaml_are_propagated_and_enforced(magnet_file, install_t assert got_ranges == expected_ranges # Build an in-range current vector (3 values) - in_currents = np.array([ - _in_range(*expected_ranges[0]), - _in_range(*expected_ranges[1]), - _in_range(*expected_ranges[2]), - ]) + in_currents = np.array( + [ + _in_range(*expected_ranges[0]), + _in_range(*expected_ranges[1]), + _in_range(*expected_ranges[2]), + ] + ) # Convert currents -> strengths (vector size 3) in_strengths = m.model.compute_strengths(in_currents) @@ -73,5 +78,3 @@ def test_cfm_ranges_from_yaml_are_propagated_and_enforced(magnet_file, install_t with pytest.raises(PyAMLException, match="out of range"): m.strengths.set(out_strengths) - - Factory.clear() diff --git a/tests/test_rf.py b/tests/test_rf.py index a3e17639..5752d4d0 100644 --- a/tests/test_rf.py +++ b/tests/test_rf.py @@ -3,7 +3,6 @@ from pyaml.accelerator import Accelerator from pyaml.common.exception import PyAMLException -from pyaml.configuration.factory import Factory def test_rf(): @@ -33,8 +32,6 @@ def test_rf(): assert np.isclose(RF.frequency.get(), 3.523721693993786e8) assert np.isclose(RF.voltage.get(), 6.5e6) - Factory.clear() - @pytest.mark.parametrize( "install_test_package", @@ -85,8 +82,6 @@ def test_rf_multi(install_test_package): assert np.isclose(RF2.voltage.get(), 2e6) assert np.isclose(RFTRA_HARMONIC.voltage.get(), 3e5) - Factory.clear() - @pytest.mark.parametrize( "install_test_package", @@ -116,5 +111,3 @@ def test_rf_multi_notrans(install_test_package): # Check that frequency and voltage has been applied on the masterclock device assert np.isclose(RF.frequency.get(), 3.523e8) - - Factory.clear() diff --git a/tests/test_serialized_magnets.py b/tests/test_serialized_magnets.py index 7f3d0003..6627bb4c 100644 --- a/tests/test_serialized_magnets.py +++ b/tests/test_serialized_magnets.py @@ -2,7 +2,6 @@ import pytest from pyaml.accelerator import Accelerator -from pyaml.configuration.factory import Factory def check_no_diff(array: list[np.float64]) -> bool: @@ -24,7 +23,9 @@ def check_no_diff(array: list[np.float64]) -> bool: ], ) def test_config_load(sr_file): - sr: Accelerator = Accelerator.load(sr_file, use_fast_loader=True, ignore_external=True) + sr: Accelerator = Accelerator.load( + sr_file, use_fast_loader=True, ignore_external=True + ) assert sr is not None magnets = [ sr.design.get_element("QF8B-C04"), @@ -46,4 +47,3 @@ def test_config_load(sr_file): currents = [magnet.hardware.get() for magnet in magnets] assert check_no_diff(strengths) assert check_no_diff(currents) - Factory.clear() diff --git a/tests/test_tune.py b/tests/test_tune.py index 4c1af3f6..631037fe 100644 --- a/tests/test_tune.py +++ b/tests/test_tune.py @@ -2,7 +2,6 @@ import pytest from pyaml.accelerator import Accelerator -from pyaml.configuration.factory import Factory def test_tune(): @@ -51,5 +50,3 @@ def test_tune(): strs = quadForTuneLive.strengths.get() strs += np.matmul(correctionmat, [0.1, 0.05]) # Ask for correction [dqx,dqy] quadForTuneLive.strengths.set(strs) - - Factory.clear() diff --git a/tests/test_tune_hardware.py b/tests/test_tune_hardware.py index 8bd806f8..d5b9529e 100644 --- a/tests/test_tune_hardware.py +++ b/tests/test_tune_hardware.py @@ -2,11 +2,12 @@ import pytest from pyaml.accelerator import Accelerator -from pyaml.configuration.factory import Factory def test_tune(): - sr: Accelerator = Accelerator.load("tests/config/EBSTune.yaml", ignore_external=True) + sr: Accelerator = Accelerator.load( + "tests/config/EBSTune.yaml", ignore_external=True + ) sr.design.get_lattice().disable_6d() quadForTuneDesign = sr.design.get_magnets("QForTune") @@ -42,4 +43,3 @@ def test_tune(): assert np.abs(currents[0] - 88.04522942) < 1e-8 assert np.abs(currents[1] - 88.26677735) < 1e-8 assert units[0] == "A" and units[1] == "A" - Factory.clear() diff --git a/tests/test_tune_monitor.py b/tests/test_tune_monitor.py index 5bf979c5..55cfe2de 100644 --- a/tests/test_tune_monitor.py +++ b/tests/test_tune_monitor.py @@ -1,18 +1,17 @@ import pytest from pyaml.accelerator import Accelerator -from pyaml.configuration.factory import Factory def test_simulator_tune_monitor(): - sr: Accelerator = Accelerator.load("tests/config/tune_monitor.yaml", ignore_external=True) + sr: Accelerator = Accelerator.load( + "tests/config/tune_monitor.yaml", ignore_external=True + ) sr.design.get_lattice().disable_6d() tune_monitor = sr.design.get_betatron_tune_monitor("BETATRON_TUNE") assert tune_monitor.tune.get()[0] == sr.design.get_lattice().get_tune()[0] assert tune_monitor.tune.get()[1] == sr.design.get_lattice().get_tune()[1] - Factory.clear() - @pytest.mark.parametrize( "install_test_package", @@ -24,5 +23,3 @@ def test_controlsystem_tune_monitor(install_test_package): tune_monitor = sr.live.get_betatron_tune_monitor("BETATRON_TUNE") assert tune_monitor.tune.get()[0] == 0.0 assert tune_monitor.tune.get()[1] == 0.0 - - Factory.clear() diff --git a/tests/test_tuning_dispersion.py b/tests/test_tuning_dispersion.py index 63ec9ed2..b96b414b 100644 --- a/tests/test_tuning_dispersion.py +++ b/tests/test_tuning_dispersion.py @@ -1,12 +1,7 @@ import logging from pathlib import Path -import numpy as np - from pyaml.accelerator import Accelerator -from pyaml.configuration.factory import Factory -from pyaml.tuning_tools.dispersion import ConfigModel as Disp_ConfigModel -from pyaml.tuning_tools.dispersion import Dispersion def test_tuning_orm(): @@ -26,5 +21,3 @@ def test_tuning_orm(): assert len(dispersion_data["frequency_response_x"]) == len(bpms) assert len(dispersion_data["frequency_response_y"]) == len(bpms) - - Factory.clear() diff --git a/tests/test_tuning_orbit_correction.py b/tests/test_tuning_orbit_correction.py index c51dfaec..ed14b141 100644 --- a/tests/test_tuning_orbit_correction.py +++ b/tests/test_tuning_orbit_correction.py @@ -4,9 +4,6 @@ import numpy as np from pyaml.accelerator import Accelerator -from pyaml.configuration.factory import Factory -from pyaml.tuning_tools.orbit import ConfigModel as Orbit_ConfigModel -from pyaml.tuning_tools.orbit import Orbit def test_tuning_orm(): @@ -48,5 +45,3 @@ def test_tuning_orm(): std_ac = np.std(positions_ac, axis=0) assert np.isclose(std_ac[0], 5.041856471193712e-07, rtol=0, atol=1e-14) assert np.isclose(std_ac[1], 4.789269566479167e-07, rtol=0, atol=1e-14) - - Factory.clear() diff --git a/tests/test_tuning_orm.py b/tests/test_tuning_orm.py index d60f5695..4eac3428 100644 --- a/tests/test_tuning_orm.py +++ b/tests/test_tuning_orm.py @@ -4,9 +4,6 @@ import numpy as np from pyaml.accelerator import Accelerator -from pyaml.configuration.factory import Factory -from pyaml.tuning_tools.orbit_response_matrix import ConfigModel as ORM_ConfigModel -from pyaml.tuning_tools.orbit_response_matrix import OrbitResponseMatrix def test_tuning_orm(): @@ -27,5 +24,3 @@ def test_tuning_orm(): orm_data = orm.get() orm_shape = np.array(orm_data["matrix"]).shape assert orm_shape == (2 * len(bpms), 8) - - Factory.clear() diff --git a/tests/test_tuning_tools.py b/tests/test_tuning_tools.py index 8a7ccd3d..dfb480ea 100644 --- a/tests/test_tuning_tools.py +++ b/tests/test_tuning_tools.py @@ -2,7 +2,6 @@ from pyaml.accelerator import Accelerator from pyaml.common.constants import ACTION_RESTORE -from pyaml.configuration.factory import Factory from pyaml.magnet.magnet import Magnet @@ -23,5 +22,3 @@ def test_tuning_tools(): tune = sr.design.tune.readback() assert np.abs(tune[0] - 0.17) < 1e-5 assert np.abs(tune[1] - 0.32) < 1e-5 - - Factory.clear()