def test_write_with_node_attributes(self): # Addresses #673. G = nx.path_graph(4) for i in range(4): G.node[i]['id'] = i G.node[i]['label'] = i G.node[i]['pid'] = i expected = """<gexf version="1.1" xmlns="http://www.gexf.net/1.1draft" xmlns:viz="http://www.gexf.net/1.1draft/viz" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance"> <graph defaultedgetype="undirected" mode="static"> <nodes> <node id="0" label="0" pid="0" /> <node id="1" label="1" pid="1" /> <node id="2" label="2" pid="2" /> <node id="3" label="3" pid="3" /> </nodes> <edges> <edge id="0" source="0" target="1" /> <edge id="1" source="1" target="2" /> <edge id="2" source="2" target="3" /> </edges> </graph> </gexf>""" obtained = '\n'.join(nx.generate_gexf(G)) assert_equal(expected, obtained)
def test_edge_id_construct(self): G = nx.Graph() G.add_edges_from([(0, 1, {"id": 0}), (1, 2, {"id": 2}), (2, 3)]) expected = f"""<gexf xmlns="http://www.gexf.net/1.2draft" xmlns:xsi\ ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.\ gexf.net/1.2draft http://www.gexf.net/1.2draft/gexf.xsd" version="1.2"> <meta lastmodifieddate="{time.strftime('%Y-%m-%d')}"> <creator>NetworkX {nx.__version__}</creator> </meta> <graph defaultedgetype="undirected" mode="static" name=""> <nodes> <node id="0" label="0" /> <node id="1" label="1" /> <node id="2" label="2" /> <node id="3" label="3" /> </nodes> <edges> <edge source="0" target="1" id="0" /> <edge source="1" target="2" id="2" /> <edge source="2" target="3" id="1" /> </edges> </graph> </gexf>""" obtained = "\n".join(nx.generate_gexf(G)) assert expected == obtained
def test_write_with_node_attributes(self): # Addresses #673. G = nx.path_graph(4) for i in range(4): G.node[i]['id'] = i G.node[i]['label'] = i G.node[i]['pid'] = i expected = """<gexf version="1.1" xmlns="http://www.gexf.net/1.1draft" xmlns:viz="http://www.gexf.net/1.1draft/viz" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance"> <graph defaultedgetype="undirected" mode="static" name="path_graph(4)"> <meta> <creator>NetworkX {}</creator> <lastmodified>{}</lastmodified> </meta> <nodes> <node id="0" label="0" pid="0" /> <node id="1" label="1" pid="1" /> <node id="2" label="2" pid="2" /> <node id="3" label="3" pid="3" /> </nodes> <edges> <edge id="0" source="0" target="1" /> <edge id="1" source="1" target="2" /> <edge id="2" source="2" target="3" /> </edges> </graph> </gexf>""".format(nx.__version__, time.strftime('%d/%m/%Y')) obtained = '\n'.join(nx.generate_gexf(G)) assert_equal(expected, obtained)
def test_write_with_node_attributes(self): # Addresses #673. G = nx.OrderedGraph() G.add_edges_from([(0, 1), (1, 2), (2, 3)]) for i in range(4): G.nodes[i]["id"] = i G.nodes[i]["label"] = i G.nodes[i]["pid"] = i G.nodes[i]["start"] = i G.nodes[i]["end"] = i + 1 expected = f"""<gexf xmlns="http://www.gexf.net/1.2draft" xmlns:xsi\ ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=\ "http://www.gexf.net/1.2draft http://www.gexf.net/1.2draft/\ gexf.xsd" version="1.2"> <meta lastmodifieddate="{time.strftime('%Y-%m-%d')}"> <creator>NetworkX {nx.__version__}</creator> </meta> <graph defaultedgetype="undirected" mode="dynamic" name="" timeformat="long"> <nodes> <node id="0" label="0" pid="0" start="0" end="1" /> <node id="1" label="1" pid="1" start="1" end="2" /> <node id="2" label="2" pid="2" start="2" end="3" /> <node id="3" label="3" pid="3" start="3" end="4" /> </nodes> <edges> <edge source="0" target="1" id="0" /> <edge source="1" target="2" id="1" /> <edge source="2" target="3" id="2" /> </edges> </graph> </gexf>""" obtained = "\n".join(nx.generate_gexf(G)) assert expected == obtained
def test_write_with_node_attributes(self): # Addresses #673. G = nx.OrderedGraph() G.add_edges_from([(0, 1), (1, 2), (2, 3)]) for i in range(4): G.nodes[i]['id'] = i G.nodes[i]['label'] = i G.nodes[i]['pid'] = i G.nodes[i]['start'] = i G.nodes[i]['end'] = i + 1 expected = """<gexf version="1.2" xmlns="http://www.gexf.net/1.2draft" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance"> <graph defaultedgetype="undirected" mode="dynamic" name="" timeformat="long"> <meta> <creator>NetworkX {}</creator> <lastmodified>{}</lastmodified> </meta> <nodes> <node end="1" id="0" label="0" pid="0" start="0" /> <node end="2" id="1" label="1" pid="1" start="1" /> <node end="3" id="2" label="2" pid="2" start="2" /> <node end="4" id="3" label="3" pid="3" start="3" /> </nodes> <edges> <edge id="0" source="0" target="1" /> <edge id="1" source="1" target="2" /> <edge id="2" source="2" target="3" /> </edges> </graph> </gexf>""".format(nx.__version__, time.strftime('%d/%m/%Y')) obtained = '\n'.join(nx.generate_gexf(G)) assert_equal(expected, obtained)
def generate_gexf(df_annotation, fileLocationStr): if len(df_annotation) > 0: df_annotation_dup = df_annotation.copy() df_annotation_dup['workflowName'] = df_annotation_dup[ 'workflowName'].str.replace(fileLocationStr, "") df_annotation_dup['invokedBy'] = df_annotation_dup[ 'invokedBy'].str.replace(fileLocationStr, "") df_invokeWf = df_annotation_dup.loc[:, ['workflowName', 'invokedBy' ]].drop_duplicates() #combine source and target with no dupes df_node_list = list( pd.concat([ df_invokeWf.loc[:, 'invokedBy'], df_invokeWf.loc[:, 'workflowName'] ], ignore_index=True).drop_duplicates()) df_node_list_wl = [(node, { 'label': node.split("/")[-1].replace(".xaml", "") }) for node in df_node_list] # translate workflow calls to a path graph G = nx.DiGraph() G.add_nodes_from(df_node_list_wl) for index, row in df_invokeWf.iterrows(): G.add_edge(row['invokedBy'], row['workflowName']) #ajax request str_gexf = '<?xml version="1.0" encoding="UTF-8"?>\n' + "".join( nx.generate_gexf( G, encoding='utf-8', prettyprint=True, version='1.2draft')) return str_gexf else: return None
def test_write_with_node_attributes(self): # Addresses #673. G = nx.path_graph(4) for i in range(4): G.node[i]['id'] = i G.node[i]['label'] = i G.node[i]['pid'] = i expected = """<gexf version="1.1" xmlns="http://www.gexf.net/1.1draft" xmlns:viz="http://www.gexf.net/1.1draft/viz" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance"> <graph defaultedgetype="undirected" mode="static"> <nodes> <node id="0" label="0" pid="0" /> <node id="1" label="1" pid="1" /> <node id="2" label="2" pid="2" /> <node id="3" label="3" pid="3" /> </nodes> <edges> <edge id="0" source="0" target="1" /> <edge id="1" source="1" target="2" /> <edge id="2" source="2" target="3" /> </edges> </graph> </gexf>""" obtained = '\n'.join(nx.generate_gexf(G)) assert_equal( expected, obtained )
def graph(nodes): """ Dessine un graphe et l'enregistre dans un fichier. Enregistre aussi un fichier gexf pour un usage dans Gephi. """ G = nx.DiGraph() G.add_weighted_edges_from(nodes) options = { 'node_color': 'red', 'node_size': 7, 'edge_color': 'tab:gray', 'with_labels': False } plt.figure(figsize=(50, 50)) pos = nx.spring_layout(G, k=0.2, iterations=50) #50 : défaut nx.draw(G, pos, **options) plt.savefig("graphe.png") linefeed = chr(10) s = linefeed.join(nx.generate_gexf(G)) with open("graphe.gexf", "w") as f: f.write(s)
def exportGEXF(self, filename): """ Método exportar la red a formato GEXF Args: filename: ruta del nuevo fichero """ self.writeFile(filename, nx.generate_gexf(self.__G))
def test_numpy_type(self): G = nx.path_graph(4) try: import numpy except ImportError: return nx.set_node_attributes(G, {n: n for n in numpy.arange(4)}, 'number') G[0][1]['edge-number'] = numpy.float64(1.1) expected = """<gexf version="1.2" xmlns="http://www.gexf.net/1.2draft"\ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation\ ="http://www.gexf.net/1.2draft http://www.gexf.net/1.2draft/gexf.xsd"> <meta lastmodifieddate="{}"> <creator>NetworkX {}</creator> </meta> <graph defaultedgetype="undirected" mode="static" name=""> <attributes class="edge" mode="static"> <attribute id="1" title="edge-number" type="float" /> </attributes> <attributes class="node" mode="static"> <attribute id="0" title="number" type="int" /> </attributes> <nodes> <node id="0" label="0"> <attvalues> <attvalue for="0" value="0" /> </attvalues> </node> <node id="1" label="1"> <attvalues> <attvalue for="0" value="1" /> </attvalues> </node> <node id="2" label="2"> <attvalues> <attvalue for="0" value="2" /> </attvalues> </node> <node id="3" label="3"> <attvalues> <attvalue for="0" value="3" /> </attvalues> </node> </nodes> <edges> <edge id="0" source="0" target="1"> <attvalues> <attvalue for="1" value="1.1" /> </attvalues> </edge> <edge id="1" source="1" target="2" /> <edge id="2" source="2" target="3" /> </edges> </graph> </gexf>""".format(time.strftime('%Y-%m-%d'), nx.__version__) obtained = '\n'.join(nx.generate_gexf(G)) assert expected == obtained
def test_numpy_type(self): G = nx.path_graph(4) try: import numpy except ImportError: return nx.set_node_attributes(G, {n:n for n in numpy.arange(4)}, 'number') G[0][1]['edge-number'] = numpy.float64(1.1) expected = """<gexf version="1.2" xmlns="http://www.gexf.net/1.2draft" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance"> <graph defaultedgetype="undirected" mode="static" name=""> <attributes class="edge" mode="static"> <attribute id="1" title="edge-number" type="float" /> </attributes> <attributes class="node" mode="static"> <attribute id="0" title="number" type="int" /> </attributes> <meta> <creator>NetworkX {}</creator> <lastmodified>{}</lastmodified> </meta> <nodes> <node id="0" label="0"> <attvalues> <attvalue for="0" value="0" /> </attvalues> </node> <node id="1" label="1"> <attvalues> <attvalue for="0" value="1" /> </attvalues> </node> <node id="2" label="2"> <attvalues> <attvalue for="0" value="2" /> </attvalues> </node> <node id="3" label="3"> <attvalues> <attvalue for="0" value="3" /> </attvalues> </node> </nodes> <edges> <edge id="0" source="0" target="1"> <attvalues> <attvalue for="1" value="1.1" /> </attvalues> </edge> <edge id="1" source="1" target="2" /> <edge id="2" source="2" target="3" /> </edges> </graph> </gexf>""".format(nx.__version__, time.strftime('%d/%m/%Y')) obtained = '\n'.join(nx.generate_gexf(G)) assert_equal(expected, obtained)
def test_numpy_type(self): np = pytest.importorskip("numpy") G = nx.path_graph(4) nx.set_node_attributes(G, {n: n for n in np.arange(4)}, "number") G[0][1]["edge-number"] = np.float64(1.1) expected = f"""<gexf xmlns="http://www.gexf.net/1.2draft"\ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation\ ="http://www.gexf.net/1.2draft http://www.gexf.net/1.2draft/gexf.xsd"\ version="1.2"> <meta lastmodifieddate="{time.strftime('%Y-%m-%d')}"> <creator>NetworkX {nx.__version__}</creator> </meta> <graph defaultedgetype="undirected" mode="static" name=""> <attributes mode="static" class="edge"> <attribute id="1" title="edge-number" type="float" /> </attributes> <attributes mode="static" class="node"> <attribute id="0" title="number" type="int" /> </attributes> <nodes> <node id="0" label="0"> <attvalues> <attvalue for="0" value="0" /> </attvalues> </node> <node id="1" label="1"> <attvalues> <attvalue for="0" value="1" /> </attvalues> </node> <node id="2" label="2"> <attvalues> <attvalue for="0" value="2" /> </attvalues> </node> <node id="3" label="3"> <attvalues> <attvalue for="0" value="3" /> </attvalues> </node> </nodes> <edges> <edge source="0" target="1" id="0"> <attvalues> <attvalue for="1" value="1.1" /> </attvalues> </edge> <edge source="1" target="2" id="1" /> <edge source="2" target="3" id="2" /> </edges> </graph> </gexf>""" obtained = "\n".join(nx.generate_gexf(G)) assert expected == obtained
def _generate_network_of_frames(topics_id, timespans_id, num_of_sources, out_name, top_media_sort, remove_media_list=None, remove_word_list=[], generate_word_lists=False, include_media_list=None, media_attribs=None, num_words=None): if remove_media_list is None: remove_media_list = [] # use this specify attributes on the media source that should be added to the node as attributes # if(media_attribs == None): # media_attribs = {} if include_media_list is None: media_sources_md = topic_media_list(user_mediacloud_key(), topics_id, timespans_id=timespans_id, limit=num_of_sources + len(remove_media_list), sort=top_media_sort)['media'] else: media_sources_md = include_media_list if remove_media_list is not None: for r in remove_media_list: media_sources_md = _remove_media_source(r, media_sources_md) top_words = _build_top_words(media_sources_md, topics_id, timespans_id, remove_word_list, num_words) if remove_word_list is not None: top_words = _clean_top_words(top_words, remove_word_list) frame_network = _build_network(top_words, media_sources_md, media_attribs) _export_gexf_network(frame_network, '%s.gexf' % out_name) _export_d3_network(frame_network, '%s' % out_name) if generate_word_lists: with open('%s.txt' % out_name, 'w', encoding="utf-8") as wl: all_words = [] media_sources = {ms['media_id']: ms['name'] for ms in media_sources_md} # counts = {} for ms in top_words: # wl.write("\n\n%s (media id: %d):\n" % (media_sources[ms].encode('ascii', 'ignore'), ms)) wl.write("\n\n{} (media id: {}):\n".format(media_sources[ms], ms)) for w in top_words[ms]: all_words.append(w['term']) # increment count to see how many media source include each word # counts[ms] # wl.write("- %s (%d)\n" % (w['term'].encode('ascii', 'ignore'), w['count'])) wl.write("- {} ({})\n".format(w['term'], w['count'])) wl.write("\n") linefeed = chr(10) # linefeed=\n s = linefeed.join(nx.generate_gexf(frame_network)) # doctest: +SKIP # for line in nx.generate_gexf(frame_network): # doctest: +SKIP # logger.debug line return s
def test_edge_id_construct(self): G = nx.Graph() G.add_edges_from([(0, 1, {'id': 0}), (1, 2, {'id': 2}), (2, 3)]) if sys.version_info < (3, 8): expected = """<gexf version="1.2" xmlns="http://www.gexf.net/\ 1.2draft" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:\ schemaLocation="http://www.gexf.net/1.2draft http://www.gexf.net/1.2draft/\ gexf.xsd"> <meta lastmodifieddate="{}"> <creator>NetworkX {}</creator> </meta> <graph defaultedgetype="undirected" mode="static" name=""> <nodes> <node id="0" label="0" /> <node id="1" label="1" /> <node id="2" label="2" /> <node id="3" label="3" /> </nodes> <edges> <edge id="0" source="0" target="1" /> <edge id="2" source="1" target="2" /> <edge id="1" source="2" target="3" /> </edges> </graph> </gexf>""".format(time.strftime('%Y-%m-%d'), nx.__version__) else: expected = """<gexf xmlns="http://www.gexf.net/1.2draft" xmlns:xsi\ ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.\ gexf.net/1.2draft http://www.gexf.net/1.2draft/gexf.xsd" version="1.2"> <meta lastmodifieddate="{}"> <creator>NetworkX {}</creator> </meta> <graph defaultedgetype="undirected" mode="static" name=""> <nodes> <node id="0" label="0" /> <node id="1" label="1" /> <node id="2" label="2" /> <node id="3" label="3" /> </nodes> <edges> <edge source="0" target="1" id="0" /> <edge source="1" target="2" id="2" /> <edge source="2" target="3" id="1" /> </edges> </graph> </gexf>""".format(time.strftime('%Y-%m-%d'), nx.__version__) obtained = '\n'.join(nx.generate_gexf(G)) assert_equal(expected, obtained)
def generate_network_of_frames(controversy_id, dump_id, timeslice_id, num_of_sources, out_name=None, remove_media_list=None, remove_word_list=[], generate_word_lists=False, include_media_list=None, media_attribs=None): if(out_name == None): out_name = 'network_of_frames-%s' % datetime.datetime.now().isoformat() if(remove_media_list == None): remove_media_list = [] # if(media_attribs == None): # media_attribs = {} if(include_media_list == None): media_sources_md = get_top_x_sources(controversy_id, num_of_sources+len(remove_media_list), dump_id, timeslice_id) else: media_sources_md = include_media_list if(remove_media_list != None): for r in remove_media_list: media_sources_md = remove_media_source(r, media_sources_md) top_words = build_top_words(media_sources_md, timeslice_id, remove_word_list) if(remove_word_list != None): top_words = clean_top_words(top_words, remove_word_list) frame_network = build_network(top_words, media_sources_md, media_attribs) export_gexf_network(frame_network, '%s.gexf' % out_name) export_d3_network(frame_network, '%s' % out_name) if(generate_word_lists == True): with open('%s-word-usage.txt' % out_name, 'wb') as wl: all_words = [] media_sources = {ms['media_id']: ms['name'] for ms in media_sources_md} counts = {} for ms in top_words: wl.write("\n\n%s (media id: %d):\n" % (media_sources[ms].encode('ascii', 'ignore'), ms)) for w in top_words[ms]: all_words.append(w['term']) # increment count to see how many media source include each word # counts[ms] wl.write("- %s (%d)\n" % (w['term'].encode('ascii', 'ignore'), w['count'])) wl.write("\n") linefeed=chr(10) # linefeed=\n s=linefeed.join(nx.generate_gexf(frame_network)) # doctest: +SKIP # for line in nx.generate_gexf(frame_network): # doctest: +SKIP # print line return s
def graph(slug): """ Get a JSON representation of the network. """ network = _get_network(slug) graph = nx.DiGraph() for entity in network.entities: entity.as_nx(graph) for relation in network.relations: relation.as_nx(graph) out = "" for line in nx.generate_gexf(graph): # print [line] out += line return Response(out, status=200, content_type="text/xml")
def graph(slug): """ Get a JSON representation of the network. """ network = _get_network(slug) graph = nx.DiGraph() for entity in network.entities: entity.as_nx(graph) for relation in network.relations: relation.as_nx(graph) out = '' for line in nx.generate_gexf(graph): #print [line] out += line return Response(out, status=200, content_type='text/xml')
def exportgefx(final_devices_list,neighborship_dict): G = nx.Graph() G.add_edges_from(neighborship_dict.keys()) #loop through and add label for edges for edge in neighborship_dict.keys(): G.edges[edge]['label'] = neighborship_dict[edge] nodes =id_to_name(final_devices_list) #loop through and add label attribute for nodes for node in nodes.keys(): G.add_node(node) G.node[node]['label'] = nodes[node] s = '\n'.join([line for line in nx.generate_gexf(G)]) return s
def graph(slug, id): """ Get a JSON representation of the network. """ network, entity = _get_entity(slug, id) entity_types = request.args.getlist('entity_type') rel_types = request.args.getlist('relation_type') exports = set() graph = nx.DiGraph() def export(entity, depth): if entity.id in exports or \ (len(entity_types) and entity.type not in entity_types): return False entity.as_nx(graph) exports.add(entity.id) if depth > 0: for rel in entity.incoming: if len(rel_types) and not rel.type in rel_types: continue if rel.id not in exports and export(rel.source, depth - 1): rel.as_nx(graph) exports.add(rel.id) for rel in entity.outgoing: if len(rel_types) and not rel.type in rel_types: continue if rel.id not in exports and export(rel.target, depth - 1): rel.as_nx(graph) exports.add(rel.id) return True export(entity, 2) out = '' for line in nx.generate_gexf(graph): #print [line] out += line # JSONP for XML. Will now go vomit quietly somewhere. if request.args.get('wrap') == 'json': return jsonify({'xml': out}) return Response(out, status=200, content_type='text/xml')
def download_cmtys(request, ds, did, cd, cdname, layer, format): fname_requested = format format = format.rsplit('.')[-1] fname = '%s-%s%s.%s'%(os.path.basename(ds.netfile.name), cdname, layer, format) if fname_requested != fname: return redirect(download_cmtys, did=did, cdname=cdname, layer=layer, format=fname) cmtys = cd.get_results()[layer] data = [ ] content_type = 'text/plain' force_download = False if format == 'txt': for cname, cnodes in cmtys.iteritems(): data.append(' '.join(str(x) for x in cnodes)) data = '\n'.join(data) elif format == 'nc': for cname, cnodes in cmtys.iteritems(): for node in cnodes: data.append('%s %s'%(node, cname)) data = '\n'.join(data) elif format == 'gexf': g = ds.get_networkx() for node, cs in cmtys.nodecmtys().iteritems(): g.node[node]['cmty'] = ' '.join(str(x) for x in cs) data = nx.generate_gexf(g) data = '\n'.join(data) elif format == 'gml': g = ds.get_networkx() for node, cs in cmtys.nodecmtys().iteritems(): g.node[node]['cmty'] = ','.join(str(x) for x in cs) data = nx.generate_gml(g) data = '\n'.join(data) response = HttpResponse(content=data, content_type=content_type, ) # If the data size is too big, force a download instead of viewing as text. if force_download or len(data) > 50 * 2**10: response['Content-Disposition'] = 'attachment; filename=%s'%fname return response
def test_edge_id_construct(self): G = nx.Graph() G.add_edges_from([(0, 1, {'id': 0}), (1, 2, {'id': 2}), (2, 3)]) expected = """<gexf version="1.2" xmlns="http://www.gexf.net/1.2draft" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance"> <graph defaultedgetype="undirected" mode="static" name=""> <meta> <creator>NetworkX {}</creator> <lastmodified>{}</lastmodified> </meta> <nodes> <node id="0" label="0" /> <node id="1" label="1" /> <node id="2" label="2" /> <node id="3" label="3" /> </nodes> <edges> <edge id="0" source="0" target="1" /> <edge id="2" source="1" target="2" /> <edge id="1" source="2" target="3" /> </edges> </graph> </gexf>""".format(nx.__version__, time.strftime('%d/%m/%Y')) obtained = '\n'.join(nx.generate_gexf(G)) assert_equal(expected, obtained)
def generate_network_of_frames(topics_id, timespans_id, num_of_sources, out_name, remove_media_list=None, remove_word_list=[], generate_word_lists=False, include_media_list=None, media_attribs=None, num_words=None): if (remove_media_list == None): remove_media_list = [] # use this specify attributes on the media source that should be added to the node as attributes # if(media_attribs == None): # media_attribs = {} if (include_media_list == None): media_sources_md = get_top_x_sources( topics_id, num_of_sources + len(remove_media_list), timespans_id) else: media_sources_md = include_media_list if (remove_media_list != None): for r in remove_media_list: media_sources_md = remove_media_source(r, media_sources_md) top_words = build_top_words(media_sources_md, topics_id, timespans_id, remove_word_list, num_words) if (remove_word_list != None): top_words = clean_top_words(top_words, remove_word_list) frame_network = build_network(top_words, media_sources_md, media_attribs) export_gexf_network(frame_network, '%s.gexf' % out_name) export_d3_network(frame_network, '%s' % out_name) if (generate_word_lists == True): with open('%s.txt' % out_name, 'wb') as wl: all_words = [] media_sources = { ms['media_id']: ms['name'] for ms in media_sources_md } counts = {} for ms in top_words: wl.write("\n\n%s (media id: %d):\n" % (media_sources[ms].encode('ascii', 'ignore'), ms)) for w in top_words[ms]: all_words.append(w['term']) # increment count to see how many media source include each word # counts[ms] wl.write("- %s (%d)\n" % (w['term'].encode('ascii', 'ignore'), w['count'])) wl.write("\n") linefeed = chr(10) # linefeed=\n s = linefeed.join(nx.generate_gexf(frame_network)) # doctest: +SKIP # for line in nx.generate_gexf(frame_network): # doctest: +SKIP # logger.debug line return s
def generator_gexf_from_query( self, query: Query, with_relation: str = "reply_to") -> Generator[str]: graph = self.graph_from_query(query, with_relation=with_relation) return generate_gexf(graph)
def graph_to_gexf(G: nx.DiGraph): return nx.generate_gexf(G)
def _generate_network_of_frames(topics_id, timespans_id, num_of_sources, out_name, top_media_sort, remove_media_list=None, remove_word_list=[], generate_word_lists=False, include_media_list=None, media_attribs=None, num_words=None): if remove_media_list is None: remove_media_list = [] # use this specify attributes on the media source that should be added to the node as attributes # if(media_attribs == None): # media_attribs = {} if include_media_list is None: media_sources_md = topic_media_list(user_mediacloud_key(), topics_id, timespans_id=timespans_id, limit=num_of_sources + len(remove_media_list), sort=top_media_sort)['media'] else: media_sources_md = include_media_list if remove_media_list is not None: for r in remove_media_list: media_sources_md = _remove_media_source(r, media_sources_md) top_words = _build_top_words(media_sources_md, topics_id, timespans_id, remove_word_list, num_words) if remove_word_list is not None: top_words = _clean_top_words(top_words, remove_word_list) frame_network = _build_network(top_words, media_sources_md, media_attribs) _export_gexf_network(frame_network, '%s.gexf' % out_name) _export_d3_network(frame_network, '%s' % out_name) if generate_word_lists: with open('%s.txt' % out_name, 'w', encoding="utf-8") as wl: all_words = [] media_sources = { ms['media_id']: ms['name'] for ms in media_sources_md } # counts = {} for ms in top_words: # wl.write("\n\n%s (media id: %d):\n" % (media_sources[ms].encode('ascii', 'ignore'), ms)) wl.write("\n\n{} (media id: {}):\n".format( media_sources[ms], ms)) for w in top_words[ms]: all_words.append(w['term']) # increment count to see how many media source include each word # counts[ms] # wl.write("- %s (%d)\n" % (w['term'].encode('ascii', 'ignore'), w['count'])) wl.write("- {} ({})\n".format(w['term'], w['count'])) wl.write("\n") linefeed = chr(10) # linefeed=\n s = linefeed.join(nx.generate_gexf(frame_network)) # doctest: +SKIP # for line in nx.generate_gexf(frame_network): # doctest: +SKIP # logger.debug line return s