Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
JoschD committed Nov 14, 2024
1 parent 59b778f commit 72d1b70
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 6 deletions.
14 changes: 8 additions & 6 deletions omc3/scripts/bad_bpms_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
from generic_parser import EntryPointParameters, entrypoint

from omc3.utils import logging_tools
from omc3.utils.iotools import PathOrStr
from omc3.utils.iotools import PathOrStr, OptionalFloat

if TYPE_CHECKING:
from collections.abc import Sequence
Expand Down Expand Up @@ -119,7 +119,7 @@ def get_params():
)
params.add_parameter(
name="print_percentage",
type=float,
type=OptionalFloat,
help="Print out BPMs that appear in more than this percentage of measurements."
)
params.add_parameter(
Expand All @@ -132,15 +132,12 @@ def get_params():


@entrypoint(get_params(), strict=True)
def bad_bpms_summary(opt: DotDict):
def bad_bpms_summary(opt: DotDict) -> tfs.TfsDataFrame:
outfile = None
if opt.outfile is not None:
outfile = Path(opt.outfile)
outfile.parent.mkdir(parents=True, exist_ok=True)

if opt.outfile is None and opt.print_percentage is None:
raise ValueError("Either `outfile` or `print_percentage` must be specified.")

df_collection = collect_bad_bpms(Path(opt.root), opt.dates, opt.accel_glob)
if outfile is not None:
tfs.write(outfile.with_stem(f"{outfile.stem}_collected"), df_collection)
Expand All @@ -152,6 +149,8 @@ def bad_bpms_summary(opt: DotDict):
if opt.print_percentage is not None:
print_results(df_evaluated, opt.print_percentage)

return df_evaluated


# Collection of Data ---

Expand Down Expand Up @@ -383,6 +382,8 @@ def print_results(df_counted: tfs.TfsDataFrame, print_percentage: float):
df_merged['max_pct'] = df_merged[[f"{PERCENTAGE}X", f"{PERCENTAGE}Y"]].max(axis=1)
df_merged = df_merged.sort_values(by='max_pct', ascending=False)
df_merged = df_merged.loc[df_merged['max_pct'] >= print_percentage, :]

# Print Table ---
header = f"{'BPM':>20s} {'X':^18s} {'Y':^18s}\n"
msg = header + "\n".join(
f"{name:>20s} " +
Expand All @@ -398,6 +399,7 @@ def print_results(df_counted: tfs.TfsDataFrame, print_percentage: float):
)

else:
# Print a list ---
df_filtered = df_counted.loc[percentage_mask & source_mask & accel_mask, :]
msg = "\n".join(
f"{row[NAME]:>20s} {row[PLANE]}: {row[PERCENTAGE]:5.1f}% ({row[COUNT]}/{row[FILE_COUNT]})"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
* NAME FEATURE VALUE AVG SCORE
$ %s %s %le %le %le
BPM.20R5.B1 AMPX 0.2739793273915 0.1983870023371 -0.08321628370052
BPM.22R5.B1 AMPX 0.1645414911027 0.1983870023371 -0.1192688174725
BPM.34R7.B1 AMPX 0.1608806519601 0.1983870023371 -0.1121605330153
BPM.27R8.B1 AMPX 0.1164325835062 0.1983870023371 -0.08355632185117
BPMS.2L5.B1 NOISE_SCALED 0.0006165859078803 0.0009016042719894 -0.1390448828672
BPMW.5L7.B1 AMPX 0.1645414911027 0.2062029479469 -0.2122963397363
BPMSW.1L8.B1 NOISE_SCALED 0.001344342784781 0.0009016042719894 -0.1248972948542
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
* NAME FEATURE VALUE AVG SCORE
$ %s %s %le %le %le
BPM.34R2.B1 AMPY 0.1684687445507 0.2816166477169 -0.07696353786083
BPM.28R3.B1 AMPY 0.2274634866394 0.2816166477169 -0.07844693271801
BPM.15R7.B1 AMPY 0.1987839789693 0.2816166477169 -0.08603128649899
BPM.19L8.B1 NOISE_SCALED 0.000701003563438 0.0006483937620229 -0.120787110444
BPMWI.4L2.B1 NOISE_SCALED 0.001066794573165 0.000649595233316 -0.1719495412989
BPMS.2R5.B1 NOISE_SCALED 0.0007888325396217 0.000649595233316 -0.1308678268808
BPMS.2R1.B1 TUNEY 0.3219851293843 0.3219858241878 -0.13606497737
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
BPMSI.A4R6.B1 not found in model
BPMSI.B4R6.B1 not found in model
BPMSE.4L6.B1 Spiky BPM, found spike higher than 20.0
BPMSX.4L8.B1 Detected from SVD, single peak value is greater then 0.925
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
BPMSI.A4R6.B1 not found in model
BPMSI.B4R6.B1 not found in model
BPM.31L5.B1 Flat BPM, the difference between min/max is smaller than 1e-05
BPMSX.A4L6.B1 Flat BPM, the difference between min/max is smaller than 1e-05
BPM.31L5.B1 Found an exact zero
BPMSX.A4L6.B1 Found an exact zero
BPMSE.4L6.B1 Detected from SVD, single peak value is greater then 0.925
BPMWI.A5L4.B1 Detected from SVD, single peak value is greater then 0.925
41 changes: 41 additions & 0 deletions tests/unit/test_bad_bpms_summary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import pytest
import tfs

from tests.conftest import INPUTS, assert_tfsdataframe_equal
from omc3.scripts.bad_bpms_summary import NAME, SOURCE, bad_bpms_summary, IFOREST, HARPY
import logging


@pytest.mark.extended
def test_bad_bpms_summary(tmp_path, caplog):

outfile = tmp_path / "bad_bpms_summary.tfs"
with caplog.at_level(logging.INFO):
df_eval = bad_bpms_summary(
root=INPUTS,
outfile=outfile,
dates=["bad_bpms"],
accel_glob="LHCB1",
print_percentage=50,
)

# Test Data has been written
assert df_eval is not None
assert_tfsdataframe_equal(df_eval, tfs.read(outfile))

# Test some random BPMs
not_in_model = ["BPMSI.A4R6.B1", ]
for bpm in not_in_model:
assert bpm not in df_eval[NAME].tolist()

iforest_bpms = ["BPM.27R8.B1", "BPMS.2R1.B1"]
df_iforest = df_eval[df_eval[SOURCE] == IFOREST]
for bpm in iforest_bpms:
assert bpm in df_iforest[NAME].tolist()
assert bpm in caplog.text

harpy_bpms = ["BPMSE.4L6.B1", "BPM.31L5.B1"]
df_harpy = df_eval[df_eval[SOURCE] == HARPY]
for bpm in harpy_bpms:
assert bpm in df_harpy[NAME].tolist()
assert bpm in caplog.text

0 comments on commit 72d1b70

Please sign in to comment.