Networkx add capacity to each edge via numpy array - networkx

I have a networkx multigraph and i want to assign each edge that i add through add_edges_from() a different capacity. The capacities are in a numpy array.
I tried set_edge_attributes() but this just sets the whole array for each edge as attribute.
Is there a clever way of doing that or do i have to add each edge indiviually with G.add_edge() and assigning my attribute there with an iterator?

Related

Creation of two networks with the same node coordinates

I create a network add nodes and edges. I view it (it creates a dot and pdf file automatically). Later, I want to create a second network with the same nodes but different edges. I want to place the nodes in the same coordinates, so that I can make a comparison of both graphs easily. I tried to get the coordinates of the first graph, and tried to set the coordinates of the nodes) but I couldn't find proper functions to do that. I also checked networkx package. I also tried to get a copy of the first network, and delete the edges with no success. Can someone please show me how to create a second network with the same node coordinates?
This is the simple network creation code
import graphviz as G
network1 = G.Digraph(
graph_attr={...},
node_attr={...},
edge_attr={...} )
network.node("xxx")
network.node("yyy")
network.node("zzz")
network.edge("xxx", "yyy")
network.edge("yyy", "zzz")
network1.view(file_name)
First, calculate the node positions for the first graph using the layout of your choice (say, the spring layout):
node_positions = nx.layout.spring_layout(G1)
Now, you can draw this graph and any other graph with the same nodes in the same positions:
nx.draw(G1, with_labels=True, pos=node_positions)
nx.draw(G2, with_labels=True, pos=node_positions)
Graphviz's layers feature might also be interesting:
https://graphviz.org/faq/#FaqOverlays
Here is a working example of using layers - ignore the last two lines that create a video.
https://forum.graphviz.org/t/stupid-dot-tricks-2-making-a-video/109
And here is some more background:
https://forum.graphviz.org/t/getting-layers-to-work-with-svg/107

three.js calculate surfaces of stl files

I think i have a difficult problem right here..
I want to able to get the surfaces of f.e. the orange object in this three.js example https://threejs.org/examples/?q=stl#webgl_loader_stl
i want to click with the mouse, find the correct surface, which should then be highlighted, so i make sure this was the surface i want.
(i already implemented raycaster successfully, so thats not an issue)
The intersectObject method returns an array of intersections, each of which has face property. The face contains vertex indices.
For STL files containing multiple solids, each solid is assigned to a different group, and the groups are available in the geometry object that is returned from STLLoader. Each group is defined by a range of vertex indices.
So, I think you can correlate the vertex indices returned from the raycaster with the vertex indices in the geometry groups.

Tracking 'objects' and labelling with the same labels between images/arrays in MATLAB

This is essentially a question about blob/feature tracking in MATLAB.
I have a series of sequential arrays (for simplicity, say 5 at this stage) with a background of 0 values, and then multiple objects (blobs) within each array. This is in MATLAB. These blobs can change position and size, and appear or disappear altogether, between arrays.
I'd like to label any of the blobs that are in the same position between arrays with the same ID (either a number or a letter).
Also need to incorporate functionality so that if a new pixel is added to the edge of a blob in a previous image, then it would be given the same ID.
And some functionality so that if a blob appeared in any previous array (say it was in array 1, disappeared in arrays 2 and 3, and then reappeared in 4 and 5), then it would be given the same ID as it originally had in the 1st array.
I've currently been trying to use the bwlabel function in MATLAB to do this but obviously each sequential array is labelled independently of the previous ones, so there is no 'tracking' of blobs from one image to another, and the numbers for each blob change based on how many blobs there are in that array and their positions.
Would be very grateful of any thoughts/comments on how to do this. If further clarification is required on this question, please also let me know. Many thanks in advance.
There is functionality for object tracking in the Computer Vision System Toolbox. There is an implementation of the Kalman filter (vision.KalmanFilter) that you can use to predict the location of an object in the next video frame, and a function called assignDetectionsToTracks that you can use to associate the objects across frames. See the Motion-Based Multiple Object Tracking example.

How do get the edges along the path returned by dijkstra in orientdb

currently the algorithm returns only the vertices , but how to get the edges that it chose ? as there can be multiple edges between the vertices
I'll have to modify current function. Search source code 'OSQLFunctionDijkstra.java' for existing function and inside 'getDistance' method it should be easy to print 'Edge' information.

Additional forces to networkx spring_layout

I would like to add additional forces to networkx spring_layout.
I have a directected graph and I would like nodes to move to different sides according to the edges that they have. Nodes that have more outgoing edges should drift to nodes that have more ingoing edges should drift right. Another alternative would be. That these groups of nodes would drift towards each other, nodes with outgoing edges would get closer while nodes with ingoing edges would also get closer to each other.
I managed to look into to the source code of spring_layout of networkx http://networkx.lanl.gov/archive/networkx-0.37/networkx.drawing.layout-pysrc.html#spring_layout
but everything there is just beyond my comprehension
G.DiGraph()
G.add_edges_from([(1,5),(2,5),(3,5),(5,6),(5,7)])
The layout should show edges 1,2,3 closer to each other, the same regarding 6 and 7.
I imagine, that I could solve this by adding invisible edges via using MultiDiGraph. I could count ingoing and outgoing edges of each node and add invisible edges that connect the two groups. However, I am very sure that there are better ways of solving the problem.
Adding weights into the mix would be a good way to group things (with those invisible nodes). But the layouts have no way of knowing left from right. To get the exact layout you want you could specify each point's x,y coordinates.
import networkx as nx
G=nx.Graph()
G.add_node(1,pos=(1,1))
G.add_node(2,pos=(2,3))
G.add_node(3,pos=(3,4))
G.add_node(4,pos=(4,5))
G.add_node(5,pos=(5,6))
G.add_node(6,pos=(6,7))
G.add_node(7,pos=(7,9))
G.add_edges_from([(1,5),(2,5),(3,5),(5,6),(5,7)])
pos=nx.get_node_attributes(G,'pos')
nx.draw(G,pos)