Skip to content

Commit 7fa1d9a

Browse files
committed
fixing not giving IPs
1 parent a4dda82 commit 7fa1d9a

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

ir_amplitude_detuning/detuning/calculations.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ def calc_effective_detuning(optics: TwissPerBeam, values: pd.Series) -> dict[int
146146
"""
147147
correctors: Correctors = values.index
148148

149-
loop_ips: list[Iterable[int]] = to_loop(sorted({c.ip for c in correctors if c.ip is not None}))
149+
ips = {c.ip for c in correctors if c.ip is not None} or {None} # latter if all are None
150+
loop_ips: list[Iterable[int]] = to_loop(sorted(ips))
150151
ip_strings: list[str] = [''.join(map(str, ips)) for ips in loop_ips]
151152

152153
loop_fields: list[str] = to_loop(sorted({c.field for c in correctors}))

tests/unit/test_detuning_calculations.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,49 @@ class TestCalcEffectiveDetuning:
225225

226226
def test_calc_effective_detuning_empty_optics(self):
227227
"""Test with empty optics dictionary."""
228-
result = calc_effective_detuning({}, pd.Series(dtype=float))
228+
result = calc_effective_detuning({}, pd.Series(0, index=[Mock(ip=None)], dtype=float))
229229

230230
# Returns empty dict with no beams
231231
assert isinstance(result, dict)
232232
assert len(result) == 0
233233

234+
@patch("ir_amplitude_detuning.detuning.calculations.calculate_matrix_row")
235+
def test_calc_effective_detuning_no_ips(self, mock_calculate_matrix_row):
236+
"""Test with correctors without IPs."""
237+
# Create mocks ---
238+
all_correctors = [
239+
Corrector(
240+
field=field,
241+
length=0.5,
242+
magnet=f"{type_}ip{ip or 0}{field}",
243+
circuit=f"k{type_}ip{ip or 0}{field}",
244+
ip=ip,
245+
)
246+
for type_, field, ip in (
247+
("c1", FieldComponent.b4, None),
248+
("c2", FieldComponent.b4, None),
249+
)
250+
]
251+
values = pd.Series([1, 2], index=all_correctors, dtype=float)
252+
253+
mock_optics = {1: Mock()}
254+
def mocked_calulation(beam, optics, correctors, term):
255+
assert correctors == all_correctors # no filtering as all ips are None, and both have same field
256+
return np.ones([1, len(correctors)])
257+
258+
mock_calculate_matrix_row.side_effect = mocked_calulation
259+
all_terms = list(FirstOrderTerm) + list(SecondOrderTerm)
260+
261+
# Run ---
262+
result = calc_effective_detuning(mock_optics, values)
263+
264+
# Check results ---
265+
assert isinstance(result, dict)
266+
assert len(result) == 1 # one beam
267+
assert len(result[1]) == 1 # one field, "one" ip (None)
268+
assert all(result[1].loc[:, all_terms] == values.sum()) # calculation returns [1, 1]
269+
assert mock_calculate_matrix_row.call_count == len(all_terms)
270+
234271
@patch("ir_amplitude_detuning.detuning.calculations.calculate_matrix_row")
235272
def test_calc_effective_detuning(self, mock_calculate_matrix_row):
236273
"""Test with single beam."""

0 commit comments

Comments
 (0)