Remove self loops and multiple edges from configuration multigraph - networkx

I am trying to draw a random network with a real sequence of degrees, using configuration model.
As I understood, the function returns a multigraph with self-loops and multiple edges.
How can I remove all self-loops and change all multiple edges for one simple edge?
I would appreciate any help!

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

Choosing a networkx layout that takes edge labels into account

I'm plotting networkx weighted graphs using the draw_networkx_edge_labels function. My problem is that, since edges sometimes cross each other, it is not always clear from the plot which weight belongs to which edge. For instance, in the following plot it is not immediately clear whether 2 is the weight of (1,2) or (3,7).
I'm currently using the neato layout, which does not take edge labels into account. In particular, this is how I'm drawing a weighted graph g:
layout = nx.nx_pydot.graphviz_layout(g, prog='neato')
nx.draw(g, pos=layout)
edge_labels = nx.get_edge_attributes(g, 'weight')
nx.draw_networkx_edge_labels(g, pos=layout, edge_labels=edge_labels)
I know I can manually control the position of the label along an edge using the label_pos parameter, but my question is whether there exists a way to automatically plot the graph such that edge labels do not usually collide (either using a layout that takes labels into account or a method that "neatly" selects label positions along edges).
I'm not expecting something that always works, but since my graphs are relatively sparsely connected, I hope there's a method that at least has a tendency to work well.
I have been meaning to implement this in netgraph, my fork of the networkx drawing utilities, for a while now. Unfortunately, I have a job interview on Thursday, so I won't have time to write this anytime soon. The basic idea, however, is pretty simple, and is also already implemented in some R packages such as ggrepel and also ggnetwork.
The basic idea is that you use a force directed layout to position your labels, given a predetermined and fixed layout for your nodes and edges. So:
Compute a node layout using the layout of your choice.
Partition each edge into a chain of many, many nodes, and compute the positions of the "edge nodes" using the already known positions of the source and target nodes of the edge. This partitioning is to give each edge a "mass" in the following force directed layout.
For each edge, add a "label" node and connect it to the most central "edge node".
Compute a force-directed layout keeping all nodes but the label nodes fixed (e.g. using spring_layout in networkx).
You should now have sensible edge label coordinates that do not overlap any of the edges. Use plt.annotate to plot a connection between the edge and the edge label.

How to identify Points Outside a 3D mesh in Matlab

I have two large data sets, one that is the outside of an object, and on that represents the fluid flow inside of the object. I am worried that with the mesh I have, some of the data might be mis-represented, or not modeled well, and is outside the first data set.
In Matlab, I used trisurf to create a mesh from the first data set and was curious if there was a way to check for points outside the mesh. Ive seen the 2D version of inpolygon, and some threshold functions, but the surface is not super regular and those don't really account for meshes. Thanks for the help!
You didn't specify how what kind of data/format your object is defined as. If for example you have a Delaunay tetrahdralization/mesh of your object (if not you can use delaunay to create one from a point cloud), you can use the tsearchn function to determine if points are in/out of the object (mesh).
https://www.mathworks.com/help/matlab/ref/tsearchn.html

Unity3D dynamic mesh with hole

Dynamically creating a mesh with a hole in it from two lists of vertices
I am currently attempting to dynamically create a mesh (2D) with a hole in it. I have a list of Vector3 vertices for both the outline and the hole's outline.
My question:
How would I go about merging these two lists of vertices into a single mesh?
More detail: I have two meshes that overlap, and I'm trying to do a boolean difference between the two, to create a new mesh that will eventually replace the bigger one, to get rid of clipping. Example
Using the Clipper-Library (see http://www.angusj.com/delphi/clipper.php) is of no use, because it returns the same two sets of vertices that I set as input.
I'm guessing I need to somehow fix the triangles for the mesh to create triangles between the outer and inner vertices? (The meshes can be any shape/size, so finding out which vertexes to combine into triangles is no easy task).
Can anybody tell me how I would create a single mesh out of the two vertex-loops?
If you need a generic boolean algorithm this is a very hard problem, for example 3D Studio Max has two seperate boolean mesh creators, each failing at different sets of objects.
If you only need to subtract rectangular, aligned shapes, which do not touch, its simpler. For your specific case you can just join the list of vertexes, and fill new list of triangles - you'll need two tris per quad, so thats eight triangles stretched across eight verices.
It gets a bit harder if they start to touch, as you need to find intersection points and basically re-triangulate the outline.

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)