def plot_data_mlp_to_html(data_points, labels=[]):
    # print(len(dataPoints))
    data_points = cull_to_number(data_points, 250)
    # print(len(dataPoints))
    # print(dataPoints)
    swap = swapaxes(data_points, 0, 1)
    # print(swap)
    timescale = []
    for i, date in enumerate(swap[0]):
        parsedDate = datetime.datetime.strptime(date, '%m/%d/%y %H:%M:%S')
        timescale.append(parsedDate)
    # timescale = [datetime.datetime.strptime(date, '%m/%d/%y %H:%M:%S') for date in swap[0]]
    dates = matplotlib.dates.date2num(timescale)
    fig, ax = plt.subplots(figsize=[9.28, 4.8])
    plt.rcParams.update({'figure.autolayout': True})
    ax.yaxis.set_major_locator(matplotlib.ticker.MultipleLocator(10))
    ax.grid(which='major', axis='both', linestyle="-", alpha=0.25, linewidth=1)
    ax.tick_params(labelsize='medium', width=1)
    # Read labels from list
    p1 = ax.plot_date(dates,
                      swap[1],
                      color='red',
                      linestyle='solid',
                      marker=None,
                      label=labels[0])
    p2 = ax.plot_date(dates,
                      swap[2],
                      color='green',
                      linestyle='solid',
                      marker=None,
                      label=labels[1])
    p3 = ax.plot_date(dates,
                      swap[3],
                      color='blue',
                      linestyle='solid',
                      marker=None,
                      label=labels[2])
    s1 = ax.scatter(dates, swap[1], color='#00000000', s=50)
    s2 = ax.scatter(dates, swap[2], color='#00000000', s=50)
    s3 = ax.scatter(dates, swap[3], color='#00000000', s=50)
    ax.legend()
    # xlabels = ax.get_xticklabels()
    # plt.setp(xlabels, rotation=30, horizontalalignment='right')
    ax.set(ylim=[0, 100],
           xlabel='Dată',
           ylabel='Procent (%)',
           title='Grafic umiditate plante')

    # Create HTML
    plugins.clear(fig)
    tooltip_plugin1 = plugins.PointLabelTooltip(
        s1, extract_point_position(swap[0], swap[1]))
    tooltip_plugin2 = plugins.PointLabelTooltip(
        s2, extract_point_position(swap[0], swap[2]))
    tooltip_plugin3 = plugins.PointLabelTooltip(
        s3, extract_point_position(swap[0], swap[3]))
    plugins.connect(fig, tooltip_plugin1, tooltip_plugin2, tooltip_plugin3,
                    plugins.Zoom(), plugins.Reset())
    html_str = fig_to_html(fig)
    return html_str
Beispiel #2
0
def mpld3_enable_notebook():
    """Change the default plugins, enable ipython notebook mode and return mpld3 module."""
    import mpld3
    from mpld3 import plugins as plugs
    plugs.DEFAULT_PLUGINS = [plugs.Reset(), plugs.Zoom(), plugs.BoxZoom(), plugs.MousePosition()]
    mpld3.enable_notebook()
    return mpld3
Beispiel #3
0
def create_plot():
    fig, ax = plt.subplots()
    points = ax.scatter(random_x, random_y, s=500, alpha=0.3)

    plugins.clear(fig)
    ax.set_title('Click info', size=14)
    plugins.connect(fig, plugins.Reset(), plugins.Zoom(), ClickInfo(points))
    return fig
Beispiel #4
0
def create_plot():
    fig, ax = plt.subplots()
    points = ax.scatter(np.random.rand(50), np.random.rand(50),
                        s=500, alpha=0.3)

    plugins.clear(fig)
    plugins.connect(fig, plugins.Reset(), plugins.Zoom(), ClickInfo(points))
    return fig
Beispiel #5
0
def plot_vdot_race(data):
    #CSS element for the tooltip label
    css = """
div
{
  font-family: Avenir, Helvetica, sans-serif;
  border: 1px solid black;
  padding-left: 5px;
  padding-right: 5px;
  text-align: center;
  color: #000000;
  background-color: #ffffff;
}
"""
    #plt.style.use('seaborn-poster') #sets the size of the charts
    plt.style.use('ggplot')
    x_axis = []
    y_axis = []

    label = []
    #print (data)
    for key in data:
        label.append("Race: " + str(data[key][1]) + "<br/>" + "VDOT: " +
                     str(data[key][3]))

        y_axis.append(data[key][3])
        datetime_obj = datetime.strptime(data[key][4],
                                         '%Y-%m-%d %H:%M:%S+00:00')
        x_axis.append(datetime_obj)

    dates = matplotlib.dates.date2num(x_axis)

    fig, ax = plt.subplots()

    plt.xlabel("Race dates", fontsize=20)
    plt.ylabel("VDOT Scores", fontsize=20)
    plt.title("VDOT Score Over Time", fontsize=25)

    line = plt.plot_date(dates,
                         y_axis,
                         linestyle='solid',
                         marker='.',
                         markersize=14)

    plugins.clear(fig)  # clear all plugins from the figure

    tooltip = plugins.PointHTMLTooltip(line[0],
                                       label,
                                       hoffset=-60,
                                       voffset=-70,
                                       css=css)

    plugins.connect(fig, plugins.Reset(), plugins.BoxZoom(), plugins.Zoom(),
                    tooltip)

    return mpld3.fig_to_html(fig)
Beispiel #6
0
def make_html_plot_for_file(filename, my_title):
    my_fontsize = 20

    # read in csv file
    if os.stat(filename).st_size:
        data = np.genfromtxt(filename, delimiter=',')
    else:
        # empty data file
        data = np.zeros([2, 2])

    if len(data.shape) == 1:
        return "<h2> " + my_title + " has no data </h2>"

    x = data[:, 0]
    fig, ax = plt.subplots(figsize=(10, 5))

    for k in range(1, data.shape[1]):
        y = data[:, k]

        # sort data
        x_sorted = [x1 for (x1, y1) in sorted(zip(x, y))]
        y_sorted = [y1 for (x1, y1) in sorted(zip(x, y))]

        lines = ax.plot(x_sorted,
                        y_sorted,
                        '.-',
                        markersize=15,
                        label='ion ' + str(k - 1))

    plt.legend(fontsize=my_fontsize, loc='best')
    plt.title(my_title, fontsize=my_fontsize)
    ax.grid()
    plt.xticks(fontsize=my_fontsize)
    plt.yticks(fontsize=my_fontsize)
    plt.xlim([np.min(x), np.max(x)])

    #90% of the time that this function takes is taken up after this line
    plugins.clear(fig)
    plugins.connect(fig, plugins.Reset(), plugins.BoxZoom(),
                    plugins.Zoom(enabled=True),
                    plugins.MousePosition(fontsize=my_fontsize))
    java_txt = mpld3.fig_to_html(fig)
    plt.close()

    return java_txt
Beispiel #7
0
def spladder_viz(options):
    """Main visualization code"""

    ### parse parameters from options object
    options = settings.parse_args(options, identity='viz')

    ### create plot directory if it does not exist yet
    dirname = options.outdir if options.testdir == '-' else options.testdir
    if not os.path.exists(os.path.join(dirname, 'plots')):
        os.mkdir(os.path.join(dirname, 'plots'))

    if options.format == 'd3':
        try:
            import mpld3
            from mpld3 import plugins
        except ImportError:
            sys.stderr.write(
                "ERROR: missing package for output format d3. Package mpld3 required"
            )
            sys.exit(1)

    ### load gene information
    all_gene_names = get_gene_names(options.outdir, options.confidence,
                                    options.validate_sg, options.verbose)

    ### set color maps
    cmap_cov = plt.get_cmap('jet')
    cmap_edg = plt.get_cmap('jet')

    ### Data Range
    RangeData = namedtuple('RangeData', ['chr', 'start', 'stop'])

    plots = []
    test_tag = []
    ### if the --test option was given, populate the options object with additional track fields
    ### otherwise just create a single plot
    if options.test is None:
        plots.append(options.data_tracks)
        test_tag.append('')
    else:
        for test_info in options.test:
            _parse_test_info(test_info, options.outdir, options.confidence,
                             plots, test_tag)
        for plot_data in plots:
            if len(options.data_tracks) > 0:
                plot_data.extend(options.data_tracks)

    ### generate all plots to be completed
    for p, plot_data in enumerate(plots):

        ### get range information
        genes, gene_names, gids = [], [], []
        events, eids = [], []
        coords = []
        for range_info in options.range:
            ### genes
            if range_info[0] == 'gene':
                _parse_gene_info(range_info[1:], genes, gene_names, gids,
                                 all_gene_names, options.outdir,
                                 options.confidence, options.validate_sg,
                                 options.verbose)
            ### events
            elif range_info[0] == 'event':
                _parse_event_info(eids, gids, range_info[1:], events,
                                  options.outdir, options.confidence,
                                  options.verbose)
            ### coordinate ranges
            elif range_info[0] == 'coordinate':
                coords.append(
                    RangeData._make([
                        range_info[1],
                        int(range_info[2]),
                        int(range_info[3])
                    ]))

        ### check data tracks for additional range information if no --range was given
        if len(genes) + len(events) + len(coords) == 0:
            for range_info in plot_data:
                ### splicegraph / transcripts
                if range_info[0] in ['splicegraph', 'transcript']:
                    _parse_gene_info(range_info[1:], genes, gene_names, gids,
                                     all_gene_names, options.outdir,
                                     options.confidence, options.validate_sg,
                                     options.verbose)
                ### events
                elif range_info[0] == 'event':
                    _parse_event_info(eids, gids, range_info[1:], events,
                                      options.outdir, options.confidence,
                                      options.verbose)
                ### gene (this is only needed internally for --test mode)
                elif range_info[0] == 'gene':
                    _parse_gene_info(range_info[1:], genes, gene_names, gids,
                                     all_gene_names, options.outdir,
                                     options.confidence, options.validate_sg,
                                     options.verbose)

        ### check that everything is on the same chromosome
        plotchrm = np.unique([_.chr for _ in events + genes + coords])
        if plotchrm.shape[0] > 1:
            sys.stderr.write(
                'ERROR: the provided gene/event/coordinate ranges are on different chromosomes and canot be plotted jointly\n'
            )
            sys.exit(1)

        ### identify the plotting range
        plotrange, plotrange_orig = _set_plotting_range(genes, events, coords)

        ### parse all elements to be plotted as data tracks
        data_tracks = []
        for data_element in plot_data:
            track_types = data_element[0].split(',')
            for track_type in track_types:
                data_tracks.append(dt.DataTrack(track_type, data_element[1:]))

        ### set up figure and layout
        gs = gridspec.GridSpec(len(data_tracks), 1, hspace=0.05)
        fig = plt.figure(figsize=(15, 3 * len(data_tracks)), dpi=200)

        axes = []
        for i, data_track in enumerate(data_tracks):

            ### plot splicing graph
            if data_track.type == 'splicegraph':
                ax = _add_ax(axes, fig, gs)
                if len(data_track.event_info) == 0:
                    _genes = genes
                    _gene_names = gene_names
                else:
                    _genes, _gene_names, _gids = [], [], []
                    _parse_gene_info(range_info[1:], _genes, _gene_names,
                                     _gids, all_gene_names, options.outdir,
                                     options.confidence, options.validate_sg,
                                     options.verbose)
                for gene in _genes:
                    gene.from_sparse()
                    plot_graph(gene.splicegraph.vertices,
                               gene.splicegraph.edges,
                               ax,
                               xlim=plotrange,
                               label=gene.name)
                    gene.to_sparse()
                ax.set_ylabel('splicing graph')
                ax.get_yaxis().set_label_coords(1.02, 0.5)
            ### plot annotated transcripts
            if data_track.type == 'transcript':
                ax = _add_ax(axes, fig, gs)
                if len(data_track.event_info) == 0:
                    _genes = genes
                    _gene_names = gene_names
                else:
                    _genes, _gene_names, _gids = [], [], []
                    _parse_gene_info(range_info[1:], _genes, _gene_names,
                                     _gids, all_gene_names, options.outdir,
                                     options.confidence, options.validate_sg,
                                     options.verbose)
                for gene in _genes:
                    gene.from_sparse()
                    multiple(gene.exons,
                             ax=ax,
                             x_range=plotrange,
                             labels=gene.transcripts,
                             grid=True)
                    gene.to_sparse()
                ax.set_ylabel('transcripts')
                ax.get_yaxis().set_label_coords(1.02, 0.5)
            ### plot events
            if data_track.type == 'event':
                ax = _add_ax(axes, fig, gs)
                ### no event ids given - plot the one from range
                if len(data_track.event_info) == 0:
                    _events = events
                ### events are given in the track - plot those instead
                else:
                    _eids, _events = [], []
                    _parse_event_info(_eids, gids, data_track.event_info,
                                      _events, options.outdir,
                                      options.confidence, options.verbose)
                event_list = [[event.exons1, event.exons2]
                              for event in _events]
                labels = ['_'.join([_.event_type, str(_.id)]) for _ in _events]
                multiple(event_list,
                         ax=ax,
                         x_range=plotrange,
                         color='green',
                         padding=None,
                         grid=True,
                         labels=labels)
                vax.clean_axis(ax, allx=True)
                ax.set_ylabel('events')
                ax.get_yaxis().set_label_coords(1.02, 0.5)
            ### plot coverage tracks
            if data_track.type == 'coverage':
                ax = _add_ax(axes, fig, gs)
                min_sample_size = 5  ### TODO make that an option
                caxes = []
                labels = []
                norm = plt.Normalize(0, len(data_track.bam_fnames))
                for g, bam_group in enumerate(data_track.bam_fnames):
                    label = 'group %i' % (g + 1) if len(
                        data_track.group_labels
                    ) == 0 else data_track.group_labels[g]
                    caxes.append(
                        cov_from_bam(plotchrm[0],
                                     plotrange[0],
                                     plotrange[1],
                                     bam_group,
                                     subsample=min_sample_size,
                                     ax=ax,
                                     intron_cnt=True,
                                     log=options.log,
                                     xlim=plotrange,
                                     color_cov=cmap_cov(norm(g)),
                                     color_intron_edge=cmap_edg(norm(g)),
                                     grid=True,
                                     min_intron_cnt=options.mincount,
                                     return_legend_handle=True,
                                     label=label))
                    labels.append(label)
                ax.get_yaxis().set_label_coords(1.02, 0.5)
                if len(caxes) > 0:
                    ax.legend(caxes, labels)
            ### plot segment counts
            if data_track.type == 'segments':
                ax = _add_ax(axes, fig, gs)
                for g, gid in enumerate(gids):
                    (segments, edges, edge_idx,
                     strains) = get_seg_counts(gid, options.outdir,
                                               options.confidence,
                                               options.validate_sg)
                    seg_sample_idx = None
                    if len(data_track.strains) > 0:
                        seg_sample_idx = []
                        for track_strains in data_track.strains:
                            seg_sample_idx.append(
                                np.where(np.in1d(strains, track_strains))[0])
                            cov_from_segments(genes[g],
                                              segments,
                                              edges,
                                              edge_idx,
                                              ax,
                                              xlim=plotrange,
                                              log=options.log,
                                              grid=True,
                                              order='C',
                                              sample_idx=seg_sample_idx)
                ax.get_yaxis().set_label_coords(1.02, 0.5)
                ax.set_ylabel('segment counts')

        ### set x axis ticks
        for i, ax in enumerate(axes):
            if i == len(axes) - 1:
                ax.set_xticks(
                    np.around(
                        np.linspace(plotrange_orig[0], plotrange_orig[1], 10)))
                ax.set_xlabel('chromosome ' + plotchrm[0])
            else:
                ax.set_xticks([])

        ### save plot into file
        if options.format == 'd3':
            out_fname = os.path.join(
                dirname, 'plots', '%s%s.html' % (options.outbase, test_tag[p]))
            plugins.clear(fig)
            plugins.connect(fig, plugins.Zoom(enabled=True))
            mpld3.save_html(fig, open(out_fname, 'w'))
        else:
            out_fname = os.path.join(
                dirname, 'plots',
                '%s%s.%s' % (options.outbase, test_tag[p], options.format))
            plt.savefig(out_fname, format=options.format, bbox_inches='tight')
        plt.close(fig)
Beispiel #8
0
def spladder_viz():
    """Main visualization code"""

    ### parse command line parameters
    options = parse_options(sys.argv)

    ### create plot directory if it does not exist yet
    if not os.path.exists(os.path.join(options.outdir, 'plots')):
        os.mkdir(os.path.join(options.outdir, 'plots'))

    if options.format == 'd3':
        try:
            import mpld3
            from mpld3 import plugins
        except ImportError:
            sys.stderr.write(
                "ERROR: missing package for format d3. Package mpld3 required")
            sys.exit(1)

    ### load gene information
    genes = load_genes(options)

    rows = get_plot_len(options)
    gs = gridspec.GridSpec(rows, 1)

    ### get coloring
    cmap_cov = plt.get_cmap('jet')
    cmap_edg = plt.get_cmap('jet')

    ### plot log scale?
    log_tag = ''
    if options.log:
        log_tag = '.log'
    event_tag = ''

    ### did we get any labels?
    if options.labels != '-':
        options.labels = options.labels.strip(',').split(',')
        assert len(options.labels) == len(
            options.bams.strip(':').split(':')
        ), "The number of given labels (%i) needs to match the number of given bam file groups (%i)" % (
            len(options.labels), len(options.bams.strip(':').split(':')))

    ### the user specified a gene to plot
    if options.gene_name is not None:
        gid = sp.where(
            sp.array([x.name.split('.')[0]
                      for x in genes]) == options.gene_name.split('.')[0])[0]
        if gid.shape[0] == 0:
            sys.stderr.write(
                'ERROR: provided gene ID %s could not be found, please check for correctness\n'
                % options.gene_name)
            sys.exit(1)
        gids = [
            sp.where(sp.array([x.name
                               for x in genes]) == options.gene_name)[0][0]
        ]
    ### no gene specified but result provided - plot all genes with confirmed events
    ### if an event_id is provided, only the associated gene will be plotted
    else:
        gids = get_gene_ids(options)

    ### iterate over genes to plot
    for gid in gids:
        if options.format == 'd3':
            fig = plt.figure(figsize=(12, 2 * rows), dpi=100)
        else:
            fig = plt.figure(figsize=(18, 3 * rows), dpi=200)
        axes = []

        ### gather information about the gene we plot
        gene = get_gene(genes[gid])
        if options.verbose:
            print 'plotting information for gene %s' % gene.name

        ### plot splicing graph
        axes.append(fig.add_subplot(gs[len(axes), 0]))
        plot_graph(gene.splicegraph.vertices, gene.splicegraph.edges, axes[-1])
        xlim = axes[-1].get_xlim()
        axes[-1].set_title('Splicing graph for %s' % gene.name)

        ### plot annotated transcripts
        if options.transcripts:
            axes.append(fig.add_subplot(gs[len(axes), 0], sharex=axes[0]))
            multiple(gene.exons, ax=axes[-1], x_range=xlim)
            axes[-1].set_title('Annotated Transcripts')

        ### plot coverage information for a set of samples
        if options.bams != '-':
            samples = options.bams.strip(':').split(':')
            plot_bam(options, gene, samples, fig, axes, gs, xlim, cmap_cov,
                     cmap_edg)

            ### plot all the samples in a single plot
            if len(samples) > 1:
                plot_bam(options,
                         gene,
                         samples,
                         fig,
                         axes,
                         gs,
                         xlim,
                         cmap_cov,
                         cmap_edg,
                         single=False)

        ### plot segment counts
        (segments, edges, edge_idx) = get_seg_counts(options, gid)
        axes.append(fig.add_subplot(gs[len(axes), 0], sharex=axes[0]))
        if identity() == 'matlab':
            cov_from_segments(gene,
                              segments,
                              edges,
                              edge_idx,
                              axes[-1],
                              xlim=xlim,
                              log=options.log,
                              grid=True,
                              order='F')
        else:
            cov_from_segments(gene,
                              segments,
                              edges,
                              edge_idx,
                              axes[-1],
                              xlim=xlim,
                              log=options.log,
                              grid=True,
                              order='C')
        axes[-1].set_title('Segment counts')

        ### plot structure of a single given event
        #if options.event_id is not None:
        axes.append(fig.add_subplot(gs[len(axes), 0], sharex=axes[0]))

        ### user defined event to plot
        if options.event_id is not None:
            event_info = [
                x[::-1] for x in re.split(
                    r'[._]', options.event_id[::-1], maxsplit=1)[::-1]
            ]
            event_info[1] = int(event_info[1]) - 1
            event_info = sp.array(event_info, dtype='str')[sp.newaxis, :]
            event_tag = '.%s' % options.event_id
        ### get all significant events of the current gene
        else:
            event_info = get_conf_events(options, gid)

        plot_event(options, event_info, axes[-1], xlim)

        ### we only need to adapt the xoom for one axis object - as we share the x
        zoom_x = [float(x) for x in options.zoom_x.split(',')]
        xlim = axes[0].get_xlim()
        xdiff = xlim[1] - xlim[0]
        axes[0].set_xlim(
            [xlim[0] + (zoom_x[0] * xdiff), xlim[0] + (zoom_x[1] * xdiff)])

        plt.tight_layout()
        ### save plot into file
        if options.format == 'd3':
            out_fname = os.path.join(
                options.outdir, 'plots', 'gene_overview_C%i_%s%s%s.html' %
                (options.confidence, gene.name, event_tag, log_tag))
            plugins.clear(fig)
            plugins.connect(fig, plugins.Zoom(enabled=True))
            mpld3.save_html(fig, open(out_fname, 'w'))
        else:
            out_fname = os.path.join(
                options.outdir, 'plots', 'gene_overview_C%i_%s%s%s.%s' %
                (options.confidence, gene.name, event_tag, log_tag,
                 options.format))
            plt.savefig(out_fname, format=options.format, bbox_inches='tight')
        plt.close(fig)
Beispiel #9
0
def plot_beats_per_mile(data):
    #divide meters by this factor to get in terms of miles
    convert_meters_to_miles = 1609.344

    #plt.style.use('seaborn-poster') #sets the size of the charts
    plt.style.use('ggplot')

    #Data is dictionary mapping id to (activity_name, start_date_local, average_hr, distance, total_elevation_gain)

    data_by_week = {}

    for key in data:
        datetime_obj = datetime.strptime(data[key][1], '%Y-%m-%d %H:%M:%S')
        year_week_val = datetime_obj.isocalendar()[:-1]

        if year_week_val not in data_by_week:
            #Storing average_hr, distance, total_elevation_gain
            data_by_week[year_week_val] = [(data[key][2], data[key][3],
                                            data[key][4])]
        else:
            data_by_week[year_week_val].append(
                (data[key][2], data[key][3], data[key][4]))

    x_axis = []
    x_label = []
    y_axis = []
    for key in data_by_week:
        print(key)
        total_hr_each_week = 0
        distance_week = 0
        for activity in data_by_week[key]:
            total_hr_each_week += activity[0] * (activity[1] /
                                                 convert_meters_to_miles)
            distance_week += activity[1] / convert_meters_to_miles
        avg_hr_per_mile = total_hr_each_week / distance_week

        x_label.append(str(key[0]) + "-" + str(key[1]))
        x_axis.append(key[0] * 52 + key[1])
        y_axis.append(avg_hr_per_mile)
        total_hr_each_week = 0
        distance_week = 0

    fig, ax = plt.subplots()

    plt.xlabel("Weeks", fontsize=20)
    plt.ylabel("Average Heartrate Per Mile", fontsize=20)
    plt.title("Average Heartrate Per Mile Sorted by Week", fontsize=25)

    line = plt.plot(x_axis,
                    y_axis,
                    linestyle='solid',
                    marker='.',
                    markersize=14)

    x = np.asarray(x_axis)
    y = np.asarray(y_axis)

    b, m = polyfit(x, y, 1)
    #Plots regression line
    plt.plot(x, b + m * x, linestyle='solid')

    plugins.clear(fig)  # clear all plugins from the figure

    #tooltip = plugins.PointHTMLTooltip(line[0], label,
    #hoffset = -60, voffset = -70, css = css)

    plugins.connect(fig, plugins.Reset(), plugins.BoxZoom(), plugins.Zoom())

    return mpld3.fig_to_html(fig)
Beispiel #10
0
plot_rest = jitter(
    reduced[rest["Membership"]]["PC1"],
    reduced[rest["Membership"]]["PC2"],
    c="b",
    alpha=0.4,
    label="Non-WMC"
)

plot_wmc = jitter(
    reduced[wmc["Membership"]]["PC1"],
    reduced[wmc["Membership"]]["PC2"],
    c="g",
    alpha=0.4,
    label="WMC"
)

plt.xlabel("PC1")
plt.ylabel("PC2")

tooltip_rest = plugins.PointHTMLTooltip(plot_rest, reduced[rest["Membership"]].index.tolist())
tooltip_wmc = plugins.PointHTMLTooltip(plot_wmc, reduced[wmc["Membership"]].index.tolist())

zoom = plugins.Zoom(button=False, enabled=True)
box_zoom = plugins.BoxZoom(button=False, enabled=True)


plugins.connect(plt.gcf(), tooltip_rest)
plugins.connect(plt.gcf(), tooltip_wmc)

mpld3.save_html(plt.gcf(), "pca-2017.html")
Beispiel #11
0
def spladder_viz(options):
    """Main visualization code"""

    ### parse parameters from options object
    options = settings.parse_args(options, identity='viz')

    ### create plot directory if it does not exist yet
    if options.testdir != '-':
        dirname = options.testdir
    else:
        dirname = options.outdir
    if not os.path.exists(os.path.join(dirname, 'plots')):
        os.mkdir(os.path.join(dirname, 'plots'))

    if options.format == 'd3':
        try:
            import mpld3
            from mpld3 import plugins
        except ImportError:
            sys.stderr.write(
                "ERROR: missing package for output format d3. Package mpld3 required"
            )
            sys.exit(1)

    ### load gene information
    gene_names = get_gene_names(options)

    rows = get_plot_len(options)
    gs = gridspec.GridSpec(rows, 1)

    ### set color maps
    cmap_cov = plt.get_cmap('jet')
    cmap_edg = plt.get_cmap('jet')

    ### plot log scale?
    log_tag = ''
    if options.log:
        log_tag = '.log'
    event_tag = ''

    ### did we get any labels?
    if ',' in options.labels:
        options.labels = options.labels.strip(',').split(',')
        assert len(options.labels) == len(
            options.bam_fnames
        ), "The number of given labels (%i) needs to match the number of given bam file groups (%i)" % (
            len(options.labels), len(options.bam_fnames))

    # Collect genes to be plotted - there are three cases possible
    # 1) the user provides a gene ID
    # 2) all genes containing a significant event of a given type from differential testing are plotted
    # 3) all genes that contain any event are plotted

    ### the user chose a specific gene for plotting
    ### create pairs of gene ids and an event_id (the latter is None by default)
    if options.gene_name is not None:
        gids = [[
            sp.where(sp.array(gene_names) == options.gene_name)[0][0],
            options.event_id
        ]]
        if len(gids) == 0:
            sys.stderr.write(
                'ERROR: provided gene ID %s could not be found, please check for correctness\n'
                % options.gene_name)
            sys.exit(1)
    ### the plotting happens on the results of spladder test
    ### the user chooses to plot the top k significant events
    ### this requires the event type to be specified
    elif options.test_result > 0:
        gene_names = []
        for event_type in options.event_types:
            ### the testing script should generate a setup file for the test
            ### SETUP is structured as follows:
            ###  [gene_strains, event_strains, dmatrix0, dmatrix1, event_type, options]
            labels = options.test_labels.split(':')
            options.labels = labels
            if options.testdir != '-':
                testdir = dirname
            else:
                testdir = os.path.join(
                    dirname, 'testing_%s_vs_%s' % (labels[0], labels[1]))
            SETUP = pickle.load(
                open(
                    os.path.join(
                        testdir, 'test_setup_C%i_%s.pickle' %
                        (options.confidence, event_type)), 'rb'))

            ### get strains to plot
            idx1 = sp.where(sp.in1d(SETUP[0], SETUP[6]['conditionA']))[0]
            idx2 = sp.where(sp.in1d(SETUP[0], SETUP[6]['conditionB']))[0]

            ### load test results
            for l, line in enumerate(
                    open(
                        os.path.join(
                            testdir, 'test_results_C%i_%s.tsv' %
                            (options.confidence, event_type)), 'r')):
                if l == 0:
                    continue
                if l > options.test_result:
                    break
                sl = line.strip().split('\t')
                gene_names.append([sl[1], sl[0]])
        gids = get_gene_ids(options, gene_names)
    ### no gene specified but result provided - plot all genes with confirmed events
    ### if an event_id is provided, only the associated gene will be plotted
    else:
        gids = get_gene_ids(options)

    ### iterate over genes to plot
    for gid in gids:
        ### gather information about the gene we plot
        gene = load_genes(options, idx=[gid[0]])[0]
        if options.verbose:
            print('plotting information for gene %s' % gene.name)
        gene.from_sparse()

        ### event to plot is specified with the gene id list
        if gid[1] is not None:
            event_info = [
                x[::-1]
                for x in re.split(r'[._]', gid[1][::-1], maxsplit=1)[::-1]
            ]
            event_info[1] = int(event_info[1]) - 1
            event_info = sp.array(event_info, dtype='str')[sp.newaxis, :]
            event_tag = '.%s' % gid[1]
        ### get all confident events of the current gene
        else:
            event_info = get_conf_events(options, gid[0])

        ### go over different plotting options
        axes = []
        ### plot result of testing
        if options.test_result > 0:
            fig = plt.figure(figsize=(9, 5), dpi=200)
            gs = gridspec.GridSpec(2, 1, height_ratios=[4, 1])
            _add_ax(fig, axes, gs)
            _add_ax(fig, axes, gs)
            _plot_event(options,
                        event_info,
                        fig,
                        axes[1],
                        gs,
                        None,
                        padding=100)
            start, stop = axes[1].get_xlim()
            plot_bam(options,
                     gene,
                     options.bam_fnames,
                     fig,
                     axes[0],
                     gs,
                     None,
                     cmap_cov,
                     cmap_edg,
                     single=False,
                     sharex=axes[1],
                     start=int(start),
                     stop=int(stop))

        ### plot custom layout
        else:
            ### set defaults
            if not options.user:
                options.splicegraph = True
                options.transcripts = True

            if options.format == 'd3':
                fig = plt.figure(figsize=(12, 2 * rows), dpi=100)
            else:
                fig = plt.figure(figsize=(18, 3 * rows), dpi=200)

            xlim = None
            ### plot splicing graph
            if options.splicegraph:
                _plot_splicegraph(gene, fig, axes, gs)
                xlim = axes[-1].get_xlim()

            ### plot annotated transcripts
            if options.transcripts:
                sharex = None if len(axes) == 0 else axes[0]
                axes.append(fig.add_subplot(gs[len(axes), 0], sharex=sharex))
                multiple(gene.exons, ax=axes[-1], x_range=xlim)
                axes[-1].set_title('Annotated Transcripts')

            ### plot coverage information for a set of given samples
            if len(options.bam_fnames) > 0:
                plot_bam(options, gene, options.bam_fnames, fig, axes, gs,
                         xlim, cmap_cov, cmap_edg)

                ### plot all the samples in a single plot
                if len(options.bam_fnames) > 1:
                    plot_bam(options,
                             gene,
                             options.bam_fnames,
                             fig,
                             axes,
                             gs,
                             xlim,
                             cmap_cov,
                             cmap_edg,
                             single=False)

            ### plot segment counts
            if len(options.bam_fnames
                   ) == 0 or False:  # add option for segment plots
                if options.test_result > 0:
                    _plot_segments(options, gid, fig, axes, gs, [idx1, idx2])
                else:
                    _plot_segments(options, gid, fig, axes, gs)

            ### plot structure of a single given event
            _plot_event(options, event_info, fig, axes, gs, xlim)

        ### we only need to adapt the xoom for one axis object - as we share the x
        zoom_x = [float(x) for x in options.zoom_x.split(',')]
        xlim = axes[0].get_xlim()
        xdiff = xlim[1] - xlim[0]
        axes[0].set_xlim(
            [xlim[0] + (zoom_x[0] * xdiff), xlim[0] + (zoom_x[1] * xdiff)])

        for ax in axes:
            vax.clean_axis(ax)

        plt.tight_layout()
        ### save plot into file
        if options.format == 'd3':
            out_fname = os.path.join(
                dirname, 'plots', 'gene_overview_C%i_%s%s%s.html' %
                (options.confidence, gene.name, event_tag, log_tag))
            plugins.clear(fig)
            plugins.connect(fig, plugins.Zoom(enabled=True))
            mpld3.save_html(fig, open(out_fname, 'w'))
        else:
            if options.test_result > 0:
                out_fname = os.path.join(
                    dirname, 'plots', 'gene_overview_C%i_%s%s%s.%s' %
                    (options.confidence, gene.name, event_tag, log_tag,
                     options.format))
            else:
                out_fname = os.path.join(
                    dirname, 'plots', 'gene_overview_C%i_%s%s%s.%s' %
                    (options.confidence, gene.name, event_tag, log_tag,
                     options.format))
            plt.savefig(out_fname, format=options.format, bbox_inches='tight')
        plt.close(fig)
Beispiel #12
0
def result():
    try:
        g.conn = sqlite3.connect('case_data.db')
        g.curr = g.conn.cursor()
        g.curr.execute("SELECT country_name FROM interval_tb")
        country = g.curr.fetchone()[0]
        g.curr.execute("SELECT duration FROM interval_tb")
        period = g.curr.fetchall()
        g.curr.execute("SELECT restriction FROM interval_tb")
        restrictions = g.curr.fetchall()
        g.curr.execute("SELECT * FROM cases_tb WHERE country_name=?",
                       (country, ))
        country_statistics = g.curr.fetchone()
        g.conn.commit()
        g.conn.close()

        plt.rcParams.update({'font.size': 12})

        def differential(initial, time, *args):

            # Holds initial conditions for the population
            susceptible, exposed, infected, recovered = initial

            # System of differential equations
            # Represents changes in the susceptible, exposed, infected, and recovered population
            dSdt = -(beta * susceptible * infected) / POPULATION_SIZE
            dEdt = beta * susceptible * infected / POPULATION_SIZE - SIGMA * exposed
            dIdt = SIGMA * exposed - GAMMA * infected
            dRdt = GAMMA * infected

            return dSdt, dEdt, dIdt, dRdt

        POPULATION_SIZE = country_statistics[3]
        BETA = 0.956
        SIGMA = 1 / 5.2
        GAMMA = 1 / 2.3
        REPRODUCTION_NUMBER = BETA / GAMMA

        period = list(period)
        time = [0]
        restrictions = list(restrictions)
        contact_state = []

        for i in range(len(period)):
            period[i] = period[i][0]

        for i in range(len(restrictions)):
            restrictions[i] = restrictions[i][0]

        time += list(accumulate(period))

        for j in range(len(period)):
            if restrictions[j] == "Lockdown":
                contact_state.append(0.26)
            elif restrictions[j] == "Vacation":
                contact_state.append(0.46)
            elif restrictions[j] == "School closure":
                contact_state.append(0.8)
            else:
                contact_state.append(1)

        susceptible_initial = POPULATION_SIZE - \
            country_statistics[2] - country_statistics[1]
        exposed_initial = 0
        infected_initial = country_statistics[2]
        recovered_initial = country_statistics[1]

        infected_population = 0
        current_maximum = 0

        fig, ax = plt.subplots()

        for graph in range(len(period)):

            beta = BETA * contact_state[graph]

            duration = np.linspace(time[graph], time[graph + 1],
                                   period[graph] * 2 + 1)

            solution = odeint(differential,
                              (susceptible_initial, exposed_initial,
                               infected_initial, recovered_initial),
                              duration,
                              args=(beta, SIGMA, GAMMA, POPULATION_SIZE))

            susceptible_initial = solution[period[graph] * 2, 0]
            exposed_initial = solution[period[graph] * 2, 1]
            infected_initial = solution[period[graph] * 2, 2]
            recovered_initial = solution[period[graph] * 2, 3]

            current_maximum = max(solution[:, 2])
            if current_maximum > infected_population:
                infected_population = current_maximum

            if graph == 0:
                ax.plot(duration, solution[:, 0], 'r', label='Susceptible')
                ax.plot(duration, solution[:, 1], 'g', label='Exposed')
                ax.plot(duration, solution[:, 2], 'b', label='Infectious')
                ax.plot(duration, solution[:, 3], 'y', label='Recovered')

            else:
                ax.plot(duration, solution[:, 0], 'r')
                ax.plot(duration, solution[:, 1], 'g')
                ax.plot(duration, solution[:, 2], 'b')
                ax.plot(duration, solution[:, 3], 'y')

        ax.set_xlim(0, time[-1])
        ax.set_ylim(0, POPULATION_SIZE + POPULATION_SIZE * 0.01)

        ax.legend(loc='best')

        ax.set_xlabel('Time (days)')
        ax.set_ylabel('Population', rotation='horizontal')

        fig.set_size_inches(8, 6)
        ax.yaxis.set_label_coords(-0.1, 1.02)

        plugins.clear(fig)
        plugins.connect(fig, plugins.Reset())
        plugins.connect(fig, plugins.BoxZoom(enabled=True))
        plugins.connect(fig, plugins.Zoom())

        seir_graph = mpld3.fig_to_html(fig)
        plt.close()

        effective_reproduction_number = REPRODUCTION_NUMBER * \
            susceptible_initial/POPULATION_SIZE
        effective_reproduction_number = float(
            "{:0.3f}".format(effective_reproduction_number))

        return {
            'graph': seir_graph,
            'r0': REPRODUCTION_NUMBER,
            'rt': effective_reproduction_number,
            'susceptible_state': int(susceptible_initial),
            'exposed_state': int(exposed_initial),
            'infected_state': int(infected_initial),
            'recovered_state': int(recovered_initial),
            'max_active_cases': int(infected_population)
        }
    except:
        return jsonify(0)
Beispiel #13
0
def problem_view(request, problem_id):
    user = request.user
    problem = get_object_or_404(Problem, id=problem_id)
    if problem.user_id != user and not user.is_superuser:
        raise PermissionDenied

    parse_error = False
    a = None
    b = None
    c = None
    a_copy = None
    b_copy = None
    result = None
    figure = None
    parse_result = parse_problem(problem.problem_text)
    print parse_result
    if parse_result is not None:
        a, b, c, x = parse_result
        if a and b and c and x:
            result = simple_simplex(copy.deepcopy(a), copy.deepcopy(b),
                                    copy.deepcopy(c))
            b.pop(0)
            a_copy = copy.deepcopy(a)
            b_copy = copy.deepcopy(b)

            if len(x) is 2:
                tmp = [0, 0]
                tmp[1] = 1
                a.insert(0, copy.deepcopy(tmp))
                tmp[0] = 1
                tmp[1] = 0
                a.insert(0, copy.deepcopy(tmp))
                b.insert(0, 0)
                b.insert(0, 0)
                dots = []
                for i in range(len(b)):
                    dots.append([])
                shape_x = []
                for i in xrange(0, len(b)):
                    for j in xrange(i + 1, len(b)):
                        if a[i][0] == a[j][0] and a[i][1] == a[j][1]:
                            continue
                        shape_a = np.array([[a[i][0], a[i][1]],
                                            [a[j][0], a[j][1]]])
                        shape_b = np.array([b[i], b[j]])
                        res = np.linalg.solve(shape_a, shape_b)
                        dots[i].append(res)
                        dots[j].append(res)
                # fig, ax = plt.subplots()
                fig = plt.figure()
                ax = fig.add_subplot(111)
                for dot in dots:
                    ax.plot(
                        [float(dot[0][0]), float(dot[1][0])],
                        [float(dot[0][1]), float(dot[1][1])],
                        ls='-',
                        color='green',
                        marker='o',
                        lw=2)
                    # print([dot[0], dot[1]])
                ax.set_xlabel('X axis')
                ax.set_ylabel('Y axis')
                ax.set_title('Plot of 2D Problem!', size=14)
                plugins.clear(fig)
                plugins.connect(fig, plugins.Reset(),
                                plugins.Zoom(enabled=True), plugins.BoxZoom())
                figure = fig_to_html(fig,
                                     d3_url=STATIC_URL + 'js/d3.min.js',
                                     mpld3_url=STATIC_URL + 'js/mpld3.v0.2.js',
                                     use_http=True)
            else:
                figure = 'Larger than 2D!'
    else:
        parse_error = True

    return render_to_response(
        'problems/view.html', {
            'user': user,
            'problem': problem,
            'parse_error': parse_error,
            'figure': figure,
            'a': a_copy,
            'b': b_copy,
            'c': c,
            'result': result,
            'view_name': 'Problem - %s' % problem.id,
        })
Beispiel #14
0
    def render(self, idf, filename='output.html'):
        progress_inst = helpers.Progress(idf.opt)
        self.progress = progress_inst.progress

        if idf.opt['outputFilename']:
            filename = idf.opt['outputFilename']

        if idf.opt['outputAs'] == 'html':
            # write matplotlib/d3 plots to html file
            import matplotlib
            import matplotlib.pyplot as plt, mpld3
            import matplotlib.axes

            from mpld3 import plugins
            from jinja2 import Environment, FileSystemLoader
        elif idf.opt['outputAs'] in ['pdf', 'interactive', 'tikz']:
            # show plots in separate matplotlib windows
            import matplotlib
            if idf.opt['outputAs'] == 'pdf':
                from matplotlib.backends.backend_pdf import PdfPages
                pp = PdfPages(filename)
            import matplotlib.pyplot as plt
            import matplotlib.axes
        else:
            print("No proper output method given. Not plotting.")
            return

        font_size = 10
        if idf.opt['outputAs'] in ['pdf', 'tikz']:
            if idf.opt['plotPerJoint']:
                font_size = 30
            else:
                font_size = 12
            matplotlib.rcParams.update({'font.size': font_size})
            matplotlib.rcParams.update({'axes.labelsize': font_size - 5})
            matplotlib.rcParams.update({'axes.linewidth': font_size / 15.})
            matplotlib.rcParams.update({'axes.titlesize': font_size - 2})
            matplotlib.rcParams.update({'legend.fontsize': font_size - 2})
            matplotlib.rcParams.update({'xtick.labelsize': font_size - 5})
            matplotlib.rcParams.update({'ytick.labelsize': font_size - 5})
            matplotlib.rcParams.update({'lines.linewidth': font_size / 15.})
            matplotlib.rcParams.update({'patch.linewidth': font_size / 15.})
            matplotlib.rcParams.update({'grid.linewidth': font_size / 20.})

        # skip some samples so graphs don't get too large/detailed TODO: change skip so that some
        # maximum number of points is plotted (determined by screen etc.)
        skip = 5

        #create figures and plots
        figures = list()
        for ds in self.progress(range(len(self.datasets))):
            group = self.datasets[ds]
            fig, axes = plt.subplots(len(group['dataset']),
                                     sharex=True,
                                     sharey=True)
            # scale unified scaling figures to same ranges and add some margin
            if group['unified_scaling']:
                ymin = 0
                ymax = 0
                for i in range(len(group['dataset'])):
                    ymin = np.min(
                        (np.min(group['dataset'][i]['data']), ymin)) * 1.05
                    ymax = np.max(
                        (np.max(group['dataset'][i]['data']), ymax)) * 1.05

            #plot each group of data
            for d_i in range(len(group['dataset'])):
                d = group['dataset'][d_i]
                if not issubclass(type(axes), matplotlib.axes.SubplotBase):
                    ax = axes[d_i]
                else:
                    ax = axes
                    axes = [axes]
                if idf.opt['outputAs'] != 'tikz':
                    ax.set_title(d['title'])
                if group['unified_scaling']:
                    ax.set_ylim([ymin, ymax])
                for data_i in range(0, len(d['data'])):
                    if len(d['data'][data_i].shape) > 1:
                        #data matrices
                        for i in range(0, d['data'][data_i].shape[1]):
                            l = group['labels'][i] if data_i == 0 else ''
                            if i < 6 and 'contains_base' in group and group[
                                    'contains_base']:
                                ls = 'dashed'
                            else:
                                ls = '-'
                            dashes = ()  # type: Tuple
                            if idf.opt['plotErrors']:
                                if idf.opt['plotPrioriTorques']:
                                    n = 3
                                else:
                                    n = 2
                                if i == n:
                                    ls = 'dashed'
                                    dashes = (3, 0.5)
                            ax.plot(d['time'][::skip],
                                    d['data'][data_i][::skip, i],
                                    label=l,
                                    color=colors[i],
                                    alpha=1 - (data_i / 2.0),
                                    linestyle=ls,
                                    dashes=dashes)
                    else:
                        #data vector
                        ax.plot(d['time'][::skip],
                                d['data'][data_i][::skip],
                                label=group['labels'][d_i],
                                color=colors[0],
                                alpha=1 - (data_i / 2.0))

                ax.grid(which='both', linestyle="dotted", alpha=0.8)
                if 'y_label' in group:
                    ax.set_ylabel(group['y_label'])

            if idf.opt['outputAs'] != 'tikz':
                ax.set_xlabel("Time (s)")

            plt.setp([a.get_xticklabels() for a in axes[:-1]], visible=False)
            #plt.setp([a.get_yticklabels() for a in axes], fontsize=8)

            if idf.opt['plotLegend']:
                handles, labels = ax.get_legend_handles_labels()
                if idf.opt['outputAs'] == 'html':
                    #TODO: show legend properly (see mpld3 bug #274)
                    #leg = fig.legend(handles, labels, loc='upper right', fancybox=True, fontsize=10, title='')
                    leg = axes[0].legend(handles,
                                         labels,
                                         loc='upper right',
                                         fancybox=True,
                                         fontsize=10,
                                         title='',
                                         prop={'size': 8})
                else:
                    leg = plt.figlegend(handles,
                                        labels,
                                        loc='upper right',
                                        fancybox=True,
                                        fontsize=font_size,
                                        title='',
                                        prop={'size': font_size - 3})
                    leg.draggable()

            fig.subplots_adjust(hspace=2)
            fig.set_tight_layout(True)

            if idf.opt['outputAs'] == 'html':
                plugins.clear(fig)
                plugins.connect(fig, plugins.Reset(), plugins.BoxZoom(),
                                plugins.Zoom(enabled=False),
                                plugins.MousePosition(fontsize=14, fmt=".5g"))
                figures.append(mpld3.fig_to_html(fig))
            elif idf.opt['outputAs'] == 'interactive':
                plt.show(block=False)
            elif idf.opt['outputAs'] == 'pdf':
                pp.savefig(plt.gcf())
            elif idf.opt['outputAs'] == 'tikz':
                from matplotlib2tikz import save as tikz_save
                tikz_save('{}_{}_{}.tex'.format(
                    filename, group['dataset'][0]['title'].replace('_', '-'),
                    ds // idf.model.num_dofs),
                          figureheight='\\figureheight',
                          figurewidth='\\figurewidth',
                          show_info=False)

        if idf.opt['outputAs'] == 'html':
            path = os.path.dirname(os.path.abspath(__file__))
            template_environment = Environment(
                autoescape=False,
                loader=FileSystemLoader(os.path.join(path, '../output')),
                trim_blocks=False)

            context = {'figures': figures, 'text': self.text}
            outfile = os.path.join(path, '..', 'output', filename)
            import codecs
            with codecs.open(outfile, 'w', 'utf-8') as f:
                html = template_environment.get_template(
                    "templates/index.html").render(context)
                f.write(html)

            print("Saved output at file://{}".format(outfile))
        elif idf.opt['outputAs'] == 'interactive':
            #keep non-blocking plot windows open
            plt.show()
        elif idf.opt['outputAs'] == 'pdf':
            pp.close()