def test_progress_called(self): mock_progress = Mock(side_effect=lambda it: it) calculate_sliding_score( self.metric, self.labels_true, self.labels_pred, progress=mock_progress ) mock_progress.assert_called()
def test_default_is_to_align_at_end(self): window_loc1, score1 = calculate_sliding_score( *self.args, **self.kwargs, length_mismatch_policy="align_end" ) window_loc2, score2 = calculate_sliding_score(*self.args, **self.kwargs) np.testing.assert_equal(window_loc1, window_loc2) np.testing.assert_equal(score1, score2)
def test_align_at_start(self): window_loc1, score1 = calculate_sliding_score( *self.args, **self.kwargs, length_mismatch_policy="align_start" ) n = min(self.n_samples_true, self.n_samples_pred) window_loc2, score2 = calculate_sliding_score( self.args[0], self.labels_true[:n], self.labels_pred, **self.kwargs ) np.testing.assert_equal(window_loc1, window_loc2) np.testing.assert_equal(score1, score2)
def test_align_at_end(self): window_loc1, score1 = calculate_sliding_score( *self.args, **self.kwargs, length_mismatch_policy="align_end" ) n = min(self.n_samples_true, self.n_samples_pred) shift = self.n_samples_true - n window_loc2, score2 = calculate_sliding_score( self.args[0], self.labels_true[-n:], self.labels_pred, **self.kwargs ) np.testing.assert_equal(window_loc1, window_loc2 + shift) np.testing.assert_equal(score1, score2)
def test_returns_empty_if_window_size_larger_than_labels_length(self): window_loc, score = calculate_sliding_score( lambda x, y: 1, [1, 2, 3], [0, 1, 2], window_size=5 ) self.assertEqual(len(window_loc), 0) self.assertEqual(len(score), 0)
def test_step_does_not_go_below_one(self): window_loc, _ = calculate_sliding_score( self.metric, self.labels_true, self.labels_pred, window_size=self.window_size, overlap_fraction=1, ) self.assertGreater(len(window_loc), 0) np.testing.assert_equal(np.diff(window_loc), 1)
def test_custom_step_overrides_fraction(self): step = 2 window_loc, _ = calculate_sliding_score( self.metric, self.labels_true, self.labels_pred, window_size=self.window_size, step=step, ) self.assertGreater(len(window_loc), 0) np.testing.assert_equal(np.diff(window_loc), step)
def setUp(self): self.n_samples = 200 self.window_size = 13 self.overlap_fraction = 0.3 self.rng = np.random.default_rng(0) self.metric_output = self.rng.normal(size=self.n_samples) self.metric = Mock(side_effect=self.metric_output) self.labels_true = self.rng.normal(size=self.n_samples) self.labels_pred = self.rng.normal(size=self.n_samples) self.window_loc, self.score = calculate_sliding_score( self.metric, self.labels_true, self.labels_pred, window_size=self.window_size, overlap_fraction=self.overlap_fraction, )
monitor=["r", "nsm.weights_", "xcorr.coef_"], ) t1 = time.time() print(f"Median accuracy score xcorr: {two_ar3.result_xcorr[0]:.2}. " f"(Took {t1 - t0:.2f} seconds.)") # %% two_ar3.rolling_score_biowta = [] two_ar3.rolling_score_xcorr = [] for i in tqdm(range(two_ar3.n_signals)): crt_dataset = two_ar3.dataset[i] crt_r_biowta = two_ar3.result_biowta[1].history[i].r crt_inferred_biowta = np.argmax(crt_r_biowta, axis=1) crt_loc_biowta, crt_sliding_score_biowta = calculate_sliding_score( two_ar3.metric, crt_dataset.usage_seq, crt_inferred_biowta) two_ar3.rolling_score_biowta.append( (crt_loc_biowta, crt_sliding_score_biowta)) crt_r_xcorr = two_ar3.result_xcorr[1].history[i].r crt_inferred_xcorr = np.argmax(crt_r_xcorr, axis=1) crt_loc_xcorr, crt_sliding_score_xcorr = calculate_sliding_score( two_ar3.metric, crt_dataset.usage_seq, crt_inferred_xcorr) two_ar3.rolling_score_xcorr.append( (crt_loc_xcorr, crt_sliding_score_xcorr)) # %% [markdown] # ### Plots for BioWTA results # %%
def test_raises_value_error_if_policy_is_raise(self): with self.assertRaises(ValueError): calculate_sliding_score( *self.args, **self.kwargs, length_mismatch_policy="raise", )
def test_returns_empty_for_empty_inputs(self): window_loc, score = calculate_sliding_score(lambda x, y: 1, [], []) self.assertEqual(len(window_loc), 0) self.assertEqual(len(score), 0)