1111- If provided a `TfsDataFrame` file with timestamps, plots of the 2D distribution and comparison
1212of fit parameters to cross sections are added.
1313"""
14+
1415import datetime
1516import glob
1617import gzip
2223import pandas as pd
2324import parse
2425import pytz
25-
2626import tfs
2727from generic_parser import EntryPointParameters , entrypoint
2828from omc3 .utils import logging_tools , time_tools
29+
2930from pylhc .constants .general import TFS_SUFFIX , TIME_COLUMN
3031from pylhc .forced_da_analysis import get_approximate_index
3132
3839
3940def get_params ():
4041 return EntryPointParameters (
41- directory = dict (
42- flags = ["-d" , "--directory" ],
43- required = True ,
44- type = str ,
45- help = "Directory containing the logged BSRT files." ,
46- ) ,
47- beam = dict (
48- flags = ["-b" , "--beam" ],
49- required = True ,
50- choices = ["B1" , "B2" ],
51- type = str ,
52- help = "Beam for which analysis is performed." ,
53- ) ,
54- outputdir = dict (
55- flags = ["-o" , "--outputdir" ],
56- type = str ,
57- default = None ,
58- help = (
42+ directory = {
43+ " flags" : ["-d" , "--directory" ],
44+ " required" : True ,
45+ " type" : str ,
46+ " help" : "Directory containing the logged BSRT files." ,
47+ } ,
48+ beam = {
49+ " flags" : ["-b" , "--beam" ],
50+ " required" : True ,
51+ " choices" : ["B1" , "B2" ],
52+ " type" : str ,
53+ " help" : "Beam for which analysis is performed." ,
54+ } ,
55+ outputdir = {
56+ " flags" : ["-o" , "--outputdir" ],
57+ " type" : str ,
58+ " default" : None ,
59+ " help" : (
5960 "Directory in which plots and dataframe will be saved in. If omitted, "
6061 "no data will be saved."
6162 ),
62- ) ,
63- starttime = dict (
64- flags = ["--starttime" ],
65- type = int ,
66- help = "Start of time window for analysis in milliseconds UTC." ,
67- ) ,
68- endtime = dict (
69- flags = ["--endtime" ],
70- type = int ,
71- help = "End of time window for analysis in milliseconds UTC." ,
72- ) ,
73- kick_df = dict (
74- flags = ["--kick_df" ],
75- default = None ,
76- help = (
63+ } ,
64+ starttime = {
65+ " flags" : ["--starttime" ],
66+ " type" : int ,
67+ " help" : "Start of time window for analysis in milliseconds UTC." ,
68+ } ,
69+ endtime = {
70+ " flags" : ["--endtime" ],
71+ " type" : int ,
72+ " help" : "End of time window for analysis in milliseconds UTC." ,
73+ } ,
74+ kick_df = {
75+ " flags" : ["--kick_df" ],
76+ " default" : None ,
77+ " help" : (
7778 f"TFS with column { TIME_COLUMN } with time stamps to be added in the plots. "
7879 f"Additionally, cross section at these timestamps will be plotted." ,
7980 ),
80- ),
81- show_plots = dict (flags = ["--show_plots" ], type = bool , default = False , help = "Show BSRT plots." ),
81+ },
82+ show_plots = {
83+ "flags" : ["--show_plots" ],
84+ "type" : bool ,
85+ "default" : False ,
86+ "help" : "Show BSRT plots." ,
87+ },
8288 )
8389
8490
@@ -159,7 +165,7 @@ def _select_files(opt, files_df):
159165
160166def _load_files_in_df (opt ):
161167 files_df = pd .DataFrame (
162- data = {"FILES" : glob .glob (str (Path (opt .directory ) / _get_bsrt_logger_fname (opt .beam , "*" )))}
168+ data = {"FILES" : glob .glob (str (Path (opt .directory ) / _get_bsrt_logger_fname (opt .beam , "*" )))} # noqa: PTH207
163169 )
164170
165171 files_df = files_df .assign (
@@ -175,8 +181,7 @@ def _load_files_in_df(opt):
175181 )
176182 files_df = files_df .assign (TIME = [f .timestamp () for f in files_df ["TIMESTAMP" ]])
177183
178- files_df = files_df .sort_values (by = ["TIME" ]).reset_index (drop = True ).set_index ("TIME" )
179- return files_df
184+ return files_df .sort_values (by = ["TIME" ]).reset_index (drop = True ).set_index ("TIME" )
180185
181186
182187def _get_timestamp_from_name (name , formatstring ):
@@ -189,7 +194,7 @@ def _get_timestamp_from_name(name, formatstring):
189194def _check_and_fix_entries (entry ):
190195 # pd.to_csv does not handle np.array as entries nicely, converting to list circumvents this
191196 for key , val in entry .items ():
192- if isinstance (val , (np .ndarray , tuple )):
197+ if isinstance (val , (np .ndarray | tuple )):
193198 entry [key ] = list (val )
194199 if np .array (val ).size == 0 :
195200 entry [key ] = np .nan
@@ -199,7 +204,8 @@ def _check_and_fix_entries(entry):
199204def _load_pickled_data (opt , files_df ):
200205 merged_df = pd .DataFrame ()
201206 for bsrtfile in files_df ["FILES" ]:
202- data = pickle .load (gzip .open (bsrtfile , "rb" ))
207+ with gzip .open (bsrtfile , "rb" ) as f :
208+ data = pickle .load (f )
203209 new_df = pd .DataFrame .from_records ([_check_and_fix_entries (entry ) for entry in data ])
204210 merged_df = pd .concat ([merged_df , new_df ], axis = "index" , ignore_index = True )
205211
@@ -224,7 +230,6 @@ def _add_kick_lines(ax, df):
224230
225231
226232def _fit_var (ax , bsrt_df , plot_dict , opt ):
227-
228233 ax [plot_dict ["idx" ]].plot (
229234 bsrt_df .index , [entry [plot_dict ["fitidx" ]] for entry in bsrt_df ["lastFitResults" ]]
230235 )
@@ -234,7 +239,6 @@ def _fit_var(ax, bsrt_df, plot_dict, opt):
234239
235240
236241def plot_fit_variables (opt , bsrt_df ):
237-
238242 fig , ax = plt .subplots (nrows = 2 , ncols = 3 , figsize = (20 , 9 ), sharex = True , constrained_layout = True )
239243
240244 plot_dicts = [
@@ -292,8 +296,8 @@ def _full_crossection(ax, bsrt_df, plot_dict, opt):
292296 ax ,
293297 bsrt_df .reset_index (),
294298 "TimeIndex" ,
295- f' projPositionSet{ plot_dict [" idx" ] } ' ,
296- f' projDataSet{ plot_dict [" idx" ] } ' ,
299+ f" projPositionSet{ plot_dict [' idx' ] } " ,
300+ f" projDataSet{ plot_dict [' idx' ] } " ,
297301 )
298302 ax .plot (
299303 bsrt_df .index ,
@@ -326,7 +330,6 @@ def _full_crossection(ax, bsrt_df, plot_dict, opt):
326330
327331
328332def plot_full_crosssection (opt , bsrt_df ):
329-
330333 plot_dicts = [
331334 {"idx" : 1 , "fitresult" : 3 , "fiterror" : 4 , "title" : "Horizontal Cross section" },
332335 {"idx" : 2 , "fitresult" : 8 , "fiterror" : 9 , "title" : "Vertical Cross section" },
@@ -344,7 +347,7 @@ def plot_full_crosssection(opt, bsrt_df):
344347
345348def _gauss (x , * p ):
346349 a , b , c = p
347- return a * np .exp (- ((x - b ) ** 2 ) / (2.0 * c ** 2.0 ))
350+ return a * np .exp (- ((x - b ) ** 2 ) / (2.0 * c ** 2.0 ))
348351
349352
350353def _reshaped_imageset (df ):
@@ -408,7 +411,6 @@ def plot_crosssection_for_timesteps(opt, bsrt_df):
408411
409412
410413def _aux_variables (ax , bsrt_df , plot_dict , opt ):
411-
412414 ax .plot (
413415 bsrt_df .index , bsrt_df [plot_dict ["variable1" ]], color = "red" , label = plot_dict ["variable1" ]
414416 )
0 commit comments