Beispiel #1
0
def test_plot_functions():
    # This is only a smoke test
    mp.use('svg', warn=False)
    import pylab as pl
    pl.switch_backend('svg')
    data = np.zeros((20, 20, 20))
    data[3:-3, 3:-3, 3:-3] = 1
    img = nibabel.Nifti1Image(data, mni_affine)
    ortho_slicer = plot_anat(img, dim=True)
    # Test saving with empty plot
    z_slicer = plot_anat(anat_img=False, display_mode='z')
    ortho_slicer.savefig(tempfile.TemporaryFile())
    z_slicer = plot_anat(display_mode='z')
    ortho_slicer.savefig(tempfile.TemporaryFile())
    z_slicer.add_edges(img, color='c')

    for func in [
            plot_anat, plot_img, plot_stat_map, plot_epi, plot_glass_brain
    ]:
        ortho_slicer = func(img, cut_coords=(80, -120, -60))
        # Saving forces a draw, and thus smoke-tests the axes locators
        ortho_slicer.savefig(tempfile.TemporaryFile())
        ortho_slicer.add_edges(img, color='c')

        # Smoke test coordinate finder, with and without mask
        masked_img = nibabel.Nifti1Image(np.ma.masked_equal(data, 0),
                                         mni_affine)
        func(masked_img, display_mode='x')
        func(img, display_mode='y')

        out = func(img, output_file=tempfile.TemporaryFile(suffix='.png'))
        assert_true(out is None)
    pl.close('all')
def reason_pylab():
    from .utils import deliver_image
    def _canvas_deliver(canvas):
        tf = tempfile.TemporaryFile()
        canvas.print_png(tf)
        tf.seek(0)
        img_data = base64.b64encode(tf.read())
        tf.close()
        deliver_image(img_data)
    def reason_draw_if_interactive():
        if matplotlib.is_interactive():
            figManager =  Gcf.get_active()
            if figManager is not None:
                _canvas_deliver(figManager.canvas)
    def reason_show(mainloop = True):
        # We ignore mainloop here
        for manager in Gcf.get_all_fig_managers():
            _canvas_deliver(manager.canvas)
    # Matplotlib has very nice backend overriding.
    # We should really use that.  This is just a hack.
    import matplotlib
    matplotlib.use("agg") # Hotfix for when we import pylab below
    new_agg = imp.new_module("reason_agg")
    import matplotlib.backends.backend_agg as bagg
    new_agg.__dict__.update(bagg.__dict__)
    new_agg.__dict__.update(
        {'show': reason_show,
         'draw_if_interactive': reason_draw_if_interactive})
    sys.modules["reason_agg"] = new_agg
    bagg.draw_if_interactive = reason_draw_if_interactive
    from matplotlib._pylab_helpers import Gcf
    matplotlib.rcParams["backend"] = "module://reason_agg"
    import pylab
    pylab.switch_backend("module://reason_agg")
def test_plot_anat():
    # This is only a smoke test
    mp.use('svg', warn=False)
    import pylab as pl
    pl.switch_backend('svg')
    data = np.zeros((20, 20, 20))
    data[3:-3, 3:-3, 3:-3] = 1
    img = nibabel.Nifti1Image(data, mni_affine)
    ortho_slicer = plot_anat(img, dim=True)
    ortho_slicer = plot_anat(img, cut_coords=(80, -120, -60))
    # Saving forces a draw, and thus smoke-tests the axes locators
    pl.savefig(tempfile.TemporaryFile())
    ortho_slicer.edge_map(img, color='c')

    # Test saving with empty plot
    z_slicer = plot_anat(anat_img=False, slicer='z')
    pl.savefig(tempfile.TemporaryFile())
    z_slicer = plot_anat(slicer='z')
    pl.savefig(tempfile.TemporaryFile())
    z_slicer.edge_map(img, color='c')
    # Smoke test coordinate finder, with and without mask
    masked_img = nibabel.Nifti1Image(np.ma.masked_equal(data, 0),
                                     mni_affine)
    plot_img(masked_img, slicer='x')
    plot_img(img, slicer='y')
Beispiel #4
0
def test_demo_plot_map():
    # This is only a smoke test
    mp.use("svg", warn=False)
    import pylab as pl

    pl.switch_backend("svg")
    demo_plot_map()
Beispiel #5
0
def test_plot_anat():
    # This is only a smoke test
    mp.use('svg', warn=False)
    import pylab as pl
    pl.switch_backend('svg')
    data = np.zeros((20, 20, 20))
    data[3:-3, 3:-3, 3:-3] = 1
    ortho_slicer = plot_anat(data, mni_sform, dim=True)
    ortho_slicer = plot_anat(data, mni_sform, cut_coords=(80, -120, -60))
    # Saving forces a draw, and thus smoke-tests the axes locators
    pl.savefig(tempfile.TemporaryFile())
    pl.close()
    ortho_slicer.edge_map(data, mni_sform, color='c')

    # Test saving with empty plot
    z_slicer = plot_anat(anat=False, slicer='z')
    pl.savefig(tempfile.TemporaryFile())
    pl.close()
    z_slicer = plot_anat(slicer='z')
    pl.savefig(tempfile.TemporaryFile())
    pl.close()
    z_slicer.edge_map(data, mni_sform, color='c')
    # Smoke test coordinate finder, with and without mask
    plot_map(np.ma.masked_equal(data, 0), mni_sform, slicer='x')
    plot_map(data, mni_sform, slicer='y')
Beispiel #6
0
def test_replace_inside():
    # This is only a smoke test
    mp.use("svg", warn=False)
    import pylab as pl

    pl.switch_backend("svg")
    replace_inside(pl.cm.jet, pl.cm.hsv, 0.2, 0.8)
def main(config, display = False):
    """
    Main part of the script
    """
    
    # uncomment the following line if you have no display access
    if display == False:
        plt.switch_backend('agg')
    else:
        None
    
    assert os.path.isfile(config), "Cannot find the config file!"
    
    # tries to create a folder in the same dir as the .config
    # if it fails, creates in the current directory
    outputFolder = create_output_folder(config) 
    
    recovClsDict = get_recov_cls_dict(config) 
    
    if checkDens2Kappa(config) == False:
        inputClsDict =  get_input_cls_dict(config, recovClsDict) 
    else:
        inputClsDict = Dens2KappaUsed(config, recovClsDict)
        
    plot_recov_vs_input(recovClsDict, inputClsDict, outputFolder, display)
Beispiel #8
0
def stdan(path, tday):
    #f = open('/home/larry/l1304/workspace/finopt/data/mds_files/std/std20151005.txt')
    pylab.switch_backend(
        'agg'
    )  # switch to agg backend that support writing in non-main threads
    f = open(join(path, '%s.txt' % tday))
    l = f.readlines()
    m = map(lambda x: (x.split(',')), l)
    q = filter(
        lambda y: y[0] in ['HSI-20151029-0--FUT-HKD-102'] and y[1] >
        '2015-10-06 08:55:34', m)
    n = filter(lambda y: float(y[3]) > 21500.0, q)

    p = map(lambda y: float(y[2]) if float(y[2]) < 15.0 else 0.0, n)

    yy = map(lambda y: float(y[3]), n)
    xx = map(lambda x: datetime.strptime(x[1], '%Y-%m-%d %H:%M:%S.%f'), n)

    print len(p), len(yy)
    pylab.figure(figsize=(20, 10))
    pylab.figure(1)
    pylab.subplot(211)
    pylab.plot(xx, yy, 'g-')
    pylab.subplot(212)
    pylab.plot(xx, p, 'ro')
    #pylab.axis(['20150930', '20151001', 20000, 22000])

    pylab.show()
    pylab.savefig('%s/std-%s.png' % (path, tday))
    pylab.close()
Beispiel #9
0
def test_plot_functions():
    # This is only a smoke test
    mp.use('svg', warn=False)
    import pylab as pl
    pl.switch_backend('svg')
    data = np.zeros((20, 20, 20))
    data[3:-3, 3:-3, 3:-3] = 1
    img = nibabel.Nifti1Image(data, mni_affine)
    ortho_slicer = plot_anat(img, dim=True)
    # Test saving with empty plot
    z_slicer = plot_anat(anat_img=False, display_mode='z')
    ortho_slicer.savefig(tempfile.TemporaryFile())
    z_slicer = plot_anat(display_mode='z')
    ortho_slicer.savefig(tempfile.TemporaryFile())
    z_slicer.add_edges(img, color='c')

    for func in [plot_anat, plot_img, plot_stat_map,
                 plot_epi, plot_glass_brain]:
        ortho_slicer = func(img, cut_coords=(80, -120, -60))
        # Saving forces a draw, and thus smoke-tests the axes locators
        ortho_slicer.savefig(tempfile.TemporaryFile())
        ortho_slicer.add_edges(img, color='c')

        # Smoke test coordinate finder, with and without mask
        masked_img = nibabel.Nifti1Image(np.ma.masked_equal(data, 0),
                                         mni_affine)
        func(masked_img, display_mode='x')
        func(img, display_mode='y')

        out = func(img, output_file=tempfile.TemporaryFile(suffix='.png'))
        assert_true(out is None)
    pl.close('all')
Beispiel #10
0
def test_plots(do_plot=False):
    ''' Basic plots '''
    if not do_plot:
        pl.switch_backend('agg')  # Plot but don't show
    pop = sp.Pop(n=5000)  # default parameters, 5k people
    fig1 = pop.plot_people()  # equivalent to cv.Sim.people.plot()
    fig2 = pop.plot_contacts()  # equivalent to sp.plot_contact_matrix(popdict)
    return [fig1, fig2]
def test_demo_plot_map():
    # This is only a smoke test
    mp.use('svg', warn=False)
    import pylab as pl
    pl.switch_backend('svg')
    demo_plot_map()
    # Test the black background code path
    demo_plot_map(black_bg=True)
Beispiel #12
0
def test_demo_plot_map():
    # This is only a smoke test
    mp.use('svg', warn=False)
    import pylab as pl
    pl.switch_backend('svg')
    demo_plot_map()
    # Test the black background code path
    demo_plot_map(black_bg=True)
Beispiel #13
0
def test_replace_inside():
    # This is only a smoke test
    mp.use('svg', warn=False)
    import pylab as pl
    pl.switch_backend('svg')
    replace_inside(pl.cm.jet, pl.cm.hsv, .2, .8)
    # We also test with gnuplot, which is defined using function
    replace_inside(pl.cm.gnuplot, pl.cm.gnuplot2, .2, .8)
Beispiel #14
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 #15
0
def test_plot_img_with_resampling():
    import pylab as pl
    pl.switch_backend('template')
    data = MNI152TEMPLATE.get_data()[:5, :5, :5]
    affine = np.array([[1., -1., 0., 0.], [1., 1., 0., 0.], [0., 0., 1., 0.],
                       [0., 0., 0., 1.]])
    img = nibabel.Nifti1Image(data, affine)
    display = plot_img(img)
    display.add_overlay(img)
Beispiel #16
0
def test_plot_map_with_auto_cut_coords():
    import pylab as pl
    pl.switch_backend('svg')
    data = np.zeros((20, 20, 20))
    data[3:-3, 3:-3, 3:-3] = 1

    for slicer in 'xyz':
        plot_map(data, np.eye(4), cut_coords=None, slicer=slicer,
                 black_bg=True)
Beispiel #17
0
def test_plot_anat():
    # This is only a smoke test
    mp.use('svg', warn=False)
    import pylab as pl
    pl.switch_backend('svg')
    ortho_slicer = plot_anat()
    data = np.zeros((100, 100, 100))
    data[3:-3, 3:-3, 3:-3] = 1
    ortho_slicer.edge_map(data, mni_sform, color='c')
Beispiel #18
0
def test_demo_ortho_slicer():
    # This is only a smoke test
    # conditioned on presence of MNI templated
    if not find_mni_template():
        raise nose.SkipTest("MNI Template is absent for the smoke test")
    mp.use('svg')
    import pylab as pl
    pl.switch_backend('svg')
    demo_ortho_slicer()
Beispiel #19
0
def test_demo_ortho_slicer():
    # This is only a smoke test
    # conditioned on presence of MNI templated
    if not find_mni_template():
        raise nose.SkipTest("MNI Template is absent for the smoke test")
    mp.use('svg', warn=False)
    import pylab as pl
    pl.switch_backend('svg')
    demo_ortho_slicer()
def test_plot_img_with_auto_cut_coords():
    import pylab as pl
    pl.switch_backend('svg')
    data = np.zeros((20, 20, 20))
    data[3:-3, 3:-3, 3:-3] = 1
    img = nibabel.Nifti1Image(data, np.eye(4))

    for slicer in 'xyz':
        plot_img(img, cut_coords=None, slicer=slicer, black_bg=True)
def test_plot_img_with_auto_cut_coords():
    import pylab as pl
    pl.switch_backend('svg')
    data = np.zeros((20, 20, 20))
    data[3:-3, 3:-3, 3:-3] = 1
    img = nibabel.Nifti1Image(data, np.eye(4))

    for slicer in 'xyz':
        plot_img(img, cut_coords=None, slicer=slicer, black_bg=True)
Beispiel #22
0
def test_demo_ortho_slicer():
    # This is only a smoke test
    # conditioned on presence of MNI templated
    mp.use('svg', warn=False)
    import pylab as pl
    pl.switch_backend('svg')
    pl.clf()
    oslicer = OrthoSlicer(cut_coords=(0, 0, 0))
    img = load_mni152_template()
    oslicer.add_overlay(img, cmap=pl.cm.gray)
Beispiel #23
0
def set_matplotlib_global(key, value):
    ''' Set a global option for Matplotlib -- not for users '''
    import pylab as pl
    if value:  # Don't try to reset any of these to a None value
        if key == 'font_size': pl.rc('font', size=value)
        elif key == 'font_family': pl.rc('font', family=value)
        elif key == 'dpi': pl.rc('figure', dpi=value)
        elif key == 'backend': pl.switch_backend(value)
        else: raise sc.KeyNotFoundError(f'Key {key} not found')
    return
Beispiel #24
0
def test_replace_inside():
    # This is only a smoke test
    mp.use('svg', warn=False)
    import pylab as pl
    pl.switch_backend('svg')
    replace_inside(pl.cm.jet, pl.cm.hsv, .2, .8)
    # We also test with gnuplot, which is defined using function
    if hasattr(pl.cm, 'gnuplot'):
        # gnuplot is only in recent version of MPL
        replace_inside(pl.cm.gnuplot, pl.cm.gnuplot2, .2, .8)
Beispiel #25
0
def test_demo_ortho_slicer():
    # This is only a smoke test
    mp.use('svg', warn=False)
    import pylab as pl
    pl.switch_backend('svg')
    pl.clf()
    oslicer = OrthoSlicer(cut_coords=(0, 0, 0))
    img = load_mni152_template()
    oslicer.add_overlay(img, cmap=pl.cm.gray)
    oslicer.close()
Beispiel #26
0
def test_demo_ortho_slicer():
    # This is only a smoke test
    mp.use('template', warn=False)
    import pylab as pl
    pl.switch_backend('template')
    pl.clf()
    oslicer = OrthoSlicer(cut_coords=(0, 0, 0))
    img = load_mni152_template()
    oslicer.add_overlay(img, cmap=pl.cm.gray)
    oslicer.close()
Beispiel #27
0
def test_demo_plot_roi():
    # This is only a smoke test
    mp.use('svg', warn=False)
    import pylab as pl
    pl.switch_backend('svg')
    demo_plot_roi()
    # Test the black background code path
    demo_plot_roi(black_bg=True)

    out = demo_plot_roi(output_file=tempfile.TemporaryFile(suffix='.png'))
    assert_true(out is None)
Beispiel #28
0
def test_demo_ortho_projector():
    # This is only a smoke test
    mp.use('svg', warn=False)
    import pylab as pl
    pl.switch_backend('svg')
    pl.clf()
    img = load_mni152_template()
    oprojector = OrthoProjector.init_with_figure(img=img)
    oprojector.add_overlay(img, cmap=pl.cm.gray)
    oprojector.savefig(tempfile.TemporaryFile())
    oprojector.close()
Beispiel #29
0
def test_plot_map_empty():
    # Test that things don't crash when we give a map with nothing above
    # threshold
    # This is only a smoke test
    mp.use('svg', warn=False)
    import pylab as pl
    pl.switch_backend('svg')
    data = np.zeros((20, 20, 20))
    plot_anat(data, mni_sform)
    plot_map(data, mni_sform, slicer='y', threshold=1)
    pl.close('all')
Beispiel #30
0
def test_demo_plot_roi():
    # This is only a smoke test
    mp.use('svg', warn=False)
    import pylab as pl
    pl.switch_backend('svg')
    demo_plot_roi()
    # Test the black background code path
    demo_plot_roi(black_bg=True)

    out = demo_plot_roi(output_file=tempfile.TemporaryFile(suffix='.png'))
    assert_true(out is None)
Beispiel #31
0
def test_plot_map_empty():
    # Test that things don't crash when we give a map with nothing above
    # threshold
    # This is only a smoke test
    mp.use('svg', warn=False)
    import pylab as pl
    pl.switch_backend('svg')
    data = np.zeros((20, 20, 20))
    plot_anat(data, mni_sform)
    plot_map(data, mni_sform, slicer='y', threshold=1)
    pl.close('all')
Beispiel #32
0
def test_demo_ortho_projector():
    # This is only a smoke test
    mp.use('template', warn=False)
    import pylab as pl
    pl.switch_backend('template')
    pl.clf()
    img = load_mni152_template()
    oprojector = OrthoProjector.init_with_figure(img=img)
    oprojector.add_overlay(img, cmap=pl.cm.gray)
    oprojector.savefig(tempfile.TemporaryFile())
    oprojector.close()
Beispiel #33
0
def test_plot_map_with_auto_cut_coords():
    import pylab as pl
    pl.switch_backend('svg')
    data = np.zeros((20, 20, 20))
    data[3:-3, 3:-3, 3:-3] = 1

    for slicer in 'xyz':
        plot_map(data,
                 np.eye(4),
                 cut_coords=None,
                 slicer=slicer,
                 black_bg=True)
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('svg', warn=False)
    import pylab as pl
    pl.switch_backend('svg')
    data = np.zeros((20, 20, 20))
    img = nibabel.Nifti1Image(data, mni_affine)
    plot_anat(img)
    plot_img(img, slicer='y', threshold=1)
    pl.close('all')
Beispiel #35
0
def test_stacked_slicer():
    # Test stacked slicers, like the XSlicer
    mp.use('svg', warn=False)
    import pylab as pl
    pl.switch_backend('svg')
    pl.clf()
    img = load_mni152_template()
    slicer = XSlicer.init_with_figure(img=img, cut_coords=3)
    slicer.add_overlay(img, cmap=pl.cm.gray)
    # Forcing a layout here, to test the locator code
    slicer.savefig(tempfile.TemporaryFile())
    slicer.close()
Beispiel #36
0
def test_plot_img_with_auto_cut_coords():
    import pylab as pl
    pl.switch_backend('template')
    data = np.zeros((20, 20, 20))
    data[3:-3, 3:-3, 3:-3] = 1
    img = nibabel.Nifti1Image(data, np.eye(4))

    for display_mode in 'xyz':
        plot_img(img,
                 cut_coords=None,
                 display_mode=display_mode,
                 black_bg=True)
Beispiel #37
0
def test_stacked_slicer():
    # Test stacked slicers, like the XSlicer
    mp.use('template', warn=False)
    import pylab as pl
    pl.switch_backend('template')
    pl.clf()
    img = load_mni152_template()
    slicer = XSlicer.init_with_figure(img=img, cut_coords=3)
    slicer.add_overlay(img, cmap=pl.cm.gray)
    # Forcing a layout here, to test the locator code
    slicer.savefig(tempfile.TemporaryFile())
    slicer.close()
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('svg', warn=False)
    import pylab as pl
    pl.switch_backend('svg')
    data = np.zeros((20, 20, 20))
    img = nibabel.Nifti1Image(data, mni_affine)
    plot_anat(img)
    plot_img(img, slicer='y', threshold=1)
    pl.close('all')
Beispiel #39
0
def test_demo_ortho_slicer():
    # This is only a smoke test
    # conditioned on presence of MNI templated
    if not find_mni_template():
        raise nose.SkipTest("MNI Template is absent for the smoke test")
    mp.use('svg', warn=False)
    import pylab as pl
    pl.switch_backend('svg')
    pl.clf()
    oslicer = OrthoSlicer(cut_coords=(0, 0, 0))
    img, _ = _AnatCache.get_anat()
    oslicer.add_overlay(img, cmap=pl.cm.gray)
Beispiel #40
0
    def detect_trend(x1, y1, num_points_ahead, ric):
        z4 = polyfit(x1, y1, 3)
        p4 = poly1d(z4)  # construct the polynomial
        #print y1

        z5 = polyfit(x1, y1, 4)
        p5 = poly1d(z5)

        extrap_y_max_limit = len(x1) * 2  # 360 days
        x2 = linspace(
            0, extrap_y_max_limit,
            50)  # 0, 160, 100 means 0 - 160 with 100 data points in between
        pylab.switch_backend(
            'agg'
        )  # switch to agg backend that support writing in non-main threads
        pylab.plot(x1, y1, 'o', x2, p4(x2), '-g', x2, p5(x2), '-b')
        pylab.legend(['%s to fit' % ric, '4th degree poly', '5th degree poly'])
        #pylab.axis([0,160,0,10])
        #
        pylab.axis([
            0, len(x1) * 1.1,
            min(y1) * 0.997,
            max(y1) * 1.002
        ])  # first pair tells the x axis boundary, 2nd pair y axis boundary

        # compute the slopes of each set of data points
        # sr - slope real contains the slope computed from real data points from d to d+5 days
        # s4 - slope extrapolated by applying 4th degree polynomial
        y_arraysize = len(y1)
        #         s4, intercept, r_value, p_value, std_err = stats.linregress(range(0,num_points_ahead), [p4(i) for i in range(y_arraysize,y_arraysize + num_points_ahead )])
        #         s5, intercept, r_value, p_value, std_err = stats.linregress(range(0,num_points_ahead), [p5(i) for i in range(y_arraysize,y_arraysize + num_points_ahead )])
        s4, intercept, r_value, p_value, std_err = stats.linregress(x1, y1)
        s5, intercept, r_value, p_value, std_err = stats.linregress(x1, y1)

        rc = (1.0 if s4 > 0.0 else 0.0, 1.0 if s5 > 0.0 else 0.0)
        print s4, s5, rc, y_arraysize
        #pylab.show()
        pylab.savefig('../data/extrapolation/%s-%s.png' % (ric, time))
        pylab.close()

        d = Q.value
        q = RedisQueue(d['qname'], d['namespace'], d['host'], d['port'],
                       d['db'])
        q.put((time, y1, s4, s5))

        if abs(s4) > Threshold.value:
            d = Q.value
            q = RedisQueue(d['alert_bot_q'][1], d['alert_bot_q'][0], d['host'],
                           d['port'], d['db'])
            q.put('%s %0.2f %0.2f' % (ric, s4, s5))

        return rc
Beispiel #41
0
def test_plot_connectome():
    import pylab as pl
    pl.switch_backend('template')
    node_color = ['green', 'blue', 'k', 'cyan']
    # 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(edge_threshold=0.38,
                  title='threshold=0.38',
                  node_size=10,
                  node_color=node_color)
    plot_connectome(*args, **kwargs)

    # node_coords not an array but a list of tuples
    plot_connectome(adjacency_matrix, map(tuple, node_coords.tolist()),
                    **kwargs)
    # saving to file
    with tempfile.NamedTemporaryFile(suffix='.png') as fp:
        display = plot_connectome(*args, output_file=fp.name, **kwargs)
        assert_true(display is None)
        assert_true(os.path.isfile(fp.name) and os.path.getsize(fp.name) > 0)

    # with node_kwargs, edge_kwargs and edge_cmap arguments
    plot_connectome(*args,
                    edge_threshold='70%',
                    node_size=[10, 20, 30, 40],
                    node_color=np.zeros((4, 3)),
                    edge_cmap='RdBu',
                    node_kwargs={'marker': 'v'},
                    edge_kwargs={'linewidth': 4})

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

    # sparse matrix support
    sparse_adjacency_matrix = sparse.coo_matrix(adjacency_matrix)
    plot_connectome(sparse_adjacency_matrix, node_coords, **kwargs)
Beispiel #42
0
def test_plot_anat():
    # This is only a smoke test
    mp.use("svg", warn=False)
    import pylab as pl

    pl.switch_backend("svg")
    data = np.zeros((20, 20, 20))
    data[3:-3, 3:-3, 3:-3] = 1
    ortho_slicer = plot_anat(data, mni_sform, dim=True)
    ortho_slicer = plot_anat(data, mni_sform, cut_coords=(80, -120, -60))
    # Saving forces a draw, and thus smoke-tests the axes locators
    pl.savefig(tempfile.TemporaryFile())
    ortho_slicer.edge_map(data, mni_sform, color="c")

    z_slicer = plot_anat(slicer="z")
    pl.savefig(tempfile.TemporaryFile())
    z_slicer.edge_map(data, mni_sform, color="c")
    # Smoke test coordinate finder, with and without mask
    plot_map(np.ma.masked_equal(data, 0), mni_sform, slicer="x")
    plot_map(data, mni_sform, slicer="y")
def kullback_leibler(samples1, samples2, show=False, savename="kullback_leibler_comparison.png"):
	"""Generic function to calculate the relative entropy of two histograms.
	"""
	p1, bins1 = np.histogram(samples1, bins=50, normed=1)
	x = (bins1[1:]+bins1[:-1])/2
	p2, bins2 = np.histogram(samples2, bins=bins1, normed=1)

	integrand = np.log(p1/p2)*p1
	kl = np.trapz(np.log(p1/p2)*p1, x)

	if show:
		import pylab as plt
		plt.switch_backend("agg")
		plt.hist(samples1, alpha=0.4, color="purple", bins=50, normed=1)
		plt.hist(samples2, alpha=0.4, color="steelblue", bins=50, normed=1)
		plt.plot(x,p1, "-", lw=2.5, color="purple")
		plt.plot(x,p2, "--", lw=2.5, color="steelblue")
		plt.title("$KL[p_1, p_2]=%3.3f$"%kl)
		plt.savefig("/home/samuroff/shear_pipeline/plot_dump/%s"%savename)
	print("KL=",kl)

	return kl
Beispiel #44
0
def reason_pylab():
    from .utils import deliver_image

    def _canvas_deliver(canvas):
        tf = tempfile.TemporaryFile()
        canvas.print_png(tf)
        tf.seek(0)
        img_data = base64.b64encode(tf.read())
        tf.close()
        deliver_image(img_data)

    def reason_draw_if_interactive():
        if matplotlib.is_interactive():
            figManager = Gcf.get_active()
            if figManager is not None:
                _canvas_deliver(figManager.canvas)

    def reason_show(mainloop=True):
        # We ignore mainloop here
        for manager in Gcf.get_all_fig_managers():
            _canvas_deliver(manager.canvas)

    # Matplotlib has very nice backend overriding.
    # We should really use that.  This is just a hack.
    import matplotlib
    matplotlib.use("agg")  # Hotfix for when we import pylab below
    new_agg = imp.new_module("reason_agg")
    import matplotlib.backends.backend_agg as bagg
    new_agg.__dict__.update(bagg.__dict__)
    new_agg.__dict__.update({
        'show': reason_show,
        'draw_if_interactive': reason_draw_if_interactive
    })
    sys.modules["reason_agg"] = new_agg
    bagg.draw_if_interactive = reason_draw_if_interactive
    from matplotlib._pylab_helpers import Gcf
    matplotlib.rcParams["backend"] = "module://reason_agg"
    import pylab
    pylab.switch_backend("module://reason_agg")
Beispiel #45
0
	def likelihood_cornerplot(self, names, colour="purple", kde=None, plots=None, lims=[], contours=True, blind=True, ls="-",fill=False, alpha=0.2, overplot=[], label='none', include=[True]*20):
		plt.style.use("y1a1")
		plt.switch_backend("pdf")
		

		npar = len(names)
		naxis = npar
		ipanel = 0
		print("Will make corner plot of %d parameters"%npar)
		for i,name1 in enumerate(names):
			fullname1 = "%s--%s"%(sections[name1], name1)
			for j, name2 in enumerate(names):
				ipanel += 1
				fullname2 = "%s--%s"%(sections[name2], name2)
				if j>i: continue
				plt.subplot(naxis,naxis,ipanel, aspect="auto")
				print(i, j, fullname1, fullname2)
				self.choose_panel_contents(i,j, fullname1, fullname2, colour=colour, kde=kde, plots=plots, contours=contours, ls=ls, overplot=overplot, fill=fill, alpha=alpha, label=label, include=include)

				plt.yticks(visible=False)
				plt.xticks(visible=False)

				if (j==0) and (i!=0):
					if not blind: plt.yticks(np.arange(lims[0][0], lims[0][1], 1)[::2][1:],visible=True, fontsize=10)
					plt.ylabel(labels[name1], fontsize=18)
				if i==naxis-1:
					if not blind: plt.xticks(np.arange(lims[1][0], lims[1][1], 1)[::2][1:],visible=True, fontsize=10)
					plt.xlabel(labels[name2], fontsize=18)
					
				if len(lims)>0 and (i!=j):
					plt.xlim(lims[j][0], lims[j][1])
					plt.ylim(lims[i][0], lims[i][1])
				if i==j:
					plt.xlim(lims[j][0], lims[j][1])
				plt.axhline(0,color="k", ls=":", alpha=0.5)
				plt.axvline(0,color="k", ls=":", alpha=0.5)

		plt.subplots_adjust(hspace=0, wspace=0)
Beispiel #46
0
def savefig(fname,figsize,fig=None,**kwargs):
  """ 
    force saving figure with a given size, useful when using tiling wm;
    if fname is a list, it saves multiple files, for example [todel.pdf,todel.png]
  """
  if isinstance(fname,str): fname = (fname,)
  if fig is None: fig = plt.gcf()
  old_bkg = plt.get_backend()
  old_inter = plt.isinteractive()
  try:
    plt.switch_backend("cairo")
    old_height = fig.get_figheight()
    old_width  = fig.get_figwidth()
    fig.set_figwidth ( figsize[0] )
    fig.set_figheight( figsize[1] )
    [  fig.savefig(f,**kwargs) for f in fname ]
    plt.switch_backend(old_bkg)
  finally:
    plt.switch_backend(old_bkg)
    plt.interactive(old_inter)
import numpy as np
import galsim
import astropy.table as tb
from py3shape import structs
import math
import pylab as plt
plt.switch_backend("agg")
import fitsio
import glob, argparse, os
import tools.shapes as s
import py3shape as p3s
from py3shape import utils
from tools.im3shape import basic as i3s
import tools.diagnostics as di


class mcmc_toy_model:
    def __init__(self):
        print "Toy model to illustrate neighbour bias."

    def get_realisation(self, samples, icent, ineigh, ireal):
        print "-------------------------------------------------"
        print "Generating Realisation %d" % ireal
        print "-------------------------------------------------"

        self.params["fc"] = samples["flux"][icent]
        self.params["fn"] = samples["neighbour_flux"][icent]
        self.params["dgn"] = samples["nearest_neighbour_pixel_dist"][icent]
        self.params["Rc"] = samples["hlr"][icent]
        self.params["Rn"] = samples["neighbour_hlr"][icent]
        self.params["psf_size"] = samples["psf_size"][icent]
import numpy as np
import pylab as pl
pl.switch_backend('agg')
from mpl_toolkits.axes_grid1 import make_axes_locatable
from scipy.ndimage.filters import gaussian_filter1d
from run import *
from sys import exit
import os

if not os.path.exists("../data/fig"):
    os.makedirs("../data/fig")
os.chdir('../data/')


#---------------------------------------#
def sigma2fwhm(sigma):
    return sigma * np.sqrt(8 * np.log(2))


#---------------------------------------#
def fwhm2sigma(fwhm):
    return fwhm / np.sqrt(8 * np.log(2))


#---------------------------------------#
nc = len(node_coupling)
ns = len(std_noise)

fig2, ax = pl.subplots(1, figsize=(8, 5))
from cycler import cycler
NUM_COLORS = len(range(0, ns, 3))
Beispiel #49
0
	stat.py
	Used to create plots of execution times.
	
	Stronly inspired by http://stackoverflow.com/a/16598291/4620080

	Author: Martin Storgaard and Konstantinos Mampentzidis
"""
from pylab import plot, show, savefig, xlim, figure, \
                hold, ylim, legend, boxplot, setp, axes, \
                title, ylabel, xlabel, subplots_adjust, suptitle, switch_backend

from utils import get_test_cases, get_test_case
from sys import argv
import numpy as np
switch_backend('TKAgg')
# Built-in colors: Blue, Green, Red,
# Cyan, Magenta, Yellow and Black
colors = "bgrcmyk"
# Number of algorithms to be plotted
num_algs = 2
#
if len(argv) != 2:
	print "Use ./stat.py [query | del | ins] to generate graphs"
	exit()
	
reading = argv[1]
plural = ""
if reading == "query":
	plural = "queries"
elif reading == "del":
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
import tempfile

import numpy as np

from nose import SkipTest
try:
    import matplotlib as mp
    # Make really sure that we don't try to open an Xserver connection.
    mp.use('svg', warn=False)
    import pylab as pl
    pl.switch_backend('svg')
except ImportError:
    raise SkipTest('Could not import matplotlib')

from ..activation_maps import demo_plot_map, plot_anat, plot_map
from ..anat_cache import mni_sform, _AnatCache



def test_demo_plot_map():
    # This is only a smoke test
    mp.use('svg', warn=False)
    import pylab as pl
    pl.switch_backend('svg')
    demo_plot_map()
    # Test the black background code path
    demo_plot_map(black_bg=True)

import numpy as np
import os
import pylab as plt
plt.switch_backend('pdf')
plt.style.use('y1a1')

import tools.emcee as mc
from chainconsumer import ChainConsumer

from matplotlib import rcParams
rcParams['xtick.major.size'] = 3.5
rcParams['xtick.minor.size'] = 1.7
rcParams['ytick.major.size'] = 3.5
rcParams['ytick.minor.size'] = 1.7
rcParams['xtick.direction'] = 'in'
rcParams['ytick.direction'] = 'in'
print('Loading chains...')

base = '/Users/hattifattener/Documents/y3cosmicshear/chains/'

c0 = mc.chain(
    '/Volumes/groke/work/chains/y3/real/final_paper_chains/chain_1x2pt_lcdm_SR_maglim.txt'
)
#chain_1x2pt_lcdm.txt')
c1 = np.genfromtxt(base + '/external/lensing/HSC_Y1_LCDM_post_fid.txt',
                   names=True)
c2 = mc.chain(
    '/Volumes/groke/work/chains/y3/real/final_paper_chains/chain_1x2agg_ML.txt'
)
#mc.chain('/Volumes/groke/work/chains/y1/fiducial/all/out_all-1x2pt-NG.txt')
c3 = mc.chain(
'''
Created on Oct 29, 2013

@author: leal
'''

import nxs
import time
import numpy as np
import pylab as plt
plt.switch_backend('macosx')
#import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.colors import LogNorm

partial_time = time.time()
start_time = time.time()

def timeit():
  global partial_time
  now = time.time()
  diff_total = now - start_time
  diff_partial = now - partial_time
  partial_time = time.time()
  print '**** Time: last: %dm %ds :: total: %dm %ds' % (int(diff_partial // 60),
    int(diff_partial % 60), int(diff_total // 60), int(diff_total % 60))


class NexusReader(object):
    '''
    Functions to a read a nexus file
"""
Copied from despotic/examples/gmcChem and modified
"""
from __future__ import print_function
import matplotlib
import pylab as pl
pl.switch_backend('Qt4Agg')
from astropy import units as u
from astropy import constants
import paths
from paths import fpath
from astropy.utils.console import ProgressBar
import pprint

# Import the despotic library and the NL99 network; also import numpy
from despotic import cloud
import despotic
import os
import numpy as np

# Use the Milky Way GMC file as a base
gmc=cloud('cloud.desp')

from despotic.chemistry import NL99
# gmc.setChemEq(network=NL99)

def turb_heating_generator(lengthscale=1*u.pc, turbulence=True):
    def turb_heating(cloud, lengthscale=lengthscale):
        """ Turbulent heating rate depends on cloud linewidth
        (sigma_nonthermal) and driving scale of the turbulence
        DESPOTIC wants units of erg/s/H (per hydrogen), so the turbulent
Beispiel #54
0
        print "Failed to create PNG for %s. (%s)" % (filename, str(e))
        html += "<td>Failed to create PNG"
        thtml += "<td>Failed to create PNG"
        if options.crash:
            raise

    try:
        svg_filename = filename[:-2] + "svg"
        f.canvas.print_svg("./output/" + svg_filename, dpi=f.dpi)
        html += "<td><img src='%s' width='%dpx' height='%dpx' />" % (svg_filename, w, h)
    except Exception, e:
        print "Failed to create SVG for %s. (%s)" % (filename, str(e))
        html += "<td>Failed to create SVG"
        if options.crash:
            raise
    switch_backend('module://mplh5canvas.backend_h5canvas')
    html += "</tr>"
    thtml += "</tr>"

print "Finished processing files..."
html += "</table><script> var total_plots = " + str(count) + "; "
pi = """
function connect() {
  s = new WebSocket("ws://192.168.184.1:8123");
}

function put_images() {
  for (var i=0; i<total_plots+1;i++) { 
   try {
   s.send(document.getElementById("name_"+i).innerText.split(".py")[0] + ".png " + document.getElementById("canvas_"+i).toDataURL());
   } catch (err) {}
import numpy as np
import tools.nbc as cal
import tools.shapes as s
import tools.diagnostics as di
import tools.plots as pl
import fitsio as fi
import pylab as plt
import os, yaml, argparse

plt.switch_backend("agg")


def main(args):
	im3shape_columns = ["e1", "e2", "mean_psf_e1_sky", "mean_psf_e2_sky", "mean_psf_fwhm", "snr", "mean_rgpp_rp", "radius", "coadd_objects_id", "mean_flux", "n_exposure", "stamp_size", "info_flag", "is_bulge", "tilename"]
	truth_columns = ['DES_id', 'cosmos_ident', 'cosmos_photoz', 'sextractor_pixel_offset', 'true_g1', 'true_g2', 'intrinsic_e1', 'intrinsic_e2', 'ra', 'dec', 'hlr', 'mag', 'flux']

	# Load the y1 data
	if args.calculate or args.catalogue:
		y1v2 = s.shapecat(res=config["i3s_dir"])
		y1v2.load(truth=False, prune=True, cols=[im3shape_columns,truth_columns])
		y1v2.res=y1v2.res[y1v2.res["info_flag"]==0]
	

	# And the simulation results
	if args.calculate:
		if ".fits" not in config["hoopoe_dir"]:
			hoopoe = s.shapecat(res="%s/bord-fits/main"%config["hoopoe_dir"] ,truth="%s/truth"%config["hoopoe_dir"])
			hoopoe.load(truth=True, cols=[im3shape_columns,truth_columns])

		else:
			hoopoe = s.shapecat(res=config["hoopoe_dir"], truth=config["hoopoe_dir"] )
Beispiel #56
0
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
import tempfile

import numpy as np

from nose import SkipTest

try:
    import matplotlib as mp

    # Make really sure that we don't try to open an Xserver connection.
    mp.use("svg", warn=False)
    import pylab as pl

    pl.switch_backend("svg")
except ImportError:
    raise SkipTest("Could not import matplotlib")

from ..activation_maps import demo_plot_map, plot_anat, plot_map
from ..anat_cache import mni_sform, _AnatCache


def test_demo_plot_map():
    # This is only a smoke test
    mp.use("svg", warn=False)
    import pylab as pl

    pl.switch_backend("svg")
    demo_plot_map()