Beispiel #1
0
def viz_vector():
    # https: // matplotlib.org / users / pyplot_tutorial.html
    dgm = d.Diagram([(2, 3), (3, 4)])
    from Esme.dgms.format import dgmxy
    dgmx, dgmy = dgmxy(dgm)
    dgms = [dgm] * 2

    params = {
        'bandwidth': 1.0,
        'weight': (1, 1),
        'im_range': [0, 1, 0, 1],
        'resolution': [5, 5]
    }
    image = dgms2vec(dgms, vectype='pi', **params)
    images = merge_dgms(dgms, dgms, vectype='pi', **params)
    print(np.shape(image), np.shape(images))

    plt.figure()
    plt.subplot(121)
    plt.scatter(dgmx, dgmy)
    plt.subplot(122)
    plt.plot(images.T)  # (n_image, dim)
    plt.show()
Beispiel #2
0
def main(graph, fil, norm, permute, ss, epd, n_cv, flip, feat, feat_kwargs,
         ntda):
    """
    All hyperprameter goes here.

    :param graph: graph dataset
    :param fil: filtration function
    :param norm: normalize or not
    :param permute: whether permute dgm
    :param ss: both sublevel and superlevel or not
    :param epd: include extended persistence or not
    :param n_cv: number of cross validation
    :return:
    """

    global gs
    print('feat kwargs', feat_kwargs)
    db = get_tda_db()
    params = {
        'graph': graph,
        'fil': fil,
        'norm': norm,
        'permute': permute,
        'ss': ss,
        'epd': epd,
        'n_cv': n_cv,
        'flip': flip,
        'feat': feat,
        'ntda': ntda,
        'feat_kwargs': feat_kwargs
    }
    if check_duplicate(db, params): return

    label_flag = dgms_dir_test(fil=fil, fil_d='sub', norm=norm, graph=graph)[1]
    # gs, labels = load_graphs(dataset=graph, labels_only=label_flag)  # step 1
    gs, labels = load_tugraphs(
        graph, labels_only=False
    )  # labels_only true means gs is None. Turned on for high speed

    # parallel

    # subdgms = gs2dgms(gs, n_jobs=-1, fil=fil, fil_d='sub', norm=norm, graph = graph, ntda = ntda, debug_flag=True)
    subdgms = gs2dgms_parallel(n_jobs=-1,
                               fil=fil,
                               fil_d='sub',
                               norm=norm,
                               graph=graph,
                               ntda=ntda)
    supdgms = gs2dgms_parallel(n_jobs=-1,
                               fil=fil,
                               fil_d='sup',
                               norm=norm,
                               graph=graph,
                               ntda=ntda)
    epddgms = gs2dgms_parallel(n_jobs=-1,
                               fil=fil,
                               one_hom=True,
                               norm=norm,
                               graph=graph,
                               ntda=ntda)

    dgms = combine_dgms(subdgms, supdgms, epddgms, ss=ss, epd=epd, flip=flip)
    dgms = permute_dgms(dgms, permute_flag=permute)  # old way
    dgms_summary(dgms)

    swdgms = dgms2swdgms(dgms)
    if feat == 'sw':
        print(feat_kwargs)
        k, _ = sw_parallel(swdgms,
                           swdgms,
                           parallel_flag=True,
                           kernel_type='sw',
                           **feat_kwargs)
        print(k.shape)
        cmargs = {'print_flag': 'off'}  # confusion matrix
        clf = classifier(labels,
                         labels,
                         method='svm',
                         n_cv=n_cv,
                         kernel=k,
                         **cmargs)
        clf.svm_kernel_(n_splits=10)

    elif feat == 'pi':  # vector
        params = {
            'bandwidth': 1.0,
            'weight': (1, 1),
            'im_range': [0, 1, 0, 1],
            'resolution': [5, 5]
        }
        images = merge_dgms(subdgms,
                            supdgms,
                            epddgms,
                            vectype='pi',
                            ss=ss,
                            epd=epd,
                            **params)
        clf = classifier(images, labels, method='svm', n_cv=n_cv)
        clf.svm(n_splits=10)

    elif feat == 'pss':
        k, _ = sw_parallel(swdgms,
                           swdgms,
                           parallel_flag=True,
                           kernel_type='pss',
                           **feat_kwargs)
        # print(k.shape, k, np.max(k))
        clf = classifier(labels, labels, method='svm', n_cv=n_cv, kernel=k)
        clf.svm_kernel_(n_splits=10)

    elif feat == 'wg':
        k, _ = sw_parallel(swdgms,
                           swdgms,
                           parallel_flag=True,
                           kernel_type='wg',
                           **feat_kwargs)
        print(k.shape)
        clf = classifier(labels, labels, method='svm', n_cv=n_cv, kernel=k)
        clf.svm_kernel_(n_splits=10)

    elif feat == 'pervec':
        cmargs = {'print_flag': 'on'}  # confusion matrix
        pd_vector = dgms2vec(dgms, vectype='pervec', **feat_kwargs)
        clf = classifier(pd_vector, labels, method='svm', n_cv=n_cv, **cmargs)
        clf.svm(n_splits=10)

    elif feat == 'pf':
        k, _ = sw_parallel(swdgms,
                           swdgms,
                           parallel_flag=False,
                           kernel_type='pf',
                           **feat_kwargs)
        clf = classifier(labels, labels, method='svm', n_cv=n_cv, kernel=k)
        clf.svm_kernel_(n_splits=10)
    else:
        raise Exception('No such feat %s' % feat)

    print(clf.stat)
    print_line()
    return clf.stat
Beispiel #3
0
def main(idx, n_iter, clf, test_size, vec, method, seg, permute, norm):

    cat_dict = prince_cat()
    for k, v in cat_dict.items():
        if idx >= k[0] and idx <= k[1]:
            print(f'idx {idx} is {v}')
            break

    # seg one shape
    dgms = loaddgm(str(idx), form='dionysus')
    dgms = flip_dgms(dgms)
    if permute: dgms = permute_dgms(dgms, permute_flag=True, seed_flag=True)

    # vectorize
    if vec == 'pvector':
        dgm_vector = dgms2vec(
            dgms, vectype='pvector'
        )  # print(np.shape(pd_vector), np.shape(pd_vectors))
    elif vec == 'pl':
        kwargs = {'num_landscapes': 5, 'resolution': 100}
        dgm_vector = dgms2vec(dgms, vectype='pl', **kwargs)
    elif vec == 'pervec':
        kwargs = {'dim': 300}
        dgm_vector = dgms2vec(
            dgms, vectype='pervec',
            **kwargs)  # print(np.shape(pd_vector), np.shape(pd_vectors))
        dgm_vector = normalize_(dgm_vector)
    else:
        raise Exception(f'No vec like {vec}')

    y = loady(model=idx, counter=True, seg=seg)

    X, Y = [], []
    n_face, n_node = face_num(str(idx)), node_num(str(idx))
    face_x = np.zeros((n_face, dgm_vector.shape[1]))
    face_indices = face_idx(str(idx))
    for i in range(n_face):
        idx1, idx2, idx3 = face_indices[i]
        idx1, idx2, idx3 = int(idx1), int(idx2), int(idx3)
        face_x[i, :] = dgm_vector[idx1][:] + dgm_vector[idx2, :] + dgm_vector[
            idx3, :]
    print(face_x.shape, y.shape)
    X.append(face_x)
    Y.append(y)

    X, Y = np.concatenate(X), np.concatenate(Y)
    if norm: X = normalize(X, axis=0)
    print(f'X is of shape {dgm_vector.shape} and Y is of shape {y.shape}\n')

    # classifer
    if clf == 'rf':
        clf = classifier(X, Y, method='svm', n_cv=1)
        res = clf.svm(n_splits=10)  # todo res format
    else:
        kwargs = {}
        res = eigenpro(X,
                       Y,
                       max_iter=n_iter,
                       test_size=test_size,
                       bd=1,
                       **kwargs)
    print('-' * 150)

    return res
Beispiel #4
0
            cmargs = {'print_flag': 'off'}  # confusion matrix
            clf = classifier(labels,
                             labels,
                             method='svm',
                             n_cv=1,
                             kernel=k,
                             **cmargs)
            clf.svm_kernel_(n_splits=10)
        sys.exit()

    # convert to vector
    # kwargs = {'num_landscapes': 5, 'resolution': 100, 'keep_zero': True}
    # x = dgms2vec(dgms, vectype='pl', **kwargs)
    kwargs = {'dim': 100}
    print('using pervec')
    x = dgms2vec(dgms, vectype='pervec', **kwargs)

    if args.random: x = np.random.random(x.shape)
    if args.norm: x = normalize_(x, axis=0)

    _, y = modelnet2graphs(version=graph[-2:],
                           print_flag=True,
                           labels_only=True)
    print(f'total num is {len(y)}')
    y = np.array(y)
    print(Counter(list(y)))

    # eigenpro
    y = np.array(labels)
    eigenpro(x, y, max_iter=args.n_iter, test_size=args.test_size)
Beispiel #5
0
        if args.idx >= k[0] and args.idx <= k[1]:
            print(f'idx {args.idx} is {v}')
            cat = v
            break

    # # seg one shape
    idx = args.idx
    dgms = loaddgm(str(idx), form='dionysus')
    dgms = flip_dgms(dgms)
    if args.permute:
        dgms = permute_dgms(dgms, permute_flag=True, seed_flag=True)

    # vectorize
    if args.vec == 'pvector':
        dgm_vector = dgms2vec(
            dgms, vectype='pvector'
        )  # print(np.shape(pd_vector), np.shape(pd_vectors))

    elif args.vec == 'pl':
        kwargs = {'num_landscapes': 5, 'resolution': 100}
        dgm_vector = dgms2vec(dgms, vectype='pl', **kwargs)

    elif args.vec == 'pi_':
        params = {
            'bandwidth': 1.0,
            'weight': lambda x: x[1],
            'im_range': [0, 1, 0, 1],
            'resolution': [20, 20]
        }
        dgm_vector = dgms2vec(dgms, vectype='pi_', **params)
        print(dgm_vector.shape)