def test_fit_shapes(): K = 5 V = 3 T = 10 es = EventSegment(K, n_iter=2) sample_data = np.random.rand(V, T) es.fit(sample_data.T) assert es.segments_[0].shape == (T, K), "Segmentation from fit " \ "has incorrect shape" assert np.isclose(np.sum(es.segments_[0], axis=1), np.ones(T)).all(), \ "Segmentation from learn_events not correctly normalized" T2 = 15 sample_data2 = np.random.rand(V, T2) test_segments, test_ll = es.find_events(sample_data2.T) assert test_segments.shape == (T2, K), "Segmentation from find_events " \ "has incorrect shape" assert np.isclose(np.sum(test_segments, axis=1), np.ones(T2)).all(), \ "Segmentation from find_events not correctly normalized" es_invalid = EventSegment(K) with pytest.raises(ValueError, message="T < K should cause error"): es_invalid.model_prior(K - 1) with pytest.raises(ValueError, message="#Events < K should cause error"): es_invalid.set_event_patterns(np.zeros((V, K - 1)))
def test_fit_shapes(): K = 5 V = 3 T = 10 es = EventSegment(K, n_iter=2) sample_data = np.random.rand(V, T) es.fit(sample_data.T) assert es.segments_[0].shape == (T, K), "Segmentation from fit " \ "has incorrect shape" assert np.isclose(np.sum(es.segments_[0], axis=1), np.ones(T)).all(), \ "Segmentation from learn_events not correctly normalized" T2 = 15 sample_data2 = np.random.rand(V, T2) test_segments, test_ll = es.find_events(sample_data2.T) assert test_segments.shape == (T2, K), "Segmentation from find_events " \ "has incorrect shape" assert np.isclose(np.sum(test_segments, axis=1), np.ones(T2)).all(), \ "Segmentation from find_events not correctly normalized" es_invalid = EventSegment(K) with pytest.raises(ValueError, message="T < K should cause error"): es_invalid.model_prior(K-1) with pytest.raises(ValueError, message="#Events < K should cause error"): es_invalid.set_event_patterns(np.zeros((V, K-1)))
def test_prior(): K = 10 T = 100 es = EventSegment(K) mp = es.model_prior(T)[0] p_bound = np.zeros((T, K - 1)) norm = comb(T - 1, K - 1) for t in range(T - 1): for k in range(K - 1): # See supplementary material of Neuron paper # https://doi.org/10.1016/j.neuron.2017.06.041 p_bound[t + 1, k] = comb(t, k) * comb(T - t - 2, K - k - 2) / norm p_bound = np.cumsum(p_bound, axis=0) mp_gt = np.zeros((T, K)) for k in range(K): if k == 0: mp_gt[:, k] = 1 - p_bound[:, 0] elif k == K - 1: mp_gt[:, k] = p_bound[:, k - 1] else: mp_gt[:, k] = p_bound[:, k - 1] - p_bound[:, k] assert np.all(np.isclose(mp, mp_gt)),\ "Prior does not match analytic solution"
def test_prior(): K = 10 T = 100 es = EventSegment(K) mp = es.model_prior(T)[0] p_bound = np.zeros((T, K-1)) norm = comb(T-1, K-1) for t in range(T-1): for k in range(K-1): # See supplementary material of Neuron paper # https://doi.org/10.1016/j.neuron.2017.06.041 p_bound[t+1, k] = comb(t, k) * comb(T-t-2, K-k-2) / norm p_bound = np.cumsum(p_bound, axis=0) mp_gt = np.zeros((T, K)) for k in range(K): if k == 0: mp_gt[:, k] = 1 - p_bound[:, 0] elif k == K - 1: mp_gt[:, k] = p_bound[:, k-1] else: mp_gt[:, k] = p_bound[:, k-1] - p_bound[:, k] assert np.all(np.isclose(mp, mp_gt)),\ "Prior does not match analytic solution"