Beispiel #1
0
    def epd(self, g__, pd_flag=False, debug_flag=False):
        w = -1
        values = nx.get_node_attributes(g__, 'fv')
        simplices = [[x[0], x[1]]
                     for x in list(g__.edges)] + [[x] for x in g__.nodes()]
        up_simplices = [
            d.Simplex(s, max(values[v] for v in s)) for s in simplices
        ]
        down_simplices = [
            d.Simplex(s + [w], min(values[v] for v in s)) for s in simplices
        ]
        if pd_flag == True:
            down_simplices = []  # mask the extended persistence here

        up_simplices.sort(key=lambda s1: (s1.dimension(), s1.data))
        down_simplices.sort(reverse=True,
                            key=lambda s: (s.dimension(), s.data))
        f = d.Filtration([d.Simplex([w], -float('inf'))] + up_simplices +
                         down_simplices)
        m = d.homology_persistence(f)
        dgms = d.init_diagrams(m, f)
        if debug_flag == True:
            print(
                'Calling compute_EPD here with success. Print first dgm in dgms'
            )
            print_dgm(dgms[0])
        return dgms
Beispiel #2
0
def cycle_graph(l1=10):
    from Esme.dgms.fil import node_fil
    from Esme.dgms.stats import print_dgm

    g1 = nx.cycle_graph(500)
    g1.remove_edge(0, 1)
    g1.add_edge(180, 180 + l1 - 1)  # the cycle length is l1
    diagram = node_fil(g1, fil='hop', norm=False, one_hom=True, base=0)
    print('dgm of cycle graph')
    print_dgm(diagram)
    return diagram
Beispiel #3
0
 def post_process(self, dgm, debug_flag=False):
     if len(dgm) == 0:
         return d.Diagram([(0, 0)])
     for p in dgm:
         if p.birth == np.float('-inf'):
             p.birth = 0
         if p.death == np.float('inf'):
             p.death = 0
     if debug_flag == True:
         print('Before flip:'),
         print_dgm(dgm)
     dgm = flip_dgm(dgm)
     if debug_flag == True:
         print('After:'),
         print_dgm(dgm)
     return dgm
Beispiel #4
0
def g2dgm(i,
          g=None,
          fil='deg',
          fil_d='sub',
          norm=False,
          one_hom=False,
          debug_flag=False,
          **kwargs):
    """
    a wrapper of node_fil_ for parallel computing dgms.
    :param g:
    :param fil:
    :param fil_d: sub/super
    :param norm: False by default
    :param one_hom: False by default
    :param debug_flag: False by default
    :param kwargs:
    :return:
    """
    # assert 'gs' in globals().keys()
    # g = gs[i].copy()
    if debug_flag:
        print('processing %s-th graph where fil is %s and fil_d is %s' %
              (i, fil, fil_d))

    components = component_graphs(g)
    dgm = d.Diagram([])
    for component in components:
        if fil in ['jaccard', 'ricci', 'edge_p']:
            tmp_dgm = edge_fil_(component,
                                fil=fil,
                                fil_d=fil_d,
                                norm=norm,
                                one_hom=one_hom,
                                **kwargs)
            print_dgm(tmp_dgm)
        else:
            tmp_dgm = node_fil_(component,
                                fil=fil,
                                fil_d=fil_d,
                                norm=norm,
                                one_hom=one_hom,
                                **kwargs)
        dgm = add_dgm(dgm, tmp_dgm)
        dgm = dgm_filter(dgm)
    dgm = dgm_filter(dgm)  # handle the case when comonents is empty
    return dgm
Beispiel #5
0
            for dgm in dgms:
                dgm = permute(dgm, seed=seed, seed_flag=seed_flag)
                permuted_dgms_list.append(dgm)
            return permuted_dgms_list
        else:
            assert permute_ratio < 1
            n = len(dgms)
            permute_idx = random_.sample(range(n), int(n * permute_ratio))
            for i in range(n):
                if i in permute_idx:
                    dgm = permute(dgms[i], seed=seed, seed_flag=seed_flag)
                else:
                    dgm = dgms[i]
                permuted_dgms_list.append(dgm)
            return permuted_dgms_list

    else:
        return dgms

if __name__ == "__main__":
    dgm = d.Diagram([[1,2], [3,4], [5,6], [7,8]])
    from Esme.dgms.format import normalize_dgm
    dgm = normalize_dgm(dgm)
    x = coordinate(dgm, dim=20)
    print(x,x.shape)

    dgms = [dgm] * 3
    dgms = permute_dgms(dgms, permute_flag=True, permute_ratio=1, seed_flag=False, seed=49)
    for dgm in dgms:
        print_dgm(dgm)
        print()
Beispiel #6
0
def g2dgm(i,
          g=None,
          fil='deg',
          fil_d='sub',
          norm=False,
          one_hom=False,
          debug_flag=False,
          **kwargs):
    """
    a wrapper of node_fil_ for parallel computing dgms.
    :param g:
    :param fil:
    :param fil_d:
    :param norm: False by default
    :param one_hom: False by default
    :param debug_flag: False by default
    :param kwargs:
    :return:
    """
    # assert 'gs' in globals().keys()
    # g = gs[i].copy()

    if len(g) > 60000:
        return d.Diagram([[0, 0]])  # todo better handling

    if debug_flag:
        print('in g2dm', kwargs)
        i += kwargs.get('a', 0)
        print(
            f'processing {i}-th graph({len(g)}/{len(g.edges)}) where fil is {fil} and fil_d is {fil_d} and one_hom is {one_hom}'
        )

    if kwargs.get('write', None) == True:  # 一个后门
        fil_d_ = 'epd' if one_hom == True else fil_d
        # if check_single_dgm(graph = 'mn'+version, fil = fil, fil_d=fil_d_, norm=norm, idx=i): return

    components = component_graphs(g)
    dgm = d.Diagram([])
    for component in components:
        if fil in ['jaccard']:
            tmp_dgm = edge_fil_(component,
                                fil=fil,
                                fil_d=fil_d,
                                norm=norm,
                                one_hom=one_hom,
                                **kwargs)
            print_dgm(tmp_dgm)
        else:
            tmp_dgm = node_fil_(g=component,
                                fil=fil,
                                fil_d=fil_d,
                                norm=norm,
                                one_hom=one_hom,
                                **kwargs)

        dgm = add_dgm(dgm, tmp_dgm)
        dgm = dgm_filter(dgm)
    dgm = dgm_filter(dgm)  # handle the case when comonents is empty

    if kwargs.get('write', None) == True:  # 一个后门
        if one_hom == True: fil_d = 'epd'
        fil_save = fil + '_nbr' + str(args.nbr_size) + '_exp' + str(args.exp)
        ntda = 'ntda_' + str(kwargs.get('ntda', 'NotFound'))
        dir = os.path.join(
            '/home/cai.507/anaconda3/lib/python3.6/site-packages/save_dgms/',
            'mn' + version, ntda, fil_save, fil_d, 'norm_' + str(norm), '')
        export_dgm(dgm, dir=dir, filename=str(i) + '.csv', print_flag=True)
    return dgm
Beispiel #7
0
    ego = egograph(g,
                   radius=radius,
                   n=len(g),
                   recompute_flag=True,
                   norm_flag=True,
                   print_flag=False)
    egographs = ego.egographs(method='parallel')
    dgms = alldgms(egographs,
                   radius=radius,
                   dataset='',
                   recompute_flag=True,
                   method='serial',
                   n=n_node,
                   zigzag=zigzag)  # compute dgms in parallel
    dgms_summary(dgms)
    print_dgm(dgms[0])

    swdgms = dgms2swdgms(dgms)
    for bw in [10]:
        kwargs = {'bw': bw, 'K': 1, 'p': 1}  # TODO: K and p is dummy here
        sw_kernel, _ = sw_parallel(swdgms,
                                   swdgms,
                                   kernel_type='sw',
                                   parallel_flag=True,
                                   **kwargs)
        sw_distancem = np.log(sw_kernel) * (-2)
        # viz_matrix(sw_distancem)
        clf = classifier(np.zeros((3 * n_node, 10)),
                         labels,
                         method=None,
                         kernel=sw_kernel)