I have the following txt file representing a network in edgelist format.
The first two columns represent the usual: which node is connected to which other nodes
The third column represents weights, representing the number of times each node has contacted the other.
I have searched the igraph documentation but there's no mention of how to include an argument for weight when importing standard file formats like txt.
The file can be accessed from here and this is the code I've been using:
read.graph("Irvine/OClinks_w.txt", format="edgelist")
This code treats the third column as something other than weight.
Does anyone know the solution?
does the following cause too much annoyance?
g <- read.table("Irvine/OClinks_w.txt")
g <- graph.data.frame(g)
if it does then directly from the file you can use
g<-read.graph("Irvine/OClinks_w.txt",format="ncol")
E(g)$weight
If you are using Python and igraph, the following line of code works to import weights and vertex names:
g1w=Graph.Read_Ncol("g1_ncol_format_weighted.txt",names=True)
Note: you must tell igraph to read names attribute with names=True, otherwise just vertex numbers will be imported.
Where g1_ncol_format_weighted.txt looks something like:
A B 2
B C 3
To make sure the import worked properly, use the following lines:
print(g1w.get_edgelist())
print(g1w.es["weight"])
print(g1w.vs["name"])
Related
I am new to Python and I tried to make a 3D network graph using a csv file, with NetworkX and Plotly. I used Spring3D function and tried to separate dimensions from the array which was generated like in a tutorial I saw on Deepnote, but I am unable to do it probably because my nodes data aren't read like integers, however I specified the nodetype int in my arguments... or maybe because I have to convert my data to matrix or array.(see my screens below)
When I call one of my nodes it sends me back a Key Error, but it works when I call it like string spring_3D ['4']. I don't know if this problem is linked to the fact that I can't assign my coordinates to my nodes but I suppose. I really don't know and I tried so many ways (like using different ways of importing my data with csv.reader and with open I was able to see my nodes like an integer with this method np.array([[int(e) for e in row] for row in csv.reader(f, delimiter=',')]) but it didn't work at the next step.
I also tried to add nodes manually but unsuccessfully. I used the read.adjlist method but it's probably the same thing. I think the answer is probably pretty obvious.
I work on python 3.6 with Jupyter notebook and Anaconda
G = nx.read_adjlist('clean Passerelles-mentales final 1bisfinal.csv', delimiter = ',', nodetype = int )
spring_pos = nx.spring_layout(G, seed=3)
nx.draw_networkx(G, with_labels = True)
spring_3D = nx.spring_layout(g,dim=3, seed=18)
x_nodes = [spring_3D[i][0] for i in range(Num_nodes)]# x-coordinates of nodes
y_nodes = [spring_3D[i][1] for i in range(Num_nodes)]# y-coordinates
z_nodes = [spring_3D[i][2] for i in range(Num_nodes)]# z-coordinates
I received a Key Error
An example of what I am trying to do:
https://deepnote.com/#deepnote/3D-network-visualisations-using-plotly-oYxeN6UXSye_3h_ulKV2Dw
I have created a weighted edge list that I am trying to use to generate a weighted undirected graph:
The data is in a csv which looks like the following in excel:
node1 node2 weight
a b 0.1
a c 0.3
As recommended by other StackOverflow posts, I have been using the following code to read in the csv:
fh=open("<file_location>.csv", 'r')
G = nx.read_weighted_edgelist(fh,delimiter=',')
The first line runs fine but the second yields the error message:
TypeError: Failed to convert weight data weight to type type 'float'
If I check G, it has read in the nodes fine but not the weights, any ideas?
EDIT: restructured to include explanation for why code fails and how to resolve, following #Joel's suggestion. #Joel's answer provides an explanation of why the code fails, but not a suggestion for how to move resolve it.
Solution
nx.read_weighted_edgelist will ignore input lines that start with #, so if you change the first line of your csv file from
node1 node2 weight
to
#node1 node2 weight
then you should be able to read in the network with weights.
Also note that read_weighted_edgelist accepts a file path (string) as well as a file handle, so if you aren't using fh again, you don't need to open it first, just pass it directly.
G = nx.read_weighted_edgelist("<file_location>.csv", delimiter=',')
Why your code failed
(this is incorporated from the answer of #Joel)
When it encounters the first line (from your comments: a_node1,b_node2,c_weight )
it interprets the first node to be a_node1, the second to be b_node2, and it tries to assign the weight c_weight to the edge between them.
It is difficult to convert the string c_weight to a float. So it gives an error.
When it encounters the first line (from your comments: a_node1,b_node2,c_weight )
it interprets the first node to be a_node1, the second to be b_node2, and it tries to assign the weight c_weight to the edge between them.
It is difficult to convert the string c_weight to a float. So it gives an error.
I'm using python+graphviz in order to create networking topologies out of the information contained in Racktables. I've succeded pretty well so far but I'm willing now to add multiple lines labels to a cluster (not a node).
For example, I have the following code with python:
for router in routers:
[...]
cluster_name = "cluster"+str(i)
router_label=router_name+"\n"+router_hw
c = gv.Graph(cluster_name)
c.body.append('label='+router_label)
When ever I run that program, I get the following:
ST120_CMS70_SARM
SARM
ST202_P9J70_SARM
SARM
Error: node "SARM" is contained in two non-comparable clusters "cluster1" and "cluster0"
But, if I change this router_label=router_name+"\n"+router_hw to this router_label=router_name+"_"+router_hw, I get no error and the topology gets drawn, but, of course, a one line label.
Any hint on this?
Many thanks!
Lucas
Ok, I've found the solution. The multiline label is achieved using HTML like labels, like the following one...
router_label="<"+router_name+"<BR />"+router_ip+">"
c = gv.Graph(cluster_name)
c.body.append('label='+router_label)
This code provides the following:
Thanks!
Lucas
I want to read sdf file (containing many molecules) and return the weighted adjacency matrix of the molecule. Atoms should be treated as vertices and bond as edges. If i and j vertex are connected by single, double, or triple bond then corresponding entries in the adjacency matrix should be 1,2, and 3 respectively. I need to further obtain a distance vector for each vertex which list the number of vertices at different distance.
Are there any python package available to do this?
I would recommend Pybel for reading and manipulating SDF files in Python. To get the bonding information, you will probably need to also use the more full-featured but less pythonic openbabel module, which can be used in concert with Pybel (as pybel.ob).
To start with, you would write something like this:
import pybel
for mol in pybel.readfile('sdf', 'many_molecules.sdf'):
for atom in mol:
coords = atom.coords
for neighbor in pybel.ob.OBAtomAtomIter(atom.OBAtom):
neighbor_coords = pybel.atom(neighbor).coords
See
http://code.google.com/p/cinfony/
However for your exact problem you will need to consult the documentation.
I'd like to know the best way to read a disconected undirected graph using igraph for python. For instance, if I have the simple graph in which 0 is linked to 1 and 2 is a node not connected to any other. I couldn't get igraph to read it from a edgelist format(Graph.Read_Edgelist(...)), because every line must be an edge, so the following is not allowed:
0 1
2
I've been just wondering if adjacency matrix is my only/best option in this case (I could get it to work through this representation)? I'd rather a format in which I could understand the data by looking it (something really hard when it comes to matrix format).
Thanks in advance!
There's the LGL format which allows isolated vertices (see Graph.Read_LGL). The format looks like this:
# nodeID
nodeID2
nodeID3
# nodeID2
nodeID4
nodeID5
nodeID
# isolatedNode
# nodeID5
I think you get the basic idea; lines starting with a hash mark indicate that a new node is being defined. After this, the lines specify the neighbors of the node that has just been defined. If you need an isolated node, you just specify the node ID prepended by a hash mark in the line, then continue with the next node.
More information about the LGL format is to be found here.
Another fairly readable format that you might want to examine is the GML format which igraph also supports.