Ejemplo n.º 1
0
def dotest_mod(g, known_mod=None):
    from pcd.old.models import Graph
    G = Graph.fromNetworkX(g)
    G.enableModularity(None)

    G.verbosity = -1
    if known_mod:
        #G.anneal(1.0, Escale=100, betafactor=1.00001, p_binary=0.05, p_collective=0)
        G.trials(1.0, 10, minimizer='anneal',
                 Escale=100, betafactor=1.001, p_binary=0.05, p_collective=0)
    else:
        G.anneal(1.0)

    cmtys = pcd.cmty.Communities.from_pcd(G)

    #print cmtys.q
    #print repr(cmtys.Q(g))
    print repr(cmtys._Q_cmty(g))
    #print repr(cmtys._Q_pcd(g))
    #print repr(G.modularity())
    print repr(cmtys._Q_pcd_energy(g))

    assert approxeq(cmtys.Q(g), cmtys._Q_cmty(g))
    assert approxeq(cmtys.Q(g), cmtys._Q_pcd(g))
    assert approxeq(cmtys.Q(g), G.modularity())
    #assert approxeq(cmtys.Q(g), cmtys._Q_pcd_energy(g))

    if known_mod:
        assert approxeq(cmtys.Q(g), known_mod, .001), (cmtys.Q(g), known_mod)
Ejemplo n.º 2
0
    def run(self):
        g = self.g

        true_q = self.true_q
        if true_q is None:
            spec = pcd.support.spectral.Flow(g, num_ev=self.q_hint+1)
            true_q = spec.q()
            self.time_ev = spec.time_ev

        for i in range(self.max_louvain_rounds):
            lv = self.parent_method(g, verbosity=-1)
            print "%s result sizes:"%self.parent_method.name(), \
                  [ r.q for r in lv.results ], ", wanted size %s"%true_q
            # lv.results should have greatest q first.
            results = [ r for r in lv.results if r.q >= true_q ]
            if len(results) > 0:
                break
        self.num_parent_method_trials = i+1
        if len(results) < 1:
            raise ValueError("All %s results return fewer than q communities."%self.parent_method.name())
        cmtys = results[-1]

        G = Graph.fromNetworkX(g)
        cmtys.load_pcd(G)
        #from fitz import interactnow
        if self.Emode == 'mod':
            G.enableModularity(mode=None)
        elif self.Emode == 'APM':
            pass
        else:        raise ValueError("self.Emode=%s not recognized"%self.Emode)

        self.fixq(G, gamma=self.gamma, q=true_q)

        self.cmtys = cmtys.from_pcd(G)
        self.results = [ self.cmtys ]
Ejemplo n.º 3
0
def dotest_mod(g, known_mod=None):
    from pcd.old.models import Graph
    G = Graph.fromNetworkX(g)
    G.enableModularity(None)

    G.verbosity = -1
    if known_mod:
        #G.anneal(1.0, Escale=100, betafactor=1.00001, p_binary=0.05, p_collective=0)
        G.trials(1.0,
                 10,
                 minimizer='anneal',
                 Escale=100,
                 betafactor=1.001,
                 p_binary=0.05,
                 p_collective=0)
    else:
        G.anneal(1.0)

    cmtys = pcd.cmty.Communities.from_pcd(G)

    #print cmtys.q
    #print repr(cmtys.Q(g))
    print repr(cmtys._Q_cmty(g))
    #print repr(cmtys._Q_pcd(g))
    #print repr(G.modularity())
    print repr(cmtys._Q_pcd_energy(g))

    assert approxeq(cmtys.Q(g), cmtys._Q_cmty(g))
    assert approxeq(cmtys.Q(g), cmtys._Q_pcd(g))
    assert approxeq(cmtys.Q(g), G.modularity())
    #assert approxeq(cmtys.Q(g), cmtys._Q_pcd_energy(g))

    if known_mod:
        assert approxeq(cmtys.Q(g), known_mod, .001), (cmtys.Q(g), known_mod)