def test_model_wasserstein(): random_seed = check_random_state(42) model = PersistenceVectorizer(n_components=4, random_state=random_seed).fit(base_data) model_dmat = model.pairwise_p_wasserstein_distance(base_data[:10]) random_seed = check_random_state(42) gmm = GaussianMixture(n_components=4, random_state=random_seed).fit(base_data) vec_data = [vectorize_diagram(base_data[i], gmm) for i in range(10)] raw_ground_distance = pairwise_gaussian_ground_distance( gmm.means_, gmm.covariances_, ) ground_distance = add_birth_death_line( raw_ground_distance, gmm.means_, gmm.covariances_, y_axis="lifetime", ) util_dmat = pairwise_distances( vec_data, metric=persistence_wasserstein_distance, ground_distance=ground_distance, ) assert np.allclose(model_dmat, util_dmat)
def test_vectorize_diagram(): gmm = GaussianMixture(n_components=4, random_state=42).fit(base_data) result = vectorize_diagram(np.array([[0.5, 0.2], [0.75, 0.1]]), gmm) assert np.allclose( result, np.array([6.24722853e-02, 5.50441490e-33, 0.00000000e00, 1.93752771e00]) )
def test_persistence_p_wasserstein_distance(): gmm = GaussianMixture(n_components=4, random_state=42).fit(base_data[10:90]) v1 = vectorize_diagram(base_data[:10], gmm) v2 = vectorize_diagram(base_data[-10:], gmm) raw_ground_distance = pairwise_gaussian_ground_distance( gmm.means_, gmm.covariances_, ) ground_distance = add_birth_death_line( raw_ground_distance, gmm.means_, gmm.covariances_, y_axis="lifetime", ) d = persistence_p_wasserstein_distance(v1, v2, ground_distance) assert np.isclose(d, 0.94303)
def test_pervect_transform_umap(): random_seed = check_random_state(42) gmm = GaussianMixture(n_components=4, random_state=random_seed).fit(base_data) util_result = np.array( [vectorize_diagram(diagram, gmm) for diagram in base_data]) model = PersistenceVectorizer( n_components=4, random_state=42, apply_umap=True, ).fit(base_data) model_result = model.transform(base_data) assert np.allclose(model.mixture_model_.means_, gmm.means_) assert np.allclose(model.mixture_model_.covariances_, gmm.covariances_) umap_util_result = umap.UMAP(metric=umap_metric, random_state=42).fit_transform(util_result) assert np.allclose(model_result, umap_util_result)
def test_pervect_transform_base(): random_seed = check_random_state(42) model = PersistenceVectorizer(n_components=4, random_state=random_seed).fit(base_data) model_result = model.transform(base_data) random_seed = check_random_state(42) gmm = GaussianMixture(n_components=4, random_state=random_seed).fit(base_data) util_result = np.array( [vectorize_diagram(diagram, gmm) for diagram in base_data]) assert np.allclose(model.mixture_model_.means_, gmm.means_) assert np.allclose(model.mixture_model_.covariances_, gmm.covariances_) assert np.allclose(model_result, util_result) random_seed = check_random_state(42) model_result = PersistenceVectorizer( n_components=4, random_state=random_seed).fit_transform(base_data) assert np.allclose(model_result, util_result)