def ClusteringCoefNodesDontExist_test(): G = Graph() G.add_nodes_from(Friends) G.add_edges_from(Relationships) obs = G.clustering_coef(['Lexa', 'Roy', 'Demmi']) exp = {} assert_equal(exp, obs)
def emptyNode_test(): Friends = [] G = Graph() G.add_nodes_from(Friends) obs = G.G exp = {} assert_equal(exp, obs)
def listOList_test(): G = Graph() G.add_nodes_from([[ 'A', ], ['B']]) obs = G.G.keys() exp = {'A', 'B'} assert_equal(exp, obs)
def clustCoefempty_test(): G = Graph() G.add_nodes_from(Friends) G.add_edges_from(Relationships) # give an empty list obs = G.clustering_coef(node_list=[]) exp = {} assert_equal(exp, obs)
def degreeNodeList(): G = Graph() G.add_nodes_from(Friends) G.add_edges_from(Relationships) # give and empty list obs = G.degree(node_L=[]) exp = False assert_equal(exp, obs)
def emptyEdges_test(): G = Graph() G.add_nodes_from(Friends) G.add_edges_from(Relationships) # test "edges" when given a string and not a list of strings obs = G.edges('Alex') exp = [['Alex', 'Julie'], ['Alex', 'Michael'], ['Alex', 'Ved']] assert_equal(exp, obs)
def hasNodeEmpty_test(): G = Graph() G.add_nodes_from(Friends) G.add_edges_from(Relationships) # given an empty list to "has_node" should return empty list exp = False obs = G.has_node([]) assert_equal(exp, obs)
def relabelNodesUneven_test(): G = Graph() G.add_nodes_from(Friends) G.add_edges_from(Relationships) Notsame = ['A', 'B', 'C', 'D', 'E', 'F'] obs = relabel_nodes(G, Notsame) exp = False # get standard out assert_equal(exp, obs)
def numNode_test(): # create a small graph G = Graph() G.add_nodes_from(Friends) G.add_edges_from(Relationships) exp = {'Alex', 'Julie', 'Rob', 'Ved', 'Michael', 'Mark', 'Jeff'} obs = G.nodes() assert_equal(exp, obs)
def addNoEdge_test(): G = Graph() G.add_nodes_from(Friends) G.add_edges_from(Relationships) exp = G.edges() # remove an edge but give an empty list G.remove_edge([]) obs = G.edges() assert_equal(exp, obs)
def removePartEdge_test(): G = Graph() G.add_nodes_from(Friends) G.add_edges_from(Relationships) exp = G.edges() # remove an edge but give a single part of the edge G.remove_edge(['Alex']) obs = G.edges() assert_equal(exp, obs)
def removeEdgeMorethan2_test(): G = Graph() G.add_nodes_from(Friends) G.add_edges_from(Relationships) # remove an edge but give three entries G.remove_edge(['Alex', 'Julie', 'Michael']) exp = 21 obs = len(G.edges()) assert_equal(exp, obs)
def removeNodeSet_test(): G = Graph() G.add_nodes_from(Friends) G.add_edges_from(Relationships) # try to remove an empty set G.remove_node({}) exp = {'Alex', 'Julie', 'Rob', 'Ved', 'Michael', 'Mark', 'Jeff'} obs = G.nodes() assert_equal(exp, obs)
def removeEmptylst_test(): G = Graph() G.add_nodes_from(Friends) G.add_edges_from(Relationships) # try and remove an empty list G.remove_node(node=[]) exp = {'Alex', 'Julie', 'Rob', 'Ved', 'Michael', 'Mark', 'Jeff'} obs = G.nodes() assert_equal(exp, obs)
def removeEmptyStr_test(): # try to remove a node but user gives an empty string G = Graph() G.add_nodes_from(Friends) G.add_edges_from(Relationships) G.remove_node('') exp = {'Alex', 'Julie', 'Rob', 'Ved', 'Michael', 'Mark', 'Jeff'} obs = G.nodes() assert_equal(exp, obs)
def neighborsNoNode_test(): G = Graph() G.add_nodes_from(Friends) G.add_edges_from(Relationships) # give empty list obs = G.neighbors([]) # need to figure how to capture standard out exp = False assert_equal(exp, obs)
def relabelNodesBlank_test(): G = Graph() G.add_nodes_from(Friends) G.add_edges_from(Relationships) Blank = ['', '', '', '', '', '', ''] # give a blank list, a list with stuff, but they are all the same # and are empty character strings obs = relabel_nodes(G, Blank) exp = {'': ['', '', '']} assert_equal(exp, obs)
def Graph_setup(): Friends = ['Alex', 'Julie', 'Rob', 'Ved', 'Michael', 'Mark', 'Jeff'] Relationships = [['Alex', 'Julie'], ['Julie', 'Alex'], ['Alex', 'Michael'], ['Michael', 'Alex'], ['Julie', 'Michael'], ['Michael', 'Julie'], ['Mark', 'Michael'], ['Michael', 'Mark'], ['Jeff', 'Mark'], ['Mark', 'Jeff'], ['Ved', 'Michael'], ['Michael', 'Ved'], ['Alex', 'Ved'], ['Ved', 'Alex'], ['Ved', 'Jeff'], ['Jeff', 'Ved'], ['Ved', 'Rob'], ['Rob', 'Ved'], ['Julie', 'Rob'], ['Rob', 'Julie'], ['Rob', 'Jeff'], ['Jeff', 'Rob']] G = Graph() G.add_nodes_from(Friends) G.add_edges_from(Relationships)
def subgraphNoneNode_test(): G = Graph() G.add_nodes_from(Friends) G.add_edges_from(Relationships) # A node in the subgraph list does not exist in "Nodes" sub_list = ['Alex', 'Michael', 'Julie', 'Matt'] obs = subgraph(G, sub_list).G exp = { 'Alex': ['Julie', 'Michael'], 'Julie': ['Alex', 'Michael'], 'Michael': ['Alex', 'Julie', 'Ved'] } assert_equal(exp, obs)
def configuration_model(self): """configuration_model() Build a configuration model using the degree distribution of the current graph.""" DD = self.degree_distribution() # build a second dictionary from DD where each node is a key # and the value is the node's degree distribution. keys_DD = DD.keys() cm_DD = {} num_edges = 0 # loop through all degrees and find all the nodes with that # degree for nn in keys_DD: # grab each node with a particular degree nodes = DD[nn] # put that node with its degree into a dictionary where # the node is the key, and its degree is the value # there should only be 1 value for each key for d in nodes: num_edges = num_edges + nn cm_DD[d] = [nn] # loops through all the nodes, and randomly # connect them with other nodes, creating a # new randomly generated graph. nodes = list(cm_DD.keys()) num_nodes = len(nodes) edge_listCM = [] # create a graph and add all nodes CMG = Graph() CMG.add_nodes_from(nodes) choices = list(range(0, num_nodes)) NN_remain = len(choices) while NN_remain > 0: #print(choices) # randomly choose a "originating node" pick_n1 = random.choice(choices) node1 = nodes[pick_n1] C_index1 = choices.index(pick_n1) # randomly choose another node for the # "orginating node" to connect to # ("connecting node") if C_index1 == len(choices) - 1: choices2 = choices[0:C_index1] elif C_index1 == 0: choices2 = choices[1:len(choices) + 1] else: P1 = choices[0:C_index1] P2 = choices[C_index1 + 1:len(choices)] choices2 = P1 + P2 pick_n2 = random.choice(choices2) node2 = nodes[pick_n2] # create an edge edge = [node1, node2] rev_edge = [node2, node1] # add the node to the graph EB1 = CMG.add_edge(edge) #EB2 = CMG.add_edge(rev_edge) #print(EB1, EB2) # if the edge sets did not exist then remove a degree # from the two randomly choosen nodes, remove a node from # NN_remain if the degree of a node reaches 0. If # the edge set already existed then choose another edge set if EB1: node_val1 = [cm_DD[node1][0] - 1] node_val2 = [cm_DD[node2][0] - 1] #print(node_val1, node_val2) cm_DD[node1] = node_val1 cm_DD[node2] = node_val2 if cm_DD[node1][0] == 0: #print(choices) #print("P1", pick_n1) #print(choices) choices.remove(pick_n1) if cm_DD[node2][0] == 0: #print(choices2) #print("P2", pick_n2) choices.remove(pick_n2) #print(choices) NN_remain = len(choices) #print(NN_remain) #print(NN_remain, choices) #print(cm_DD) #num_edges = num_edges/2 #for e in num_edges: #random.random() #return(cm_DD) return (CMG.G)