[SciPy-User] Convert linkage matrix to tree (scipy.cluster.hierarchy.to_tree, python3)

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[SciPy-User] Convert linkage matrix to tree (scipy.cluster.hierarchy.to_tree, python3)

Robert Winkler

Hi,

I want to translate a linkage matrix to a tree (to convert it in further steps to newick format). The linkage matrix looks like this:

[[ 0.  1.  1.  2.]
[ 2.  4.  4.  3.]
[ 3.  5.  6.  4.]]

When I run the code ('from ete3 import Tree'):

T = scipy.cluster.hierarchy.to_tree(Z)
root = Tree()
root.dist = 0
root.name = "root"
item2node = {T: root}

the program terminates at item2node with the error message:

Exception "unhandled TypeError"
unhashable type: 'ClusterNode'

Any ideas?

Robert


The data, I run

Y = scipy.cluster.hierarchy.linkage(refspec_array,  'complete')
print(Y)
T = scipy.cluster.hierarchy.to_tree(Y)

look like this:

[ ('1', 0.0, 0.0, 0.0, 0.0, 4.38755898755899, 0.0, 0.0, 11.4813953488372, 0.0, 0.0, 0.0, 0.0, 0.0, 3.37222222222222, 17.453347427766, 0.0, 0.0, 0.0, 6.10327695560254, 0.0, 0.0, 0.0, 12.4255813953488, 0.0, 5.1536069904491, 0.0, 0.0, 15.016330162086, 3.16285714285714, 3.83170731707317, 0.0, 18.7087586831773, 7.88292682926829, 45.4125613035912, 10.4775101513474, 12.0366102889359, 0.0, 0.0, 0.0, 3.2839463523674, 14.2130258077626, 70.5372093023256, 13.6720930232558, 19.715657343224, 4.91362382790954, 7.00714285714286, 0.0, 3.6776026026026, 0.0, 15.3790697674419, 4.15201131363922, 56.9372093023256, 0.0, 0.0, 0.0, 22.5604743447767, 0.0, 8.04212974296206, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 19.002380952381, 4.03296703296703, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 4.67, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
 ('2', 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 3.27097625891554, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 3.80990740740741, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 21.6565696834911, 6.580014781966, 19.9112912622229, 0.0, 0.0, 0.0, 0.0, 5.2982335122579, 52.672519005238, 25.1761382113821, 58.6225154191197, 6.53954083820578, 5.0235253034919, 0.0, 5.59207185091331, 6.8450632459782, 41.9248475609756, 8.94831608491355, 17.9927195135152, 3.56784893267652, 0.0, 0.0, 14.6637340301974, 10.2320372068382, 87.4304539295393, 17.6128532713899, 3.61942687747036, 4.9223667570009, 21.7575067583061, 4.87, 10.0497729362015, 7.4825, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)..]


_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Convert linkage matrix to tree (scipy.cluster.hierarchy.to_tree, python3)

Warren Weckesser-2


On Mon, Jun 26, 2017 at 6:23 PM, Robert Winkler <[hidden email]> wrote:

Hi,

I want to translate a linkage matrix to a tree (to convert it in further steps to newick format). The linkage matrix looks like this:

[[ 0.  1.  1.  2.]
[ 2.  4.  4.  3.]
[ 3.  5.  6.  4.]]

When I run the code ('from ete3 import Tree'):

T = scipy.cluster.hierarchy.to_tree(Z)
root = Tree()
root.dist = 0
root.name = "root"
item2node = {T: root}

the program terminates at item2node with the error message:

Exception "unhandled TypeError"
unhashable type: 'ClusterNode'

Any ideas?

Robert



I don't know what is causing the unhandled exception, but the second part of that error message explains the problem (well, in Python-speak, anyway): you can't use T as the key of a dictionary because T is "unhashable".    Here's another example of the error:

In [13]: y = np.random.randint(0, 5, size=(4, 3))

In [14]: Z = linkage(y)

In [15]: T = to_tree(Z)

In [16]: d = {T: 0}  # Try to create a dictionary with T as a key
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-16-bbf38b1398c4> in <module>()
----> 1 d = {T: 0}

TypeError: unhashable type: 'ClusterNode'



You'll also get that error if you try hash(T) (where hash is the builtin Python function):

In [22]: hash(T)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-22-a86c9d8e8238> in <module>()
----> 1 hash(T)

TypeError: unhashable type: 'ClusterNode'



Warren


 

The data, I run

Y = scipy.cluster.hierarchy.linkage(refspec_array,  'complete')
print(Y)
T = scipy.cluster.hierarchy.to_tree(Y)

look like this:

[ ('1', 0.0, 0.0, 0.0, 0.0, 4.38755898755899, 0.0, 0.0, 11.4813953488372, 0.0, 0.0, 0.0, 0.0, 0.0, 3.37222222222222, 17.453347427766, 0.0, 0.0, 0.0, 6.10327695560254, 0.0, 0.0, 0.0, 12.4255813953488, 0.0, 5.1536069904491, 0.0, 0.0, 15.016330162086, 3.16285714285714, 3.83170731707317, 0.0, 18.7087586831773, 7.88292682926829, 45.4125613035912, 10.4775101513474, 12.0366102889359, 0.0, 0.0, 0.0, 3.2839463523674, 14.2130258077626, 70.5372093023256, 13.6720930232558, 19.715657343224, 4.91362382790954, 7.00714285714286, 0.0, 3.6776026026026, 0.0, 15.3790697674419, 4.15201131363922, 56.9372093023256, 0.0, 0.0, 0.0, 22.5604743447767, 0.0, 8.04212974296206, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 19.002380952381, 4.03296703296703, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 4.67, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
 ('2', 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 3.27097625891554, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 3.80990740740741, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 21.6565696834911, 6.580014781966, 19.9112912622229, 0.0, 0.0, 0.0, 0.0, 5.2982335122579, 52.672519005238, 25.1761382113821, 58.6225154191197, 6.53954083820578, 5.0235253034919, 0.0, 5.59207185091331, 6.8450632459782, 41.9248475609756, 8.94831608491355, 17.9927195135152, 3.56784893267652, 0.0, 0.0, 14.6637340301974, 10.2320372068382, 87.4304539295393, 17.6128532713899, 3.61942687747036, 4.9223667570009, 21.7575067583061, 4.87, 10.0497729362015, 7.4825, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)..]


_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user



_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user