예제 #1
0
    def bscoreCumulative(self, figurename, dataname):
        """-----------------------------------------------------------------------------------------
        Bokeh plot of cumulative distribution as a line on right hand axis

        :param figurename: string, name of figures (stored in self.figure)
        :param dataname: string, name of data frame (stored in self.frame)
        :return: True
        -----------------------------------------------------------------------------------------"""
        data = self.frame[dataname]
        figure = self.figure[figurename]

        source = ColumnDataSource(data)

        figure.extra_y_ranges = {"cumulative": Range1d(start=0.0, end=1.0)}
        axis2 = LinearAxis(y_range_name="cumulative")
        axis2.ticker.num_minor_ticks = 10
        figure.add_layout(axis2, 'right')
        figure.line(source=source,
                    x='score',
                    y='cumulative',
                    y_range_name='cumulative',
                    line_width=2,
                    color='#1122cc')

        # shaded box showing 95% level
        box = BoxAnnotation(bottom=0.95,
                            top=1.0,
                            y_range_name='cumulative',
                            fill_color='#FFBBBB',
                            line_width=3,
                            line_dash='dashed')
        figure.add_layout(box)

        return True
예제 #2
0
    def make_new_graph(self):
        """
        Creates a new set of graphs.
        """
        self.active_lines = {}
        legend_strings = []
        figure = default_figure(self.get_figure_opts())

        for y, i in zip(self.key_group, range(len(self.key_group))):
            print("make_new_graph(): plotting {}".format(y))

            if y == self.indep_var:
                print("make_new_graph(): y={} is the indep var".format(y))
                continue

            print("make_new_graph(): y={} is a dep var".format(y))

            # storing the line renderer objects will allow us to update the
            # lines without triggering a page redraw
            line = figure.line(self.indep_var, y, source=self.source,
                    color=self.colors[i], line_width=1.5)
            self.active_lines[y] = line
            legend_strings.append((y, [line]))

        l = Legend(legends=legend_strings, location=(0, 0))
        figure.add_layout(l, 'right')
        return figure
예제 #3
0
def calibrate(current, reference, figure):
    r = scipy.stats.linregress(x=current, y=reference, alternative='two-sided')
    a, b = current[0], current[-1]
    ca, cb = (
        a * r.slope + r.intercept,
        b * r.slope + r.intercept,
    )
    figure.line(
        x=[a, b],
        y=[ca, cb],
        color="red",
    )

    rps = Label(x=min(current),
                y=(max(reference) - min(reference)) * .8 + min(reference),
                x_units="data",
                y_units="data",
                text=f"slope={r.slope}\nintercept={r.intercept}\n"
                f"rvalue={r.rvalue}\npvalue={r.pvalue}\n"
                f"stderr={r.stderr}\nintercept_stderr={r.intercept_stderr}",
                render_mode='css',
                border_line_color='black',
                border_line_alpha=1.0,
                background_fill_color='white',
                background_fill_alpha=1.0)
    figure.add_layout(rps)
    return dict(slope=r.slope, intercept=r.intercept)
예제 #4
0
    def render(**figure_kwargs):

        figure = Figure(**figure_kwargs)
        figure.add_layout(Legend())
        figure.legend.location = "top_left"
        figure.legend.click_policy = "hide"

        figure.line(legend_label=yfast,
                    source=onesource,
                    x='datetime',
                    y=yfast,
                    color='red')
        figure.line(legend_label=yslow,
                    source=onesource,
                    x='datetime',
                    y=yslow,
                    color='blue')
        figure.segment(legend_label=y1hist,
                       source=onesource,
                       x0='datetime',
                       x1='datetime',
                       y0=0,
                       y1=y1hist,
                       line_width=6,
                       color='black',
                       alpha=0.5)

        return figure
    def plot(self, figure, color_attr=None, history_step=None, solid_color='#0485d1', circle_attr=None,
             circle_attr_transform=lambda x: x, circle_line_color='#cb7723', circle_fill_color='#fcb001',
             circle_hover_attrs=[], color_attr_bounds=None):
        """ To simply plot the network in a solid color, use color_attr=None and history_step=None.
            To plot a fixed attribute of each network reach such as redd capacity, set color_attr to the
            name of that attribute and history_step=None.
            To plot an attribute of the network's history that varies over time, use history_step along
            with the name of that attribute.
            color_attr_bounds is None to use the min and max values of that variable in the current plot, or
            specifiable to use a standard color range across multiple plots"""
        lines = [reach.points for reach in self.reaches]
        source = ColumnDataSource({'xs': [list(np.array(line).T[0]) for line in lines],
                                   'ys': [list(np.array(line).T[1]) for line in lines],
                                   'line_widths': [0.5 * reach.strahler_order for reach in self.reaches]})

        figure.add_layout(Label(x=self.migration_reach.midpoint[0], y=self.migration_reach.midpoint[1]+750,
                                text='Migration', text_align='center'))
        figure.add_layout(Label(x=self.ocean_reach.midpoint[0], y=self.ocean_reach.midpoint[1]+750,
                                text='Ocean', text_align='center'))
        if history_step is not None:
            year = 1 + math.floor(history_step / time_settings['WEEKS_PER_YEAR'])
            step_within_year = history_step % time_settings['WEEKS_PER_YEAR']
            days_into_year = 1 + step_within_year * time_settings['DAYS_PER_WEEK']
            date1 = datetime.date.fromordinal(days_into_year).strftime("%b %e")
            date2 = datetime.date.fromordinal(days_into_year + 7).strftime("%b %e")
            timestring = "Timestep {0} (step {1} of year {2}, {3} - {4})".format(history_step,
                                                                                 1 + step_within_year,
                                                                                 year,
                                                                                 date1,
                                                                                 date2)
            figure.add_layout(Label(x=30, y=700, x_units='screen', y_units='screen', text=timestring))
            season, season_color = self.season_label(history_step)
            figure.add_layout(Label(x=650, y=700, x_units='screen', y_units='screen', text=season, text_color=season_color))
        if color_attr is None:
            figure.multi_line('xs', 'ys', source=source, line_color=solid_color, line_width='line_widths')
        else:
            source.add([reach.reach_statistic(color_attr, history_step) for reach in self.reaches], name='color_values')
            color_low_value = min(source.data['color_values']) if color_attr_bounds is None else color_attr_bounds[0]
            color_high_value = max(source.data['color_values']) if color_attr_bounds is None else color_attr_bounds[1]
            mapper = LinearColorMapper(palette='Viridis256', low=color_low_value, high=color_high_value)
            figure.multi_line('xs', 'ys', source=source, line_color={'field': 'color_values', 'transform': mapper}, line_width='line_widths')
            fmt = NumeralTickFormatter(format='00')
            color_bar = ColorBar(color_mapper=mapper, location=(0, 0), title=color_attr, formatter=fmt, label_standoff=7)
            figure.add_layout(color_bar, 'right')
        if circle_attr is not None:
            circle_source = ColumnDataSource({'xs': [reach.midpoint[0] for reach in self.reaches],
                                              'ys': [reach.midpoint[1] for reach in self.reaches]})
            circle_source.add([circle_attr_transform(reach.reach_statistic(circle_attr, history_step))
                               for reach in self.reaches], name='circle_sizes')
            for attr in circle_hover_attrs:
                circle_source.add([reach.reach_statistic(attr, history_step) for reach in self.reaches], name=attr)
            figure.scatter('xs', 'ys', source=circle_source, name='scatterplot', marker='circle', size='circle_sizes',
                           line_color=circle_line_color, fill_color=circle_fill_color, alpha=0.5)
            hover_tooltips = []
            for attr in circle_hover_attrs:
                hover_tooltips.append((attr, "@"+attr))
            figure.add_tools(HoverTool(tooltips=hover_tooltips, names=['scatterplot']))
            figure.toolbar.logo = None
예제 #6
0
def color_bar(figure, color_mapper, **kwargs):
    """
    Add a ColorBar to the input figure
    Arguments:
        figure (bokeh.plotting.GlyphRenderer): figure to add the ColorBar to
        color_mapper (bokeh.models.mappers.Mapper): mapper object
        kwargs: additional ColorBar properties
    """
    figure.add_layout(ColorBar(color_mapper=color_mapper, **kwargs), 'left')
                line_color=c_black,
                line_width=1,
                fill_color=c_orange,
                fill_alpha=0.75)
 figure.add_glyph(source, visual)
 # sigma arrows
 source = force_symbol.data_source['sigma_arrows']
 visual = Arrow(end=OpenHead(line_color=c_black, line_width=1, size=5),
                x_start='xS',
                y_start='yS',
                x_end='xE',
                y_end='yE',
                line_color=c_black,
                line_width=1,
                source=source)
 figure.add_layout(visual)
 # R arrows
 source = force_symbol.data_source['R_arrow']
 visual = Arrow(end=OpenHead(line_color=c_black, line_width=2, size=10),
                x_start='xS',
                y_start='yS',
                x_end='xE',
                y_end='yE',
                line_color=c_black,
                line_width=2,
                source=source)
 figure.add_layout(visual)
 # plot labels
 source = force_symbol.data_source['R_label']
 visual = LatexLabelSet(x='x',
                        y='y',