0

I have the following dictionary that I need to visualise as a dendrogram.

d = {'L0': ['L5_0', 'L5_1', 'L5_2', 'L5_3'],
         'L10_0': ['L20_0'],
         'L10_1': ['L20_1', 'L20_7'],
         'L10_2': ['L20_2', 'L20_5'],
         'L10_3': ['L20_4', 'L20_10'],
         'L10_4': ['L20_9'],
         'L10_5': ['L20_3'],
         'L10_6': ['L20_6'],
         'L10_7': ['L20_18'],
         'L10_8': ['L20_15', 'L20_17'],
         'L10_9': ['L20_8'],
         'L20_0': ['L40_3'],
         'L20_1': ['L40_5'],
         'L20_10': ['L40_4'],
         'L20_11': ['L40_11'],
         'L20_12': ['L40_23'],
         'L20_13': ['L40_8'],
         'L20_14': ['L40_18'],
         'L20_15': ['L40_19', 'L40_38'],
         'L20_16': ['L40_10', 'L40_20', 'L40_36'],
         'L20_17': ['L40_30'],
         'L20_18': ['L40_26'],
         'L20_19': ['L40_22'],
         'L20_3': ['L40_6', 'L40_7'],
         'L20_4': ['L40_12', 'L40_15'],
         'L20_6': ['L40_17'],
         'L20_7': ['L40_13'],
         'L20_8': ['L40_9'],
         'L20_9': ['L40_1', 'L40_16'],
         'L40_0': [],
         'L40_1': [],
         'L40_10': [],
         'L40_11': [],
         'L40_12': [],
         'L40_13': [],
         'L40_14': [],
         'L40_15': [],
         'L40_16': [],
         'L40_17': [],
         'L40_18': [],
         'L40_19': [],
         'L40_2': [],
         'L40_20': [],
         'L40_21': [],
         'L40_22': [],
         'L40_23': [],
         'L40_24': [],
         'L40_25': [],
         'L40_26': [],
         'L40_27': [],
         'L40_28': [],
         'L40_29': [],
         'L40_3': [],
         'L40_30': [],
         'L40_31': [],
         'L40_32': [],
         'L40_33': [],
         'L40_34': [],
         'L40_35': [],
         'L40_36': [],
         'L40_37': [],
         'L40_38': [],
         'L40_39': [],
         'L40_4': [],
         'L40_5': [],
         'L40_6': [],
         'L40_7': [],
         'L40_8': [],
         'L40_9': [],
         'L5_0': ['L10_0', 'L10_3'],
         'L5_1': ['L10_4', 'L10_5', 'L10_6', 'L10_8'],
         'L5_2': ['L10_9'],
         'L5_3': ['L10_2'],
         'L5_4': ['L10_1', 'L10_7']})

I'm using code I found on another stackoverflow answer but it gives me an error when I try it with my dictionary.

Code:

# Load required modules
import networkx as nx
import matplotlib.pyplot as plt
from scipy.cluster.hierarchy import dendrogram

# Construct the graph/hierarchy
G           = nx.DiGraph(d)
nodes       = G.nodes()
leaves      = set( n for n in nodes if G.out_degree(n) == 0 )
inner_nodes = [ n for n in nodes if G.out_degree(n) > 0 ]

# Compute the size of each subtree
subtree = dict( (n, [n]) for n in leaves )
for u in inner_nodes:
    children = set()
    node_list = list(d[u])
    while len(node_list) > 0:
        v = node_list.pop(0)
        children.add( v )
        node_list += d[v]

    subtree[u] = sorted(children & leaves)

inner_nodes.sort(key=lambda n: len(subtree[n])) # <-- order inner nodes     ascending by subtree size, root is last

# Construct the linkage matrix
leaves = sorted(leaves)
index  = dict( (tuple([n]), i) for i, n in enumerate(leaves) )
Z = []
k = len(leaves)
for i, n in enumerate(inner_nodes):
    children = d[n]
    x = children[0]
    for y in children[1:]:
        z = tuple(subtree[x] + subtree[y])
        i, j = index[tuple(subtree[x])], index[tuple(subtree[y])]
        Z.append([i, j, float(len(subtree[n])), len(z)]) # <-- float is   required by the dendrogram function
        print 'Z', Z
        index[z] = k
        subtree[z] = list(z)
        x = z
        k += 1

# Visualize
dendrogram(Z, labels=leaves)
plt.show()

When I run it I get the following error: KeyError: ('L40_13', 'L40_5')

Can anyone help? Thanks

Whole traceback:

KeyError                                  Traceback (most recent call last)
<ipython-input-105-ed0150b8f170> in <module>()
     43         z = tuple(subtree[x] + subtree[y])
     44         print 'z',z
---> 45         i, j = index[tuple(subtree[x])], index[tuple(subtree[y])]
     46         print 'i',i, 'j',j
     47         Z.append([i, j, float(len(subtree[n])), len(z)]) # <-- float is required by the dendrogram function

KeyError: ('L40_13', 'L40_5')
2
  • Hi! Could you post the whole traceback? Commented Jan 15, 2017 at 9:30
  • Hi, I just did. Thanks Commented Jan 15, 2017 at 9:35

1 Answer 1

1

I was able to make this work for my dictionary by adding a sort function to this line:

z = tuple(subtree[x] + subtree[y])

to create

z = tuple(sorted(subtree[x] + subtree[y]))

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.