Skip to content

Commit 58afeac

Browse files
authored
fix K-mod import filtering (#471)
1 parent 63948bc commit 58afeac

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# OMC3 Changelog
22

3+
#### 2024-11-14 - v0.20.1 - _jdilly_
4+
5+
- Fixed:
6+
- `kmod_import` fixed issue with too harsh filtering of BPM data.
7+
38
#### 2024-11-14 - v0.20.0 - _jdilly_, _awegsche_
49

510
- Added:

omc3/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
__title__ = "omc3"
1212
__description__ = "An accelerator physics tools package for the OMC team at CERN."
1313
__url__ = "https://github.com/pylhc/omc3"
14-
__version__ = "0.20.0"
14+
__version__ = "0.20.1"
1515
__author__ = "pylhc"
1616
__author_email__ = "[email protected]"
1717
__license__ = "MIT"

omc3/scripts/kmod_import.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -362,17 +362,25 @@ def _read_kmod_results(paths: Sequence[Path | str], beam: int
362362
# sort into bpm and betastar --
363363
bpm_results = _filter_bpm_results(all_dfs, beam=beam)
364364
betastar_results = _filter_betastar_results(all_dfs)
365-
365+
LOG.debug(
366+
f"Found {len(bpm_results)} BPM results and {len(betastar_results)} betastar results for beam {beam}."
367+
)
366368
return bpm_results, betastar_results
367369

368370

371+
def _is_bpm_df(df: tfs.TfsDataFrame, beam: int) -> bool:
372+
""" Check if the given df is a BPM results file for the given beam. """
373+
# bpm files must have a beta column
374+
if f"{BETA}X" not in df.columns:
375+
return False
376+
377+
# They should also have at least one element (e.g. the BPM) matching the beam
378+
elements = df.index if NAME not in df.columns else df[NAME]
379+
return elements.str.match(fr".*\.B{beam}$", flags=re.IGNORECASE).any()
380+
381+
369382
def _filter_bpm_results(dfs: Sequence[tfs.TfsDataFrame], beam: int) -> list[tfs.TfsDataFrame]:
370-
bpm_dfs = [df for df in dfs if f"{BETA}X" in df.columns]
371-
bpm_dfs = [df for df in bpm_dfs if (
372-
NAME not in df.columns or all(df[NAME].str.match(fr"(.*\.B{beam}$|IP\d$)", flags=re.IGNORECASE))
373-
)
374-
]
375-
return bpm_dfs
383+
return [df for df in dfs if _is_bpm_df(df, beam)]
376384

377385

378386
def _filter_betastar_results(dfs: Sequence[tfs.TfsDataFrame]) -> list[tfs.TfsDataFrame]:
@@ -386,8 +394,9 @@ def _write_output(dfs: dict[str, tfs.TfsDataFrame], output_dir: Path):
386394
df = dfs.get(f"{id_}{plane}")
387395
if df is None:
388396
continue
389-
390-
tfs.write(output_dir / f"{filename}{plane.lower()}{EXT}", df, save_index=NAME)
397+
outfile = output_dir / f"{filename}{plane.lower()}{EXT}"
398+
LOG.info(f"Writing output file {outfile}")
399+
tfs.write(outfile, df, save_index=NAME)
391400

392401

393402
# Script Mode ------------------------------------------------------------------

0 commit comments

Comments
 (0)