1- """
1+ """
22Full Import of K-Modulation Results
33-----------------------------------
44
99The results are first sorted by IP and averaged. The averaged results are
1010written into a sub-folder of the given `output_dir`.
1111
12- If data for both beams is present, these averages are then used to calculate the
13- luminosity imbalance between each combination of IPs.
12+ If data for both beams is present, these averages are then used to calculate the
13+ luminosity imbalance between each combination of IPs.
1414These results are again written out into the same sub-folder of the given `output_dir`.
1515
16- Finally, the averaged results for the given `beam` are then written out into
16+ Finally, the averaged results for the given `beam` are then written out into
1717the `beta_kmod` and `betastar` tfs-files in the `output_dir`.
1818
1919
2323
2424- **meas_paths** *(PathOrStr)*:
2525
26- Directories of K-modulation results to import.
26+ Directories of K-modulation results to import.
2727 These need to be the paths to the root-folders containing B1 and B2 sub-dirs.
2828
2929
3535- **beam** *(int)*:
3636
3737 Beam for which to import.
38-
38+
3939
4040- **output_dir** *(PathOrStr)*:
4141
9191def _get_params ():
9292 """
9393 Creates and returns the parameters for the kmod_output function.
94-
94+
9595 """
9696 params = EntryPointParameters ()
9797 params .add_parameter (
9898 name = "meas_paths" ,
9999 required = True ,
100- nargs = '+' ,
100+ nargs = "+" ,
101101 type = PathOrStr ,
102102 help = "Directories of K-modulation results to import. "
103- "These need to be the paths to the root-folders containing B1 and B2 sub-dirs."
103+ "These need to be the paths to the root-folders containing B1 and B2 sub-dirs." ,
104104 )
105105 params .add_parameter (
106106 name = "model" ,
107107 required = True ,
108108 type = PathOrStr ,
109- help = "Path to the model."
109+ help = "Path to the model." ,
110110 )
111111 params .add_parameter (
112112 name = "beam" ,
113113 required = True ,
114114 type = int ,
115- help = "Beam for which to import."
115+ help = "Beam for which to import." ,
116116 )
117117 params .add_parameter (
118118 name = "output_dir" ,
119119 type = PathOrStr ,
120120 required = True ,
121- help = "Path to the directory where to write the output files."
121+ help = "Path to the directory where to write the output files." ,
122122 )
123123 params .add_parameter (
124124 name = "show_plots" ,
@@ -132,32 +132,32 @@ def _get_params():
132132def import_kmod_results (opt : DotDict ) -> None :
133133 """
134134 Performs the full import procedure of the "raw" K-Modulation results.
135-
135+
136136 Args:
137137 meas_paths (Sequence[Path|str]):
138138 Directories of K-modulation results to import.
139139 These need to be the paths to the root-folders containing B1 and B2 sub-dirs.
140-
140+
141141 model (Path|str):
142142 Path to the model Twiss file.
143-
143+
144144 beam (int):
145145 Beam for which to import.
146-
146+
147147 output_dir (Path|str):
148- Path to the output directory, i.e. the optics-measurement directory
148+ Path to the output directory, i.e. the optics-measurement directory
149149 into which to import these K-Modulation results.
150-
150+
151151 show_plots (bool):
152152 If True, show the plots. Default: False.
153153
154-
154+
155155 Returns:
156156 Dictionary of kmod-DataFrames by planes.
157157 """
158158 LOG .info ("Starting full K-modulation import." )
159159
160- # Prepare IO ---
160+ # Prepare IO ---
161161 opt .output_dir = Path (opt .output_dir )
162162 opt .output_dir .mkdir (exist_ok = True )
163163 save_config (opt .output_dir , opt , __file__ )
@@ -166,27 +166,25 @@ def import_kmod_results(opt: DotDict) -> None:
166166 average_output_dir .mkdir (exist_ok = True )
167167
168168 df_model = read_model_df (opt .model )
169-
169+
170170 # Perform averaging and import ---
171171 averaged_results = average_all_results (
172- meas_paths = opt .meas_paths ,
173- df_model = df_model ,
174- beam = opt .beam ,
175- output_dir = average_output_dir
172+ meas_paths = opt .meas_paths ,
173+ df_model = df_model ,
174+ beam = opt .beam ,
175+ output_dir = average_output_dir ,
176176 )
177-
177+
178178 calculate_all_lumi_imbalances (
179- averaged_results ,
180- df_model = df_model ,
181- output_dir = average_output_dir
179+ averaged_results , df_model = df_model , output_dir = average_output_dir
182180 )
183181
184182 results_list = [
185- df
186- for ip in averaged_results .keys ()
183+ df
184+ for ip in averaged_results .keys ()
187185 for df in (
188- averaged_results [ip ][0 ], # beta-star results
189- averaged_results [ip ][opt .beam ] # bpm results of the specific beam
186+ averaged_results [ip ][0 ], # beta-star results
187+ averaged_results [ip ][opt .beam ], # bpm results of the specific beam
190188 )
191189 ]
192190 import_kmod_data (
@@ -199,24 +197,25 @@ def import_kmod_results(opt: DotDict) -> None:
199197
200198# Averaging ---
201199
200+
202201def average_all_results (
203202 meas_paths : Sequence [Path | str ],
204203 df_model : tfs .TfsDataFrame ,
205204 beam : int ,
206205 output_dir : Path | str ,
207206 show_plots : bool = False ,
208207 ) -> dict [str , dict [int , tfs .TfsDataFrame ]]:
209- """ Averages all kmod results.
208+ """Averages all kmod results.
210209
211210 Args:
212- meas_paths (Sequence[Path | str]): Paths to the K-modulation results.
213- df_model (tfs.TfsDataFrame): DataFrame with the model.
214- beam (int): Beam for which to average.
211+ meas_paths (Sequence[Path | str]): Paths to the K-modulation results.
212+ df_model (tfs.TfsDataFrame): DataFrame with the model.
213+ beam (int): Beam for which to average.
215214 output_dir (Path | str, optional): Path to the output directory. Defaults to None.
216215 show_plots (bool, optional): If True, show the plots. Defaults to False.
217216
218217 Returns:
219- dict[int, tfs.TfsDataFrame]: Averaged kmod results, sorted by IP.
218+ dict[int, tfs.TfsDataFrame]: Averaged kmod results, sorted by IP.
220219 """
221220 sorted_paths = _sort_paths_by_ip (meas_paths , beam )
222221
@@ -230,16 +229,18 @@ def average_all_results(
230229 meas_paths = paths ,
231230 output_dir = output_dir ,
232231 plot = True ,
233- show_plots = show_plots
232+ show_plots = show_plots ,
234233 )
235234 averaged_results [ip ] = average
236235
237236 return averaged_results
238237
239238
240- def _sort_paths_by_ip (paths : Sequence [str | Path ], beam : int ) -> dict [str , list [str | Path ]]:
241- """ Sorts the kmod results files by IP.
242-
239+ def _sort_paths_by_ip (
240+ paths : Sequence [str | Path ], beam : int
241+ ) -> dict [str , list [str | Path ]]:
242+ """Sorts the kmod results files by IP.
243+
243244 Identification of the IP is done by reading the `lsa_results.tfs` files.
244245 """
245246 sorted_paths = defaultdict (list )
@@ -255,33 +256,39 @@ def _sort_paths_by_ip(paths: Sequence[str | Path], beam: int) -> dict[str, list[
255256
256257# Lumi Imbalance ---
257258
259+
258260def calculate_all_lumi_imbalances (
259- averaged_results : dict [str , dict [int , tfs .TfsDataFrame ]],
261+ averaged_results : dict [str , dict [int , tfs .TfsDataFrame ]],
260262 df_model : tfs .TfsDataFrame ,
261- output_dir : Path | str = None
263+ output_dir : Path | str = None ,
262264 ) -> None :
263- """ Calculates the luminosity imbalance between two IPs.
264-
265+ """Calculates the luminosity imbalance between two IPs.
266+
265267 Args:
266- averaged_results (dict[str, dict[int, tfs.TfsDataFrame]]): Averaged kmod results, sorted by IP.
267- df_model (tfs.TfsDataFrame): DataFrame with the model.
268+ averaged_results (dict[str, dict[int, tfs.TfsDataFrame]]): Averaged kmod results, sorted by IP.
269+ df_model (tfs.TfsDataFrame): DataFrame with the model.
268270 output_dir (Path | str, optional): Path to the output directory. Defaults to None.
269271
270272 Returns:
271273 tfs.TfsDataFrame: DataFrame with the luminosity imbalance.
272274 """
273275 sets_of_ips = list (combinations (averaged_results .keys (), 2 ))
274- for ( ipA , ipB ) in sets_of_ips :
276+ for ipA , ipB in sets_of_ips :
275277 LOG .debug (f"Calculating lumi imbalance between { ipA } and { ipB } " )
276- betastar = _get_betastar (df_model , ipA ) # does not really matter which IP, for output name only
278+ betastar = _get_betastar (
279+ df_model , ipA
280+ ) # does not really matter which IP, for output name only
277281
278282 # Calculate luminosity imbalance
279283 data = {ip .lower (): averaged_results [ip ][0 ] for ip in (ipA , ipB )}
280284 try :
281285 df = calculate_lumi_imbalance (** data , output_dir = output_dir , betastar = betastar )
282286 except KeyError as e :
283287 # Most likely because not all data available (e.g. only one beam).
284- LOG .debug (f"Could not calculate lumi imbalance between { ipA } and { ipB } . Skipping." , exc_info = e )
288+ LOG .debug (
289+ f"Could not calculate lumi imbalance between { ipA } and { ipB } . Skipping." ,
290+ exc_info = e ,
291+ )
285292 continue
286293
287294 # Print luminosity imbalance
@@ -297,4 +304,4 @@ def _get_betastar(df_model: tfs.TfsDataFrame, ip: str) -> list[float, float]:
297304# Script Mode ------------------------------------------------------------------
298305
299306if __name__ == "__main__" :
300- import_kmod_data ()
307+ import_kmod_results ()
0 commit comments