示例#1
0
文件: fil_.py 项目: Chen-Cai-OSU/Esme
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
示例#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
示例#3
0
文件: fil_.py 项目: Chen-Cai-OSU/Esme
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
示例#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:
    :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
示例#5
0
    # viz 3d graph
    args = parser.parse_args()
    idx = args.idx
    version = '10'
    gs = modelnet2pts2gs(version, exp_flag=True, a=1, b=20)

    sys.exit()

    train_dataset, test_dataset = load_modelnet(version, point_flag=False)
    print(len(test_dataset[idx].pos))

    data = FaceToEdge()(
        test_dataset[idx]) if version == '10' else test_dataset[idx]
    print(data)
    gs, _ = torch_geometric_2nx([data], weight_flag=args.w)
    g = component_graphs(gs[0])[0]
    from Esme.dgms.fil import nodefeat  # do to move up. otherwise will have circular import with fil.py
    print(nodefeat(g, 'fiedler')[:5])

    sys.exit()
    from Esme.viz.graph import viz_mesh_graph, network_plot_3D

    edge_index, pos = data.edge_index.numpy(), data.pos.numpy()
    g = viz_mesh_graph(
        edge_index, pos, viz_flag=False
    )  # generate_random_3Dgraph(n_nodes=n, radius=0.25, seed=1)
    components_size = [
        len(c)
        for c in sorted(nx.connected_components(g), key=len, reverse=True)
    ]
    print(components_size)