0

I don't know where to start with this (programmatically) so I will describe input and output.

I have dictionary like this:

racks = {
         "Rack_01" : [1, 2, 3],
         "Rack_02" : [3, 4, 5],
         "Rack_03" : [1, 2, 4, 5],
         }

So generally, rack names with cable names. If the same cable is present in the two racks, it means they are connected.

Of course I have like 20 racks, and around 140 cables. Maximum connection to one rack is around 40 cables.

I would like to have nodes with names of the racks and connections to be named as cable that is connecting them.

Similar to this (shape could be different, just symbolic representation):

Example of an output

Adam Satyr
  • 51
  • 11
  • 1
    Do you want networkx inside python to draw a diagram similar to the one you show? – Dschoni Mar 30 '22 at 14:08
  • Basically yes, I'm also open for other solutions, but I know only python (pandas, numpy, matplotlib) – Adam Satyr Mar 30 '22 at 14:10
  • 1
    Probably do one step after another. Build the graph first and take care of styling after. This might be a good starting point https://stackoverflow.com/questions/29738288/displaying-networkx-graph-with-labels or https://stackoverflow.com/questions/15191811/forcing-orthogonal-vertical-or-horizontal-edges-with-dot – Dschoni Mar 30 '22 at 14:22
  • Thanks, looks like good starting point. but there is no info how to add names to connections. – Adam Satyr Mar 30 '22 at 14:27

1 Answers1

0

As a starting point, here's a script to convert that dictionary into a networkx graph, with the nodes and edges labeled correctly. Each pair of nodes has the correct number of edges connecting them.

from collections import defaultdict as dd

d = {
    "Rack_01" : [1, 2, 3],
    "Rack_02" : [3, 4, 5],
    "Rack_03" : [1, 2, 4, 5],
    }
m = len(d) #number of nodes
edge_set = set([i for v in d.values() for i in v])
n = len(edge_set) # number of edges

edge_label = dict(enumerate(edge_set))
node_label = dict(enumerate(d))
# number

inc_mat = np.array([[edge_label[j] in d[label[i]] 
                     for j in range(n)] 
                    for i in range(m)],dtype=int)
adj_mat = np.zeros((m,m),dtype=int)
nodes_to_edge = dd(list)
for k,col in enumerate(inc_mat.T):
    i,j = np.nonzero(col)[0]
    adj_mat[[i,j],[j,i]]+=1
    nodes_to_edge[(i,j)].append(edge_label[k])

G = nx.from_numpy_array(adj_mat, parallel_edges=True,create_using=nx.MultiGraph)

for u,v,d in G.edges(data=True):
    d['label'] = nodes_to_edge[(u,v)].pop()

nx.relabel_nodes(G,node_label,copy=False)

From there, you could use the answer here to generate your visualization.

The result of print(G.edges(data=True)), for reference:

[('Rack_01', 'Rack_02', {'weight': 1, 'label': 3}), ('Rack_01', 'Rack_03', {'weight': 1, 'label': 2}), ('Rack_01', 'Rack_03', {'weight': 1, 'label': 1}), ('Rack_02', 'Rack_03', {'weight': 1, 'label': 5}), ('Rack_02', 'Rack_03', {'weight': 1, 'label': 4})]
Ben Grossmann
  • 4,387
  • 1
  • 12
  • 16