def test_plot_anat():
    img = _generate_img()

    # Test saving with empty plot
    z_slicer = plot_anat(anat_img=False, display_mode='z')
    filename = tempfile.mktemp(suffix='.png')
    try:
        z_slicer.savefig(filename)
    finally:
        os.remove(filename)

    z_slicer = plot_anat(display_mode='z')
    filename = tempfile.mktemp(suffix='.png')
    try:
        z_slicer.savefig(filename)
    finally:
        os.remove(filename)

    ortho_slicer = plot_anat(img, dim='auto')
    filename = tempfile.mktemp(suffix='.png')
    try:
        ortho_slicer.savefig(filename)
    finally:
        os.remove(filename)

    # Save execution time and memory
    plt.close()
Beispiel #2
0
def test_plot_anat():
    img = _generate_img()

    # Test saving with empty plot
    z_slicer = plot_anat(anat_img=False, display_mode='z')
    filename = tempfile.mktemp(suffix='.png')
    try:
        z_slicer.savefig(filename)
    finally:
        os.remove(filename)

    z_slicer = plot_anat(display_mode='z')
    filename = tempfile.mktemp(suffix='.png')
    try:
        z_slicer.savefig(filename)
    finally:
        os.remove(filename)

    ortho_slicer = plot_anat(img, dim='auto')
    filename = tempfile.mktemp(suffix='.png')
    try:
        ortho_slicer.savefig(filename)
    finally:
        os.remove(filename)

    # Save execution time and memory
    plt.close()
Beispiel #3
0
def test_plot_functions():
    # This is only a smoke test
    mp.use('template', warn=False)
    import pylab as pl
    pl.switch_backend('template')
    data_positive = np.zeros((7, 7, 3))
    data_positive[1:-1, 2:-1, 1:] = 1
    data_negative = -data_positive
    rng = np.random.RandomState(42)
    data_heterogeneous = data_positive * rng.randn(*data_positive.shape)
    img_positive = nibabel.Nifti1Image(data_positive, mni_affine)
    img_negative = nibabel.Nifti1Image(data_negative, mni_affine)
    img_heterogeneous = nibabel.Nifti1Image(data_heterogeneous, mni_affine)

    # Test saving with empty plot
    ax = pl.subplot(111, rasterized=True)
    z_slicer = plot_anat(anat_img=False, display_mode='z', axes=ax)
    z_slicer = plot_anat(display_mode='z', axes=ax)
    z_slicer.add_edges(img_positive, color='c')
    z_slicer.savefig(tempfile.TemporaryFile())
    pl.close()
    for img in [img_positive, img_negative, img_heterogeneous]:
        ortho_slicer = plot_anat(img, dim=True)
        ortho_slicer.savefig(tempfile.TemporaryFile())
        pl.close()

        for func in [
                plot_anat, plot_img, plot_stat_map, plot_epi, plot_glass_brain,
                partial(plot_stat_map, symmetric_cbar=True),
                partial(plot_stat_map, symmetric_cbar=False),
                partial(plot_stat_map, symmetric_cbar=False, vmax=10),
                partial(plot_stat_map, symmetric_cbar=True, vmax=10),
                partial(plot_stat_map, colorbar=False)
        ]:
            ax = pl.subplot(111, rasterized=True)
            ortho_slicer = func(img, cut_coords=(80, -120, -60), axes=ax)
            # Saving forces a draw, and thus smoke-tests the axes locators
            ortho_slicer.savefig(tempfile.TemporaryFile())
            ortho_slicer.add_edges(img, color='c')
            pl.close()

            # Smoke test coordinate finder, with and without mask
            masked_img = nibabel.Nifti1Image(
                np.ma.masked_equal(img.get_data(), 0), mni_affine)
            ax = pl.subplot(111, rasterized=True)
            func(masked_img, display_mode='x', axes=ax)
            pl.close()
            ax = pl.subplot(111, rasterized=True)
            func(img, display_mode='y', axes=ax)
            pl.close()

            ax = pl.subplot(111, rasterized=True)
            out = func(img,
                       output_file=tempfile.TemporaryFile(suffix='.png'),
                       axes=ax)
            assert_true(out is None)
            pl.close()
Beispiel #4
0
def test_plot_img_empty():
    # Test that things don't crash when we give a map with nothing above
    # threshold
    # This is only a smoke test
    mp.use('template', warn=False)
    import pylab as pl
    pl.switch_backend('template')
    data = np.zeros((20, 20, 20))
    img = nibabel.Nifti1Image(data, mni_affine)
    plot_anat(img)
    slicer = plot_img(img, display_mode='y', threshold=1)
    slicer.close()
    pl.close('all')
def test_plot_anat():
    img = _generate_img()

    # Test saving with empty plot
    z_slicer = plot_anat(anat_img=False, display_mode='z')
    with tempfile.NamedTemporaryFile(suffix='.png') as fp:
        z_slicer.savefig(fp.name)
    z_slicer = plot_anat(display_mode='z')
    with tempfile.NamedTemporaryFile(suffix='.png') as fp:
        z_slicer.savefig(fp.name)

    ortho_slicer = plot_anat(img, dim=True)
    with tempfile.NamedTemporaryFile(suffix='.png') as fp:
        ortho_slicer.savefig(fp.name)
Beispiel #6
0
def test_plot_anat():
    img = _generate_img()

    # Test saving with empty plot
    z_slicer = plot_anat(anat_img=False, display_mode='z')
    with tempfile.NamedTemporaryFile(suffix='.png') as fp:
        z_slicer.savefig(fp.name)
    z_slicer = plot_anat(display_mode='z')
    with tempfile.NamedTemporaryFile(suffix='.png') as fp:
        z_slicer.savefig(fp.name)

    ortho_slicer = plot_anat(img, dim=True)
    with tempfile.NamedTemporaryFile(suffix='.png') as fp:
        ortho_slicer.savefig(fp.name)
Beispiel #7
0
def test_plot_anat(testdata_3d, tmpdir):
    img = testdata_3d['img']

    # Test saving with empty plot
    z_slicer = plot_anat(anat_img=False, display_mode='z')
    filename = str(tmpdir.join('test.png'))
    z_slicer.savefig(filename)

    z_slicer = plot_anat(display_mode='z')
    z_slicer.savefig(filename)

    ortho_slicer = plot_anat(img, dim='auto')
    ortho_slicer.savefig(filename)

    # Save execution time and memory
    plt.close()
Beispiel #8
0
def test_plot_anat():
    mp.use('template', warn=False)
    import matplotlib.pyplot as plt
    plt.switch_backend('template')
    img = _generate_img()

    # Test saving with empty plot
    z_slicer = plot_anat(anat_img=False, display_mode='z')
    with tempfile.TemporaryFile(suffix='.png') as fp:
        z_slicer.savefig(fp.name)
    z_slicer = plot_anat(display_mode='z')
    with tempfile.TemporaryFile(suffix='.png') as fp:
        z_slicer.savefig(fp.name)

    ortho_slicer = plot_anat(img, dim=True)
    with tempfile.TemporaryFile(suffix='.png') as fp:
        ortho_slicer.savefig(fp.name)
Beispiel #9
0
def test_plot_anat():
    mp.use('template', warn=False)
    import matplotlib.pyplot as plt
    plt.switch_backend('template')
    img = _generate_img()

    # Test saving with empty plot
    z_slicer = plot_anat(anat_img=False, display_mode='z')
    with tempfile.TemporaryFile(suffix='.png') as fp:
        z_slicer.savefig(fp.name)
    z_slicer = plot_anat(display_mode='z')
    with tempfile.TemporaryFile(suffix='.png') as fp:
        z_slicer.savefig(fp.name)

    ortho_slicer = plot_anat(img, dim=True)
    with tempfile.TemporaryFile(suffix='.png') as fp:
        ortho_slicer.savefig(fp.name)
Beispiel #10
0
def test_plotting_functions_with_nans_in_bg_img():
    bg_img = _generate_img()
    bg_data = bg_img.get_data()

    bg_data[6, 5, 1] = np.nan
    bg_data[1, 5, 2] = np.nan
    bg_data[1, 3, 2] = np.nan
    bg_data[6, 5, 2] = np.inf

    bg_img = nibabel.Nifti1Image(bg_data, mni_affine)
    plot_anat(bg_img)
    # test with plot_roi passing background image which contains nans values
    # in it
    roi_img = _generate_img()
    plot_roi(roi_img=roi_img, bg_img=bg_img)
    stat_map_img = _generate_img()
    plot_stat_map(stat_map_img=stat_map_img, bg_img=bg_img)

    plt.close()
def test_plot_anat():
    img = _generate_img()

    # Test saving with empty plot
    z_slicer = plot_anat(anat_img=False, display_mode='z')
    filename = tempfile.mktemp(suffix='.png')
    try:
        z_slicer.savefig(filename)
    finally:
        os.remove(filename)

    z_slicer = plot_anat(display_mode='z')
    filename = tempfile.mktemp(suffix='.png')
    try:
        z_slicer.savefig(filename)
    finally:
        os.remove(filename)

    ortho_slicer = plot_anat(img, dim=True)
    filename = tempfile.mktemp(suffix='.png')
    try:
        ortho_slicer.savefig(filename)
    finally:
        os.remove(filename)
Beispiel #12
0
def test_plot_anat():
    img = _generate_img()

    # Test saving with empty plot
    z_slicer = plot_anat(anat_img=False, display_mode='z')
    filename = tempfile.mktemp(suffix='.png')
    try:
        z_slicer.savefig(filename)
    finally:
        os.remove(filename)

    z_slicer = plot_anat(display_mode='z')
    filename = tempfile.mktemp(suffix='.png')
    try:
        z_slicer.savefig(filename)
    finally:
        os.remove(filename)

    ortho_slicer = plot_anat(img, dim=True)
    filename = tempfile.mktemp(suffix='.png')
    try:
        ortho_slicer.savefig(filename)
    finally:
        os.remove(filename)
Beispiel #13
0
def test_plotting_functions_with_display_mode_tiled():
    img = _generate_img()
    plot_stat_map(img, display_mode='tiled')
    plot_anat(display_mode='tiled')
    plot_img(img, display_mode='tiled')
    plt.close()
combined_df.to_csv('%s/metadata.tsv' % dest_dir, encoding='utf8',sep="\t")

# Example 1: Cognitive Decoding with NeuroSynth
terms_df = get_neurosynth_terms(combined_df)
print combined_df["name_collection"].value_counts()
combined_df = combined_df.merge(terms_df, on=['image_id', ])


# 3) Do analysis

#--------------------------------------------------
# Plot a map of frequency of activation
freq_nii = get_frequency_map(combined_df, dest_dir, target)
freq_nii.to_filename("%s/freq_map.nii.gz" %dest_dir)
display = plot_anat("/usr/share/fsl/data/standard/MNI152_T1_2mm.nii.gz",
                    display_mode='z',
                    cut_coords=np.linspace(-30, 60, 7))
display.add_overlay(freq_nii, vmin=0, cmap=plt.cm.hot,colorbar=True)
display._colorbar_ax.set_yticklabels(["% 3i" % float(t.get_text()) for t in display._colorbar_ax.yaxis.get_ticklabels()])
display.title('Percentage of activations (Z or T > 3)')
display.savefig('%s/activation_frequency.png' %(dest_dir))
display.savefig('%s/activation_frequency.pdf' %(dest_dir))

#--------------------------------------------------
# Plot the frequency of occurence of neurosynth terms
# Use the terms from neurosynth to label the ICAs
terms = combined_df[[c for c in combined_df.columns if c.startswith('neurosynth decoding')]]
terms = terms.fillna(0)
term_matrix = terms.as_matrix()

# Labels that have a negative correlation are not present in the map
Beispiel #15
0
def test_plotting_functions_with_display_mode_tiled(testdata_3d):
    img = testdata_3d['img']
    plot_stat_map(img, display_mode='tiled')
    plot_anat(display_mode='tiled')
    plot_img(img, display_mode='tiled')
    plt.close()
Beispiel #16
0
def plot_mr(input_mr,output_png):
  display = plot_anat(standard,display_mode='z',cut_coords=np.linspace(-30, 60, 7))
  display.add_overlay(newimage, vmin=0,cmap=plt.cm.hot, colorbar=True)
  display._colorbar_ax.set_yticklabels(["% 3i" % (100 * float(t.get_text())) for t in display._colorbar_ax.yaxis.get_ticklabels()])
  display.title("Vanessa's First Plot in Nilearn!'")
  display.savefig(output_png)