@@ -85,5 +85,47 @@ def test_remove_artifacts():
8585 )
8686
8787
88+ def test_remove_artifacts_sparsity ():
89+ # non-regression test for issue #3290: "median"/"average" modes with sparsity
90+ # used to raise "shapes not aligned" because the template was kept full-channel
91+ # while the traces were sparsified before the np.dot / subtraction.
92+ rec = generate_recording (num_channels = 4 , durations = [10.0 ], seed = 0 )
93+ rec .annotate (is_filtered = True )
94+
95+ ms = 10
96+ ms_frames = int (ms * rec .get_sampling_frequency () / 1000 )
97+
98+ # two artifact labels, each removed on a different subset of channels
99+ triggers = [15000 , 30000 , 45000 , 60000 ]
100+ labels = [0 , 1 , 0 , 1 ]
101+ list_triggers = [triggers ]
102+ list_labels = [labels ]
103+ sparsity = {
104+ 0 : np .array ([True , False , True , False ]),
105+ 1 : np .array ([False , True , False , True ]),
106+ }
107+
108+ for mode in ("median" , "average" ):
109+ rec_rmart = remove_artifacts (
110+ rec ,
111+ list_triggers ,
112+ ms_before = ms ,
113+ ms_after = ms ,
114+ mode = mode ,
115+ list_labels = list_labels ,
116+ sparsity = sparsity ,
117+ )
118+ for trig , label in zip (triggers , labels ):
119+ mask = sparsity [label ]
120+ traces_clean = rec .get_traces (start_frame = trig - ms_frames , end_frame = trig + ms_frames )
121+ # get_traces must not raise (the bug reported in issue #3290)
122+ traces = rec_rmart .get_traces (start_frame = trig - ms_frames , end_frame = trig + ms_frames )
123+ # channels outside the sparsity mask are left untouched
124+ assert np .array_equal (traces [:, ~ mask ], traces_clean [:, ~ mask ])
125+ # channels inside the sparsity mask have the artifact subtracted
126+ assert not np .allclose (traces [:, mask ], traces_clean [:, mask ])
127+
128+
88129if __name__ == "__main__" :
89130 test_remove_artifacts ()
131+ test_remove_artifacts_sparsity ()
0 commit comments