def user_network(storage, track, session): g = Graph() users = defaultdict(g.add_vertex) g.graph_properties["track"] = g.new_graph_property("string", track) g.graph_properties["session"] = g.new_graph_property("string", session) g.edge_properties["created_at"] = g.new_edge_property("int64_t") for tweet in storage: tweeter_id = tweet["user__id_str"] origin_id = tweet["retweeted_status__user__id_str"] created_at = arrow.get(tweet["created_at"], DATE_FORMAT).timestamp if origin_id: edge = g.add_edge(users[tweeter_id], users[origin_id]) g.edge_properties["created_at"][edge] = created_at return g
def transform_nx_gt(g, rescale=False): from graph_tool import Graph from limic.util import haversine_distance h = Graph(directed=False) h.vp.id = h.new_vertex_property("int64_t") if rescale: h.vp.lat = h.new_vertex_property("int32_t") h.vp.long = h.new_vertex_property("int32_t") h.ep.weight = h.new_edge_property("int32_t") else: h.vp.lat = h.new_vertex_property("double") h.vp.long = h.new_vertex_property("double") h.ep.weight = h.new_edge_property("float") h.ep.type = h.new_edge_property("int32_t") h.gp.rescaled = h.new_graph_property("bool") h.gp.rescaled = rescale pos2vertex = {} intersection_id = 0 for n in g.nodes(): v = h.add_vertex() pos2vertex[n[1], n[2]] = v if n[0] < 0: intersection_id -= 1 h.vp.id[v] = intersection_id else: h.vp.id[v] = n[0] h.vp.lat[v] = int(n[1] * 10000000) if rescale else n[1] h.vp.long[v] = int(n[2] * 10000000) if rescale else n[2] for n, m, d in g.edges.data(data=True): w = d['weight'] air = d['type'] e = h.add_edge(pos2vertex[n[1], n[2]], pos2vertex[m[1], m[2]]) h.ep.type[e] = air if rescale and air < 0: w = haversine_distance(longx=n[2], latx=n[1], longy=m[2], laty=m[1]) h.ep.weight[e] = int(w * 1000) if rescale else w return h
e1615 = child_graph.add_edge(child_graph.vertex_index[16], child_graph.vertex_index[15]) e2015 = child_graph.add_edge(child_graph.vertex_index[20], child_graph.vertex_index[15]) e2115 = child_graph.add_edge(child_graph.vertex_index[21], child_graph.vertex_index[15]) e1716 = child_graph.add_edge(child_graph.vertex_index[17], child_graph.vertex_index[16]) e2116 = child_graph.add_edge(child_graph.vertex_index[21], child_graph.vertex_index[16]) e2216 = child_graph.add_edge(child_graph.vertex_index[22], child_graph.vertex_index[16]) e2317 = child_graph.add_edge(child_graph.vertex_index[23], child_graph.vertex_index[17]) e1918 = child_graph.add_edge(child_graph.vertex_index[19], child_graph.vertex_index[18]) e2019 = child_graph.add_edge(child_graph.vertex_index[20], child_graph.vertex_index[19]) e2120 = child_graph.add_edge(child_graph.vertex_index[21], child_graph.vertex_index[20]) e2221 = child_graph.add_edge(child_graph.vertex_index[22], child_graph.vertex_index[21]) e2322 = child_graph.add_edge(child_graph.vertex_index[23], child_graph.vertex_index[22]) ## Property definition graph_name = child_graph.new_graph_property("string") layer_capacities = child_graph.new_edge_property("int") layer_res_capacity = child_graph.new_edge_property("int") layer_flow = child_graph.new_edge_property("int") alternate_path = child_graph.new_edge_property("int") flag_path = child_graph.new_edge_property("int") ## Property Assignment child_graph.gp.layer_name = graph_name child_graph.ep.edge_capacity = layer_capacities child_graph.ep.residual_capacity = layer_res_capacity child_graph.ep.edge_flow = layer_flow child_graph.ep.shared_path = alternate_path child_graph.ep.path_flag = flag_path ## Setting the name of the graph
def cumulative_cooccurrence_graph(steps, sequences, directed=False): '''cumulative_cooccurrence_graph Creates a cumulative cooccurrence graph. Parameters ---------- steps : :obj:`iter` of :obj:`int` or :obj:`str` A series that contains sequential labels for the nested groups. sequences : :obj:`iter` of :obj:`iter` of :obj:`int` Nested iterable of integers representing vertices in the graph. Number of nested iterables should be equal to `len(steps)`. directed : :obj:`bool` Currently has no effect. In future this will determine whether to build a bi-directional cooccurrence graph. Returns ------- g : :obj:`graph_tool.Graph` A graph. Vertices are elements. Edges link terms that have cooccurred at least once in the series. o_props : :obj:`dict` Property maps with vertex occurrence values at each step. o_cumsum_props : :obj:`dict` Property maps with cumulative vertex cooccurrence values at each step. co_props : :obj:`dict` Property maps with edge cooccurrnce values at each step. co_cumsum_props : :obj:`dict` Property maps with cumulative edge cooccurrence values at each step. ''' g = Graph(directed=directed) o_total = Counter(chain(*chain(*sequences))) n_vertices = len(o_total) g.add_vertex(n_vertices) o_max = dict_to_vertex_prop(g, o_total, 'int') co_total = cooccurrence_counts(chain(*sequences)) edge_list = ((c[0], c[1], count) for c, count in co_total.items()) co_max = g.new_edge_property('int') g.add_edge_list(edge_list, eprops=[co_max]) edges = g.get_edges() edge_indices = dict(zip([(e[0], e[1]) for e in edges], edges[:, 2])) o_props = {} co_props = {} o_cumsum_props = {} co_cumsum_props = {} for i, (step, seq) in enumerate(zip(steps[:-1], sequences[:-1])): logging.info(f'Calculating cooccurrences at step {step}') o_step = Counter(chain(*seq)) o_props[step] = dict_to_vertex_prop(g, o_step, 'int') combos = (combinations(sorted(ids), 2) for ids in seq) co_step = Counter(chain(*combos)) co_props[step] = dict_to_edge_prop(g, co_step, 'int', edge_indices) o_cumsum = g.new_vertex_property('int') co_cumsum = g.new_edge_property('int') if i == 0: o_cumsum.a = o_cumsum.a + o_props[step].a co_cumsum.a = co_cumsum.a + co_props[step].a else: o_cumsum.a = o_cumsum_props[steps[i - 1]].a + o_props[step].a co_cumsum.a = co_cumsum_props[steps[i - 1]].a + co_props[step].a o_cumsum_props[step] = o_cumsum co_cumsum_props[step] = co_cumsum # fill in the last step without needing to count occurrences # or cooccurrences step_max = steps[-1] o = g.new_vertex_property('int') co = g.new_edge_property('int') o.a = o_max.a - o_cumsum.a co.a = co_max.a - co_cumsum.a o_props[step_max] = o co_props[step_max] = co o_cumsum_props[step_max] = o_max co_cumsum_props[step_max] = co_max steps_prop = g.new_graph_property('vector<int>') steps_prop.set_value(steps) g.gp['steps'] = steps_prop return g, o_props, o_cumsum_props, co_props, co_cumsum_props