From 9fb4261685d5e58ce02a8cd2ad6f3d190921cef0 Mon Sep 17 00:00:00 2001 From: JoschD <26184899+JoschD@users.noreply.github.com> Date: Wed, 4 Dec 2024 11:41:47 +0100 Subject: [PATCH] DPP parameter fixes (#476) * fixes #477 --- CHANGELOG.md | 6 ++++++ omc3/__init__.py | 2 +- omc3/hole_in_one.py | 6 ++++-- omc3/optics_measurements/beta_from_amplitude.py | 3 +-- omc3/optics_measurements/data_models.py | 2 +- omc3/optics_measurements/dispersion.py | 12 ++++++++++-- omc3/optics_measurements/measure_optics.py | 2 +- 7 files changed, 24 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 93c3ac3b..44abca80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # OMC3 Changelog +#### 2024-11-21 - v0.20.3 - _jdilly_ + +- Fixed: + - `analyse_dpp` issues in `beta_from_amplitude`, `chromatic_beating` and `dispersion`. + Skips `dispersion` calculation if `NaN`s are present. + #### 2024-11-21 - v0.20.2 - _jdilly_, _awegsche_ - Added: diff --git a/omc3/__init__.py b/omc3/__init__.py index ad3bb8ec..c41146bf 100644 --- a/omc3/__init__.py +++ b/omc3/__init__.py @@ -11,7 +11,7 @@ __title__ = "omc3" __description__ = "An accelerator physics tools package for the OMC team at CERN." __url__ = "https://github.com/pylhc/omc3" -__version__ = "0.20.2" +__version__ = "0.20.3" __author__ = "pylhc" __author_email__ = "pylhc@github.com" __license__ = "MIT" diff --git a/omc3/hole_in_one.py b/omc3/hole_in_one.py index b39dd7f8..eacb8cbb 100644 --- a/omc3/hole_in_one.py +++ b/omc3/hole_in_one.py @@ -303,7 +303,7 @@ def hole_in_one_entrypoint(opt, rest): Action: ``store_true`` - **analyse_dpp** *(float)*: Filter files to analyse by this value - (in analysis for tune, phase, rdt and crdt).. + (in analysis for tune, phase, rdt and crdt). Use `None` for no filtering. Flags: **--analyse_dpp** Default: ``0`` @@ -617,7 +617,9 @@ def optics_params(): params.add_parameter(name="chromatic_beating", action="store_true", help="Calculate chromatic beatings: W, PHI and coupling") params.add_parameter(name="analyse_dpp", type=iotools.OptionalFloat, default=OPTICS_DEFAULTS["analyse_dpp"], - help="Filter files to analyse by this value (in analysis for tune, phase, rdt and crdt).") + help="Filter files to analyse by this value (in analysis for tune, phase, rdt and crdt). " + "Use `None` for no filtering" + ) return params diff --git a/omc3/optics_measurements/beta_from_amplitude.py b/omc3/optics_measurements/beta_from_amplitude.py index f05c9e56..3ffe3265 100644 --- a/omc3/optics_measurements/beta_from_amplitude.py +++ b/omc3/optics_measurements/beta_from_amplitude.py @@ -65,8 +65,7 @@ def beta_from_amplitude(meas_input, input_files, plane, tunes): df = pd.DataFrame(meas_input.accelerator.model).loc[:, ["S", f"MU{plane}", f"BET{plane}"]] df.rename(columns={f"MU{plane}": f"MU{plane}{MDL}", f"BET{plane}": f"BET{plane}{MDL}"}, inplace=True) - dpp_value = meas_input.dpp if "dpp" in meas_input.keys() else 0 - df = pd.merge(df, input_files.joined_frame(plane, [f"AMP{plane}", f"MU{plane}"], dpp_value=dpp_value), + df = pd.merge(df, input_files.joined_frame(plane, [f"AMP{plane}", f"MU{plane}"], dpp_value=meas_input.analyse_dpp), how='inner', left_index=True, right_index=True) df['COUNT'] = len(input_files.get_columns(df, f"AMP{plane}")) diff --git a/omc3/optics_measurements/data_models.py b/omc3/optics_measurements/data_models.py index 5ef00828..b081c1a1 100644 --- a/omc3/optics_measurements/data_models.py +++ b/omc3/optics_measurements/data_models.py @@ -86,7 +86,7 @@ def dpp_frames(self, plane: str, dpp_value: float | None): dpp_dfs = [self[plane][i] for i in self.dpp_frames_indices(plane, dpp_value)] if len(dpp_dfs) == 0: - raise ValueError(f"No data found for dp/p {dpp}") + raise ValueError(f"No data found for dp/p {dpp_value} in plane {plane}") return dpp_dfs def dpp_frames_indices(self, plane: str, dpp_value: float | None): diff --git a/omc3/optics_measurements/dispersion.py b/omc3/optics_measurements/dispersion.py index f259b3ff..c754021b 100644 --- a/omc3/optics_measurements/dispersion.py +++ b/omc3/optics_measurements/dispersion.py @@ -27,6 +27,7 @@ ) from omc3.optics_measurements import dpp from omc3.utils import stats +import logging if TYPE_CHECKING: from generic_parser import DotDict @@ -34,6 +35,9 @@ from omc3.optics_measurements.data_models import InputFiles +LOGGER = logging.getLogger(__name__) + + def calculate_orbit(meas_input: DotDict, input_files: InputFiles, header, plane): """ Calculates orbit. @@ -277,8 +281,12 @@ def _get_delta_columns(df, plane): return df -def _is_single_dpp_bin(dpps: Sequence[float], tolerance: float = dpp.DPP_BIN_TOLERANCE) -> bool: +def _is_single_dpp_bin(dpps: Sequence[float]) -> bool: """ Checks if the files would be grouped into a single dpp-bin by :func:`omc3.optics_measurements.dpp._compute_ranges`. """ # alternatively: len(omc3.optics_measurements.dpp._compute_ranges(dpps, tolerance)) == 1 - return np.abs(np.max(dpps) - np.min(dpps)) <= 2 * tolerance + if any(np.isnan(dpps)): + LOGGER.warning("DPPs contain NaN values. Skipping dispersion calculation.") + return True # i.e. skip dispersion calculation + + return np.abs(np.max(dpps) - np.min(dpps)) <= 2 * dpp.DPP_BIN_TOLERANCE diff --git a/omc3/optics_measurements/measure_optics.py b/omc3/optics_measurements/measure_optics.py index caecd0f4..a1e3e33e 100644 --- a/omc3/optics_measurements/measure_optics.py +++ b/omc3/optics_measurements/measure_optics.py @@ -131,7 +131,7 @@ def chromatic_beating(input_files: InputFiles, measure_input: DotDict, tune_dict betas = [] for dpp_val in dpps: dpp_meas_input = deepcopy(measure_input) - dpp_meas_input["dpp"] = dpp_val + dpp_meas_input["analyse_dpp"] = dpp_val phase_res, out_dfs = phase.calculate(dpp_meas_input, input_files, tune_dict, plane) beta_df, _ = beta_from_phase.calculate(dpp_meas_input, tune_dict, phase_res[phase.COMPENSATED], {}, plane) betas.append(beta_df)