Esempio n. 1
0
 def test_correspond_4_and_up_2(self):
     # Tests correspond(Z, y) on linkage and CDMs over observation sets of
     # different sizes. Correspondance should be false.
     for (i, j) in (list(zip(list(range(2, 7)), list(range(16, 21)))) +
                    list(zip(list(range(2, 7)), list(range(16, 21))))):
         y = np.random.rand(i*(i-1)//2)
         y2 = np.random.rand(j*(j-1)//2)
         Z = linkage(y)
         Z2 = linkage(y2)
         self.assertTrue(correspond(Z, y2) == False)
         self.assertTrue(correspond(Z2, y) == False)
Esempio n. 2
0
 def test_correspond_2_and_up(self):
     # Tests correspond(Z, y) on linkage and CDMs over observation sets of
     # different sizes.
     for i in xrange(2, 4):
         y = np.random.rand(i*(i-1)//2)
         Z = linkage(y)
         self.assertTrue(correspond(Z, y))
     for i in xrange(4, 15, 3):
         y = np.random.rand(i*(i-1)//2)
         Z = linkage(y)
         self.assertTrue(correspond(Z, y))
Esempio n. 3
0
 def test_correspond_4_and_up_2(self):
     # Tests correspond(Z, y) on linkage and CDMs over observation sets of
     # different sizes. Correspondance should be false.
     for (i, j) in (list(zip(list(range(2, 7)), list(range(16, 21)))) +
                    list(zip(list(range(2, 7)), list(range(16, 21))))):
         y = np.random.rand(i*(i-1)//2)
         y2 = np.random.rand(j*(j-1)//2)
         Z = linkage(y)
         Z2 = linkage(y2)
         self.assertTrue(correspond(Z, y2) == False)
         self.assertTrue(correspond(Z2, y) == False)
Esempio n. 4
0
 def test_correspond_2_and_up(self):
     # Tests correspond(Z, y) on linkage and CDMs over observation sets of
     # different sizes.
     for i in range(2, 4):
         y = np.random.rand(i*(i-1)//2)
         Z = linkage(y)
         self.assertTrue(correspond(Z, y))
     for i in range(4, 15, 3):
         y = np.random.rand(i*(i-1)//2)
         Z = linkage(y)
         self.assertTrue(correspond(Z, y))
Esempio n. 5
0
 def test_is_valid_linkage_4_and_up(self):
     # Tests is_valid_linkage(Z) on linkage on observation sets between
     # sizes 4 and 15 (step size 3).
     for i in xrange(4, 15, 3):
         y = np.random.rand(i*(i-1)//2)
         Z = linkage(y)
         assert_(is_valid_linkage(Z) == True)
Esempio n. 6
0
 def test_num_obs_linkage_4_and_up(self):
     # Tests num_obs_linkage(Z) on linkage on observation sets between sizes
     # 4 and 15 (step size 3).
     for i in xrange(4, 15, 3):
         y = np.random.rand(i*(i-1)//2)
         Z = linkage(y)
         self.assertTrue(num_obs_linkage(Z) == i)
Esempio n. 7
0
 def check_maxdists_Q_linkage(self, method):
     # Tests maxdists(Z) on the Q data set
     X = hierarchy_test_data.Q_X
     Z = linkage(X, method)
     MD = maxdists(Z)
     expectedMD = calculate_maximum_distances(Z)
     assert_allclose(MD, expectedMD, atol=1e-15)
Esempio n. 8
0
    def check_dendrogram_plot(self, orientation):
        # Tests dendrogram plotting.
        Z = linkage(hierarchy_test_data.ytdist, 'single')
        expected = {'color_list': ['g', 'b', 'b', 'b', 'b'],
                    'dcoord': [[0.0, 138.0, 138.0, 0.0],
                               [0.0, 219.0, 219.0, 0.0],
                               [0.0, 255.0, 255.0, 219.0],
                               [0.0, 268.0, 268.0, 255.0],
                               [138.0, 295.0, 295.0, 268.0]],
                    'icoord': [[5.0, 5.0, 15.0, 15.0],
                               [45.0, 45.0, 55.0, 55.0],
                               [35.0, 35.0, 50.0, 50.0],
                               [25.0, 25.0, 42.5, 42.5],
                               [10.0, 10.0, 33.75, 33.75]],
                    'ivl': ['2', '5', '1', '0', '3', '4'],
                    'leaves': [2, 5, 1, 0, 3, 4]}

        fig = plt.figure()
        ax = fig.add_subplot(111)

        # test that dendrogram accepts ax keyword
        R1 = dendrogram(Z, ax=ax, orientation=orientation)
        plt.close()
        assert_equal(R1, expected)

        # test plotting to gca (will import pylab)
        R2 = dendrogram(Z, orientation=orientation)
        plt.close()
        assert_equal(R2, expected)
Esempio n. 9
0
 def test_Q_subtree_pre_order(self):
     # Tests that pre_order() works when called on sub-trees.
     X = hierarchy_test_data.Q_X
     Z = linkage(X, 'single')
     node = to_tree(Z)
     assert_equal(node.pre_order(), (node.get_left().pre_order()
                                     + node.get_right().pre_order()))
Esempio n. 10
0
    def check_dendrogram_plot(self, orientation):
        # Tests dendrogram plotting.
        Z = linkage(hierarchy_test_data.ytdist, 'single')
        expected = {'color_list': ['g', 'b', 'b', 'b', 'b'],
                    'dcoord': [[0.0, 138.0, 138.0, 0.0],
                               [0.0, 219.0, 219.0, 0.0],
                               [0.0, 255.0, 255.0, 219.0],
                               [0.0, 268.0, 268.0, 255.0],
                               [138.0, 295.0, 295.0, 268.0]],
                    'icoord': [[5.0, 5.0, 15.0, 15.0],
                               [45.0, 45.0, 55.0, 55.0],
                               [35.0, 35.0, 50.0, 50.0],
                               [25.0, 25.0, 42.5, 42.5],
                               [10.0, 10.0, 33.75, 33.75]],
                    'ivl': ['2', '5', '1', '0', '3', '4'],
                    'leaves': [2, 5, 1, 0, 3, 4]}

        fig = plt.figure()
        ax = fig.add_subplot(111)

        # test that dendrogram accepts ax keyword
        R1 = dendrogram(Z, ax=ax, orientation=orientation)
        plt.close()
        assert_equal(R1, expected)

        # test plotting to gca (will import pylab)
        R2 = dendrogram(Z, orientation=orientation)
        plt.close()
        assert_equal(R2, expected)
Esempio n. 11
0
 def test_num_obs_linkage_4_and_up(self):
     # Tests num_obs_linkage(Z) on linkage on observation sets between sizes
     # 4 and 15 (step size 3).
     for i in range(4, 15, 3):
         y = np.random.rand(i*(i-1)//2)
         Z = linkage(y)
         self.assertTrue(num_obs_linkage(Z) == i)
Esempio n. 12
0
 def check_maxdists_Q_linkage(self, method):
     # Tests maxdists(Z) on the Q data set
     X = hierarchy_test_data.Q_X
     Z = linkage(X, method)
     MD = maxdists(Z)
     expectedMD = calculate_maximum_distances(Z)
     assert_allclose(MD, expectedMD, atol=1e-15)
Esempio n. 13
0
 def test_is_valid_linkage_4_and_up(self):
     # Tests is_valid_linkage(Z) on linkage on observation sets between
     # sizes 4 and 15 (step size 3).
     for i in range(4, 15, 3):
         y = np.random.rand(i*(i-1)//2)
         Z = linkage(y)
         assert_(is_valid_linkage(Z) == True)
Esempio n. 14
0
 def test_num_obs_linkage_multi_matrix(self):
     # Tests num_obs_linkage with observation matrices of multiple sizes.
     for n in range(2, 10):
         X = np.random.rand(n, 4)
         Y = pdist(X)
         Z = linkage(Y)
         self.assertTrue(num_obs_linkage(Z) == n)
Esempio n. 15
0
 def test_num_obs_linkage_multi_matrix(self):
     # Tests num_obs_linkage with observation matrices of multiple sizes.
     for n in xrange(2, 10):
         X = np.random.rand(n, 4)
         Y = pdist(X)
         Z = linkage(Y)
         self.assertTrue(num_obs_linkage(Z) == n)
Esempio n. 16
0
    def test_dendrogram_truncate_mode(self):
        Z = linkage(hierarchy_test_data.ytdist, 'single')

        R = dendrogram(Z, 2, 'lastp', show_contracted=True)
        plt.close()
        assert_equal(
            R, {
                'color_list': ['b'],
                'dcoord': [[0.0, 295.0, 295.0, 0.0]],
                'icoord': [[5.0, 5.0, 15.0, 15.0]],
                'ivl': ['(2)', '(4)'],
                'leaves': [6, 9]
            })

        R = dendrogram(Z, 2, 'mtica', show_contracted=True)
        plt.close()
        assert_equal(
            R, {
                'color_list': ['g', 'b', 'b', 'b'],
                'dcoord': [[0.0, 138.0, 138.0, 0.0], [0.0, 255.0, 255.0, 0.0],
                           [0.0, 268.0, 268.0, 255.0],
                           [138.0, 295.0, 295.0, 268.0]],
                'icoord': [[5.0, 5.0, 15.0, 15.0], [35.0, 35.0, 45.0, 45.0],
                           [25.0, 25.0, 40.0, 40.0], [10.0, 10.0, 32.5, 32.5]],
                'ivl': ['2', '5', '1', '0', '(2)'],
                'leaves': [2, 5, 1, 0, 7]
            })
Esempio n. 17
0
 def test_Q_subtree_pre_order(self):
     # Tests that pre_order() works when called on sub-trees.
     X = hierarchy_test_data.Q_X
     Z = linkage(X, 'single')
     node = to_tree(Z)
     assert_equal(node.pre_order(), (node.get_left().pre_order()
                                     + node.get_right().pre_order()))
Esempio n. 18
0
 def test_is_valid_im_4_and_up(self):
     # Tests is_valid_im(R) on im on observation sets between sizes 4 and 15
     # (step size 3).
     for i in xrange(4, 15, 3):
         y = np.random.rand(i*(i-1)//2)
         Z = linkage(y)
         R = inconsistent(Z)
         assert_(is_valid_im(R) == True)
Esempio n. 19
0
 def check_maxRstat_Q_linkage(self, method, i):
     # Tests maxRstat(Z, R, i) on the Q data set
     X = hierarchy_test_data.Q_X
     Z = linkage(X, method)
     R = inconsistent(Z)
     MD = maxRstat(Z, R, 1)
     expectedMD = calculate_maximum_inconsistencies(Z, R, 1)
     assert_allclose(MD, expectedMD, atol=1e-15)
Esempio n. 20
0
 def check_maxRstat_Q_linkage(self, method, i):
     # Tests maxRstat(Z, R, i) on the Q data set
     X = hierarchy_test_data.Q_X
     Z = linkage(X, method)
     R = inconsistent(Z)
     MD = maxRstat(Z, R, 1)
     expectedMD = calculate_maximum_inconsistencies(Z, R, 1)
     assert_allclose(MD, expectedMD, atol=1e-15)
Esempio n. 21
0
 def test_is_valid_im_4_and_up(self):
     # Tests is_valid_im(R) on im on observation sets between sizes 4 and 15
     # (step size 3).
     for i in range(4, 15, 3):
         y = np.random.rand(i*(i-1)//2)
         Z = linkage(y)
         R = inconsistent(Z)
         assert_(is_valid_im(R) == True)
Esempio n. 22
0
 def test_leaders_single(self):
     # Tests leaders using a flat clustering generated by single linkage.
     X = hierarchy_test_data.Q_X
     Y = pdist(X)
     Z = linkage(Y)
     T = fcluster(Z, criterion='maxclust', t=3)
     Lright = (np.array([53, 55, 56]), np.array([2, 3, 1]))
     L = leaders(Z, T)
     assert_equal(L, Lright)
Esempio n. 23
0
 def test_is_valid_linkage_4_and_up_neg_counts(self):
     # Tests is_valid_linkage(Z) on linkage on observation sets between
     # sizes 4 and 15 (step size 3) with negative counts.
     for i in range(4, 15, 3):
         y = np.random.rand(i*(i-1)//2)
         Z = linkage(y)
         Z[i//2,3] = -2
         assert_(is_valid_linkage(Z) == False)
         assert_raises(ValueError, is_valid_linkage, Z, throw=True)
Esempio n. 24
0
 def test_is_valid_linkage_4_and_up_neg_counts(self):
     # Tests is_valid_linkage(Z) on linkage on observation sets between
     # sizes 4 and 15 (step size 3) with negative counts.
     for i in xrange(4, 15, 3):
         y = np.random.rand(i*(i-1)//2)
         Z = linkage(y)
         Z[i//2,3] = -2
         assert_(is_valid_linkage(Z) == False)
         assert_raises(ValueError, is_valid_linkage, Z, throw=True)
Esempio n. 25
0
 def test_leaders_single(self):
     # Tests leaders using a flat clustering generated by single linkage.
     X = hierarchy_test_data.Q_X
     Y = pdist(X)
     Z = linkage(Y)
     T = fcluster(Z, criterion='maxclust', t=3)
     Lright = (np.array([53, 55, 56]), np.array([2, 3, 1]))
     L = leaders(Z, T)
     assert_equal(L, Lright)
Esempio n. 26
0
 def test_is_valid_im_4_and_up_neg_dist(self):
     # Tests is_valid_im(R) on im on observation sets between sizes 4 and 15
     # (step size 3) with negative link counts.
     for i in xrange(4, 15, 3):
         y = np.random.rand(i*(i-1)//2)
         Z = linkage(y)
         R = inconsistent(Z)
         R[i//2,2] = -0.5
         assert_(is_valid_im(R) == False)
         assert_raises(ValueError, is_valid_im, R, throw=True)
Esempio n. 27
0
 def test_is_valid_im_4_and_up_neg_index_right(self):
     # Tests is_valid_im(R) on im on observation sets between sizes 4 and 15
     # (step size 3) with negative link height standard deviations.
     for i in xrange(4, 15, 3):
         y = np.random.rand(i * (i - 1) // 2)
         Z = linkage(y)
         R = inconsistent(Z)
         R[i // 2, 1] = -2.0
         assert_(is_valid_im(R) == False)
         assert_raises(ValueError, is_valid_im, R, throw=True)
Esempio n. 28
0
 def test_is_valid_im_4_and_up_neg_dist(self):
     # Tests is_valid_im(R) on im on observation sets between sizes 4 and 15
     # (step size 3) with negative link counts.
     for i in range(4, 15, 3):
         y = np.random.rand(i*(i-1)//2)
         Z = linkage(y)
         R = inconsistent(Z)
         R[i//2,2] = -0.5
         assert_(is_valid_im(R) == False)
         assert_raises(ValueError, is_valid_im, R, throw=True)
Esempio n. 29
0
    def test_dendrogram_truncate_mode(self):
        Z = linkage(hierarchy_test_data.ytdist, 'single')

        R = dendrogram(Z, 2, 'lastp', show_contracted=True)
        plt.close()
        assert_equal(R, {'color_list': ['b'],
                         'dcoord': [[0.0, 295.0, 295.0, 0.0]],
                         'icoord': [[5.0, 5.0, 15.0, 15.0]],
                         'ivl': ['(2)', '(4)'],
                         'leaves': [6, 9]})

        R = dendrogram(Z, 2, 'mtica', show_contracted=True)
        plt.close()
        assert_equal(R, {'color_list': ['g', 'b', 'b', 'b'],
                         'dcoord': [[0.0, 138.0, 138.0, 0.0],
                                    [0.0, 255.0, 255.0, 0.0],
                                    [0.0, 268.0, 268.0, 255.0],
                                    [138.0, 295.0, 295.0, 268.0]],
                         'icoord': [[5.0, 5.0, 15.0, 15.0],
                                    [35.0, 35.0, 45.0, 45.0],
                                    [25.0, 25.0, 40.0, 40.0],
                                    [10.0, 10.0, 32.5, 32.5]],
                         'ivl': ['2', '5', '1', '0', '(2)'],
                         'leaves': [2, 5, 1, 0, 7]})
Esempio n. 30
0
 def test_valid_orientation(self):
     Z = linkage(hierarchy_test_data.ytdist, 'single')
     assert_raises(ValueError, dendrogram, Z, orientation="foo")
Esempio n. 31
0
 def test_dendrogram_single_linkage_tdist(self):
     # Tests dendrogram calculation on single linkage of the tdist data set.
     Z = linkage(hierarchy_test_data.ytdist, 'single')
     R = dendrogram(Z, no_plot=True)
     leaves = R["leaves"]
     assert_equal(leaves, [2, 5, 1, 0, 3, 4])
Esempio n. 32
0
 def check_leaves_list_Q(self, method):
     # Tests leaves_list(Z) on the Q data set
     X = hierarchy_test_data.Q_X
     Z = linkage(X, method)
     node = to_tree(Z)
     assert_equal(node.pre_order(), leaves_list(Z))
Esempio n. 33
0
 def test_is_monotonic_tdist_linkage1(self):
     # Tests is_monotonic(Z) on clustering generated by single linkage on
     # tdist data set. Expecting True.
     Z = linkage(hierarchy_test_data.ytdist, 'single')
     self.assertTrue(is_monotonic(Z) == True)
Esempio n. 34
0
 def test_is_monotonic_Q_linkage(self):
     # Tests is_monotonic(Z) on clustering generated by single linkage on
     # Q data set. Expecting True.
     X = hierarchy_test_data.Q_X
     Z = linkage(X, 'single')
     self.assertTrue(is_monotonic(Z) == True)
Esempio n. 35
0
 def test_is_monotonic_tdist_linkage2(self):
     # Tests is_monotonic(Z) on clustering generated by single linkage on
     # tdist data set. Perturbing. Expecting False.
     Z = linkage(hierarchy_test_data.ytdist, 'single')
     Z[2,2] = 0.0
     self.assertTrue(is_monotonic(Z) == False)
Esempio n. 36
0
def test_2x2_linkage():
    Z1 = linkage([1], method='single', metric='euclidean')
    Z2 = linkage([[0, 1], [0, 0]], method='single', metric='euclidean')
    assert_allclose(Z1, Z2)
Esempio n. 37
0
 def test_is_monotonic_tdist_linkage2(self):
     # Tests is_monotonic(Z) on clustering generated by single linkage on
     # tdist data set. Perturbing. Expecting False.
     Z = linkage(hierarchy_test_data.ytdist, 'single')
     Z[2,2] = 0.0
     self.assertTrue(is_monotonic(Z) == False)
Esempio n. 38
0
 def test_is_monotonic_Q_linkage(self):
     # Tests is_monotonic(Z) on clustering generated by single linkage on
     # Q data set. Expecting True.
     X = hierarchy_test_data.Q_X
     Z = linkage(X, 'single')
     self.assertTrue(is_monotonic(Z) == True)
Esempio n. 39
0
 def test_dendrogram_single_linkage_tdist(self):
     # Tests dendrogram calculation on single linkage of the tdist data set.
     Z = linkage(hierarchy_test_data.ytdist, 'single')
     R = dendrogram(Z, no_plot=True)
     leaves = R["leaves"]
     assert_equal(leaves, [2, 5, 1, 0, 3, 4])
Esempio n. 40
0
 def test_valid_orientation(self):
     Z = linkage(hierarchy_test_data.ytdist, 'single')
     assert_raises(ValueError, dendrogram, Z, orientation="foo")
Esempio n. 41
0
 def check_leaves_list_Q(self, method):
     # Tests leaves_list(Z) on the Q data set
     X = hierarchy_test_data.Q_X
     Z = linkage(X, method)
     node = to_tree(Z)
     assert_equal(node.pre_order(), leaves_list(Z))
Esempio n. 42
0
 def check_linkage_tdist(self, method):
     # Tests linkage(Y, method) on the tdist data set.
     Z = linkage(hierarchy_test_data.ytdist, method)
     expectedZ = getattr(hierarchy_test_data, 'linkage_ytdist_' + method)
     assert_allclose(Z, expectedZ, atol=1e-10)
Esempio n. 43
0
 def check_linkage_tdist(self, method):
     # Tests linkage(Y, method) on the tdist data set.
     Z = linkage(hierarchy_test_data.ytdist, method)
     expectedZ = getattr(hierarchy_test_data, 'linkage_ytdist_' + method)
     assert_allclose(Z, expectedZ, atol=1e-10)
Esempio n. 44
0
 def test_is_monotonic_tdist_linkage1(self):
     # Tests is_monotonic(Z) on clustering generated by single linkage on
     # tdist data set. Expecting True.
     Z = linkage(hierarchy_test_data.ytdist, 'single')
     self.assertTrue(is_monotonic(Z) == True)
Esempio n. 45
0
 def check_linkage_q(self, method):
     # Tests linkage(Y, method) on the Q data set.
     Z = linkage(hierarchy_test_data.X, method)
     expectedZ = getattr(hierarchy_test_data, 'linkage_X_' + method)
     assert_allclose(Z, expectedZ, atol=1e-06)
Esempio n. 46
0
 def check_linkage_q(self, method):
     # Tests linkage(Y, method) on the Q data set.
     Z = linkage(hierarchy_test_data.X, method)
     expectedZ = getattr(hierarchy_test_data, 'linkage_X_' + method)
     assert_allclose(Z, expectedZ, atol=1e-06)
Esempio n. 47
0
def test_2x2_linkage():
    Z1 = linkage([1], method='single', metric='euclidean')
    Z2 = linkage([[0, 1], [0, 0]], method='single', metric='euclidean')
    assert_allclose(Z1, Z2)