Skip to content

Commit 3163c5e

Browse files
authored
Merge pull request #20 from pylhc/doros_oscillations_positions
DOROS: oscillations and positions
2 parents 4fb38eb + 21dfd13 commit 3163c5e

File tree

7 files changed

+184
-103
lines changed

7 files changed

+184
-103
lines changed

tests/inputs/doros_shrink_data.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
import subprocess
77
import h5py
88

9-
from turn_by_turn.doros import DEFAULT_OSCILLATION_DATA, N_ORBIT_SAMPLES, N_OSCILLATION_SAMPLES, OSCILLATIONS, POSITIONS
9+
from turn_by_turn.doros import DataKeys
1010

1111
file_in = "/afs/cern.ch/work/j/jdilly/dorostest/DOROS-2024-09-29_01_37_13_522358-NO_USER.h5"
12-
file_out = "./tests/inputs/test_doros_2024-09-29.h5"
12+
file_out = "./tests/inputs/test_doros.h5"
1313

1414
# Values need to coincide with the values in the test_doros.py
1515
N_BPMS= 3
@@ -18,26 +18,25 @@
1818
# Copy file to temporary location and "delete" unneccessary data (only removes references to data)
1919
file_temp = file_out + ".tmp"
2020
shutil.copyfile(file_in, file_temp)
21+
22+
data_keys_list = [DataKeys.get_data_keys(datatype) for datatype in DataKeys.types()]
23+
2124
with h5py.File(file_temp, "r+", track_order=True) as hdf_file:
22-
bpms = [name for name in hdf_file["/"].keys() if N_ORBIT_SAMPLES in hdf_file[f"/{name}"].keys()]
25+
26+
bpms = [name for name in hdf_file["/"].keys() if data_keys_list[0].n_samples in hdf_file[f"/{name}"].keys()]
2327

2428
for bpm in bpms[:N_BPMS]:
25-
del hdf_file[bpm][N_ORBIT_SAMPLES]
26-
hdf_file[bpm].create_dataset(N_ORBIT_SAMPLES, data=[NTURNS])
27-
data_x = hdf_file[bpm][POSITIONS["X"]][:NTURNS]
28-
data_y = hdf_file[bpm][POSITIONS["Y"]][:NTURNS]
29-
30-
del hdf_file[bpm][POSITIONS["X"]]
31-
del hdf_file[bpm][POSITIONS["Y"]]
32-
hdf_file[bpm].create_dataset(POSITIONS["X"], data=data_x)
33-
hdf_file[bpm].create_dataset(POSITIONS["Y"], data=data_y)
34-
35-
del hdf_file[bpm][N_OSCILLATION_SAMPLES]
36-
del hdf_file[bpm][OSCILLATIONS["X"]]
37-
del hdf_file[bpm][OSCILLATIONS["Y"]]
38-
hdf_file[bpm].create_dataset(N_OSCILLATION_SAMPLES, data=0)
39-
hdf_file[bpm].create_dataset(OSCILLATIONS["X"], data=[DEFAULT_OSCILLATION_DATA])
40-
hdf_file[bpm].create_dataset(OSCILLATIONS["Y"], data=[DEFAULT_OSCILLATION_DATA])
29+
for data_keys in data_keys_list:
30+
del hdf_file[bpm][data_keys.n_samples]
31+
hdf_file[bpm].create_dataset(data_keys.n_samples, data=[NTURNS])
32+
data_x = hdf_file[bpm][data_keys.data["X"]][:NTURNS]
33+
data_y = hdf_file[bpm][data_keys.data["Y"]][:NTURNS]
34+
35+
del hdf_file[bpm][data_keys.data["X"]]
36+
del hdf_file[bpm][data_keys.data["Y"]]
37+
hdf_file[bpm].create_dataset(data_keys.data["X"], data=data_x)
38+
hdf_file[bpm].create_dataset(data_keys.data["Y"], data=data_y)
39+
4140

4241
for bpm in bpms[N_BPMS:]:
4342
del hdf_file[bpm]

tests/inputs/test_doros.h5

100755100644
1.12 MB
Binary file not shown.
-2.28 MB
Binary file not shown.

tests/test_doros.py

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,81 +2,96 @@
22
from datetime import datetime
33
from pathlib import Path
44

5+
import h5py
56
import numpy as np
67
import pandas as pd
78
import pytest
8-
import h5py
99

10+
import turn_by_turn as tbt
11+
from tests.test_lhc_and_general import compare_tbt, create_data
12+
from turn_by_turn.doros import DEFAULT_BUNCH_ID, DataKeys, read_tbt, write_tbt
1013
from turn_by_turn.structures import TbtData, TransverseData
11-
from tests.test_lhc_and_general import create_data, compare_tbt
12-
13-
from turn_by_turn.doros import N_ORBIT_SAMPLES, read_tbt, write_tbt, DEFAULT_BUNCH_ID, POSITIONS
1414

1515
INPUTS_DIR = Path(__file__).parent / "inputs"
1616

17-
@pytest.mark.parametrize("filename", ["test_doros.h5", "test_doros_2024-09-29.h5"])
18-
def test_read_write_real_data(tmp_path, filename):
19-
tbt = read_tbt(INPUTS_DIR / filename, bunch_id=10)
17+
@pytest.mark.parametrize("datatype", DataKeys.types())
18+
def test_read_write_real_data(tmp_path, datatype):
19+
tbt_data = read_tbt(INPUTS_DIR / "test_doros.h5", bunch_id=10, data_type=datatype)
2020

21-
assert tbt.nbunches == 1
22-
assert len(tbt.matrices) == 1
23-
assert tbt.nturns == 50000
24-
assert tbt.matrices[0].X.shape == (3, tbt.nturns)
25-
assert tbt.matrices[0].Y.shape == (3, tbt.nturns)
26-
assert len(set(tbt.matrices[0].X.index)) == 3
27-
assert np.all(tbt.matrices[0].X.index == tbt.matrices[0].Y.index)
21+
assert tbt_data.nbunches == 1
22+
assert len(tbt_data.matrices) == 1
23+
assert tbt_data.nturns == 50000
24+
assert tbt_data.matrices[0].X.shape == (3, tbt_data.nturns)
25+
assert tbt_data.matrices[0].Y.shape == (3, tbt_data.nturns)
26+
assert len(set(tbt_data.matrices[0].X.index)) == 3
27+
assert np.all(tbt_data.matrices[0].X.index == tbt_data.matrices[0].Y.index)
2828

2929
file_path = tmp_path / "test_file.h5"
30-
write_tbt(tbt, file_path)
31-
new = read_tbt(file_path, bunch_id=10)
32-
compare_tbt(tbt, new, no_binary=False)
30+
write_tbt(file_path, tbt_data, data_type=datatype)
31+
new = read_tbt(file_path, bunch_id=10, data_type=datatype)
32+
compare_tbt(tbt_data, new, no_binary=False)
3333

3434

35-
def test_write_read(tmp_path):
36-
tbt = _tbt_data()
35+
@pytest.mark.parametrize("datatype", DataKeys.types())
36+
def test_write_read(tmp_path, datatype):
37+
tbt_data = _tbt_data()
3738
file_path = tmp_path / "test_file.h5"
38-
write_tbt(tbt, file_path)
39-
new = read_tbt(file_path)
40-
compare_tbt(tbt, new, no_binary=False)
39+
write_tbt(file_path, tbt_data, data_type=datatype)
40+
new = read_tbt(file_path, data_type=datatype)
41+
compare_tbt(tbt_data, new, no_binary=False)
42+
43+
44+
@pytest.mark.parametrize("datatype", ["doros_oscillations", "doros_positions"])
45+
def test_write_read_via_io_module(tmp_path, datatype):
46+
tbt_data = _tbt_data()
47+
file_path = tmp_path / "test_file.h5"
48+
tbt.write(file_path, tbt_data, datatype=datatype)
49+
new = tbt.read(file_path, datatype=datatype)
50+
compare_tbt(tbt_data, new, no_binary=False)
4151

4252

4353
def test_read_raises_different_bpm_lengths(tmp_path):
44-
tbt = _tbt_data()
54+
tbt_data = _tbt_data()
4555
file_path = tmp_path / "test_file.h5"
46-
write_tbt(tbt, file_path)
56+
data_type = DataKeys.OSCILLATIONS
57+
write_tbt(file_path, tbt_data, data_type=data_type)
58+
keys = DataKeys.get_data_keys(data_type)
4759

48-
bpm = tbt.matrices[0].X.index[0]
60+
bpm = tbt_data.matrices[0].X.index[0]
4961

5062
# modify the BPM lengths in the file
5163
with h5py.File(file_path, "r+") as h5f:
5264
delta = 10
53-
del h5f[bpm][N_ORBIT_SAMPLES]
54-
h5f[bpm][N_ORBIT_SAMPLES] = [tbt.matrices[0].X.shape[1] - delta]
55-
for key in POSITIONS.values():
65+
del h5f[bpm][keys.n_samples]
66+
h5f[bpm][keys.n_samples] = [tbt_data.matrices[0].X.shape[1] - delta]
67+
for key in keys.data.values():
5668
data = h5f[bpm][key][:-delta]
5769
del h5f[bpm][key]
5870
h5f[bpm][key] = data
5971

6072
with pytest.raises(ValueError) as e:
61-
read_tbt(file_path)
73+
read_tbt(file_path, data_type=DataKeys.OSCILLATIONS)
6274
assert "Not all BPMs have the same number of turns!" in str(e)
6375

6476

6577
def test_read_raises_on_different_bpm_lengths_in_data(tmp_path):
66-
tbt = _tbt_data()
78+
tbt_data = _tbt_data()
6779
file_path = tmp_path / "test_file.h5"
68-
write_tbt(tbt, file_path)
80+
data_type = DataKeys.OSCILLATIONS
81+
keys = DataKeys.get_data_keys(data_type)
82+
83+
write_tbt(file_path, tbt_data, data_type=data_type)
6984

70-
bpms = [tbt.matrices[0].X.index[i] for i in (0, 2)]
85+
bpms = [tbt_data.matrices[0].X.index[i] for i in (0, 2)]
7186

7287
# modify the BPM lengths in the file
7388
with h5py.File(file_path, "r+") as h5f:
7489
for bpm in bpms:
75-
del h5f[bpm][N_ORBIT_SAMPLES]
76-
h5f[bpm][N_ORBIT_SAMPLES] = [tbt.matrices[0].X.shape[1] + 10]
90+
del h5f[bpm][keys.n_samples]
91+
h5f[bpm][keys.n_samples] = [tbt_data.matrices[0].X.shape[1] + 10]
7792

7893
with pytest.raises(ValueError) as e:
79-
read_tbt(file_path)
94+
read_tbt(file_path, data_type=data_type)
8095
assert "Found BPMs with different data lengths" in str(e)
8196
assert all(bpm in str(e) for bpm in bpms)
8297

turn_by_turn/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
__title__ = "turn_by_turn"
66
__description__ = "Read and write turn-by-turn measurement files from different particle accelerator formats."
77
__url__ = "https://github.com/pylhc/turn_by_turn"
8-
__version__ = "0.7.1"
8+
__version__ = "0.7.2"
99
__author__ = "pylhc"
1010
__author_email__ = "[email protected]"
1111
__license__ = "MIT"

0 commit comments

Comments
 (0)