예제 #1
0
def test_matrix_plotting():
    from numpy import zeros
    from distutils.version import LooseVersion
    mat = zeros((10, 10))
    labels = [str(i) for i in range(10)]
    ax = plot_matrix(mat, labels=labels, title='foo')
    plt.close()
    # test if plotting lower triangle works
    ax = plot_matrix(mat, labels=labels, tri='lower')
    # test if it returns an AxesImage
    ax.axes.set_title('Title')
    plt.close()
    import scipy
    if LooseVersion(scipy.__version__) >= LooseVersion('1.0.0'):
        # test if a ValueError is raised when reorder=True without labels
        assert_raises(ValueError, plot_matrix, mat, labels=None, reorder=True)
        # test if a ValueError is raised when reorder argument is wrong
        assert_raises(ValueError, plot_matrix, mat, labels=labels, reorder=' ')
        # test if reordering with default linkage works
        idx = [2, 3, 5]
        from itertools import permutations
        # make symmetric matrix of similarities so we can get a block
        for perm in permutations(idx, 2):
            mat[perm] = 1
        ax = plot_matrix(mat, labels=labels, reorder=True)
        assert_equal(len(labels), len(ax.axes.get_xticklabels()))
        reordered_labels = [int(lbl.get_text())
                            for lbl in ax.axes.get_xticklabels()]
        # block order does not matter
        assert_true(reordered_labels[:3] == idx or reordered_labels[-3:] == idx,
                    'Clustering does not find block structure.')
        plt.close()
        # test if reordering with specific linkage works
        ax = plot_matrix(mat, labels=labels, reorder='complete')
        plt.close()
예제 #2
0
def test_matrix_plotting():
    from numpy import zeros
    mat = zeros((10, 10))
    labels = str(range(10))
    ax = plot_matrix(mat, labels=labels, title='foo')
    plt.close()
    # test if plotting lower triangle works
    ax = plot_matrix(mat, labels=labels, tri='lower')
    # test if it returns an AxesImage
    ax.axes.set_title('Title')
    plt.close()
예제 #3
0
def test_matrix_plotting_set_title(mat, labels, title):
    ax = plot_matrix(mat, labels=labels, title=title)
    nb_txt = 0 if title is None else 1
    assert len(ax._axes.texts) == nb_txt
    if title is not None:
        assert ax._axes.texts[0].get_text() == title
    plt.close()
예제 #4
0
def test_matrix_plotting_with_labels_and_different_tri(mat, labels, tri):
    ax = plot_matrix(mat, labels=labels, tri=tri)
    assert isinstance(ax, mpl.image.AxesImage)
    ax.axes.set_title('Title')
    assert ax._axes.get_title() == 'Title'
    for axis in [ax._axes.xaxis, ax._axes.yaxis]:
        assert len(axis.majorTicks) == len(labels)
        for tick, label in zip(axis.majorTicks, labels):
            assert tick.label1.get_text() == label
    plt.close()
예제 #5
0
def test_matrix_plotting_reorder(mat, labels):
    from itertools import permutations
    # test if reordering with default linkage works
    idx = [2, 3, 5]
    # make symmetric matrix of similarities so we can get a block
    for perm in permutations(idx, 2):
        mat[perm] = 1
    ax = plot_matrix(mat, labels=labels, reorder=True)
    assert len(labels) == len(ax.axes.get_xticklabels())
    reordered_labels = [
        int(lbl.get_text()) for lbl in ax.axes.get_xticklabels()
    ]
    # block order does not matter
    assert (  # noqa
        (reordered_labels[:3] == idx or reordered_labels[-3:] == idx),
        'Clustering does not find block structure.')
    plt.close()
    # test if reordering with specific linkage works
    ax = plot_matrix(mat, labels=labels, reorder='complete')
    plt.close()
예제 #6
0
def test_matrix_plotting():
    from numpy import zeros, array
    from distutils.version import LooseVersion
    mat = zeros((10, 10))
    labels = [str(i) for i in range(10)]
    ax = plot_matrix(mat, labels=labels, title='foo')
    plt.close()
    # test if plotting lower triangle works
    ax = plot_matrix(mat, labels=labels, tri='lower')
    # test if it returns an AxesImage
    ax.axes.set_title('Title')
    plt.close()
    ax = plot_matrix(mat, labels=labels, tri='diag')
    ax.axes.set_title('Title')
    plt.close()
    # test if an empty list works as an argument for labels
    ax = plot_matrix(mat, labels=[])
    plt.close()
    # test if an array gets correctly cast to a list
    ax = plot_matrix(mat, labels=array(labels))
    plt.close()
    # test if labels can be None
    ax = plot_matrix(mat, labels=None)
    plt.close()
    pytest.raises(ValueError, plot_matrix, mat, labels=[0, 1, 2])

    import scipy
    if LooseVersion(scipy.__version__) >= LooseVersion('1.0.0'):
        # test if a ValueError is raised when reorder=True without labels
        pytest.raises(ValueError, plot_matrix, mat, labels=None, reorder=True)
        # test if a ValueError is raised when reorder argument is wrong
        pytest.raises(ValueError, plot_matrix, mat, labels=labels, reorder=' ')
        # test if reordering with default linkage works
        idx = [2, 3, 5]
        from itertools import permutations
        # make symmetric matrix of similarities so we can get a block
        for perm in permutations(idx, 2):
            mat[perm] = 1
        ax = plot_matrix(mat, labels=labels, reorder=True)
        assert len(labels) == len(ax.axes.get_xticklabels())
        reordered_labels = [
            int(lbl.get_text()) for lbl in ax.axes.get_xticklabels()
        ]
        # block order does not matter
        assert reordered_labels[:3] == idx or reordered_labels[
            -3:] == idx, 'Clustering does not find block structure.'
        plt.close()
        # test if reordering with specific linkage works
        ax = plot_matrix(mat, labels=labels, reorder='complete')
        plt.close()
예제 #7
0
def test_matrix_plotting_grid(mat, labels, tri):
    plot_matrix(mat, labels=labels, grid=True, tri=tri)
예제 #8
0
def test_matrix_plotting_labels(mat, lab):
    plot_matrix(mat, labels=lab)
    plt.close()
예제 #9
0
def test_matrix_plotting_errors(matrix, lab, reorder):
    with pytest.raises(ValueError):
        plot_matrix(matrix, labels=lab, reorder=reorder)
        plt.close()