def _generate_graph(self): """ This function generates a graph representing the airline network. :return :The graph representing the network. """ with open(self.routes_file, encoding="utf8") as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') g = Graph() for line in csv_reader: #decode srcfrom file src_airport_cord = line[3] dst_airport_cord = line[5] if src_airport_cord in self.cordinates and dst_airport_cord in self.cordinates: d = self._haversine(self.cordinates[src_airport_cord], self.cordinates[dst_airport_cord]) #add_edge(from_node, to_node, weight, stops, airline_code) g.add_edge(line[2], line[4], d, line[7], line[0]) #decode src from file # print(self.cordinates.get(self.source.lower())) # print(self.source) if self.cordinates.get(self.source.lower(), None) == line[3]: self.source_code = line[2] self.source_airline = line[0] #decode dest from file if self.cordinates.get(self.destination.lower(), None) == line[5]: self.destination_code = line[4] return g
def algorithm_runningTime(bdlist): nValues1 = [] tValues1 = [] nValues2 = [] tValues2 = [] for bd in bdlist: g = Graph(100, 100, bd.d) # We 'Draw' a map in a coordinate system mapFlight = g.read_graph() # read map distanceMatrix = g.generateMatrix() # create distance matrix flightPlanMatrix = g.randomFlightPlan( distanceMatrix, bd.b) # create flight plan matrix # intialize map startPoint = random.randint(0, bd.d - 1) endPoint = random.randint(0, bd.d - 1) # decide start and start1 = time.time() #start time algorithm.uniform_cost_search(flightPlanMatrix, startPoint, endPoint) #call method end1 = time.time() #end time start2 = time.time() #start time algorithm.a_star_search(flightPlanMatrix, distanceMatrix, startPoint, endPoint) #call method end2 = time.time() #end time quantity = bd.d * math.log10(bd.b) nValues1.append(quantity) tValues1.append((end1 - start1) * 1000) nValues2.append(quantity) tValues2.append((end2 - start2) * 1000) return nValues1, tValues1, nValues2, tValues2
def test_has_vertex(self): g = Graph() assert g.has_vertex('A') is False g.add_vertex('B') assert g.has_vertex('B') is True g.add_edge('C', 'D') assert g.has_vertex('C') is True assert g.has_vertex('D') is True
def test_get_neighbors(self): g = Graph() g.add_edge('A', 'B') g.add_edge('C', 'D') g.add_edge('E', 'F') g.add_edge('G', 'H') self.assertCountEqual(g.get_neighbors('A'), ['B']) self.assertCountEqual(g.get_neighbors('B'), []) self.assertCountEqual(g.get_neighbors('C'), ['D']) self.assertCountEqual(g.get_neighbors('D'), []) with self.assertRaises(KeyError): g.get_neighbors('Z')
def test_one(): graph = Graph(a='bc', b='ade', c='af', d='b', e='bf', f='ce') assert graph.vertices == ('a', 'b', 'c', 'd', 'e', 'f') assert graph.edges == (('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'd'), ('b', 'e'), ('c', 'a'), ('c', 'f'), ('d', 'b'), ('e', 'b'), ('e', 'f'), ('f', 'c'), ('f', 'e')) assert graph.depth_first('a') == {'a', 'b', 'c', 'd', 'e', 'f'} assert tuple(sorted(graph.depth_first_paths( 'a', 'e'))) == (['a', 'b', 'e'], ['a', 'c', 'f', 'e']) assert graph.breadth_first('a') == {'a', 'b', 'c', 'd', 'e', 'f'} assert tuple(sorted(graph.breadth_first_paths( 'a', 'e'))) == (['a', 'b', 'e'], ['a', 'c', 'f', 'e']) assert graph.dijkstra('a', 'e') == (2, ['a', 'b', 'e']) assert graph.a_star('a', 'e') == (2, ['a', 'b', 'e']) assert graph.greedy_best_first('a', 'e') == ['a', 'b', 'e'] assert graph.warshall('a', 'e') == [2, ('a', 'b', 'e')]
def test_two(): g = ( (0, 4, 0, 0, 0, 0, 0, 8, 0), # 0 (4, 0, 8, 0, 0, 0, 0, 11, 0), # 1 (0, 8, 0, 7, 0, 4, 0, 0, 2), # 2 (0, 0, 7, 0, 9, 14, 0, 0, 0), # 3 (0, 0, 0, 9, 0, 10, 0, 0, 0), # 4 (0, 0, 4, 14, 10, 0, 2, 0, 0), # 5 (0, 0, 0, 0, 0, 2, 0, 1, 6), # 6 (8, 11, 0, 0, 0, 0, 1, 0, 7), # 7 (0, 0, 2, 0, 0, 0, 6, 7, 0) # 8 ) rows = len(g) cols = len(g[0]) assert rows == cols graph = Graph() for row in range(rows): graph[row] = set(col for col in range(cols) if g[row][col] != 0) assert len(graph.vertices) == rows assert len(graph.edges) == sum( len(list(col for col in range(cols) if g[row][col] != 0)) for row in range(rows)) def heuristic(a, b): return g[a][b] assert graph.dijkstra(0, 0, heuristic)[0] == 0 assert graph.dijkstra(0, 1, heuristic)[0] == 4 assert graph.dijkstra(0, 2, heuristic)[0] == 12 assert graph.dijkstra(0, 3, heuristic)[0] == 19 assert graph.dijkstra(0, 4, heuristic)[0] == 21 assert graph.dijkstra(0, 5, heuristic)[0] == 11 assert graph.dijkstra(0, 6, heuristic)[0] == 9 assert graph.dijkstra(0, 7, heuristic)[0] == 8 assert graph.dijkstra(0, 8, heuristic)[0] == 14
def __init__(self, graph_obj: Graph): # some_query_img_objects = (query_video_distinct_frames.get_objects(0, 2)) # img_objects_list contains 3 elements # nodes_matched = self.match_node_with_frames(some_query_img_objects, graph_obj) print("atleast started") # nodes_matched = [] # self.nodes_matched.append(graph_obj.get_node(2)) self.nodes_matched.append(graph_obj.get_node(0)) # self.find_edge_with_nodes(0) return
def test_get_vertices(self): g = Graph() g.add_vertex('A') g.add_vertex('B') g.add_vertex('C') g.add_vertex('D') self.assertCountEqual(g.get_vertices(), ['A', 'B', 'C', 'D'])
def setUp(self): self.graph = Graph() self.graph.add_vertex(1) self.graph.add_vertex(2) self.graph.add_vertex(3) self.graph.add_vertex(4) self.graph.add_vertex(5) self.graph.add_vertex(6) self.graph.add_vertex(7) self.graph.add_edge(5, 3) self.graph.add_edge(6, 3) self.graph.add_edge(7, 1) self.graph.add_edge(4, 7) self.graph.add_edge(1, 2) self.graph.add_edge(7, 6) self.graph.add_edge(2, 4) self.graph.add_edge(3, 5) self.graph.add_edge(2, 3) self.graph.add_edge(4, 6)
def read_graph_from_file(file_name): '''Read graph from a file''' file = open(file_name, 'r') file_list = file.readlines() vertices = file_list[1].rstrip().split(',') valid_types = 'gGdD' graph_type = '' directed = False weighted = False edges_list = [] if file_list[0].rstrip() in valid_types: graph_type = file_list[0].upper() else: raise ValueError('G or D is not specified') if graph_type == 'D': directed = True for item in range(2, len(file_list)): edge = file_list[item].rstrip().replace('(', '').replace(')', '').split(',') if len(edge) == 3: weighted = True edges_list.append(edge) graph = Graph(weighted, directed) for edge in edges_list: if weighted: graph.add_weighted_edge(int(edge[0]), int(edge[1]), int(edge[2])) else: graph.add_edge(int(edge[0]), int(edge[1])) return graph
def test_get_edge_list(self): g = Graph(weighted=True, directed=False) g.add_weighted_edge('A', 'B', 5) g.add_weighted_edge('C', 'D', 3) g.add_weighted_edge('E', 'F', 2) g.add_weighted_edge('G', 'H', 1) self.assertCountEqual(g.get_edge_list(), [('A', 'B', 5), ('C', 'D', 3), ('E', 'F', 2), ('G', 'H', 1)])
def erdoes_gallai(dsequence): """ Checks if the condition of the Erdoes-Gallai inequality is fullfilled """ if sum(dsequence) % 2: # sum of sequence is odd return False if Graph.is_degree_sequence(dsequence): for k in range(1,len(dsequence) + 1): left = sum(dsequence[:k]) right = k * (k-1) + sum([min(x,k) for x in dsequence[k:]]) if left > right: return False else: # sequence is increasing return False return True
def build_graph(directed): g = Graph(directed) vertices = [] for val in ['a', 'b', 'c', 'd', 'e', 'f', 'g']: vertex = Vertex(val) vertices.append(vertex) g.add_vertex(vertex) for v in range(len(vertices)): v_idx = randrange(0, len(vertices) - 1) v1 = vertices[v_idx] v_idx = randrange(0, len(vertices) - 1) v2 = vertices[v_idx] g.add_edge(v1, v2, randrange(1, 10)) print_graph(g)
def locate_new_edge_using_angle(initial_edge: Edge, graph_obj: Graph, angle_turned, allowed_angle_error: int = 20): located_edge = None for new_edge in initial_edge.angles: if angle_turned < new_edge[ 1] + allowed_angle_error and angle_turned > new_edge[ 1] - allowed_angle_error: print("new edge located is " + new_edge[0] + " as stored angle is " + str(new_edge[1]) + " and query angle is " + str(angle_turned)) located_edge = new_edge[0] locations = located_edge.split("_") located_edge_obj = graph_obj.get_edge(locations[0], locations[1]) return located_edge_obj else: print(new_edge[0] + " is not matched as stored angle is " + str(new_edge[1]) + " and query angle is " + str(angle_turned)) return "no edge found"
def match_node_with_frames(some_query_img_objects: list, graph_obj: Graph): search_list = graph_obj.Nodes node_confidence = [] # node_confidence is list of (node.identity:int , confidence:int , total_fraction_matched:float) for node in search_list: for img_obj in some_query_img_objects: node_images: vo2.DistinctFrames = node.node_images if node_images is not None: for data_obj in node_images.get_objects(): image_fraction_matched, min_good_matches = mt.SURF_returns( img_obj.get_elements(), data_obj.get_elements(), 2500, 0.7) if min_good_matches > 100 and image_fraction_matched != -1: if image_fraction_matched > 0.05 or min_good_matches > 225: print("Match found btw" + str(img_obj.get_time()) + " of query video and " + str(data_obj.get_time()) + " of node data") if len(node_confidence ) > 0 and node_confidence[-1][ 0] == node.identity: entry = node_confidence[-1] node_confidence[-1] = ( node.identity, entry[1] + 1, entry[2] + image_fraction_matched) # print(str(node.identity) + " matched by " + str(image_fraction_matched)) else: node_confidence.append( (node.identity, 1, image_fraction_matched)) node_confidence = sorted(node_confidence, key=lambda x: (x[1], x[2]), reverse=True) print(node_confidence) final_node_list = [] for entry in node_confidence: final_node_list.append(graph_obj.get_node(entry[0])) return final_node_list
if state == goalTest: return True for neighbor in state.neighbors(): update_cost(graph, neighbor, state) if neighbor.get_label() not in list( set([node.get_label() for node in frontier + explored])): neighbor.f = neighbor.cost + neighbor.goal_cost heapq.heappush(frontier, neighbor) elif find_by_label(frontier, neighbor) >= 0: update_frontier(frontier, neighbor) neighbor.f = neighbor.cost + neighbor.goal_cost return False if __name__ == "__main__": graph = Graph() graph.add_node_from([ "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O" ]) graph.add_edges_from([ ("A", "B", 2), ("A", "C", 1), ("A", "D", 3), ("B", "E", 5), ("B", "F", 4), ("C", "G", 6), ("C", "H", 3), ("D", "I", 2), ("D", "J", 4), ("F", "K", 2),
state = heapq.heappop(frontier) explored.append(state) if state == goalTest: print(df) return True for neighbor in state.neighbors(): if neighbor.get_label() not in list( set([node.get_label() for node in frontier + explored])): heapq.heappush(frontier, neighbor) elif find_by_label(array_of_node=frontier, node=neighbor) >= 0: update_frontier(frontier=frontier, new_node=neighbor) return False if __name__ == "__main__": graph = Graph() graph.add_node_from([ "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O" ]) graph.add_edges_from([ ("A", "B"), ("A", "C"), ("A", "D"), ("B", "E"), ("B", "F"), ("C", "G"), ("C", "H"), ("D", "I"), ("D", "J"), ("F", "K"),
def test_init(self): g = Graph() assert isinstance(g, Graph)
def test_add_vertex(self): g = Graph() g.add_vertex('A') g.add_vertex('B') g.add_vertex('C') g.add_vertex('D') assert g.has_vertex('A') is True assert g.has_vertex('B') is True assert g.has_vertex('C') is True assert g.has_vertex('D') is True assert g.has_vertex('E') is False assert g.has_vertex('F') is False assert g.has_vertex('G') is False assert g.has_vertex('H') is False g.add_edge('A', 'B') g.add_edge('C', 'D') self.assertCountEqual(g.get_neighbors('A'), ['B']) self.assertCountEqual(g.get_neighbors('B'), []) self.assertCountEqual(g.get_neighbors('C'), ['D']) self.assertCountEqual(g.get_neighbors('D'), []) g.add_edge('A', 'F') g.add_edge('B', 'E') self.assertCountEqual(g.get_neighbors('A'), ['B', 'F']) self.assertCountEqual(g.get_neighbors('B'), ['E'])
def test_size(self): g = Graph() assert g.size == 0 g.add_vertex('A') g.add_vertex('B') g.add_vertex('C') g.add_vertex('D') assert g.size == 4 g.add_edge('A', 'B') g.add_edge('C', 'D') g.add_edge('E', 'F') g.add_edge('G', 'H') assert g.size == 8 with self.assertRaises(KeyError): g.add_vertex('A') assert g.size == 8
i = i + 1 continue a = (len(keypoints), descriptors, vo.serialize_keypoints(keypoints), gray.shape) img_obj = vo.ImgObj(a[0], a[1], i, a[2], a[3]) self.query_objects.add_img_obj(img_obj) if write_to_disk: general.save_to_memory(img_obj, 'image' + str(i) + '.pkl', folder) cv2.imwrite(folder + '/jpg/image' + str(i) + '.jpg', gray) if (cv2.waitKey(1) & 0xFF == ord('q')) or break_video: break # Calling the localisation functions self.handle_edges() i = i + 1 cap.release() cv2.destroyAllWindows() graph1: Graph = Graph.load_graph("new_objects/graph.pkl") realTimeMatching = RealTimeMatching(graph1) url = "http://10.194.36.234:8080/shot.jpg" realTimeMatching.save_query_objects(url, livestream=True, frames_skipped=0)
from tree import TriangleNode, parseArrayIntoTree, parseEdgeArrayIntoTree, cutTreeIntoPatches from stl_reader import Reader from graph2 import Graph, TreeNode, treeLength from utilities import getMatrixArbitraryAxis from dxf_writer import DXFWriter from evolution import TreeWorld # Assumes SolidPython is in site-packages or elsewhwere in sys.path from solid import * from solid.utils import * SEGMENTS = 48 filename = "kitten-122.stl" triangles = Reader.read("stl/" + filename) # triangles = Reader.read("stl/icosahedron.stl") g = Graph(triangles) n = len(g.nodes) print "There are a total of ", n, " nodes" hFn = lambda e: g.defaultHeuristic(e) msp = g.toMSPTree(hFn) edge_rep = msp.makeEdgeRepresentation() hFn = lambda e: -g.defaultHeuristic(e) msp2 = g.toMSPTree(hFn) edge_rep2 = msp2.makeEdgeRepresentation() world = TreeWorld(g, [edge_rep, edge_rep2]) child1 = world.generateFittest() print child1 tn = parseEdgeArrayIntoTree(g.nodes, child1) print treeLength(msp, set()), "faces"
def main(): projects = Graph() projects.addVertex('a') projects.addVertex('b') projects.addVertex('c') projects.addVertex('d') projects.addVertex('e') projects.addVertex('f') projects.addEdge('f', 'a') projects.addEdge('a', 'd') projects.addEdge('f', 'b') projects.addEdge('b', 'd') projects.addEdge('d', 'c') projectList = [] topologicalSort(projects, projectList) length = len(projectList) for i in xrange(length / 2): projectList[i], projectList[length - i - 1] = projectList[length - i - 1], projectList[i] print projectList
class Test(unittest.TestCase): def setUp(self): self.graph = Graph() self.graph.add_vertex(1) self.graph.add_vertex(2) self.graph.add_vertex(3) self.graph.add_vertex(4) self.graph.add_vertex(5) self.graph.add_vertex(6) self.graph.add_vertex(7) self.graph.add_edge(5, 3) self.graph.add_edge(6, 3) self.graph.add_edge(7, 1) self.graph.add_edge(4, 7) self.graph.add_edge(1, 2) self.graph.add_edge(7, 6) self.graph.add_edge(2, 4) self.graph.add_edge(3, 5) self.graph.add_edge(2, 3) self.graph.add_edge(4, 6) def test_vertices(self): vertices = { 1: {2}, 2: {3, 4}, 3: {5}, 4: {6, 7}, 5: {3}, 6: {3}, 7: {1, 6} } self.assertDictEqual(self.graph.vertices, vertices) def test_bft(self): bft = [ "1\n2\n3\n4\n5\n6\n7\n", "1\n2\n3\n4\n5\n7\n6\n", "1\n2\n3\n4\n6\n7\n5\n", "1\n2\n3\n4\n6\n5\n7\n", "1\n2\n3\n4\n7\n6\n5\n", "1\n2\n3\n4\n7\n5\n6\n", "1\n2\n4\n3\n5\n6\n7\n", "1\n2\n4\n3\n5\n7\n6\n", "1\n2\n4\n3\n6\n7\n5\n", "1\n2\n4\n3\n6\n5\n7\n", "1\n2\n4\n3\n7\n6\n5\n", "1\n2\n4\n3\n7\n5\n6\n" ] stdout_ = sys.stdout sys.stdout = io.StringIO() self.graph.bft(1) output = sys.stdout.getvalue() self.assertIn(output, bft) sys.stdout = stdout_ # Restore stdout def test_dft(self): dft = [ "1\n2\n3\n5\n4\n6\n7\n", "1\n2\n3\n5\n4\n7\n6\n", "1\n2\n4\n7\n6\n3\n5\n", "1\n2\n4\n6\n3\n5\n7\n" ] stdout_ = sys.stdout sys.stdout = io.StringIO() self.graph.dft(1) output = sys.stdout.getvalue() self.assertIn(output, dft) sys.stdout = stdout_ # Restore stdout def test_dft_recursive(self): dft = [ "1\n2\n3\n5\n4\n6\n7\n", "1\n2\n3\n5\n4\n7\n6\n", "1\n2\n4\n7\n6\n3\n5\n", "1\n2\n4\n6\n3\n5\n7\n" ] stdout_ = sys.stdout sys.stdout = io.StringIO() self.graph.dft_recursive(1) output = sys.stdout.getvalue() self.assertIn(output, dft) sys.stdout = stdout_ # Restore stdout def test_bfs(self): bfs = [1, 2, 4, 6] self.assertListEqual(self.graph.bfs(1, 6), bfs) def test_dfs(self): dfs = [[1, 2, 4, 6], [1, 2, 4, 7, 6]] self.assertIn(self.graph.dfs(1, 6), dfs) def test_dfs_recursive(self): dfs = [[1, 2, 4, 6], [1, 2, 4, 7, 6]] self.assertIn(self.graph.dfs_recursive(1, 6), dfs)
18 : [17, 19, 9], 19 : [18, 20, 11], 20 : [13, 16, 19] } """ g = { 1: [2, 3, 4], 2: [1, 5], 3: [1, 4, 5], 4: [1, 3, 6], 5: [2, 3, 6], 6: [4, 5] } graph = Graph(g) vertices = graph.vertices() grados = [] numVertices = 0 nodosAdyacentes = [] nodosRecorridos = [] gradoAdyacentes = [] hamiltoniano = True nodoActual = None #creamos la lista de grados vacía pero #con tantos elementos como vértices #Cada entrada contendrá el grado del nodo i, siendo i la posición #relativa en la lista for item in vertices:
from tree import TriangleNode, parseArrayIntoTree, parseEdgeArrayIntoTree, cutTreeIntoPatches from stl_reader import Reader from graph2 import Graph,TreeNode,treeLength from utilities import getMatrixArbitraryAxis from dxf_writer import DXFWriter from evolution import TreeWorld # Assumes SolidPython is in site-packages or elsewhwere in sys.path from solid import * from solid.utils import * SEGMENTS = 48 filename = "kitten-122.stl" triangles = Reader.read("stl/" + filename) # triangles = Reader.read("stl/icosahedron.stl") g = Graph(triangles) n = len(g.nodes) print "There are a total of ", n, " nodes" hFn = lambda e: g.defaultHeuristic(e) msp = g.toMSPTree(hFn) edge_rep = msp.makeEdgeRepresentation() hFn = lambda e: -g.defaultHeuristic(e) msp2 = g.toMSPTree(hFn) edge_rep2 = msp2.makeEdgeRepresentation() world = TreeWorld(g, [edge_rep, edge_rep2]) child1 = world.generateFittest() print child1 tn = parseEdgeArrayIntoTree(g.nodes, child1) print treeLength(msp,set()), "faces"
} g2 = { "a": ["d", "f"], "b": ["c"], "c": ["b", "c", "d", "e"], "d": ["a", "c"], "e": ["c"], "f": ["a"] } g3 = { "a": ["d", "f"], "b": ["c", "b"], "c": ["b", "c", "d", "e"], "d": ["a", "c"], "e": ["c"], "f": ["a"] } graph = Graph(g) print(graph) print(graph.is_connected()) graph = Graph(g2) print(graph) print(graph.is_connected()) graph = Graph(g3) print(graph) print(graph.is_connected())
state = heapq.heappop(frontier) explored.append(state) if goalTest == state: return True for neighbor in state.neighbors(): update_cost(graph, current_node=neighbor, prev_node=state) if neighbor.get_label() not in list( set([e.get_label() for e in frontier + explored])): heapq.heappush(frontier, neighbor) elif find_by_label(frontier, neighbor) is not -1: update_frontier(frontier, neighbor) return False if __name__ == "__main__": graph = Graph() graph.add_node("S") graph.add_node_from(["S", "A", "B", "C", "D", "E", "F", "G", "H"]) graph.add_edges_from( [ ("S", "A", 3), ("S", "B", 6), ("S", "C", 2), ("A", "D", 3), ("B", "D", 4), ("B", "G", 9), ("B", "E", 2), ("C", "E", 1), ("D", "F", 5), ("E", "H", 5), ("F", "E", 6),
# chosse a vertex from graph as a starting point start_vertex = vertices[0] vertices_encountered.add(start_vertex) if len(vertices_encountered) != len(vertices): for vertex in gdi from graph2 import Graph g = {"a": ["d"], "b": ["c"], "c": ["b", "c", "d", "e"], "d": ["a", "c"], "e": ["c"], "f": [] } graph = Graph(g) print(graph) for node in graph.vertices(): print(graph.vertex_degree(node)) print("List of isolated vertices:") print(graph.find_isolated_vertices()) print("""A path from "a" to "e":""") print(graph.find_path("a", "e")) print("""All pathes from "a" to "e":""") print(graph.find_all_paths("a", "e")) print("The maximum degree of the graph is:")
x = [graph.vertices[vertex_id].x for vertex_id in graph.vertices] y = [graph.vertices[vertex_id].y for vertex_id in graph.vertices] graph_layout = dict(zip(node_indices, zip(x, y))) graph_renderer.layout_provider = StaticLayoutProvider(graph_layout=graph_layout) plot.renderers.append(graph_renderer) labelSource = ColumnDataSource(data=dict(x=x, y=y, names=[graph.vertices[vertex_id].value for vertex_id in graph.vertices])) labels = LabelSet(x='x', y='y', text='names', level='glyph', text_align='center', text_baseline='middle', source=labelSource, render_mode='canvas') plot.add_layout(labels) output_file('graph.html') show(plot) graph = Graph() # Instantiate your graph graph.add_vertex('0') graph.add_vertex('1') graph.add_vertex('2') graph.add_vertex('3') graph.add_edge('0', '1') graph.add_edge('0', '3') graph.add_edge('1', '2') smth = BokehGraph(graph) smth.draw()
def main(): try: with open(sys.argv[1], 'r') as tweets, open(sys.argv[2], 'w') as output: # make a pretty global dictionary to store time and hashtag info hashtag_dict = OrderedDict() result = '0.00' # iterate over the input file, one line at a time for line in tweets: tweet = line.decode('utf-8') tweet_json = json.loads(tweet) # dict object if 'created_at' in tweet_json: # needed to address keyError # get the time of the tweet created_at = datetime.strptime( tweet_json['created_at'].encode('utf-8'), "%a %b %d %H:%M:%S +0000 %Y") # get the time of the last tweet if len(hashtag_dict.keys()) == 0: time_last_tweet = created_at else: time_last_tweet = sorted(hashtag_dict.keys())[-1] # ensure to only address tweets not over 60 seconds older # than the last time_delta = created_at - time_last_tweet if time_delta.total_seconds() > - 60: # if 'entities' in tweet_json: if 'hashtags' in tweet_json['entities']: hashtags = tweet_json['entities']['hashtags'] hashtag_list = [i['text'].encode('utf-8').strip() for i in hashtags] # keep only unique tags hashtag_list = list(set(hashtag_list)) # update hashtag_dict with tweet of at least 2 tags if len(hashtag_list) > 1: hashtag_dict[created_at] = hashtag_list # for those with 0 or 1 tag, the result is the # same as in the last else: output.write(str(result) + '\n') continue else: output.write(str(result) + '\n') continue # ignore tweets over 60 seconds older than the last else: continue # remove the tweets more than 60 second older than the # current tweet sorted_key_list = sorted(hashtag_dict.keys()) for k in sorted_key_list: time_elapsed = created_at - k if time_elapsed.total_seconds() > 60: del hashtag_dict[k] else: break # finally, initialize the graph, and derive average degree graph = Graph() for t in hashtag_dict: for tag in hashtag_dict[t]: graph.add_vertex(tag) length = len(hashtag_dict[t]) for start in range(length - 1): for end in range(start+1, length): graph.add_edge( hashtag_dict[t][start], hashtag_dict[t][end]) result = average_degree_graph(graph) # ignore those lines without 'created_at' else: continue # for each valid line, report the result print result output.write(str(result) + '\n') else: print 'All done!' except IOError as err: print 'File error:', str(err)