def test_dropNodesByDegree(self): metaknowledge.dropNodesByDegree(self.G, minDegree=20, maxDegree=100) self.assertEqual( metaknowledge.graphStats(self.G, sentenceString=True), "The graph has 385 nodes, 5929 edges, 0 isolates, 11 self loops, a density of 0.0802083 and a transitivity of 0.954487" ) self.assertTrue(self.G.edge['Mazur P, 1953, MEM ACAD ROY BELG'] ['Livens Gh, 1948, P CAMB PHILOS SOC']['weight'] == 1)
def getThresholds(clargs, grph): thresDict = collections.OrderedDict([ ('0', "Continue"), ('1', "Drop isolates"), ('2', "Remove self loops"), ('3', "Remove edges below some weight"), ('4', "Remove edges above some weight"), ('5', "Remove nodes below some degree"), ('6', "Remove nodes above some degree"), ]) print("The network contains {0} nodes and {1} edges, of which {2} are isolated and {3} are self loops.".format(len(grph.nodes()), len(grph.edges()), len(nx.isolates(grph)), len(grph.selfloop_edges()))) thresID = int(inputMenu(thresDict, header = "What type of filtering to you want? ")) if thresID == 0: return grph elif thresID == 1: metaknowledge.dropNodesByDegree(grph, minDegree = 1) return getThresholds(clargs, grph) elif thresID == 2: metaknowledge.dropEdges(grph, dropSelfLoops = True) return getThresholds(clargs, grph) elif thresID == 3: metaknowledge.dropEdges(grph, minWeight = getNum("What is the minumum weight for an edge to be included? ")) return getThresholds(clargs, grph) elif thresID == 4: metaknowledge.dropEdges(grph, minWeight = getNum("What is the maximum weight for an edge to be included? ")) return getThresholds(clargs, grph) elif thresID == 5: metaknowledge.dropNodesByDegree(grph, minDegree = getNum("What is the minumum degree for an edge to be included? ")) return getThresholds(clargs, grph) else: metaknowledge.dropNodesByDegree(grph, minDegree = getNum("What is the maximum degree for an edge to be included? ")) return getThresholds(clargs, grph)
def getThresholds(clargs, grph): thresDict = collections.OrderedDict([ ('0', "Continue"), ('1', "Drop isolates"), ('2', "Remove self loops"), ('3', "Remove edges below some weight"), ('4', "Remove edges above some weight"), ('5', "Remove nodes below some degree"), ('6', "Remove nodes above some degree"), ]) print("The network contains {0} nodes and {1} edges, of which {2} are isolated and {3} are self loops.".format(len(list(grph.nodes())), len(list(grph.edges())), len(list(nx.isolates(grph))), len(list(grph.selfloop_edges())))) thresID = int(inputMenu(thresDict, header = "What type of filtering to you want? ")) if thresID == 0: return grph elif thresID == 1: metaknowledge.dropNodesByDegree(grph, minDegree = 1) return getThresholds(clargs, grph) elif thresID == 2: metaknowledge.dropEdges(grph, dropSelfLoops = True) return getThresholds(clargs, grph) elif thresID == 3: metaknowledge.dropEdges(grph, minWeight = getNum("What is the minumum weight for an edge to be included? ")) return getThresholds(clargs, grph) elif thresID == 4: metaknowledge.dropEdges(grph, minWeight = getNum("What is the maximum weight for an edge to be included? ")) return getThresholds(clargs, grph) elif thresID == 5: metaknowledge.dropNodesByDegree(grph, minDegree = getNum("What is the minumum degree for an edge to be included? ")) return getThresholds(clargs, grph) else: metaknowledge.dropNodesByDegree(grph, minDegree = getNum("What is the maximum degree for an edge to be included? ")) return getThresholds(clargs, grph)
def test_dropNodesByDegree(self): metaknowledge.dropNodesByDegree(self.G, minDegree = 20, maxDegree = 100) self.assertEqual(metaknowledge.graphStats(self.G), "The graph has 384 nodes, 5902 edges, 0 isolates, 11 self loops, a density of 0.08026 and a transitivity of 0.954765") self.assertTrue(self.G.edge['Mazur P, 1953, MEM ACAD ROY BELG']['Livens Gh, 1948, P CAMB PHILOS SOC']['weight'] == 1)