Beispiel #1
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 #2
0
def gs2dgms(gs,
            fil='deg',
            fil_d='sub',
            norm=False,
            one_hom=False,
            debug_flag=False,
            **kwargs):
    """
    serial computing dgms
    :param gs: a list of raw nx graphs(no function value)
    :param fil: filtration(deg, ricci)
    :param fil_d : sub or sup
    :param norm: whether normalize or not
    :param one_hom: one homology or not
    :param debug_flag: False by default
    :return: dgms: a list of dgm
    """
    dgms = []
    for i in range(len(gs)):
        if debug_flag:
            print(
                f'process {i}-th graph({len(gs[i])}/{len(nx.edges(gs[i]))}) where one_hom is {one_hom} fil is {fil} and fil_d is {fil_d}'
            )

        components = component_graphs(gs[i])  # todo chnage back to 4
        # components4 = component_graphs(gs[i], threshold=4) #todo
        # components5 = component_graphs(gs[i], threshold=5) #todo

        # print(f'threshold 4/5 has {len(components4)}/{len(components5)}')
        if len(components) == 0: return d.Diagram([[0, 0]])

        dgm = d.Diagram([])
        for component in components:
            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)
            # TODO: implement edge_fil_
        assert len(dgm) > 0
        dgms.append(dgm)

    return dgms
Beispiel #3
0
def load_clfdgm(idx=1, ntda=False):
    dgm = d.Diagram([[np.random.random(), 1]])
    for fil_d in ['sub']:  #['sub', 'sup', 'epd']:
        dir = os.path.join(DIRECT, graph, fil, fil_d, 'norm_True', '')
        if ntda:
            dir = os.path.join(DIRECT, graph, 'ntda_True', fil, fil_d,
                               'norm_True', '')
        f = dir + str(idx) + '.csv'

        try:
            tmp_dgm = load_dgm(dir, filename=f)
        except FileNotFoundError:
            print(
                f'{f} of size {all_dataset[idx].pos.shape[0]}/{all_dataset[idx].face.shape[1]} not found. Added a dummy one'
            )
            tmp_dgm = d.Diagram([[0, 0]])

        dgm = add_dgm(dgm, tmp_dgm)
    # print(f'finsih {idx}-th diagram')
    return dgm
Beispiel #4
0
def gs2dgms(gs,
            fil='deg',
            fil_d='sub',
            norm=False,
            one_hom=False,
            debug_flag=False,
            **kwargs):
    """
    serial computing dgms
    :param gs: a list of nx graphs
    :param fil: filtration(deg, ricci)
    :param fil_d : sub or sup
    :param norm: whether normalize or not
    :param one_hom: one homology or not
    :param debug_flag: False by default
    :return: dgms: a list of dgm
    """
    dgms = []
    for i in range(len(gs)):
        if debug_flag:
            print('processing %s-th graph where fil is %s and fil_d is %s' %
                  (i, fil, fil_d))
        components = component_graphs(gs[i])
        if len(components) == 0: return d.Diagram([[0, 0]])

        dgm = d.Diagram([])
        for component in components:
            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)
        assert len(dgm) > 0
        dgms.append(dgm)

    return dgms
Beispiel #5
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