Ejemplo 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)
Ejemplo 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))
Ejemplo 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)
Ejemplo 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))
Ejemplo 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)
Ejemplo 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)
Ejemplo 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)
Ejemplo 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)
Ejemplo 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()))
Ejemplo 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)
Ejemplo 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)
Ejemplo 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)
Ejemplo 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)
Ejemplo 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)
Ejemplo 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)
Ejemplo 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]
            })
Ejemplo 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()))
Ejemplo 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)
Ejemplo 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)
Ejemplo 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)
Ejemplo 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)
Ejemplo 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)
Ejemplo 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)
Ejemplo 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)
Ejemplo 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)
Ejemplo 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)
Ejemplo 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)
Ejemplo 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)
Ejemplo 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]})
Ejemplo n.º 30
0
 def test_valid_orientation(self):
     Z = linkage(hierarchy_test_data.ytdist, 'single')
     assert_raises(ValueError, dendrogram, Z, orientation="foo")
Ejemplo 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])
Ejemplo 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))
Ejemplo 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)
Ejemplo 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)
Ejemplo 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)
Ejemplo 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)
Ejemplo 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)
Ejemplo 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)
Ejemplo 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])
Ejemplo n.º 40
0
 def test_valid_orientation(self):
     Z = linkage(hierarchy_test_data.ytdist, 'single')
     assert_raises(ValueError, dendrogram, Z, orientation="foo")
Ejemplo 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))
Ejemplo 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)
Ejemplo 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)
Ejemplo 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)
Ejemplo 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)
Ejemplo 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)
Ejemplo 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)