示例#1
0
def test_plot_connectome_strength_exceptions():
    node_coords = np.arange(2 * 3).reshape((2, 3))

    # Used to speed-up tests because the glass brain is always plotted
    # before any error occurs
    kwargs = {'display_mode': 'x'}

    # adjacency_matrix is not symmetric
    non_symmetric_adjacency_matrix = np.array([[1., 2], [0.4, 1.]])
    with pytest.raises(ValueError, match='should be symmetric'):
        plot_connectome_strength(non_symmetric_adjacency_matrix, node_coords,
                                 **kwargs)

    adjacency_matrix = np.array([[1., 2.], [2., 1.]])
    # adjacency_matrix mask is not symmetric
    masked_adjacency_matrix = np.ma.masked_array(
        adjacency_matrix, [[False, True], [False, False]])

    with pytest.raises(ValueError, match='non symmetric mask'):
        plot_connectome_strength(masked_adjacency_matrix, node_coords,
                                 **kwargs)

    # wrong shapes for node_coords or adjacency_matrix
    with pytest.raises(ValueError,
                       match=r'supposed to have shape \(n, n\).+\(1L?, 2L?\)'):
        plot_connectome_strength(adjacency_matrix[:1, :], node_coords,
                                 **kwargs)

    with pytest.raises(ValueError, match=r'shape \(2L?, 3L?\).+\(2L?,\)'):
        plot_connectome_strength(adjacency_matrix, node_coords[:, 2], **kwargs)

    wrong_adjacency_matrix = np.zeros((3, 3))
    with pytest.raises(ValueError,
                       match=r'Shape mismatch.+\(3L?, 3L?\).+\(2L?, 3L?\)'):
        plot_connectome_strength(wrong_adjacency_matrix, node_coords, **kwargs)
示例#2
0
def test_plot_connectome_strength_deprecation_warning():
    with pytest.deprecated_call():
        adjacency_matrix = np.array([[1, -2, 0.3, 0.],
                                     [-2, 1, 0, 0],
                                     [0.3, 0, 1, 0],
                                     [0, 0, 0, 1]])
        node_coords = np.arange(3 * 4).reshape(4, 3)
        plot_connectome_strength(adjacency_matrix, node_coords)
示例#3
0
def test_connectome_strength():
    # symmetric up to 1e-3 relative tolerance
    adjacency_matrix = np.array([[1., -2., 0.3, 0.], [-2.002, 1, 0., 0.],
                                 [0.3, 0., 1., 0.], [0., 0., 0., 1.]])
    node_coords = np.arange(3 * 4).reshape(4, 3)

    args = adjacency_matrix, node_coords
    kwargs = dict()
    plot_connectome_strength(*args, **kwargs)
    plt.close()

    # used to speed-up tests for the net plots
    kwargs['display_mode'] = 'x'

    # node_coords not an array but a list of tuples
    plot_connectome_strength(adjacency_matrix,
                             [tuple(each) for each in node_coords], **kwargs)

    # saving to file
    filename = tempfile.mktemp(suffix='.png')
    try:
        display = plot_connectome_strength(*args,
                                           output_file=filename,
                                           **kwargs)
        assert display is None
        assert (os.path.isfile(filename) and  # noqa: W504
                os.path.getsize(filename) > 0)
    finally:
        os.remove(filename)
    plt.close()

    # passing node args
    plot_connectome_strength(*args, node_size=10, cmap='RdBu')
    plt.close()
    plot_connectome_strength(*args, node_size=10, cmap=plt.cm.RdBu)
    plt.close()

    # masked array support
    masked_adjacency_matrix = np.ma.masked_array(
        adjacency_matrix,
        np.abs(adjacency_matrix) < 0.5)
    plot_connectome_strength(masked_adjacency_matrix, node_coords, **kwargs)
    plt.close()

    # sparse matrix support
    sparse_adjacency_matrix = sparse.coo_matrix(adjacency_matrix)
    plot_connectome_strength(sparse_adjacency_matrix, node_coords, **kwargs)
    plt.close()

    # NaN matrix support
    nan_adjacency_matrix = np.array([[1., np.nan, 0.], [np.nan, 1., 2.],
                                     [np.nan, 2., 1.]])
    nan_node_coords = np.arange(3 * 3).reshape(3, 3)
    plot_connectome_strength(nan_adjacency_matrix, nan_node_coords, **kwargs)
    plt.close()

    # smoke-test with hemispheric sagital cuts
    plot_connectome_strength(*args, display_mode='lzry')
    plt.close()