Ejemplo n.º 1
0
    ("line", Line(x="x", y="y", line_color="#F46D43")),
    ("multi_line", MultiLine(xs="xs", ys="ys", line_color="#8073AC", line_width=2)),
    ("oval", Oval(x="x", y="y", width=screen(15), height=screen(25), angle=-0.7, fill_color="#1D91C0")),
    ("patch", Patch(x="x", y="y", fill_color="#A6CEE3")),
    ("patches", Patches(xs="xs", ys="ys", fill_color="#FB9A99")),
    ("quad", Quad(left="x", right="xp01", top="y", bottom="ym01", fill_color="#B3DE69")),
    ("quadratic", Quadratic(x0="x", y0="y", x1="xp02", y1="y", cx="xp01", cy="yp01", line_color="#4DAF4A", line_width=3)),
    ("ray", Ray(x="x", y="y", length=45, angle=-0.7, line_color="#FB8072", line_width=2)),
    ("rect", Rect(x="x", y="y", width=screen(10), height=screen(20), angle=-0.7, fill_color="#CAB2D6")),
    ("segment", Segment(x0="x", y0="y", x1="xm01", y1="ym01", line_color="#F4A582", line_width=3)),
    ("text", Text(x="x", y="y", text=["hello"])),
    ("wedge", Wedge(x="x", y="y", radius=screen(15), start_angle=0.6, end_angle=4.1, fill_color="#B3DE69")),
]

markers = [
    ("circle", Circle(x="x", y="y", radius=0.1, fill_color="#3288BD")),
    ("circle_x", CircleX(x="x", y="y", size="sizes", line_color="#DD1C77", fill_color=None)),
    ("circle_cross", CircleCross(x="x", y="y", size="sizes", line_color="#FB8072", fill_color=None, line_width=2)),
    ("square", Square(x="x", y="y", size="sizes", fill_color="#74ADD1")),
    ("square_x", SquareX(x="x", y="y", size="sizes", line_color="#FDAE6B", fill_color=None, line_width=2)),
    ("square_cross", SquareCross(x="x", y="y", size="sizes", line_color="#7FC97F", fill_color=None, line_width=2)),
    ("diamond", Diamond(x="x", y="y", size="sizes", line_color="#1C9099", line_width=2)),
    ("diamond_cross", DiamondCross(x="x", y="y", size="sizes", line_color="#386CB0", fill_color=None, line_width=2)),
    ("triangle", Triangle(x="x", y="y", size="sizes", line_color="#99D594", line_width=2)),
    ("inverted_triangle", InvertedTriangle(x="x", y="y", size="sizes", line_color="#DE2D26", line_width=2)),
    ("cross", Cross(x="x", y="y", size="sizes", line_color="#E6550D", fill_color=None, line_width=2)),
    ("asterisk", Asterisk(x="x", y="y", size="sizes", line_color="#F0027F", fill_color=None, line_width=2)),
    ("x", X(x="x", y="y", size="sizes", line_color="thistle", fill_color=None, line_width=2)),
]

def make_tab(title, glyph):
Ejemplo n.º 2
0
# function call to base map

if __name__ == '__main__':
    df = mercator_df()
    fig = nyc_map('Citi bike stations in Jersey city and New york city')
    # colors for Jersey city and NYC regions
    fill_color = {70: 'blue', 71: 'firebrick'}
    line_color = {70: 'blue', 71: 'firebrick'}
    df["fill"] = df['region_id'].map(lambda x: fill_color[x])
    df["line"] = df['region_id'].map(lambda x: line_color[x])

    source = ColumnDataSource(df)
    # circle glyph for the stations
    region_glyph = Circle(x="x",
                          y="y",
                          fill_color="fill",
                          line_color="line",
                          size=10,
                          fill_alpha=0.5)
    region = fig.add_glyph(source, region_glyph)

    # hover tooltip to display name
    tooltips = """
    <div>
        <span style="font-size: 15px;">@name</span>&nbsp;<br>
        <span style="font-size: 15px;">station id: @station_id</span>&nbsp;
    </div>
    """
    # add hover tool to figure
    hover = HoverTool(tooltips=tooltips, renderers=[region])
    fig.add_tools(hover)
    show(fig)
Ejemplo n.º 3
0
                   axis_line_color=None,
                   major_tick_line_color=None,
                   axis_label="Meters behind 2012 Bolt",
                   axis_label_text_font_size="10pt",
                   axis_label_text_font_style="bold")
plot.add_layout(xaxis, "below")
xgrid = Grid(dimension=0, ticker=xaxis.ticker, grid_line_dash="dashed")
plot.add_layout(xgrid)
yticker = SingleIntervalTicker(interval=12, num_minor_ticks=0)
yaxis = LinearAxis(ticker=yticker, major_tick_in=-5, major_tick_out=10)
plot.add_layout(yaxis, "right")

radius = dict(value=5, units="screen")
medal_glyph = Circle(x="MetersBack",
                     y="Year",
                     radius=radius,
                     fill_color="MedalFill",
                     line_color="MedalLine",
                     fill_alpha=0.5)
medal = plot.add_glyph(source, medal_glyph)

athlete_glyph = Text(x="MetersBack",
                     y="Year",
                     x_offset=10,
                     text="SelectedName",
                     text_align="left",
                     text_baseline="middle",
                     text_font_size="9pt")
athlete = plot.add_glyph(source, athlete_glyph)

no_olympics_glyph = Text(x=7.5,
                         y=1942,
Ejemplo n.º 4
0
    def generate_plot(self):
        """Iteratively reformats and plots self.data for each cell+model combo.

        TODO: Finish this doc

        """

        plots = []
        modelnames = self.data.index.levels[0].tolist()

        # Iterate over a list of tuples representing all unique pairs of models.
        for pair in list(itertools.combinations(modelnames, 2)):
            tools = [
                PanTool(),
                SaveTool(),
                WheelZoomTool(),
                ResetTool(),
                self.create_hover(),
            ]

            modelX = pair[0]
            modelY = pair[1]

            dataX = self.data.loc[modelX]
            dataY = self.data.loc[modelY]

            # Only necessary b/c bokeh's $index identifier for HoverTool()
            # is pulling an integer index despite data being indexed by cellid.
            # If cellid strings can be pulled from index instead,
            # this code will no longer be needed.
            cells = []
            cellsX = list(set(dataX.index.values.tolist()))
            cellsY = list(set(dataY.index.values.tolist()))
            if self.fair:
                # cellsX and cellsY should be the same if fair was checked
                cells = cellsX
                if cells != cellsY:
                    self.script = 'Problem with form_data_array:'
                    self.div = 'Model x: ' + modelX + 'and Model y: ' + modelY\
                            + ' applied to different cells despite fair check.'
                    return
            else:
                # If fair wasn't checked, use the longer list to avoid errors.
                if len(cellsX) >= len(cellsY):
                    cells = cellsX
                else:
                    cells = cellsY

            x_mean = np.mean(dataX[self.measure[0]])
            x_median = np.median(dataX[self.measure[0]])
            y_mean = np.mean(dataY[self.measure[0]])
            y_median = np.median(dataY[self.measure[0]])

            x_label = ("{0}, mean: {1:5.4f}, median: {2:5.4f}".format(
                modelX, x_mean, x_median))
            y_label = ("{0}, mean: {1:5.4f}, median: {2:5.4f}".format(
                modelY, y_mean, y_median))

            data = pd.DataFrame({
                'x_values': dataX[self.measure[0]],
                'y_values': dataY[self.measure[0]],
                'cellid': cells,
            })
            dat_source = ColumnDataSource(data)

            p = figure(
                x_range=[0, 1],
                y_range=[0, 1],
                x_axis_label=x_label,
                y_axis_label=y_label,
                title=("{0}, prefix: {1}, suffix: {2}".format(
                    self.measure[0], self.pre, self.suf)),
                tools=tools,
                toolbar_location=TOOL_LOC,
                toolbar_sticky=TOOL_STICK,
                output_backend="svg",
                sizing_mode='scale_width',
            )
            glyph = Circle(
                x='x_values',
                y='y_values',
                size=CIRCLE_SIZE,
                fill_color=CIRCLE_FILL,
                fill_alpha=CIRCLE_ALPHA,
            )
            p.add_glyph(dat_source, glyph)
            p.line([0, 1], [0, 1], line_width=1, color='black')
            plots.append(p)

        # If more than one plot was made (i.e. 2 or more models were selected),
        # put them in a grid.

        #if len(plots) == 1:
        #    singleplot = plots[0]
        #    self.script,self.div = components(singleplot)
        #    return
        #elif len(plots) > 1:
        grid = gridplot(
            plots,
            ncols=GRID_COLS,
            sizing_mode='scale_width',
        )
        self.script, self.div = components(grid)
        if not plots:
            self.script, self.div = ('Error, no plots to display.',
                                     'Make sure you selected two models.')
        else:
            self.plot = grid
            if self.display:
                show(grid)
Ejemplo n.º 5
0
    border_fill_alpha = Override(default=0.0)


source = ColumnDataSource(
    data=dict(x=[1, 2, 3, 4, 4, 5, 5],
              y=[5, 4, 3, 2, 2.1, 1, 1.1],
              color=[
                  "rgb(0, 100, 120)", "green", "blue", "#2c7fb8", "#2c7fb8",
                  "rgba(120, 230, 150, 0.5)", "rgba(120, 230, 150, 0.5)"
              ]))

plot = MyPlot(gradient_angle=45)

circle = Circle(x="x",
                y="y",
                radius=0.2,
                fill_color="color",
                line_color="black")
circle_renderer = plot.add_glyph(source, circle)

plot.add_layout(LinearAxis(), 'below')
plot.add_layout(LinearAxis(), 'left')

tap = TapTool(renderers=[circle_renderer],
              callback=Popup(message="Selected color: @color"))
plot.add_tools(PanTool(), WheelZoomTool(), tap)

doc = Document()
doc.add_root(plot)

if __name__ == "__main__":
Ejemplo n.º 6
0
    def generate_plots(self, doc):
        '''
        :param doc: curdoc() required for bokeh function handler
        :return: None
        '''
        '''
        ----------------------------------------------------------------------------------------------------------
        Define Histogram
        ----------------------------------------------------------------------------------------------------------
        '''
        self.hist_plot = {}

        self.hist_plot['plotting_data'] = np.array(
            self.total_pandas_df[self.select_widget_hist.value])
        self.hist_plot['plot_width'] = 610
        self.hist_plot['plot_height'] = 400
        self.hist_plot[
            'title'] = 'Gaussian Fit of ' + self.select_widget_hist.value

        self.hist_plot['object_figure'] = figure(
            width=self.hist_plot['plot_width'],
            height=self.hist_plot['plot_height'],
            title=self.hist_plot['title'])

        self.hist_plot['source'] = ColumnDataSource(
            data=dict(hist=[], left_edge=[], right_edge=[]))
        self.hist_plot['pdf_source'] = ColumnDataSource(
            data=dict(x=[], y_pdf=[]))

        # get stats and gaus fit
        hist_stats_list = basic_stats([self.select_widget_hist.value],
                                      self.total_pandas_df)
        mu = hist_stats_list[0][self.select_widget_hist.value][0]
        sigma = hist_stats_list[0][self.select_widget_hist.value][2]
        gauss_dict = gaussian_fit(self.select_widget_hist.value,
                                  self.total_pandas_df)
        hist = gauss_dict['hist']
        edges = gauss_dict['edges']
        x = gauss_dict['x']
        pdf = gauss_dict['pdf']

        self.hist_plot['source'].data = {
            'hist': hist,
            'left_edge': edges[:-1],
            'right_edge': edges[1:]
        }
        self.hist_plot['pdf_source'].data = {'x': x, 'y_pdf': pdf}

        self.hist_plot['quad_glyph'] = Quad(top='hist',
                                            bottom=0,
                                            left='left_edge',
                                            right='right_edge')
        self.hist_plot['pdf_glyph'] = Line(x='x',
                                           y='y_pdf',
                                           line_color="#D95B43",
                                           line_width=8,
                                           line_alpha=0.7)

        self.hist_plot['object_figure'].add_glyph(self.hist_plot['source'],
                                                  self.hist_plot['quad_glyph'])
        self.hist_plot['object_figure'].add_glyph(self.hist_plot['pdf_source'],
                                                  self.hist_plot['pdf_glyph'])
        '''
        ----------------------------------------------------------------------------------------------------------
        Define Heatmap
        ----------------------------------------------------------------------------------------------------------
        '''

        self.heat_map = {}

        heat_frame = set_color(self.select_widget_1.value,
                               self.select_widget_2.value,
                               self.total_pandas_df)
        self.heat_map_source = ColumnDataSource(heat_frame)

        self.points_source = ColumnDataSource(
            data=dict(x=self.total_pandas_df[self.select_widget_1.value],
                      y=self.total_pandas_df[self.select_widget_2.value]))

        self.heat_map['plot_width'] = 610
        self.heat_map['plot_height'] = 400
        self.heat_map[
            'title'] = "Heat Map of " + self.select_widget_1.value + ' vs. ' + self.select_widget_2.value
        self.heat_map['object_figure'] = figure(
            width=self.heat_map['plot_width'],
            height=self.heat_map['plot_height'],
            title=self.heat_map['title'])

        colors = [
            '#FF0000', '#F2000D', '#E6001A', '#D90026', '#CC0033', '#BF0040',
            '#B2004C', '#A60059', '#990066', '#8C0073', '#800080', '#73008C',
            '#660099', '#5900A6', '#4D00B2', '#4000BF', '#3300CC', '#2600D9',
            '#1900E6', '#0D00F2', '#0000FF'
        ]

        # mapper is a transform required by bokeh to generate heatmaps
        mapper = LinearColorMapper(palette=colors,
                                   low=heat_frame['heat_values'].min(),
                                   high=heat_frame['heat_values'].max())

        self.heat_map['circle_glyph'] = Circle(x='x',
                                               y='y',
                                               size=1,
                                               line_color=None,
                                               fill_color='black')
        self.heat_map['rect_glyph'] = Rect(
            x="rect_x",
            y="rect_y",
            width=heat_frame['rect_width'].loc[0],
            height=heat_frame['rect_width'].loc[0],
            fill_color={
                'field': 'heat_values',
                'transform': mapper
            },
            line_color=None,
            fill_alpha=0.75)
        self.heat_map['object_figure'].add_glyph(self.points_source,
                                                 self.heat_map['circle_glyph'])
        self.heat_map['object_figure'].add_glyph(self.heat_map_source,
                                                 self.heat_map['rect_glyph'])

        self.heat_map['color_bar'] = ColorBar(
            color_mapper=mapper,
            major_label_text_font_size="5pt",
            ticker=BasicTicker(desired_num_ticks=len(colors)),
            label_standoff=6,
            border_line_color=None,
            location=(0, 0),
            background_fill_alpha=0.75)
        self.heat_map['object_figure'].add_layout(self.heat_map['color_bar'],
                                                  'left')

        WIDGETBOX_WIDTH = 610

        self.layout = layout([
            self.select_widget_1, self.select_widget_2, self.select_widget_hist
        ], [self.heat_map['object_figure'], self.hist_plot['object_figure']
            ], [self.x_range_slider, self.y_range_slider], [
                widgetbox(self.data_table_heat, width=WIDGETBOX_WIDTH),
                widgetbox(self.data_table_hist, width=WIDGETBOX_WIDTH)
            ])

        # unavoidable resize error forces two figures to be displayed at once rather that switch between
        # data can be updated easily but plot type should remain static
        # https://groups.google.com/a/continuum.io/forum/#!topic/bokeh/P2WPJ9an7IQ

        doc.add_root(self.layout)
        """
        --------------------------------------------------------------------
        Callbacks
        --------------------------------------------------------------------
        """
        def select_1_callback(attrname, old, new):

            self.points_source.data['x'] = self.total_pandas_df[new]

            self.data_table_heat.columns = [
                TableColumn(field="labels",
                            title="Statistics",
                            width=int(self.DATA_TABLE_COL_WIDTH * 0.6)),
                TableColumn(field="variable_1",
                            title=new,
                            width=self.DATA_TABLE_COL_WIDTH),
                TableColumn(field="variable_2",
                            title=self.select_widget_2.value,
                            width=self.DATA_TABLE_COL_WIDTH),
                TableColumn(field="variable_1_2",
                            title=new + ' and ' + self.select_widget_2.value,
                            width=int(self.DATA_TABLE_COL_WIDTH * 1.8))
            ]

            heat_frame = set_color(new, self.select_widget_2.value,
                                   self.total_pandas_df)

            self.heat_map_source.data['rect_x'] = heat_frame['rect_x']
            self.heat_map_source.data['rect_width'] = heat_frame['rect_width']
            self.heat_map_source.data['heat_values'] = heat_frame[
                'heat_values']

            self.data_table_heat.source = ColumnDataSource(
                fill_data_table([new, self.select_widget_2.value],
                                self.total_pandas_df))

            self.heat_map[
                'object_figure'].title.text = "Heat Map of " + new + ' vs. ' + self.select_widget_2.value

            self.x_range_slider.start = self.total_pandas_df[new].min()
            self.x_range_slider.end = self.total_pandas_df[new].max()
            self.x_range_slider.step = (self.total_pandas_df[new].max() -
                                        self.total_pandas_df[new].min()) / 100
            self.x_range_slider.value = (self.total_pandas_df[new].min(),
                                         self.total_pandas_df[new].max())

        self.select_widget_1.on_change('value', select_1_callback)

        def select_2_callback(attrname, old, new):

            self.points_source.data['y'] = self.total_pandas_df[new]

            self.data_table_heat.columns = [
                TableColumn(field="labels",
                            title="Statistics",
                            width=int(self.DATA_TABLE_COL_WIDTH * 0.6)),
                TableColumn(field="variable_1",
                            title=self.select_widget_1.value,
                            width=self.DATA_TABLE_COL_WIDTH),
                TableColumn(field="variable_2",
                            title=new,
                            width=self.DATA_TABLE_COL_WIDTH),
                TableColumn(field="variable_1_2",
                            title=self.select_widget_1.value + ' and ' + new,
                            width=int(self.DATA_TABLE_COL_WIDTH * 1.8))
            ]

            heat_frame = set_color(self.select_widget_1.value, new,
                                   self.total_pandas_df)

            self.heat_map_source.data['rect_y'] = heat_frame['rect_y']
            self.heat_map_source.data['rect_height'] = heat_frame[
                'rect_height']
            self.heat_map_source.data['heat_values'] = heat_frame[
                'heat_values']

            self.data_table_heat.source = ColumnDataSource(
                fill_data_table([new, self.select_widget_2.value],
                                self.total_pandas_df))

            self.heat_map[
                'object_figure'].title.text = "Heat Map of " + self.select_widget_1.value + ' vs. ' + new

            self.y_range_slider.start = self.total_pandas_df[new].min()
            self.y_range_slider.end = self.total_pandas_df[new].max()
            self.y_range_slider.step = (self.total_pandas_df[new].max() -
                                        self.total_pandas_df[new].min()) / 100
            self.y_range_slider.value = (self.total_pandas_df[new].min(),
                                         self.total_pandas_df[new].max())

        self.select_widget_2.on_change('value', select_2_callback)

        def select_hist_callback(attrname, old, new):
            # get stats and gaus fit
            hist_stats_list = basic_stats([new], self.total_pandas_df)
            mu = hist_stats_list[0][new][0]
            sigma = hist_stats_list[0][new][2]
            gauss_dict = gaussian_fit(new, self.total_pandas_df)
            hist = gauss_dict['hist']
            edges = gauss_dict['edges']
            x = gauss_dict['x']
            self.hist_plot['source'].data = {
                'hist': hist,
                'left_edge': edges[:-1],
                'right_edge': edges[1:]
            }

            if new not in self.sq_err_names:
                pdf = gauss_dict['pdf']
            else:
                pdf = chi_squared(new, self.total_pandas_df)

            self.hist_plot['pdf_source'].data = {'x': x, 'y_pdf': pdf}

            self.hist_plot[
                'object_figure'].title.text = "Gaussian Fit of " + new

            self.data_table_hist.source = ColumnDataSource(
                fill_data_table([new], self.total_pandas_df))
            self.data_table_hist.columns = [
                TableColumn(field="labels",
                            title="Statistics",
                            width=self.DATA_TABLE_COL_WIDTH),
                TableColumn(field="variable_1",
                            title=new,
                            width=self.DATA_TABLE_COL_WIDTH)
            ]

        self.select_widget_hist.on_change('value', select_hist_callback)

        def x_range_callback(attrname, old, new):
            self.heat_map['object_figure'].x_range.start = new[0]
            self.heat_map['object_figure'].x_range.end = new[1]

        def y_range_callback(attrname, old, new):
            self.heat_map['object_figure'].y_range.start = new[0]
            self.heat_map['object_figure'].y_range.end = new[1]

        self.x_range_slider.on_change('value', x_range_callback)
        self.y_range_slider.on_change('value', y_range_callback)
Ejemplo n.º 7
0
# set to roughly extent of points
x_range = Range1d(start=airports['x'].min() - 10000, end=airports['x'].max() + 10000)
y_range = Range1d(start=airports['y'].min() - 10000, end=airports['y'].max() + 10000)

# create plot and add tools
hover_tool = HoverTool(tooltips=[("Name", "@name"), ("Elevation", "@elevation (m)")])
p = Plot(x_range=x_range, y_range=y_range, plot_height=690, plot_width=990, title=title)
p.add_tools(ResizeTool(), WheelZoomTool(), PanTool(), BoxZoomTool(), hover_tool)
p.add_tile(tile_source)

# create point glyphs
point_options = {}
point_options['x'] = 'x'
point_options['y'] = 'y'
point_options['size'] = 9
point_options['fill_color'] = "#60ACA1"
point_options['line_color'] = "#D2C4C1"
point_options['line_width'] = 1.5
points_glyph = Circle(**point_options)
p.add_glyph(points_source, points_glyph)

doc = Document()
doc.add(p)

if __name__ == "__main__":
    filename = "airports_map.html"
    with open(filename, "w") as f:
        f.write(file_html(doc, INLINE, "Bokeh Airports Example"))
    print("Wrote %s" % filename)
    view(filename)
Ejemplo n.º 8
0
API_KEY = "GOOGLE_API_KEY"

map_options = GMapOptions(lat=15, lng=0, zoom=2)

plot = GMapPlot(
    plot_width=1000, plot_height=500,
    map_options=map_options, api_key=API_KEY, output_backend="webgl",
)

if plot.api_key == "GOOGLE_API_KEY":
    plot.add_layout(Label(x=500, y=320, x_units='screen', y_units='screen',
                          text='Replace GOOGLE_API_KEY with your own key',
                          text_color='red', text_align='center'))

plot.title.text = "Cities of the world with a population over 5,000 people."

circle = Circle(x="lng", y="lat", size=5, line_color=None, fill_color='firebrick', fill_alpha=0.2)
plot.add_glyph(ColumnDataSource(data), circle)
plot.add_tools(PanTool(), WheelZoomTool())

doc = Document()
doc.add_root(plot)

if __name__ == "__main__":
    doc.validate()
    filename = "maps_cities.html"
    with open(filename, "w") as f:
        f.write(file_html(doc, INLINE, "Google Maps - World cities Example"))
    print("Wrote %s" % filename)
    view(filename)
Ejemplo n.º 9
0
                    toolbar_sticky=False,
                    tools=TOOLS,
                    x_range=Range1d(cluster_hist["min_long"],
                                    cluster_hist["max_long"]),
                    y_range=Range1d(cluster_hist["min_lat"],
                                    cluster_hist["max_lat"]))

clusters = kmeans_viz.add_glyph(
    source,
    Ellipse(x="longitude",
            y="latitude",
            width="width",
            height="height",
            fill_color="LightSeaGreen"))
centroids = kmeans_viz.add_glyph(
    source, Circle(x="longitude", y="latitude", size=10, fill_alpha=1))
hover = kmeans_viz.select_one(HoverTool)
hover.point_policy = "follow_mouse"
hover.tooltips = [
    ("Count", "@count"),
    ("(Long, Lat)", "($x.2, $y.2)"),
    ("Width", "@width"),
    ("Height", "@height"),
]

iter_slider = Slider(start=0,
                     end=len(cluster_hist) - 5,
                     value=0,
                     step=1,
                     title="Iteration")
Ejemplo n.º 10
0
xdr = Range1d(start=-1.25, end=1.25)
ydr = Range1d(start=-1.25, end=1.25)
ds = ColumnDataSource(dict(dummy=[0]))

plot = Plot(title="Speedometer", x_range=xdr, y_range=ydr, plot_width=600, plot_height=600)

start_angle = pi + pi/4
end_angle = -pi/4

max_kmh = 250
max_mph = max_kmh*0.621371

major_step, minor_step = 25, 5

plot.add_glyph(ds, Circle(x=0, y=0, radius=1.00, fill_color="white", line_color="black"))
plot.add_glyph(ds, Circle(x=0, y=0, radius=0.05, fill_color="gray", line_color="black"))

plot.add_glyph(ds, Text(x=0, y=+0.15, angle=0, text=["km/h"], text_color="red", text_align="center", text_baseline="bottom", text_font_style="bold"))
plot.add_glyph(ds, Text(x=0, y=-0.15, angle=0, text=["mph"], text_color="blue", text_align="center", text_baseline="top", text_font_style="bold"))

def data(value):
    """Shorthand to override default units with "data", for e.g. `Ray.length`. """
    return dict(value=value, units="data")

def speed_to_angle(speed, units):
    max_speed = max_kmh if units == "kmh" else max_mph
    speed = min(max(speed, 0), max_speed)
    total_angle = start_angle - end_angle
    angle = total_angle*float(speed)/max_speed
    return start_angle - angle
Ejemplo n.º 11
0
        (x + 2 * e, y + 2 * e, 1, "green", "%s02" % n),
        (x + 3 * e, y + 3 * e, 1, "violet", "%s03" % n),
        (x + 4 * e, y + 4 * e, 1, "pink", "%s04" % n),
        (x + 5 * e, y + 5 * e, 1, "black", "%s05" % n),
        (x + 6 * e, y + 6 * e, 1, "gray", "%s06" % n),
        (x + 7 * e, y + 7 * e, 1, "olive", "%s07" % n),
        (x + 8 * e, y + 8 * e, 1, "yellow", "%s08" % n),
        (x + 9 * e, y + 9 * e, 1, "orange", "%s09" % n),
    ]
    f = lambda i: [t[i] for t in d]
    return dict(x=f(0), y=f(1), s=f(2), c=f(3), name=f(4))


ds1 = ColumnDataSource(data=fds(0, 0, 0.1, "c"))
cr1 = plot.add_glyph(
    ds1, Circle(x="x", y="y", radius="s", fill_color="c", line_color="c"))

ds2 = ColumnDataSource(data=fds(-5, 5, 0.5, "d"))
cr2 = plot.add_glyph(
    ds2, Circle(x="x", y="y", radius="s", fill_color="c", line_color="c"))
ln2 = plot.add_glyph(ds2, Line(x="x", y="y", line_width=3, line_color="red"))

ds3 = ColumnDataSource(data=fds(5, 5, 0.0, "e"))
cr3 = plot.add_glyph(
    ds3, Circle(x="x", y="y", radius="s", fill_color="c", line_color="c"))

tooltips = "<b>@name</b> = (@x{0.00}, @y{0.00})"

hover = HoverTool(tooltips=tooltips,
                  renderers=[cr1, cr2, ln2, cr3],
                  point_policy="follow_mouse")
Ejemplo n.º 12
0
Archivo: gauges.py Proyecto: xnx/bokeh
ydr = Range1d(start=-1.25, end=1.25)

plot = Plot(x_range=xdr, y_range=ydr, plot_width=600, plot_height=600)
plot.title.text = "Speedometer"
plot.toolbar_location = None

start_angle = pi + pi / 4
end_angle = -pi / 4

max_kmh = 250
max_mph = max_kmh * 0.621371

major_step, minor_step = 25, 5

plot.add_glyph(
    Circle(x=0, y=0, radius=1.00, fill_color="white", line_color="black"))
plot.add_glyph(
    Circle(x=0, y=0, radius=0.05, fill_color="gray", line_color="black"))

plot.add_glyph(
    Text(x=0,
         y=+0.15,
         text=["km/h"],
         text_color="red",
         text_align="center",
         text_baseline="bottom",
         text_font_style="bold"))
plot.add_glyph(
    Text(x=0,
         y=-0.15,
         text=["mph"],
Ejemplo n.º 13
0
                          zoom=13)

plot = GMapPlot(x_range=DataRange1d(),
                y_range=DataRange1d(),
                map_options=map_options,
                title="Washington, DC",
                plot_width=1280,
                plot_height=1280,
                responsive=True)

# Finally plot it
lines = MultiLine(xs="lons",
                  ys="lats",
                  line_alpha="line_alpha",
                  line_width="line_width",
                  line_color="red",
                  line_cap="round")
circle = Circle(x="lon",
                y="lat",
                size=10,
                fill_color="blue",
                fill_alpha=0.8,
                line_color=None)
plot.add_glyph(source, circle)
plot.add_glyph(line_source, lines)

plot.add_tools(PanTool(), WheelZoomTool(), BoxZoomTool(), hover, ResetTool(),
               UndoTool(), RedoTool(), PreviewSaveTool())
output_file("gmap_plot.html")
show(plot)
Ejemplo n.º 14
0
from bokeh.models.glyphs import Circle
from bokeh.models import Plot, ColumnDataSource, Range1d, Grid, DataRange1d
from bokeh.io import output_file, show

output_file("Line_from_models.html")

x = [1,2,3,4,5]
y= [6,7,8,9,10]

data = ColumnDataSource(dict(x=x,y=y))

p = Plot(x_range=DataRange1d(start=0,end=15),y_range=DataRange1d(start=0,end=15))

circle = Circle(x="x", y="y")

p.add_glyph(data,circle)

show(p)


Ejemplo n.º 15
0
def make_scatter_plot(source,
                      data,
                      country,
                      xname,
                      yname,
                      xax=False,
                      yax=False):
    # sets range and borders for plot
    xdr = DataRange1d(bounds=None)
    ydr = DataRange1d(bounds=None)
    mbl = 40 if yax else 0
    mbb = 40 if xax else 0
    plot = figure(x_range=xdr,
                  y_range=ydr,
                  background_fill_color="#efe8e2",
                  border_fill_color='white',
                  plot_width=200 + mbl,
                  plot_height=200 + mbb,
                  min_border_left=2 + mbl,
                  min_border_right=2,
                  min_border_top=2,
                  min_border_bottom=2 + mbb,
                  title=country)

    # plots points
    circle = Circle(x=xname,
                    y=yname,
                    fill_color="blue",
                    fill_alpha=0.2,
                    size=4,
                    line_color="blue")
    plot.add_glyph(source, circle)

    # calculates and plots regression line
    a, b, mse = make_regression_line(data, xname, yname)
    mse_mean = mse.mean()
    print(xname, yname, mse_mean)
    x = data[xname]
    plot.line(x, a * x + b, color='red')
    plot.axis.visible = False

    # makes axis according to place in matrix
    xticker = BasicTicker()
    if xax:
        xaxis = LinearAxis()
        xaxis.axis_label = xname
        plot.add_layout(xaxis, 'below')
        xticker = xaxis.ticker
    plot.add_layout(Grid(dimension=0, ticker=xticker))

    yticker = BasicTicker()
    if yax:
        yaxis = LinearAxis()
        yaxis.axis_label = yname
        yaxis.major_label_orientation = 'vertical'
        plot.add_layout(yaxis, 'left')
        yticker = yaxis.ticker

    plot.add_layout(Grid(dimension=1, ticker=yticker))

    return plot
Ejemplo n.º 16
0
ydr = DataRange1d()

plot = Plot(x_range=xdr, y_range=ydr)

circle = Circle(x="x", y="y", radius=0.2,
    # Set the fill color to be dependent on the "color" field of the
    # data source.  If the field is missing, then the default value is
    # used. Since no explicit default is provided, this picks up the
    # default in FillProps, which is "gray".
    fill_color="color",

    # Alternative to using fill_color with rgba values, you can also use
    # the fill_alpha setting to set the alpha values of your circle or
    # other glyphs. This can be set as a single value or powered by a
    # column in your data source. Uncomment the following line
    # to see the effect.
    # fill_alpha=0.2,

    # An alternative form that explicitly sets a default value:
    #fill_color={"default": "red", "field": "color"},

    # Note that line_color is set to a fixed value. This can be any of
    # the SVG named 147 colors, or a hex color string starting with "#",
    # or a string "rgb(r,g,b)" or "rgba(r,g,b,a)".
    # Any other string will be interpreted as a field name to look up
    # on the datasource.
    line_color="black")

plot.add_glyph(source, circle)

plot.add_layout(LinearAxis(), 'below')
Ejemplo n.º 17
0
    x_range=x_range, y_range=y_range,
    map_options=map_options,
    title="London Meetups"
)

source = ColumnDataSource(
    data=dict(
        lat=[51.49013, 51.50013, 51.51013],
        lon=[-0.130305, -0.126305, -0.120305],
        fill=['orange', 'blue', 'green'],
        name=['LondonDataScience', 'Spark', 'MachineLearning'],
        text=['Graph Data & Algorithms','Spark Internals','Deep Learning on Spark']
    )
)

circle = Circle(x="lon", y="lat", size=15, fill_color="fill", line_color=None)
plot.add_glyph(source, circle)

# TOOLS="pan,wheel_zoom,box_zoom,reset,hover,save"
pan = PanTool()
wheel_zoom = WheelZoomTool()
box_select = BoxSelectTool()
reset = ResetTool()
hover = HoverTool()
# save = SaveTool()

plot.add_tools(pan, wheel_zoom, box_select, reset, hover)
overlay = BoxSelectionOverlay(tool=box_select)
plot.add_layout(overlay)

hover = plot.select(dict(type=HoverTool))
Ejemplo n.º 18
0
def Unplanned_Circles(points, radius):
    circles = [Circle(p, radius) for p in points]
    return circles
def main(ini_path, show_flag=False, overwrite_flag=True):
    """Generate Bokeh figures

    Bokeh issues:
    Adjust y range based on non-muted data
        https://stackoverflow.com/questions/43620837/how-to-get-bokeh-to-dynamically-adjust-y-range-when-panning
    Linked interactive legends so that there is only one legend for the gridplot
    Maybe hide or mute QA values above max (instead of filtering them in advance)

    Args:
        ini_path (str):
        show_flag (bool): if True, show the figures in the browser.
            Default is False.
        overwrite_flag (bool): if True, overwrite existing tables.
            Default is True (for now)
    """
    logging.info('\nGenerate interactive timeseries figures')

    # Eventually read from INI
    plot_var_list = ['NDVI_TOA', 'ALBEDO_SUR', 'TS', 'NDWI_TOA', 'EVI_SUR']
    # plot_var_list = [
    #     'NDVI_TOA', 'ALBEDO_SUR', 'TS', 'NDWI_TOA',
    #     'CLOUD_SCORE', 'FMASK_PCT']
    output_folder = 'figures'

    # Read config file
    ini = inputs.read(ini_path)
    inputs.parse_section(ini, section='INPUTS')
    inputs.parse_section(ini, section='ZONAL_STATS')
    inputs.parse_section(ini, section='SUMMARY')
    inputs.parse_section(ini, section='FIGURES')
    inputs.parse_section(ini, section='BEAMER')

    # Hardcode GRIDMET month range to the water year
    ini['SUMMARY']['gridmet_start_month'] = 10
    ini['SUMMARY']['gridmet_end_month'] = 9

    # Start/end year
    year_list = list(range(
        ini['INPUTS']['start_year'], ini['INPUTS']['end_year'] + 1))
    month_list = list(utils.wrapped_range(
        ini['INPUTS']['start_month'], ini['INPUTS']['end_month'], 1, 12))
    doy_list = list(utils.wrapped_range(
        ini['INPUTS']['start_doy'], ini['INPUTS']['end_doy'], 1, 366))

    # GRIDMET month range (default to water year)
    gridmet_start_month = ini['SUMMARY']['gridmet_start_month']
    gridmet_end_month = ini['SUMMARY']['gridmet_end_month']
    gridmet_months = list(utils.month_range(
        gridmet_start_month, gridmet_end_month))
    logging.info('\nGridmet months: {}'.format(
        ', '.join(map(str, gridmet_months))))

    # Get ee features from shapefile
    zone_geom_list = gdc.shapefile_2_geom_list_func(
        ini['INPUTS']['zone_shp_path'], zone_field=ini['INPUTS']['zone_field'],
        reverse_flag=False)

    # Filter features by FID before merging geometries
    if ini['INPUTS']['fid_keep_list']:
        zone_geom_list = [
            zone_obj for zone_obj in zone_geom_list
            if zone_obj[0] in ini['INPUTS']['fid_keep_list']]
    if ini['INPUTS']['fid_skip_list']:
        zone_geom_list = [
            zone_obj for zone_obj in zone_geom_list
            if zone_obj[0] not in ini['INPUTS']['fid_skip_list']]

    # # Filter features by FID before merging geometries
    # if ini['INPUTS']['fid_keep_list']:
    #     landsat_df = landsat_df[landsat_df['ZONE_FID'].isin(
    #         ini['INPUTS']['fid_keep_list'])]
    # if ini['INPUTS']['fid_skip_list']:
    #     landsat_df = landsat_df[~landsat_df['ZONE_FID'].isin(
    #         ini['INPUTS']['fid_skip_list'])]

    logging.info('\nProcessing zones')
    for zone_fid, zone_name, zone_json in zone_geom_list:
        zone_name = zone_name.replace(' ', '_')
        logging.info('ZONE: {} (FID: {})'.format(zone_name, zone_fid))

        zone_stats_ws = os.path.join(
            ini['ZONAL_STATS']['output_ws'], zone_name)
        zone_figures_ws = os.path.join(
            ini['SUMMARY']['output_ws'], zone_name, output_folder)
        if not os.path.isdir(zone_stats_ws):
            logging.debug('  Folder {} does not exist, skipping'.format(
                zone_stats_ws))
            continue
        elif not os.path.isdir(zone_figures_ws):
            os.makedirs(zone_figures_ws)

        # Input paths
        landsat_daily_path = os.path.join(
            zone_stats_ws, '{}_landsat_daily.csv'.format(zone_name))
        gridmet_daily_path = os.path.join(
            zone_stats_ws, '{}_gridmet_daily.csv'.format(zone_name))
        gridmet_monthly_path = os.path.join(
            zone_stats_ws, '{}_gridmet_monthly.csv'.format(zone_name))
        if not os.path.isfile(landsat_daily_path):
            logging.error('  Landsat daily CSV does not exist, skipping zone')
            continue
        elif (not os.path.isfile(gridmet_daily_path) and
              not os.path.isfile(gridmet_monthly_path)):
            logging.error(
                '  GRIDMET daily or monthly CSV does not exist, skipping zone')
            continue
            # DEADBEEF - Eventually support generating only Landsat figures
            # logging.error(
            #     '  GRIDMET daily and/or monthly CSV files do not exist.\n'
            #     '  ETo and PPT will not be processed.')

        # Output paths
        output_doy_path = os.path.join(
            zone_figures_ws, '{}_timeseries_doy.html'.format(zone_name))
        output_date_path = os.path.join(
            zone_figures_ws, '{}_timeseries_date.html'.format(zone_name))

        logging.debug('  Reading Landsat CSV')
        landsat_df = pd.read_csv(landsat_daily_path)

        logging.debug('  Filtering Landsat dataframe')
        landsat_df = landsat_df[landsat_df['PIXEL_COUNT'] > 0]

        # QA field should have been written in zonal stats code
        # Eventually this block can be removed
        if 'QA' not in landsat_df.columns.values:
            landsat_df['QA'] = 0

        # # This assumes that there are L5/L8 images in the dataframe
        # if not landsat_df.empty:
        #     max_pixel_count = max(landsat_df['PIXEL_COUNT'])
        # else:
        #     max_pixel_count = 0

        if year_list:
            landsat_df = landsat_df[landsat_df['YEAR'].isin(year_list)]
        if month_list:
            landsat_df = landsat_df[landsat_df['MONTH'].isin(month_list)]
        if doy_list:
            landsat_df = landsat_df[landsat_df['DOY'].isin(doy_list)]

        # Assume the default is for these to be True and only filter if False
        if not ini['INPUTS']['landsat4_flag']:
            landsat_df = landsat_df[landsat_df['PLATFORM'] != 'LT04']
        if not ini['INPUTS']['landsat5_flag']:
            landsat_df = landsat_df[landsat_df['PLATFORM'] != 'LT05']
        if not ini['INPUTS']['landsat7_flag']:
            landsat_df = landsat_df[landsat_df['PLATFORM'] != 'LE07']
        if not ini['INPUTS']['landsat8_flag']:
            landsat_df = landsat_df[landsat_df['PLATFORM'] != 'LC08']

        if ini['INPUTS']['path_keep_list']:
            landsat_df = landsat_df[
                landsat_df['PATH'].isin(ini['INPUTS']['path_keep_list'])]
        if (ini['INPUTS']['row_keep_list'] and
                ini['INPUTS']['row_keep_list'] != ['XXX']):
            landsat_df = landsat_df[
                landsat_df['ROW'].isin(ini['INPUTS']['row_keep_list'])]

        if ini['INPUTS']['scene_id_keep_list']:
            # Replace XXX with primary ROW value for checking skip list SCENE_ID
            scene_id_df = pd.Series([
                s.replace('XXX', '{:03d}'.format(int(r)))
                for s, r in zip(landsat_df['SCENE_ID'], landsat_df['ROW'])])
            landsat_df = landsat_df[scene_id_df.isin(
                ini['INPUTS']['scene_id_keep_list']).values]
            # This won't work: SCENE_ID have XXX but scene_id_skip_list don't
            # landsat_df = landsat_df[landsat_df['SCENE_ID'].isin(
            #     ini['INPUTS']['scene_id_keep_list'])]
        if ini['INPUTS']['scene_id_skip_list']:
            # Replace XXX with primary ROW value for checking skip list SCENE_ID
            scene_id_df = pd.Series([
                s.replace('XXX', '{:03d}'.format(int(r)))
                for s, r in zip(landsat_df['SCENE_ID'], landsat_df['ROW'])])
            landsat_df = landsat_df[np.logical_not(scene_id_df.isin(
                ini['INPUTS']['scene_id_skip_list']).values)]
            # This won't work: SCENE_ID have XXX but scene_id_skip_list don't
            # landsat_df = landsat_df[np.logical_not(landsat_df['SCENE_ID'].isin(
            #     ini['INPUTS']['scene_id_skip_list']))]

        # Filter by QA/QC value
        if ini['SUMMARY']['max_qa'] >= 0 and not landsat_df.empty:
            logging.debug('    Maximum QA: {0}'.format(
                ini['SUMMARY']['max_qa']))
            landsat_df = landsat_df[
                landsat_df['QA'] <= ini['SUMMARY']['max_qa']]

        # Filter by average cloud score
        if ini['SUMMARY']['max_cloud_score'] < 100 and not landsat_df.empty:
            logging.debug('    Maximum cloud score: {0}'.format(
                ini['SUMMARY']['max_cloud_score']))
            landsat_df = landsat_df[
                landsat_df['CLOUD_SCORE'] <= ini['SUMMARY']['max_cloud_score']]

        # Filter by Fmask percentage
        if ini['SUMMARY']['max_fmask_pct'] < 100 and not landsat_df.empty:
            landsat_df['FMASK_PCT'] = 100 * (
                landsat_df['FMASK_COUNT'] / landsat_df['FMASK_TOTAL'])
            logging.debug('    Max Fmask threshold: {}'.format(
                ini['SUMMARY']['max_fmask_pct']))
            landsat_df = landsat_df[
                landsat_df['FMASK_PCT'] <= ini['SUMMARY']['max_fmask_pct']]

        # Filter low count SLC-off images
        if ini['SUMMARY']['min_slc_off_pct'] > 0 and not landsat_df.empty:
            logging.debug('    Mininum SLC-off threshold: {}%'.format(
                ini['SUMMARY']['min_slc_off_pct']))
            # logging.debug('    Maximum pixel count: {}'.format(
            #     max_pixel_count))
            slc_off_mask = (
                (landsat_df['PLATFORM'] == 'LE07') &
                ((landsat_df['YEAR'] >= 2004) |
                 ((landsat_df['YEAR'] == 2003) & (landsat_df['DOY'] > 151))))
            slc_off_pct = 100 * (landsat_df['PIXEL_COUNT'] / landsat_df['PIXEL_TOTAL'])
            # slc_off_pct = 100 * (landsat_df['PIXEL_COUNT'] / max_pixel_count)
            landsat_df = landsat_df[
                ((slc_off_pct >= ini['SUMMARY']['min_slc_off_pct']) & slc_off_mask) |
                (~slc_off_mask)]

        if landsat_df.empty:
            logging.error(
                '  Empty Landsat dataframe after filtering, skipping zone')
            continue

        # Aggregate GRIDMET (to water year)
        if os.path.isfile(gridmet_monthly_path):
            logging.debug('  Reading montly GRIDMET CSV')
            gridmet_df = pd.read_csv(gridmet_monthly_path)
        elif os.path.isfile(gridmet_daily_path):
            logging.debug('  Reading daily GRIDMET CSV')
            gridmet_df = pd.read_csv(gridmet_daily_path)

        logging.debug('  Computing GRIDMET summaries')
        # Summarize GRIDMET for target months year
        if (gridmet_start_month in [10, 11, 12] and
                gridmet_end_month in [10, 11, 12]):
            month_mask = (
                (gridmet_df['MONTH'] >= gridmet_start_month) &
                (gridmet_df['MONTH'] <= gridmet_end_month))
            gridmet_df.loc[month_mask, 'GROUP_YEAR'] = gridmet_df['YEAR'] + 1
        elif (gridmet_start_month in [10, 11, 12] and
              gridmet_end_month not in [10, 11, 12]):
            month_mask = gridmet_df['MONTH'] >= gridmet_start_month
            gridmet_df.loc[month_mask, 'GROUP_YEAR'] = gridmet_df['YEAR'] + 1
            month_mask = gridmet_df['MONTH'] <= gridmet_end_month
            gridmet_df.loc[month_mask, 'GROUP_YEAR'] = gridmet_df['YEAR']
        else:
            month_mask = (
                (gridmet_df['MONTH'] >= gridmet_start_month) &
                (gridmet_df['MONTH'] <= gridmet_end_month))
            gridmet_df.loc[month_mask, 'GROUP_YEAR'] = gridmet_df['YEAR']
        # GROUP_YEAR for rows not in the GRIDMET month range will be NAN
        gridmet_df = gridmet_df[~pd.isnull(gridmet_df['GROUP_YEAR'])]

        if year_list:
            gridmet_df = gridmet_df[gridmet_df['GROUP_YEAR'].isin(year_list)]

        if gridmet_df.empty:
            logging.error(
                '    Empty GRIDMET dataframe after filtering by year')
            continue

        # Group GRIDMET data by user specified range (default is water year)
        gridmet_group_df = gridmet_df \
            .groupby(['ZONE_NAME', 'ZONE_FID', 'GROUP_YEAR']) \
            .agg({'ETO': np.sum, 'PPT': np.sum}) \
            .reset_index() \
            .sort_values(by='GROUP_YEAR')
            # .rename(columns={'ETO': 'ETO', 'PPT': 'ETO'}) \
        # Rename wasn't working when chained...
        gridmet_group_df.rename(columns={'GROUP_YEAR': 'YEAR'}, inplace=True)
        gridmet_group_df['YEAR'] = gridmet_group_df['YEAR'].astype(int)

        # # Group GRIDMET data by month
        # gridmet_month_df = gridmet_df\
        #     .groupby(['ZONE_NAME', 'ZONE_FID', 'GROUP_YEAR', 'MONTH']) \
        #     .agg({'ETO': np.sum, 'PPT': np.sum}) \
        #     .reset_index() \
        #     .sort_values(by=['GROUP_YEAR', 'MONTH'])
        # gridmet_month_df.rename(columns={'GROUP_YEAR': 'YEAR'}, inplace=True)
        # # Rename monthly PPT columns
        # gridmet_month_df['MONTH'] = 'PPT_M' + gridmet_month_df['MONTH'].astype(str)
        # # Pivot rows up to separate columns
        # gridmet_month_df = gridmet_month_df.pivot_table(
        #     'PPT', ['ZONE_NAME', 'YEAR'], 'MONTH')
        # gridmet_month_df.reset_index(inplace=True)
        # columns = ['ZONE_NAME', 'YEAR'] + ['PPT_M{}'.format(m) for m in gridmet_months]
        # gridmet_month_df = gridmet_month_df[columns]
        # del gridmet_month_df.index.name

        # Merge Landsat and GRIDMET collections
        zone_df = landsat_df.merge(
            gridmet_group_df, on=['ZONE_NAME', 'ZONE_FID', 'YEAR'])
        if zone_df is None or zone_df.empty:
            logging.info('  Empty zone dataframe, not generating figures')
            continue

        # Compute ETg
        zone_df['ETG_MEAN'] = zone_df['ETSTAR_MEAN'] * (
            zone_df['ETO'] - zone_df['PPT'])
        zone_df['ETG_LPI'] = zone_df['ETSTAR_LPI'] * (
            zone_df['ETO'] - zone_df['PPT'])
        zone_df['ETG_UPI'] = zone_df['ETSTAR_UPI'] * (
            zone_df['ETO'] - zone_df['PPT'])
        zone_df['ETG_LCI'] = zone_df['ETSTAR_LCI'] * (
            zone_df['ETO'] - zone_df['PPT'])
        zone_df['ETG_UCI'] = zone_df['ETSTAR_UCI'] * (
            zone_df['ETO'] - zone_df['PPT'])

        # Compute ET
        zone_df['ET_MEAN'] = zone_df['ETG_MEAN'] + zone_df['PPT']
        zone_df['ET_LPI'] = zone_df['ETG_LPI'] + zone_df['PPT']
        zone_df['ET_UPI'] = zone_df['ETG_UPI'] + zone_df['PPT']
        zone_df['ET_LCI'] = zone_df['ETG_LCI'] + zone_df['PPT']
        zone_df['ET_UCI'] = zone_df['ETG_UCI'] + zone_df['PPT']



        # ORIGINAL PLOTTING CODE
        # Check that plot variables are present
        for plot_var in plot_var_list:
            if plot_var not in landsat_df.columns.values:
                logging.error(
                    '  The plotting variable {} does not exist in the '
                    'dataframe'.format(plot_var))
                sys.exit()

        # if ini['INPUTS']['scene_id_keep_list']:
        #     # Replace XXX with primary ROW value for checking skip list SCENE_ID
        #     scene_id_df = pd.Series([
        #         s.replace('XXX', '{:03d}'.format(int(r)))
        #         for s, r in zip(landsat_df['SCENE_ID'], landsat_df['ROW'])])
        #     landsat_df = landsat_df[scene_id_df.isin(
        #         ini['INPUTS']['scene_id_keep_list']).values]
        #     # This won't work: SCENE_ID have XXX but scene_id_skip_list don't
        #     # landsat_df = landsat_df[landsat_df['SCENE_ID'].isin(
        #     #     ini['INPUTS']['scene_id_keep_list'])]
        # if ini['INPUTS']['scene_id_skip_list']:
        #     # Replace XXX with primary ROW value for checking skip list SCENE_ID
        #     scene_id_df = pd.Series([
        #         s.replace('XXX', '{:03d}'.format(int(r)))
        #         for s, r in zip(landsat_df['SCENE_ID'], landsat_df['ROW'])])
        #     landsat_df = landsat_df[np.logical_not(scene_id_df.isin(
        #         ini['INPUTS']['scene_id_skip_list']).values)]
        #     # This won't work: SCENE_ID have XXX but scene_id_skip_list don't
        #     # landsat_df = landsat_df[np.logical_not(landsat_df['SCENE_ID'].isin(
        #     #     ini['INPUTS']['scene_id_skip_list']))]


        # Compute colors for each QA value
        logging.debug('  Building column data source')
        qa_values = sorted(list(set(zone_df['QA'].values)))
        colors = {
            qa: "#%02x%02x%02x" % (int(r), int(g), int(b))
            for qa, (r, g, b, _) in zip(
                qa_values,
                255 * cm.viridis(mpl.colors.Normalize()(qa_values)))
        }
        logging.debug('  QA values: {}'.format(
            ', '.join(map(str, qa_values))))

        # Unpack the data by QA type to support interactive legends
        sources = dict()
        # sources = defaultdict(dict)
        # platform_list = ['LT04', 'LT05', 'LE07', 'LC08']
        for qa_value in qa_values:
            # for platform in platform_list:
            # qa_df = zone_df[
            #     (zone_df['PLATFORM'] == platform) &
            #     (zone_df['QA'] == qa_value)]
            qa_df = zone_df[zone_df['QA'] == qa_value]
            qa_data = {
                'INDEX': list(range(len(qa_df.index))),
                'PLATFORM': qa_df['PLATFORM'],
                'DATE': pd.to_datetime(qa_df['DATE']),
                'TIME': pd.to_datetime(qa_df['DATE']).map(
                    lambda x: x.strftime('%Y-%m-%d')),
                'DOY': qa_df['DOY'].values,
                'QA': qa_df['QA'].values,
                'COLOR': [colors[qa] for qa in qa_df['QA'].values]
            }
            for plot_var in plot_var_list:
                if plot_var in qa_df.columns.values:
                    qa_data.update({plot_var: qa_df[plot_var].values})
            sources[qa_value] = bokeh.models.ColumnDataSource(qa_data)
            # sources[qa_value][platform] = bokeh.models.ColumnDataSource(
            #     qa_data)

        tooltips = [
            ("LANDSAT", "@PLATFORM"),
            ("DATE", "@TIME"),
            ("DOY", "@DOY")]
        # hover_tool = bokeh.models.HoverTool(tooltips=tooltips)
        # tools="xwheel_zoom,xpan,xbox_zoom,reset,box_select"
        # tools = [
        #     hover_tool,
        #     bokeh.models.WheelZoomTool(dimensions='width'),
        #     bokeh.models.PanTool(dimensions='width'),
        #     bokeh.models.BoxZoomTool(dimensions='width'),
        #     bokeh.models.ResetTool(),
        #     bokeh.models.BoxSelectTool()]

        # Selection
        hover_circle = Circle(
            fill_color='#ff0000', line_color='#ff0000')
        selected_circle = Circle(
            fill_color='COLOR', line_color='COLOR')
        nonselected_circle = Circle(
            fill_color='#aaaaaa', line_color='#aaaaaa')

        # Plot the data by DOY
        logging.debug('  Building DOY timeseries figure')
        if os.path.isfile(output_doy_path):
            os.remove(output_doy_path)
        output_file(output_doy_path, title=zone_name)

        figure_args = dict(
            plot_width=750, plot_height=250, title=None,
            tools="xwheel_zoom,xpan,xbox_zoom,reset,box_select",
            # tools="xwheel_zoom,xpan,xbox_zoom,reset,tap",
            active_scroll="xwheel_zoom")
        plot_args = dict(
            size=4, alpha=0.9, color='COLOR')
        if ini['SUMMARY']['max_qa'] > 0:
            plot_args['legend'] = 'QA'

        figures = []
        for plot_i, plot_var in enumerate(plot_var_list):
            if plot_i == 0:
                f = figure(
                    # x_range=bokeh.models.Range1d(1, 366, bounds=(1, 366)),
                    y_axis_label=plot_var, **figure_args)
            else:
                f = figure(
                    x_range=f.x_range, y_axis_label=plot_var, **figure_args)

            # # Add each QA level as a separate object
            # for qa, platform_sources in sorted(sources.items()):
            #     for platform, source in platform_sources.items():
            #         if platform == 'LT05':
            #             r = f.triangle(
            #                 'DOY', plot_var, source=source, **plot_args)
            #         elif platform == 'LE07':
            #             r = f.square(
            #                 'DOY', plot_var, source=source, **plot_args)
            #         elif platform == 'LC08':
            #             r = f.circle(
            #                 'DOY', plot_var, source=source, **plot_args)
            #         else:
            #             r = f.diamond(
            #                 'DOY', plot_var, source=source, **plot_args)
            #         r.hover_glyph = hover_circle
            #         r.selection_glyph = selected_circle
            #         r.nonselection_glyph = nonselected_circle
            #         r.muted_glyph = nonselected_circle
            #         hover_tool.renderers.append(r)

            # Add each QA level as a separate object
            for qa, source in sorted(sources.items()):
                r = f.circle('DOY', plot_var, source=source, **plot_args)
                r.hover_glyph = hover_circle
                r.selection_glyph = selected_circle
                r.nonselection_glyph = nonselected_circle
                r.muted_glyph = nonselected_circle

                # # DEADBEEF - This will display high QA points as muted
                # if qa > ini['SUMMARY']['max_qa']:
                #     r.muted = True
                #     # r.visible = False

            f.add_tools(bokeh.models.HoverTool(tooltips=tooltips))

            # if ini['SUMMARY']['max_qa'] > 0:
            f.legend.location = "top_left"
            f.legend.click_policy = "hide"
            # f.legend.click_policy = "mute"
            f.legend.orientation = "horizontal"

            figures.append(f)
        del f

        # Try to not allow more than 4 plots in a column
        p = gridplot(
            figures, ncols=len(plot_var_list) // 3,
            sizing_mode='stretch_both')

        if show_flag:
            show(p)
        save(p)


        # Plot the data by DATE
        logging.debug('  Building date timeseries figure')
        if os.path.isfile(output_date_path):
            os.remove(output_date_path)
        output_file(output_date_path, title=zone_name)

        figure_args = dict(
            plot_width=750, plot_height=250, title=None,
            tools="xwheel_zoom,xpan,xbox_zoom,reset,box_select",
            # tools="xwheel_zoom,xpan,xbox_zoom,reset,tap",
            active_scroll="xwheel_zoom",
            x_axis_type="datetime",)
        plot_args = dict(
            size=4, alpha=0.9, color='COLOR')
        if ini['SUMMARY']['max_qa'] > 0:
            plot_args['legend'] = 'QA'

        figures = []
        for plot_i, plot_var in enumerate(plot_var_list):
            if plot_i == 0:
                f = figure(
                    # x_range=bokeh.models.Range1d(x_limit[0], x_limit[1], bounds=x_limit),
                    y_axis_label=plot_var, **figure_args)
            else:
                f = figure(
                    x_range=f.x_range, y_axis_label=plot_var, **figure_args)

            if plot_var == 'TS':
                f.y_range.bounds = (270, None)

            # # Add each QA level as a separate object
            # for qa, platform_sources in sorted(sources.items()):
            #     for platform, source in sorted(platform_sources.items()):
            #         if platform == 'LT05':
            #             r = f.triangle(
            #                 'DATE', plot_var, source=source, **plot_args)
            #         elif platform == 'LE07':
            #             r = f.square(
            #                 'DATE', plot_var, source=source, **plot_args)
            #         elif platform == 'LC08':
            #             r = f.circle(
            #                 'DATE', plot_var, source=source, **plot_args)
            #         else:
            #             r = f.diamond(
            #                 'DATE', plot_var, source=source, **plot_args)
            #         r.hover_glyph = hover_circle
            #         r.selection_glyph = selected_circle
            #         r.nonselection_glyph = nonselected_circle
            #         r.muted_glyph = nonselected_circle
            #         hover_tool.renderers.append(r)

            # Add each QA level as a separate object
            for qa, source in sorted(sources.items()):
                r = f.circle('DATE', plot_var, source=source, **plot_args)
                r.hover_glyph = hover_circle
                r.selection_glyph = selected_circle
                r.nonselection_glyph = nonselected_circle
                r.muted_glyph = nonselected_circle

                # # DEADBEEF - This will display high QA points as muted
                # if qa > ini['SUMMARY']['max_qa']:
                #     r.muted = True
                #     # r.visible = False
            f.add_tools(bokeh.models.HoverTool(tooltips=tooltips))

            # if ini['SUMMARY']['max_qa'] > 0:
            f.legend.location = "top_left"
            f.legend.click_policy = "hide"
            # f.legend.click_policy = "mute"
            f.legend.orientation = "horizontal"

            figures.append(f)
        del f

        # Try to not allow more than 4 plots in a column
        p = gridplot(
            figures, ncols=len(plot_var_list) // 3,
            sizing_mode='stretch_both')

        if show_flag:
            show(p)
        save(p)

        # Pause after each iteration if show is True
        if show_flag:
            input('Press ENTER to continue')
Ejemplo n.º 20
0
colormap = {'setosa': 'red', 'versicolor': 'green', 'virginica': 'blue'}

flowers['color'] = flowers['species'].map(lambda x: colormap[x])

source = ColumnDataSource(data=dict(petal_length=flowers['petal_length'],
                                    petal_width=flowers['petal_width'],
                                    sepal_length=flowers['sepal_length'],
                                    sepal_width=flowers['sepal_width'],
                                    color=flowers['color']))

plot = Plot(plot_width=800, plot_height=400)
plot.title.text = "Iris Data"

circle = Circle(x="petal_length",
                y="petal_width",
                size=10,
                fill_color="color",
                fill_alpha=0.2,
                line_color="color")
plot.add_glyph(source, circle)

xaxis = LinearAxis(axis_label="petal length", major_tick_in=0)
plot.add_layout(xaxis, 'below')

yaxis = LinearAxis(axis_label="petal width", major_tick_in=0)
plot.add_layout(yaxis, 'left')

plot.add_layout(Grid(dimension=0, ticker=xaxis.ticker))
plot.add_layout(Grid(dimension=1, ticker=yaxis.ticker))

plot.add_tools(PanTool(), WheelZoomTool())
Ejemplo n.º 21
0
def survey():
    if request.method == "GET":
        return render_template('survey.html')
 
    else: #Request was a POST
        
        user_input = pd.DataFrame(data=[[request.form['Average_Sleep'],
                                         request.form['Sex'],
                                         request.form['Marital'],
                                         request.form['Employment'],
                                         request.form['Physical_QoL'],
                                         request.form['Mental_QoL'],
                                         request.form['Physical_Activity'],
                                         request.form['Race'],
                                         request.form['Age'],
                                         request.form['Weight'], 
                                         request.form['Height_feet'],
                                         request.form['Height_inches'],                                           
                                         request.form['Education'],
                                         request.form['Income'],
                                         request.form['Soda_Consumption'], 
                                         request.form['Sugary_Drink_Consumption'],                                          
                                         request.form['ALCDAY5'], 
                                         request.form['AVEDRNK2'],
                                         request.form['Smoker_Status'], 
                                         request.form['Marijuana_Use'],   
                                         request.form['Calorie_Informed_Choices'], 
                                         request.form['HeartAttackOutcomeDummy'],
                                         request.form['AnginaCoronaryHDOutcomeDummy'], 
                                         request.form['StrokeOutcomeDummy'],
                                         request.form['AsthmaLifetimeOutcomeDummy'], 
                                         request.form['SkinCancerOutcomeDummy'],
                                         request.form['OtherCancerOutcomeDummy'], 
                                         request.form['COPDEmphysemaChronicBroncOutcomeDummy'],
                                         request.form['ArthritisOutcomeDummy'], 
                                         request.form['DepressionOutcomeDummy'], 
                                         request.form['KidneyDiseaseOutcomeDummy'], 
                                         request.form['Life_Satisfaction']]],                        
                    columns=['Average Sleep', 'Sex', 'Marital', 'Employment', 
                             'Physical QoL', 'Mental QoL', 'Physical Activity',
                             'Race', 'Age', 'Weight', 'Height_feet','Height_inches',
                             'Education', 'Income', 'Soda Consumption', 
                             'Sugary Drink Consumption', 
                             'Alcohol Days', 'Alcohol Drinks','Smoker Status',
                             'Marijuana Use', 'Calorie Informed Choices', 
                             'HeartAttackOutcomeDummy', 'AnginaCoronaryHDOutcomeDummy', 
                             'StrokeOutcomeDummy', 'AsthmaLifetimeOutcomeDummy',
                             'SkinCancerOutcomeDummy', 'OtherCancerOutcomeDummy', 
                             'COPDEmphysemaChronicBroncOutcomeDummy',
                             'ArthritisOutcomeDummy', 'DepressionOutcomeDummy', 
                             'KidneyDiseaseOutcomeDummy', 'Life Satisfaction'], dtype=np.float)#.astype(float)
        
        
        #return model1.predict(transform(user_input))
       # print(user_input.values)
        prediction=model1.predict_proba(transform(user_input).values)[-1][-1]
        
        def risk_category(risk_probability):
            """
            Takes a risk probability (e.g., "prediction") as a float and returns
            the corresponding risk level category as a string.
            
            @risk_probably: float
            @reutnr: str
            """
            if risk_probability < 0.25:
                return "Low Risk"
            if risk_probability >= 0.25 and risk_probability < 0.50:
                return "Low Moderate Risk"
            if risk_probability >=.50 and risk_probability < 0.75:
                return "High Moderate Risk"
            else:
                return "High Risk"
            
        def adjusted_risk(user_input): #Need to test this function
            """
            Takes DataFrame of user_input, transforms it, and calculates
            adjusted predicted risk based on changes to modifible health
            behaviors. Returns a new adjusted probability (float) and
            the list of suggested health behavior changes contributing
            to the reduced risk.
            
            @user_input: DataFrame
            @return: float, list<str>
            """
            transformed_df = transform(user_input)
            
            suggested_modifications = [] #Append to suggested modification list
            
            #Sets sleep to average sleep recommended for adults 18 - 64 yrs 
            #(7-9 hrs), adults 65+ (7-8 hrs) by National Sleep Foundation
            if transformed_df['Average Sleep'].values[0] < 7.0 or transformed_df['Average Sleep'].values[0] > 9.0:
                orig_sleep = transformed_df['Average Sleep'].values[0]
                transformed_df['Average Sleep'].values[0] = 8.0
                suggested_modifications.append("Get an average of 8.0 hours of sleep per night instead of "+ str(orig_sleep)+ " hours.")
            
            #Sets Physical Activity to had physical activity in past 30 days
            if transformed_df['Physical Activity'].values[0] == 0:
                transformed_df['Physical Activity'].values[0] = 1
                suggested_modifications.append("Incorporate a regular exercise routine into your schedule – this could be as simple as walking versus driving to the corner store or  taking the stairs instead of the elevator.")
            
            #Sets Obese -> Overweight and Overweight -> Normal weight
            if transformed_df['BMI'].values[0] == 3.0 or transformed_df['BMI'].values[0] == 4.0:
                orig_BMI = transformed_df['BMI'].values[0]
                transformed_df['BMI'].values[0] = transformed_df['BMI'].values[0] - 1.0
                
                if orig_BMI == 3.0:
                    orig_BMI = "Overweight (BMI: 25 –  < 30)"
                    suggested_BMI = "Normal Weight (BMI: 18.5 - < 25)"
                    suggested_modifications.append("Reduce BMI through gradual, healthy weight loss from "+ orig_BMI+ " to "+ suggested_BMI+ ".")
                    
                if orig_BMI == 4.0:
                    orig_BMI = "Obese (BMI: >= 30)"
                    suggested_BMI = "Overweight (BMI: 25 –  < 30)"
                    suggested_modifications.append("Reduce BMI through gradual, healthy weight loss from "+ orig_BMI+ " to "+ suggested_BMI+ ".")
            
            #Sets Underweight -> Normal weight
            if transformed_df['BMI'].values[0] == 1:
                transformed_df['BMI'].values[0] = transformed_df['BMI'].values[0] + 1.0
                orig_BMI = "Underweight (BMI: < 18.5)"
                suggested_BMI = "Normal Weight (BMI: 18.5 - < 25)"
                suggested_modifications.append("Reduce BMI through gradual, healthy weight loss from "+ orig_BMI+ " to "+ suggested_BMI+ ".")
            
            #Sets Daily smokers -> Occasional smokers and Occasional smokers -> Former smokers  
            if transformed_df['Smoker Status'].values[0] == 1.0 or transformed_df['Smoker Status'].values[0] == 2.0:
                orig_smoker = transformed_df['Smoker Status'].values[0]
                transformed_df['Smoker Status'].values[0] = transformed_df['Smoker Status'].values[0] + 1.0
                
                if orig_smoker == 1.0:
                    orig_smoker = "smoking every day"
                    suggested_smoker = "smoking some days"
                    suggested_modifications.append("Reduce smoking frequency from "+orig_smoker+" to "+suggested_smoker+ ".")
                    
                if orig_smoker == 2.0:
                    orig_smoker = "smoking some days"
                    suggested_smoker = "former smoker (quit)"
                    suggested_modifications.append("Reduce smoking frequency from "+orig_smoker+" to "+suggested_smoker+ ".")
            
            #Reduces weekly alcohol consumption by 25% (cutoff arbitrary)
            if transformed_df['Alcohol Consumption'].values[0] >= 1.0:
                orig_alcohol = transformed_df['Alcohol Consumption'].values[0]
                suggested_alcohol = transformed_df['Alcohol Consumption'].values[0]*0.25
                transformed_df['Alcohol Consumption'].values[0] = transformed_df['Alcohol Consumption'].values[0]*0.25
                suggested_modifications.append("Reduce average weekly alcohol consumption from " +str(orig_alcohol)+ " drink(s) per week to " +str(suggested_alcohol)+ " drink(s) per week.")
                
            #Sets 1-13 days poor Mental QoL -> 0 days and 14+ poor days -> 1-13 days
            if transformed_df['Mental QoL'].values[0] == 2.0 or transformed_df['Mental QoL'].values[0] == 3.0:
                #orig_mental = transformed_df['Mental QoL'].values[0]
                transformed_df['Mental QoL'].values[0] = transformed_df['Mental QoL'].values[0] - 1.0
                suggested_modifications.append("Consider consulting a psychologist/psychiatrist to learn tools for coping with stress and emotional difficulties in order to improve your mental health.")
                    
                
            return (transformed_df, suggested_modifications)
          
        adjusted_prediction=model1.predict_proba(adjusted_risk(user_input)[0].values)[-1][-1]
        
        modification_list=adjusted_risk(user_input)[1]
        
                
#Gauge Chart Rendering        
        xdr = Range1d(start=-1.25, end=1.25)
        ydr = Range1d(start=-1.25, end=1.25)
        
        plot = Plot(x_range=xdr, y_range=ydr, plot_width=500, plot_height=500)
        plot.title.text = "Predicted Diabetes Risk"
        plot.toolbar_location = None
        
        start_angle = pi + pi/4
        end_angle = -pi/4
        
        max_kmh = 1.0
        max_mph = 1.0
        
        major_step, minor_step = .25, .05
        
        plot.add_glyph(Circle(x=0, y=0, radius=1.00, fill_color="white", line_color="black"))
        plot.add_glyph(Circle(x=0, y=0, radius=0.05, fill_color="gray", line_color="black"))
        
        plot.add_glyph(Text(x=0, y=+0.15, text=["Current Risk Probability"], text_color="red", text_align="center", text_baseline="bottom", text_font_style="bold"))
        plot.add_glyph(Text(x=0, y=-0.15, text=["Adjusted Risk Probability"], text_color="blue", text_align="center", text_baseline="top", text_font_style="bold"))
        
        def data(value):
            """Shorthand to override default units with "data", for e.g. `Ray.length`. """
            return dict(value=value, units="data")
        
        def speed_to_angle(speed, units):
            max_speed = max_kmh
            speed = min(max(speed, 0), max_speed)
            total_angle = start_angle - end_angle
            angle = total_angle*float(speed)/max_speed
            return start_angle - angle
        
        def add_needle(speed, units, color_choice, line_weight):
            angle = speed_to_angle(speed, units)
            plot.add_glyph(Ray(x=0, y=0, length=data(0.75), angle=angle,    line_color=color_choice, line_width=line_weight))
            plot.add_glyph(Ray(x=0, y=0, length=data(0.10), angle=angle-pi, line_color=color_choice, line_width=line_weight))
        
        def polar_to_cartesian(r, alpha):
            return r*cos(alpha), r*sin(alpha)
        
        def add_gauge(radius, max_value, length, direction, color, major_step, minor_step):
            major_angles, minor_angles = [], []
            major_labels, minor_labels = [], []
        
            total_angle = start_angle - end_angle
        
            major_angle_step = float(major_step)/max_value*total_angle
            minor_angle_step = float(minor_step)/max_value*total_angle
        
            major_angle = 0
        
            while major_angle <= total_angle:
                major_angles.append(start_angle - major_angle)
                major_angle += major_angle_step
        
            minor_angle = 0
        
            while minor_angle <= total_angle:
                minor_angles.append(start_angle - minor_angle)
                minor_angle += minor_angle_step
        
            major_labels = [ major_step*i for i, _ in enumerate(major_angles) ]
            minor_labels = [ minor_step*i for i, _ in enumerate(minor_angles) ]
        
            n = major_step/minor_step
            minor_angles = [ x for i, x in enumerate(minor_angles) if i % n != 0 ]
            minor_labels = [ x for i, x in enumerate(minor_labels) if i % n != 0 ]
        
            glyph = Arc(x=0, y=0, radius=radius, start_angle=start_angle, end_angle=end_angle, direction="clock", line_color=color, line_width=2)
            plot.add_glyph(glyph)
        
            rotation = 0 if direction == 1 else -pi
        
            x, y = zip(*[ polar_to_cartesian(radius, angle) for angle in major_angles ])
            angles = [ angle + rotation for angle in major_angles ]
            source = ColumnDataSource(dict(x=x, y=y, angle=angles))
        
            glyph = Ray(x="x", y="y", length=data(length), angle="angle", line_color=color, line_width=2)
            plot.add_glyph(source, glyph)
        
            x, y = zip(*[ polar_to_cartesian(radius, angle) for angle in minor_angles ])
            angles = [ angle + rotation for angle in minor_angles ]
            source = ColumnDataSource(dict(x=x, y=y, angle=angles))
        
            glyph = Ray(x="x", y="y", length=data(length/2), angle="angle", line_color=color, line_width=1)
            plot.add_glyph(source, glyph)
        
            x, y = zip(*[ polar_to_cartesian(radius+2*length*direction, angle) for angle in major_angles ])
            text_angles = [ angle - pi/2 for angle in major_angles ]
            source = ColumnDataSource(dict(x=x, y=y, angle=text_angles, text=major_labels))
        
            glyph = Text(x="x", y="y", angle="angle", text="text", text_align="center", text_baseline="middle")
            plot.add_glyph(source, glyph)
        
        add_gauge(0.75, max_kmh, 0.05, +1, "red", major_step, minor_step)
        add_gauge(0.70, max_mph, 0.05, -1, "blue", major_step, minor_step)
        
        add_needle(prediction, "Current Risk", "red", 6)
        add_needle(adjusted_prediction, "Adjusted Risk", "blue", 3)
        
        script, div = components(plot)
        
        return render_template('orig_output.html', script=script, div=div, prediction=prediction, adjusted_prediction=adjusted_prediction, risk_level=risk_category(prediction), adjusted_risk_level=risk_category(adjusted_prediction), modifications=modification_list)
Ejemplo n.º 22
0
HEIGHT = 400
plot = Plot(x_range=xdr,
            y_range=ydr,
            min_border=50,
            plot_width=1000,
            plot_height=HEIGHT)

line_glyph = Line(x="x",
                  y="y",
                  line_color="navy",
                  line_width=2,
                  line_dash="dashed")
line = plot.add_glyph(source, line_glyph)
circle = Circle(x="x",
                y="y2",
                size=6,
                line_color="red",
                fill_color="orange",
                fill_alpha=0.6)
circle = plot.add_glyph(source, circle)

pan = PanTool()
wheel_zoom = WheelZoomTool()
preview_save = SaveTool()

plot.add_tools(pan, wheel_zoom, preview_save)

# Add axes (Note it's important to add these before adding legends in side panels)
plot.add_layout(LinearAxis(), 'below')
plot.add_layout(LinearAxis(), 'left')
#plot.add_layout(LinearAxis(), 'right') - Due to a bug cannot have two things on the right side
Ejemplo n.º 23
0
session.use_doc('glyph2_server')
session.load_document(document)

x = arange(-2*pi, 2*pi, 0.1)
y = sin(x)
r = (cos(x)+1) * 6 + 6

source = ColumnDataSource(data=dict(x=x, y=y, r=r))

xdr = DataRange1d()
ydr = DataRange1d()

plot = Plot(x_range=xdr, y_range=ydr, min_border=80)

circle = Circle(
    x="x", y="y", size="r",
    fill_color="red", line_color="black"
)
plot.add_glyph(source, circle)

xaxis = LinearAxis()
plot.add_layout(xaxis, 'below')

yaxis = LinearAxis()
plot.add_layout(yaxis, 'left')

plot.add_layout(Grid(dimension=0, ticker=xaxis.ticker))
plot.add_layout(Grid(dimension=1, ticker=yaxis.ticker))

plot.add_tools(PanTool(), WheelZoomTool())

document.add(plot)
Ejemplo n.º 24
0
    def plot_system_metrics(self):

        self.figures = []
        x_range = None
        # iterate over metrics e.g. cpu usage, gpu usage, i/o reads and writes etc
        # create a histogram per dimension and event
        for dimension in self.filtered_dimensions:
            self.sources[dimension] = {}
            for event in self.filtered_events:
                if event in self.system_metrics[dimension]:

                    values = self.system_metrics[dimension][event]
                    values = values[values[:, 0].argsort()]
                    # set y ranges for cpu and gpu which are measured in percent
                    if "Utilization" in dimension or "Memory" in dimension:
                        y_range = (0, 102)
                    else:
                        y_range = (
                            np.min(values[-self.width:, 1]),
                            np.max(values[-self.width:, 1]),
                        )

                    # create figure: each system metric has its own figure

                    if x_range == None:
                        plot = figure(
                            plot_height=200,
                            plot_width=1000,
                            x_range=(values[-self.width, 0], values[-1, 0]),
                            y_range=y_range,
                            tools=
                            "crosshair,xbox_select,pan,reset,save,xwheel_zoom",
                        )
                        x_range = plot.x_range
                    else:
                        plot = figure(
                            plot_height=200,
                            plot_width=1000,
                            x_range=x_range,
                            y_range=y_range,
                            tools=
                            "crosshair,xbox_select,pan,reset,save,xwheel_zoom",
                        )

                    plot.xgrid.visible = False
                    plot.ygrid.visible = False

                    # create line chart for system metric
                    source = ColumnDataSource(
                        data=dict(x=values[:, 0], y=values[:, 1]))

                    callback = CustomJS(
                        args=dict(s1=source, div=self.div),
                        code="""
                            console.log('Running CustomJS callback now.');
                            var inds = s1.selected.indices;
                            console.log(inds);
                            var line = "<span style=float:left;clear:left;font_size=13px><b> Selected index range: [" + Math.min.apply(Math,inds) + "," + Math.max.apply(Math,inds) + "]</b></span>\\n";
                            console.log(line)
                            var text = div.text.concat(line);
                            var lines = text.split("\\n")
                            if (lines.length > 35)
                                lines.shift();
                            div.text = lines.join("\\n");""",
                    )

                    plot.js_on_event("selectiongeometry", callback)

                    line = Line(x="x", y="y", line_color="blue")
                    circle = Circle(x="x", y="y", fill_alpha=0, line_width=0)
                    p = plot.add_glyph(source, line)
                    p = plot.add_glyph(source, circle)

                    # create tooltip for hover tool
                    hover = HoverTool(renderers=[p],
                                      tooltips=[("index", "$index"),
                                                ("(x,y)", "($x, $y)")])

                    plot.xaxis.axis_label = "Time in ms"
                    plot.yaxis.axis_label = dimension + "_" + event
                    plot.add_tools(hover)

                    # store figure and datasource
                    self.figures.append(plot)
                    self.sources[dimension][event] = source

        return self.figures
Ejemplo n.º 25
0
def index():
    if request.method == 'GET':

        return render_template('index.html')
    else:
        if request.form.get("address"):
            app.address = request.form['address']
        else:
            app.address = False
        app.dayofweek = request.form['dayofweek']
        app.direction = request.form['direction']
        app.timeofday = int(request.form['timeofday'])
        app.ampm = request.form['ampm']

        if app.address == False:
            app.msg = 'You must enter your desired address.'
            return render_template('error_page.html', msg=app.msg)

        params = {'address': str(app.address)}
        r = (requests.get('https://maps.googleapis.com/maps/api/geocode/json',
                          params=params)).json()

        app.formatted_address = r['results'][0]['formatted_address']
        my_latitude = r['results'][0]['geometry']['location']['lat']
        my_longitude = r['results'][0]['geometry']['location']['lng']

        x_min = -74.293396  #longitude  SW: 40.481965, -74.293396 NE:40.911486, -73.733866
        x_max = -73.733866  #longitude
        y_min = 40.481965  #latitude
        y_max = 40.911486  #latitude
        mesh_space = 0.01

        if my_latitude < y_min or my_latitude >= y_max or my_longitude < x_min or my_longitude >= x_max or app.address == "" or app.address == False:
            app.msg = 'The address you entered is outside the boundaries of NYC.'
            return render_template('error_page.html', msg=app.msg)

        centers_x, centers_rx = np.linspace(x_min,
                                            x_max,
                                            (x_max - x_min) / mesh_space,
                                            retstep="True")
        centers_y, centers_ry = np.linspace(y_min,
                                            y_max,
                                            (y_max - y_min) / mesh_space,
                                            retstep="True")

        my_center_id = get_center_id(my_longitude,
                                     my_latitude,
                                     x_min=x_min,
                                     x_max=x_max,
                                     y_min=y_min,
                                     y_max=y_max,
                                     mesh_space=mesh_space)

        input = open(
            '../network_mesh_space=0.01_day=' + str(app.dayofweek) + '.pickle',
            'rb')
        network_df = pickle.load(input)
        input.close()

        input = open(
            '../main_data_mesh_space=0.01_day=' + str(app.dayofweek) +
            '.pickle', 'rb')
        data_df = pickle.load(input)
        input.close()

        ##################################################
        ########Bokeh MAP block##############################
        x_range = Range1d()
        y_range = Range1d()

        # JSON style string taken from: https://snazzymaps.com/style/1/pale-dawn
        map_options = GMapOptions(lat=my_latitude,
                                  lng=my_longitude,
                                  map_type="roadmap",
                                  zoom=13,
                                  styles="""
        [{"featureType":"administrative","elementType":"all","stylers":[{"visibility":"on"},{"lightness":33}]},
        {"featureType":"landscape","elementType":"all","stylers":[{"color":"#f2e5d4"}]},
        {"featureType":"poi.park","elementType":"geometry","stylers":[{"color":"#c5dac6"}]},
        {"featureType":"poi.park","elementType":"labels","stylers":[{"visibility":"on"},{"lightness":20}]},
        {"featureType":"road","elementType":"all","stylers":[{"lightness":20}]},
        {"featureType":"road.highway","elementType":"geometry","stylers":[{"color":"#c5c6c6"}]},
        {"featureType":"road.arterial","elementType":"geometry","stylers":[{"color":"#e4d7c6"}]},
        {"featureType":"road.local","elementType":"geometry","stylers":[{"color":"#fbfaf7"}]},
        {"featureType":"water","elementType":"all","stylers":[{"visibility":"on"},{"color":"#acbcc9"}]}]"""
                                  )

        plot = GMapPlot(x_range=x_range,
                        y_range=y_range,
                        map_options=map_options,
                        title=""
                        #plot_width=750, plot_height=750
                        )

        plot.plot_width = 800
        plot.plot_height = 800

        source = ColumnDataSource(data=dict(lat=[my_latitude],
                                            lon=[my_longitude],
                                            fill=['orange', 'blue', 'green']))
        circle = Circle(x="lon",
                        y="lat",
                        size=10,
                        fill_color="fill",
                        line_color="black")
        plot.add_glyph(source, circle)

        source = ColumnDataSource(data=dict(
            lat=[my_latitude - 0.005, my_latitude + 0.005],
            lon=[my_longitude - 0.005, my_longitude - 0.005],
        ))
        line = Line(x="lon", y="lat", line_width=2, line_color="green")
        plot.add_glyph(source, line)

        source = ColumnDataSource(data=dict(
            lat=[my_latitude + 0.005, my_latitude + 0.005],
            lon=[my_longitude - 0.005, my_longitude + 0.005],
        ))
        line = Line(x="lon", y="lat", line_width=2, line_color="green")
        plot.add_glyph(source, line)

        source = ColumnDataSource(data=dict(
            lat=[my_latitude - 0.005, my_latitude + 0.005],
            lon=[my_longitude + 0.005, my_longitude + 0.005],
        ))
        line = Line(x="lon", y="lat", line_width=2, line_color="green")
        plot.add_glyph(source, line)

        source = ColumnDataSource(data=dict(
            lat=[my_latitude - 0.005, my_latitude - 0.005],
            lon=[my_longitude - 0.005, my_longitude + 0.005],
        ))
        line = Line(x="lon", y="lat", line_width=2, line_color="green")
        plot.add_glyph(source, line)

        #hover.tooltips = [
        #("index", "$index"),
        #("(x,y)", "($x, $y)"),
        #("radius", "@radius"),
        #("fill color", "$color[hex, swatch]:fill_color"),
        #("foo", "@foo"),
        #("bar", "@bar"),
        #]

        if app.ampm == "pm":
            app.timeofday = app.timeofday + 24

        if app.direction == "from":
            n0 = network_df[app.timeofday][my_center_id]
            n0.sort(ascending=False, axis=1)
            n0 = n0.irow(range(20))
        elif app.direction == "to":
            n0 = network_df[app.timeofday][:, my_center_id]
            n0.sort(ascending=False, axis=1)
            n0 = n0.irow(range(20))

        #widths = [9,8,8,7,6,5,4,3,2,1]
        ww = 0.
        for index, row in n0.iteritems():
            ww += 0.3
            #p_i = get_center_coordinates(my_center_id,x_min=x_min, x_max=x_max, y_min=y_min, y_max=y_max,mesh_space=mesh_space)
            p_f = get_center_coordinates(index,
                                         x_min=x_min,
                                         x_max=x_max,
                                         y_min=y_min,
                                         y_max=y_max,
                                         mesh_space=mesh_space)

            ss = ColumnDataSource(data=dict(
                lat=[my_latitude, p_f[1]],
                lon=[my_longitude, p_f[0]],
            ))
            line = Line(x="lon", y="lat", line_width=ww)
            plot.add_glyph(ss, line)

        pan = PanTool()
        wheel_zoom = WheelZoomTool()
        box_select = BoxSelectTool()

        plot.add_tools(pan, wheel_zoom, box_select)

        #xaxis = LinearAxis(axis_label="lat", major_tick_in=0, formatter=NumeralTickFormatter(format="0.000"))
        #plot.add_layout(xaxis, 'below')

        #yaxis = LinearAxis(axis_label="lon", major_tick_in=0, formatter=PrintfTickFormatter(format="%.3f"))
        #plot.add_layout(yaxis, 'left')

        overlay = BoxSelectionOverlay(tool=box_select)
        plot.add_layout(overlay)

        app.script, app.div = components(plot)

        ##################################################
        ##################################################

        ##################################################
        ########Bokeh FIG block##############################

        # select the tools we want
        TOOLS = "pan,wheel_zoom,box_zoom,reset,save"

        #print datetime.datetime(hour=int(33)/2, minute=int(33)%2*30)
        #print data_df.index

        p1 = figure(tools=TOOLS,
                    plot_width=400,
                    plot_height=400,
                    x_axis_label='Time',
                    y_axis_label='Number of trips')  #,x_axis_type='datetime')
        p1.line((data_df.index) / 2,
                data_df['number_of_pickups_at_time_slot'],
                line_width=2,
                color="blue",
                legend="Total number of pickups in NYC in a typical day")
        p1.line((data_df.index) / 2,
                data_df['number_of_dropoffs_at_time_slot'],
                line_width=2,
                color="red",
                legend="Total number of dropoffs in NYC in a typical day")
        #p1.circle(dates, closing_prices, fill_color="red", size=6)

        #plots = {'Red': p1}

        #script_graph1, div_graph1 = components(plots)
        #app.script_graph1 = script_graph1
        #app.div_graph1 = div_graph1.values()[0]
        ##################################################

        ##################################################

        ###ADD plot of NUMBER OF TRIPS (PICK UP AND DROPOFF FROM LOCATION)
        pickup_count = [0 for i in range(48)]
        dropoff_count = [0 for i in range(48)]
        for ind in network_df.index.levels[0]:
            try:
                pickup_count[ind] = network_df[ind][my_center_id].count()
            except KeyError:
                pass
            try:
                dropoff_count[ind] = network_df[ind][:, my_center_id].count()
            except KeyError:
                continue

        TOOLS = "pan,wheel_zoom,box_zoom,reset,save"

        p2 = figure(tools=TOOLS,
                    plot_width=400,
                    plot_height=400,
                    x_axis_label='Time',
                    y_axis_label='Number of trips')  #,x_axis_type='datetime')
        p2.line(np.array(range(48)) / 2,
                pickup_count,
                line_width=2,
                color="blue",
                legend="Average pickups from your location")
        p2.line(np.array(range(48)) / 2,
                dropoff_count,
                line_width=2,
                color="red",
                legend="Average dropoffs at your location")
        #p1.circle(dates, closing_prices, fill_color="red", size=6)

        #plots2 = {'Red': p2}

        plots1and2 = gridplot([[p1, p2]])

        script_graph2, div_graph2 = components(plots1and2)
        app.script_graph2 = script_graph2
        app.div_graph2 = div_graph2

        return redirect('/graph_page')
Ejemplo n.º 26
0
    def generate_plot(self):
        """TODO: write this doc."""

        tools = [
            PanTool(),
            SaveTool(),
            WheelZoomTool(),
            ResetTool(),
        ]

        x_values = []
        y_values = []
        std_errors = []
        models = self.data.index.levels[0].tolist()
        for model in models:
            values = self.data[self.measure[0]].loc[model].values
            mean = np.mean(values)
            stderr = np.around(st.sem(values, nan_policy='omit'), 5)
            n_parms = self.data['n_parms'].loc[model].values[0]
            x_values.append(n_parms)
            y_values.append(mean)
            std_errors.append(stderr)

        newData = pd.DataFrame.from_dict({
            'stderr': std_errors,
            'mean': y_values,
            'n_parms': x_values,
            'modelname': models,
        })
        # Drop any models with NaN values, since that means they had no
        # performance data for one or more columns.
        newData.dropna(axis=0, how='any', inplace=True)
        if newData.size == 0:
            self.script, self.div = (
                "Error, no plot to display.",
                "None of the models contained valid performance data.")
            return
        dat_source = ColumnDataSource(newData)

        p = figure(
            tools=tools,
            x_axis_label=("N Parms, model prefix: {0}, "
                          "suffix: {1}".format(self.pre, self.suf)),
            y_axis_label=("Mean {0}, +/- Standard Error".format(
                self.measure[0])),
            title="Mean {0} per Model vs Complexity".format(self.measure[0]),
            output_backend=OUTPUT_FORMAT,
            sizing_mode='stretch_both',
        )

        circles = Circle(
            x='n_parms',
            y='mean',
            size=6,
            fill_color="navy",
            fill_alpha=0.7,
        )
        circle_renderer = p.add_glyph(dat_source, circles)
        hover = self.create_hover()
        hover.renderers = [circle_renderer]
        p.add_tools(hover)
        #p.circle(x_values, y_values, size=6, color="navy", alpha=0.7)
        error_bars_x = []
        error_bars_y = []
        for i, std in enumerate(std_errors):
            error_bars_x.append([x_values[i], x_values[i]])
            error_bars_y.append([y_values[i] - std, y_values[i] + std])
        p.multi_line(
            error_bars_x,
            error_bars_y,
            color="firebrick",
            alpha=0.4,
            line_width=2,
        )

        # workaround to prevent title and toolbar from overlapping
        grid = gridplot([p], ncols=GRID_COLS, sizing_mode='stretch_both')
        self.script, self.div = components(grid)
        self.plot = grid
        if self.display:
            show(grid)
Ejemplo n.º 27
0
def radialplot(tree,
               node_color='node_color',
               node_size='node_size',
               node_alpha='node_alpha',
               edge_color='edge_color',
               edge_alpha='edge_alpha',
               edge_width='edge_width',
               hover_var='hover_var',
               figsize=(500, 500),
               **kwargs):
    """ Plots unrooted radial tree.

    Parameters
    ----------
    tree : instance of skbio.TreeNode
       Input tree for plotting.
    node_color : str
       Name of variable in `tree` to color nodes.
    node_size : str
       Name of variable in `tree` that specifies the radius of nodes.
    node_alpha : str
       Name of variable in `tree` to specify node transparency.
    edge_color : str
       Name of variable in `tree` to color edges.
    edge_alpha : str
       Name of variable in `tree` to specify edge transparency.
    edge_width : str
       Name of variable in `tree` to specify edge width.
    hover_var : str
       Name of variable in `tree` to display in the hover menu.
    figsize : tuple, int
       Size of resulting figure.  default: (500, 500)
    **kwargs: dict
       Plotting options to pass into bokeh.models.Plot

    Returns
    -------
    bokeh.models.Plot
       Interactive plotting instance.


    Notes
    -----
    This assumes that the tree is strictly bifurcating.

    See also
    --------
    bifurcate
    """
    # TODO: Add in example doc string

    # This entire function was motivated by
    # http://chuckpr.github.io/blog/trees2.html
    t = UnrootedDendrogram.from_tree(tree)

    nodes = t.coords(figsize[0], figsize[1])

    # fill in all of the node attributes
    def _retreive(tree, x, default):
        return pd.Series(
            {n.name: getattr(n, x, default)
             for n in tree.levelorder()})

    # default node color to light grey
    nodes[node_color] = _retreive(t, node_color, default='#D3D3D3')
    nodes[node_size] = _retreive(t, node_size, default=1)
    nodes[node_alpha] = _retreive(t, node_alpha, default=1)
    nodes[hover_var] = _retreive(t, hover_var, default=None)

    edges = nodes[['child0', 'child1']]
    edges = edges.dropna(subset=['child0', 'child1'])
    edges = edges.unstack()
    edges = pd.DataFrame({
        'src_node': edges.index.get_level_values(1),
        'dest_node': edges.values
    })
    edges['x0'] = [nodes.loc[n].x for n in edges.src_node]
    edges['x1'] = [nodes.loc[n].x for n in edges.dest_node]
    edges['y0'] = [nodes.loc[n].y for n in edges.src_node]
    edges['y1'] = [nodes.loc[n].y for n in edges.dest_node]
    ns = [n.name for n in t.levelorder(include_self=True)]
    attrs = pd.DataFrame(index=ns)

    # default edge color to black
    attrs[edge_color] = _retreive(t, edge_color, default='#000000')
    attrs[edge_width] = _retreive(t, edge_width, default=1)
    attrs[edge_alpha] = _retreive(t, edge_alpha, default=1)

    edges = pd.merge(edges,
                     attrs,
                     left_on='dest_node',
                     right_index=True,
                     how='outer')
    edges = edges.dropna(subset=['src_node'])

    node_glyph = Circle(x="x",
                        y="y",
                        radius=node_size,
                        fill_color=node_color,
                        fill_alpha=node_alpha)

    edge_glyph = Segment(x0="x0",
                         y0="y0",
                         x1="x1",
                         y1="y1",
                         line_color=edge_color,
                         line_alpha=edge_alpha,
                         line_width=edge_width)

    def df2ds(df):
        return ColumnDataSource(ColumnDataSource.from_df(df))

    ydr = DataRange1d(range_padding=0.05)
    xdr = DataRange1d(range_padding=0.05)

    plot = Plot(x_range=xdr, y_range=ydr, **kwargs)
    plot.add_glyph(df2ds(edges), edge_glyph)
    ns = plot.add_glyph(df2ds(nodes), node_glyph)

    tooltip = [("Feature ID", "@index")]
    if hover_var is not None:
        tooltip += [(hover_var, "@" + hover_var)]

    hover = HoverTool(renderers=[ns], tooltips=tooltip)
    plot.add_tools(hover, BoxZoomTool(), ResetTool())

    return plot
Ejemplo n.º 28
0
                          WheelZoomTool)
from bokeh.resources import INLINE

x = arange(-2 * pi, 2 * pi, 0.1)
y = sin(x)
y2 = linspace(0, 100, len(y))

source = ColumnDataSource(data=dict(x=x, y=y, y2=y2))

plot = Plot(x_range=Range1d(start=-6.5, end=6.5),
            y_range=Range1d(start=-1.1, end=1.1),
            min_border=80)

plot.extra_y_ranges = {"foo": Range1d(start=0, end=100)}

circle = Circle(x="x", y="y", fill_color="red", size=5, line_color="black")
plot.add_glyph(source, circle)

plot.add_layout(LinearAxis(), 'below')
plot.add_layout(LinearAxis(), 'left')

circle2 = Circle(x="x", y="y2", fill_color="blue", size=5, line_color="black")
plot.add_glyph(source, circle2, y_range_name="foo")

plot.add_layout(LinearAxis(y_range_name="foo"), 'left')

plot.add_tools(PanTool(), WheelZoomTool())

doc = Document()
doc.add_root(plot)
Ejemplo n.º 29
0
    map_options=map_options,
    api_key=API_KEY,
)
plot.title.text = "Meteorite Landings"

# Defining data elements (as lists) that will go into the Map and its Tooltip:
source = ColumnDataSource(data=dict(lat=list(df.reclat),
                                    lon=list(df.reclong),
                                    fill=list(df.fill),
                                    mass=list(df.mass),
                                    year=list(df.year),
                                    recclass=list(df.recclass),
                                    name=list(df.name)))

# Adding Map's Glyphs:
circle = Circle(x="lon", y="lat", size=5, fill_color="fill", line_alpha=0)
plot.add_glyph(source, circle)

# Initializing and adding Map Tools to the plot object:
pan = PanTool()
wheel_zoom = WheelZoomTool()
box_select = BoxSelectTool()
box_zoom = BoxZoomTool()
reset = ResetTool()
# This allows you to specify what goes in the Map's Tooltip
hover = HoverTool(tooltips=[("Class", "@recclass"), (
    "Name", "@name"), ("Year", "@year"), ("(lat, long)",
                                          "(@lat, @lon)"), ("Mass", "@mass")])

plot.add_tools(pan, wheel_zoom, box_select, box_zoom, reset, hover)