def test_init(self): empty = {} with pytest.raises(ValueError): _ = graph.Graph(empty) d = {'1': object(), '2': object()} with pytest.raises(TypeError): _ = graph.Graph(d)
def test_graph_mutual_link(self): g = graph.Graph() m = movie.Movie('https://en.wikipedia.org/wiki/Marie_(film)') m.set_name('Marie') m.set_year(1985) m.set_gross(3712170) a1 = actor.Actor('https://en.wikipedia.org/wiki/Sissy_Spacek') a1.set_name('Sissy Spacek') a1.set_age(69) a2 = actor.Actor('https://en.wikipedia.org/wiki/Jeff_Daniels') a2.set_name('Jeff Daniels') a2.set_age(64) g.add_movie(m) g.add_actor(a1) g.add_actor(a2) g.movies['Marie'].add_actor(a1) g.movies['Marie'].add_actor(a2) g.add_edge(m, a1, 69) g.add_edge(m, a2, 64) assert g.is_connected(m, a1) and g.is_connected(a1, m) assert g.is_connected(m, a2) and g.is_connected(a2, m) JSON.store_to_Json(g, 'test2.json')
def test_store_to_Json(self): g = graph.Graph() m = movie.Movie('https://en.wikipedia.org/wiki/Marie_(film)') m.set_name('Marie (film)') m.set_year(1985) m.set_gross(3712170) a1 = actor.Actor('https://en.wikipedia.org/wiki/Sissy_Spacek') a1.set_name('Sissy Spacek') a1.set_age(69) a2 = actor.Actor('https://en.wikipedia.org/wiki/Jeff_Daniels') a2.set_name('Jeff Daniels') a2.set_age(64) g.add_movie(m) g.add_actor(a1) g.add_actor(a2) # m.add_actor(a1) # m.add_actor(a2) # a1.add_movie(m) # a2.add_movie(m) # movieList = [m] # actorList = [a1, a2] g.add_edge(m, a1) g.add_edge(m, a2) JSON.store_to_Json(g, 'test.json')
def test_query(self): g = graph.Graph() m = movie.Movie('https://en.wikipedia.org/wiki/Marie_(film)') m.set_name('Marie (film)') m.set_year(1985) m.set_gross(3712170) a1 = actor.Actor('https://en.wikipedia.org/wiki/Sissy_Spacek') a1.set_name('Sissy Spacek') a1.set_age(69) a2 = actor.Actor('https://en.wikipedia.org/wiki/Jeff_Daniels') a2.set_name('Jeff Daniels') a2.set_age(64) g.add_movie(m) g.add_actor(a1) g.add_actor(a2) g.add_edge(m, a1) g.add_edge(m, a2) assert g.find_movie_gross(m) == m.gross #(g.list_actor_movies(a1)) assert g.list_actor_movies(a1) == [m.movie_name] assert g.list_actor_movies(a2) == [m.movie_name] assert g.list_movie_actors(m) == [a1.actor_name, a2.actor_name] assert g.list_oldest_x_actors(1) == [a1] assert g.list_movies_for_a_year(1970) == [] assert g.list_movies_for_a_year(1980) == [] assert g.list_movies_for_a_year(1985) == [m] assert g.list_actors_for_a_year(1800) == [] assert g.list_actors_for_a_year(2019 - 69) == [a1]
def setUp(self): """builds a basic graph for testing""" self.node1 = node.Node("N1", "HDD") self.node2 = node.Node("N2", "Paper") self.node3 = node.Node("N3", "Disk") self.link1 = link.Link(self.node1, self.node2, "L1", "IO", 5) self.link2 = link.Link(self.node2, self.node1, "L2", "WIFI", 9) self.graph = graph.Graph()
def g(scope='module'): d = { 'A': graph._Point2D(0, 3), 'B': graph._Point2D(4, 0), 'C': graph._Point2D(0, 0) } g = graph.Graph(d) return g
def g(scope='module'): # TODO: maybe randomness comes from here? d = { 'A': graph._Point2D(0, 0), 'B': graph._Point2D(0, 3), 'C': graph._Point2D(4, 3), 'D': graph._Point2D(4, 0) } g = graph.Graph(d) return g
def scrap(): g = graph.Graph() actorList = [] movieList = [] urlQueue = deque(['/wiki/Christopher_Lee_filmography' ]) # start with this wiki page while len(movieList) < 125 or len(actorList) < 250: if not urlQueue: urlQueue.append( '/wiki/Michael_Caine_filmography') # add a new start page curr_url = urlQueue.popleft() logging.info("Current Page: " + wiki + curr_url) # check if the current url is a movie page or actor page or a filmography page if is_filmography_page(curr_url): m = get_movie_from_filmography(curr_url, urlQueue, g) # for item in m: # if item not in g.movies: # g.add_movie(item) movieList.extend(m) print("number of movies: ", len(movieList)) elif is_movie_page(curr_url): a = get_actor_from_movie(curr_url, urlQueue, g) # for item in a: # if item not in g.actors: # g.add_actor(item) actorList.extend(a) # print("number of actors: ", len(actorList)) elif is_actor_page(curr_url): get_filmography_from_actor(curr_url, urlQueue) # add links between movies and actors # for a in g.actors.values(): # a.add_to_each_other() # for m in g.movies.values(): # m.add_to_each_other() for m in g.movies.values(): for a in g.actors.values(): if a in m.actorList: if not g.is_connected(a, m): g.add_edge(a, m, m.gross) if not g.is_connected(m, a): g.add_edge(m, a, m.gross) print("number of movies: ", len(movieList)) print("number of actors: ", len(actorList)) #JSON.store_to_Json(movieList, actorList, 'data.json') JSON.store_to_Json(g, 'data.json')
def __init__(self, primitives, dump_graph): ErrorHandler.is_list(primitives) self.graph = graph.Graph() # [1] Create nodes. self.__create_nodes(primitives) # [2] Connect nodes. self.graph.make_connections() # [3] Mark inputs and outputs nodes self.graph.set_inputs_and_outputs() # [4] Calc DFS (execution order) self.graph.calc_dfs() if dump_graph is True: self.__dump_graph()
def test_graph(self): g = graph.Graph() assert isinstance(g.movies, dict) assert isinstance(g.actors, dict) m1 = movie.Movie('https://en.wikipedia.org/wiki/Marie_(film)') m1.set_name("Marie") g.add_movie(m1) assert g.movies == {m1.movie_name: m1} a1 = actor.Actor('https://en.wikipedia.org/wiki/Sissy_Spacek') a1.set_name('Sissy Spacek') g.add_actor(a1) assert g.actors == {a1.actor_name: a1} g.add_edge(m1, a1, 69) # date of birth as edge weight assert (m1.actorList == [a1.actor_name]) assert (a1.movieList == [m1.movie_name]) a2 = actor.Actor('https://en.wikipedia.org/wiki/Jeff_Daniels') a2.set_name('eff Daniels') g.add_actor(a2) g.add_edge(a2, m1, 64) assert a2.movieList == [m1.movie_name] assert m1.actorList == [a1.actor_name, a2.actor_name] assert g.is_connected(a2, m1) is True assert g.is_connected(m1, a2) is True assert g.is_connected(a1, m1) is True assert g.is_connected(m1, a1) is True assert g.get_neighbors(m1) == [a1.actor_name, a2.actor_name] assert g.get_neighbors(a1) == [m1.movie_name] assert g.get_neighbors(a2) == [m1.movie_name] a3 = actor.Actor('https://en.wikipedia.org/wiki/Morgan_Freeman') g.add_actor(a3) assert g.get_neighbors(a3) == [] g.add_edge(m1, a2, 60) # already exist this edge assert m1.actorList == g.get_neighbors(m1) assert a1.movieList == g.get_neighbors(a1) assert a2.movieList == g.get_neighbors(a2)
def test_rm_vertex__vertex_is_removed(): g = graph.Graph(vertices=[1, 2], edges={(1, 2)}) g.rm_vertex(1) assert 1 not in g.neighbors
def test_rm_edge__edge_DNE__returns_False(): g = graph.Graph(vertices=[1, 2], edges={(1, 2)}) assert g.rm_edge(3, 4) is False
def test_rm_edge__success_returns_True(): g = graph.Graph(vertices=[1, 2], edges={(1, 2)}) assert g.rm_edge(1, 2)
def test_rm_edge__neighbors_are_removed(): g = graph.Graph(vertices=[1, 2], edges={(1, 2)}) assert g.neighbors == {1: {2}, 2: {1}} g.rm_edge(1, 2) assert g.neighbors == {1: set(), 2: set()}
def test_add_vertex__success_returns_True(): g = graph.Graph() assert g.add_vertex(1)
induce = self.graphs[i].satisfyDegree[j] # Judge if it is visited or not if str(induce) in self.isVisited: continue else: self.isVisited[str(induce)] = True count = 0 for k in range(0, len(self.graphs)): for l in range(0, len(self.graphs[k].satisfyDegree)): if op.eq(induce, self.graphs[k].satisfyDegree[l]): count += 1 break if count >= self.support: self.frequentGraph.append(induce) print("Frequent Graph:") print(self.frequentGraph) if __name__ == "__main__": starttime = time.time() graphs = [] for i in range(0, 15): graph_single = graph.Graph(12, 50, 3) graphs.append(graph_single) multigraph = MultiGraph(graphs, 9) endtime = time.time() print(endtime - starttime)
def test_add_edge__vertices_exist__returns_True(): g = graph.Graph(vertices=[1, 2]) assert g.add_edge(1, 2)
def test_add_edge__vertices_exist__adds_edge(): g = graph.Graph(vertices=[1, 2]) g.add_edge(1, 2) assert g.edges == {frozenset({1, 2})}
def test_bfs__3_edges(): edges = 'ab ac bc'.split() g = graph.Graph(vertices=['a', 'b', 'c'], edges=edges) result = g.bfs('a') assert result == {'a': None, 'b': 'a', 'c': 'a'}
def test_has_edge__edge_exists(): g = graph.Graph(vertices=[1, 2], edges={(1, 2)}) assert g.has_edge(1, 2)
def test_has_edge__edge_DNE(): g = graph.Graph(vertices=[1, 2]) assert g.has_edge(2, 3) is False
def test_add_vertex__already_exists_returns_False(): g = graph.Graph(vertices=[1]) assert g.add_vertex(1) is False
def test_rm_vertex__neighbors_are_removed(): g = graph.Graph(vertices=[1, 2], edges={(1, 2)}) g.rm_vertex(1) e = frozenset([1, 2]) assert e not in g.edges
def test_add_edge__vertices_DNE__returns_False(): g = graph.Graph() assert not g.add_edge(1, 2)
def retrieve_from_Json(file): wiki = 'https://en.wikipedia.org' data = json.loads(open(file).read()) actor_dict = {} movie_dict = {} g = graph.Graph() #print(data) actor_data = data[0] movie_data = data[1] actor_list = [*actor_data] movie_list = [*movie_data] for key, value in actor_data.items(): if value['json_class'] == 'Actor': if key in actor_dict: # exist before new_actor = actor_dict[key] else: # get data related to actor actor_name = value['name'] actor_age = value['age'] actor_gross = value['total_gross'] actor_movies = value['movies'] # construct new actor class and set info actor_url = wiki + '/wiki/' + actor_name.replace(" ", "_") new_actor = actor.Actor(actor_url) new_actor.set_name(actor_name) new_actor.set_age(actor_age) new_actor.set_gross(actor_gross) g.add_actor(new_actor) # # add a new actor vertex to the graph for m in actor_movies: new_actor.movieList.append(m) actor_dict[actor_name] = new_actor # add new movie for m in actor_movies: if m not in movie_data: # not exist in the movie database continue movie_item = movie_data[m] if m in movie_dict: # exist before new_movie = movie_dict[m] else: movie_name = movie_item['name'] movie_url = movie_item['wiki_page'] movie_gross = movie_item['box_office'] movie_year = movie_item['year'] movie_actors = movie_item['actors'] # construct new movie class and set info new_movie = movie.Movie(movie_url) new_movie.set_name(movie_name) new_movie.set_year(movie_year) new_movie.set_gross(movie_gross) g.add_movie( new_movie) # add a new movie vertex to the graph # add new edges to the graph for a in movie_actors: new_movie.actorList.append(a) movie_dict[m] = new_movie g.add_edge(new_actor, new_movie, movie_gross) # for key, value in movie_data.items(): # # if value['json_class'] == 'Movie': # # get data related to movie # movie_name = value['name'] # movie_url = value['wiki_page'] # movie_gross = value['box_office'] # movie_year = value['year'] # movie_actors = value['actors'] # # # construct new movie class and set info # new_movie = movie.Movie(movie_url) # new_movie.set_name(movie_name) # new_movie.set_year(movie_year) # new_movie.set_gross(movie_gross) # g.add_movie(new_movie) # add a new movie vertex to the graph # # add new edges to the graph # for a in movie_actors: # new_movie.actorList.append(a) return g, actor_data, movie_data
def test_add_edge__edge_already_exists__returns_False(): g = graph.Graph(vertices=[1, 2], edges={(1, 2)}) assert not g.add_edge(1, 2)
print(e) g.add_nodes_from(v) g.add_edges_from(e) nx.draw_circular(g, with_labels=True, font_weight='bold') plt.plot() plt.savefig("graph" + str(index) + ".png") plt.show() def drawGraphs(self, graphs, index=0): for i in range(0, len(graphs)): graph = graphs[i] self.drawGraph(graph, i) if __name__ == "__main__": graphs = [] graph1 = graph.Graph(6, 10, 3) graphs.append(graph1) graph2 = graph.Graph(6, 10, 3) graphs.append(graph2) graph3 = graph.Graph(6, 10, 3) graphs.append(graph3) graph4 = graph.Graph(6, 10, 3) graphs.append(graph4) draw = DrawGraph() draw.drawGraphs(graphs) multigraph = multigraph.MultiGraph(graphs, 2)
def test_rm_edge__edge_is_removed(): g = graph.Graph(vertices=[1, 2], edges={(1, 2)}) g.rm_edge(1, 2) assert frozenset([1, 2]) not in g.edges
def test_init(): g = graph.Graph() assert g.edges == set() assert g.neighbors == {}
def test_add_vertex(): g = graph.Graph() g.add_vertex(1) assert 1 in g.neighbors