-
Notifications
You must be signed in to change notification settings - Fork 2
/
bsrt_analysis.py
459 lines (368 loc) · 15.1 KB
/
bsrt_analysis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
"""
BSRT Analysis
-------------
Top-level script to query BRST data, then perform an analysis and output the generated plots. The
script runs through the following steps:
- Processes the output files of ``BSRT_logger.py`` for a given timeframe, returns them in as a
`TfsDataFrame` for further processing.
- Additionally, plots for quick checks of fit parameters, auxiliary variables and beam evolution
are generated.
- If provided a `TfsDataFrame` file with timestamps, plots of the 2D distribution and comparison
of fit parameters to cross sections are added.
"""
import datetime
import glob
import gzip
import pickle
from pathlib import Path
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import parse
import pytz
import tfs
from generic_parser import EntryPointParameters, entrypoint
from omc3.utils import logging_tools, time_tools
from pylhc.constants.general import TFS_SUFFIX, TIME_COLUMN
from pylhc.forced_da_analysis import get_approximate_index
LOG = logging_tools.get_logger(__name__)
PLOT_FILE_SUFFIX = ".pdf"
BSRT_FESA_TIME_FORMAT = "%Y/%m/%d %H:%M:%S.%f"
OLD_FILENAMING_CONV = "{}-{}-{}-{}-{}-{}.{}"
NEW_FILENAMING_CONV = "{}_{}_{}@{}_{}_{}_{}"
def get_params():
return EntryPointParameters(
directory=dict(
flags=["-d", "--directory"],
required=True,
type=str,
help="Directory containing the logged BSRT files.",
),
beam=dict(
flags=["-b", "--beam"],
required=True,
choices=["B1", "B2"],
type=str,
help="Beam for which analysis is performed.",
),
outputdir=dict(
flags=["-o", "--outputdir"],
type=str,
default=None,
help=(
"Directory in which plots and dataframe will be saved in. If omitted, "
"no data will be saved."
),
),
starttime=dict(
flags=["--starttime"],
type=int,
help="Start of time window for analysis in milliseconds UTC.",
),
endtime=dict(
flags=["--endtime"],
type=int,
help="End of time window for analysis in milliseconds UTC.",
),
kick_df=dict(
flags=["--kick_df"],
default=None,
help=(
f"TFS with column {TIME_COLUMN} with time stamps to be added in the plots. "
f"Additionally, cross section at these timestamps will be plotted.",
),
),
show_plots=dict(flags=["--show_plots"], type=bool, default=False, help="Show BSRT plots."),
)
@entrypoint(get_params(), strict=True)
def main(opt):
LOG.info("Starting BSRT analysis.")
LOG.info("Indexing files in directory.")
files_df = _load_files_in_df(opt)
LOG.info("Select files based on provided timestamps.")
files_df = _select_files(opt, files_df)
LOG.info("Load BSRT files in selected time frame.")
bsrt_df = _load_pickled_data(opt, files_df)
results = {"bsrt_df": bsrt_df}
if not opt["show_plots"] and opt["outputdir"] is None:
LOG.info("Neither plot display nor outputdir was selected. Plotting is omitted")
return results
if opt["kick_df"] is not None and isinstance(opt["kick_df"], str):
opt["kick_df"] = tfs.read(opt["kick_df"], index=TIME_COLUMN)
LOG.info("Plotting Fitvariables.")
results.update(fitvariables=plot_fit_variables(opt, bsrt_df))
LOG.info("Plotting full cross section.")
results.update(full_crosssection=plot_full_crosssection(opt, bsrt_df))
LOG.info("Plotting auxiliary variables.")
results.update(auxiliary_variables=plot_auxiliary_variables(opt, bsrt_df))
if opt["kick_df"] is not None:
LOG.info("Plotting cross section for timesteps.")
results.update(crosssection_for_timesteps=plot_crosssection_for_timesteps(opt, bsrt_df))
return results
# File Name Functions ----------------------------------------------------------
def _get_bsrt_logger_fname(beam, timestamp) -> str:
return f"data_BSRT_{beam}_{timestamp}.dat.gz"
def _get_bsrt_tfs_fname(beam) -> str:
return f"data_BSRT_{beam}{TFS_SUFFIX}"
def _get_fitvar_plot_fname(beam) -> str:
return f"plot_BSRT_FitVariables_{beam}{PLOT_FILE_SUFFIX}"
def _get_2dcrossection_plot_fname(beam) -> str:
return f"plot_BSRT_2DCross_section_{beam}{PLOT_FILE_SUFFIX}"
def _get_crossection_plot_fname(beam, timestamp) -> str:
return f"plot_BSRT_Cross_section_{timestamp}_{beam}{PLOT_FILE_SUFFIX}"
def _get_auxiliary_var_plot_fname(beam) -> str:
return f"plot_BSRT_auxVariables_{beam}{PLOT_FILE_SUFFIX}"
# File Handling ---------------------------------------------------------------
def _select_files(opt, files_df):
if opt["endtime"] is not None and opt["starttime"] is not None:
assert opt["endtime"] >= opt["starttime"]
indices = []
for time, fct in zip(
[opt["starttime"], opt["endtime"]], ["first_valid_index", "last_valid_index"]
):
indices.append(
get_approximate_index(files_df, time if time is not None else getattr(files_df, fct)())
)
return files_df.iloc[indices[0] : indices[1] + 1]
def _load_files_in_df(opt):
files_df = pd.DataFrame(
data={"FILES": glob.glob(str(Path(opt.directory) / _get_bsrt_logger_fname(opt.beam, "*")))}
)
files_df = files_df.assign(
TIMESTAMP=[
_get_timestamp_from_name(
Path(f).name,
_get_bsrt_logger_fname(
opt.beam, NEW_FILENAMING_CONV if ("@" in Path(f).name) else OLD_FILENAMING_CONV
),
)
for f in files_df["FILES"]
]
)
files_df = files_df.assign(TIME=[f.timestamp() for f in files_df["TIMESTAMP"]])
files_df = files_df.sort_values(by=["TIME"]).reset_index(drop=True).set_index("TIME")
return files_df
def _get_timestamp_from_name(name, formatstring):
year, month, day, hour, minute, second, microsecond = map(int, parse.parse(formatstring, name))
return datetime.datetime(
year, month, day, hour, minute, second, microsecond, tzinfo=pytz.timezone("UTC")
)
def _check_and_fix_entries(entry):
# pd.to_csv does not handle np.array as entries nicely, converting to list circumvents this
for key, val in entry.items():
if isinstance(val, (np.ndarray, tuple)):
entry[key] = list(val)
if np.array(val).size == 0:
entry[key] = np.nan
return entry
def _load_pickled_data(opt, files_df):
merged_df = pd.DataFrame()
for bsrtfile in files_df["FILES"]:
data = pickle.load(gzip.open(bsrtfile, "rb"))
new_df = pd.DataFrame.from_records([_check_and_fix_entries(entry) for entry in data])
merged_df = pd.concat([merged_df, new_df], axis="index", ignore_index=True)
merged_df = merged_df.set_index(
pd.to_datetime(merged_df["acqTime"], format=BSRT_FESA_TIME_FORMAT, utc=True)
)
merged_df.index.name = "TimeIndex"
merged_df = merged_df.drop_duplicates(subset=["acqCounter", "acqTime"])
if opt.outputdir is not None:
merged_df.to_csv(Path(opt.outputdir, _get_bsrt_tfs_fname(opt.beam)))
return merged_df
# Plotting Functions ----------------------------------------------------------
def _add_kick_lines(ax, df):
if df is not None:
for idx, _ in df.iterrows():
ax.axvline(x=time_tools.cern_utc_string_to_utc(idx), color="red", linestyle="--")
def _fit_var(ax, bsrt_df, plot_dict, opt):
ax[plot_dict["idx"]].plot(
bsrt_df.index, [entry[plot_dict["fitidx"]] for entry in bsrt_df["lastFitResults"]]
)
ax[plot_dict["idx"]].set_title(plot_dict["title"])
ax[plot_dict["idx"]].set_ylim(bottom=plot_dict["bottom"])
_add_kick_lines(ax[plot_dict["idx"]], opt["kick_df"])
def plot_fit_variables(opt, bsrt_df):
fig, ax = plt.subplots(nrows=2, ncols=3, figsize=(20, 9), sharex=True, constrained_layout=True)
plot_dicts = [
{"idx": (0, 0), "fitidx": 2, "title": "Horizontal Amplitude", "bottom": 0},
{"idx": (0, 1), "fitidx": 3, "title": "Horizontal Center", "bottom": None},
{"idx": (0, 2), "fitidx": 4, "title": "Horizontal Sigma", "bottom": 0},
{"idx": (1, 0), "fitidx": 7, "title": "Vertical Amplitude", "bottom": 0},
{"idx": (1, 1), "fitidx": 8, "title": "Vertical Center", "bottom": None},
{"idx": (1, 2), "fitidx": 9, "title": "Vertical Sigma", "bottom": 0},
]
[_fit_var(ax, bsrt_df, plot_dict, opt) for plot_dict in plot_dicts]
if opt["outputdir"] is not None:
plt.savefig(Path(opt.outputdir, _get_fitvar_plot_fname(opt.beam)))
if opt["show_plots"]:
plt.show()
return fig
def flattend_column(df, col):
flat_column = []
for _, entry in df.iterrows():
flat_column = [*flat_column, *entry[col]]
return flat_column
def pcolormesh_irregulargrid(ax, df, x_column, y_column, z_column):
df["Starttime"] = df[x_column] - 0.5 * df[x_column].diff().fillna(pd.Timedelta(seconds=0))
df["Endtime"] = df[x_column] + 0.5 * df[x_column].diff().shift(periods=-1).fillna(
pd.Timedelta(seconds=0)
)
vmax = np.max(flattend_column(df, z_column))
vmin = np.min(flattend_column(df, z_column))
for _, row in df.iterrows():
ax.pcolormesh(
[row["Starttime"], row["Endtime"]],
np.concatenate(
[
row[y_column][0],
row[y_column][:-1] + 0.5 * np.diff(row[y_column]),
row[y_column][-1],
],
axis=None,
),
np.array([row[z_column]]).T,
vmin=vmin,
vmax=vmax,
cmap="inferno",
)
def _full_crossection(ax, bsrt_df, plot_dict, opt):
pcolormesh_irregulargrid(
ax,
bsrt_df.reset_index(),
"TimeIndex",
f'projPositionSet{plot_dict["idx"]}',
f'projDataSet{plot_dict["idx"]}',
)
ax.plot(
bsrt_df.index,
[entry[plot_dict["fitresult"]] for entry in bsrt_df["lastFitResults"]],
color="white",
linewidth=0.5,
)
ax.plot(
bsrt_df.index,
[
entry[plot_dict["fitresult"]] + entry[plot_dict["fiterror"]]
for entry in bsrt_df["lastFitResults"]
],
color="white",
linestyle="--",
linewidth=0.3,
)
ax.plot(
bsrt_df.index,
[
entry[plot_dict["fitresult"]] - entry[plot_dict["fiterror"]]
for entry in bsrt_df["lastFitResults"]
],
color="white",
linestyle="--",
linewidth=0.3,
)
ax.set_title(plot_dict["title"])
_add_kick_lines(ax, opt["kick_df"])
def plot_full_crosssection(opt, bsrt_df):
plot_dicts = [
{"idx": 1, "fitresult": 3, "fiterror": 4, "title": "Horizontal Cross section"},
{"idx": 2, "fitresult": 8, "fiterror": 9, "title": "Vertical Cross section"},
]
fig, ax = plt.subplots(nrows=len(plot_dicts), ncols=1, figsize=(18, 9), constrained_layout=True)
[_full_crossection(axis, bsrt_df, plot_dict, opt) for axis, plot_dict in zip(ax, plot_dicts)]
if opt["outputdir"] is not None:
plt.savefig(Path(opt.outputdir, _get_2dcrossection_plot_fname(opt.beam)))
if opt["show_plots"]:
plt.show()
return fig
def _gauss(x, *p):
a, b, c = p
return a * np.exp(-((x - b) ** 2) / (2.0 * c ** 2.0))
def _reshaped_imageset(df):
return np.reshape(
df["imageSet"], (df["acquiredImageRectangle"][3], df["acquiredImageRectangle"][2])
)
def plot_crosssection_for_timesteps(opt, bsrt_df):
kick_df = opt["kick_df"]
figlist = []
for idx, _ in kick_df.iterrows():
timestamp = pd.to_datetime(time_tools.cern_utc_string_to_utc(idx))
data_row = bsrt_df.iloc[get_approximate_index(bsrt_df, timestamp)]
fig, ax = plt.subplots(nrows=1, ncols=3, figsize=(18, 9), constrained_layout=True)
fig.suptitle(f"Timestamp: {timestamp}")
ax[0].imshow(_reshaped_imageset(data_row), cmap="hot", interpolation="nearest")
ax[0].set_title("2D Pixel count")
ax[1].plot(data_row["projPositionSet1"], data_row["projDataSet1"], color="darkred")
ax[1].plot(
data_row["projPositionSet1"],
_gauss(
np.array(data_row["projPositionSet1"]),
data_row["lastFitResults"][2],
data_row["lastFitResults"][3],
data_row["lastFitResults"][4],
),
color="darkgreen",
label="Gaussian Fit",
)
ax[1].set_ylim(bottom=0)
ax[1].legend()
ax[1].set_title("Horizontal Projection")
ax[2].plot(data_row["projPositionSet2"], data_row["projDataSet2"], color="darkred")
ax[2].plot(
data_row["projPositionSet2"],
_gauss(
np.array(data_row["projPositionSet2"]),
data_row["lastFitResults"][7],
data_row["lastFitResults"][8],
data_row["lastFitResults"][9],
),
color="darkgreen",
label="Gaussian Fit",
)
ax[2].set_ylim(bottom=0)
ax[2].legend()
ax[2].set_title("Vertical Projection")
if opt["outputdir"] is not None:
plt.savefig(Path(opt.outputdir, _get_crossection_plot_fname(opt.beam, timestamp)))
if opt["show_plots"]:
plt.show()
figlist.append(fig)
return figlist
def _aux_variables(ax, bsrt_df, plot_dict, opt):
ax.plot(
bsrt_df.index, bsrt_df[plot_dict["variable1"]], color="red", label=plot_dict["variable1"]
)
ax.legend(loc="upper left")
ax.set_title(plot_dict["title"])
_add_kick_lines(ax, opt["kick_df"])
if plot_dict["variable2"] is not None:
ax2 = ax.twinx()
ax2.plot(
bsrt_df.index,
bsrt_df[plot_dict["variable2"]],
color="blue",
label=plot_dict["variable2"],
)
ax2.legend(loc="upper right")
def plot_auxiliary_variables(opt, bsrt_df):
plot_dicts = [
{"variable1": "acqCounter", "variable2": None, "title": "acqCounter"},
{"variable1": "lastAcquiredBunch", "variable2": None, "title": "lastAcquiredBunch"},
{"variable1": "cameraGainVoltage", "variable2": None, "title": "cameraGainVoltage"},
{
"variable1": "opticsResolutionSet1",
"variable2": "opticsResolutionSet2",
"title": "opticsResolutionSet",
},
{"variable1": "imageCenterSet1", "variable2": "imageCenterSet2", "title": "imageCenter"},
{"variable1": "betaTwissSet1", "variable2": "betaTwissSet2", "title": "betaTwiss"},
{"variable1": "imageScaleSet1", "variable2": "imageScaleSet2", "title": "imageScale"},
]
fig, ax = plt.subplots(nrows=len(plot_dicts), ncols=1, figsize=(9, 20), sharex=True)
[_aux_variables(axis, bsrt_df, plot_dict, opt) for axis, plot_dict in zip(ax, plot_dicts)]
if opt["outputdir"] is not None:
plt.savefig(Path(opt.outputdir, _get_auxiliary_var_plot_fname(opt.beam)))
if opt["show_plots"]:
plt.show()
return fig
# Script Mode ------------------------------------------------------------------
if __name__ == "__main__":
main()