Matrix Science header

tools_treecluster.py

Using the matrix_science::ms_treecluster class.

#!/usr/bin/python
##############################################################################
# file: tools_treecluster.py                                                       #
# 'msparser' toolkit example code                                            #
##############################################################################
# COPYRIGHT NOTICE                                                           #
# Copyright 1998-2010 Matrix Science Limited  All Rights Reserved.           #
#                                                                            #
##############################################################################
#     $Source: /vol/cvsroot/parser/examples/test_python/tools_treecluster.py,v $   #
#     $Author: neilr $                                                      #
#       $Date: 2016/03/17 14:53:27 $                                         #
#   $Revision: 1.2 $                                                         #
# $NoKeywords::                                                            $ #
##############################################################################

import msparser

rows = 11
cols = 5
tc = msparser.ms_treecluster(rows, cols)

data = msparser.vectord()
mask = msparser.vectori()

data.append(1.623866862); data.append(-0.052894948); data.append(1.182692298); data.append(2.298658316); data.append(1.13422094)
mask.append(1);           mask.append(1);            mask.append(1);           mask.append(1);           mask.append(1)
tc.setRow(0 , data, mask); data.clear(); mask.clear()

data.append(1.156396617); data.append(0.0000000000); data.append(0.521050737); data.append(1.544979883); data.append(0.65718266)
mask.append(1);           mask.append(0);            mask.append(1);           mask.append(1);           mask.append(1)
tc.setRow(1 , data, mask); data.clear(); mask.clear()

data.append(1.523561956); data.append(-0.017417053); data.append(1.168000125); data.append(2.459693903); data.append(1.308011315)
mask.append(1);           mask.append(1);            mask.append(1);           mask.append(1);           mask.append(1)
tc.setRow(2 , data, mask); data.clear(); mask.clear()

data.append(1.55875743);  data.append(-0.241270432); data.append(0.440420721); data.append(2.427337989); data.append(1.043344505)
mask.append(1);           mask.append(1);            mask.append(1);           mask.append(1);           mask.append(1)
tc.setRow(3 , data, mask); data.clear(); mask.clear()
 
data.append(1.449957484); data.append(-0.169744676); data.append(0.867896464); data.append(2.418999465); data.append(1.171206827)
mask.append(1);           mask.append(1);            mask.append(1);           mask.append(1);           mask.append(1) 
tc.setRow(4 , data, mask); data.clear(); mask.clear()

data.append(1.171206827); data.append(0.0000000000); data.append(0.854394678); data.append(2.075532631); data.append(0.950095094)
mask.append(1);           mask.append(0);            mask.append(1);           mask.append(1);           mask.append(1) 
tc.setRow(5 , data, mask); data.clear(); mask.clear()

data.append(1.361768359); data.append(-0.120294234); data.append(0.992043276); data.append(2.238786860); data.append(1.090175950)
mask.append(1);           mask.append(1);            mask.append(1);           mask.append(1);           mask.append(1) 
tc.setRow(6 , data, mask); data.clear(); mask.clear()

data.append(1.781149852); data.append(0.0028825090); data.append(1.079975377); data.append(2.464929601); data.append(1.301002256)
mask.append(1);           mask.append(1);            mask.append(1);           mask.append(1);           mask.append(1) 
tc.setRow(7 , data, mask); data.clear(); mask.clear()

data.append(-1.227692025); data.append(-3.522840789);data.append(-2.434402824);data.append(-0.873027144);data.append(-1.977099598)
mask.append(1);           mask.append(1);            mask.append(1);           mask.append(1);           mask.append(1) 
tc.setRow(8 , data, mask); data.clear(); mask.clear()

data.append(1.272023189); data.append(-0.535331733); data.append(0.608809243); data.append(2.004681156); data.append(0.826192536)
mask.append(1);           mask.append(1);            mask.append(1);           mask.append(1);           mask.append(1) 
tc.setRow(9 , data, mask); data.clear(); mask.clear()

data.append(1.069014678); data.append(-0.623709617); data.append(0.412510571); data.append(1.82822536);  data.append(0.650764559)
mask.append(1);           mask.append(1);            mask.append(1);           mask.append(1);           mask.append(1) 
tc.setRow(10 , data, mask); data.clear(); mask.clear()

weights = msparser.vectord()
left = msparser.vectori()
right = msparser.vectori()
distance = msparser.vectord()

if not tc.cluster(msparser.ms_treecluster.TCD_EUCLIDEAN, msparser.ms_mascotresults.TCM_PAIRWISE_AVERAGE, weights, left, right, distance):
    print("Failed")
else:
    print("Node\tleft\tright\tdistance\n")
    for i in range(0, len(left)):
        node = -1 - i 
        print("%d \t %d \t %d \t %.16f " % (node, left[i], right[i], distance[i]))
        
"""
tools_treecluster.py
Will give the following output: 

Node    left    right   distance

-1       6       4       0.0129355369880599
-2       2       0       0.0135342339743674
-3       7       -2      0.0168450469179865
-4       5       9       0.0227119800299183
-5       10      1       0.0249214407900277
-6       -1      -3      0.0358259860933716
-7       -4      -5      0.0759763042954681
-8       3       -6      0.0985921612204670
-9       -7      -8      0.2259074963919064
-10      8       -9      9.2461259564223610
"""
        
Copyright © 2016 Matrix Science Ltd.  All Rights Reserved. Generated on Fri Jun 2 2017 01:44:50