Skip to content

Commit 5fe548b

Browse files
Analyse rawdata using oscillation midpoint in FBBA.
1 parent c9b79e4 commit 5fe548b

2 files changed

Lines changed: 50 additions & 45 deletions

File tree

src/dls_bba/fbba.py

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging as log
22
from math import ceil
3-
from typing import Any, Dict, List, Tuple
3+
from typing import Any
44

55
import numpy as np
66
from cothread import Sleep
@@ -28,7 +28,7 @@ def __init__(self, machine: Machine) -> None:
2828
"""
2929
super().__init__(machine)
3030

31-
def run(self, components_pair: List[Components]) -> RawData:
31+
def run(self, components_pair: list[Components]) -> RawData:
3232
"""The Fast BBA Process.
3333
3434
Args:
@@ -37,8 +37,8 @@ def run(self, components_pair: List[Components]) -> RawData:
3737
Returns:
3838
The RawData object
3939
"""
40-
rawdata: Dict[str, Any] = {}
41-
metadata: Dict[str, Any] = {}
40+
rawdata: dict[str, Any] = {}
41+
metadata: dict[str, Any] = {}
4242
config = self._machine.config.get_settings()
4343
metadata.update(config)
4444
metadata["method"] = "FastBBA"
@@ -52,7 +52,7 @@ def run(self, components_pair: List[Components]) -> RawData:
5252
for components in components_pair:
5353
log.debug(f"Component: {components}")
5454
for quadrupole, quad_name in zip(
55-
components.quadrupoles, components.quadrupoles_names
55+
components.quadrupoles, components.quadrupoles_names, strict=True
5656
):
5757
log.debug(f"Quad: {quad_name} of {components.quadrupoles_names}")
5858
(
@@ -143,9 +143,9 @@ def select_data(
143143
self,
144144
data: np.ndarray,
145145
axis: str,
146-
exc_data: Tuple[Excitation, Excitation],
146+
exc_data: tuple[Excitation, Excitation],
147147
decimated: bool,
148-
) -> List[np.ndarray]:
148+
) -> list[np.ndarray]:
149149
"""Extract FA data that covers the excitations exc_high and exc_low.
150150
151151
The input data array should cover the full length of both excitations.
@@ -165,15 +165,12 @@ def select_data(
165165

166166
exc_high, exc_low = exc_data
167167
# Note: array data must include the timestamps.
168-
log.debug("Raw data shape: {}".format(data.shape))
168+
log.debug(f"Raw data shape: {data.shape}")
169+
log.debug(f"Timestamp range in raw data: {data[0, 0, 0]} - {data[-1, 0, 0]}")
170+
log.debug(f"Excitation length: {exc_high.count}")
169171
log.debug(
170-
"Timestamp range in raw data: {} - {}".format(data[0, 0, 0], data[-1, 0, 0])
171-
)
172-
log.debug("Excitation length: {}".format(exc_high.count))
173-
log.debug(
174-
"Trailing data to crop: {}.".format(
175-
data[-1, 0, 0] - (exc_low.start_time + exc_low.count)
176-
)
172+
f"Trailing data to crop: "
173+
f"{data[-1, 0, 0] - (exc_low.start_time + exc_low.count)}."
177174
)
178175
assert exc_high.count == exc_low.count, "Excitations different lengths"
179176
# Extract timestamps from data
@@ -186,7 +183,7 @@ def select_data(
186183
length = ceil(exc_high.count / 10) if decimated else exc_high.count
187184
high_data = data[high_start : high_start + length, :, plane]
188185
low_data = data[low_start : low_start + length, :, plane]
189-
log.debug("Selected data shape: {} {}".format(high_data.shape, low_data.shape))
186+
log.debug(f"Selected data shape: {high_data.shape} {low_data.shape}")
190187
assert high_data.shape == low_data.shape
191188
return [high_data, low_data]
192189

@@ -260,8 +257,8 @@ def analyse(self, rawdata: RawData) -> Results:
260257
bpm_index = bpm_number - np.sum(
261258
enabled_bpms[:bpm_number] == False # noqa false positive
262259
)
263-
results: Dict[str, List[float]] = {}
264-
plotting: Dict[str, Dict[str, np.ndarray]] = {}
260+
results: dict[str, list[float]] = {}
261+
plotting: dict[str, dict[str, np.ndarray]] = {}
265262

266263
quad_names = []
267264
for key in data.keys():
@@ -289,9 +286,13 @@ def analyse(self, rawdata: RawData) -> Results:
289286
good = q_diff.std(0) > q_diff.std(0).max() / 2
290287
q_diff_good = q_diff[:, good]
291288

292-
# Use a single fit operation, then transform with the straight line equation
289+
# Use a single fit operation, then transform with straight line equation
290+
oscillation_midpoint = (
291+
q_high_clean[:, bpm_index] + q_low_clean[:, bpm_index]
292+
) / 2
293+
oscillation_size = q_diff_good
293294
fit = np.polynomial.polynomial.polyfit(
294-
q_high_clean[:, bpm_index], q_diff_good, 1
295+
oscillation_midpoint, oscillation_size, 1
295296
)
296297
p = np.array([1 / fit[1], -fit[0] / fit[1]]).T
297298

@@ -302,8 +303,8 @@ def analyse(self, rawdata: RawData) -> Results:
302303

303304
# plotting data
304305
plotting[key] = {
305-
"x": q_high_clean[:, bpm_index] / NM_TO_MM_UNIT_CONV,
306-
"y": q_diff_good / NM_TO_MM_UNIT_CONV,
306+
"x": oscillation_midpoint / NM_TO_MM_UNIT_CONV,
307+
"y": oscillation_size / NM_TO_MM_UNIT_CONV,
307308
}
308309

309310
offsets = self.create_offsets_dict(results, metadata)

src/dls_bba/simfbba.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging as log
22
from math import ceil
3-
from typing import Any, Dict, List
3+
from typing import Any
44

55
import numpy as np
66
from cothread import Sleep
@@ -29,7 +29,7 @@ def __init__(self, machine: Machine) -> None:
2929
"""
3030
super().__init__(machine)
3131

32-
def run(self, components_pair: List[Components]) -> RawData:
32+
def run(self, components_pair: list[Components]) -> RawData:
3333
"""The Simultaneous Fast BBA Process.
3434
3535
Args:
@@ -38,8 +38,8 @@ def run(self, components_pair: List[Components]) -> RawData:
3838
Returns:
3939
The RawData object.
4040
"""
41-
rawdata: Dict[str, Any] = {}
42-
metadata: Dict[str, Any] = {}
41+
rawdata: dict[str, Any] = {}
42+
metadata: dict[str, Any] = {}
4343
config = self._machine.config.get_settings()
4444
metadata.update(config)
4545
metadata["method"] = "SimFastBBA"
@@ -51,12 +51,15 @@ def run(self, components_pair: List[Components]) -> RawData:
5151

5252
log.info(f"BPM: {components_pair[0].bpm_name}")
5353
for quadrupole, quad_name in zip(
54-
components_pair[0].quadrupoles, components_pair[0].quadrupoles_names
54+
components_pair[0].quadrupoles,
55+
components_pair[0].quadrupoles_names,
56+
strict=True,
5557
):
5658
log.debug(f"BPM: {components_pair[0].bpm_name}")
5759
log.debug(f"Quad: {quad_name} of {components_pair[0].quadrupoles_names}")
5860
log.debug(
59-
f"Corrector1: {components_pair[0].corrector_name}, Corrector2: {components_pair[1].corrector_name}"
61+
f"Corrector1: {components_pair[0].corrector_name}, "
62+
f"Corrector2: {components_pair[1].corrector_name}"
6063
)
6164
(
6265
quad_start,
@@ -184,9 +187,9 @@ def select_data(
184187
self,
185188
data: np.ndarray,
186189
plane_index: int,
187-
exc_data: List[Excitation],
190+
exc_data: list[Excitation],
188191
decimated: bool,
189-
) -> List[np.ndarray]:
192+
) -> list[np.ndarray]:
190193
"""Extract FA data that covers the excitations exc_high and exc_low.
191194
192195
The input data array should cover the full length of both excitations.
@@ -201,15 +204,12 @@ def select_data(
201204
"""
202205
# Note: array data must include the timestamps.
203206
exc_high, exc_low = exc_data
204-
log.debug("Raw data shape: {}".format(data.shape))
207+
log.debug(f"Raw data shape: {data.shape}")
208+
log.debug(f"Timestamp range in raw data: {data[0, 0, 0]} - {data[-1, 0, 0]}")
209+
log.debug(f"Excitation length: {exc_high.count}")
205210
log.debug(
206-
"Timestamp range in raw data: {} - {}".format(data[0, 0, 0], data[-1, 0, 0])
207-
)
208-
log.debug("Excitation length: {}".format(exc_high.count))
209-
log.debug(
210-
"Trailing data to crop: {}.".format(
211-
data[-1, 0, 0] - (exc_low.start_time + exc_low.count)
212-
)
211+
f"Trailing data to crop: "
212+
f"{data[-1, 0, 0] - (exc_low.start_time + exc_low.count)}."
213213
)
214214
assert exc_high.count == exc_low.count, "Excitations different lengths"
215215
# Extract timestamps from data
@@ -222,7 +222,7 @@ def select_data(
222222
length = ceil(exc_high.count / 10) if decimated else exc_high.count
223223
high_data = data[high_start : high_start + length, :, plane_index]
224224
low_data = data[low_start : low_start + length, :, plane_index]
225-
log.debug("Selected data shape: {} {}".format(high_data.shape, low_data.shape))
225+
log.debug(f"Selected data shape: {high_data.shape} {low_data.shape}")
226226
assert high_data.shape == low_data.shape
227227
return [high_data, low_data]
228228

@@ -295,8 +295,8 @@ def analyse(self, rawdata: RawData) -> Results:
295295
bpm_index = bpm_number - np.sum(
296296
enabled_bpms[:bpm_number] == False # noqa false positive
297297
)
298-
results: Dict[str, List[float]] = {}
299-
plotting: Dict[str, Dict[str, np.ndarray]] = {}
298+
results: dict[str, list[float]] = {}
299+
plotting: dict[str, dict[str, np.ndarray]] = {}
300300

301301
quad_names = []
302302
for key in data.keys():
@@ -323,9 +323,13 @@ def analyse(self, rawdata: RawData) -> Results:
323323
good = q_diff.std(0) > q_diff.std(0).max() / 2
324324
q_diff_good = q_diff[:, good]
325325

326-
# Use a single fit operation, then transform with the straight line equation
326+
# Use a single fit operation, then transform with straight line equation
327+
oscillation_midpoint = (
328+
q_high_clean[:, bpm_index] + q_low_clean[:, bpm_index]
329+
) / 2
330+
oscillation_size = q_diff_good
327331
fit = np.polynomial.polynomial.polyfit(
328-
q_high_clean[:, bpm_index], q_diff_good, 1
332+
oscillation_midpoint, oscillation_size, 1
329333
)
330334
p = np.array([1 / fit[1], -fit[0] / fit[1]]).T
331335

@@ -335,8 +339,8 @@ def analyse(self, rawdata: RawData) -> Results:
335339

336340
# plotting data
337341
plotting[key] = {
338-
"x": q_high_clean[:, bpm_index] / NM_TO_MM_UNIT_CONV,
339-
"y": q_diff_good / NM_TO_MM_UNIT_CONV,
342+
"x": oscillation_midpoint / NM_TO_MM_UNIT_CONV,
343+
"y": oscillation_size / NM_TO_MM_UNIT_CONV,
340344
}
341345

342346
offsets = self.create_offsets_dict(results, metadata)

0 commit comments

Comments
 (0)