Esempio n. 1
0
def plot_hwe(variations, max_num_alleles, data_dir, ploidy=2,
             min_num_genotypes=MIN_NUM_GENOTYPES_FOR_POP_STAT,
             chunk_size=SNPS_PER_CHUNK):
    fpath = join(data_dir, 'hwe_chi2_distrib.png')
    fhand = open(fpath, 'w')
    fig = Figure(figsize=(10, 20))
    canvas = FigureCanvas(fig)
    
    num_alleles = range(2, max_num_alleles + 1)
    gs = gridspec.GridSpec(len(num_alleles), 1)
    for i, num_allele in enumerate(num_alleles):
        df = len(list(combinations_with_replacement(range(num_allele),
                                                    ploidy))) - num_allele
                                                    
        hwe_test =  calc_hwe_chi2_test(variations, num_allele=num_allele,
                                       min_num_genotypes=min_num_genotypes,
                                       chunk_size=chunk_size)
        hwe_chi2 = hwe_test[:, 0]
        hwe_chi2_distrib, bins = histogram(hwe_chi2, n_bins=50)
        
        # Plot observed distribution
        axes = fig.add_subplot(gs[i, 0])
        title = 'Chi2 df={} statistic values distribution'.format(df)
        mpl_params = {'set_xlabel': {'args': ['Chi2 statistic'], 'kwargs': {}},
                      'set_ylabel': {'args': ['SNP number'], 'kwargs': {}},
                      'set_title': {'args': [title], 'kwargs': {}}}
        plot_distrib(hwe_chi2_distrib, bins, axes=axes, mpl_params=mpl_params)
        
        # Plot expected chi2 distribution
        axes = axes.twinx()
        rv = chi2(df)
        x = numpy.linspace(0, max(hwe_chi2), 1000)
        axes.plot(x, rv.pdf(x), color='b', lw=2, label='Expected Chi2')
        axes.set_ylabel('Expected Chi2 density')
    canvas.print_figure(fhand)
Esempio n. 2
0
def plot_call_field_distribs_per_gt_type(variations, field, max_value,
                                         data_dir, chunk_size=SNPS_PER_CHUNK):
    # Field distribution per sample
    field_name = field.split('/')[-1]
    fpath = join(data_dir, '{}_distribution_per_sample.png'.format(field_name))
    mask_funcs = [call_is_het, call_is_hom]
    names = ['Heterozygous', 'Homozygous']
    distribs = []
    for mask_func in mask_funcs:
        dp_distribs, bins = calc_field_distribs_per_sample(variations,
                                                           field=field,
                                                           range_=(0, max_value),
                                                           n_bins=max_value,
                                                           chunk_size=chunk_size,
                                                           mask_func=mask_func,
                                                           mask_field=GT_FIELD)
        distribs.append(dp_distribs)
        
    title = '{} distribution per sample'.format(field_name)
    mpl_params = {'set_xlabel': {'args': ['Samples'], 'kwargs': {}},
                  'set_ylabel': {'args': [field_name], 'kwargs': {}},
                  'set_title': {'args': [title], 'kwargs': {}}}
    figsize = (variations[GT_FIELD].shape[1], 7)
    plot_boxplot_from_distribs_series(distribs, fhand=open(fpath, 'w'),
                                      mpl_params=mpl_params, figsize=figsize,
                                      colors=['pink', 'tan'],
                                      labels=names,
                                      xticklabels=variations.samples)
    
    # Overall field distributions
    fpath = join(data_dir, '{}_distribution.png'.format(field_name))
    fhand = open(fpath, 'w')
    fig = Figure(figsize=(20, 15))
    canvas = FigureCanvas(fig)
    i = 1
    for distrib, name in zip(distribs, names):
        distrib = numpy.sum(dp_distribs, axis=0)
        distrib_cum = calc_cum_distrib(distrib)
        axes = fig.add_subplot(len(names) * 100 + 20 + i)
        i += 1
        title = '{} distribution all samples {}'.format(field_name, name)
        plot_distrib(distrib, bins, axes=axes,
                     mpl_params={'set_xlabel': {'args': [field_name],
                                                'kwargs': {}},
                                 'set_ylabel': {'args': ['Number of GTs'],
                                                'kwargs': {}},
                                 'set_title': {'args': [title], 'kwargs': {}}})
        distrib_cum = distrib_cum/distrib_cum[0] * 100
        axes = fig.add_subplot(len(names) * 100 + 20 + i)
        i += 1
        title = '{} cumulative distribution all samples {}'.format(field_name,
                                                                   name)
        plot_distrib(distrib_cum, bins, axes=axes,
                     mpl_params={'set_xlabel': {'args': [field_name],
                                                'kwargs': {}},
                                 'set_ylabel': {'args': ['% calls > Depth '],
                                                'kwargs': {}},
                                 'set_title': {'args': [title], 'kwargs': {}}})
    canvas.print_figure(fhand)
Esempio n. 3
0
def plot_allele_obs_distrib_2D(variations, data_dir, max_allele_counts,
                               chunk_size=SNPS_PER_CHUNK):
    # Allele observation distribution 2D
    masks = [call_is_het, call_is_hom_alt, call_is_hom_ref]
    names = ['Heterozygous', 'Alt Homozygous', 'Ref Homozygous']
    
    fig = Figure(figsize=(22, 25))
    canvas = FigureCanvas(fig)
    gs = gridspec.GridSpec(3, 2)
    fpath = join(data_dir, 'allele_obs_distrib_per_gt.png')
    fhand = open(fpath, 'w')
    
    counts_range = [[0, max_allele_counts], [0, max_allele_counts]]
    
    for i, (mask_func, name) in enumerate(zip(masks, names)):
        hist2d = hist2d_allele_observations(variations,
                                            n_bins=max_allele_counts,
                                            range_=counts_range,
                                            mask_func=mask_func,
                                            chunk_size=chunk_size)
        counts_distrib2d, xbins, ybins = hist2d
        
        axes = fig.add_subplot(gs[i, 0])
        title = 'Allele counts distribution 2D {}'.format(name)
        plot_hist2d(numpy.log10(counts_distrib2d), xbins, ybins, axes=axes,
                    mpl_params={'set_xlabel': {'args': ['Alt allele counts'],
                                               'kwargs': {}},
                                'set_ylabel': {'args': ['Ref allele counts'],
                                               'kwargs': {}},
                                'set_title': {'args': [title], 'kwargs': {}}},
                    colorbar_label='log10(counts)', fig=fig)

        hist2d = hist2d_gq_allele_observations(variations,
                                               n_bins=max_allele_counts,
                                               range_=counts_range,
                                               mask_func=mask_func,
                                               chunk_size=chunk_size,
                                               hist_counts=counts_distrib2d)
        gq_distrib2d, xbins, ybins = hist2d
        
        axes = fig.add_subplot(gs[i, 1])
        title = 'Allele counts GQ distribution 2D {}'.format(name)
        plot_hist2d(gq_distrib2d, xbins, ybins, axes=axes, fig=fig,
                    mpl_params={'set_xlabel': {'args': ['Alt allele counts'],
                                               'kwargs': {}},
                                'set_ylabel': {'args': ['Ref allele counts'],
                                               'kwargs': {}},
                                'set_title': {'args': [title], 'kwargs': {}}},
                    colorbar_label='Genotype Quality (GQ)')

    canvas.print_figure(fhand)
Esempio n. 4
0
def plot_obs_het(variations, data_dir, chunk_size=SNPS_PER_CHUNK,
                 min_num_genotypes=MIN_NUM_GENOTYPES_FOR_POP_STAT):
    # Calculate observed heterozygosity distribution by snp
    _calc_obs_het_by_var = partial(calc_obs_het,
                                   min_num_genotypes=min_num_genotypes)
    distrib = histogram_for_chunks(variations, calc_funct=_calc_obs_het_by_var,
                                   n_bins=25, range_=(0, 1),
                                   chunk_size=chunk_size)
    obs_het_var_distrib, bins1 = distrib
    
    # Calculate observed heterozygosity distribution by sample
    obs_het_by_sample = calc_obs_het_by_sample(variations,
                                               chunk_size=chunk_size)
    obs_het_sample_distrib, bins2 = histogram(obs_het_by_sample, n_bins=25,
                                              range_=(0, 1))
    
    # Plot distributions
    fpath = join(data_dir, 'obs_het.png')
    fhand = open(fpath, 'w')
    fig = Figure(figsize=(10, 10))
    canvas = FigureCanvas(fig)
    axes = fig.add_subplot(211)
    title = 'SNP observed Heterozygosity distribution'
    plot_distrib(obs_het_var_distrib, bins=bins1, fhand=open(fpath, 'w'),
                 mpl_params={'set_xlabel': {'args': ['Heterozygosity'],
                                            'kwargs': {}},
                             'set_ylabel': {'args': ['SNP number'], 'kwargs': {}},
                             'set_title': {'args': [title], 'kwargs': {}},
                             'set_yscale': {'args': ['log'], 'kwargs': {}}},
                 axes=axes, color='c')
    axes = fig.add_subplot(212)
    title = 'Sample observed Heterozygosity distribution'
    plot_distrib(obs_het_sample_distrib, bins=bins2, fhand=open(fpath, 'w'),
                 mpl_params={'set_xlabel': {'args': ['Heterozygosity'],
                                            'kwargs': {}},
                             'set_ylabel': {'args': ['Sample number'],
                                            'kwargs': {}},
                             'set_title': {'args': [title], 'kwargs': {}}},
                 axes=axes, color='c')
    canvas.print_figure(fhand)
Esempio n. 5
0
def show_stamps(pscs,
                frame_idx=None,
                stamp_size=11,
                aperture_position=None,
                aperture_size=None,
                show_residual=False,
                stretch=None,
                save_name=None,
                show_max=False,
                show_pixel_grid=False,
                **kwargs):

    if aperture_position is None:
        midpoint = (stamp_size - 1) / 2
        aperture_position = (midpoint, midpoint)

    if aperture_size:
        aperture = RectangularAperture(
            aperture_position, w=aperture_size, h=aperture_size, theta=0)

    ncols = len(pscs)

    if show_residual:
        ncols += 1

    nrows = 1

    fig = Figure()
    FigureCanvas(fig)
    fig.set_figheight(4)
    fig.set_figwidth(8)

    if frame_idx is not None:
        s0 = pscs[0][frame_idx]
        s1 = pscs[1][frame_idx]
    else:
        s0 = pscs[0]
        s1 = pscs[1]

    if stretch == 'log':
        stretch = LogStretch()
    else:
        stretch = LinearStretch()

    norm = ImageNormalize(s0, interval=MinMaxInterval(), stretch=stretch)

    ax1 = fig.add_subplot(nrows, ncols, 1)

    im = ax1.imshow(s0, cmap=get_palette(), norm=norm)

    if aperture_size:
        aperture.plot(color='r', lw=4, ax=ax1)
        # annulus.plot(color='c', lw=2, ls='--', ax=ax1)

    # create an axes on the right side of ax. The width of cax will be 5%
    # of ax and the padding between cax and ax will be fixed at 0.05 inch.
    # https://stackoverflow.com/questions/18195758/set-matplotlib-colorbar-size-to-match-graph
    divider = make_axes_locatable(ax1)
    cax = divider.append_axes("right", size="5%", pad=0.05)
    fig.colorbar(im, cax=cax)
    ax1.set_title('Target')

    # Comparison
    ax2 = fig.add_subplot(nrows, ncols, 2)
    im = ax2.imshow(s1, cmap=get_palette(), norm=norm)

    if aperture_size:
        aperture.plot(color='r', lw=4, ax=ax1)
        # annulus.plot(color='c', lw=2, ls='--', ax=ax1)

    divider = make_axes_locatable(ax2)
    cax = divider.append_axes("right", size="5%", pad=0.05)
    fig.colorbar(im, cax=cax)
    ax2.set_title('Comparison')

    if show_pixel_grid:
        add_pixel_grid(ax1, stamp_size, stamp_size, show_superpixel=False)
        add_pixel_grid(ax2, stamp_size, stamp_size, show_superpixel=False)

    if show_residual:
        ax3 = fig.add_subplot(nrows, ncols, 3)

        # Residual
        residual = s0 - s1
        im = ax3.imshow(residual, cmap=get_palette(), norm=ImageNormalize(
            residual, interval=MinMaxInterval(), stretch=LinearStretch()))

        divider = make_axes_locatable(ax3)
        cax = divider.append_axes("right", size="5%", pad=0.05)
        fig.colorbar(im, cax=cax)
        ax3.set_title('Noise Residual')
        ax3.set_title('Residual RMS: {:.01%}'.format(residual.std()))
        ax3.set_yticklabels([])
        ax3.set_xticklabels([])

        if show_pixel_grid:
            add_pixel_grid(ax1, stamp_size, stamp_size, show_superpixel=False)

    # Turn off tick labels
    ax1.set_yticklabels([])
    ax1.set_xticklabels([])
    ax2.set_yticklabels([])
    ax2.set_xticklabels([])

    if save_name:
        try:
            fig.savefig(save_name)
        except Exception as e:
            warn("Can't save figure: {}".format(e))

    return fig
Esempio n. 6
0
def plot_fancy_occupancy(hist, z_max=None, filename=None):
    if z_max == 'median':
        median = np.ma.median(hist)
        z_max = median * 2  # round_to_multiple(median * 2, math.floor(math.log10(median * 2)))
    elif z_max == 'maximum' or z_max is None:
        maximum = np.ma.max(hist)
        z_max = maximum  # round_to_multiple(maximum, math.floor(math.log10(maximum)))
    if z_max < 1 or hist.all() is np.ma.masked:
        z_max = 1

#     ax.set_title('Occupancy (%d entries)' % np.sum(hist))
    fig = Figure()
    canvas = FigureCanvas(fig)
    ax = fig.add_subplot(111)
    extent = [0.5, 80.5, 336.5, 0.5]
    bounds = np.linspace(start=0, stop=z_max, num=255, endpoint=True)
    cmap = cm.get_cmap('jet')
    cmap.set_bad('w')
    norm = colors.BoundaryNorm(bounds, cmap.N)
#     norm = colors.LogNorm()

    im = ax.imshow(hist, interpolation='nearest', aspect='auto', cmap=cmap, norm=norm, extent=extent)  # TODO: use pcolor or pcolormesh
    ax.set_ylim((336.5, 0.5))
    ax.set_xlim((0.5, 80.5))
#     ax.set_title('Occupancy (%d entries)' % np.sum(hist))
    ax.set_xlabel('Column')
    ax.set_ylabel('Row')

    # create new axes on the right and on the top of the current axes
    # The first argument of the new_vertical(new_horizontal) method is
    # the height (width) of the axes to be created in inches.
    divider = make_axes_locatable(ax)
    axHistx = divider.append_axes("top", 1.2, pad=0.2, sharex=ax)
    axHisty = divider.append_axes("right", 1.2, pad=0.2, sharey=ax)

    cax = divider.append_axes("right", size="5%", pad=0.1)
    cb = fig.colorbar(im, cax=cax, ticks=np.linspace(start=0, stop=z_max, num=9, endpoint=True))
    cb.set_label("#")
    # make some labels invisible
    setp(axHistx.get_xticklabels() + axHisty.get_yticklabels(), visible=False)
    hight = np.ma.sum(hist, axis=0)
    # hight[hight.mask] = 0
    axHistx.bar(left=range(1, 81), height=hight, align='center', linewidth=0)
    axHistx.set_xlim((0.5, 80.5))
    if hist.all() is np.ma.masked:
        axHistx.set_ylim((0, 1))
    axHistx.locator_params(axis='y', nbins=3)
    axHistx.ticklabel_format(style='sci', scilimits=(0, 4), axis='y')
    axHistx.set_ylabel('#')
    width = np.ma.sum(hist, axis=1)
    # width[hight.mask] = 0
    axHisty.barh(bottom=range(1, 337), width=width, align='center', linewidth=0)
    axHisty.set_ylim((336.5, 0.5))
    if hist.all() is np.ma.masked:
        axHisty.set_xlim((0, 1))
    axHisty.locator_params(axis='x', nbins=3)
    axHisty.ticklabel_format(style='sci', scilimits=(0, 4), axis='x')
    axHisty.set_xlabel('#')

    if not filename:
        fig.show()
    elif isinstance(filename, PdfPages):
        filename.savefig(fig)
    else:
        fig.savefig(filename)
Esempio n. 7
0
from get_sample import get_sample_cube

(ndata, dts) = get_sample_cube(start, end)

# Plot the resulting array as a 2d colourmap
fig = Figure(
    figsize=(19.2, 6),  # Width, Height (inches)
    dpi=300,
    facecolor=(0.5, 0.5, 0.5, 1),
    edgecolor=None,
    linewidth=0.0,
    frameon=False,
    subplotpars=None,
    tight_layout=None)
canvas = FigureCanvas(fig)
matplotlib.rc('image', aspect='auto')


def add_latline(ax, latitude):
    latl = (latitude + 90) / 180
    ax.add_line(
        Line2D([start.timestamp(), end.timestamp()], [latl, latl],
               linewidth=0.5,
               color=(0.8, 0.8, 0.8, 1),
               zorder=200))


# Add a textured grey background
s = (2000, 600)
ax2 = fig.add_axes([0, 0.05, 1, 0.95], facecolor='green')
Esempio n. 8
0
def thumbnail(infile,
              thumbfile,
              scale=0.1,
              interpolation='bilinear',
              preview=False):
    """
    make a thumbnail of image in *infile* with output filename
    *thumbfile*.

      *infile* the image file -- must be PNG or Pillow-readable if you
         have `Pillow <http://python-pillow.github.io/>`_ installed

      *thumbfile*
        the thumbnail filename

      *scale*
        the scale factor for the thumbnail

      *interpolation*
        the interpolation scheme used in the resampling


      *preview*
        if True, the default backend (presumably a user interface
        backend) will be used which will cause a figure to be raised
        if :func:`~matplotlib.pyplot.show` is called.  If it is False,
        a pure image backend will be used depending on the extension,
        'png'->FigureCanvasAgg, 'pdf'->FigureCanvasPdf,
        'svg'->FigureCanvasSVG


    See examples/misc/image_thumbnail.py.

    .. htmlonly::

        :ref:`misc-image_thumbnail`

    Return value is the figure instance containing the thumbnail

    """
    basedir, basename = os.path.split(infile)
    baseout, extout = os.path.splitext(thumbfile)

    im = imread(infile)
    rows, cols, depth = im.shape

    # this doesn't really matter, it will cancel in the end, but we
    # need it for the mpl API
    dpi = 100

    height = float(rows) / dpi * scale
    width = float(cols) / dpi * scale

    extension = extout.lower()

    if preview:
        # let the UI backend do everything
        import matplotlib.pyplot as plt
        fig = plt.figure(figsize=(width, height), dpi=dpi)
    else:
        if extension == '.png':
            from matplotlib.backends.backend_agg \
                import FigureCanvasAgg as FigureCanvas
        elif extension == '.pdf':
            from matplotlib.backends.backend_pdf \
                import FigureCanvasPdf as FigureCanvas
        elif extension == '.svg':
            from matplotlib.backends.backend_svg \
                import FigureCanvasSVG as FigureCanvas
        else:
            raise ValueError("Can only handle "
                             "extensions 'png', 'svg' or 'pdf'")

        from matplotlib.figure import Figure
        fig = Figure(figsize=(width, height), dpi=dpi)
        FigureCanvas(fig)

    ax = fig.add_axes([0, 0, 1, 1],
                      aspect='auto',
                      frameon=False,
                      xticks=[],
                      yticks=[])

    basename, ext = os.path.splitext(basename)
    ax.imshow(im, aspect='auto', resample=True, interpolation=interpolation)
    fig.savefig(thumbfile, dpi=dpi)
    return fig
Esempio n. 9
0
def make_triangulation_video(video_path,
                             triangulated_csv_path,
                             skeleton_config=None,
                             output_path=None,
                             frame_range=None,
                             view=(90, 120),
                             figure_size=(9, 5),
                             figure_dpi=150,
                             marker_size=5,
                             skeleton_thickness=1,
                             frame_count=False,
                             frame_rate=None,
                             thrd_video_path=None,
                             thrd_video_frame_offset=0,
                             third_video_crop_hw=None,
                             ranges=None,
                             plot_markers=True,
                             horizontal_subplots=True,
                             timeseries=None):
    '''Makes a video based on triangulated marker positions.

    Arguments:
        video_path {str} -- Full file path of video.
        triangulated_csv_path {str} -- Full file path of csv with triangulated points.
    Keyword Arguments:
        skeleton_config {str} -- Path to yaml file with both 'bodyparts' and 'skeleton' as shown in
            the example config. (default: None)
        output_path {str} -- Path to place the video. Will accept full file name.
            (default: None = same as triangulated_csv)
        frame_range {tuple or None} --  part of video and points to create a video for. If a tuple
            then indicates the start and end frame number, including both as an interval. If None
            then all frames will be used. (default: None)
        view {tuple} -- The desired (elevation, azimuth) required for the 3d plot. (default:
            (90, 90))
        figure_size {tuple} -- desired (width, height) of the figure. (default:(9, 5))
        figure_dpi {int} -- DPI of the video. (default: 150)
        marker_size {int} -- size of the markers in the 3d plot. (default: 5)
        skeleton_thickness {int} -- thickness of the connecting lines in the 3d plot. (default: 1)
        thrd_video_path {str} -- add another video from this path to the side. (default: None)
        thrd_video_frame_offset {int} -- start the added vide from this frame. (default: 0)
        third_video_crop_hw {list of 2 slices} -- crop the third video using slices.
            (default: None)
        ranges {list of 2-lists} -- overwrites xrange, yrange and zrange for the 3d plot. Individual
            elements can be None. (default: None)
        plot_markers {bool} -- plot 3D view of the markers. Having it False with no third_video_path
            set can lead to unexpected behavior. (default: True)
        horizontal_subplots {bool} -- makes subplots horizontal, otherwise vertical. (default: True)
    '''
    if skeleton_config is not None:
        with open(skeleton_config, 'r') as yaml_file:
            dic = yaml.safe_load(yaml_file)
            bp_list = dic['bodyparts']
            bp_connections = dic['skeleton']
        skeleton = True
    else:
        skeleton = False

    with open(triangulated_csv_path, 'r') as f:
        triagreader = csv.reader(f)
        l = next(triagreader)
        bodyparts = []
        for i, bp in enumerate(l):
            if (i - 1) % 3 == 0:
                bodyparts.append(bp)
        num_bodyparts = len(bodyparts)
        next(triagreader)
        triangulated_points = []
        for row in triagreader:
            triangulated_points.append([[] for _ in range(3)])
            for ibp in range(num_bodyparts):
                triangulated_points[-1][0].append(float(row[1 + ibp * 3]))
                triangulated_points[-1][1].append(float(row[2 + ibp * 3]))
                triangulated_points[-1][2].append(float(row[3 + ibp * 3]))

    triangulated_points = np.array(triangulated_points)

    cmap = matplotlib.cm.get_cmap('jet')
    color_idx = np.linspace(0, 1, num_bodyparts)
    bp_cmap = cmap(color_idx)
    # Limits in space of the markers + 10%
    margin_min = 0.7
    margin_max = 1.3
    pcntl = 0.1
    if ranges is None or ranges[0] is None:
        x_range = (
            np.nanpercentile(triangulated_points[:, 0, :], pcntl) * margin_min,
            np.nanpercentile(triangulated_points[:, 0, :], 100 - pcntl) *
            margin_max)
    else:
        x_range = ranges[0]
    if ranges is None or ranges[1] is None:
        y_range = (
            np.nanpercentile(triangulated_points[:, 1, :], pcntl) * margin_min,
            np.nanpercentile(triangulated_points[:, 1, :], 100 - pcntl) *
            margin_max)
    else:
        y_range = ranges[1]
    if ranges is None or ranges[2] is None:
        z_range = (
            np.nanpercentile(triangulated_points[:, 2, :], pcntl) * margin_min,
            np.nanpercentile(triangulated_points[:, 2, :], 100 - pcntl) *
            margin_max)
    else:
        z_range = ranges[2]

    # Inspect the video
    video = cv2.VideoCapture(video_path)
    fps = int(video.get(cv2.CAP_PROP_FPS))
    num_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))

    if output_path is None:  # Use the default file name
        csv_path_head = os.path.splitext(triangulated_csv_path)[0]
        video_path_body = os.path.splitext(os.path.split(video_path)[1])[0]
        output_filename = (csv_path_head + '_' + video_path_body +
                           '_triangulated.mp4')
    else:
        if os.path.isdir(output_path):  # Check if an existing directory
            csv_path_body = os.path.splitext(
                os.path.split(triangulated_csv_path)[1])[0]
            video_path_body = os.path.splitext(os.path.split(video_path)[1])[0]
            output_filename = os.path.join(
                output_path,
                csv_path_body + '_' + video_path_body + '_triangulated.mp4')
        else:
            output_filename = output_path

    output_filename = utils.iterative_filename(output_filename)
    print('File path: {}'.format(output_filename))

    # Check the frame range
    if frame_range is not None:
        video.set(cv2.CAP_PROP_POS_FRAMES,
                  frame_range[0])  # Set the start position
        if frame_range[1] >= num_frames:
            print(
                'Too many frames requested, the video will be truncated appropriately.\n'
            )
            frame_range = (frame_range[0], num_frames - 1)

        video.set(cv2.CAP_PROP_POS_FRAMES,
                  frame_range[0])  # Set the start position
        # # If the above method does not work with MPEG/FFMPEG, see
        # # @https://stackoverflow.com/questions/19404245/opencv-videocapture-set-cv-cap-prop-pos-frames-not-working
        # and try:
        # for i in range(frame_range[0]):
        #     video.read()
    else:
        frame_range = (0, num_frames - 1)

    # load the third video
    if thrd_video_path is not None:
        thrd_video = cv2.VideoCapture(thrd_video_path)
        # thrd_video.set(cv2.CAP_PROP_POS_FRAMES, thrd_video_frame_offset) # Set the start position
        thrd_fe, thrd_frame = thrd_video.read()
        thrd_video_fps = int(thrd_video.get(cv2.CAP_PROP_FPS))

    # Create the figure
    fig = mpl_pp.figure(figsize=figure_size, dpi=figure_dpi)
    fw, fh = fig.get_size_inches() * fig.get_dpi()
    canvas = FigureCanvas(fig)
    # Make a new video keeping the old properties - need to know figure size first
    if frame_rate is None:
        frame_rate = fps

    if horizontal_subplots:
        xn_sbps = lambda num_subplots: 1
        yn_sbps = lambda num_subplots: num_subplots
    else:
        xn_sbps = lambda num_subplots: num_subplots
        yn_sbps = lambda num_subplots: 1

    fourcc = cv2.VideoWriter_fourcc(*'MPEG')
    output_video = cv2.VideoWriter(output_filename, fourcc, frame_rate,
                                   (int(fw), int(fh)))
    # Create the axes
    if thrd_video_path is None and plot_markers:
        num_subplots = 2
        ax_video = fig.add_subplot(xn_sbps(num_subplots),
                                   yn_sbps(num_subplots), 1)
        ax_3d = fig.add_subplot(xn_sbps(num_subplots),
                                yn_sbps(num_subplots),
                                2,
                                projection='3d')
    elif thrd_video_path is not None:
        num_subplots = 2
        ax_video = fig.add_subplot(xn_sbps(num_subplots),
                                   yn_sbps(num_subplots), 1)
        ax_third = fig.add_subplot(xn_sbps(num_subplots),
                                   yn_sbps(num_subplots), 2)
    else:
        num_subplots = 3
        ax_video = fig.add_subplot(xn_sbps(num_subplots),
                                   yn_sbps(num_subplots), 1)
        ax_3d = fig.add_subplot(xn_sbps(num_subplots),
                                yn_sbps(num_subplots),
                                2,
                                projection='3d')
        ax_third = fig.add_subplot(xn_sbps(num_subplots),
                                   yn_sbps(num_subplots), 3)

    if plot_markers:
        ax_3d.view_init(elev=view[0], azim=view[1])

    for f_idx in tqdm(range(frame_range[0], frame_range[1] + 1),
                      desc='Writing frame'):
        fe, frame = video.read()  # Read the next frame
        if fe is False:
            print('Could not read the frame. Aborting and saving.')
            break

        frame_rgb = frame[..., ::-1].copy()
        # Clear axis 1
        ax_video.cla()
        ax_video.imshow(frame_rgb)
        ax_video.set_xticks([])
        ax_video.set_yticks([])
        # Clear axis 2
        if plot_markers:
            ax_3d.cla()
            ax_3d.set_xlim(x_range)
            ax_3d.set_ylim(y_range)
            ax_3d.set_zlim(z_range)
            if frame_count:
                ax_3d.set_title('Frame: ' + str(f_idx))

        # Handle video 3
        if thrd_video_path is not None:
            frame_from_start = f_idx - frame_range[0]
            thrd_frame_from_start = (
                thrd_video_frame_offset +
                int(frame_from_start / fps * thrd_video_fps))
            # tv_cf = thrd_video.get(cv2.CAP_PROP_POS_FRAMES)
            while thrd_video.get(
                    cv2.CAP_PROP_POS_FRAMES) < thrd_frame_from_start:
                thrd_fe, thrd_frame = thrd_video.read()
            # thrd_video.set(cv2.CAP_PROP_POS_FRAMES, thrd_frame_from_start)
            # thrd_fe, thrd_frame = thrd_video.read()  # Read the next frame
            if thrd_fe is False:
                print(
                    'Could not read the third video frame. Frame in the raw {},'
                    ' frame_from_start: {} thrd_frame_from_start: {}'.format(
                        f_idx, frame_from_start, thrd_frame_from_start))
            else:
                thrd_frame_rgb = thrd_frame[..., ::-1].copy()
                if third_video_crop_hw is not None:
                    if third_video_crop_hw[0] is not None:
                        thrd_frame_rgb = thrd_frame_rgb[
                            third_video_crop_hw[0], :]
                    if third_video_crop_hw[1] is not None:
                        thrd_frame_rgb = thrd_frame_rgb[:,
                                                        third_video_crop_hw[1]]
                ax_third.cla()
                ax_third.imshow(thrd_frame_rgb)
                ax_third.set_xticks([])
                ax_third.set_yticks([])

        # Underlying skeleton
        if plot_markers and skeleton:
            for bpc in bp_connections:
                ibp1 = bp_list.index(bpc[0])
                ibp2 = bp_list.index(bpc[1])

                t_point1 = triangulated_points[f_idx, :, ibp1]
                t_point2 = triangulated_points[f_idx, :, ibp2]

                if any(np.isnan(t_point1)) or any(np.isnan(t_point1)):
                    continue
                ax_3d.plot([t_point1[0], t_point2[0]],
                           [t_point1[1], t_point2[1]],
                           [t_point1[2], t_point2[2]],
                           color='k',
                           linewidth=skeleton_thickness)

        # Bodypart markers
        if plot_markers:
            for ibp in range(np.size(triangulated_points, 2)):
                # Markers
                ax_3d.scatter(triangulated_points[f_idx, 0, ibp],
                              triangulated_points[f_idx, 1, ibp],
                              triangulated_points[f_idx, 2, ibp],
                              color=bp_cmap[ibp, :],
                              s=marker_size)

        # Pull matplotlib data to a variable and format for writing
        canvas.draw()
        temp_frame = np.fromstring(canvas.tostring_rgb(),
                                   dtype='uint8').reshape(int(fh), int(fw), 3)
        temp_frame = temp_frame[..., ::-1].copy()
        output_video.write(temp_frame)

    # Release objects
    mpl_pp.close(fig)
    video.release()
    output_video.release()

    print('*  Video saved to:\n\t' + output_filename)
Esempio n. 10
0
def plot_mass_accumulation_age_map(dataset, plot_path, fit_key='oir_all'):
    """Similar to plot_epoch_sfr_map_vertical, but plots age when each
    patch reaches a threshold fraction of mass accumulation.
    """
    fractions = [0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
    nx = 4
    ny = 2
    assert len(fractions) == nx * ny

    basemap = load_galex_map()

    fig = Figure(figsize=(6.5, 4.5), frameon=False)
    canvas = FigureCanvas(fig)
    gs = gridspec.GridSpec(ny,
                           nx + 1,
                           left=0.12,
                           right=0.9,
                           bottom=0.05,
                           top=0.93,
                           wspace=0.05,
                           hspace=0.1,
                           width_ratios=[1] * nx + [0.1],
                           height_ratios=None)
    ax_cb = fig.add_subplot(gs[:, nx])
    axes = []
    for i, mass_frac in enumerate(fractions):
        iy = i / nx
        ix = i % nx
        ax = setup_galex_axes(fig, gs[iy, ix], basemap)
        if ix > 0:
            ax.coords[1].ticklabels.set_visible(False)
        if iy < (ny - 1):
            ax.coords[0].ticklabels.set_visible(False)
        axes.append(ax)

    for mass_frac, ax in zip(fractions, axes):
        cmap = perceptual_rainbow_16.mpl_colormap
        normalizer = mpl.colors.Normalize(vmin=0, vmax=6, clip=True)
        ra, dec, logage_at_mass_frac = _compute_ages_at_mass_frac(
            dataset, mass_frac, fit_key)
        age_gyr = 10.**(logage_at_mass_frac - 9.)
        mapper = ax.scatter(ra,
                            dec,
                            c=age_gyr,
                            norm=normalizer,
                            cmap=cmap,
                            edgecolors='None',
                            s=16,
                            transform=ax.get_transform('world'))
        label = r'$\frac{{M(t_\mathrm{{L}}>A)}}{{\sum M}} ' \
            '\geq {0:.1f}$'.format(mass_frac)
        ax.text(0.5,
                1.02,
                label,
                ha='center',
                va='bottom',
                size=10,
                transform=ax.transAxes,
                backgroundcolor='w')

    cbar = fig.colorbar(mapper, cax=ax_cb, orientation='vertical')
    cbar.set_label(r'$A$ (Gyr)')

    gs.tight_layout(fig, pad=1.08, h_pad=None, w_pad=None, rect=None)
    canvas.print_figure(plot_path + ".pdf", format="pdf")
Esempio n. 11
0
File: plot.py Progetto: yxngl/GSEApy
def gseaplot(rank_metric,
             term,
             hits_indices,
             nes,
             pval,
             fdr,
             RES,
             pheno_pos='',
             pheno_neg='',
             figsize=(6, 5.5),
             cmap='seismic',
             ofname=None,
             **kwargs):
    """This is the main function for reproducing the gsea plot.

    :param rank_metric: pd.Series for rankings, rank_metric.values.
    :param term: gene_set name
    :param hits_indices: hits indices of rank_metric.index presented in gene set S.
    :param nes: Normalized enrichment scores.
    :param pval: nominal p-value.
    :param fdr: false discovery rate.
    :param RES: running enrichment scores.
    :param pheno_pos: phenotype label, positive correlated.
    :param pheno_neg: phenotype label, negative correlated.
    :param figsize: matplotlib figsize.
    :param ofname: output file name. If None, don't save figure 

    """
    # plt.style.use('classic')
    # center color map at midpoint = 0
    norm = _MidpointNormalize(midpoint=0)

    #dataFrame of ranked matrix scores
    x = np.arange(len(rank_metric))
    rankings = rank_metric.values
    # figsize = (6,6)
    phenoP_label = pheno_pos + ' (Positively Correlated)'
    phenoN_label = pheno_neg + ' (Negatively Correlated)'
    zero_score_ind = np.abs(rankings).argmin()
    z_score_label = 'Zero score at ' + str(zero_score_ind)
    nes_label = 'NES: ' + "{:.3f}".format(float(nes))
    pval_label = 'Pval: ' + "{:.3f}".format(float(pval))
    fdr_label = 'FDR: ' + "{:.3f}".format(float(fdr))
    im_matrix = np.tile(rankings, (2, 1))

    # output truetype
    plt.rcParams.update({'pdf.fonttype': 42, 'ps.fonttype': 42})
    # in most case, we will have many plots, so do not display plots
    # It's also usefull to run this script on command line.

    # GSEA Plots
    gs = plt.GridSpec(16, 1)
    if hasattr(sys, 'ps1') and (ofname is None):
        # working inside python console, show figure
        fig = plt.figure(figsize=figsize)
    else:
        # If working on commandline, don't show figure
        fig = Figure(figsize=figsize)
        canvas = FigureCanvas(fig)
    # Ranked Metric Scores Plot
    ax1 = fig.add_subplot(gs[11:])
    module = 'tmp' if ofname is None else ofname.split(".")[-2]
    if module == 'ssgsea':
        nes_label = 'ES: ' + "{:.3f}".format(float(nes))
        pval_label = 'Pval: '
        fdr_label = 'FDR: '
        ax1.fill_between(x, y1=np.log(rankings), y2=0, color='#C9D3DB')
        ax1.set_ylabel("log ranked metric", fontsize=14)
    else:
        ax1.fill_between(x, y1=rankings, y2=0, color='#C9D3DB')
        ax1.set_ylabel("Ranked list metric", fontsize=14)
    ax1.text(.05,
             .9,
             phenoP_label,
             color='red',
             horizontalalignment='left',
             verticalalignment='top',
             transform=ax1.transAxes)
    ax1.text(.95,
             .05,
             phenoN_label,
             color='Blue',
             horizontalalignment='right',
             verticalalignment='bottom',
             transform=ax1.transAxes)
    # the x coords of this transformation are data, and the y coord are axes
    trans1 = transforms.blended_transform_factory(ax1.transData, ax1.transAxes)
    if module != 'ssgsea':
        ax1.vlines(zero_score_ind,
                   0,
                   1,
                   linewidth=.5,
                   transform=trans1,
                   linestyles='--',
                   color='grey')
        ax1.text(zero_score_ind,
                 0.5,
                 z_score_label,
                 horizontalalignment='center',
                 verticalalignment='center',
                 transform=trans1)
    ax1.set_xlabel("Rank in Ordered Dataset", fontsize=14)
    ax1.spines['top'].set_visible(False)
    ax1.tick_params(axis='both',
                    which='both',
                    top=False,
                    right=False,
                    left=False)
    ax1.locator_params(axis='y', nbins=5)
    ax1.yaxis.set_major_formatter(
        plt.FuncFormatter(
            lambda tick_loc, tick_num: '{:.1f}'.format(tick_loc)))

    # use round method to control float number
    # ax1.yaxis.set_major_formatter(plt.FuncFormatter(lambda tick_loc,tick_num :  round(tick_loc, 1) ))

    # gene hits
    ax2 = fig.add_subplot(gs[8:10], sharex=ax1)

    # the x coords of this transformation are data, and the y coord are axes
    trans2 = transforms.blended_transform_factory(ax2.transData, ax2.transAxes)
    ax2.vlines(hits_indices, 0, 1, linewidth=.5, transform=trans2)
    ax2.spines['bottom'].set_visible(False)
    ax2.tick_params(axis='both',
                    which='both',
                    bottom=False,
                    top=False,
                    labelbottom=False,
                    right=False,
                    left=False,
                    labelleft=False)
    # colormap
    ax3 = fig.add_subplot(gs[10], sharex=ax1)
    ax3.imshow(im_matrix,
               aspect='auto',
               norm=norm,
               cmap=cmap,
               interpolation='none')  # cm.coolwarm
    ax3.spines['bottom'].set_visible(False)
    ax3.tick_params(axis='both',
                    which='both',
                    bottom=False,
                    top=False,
                    labelbottom=False,
                    right=False,
                    left=False,
                    labelleft=False)

    # Enrichment score plot
    ax4 = fig.add_subplot(gs[:8], sharex=ax1)
    ax4.plot(x, RES, linewidth=4, color='#88C544')
    ax4.text(.1, .1, fdr_label, transform=ax4.transAxes)
    ax4.text(.1, .2, pval_label, transform=ax4.transAxes)
    ax4.text(.1, .3, nes_label, transform=ax4.transAxes)

    # the y coords of this transformation are data, and the x coord are axes
    trans4 = transforms.blended_transform_factory(ax4.transAxes, ax4.transData)
    ax4.hlines(0, 0, 1, linewidth=.5, transform=trans4, color='grey')
    ax4.set_ylabel("Enrichment score (ES)", fontsize=14)
    ax4.set_xlim(min(x), max(x))
    ax4.tick_params(axis='both',
                    which='both',
                    bottom=False,
                    top=False,
                    labelbottom=False,
                    right=False)
    ax4.locator_params(axis='y', nbins=5)
    # FuncFormatter need two argument, I don't know why. this lambda function used to format yaxis tick labels.
    ax4.yaxis.set_major_formatter(
        plt.FuncFormatter(
            lambda tick_loc, tick_num: '{:.1f}'.format(tick_loc)))

    # fig adjustment
    fig.suptitle(term, fontsize=16, fontweight='bold')
    fig.subplots_adjust(hspace=0)
    # fig.tight_layout()
    if ofname is not None:
        # canvas.print_figure(ofname, bbox_inches='tight', dpi=300)
        fig.savefig(ofname, bbox_inches='tight', dpi=300)
    return
def matplotlib(pltid):
    """Generate a random image using Matplotlib and display it"""
    # in the future create a private function __import__ to import third-party
    # libraries, so that it can respond gracefully.  See for example the
    # Examples section at https://docs.python.org/2/library/imp.html
    from pylab import savefig
    import numpy as np
    import StringIO
    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
    from matplotlib.figure import Figure
    global user
    app = request.query.app
    cid = request.query.cid

    fig = Figure()
    ax = fig.add_subplot(111)

    # get info about plot
    p = plotmod.plot()
    result = db(plots.id==pltid).select().first()
    plottype = result['ptype']
    options = result['options']
    title = result['title']

    # get info about data source
    # fix in the future to handle multiple data sources
    result = db(datasource.pltid==pltid).select()
    for r in result:
        plotfn = r['filename']
        cols = r['cols']
        line_range = r['line_range']
        (col1str,col2str) = cols.split(":")
        col1 = int(col1str)
        col2 = int(col2str)
        if line_range is not None:
            (line1str,line2str) = line_range.split(":")
            line1 = int(line1str)
            line2 = int(line2str)

    plotfn = re.sub(r"<cid>", cid, plotfn)
    sim_dir = os.path.join(myapps[app].user_dir,user,app,cid)
    plotpath = os.path.join(sim_dir,plotfn)
    xx = p.get_column_of_data(plotpath,col1)
    yy = p.get_column_of_data(plotpath,col2)

    # plot
    if plottype == 'mpl-line':
        ax.plot(xx, yy)
    elif plottype == 'mpl-bar':
        ax.hist(xx, yy, normed=1, histtype='bar', rwidth=0.8)
    else:
        return "ERROR: plottype not supported"
    canvas = FigureCanvas(fig)
    png_output = StringIO.StringIO()
    canvas.print_png(png_output)

    # save file
    if not os.path.exists(config.tmp_dir):
        os.makedirs(config.tmp_dir)
    fn = title+'.png'
    fig.set_size_inches(7,4)
    img_path = os.path.join(sim_dir,fn)
    fig.savefig(img_path)

    # get list of all plots for this app
    query = (apps.id==plots.appid) & (apps.name==app)
    list_of_plots = db(query).select()

    params = {'image': fn, 'app': app, 'cid': cid, 'pltid': pltid,
              'plotpath': plotpath, 'img_path': img_path, 'title': title,
              'rows': list_of_plots, 'apps': myapps.keys() }
    return template('plots/matplotlib', params)
Esempio n. 13
0
def plot_png():
    fig = create_figure()
    output = io.BytesIO()
    FigureCanvas(fig).print_png(output)
    return Response(output.getvalue(), mimetype='image/png')
Esempio n. 14
0
def make_diagnostic_plot(dolphotTable,
                         i,
                         imageKey,
                         band,
                         fmt,
                         plotPath,
                         magLim=None):
    print dolphotTable.image_paths
    nImages = len(dolphotTable.photTable.attrs.image_paths)
    objtype = dolphotTable.photTable.read(field='type')
    if nImages > 1:
        mag = dolphotTable.photTable.read(field='mag')[:, i]
        quality = dolphotTable.photTable.read(field='quality')[:, i]
        chi = dolphotTable.photTable.read(field='chi')[:, i]
        sn = dolphotTable.photTable.read(field='sn')[:, i]
        sharp = dolphotTable.photTable.read(field='sharp')[:, i]
        crowding = dolphotTable.photTable.read(field='crowding')[:, i]
        ecc = dolphotTable.photTable.read(field='ecc')[:, i]
    else:
        mag = dolphotTable.photTable.read(field='mag')
        quality = dolphotTable.photTable.read(field='quality')
        chi = dolphotTable.photTable.read(field='chi')
        sn = dolphotTable.photTable.read(field='sn')
        sharp = dolphotTable.photTable.read(field='sharp')
        crowding = dolphotTable.photTable.read(field='crowding')
        ecc = dolphotTable.photTable.read(field='ecc')

    if magLim is None:
        magBins = np.arange(mag.min(), mag.max(), 0.5)
    else:
        magBins = np.arange(magLim[0], magLim[1], 0.5)
    inds = np.digitize(mag, magBins)
    nBins = inds.max()

    magFcn = _measure_lf(mag, inds, nBins)
    chiMean, chiStd = _measure_luminosity_trend(chi, inds, nBins)
    objTypeFractions = _measure_object_types(objtype, inds, nBins)
    flagFrequencies = _measure_flag_frequencies(quality, inds, nBins)
    snMean, snStd = _measure_luminosity_trend(sn, inds, nBins)
    sharpMean, sharpStd = _measure_luminosity_trend(sharp, inds, nBins)
    crowdingMean, crowdingStd = _measure_luminosity_trend(
        crowding, inds, nBins)
    eccMean, eccStd = _measure_luminosity_trend(ecc, inds, nBins)

    fig = Figure(figsize=(7, 10))
    canvas = FigureCanvas(fig)
    gs = gridspec.GridSpec(8,
                           1,
                           left=0.15,
                           right=0.95,
                           bottom=0.05,
                           top=0.98,
                           wspace=None,
                           hspace=None,
                           width_ratios=None,
                           height_ratios=None)

    axMag = fig.add_subplot(gs[0])
    axMag.semilogy(magBins, magFcn, 'k')
    axMag.set_ylabel(r"$N(M)$")
    for tl in axMag.get_xmajorticklabels():
        tl.set_visible(False)
    axMag.set_xlim(magLim)

    axType = fig.add_subplot(gs[1])
    axType.plot(magBins, objTypeFractions[1], ls='-', c='k', label="1, Good")
    axType.plot(magBins, objTypeFractions[2], ls='-', c='r', label="2, Faint")
    axType.plot(magBins,
                objTypeFractions[3],
                ls='-',
                c='g',
                label="3, Elongated")
    axType.plot(magBins, objTypeFractions[4], ls='-', c='b', label="4, Sharp")
    axType.plot(magBins,
                objTypeFractions[5],
                ls='-',
                c='c',
                label="5, Extended")
    axType.set_ylabel(r"Type")
    for tl in axType.get_xmajorticklabels():
        tl.set_visible(False)
    axType.set_xlim(magLim)

    axFlags = fig.add_subplot(gs[2])
    axFlags.plot(magBins, flagFrequencies[0], ls='-', c='k', label="0, Good")
    axFlags.plot(magBins,
                 flagFrequencies[1],
                 ls='-',
                 c='r',
                 label="1, Ap. Off Chip")
    axFlags.plot(magBins,
                 flagFrequencies[2],
                 ls='-',
                 c='g',
                 label="2, Many Bad Pix")
    axFlags.plot(magBins,
                 flagFrequencies[4],
                 ls='-',
                 c='b',
                 label="4, Sat at Centre")
    axFlags.plot(magBins, flagFrequencies[8], ls='-', c='c', label="8, V. Bad")
    axFlags.set_ylabel(r"Flag")
    for tl in axFlags.get_xmajorticklabels():
        tl.set_visible(False)
    axFlags.set_xlim(magLim)

    axSN = fig.add_subplot(gs[3])
    axSN.errorbar(magBins, snMean, yerr=snStd, color='k')
    axSN.set_ylabel(r"SN")
    for tl in axSN.get_xmajorticklabels():
        tl.set_visible(False)
    axSN.set_xlim(magLim)
    axSN.set_ylim(0., 200)

    axChi = fig.add_subplot(gs[4])
    axChi.errorbar(magBins, chiMean, yerr=chiStd, color='k')
    axChi.set_ylabel("chi")
    for tl in axChi.get_xmajorticklabels():
        tl.set_visible(False)
    axChi.set_xlim(magLim)
    axChi.set_ylim(0., 50)

    axSharp = fig.add_subplot(gs[5])
    axSharp.errorbar(magBins, sharpMean, yerr=sharpStd, color='k')
    axSharp.set_ylabel("sharp")
    for tl in axSharp.get_xmajorticklabels():
        tl.set_visible(False)
    axSharp.set_xlim(magLim)

    axCrowd = fig.add_subplot(gs[6])
    axCrowd.errorbar(magBins, crowdingMean, yerr=crowdingStd, color='k')
    axCrowd.set_ylabel(r"crowd")
    for tl in axCrowd.get_xmajorticklabels():
        tl.set_visible(False)
    axCrowd.set_xlim(magLim)

    axEcc = fig.add_subplot(gs[7])
    axEcc.errorbar(magBins, eccMean, yerr=eccStd, color='k')
    axEcc.set_ylabel(r"Ecc")
    axEcc.set_xlabel(r"mag (%s)" % band)
    axEcc.set_xlim(magLim)

    gs.tight_layout(fig, pad=1.08, h_pad=None, w_pad=None, rect=None)
    canvas.print_figure(plotPath, format=fmt)
Esempio n. 15
0
def positions_img():
    global last_img
    global show_circles
    global show_rssi

    if request.method == 'POST':
        plotopts = request.form.getlist('plotopt')
        print(plotopts)
        if "Circles" in plotopts:
            show_circles = True
        else:
            show_circles = False

        if "RSSI" in plotopts:
            show_rssi = True
        else:
            show_rssi = False

        return redirect(url_for('plot_positions'))

    # creamos la imagen. Será un plot de matplotlib con los anchors y los devices. retorna un png
    if request.method == 'GET':
        dev_pos = ctrl.getDevicesPositions()
        anchors_pos = ctrl.getAnchorsPositions()
        print("estimated position RSSI+ACC+ORI: ")
        print(dev_pos)
        print()

        # imprimimos los anchors (launchpads) primeramente.

        x_anc = []
        y_anc = []
        anc_names = []
        for a in anchors_pos:
            x_anc.append(float(anchors_pos[a]['X']))
            y_anc.append(float(anchors_pos[a]['Y']))
            anc_names.append(a)

        fig, ax = plt.subplots()
        plt.xlim((ctrl.getRoomXMin(), ctrl.getRoomXMax()))
        plt.ylim((ctrl.getRoomYMin(), ctrl.getRoomYMax()))
        color_array = ["green", "yellow", "purple"]
        ax.scatter(x_anc, y_anc, c=color_array)

        for i, txt in enumerate(anc_names):
            ax.text(x_anc[i], y_anc[i], txt, fontsize=14)
        # ax.annotate(txt, (x_anc[i],y_anc[i]))

        # imprimimos los devices (móviles) a continuación.

        x_dev = []
        y_dev = []
        dev_names = []
        try:
            for d in dev_pos:
                x_dev.append(float(dev_pos[d]['X']))
                y_dev.append(float(dev_pos[d]['Y']))
                dev_names.append(d)

            ax.scatter(x_dev, y_dev, c='b')

            for i, txt in enumerate(dev_names):
                ax.text(x_dev[i], y_dev[i], txt, fontsize=14)

            ax.set(xlabel='distance (m)',
                   ylabel='distance (m)',
                   title='Positions')
            ax.grid()

            # imprimimos los circulos de alcance de los anchors. Miramos si el usuario lo pide.
            # DE MOMENTO SUPONEMOS QUE SOLO HAY 1 DEVICE. PARA MAS DE UNO, HABRÁ QUE DAR A ESCOGER AL USUARIO
            # DE CUAL QUIERE VER LOS CIRCULOS Y LA RSSI.

            if show_circles:  # usuario pide que se muestren los circulos
                res = list(zip(x_anc, y_anc, anc_names))
                i = 0
                for x, y, name in res:
                    ax.add_artist(
                        plt.Circle((x, y),
                                   ctrl.getDistanceFromAnchorToDevice(
                                       name, dev_names[0]),
                                   color=color_array[i],
                                   alpha=0.25))
                    i += 1

            if show_rssi:

                for devname in dev_names:
                    for i, anc_name in enumerate(anc_names):
                        rssi = ctrl.getRssiFromAnchorOfDevice(
                            anc_name, devname)
                        ax.text(x_anc[i],
                                y_anc[i] - 0.15,
                                'RSSI: ' + str(rssi),
                                weight="bold")

        except KeyError:
            return last_img

        # NO TOCAR A PARTIR DE AQUI!
        canvas = FigureCanvas(fig)
        plt.close(fig)
        output = io.BytesIO()
        canvas.print_png(output)
        response = make_response(output.getvalue())
        response.mimetype = 'image/png'
        last_img = response
        return response
Esempio n. 16
0
def show(pdb_id, ca=True, norm=False):
    """Create B-factor plots for both BDB and PDB entries.

    If ca is false, show a bigger plot with all atoms
    If norm is true, subtract mean and scale to unit variance

    If the pdb_id is invalid return None.
    If both PDB and BDB entries do not exist, return None.
    If only the BDB entry does not exist, only plot the PDB B-factors.
    """
    response = None
    minor = ca

    # Create Bio.PDB structures
    sp = get_structure(pdb_id, "pdb")
    if not sp:
        return None
    sb = get_structure(pdb_id, "bdb")

    # PDB
    # Get a list of (full atom id, B-factor) tuples per chain
    bp, b_num = get_b_factors(sp)

    # Calculate figure size
    dpi = 80
    w_pad = 10
    fig_size, minor = calc_fig_size(b_num=b_num, ca=ca, dpi=dpi, w_pad=w_pad)

    # Create the figure
    _log.debug("Creating figure...")
    fig = Figure(figsize=fig_size, dpi=dpi)
    ax = fig.add_subplot(111)
    fig.tight_layout(pad=w_pad)

    # Set title, ylabel and grid
    ax.set_title(pdb_id, fontsize=20)
    ylab = "Normalized B-factor" if norm else "B-factor"
    ax.set_ylabel(ylab)
    ax.grid(True)
    ax.set_axisbelow(True)

    # PDB B-factors
    b_fac_p, b_ind_p = get_bdata(chain_list=bp, ca=ca, norm=norm)
    b_fac_plot = [item for sublist in b_fac_p for item in sublist]
    p_line, = ax.plot(b_fac_plot, color="#B98C6A", ls="-", lw=2)
    ax.set_xlim(0, len(b_fac_plot))

    # BDB B-factors
    if sb:
        bb, b_num = get_b_factors(sb)
        b_fac_b, b_ind_b = get_bdata(chain_list=bb, ca=ca, norm=norm)
        b_fac_blot = [item for sublist in b_fac_b for item in sublist]
        b_line, = ax.plot(b_fac_blot, color="#49597C", ls="-", lw=2)
        ax.legend(("pdb", "bdb"))
    else:
        ax.legend(("pdb"))

    # Collapse the labels and indices
    # sub_bp = [bp[i] for i in b_ind_p]
    sub_bp = []
    for i, chain in enumerate(bp):
        for j in b_ind_p[i]:
            sub_bp.append(chain[j])

    # X-axis ticks and labels
    xt, xtl, xtm = get_xticks(b_list=sub_bp, ca=ca, minor=minor)
    ax.xaxis.set_ticks(xt)
    ax.xaxis.set_ticklabels(xtl, rotation="vertical")
    if len(xtm) > 0:
        ax.xaxis.set_ticks(xtm, minor=True)

    # Create a response
    canvas = FigureCanvas(fig)
    output = StringIO.StringIO()
    canvas.print_png(output)
    response = make_response(output.getvalue())
    response.mimetype = "image/png"

    _log.debug("'Uploading' figure...")

    return response
Esempio n. 17
0
def make_word_board():
    fig = generate_word_board()
    output = io.BytesIO()
    FigureCanvas(fig).print_png(output)
    return Response(output.getvalue(), mimetype='image/png')
Esempio n. 18
0
def plot_linear_relation(x, y, x_err=None, y_err=None, title=None, point_label=None, legend=None, plot_range=None, plot_range_y=None, x_label=None, y_label=None, y_2_label=None, marker_style='-o', log_x=False, log_y=False, size=None, filename=None):
    ''' Takes point data (x,y) with errors(x,y) and fits a straight line. The deviation to this line is also plotted, showing the offset.

     Parameters
    ----------
    x, y, x_err, y_err: iterable

    filename: string, PdfPages object or None
        PdfPages file object: plot is appended to the pdf
        string: new plot file with the given filename is created
        None: the plot is printed to screen
    '''
    fig = Figure()
    canvas = FigureCanvas(fig)
    ax = fig.add_subplot(111)
    if x_err is not None:
        x_err = [x_err, x_err]
    if y_err is not None:
        y_err = [y_err, y_err]
    ax.set_title(title)
    if y_label is not None:
        ax.set_ylabel(y_label)
    if log_x:
        ax.set_xscale('log')
    if log_y:
        ax.set_yscale('log')
    if plot_range:
        ax.set_xlim((min(plot_range), max(plot_range)))
    if plot_range_y:
        ax.set_ylim((min(plot_range_y), max(plot_range_y)))
    if legend:
        fig.legend(legend, 0)
    ax.grid(True)
    ax.errorbar(x, y, xerr=x_err, yerr=y_err, fmt='o', color='black')  # plot points
    # label points if needed
    if point_label is not None:
        for X, Y, Z in zip(x, y, point_label):
            ax.annotate('{}'.format(Z), xy=(X, Y), xytext=(-5, 5), ha='right', textcoords='offset points')
    # line fit
    line_fit, pcov = np.polyfit(x, y, 1, full=False, cov=True)
#     print pcov
#     chi_squared = np.sum((np.polyval(line_fit, x) - y) ** 2)
    fit_fn = np.poly1d(line_fit)
    ax.plot(x, fit_fn(x), '-', lw=2, color='gray')
    line_fit_legend_entry = 'line fit: ax + b\na=$%.2f\pm%.2f$\nb=$%.2f\pm%.2f$' % (line_fit[0], np.absolute(pcov[0][0]) ** 0.5, abs(line_fit[1]), np.absolute(pcov[1][1]) ** 0.5)

#     fig.legend(["data", line_fit_legend_entry], 0)
    setp(ax.get_xticklabels(), visible=False)  # remove ticks at common border of both plots

    divider = make_axes_locatable(ax)
    ax_bottom_plot = divider.append_axes("bottom", 2.0, pad=0.0, sharex=ax)

    ax_bottom_plot.bar(x, y - fit_fn(x), align='center', width=np.amin(np.diff(x)) / 2, color='gray')
#     plot(x, y - fit_fn(x))
    ax_bottom_plot.grid(True)
    if x_label is not None:
        ax.set_xlabel(x_label)
    if y_2_label is not None:
        ax.set_ylabel(y_2_label)

    ax.set_ylim((-np.amax(np.abs(y - fit_fn(x)))), (np.amax(np.abs(y - fit_fn(x)))))

    ax.plot(ax.set_xlim(), [0, 0], '-', color='black')
    setp(ax_bottom_plot.get_yticklabels()[-2:-1], visible=False)
#     print ax_bottom_plot.get_yticklabels()[1]

#     ax.set_aspect(2)
#     ax_bottom_plot.set_aspect(2)

    if size is not None:
        fig.set_size_inches(size)

    if not filename:
        fig.show()
    elif isinstance(filename, PdfPages):
        filename.savefig(fig)
    elif filename:
        fig.savefig(filename, bbox_inches='tight')

    return fig
Esempio n. 19
0
File: plot.py Progetto: yxngl/GSEApy
def dotplot(df,
            column='Adjusted P-value',
            title='',
            cutoff=0.05,
            top_term=10,
            sizes=None,
            norm=None,
            legend=True,
            figsize=(6, 5.5),
            cmap='RdBu_r',
            ofname=None,
            **kwargs):
    """Visualize enrichr results.

    :param df: GSEApy DataFrame results.
    :param column: which column of DataFrame to show. Default: Adjusted P-value
    :param title: figure title
    :param cutoff: p-adjust cut-off.
    :param top_term: number of enriched terms to show.
    :param ascending: bool, the order of y axis.
    :param sizes: tuple, (min, max) scatter size. Not functional for now
    :param norm: maplotlib.colors.Normalize object.
    :param legend: bool, whether to show legend.
    :param figsize: tuple, figure size. 
    :param cmap: matplotlib colormap
    :param ofname: output file name. If None, don't save figure 

    """

    colname = column
    # sorting the dataframe for better visualization
    if colname in ['Adjusted P-value', 'P-value']:
        df = df[df[colname] <= cutoff]
        if len(df) < 1:
            msg = "Warning: No enrich terms when cutoff = %s" % cutoff
            return msg
        df = df.assign(logAP=lambda x: -x[colname].apply(np.log10))
        colname = 'logAP'
    df = df.sort_values(by=colname).iloc[-top_term:, :]
    #
    temp = df['Overlap'].str.split("/", expand=True).astype(int)
    df = df.assign(Hits=temp.iloc[:, 0], Background=temp.iloc[:, 1])
    df = df.assign(Hits_ratio=lambda x: x.Hits / x.Background)
    # x axis values
    x = df.loc[:, colname].values
    combined_score = df['Combined Score'].round().astype('int')
    # y axis index and values
    y = [i for i in range(0, len(df))]
    ylabels = df['Term'].values
    # Normalise to [0,1]
    # b = (df['Count']  - df['Count'].min())/ np.ptp(df['Count'])
    # area = 100 * b

    # control the size of scatter and legend marker
    levels = numbers = np.sort(df.Hits.unique())
    if norm is None:
        norm = Normalize()
    elif isinstance(norm, tuple):
        norm = Normalize(*norm)
    elif not isinstance(norm, Normalize):
        err = ("``size_norm`` must be None, tuple, " "or Normalize object.")
        raise ValueError(err)
    min_width, max_width = np.r_[20, 100] * plt.rcParams["lines.linewidth"]
    norm.clip = True
    if not norm.scaled():
        norm(np.asarray(numbers))
    size_limits = norm.vmin, norm.vmax
    scl = norm(numbers)
    widths = np.asarray(min_width + scl * (max_width - min_width))
    if scl.mask.any():
        widths[scl.mask] = 0
    sizes = dict(zip(levels, widths))
    df['sizes'] = df.Hits.map(sizes)
    area = df['sizes'].values

    # creat scatter plot
    if hasattr(sys, 'ps1') and (ofname is None):
        # working inside python console, show figure
        fig, ax = plt.subplots(figsize=figsize)
    else:
        # If working on commandline, don't show figure
        fig = Figure(figsize=figsize)
        canvas = FigureCanvas(fig)
        ax = fig.add_subplot(111)
    vmin = np.percentile(combined_score.min(), 2)
    vmax = np.percentile(combined_score.max(), 98)
    sc = ax.scatter(x=x,
                    y=y,
                    s=area,
                    edgecolors='face',
                    c=combined_score,
                    cmap=cmap,
                    vmin=vmin,
                    vmax=vmax)

    if column in ['Adjusted P-value', 'P-value']:
        xlabel = "-log$_{10}$(%s)" % column
    else:
        xlabel = column
    ax.set_xlabel(xlabel, fontsize=14, fontweight='bold')
    ax.yaxis.set_major_locator(plt.FixedLocator(y))
    ax.yaxis.set_major_formatter(plt.FixedFormatter(ylabels))
    ax.set_yticklabels(ylabels, fontsize=16)

    # ax.set_ylim([-1, len(df)])
    ax.grid()
    # colorbar
    cax = fig.add_axes([0.95, 0.20, 0.03, 0.22])
    cbar = fig.colorbar(
        sc,
        cax=cax,
    )
    cbar.ax.tick_params(right=True)
    cbar.ax.set_title('Combined\nScore', loc='left', fontsize=12)

    # for terms less than 3
    if len(df) >= 3:
        # find the index of the closest value to the median
        idx = [
            area.argmax(),
            np.abs(area - area.mean()).argmin(),
            area.argmin()
        ]
        idx = unique(idx)
    else:
        idx = df.index.values
    label = df.iloc[idx, df.columns.get_loc('Hits')]

    if legend:
        handles, _ = ax.get_legend_handles_labels()
        legend_markers = []
        for ix in idx:
            legend_markers.append(ax.scatter([], [], s=area[ix], c='b'))
        # artist = ax.scatter([], [], s=size_levels,)
        ax.legend(legend_markers, label, title='Hits')
    ax.set_title(title, fontsize=20, fontweight='bold')

    if ofname is not None:
        # canvas.print_figure(ofname, bbox_inches='tight', dpi=300)
        fig.savefig(ofname, bbox_inches='tight', dpi=300)
        return
    return ax
Esempio n. 20
0
def ff_img(filename):
    result = get_ncaa_data()
    df = DataFrame(result)
    df.columns = [
        'id', 'tourney_year', 'winner_seed', 'winner_team', 'loser_seed',
        'loser_team', 'other_seed_1', 'other_team_1', 'other_seed_2',
        'other_team_2', 'final_four_sum'
    ]

    x = df['tourney_year']
    #x = np.arange(1979,2016,1)
    y = df['final_four_sum']
    #y = x**2 + 2*x - 5
    fig = Figure()  # Plotting
    canvas = FigureCanvas(fig)
    ax1 = fig.add_subplot(311)
    ax2 = fig.add_subplot(312)
    ax3 = fig.add_subplot(313)

    colors = cm.rainbow(np.linspace(0, 1, len(df)))
    ax1.scatter(x, y, c=colors)
    ax1.set_xlabel('')
    ax1.set_ylabel('SUM OF SEEDS')

    ax2.hist(y, bins=22)
    ax2.set_xlabel('SUM OF SEEDS')
    ax2.set_ylabel('COUNT')

    db_url = 'da602.sqlite'
    conn = sqlite3.connect(db_url)
    c = conn.cursor()

    c.execute('select seed, team, percent_picked from PICKS_2016')
    picks_results = c.fetchall()

    seeds = []
    teams = []
    probs = []

    team_sample = []

    for picks_row in picks_results:
        seeds.append(int(picks_row[0]))
        teams.append(picks_row[1])
        probs.append(float(picks_row[2]))

    print(probs)

    # make probs add to 1
    multiplier = (1.0 / sum(probs))

    numpy_probs = np.asarray(probs)

    numpy_probs = numpy_probs * multiplier
    print(sum(numpy_probs))

    seed_sums = []

    for i in range(0, 99000):
        four_seeds = np.random.choice(seeds, 4, p=numpy_probs)
        four_seeds_sum = sum(four_seeds)
        seed_sums.append(four_seeds_sum)
        #print four_seeds_sum

    ax3.hist(seed_sums, normed=1, bins=15)
    #ax3.set_title("Peoples Real Picks Sums Histogram")
    ax3.set_xlabel("Sum")
    ax3.set_ylabel("Frequency")

    conn.commit()
    conn.close()

    canvas.print_figure(filename)
    return static_file(filename, root='./', mimetype='image/png')
Esempio n. 21
0
File: plot.py Progetto: yxngl/GSEApy
def barplot(df,
            column='Adjusted P-value',
            title="",
            cutoff=0.05,
            top_term=10,
            figsize=(6.5, 6),
            color='salmon',
            ofname=None,
            **kwargs):
    """Visualize enrichr results.

    :param df: GSEApy DataFrame results.
    :param column: which column of DataFrame to show. Default: Adjusted P-value
    :param title: figure title.
    :param cutoff: cut-off of the cloumn you've chosen.
    :param top_term: number of top enriched terms to show.
    :param figsize: tuple, matplotlib figsize.
    :param color: color for bars.
    :param ofname: output file name. If None, don't save figure    
    
    """

    colname = column
    if colname in ['Adjusted P-value', 'P-value']:
        df = df[df[colname] <= cutoff]
        if len(df) < 1:
            msg = "Warning: No enrich terms using library %s when cutoff = %s" % (
                title, cutoff)
            return msg
        df = df.assign(logAP=lambda x: -x[colname].apply(np.log10))
        colname = 'logAP'
    dd = df.sort_values(by=colname).iloc[-top_term:, :]
    # dd = d.head(top_term).sort_values('logAP')
    # create bar plot
    if hasattr(sys, 'ps1') and (ofname is None):
        # working inside python console, show (True) figure
        fig = plt.figure(figsize=figsize)
    else:
        # If working on commandline, don't show figure
        fig = Figure(figsize=figsize)
        canvas = FigureCanvas(fig)
    ax = fig.add_subplot(111)
    bar = dd.plot.barh(x='Term',
                       y=colname,
                       color=color,
                       alpha=0.75,
                       fontsize=16,
                       ax=ax)

    if column in ['Adjusted P-value', 'P-value']:
        xlabel = "-log$_{10}$(%s)" % column
    else:
        xlabel = column
    bar.set_xlabel(xlabel, fontsize=16, fontweight='bold')
    bar.set_ylabel("")
    bar.set_title(title, fontsize=24, fontweight='bold')
    bar.xaxis.set_major_locator(MaxNLocator(integer=True))
    bar.legend_.remove()
    adjust_spines(ax, spines=['left', 'bottom'])

    if ofname is not None:
        # canvas.print_figure(ofname, bbox_inches='tight', dpi=300)
        fig.savefig(ofname, bbox_inches='tight', dpi=300)
        return
    return ax
Esempio n. 22
0
    def render(self, mode='human'):
        from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
        from mpl_toolkits.mplot3d import Axes3D
        from matplotlib import pyplot as plt
        from matplotlib.figure import Figure

        obs = (self.get_obs(quantity='ligand')[:, :, :, -1]).squeeze()
        obs1 = (self.get_obs(quantity='protein')[:, :, :, -1]).squeeze()
        # np.save("/Users/austin/obs.npy", obs)
        # np.save("/Users/austin/pro.npy", obs1)
        print(obs.shape)
        fig = plt.figure(figsize=(10, 10), dpi=100)
        ax = fig.gca(projection='3d')
        canvas = FigureCanvas(fig)

        coords_x = []
        coords_y = []
        coords_z = []
        for i in range(obs.shape[0]):
            for j in range(obs.shape[1]):
                for z in range(obs.shape[2]):
                    if obs[i, j, z] == 1:
                        coords_x.append(i)
                        coords_y.append(j)
                        coords_z.append(z)

        coords_x1 = []
        coords_y1 = []
        coords_z1 = []
        for i in range(obs.shape[0]):
            for j in range(obs.shape[1]):
                for z in range(obs.shape[2]):
                    if obs1[i, j, z] == 1:
                        coords_x1.append(i)
                        coords_y1.append(j)
                        coords_z1.append(z)

        ax.set_title("Current step:" + str(self.steps) + ", Curr Reward" +
                     str(self.last_reward) + ', Curr RSUm' +
                     str(self.cur_reward_sum) + 'score' + str(self.last_score))
        try:
            ax.plot_trisurf(coords_x,
                            coords_y,
                            coords_z,
                            linewidth=0.2,
                            antialiased=True)
            ax.plot_trisurf(coords_x1,
                            coords_y1,
                            coords_z1,
                            linewidth=0.2,
                            antialiased=True,
                            alpha=0.5)

        except:
            pass

        ax.set_xlim(0, 25)
        ax.set_ylim(0, 26)
        ax.set_zlim(0, 27)
        # fig.show()
        canvas.draw()  # draw the canvas, cache the renderer
        width, height = fig.get_size_inches() * fig.get_dpi()
        img = np.fromstring(canvas.tostring_rgb(), dtype=np.uint8)
        img = img.reshape(100 * 10, 100 * 10, 3)

        if mode == 'rgb_array':
            return img
        elif mode == 'human':
            from gym.envs.classic_control import rendering
            if self.viewer is None:
                self.viewer = rendering.SimpleImageViewer()
            self.viewer.imshow(img)
            return self.viewer.isopen
Esempio n. 23
0
File: plot.py Progetto: yxngl/GSEApy
def heatmap(df,
            z_score=None,
            title='',
            figsize=(5, 5),
            cmap='RdBu_r',
            xticklabels=True,
            yticklabels=True,
            ofname=None,
            **kwargs):
    """Visualize the dataframe.

    :param df: DataFrame from expression table.
    :param z_score: z_score axis{0, 1}. If None, don't normalize data.
    :param title: gene set name.
    :param outdir: path to save heatmap.
    :param figsize: heatmap figsize.
    :param cmap: matplotlib colormap.
    :param ofname: output file name. If None, don't save figure 

    """
    df = zscore(df, axis=z_score)
    df = df.iloc[::-1]
    # Get the positions and used label for the ticks
    ny, nx = df.shape
    xticks = np.arange(0, nx, 1) + .5
    yticks = np.arange(0, ny, 1) + .5

    # If working on commandline, don't show figure
    if hasattr(sys, 'ps1') and (ofname is None):
        fig = plt.figure(figsize=figsize)
    else:
        fig = Figure(figsize=figsize)
        canvas = FigureCanvas(fig)
    ax = fig.add_subplot(111)
    vmin = np.percentile(df.min(), 2)
    vmax = np.percentile(df.max(), 98)
    matrix = ax.pcolormesh(df.values, cmap=cmap, vmin=vmin, vmax=vmax)
    ax.set_ylim([0, len(df)])
    ax.set(xticks=xticks, yticks=yticks)
    ax.set_xticklabels(df.columns.values if xticklabels else '',
                       fontsize=14,
                       rotation=90)
    ax.set_yticklabels(df.index.values if yticklabels else '', fontsize=14)
    ax.set_title("%s\nHeatmap of the Analyzed Geneset" % title, fontsize=20)
    ax.tick_params(axis='both',
                   which='both',
                   bottom=False,
                   top=False,
                   right=False,
                   left=False)
    # cax=fig.add_axes([0.93,0.25,0.05,0.20])
    # cbar = fig.colorbar(matrix, cax=cax)
    cbar = colorbar(matrix)
    cbar.ax.tick_params(axis='both',
                        which='both',
                        bottom=False,
                        top=False,
                        right=False,
                        left=False)
    for side in ["top", "right", "left", "bottom"]:
        ax.spines[side].set_visible(False)
        cbar.ax.spines[side].set_visible(False)
    # cbar.ax.set_title('',loc='left')

    if ofname is not None:
        # canvas.print_figure(ofname, bbox_inches='tight', dpi=300)
        fig.savefig(ofname, bbox_inches='tight', dpi=300)
    return
Esempio n. 24
0
def plot_major_ax_sfr_linear(dataset, plot_path):
    age_spans = [(0, 25), (25, 50), (50, 79), (79, 100), (100, 158),
                 (158, 200), (200, 251), (251, 316), (316, 398), (0, 400)]
    palette = palettable.cubehelix.Cubehelix.make(start_hue=240.,
                                                  end_hue=-300.,
                                                  min_sat=1.,
                                                  max_sat=2.5,
                                                  min_light=0.3,
                                                  max_light=0.8,
                                                  gamma=.9,
                                                  n=len(age_spans) - 1)
    colors = list(palette.mpl_colors) + ['k']
    labels = ['{0} - {1} Myr'.format(*a) for a in age_spans] + ['400 Myr Mean']

    basemap = load_galex_map()
    fig = Figure(figsize=(6.5, 3.0), frameon=False)
    canvas = FigureCanvas(fig)
    gs = gridspec.GridSpec(1,
                           3,
                           left=0.12,
                           right=0.97,
                           bottom=0.15,
                           top=0.95,
                           wspace=0.1,
                           hspace=None,
                           width_ratios=(1, 1, 0.3),
                           height_ratios=None)
    ax_ms = fig.add_subplot(gs[0])
    ax_oir = fig.add_subplot(gs[1])
    for ax in (ax_ms, ax_oir):
        ax.set_xlabel(r'$R_\mathrm{maj}~(\mathrm{kpc})$')
        ax.set_xlim(0, 20.)
        ax.set_ylim(-5, 4000)
    ax_ms.set_ylabel(LIN_SFR_LABEL)
    for tl in ax_oir.get_ymajorticklabels():
        tl.set_visible(False)
    ax_ms.text(0.9,
               0.9,
               'ACS-MS',
               ha='right',
               va='top',
               transform=ax_ms.transAxes)
    ax_oir.text(0.9,
                0.9,
                'OIR-ALL',
                ha='right',
                va='top',
                transform=ax_oir.transAxes)

    ax_map = setup_galex_axes(fig, gs[2], basemap)
    plot_patch_footprints(ax_map)
    patch_keys = majoraxplot.select_patches(dataset)
    majoraxplot.plot_highlighted_patches(dataset, patch_keys, ax_map)
    ax_map.coords[0].ticklabels.set_visible(False)
    ax_map.coords[1].ticklabels.set_visible(False)

    r_grid, binned_patches = majoraxplot.bin_patches_radially(
        dataset, patch_keys)
    for fit_key, ax in zip(('lewis', 'oir_all'), (ax_ms, ax_oir)):
        for (age_min, age_max), c, label in zip(age_spans, colors, labels):
            sfr = [
                majoraxplot.compute_sfr_in_span(dataset,
                                                patches,
                                                fit_key,
                                                age_min,
                                                age_max,
                                                lin_scale=True)
                for patches in binned_patches
            ]
            ax.plot(r_grid, sfr, c=c, label=label)

    ax_ms.legend(loc='center left', frameon=False, ncol=2, fontsize=6)

    gs.tight_layout(fig, pad=1.08, h_pad=None, w_pad=None, rect=None)
    canvas.print_figure(plot_path + ".pdf", format="pdf")
Esempio n. 25
0
def candle(request, data):
    import matplotlib.pyplot as plt
    from matplotlib.dates import DateFormatter, HourLocator,\
        DayLocator, MONDAY
    from matplotlib.finance import quotes_historical_yahoo_ohlc, candlestick_ohlc, candlestick2_ohlc
    from matplotlib.figure import Figure
    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
    import base64
    import io
    from datetime import timedelta
    import datetime

    data = data.all().reverse()

    hours = HourLocator()  # minor ticks on the hours
    alldays = DayLocator()  # major ticks on the days
    dayFormatter = DateFormatter('%d %b')  # e.g., 12 Jan
    hourFormatter = DateFormatter('%H:%M:%S.%f')  # e.g., 12

    if len(data) == 0:
        raise SystemExit

    #fig, ax = plt.subplots()
    #fig.subplots_adjust(bottom=0.2)
    fig = Figure(figsize=(10, 5))
    ax = fig.add_subplot(111)
    #ax.xaxis.set_major_locator(alldays)
    #ax.xaxis.set_minor_locator(hours)
    #ax.xaxis.set_major_formatter(dayFormatter)
    #ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d'))
    #ax.xaxis.set_minor_formatter(hourFormatter)

    #plot_day_summary(ax, quotes, ticksize=3)
    #candlestick_ohlc(ax, quotes, width=0.6)
    d = timedelta(minutes=45)

    startTime = ""
    opens = ()
    closes = ()
    highs = ()
    lows = ()
    xlabels = ()
    minPrice = 0
    maxPrice = 0

    for row in data:
        if startTime == "":
            startTime = row.trade_time
            opens += (row.price, )
            xlabels += (row.trade_time.strftime("%d/%m %H:%M"), )
            minPrice = row.price
            maxPrice = row.price
        if minPrice > row.price:
            minPrice = row.price
        if maxPrice < row.price:
            maxPrice = row.price
        if row.trade_time > startTime + d:
            closes += (row.price, )
            lows += (minPrice, )
            highs += (maxPrice, )
            startTime = ""
    if startTime != "":
        closes += (data.all().reverse()[0].price, )
        lows += (minPrice, )
        highs += (maxPrice, )

    #plot_day_summary(ax, quotes2, ticksize=3)
    #candlestick2_ohlc(ax, quotes2['opens'], quotes2['highs'], quotes2['lows'], quotes2['closes'], width=0.2, colorup='k', colordown='r', alpha=1.0)
    candlestick2_ohlc(ax, opens, highs, lows, closes, width=0.45)

    #ax.xaxis_date()
    ax.autoscale_view()
    ax.set_ylabel('Sale Price (p)')
    ax.set_xlabel('Time')
    ax.set_title(data[1].symbol)
    #ax.plot_date(x, y, '-')
    #ax.xaxis.set_major_formatter(DateFormatter('%d/%m %H:%M'))
    #ax.set_xticklabels(xlabels, minor=False)
    #fig.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')
    fig.autofmt_xdate()
    canvas = FigureCanvas(fig)
    sio = io.BytesIO()
    fig.savefig(sio, format="png")
    #plt.show()
    return base64.encodebytes(sio.getvalue()).decode()
Esempio n. 26
0
def plot_to_img(fig):
    pngImage = io.BytesIO()
    FigureCanvas(fig).print_png(pngImage)
    pngImageB64String = "data:image/png;base64,"
    pngImageB64String += base64.b64encode(pngImage.getvalue()).decode('utf8')
    return pngImageB64String
Esempio n. 27
0
    def plot_vsection(self, data, lats, lons, valid_time, init_time,
                      resolution=(-1, -1), bbox=(-1, 1050, -1, 200), style=None,
                      show=False,
                      highlight=None, noframe=False, figsize=(960, 480), draw_verticals=False,
                      numlabels=10, orography_color='k', transparent=False,
                      return_format="image/png"):
        """
        """
        # Check if required data is available.
        self.data_units = self.driver.data_units.copy()
        for datatype, dataitem, dataunit in self.required_datafields:
            if dataitem not in data:
                raise KeyError(f"required data field '{dataitem}' not found")
            origunit = self.driver.data_units[dataitem]
            if dataunit is not None:
                data[dataitem] = convert_to(data[dataitem], origunit, dataunit)
                self.data_units[dataitem] = dataunit
            else:
                logging.debug("Please add units to plot variables")

        # Copy parameters to properties.
        self.data = data
        self.lats = lats
        self.lat_inds = np.arange(len(lats))
        self.lons = lons
        self.valid_time = valid_time
        self.init_time = init_time
        self.resolution = resolution
        self.style = style
        self.highlight = highlight
        self.noframe = noframe
        self.draw_verticals = draw_verticals
        self.p_bot = bbox[1] * 100
        self.p_top = bbox[3] * 100
        self.numlabels = numlabels
        self.orography_color = orography_color

        # Provide an air_pressured 2-D field in 'Pa' from vertical axis
        if (("air_pressure" not in self.data) and
                units(self.driver.vert_units).check("[pressure]")):
            self.data_units["air_pressure"] = "Pa"
            self.data["air_pressure"] = convert_to(
                self.driver.vert_data[::-self.driver.vert_order, np.newaxis],
                self.driver.vert_units, self.data_units["air_pressure"]).repeat(
                    len(self.lats), axis=1)
        if (("air_potential_temperature" not in self.data) and
                units(self.driver.vert_units).check("[temperature]")):
            self.data_units["air_potential_temperature"] = "K"
            self.data["air_potential_temperature"] = convert_to(
                self.driver.vert_data[::-self.driver.vert_order, np.newaxis],
                self.driver.vert_units, self.data_units["air_potential_temperature"]).repeat(
                    len(self.lats), axis=1)

        # Derive additional data fields and make the plot.
        self._prepare_datafields()
        if "air_pressure" not in self.data:
            raise KeyError(
                "'air_pressure' need to be available for VSEC plots."
                "Either provide as data or compute in _prepare_datafields")

        # Code for producing a png image with Matplotlib.
        # ===============================================
        if return_format == "image/png":

            logging.debug("creating figure..")
            dpi = 80
            figsize = (figsize[0] / dpi), (figsize[1] / dpi)
            facecolor = "white"
            self.fig = mpl.figure.Figure(figsize=figsize, dpi=dpi, facecolor=facecolor)
            logging.debug("\twith frame and legends" if not noframe else
                          "\twithout frame")
            if noframe:
                self.ax = self.fig.add_axes([0.0, 0.0, 1.0, 1.0])
            else:
                self.ax = self.fig.add_axes([0.07, 0.17, 0.9, 0.72])

            # prepare horizontal axis
            self.horizontal_coordinate = self.lat_inds[np.newaxis, :].repeat(
                self.data["air_pressure"].shape[0], axis=0)

            self._plot_style()

            # Set transparency for the output image.
            if transparent:
                self.fig.patch.set_alpha(0.)

            # Return the image as png embedded in a StringIO stream.
            canvas = FigureCanvas(self.fig)
            output = io.BytesIO()
            canvas.print_png(output)

            if show:
                logging.debug("saving figure to mpl_vsec.png ..")
                canvas.print_png("mpl_vsec.png")

            # Convert the image to an 8bit palette image with a significantly
            # smaller file size (~factor 4, from RGBA to one 8bit value, plus the
            # space to store the palette colours).
            # NOTE: PIL at the current time can only create an adaptive palette for
            # RGB images, hence alpha values are lost here. If transparency is
            # requested, the figure face colour is stored as the "transparent"
            # colour in the image. This works in most cases, but might lead to
            # visible artefacts in some cases.
            logging.debug("converting image to indexed palette.")
            # Read the above stored png into a PIL image and create an adaptive
            # colour palette.
            output.seek(0)  # necessary for PIL.Image.open()
            palette_img = PIL.Image.open(output).convert(
                mode="RGB").convert("P", palette=PIL.Image.ADAPTIVE)
            output = io.BytesIO()
            if not transparent:
                logging.debug("saving figure as non-transparent PNG.")
                palette_img.save(output, format="PNG")  # using optimize=True doesn't change much
            else:
                # If the image has a transparent background, we need to find the
                # index of the background colour in the palette. See the
                # documentation for PIL's ImagePalette module
                # (http://www.pythonware.com/library/pil/handbook/imagepalette.htm). The
                # idea is to create a 256 pixel image with the same colour palette
                # as the original image and use it as a lookup-table. Converting the
                # lut image back to RGB gives us a list of all colours in the
                # palette. (Why doesn't PIL provide a method to directly access the
                # colours in a palette??)
                lut = palette_img.resize((256, 1))
                lut.putdata(list(range(256)))
                lut = [c[1] for c in lut.convert("RGB").getcolors()]
                facecolor_rgb = list(mpl.colors.hex2color(mpl.colors.cnames[facecolor]))
                for i in [0, 1, 2]:
                    facecolor_rgb[i] = int(facecolor_rgb[i] * 255)
                facecolor_index = lut.index(tuple(facecolor_rgb))

                logging.debug("saving figure as transparent PNG with transparency index %i.",
                              facecolor_index)
                palette_img.save(output, format="PNG", transparency=facecolor_index)

            logging.debug("returning figure..")
            return output.getvalue()

        # Code for generating an XML document with the data values in ASCII format.
        # =========================================================================
        elif return_format == "text/xml":

            impl = getDOMImplementation()
            xmldoc = impl.createDocument(None, "MSS_VerticalSection_Data", None)

            # Title of this section.
            node = xmldoc.createElement("Title")
            node.appendChild(xmldoc.createTextNode(self.title))
            xmldoc.documentElement.appendChild(node)

            # Time information of this section.
            node = xmldoc.createElement("ValidTime")
            node.appendChild(xmldoc.createTextNode(self.valid_time.strftime("%Y-%m-%dT%H:%M:%SZ")))
            xmldoc.documentElement.appendChild(node)

            node = xmldoc.createElement("InitTime")
            node.appendChild(xmldoc.createTextNode(self.init_time.strftime("%Y-%m-%dT%H:%M:%SZ")))
            xmldoc.documentElement.appendChild(node)

            # Longitude data.
            node = xmldoc.createElement("Longitude")
            node.setAttribute("num_waypoints", f"{len(self.lons)}")

            data_str = ""
            for value in self.lons:
                data_str += str(value) + ","
            data_str = data_str[:-1]

            node.appendChild(xmldoc.createTextNode(data_str))
            xmldoc.documentElement.appendChild(node)

            # Latitude data.
            node = xmldoc.createElement("Latitude")
            node.setAttribute("num_waypoints", f"{len(self.lats)}")

            data_str = ""
            for value in self.lats:
                data_str += str(value) + ","
            data_str = data_str[:-1]

            node.appendChild(xmldoc.createTextNode(data_str))
            xmldoc.documentElement.appendChild(node)

            # Variable data.
            data_node = xmldoc.createElement("Data")

            for var in self.data:
                node = xmldoc.createElement(var)
                data_shape = self.data[var].shape
                node.setAttribute("num_levels", f"{data_shape[0]}")
                node.setAttribute("num_waypoints", f"{data_shape[1]}")

                data_str = ""
                for data_row in self.data[var]:
                    for value in data_row:
                        data_str += str(value) + ","
                    data_str = data_str[:-1] + "\n"
                data_str = data_str[:-1]

                node.appendChild(xmldoc.createTextNode(data_str))
                data_node.appendChild(node)

            xmldoc.documentElement.appendChild(data_node)

            # Return the XML document as formatted string.
            return xmldoc.toprettyxml(indent="  ")
Esempio n. 28
0
def imsave(fname,
           arr,
           vmin=None,
           vmax=None,
           cmap=None,
           format=None,
           origin=None,
           dpi=100):
    """
    Save an array as in image file.

    The output formats available depend on the backend being used.

    Arguments:
      *fname*:
        A string containing a path to a filename, or a Python file-like object.
        If *format* is *None* and *fname* is a string, the output
        format is deduced from the extension of the filename.
      *arr*:
        An MxN (luminance), MxNx3 (RGB) or MxNx4 (RGBA) array.
    Keyword arguments:
      *vmin*/*vmax*: [ None | scalar ]
        *vmin* and *vmax* set the color scaling for the image by fixing the
        values that map to the colormap color limits. If either *vmin*
        or *vmax* is None, that limit is determined from the *arr*
        min/max value.
      *cmap*:
        cmap is a colors.Colormap instance, e.g., cm.jet.
        If None, default to the rc image.cmap value.
      *format*:
        One of the file extensions supported by the active
        backend.  Most backends support png, pdf, ps, eps and svg.
      *origin*
        [ 'upper' | 'lower' ] Indicates where the [0,0] index of
        the array is in the upper left or lower left corner of
        the axes. Defaults to the rc image.origin value.
      *dpi*
        The DPI to store in the metadata of the file.  This does not affect the
        resolution of the output image.
    """
    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
    from matplotlib.figure import Figure

    # Fast path for saving to PNG
    if (format == 'png' or format is None
            or isinstance(fname, six.string_types)
            and fname.lower().endswith('.png')):
        image = AxesImage(None, cmap=cmap, origin=origin)
        image.set_data(arr)
        image.set_clim(vmin, vmax)
        image.write_png(fname)
    else:
        fig = Figure(dpi=dpi, frameon=False)
        FigureCanvas(fig)
        fig.figimage(arr,
                     cmap=cmap,
                     vmin=vmin,
                     vmax=vmax,
                     origin=origin,
                     resize=True)
        fig.savefig(fname, dpi=dpi, format=format, transparent=True)
Esempio n. 29
0
def generate_plot(data, size, data_range, user_selected=None):
    """
    Generates interactive plots of site data
    :param data:    Site data
    :param size:    Desired size of plot; 0 for small, 1 for large
    :param data_range   Selected timestamp range of data in integer form
    :param user_selected:   Text indicating user selected time range (for title of plot)
    :return:        Interactive mpld3 plot
    """

    # Load data
    x = []
    y = []
    mean_array = []
    for data in data:
        time_stamp = data.timestamp - datetime.timedelta(hours=7)
        x.append(time_stamp)
        y.append(float(data.load_time))
        if data.load_time != 0:
            mean_array.append(float(data.load_time))

    # Average of data
    avg = mean(mean_array)
    avg = float(round(avg, 3))
    avg_text = "Average: " + str(avg)

    # Clear existing figures and create current plot
    for fig in figures:
        fig.clf()
    mpl_figure = figures[size]

    canvas = FigureCanvas(mpl_figure)
    ax = mpl_figure.add_subplot(111)

    title = "Page Load Times: "

    # Give title based on the selected data range
    if data_range == -1:
        title += user_selected

    elif data_range == 1:
        title += "Last 5 minutes"

    elif data_range == 2:
        title += "Last 30 minutes"

    elif data_range == 3:
        title += "Last hour"

    elif data_range == 4:
        title += "Last day"

    elif data_range == 5:
        title += "All time"

    ax.plot(x, y)
    ax.set_title(title, size=18)
    ax.set_xlabel('Time', size=15)
    ax.set_ylabel('Load Time (s)', size=15)
    ax.annotate(avg_text, xy=(0.65, 0.92), xycoords='axes fraction', size=16)

    mpld3_plot = mpld3.fig_to_html(mpl_figure)

    #plt.clf()

    #plt.close()

    return mpld3_plot
Esempio n. 30
0
def pretty_vis(image,
               annList,
               show_class=False,
               alpha=0.0,
               dpi=100,
               **options):
    import cv2
    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
    from matplotlib.patches import Polygon
    from matplotlib.figure import Figure
    from . import ann_utils as au
    # print(image)
    # if not image.as > 1:
    #     image = image.astype(float)/255.
    image = f2l(image).squeeze().clip(0, 255)
    if image.max() > 1:
        image /= 255.

    # box_alpha = 0.5
    # print(image.clip(0, 255).max())
    color_list = colormap(rgb=True) / 255.

    # fig = Figure()
    fig = plt.figure(frameon=False)
    canvas = FigureCanvas(fig)
    fig.set_size_inches(image.shape[1] / dpi, image.shape[0] / dpi)
    # ax = fig.gca()

    ax = plt.Axes(fig, [0., 0., 1., 1.])
    ax.axis('off')
    fig.add_axes(ax)
    # im = im.clip(0, 1)
    # print(image)
    ax.imshow(image)

    # Display in largest to smallest order to reduce occlusion
    # areas = (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1])
    # sorted_inds = np.argsort(-areas)

    mask_color_id = 0
    for i in range(len(annList)):
        ann = annList[i]

        # bbox = boxes[i, :4]
        # score = boxes[i, -1]

        # bbox = au.ann2bbox(ann)["shape"]
        # score = ann["score"]

        # if score < thresh:
        #     continue

        # show box (off by default, box_alpha=0.0)
        if "bbox" in ann:
            bbox = ann["bbox"]
            ax.add_patch(
                plt.Rectangle((bbox[0], bbox[1]),
                              bbox[2],
                              bbox[3],
                              fill=False,
                              edgecolor='r',
                              linewidth=3.0,
                              alpha=0.5))

        # if show_class:
        # if options.get("show_text") == True or options.get("show_text") is None:
        #     score = ann["score"] or -1
        #     ax.text(
        #         bbox[0], bbox[1] - 2,
        #         "%.1f" % score,
        #         fontsize=14,
        #         family='serif',
        #         bbox=dict(facecolor='g', alpha=1.0, pad=0, edgecolor='none'),
        #         color='white')

        # show mask
        if "segmentation" in ann:
            mask = au.ann2mask(ann)["mask"]
            img = np.ones(image.shape)
            # category_id = ann["category_id"]
            # mask_color_id = category_id - 1
            # color_list = ["r", "g", "b","y", "w","orange","purple"]
            # color_mask = color_list[mask_color_id % len(color_list)]
            color_mask = color_list[mask_color_id % len(color_list), 0:3]
            mask_color_id += 1
            # print("color id: %d - category_id: %d - color mask: %s"
            # %(mask_color_id, category_id, str(color_mask)))
            w_ratio = .4
            for c in range(3):
                color_mask[c] = color_mask[c] * (1 - w_ratio) + w_ratio
            for c in range(3):
                img[:, :, c] = color_mask[c]
            e = mask

            contour, hier = cv2.findContours(e.copy(), cv2.RETR_CCOMP,
                                             cv2.CHAIN_APPROX_NONE)

            for c in contour:
                polygon = Polygon(c.reshape((-1, 2)),
                                  fill=True,
                                  facecolor=color_mask,
                                  edgecolor="white",
                                  linewidth=1.5,
                                  alpha=0.7)
                ax.add_patch(polygon)

    canvas.draw()  # draw the canvas, cache the renderer
    width, height = fig.get_size_inches() * fig.get_dpi()
    # image = np.fromstring(canvas.tostring_rgb(), dtype='uint8')

    fig_image = np.fromstring(canvas.tostring_rgb(),
                              dtype='uint8').reshape(int(height), int(width),
                                                     3)
    plt.close()
    # print(fig_image)
    return fig_image
Esempio n. 31
0
def render_animation_test(keypoints,
                          poses,
                          skeleton,
                          fps,
                          bitrate,
                          azim,
                          output,
                          viewport,
                          limit=-1,
                          downsample=1,
                          size=6,
                          input_video_frame=None,
                          input_video_skip=0,
                          num=None):
    t0 = ckpt_time()
    fig = plt.figure(figsize=(12, 6))
    canvas = FigureCanvas(fig)
    fig.add_subplot(121)
    plt.imshow(input_video_frame)
    # 3D
    ax = fig.add_subplot(122, projection='3d')
    ax.view_init(elev=15., azim=azim)
    # set 长度范围
    radius = 1.7
    ax.set_xlim3d([-radius / 2, radius / 2])
    ax.set_zlim3d([0, radius])
    ax.set_ylim3d([-radius / 2, radius / 2])
    ax.set_aspect('equal')
    # 坐标轴刻度
    ax.set_xticklabels([])
    ax.set_yticklabels([])
    ax.set_zticklabels([])
    ax.dist = 7.5

    # lxy add
    ax.set_xlabel('X Label')
    ax.set_ylabel('Y Label')
    ax.set_zlabel('Z Label')

    # array([-1,  0,  1,  2,  0,  4,  5,  0,  7,  8,  9,  8, 11, 12,  8, 14, 15])
    parents = skeleton.parents()
    pos = poses['Reconstruction'][-1]
    _, t1 = ckpt_time(t0, desc='1 ')
    for j, j_parent in enumerate(parents):
        if j_parent == -1:
            continue

        if len(parents) == keypoints.shape[1]:
            color_pink = 'pink'
            if j == 1 or j == 2:
                color_pink = 'black'

        col = 'red' if j in skeleton.joints_right() else 'black'
        # 画图3D
        ax.plot([pos[j, 0], pos[j_parent, 0]], [pos[j, 1], pos[j_parent, 1]],
                [pos[j, 2], pos[j_parent, 2]],
                zdir='z',
                c=col)

    #  plt.savefig('test/3Dimage_{}.png'.format(1000+num))
    width, height = fig.get_size_inches() * fig.get_dpi()
    _, t2 = ckpt_time(t1, desc='2 ')
    canvas.draw()  # draw the canvas, cache the renderer
    image = np.fromstring(canvas.tostring_rgb(),
                          dtype='uint8').reshape(int(height), int(width), 3)
    cv2.imshow('im', image)
    cv2.waitKey(5)
    _, t3 = ckpt_time(t2, desc='3 ')
    return image
Esempio n. 32
0
def hotspot():
    fig = barPlotHotspots()
    output = io.BytesIO()
    FigureCanvas(fig).print_png(output)
    return Response(output.getvalue(), mimetype='image/png')
Esempio n. 33
0
def plot_ideal_mean_age_accuracy(plot_path):
    colors = Dark2_6.mpl_colors
    print colors

    real_experiments = ['m3', 'm4', 'm5', 'm6']
    real_labels = [r'\#3', r'\#4', r'\#5', r'\#6']
    real_colors = colors[:4]

    ideal_experiment = 'idealall'
    ideal_planes = ['oir_all', 'oir_all_28']
    ideal_labels = ['Errorless OIR-ALL',
                    'Errorless OIR-ALL-28']
    ideal_colors = colors[4:]
    print ideal_colors

    fig = Figure(figsize=(6.5, 3.5), frameon=False)
    canvas = FigureCanvas(fig)
    gs = gridspec.GridSpec(1, 1,
                           left=0.1, right=0.95, bottom=0.15, top=0.95,
                           wspace=0.1, hspace=None,
                           width_ratios=None, height_ratios=None)
    ax = fig.add_subplot(gs[0])

    itr = zip(real_experiments, real_labels, real_colors)
    for experiment, label, color in itr:
        t = ssp_mean_age_table(experiment=experiment)
        age_diff = t['{0}_mean_age'.format('oir_all')] - t['mock_age']
        # age_sigma = t['{0}_mean_age_sigma'.format('oir_all')]
        print experiment, color
        ax.plot(t['mock_age'],
                age_diff,
                c=color, label=label, lw=3)
                # dashes=(5, 7.5),
                # dash_joinstyle='round', dash_capstyle='round')
        # ax.plot(t['mock_age'],
        #         -age_sigma,
        #         c=color, lw=0.5, ls='--')
        # ax.plot(t['mock_age'],
        #         age_sigma,
        #         c=color, lw=0.5, ls='--')

    for plane, label, color in zip(ideal_planes, ideal_labels, ideal_colors):
        t = ssp_mean_age_table(experiment=ideal_experiment,
                               plane_keys=ideal_planes)
        age_diff = t['{0}_mean_age'.format(plane)] - t['mock_age']
        # age_sigma = t['{0}_mean_age_sigma'.format(plane)]
        print label, color
        ax.plot(t['mock_age'],
                age_diff,
                c=color, label=label, lw=3,
                dashes=(5, 7.5),
                dash_joinstyle='round', dash_capstyle='round')

    ax.set_xlabel(r'$\langle A \rangle_\mathrm{mock}$ (Gyr)')
    ax.set_ylim(-10., 10.)

    ax.yaxis.set_minor_locator(mpl.ticker.MultipleLocator(base=1))

    ax.set_ylabel(
        r'$\langle A \rangle_\mathrm{fit} - \langle A \rangle_\mathrm{mock}$ (Gyr)')  # NOQA
    ax.legend(loc='lower left', fontsize=7, handlelength=5)
    gs.tight_layout(fig, pad=1.08, h_pad=None, w_pad=None, rect=None)
    canvas.print_figure(plot_path + ".pdf", format="pdf")
Esempio n. 34
0
def plot_nucleotide_diversity_measures(variations, max_num_alleles,
                                       window_size, data_dir,
                                       chunk_size=SNPS_PER_CHUNK,
                                       write_bg=False,
                                       min_num_genotypes=MIN_NUM_GENOTYPES_FOR_POP_STAT):
    fig = Figure(figsize=(20, 20))
    canvas = FigureCanvas(fig)
    marker = 'k'
    chrom = _load_matrix(variations, CHROM_FIELD)
    pos = _load_matrix(variations, POS_FIELD)

    # Number of variable positions per bp
    snp_density = PositionalStatsCalculator(chrom, pos,
                                            numpy.ones(pos.shape),
                                            window_size=window_size,
                                            step=window_size)
    snp_density = snp_density.calc_window_stat()
    bg_fhand = open(join(data_dir, 'diversity_s.bg'), 'w')
    if write_bg:
        snp_density.write(bg_fhand, 's',
                          'SNP density in windows of {} bp'.format(window_size),
                          track_type='bedgraph')
    axes = fig.add_subplot(311)
    title = 'Nucleotide diversity measures averaged in windows of {} bp'
    title = title.format(window_size)
    mpl_params = {'set_title': {'args': [title], 'kwargs': {}},
                  'set_ylabel': {'args': ['SNPs number / bp'], 'kwargs': {}},
                  'set_ylim': {'args': [0, 1.2*numpy.max(snp_density.stat)],
                               'kwargs': {}}}
    manhattan_plot(snp_density.chrom, snp_density.pos, snp_density.stat,
                   mpl_params=mpl_params, axes=axes, ylim=0, show_chroms=False,
                   marker=marker)

    # Watterson estimator of nucleotide diversity
    n_seqs = variations[GT_FIELD].shape[1] * variations[GT_FIELD].shape[2]
    correction_factor = numpy.sum(1 / numpy.arange(1, n_seqs))
    watterson = snp_density
    watterson.stat = watterson.stat / correction_factor
    bg_fhand = open(join(data_dir, 'diversity_s.bg'), 'w')
    description = 'SNP density in windows of {} bp'.format(window_size)
    if write_bg:
        watterson.write(bg_fhand, 's', description, track_type='bedgraph')
    axes = fig.add_subplot(312)
    mpl_params={'set_ylabel': {'args': ['Watterson estimator'], 'kwargs': {}},
                'set_ylim': {'args': [0, 1.2*numpy.max(watterson.stat)],
                             'kwargs': {}}}
    manhattan_plot(watterson.chrom, watterson.pos, watterson.stat,
                   mpl_params=mpl_params, axes=axes, ylim=0, show_chroms=False,
                   marker=marker)

    # Expected heterozygosity (Pi)
    exp_het = calc_expected_het(variations, chunk_size=chunk_size,
                                min_num_genotypes=min_num_genotypes)
    pi = PositionalStatsCalculator(chrom, pos, exp_het,
                                   window_size=window_size, step=window_size)
    pi = pi.calc_window_stat()
    bg_fhand = open(join(data_dir, 'diversity_pi.bg'), 'w')
    description = 'Pi in windows of {} bp'.format(window_size)
    if write_bg:
        pi.write(bg_fhand, 's', description, track_type='bedgraph')
    axes = fig.add_subplot(313)
    mpl_params={'set_xlabel': {'args': ['Chromosome'], 'kwargs': {}},
                'set_ylabel': {'args': ['Pi'], 'kwargs': {}},
                'set_ylim': {'args': [0, 1.2*numpy.max(pi.stat)],
                             'kwargs': {}}}
    manhattan_plot(pi.chrom, pi.pos, pi.stat, axes=axes, ylim=0, marker=marker,
                   mpl_params=mpl_params)
    canvas.print_figure(open(join(data_dir, 'nucleotide_diversity.png'), 'w'))
Esempio n. 35
0
    def _generate_and_save_gif(self,
                               top_images,
                               bottom_images,
                               size_fig,
                               is_mask=False):
        """
        Create figure with two images for sct_fmri_moco and sct_dmri_moco and save gif

        :param top_images: list of images of mosaic before motion correction
        :param bottom_images: list of images of mosaic after motion correction
        :param size_fig: size of figure in inches
        :param is_mask: display grid on top of mosaic
        :return:
        """

        if is_mask:
            aspect = self.aspect_mask
        else:
            aspect = self.aspect_img

        fig = Figure()
        FigureCanvas(fig)
        fig.set_size_inches(size_fig[0], size_fig[1], forward=True)
        fig.subplots_adjust(left=0, top=0.9, bottom=0.1)

        ax1 = fig.add_subplot(211)
        null_image = np.zeros(np.shape(top_images[0]))
        img1 = ax1.imshow(null_image, cmap='gray', aspect=float(aspect))
        ax1.set_title('Before motion correction',
                      fontsize=8,
                      loc='left',
                      pad=2)
        ax1.get_xaxis().set_visible(False)
        ax1.get_yaxis().set_visible(False)
        self._add_orientation_label(ax1)
        if is_mask:
            QcImage.grid(self, top_images[0], ax1)

        ax2 = fig.add_subplot(212)
        img2 = ax2.imshow(null_image, cmap='gray', aspect=float(aspect))
        ax2.set_title('After motion correction', fontsize=8, loc='left', pad=2)
        ax2.get_xaxis().set_visible(False)
        ax2.get_yaxis().set_visible(False)
        self._add_orientation_label(ax2)
        if is_mask:
            QcImage.grid(self, bottom_images[0], ax2)

        ann = ax2.annotate('',
                           xy=(0, .025),
                           xycoords='figure fraction',
                           horizontalalignment='left',
                           verticalalignment='bottom',
                           fontsize=6)

        def update_figure(i):
            img1.set_data(top_images[i])
            img1.set_clim(vmin=np.amin(top_images[i]),
                          vmax=np.amax(top_images[i]))
            img2.set_data(bottom_images[i])
            img2.set_clim(vmin=np.amin(bottom_images[i]),
                          vmax=np.amax(bottom_images[i]))
            ann.set_text(f'Volume: {i + 1}/{len(top_images)}')

        # FuncAnimation creates an animation by repeatedly calling the function update_figure for each frame
        ani = FuncAnimation(fig, update_figure, frames=len(top_images))

        if is_mask:
            gif_out_path = self.qc_report.qc_params.abs_overlay_img_path()
        else:
            gif_out_path = self.qc_report.qc_params.abs_bkg_img_path()

        if self._fps is None:
            self._fps = 3
        writer = PillowWriter(self._fps)
        logger.info('Saving gif %s', gif_out_path)
        ani.save(gif_out_path, writer=writer, dpi=self.qc_report.qc_params.dpi)