Back to index

python-biopython  1.60
Public Member Functions | Static Public Attributes
test_Cluster.TestCluster Class Reference
Collaboration diagram for test_Cluster.TestCluster:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def test_median_mean
def test_matrix_parse
def test_kcluster
def test_clusterdistance
def test_treecluster
def test_somcluster
def test_distancematrix_kmedoids
def test_pca

Static Public Attributes

string module = 'Bio.Cluster'

Detailed Description

Definition at line 22 of file test_Cluster.py.


Member Function Documentation

Definition at line 184 of file test_Cluster.py.

00184 
00185     def test_clusterdistance(self):
00186         if TestCluster.module=='Bio.Cluster':
00187             from Bio.Cluster import clusterdistance
00188         elif TestCluster.module=='Pycluster':
00189             from Pycluster import clusterdistance
00190 
00191         # First data set
00192         weight = numpy.array([ 1,1,1,1,1 ])
00193         data   = numpy.array([[  1.1, 2.2, 3.3, 4.4, 5.5, ], 
00194                               [  3.1, 3.2, 1.3, 2.4, 1.5, ], 
00195                               [  4.1, 2.2, 0.3, 5.4, 0.5, ], 
00196                               [ 12.1, 2.0, 0.0, 5.0, 0.0, ]])
00197         mask   = numpy.array([[ 1, 1, 1, 1, 1], 
00198                               [ 1, 1, 1, 1, 1], 
00199                               [ 1, 1, 1, 1, 1], 
00200                               [ 1, 1, 1, 1, 1]], int)
00201 
00202         # Cluster assignments
00203         c1 = [0]
00204         c2 = [1,2]
00205         c3 = [3]
00206 
00207         distance = clusterdistance(data, mask=mask, weight=weight,
00208                                    index1=c1, index2=c2, dist='e',
00209                                    method='a', transpose=0);
00210         self.assertAlmostEqual(distance, 6.650, places=3)
00211         distance = clusterdistance(data, mask=mask, weight=weight,
00212                                    index1=c1, index2=c3, dist='e',
00213                                    method='a', transpose=0);
00214         self.assertAlmostEqual(distance, 32.508, places=3)
00215         distance = clusterdistance(data, mask=mask, weight=weight,
00216                                    index1=c2, index2=c3, dist='e',
00217                                    method='a', transpose=0);
00218         self.assertAlmostEqual(distance, 15.118, places=3)
00219 
00220         # Second data set
00221         weight =  numpy.array([ 1,1 ])
00222         data   =  numpy.array([[ 1.1, 1.2 ],
00223                          [ 1.4, 1.3 ],
00224                          [ 1.1, 1.5 ],
00225                          [ 2.0, 1.5 ],
00226                          [ 1.7, 1.9 ],
00227                          [ 1.7, 1.9 ],
00228                          [ 5.7, 5.9 ],
00229                          [ 5.7, 5.9 ],
00230                          [ 3.1, 3.3 ],
00231                          [ 5.4, 5.3 ],
00232                          [ 5.1, 5.5 ],
00233                          [ 5.0, 5.5 ],
00234                          [ 5.1, 5.2 ]])
00235         mask = numpy.array([[ 1, 1 ],
00236                             [ 1, 1 ],
00237                             [ 1, 1 ],
00238                             [ 1, 1 ],
00239                             [ 1, 1 ],
00240                             [ 1, 1 ],
00241                             [ 1, 1 ],
00242                             [ 1, 1 ],
00243                             [ 1, 1 ],
00244                             [ 1, 1 ],
00245                             [ 1, 1 ],
00246                             [ 1, 1 ],
00247                             [ 1, 1 ]], int)
00248 
00249         # Cluster assignments
00250         c1 = [ 0, 1, 2, 3 ]
00251         c2 = [ 4, 5, 6, 7 ]
00252         c3 = [ 8 ]
00253 
00254         distance = clusterdistance(data, mask=mask, weight=weight,
00255                                    index1=c1, index2=c2, dist='e',
00256                                    method='a', transpose=0);
00257         self.assertAlmostEqual(distance, 5.833, places=3)
00258         distance = clusterdistance(data, mask=mask, weight=weight,
00259                                    index1=c1, index2=c3, dist='e',
00260                                    method='a', transpose=0);
00261         self.assertAlmostEqual(distance, 3.298, places=3)
00262         distance = clusterdistance(data, mask=mask, weight=weight,
00263                                    index1=c2, index2=c3, dist='e',
00264                                    method='a', transpose=0);
00265         self.assertAlmostEqual(distance, 0.360, places=3)
00266 

Here is the call graph for this function:

Definition at line 593 of file test_Cluster.py.

00593 
00594     def test_distancematrix_kmedoids(self):
00595         if TestCluster.module=='Bio.Cluster':
00596             from Bio.Cluster import distancematrix, kmedoids
00597         elif TestCluster.module=='Pycluster':
00598             from Pycluster import distancematrix, kmedoids
00599 
00600         data = numpy.array([[2.2, 3.3, 4.4],
00601                             [2.1, 1.4, 5.6],
00602                             [7.8, 9.0, 1.2],
00603                             [4.5, 2.3, 1.5],
00604                             [4.2, 2.4, 1.9],
00605                             [3.6, 3.1, 9.3],
00606                             [2.3, 1.2, 3.9],
00607                             [4.2, 9.6, 9.3],
00608                             [1.7, 8.9, 1.1]])
00609         mask = numpy.array([[1, 1, 1],
00610                             [1, 1, 1],
00611                             [0, 1, 1],
00612                             [1, 1, 1],
00613                             [1, 1, 1],
00614                             [0, 1, 0],
00615                             [1, 1, 1],
00616                             [1, 0, 1],
00617                             [1, 1, 1]], int)
00618         weight = numpy.array([2.0, 1.0, 0.5])
00619         matrix = distancematrix(data, mask=mask, weight=weight)
00620 
00621         self.assertAlmostEqual(matrix[1][0], 1.243, places=3)
00622 
00623         self.assertAlmostEqual(matrix[2][0], 25.073, places=3)
00624         self.assertAlmostEqual(matrix[2][1], 44.960, places=3)
00625 
00626         self.assertAlmostEqual(matrix[3][0], 4.510, places=3)
00627         self.assertAlmostEqual(matrix[3][1], 5.924, places=3)
00628         self.assertAlmostEqual(matrix[3][2], 29.957, places=3)
00629 
00630         self.assertAlmostEqual(matrix[4][0], 3.410, places=3)
00631         self.assertAlmostEqual(matrix[4][1], 4.761, places=3)
00632         self.assertAlmostEqual(matrix[4][2], 29.203, places=3)
00633         self.assertAlmostEqual(matrix[4][3], 0.077, places=3)
00634 
00635         self.assertAlmostEqual(matrix[5][0], 0.040, places=3)
00636         self.assertAlmostEqual(matrix[5][1], 2.890, places=3)
00637         self.assertAlmostEqual(matrix[5][2], 34.810, places=3)
00638         self.assertAlmostEqual(matrix[5][3], 0.640, places=3)
00639         self.assertAlmostEqual(matrix[5][4], 0.490, places=3)
00640 
00641         self.assertAlmostEqual(matrix[6][0], 1.301, places=3)
00642         self.assertAlmostEqual(matrix[6][1], 0.447, places=3)
00643         self.assertAlmostEqual(matrix[6][2], 42.990, places=3)
00644         self.assertAlmostEqual(matrix[6][3], 3.934, places=3)
00645         self.assertAlmostEqual(matrix[6][4], 3.046, places=3)
00646         self.assertAlmostEqual(matrix[6][5], 3.610, places=3)
00647 
00648         self.assertAlmostEqual(matrix[7][0], 8.002, places=3)
00649         self.assertAlmostEqual(matrix[7][1], 6.266, places=3)
00650         self.assertAlmostEqual(matrix[7][2], 65.610, places=3)
00651         self.assertAlmostEqual(matrix[7][3], 12.240, places=3)
00652         self.assertAlmostEqual(matrix[7][4], 10.952, places=3)
00653         self.assertAlmostEqual(matrix[7][5], 0.000, places=3)
00654         self.assertAlmostEqual(matrix[7][6], 8.720, places=3)
00655 
00656         self.assertAlmostEqual(matrix[8][0], 10.659, places=3)
00657         self.assertAlmostEqual(matrix[8][1], 19.056, places=3)
00658         self.assertAlmostEqual(matrix[8][2], 0.010, places=3)
00659         self.assertAlmostEqual(matrix[8][3], 16.949, places=3)
00660         self.assertAlmostEqual(matrix[8][4], 15.734, places=3)
00661         self.assertAlmostEqual(matrix[8][5], 33.640, places=3)
00662         self.assertAlmostEqual(matrix[8][6], 18.266, places=3)
00663         self.assertAlmostEqual(matrix[8][7], 18.448, places=3)
00664         clusterid, error, nfound = kmedoids(matrix, npass=1000)
00665         self.assertEqual(clusterid[0], 5)
00666         self.assertEqual(clusterid[1], 5)
00667         self.assertEqual(clusterid[2], 2)
00668         self.assertEqual(clusterid[3], 5)
00669         self.assertEqual(clusterid[4], 5)
00670         self.assertEqual(clusterid[5], 5)
00671         self.assertEqual(clusterid[6], 5)
00672         self.assertEqual(clusterid[7], 5)
00673         self.assertEqual(clusterid[8], 2)
00674         self.assertAlmostEqual(error, 7.680, places=3)

Here is the call graph for this function:

Definition at line 116 of file test_Cluster.py.

00116 
00117     def test_kcluster(self):
00118         if TestCluster.module=='Bio.Cluster':
00119             from Bio.Cluster import kcluster
00120         elif TestCluster.module=='Pycluster':
00121             from Pycluster import kcluster
00122 
00123         nclusters = 3
00124         # First data set
00125         weight = numpy.array([1,1,1,1,1])
00126         data   = numpy.array([[ 1.1, 2.2, 3.3, 4.4, 5.5],
00127                               [ 3.1, 3.2, 1.3, 2.4, 1.5], 
00128                               [ 4.1, 2.2, 0.3, 5.4, 0.5], 
00129                               [12.1, 2.0, 0.0, 5.0, 0.0]]) 
00130         mask =  numpy.array([[ 1, 1, 1, 1, 1], 
00131                              [ 1, 1, 1, 1, 1], 
00132                              [ 1, 1, 1, 1, 1], 
00133                              [ 1, 1, 1, 1, 1]], int) 
00134       
00135         clusterid, error, nfound = kcluster(data, nclusters=nclusters,
00136                                             mask=mask, weight=weight,
00137                                             transpose=0, npass=100,
00138                                             method='a', dist='e')
00139         self.assertEqual(len(clusterid), len(data))
00140 
00141         correct = [0,1,1,2]
00142         mapping = [clusterid[correct.index(i)] for i in range(nclusters)]
00143         for i in range(len(clusterid)):
00144             self.assertEqual(clusterid[i], mapping[correct[i]])
00145       
00146         # Second data set
00147         weight = numpy.array([1,1])
00148         data = numpy.array([[ 1.1, 1.2 ],
00149                       [ 1.4, 1.3 ],
00150                       [ 1.1, 1.5 ],
00151                       [ 2.0, 1.5 ],
00152                       [ 1.7, 1.9 ],
00153                       [ 1.7, 1.9 ],
00154                       [ 5.7, 5.9 ],
00155                       [ 5.7, 5.9 ],
00156                       [ 3.1, 3.3 ],
00157                       [ 5.4, 5.3 ],
00158                       [ 5.1, 5.5 ],
00159                       [ 5.0, 5.5 ],
00160                       [ 5.1, 5.2 ]])
00161         mask = numpy.array([[ 1, 1 ],
00162                             [ 1, 1 ],
00163                             [ 1, 1 ],
00164                             [ 1, 1 ],
00165                             [ 1, 1 ],
00166                             [ 1, 1 ],
00167                             [ 1, 1 ],
00168                             [ 1, 1 ],
00169                             [ 1, 1 ],
00170                             [ 1, 1 ],
00171                             [ 1, 1 ],
00172                             [ 1, 1 ],
00173                             [ 1, 1 ]], int)
00174 
00175         clusterid, error, nfound = kcluster(data, nclusters=3, mask=mask,
00176                                             weight=weight, transpose=0,
00177                                             npass=100, method='a', dist='e')
00178         self.assertEqual(len(clusterid), len(data))
00179 
00180         correct = [0, 0, 0, 0, 0, 0, 1, 1, 2, 1, 1, 1, 1]
00181         mapping = [clusterid[correct.index(i)] for i in range(nclusters)]
00182         for i in range(len(clusterid)):
00183             self.assertEqual(clusterid[i], mapping[correct[i]])

Here is the call graph for this function:

Definition at line 49 of file test_Cluster.py.

00049 
00050     def test_matrix_parse(self):
00051         if TestCluster.module=='Bio.Cluster':
00052             from Bio.Cluster import treecluster
00053         elif TestCluster.module=='Pycluster':
00054             from Pycluster import treecluster
00055 
00056         # Normal matrix, no errors
00057         data1 = numpy.array([[ 1.1, 1.2 ],
00058                              [ 1.4, 1.3 ],
00059                              [ 1.1, 1.5 ],
00060                              [ 2.0, 1.5 ],
00061                              [ 1.7, 1.9 ],
00062                              [ 1.7, 1.9 ],
00063                              [ 5.7, 5.9 ],
00064                              [ 5.7, 5.9 ],
00065                              [ 3.1, 3.3 ],
00066                              [ 5.4, 5.3 ],
00067                              [ 5.1, 5.5 ],
00068                              [ 5.0, 5.5 ],
00069                              [ 5.1, 5.2 ]])
00070       
00071         # Another normal matrix, no errors; written as a list
00072         data2 =  [[  1.1, 2.2, 3.3, 4.4, 5.5 ], 
00073                   [  3.1, 3.2, 1.3, 2.4, 1.5 ], 
00074                   [  4.1, 2.2, 0.3, 5.4, 0.5 ], 
00075                   [ 12.1, 2.0, 0.0, 5.0, 0.0 ]]
00076       
00077         # Ragged matrix
00078         data3 =  [[ 91.1, 92.2, 93.3, 94.4, 95.5], 
00079                   [ 93.1, 93.2, 91.3, 92.4 ], 
00080                   [ 94.1, 92.2, 90.3 ], 
00081                   [ 12.1, 92.0, 90.0, 95.0, 90.0 ]]
00082       
00083         # Matrix with bad cells
00084         data4 =  [ [ 7.1, 7.2, 7.3, 7.4, 7.5, ],
00085                    [ 7.1, 7.2, 7.3, 7.4, 'snoopy' ], 
00086                    [ 7.1, 7.2, 7.3, None, None]] 
00087 
00088         # Matrix with a bad row
00089         data5 =  [ [ 23.1, 23.2, 23.3, 23.4, 23.5], 
00090                    None,
00091                    [ 23.1, 23.0, 23.0, 23.0, 23.0]]
00092 
00093         # Various references that don't point to matrices at all
00094         data6 = "snoopy"
00095         data7 = {'a': [[2.3,1.2],[3.3,5.6]]}
00096         data8 = []
00097         data9 = [None]
00098       
00099         try:
00100             treecluster(data1)
00101         except:
00102             self.fail("treecluster failed to accept matrix data1")
00103 
00104         try:
00105             treecluster(data2)
00106         except:
00107             self.fail("treecluster failed to accept matrix data2")
00108 
00109         self.assertRaises(TypeError, lambda : treecluster(data3))
00110         self.assertRaises(TypeError, lambda : treecluster(data4))
00111         self.assertRaises(TypeError, lambda : treecluster(data5))
00112         self.assertRaises(TypeError, lambda : treecluster(data6))
00113         self.assertRaises(TypeError, lambda : treecluster(data7))
00114         self.assertRaises(TypeError, lambda : treecluster(data8))
00115         self.assertRaises(TypeError, lambda : treecluster(data9))

Here is the call graph for this function:

Definition at line 26 of file test_Cluster.py.

00026 
00027     def test_median_mean(self):
00028         if TestCluster.module=='Bio.Cluster':
00029             from Bio.Cluster import mean, median
00030         elif TestCluster.module=='Pycluster':
00031             from Pycluster import mean, median
00032 
00033         data = numpy.array([ 34.3, 3, 2 ])
00034         self.assertAlmostEqual(mean(data), 13.1, places=3)
00035         self.assertAlmostEqual(median(data), 3.0, places=3)
00036 
00037         data = [ 5, 10, 15, 20]
00038         self.assertAlmostEqual(mean(data), 12.5, places=3)
00039         self.assertAlmostEqual(median(data), 12.5, places=3)
00040 
00041         data = [ 1, 2, 3, 5, 7, 11, 13, 17]
00042         self.assertAlmostEqual(mean(data), 7.375, places=3)
00043         self.assertAlmostEqual(median(data), 6.0, places=3)
00044 
00045         data = [ 100, 19, 3, 1.5, 1.4, 1, 1, 1]
00046         self.assertAlmostEqual(mean(data), 15.988, places=3)
00047         self.assertAlmostEqual(median(data), 1.45, places=3)
00048       

Here is the call graph for this function:

Definition at line 675 of file test_Cluster.py.

00675 
00676     def test_pca(self):
00677         if TestCluster.module=='Bio.Cluster':
00678             from Bio.Cluster import pca
00679         elif TestCluster.module=='Pycluster':
00680             from Pycluster import pca
00681 
00682         data = numpy.array([[ 3.1, 1.2 ],
00683                             [ 1.4, 1.3 ],
00684                             [ 1.1, 1.5 ],
00685                             [ 2.0, 1.5 ],
00686                             [ 1.7, 1.9 ],
00687                             [ 1.7, 1.9 ],
00688                             [ 5.7, 5.9 ],
00689                             [ 5.7, 5.9 ],
00690                             [ 3.1, 3.3 ],
00691                             [ 5.4, 5.3 ],
00692                             [ 5.1, 5.5 ],
00693                             [ 5.0, 5.5 ],
00694                             [ 5.1, 5.2 ],
00695                            ])
00696 
00697         mean, coordinates, pc, eigenvalues =  pca(data)
00698         self.assertAlmostEqual(mean[0], 3.5461538461538464)
00699         self.assertAlmostEqual(mean[1], 3.5307692307692311)
00700         self.assertAlmostEqual(coordinates[0,0],  2.0323189722653883)
00701         self.assertAlmostEqual(coordinates[0,1],  1.2252420399694917)
00702         self.assertAlmostEqual(coordinates[1,0],  3.0936985166252251)
00703         self.assertAlmostEqual(coordinates[1,1], -0.10647619705157851)
00704         self.assertAlmostEqual(coordinates[2,0],  3.1453186907749426)
00705         self.assertAlmostEqual(coordinates[2,1], -0.46331699855941139)
00706         self.assertAlmostEqual(coordinates[3,0],  2.5440202962223761)
00707         self.assertAlmostEqual(coordinates[3,1],  0.20633980959571077)
00708         self.assertAlmostEqual(coordinates[4,0],  2.4468278463376221)
00709         self.assertAlmostEqual(coordinates[4,1], -0.28412285736824866)
00710         self.assertAlmostEqual(coordinates[5,0],  2.4468278463376221)
00711         self.assertAlmostEqual(coordinates[5,1], -0.28412285736824866)
00712         self.assertAlmostEqual(coordinates[6,0], -3.2018619434743254)
00713         self.assertAlmostEqual(coordinates[6,1],  0.019692314198662915)
00714         self.assertAlmostEqual(coordinates[7,0], -3.2018619434743254)
00715         self.assertAlmostEqual(coordinates[7,1],  0.019692314198662915)
00716         self.assertAlmostEqual(coordinates[8,0],  0.46978641990344067)
00717         self.assertAlmostEqual(coordinates[8,1], -0.17778754731982949)
00718         self.assertAlmostEqual(coordinates[9,0], -2.5549912731867215)
00719         self.assertAlmostEqual(coordinates[9,1],  0.19733897451533403)
00720         self.assertAlmostEqual(coordinates[10,0], -2.5033710990370044)
00721         self.assertAlmostEqual(coordinates[10,1], -0.15950182699250004)
00722         self.assertAlmostEqual(coordinates[11,0], -2.4365601663089413)
00723         self.assertAlmostEqual(coordinates[11,1], -0.23390813900973562)
00724         self.assertAlmostEqual(coordinates[12,0], -2.2801521629852974)
00725         self.assertAlmostEqual(coordinates[12,1],  0.0409309711916888)
00726         self.assertAlmostEqual(pc[0,0], -0.66810932728062988)
00727         self.assertAlmostEqual(pc[0,1], -0.74406312017235743)
00728         self.assertAlmostEqual(pc[1,0],  0.74406312017235743)
00729         self.assertAlmostEqual(pc[1,1], -0.66810932728062988)
00730         self.assertAlmostEqual(eigenvalues[0], 9.3110471246032844)
00731         self.assertAlmostEqual(eigenvalues[1], 1.4437456297481428)
00732 
00733         data = numpy.array([[ 2.3, 4.5, 1.2, 6.7, 5.3, 7.1],
00734                             [ 1.3, 6.5, 2.2, 5.7, 6.2, 9.1],
00735                             [ 3.2, 7.2, 3.2, 7.4, 7.3, 8.9],
00736                             [ 4.2, 5.2, 9.2, 4.4, 6.3, 7.2]])
00737         mean, coordinates, pc, eigenvalues =  pca(data)
00738         self.assertAlmostEqual(mean[0], 2.7500)
00739         self.assertAlmostEqual(mean[1], 5.8500)
00740         self.assertAlmostEqual(mean[2], 3.9500)
00741         self.assertAlmostEqual(mean[3], 6.0500)
00742         self.assertAlmostEqual(mean[4], 6.2750)
00743         self.assertAlmostEqual(mean[5], 8.0750)
00744         self.assertAlmostEqual(coordinates[0,0],  2.6460846688406905)
00745         self.assertAlmostEqual(coordinates[0,1], -2.1421701432732418)
00746         self.assertAlmostEqual(coordinates[0,2], -0.56620932754145858)
00747         self.assertAlmostEqual(coordinates[0,3],  0.0)
00748         self.assertAlmostEqual(coordinates[1,0],  2.0644120899917544)
00749         self.assertAlmostEqual(coordinates[1,1],  0.55542108669180323)
00750         self.assertAlmostEqual(coordinates[1,2],  1.4818772348457117)
00751         self.assertAlmostEqual(coordinates[1,3],  0.0)
00752         self.assertAlmostEqual(coordinates[2,0],  1.0686641862092987)
00753         self.assertAlmostEqual(coordinates[2,1],  1.9994412069101073)
00754         self.assertAlmostEqual(coordinates[2,2], -1.000720598980291)
00755         self.assertAlmostEqual(coordinates[2,3],  0.0)
00756         self.assertAlmostEqual(coordinates[3,0], -5.77916094504174)
00757         self.assertAlmostEqual(coordinates[3,1], -0.41269215032867046)
00758         self.assertAlmostEqual(coordinates[3,2],  0.085052691676038017)
00759         self.assertAlmostEqual(coordinates[3,3],  0.0)
00760         self.assertAlmostEqual(pc[0,0], -0.26379660005997291)
00761         self.assertAlmostEqual(pc[0,1],  0.064814972617134495)
00762         self.assertAlmostEqual(pc[0,2], -0.91763310094893846)
00763         self.assertAlmostEqual(pc[0,3],  0.26145408875373249)
00764         self.assertAlmostEqual(pc[1,0],  0.05073770520434398)
00765         self.assertAlmostEqual(pc[1,1],  0.68616983388698793)
00766         self.assertAlmostEqual(pc[1,2],  0.13819106187213354)
00767         self.assertAlmostEqual(pc[1,3],  0.19782544121828985)
00768         self.assertAlmostEqual(pc[2,0], -0.63000893660095947)
00769         self.assertAlmostEqual(pc[2,1],  0.091155993862151397)
00770         self.assertAlmostEqual(pc[2,2],  0.045630391256086845)
00771         self.assertAlmostEqual(pc[2,3], -0.67456694780914772)
00772         # As the last eigenvalue is zero, the corresponding eigenvector is
00773         # strongly affected by roundoff error, and is not being tested here.
00774         # For PCA, this doesn't matter since all data have a zero coefficient
00775         # along this eigenvector.
00776         self.assertAlmostEqual(eigenvalues[0], 6.7678878332578778)
00777         self.assertAlmostEqual(eigenvalues[1], 3.0108911400291856)
00778         self.assertAlmostEqual(eigenvalues[2], 1.8775592718563467)
00779         self.assertAlmostEqual(eigenvalues[3], 0.0)

Here is the call graph for this function:

Definition at line 535 of file test_Cluster.py.

00535 
00536     def test_somcluster(self):
00537         if TestCluster.module=='Bio.Cluster':
00538             from Bio.Cluster import somcluster
00539         elif TestCluster.module=='Pycluster':
00540             from Pycluster import somcluster
00541 
00542         # First data set
00543         weight = [ 1,1,1,1,1 ]
00544         data = numpy.array([[  1.1, 2.2, 3.3, 4.4, 5.5], 
00545                             [  3.1, 3.2, 1.3, 2.4, 1.5], 
00546                             [  4.1, 2.2, 0.3, 5.4, 0.5], 
00547                             [ 12.1, 2.0, 0.0, 5.0, 0.0]])
00548         mask = numpy.array([[ 1, 1, 1, 1, 1], 
00549                             [ 1, 1, 1, 1, 1], 
00550                             [ 1, 1, 1, 1, 1], 
00551                             [ 1, 1, 1, 1, 1]], int)
00552 
00553         clusterid, celldata = somcluster(data=data, mask=mask, weight=weight,
00554                                          transpose=0, nxgrid=10, nygrid=10,
00555                                          inittau=0.02, niter=100, dist='e')
00556         self.assertEqual(len(clusterid), len(data))
00557         self.assertEqual(len(clusterid[0]), 2)
00558 
00559         # Second data set
00560         weight =  [ 1,1 ]
00561         data = numpy.array([[ 1.1, 1.2 ],
00562                             [ 1.4, 1.3 ],
00563                             [ 1.1, 1.5 ],
00564                             [ 2.0, 1.5 ],
00565                             [ 1.7, 1.9 ],
00566                             [ 1.7, 1.9 ],
00567                             [ 5.7, 5.9 ],
00568                             [ 5.7, 5.9 ],
00569                             [ 3.1, 3.3 ],
00570                             [ 5.4, 5.3 ],
00571                             [ 5.1, 5.5 ],
00572                             [ 5.0, 5.5 ],
00573                             [ 5.1, 5.2 ]])
00574         mask = numpy.array([[ 1, 1 ],
00575                             [ 1, 1 ],
00576                             [ 1, 1 ],
00577                             [ 1, 1 ],
00578                             [ 1, 1 ],
00579                             [ 1, 1 ],
00580                             [ 1, 1 ],
00581                             [ 1, 1 ],
00582                             [ 1, 1 ],
00583                             [ 1, 1 ],
00584                             [ 1, 1 ],
00585                             [ 1, 1 ],
00586                             [ 1, 1 ]], int)
00587 
00588         clusterid, celldata = somcluster(data=data, mask=mask, weight=weight,
00589                                          transpose=0, nxgrid=10, nygrid=10,
00590                                          inittau=0.02, niter=100, dist='e')
00591         self.assertEqual(len(clusterid), len(data))
00592         self.assertEqual(len(clusterid[0]), 2)

Here is the call graph for this function:

Definition at line 267 of file test_Cluster.py.

00267 
00268     def test_treecluster(self):
00269         if TestCluster.module=='Bio.Cluster':
00270             from Bio.Cluster import treecluster
00271         elif TestCluster.module=='Pycluster':
00272             from Pycluster import treecluster
00273 
00274         # First data set
00275         weight1 =  [ 1,1,1,1,1 ]
00276         data1   =  numpy.array([[  1.1, 2.2, 3.3, 4.4, 5.5], 
00277                                 [  3.1, 3.2, 1.3, 2.4, 1.5], 
00278                                 [  4.1, 2.2, 0.3, 5.4, 0.5], 
00279                                 [ 12.1, 2.0, 0.0, 5.0, 0.0]])
00280         mask1 = numpy.array([[ 1, 1, 1, 1, 1], 
00281                              [ 1, 1, 1, 1, 1], 
00282                              [ 1, 1, 1, 1, 1], 
00283                              [ 1, 1, 1, 1, 1]], int)
00284       
00285         # test first data set
00286         # Pairwise average-linkage clustering"
00287         tree = treecluster(data=data1, mask=mask1, weight=weight1,
00288                            transpose=0, method='a', dist='e')
00289         self.assertEqual(len(tree), len(data1)-1)
00290         self.assertEqual(tree[0].left, 2)
00291         self.assertEqual(tree[0].right, 1)
00292         self.assertAlmostEqual(tree[0].distance, 2.600, places=3)
00293         self.assertEqual(tree[1].left, -1)
00294         self.assertEqual(tree[1].right, 0)
00295         self.assertAlmostEqual(tree[1].distance, 7.300, places=3)
00296         self.assertEqual(tree[2].left, 3)
00297         self.assertEqual(tree[2].right, -2)
00298         self.assertAlmostEqual(tree[2].distance, 21.348, places=3)
00299 
00300         # Pairwise single-linkage clustering
00301         tree = treecluster(data=data1, mask=mask1, weight=weight1,
00302                            transpose=0, method='s', dist='e')
00303         self.assertEqual(len(tree), len(data1)-1)
00304         self.assertEqual(tree[0].left, 1)
00305         self.assertEqual(tree[0].right, 2)
00306         self.assertAlmostEqual(tree[0].distance, 2.600, places=3)
00307         self.assertEqual(tree[1].left, 0)
00308         self.assertEqual(tree[1].right, -1)
00309         self.assertAlmostEqual(tree[1].distance, 5.800, places=3)
00310         self.assertEqual(tree[2].left, -2)
00311         self.assertEqual(tree[2].right, 3)
00312         self.assertAlmostEqual(tree[2].distance, 12.908, places=3)
00313 
00314         # Pairwise centroid-linkage clustering
00315         tree = treecluster(data=data1, mask=mask1, weight=weight1,
00316                            transpose=0, method='c', dist='e')
00317         self.assertEqual(len(tree), len(data1)-1)
00318         self.assertEqual(tree[0].left, 1)
00319         self.assertEqual(tree[0].right, 2)
00320         self.assertAlmostEqual(tree[0].distance, 2.600, places=3)
00321         self.assertEqual(tree[1].left, 0)
00322         self.assertEqual(tree[1].right, -1)
00323         self.assertAlmostEqual(tree[1].distance, 6.650, places=3)
00324         self.assertEqual(tree[2].left, -2)
00325         self.assertEqual(tree[2].right, 3)
00326         self.assertAlmostEqual(tree[2].distance, 19.437, places=3)
00327 
00328         # Pairwise maximum-linkage clustering
00329         tree = treecluster(data=data1, mask=mask1, weight=weight1,
00330                            transpose=0, method='m', dist='e')
00331         self.assertEqual(len(tree), len(data1)-1)
00332         self.assertEqual(tree[0].left, 2)
00333         self.assertEqual(tree[0].right, 1)
00334         self.assertAlmostEqual(tree[0].distance, 2.600, places=3)
00335         self.assertEqual(tree[1].left, -1)
00336         self.assertEqual(tree[1].right, 0)
00337         self.assertAlmostEqual(tree[1].distance, 8.800, places=3)
00338         self.assertEqual(tree[2].left, 3)
00339         self.assertEqual(tree[2].right, -2)
00340         self.assertAlmostEqual(tree[2].distance, 32.508, places=3)
00341       
00342         # Second data set
00343         weight2 =  [ 1,1 ]
00344         data2 = numpy.array([[ 0.8223, 0.9295 ],
00345                              [ 1.4365, 1.3223 ],
00346                              [ 1.1623, 1.5364 ],
00347                              [ 2.1826, 1.1934 ],
00348                              [ 1.7763, 1.9352 ],
00349                              [ 1.7215, 1.9912 ],
00350                              [ 2.1812, 5.9935 ],
00351                              [ 5.3290, 5.9452 ],
00352                              [ 3.1491, 3.3454 ],
00353                              [ 5.1923, 5.3156 ],
00354                              [ 4.7735, 5.4012 ],
00355                              [ 5.1297, 5.5645 ],
00356                              [ 5.3934, 5.1823 ]])
00357         mask2 = numpy.array([[ 1, 1 ],
00358                              [ 1, 1 ],
00359                              [ 1, 1 ],
00360                              [ 1, 1 ],
00361                              [ 1, 1 ],
00362                              [ 1, 1 ],
00363                              [ 1, 1 ],
00364                              [ 1, 1 ],
00365                              [ 1, 1 ],
00366                              [ 1, 1 ],
00367                              [ 1, 1 ],
00368                              [ 1, 1 ],
00369                              [ 1, 1 ]], int)
00370       
00371         # Test second data set
00372         # Pairwise average-linkage clustering
00373         tree = treecluster(data=data2, mask=mask2, weight=weight2,
00374                            transpose=0, method='a', dist='e')
00375         self.assertEqual(len(tree), len(data2)-1)
00376         self.assertEqual(tree[0].left, 5)
00377         self.assertEqual(tree[0].right, 4)
00378         self.assertAlmostEqual(tree[0].distance, 0.003, places=3)
00379         self.assertEqual(tree[1].left, 9)
00380         self.assertEqual(tree[1].right, 12)
00381         self.assertAlmostEqual(tree[1].distance, 0.029, places=3)
00382         self.assertEqual(tree[2].left, 2)
00383         self.assertEqual(tree[2].right, 1)
00384         self.assertAlmostEqual(tree[2].distance, 0.061, places=3)
00385         self.assertEqual(tree[3].left, 11)
00386         self.assertEqual(tree[3].right, -2)
00387         self.assertAlmostEqual(tree[3].distance, 0.070, places=3)
00388         self.assertEqual(tree[4].left, -4)
00389         self.assertEqual(tree[4].right, 10)
00390         self.assertAlmostEqual(tree[4].distance, 0.128, places=3)
00391         self.assertEqual(tree[5].left, 7)
00392         self.assertEqual(tree[5].right, -5)
00393         self.assertAlmostEqual(tree[5].distance, 0.224, places=3)
00394         self.assertEqual(tree[6].left, -3)
00395         self.assertEqual(tree[6].right, 0)
00396         self.assertAlmostEqual(tree[6].distance, 0.254, places=3)
00397         self.assertEqual(tree[7].left, -1)
00398         self.assertEqual(tree[7].right, 3)
00399         self.assertAlmostEqual(tree[7].distance, 0.391, places=3)
00400         self.assertEqual(tree[8].left, -8)
00401         self.assertEqual(tree[8].right, -7)
00402         self.assertAlmostEqual(tree[8].distance, 0.532, places=3)
00403         self.assertEqual(tree[9].left, 8)
00404         self.assertEqual(tree[9].right, -9)
00405         self.assertAlmostEqual(tree[9].distance, 3.234, places=3)
00406         self.assertEqual(tree[10].left, -6)
00407         self.assertEqual(tree[10].right, 6)
00408         self.assertAlmostEqual(tree[10].distance, 4.636, places=3)
00409         self.assertEqual(tree[11].left, -11)
00410         self.assertEqual(tree[11].right, -10)
00411         self.assertAlmostEqual(tree[11].distance, 12.741, places=3)
00412       
00413         # Pairwise single-linkage clustering
00414         tree = treecluster(data=data2, mask=mask2, weight=weight2,
00415                            transpose=0, method='s', dist='e')
00416         self.assertEqual(len(tree), len(data2)-1)
00417         self.assertEqual(tree[0].left, 4)
00418         self.assertEqual(tree[0].right, 5)
00419         self.assertAlmostEqual(tree[0].distance, 0.003, places=3)
00420         self.assertEqual(tree[1].left, 9)
00421         self.assertEqual(tree[1].right, 12)
00422         self.assertAlmostEqual(tree[1].distance, 0.029, places=3)
00423         self.assertEqual(tree[2].left, 11)
00424         self.assertEqual(tree[2].right, -2)
00425         self.assertAlmostEqual(tree[2].distance, 0.033, places=3)
00426         self.assertEqual(tree[3].left, 1)
00427         self.assertEqual(tree[3].right, 2)
00428         self.assertAlmostEqual(tree[3].distance, 0.061, places=3)
00429         self.assertEqual(tree[4].left, 10)
00430         self.assertEqual(tree[4].right, -3)
00431         self.assertAlmostEqual(tree[4].distance, 0.077, places=3)
00432         self.assertEqual(tree[5].left, 7)
00433         self.assertEqual(tree[5].right, -5)
00434         self.assertAlmostEqual(tree[5].distance, 0.092, places=3)
00435         self.assertEqual(tree[6].left, 0)
00436         self.assertEqual(tree[6].right, -4)
00437         self.assertAlmostEqual(tree[6].distance, 0.242, places=3)
00438         self.assertEqual(tree[7].left, -7)
00439         self.assertEqual(tree[7].right, -1)
00440         self.assertAlmostEqual(tree[7].distance, 0.246, places=3)
00441         self.assertEqual(tree[8].left, 3)
00442         self.assertEqual(tree[8].right, -8)
00443         self.assertAlmostEqual(tree[8].distance, 0.287, places=3)
00444         self.assertEqual(tree[9].left, -9)
00445         self.assertEqual(tree[9].right, 8)
00446         self.assertAlmostEqual(tree[9].distance, 1.936, places=3)
00447         self.assertEqual(tree[10].left, -10)
00448         self.assertEqual(tree[10].right, -6)
00449         self.assertAlmostEqual(tree[10].distance, 3.432, places=3)
00450         self.assertEqual(tree[11].left, 6)
00451         self.assertEqual(tree[11].right, -11)
00452         self.assertAlmostEqual(tree[11].distance, 3.535, places=3)
00453       
00454         # Pairwise centroid-linkage clustering
00455         tree = treecluster(data=data2, mask=mask2, weight=weight2,
00456                            transpose=0, method='c', dist='e')
00457         self.assertEqual(len(tree), len(data2)-1)
00458         self.assertEqual(tree[0].left, 4)
00459         self.assertEqual(tree[0].right, 5)
00460         self.assertAlmostEqual(tree[0].distance, 0.003, places=3)
00461         self.assertEqual(tree[1].left, 12)
00462         self.assertEqual(tree[1].right, 9)
00463         self.assertAlmostEqual(tree[1].distance, 0.029, places=3)
00464         self.assertEqual(tree[2].left, 1)
00465         self.assertEqual(tree[2].right, 2)
00466         self.assertAlmostEqual(tree[2].distance, 0.061, places=3)
00467         self.assertEqual(tree[3].left, -2)
00468         self.assertEqual(tree[3].right, 11)
00469         self.assertAlmostEqual(tree[3].distance, 0.063, places=3)
00470         self.assertEqual(tree[4].left, 10)
00471         self.assertEqual(tree[4].right, -4)
00472         self.assertAlmostEqual(tree[4].distance, 0.109, places=3)
00473         self.assertEqual(tree[5].left, -5)
00474         self.assertEqual(tree[5].right, 7)
00475         self.assertAlmostEqual(tree[5].distance, 0.189, places=3)
00476         self.assertEqual(tree[6].left, 0)
00477         self.assertEqual(tree[6].right, -3)
00478         self.assertAlmostEqual(tree[6].distance, 0.239, places=3)
00479         self.assertEqual(tree[7].left, 3)
00480         self.assertEqual(tree[7].right, -1)
00481         self.assertAlmostEqual(tree[7].distance, 0.390, places=3)
00482         self.assertEqual(tree[8].left, -7)
00483         self.assertEqual(tree[8].right, -8)
00484         self.assertAlmostEqual(tree[8].distance, 0.382, places=3)
00485         self.assertEqual(tree[9].left, -9)
00486         self.assertEqual(tree[9].right, 8)
00487         self.assertAlmostEqual(tree[9].distance, 3.063, places=3)
00488         self.assertEqual(tree[10].left, 6)
00489         self.assertEqual(tree[10].right, -6)
00490         self.assertAlmostEqual(tree[10].distance, 4.578, places=3)
00491         self.assertEqual(tree[11].left, -10)
00492         self.assertEqual(tree[11].right, -11)
00493         self.assertAlmostEqual(tree[11].distance, 11.536, places=3)
00494       
00495         # Pairwise maximum-linkage clustering
00496         tree = treecluster(data=data2, mask=mask2, weight=weight2,
00497                            transpose=0, method='m', dist='e')
00498         self.assertEqual(len(tree), len(data2)-1)
00499         self.assertEqual(tree[0].left, 5)
00500         self.assertEqual(tree[0].right, 4)
00501         self.assertAlmostEqual(tree[0].distance, 0.003, places=3)
00502         self.assertEqual(tree[1].left, 9)
00503         self.assertEqual(tree[1].right, 12)
00504         self.assertAlmostEqual(tree[1].distance, 0.029, places=3)
00505         self.assertEqual(tree[2].left, 2)
00506         self.assertEqual(tree[2].right, 1)
00507         self.assertAlmostEqual(tree[2].distance, 0.061, places=3)
00508         self.assertEqual(tree[3].left, 11)
00509         self.assertEqual(tree[3].right, 10)
00510         self.assertAlmostEqual(tree[3].distance, 0.077, places=3)
00511         self.assertEqual(tree[4].left, -2)
00512         self.assertEqual(tree[4].right, -4)
00513         self.assertAlmostEqual(tree[4].distance, 0.216, places=3)
00514         self.assertEqual(tree[5].left, -3)
00515         self.assertEqual(tree[5].right, 0)
00516         self.assertAlmostEqual(tree[5].distance, 0.266, places=3)
00517         self.assertEqual(tree[6].left, -5)
00518         self.assertEqual(tree[6].right, 7)
00519         self.assertAlmostEqual(tree[6].distance, 0.302, places=3)
00520         self.assertEqual(tree[7].left, -1)
00521         self.assertEqual(tree[7].right, 3)
00522         self.assertAlmostEqual(tree[7].distance, 0.425, places=3)
00523         self.assertEqual(tree[8].left, -8)
00524         self.assertEqual(tree[8].right, -6)
00525         self.assertAlmostEqual(tree[8].distance, 0.968, places=3)
00526         self.assertEqual(tree[9].left, 8)
00527         self.assertEqual(tree[9].right, 6)
00528         self.assertAlmostEqual(tree[9].distance, 3.975, places=3)
00529         self.assertEqual(tree[10].left, -10)
00530         self.assertEqual(tree[10].right, -7)
00531         self.assertAlmostEqual(tree[10].distance, 5.755, places=3)
00532         self.assertEqual(tree[11].left, -11)
00533         self.assertEqual(tree[11].right, -9)
00534         self.assertAlmostEqual(tree[11].distance, 22.734, places=3)

Here is the call graph for this function:


Member Data Documentation

string test_Cluster.TestCluster.module = 'Bio.Cluster' [static]

Definition at line 24 of file test_Cluster.py.


The documentation for this class was generated from the following file: