示例#1
0
    def _set_tools(self):
        wheel_zoom = WheelZoomTool()
        pan = PanTool()
        box_zoom = BoxZoomTool()
        box_select = BoxSelectTool()
        crosshair = CrosshairTool()
        tap = TapTool()
        save = SaveTool()
        reset = ResetTool()
        self.lasso_select = LassoSelectTool(
            renderers=self.circles,          # default = all available renderers
            select_every_mousemove=False,    # to enhance performance
        )

        self.lasso_select.overlay.line_alpha=0.9
        self.lasso_select.overlay.line_color="black"
        self.lasso_select.overlay.fill_alpha=0.2
        self.lasso_select.overlay.fill_color="grey"

        hover = self._get_hover_tool()
        self.tools = (
            pan, box_zoom, self.lasso_select, box_select,
            crosshair, save, reset, tap, wheel_zoom
        )
        self.plot.add_tools(*self.tools)
示例#2
0
def ridgeplot(courses_obj):
    # first format 'courses_obj' into 'probly' DataFrame format
    # courses_obj: [{'course_name': 'Calculus...', ...}, ...]
    grades = [[100*y[2]/y[3] for y in x['assignments']] for x in courses_obj]
    # turn this list of lists into a complete NumPy array
    length = len(sorted(grades, key=len, reverse=True)[0])
    grades = np.array([xi+[None]*(length-len(xi)) for xi in grades], dtype='float')
    columns = [x['course_name'] for x in courses_obj]
    grades = grades.transpose()
    probly = pd.DataFrame(grades, columns=columns)


    cats = list(reversed(probly.keys()))


    palette = [cc.rainbow[i*15] for i in range(17)]

    x = np.linspace(-20,110, 500)

    source = ColumnDataSource(data=dict(x=x))

    p = figure(y_range=cats, plot_width=900, plot_height = 300, x_range=(-5, 120))#, toolbar_location=None)

    for i, cat in enumerate(reversed(cats)):
        adjusted = probly[cat].replace([np.inf, -np.inf], np.nan).dropna(how="all")
        if adjusted.size == 1 or pd.unique(adjusted).size == 1: # this means we can't compute
            continue
        pdf = gaussian_kde(adjusted)
        #p = figure(plot_width=400, plot_height=400)
        #p.line(x, pdf(x))
        y = ridge(cat, pdf(x), scale=2)
        #p.line(x, y, color='black')
        #show(p)
        source.add(y, cat)
        p.patch('x', cat, color=palette[i], alpha=0.6, line_color="black", source=source)

    p.outline_line_color = None
    p.background_fill_color = "#efefef"

    p.tools = [PanTool(), CrosshairTool(), HoverTool(), SaveTool(), BoxZoomTool(), WheelZoomTool(), ResetTool()]

    ticks = list(np.array([np.array([0,3,7])+i*10 for i in range(10)]).flatten()) + [100]
    p.xaxis.ticker = FixedTicker(ticks=ticks)
    p.xaxis.formatter = PrintfTickFormatter(format="%d")
    p.xaxis.axis_label = "Your Grade Distribution"

    p.yaxis.axis_label = "Your Courses"

    p.ygrid.grid_line_color = None
    p.xgrid.grid_line_color = "Grey"
    p.xgrid.ticker = p.xaxis[0].ticker

    p.axis.minor_tick_line_color = None
    p.axis.major_tick_line_color = None
    p.axis.axis_line_color = None

    p.y_range.range_padding = 0.12

    return p
示例#3
0
    def _set_tools(self):
        wheel_zoom = WheelZoomTool()
        pan = PanTool()
        box_zoom = BoxZoomTool()
        box_select = BoxSelectTool()
        crosshair = CrosshairTool()
        tap = TapTool()
        save = SaveTool()

        lasso_select = LassoSelectTool(
            select_every_mousemove=False,  # enhance performance
        )

        code = """
            var projections = require("core/util/projections");
            var x = special_vars.x
            var y = special_vars.y
            var coords = projections.wgs84_mercator.inverse([x, y])
            return coords[%d].toFixed(2)
        """

        tooltips = '''
            <style>
                .bk-tooltip>div:not(:nth-child(-n+5)) {{
                    display:none;
                }}

                .bk-tooltip>div {{
                    background-color: #dff0d8;
                    padding: 5px;
                }}
            </style>

            <b>STATION: </b> @{STNNBR} <br />
            <b>LON: </b> @X_WMTS{custom} <br />
            <b>LAT: </b> @Y_WMTS{custom} <br />
        '''

        hover = HoverTool(toggleable=True,
                          mode='mouse',
                          tooltips=tooltips,
                          renderers=[self.env.wmts_map_scatter],
                          formatters={
                              'X_WMTS': CustomJSHover(code=code % 0),
                              'Y_WMTS': CustomJSHover(code=code % 1),
                          })

        tools = (pan, box_zoom, lasso_select, hover, crosshair, tap,
                 wheel_zoom)
        self.env.wmts_map.add_tools(*tools)

        # set defaults
        self.env.wmts_map.toolbar.active_drag = pan
        self.env.wmts_map.toolbar.active_inspect = [crosshair, hover]
        self.env.wmts_map.toolbar.active_scroll = wheel_zoom
        self.env.wmts_map.toolbar.active_tap = None
示例#4
0
def gradesplot(course_obj):
    assignment_array = course_obj['assignments']
    assignment_array.reverse()
    all_categories = get_categories(assignment_array)
    # get a list of lists
    # so each list within has all of the assignments belonging to that category/group
    data = []
    for category in all_categories:
        category_data = []
        for assignment in assignment_array:
            if assignment[1] == category:
                category_data.append(assignment)
        data.append(category_data)
    p = figure(x_axis_type="datetime", plot_width=1000, plot_height=300, y_range=(50,120))
    p.title.text = "Grades over time"

    colors = Spectral[len(all_categories)] if len(all_categories) >= 3 else Spectral[3][:len(all_categories)]
    
    assert(len(data) == len(all_categories) == len(colors))

    for datum, category, color in zip(data, all_categories, colors):
        df = pd.DataFrame(datum)
        df = df.rename({0:'name',1:'grouping', 2:'score', 3:'possible',4:'assigned',5:'due'}, axis='columns')

        df['due'] = pd.to_datetime(df['due'])
        source = ColumnDataSource(data=dict(
                x=df['due'],
                y=100*df['score']/df['possible'],
                category=df['grouping'],
                name=df['name']
            ))
        p.line('x', 'y', line_width=2, color=color, source=source, legend=category, tags=[category])
        p.circle('x', 'y', color=color, legend=category, source=source, tags=[category])

    p.ygrid.grid_line_alpha = 0.75
    p.ygrid.grid_line_color = "Black"
    p.legend.location = "bottom_left"
    p.legend.click_policy="hide"
    p.legend.label_text_font_size = '8pt'
    p.legend.spacing = -6

    p.toolbar.logo = None
    p.tools = [PanTool(), CrosshairTool(), HoverTool(), SaveTool(), BoxZoomTool(), WheelZoomTool(), ResetTool()]

    hover = p.select(dict(type=HoverTool))
    hover.tooltips = """
        <style>
            .bk-tooltip>div:not(:first-child) {display:none;}
        </style>
        <span style="font-size: 12px;">@name</span>
        <span style="font-size: 10px;"> (@category) <br>
        Grade: @y%</span>
    """

    return p
示例#5
0
def workloadplot(courses_obj, is_ridge=True):
    # we just need to first get a list of lists of dicts
    # each sublist is a separate course
    # within each sublist you're gonna have a bunch of datetimes
    # for each datetime, you want to repeat it depending on the category
    datetimes = [[[xi[5]]*repeat(xi[1].lower()) for xi in x['assignments']] for x in courses_obj]
    datetimes = [list(itertools.chain.from_iterable(x)) for x in datetimes]
    datetimes = pd.DataFrame(datetimes, index=[x['course_name'] for x in courses_obj]).transpose()
    datetimes = datetimes.apply(pd.to_datetime) # convert to datetime
    datetimes = pd.DataFrame([datetimes[x].dt.week for x in datetimes])

    counts = pd.DataFrame([datetimes[x].value_counts() for x in datetimes]).transpose()
    #counts = 
    # use something like a[0].combine(pd.Series([0 for x in range(50)]), lambda x1, x2: x1 if type(x1)==type(pd.to_datetime('8/8/2018')) else pd.to_datetime('1/1/2018' ))

    assert(datetimes.shape[1] == len(courses_obj))

    # for each course, need a list where each element is the number of assignments
    # due that week (by index)
    first_date = time.mktime(datetimes.apply(min).min().timetuple())
    last_date = time.mktime(datetimes.apply(max).max().timetuple())

    x = np.arange(first_date, last_date+DIVISION, TWO_WEEKS)
    x_ = np.linspace(0,101,2)

    source = ColumnDataSource(data=dict(x_=x_))

    cats = list(datetimes.keys())



    p.outline_line_color = None
    p.background_fill_color = "#efefef"

    p.tools = [PanTool(), CrosshairTool(), HoverTool(), SaveTool(), BoxZoomTool(), WheelZoomTool(), ResetTool()]
    p.xaxis.axis_label = "Your Workload Over Time"

    p.yaxis.axis_label = "Your Courses"

    p.ygrid.grid_line_color = None
    p.xgrid.grid_line_color = "Grey"

    #ticks = ['Beginning', 'End']
    #p.xaxis.ticker = FixedTicker(ticks=ticks)

    p.xgrid.ticker = p.xaxis[0].ticker

    p.axis.minor_tick_line_color = None
    p.axis.major_tick_line_color = None
    p.axis.axis_line_color = None

    p.y_range.range_padding = 0.8
    #p.y_range.group_padding = 3

    return p
示例#6
0
    def _set_tools(self):
        wheel_zoom = WheelZoomTool()
        pan = PanTool()
        box_zoom = BoxZoomTool()
        box_select = BoxSelectTool()
        crosshair = CrosshairTool()
        tap = TapTool()
        save = SaveTool()
        reset = ResetTool()     # TODO: add only to one plot, maybe with n_plot

        self.lasso_select = LassoSelectTool(
            renderers=self.circles,                  # default all available renderers
            select_every_mousemove=False,            # enhance performance
        )

        tooltips = '''
            <style>
                .bk-tooltip>div:not(:nth-child(-n+5)) {{
                    display:none;
                }}

                /* .bk-tooltip-custom + .bk-tooltip-custom {{
                    display: none;  sometimes everything is hidden with this
                }} */

                .bk-tooltip>div {{
                    background-color: #dff0d8;
                    padding: 5px;
                }}
            </style>

            <b>INDEX: </b> @INDEX <br>
            <b>{x}: </b> @{x} <br>
            <b>{x}_FLAG_W: </b> @{x}_FLAG_W <br>
            <b>{y}: </b> @{y} <br>
            <b>{y}_FLAG_W: </b> @{y}_FLAG_W <br>
        '''.format(x=self.x, y=self.y)

        hover = HoverTool(                  # TODO: try to make this toggleable
            renderers=self.circles,
            toggleable=True,
            mode='mouse',
            tooltips=tooltips,
        )

        tools = (
            pan, box_zoom, self.lasso_select, box_select,
            crosshair, save, reset, tap, wheel_zoom
        )
        self.plot.add_tools(*tools)
示例#7
0
    def plot_pulses2(self, km=False):

        import matplotlib
        matplotlib.use('Agg')
        import matplotlib.pyplot as plt
        from bokeh.resources import CDN
        from bokeh.embed import components
        from bokeh.mpl import to_bokeh
        from bokeh.models.tools import WheelZoomTool, ResetTool, PanTool, HoverTool, SaveTool

        lines = self.get_lines()

        N = len(lines)
        npoints = self.total_units / self.km2unit if km else self.total_units
        fig = plt.figure(figsize=(12, 2 + N * 0.5))
        ax = fig.add_subplot(111)
        labels = ['IPP']

        for i, line in enumerate(lines):
            labels.append(line.get_name(channel=True))
            l = ax.plot((0, npoints), (N - i - 1, N - i - 1))
            points = [(tup[0], tup[1] - tup[0])
                      for tup in line.pulses_as_points(km=km) if tup != (0, 0)]
            ax.broken_barh(points, (N - i - 1, 0.5),
                           edgecolor=l[0].get_color(),
                           facecolor='none')

        n = 0
        f = ((self.ntx + 50) / 100) * 5 if (
            (self.ntx + 50) / 100) * 10 > 0 else 2
        for x in np.arange(0, npoints,
                           self.ipp if km else self.ipp * self.km2unit):
            if n % f == 0:
                ax.text(x, N, '%s' % n, size=10)
            n += 1

        labels.reverse()
        ax.set_yticks(range(len(labels)))
        ax.set_yticklabels(labels)
        ax.set_xlabel = 'Units'
        plot = to_bokeh(fig, use_pandas=False)
        plot.tools = [
            PanTool(dimensions=['width']),
            WheelZoomTool(dimensions=['width']),
            ResetTool(),
            SaveTool()
        ]
        plot.toolbar_location = "above"

        return components(plot, CDN)
示例#8
0
 def __create_tools(cls, **hoverkwargs):
     return [
         TapTool(),
         BoxSelectTool(dimensions='width'),
         BoxSelectTool(dimensions='height'),
         BoxSelectTool(),
         WheelZoomTool(),
         BoxZoomTool(),
         ResetTool(),
         SaveTool(),
         HoverTool(tooltips=[('workflow', '@Workflow'),
                             ('activity', '@Activity'),
                             ('result', '@Result'),
                             ('duration', '@DurationStr'),
                             ('started', '@StartedOnTimestampStr'),
                             ('ended', '@EndedOnTimeStampStr')],
                   formatters={
                       'started': 'printf',
                       'ended': 'printf'
                   },
                   show_arrow=True,
                   **hoverkwargs)
     ]
示例#9
0
文件: rca.py 项目: smartyal/21datalab
def rca(functionNode):
    logger = functionNode.get_logger()
    logger.info("==>>>> in rca (root cause analysis " +
                functionNode.get_browse_path())
    progressNode = functionNode.get_child("control").get_child("progress")
    progressNode.set_value(0.1)

    variables = functionNode.get_child("selectedVariables").get_leaves()
    tag = functionNode.get_child("selectedTags").get_value()  #only one tag
    annotations = functionNode.get_child("annotations").get_leaves()
    feature = functionNode.get_child("selectedFeatures").get_value()
    algo = functionNode.get_child("selectedAlgorithms").get_value()
    target = functionNode.get_child("selectedTarget").get_target()

    p = Progress(progressNode)
    p.set_divisor(len(annotations) / 0.5)
    p.set_offset(0.1)
    #now create the data as x-y

    results = {"x": [], "y": []}
    var = variables[0]
    #now iterate over all annotations of the matching type and create feature
    for idx, anno in enumerate(annotations):
        p.set_progress(idx)
        if (anno.get_child("type").get_value()
                == "time") and (tag in anno.get_child("tags").get_value()):
            startTime = anno.get_child("startTime").get_value()
            endTime = anno.get_child("endTime").get_value()
            data = var.get_time_series(startTime, endTime)
            #now create the feature
            feat = calc_feature(data["values"], feature)
            targetValue = get_target(
                target, (date2secs(startTime) + date2secs(endTime)) / 2)
            if feat and targetValue and numpy.isfinite(
                    feat) and numpy.isfinite(targetValue):
                results["x"].append(feat)
                results["y"].append(targetValue)
            else:
                logger.warning(
                    f"no result for {var.get_name} @ {startTime}, anno:{tag}, feat:{feat}, target: {target}"
                )

    #now we have all the x-y

    progressNode.set_value(0.7)
    fig = figure(title="x-y Correlation Plot " + var.get_name(),
                 tools=[PanTool(),
                        WheelZoomTool(),
                        ResetTool(),
                        SaveTool()],
                 plot_height=300,
                 x_axis_label=feature + "(" + var.get_name() + ") @ " + tag,
                 y_axis_label=target.get_name())
    fig.toolbar.logo = None
    curdoc().theme = Theme(json=themes.darkTheme)
    fig.xaxis.major_label_text_color = themes.darkTickColor
    fig.yaxis.major_label_text_color = themes.darkTickColor

    fig.scatter(x=results["x"],
                y=results["y"],
                size=5,
                fill_color="#d9b100",
                marker="o")
    fileName = functionNode.get_child("outputFileName").get_value()
    filePath = os.path.join(myDir, './../web/customui/' + fileName)
    progressNode.set_value(0.8)
    output_file(
        filePath, mode="inline"
    )  #inline: put the bokeh .js into this html, otherwise the default cdn will be taken, might cause CORS problems)
    save(fig)

    #print(results)

    return True
def make_region_plot(src, src_gene, src_tss, src_rna, fixed_yaxis=None):
    '''
    Construct pileup plot based on src
    '''

    # output_file(html_output)

    # flatten list of lists in count column of src, find max value of absolute expression
    frag_count_range = max(
        map(abs, [x for count in src.data['count'] for x in count]))
    if len(src_rna.data['y_plus']) > 0:
        rna_count_range = max(max(src_rna.data['y_plus']),
                              max(abs(src_rna.data['y_minus'])))
    else:
        rna_count_range = 0

    count_range = max(frag_count_range, rna_count_range)

    if fixed_yaxis:
        ymin = -1 * (fixed_yaxis)
        ymax = fixed_yaxis
    else:
        ymin = -(count_range + 1)
        ymax = count_range + 1

    # draw blank figure of correct size with tools
    p = figure(y_range=(ymin, ymax),
               plot_width=900,
               plot_height=700,
               tools=[
                   BoxSelectTool(),
                   BoxZoomTool(),
                   PanTool(),
                   WheelZoomTool(),
                   SaveTool(),
                   ResetTool()
               ],
               toolbar_location='above')

    legends = []

    # format axis and colors
    p.xaxis.axis_label = 'position'
    p.xaxis.major_label_orientation = pi / 4
    p.xaxis[0].formatter.use_scientific = False
    # p.xaxis[0].ticker=FixedTicker(ticks=range(start, end, 100))
    p.yaxis.axis_label = 'log normalized activity'

    patches = p.patches(source=src,
                        xs='position',
                        ys='count',
                        fill_color='color',
                        line_color=None,
                        alpha=0.50)
    legends.append(
        LegendItem(label='promoter activity (plus strand)',
                   renderers=[patches],
                   index=0))
    legends.append(
        LegendItem(label='promoter activity (minus strand)',
                   renderers=[patches],
                   index=1))

    # draw RNA lines
    if len(src_rna.data['x_minus']) > 0:
        plus_line = p.line(x='x_plus',
                           y='y_plus',
                           line_color='#528ecb',
                           line_width=2,
                           source=src_rna)
        legends.append(
            LegendItem(label='RNA-seq (plus strand)',
                       renderers=[plus_line],
                       index=0))
        minus_line = p.line(x='x_minus',
                            y='y_minus',
                            line_color='#ef8137',
                            line_width=2,
                            source=src_rna)
        legends.append(
            LegendItem(label='RNA-seq (minus strand)',
                       renderers=[minus_line],
                       index=1))

    # add second y-axis for TSS strength
    max_tss = max(map(abs, src_tss.data['y0']))
    p.extra_y_ranges = {
        'tss': Range1d(start=-(max_tss * 4), end=(max_tss * 4))
    }
    p.add_layout(LinearAxis(y_range_name='tss', axis_label='TSS expression'),
                 'right')

    # draw vertical rectangle
    p.quad(top='top',
           bottom='bottom',
           left='left',
           right='right',
           color='color',
           source=src_tss,
           y_range_name='tss')
    # draw horizontal line for arrow
    p.segment(x0='x0',
              y0='y0',
              x1='x1',
              y1='y1',
              color='color',
              source=src_tss,
              line_width=4,
              y_range_name='tss')
    # center of triangle is endpoint of segment
    tri = p.triangle(x='x1',
                     y='y1',
                     size=9,
                     angle='angle',
                     angle_units='deg',
                     color='color',
                     source=src_tss,
                     y_range_name='tss')
    legends.append(LegendItem(label='inactive TSS', renderers=[tri], index=9))
    legends.append(LegendItem(label='active TSS', renderers=[tri], index=0))

    # plot genes
    p.rect(x='gene_center',
           y='gene_center_y',
           width='gene_width',
           color='gene_color',
           height=10,
           height_units='screen',
           source=src_gene)
    p.triangle(x='tri_x',
               y=0,
               size=20,
               angle='angle',
               angle_units='deg',
               fill_color='gene_color',
               line_color=None,
               source=src_gene)
    p.text(x='gene_center',
           y='gene_center_y',
           text='gene_name',
           text_color='black',
           text_align='center',
           text_baseline='middle',
           text_font_size='10pt',
           source=src_gene)

    p.add_layout(Legend(items=legends), 'below')

    return p
示例#11
0
multi_plot = Plot(plot_width=800,
                  plot_height=400,
                  background_fill_color='silver')
multi_glyph = MultiLine(xs='waves', ys='ys', line_width=2, line_color='label')
multi_plot.add_glyph(spectra_visible_multi, multi_glyph)
xaxis, yaxis = LinearAxis(), LinearAxis()
multi_plot.add_layout(xaxis, 'below')
multi_plot.xaxis.axis_label = 'Wavelength (nm)'
multi_plot.add_layout(yaxis, 'left')
multi_plot.yaxis.axis_label = 'Intensity (CPS)'
multi_plot.add_layout(Grid(dimension=0, ticker=xaxis.ticker))
multi_plot.add_layout(Grid(dimension=1, ticker=yaxis.ticker))
multi_plot.add_tools(BoxZoomTool())
multi_plot.add_tools(WheelZoomTool())
multi_plot.add_tools(ResetTool())
multi_plot.add_tools(SaveTool())
multi_plot.add_layout(span)
# =============================================================================
# ~~  Clustering figures
# ~  labeled_materials_image: map image of predicted material labels
# ~  elbow inertia vs n_materials plot
# ~  varBar bar plot of pca explained variances
# =============================================================================
labeled_materials_image, label_mapper = basicMap(labeled_materials, 400,
                                                 'image')
# elbow = figure(x_axis_label='Number of Materials', y_axis_label='Learning Inertia',
#                plot_width=400, plot_height=200, toolbar_location=None)
# elbow.line('num_clusters', 'inertia', source=elbow_plot)
varBar = figure(plot_width=400, plot_height=200, toolbar_location=None)
varBar.vbar(x='x', top='top', source=pca_variance, width=0.9)
# =============================================================================
示例#12
0
def create_bk_fig(x=None, xlab=None, x_min=None, x_max=None,
                  ylab=None, fh=None, fw=None,
                  title=None, pw=None, ph=None, x_axis_type="linear",
                  y_axis_type="linear", x_name=None, y_name=None, **kwargs):
    """ Generates a bokeh figure

    Parameters
    ----------
    x :obj:`DataArray`
        Contains x-axis data
    xlab : :obj:`str`
        X-axis label
    x_min : :obj:`float`
        Min x value
    x_max : :obj:`float`
        Max x value
    ylab : :obj:`str`
        Y-axis label
    fh: :obj:`int`
        True height of figure without legends, axes titles etc
    fw: :obj:`int`
        True width of figure without legends, axes etc
    title: :obj:`str`
        Title of plot
    pw: :obj:`int`
        Plot width including legends, axes etc
    ph: :obj:`int`
        Plot height including legends, axes etc
    x_axis_type: :obj:`str`
        Type of x-axis can be linear, log, or datetime
    y_axis_type: :obj:`str`
        Can be linear, log or datetime
    x_name: :obj:`str`
        Name of the column used for the x-axis. Mostly used to form tooltips
    y_name: :obj:`str`
        Name of the column used for the y-axis. Also used for tooltips
    add_grid: :obj:`bool`
        Whether or not to add grid
    add_title: :obj:`bool`
        Whether or not to add title to plot
    add_xaxis: :obj:`bool`
        Whether or not to add x-axis and tick marks
    add_yaxis: :obj:`bool`
        Add y-axis or not
    fix_plotsize: :obj:`bool`
        Enforce certain dimensions on plot. This is useful for ensuring a plot
        is not obscure by axes and other things. If activated, plot's
        dimensions will not be responsive. It utilises fw and fh.

    Returns
    -------
    p : :obj:`Plot`
        A bokeh Plot object

    """

    add_grid = kwargs.pop("add_grid", False)
    add_title = kwargs.pop("add_title", True)
    add_xaxis = kwargs.pop("add_xaxis", False)
    add_yaxis = kwargs.pop("add_yaxis", False)
    fix_plotsize = kwargs.pop("fix_plotsize", True)
    # addition plot specs
    pl_specs = kwargs.pop("pl_specs", {})
    # additional axis specs
    ax_specs = kwargs.pop("ax_specs", {})
    # ticker specs
    ti_specs = kwargs.pop("ti_specs", {})

    plot_specs = dict(background="white", border_fill_alpha=0.1,
                      border_fill_color="white", min_border=3,
                      name="plot", outline_line_dash="solid",
                      outline_line_width=2, outline_line_color="#017afe",
                      outline_line_alpha=0.4, output_backend="canvas",
                      sizing_mode="stretch_width", title_location="above",
                      toolbar_location="above")
    plot_specs.update(pl_specs)

    axis_specs = dict(minor_tick_line_alpha=0, axis_label_text_align="center",
                      axis_label_text_font="monospace",
                      axis_label_text_font_size="10px",
                      axis_label_text_font_style="normal",
                      major_label_orientation="horizontal")
    axis_specs.update(ax_specs)

    tick_specs = dict(desired_num_ticks=5)
    tick_specs.update(ti_specs)

    # Define frame width and height
    # This is the actual size of the plot without the titles et al
    if fix_plotsize and not(fh or fw):
        fw = int(0.98 * pw)
        fh = int(0.93 * ph)

    # define the axes ranges
    x_range = DataRange1d(name="p_x_range", only_visible=True)

    y_range = DataRange1d(name="p_y_range", only_visible=True)

    if x_min is not None and x_max is not None and x_name.lower() in ["channel", "frequency"]:
        x_range = Range1d(name="p_x_range", start=x_min, end=x_max)
        y_range.only_visible = False

    # define items to add on the plot
    p_htool = HoverTool(tooltips=[(x_name, "$x"),
                                  (y_name, "$y")],
                        name="p_htool", point_policy="snap_to_data")

    if x_name.lower() == "time":
        p_htool.tooltips[0] = (x_name, "$x{%d-%m-%Y %H:%M}")
        p_htool.formatters = {"$x": "datetime"}

    p_toolbar = Toolbar(name="p_toolbar",
                        tools=[p_htool, BoxSelectTool(), BoxZoomTool(),
                               # EditTool(), # BoxEditTool(), # RangeTool(),
                               LassoSelectTool(), PanTool(), ResetTool(),
                               SaveTool(), UndoTool(), WheelZoomTool()])
    p_ticker = BasicTicker(name="p_ticker", **tick_specs)

    # select the axis scales for x and y
    if x_axis_type == "linear":
        x_scale = LinearScale(name="p_x_scale")
        # define the axes and tickers
        p_x_axis = LinearAxis(axis_label=xlab, name="p_x_axis",
                              ticker=p_ticker, **axis_specs)
    elif x_axis_type == "datetime":
        x_scale = LinearScale(name="p_x_scale")
        # define the axes and tickers
        p_x_axis = DatetimeAxis(axis_label=xlab, name="p_x_axis",
                                ticker=p_ticker, **axis_specs)
    elif x_axis_type == "log":
        x_scale = LogScale(name="p_x_scale")
        p_x_axis = LogAxis(axis_label=xlab, name="p_x_axis",
                           ticker=p_ticker, **axis_specs)

    if y_axis_type == "linear":
        y_scale = LinearScale(name="p_y_scale")
        # define the axes and tickers
        p_y_axis = LinearAxis(axis_label=ylab, name="p_y_axis",
                              ticker=p_ticker, **axis_specs)
    elif x_axis_type == "datetime":
        y_scale = LinearScale(name="p_y_scale")
        # define the axes and tickers
        p_y_axis = DatetimeAxis(axis_label=xlab, name="p_y_axis",
                                ticker=p_ticker, **axis_specs)
    elif y_axis_type == "log":
        y_scale = LogScale(name="p_y_scale")
        # define the axes and tickers
        p_y_axis = LogAxis(axis_label=ylab, name="p_y_axis",
                           ticker=p_ticker, **axis_specs)

    # Create the plot object
    p = Plot(plot_width=pw, plot_height=ph, frame_height=fh, frame_width=fw,
             toolbar=p_toolbar, x_range=x_range, x_scale=x_scale,
             y_range=y_range, y_scale=y_scale, **plot_specs)

    if add_title:
        p_title = Title(align="center", name="p_title", text=title,
                        text_font_size="24px",
                        text_font="monospace", text_font_style="bold",)
        p.add_layout(p_title, "above")

    if add_xaxis:
        p.add_layout(p_x_axis, "below")

    if add_yaxis:
        p.add_layout(p_y_axis, "left")

    if add_grid:
        p_x_grid = Grid(dimension=0, ticker=p_ticker)
        p_y_grid = Grid(dimension=1, ticker=p_ticker)
        p.add_layout(p_x_grid)
        p.add_layout(p_y_grid)

    return p
示例#13
0
take_picture_label = plot_util.make_take_picture_label()
picture_stream_label = plot_util.make_steam_picture_label()
title_div = plot_util.make_title_div()
description_div = plot_util.make_description_div()

# create the plot in which we will act on
plot = generate_initial_plot(test=True,
                             n_imgs=50,
                             img_width=0.3,
                             dim=figure_size,
                             random_state=42)
plot.toolbar.active_drag = None
plot.toolbar.active_scroll = None
plot.toolbar.active_tap = None
plot.toolbar.logo = None
plot.tools = [SaveTool()]

# load the default/starting image and move it to RGBA (bokeh) color format
# we must flip it because for some reason bokeh plots images upside down
im = cv2.imread("imgs/wanted.png")
im = cv2.cvtColor(im, cv2.COLOR_BGR2RGBA)
imarray = np.flipud(im)
e = plot.image_rgba(image=[imarray], x=[0], y=[0], dw=[0.3588], dh=[0.3588])

# add the image label to the plot
img_label = plot_util.make_image_label()
plot.add_layout(img_label)
ds = e.data_source

# add a text renderer to our plot (no data yet)
text_renderer = plot.text(
示例#14
0
def create_daily_res_plot(res_forecast, load_forecast):
    """
    Graph the res injection forecast.

    Arguments:
        res_forecast (list): list of renewable energy injection forecast
        load_forecast (list): list of load forecast
    """
    # Datetime range
    time_of_day = []

    # Create x-axis
    # beginning of day
    today = datetime.datetime.today()
    beginning_of_day = datetime.datetime(year=today.year,
                                         month=today.month,
                                         day=today.day)

    for i in range(len(res_forecast)):
        time_of_day.append(beginning_of_day +
                           datetime.timedelta(minutes=i * 30))

    # Compute 75 percentile
    percentile = np.percentile(res_forecast, 75)

    # Initialize dictionaries
    normal_dict = {'x': [], 'y': [], 'percentage': []}
    peak_dict = {'x': [], 'y': [], 'percentage': []}

    for i in range(len(res_forecast)):
        if res_forecast[i] >= percentile:
            peak_dict['x'].append(time_of_day[i])
            peak_dict['y'].append(res_forecast[i])
            peak_dict['percentage'].append(
                percentage_of(res_forecast[i], load_forecast[i]))
        else:
            normal_dict['x'].append(time_of_day[i])
            normal_dict['y'].append(res_forecast[i])
            normal_dict['percentage'].append(
                percentage_of(res_forecast[i], load_forecast[i]))

    # Hover tool to properly display time of day and value on hover
    hover = HoverTool(
        tooltips=[("Time of day", "@x{%H:%M}"), ("Forecast Value", "@y MWh"),
                  ("Percentage of Daily Load", "@percentage{1.11} %")],
        formatters={'@x': 'datetime'},
    )

    # Create the figure
    plot = figure(
        x_axis_label="Time of Day",
        y_axis_label="Megawatts Per Hour",
        x_axis_type='datetime',
        sizing_mode="stretch_width",
        tools=[
            hover,
            BoxZoomTool(),
            ResetTool(),
            LassoSelectTool(),
            WheelZoomTool(),
            PanTool(),
            SaveTool()
        ],
    )

    plot.xaxis.formatter = DatetimeTickFormatter(
        minutes=["%H:%M"],
        hours=["%H:%M"],
    )

    # Set x-range and y-range
    plot.y_range = Range1d(min(res_forecast) - 200, max(res_forecast) + 100)
    plot.x_range = Range1d(time_of_day[0] - datetime.timedelta(minutes=5),
                           time_of_day[-1] + datetime.timedelta(minutes=5))

    # Set a grid
    plot.grid.minor_grid_line_color = '#eeeeee'

    # Set the font and style of labels
    plot.axis.axis_label_text_font = "raleway"
    plot.axis.axis_label_text_font_style = "normal"

    # Set the font of ticks on the axis
    plot.axis.major_label_text_font = "raleway"

    # Set the desired ticks
    plot.xaxis.ticker = DatetimeTicker(desired_num_ticks=24)
    plot.yaxis.ticker = AdaptiveTicker(desired_num_ticks=20)

    # Add a line plot
    plot.line(time_of_day,
              res_forecast,
              line_alpha=0.2,
              color="#264b01",
              line_width=1.5)

    # Add two circle plots one for the normal values and one for those that
    # are at or above the 75-percentile
    plot.circle('x', 'y', source=normal_dict, size=8, color="#264b01")
    plot.circle('x', 'y', source=peak_dict, size=15, color="#264b01")

    return components(plot)
示例#15
0
# Create the tools for the toolbar
ts_cnt = np.arange(3)
cross = [CrosshairTool() for n in ts_cnt]
hover = [
    HoverTool(tooltips=[('time', '$x'), ('sample', '@x')]),
    HoverTool(tooltips=[('time', '$x'), ('val',
                                         '@p1'), ('raw', '@raw_lp_decim_p1')]),
    HoverTool(tooltips=[('time', '$x'), ('val',
                                         '@p2'), ('raw', '@raw_lp_decim_p2')])
]
xzoom = [BoxZoomTool(dimensions=['width']) for n in ts_cnt]
xwzoom = [WheelZoomTool(dimensions=['width']) for n in ts_cnt]
xsel = [BoxSelectTool(dimensions=['width']) for n in ts_cnt]
xtsel = [TapTool() for n in ts_cnt]
xpan = [PanTool(dimensions=['width']) for n in ts_cnt]
save = [SaveTool() for n in ts_cnt]
reset = [ResetTool() for n in ts_cnt]
tools = [[
    cross[n], hover[n], xpan[n], xzoom[n], xwzoom[n], xsel[n], xtsel[n],
    save[n], reset[n]
] for n in ts_cnt]

data_update_in_progress = False

play_all_button = Button(label='Play', button_type='success', width=60)
play_all_button.on_click(play_all)
play_all_sox_button = Button(label='Play sox', button_type='success', width=60)
play_all_sox_button.on_click(play_all_sox)
audio_first_checkbox = CheckboxGroup(labels=['audio first'], active=[0])
audio_first_checkbox.on_click(audio_first_selected)
示例#16
0
MARKERS = [
    "o", "v", "^", "<", ">", "1", "2", "3", "4", "8", "s", "p", "P", "*", "h",
    "H", "+", "x", "X", "D", "d"
]
COLORS = Category20[
    19]  #["red", "yellow", "blue", "green", "rosybrown","darkorange", "fuchsia", "grey", ]

TOOLS = [
    PanTool(),
    BoxZoomTool(),
    WheelZoomTool(),
    UndoTool(),
    RedoTool(),
    ResetTool(),
    SaveTool(),
    HoverTool(tooltips=[("Price", "$y"), ("Time", "$x")])
]

NAME_RESULT_SHOW_VARS = "resultat_show_variables_pi_plus_{}_pi_minus_{}.html"

#------------------------------------------------------------------------------
#                   definitions of functions
#------------------------------------------------------------------------------

#------------------------------------------------------------------------------
#                   definitions of functions
#------------------------------------------------------------------------------

# _____________________________________________________________________________
#
示例#17
0
def create_figures(source,
                   source_columns,
                   fig_len=fig_len,
                   fig_height=fig_height,
                   data_colors=data_colors,
                   hover_data=hover_data,
                   alpha=0.6,
                   figure_collector=figure_collector,
                   fig_title=fig_title,
                   sub_title=sub_title):

    # title = create_text(fig_title, text_color='#2b2d2f', text_font_size='40px', fig_height=60)
    # sub_title = create_text(sub_title, text_color='grey', text_font_size='20px', fig_height=40)
    # figure_collector.append(title)
    # figure_collector.append(sub_title)

    num_figures = len(source_columns)
    num = 0

    while num < num_figures:
        # create figure
        fig = figure(plot_width=fig_len,
                     plot_height=fig_height,
                     x_axis_type='datetime',
                     name='data_series',
                     output_backend="webgl")
        fig.sizing_mode = 'scale_width'
        # add tools
        tools = [
            PanTool(),
            BoxZoomTool(),
            WheelZoomTool(),
            SaveTool(),
            ResetTool()
        ]
        fig.add_tools(*tools)

        # create the scatter plot
        scatter = Circle(x='publishedAt',
                         y=source_columns[num],
                         line_color=data_colors[num],
                         fill_color=data_colors[num],
                         size=6,
                         fill_alpha=alpha,
                         line_alpha=alpha)
        scatter_render = fig.add_glyph(source_or_glyph=source, glyph=scatter)
        # hover only over scatter plot
        hover = HoverTool(renderers=[scatter_render],
                          tooltips=hover_data,
                          formatters={'@publishedAt': 'datetime'})
        fig.add_tools(hover)
        # open video url on tap scatter points
        taptool = TapTool(renderers=[scatter_render])
        taptool.callback = OpenURL(url=url)
        fig.add_tools(taptool)

        # create line plot without hover or top
        # line = Line(x='publishedAt', y=source_columns[num], line_color=data_colors[num], line_width=2, line_alpha=1)
        # line_render = fig.add_glyph(source_or_glyph=source, glyph=line)

        # add series title
        title = Text(x=data['publishedAt'].min(),
                     y=0.2,
                     text=[figure_titles[num]],
                     text_color=data_colors[num],
                     text_font_size='35px')
        fig.add_glyph(title)

        # decrease clutter
        fig.outline_line_color = None
        fig.xgrid.grid_line_color = None
        fig.ygrid.grid_line_color = None
        fig.yaxis.visible = False
        fig.xaxis.visible = False

        # format x-axis ticks
        fig.xaxis.major_tick_line_color = "grey"
        fig.xaxis.major_label_text_font_size = "15pt"
        fig.xaxis.major_label_text_color = "grey"

        # collect figures in a list
        figure_collector.append(fig)
        num += 1
    return figure_collector
示例#18
0
文件: rca.py 项目: smartyal/21datalab
def rca2(functionNode):
    logger = functionNode.get_logger()
    logger.info("==>>>> in rca2 (root cause analysis " +
                functionNode.get_browse_path())
    progressNode = functionNode.get_child("control").get_child("progress")
    progressNode.set_value(0.1)
    m = functionNode.get_model()

    report = '<i>REPORT</i><br><div style="font-size:85%">'

    annotations = functionNode.get_child("annotations").get_leaves()
    #order = ["Step"+str(no) for no in range(1,19)]
    order = ["Phase" + str(no) for no in range(3, 28)]
    order = functionNode.get_child("annotationsOrder").get_value()
    annotations = data_cleaning(annotations, order=order,
                                logger=logger)  #Step1,Step2,...Step18
    report += (f"found {len(annotations)} valid processes <br>")

    #for now, flatten them out
    annotations = [
        subprocess for process in annotations for subprocess in process
    ]

    algo = functionNode.get_child("selectedAlgorithm").get_value()
    target = functionNode.get_child("selectedTarget").get_target()

    progressNode.set_value(0.3)
    #now we are building up the table by iterating all the children in "selection"
    entries = functionNode.get_child("selection").get_children()

    table = {"target": []}
    firstVariable = True

    for entry in entries:
        logger.debug(f"entry {entry.get_name()}")
        #each entry is a combination of variable, tags and feature
        vars = entry.get_child("selectedVariables").get_targets()
        tags = entry.get_child("selectedTags").get_value()
        features = entry.get_child("selectedFeatures").get_value()
        #for iterate over variables
        for var in vars:
            logger.debug(
                f"processing variable: {var.get_name()} with tags {tags} and features {features}"
            )
            #columnName = var.get_name()+str(tags)+m.getRandomId()
            for tag in tags:
                row = 0
                #table[columnName]=[]# make a column
                for idx, anno in enumerate(annotations):
                    if anno.get_child("type").get_value() != "time":
                        continue
                    if tag in anno.get_child("tags").get_value():
                        startTime = anno.get_child("startTime").get_value()
                        endTime = anno.get_child("endTime").get_value()
                        data = var.get_time_series(startTime,
                                                   endTime)["values"]
                        #we take only the values "inside" the annotation
                        if len(data) > 2:
                            data = data[1:-1]
                        #now create the features
                        for feature in features:
                            feat = calc_feature(data, feature)
                            columnName = var.get_name(
                            ) + "_" + tag + "_" + feature
                            if not columnName in table:
                                table[columnName] = []
                            table[columnName].append(feat)

                        targetValue = get_target(
                            target,
                            (date2secs(startTime) + date2secs(endTime)) / 2)
                        if targetValue:
                            if firstVariable:
                                #for the first variable we also write the target
                                table["target"].append(targetValue)
                            else:
                                #for all others we make sure we have the same target value for that case (sanity check)
                                if table["target"][row] != targetValue:
                                    logger.warning(
                                        f'problem target {table["target"][row]} !=> {targetValue}'
                                    )
                            row = row + 1
                        else:
                            logger.warning(
                                f"no corrrect target value for {startTime} - {endTime}"
                            )

                firstVariable = False
    #now we have the table, plot it
    import json
    #print(json.dumps(table,indent=2))
    progressNode.set_value(0.5)
    #try a model

    algo = functionNode.get_child("selectedAlgorithm").get_value()
    if algo == "lasso":
        reg = linear_model.LassoCV()
        report += " using lasso Regression with auto-hyperparams <br>"
    else:
        #default
        report += " using linear Regression <br>"
        reg = linear_model.LinearRegression()  #try rigde, lasso

    columnNames = []
    dataTable = []
    for k, v in table.items():
        if k == "target":
            continue
        dataTable.append(v)
        columnNames.append(k)

    dataTable = numpy.asarray(dataTable)
    x = dataTable.T
    y = table["target"]
    x_train, x_test, y_train, y_test = train_test_split(x, y)
    reg.fit(x_train, y_train)

    print(reg.coef_)
    y_hat = reg.predict(x_test)
    y_repeat = reg.predict(x_train)
    print(f"predict: {y_hat} vs real: {y_test}")

    #check over/underfitting
    r_train = r2_score(y_train, y_repeat)
    r_test = r2_score(y_test, y_hat)

    report += "R<sup>2</sup> train= %.4g, R<sup>2</sup> test = %.4g <br>" % (
        r_train, r_test)

    pearsons = []
    for col in x.T:
        pearsons.append(pearsonr(col, y)[0])

    #and finally the correlations between y and yhat
    y_pearson_train = pearsonr(y_train, y_repeat)[0]
    y_pearson_test = pearsonr(y_test, y_hat)[0]

    report += "pearsonCorr y/y_hat train:%.4g , test:%.4g <br>" % (
        y_pearson_train, y_pearson_test)

    report += "regression coefficients, pearsons correlations:<br>"
    for col, coef, pear in zip(columnNames, reg.coef_, pearsons):
        report += "&nbsp &nbsp %s:%.4g, &nbsp %.4g <br>" % (col, coef, pear)

    #write report
    progressNode.set_value(0.8)
    report += "<div>"  #close the style div
    functionNode.get_child("report").set_value(report)
    #make a plot
    hover1 = HoverTool(tooltips=[('x,y', '$x,$y')], mode='mouse')
    hover1.point_policy = 'snap_to_data'
    hover1.line_policy = "nearest"
    tools = [
        PanTool(),
        WheelZoomTool(),
        BoxZoomTool(),
        ResetTool(),
        SaveTool(), hover1
    ]
    title = "prediction results on " + functionNode.get_child(
        "selectedAlgorithm").get_value()
    fig = figure(title=title, tools=tools, plot_height=400, plot_width=500)
    fig.toolbar.logo = None

    curdoc().theme = Theme(json=themes.darkTheme)
    fig.xaxis.major_label_text_color = themes.darkTickColor
    fig.yaxis.major_label_text_color = themes.darkTickColor
    fig.xaxis.axis_label = target.get_name()
    fig.xaxis.axis_label_text_color = "white"
    fig.yaxis.axis_label = "predicted Values for " + target.get_name()
    fig.yaxis.axis_label_text_color = "white"
    fig.circle(y_train,
               y_repeat,
               size=4,
               line_color="white",
               fill_color="white",
               name="train",
               legend_label="train")
    fig.circle(y_test,
               y_hat,
               line_color="#d9b100",
               fill_color="#d9b100",
               size=4,
               name="test",
               legend_label="test")

    fileName = functionNode.get_child("outputFileName").get_value()
    filePath = os.path.join(myDir, './../web/customui/' + fileName)
    fig.legend.location = "top_left"
    output_file(filePath, mode="inline")
    save(fig)

    return True
示例#19
0
    def plot_pulses(self, km=False):

        from bokeh.plotting import figure
        from bokeh.resources import CDN
        from bokeh.embed import components
        from bokeh.models import FixedTicker, PrintfTickFormatter
        from bokeh.models.tools import WheelZoomTool, ResetTool, PanTool, HoverTool, SaveTool
        from bokeh.models.sources import ColumnDataSource

        lines = self.get_lines().reverse()

        N = len(lines)
        npoints = self.total_units / self.km2unit if km else self.total_units
        ipp = self.ipp if km else self.ipp * self.km2unit

        hover = HoverTool(tooltips=[("Line", "@name"), ("IPP",
                                                        "@ipp"), ("X",
                                                                  "@left")])

        tools = [
            PanTool(dimensions=['width']),
            WheelZoomTool(dimensions=['width']), hover,
            SaveTool()
        ]

        plot = figure(
            width=1000,
            height=40 + N * 50,
            y_range=(0, N),
            tools=tools,
            toolbar_location='above',
            toolbar_sticky=False,
        )

        plot.xaxis.axis_label = 'Km' if km else 'Units'
        plot.xaxis[0].formatter = PrintfTickFormatter(format='%d')
        plot.yaxis.axis_label = 'Pulses'
        plot.yaxis[0].ticker = FixedTicker(ticks=list(range(N)))
        plot.yaxis[0].formatter = PrintfTickFormatter(format='Line %d')

        for i, line in enumerate(lines):

            points = [
                tup for tup in line.pulses_as_points(km=km) if tup != (0, 0)
            ]

            source = ColumnDataSource(
                data=dict(bottom=[i for tup in points],
                          top=[i + 0.5 for tup in points],
                          left=[tup[0] for tup in points],
                          right=[tup[1] for tup in points],
                          ipp=[int(tup[0] / ipp) for tup in points],
                          name=[line.get_name() for tup in points]))

            plot.quad(
                bottom='bottom',
                top='top',
                left='left',
                right='right',
                source=source,
                fill_alpha=0,
                #line_color = 'blue',
            )

            plot.line([0, npoints], [i, i])  #, color='blue')

        return components(plot, CDN)
示例#20
0
def varstatistics(functionNode):
    logger = functionNode.get_logger()
    logger.info("==>>>> statistics " + functionNode.get_browse_path())
    progressNode = functionNode.get_child("control").get_child("progress")
    progressNode.set_value(0)
    #functionNode.get_child("control.signal").set_value(None)

    vars = functionNode.get_child("variable").get_targets()
    widget = functionNode.get_child("widget").get_target()
    bins = functionNode.get_child("bins").get_value()
    tags = functionNode.get_child("annotations").get_value()
    startTime = date2secs(widget.get_child("startTime").get_value())
    endTime = date2secs(widget.get_child("endTime").get_value())

    vars = {var.get_id(): {"node": var} for var in vars}

    #first 30% progress:
    prog = Progress(progressNode)
    progressNode.set_value(0.1)
    prog.set_offset(0.1)
    #prog.set_divisor()

    if tags:
        allAnnoNodes = widget.get_child(
            "hasAnnotation.annotations").get_leaves()
        allAnnos = []
        prog.set_divisor(len(allAnnoNodes) / 0.2)
        for index, node in enumerate(allAnnoNodes):
            prog.set_progress(index)
            if node.get_child("type").get_value() == "time":
                thisTags = node.get_child("tags").get_value()
                if any(tag in tags for tag in thisTags):
                    anno = {}
                    for child in node.get_children():
                        anno[child.get_name()] = child.get_value()
                    if date2secs(anno["startTime"]) >= startTime and date2secs(
                            anno["endTime"]
                    ) <= endTime:  #take this anno only if it is inside the current start/end time
                        allAnnos.append(anno)
        if allAnnos == []:
            give_up(functionNode, "no matching annotations in selected time")
            return False
    else:
        allAnnos = []

    progressNode.set_value(0.3)

    logger.debug(f"statistics annotations to look at: {len(allAnnos)}")
    prog.set_offset(0.3)
    totalAnnos = max(len(allAnnos), 1)
    totalCount = len(vars) * totalAnnos

    prog.set_divisor(totalCount / 0.3)
    totalValids = 0
    for varIndex, var in enumerate(vars):
        info = vars[var]
        if tags:
            #iterate over all start and end times
            values = numpy.asarray([], dtype=numpy.float64)
            for annoIndex, anno in enumerate(allAnnos):
                thisValues = info["node"].get_time_series(
                    anno["startTime"], anno["endTime"])["values"]
                values = numpy.append(values, thisValues)
                myCount = varIndex * totalAnnos + annoIndex
                prog.set_progress(myCount)
        else:
            values = info["node"].get_time_series(startTime, endTime)["values"]

        valids = numpy.count_nonzero(~numpy.isfinite(values))
        totalValids += valids
        hist, edges = numpy.histogram(values, bins=bins)
        hist = hist / len(values)  #normalize
        info["hist"] = hist
        info["edges"] = edges

    #make a plot
    if totalValids == 0:
        give_up(
            functionNode,
            "all Variables are have no data in the time and annotations selected"
        )
        return False

    progressNode.set_value(0.6)

    hover1 = HoverTool(tooltips=[('x,y', '$x,$y')], mode='mouse')
    hover1.point_policy = 'snap_to_data'
    hover1.line_policy = "nearest"

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

    title = "Statistics of " + str(
        [info["node"].get_name() for var, info in vars.items()])
    if tags:
        title = title + " in annotation: " + str(tags)

    fig = figure(title=title, tools=tools, plot_height=300)
    fig.toolbar.logo = None

    curdoc().theme = Theme(json=themes.darkTheme)
    fig.xaxis.major_label_text_color = themes.darkTickColor
    fig.yaxis.major_label_text_color = themes.darkTickColor

    for index, var in enumerate(vars):
        info = vars[var]
        col = themes.darkLineColors[index]
        hist = info["hist"]
        edges = info["edges"]

        fig.quad(top=hist,
                 bottom=0,
                 left=edges[:-1],
                 right=edges[1:],
                 fill_color=col,
                 line_color=col,
                 alpha=0.8,
                 legend_label=info["node"].get_name())

    fig.legend.location = "top_left"
    fileName = functionNode.get_child("fileName").get_value()
    filePath = os.path.join(myDir, './../web/customui/' + fileName)

    # now make the trend box plot, but only for tags
    # for each variable we create statistics for the annotations and prepare the data
    # {"node":Node(), "boxLower":[], "boxUpper", "mean", "limitUpper", "limitLower"}
    #

    startTime = date2secs(widget.get_child("startTime").get_value(
    ))  #we only take tags that are inside the current zoom of the widgets
    endTime = date2secs(widget.get_child("endTime").get_value())

    boxPlots = []
    allTimes = []
    if tags:
        for index, var in enumerate(vars):
            info = {
                "node": vars[var]["node"],
                "boxLower": [],
                "boxUpper": [],
                "median": [],
                "time": [],
                "limitUpper": [],
                "limitLower": [],
                "mean": []
            }
            for anno in allAnnos:
                data = info["node"].get_time_series(anno["startTime"],
                                                    anno["endTime"])
                if len(data["values"]):
                    data["values"] = data["values"][numpy.isfinite(
                        data["values"])]
                    #remove the nan
                if len(data["values"]):

                    #make the statistics
                    info["time"].append(numpy.median(data["__time"]) * 1000)
                    allTimes.append(numpy.median(data["__time"]) * 1000)
                    info["limitLower"].append(
                        numpy.quantile(data["values"], 0.01))
                    info["limitUpper"].append(
                        numpy.quantile(data["values"], 0.99))
                    info["boxLower"].append(
                        numpy.quantile(data["values"], 0.25))
                    info["boxUpper"].append(
                        numpy.quantile(data["values"], 0.75))
                    info["median"].append(numpy.median(data["values"]))
                    info["mean"].append(numpy.mean(data["values"]))
            boxPlots.append(info)

        format = "%Y-%m-%d-T%H:%M:%S"
        custom = """var local = moment(value).tz('UTC'); return local.format();"""  #%self.server.get_settings()["timeZone"]

        hover = HoverTool(tooltips=[('date', '@x{%F}')],
                          formatters={'@x': CustomJSHover(code=custom)},
                          mode='mouse')
        hover.point_policy = 'snap_to_data'
        hover.line_policy = "nearest"
        tools = [
            PanTool(),
            BoxZoomTool(),
            WheelZoomTool(),
            ResetTool(), hover,
            SaveTool()
        ]

        fig2 = figure(title="trends",
                      tools=tools,
                      plot_height=300,
                      x_axis_type='datetime')
        fig2.xaxis.major_label_text_color = themes.darkTickColor
        fig2.yaxis.major_label_text_color = themes.darkTickColor

        progressNode.set_value(0.7)

        fig2.xaxis.formatter = DatetimeTickFormatter(years=format,
                                                     days=format,
                                                     months=format,
                                                     hours=format,
                                                     hourmin=format,
                                                     minutes=format,
                                                     minsec=format,
                                                     seconds=format)
        fig2.toolbar.logo = None
        #fig2.line([1,2,3],[1,2,3])
        #calc with of vbars
        if len(allAnnos) > 1:
            xTimesStart = min(allTimes)
            xTimesEnd = max(allTimes)
            width = (xTimesEnd - xTimesStart) / 2 / len(allAnnos)
        else:
            width = 1000000

        for index, info in enumerate(boxPlots):
            #each info is for one variable
            col = themes.darkLineColors[index]
            fig2.segment(info["time"],
                         info["limitUpper"],
                         info["time"],
                         info["boxUpper"],
                         line_color=col)
            fig2.segment(info["time"],
                         info["limitLower"],
                         info["time"],
                         info["boxLower"],
                         line_color=col)

            width = 20
            #fig2.vbar(info["time"],width=width,bottom=info["median"],top=info["boxUpper"],fill_color=col,line_color="black",width_units='screen')
            #fig2.vbar(info["time"],width=width,bottom=info["boxLower"],top=info["median"],fill_color=col,line_color="black",width_units='screen')
            #upper box
            sizUpper = numpy.asarray(info["boxUpper"]) - numpy.asarray(
                info["median"])
            medUpper = numpy.asarray(info["median"]) + sizUpper / 2
            fig2.rect(x=info["time"],
                      y=medUpper,
                      width_units='screen',
                      width=20,
                      height=sizUpper,
                      fill_color=col,
                      line_color="black")

            #lower box
            sizLower = numpy.asarray(info["median"]) - numpy.asarray(
                info["boxLower"])
            medLower = numpy.asarray(info["median"]) - sizLower / 2
            fig2.rect(x=info["time"],
                      y=medLower,
                      width_units='screen',
                      width=20,
                      height=sizLower,
                      fill_color=col,
                      line_color="black")

            #sort data for line
            x = numpy.asarray(info["time"])
            y = numpy.asarray(info["mean"])
            order = numpy.argsort(x)
            x = x[order]
            y = y[order]
            fig2.line(x, y, line_color=col)

        progressNode.set_value(0.8)
    else:
        #no fig2
        pass

    output_file(
        filePath, mode="inline"
    )  #inline: put the bokeh .js into this html, otherwise the default cdn will be taken, might cause CORS problems
    if tags:
        save(layout([[fig], [fig2]]))
    else:
        save(fig)

    return True
tooltips_top = [
    ('open', '@open{0.2f}'),
    ('high', '@high{0.2f}'),
    ('low', '@low{0.2f}'),
    ('close', '@close{0.2f}'),
    ('ma-slow', '@ma_slow{0.2f}'),
    ('ma-fast', '@ma_fast{0.2f}'),
    ('time', '@time{%F %T}')
]

box_zoom = BoxZoomTool()
pan_tool = PanTool(dimensions='width')
wheel_zoom = WheelZoomTool()
reset_tool = ResetTool()
crosshair = CrosshairTool()
save = SaveTool()
hover_tool = HoverTool(
    tooltips=tooltips_top,
    formatters={'@time': 'datetime'}
)
box_selection = BoxSelectTool()

tools_top = [
    box_zoom,
    pan_tool,
    wheel_zoom,
    reset_tool,
    crosshair,
    save,
    hover_tool,
    box_selection
        TableColumn(field="koi_score",
                    title="Disp Score",
                    formatter=NumberFormatter(format="0.00"))
    ]
    # Make the bokeh data table
    data_table = DataTable(source=source,
                           columns=columns,
                           height=300,
                           width=1000)

    # Do the Figure initially it is log period vs log planet radius
    # Include hovertool
    hover = HoverTool(tooltips=[("KOI", "@koi_number{0.00}")])

    data_figure = figure(height=600, width=1000,x_axis_label='Log Period [Day]', \
                    y_axis_label='Log Rp [Rearth]', tools=[hover, TapTool(), BoxZoomTool(), PanTool(), ZoomOutTool(), ZoomInTool(), ResetTool(), SaveTool()])
    data_figure.axis.axis_line_width = 3
    circle_handle = data_figure.circle(x='Logkoi_period',
                                       y='Logkoi_prad',
                                       source=source,
                                       size=10)
    data_figure.axis.axis_label_text_font_size = "20pt"
    data_figure.axis.major_label_text_font_size = "15pt"
    data_figure.axis.major_tick_line_width = 3
    xAxisHandle = data_figure.axis[0]
    yAxisHandle = data_figure.axis[1]
    # Include tap tool to open url with click on point
    taptool = data_figure.select(type=TapTool)
    taptool.callback = OpenURL(url="@koi_url")

    # START JAVASCRIPT literal code for handling figure data axis change