def testAccept(self): "Generator should accept better ProbMeasures." generator = mfng.Generator(T0=.2, steps=10, Tlimit=.04, m=3) generator.append_property(mfng.DistributionFunction("k**-2", maxdeg=10, mindeg=1)) for i in range(3): divs = known_values[i]["divs"] probs = known_values[i]["probs"] energy = known_values[i]["energy"] self.assertTrue(generator.has_accepted(mfng.ProbMeasure(divs, probs)))
def properties(self, n=100): """ Prints the statistical properties (maximal and average degree) of the results. It goes through the labels we have set with `self.set_labels`. Parameters: `n`: optional the number of the generated networks to create statistics. Example:: >>> r.set_labels("200_020") ['200_020'] >>> r.properties(n=100) ==================== label = 200_020 number of generated networks = 100 divs = [0.81104014013270886, 1.0], probs=[[ 0.27609856 0.23149258] [ 0.23149258 0.26091629]] avg max deg = 9.87+-1.0115993937, avg average deg=3.5651+-0.175231419857 """ out = Output(os.path.join(self.project_dir, "properties.txt")) assert isinstance(n, int) and n > 0 if not isinstance(self.labels, list) or not self.labels: raise NoLabelsError for label in self.labels: run = self.runs[label] doubleline = "=" * 20 out.write(doubleline) out.write("label = %s" % label) divs, probs = run["divs"], run["probs"] pm = mfng.ProbMeasure(divs, probs) K = run.get("K") or run.get( "k") # Older files have k instead of K. ipm = pm.iterate(K=K) out.write( "number of generated networks = %s\ndivs = %s,\nprobs=%s" % (n, divs, probs)) maxdegs = [] avgdegs = [] for i in range(n): nw = ipm.generate(n=run["n"]) deg = nw.degree() maxdegs.append(max(deg)) avgdegs.append(pylab.average(deg)) avgmaxdeg, avgmaxdegsigma = avg_sigma(maxdegs) avgavgdeg, avgavgdegsigma = avg_sigma(avgdegs) out.write("\navg max deg = %5s+-%5s, avg average deg=%5s+-%5s\n" % (avgmaxdeg, avgmaxdegsigma, avgavgdeg, avgavgdegsigma)) del out
def testDegdistValues(self): "The calculated degree distribution should be correct." for i in range(3): divs = known_values[i]["divs"] probs = known_values[i]["probs"] result = known_values[i]["result"] pm = mfng.ProbMeasure(divs, probs) lpm = pm.iterate(4) ddnumpy = lpm.degdist_numpy(n=2000, maxdeg=20) dditerated = pm.degdist_iterated(n=2000, maxdeg=20) for i in range(20): self.assertAlmostEqual(ddnumpy[i]/result[i], 1, places=5) for i in range(20): self.assertAlmostEqual(dditerated[i]/result[i], 1, places=5)
def testDistributionFunctionValues(self): "The energy of the DistributionFunction should be correct." for i in range(2): divs = known_values[i]["divs"] probs = known_values[i]["probs"] energy = known_values[i]["energy"] pm = mfng.ProbMeasure(divs, probs) lpm = pm.iterate(4) prop = mfng.DistributionFunction('k**-2', maxdeg=2000 - 1, mindeg=1) self.assertAlmostEqual(prop.energy(lpm, 2000)[0], energy) prop = mfng.DistributionFunctionC('k**-2', maxdeg=2000 - 1, mindeg=1) self.assertAlmostEqual(prop.energy(pm, 2000)[0], energy, places=4)
def setUp(self): self.pm = mfng.ProbMeasure(numpy.array([.3, 1]), numpy.array([[0.1, .25], [0.25, 0.4]])) self.iterated_pm = self.pm.iterate(2)