Skip to content

Commit 1ca42fe

Browse files
committed
Making lossmap plot test dependent on existing matplotlib installation.
1 parent b72021d commit 1ca42fe

File tree

1 file changed

+31
-11
lines changed

1 file changed

+31
-11
lines changed

tests/test_lossmap.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,16 @@
1212
import pytest
1313
from xpart.test_helpers import flaky_assertions, retry
1414
from xobjects.test_helpers import for_all_test_contexts
15+
try:
16+
import matplotlib.pyplot as plt
17+
matplotlib_installed = True
18+
except (ImportError, ModuleNotFoundError):
19+
matplotlib_installed = False
1520

1621
path = Path(__file__).parent / 'data'
1722

1823

24+
@pytest.mark.skipif(not matplotlib_installed, reason="matplolib not installed")
1925
@for_all_test_contexts(
2026
excluding=('ContextCupy', 'ContextPyopencl') # Rutherford RNG not on GPU
2127
)
@@ -26,31 +32,48 @@
2632
[1, 'V', 3500, False, False],
2733
[2, 'H', 30000, 0.15, False]
2834
], ids=["B1H", "B2V", "B1V_crystals", "B2H_crystals"])
29-
def test_lossmap_everest(beam, plane, npart, interpolation, ignore_crystals, test_context):
35+
def test_lossmap_everest_with_plot(beam, plane, npart, interpolation, ignore_crystals, test_context):
36+
_test_lossmap_everest(beam, plane, npart, interpolation, ignore_crystals, test_context, True)
3037

31-
line = xt.Line.from_json(path / f'sequence_lhc_run3_b{beam}.json')
3238

39+
@pytest.mark.skipif(matplotlib_installed, reason="matplolib installed")
40+
@for_all_test_contexts(
41+
excluding=('ContextCupy', 'ContextPyopencl') # Rutherford RNG not on GPU
42+
)
43+
@retry()
44+
@pytest.mark.parametrize("beam, plane, npart, interpolation, ignore_crystals", [
45+
[1, 'H', 2500, 0.2, True],
46+
[2, 'V', 500, 0.3, True],
47+
[1, 'V', 3500, False, False],
48+
[2, 'H', 30000, 0.15, False]
49+
], ids=["B1H", "B2V", "B1V_crystals", "B2H_crystals"])
50+
def test_lossmap_everest_without_plot(beam, plane, npart, interpolation, ignore_crystals, test_context):
51+
_test_lossmap_everest(beam, plane, npart, interpolation, ignore_crystals, test_context, False)
52+
53+
54+
def _test_lossmap_everest(beam, plane, npart, interpolation, ignore_crystals, test_context, do_plot):
55+
line = xt.Line.from_json(path / f'sequence_lhc_run3_b{beam}.json')
3356
colldb = xc.CollimatorDatabase.from_yaml(path / f'colldb_lhc_run3_ir7.yaml',
3457
beam=beam, ignore_crystals=ignore_crystals)
35-
3658
colldb.install_everest_collimators(line=line)
3759
df_with_coll = line.check_aperture()
3860
assert not np.any(df_with_coll.has_aperture_problem)
39-
4061
line.build_tracker(_context=test_context)
4162
line.collimators.assign_optics()
42-
4363
tcp = f"tcp.{'c' if plane=='H' else 'd'}6{'l' if beam==1 else 'r'}7.b{beam}"
4464
part = line[tcp].generate_pencil(npart)
45-
4665
line.scattering.enable()
4766
line.track(part, num_turns=2)
4867
line.scattering.disable()
4968
this_id = f"B{beam}{plane}-{npart}-{interpolation}-{ignore_crystals}-{test_context}"
50-
assert_lossmap(beam, npart, line, part, tcp, interpolation, ignore_crystals, 'EverestCollimator', 'EverestCrystal', this_id)
69+
_assert_lossmap(beam, npart, line, part, tcp, interpolation, ignore_crystals, 'EverestCollimator', 'EverestCrystal', this_id)
70+
if do_plot:
71+
ThisLM.plot(show=False, savefig=f"test-{this_id}.jpg")
72+
assert Path(f"test-{this_id}.jpg").exists()
73+
Path(f"test-{this_id}.jpg").unlink()
5174

5275

53-
def assert_lossmap(beam, npart, line, part, tcp, interpolation, ignore_crystals, coll_cls, cry_cls, this_id):
76+
def _assert_lossmap(beam, npart, line, part, tcp, interpolation, ignore_crystals, coll_cls, cry_cls, this_id):
5477
with flaky_assertions():
5578
line_is_reversed = True if beam == 2 else False
5679
ThisLM = xc.LossMap(line, line_is_reversed=line_is_reversed, part=part,
@@ -73,9 +96,6 @@ def assert_lossmap(beam, npart, line, part, tcp, interpolation, ignore_crystals,
7396
ThisLM.save_summary(f"coll_summary-{this_id}.txt")
7497
assert Path(f"coll_summary-{this_id}.txt").exists()
7598
Path(f"coll_summary-{this_id}.txt").unlink()
76-
ThisLM.plot(show=False, savefig=f"test-{this_id}.jpg")
77-
assert Path(f"test-{this_id}.jpg").exists()
78-
Path(f"test-{this_id}.jpg").unlink()
7999

80100
# TODO: check the lossmap quantitaively: rough amount of losses at given positions
81101
summ = ThisLM.summary

0 commit comments

Comments
 (0)