Simple Network Graph Plotting in Python? - python

I am working on some algorithms that create network graphs, and I am finding it really hard to debug the output. The code is written in Python, and I am looking for the simplest way to view the resulting network.
Every node has a reference to its parent elements, but a helper function could be written to format the network in any other way.
What is the simplest way to display a network graph from Python? Even if it's not fully written in Python, ie it uses some other programs available to Linux, it would be fine.

It sounds like you want something to help debugging the network you are constructing. For this you might want to consider implementing a function that converts your network to DOT, a graph description language, which can then be rendered to a graph visualization using a number of tools, such as GraphViz. You can then log the output from this function to help debug.

Have you tried Netwulf? It takes a networkx.Graph object as input and launches an interactive d3-powered visualization in a separate browser window. The resulting image (and data) can then be posted back to Python for further processing.
Disclaimer: I'm a co-author of Netwulf.

Think about using existing graph libraries for your problem domain, e.g. NetworkX. Drawing can be done from there with matplotlib or pygraphviz.
For bigger projects, you might also want to check out a graph database like Neo4j with its toolkit (and own query language CYPHER) for working with python.
A good interface markup is also GraphML, can be useful with drawing tools like yEd in case you have small graphs and need some manual finish.

Related

pyAgrum - printing and plotting

I'm just starting to learn pyAgrum. I am looking for functions to both plot a network and print the tables/potentials within a python session. I.e., I'm looking to do this without the need for any sort of HTML-based interpreter (pylab, etc.). I'm coming from the R-world, where I'm used to this kind of workflow using R's version of igraph e.g., and where tables can be printed as ordinary R-arrays. I know, that pyAgrum::Potential's are lower-level C++ classes but is there a way to achieve the above? I like to stay in my editor :)
I already answered you somewhere else :-) but for the sake of the other readers :
to print an ascii version of a table, you can just use the __str__() methods. Hence print(p) where pis a Potential will do the job.
to export an image of a BN, you can use pyAgrum.lib.image :
import pyAgrum
import pyAgrum.lib.image as gimg
bn=gum.fastBN("A->B->C")
gimg.export(bn,"test.pdf")
gimg.exportInference(bn,"test.png",evs={"A":1})
will export test.pdf containing the graph and test.png containing the (graphical) result of an inference.

QGIS python processing tools - saving/exporting graph

I just tried QGIS processing tools for the first time.
When using shortest path search for the first time, the algorithm is building a path graph (of course). However, the graph is never re-used. Every time the algorithm is used, the graph building starts again. I am not familiar with the exact code used for it, but I guess the graph is network-wide, not related to the specific points I selected. So is there a way to re-use the graph? Er even export it to a file?
My network is large (more than 200k features), so efficiency is important. The network is rarely updated, so calculating a graph could easily be done just once in a while.
I looked up the docs and settings of processing tools, and this option seems to be unavailable (which is surprising). So maybe I am missing something, or maybe someone could suggest a way to serialize the graph and save it using python code? I am using QGIS 3.1 (A Coruna).
As found on anitagraser github page processing tools that use graphs look straightforward using Dijkstra algoritghm.
builder = QgsGraphBuilder( crs )
graph = builder.graph()
from_id = graph.findVertex(from_point)
to_id = graph.findVertex(to_point)
(tree,cost) = QgsGraphAnalyzer.dijkstra(graph,from_id,0)
I guess using that would require building a different tool and not using the user-friendly shortest path search tool with a nice GUI and integration with point selecting and so on. That would be not acceptable. The goal was to be able to perform the same task on any pc with un-altered QGIS (no add-ins needed, only a script). But it may be that it is not possible. So the problem leads to:
is it possible to tweak the existing processing tool, to cache the graph or even store it in a file?
can I somehow duplicate the tool and apply some small changes?
I asked this question already on gis stack echange. My question probably has a programming answer, so I am reposting as an exception.

create p2p-network with "save-option" in python

I need an implementation of network with nodes (<100) in python. Nodes can send response on all nodes and on two neighbor-nodes. Nodes can save small data. Does anyone know of such library?
I use btpeer http://cs.berry.edu/~nhamid/p2p/framework-python.html, but there is no "save-data"-option.
You may find doozerd an interesting concept. I am not sure about consistency guarantees it provides though.

Python graphing from csv

I have extracted 6 months of email metadata and saved it as a csv file. The csv now only contains two columns (from and to email addresses). I want to build a graph where the vertices are those with whom I am communicating and whom communicated with me and the edges are created by a communications link labeling the edges by how many communications I had. What is the best approach for going about this?
One approach is to use Linked Data principles (although not advisable if you are short on time and don't have a background in Linked Data). Here's a possible approach:
Depict each entity as a URI
Use an existing ontology (such as foaf) to describe the data
The data is transformed into Resource Description Framework (RDF)
Use an RDF visualization tool.
Since RDF is inherently a graph, you will be able to visualize your data as well as extend it.
If you are unfamiliar with Linked Data, a way to view the garphs is using Pajek (http://vlado.fmf.uni-lj.si/pub/networks/pajek/). This approach is much simpler but lacks the benefits of semantic interoperability, provided you care about them in the first place.
Cytoscape might be able to import your data in that format and build a network from it.
http://www.cytoscape.org/
Your question (while mentioning Python) does not say what part or how much you want to do with Python. I will assume Python is a tool you know but that the main goal is to get the data visualized. In that case:
1) use Gephi network analysis tool - there are tools that can use your CSV file as-is and Gephi is one of them. in your case edge weights need to be preserved (= number of emails exchanged b/w 2 email addresses) which can be done using the "mixed" variation of Gephi's CSV format.
2) another option is to pre-process your CSV file (e.g. using Python), calculate edge weights (the number of e-mail between every 2 email addresses) and save it in any format you like. The result can be visualized in network analysis tools (such as Gephi) or directly in Python (e.g. using https://graph-tool.skewed.de).
Here's an example of an email network analysis project (though their graph does not show weights).

Suitable file format and tool for 3d modeling pipeline

I'm working on a modeling/reconstruction algorithm for point cloud data. So far I've been developing in Python, and been relatively happy with VPython for my visualization needs.
One problem I have is that VPython becomes quite slow when rendering a great many objects (at least on my non-3d accelerated Linux laptop), making visual inspection of complicated models quite difficult.
I've been trying to use an external tool for visualization, but the problem is that I'm a bit lost in the sea of possible file formats and available tools. I've been trying MeshLab for instance, which works great for displaying point cloud data in simple ascii formats, but I couldn't decide in which compatible format to export my other types of geometry, to superimpose on the point cloud layer.
Here are the requirements for my whole pipeline:
The point cloud data may contain millions of points, stored as simple xyz ascii coords
The modeling primitives are primarily lines and cylinders (i.e. no polygons), numbered in the thousands
The visualization tool should ideally be cross-platform (it must run at least on Linux)
There should be a Python module for easy data import/export of the chosen file format (or the format is simple enough to write a simple converter, if not)
I've been googling a lot about this so I have tentative answers for all of these, but none that is 100% satisfying in my context. Any help or advice would be greatly appreciated.. many thanks in advance!
I finally settled for Geomview: the viewer itself is powerful enough, and the many OOGL file formats that it implements answer my needs. I use the .off format for point cloud data, and .skel for my other modeling primitives. These file formats are also human-readable, which makes writing import/export functions easy.
How about Panda3D? It's cross-platform, and it should be able to handle rendering millions of points as long as you have a decent graphics card.

Categories