Skip to content

Commit c7d78ec

Browse files
committed
comments
1 parent e928be6 commit c7d78ec

File tree

3 files changed

+33
-32
lines changed

3 files changed

+33
-32
lines changed

turn_by_turn/iota.py

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,17 @@ def read_tbt(file_path: str | Path, version: Version = Version.two) -> TbtData:
4646

4747
match version:
4848
case Version.one:
49-
reader = VersionOne(file_path)
49+
read_data = VersionOneReader(file_path)
5050
case Version.two:
51-
reader = VersionTwo(file_path)
51+
read_data = VersionTwoReader(file_path)
5252
case _:
5353
raise ValueError(f"Version {version} unknown for IOTA reader.")
5454

55-
return reader.tbt_data
55+
return read_data.tbt_data
5656

5757

58-
class Reader(abc.ABC):
59-
""" Class that reads the IOTA turn-by-turn data.
58+
class AbstractIotaReader(abc.ABC):
59+
"""Class that reads the IOTA turn-by-turn data.
6060
6161
This abstract class implements the whole reading in its `__init__`,
6262
but cannot run by itself, as the version specific functions (see below)
@@ -75,7 +75,7 @@ def __init__(self, path: Path):
7575
self.tbt_data = self._read_turn_by_turn_data()
7676

7777
def _prepare(self):
78-
""" Prepare attributes and check that the correct version is used. """
78+
"""Prepare attributes and check that the correct version is used."""
7979
bpm_names = [self.map_bpm_name(key) for key in self.hdf5_file if self.is_bpm_key(key)]
8080
if not bpm_names:
8181
msg = f"Wrong version of the IOTA-HDF5 format was used for file {self.path!s}!"
@@ -87,7 +87,7 @@ def _prepare(self):
8787
self.nturns: int = self._get_number_of_turns()
8888

8989
def _read_turn_by_turn_data(self) -> TbtData:
90-
""" Create the turn-by-turn data object. """
90+
"""Read data and create the turn-by-turn data object."""
9191
return TbtData(
9292
bunch_ids=[1],
9393
nturns=self.nturns,
@@ -112,8 +112,8 @@ def _read_turn_by_turn_data(self) -> TbtData:
112112
)
113113

114114
def _get_data_for_plane(self, plane: str) -> np.ndarray:
115-
""" Extract the turn-by-turn data for the given plane as numpy array,
116-
truncated to the maximum common number of turns. """
115+
"""Extract the turn-by-turn data for the given plane as numpy array,
116+
truncated to the maximum common number of turns."""
117117
data = np.zeros((self.nbpms, self.nturns))
118118
bpm_keys = [key for key in self.hdf5_file if self.is_bpm_key(key, plane)]
119119

@@ -123,8 +123,8 @@ def _get_data_for_plane(self, plane: str) -> np.ndarray:
123123
return data
124124

125125
def _get_number_of_turns(self) -> int:
126-
""" Get the maximum common number of tuns over all BPMs,
127-
such that the arrays can be trimmed to be of equal lengths. """
126+
"""Get the maximum common number of tuns over all BPMs,
127+
such that the arrays can be trimmed to be of equal lengths."""
128128
return min(
129129
len(self._get_data_for_key(key, plane))
130130
for plane in ("X", "Y")
@@ -133,59 +133,59 @@ def _get_number_of_turns(self) -> int:
133133
)
134134

135135
def _get_data_for_key(self, key: str, plane: Literal["X", "Y"]) -> np.ndarray:
136-
""" Extract the turn-by-turn data for the given key and plane as numpy array. """
136+
"""Extract the turn-by-turn data for the given key and plane as numpy array."""
137137
...
138138

139139
@staticmethod
140140
def map_bpm_name(key: str) -> str:
141-
""" Convert the given key to a BPM name. """
141+
"""Convert the given key to a BPM name."""
142142
...
143143

144144
@staticmethod
145145
def is_bpm_key(key: str, plane: Literal["X", "Y"] | None = None) -> bool:
146-
""" Check if the entry of the file contains BPM data. """
146+
"""Check if the entry of the file contains BPM data."""
147147
...
148148

149149

150-
class VersionOne(Reader):
151-
""" Version 1 contains three keys per BPM: X, Y and Intensity. """
150+
class VersionOneReader(AbstractIotaReader):
151+
"""Version 1 contains three keys per BPM: X, Y and Intensity."""
152152

153153
planes: dict[str, str] = {"X": "H", "Y": "V"}
154154

155155
def _get_data_for_key(self, key: str, plane: Literal["X", "Y"]) -> np.ndarray:
156-
""" Extract the turn-by-turn data for the given key and plane as numpy array. """
156+
"""Extract the turn-by-turn data for the given key and plane as numpy array."""
157157
return self.hdf5_file[key] # assumes plane is already in key name
158158

159159
@staticmethod
160160
def map_bpm_name(key: str) -> str:
161-
""" Convert the given key to a BPM name. """
161+
"""Convert the given key to a BPM name."""
162162
return f"IBPM{key[4:-1]}"
163163

164164
@staticmethod
165165
def is_bpm_key(key: str, plane: Literal["X", "Y"] | None = None) -> bool:
166-
""" Check if the entry of the file contains BPM data. """
166+
"""Check if the entry of the file contains BPM data."""
167167
is_bpm = ("state" not in key) or key.startswith("N:")
168168
if plane is None:
169-
return is_bpm and (key.endswith(VersionOne.planes["X"]) or key.endswith(VersionOne.planes["Y"]))
170-
return is_bpm and key.endswith(VersionOne.planes[plane])
169+
return is_bpm and (key.endswith(VersionOneReader.planes["X"]) or key.endswith(VersionOneReader.planes["Y"]))
170+
return is_bpm and key.endswith(VersionOneReader.planes[plane])
171171

172172

173-
class VersionTwo(Reader):
174-
""" Version 2 contains a single key per BPM, which contains data for both planes
175-
(and possibly more which we ignore). """
173+
class VersionTwoReader(AbstractIotaReader):
174+
"""Version 2 contains a single key per BPM, which contains data for both planes
175+
(and possibly more which we ignore)."""
176176

177177
planes: dict[str, str] = {"X": "Horizontal", "Y": "Vertical"}
178178

179179
def _get_data_for_key(self, key: str, plane: Literal["X", "Y"]) -> np.ndarray:
180-
""" Extract the turn-by-turn data for the given key and plane as numpy array. """
180+
"""Extract the turn-by-turn data for the given key and plane as numpy array."""
181181
return self.hdf5_file[key][self.planes[plane]]
182182

183183
@staticmethod
184184
def map_bpm_name(key: str) -> str:
185-
""" Convert the given key to a BPM name. """
185+
"""Convert the given key to a BPM name."""
186186
return f"IBPM{key}"
187187

188188
@staticmethod
189189
def is_bpm_key(key: str, plane: Literal["X", "Y"] | None = None) -> bool:
190-
""" Check if the entry of the file contains BPM data. """
190+
"""Check if the entry of the file contains BPM data."""
191191
return "NL" not in key and not key.startswith("N:") # latter: filter v1 data to be safe

turn_by_turn/madng.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ def read_tbt(file_path: str | Path) -> TbtData:
7777
df = tfs.read(file_path)
7878
tbt_data = convert_to_tbt(df)
7979
tbt_data.meta["file"] = file_path
80-
tbt_data.meta["source_datatype"] = "madng"
8180
return tbt_data
8281

8382

@@ -110,7 +109,9 @@ def convert_to_tbt(df: pd.DataFrame | tfs.TfsDataFrame) -> TbtData:
110109
LOGGER.debug("The 'tfs' package is not installed. Assuming a pandas DataFrame.")
111110
is_tfs_df = False
112111

113-
meta: MetaDict = {}
112+
meta: MetaDict = {
113+
"source_datatype": "madng",
114+
}
114115

115116
if is_tfs_df:
116117
date_str = df.headers.get(DATE)

turn_by_turn/structures.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ class TransverseData:
2323
"""
2424
Object holding measured turn-by-turn data for both transverse planes in the form of pandas DataFrames.
2525
26-
The DataFrames should be N_observation-points x M_turns matrices, and usually contain
26+
The DataFrames should be N_(observation-points) x M_(turns) matrices, and usually contain
2727
the BPM/observation-point names as index, while the columns are simply numbered starting from ``0``.
28-
All DtatFrames should have the same N x M size.
28+
All DataFrames should have the same N x M size.
2929
3030
Attributes:
3131
X (pd.DataFrame): Horizontal position data
@@ -121,7 +121,7 @@ def __post_init__(self):
121121
self.nbunches = len(self.matrices)
122122

123123
if self.nturns is None or self.nturns < 1:
124-
raise ValueError("Number of turns need to be larger than zero.")
124+
raise ValueError("Number of turns needs to be larger than zero.")
125125

126126
if self.bunch_ids is None:
127127
self.bunch_ids = list(range(self.nbunches)) # we always need bunch-ids

0 commit comments

Comments
 (0)