Skip to content

Commit ad97776

Browse files
committed
updated iota tests
1 parent eb1a661 commit ad97776

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

tests/test_iota.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from datetime import datetime
21
from pathlib import Path
32

43
import h5py
@@ -12,36 +11,39 @@
1211
from turn_by_turn.structures import TbtData, TransverseData
1312

1413

15-
def test_tbt_read_hdf5(_hdf5_file_v1):
16-
origin = _hdf5_file_content()
14+
def test_tbt_read_hdf5(_hdf5_file_v1, _hdf5_file_content):
1715
new = iota.read_tbt(_hdf5_file_v1, version=1)
18-
compare_tbt(origin, new, no_binary=False)
16+
compare_tbt(_hdf5_file_content, new, no_binary=False)
1917

2018

21-
def test_tbt_read_hdf5_v2(_hdf5_file_v2):
22-
origin = _hdf5_file_content()
19+
def test_tbt_read_hdf5_v2(_hdf5_file_v2, _hdf5_file_content):
2320
new = iota.read_tbt(_hdf5_file_v2)
24-
compare_tbt(origin, new, no_binary=False)
21+
compare_tbt(_hdf5_file_content, new, no_binary=False)
2522

2623

27-
def test_tbt_raises_on_wrong_hdf5_version(_hdf5_file_v1):
24+
def test_tbt_raises_on_wrong_hdf5_version(_hdf5_file_v1, _hdf5_file_v2):
2825
with pytest.raises(HDF5VersionError):
2926
iota.read_tbt(_hdf5_file_v1, version=2)
3027

28+
with pytest.raises(HDF5VersionError):
29+
iota.read_tbt(_hdf5_file_v2, version=1)
30+
3131

32+
33+
@pytest.fixture(scope="module")
3234
def _hdf5_file_content() -> TbtData:
3335
"""TbT data as had been written out to hdf5 files (see below)."""
3436
return TbtData(
3537
matrices=[
3638
TransverseData(
3739
X=pd.DataFrame(
3840
index=["IBPMA1C", "IBPME2R"],
39-
data=create_data(np.linspace(-np.pi, np.pi, 2000, endpoint=False), 2, np.sin),
41+
data=create_data(np.linspace(-np.pi, np.pi, 2000, endpoint=False), 2, np.sin, noise=0.02),
4042
dtype=float,
4143
),
4244
Y=pd.DataFrame(
4345
index=["IBPMA1C", "IBPME2R"],
44-
data=create_data(np.linspace(-np.pi, np.pi, 2000, endpoint=False), 2, np.cos),
46+
data=create_data(np.linspace(-np.pi, np.pi, 2000, endpoint=False), 2, np.cos, noise=0.015),
4547
dtype=float,
4648
),
4749
)
@@ -52,18 +54,18 @@ def _hdf5_file_content() -> TbtData:
5254

5355

5456
@pytest.fixture()
55-
def _hdf5_file_v1(tmp_path) -> Path:
56-
"""IOTA File standard."""
57-
content: TransverseData = _hdf5_file_content().matrices[0]
57+
def _hdf5_file_v1(tmp_path, _hdf5_file_content) -> Path:
58+
"""IOTA File v1 standard."""
59+
content: TransverseData = _hdf5_file_content.matrices[0]
5860

5961
with h5py.File(tmp_path / "test_file.hdf5", "w") as hd5_file:
6062
hd5_file.create_dataset(
6163
"N:IBE2RH",
62-
data=create_data(np.linspace(-np.pi, np.pi, 2000, endpoint=False), 1, np.sin).flatten(),
64+
data=content.X.loc["IBPME2R"].to_numpy(),
6365
)
6466
hd5_file.create_dataset(
6567
"N:IBE2RV",
66-
data=create_data(np.linspace(-np.pi, np.pi, 2000, endpoint=False), 1, np.cos).flatten(),
68+
data=content.Y.loc["IBPME2R"].to_numpy(),
6769
)
6870
hd5_file.create_dataset(
6971
"N:IBE2RS",
@@ -72,11 +74,11 @@ def _hdf5_file_v1(tmp_path) -> Path:
7274

7375
hd5_file.create_dataset(
7476
"N:IBA1CH",
75-
data=create_data(np.linspace(-np.pi, np.pi, 2000, endpoint=False), 1, np.sin).flatten(),
77+
data=content.X.loc["IBPMA1C"].to_numpy(),
7678
)
7779
hd5_file.create_dataset(
7880
"N:IBA1CV",
79-
data=create_data(np.linspace(-np.pi, np.pi, 2000, endpoint=False), 1, np.cos).flatten(),
81+
data=content.Y.loc["IBPMA1C"].to_numpy(),
8082
)
8183
hd5_file.create_dataset(
8284
"N:IBA1CS",
@@ -86,17 +88,19 @@ def _hdf5_file_v1(tmp_path) -> Path:
8688

8789

8890
@pytest.fixture()
89-
def _hdf5_file_v2(tmp_path) -> Path:
90-
"""IOTA File standard."""
91+
def _hdf5_file_v2(tmp_path, _hdf5_file_content) -> Path:
92+
"""IOTA File v2 standard."""
93+
content: TransverseData = _hdf5_file_content.matrices[0]
94+
9195
with h5py.File(tmp_path / "test_file_v2.hdf5", "w") as hd5_file:
9296
hd5_file.create_group("A1C")
9397
hd5_file["A1C"].create_dataset(
9498
"Horizontal",
95-
data=create_data(np.linspace(-np.pi, np.pi, 2000, endpoint=False), 1, np.sin).flatten(),
99+
data=content.X.loc["IBPMA1C"].to_numpy(),
96100
)
97101
hd5_file["A1C"].create_dataset(
98102
"Vertical",
99-
data=create_data(np.linspace(-np.pi, np.pi, 2000, endpoint=False), 1, np.cos).flatten(),
103+
data=content.Y.loc["IBPMA1C"].to_numpy(),
100104
)
101105
hd5_file["A1C"].create_dataset(
102106
"Intensity",
@@ -106,11 +110,11 @@ def _hdf5_file_v2(tmp_path) -> Path:
106110
hd5_file.create_group("E2R")
107111
hd5_file["E2R"].create_dataset(
108112
"Horizontal",
109-
data=create_data(np.linspace(-np.pi, np.pi, 2000, endpoint=False), 1, np.sin).flatten(),
113+
data=content.X.loc["IBPME2R"].to_numpy(),
110114
)
111115
hd5_file["E2R"].create_dataset(
112116
"Vertical",
113-
data=create_data(np.linspace(-np.pi, np.pi, 2000, endpoint=False), 1, np.cos).flatten(),
117+
data=content.Y.loc["IBPME2R"].to_numpy(),
114118
)
115119
hd5_file["E2R"].create_dataset(
116120
"Intensity",

tests/test_lhc_and_general.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ def compare_tbt(
7878
assert np.all(origin_mat == new_mat)
7979

8080

81-
def create_data(phases, nbpm, function, noise: float = 0) -> np.ndarray:
82-
rng = np.random.default_rng()
81+
def create_data(phases, nbpm, function, noise: float = 0, seed: int = None) -> np.ndarray:
82+
rng = np.random.default_rng(seed=seed)
8383
return np.ones((nbpm, len(phases))) * function(phases) + noise * rng.standard_normal(
8484
size=(nbpm, len(phases))
8585
)

0 commit comments

Comments
 (0)