Skip to content

Commit 02bb4e8

Browse files
authored
Merge pull request #185 from python-accelerator-middle-layer/fix-load-multiple
Clear factory before loading
2 parents 5f057cc + c684962 commit 02bb4e8

18 files changed

+27
-92
lines changed

pyaml/accelerator.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ def from_dict(config_dict: dict, ignore_external=False) -> "Accelerator":
153153
if ignore_external:
154154
# control systems are external, so remove controls field
155155
config_dict.pop("controls", None)
156+
# Ensure factory is clean before building a new accelerator
157+
Factory.clear()
156158
return Factory.depth_first_build(config_dict, ignore_external)
157159

158160
@staticmethod

tests/test_arrays.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@
99
from pyaml.arrays.bpm_array import BPMArray
1010
from pyaml.arrays.cfm_magnet import CombinedFunctionMagnet
1111
from pyaml.arrays.cfm_magnet import ConfigModel as CombinedFunctionMagnetConfigModel
12-
from pyaml.arrays.cfm_magnet_array import CombinedFunctionMagnetArray
1312
from pyaml.arrays.element_array import ElementArray
1413
from pyaml.arrays.magnet import ConfigModel as MagnetArrayConfigModel
1514
from pyaml.arrays.magnet import Magnet
1615
from pyaml.arrays.magnet_array import MagnetArray
17-
from pyaml.configuration.factory import Factory
1816

1917

2018
@pytest.mark.parametrize(
@@ -205,8 +203,6 @@ def test_arrays(install_test_package):
205203
bpmsLive = BPMArray("", sr.live.get_all_bpms())
206204
bpmsLive.positions.get()
207205

208-
Factory.clear()
209-
210206
# Test dynamic arrays
211207

212208
sr: Accelerator = Accelerator.load(
@@ -244,7 +240,6 @@ def test_arrays(install_test_package):
244240
v = sr.design.get_cfm_magnets("emptyCFM").strengths.get() # Ensure good attach
245241
assert np.shape(v) == (0,)
246242

247-
Factory.clear()
248243

249244
@pytest.mark.parametrize(
250245
"sr_file",
@@ -253,7 +248,9 @@ def test_arrays(install_test_package):
253248
],
254249
)
255250
def test_serialized_magnets_arrays(sr_file):
256-
sr: Accelerator = Accelerator.load(sr_file, use_fast_loader=True, ignore_external=True)
251+
sr: Accelerator = Accelerator.load(
252+
sr_file, use_fast_loader=True, ignore_external=True
253+
)
257254
the_serie = sr.design.get_serialized_magnets("series")
258255
strength = the_serie.strengths.get()
259256
assert len(strength) == 5
@@ -263,4 +260,3 @@ def test_serialized_magnets_arrays(sr_file):
263260
assert len(hardwares) == 5
264261
print(hardwares)
265262
the_serie.hardwares.set([10])
266-

tests/test_bpm.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import pytest
33

44
from pyaml.accelerator import Accelerator
5-
from pyaml.configuration.factory import Factory
65

76

87
def test_simulator_bpm_tilt():
@@ -13,8 +12,6 @@ def test_simulator_bpm_tilt():
1312
bpm.tilt.set(0.01)
1413
assert bpm.tilt.get() == 0.01
1514

16-
Factory.clear()
17-
1815

1916
def test_simulator_bpm_offset():
2017
sr: Accelerator = Accelerator.load("tests/config/bpms.yaml", ignore_external=True)
@@ -28,8 +25,6 @@ def test_simulator_bpm_offset():
2825
assert bpm.offset.get()[1] == 0.2
2926
assert np.allclose(bpm.positions.get(), np.array([0.0, 0.0]))
3027

31-
Factory.clear()
32-
3328

3429
@pytest.mark.parametrize(
3530
"install_test_package",
@@ -45,8 +40,6 @@ def test_simulator_bpm_position(install_test_package):
4540
assert np.allclose(bpm.positions.get(), np.array([0.0, 0.0]))
4641
assert np.allclose(bpm_simple.positions.get(), np.array([0.0, 0.0]))
4742

48-
Factory.clear()
49-
5043

5144
def test_simulator_bpm_position_with_bad_corrector_strength():
5245
sr: Accelerator = Accelerator.load("tests/config/bpms.yaml", ignore_external=True)
@@ -60,5 +53,3 @@ def test_simulator_bpm_position_with_bad_corrector_strength():
6053
for bpm in [bpm1, bpm_simple, bpm3]:
6154
assert bpm.positions.get()[0] != 0.0
6255
assert bpm.positions.get()[1] != 0.0
63-
64-
Factory.clear()

tests/test_bpm_controlsystem.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import pytest
33

44
from pyaml.accelerator import Accelerator
5-
from pyaml.configuration.factory import Factory
65

76

87
@pytest.mark.parametrize(
@@ -19,8 +18,6 @@ def test_controlsystem_bpm_tilt(install_test_package):
1918
bpm.tilt.set(0.01)
2019
assert bpm.tilt.get() == 0.01
2120

22-
Factory.clear()
23-
2421

2522
@pytest.mark.parametrize(
2623
"install_test_package",
@@ -38,8 +35,6 @@ def test_controlsystem_bpm_offset(install_test_package):
3835
assert bpm.offset.get()[1] == 0.2
3936
assert np.allclose(bpm.positions.get(), np.array([0.0, 0.0]))
4037

41-
Factory.clear()
42-
4338

4439
@pytest.mark.parametrize(
4540
"install_test_package",
@@ -65,5 +60,3 @@ def test_controlsystem_bpm_position_indexed(install_test_package):
6560
bpm = sr.live.get_bpm("BPM_C01-04")
6661

6762
assert np.allclose(bpm.positions.get(), np.array([0.0, 1.0]))
68-
69-
Factory.clear()

tests/test_chromaticity_monitor.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import pytest
33

44
from pyaml.accelerator import Accelerator
5-
from pyaml.configuration.factory import Factory
65

76

87
def test_simulator_chromaticity_monitor():
@@ -20,8 +19,6 @@ def test_simulator_chromaticity_monitor():
2019
== sr.design.get_lattice().get_chrom()[1]
2120
)
2221

23-
Factory.clear()
24-
2522

2623
@pytest.mark.parametrize(
2724
"install_test_package",
@@ -44,5 +41,3 @@ def test_controlsystem_chromaticity_monitor(install_test_package):
4441
ksi = np.abs(chromaticity_monitor.chromaticity.get())
4542
assert abs(ksi[0]) < 1e-17
4643
assert abs(ksi[1]) < 1e-17
47-
48-
Factory.clear()

tests/test_errors.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from pyaml.accelerator import Accelerator
44
from pyaml.arrays.magnet_array import MagnetArray
55
from pyaml.common.exception import PyAMLConfigException, PyAMLException
6-
from pyaml.configuration.factory import Factory
76

87

98
@pytest.mark.parametrize(
@@ -15,18 +14,15 @@ def test_tune(install_test_package):
1514
with pytest.raises(PyAMLConfigException) as exc:
1615
ml: Accelerator = Accelerator.load("tests/config/bad_conf_duplicate_1.yaml")
1716
assert "MagnetArray HCORR : duplicate name SH1A-C02-H @index 2" in str(exc)
18-
Factory.clear()
1917

2018
with pytest.raises(PyAMLConfigException) as exc:
2119
ml: Accelerator = Accelerator.load("tests/config/bad_conf_duplicate_2.yaml")
2220
assert "BPMArray BPM : duplicate name BPM_C04-06 @index 3" in str(exc)
23-
Factory.clear()
2421

2522
with pytest.raises(PyAMLConfigException) as exc:
2623
ml: Accelerator = Accelerator.load("tests/config/bad_conf_duplicate_3.yaml")
2724
assert "element BPM_C04-06 already defined" in str(exc)
2825
assert "line 58, column 3" in str(exc)
29-
Factory.clear()
3026

3127
sr: Accelerator = Accelerator.load("tests/config/EBSTune.yaml")
3228
m1 = sr.live.get_magnet("QF1E-C04")
@@ -45,5 +41,3 @@ def test_tune(install_test_package):
4541
with pytest.raises(PyAMLException) as exc:
4642
m2 = sr.design.get_bpm("QF1A-C05XX")
4743
assert "BPM QF1A-C05XX not defined" in str(exc)
48-
49-
Factory.clear()

tests/test_ident_models.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33

44
from pyaml.accelerator import Accelerator
55
from pyaml.configuration.factory import Factory
6-
from pyaml.magnet.cfm_magnet import CombinedFunctionMagnet
76
from pyaml.magnet.hcorrector import HCorrector
8-
from pyaml.magnet.model import MagnetModel
97
from pyaml.magnet.vcorrector import VCorrector
108

119

@@ -44,5 +42,3 @@ def test_cfm_magnets(magnet_file, install_test_package):
4442
assert np.abs(o[1] - 3.39661431e-07) < 1e-10
4543
assert np.abs(o[2] + 1.59928207e-06) < 1e-10
4644
assert np.abs(o[3] + 1.74771216e-05) < 1e-10
47-
48-
Factory.clear()

tests/test_load_quad.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
)
1717
from pyaml.magnet.cfm_magnet import CombinedFunctionMagnet
1818
from pyaml.magnet.hcorrector import HCorrector
19-
from pyaml.magnet.identity_model import IdentityMagnetModel
20-
from pyaml.magnet.quadrupole import ConfigModel as QuadrupoleConfigModel
2119
from pyaml.magnet.quadrupole import Quadrupole
2220

2321
# TODO: Generate JSON pydantic schema for MetaConfigurator

tests/test_ranges_cfm_deviceaccess.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
from pyaml import PyAMLException
55
from pyaml.accelerator import Accelerator
6-
from pyaml.configuration.factory import Factory
76

87

98
def _in_range(vmin, vmax) -> float:
@@ -21,7 +20,9 @@ def _out_of_range(vmin, vmax) -> float:
2120
return float(vmax) + 0.1
2221
if vmin is not None:
2322
return float(vmin) - 0.1
24-
raise RuntimeError("Unbounded range [None, None], cannot build an out-of-range value.")
23+
raise RuntimeError(
24+
"Unbounded range [None, None], cannot build an out-of-range value."
25+
)
2526

2627

2728
@pytest.mark.parametrize(
@@ -34,7 +35,9 @@ def _out_of_range(vmin, vmax) -> float:
3435
],
3536
indirect=["install_test_package"],
3637
)
37-
def test_cfm_ranges_from_yaml_are_propagated_and_enforced(magnet_file, install_test_package):
38+
def test_cfm_ranges_from_yaml_are_propagated_and_enforced(
39+
magnet_file, install_test_package
40+
):
3841
sr: Accelerator = Accelerator.load(magnet_file)
3942
sr.design.get_lattice().disable_6d()
4043

@@ -55,11 +58,13 @@ def test_cfm_ranges_from_yaml_are_propagated_and_enforced(magnet_file, install_t
5558
assert got_ranges == expected_ranges
5659

5760
# Build an in-range current vector (3 values)
58-
in_currents = np.array([
59-
_in_range(*expected_ranges[0]),
60-
_in_range(*expected_ranges[1]),
61-
_in_range(*expected_ranges[2]),
62-
])
61+
in_currents = np.array(
62+
[
63+
_in_range(*expected_ranges[0]),
64+
_in_range(*expected_ranges[1]),
65+
_in_range(*expected_ranges[2]),
66+
]
67+
)
6368

6469
# Convert currents -> strengths (vector size 3)
6570
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
7378

7479
with pytest.raises(PyAMLException, match="out of range"):
7580
m.strengths.set(out_strengths)
76-
77-
Factory.clear()

tests/test_rf.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
from pyaml.accelerator import Accelerator
55
from pyaml.common.exception import PyAMLException
6-
from pyaml.configuration.factory import Factory
76

87

98
def test_rf():
@@ -33,8 +32,6 @@ def test_rf():
3332
assert np.isclose(RF.frequency.get(), 3.523721693993786e8)
3433
assert np.isclose(RF.voltage.get(), 6.5e6)
3534

36-
Factory.clear()
37-
3835

3936
@pytest.mark.parametrize(
4037
"install_test_package",
@@ -85,8 +82,6 @@ def test_rf_multi(install_test_package):
8582
assert np.isclose(RF2.voltage.get(), 2e6)
8683
assert np.isclose(RFTRA_HARMONIC.voltage.get(), 3e5)
8784

88-
Factory.clear()
89-
9085

9186
@pytest.mark.parametrize(
9287
"install_test_package",
@@ -116,5 +111,3 @@ def test_rf_multi_notrans(install_test_package):
116111

117112
# Check that frequency and voltage has been applied on the masterclock device
118113
assert np.isclose(RF.frequency.get(), 3.523e8)
119-
120-
Factory.clear()

0 commit comments

Comments
 (0)