示例#1
0
import gudhi

from scripts.ssc.persistence_pairings_visualization.utils_definitions import make_plot
from src.datasets.datasets import SwissRoll

if __name__ == "__main__":
    dataset_sampler = SwissRoll()
    n_points = 2048
    seed = 13
    samples, color = dataset_sampler.sample(n_points, seed=seed)

    tc = gudhi.TangentialComplex(intrisic_dim=1, points=samples)
    tc.compute_tangential_complex()
    simplex_tree = tc.create_simplex_tree()

    print(simplex_tree.get_skeleton(1))

    skeleton_sorted = sorted(simplex_tree.get_skeleton(1), key=lambda t: t[1])

    pairings = []
    for element in skeleton_sorted:
        pair = element[0]
        if len(pair) == 2 and element[1] == 0:
            print(pair)

            pairings.append(pair)

    make_plot(samples, pairings, color, name='witness_TEST')
示例#2
0
    dataset_sampler = SwissRoll()

    N_WITNESSES = 2048
    n_samples = 128

    path_to_save = '/Users/simons/PycharmProjects/MT-VAEs-TDA/output/visualisation_nnsys/wc{}_w{}/'.format(
        n_samples, N_WITNESSES)

    N_sim = 100

    ks = [1, 2, 3, 4, 6, 8, 12, 16]
    ntot = int(len(ks) * N_sim)

    counter = 1
    for seed in list(set(np.random.randint(1, 100000, N_sim))):
        witnesses, color_ = dataset_sampler.sample(N_WITNESSES, seed=seed)

        ind = random.sample(range(N_WITNESSES), n_samples)

        landmarks, color = witnesses[ind, :], color_[ind]

        witnesses_tensor = torch.from_numpy(witnesses)
        landmarks_tensor = torch.from_numpy(landmarks)

        witness_complex = WitnessComplex(landmarks_tensor, witnesses_tensor)
        witness_complex.compute_simplicial_complex(d_max=1,
                                                   r_max=10,
                                                   create_simplex_tree=False,
                                                   create_metric=True)

        for k in ks:
示例#3
0
import time

from sklearn.manifold import SpectralEmbedding

from src.datasets.datasets import SwissRoll, Spheres
from src.utils.plots import plot_2Dscatter

if __name__ == "__main__":


    dataset = SwissRoll()

    data, color = dataset.sample(n_samples=2560)

    start = time.time()
    embedding = SpectralEmbedding(n_components=2,n_jobs=1, n_neighbors=90)

    X_transformed = embedding.fit_transform(data)
    end = time.time()
    print('It took: {}'.format(end - start))

    plot_2Dscatter(data = X_transformed, labels=color, path_to_save= None, title = None, show = True)
示例#4
0
    #
    #             count_pairings(n_samples, pairs_filtered)
    #             make_plot(landmarks, pairs_filtered, color, name=name)

    n_samples_array = [32,48,64,96,128]
    n_witnesses_array = [256,512,1024]
    seeds = [10,13,20]
    n_samples_array = [64]
    n_witnesses_array = [512]
    seeds = [27]
    for n_witnesses in n_witnesses_array:
        for seed in seeds:
            for n_samples in n_samples_array:

                name = 'witness_ssc_corrected_nl{}_nw{}_seed{}'.format(n_samples, n_witnesses, seed)
                dataset_sampler = SwissRoll()
                n_landmarks = n_samples
                seed = seed
                landmarks, color = dataset_sampler.sample(n_landmarks, seed = seed)
                witnesses, _ = dataset_sampler.sample(n_witnesses, seed=(seed+17))


                distances = wl_table(witnesses,landmarks)
                pairs = get_pairs_0(distances)

                pairs_filtered = get_persistence_pairs(pairs, n_samples)

                count_pairings(n_samples, pairs_filtered)
                make_plot(landmarks, pairs_filtered, color, name=name)

示例#5
0
    #     'WCTopoAE128': wctopoae_128,
    #
    # }
    # eval_models_dict = {
    #     'TopoAE64'  : topoae_64
    # }
    # set metrices to evaluate
    #metrics = ['K_min', 'K_max','K_avg','llrme','continuity','trustworthiness']
    #metrics = ['Trust', 'Cont', 'LGD', 'K_min', 'K_max', 'K_avg']

    metrics = ['RRE', 'Trust', 'Cont', 'IsoX', 'IsoZ', 'IsoXlist', 'IsoZlist']

    # sample data
    n_samples = 2560
    dataset = SwissRoll()
    data, labels = dataset.sample(n_samples=n_samples, seed=1)

    for model_name, path in eval_models_dict.items():
        # load WC-AE
        print('START: {}'.format(model_name))
        model_kwargs = dict(input_dim=3,
                            latent_dim=2,
                            size_hidden_layers=[32, 32])
        autoencoder = Autoencoder_MLP_topoae(**model_kwargs)
        model = WitnessComplexAutoencoder(autoencoder)
        state_dict = torch.load(os.path.join(path, 'model_state.pth'))
        model.load_state_dict(state_dict)
        model.eval()

        dataset_test = TensorDataset(torch.Tensor(data), torch.Tensor(labels))
        dataloader_eval = torch.utils.data.DataLoader(dataset_test,
import torch
from sklearn.metrics import pairwise_distances

from src.datasets.datasets import SwissRoll
from src.topology.witness_complex import WitnessComplex

if __name__ == "__main__":

    N_WITNESSES = 512
    N_LANDMARKS = 32

    landmark_dist = torch.ones(N_LANDMARKS, N_LANDMARKS) * 1000000

    dataset = SwissRoll()
    X_witnesses, _ = dataset.sample(n_samples=N_WITNESSES)

    ind_l = random.sample(range(N_WITNESSES), N_LANDMARKS)
    X_landmarks = X_witnesses[ind_l, :]

    witness_complex1 = WitnessComplex(X_landmarks, X_witnesses)

    for n_jobs in [1]:
        witness_complex1 = WitnessComplex(X_landmarks, X_witnesses)
        start = time.time()
        witness_complex1.compute_metric_optimized(n_jobs=-1)
        end = time.time()
        print('{} jobs --- Time needed: {}'.format(n_jobs, end - start))

        witness_complex2 = WitnessComplex(X_landmarks, X_witnesses)
        start = time.time()
    if len(element[0]) == 2:
        if element[1] == 0:
            pairs.append(element[0])
print('Skeleton W-LIST: {}'.format(skeleton))
print('Pairs W-LIST: {}'.format(pairs))





dataset_sampler = SwissRoll()
n_landmarks = (64+64)
n_total = 4048*2*2*2*2
seed = 9
#landmarks_, color_ = dataset_sampler.sample(n_landmarks, seed = 9)
witnesses_, color_ = dataset_sampler.sample(n_total, seed = seed)

landmark_indices = random.sample(list(range(0,n_total)), n_landmarks)

#landmarks_ = gudhi.pick_n_random_points(points=witnesses_, nb_points=n_landmarks)

landmarks, color_landmarks = witnesses_[landmark_indices,:], color_[landmark_indices]

witness_complex = gudhi.EuclideanWitnessComplex(witnesses=witnesses_, landmarks=landmarks)
simplex_tree = witness_complex.create_simplex_tree(max_alpha_square = 100,
                                                   limit_dimension=1)

skeleton = simplex_tree.get_skeleton(1)
pairs = []
for element in skeleton:
    if len(element[0]) == 2:
from src.datasets.datasets import SwissRoll
from src.evaluation.eval import Multi_Evaluation

dataset_sampler = SwissRoll()
data, label = dataset_sampler.sample(1000, seed=1)
data2, label = dataset_sampler.sample(1000, seed=2)



evaluator = Multi_Evaluation(seed=1)
ev_result = evaluator.get_multi_evals(
    data, data, label, ks=[5,10,15])


print(ev_result)
示例#9
0
                             data_obs[:, 1],
                             data_obs[:, 2],
                             c='tab:cyan')
        plt_out = ax.scatter(X_out, Y_out, Z_out, c='tab:orange')
        ax.set_title(
            str(4 * N_obs) + '-sampling of the necklace with ' + str(N_out) +
            ' outliers')
        ax.legend((plt_obs, plt_out), ('data', 'outliers'), loc='lower left')
    return data


if __name__ == "__main__":

    dataset_sampler = SwissRoll()
    name = '128_noise4_DTM'
    data, color = dataset_sampler.sample(128, noise=0.4)

    st = DTMFiltration(data, m=0.01, p=10, dimension_max=1)
    st.persistence()
    pers_pairs = st.persistence_pairs()
    print(pers_pairs)
    pairings = np.array([[pers_pairs[0][1][0], pers_pairs[0][1][1]]])
    for pair in pers_pairs[1:-1]:
        pairings = np.vstack((pairings, np.array([[pair[1][0], pair[1][1]]])))

    make_plot(data, pairings, color, name=name)

    # name = '128_noise4_reg'
    # make_data(data, color, name = name)
    #
    # path_pairings = '{}pairings_{}.npy'.format(PATH_ROOT, name)
示例#10
0
    dataset_sampler = SwissRoll()

    N_WITNESSES = 2048
    n_samples = 128

    path_to_save = '/Users/simons/PycharmProjects/MT-VAEs-TDA/output/visualisation_nnsys/wc{}_w{}/'.format(
        n_samples, N_WITNESSES)

    N_sim = 100

    ks = [1]
    ntot = int(len(ks) * N_sim)

    counter = 1
    for seed in [30]:
        witnesses, color_ = dataset_sampler.sample((N_WITNESSES - 128),
                                                   seed=seed)
        landmarks, color = dataset_sampler.sample(128, seed=30)

        witnesses_all = np.vstack((witnesses, landmarks))

        witnesses_tensor = torch.from_numpy(witnesses_all)
        landmarks_tensor = torch.from_numpy(landmarks)

        witness_complex = WitnessComplex(landmarks_tensor, witnesses_tensor)
        # witness_complex.compute_simplicial_complex(d_max=1,
        #                                            r_max=10,
        #                                            create_simplex_tree=False,
        #                                            create_metric=True)

        witness_complex.compute_metric_optimized(n_jobs=4)
        for k in ks:
    tSNE_path = '/Users/simons/MT_data/sync/euler_sync_scratch/schsimo/output/tsne_swissroll_2/SwissRoll-n_samples2560-tSNE--n_jobs1-perplexity50-seed672-017a5cba/train_latents.npz'

    eval_models_dict = {'UMAP': UMAP_path, 'tSNE': tSNE_path}
    eval_seeds = {'UMAP': UMAP_seed, 'tSNE': tSNE_see}

    metrics = ['RRE', 'Trust', 'Cont', 'IsoX', 'IsoZ', 'IsoXlist', 'IsoZlist']

    # sample data

    for model_name, path in eval_models_dict.items():
        # load WC-AE
        print('START: {}'.format(model_name))

        n_samples = 2560
        dataset = SwissRoll()
        X_eval, labels = dataset.sample(n_samples=n_samples,
                                        seed=eval_seeds[model_name])
        X_eval, X_val, y_train, y_val, = train_test_split(
            X_eval, X_eval, test_size=0.2, random_state=eval_seeds[model_name])
        Z_eval = np.load(path)['latents']

        # evaluate for multiple ks, what? -> Cont, Trust, ll-RMSE, K
        ks = [15, 30, 45]
        #ks = [int(k) for k in np.linspace(15,150,10)]

        # eval = Multi_Evaluation(model=model)
        # ev_result = eval.get_multi_evals(data=X_eval, latent=Z_eval, labels=Y_eval, ks=ks)
        ev_result = CompPerformMetrics(X_eval, Z_eval, ks=ks, dataset='norm')

        print('Done')

        # collect results and save in df.
示例#12
0
    n_w = 512

    #path = '/Users/simons/PycharmProjects/MT-VAEs-TDA/output/TDA/DoubleAnnulus'

    dataset = SwissRoll()
    small = [0.5,0.8]
    large = [1, 1.3]

    area_l = math.pi*(large[1]**2-large[0]**2)
    area_s = math.pi*(small[1]**2-small[0]**2)

    sample_ratio = area_l/area_s

    for seed in [22]:
        for n_l in [64,128]:
            X_w, w_ = dataset.sample(n_w, seed=seed)
            ind = random.sample(range(n_w), n_l)
            X_l, l_ = X_w[ind, :], w_[ind]

            # n_l = int(n_l * (1+sample_ratio))
            #
            # df_an1 = annulus(int(sample_ratio*n_w), large[0], large[1], seed=seed)
            # df_an2 = annulus(n_w, small[0], small[1], label=1, seed=(seed+12))
            # df_an = df_an1.append(df_an2, ignore_index=True)
            #
            # X_w = df_an[['x','y']].to_numpy()
            # ind = random.sample(range(n_w), n_l)
            # X_l = X_w[ind, :]

            witness_complex = WitnessComplex(X_l, X_w)
示例#13
0

if __name__ == "__main__":
    dataset_sampler = SwissRoll()
    n_samples_array = [128]
    tot_count = len(n_samples_array) * 100
    progress_count = 1
    for n_samples in n_samples_array:
        path_to_save = '/Users/simons/PycharmProjects/MT-VAEs-TDA/output/visualisation_nnsys/final_pretty/vr/'.format(
            n_samples)
        for seed in [30]:
            print('{} out of {}'.format(progress_count, tot_count))
            progress_count += 1
            name = 'vr_ns{}_seed{}'.format(n_samples, seed)

            data, color = dataset_sampler.sample(n_samples, seed=seed)
            data, pairings, color = make_data(data, color, name=name)

            # path_pairings = '{}pairings_{}.npy'.format(PATH_ROOT_SWISSROLL, name)
            # path_data = '{}data_{}.npy'.format(PATH_ROOT_SWISSROLL, name)
            # path_color = '{}color_{}.npy'.format(PATH_ROOT_SWISSROLL, name)
            # pairings, data, color = np.load(path_pairings), np.load(path_data), np.load(path_color)
            #
            make_plot(data,
                      pairings,
                      color,
                      name=name,
                      path_root=path_to_save,
                      cmap=plt.cm.viridis)

    # name = '512_1'
示例#14
0
from sklearn.manifold import TSNE

from src.datasets.datasets import SwissRoll

if __name__ == "__main__":
    dataset = SwissRoll()

    data, labels = dataset.sample(n_samples=100)

    model = TSNE()



示例#15
0
from src.datasets.datasets import SwissRoll
from src.topology.witness_complex import WitnessComplex

if __name__ == "__main__":

    unique_id = str(uuid.uuid4())[:4]
    df_timing = pd.DataFrame()

    dataset = SwissRoll()

    n_witnesses = [512, 1024, 2048]
    #n_landmarks = [int(i) for i in np.logspace(5, 9, num=5, base=2.0)]
    n_landmarks = [8, 16, 32, 64]

    data, _ = dataset.sample(n_samples=max(n_witnesses))

    df_timing_data = []

    for n_w in n_witnesses:
        ind_w = random.sample(range(max(n_witnesses)), n_w)
        X_witnesses = data[ind_w, :]

        for n_l in n_landmarks:
            ind_l = random.sample(range(max(n_witnesses)), n_l)
            X_landmarks = data[ind_l, :]
            distance_matrix_not_computed = True
            r_max = 15
            while (distance_matrix_not_computed):
                witness_complex1 = WitnessComplex(X_landmarks, X_witnesses)
                start = time.time()