@@ -29,7 +29,7 @@ def test_filter_tune(tmp_path):
2929
3030 for plane in PLANES :
3131 assert len (filtered [plane ]) == 4
32- assert unfiltered [plane ][COL_NAME ][2 ] not in filtered [plane ][COL_NAME ]
32+ assert unfiltered [plane ][COL_NAME ][2 ] not in filtered [plane ][COL_NAME ]. to_list ()
3333
3434
3535def test_filter_tune_limit (tmp_path ):
@@ -48,6 +48,49 @@ def test_filter_tune_limit(tmp_path):
4848 assert_frame_equal (unfiltered [plane ], filtered [plane ])
4949
5050
51+ @pytest .mark .basic
52+ def test_keep_bpms (tmp_path ):
53+ """ Test that keeping BPMs works. """
54+ columns = [COL_TUNE ]
55+ plane_columns = [f"{ col } { p } " for col in columns for p in PLANES ]
56+
57+ # To be filtered BPMS are (due to the values in the example linfiles)
58+ filtered_bpms = {
59+ "X" : ["BPM.10L4.B1" , "BPM.10L2.B1" ],
60+ "Y" : ["BPM.10L1.B1" , "BPM.10L2.B1" ],
61+ }
62+
63+ # Test that all BPMs are filtered without the keep-flag --------------------
64+ linfiles = _copy_and_modify_linfiles (tmp_path , columns = columns )
65+ unfiltered = {p : tfs .read (f ) for p , f in linfiles .items ()}
66+
67+ # if limit not given, filters two elements in X
68+ clean_columns (files = linfiles .values (), columns = plane_columns )
69+ filtered = {p : tfs .read (f ) for p , f in linfiles .items ()}
70+
71+
72+ for plane in PLANES :
73+ assert len (filtered [plane ]) == len (unfiltered [plane ]) - 2
74+ for bpm in filtered_bpms [plane ]:
75+ assert bpm not in filtered [plane ][COL_NAME ].to_list ()
76+ assert bpm in unfiltered [plane ][COL_NAME ].to_list ()
77+
78+ # Now with keeping one of them ---------------------------------------------
79+ linfiles = _copy_and_modify_linfiles (tmp_path , columns = columns )
80+ unfiltered = {p : tfs .read (f ) for p , f in linfiles .items ()}
81+
82+ # if limit not given, filters two elements in X
83+ clean_columns (files = linfiles .values (), columns = plane_columns , keep = [filtered_bpms ["X" ][1 ]])
84+ filtered = {p : tfs .read (f ) for p , f in linfiles .items ()}
85+ for plane in PLANES :
86+ assert len (filtered [plane ]) == len (unfiltered [plane ]) - 1
87+ for bpm in filtered_bpms [plane ]:
88+ assert bpm in unfiltered [plane ][COL_NAME ].to_list ()
89+
90+ assert filtered_bpms [plane ][0 ] not in filtered [plane ][COL_NAME ].to_list ()
91+ assert filtered_bpms [plane ][1 ] in filtered [plane ][COL_NAME ].to_list ()
92+
93+
5194@pytest .mark .basic
5295def test_filter_tune_nattune (tmp_path ):
5396 """Tests that filtering works for two columns."""
@@ -64,6 +107,55 @@ def test_filter_tune_nattune(tmp_path):
64107 assert len (filtered [plane ]) == 2 # empirically determined
65108
66109
110+ @pytest .mark .basic
111+ def test_filter_between_limits (tmp_path ):
112+ """ Test filtering works on outlier created by modify linfiles function. """
113+ columns = [COL_TUNE ]
114+ plane_columns = [f"{ col } { p } " for col in columns for p in PLANES ]
115+
116+ # Test that no BPMs are filtered by the auto-clean (sanity check) ----------
117+ linfiles = _copy_and_modify_linfiles (tmp_path , columns = columns , index = [2 , 3 ], by = 0.1 )
118+ unfiltered = {p : tfs .read (f ) for p , f in linfiles .items ()}
119+
120+ clean_columns (files = linfiles .values (), columns = plane_columns )
121+
122+ filtered = {p : tfs .read (f ) for p , f in linfiles .items ()}
123+
124+ for plane in PLANES :
125+ assert_frame_equal (unfiltered [plane ], filtered [plane ])
126+
127+ # Test that the two BPMs are filtered by the limits-clean ------------------
128+ linfiles = _copy_and_modify_linfiles (tmp_path , columns = columns , index = [2 , 3 ], by = 0.1 )
129+ unfiltered = {p : tfs .read (f ) for p , f in linfiles .items ()}
130+
131+ # choosing values so that both planes are filtered
132+ # X tunes are 0.26 + 0.1, Y tunes are 0.32 + 0.1
133+ clean_columns (files = linfiles .values (), columns = plane_columns , limit = (0.20 , 0.35 ))
134+
135+ filtered = {p : tfs .read (f ) for p , f in linfiles .items ()}
136+
137+ for plane in PLANES :
138+ assert len (filtered [plane ]) == 3
139+ assert unfiltered [plane ][COL_NAME ][2 ] not in filtered [plane ][COL_NAME ].to_list ()
140+ assert unfiltered [plane ][COL_NAME ][3 ] not in filtered [plane ][COL_NAME ].to_list ()
141+
142+
143+ # Test that keep flag is also respected in the limits-clean ----------------
144+ linfiles = _copy_and_modify_linfiles (tmp_path , columns = columns , index = [2 , 3 ], by = 0.1 )
145+ unfiltered = {p : tfs .read (f ) for p , f in linfiles .items ()}
146+
147+ # choosing values so that both planes are filtered
148+ # X tunes are 0.26 + 0.1, Y tunes are 0.32 + 0.1
149+ clean_columns (files = linfiles .values (), columns = plane_columns , limit = (0.20 , 0.35 ), keep = [unfiltered ["X" ][COL_NAME ][2 ]])
150+
151+ filtered = {p : tfs .read (f ) for p , f in linfiles .items ()}
152+
153+ for plane in PLANES :
154+ assert len (filtered [plane ]) == 4
155+ assert unfiltered [plane ][COL_NAME ][2 ] in filtered [plane ][COL_NAME ].to_list ()
156+ assert unfiltered [plane ][COL_NAME ][3 ] not in filtered [plane ][COL_NAME ].to_list ()
157+
158+
67159@pytest .mark .basic
68160def test_backup_and_restore (tmp_path ):
69161 """Test that the backup and restore functionality works."""
@@ -106,7 +198,7 @@ def test_main(tmp_path):
106198 unfiltered = {p : tfs .read (f ) for p , f in linfiles .items ()}
107199
108200 # if limit not given, would filter two elements in X
109- main (files = list (linfiles .values ()), columns = plane_columns , limit = 0.01 , backup = True )
201+ main (files = list (linfiles .values ()), columns = plane_columns , limit = [ 0.01 ] , backup = True )
110202 _assert_nlinfiles (tmp_path , 2 )
111203
112204 filtered = {p : tfs .read (f ) for p , f in linfiles .items ()}
0 commit comments