Beispiel #1
0
def displayMetricEvolution(
        data,
        metric,
        outputDir='../results/dae/neuralNetwork/resultsAnalysis/',
        configuration='concatParams'):
    reset_output()
    p = figure(tools=TOOLS, plot_width=350, plot_height=350)

    p.yaxis.axis_label = metric
    p.xaxis.axis_label = 'models'
    p.xaxis.axis_label_text_font_size = "12pt"
    p.yaxis.axis_label_text_font_size = "12pt"

    source = ColumnDataSource({
        metric: data[metric],
        'index': data.index.values,
        'conf': data[configuration]
    })

    p.line('index', metric, source=source)

    p.select_one(HoverTool).tooltips = [('conf', '@conf')]

    output_notebook()
    show(p)
    output_file(outputDir + metric + 'Evolution.html')
    save(p)
    return True
Beispiel #2
0
def plot_empty(geo='state', formatting=None, output=False):
    """Generate map outline (no fill).

    :param (str) geo: 'state' or 'county'
    :param (dict) formatting: if custom dict is passed, update DEFAULTFORMAT with those key-values
    :param (str) output: if specified, filepath to save html file. see save_plot().
    :return: if output is 'bokeh', returns Bokeh object; else None """

    ## get default plot formatting and update if necessary
    temp_format = DEFAULTFORMAT.copy()
    if formatting:
        temp_format.update(formatting)

    ## process data
    shape_df = prc.shape_geojson(geo,
                                 temp_format['simplify'],
                                 epsg=temp_format['epsg'])
    geo_src = models.GeoJSONDataSource(geojson=shape_df.to_json())

    ## plot and save choropleth
    bkplot = initialize_plot(temp_format)
    bkplot.patches('xs',
                   'ys',
                   fill_color=None,
                   line_color=temp_format['line_color'],
                   line_width=temp_format['line_width'],
                   source=geo_src)

    if temp_format['svg']:
        bkplot.output_backend = 'svg'
    save_plot(bkplot, output)
    ## return to original state
    io.reset_output()

    return bkplot
Beispiel #3
0
def show_html_image(field_2d, output_file_name='test.html'):
    """
    Plot a 2D field as image using Bokeh, output to html file only.
    """

    # Needs resetting, otherwise it appends new result to old one
    reset_output()

    # Define ranges
    xR, yR = field_2d.shape
    x = np.linspace(0, xR - 1, xR)
    y = np.linspace(0, yR - 1, yR)
    # Generate figure canvas with tooltips
    p = figure(x_range=(0, 1), y_range=(0, 1), \
           tooltips=[("x", "$x"), ("y", "$y"), ("value", "@image")], \
           match_aspect=True)
    # Generate image
    p.image(image=[field_2d], x=0, y=0, dw=1, dh=1, \
            palette="Spectral11")

    # Output to html file
    output_file(output_file_name, title="visualization")

    # Display in browser
    show(p)
    return None
Beispiel #4
0
def bok_sp(plot_dict, **kwargs):
    if kwargs: figsize = kwargs['figsize']
    else: figsize = (800, 400)
    plot_list = []
    plot_width, plot_height = figsize
    i = 0
    for unit, values in plot_dict.items():
        p = figure(plot_width=plot_width, plot_height=plot_height)
        p.xaxis.formatter = DatetimeTickFormatter()
        glyph_list = []
        column_list = []
        for data in values:
            column_name, x, y = data
            g = p.line(x=x, y=y, line_width=2, color=colors[i])
            i += 1
            column_list.append(column_name)
            glyph_list.append(g)
        items = [(column, [glyph])
                 for column, glyph in zip(column_list, glyph_list)]
        legend = Legend(items=items, location=(40, 0))
        p.add_layout(legend, 'below')
        plot_list.append(p)
    grid = [[p] for p in plot_list]
    p = gridplot(grid)
    reset_output()
    return p
Beispiel #5
0
def imshow(im, im2=None, p1=None, p2=None):
    # Bokeh Plotting
    io.reset_output()
    io.output_file('bokeh plots.html', title='imshow')
    colorImage = (len(im.shape) == 3)
    if colorImage:
        h, w, _ = im.shape
    else:
        h, w = im.shape

    # Setup tools
    hoverImage = models.HoverTool(tooltips=[('index', '$index'), ('(x, y)', '($x{(1.11)}, $y{(1.11)})')],
                                  point_policy='follow_mouse')

    p = plotting.figure(x_range=(0, w), y_range=(h, 0), plot_width=round(800 * w / h), plot_height=800,
                        x_axis_label='pixel (1 - ' + str(w) + ')', y_axis_label='pixel (1 - ' + str(h) + ')',
                        title='image', tools='box_zoom,pan,save,reset,wheel_zoom,crosshair',
                        active_scroll='wheel_zoom', active_inspect=None)

    # Plot image
    if colorImage:  # RGB
        imc = np.ones((im.shape[0], im.shape[1], 4), dtype=np.uint32) * 255
        imc[:, :, 0:3] = im

        img = np.empty((h, w), dtype=np.uint32)
        view = img.view(dtype=np.uint8).reshape((h, w, 4))
        view[:, :, :] = np.flipud(np.asarray(imc))
        p.image_rgba(image=[img], x=0, y=h, dw=w, dh=h)
    else:
        p.image(image=[np.flip(im, 0)], x=0, y=h, dw=w, dh=h, palette=palettes.Greys256)
    p.quad(top=[h], bottom=[0], left=[0], right=[w], alpha=0)  # clear rectange hack for tooltip image x,y
    p.add_tools(hoverImage)

    # Show plot
    colors = bokeh_colors(3)
    if im2 is None:
        if p1 is not None:
            p.circle(p1[:, 0], p1[:, 1], color=colors[0], line_width=2)
        if p2 is not None:
            p.circle(p2[:, 0], p2[:, 1], color=colors[1], line_width=2)
            p.multi_line(np.concatenate((p1[:, 0, None], p2[:, 0, None]), 1).tolist(),
                         np.concatenate((p1[:, 1, None], p2[:, 1, None]), 1).tolist(),
                         color='white', alpha=.3, line_width=1)

        io.show(p)  # open a browser
    else:
        h, w = im2.shape
        q = plotting.figure(x_range=(0, w), y_range=(h, 0), plot_width=round(600 * w / h), plot_height=600,
                            x_axis_label='pixel (1 - ' + str(w) + ')', y_axis_label='pixel (1 - ' + str(h) + ')',
                            title='image', tools='box_zoom,pan,save,reset,wheel_zoom,crosshair',
                            active_scroll='wheel_zoom', active_inspect=None)
        q.add_tools(hoverImage)

        # Plot image
        q.image(image=[np.flip(im2, 0)], x=0, y=h, dw=w, dh=h, palette=palettes.Greys256)
        q.quad(top=[h], bottom=[0], left=[0], right=[w], alpha=0)  # clear rectange hack for tooltip image x,y

        # Show plots
        io.show(column(p, q))  # open a browser
Beispiel #6
0
def clear_bokeh_memory():
    """Run garbage collection after bokeh plots in
    an attempt to reduce memory usage
    """
    curdoc().clear()
    state.State().reset()
    reset_output()
    gc.collect()
Beispiel #7
0
def plot(file_or_df,
         geoid_var,
         geoid_type,
         y_var,
         y_type,
         geolvl='county',
         geolabel='name',
         formatting=None,
         output=False,
         dropna=True):
    """Short summary.

    :param (str/pd.DataFrame) file_or_df: csv filepath or pandas/geopandas DataFrame with geoid_var
    :param (str) geoid_var: name of column containing the geo ID to match on
    :param (str) geoid_type: 'fips' (recommended), 'cbsa', 'name', or 'abbrev' (state only)
    :param (str) y_var: column name of variable to plot
    :param (str) y_type: 'sequential', 'sequential_single' (hue), 'divergent', or 'categorical'
    :param (str) geolvl: 'county' or 'state'
    :param (str) geolabel: column name to use. default is county/state name from shapefile
    :param (dict) formatting: if custom dict is passed, update DEFAULTFORMAT with those key-values
    :param (str) output: if specified, filepath to save html file. see save_plot().
    :param (bool) dropna: default True, if false, keeps rows where y_var is nan.
    :return: if output is 'bokeh', returns Bokeh object; else None """

    ## get default plot formatting and update if necessary
    temp_format = DEFAULTFORMAT.copy()
    if formatting:
        temp_format.update(formatting)

    ## process data
    shape_df = prc.shape_geojson(geolvl,
                                 temp_format['simplify'],
                                 epsg=temp_format['epsg'])
    geo_df = prc.merge_to_geodf(shape_df,
                                file_or_df,
                                geoid_var,
                                geoid_type,
                                geolvl=geolvl)

    if dropna:
        geo_df = geo_df[geo_df[y_var].notnull()]

    ## make sure column name does not have spaces -- important for hover tooltip
    geo_df.rename(columns={y_var: y_var.replace(' ', '_')}, inplace=True)
    y_var = y_var.replace(' ', '_')

    ## plot and save choropleth
    bkplot = initialize_plot(temp_format)
    draw_choropleth_layers(bkplot, geo_df, y_var, y_type, geolabel,
                           temp_format)

    if temp_format['svg']:
        bkplot.output_backend = 'svg'
    save_plot(bkplot, output)
    ## return to original state
    io.reset_output()

    return bkplot
Beispiel #8
0
def xyComparison(xdata,
                 ydata,
                 xlabel='Smoothness',
                 ylabel='Loss',
                 what='lossSmoothness',
                 outputDir='../results/dae/neuralNetwork/resultsAnalysis/'):

    N = len(xdata.values)
    counts = np.zeros((N, N))
    for value in xdata.values:
        i = ydata.values.tolist().index(value)  # i for line
        j = xdata.values.tolist().index(value)  # j for column
        counts[i, j] = 1

    xname = []
    yname = []
    color = []
    alpha = []
    for i, valY in enumerate(ydata):
        for j, valX in enumerate(xdata):
            xname.append(valX)
            yname.append(valY)
            if counts[i, j] == 1: color.append('red')
            else: color.append('white')
            alpha.append(1)

    source = ColumnDataSource(data=dict(xname=xname,
                                        yname=yname,
                                        colors=color,
                                        alphas=alpha,
                                        count=counts.flatten()))

    p = figure(x_axis_location="above",
               x_range=list(xdata),
               y_range=list(reversed(ydata.tolist())),
               tools=TOOLS)

    p.grid.grid_line_color = None
    p.axis.axis_line_color = None
    p.axis.visible = None

    p.rect('xname',
           'yname',
           0.9,
           0.9,
           source=source,
           color='colors',
           line_color=None)

    p.select_one(HoverTool).tooltips = [('conf', '@xname')]

    reset_output()
    output_notebook()
    show(p)

    output_file(outputDir + what + 'MatrixComparison.html')
    save(p)
    return True
Beispiel #9
0
def update_notebook():
    print('Repo: \'{}\': {}\nTotal commits: {}\n{} commits: {}'.format(
        cloned_repo.remotes.origin.name, cloned_repo.remotes.origin.url,
        len(list(cloned_repo.iter_commits('master'))), html_path,
        len(list(cloned_repo.iter_commits('master', paths=html_path)))))
    display(df_totals)
    display(df_2016_totals)
    display(mfig)
    reset_output()
    output_notebook()
    p = vote2020_bokeh(df_totals, df_2016_totals)
    show(p)
Beispiel #10
0
def setOutput(interactive, outputFileAddress):
    reset_output()
    if interactive:
        print('outputting interactive to notebook.')
        output_notebook()
    else:
        if outputFileAddress is None:
            # bokio.output_file('TempBokeh.png')
            print('outputting static to notebook.')
        else:
            output_file(outputFileAddress)
            print('outputting to file.')
def intfileplot():
    filesA = os.listdir(inputpath)
    filesB = []
    for y in filesA:
        if y[-8:] == "hits.csv" and y[-10:] != "nohits.csv" and y[
                -11:] != "isohits.csv":
            filesB.append(y)
    for y in filesB:
        data, hetclassintdf = FTPM.mycsvreader(inputpath + y)
        isodata = FTPM.isocsvreader(inputpath + y[:-8] + "isohits.csv")
        nodata = FTPM.nohitsreader(inputpath + y[:-8] + "nohits.csv")
        intplotter(data, isodata, nodata, y, hetclassintdf)
        reset_output()  #cleans up the cache which reduces file size
Beispiel #12
0
def hist(data, attribute, group, name='figure'):
    output = join(os.getcwd(), 'figures', name)
    output_file(output + '.html')

    x = np.linspace(0, data[attribute].max(), data[attribute].max())
    pdf = gaussian_kde(data[attribute])

    plot = figure(title=name, x_axis_label=attribute)

    hist, edges = np.histogram(data[attribute], density=True, bins=12)
    plot.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:], alpha=0.4)

    plot.line(x, pdf(x))

    save(plot)
    reset_output()
def intfileplot():
    filesA = os.listdir(inputdata)
    for y in filesA:
        if y[-4:] == "xlsx":
            df = pd.read_excel(inputdata + y)
            df = df.rename(columns={
                y[:-5]: 'RA',
                'Error_ppm': 'Error',
                'Mass': 'mz'
            })
            df['VKsize'] = np.sqrt((df["RA"] * 1E-5) / np.pi)
            nodata = df[df['Class'] == "None"]
            data = df[(df['C13'] == 0) & (df['Class'] != "None")]
            isodata = df[(df['C13'] == 1) & (df['Class'] != "None")]
            intplotter(data, isodata, nodata, y)  #,hetclassintdf)
            reset_output()  #cleans up the cache which reduces file size
    """
Beispiel #14
0
def bokeh_plot(site, obs, obs_label, data_dict, outfile, title, resample_frequency, add_statistics=True):
    '''
    Plots a bokeh (interactive) daily or weekly series. 
    Inputs:
    site: a string ID which is input to the obs and data_dict objects
    obs: a pandas dataframe with columns labeled with the site IDs
    data_dict: an ordered dictionary with keys each of the series you want to plot with structure:
    {'series_name': {'data': pandas_dataframe, 'color': 'the color you want the timeseries to plot as'}}
    outfile: path where you want to save the bokeh plot
    title: title string for your plot
    resample_frequency: either 'w' for weekly or 'd' for daily (this is used to decide the frequency of the plotting)
    add_statistics: if True (default) will include the statistics of KGE and bias in the plot's legend
    '''
    reset_output()
    resample_dict = {'W': 'Weekly',
                'D': 'Daily'}
    output_file(outfile, title=title)

    p1 = figure(width=1000, height=400, x_axis_type = "datetime", title=title)
    obs_resampled =obs.resample(resample_frequency, how='mean')
    p1.line(obs_resampled.index.values,obs_resampled.values/100000, color='#000000',  legend=obs_label, line_width=2)
    
    for scenario in data_dict.keys():
        sim = data_dict[scenario]['data'][site]
        daily_kge = str(np.round(kge(sim, obs, 'D'), 2))
        weekly_kge = str(np.round(kge(sim, obs, 'W'), 2))
        scenario_bias = str(np.round(bias(sim, obs),2))
        if add_statistics:
            legend_label = scenario+', Daily KGE='+daily_kge+',\nWeekly KGE='+weekly_kge+' Bias='+scenario_bias+'%'
        else:
            legend_label = scenario
        sim_resampled = data_dict[scenario]['data'][site].resample(resample_frequency, how='mean')
        p1.line(sim_resampled.index.values, sim_resampled.values/100000, 
            color=data_dict[scenario]['color'], line_width=2, 
                legend=legend_label)
    p1.grid.grid_line_alpha=0.3
    p1.xaxis.axis_label = 'Date'
    p1.yaxis.axis_label = 'Streamflow [CFS*100,000]'
    p1.legend.label_text_font_size = '8'

    window_size = 30
    window = np.ones(window_size)/float(window_size)
    g = vplot(p1)
    save(g)    
Beispiel #15
0
def plotCombinedPortfoliosPerf(
        stratValues,
        conf,
        kind='gains',  # Or returns
        outputDir='../results/dae/portfolios/resultsAnalysis/',
        benchmark=None):  # Portfolios of portfolios actually
    reset_output()
    output_notebook()

    numlines = stratValues.shape[1]
    colors_list = list(reversed(RdBu10[0:numlines]))

    p = figure(x_axis_type="datetime")
    p.yaxis.axis_label = kind

    source = ColumnDataSource(stratValues)
    legs = stratValues.columns.values.tolist()

    for (colr, leg) in zip(colors_list, legs):
        p.line(x=stratValues.index.values,
               y=leg,
               source=source,
               color=colr,
               legend=leg)

    hline = Span(location=0,
                 dimension='width',
                 line_color='black',
                 line_width=2)
    p.renderers.extend([hline])

    p.legend.orientation = "top_left"

    if benchmark != None:
        p.line(benchmark.gains.index.values,
               benchmark.gains['cumulReturn'],
               color='darkgreen',
               legend='benchmark')

    show(p)
    output_file(outputDir + conf + '_' + kind + '.html')
    save(p)
    return True
    def test_io_push_to_server(self):
        from bokeh.io import output_server, push, curdoc, reset_output
        application = Application()
        with ManagedServerLoop(application) as server:
            reset_output()
            doc = curdoc()
            doc.clear()

            client_root = SomeModelInTestClientServer(foo=42)

            session_id = 'test_io_push_to_server'
            output_server(session_id=session_id,
                          url=("http://localhost:%d/" % server.port))

            doc.add_root(client_root)
            push(io_loop=server.io_loop)

            server_session = server.get_session('/', session_id)

            print(repr(server_session.document.roots))

            assert len(server_session.document.roots) == 1
            server_root = next(iter(server_session.document.roots))

            assert client_root.foo == 42
            assert server_root.foo == 42

            # Now modify the client document and push
            client_root.foo = 57
            push(io_loop=server.io_loop)
            server_root = next(iter(server_session.document.roots))
            assert server_root.foo == 57

            # Remove a root and push
            doc.remove_root(client_root)
            push(io_loop=server.io_loop)
            assert len(server_session.document.roots) == 0

            # Clean up global IO state
            reset_output()
Beispiel #17
0
    def test_io_push_to_server(self):
        from bokeh.io import output_server, push, curdoc, reset_output
        application = Application()
        with ManagedServerLoop(application) as server:
            reset_output()
            doc = curdoc()
            doc.clear()

            client_root = SomeModelInTestClientServer(foo=42)

            session_id = 'test_io_push_to_server'
            output_server(session_id=session_id,
                          url=("http://localhost:%d/" % server.port))

            doc.add_root(client_root)
            push(io_loop=server.io_loop)

            server_session = server.get_session('/', session_id)

            print(repr(server_session.document.roots))

            assert len(server_session.document.roots) == 1
            server_root = next(iter(server_session.document.roots))

            assert client_root.foo == 42
            assert server_root.foo == 42

            # Now modify the client document and push
            client_root.foo = 57
            push(io_loop=server.io_loop)
            server_root = next(iter(server_session.document.roots))
            assert server_root.foo == 57

            # Remove a root and push
            doc.remove_root(client_root)
            push(io_loop=server.io_loop)
            assert len(server_session.document.roots) == 0

            # Clean up global IO state
            reset_output()
Beispiel #18
0
def hbar(data, attribute, index, category, name=None):
    output = join(os.getcwd(), 'figures', name)
    output_file(output + '.html')

    source = data.sort_values(by=attribute, ascending=True)
    cmap = factor_cmap(category,
                       palette=Accent5,
                       factors=sorted(source[category].unique()))

    plot = figure(y_range=source[index],
                  title=name,
                  x_axis_label=attribute,
                  tooltips=[(attribute.replace('_', ' '), f'@{attribute}')])
    plot.hbar(y=index,
              right=attribute,
              height=0.8,
              source=source,
              fill_color=cmap,
              line_color=cmap)

    save(plot)
    reset_output()
Beispiel #19
0
def plot_in_bokeh(x, y):
    reset_output()
    output_file("pathname")

    fig = figure(height=300,
                 width=900,
                 responsive=True,
                 x_axis_type="datetime")

    fig.scatter(x, y, legend="Test")

    fig.y_range = Range1d(10, 40)
    fig.yaxis.axis_label = "BrPM"
    '''
    # Setting the second y axis range name and range
    fig.extra_y_ranges = {"act_level": Range1d(start=0, end=2)}
    # Adding the second axis to the plot.
    fig.add_layout(LinearAxis(y_range_name="act_level",axis_label="Activity level"), 'right')
    
    fig.line(act_level_x, act_level_y, y_range_name="act_level", color="lightgrey", legend="activity level")
    '''
    show(fig)
Beispiel #20
0
def displayParameterEvolution(
        groupedData,
        metrics,
        parameter,
        configuration='concatParams',
        outputDir='../results/dae/neuralNetwork/resultsAnalysis/'):
    reset_output()
    output_notebook()

    grid = []
    subGrid = []
    for metric in metrics:
        p = figure(tools=TOOLS)
        p.xaxis.axis_label = parameter
        p.yaxis.axis_label = metric

        for name, group in groupedData:
            tmp = group.sort(columns=parameter)
            source = ColumnDataSource({
                parameter: tmp[parameter],
                metric: tmp[metric],
                'conf': tmp[configuration]
            })
            p.line(parameter, metric, source=source)

        p.select_one(HoverTool).tooltips = [('conf', '@conf')]

        subGrid.append(p)

    grid.append(subGrid)
    p = gridplot(grid)

    show(p)
    output_file(outputDir + parameter + '.html')
    save(p)
    return True
Beispiel #21
0
def save_to_disk(p, filename, title, show_inline=True):
    if show_inline:
        reset_output()
        output_notebook(resources=INLINE)
        show(p)
    reset_output()
    output_file(filename, title=title)
    save(p)
    reset_output()
    output_notebook(resources=INLINE)
Beispiel #22
0
 def test(self):
     io.reset_output()
     self.assertTrue(io._state.reset.called)
Beispiel #23
0
 def test(self):
     io.reset_output()
     self.assertTrue(io._state.reset.called)
Beispiel #24
0
def plotresults(cam, im, P, S, B, bbox):
    # Bokeh Plotting
    io.reset_output()
    io.output_file('bokeh plots.html', title='bokeh plots')
    h, w = im.shape
    n = P.shape[2]  # number of images
    xn = list(range(0, n, 1))
    # colors = bokeh_colors(n)
    colorbyframe = True
    colors = []
    for i in np.linspace(0, 255, n, dtype=int):
        colors.append(palettes.Viridis256[i])

    # Setup tools
    hoverImage = models.HoverTool(tooltips=[('(x, y)', '$x{1.11}, $y{1.11}')],
                                  point_policy='follow_mouse')

    # Plot image
    a = plotting.figure(x_range=(0, w),
                        y_range=(h, 0),
                        plot_width=round(600 * w / h),
                        plot_height=600,
                        x_axis_label='pixel (1 - ' + str(w) + ')',
                        y_axis_label='pixel (1 - ' + str(h) + ')',
                        title=cam['filename'],
                        tools='box_zoom,pan,save,reset,wheel_zoom',
                        active_scroll='wheel_zoom',
                        active_inspect=None)
    a.add_tools(hoverImage)
    a.image(image=[np.flip(im, 0)],
            x=0,
            y=h,
            dw=w,
            dh=h,
            palette=palettes.Greys256)
    a.quad(top=[h], bottom=[0], left=[0], right=[w],
           alpha=0)  # clear rectange hack for tooltip image x,y

    # Plog bounding box
    a.quad(top=bbox[3],
           bottom=bbox[2],
           left=bbox[0],
           right=bbox[1],
           color=colors[1],
           line_width=2,
           alpha=.3)

    # Plot license plate outline
    a.patch(P[0, 0:4, 0], P[1, 0:4, 0], alpha=0.3, line_width=4)

    # Plot points
    if colorbyframe:
        # i = ~np.isnan(P[0].ravel())
        # c = P[4].ravel()[i].astype(int)
        # fc = [colors[i] for i in c]
        # a.scatter(P[0].ravel()[i], P[1].ravel()[i], size=4, fill_color=fc, line_color=None)
        # a.scatter(P[2].ravel()[i], P[3].ravel()[i], size=9, fill_color=fc, fill_alpha=0.3, line_color=None)
        for i in range(n - 1):  # list((0, n - 1)):  # plot first and last
            # for i in range(n):  # plot all
            a.circle(P[0, :, i],
                     P[1, :, i],
                     color=colors[i],
                     legend='image ' + str(i),
                     line_width=1)
            a.circle(P[2, :, i],
                     P[3, :, i],
                     color=colors[i],
                     size=10,
                     alpha=.6)
    else:
        a.circle(P[0].ravel(),
                 P[1].ravel(),
                 color=colors[0],
                 legend='Points',
                 line_width=2)
        a.circle(P[2].ravel(),
                 P[3].ravel(),
                 color=colors[1],
                 legend='Reprojections',
                 size=10,
                 alpha=.6)

    # Plot 2 - 3d
    x, y, z = np.split(B[:, :3], 3, axis=1)
    b = plotting.figure(plot_width=350,
                        plot_height=300,
                        x_axis_label='Y (m)',
                        y_axis_label='X (m)',
                        title='Position',
                        tools='save,reset,hover',
                        active_inspect='hover')

    # Plot 3 - distance
    c = plotting.figure(x_range=(0, n),
                        y_range=(0, round(S[1:, 7].max())),
                        plot_width=350,
                        plot_height=300,
                        x_axis_label='image',
                        y_axis_label='distance (m)',
                        title='Distance = %.2fm in %.3fs' %
                        (S[-1, 7], S[-1, 5] - S[0, 5]),
                        tools='save,reset,hover',
                        active_inspect='hover')

    # Plot 4 - speed
    d = plotting.figure(x_range=(0, n),
                        y_range=(0, S[1:, 8].max() + 1),
                        plot_width=350,
                        plot_height=300,
                        x_axis_label='image',
                        y_axis_label='speed (km/h)',
                        title='Speed = %.2f +/- %.2f km/h' %
                        (S[1:, 8].mean(), S[1:, 8].std()),
                        tools='save,reset,hover',
                        active_inspect='hover')

    # Circles plots 2-4
    b.circle(0, 0, color=colors[-1], line_width=15)
    if colorbyframe:
        b.scatter(x.ravel(),
                  z.ravel(),
                  fill_color=colors,
                  line_color=colors,
                  size=10)
        c.scatter(xn[1:],
                  S[1:, 7],
                  fill_color=colors[1:],
                  line_color=colors[1:],
                  size=10)
        d.scatter(xn[1:],
                  S[1:, 8],
                  fill_color=colors[1:],
                  line_color=colors[1:],
                  size=10)
    else:
        b.circle(x.ravel(), z.ravel(), color=colors[0], line_width=2)
        c.circle(xn[1:], S[1:, 7], color=colors[0], line_width=2)
        d.circle(xn[1:], S[1:, 8], color=colors[0], line_width=2)

    # Plot lines
    a.multi_line(P[0].tolist(),
                 P[1].tolist(),
                 color='white',
                 alpha=.7,
                 line_width=1)

    # Show plot
    io.show(column(a, row(b, c, d)))  # open a browser
Beispiel #25
0
def assembly_chart(df, complements):
    """function to assembly the chart"""
    print('starting the plot...')

    # specify the output file name
    output_file("movigrama_chart.html")
    # force to show only one plot when multiples executions of the code occur
    # otherwise the plots will append each time one new calling is done
    reset_output()

    # create ColumnDataSource objects directly from Pandas data frames
    source = ColumnDataSource(df)

    # use the column DT as index
    df.set_index('DT', inplace=True)

    ###########################################################################
    #
    #  Movigrama Plot
    #
    ###########################################################################

    # build figure of the plot
    p = figure(x_axis_type='datetime',
               x_axis_label='days of moviment',
               y_axis_label='unities movimented',
               plot_width=1230,
               plot_height=500,
               active_scroll='wheel_zoom')

    # TODO Specify X range (not all plots have 365 days of moviment)

    # build the Stock Level bar
    r1 = p.vbar(x='DT',
                bottom=0,
                top='STOCK',
                width=pd.Timedelta(days=1),
                fill_alpha=0.4,
                color='paleturquoise',
                source=source)

    # build the OUT bar
    p.vbar(x='DT',
           bottom=0,
           top='SOMA_SAI',
           width=pd.Timedelta(days=1),
           fill_alpha=0.8,
           color='crimson',
           source=source)

    # build the IN bar
    p.vbar(x='DT',
           bottom=0,
           top='SOMA_ENTRA',
           width=pd.Timedelta(days=1),
           fill_alpha=0.8,
           color='seagreen',
           source=source)

    # edit title
    # adds warehouse title
    p.add_layout(
        Title(text=complements['warehouse'],
              text_font='helvetica',
              text_font_size='10pt',
              text_color='orangered',
              text_alpha=0.5,
              align='center',
              text_font_style="italic"), 'above')
    # adds product title
    p.add_layout(
        Title(text=complements['product'],
              text_font='helvetica',
              text_font_size='10pt',
              text_color='orangered',
              text_alpha=0.5,
              align='center',
              text_font_style="italic"), 'above')
    # adds main title
    p.add_layout(
        Title(text='Movigrama Endicon',
              text_font='helvetica',
              text_font_size='16pt',
              text_color='orangered',
              text_alpha=0.9,
              align='center',
              text_font_style="bold"), 'above')

    # adds horizontal line
    hline = Span(location=0,
                 line_alpha=0.4,
                 dimension='width',
                 line_color='gray',
                 line_width=3)
    p.renderers.extend([hline])

    # adapt the range to the plot
    p.x_range.range_padding = 0.1
    p.y_range.range_padding = 0.1

    # format the plot's outline
    p.outline_line_width = 4
    p.outline_line_alpha = 0.1
    p.outline_line_color = 'orangered'

    # format major labels
    p.axis.major_label_text_color = 'gray'
    p.axis.major_label_text_font_style = 'bold'

    # format labels
    p.axis.axis_label_text_color = 'gray'
    p.axis.axis_label_text_font_style = 'bold'

    #    p.xgrid.grid_line_color = None  # disable vertical bars
    #    p.ygrid.grid_line_color = None  # disable horizontal bars

    # change placement of minor and major ticks in the plot
    p.axis.major_tick_out = 10
    p.axis.minor_tick_in = -3
    p.axis.minor_tick_out = 6
    p.axis.minor_tick_line_color = 'gray'

    # format properly the X datetime axis
    p.xaxis.formatter = DatetimeTickFormatter(days=['%d/%m'],
                                              months=['%m/%Y'],
                                              years=['%Y'])

    # iniciate hover object
    hover = HoverTool()
    hover.mode = "vline"  # activate hover by vertical line
    hover.tooltips = [("SUM-IN", "@SOMA_ENTRA"), ("SUM-OUT", "@SOMA_SAI"),
                      ("COUNT-IN", "@TRANSACT_ENTRA"),
                      ("COUNT-OUT", "@TRANSACT_SAI"), ("STOCK", "@STOCK"),
                      ("DT", "@DT{%d/%m/%Y}")]
    # use 'datetime' formatter for 'DT' field
    hover.formatters = {"DT": 'datetime'}
    hover.renderers = [r1]  # display tolltip only to one render
    p.add_tools(hover)

    ###########################################################################
    #
    #  Demand analysis
    #
    ###########################################################################

    # change to positive values
    df['out_invert'] = df['SOMA_SAI'] * -1
    # moving average with n=30 days
    df['MA30'] = df['out_invert'].rolling(30).mean().round(0)
    # moving standard deviation with n=30 days
    df['MA30_std'] = df['out_invert'].rolling(30).std().round(0)
    # lower control limit for 1 sigma deviation
    df['lcl_1sigma'] = (df['MA30'] - df['MA30_std'])
    # upper control limit for 1 sigma deviation
    df['ucl_1sigma'] = (df['MA30'] + df['MA30_std'])

    source = ColumnDataSource(df)

    p1 = figure(plot_width=1230,
                plot_height=500,
                x_range=p.x_range,
                x_axis_type="datetime",
                active_scroll='wheel_zoom')

    # build the Sum_out bar
    r1 = p1.vbar(x='DT',
                 top='out_invert',
                 width=pd.Timedelta(days=1),
                 color='darkred',
                 line_color='salmon',
                 fill_alpha=0.4,
                 source=source)

    # build the moving average line
    p1.line(x='DT', y='MA30', source=source)

    # build the confidence interval
    band = Band(base='DT',
                lower='lcl_1sigma',
                upper='ucl_1sigma',
                source=source,
                level='underlay',
                fill_alpha=1.0,
                line_width=1,
                line_color='black')
    p1.renderers.extend([band])

    # adds title
    p1.add_layout(
        Title(text='Demand Variability',
              text_font='helvetica',
              text_font_size='16pt',
              text_color='orangered',
              text_alpha=0.5,
              align='center',
              text_font_style="bold"), 'above')

    # adds horizontal line
    hline = Span(location=0,
                 line_alpha=0.4,
                 dimension='width',
                 line_color='gray',
                 line_width=3)
    p1.renderers.extend([hline])

    # format the plot's outline
    p1.outline_line_width = 4
    p1.outline_line_alpha = 0.1
    p1.outline_line_color = 'orangered'

    # format major labels
    p1.axis.major_label_text_color = 'gray'
    p1.axis.major_label_text_font_style = 'bold'

    # format labels
    p1.axis.axis_label_text_color = 'gray'
    p1.axis.axis_label_text_font_style = 'bold'

    # change placement of minor and major ticks in the plot
    p1.axis.major_tick_out = 10
    p1.axis.minor_tick_in = -3
    p1.axis.minor_tick_out = 6
    p1.axis.minor_tick_line_color = 'gray'

    # format properly the X datetime axis
    p1.xaxis.formatter = DatetimeTickFormatter(days=['%d/%m'],
                                               months=['%m/%Y'],
                                               years=['%Y'])

    # iniciate hover object
    hover = HoverTool()
    hover.mode = "vline"  # activate hover by vertical line
    hover.tooltips = [("DEMAND", '@out_invert'), ("UCL 1σ", "@ucl_1sigma"),
                      ("LCL 1σ", "@lcl_1sigma"), ("M AVG 30d", "@MA30"),
                      ("DT", "@DT{%d/%m/%Y}")]
    # use 'datetime' formatter for 'DT' field
    hover.formatters = {"DT": 'datetime'}
    hover.renderers = [r1]  # display tolltip only to one render
    p1.add_tools(hover)

    ###########################################################################
    #
    #  Demand groupped by month
    #
    ###########################################################################

    resample_M = df.iloc[:, 0:6].resample('M').sum()  # resample to month
    # create column date as string
    resample_M['date'] = resample_M.index.strftime('%b/%y').values
    # moving average with n=3 months
    resample_M['MA3'] = resample_M['out_invert'].rolling(3).mean()

    resample_M['MA3'] = np.ceil(resample_M.MA3)  # round up the column MA3
    # resample to month with mean
    resample_M['mean'] = np.ceil(resample_M['out_invert'].mean())
    # resample to month with standard deviation
    resample_M['std'] = np.ceil(resample_M['out_invert'].std())
    # moving standard deviation with n=30 days
    resample_M['MA3_std'] = np.ceil(resample_M['out_invert'].rolling(3).std())
    # lower control limit for 1 sigma deviation
    resample_M['lcl_1sigma'] = resample_M['MA3'] - resample_M['MA3_std']
    # upper control limit for 1 sigma deviation
    resample_M['ucl_1sigma'] = resample_M['MA3'] + resample_M['MA3_std']

    source = ColumnDataSource(resample_M)

    p2 = figure(plot_width=1230,
                plot_height=500,
                x_range=FactorRange(factors=list(resample_M.date)),
                title='demand groupped by month')

    colors = factor_cmap('date',
                         palette=Category20_20,
                         factors=list(resample_M.date))

    p2.vbar(x='date',
            top='out_invert',
            width=0.8,
            fill_color=colors,
            fill_alpha=0.8,
            source=source,
            legend=value('OUT'))

    p2.line(x='date',
            y='MA3',
            color='red',
            line_width=3,
            line_dash='dotted',
            source=source,
            legend=value('MA3'))

    p2.line(x='date',
            y='mean',
            color='blue',
            line_width=3,
            line_dash='dotted',
            source=source,
            legend=value('mean'))

    band = Band(base='date',
                lower='lcl_1sigma',
                upper='ucl_1sigma',
                source=source,
                level='underlay',
                fill_alpha=1.0,
                line_width=1,
                line_color='black')

    labels1 = LabelSet(x='date',
                       y='MA3',
                       text='MA3',
                       level='glyph',
                       y_offset=5,
                       source=source,
                       render_mode='canvas',
                       text_font_size="8pt",
                       text_color='darkred')

    labels2 = LabelSet(x='date',
                       y='out_invert',
                       text='out_invert',
                       level='glyph',
                       y_offset=5,
                       source=source,
                       render_mode='canvas',
                       text_font_size="8pt",
                       text_color='gray')

    low_box = BoxAnnotation(
        top=resample_M['mean'].iloc[0] -
        resample_M['std'].iloc[0],  # analysis:ignore
        fill_alpha=0.1,
        fill_color='red')
    mid_box = BoxAnnotation(
        bottom=resample_M['mean'].iloc[0] -
        resample_M['std'].iloc[0],  # analysis:ignore
        top=resample_M['mean'].iloc[0] +
        resample_M['std'].iloc[0],  # analysis:ignore
        fill_alpha=0.1,
        fill_color='green')
    high_box = BoxAnnotation(
        bottom=resample_M['mean'].iloc[0] +
        resample_M['std'].iloc[0],  # analysis:ignore
        fill_alpha=0.1,
        fill_color='red')

    p2.renderers.extend([band, labels1, labels2, low_box, mid_box, high_box])
    p2.legend.click_policy = "hide"
    p2.legend.background_fill_alpha = 0.4

    p2.add_layout(
        Title(text='Demand Grouped by Month',
              text_font='helvetica',
              text_font_size='16pt',
              text_color='orangered',
              text_alpha=0.5,
              align='center',
              text_font_style="bold"), 'above')

    # adds horizontal line
    hline = Span(location=0,
                 line_alpha=0.4,
                 dimension='width',
                 line_color='gray',
                 line_width=3)
    p2.renderers.extend([hline])

    # format the plot's outline
    p2.outline_line_width = 4
    p2.outline_line_alpha = 0.1
    p2.outline_line_color = 'orangered'

    # format major labels
    p2.axis.major_label_text_color = 'gray'
    p2.axis.major_label_text_font_style = 'bold'

    # format labels
    p2.axis.axis_label_text_color = 'gray'
    p2.axis.axis_label_text_font_style = 'bold'

    # change placement of minor and major ticks in the plot
    p2.axis.major_tick_out = 10
    p2.axis.minor_tick_in = -3
    p2.axis.minor_tick_out = 6
    p2.axis.minor_tick_line_color = 'gray'

    # iniciate hover object
    # TODO develop hoverTool
    #    hover = HoverTool()
    #    hover.mode = "vline"  # activate hover by vertical line
    #    hover.tooltips = [("SUM-IN", "@SOMA_ENTRA"),
    #                      ("SUM-OUT", "@SOMA_SAI"),
    #                      ("COUNT-IN", "@TRANSACT_ENTRA"),
    #                      ("COUNT-OUT", "@TRANSACT_SAI"),
    #                      ("STOCK", "@STOCK")]
    #    hover.renderers = [r1]  # display tolltip only to one render
    #    p2.add_tools(hover)

    ###########################################################################
    #
    #  Plot figures
    #
    ###########################################################################

    # put the results in a column and show
    show(column(p, p1, p2))

    # show(p)  # plot action

    print('plot finished')
Beispiel #26
0
 def connect_to_server(self):
     try: 
         output_server("dreaml")
     except ConnectionError: 
         reset_output()
         print "Failed to connect to bokeh server"
Beispiel #27
0
    def plot(self,
             front: List[S],
             reference: List[S] = None,
             output: str = '',
             show: bool = True) -> None:
        # This is important to purge front (if any) between calls
        reset_output()

        # Set up figure
        self.figure_xy = Figure(output_backend='webgl',
                                sizing_mode='scale_width',
                                title=self.plot_title,
                                tools=self.plot_tools)
        self.figure_xy.scatter(x='x',
                               y='y',
                               legend='solution',
                               fill_alpha=0.7,
                               source=self.source)
        self.figure_xy.xaxis.axis_label = self.xaxis_label
        self.figure_xy.yaxis.axis_label = self.yaxis_label

        x_values, y_values, z_values = self.get_objectives(front)

        if self.number_of_objectives == 2:
            # Plot reference solution list (if any)
            if reference:
                ref_x_values, ref_y_values, _ = self.get_objectives(reference)
                self.figure_xy.line(x=ref_x_values,
                                    y=ref_y_values,
                                    legend='reference',
                                    color='green')

            # Push front to server
            self.source.stream({
                'x': x_values,
                'y': y_values,
                'str': [s.__str__() for s in front]
            })
            self.doc.add_root(column(self.figure_xy))
        else:
            # Add new figures for each axis
            self.figure_xz = Figure(title='xz',
                                    output_backend='webgl',
                                    sizing_mode='scale_width',
                                    tools=self.plot_tools)
            self.figure_xz.scatter(x='x',
                                   y='z',
                                   legend='solution',
                                   fill_alpha=0.7,
                                   source=self.source)
            self.figure_xz.xaxis.axis_label = self.xaxis_label
            self.figure_xz.yaxis.axis_label = self.zaxis_label

            self.figure_yz = Figure(title='yz',
                                    output_backend='webgl',
                                    sizing_mode='scale_width',
                                    tools=self.plot_tools)
            self.figure_yz.scatter(x='y',
                                   y='z',
                                   legend='solution',
                                   fill_alpha=0.7,
                                   source=self.source)
            self.figure_yz.xaxis.axis_label = self.yaxis_label
            self.figure_yz.yaxis.axis_label = self.zaxis_label

            # Plot reference solution list (if any)
            if reference:
                ref_x_values, ref_y_values, ref_z_values = self.get_objectives(
                    reference)
                self.figure_xy.line(x=ref_x_values,
                                    y=ref_y_values,
                                    legend='reference',
                                    color='green')
                self.figure_xz.line(x=ref_x_values,
                                    y=ref_z_values,
                                    legend='reference',
                                    color='green')
                self.figure_yz.line(x=ref_y_values,
                                    y=ref_z_values,
                                    legend='reference',
                                    color='green')

            # Push front to server
            self.source.stream({
                'x': x_values,
                'y': y_values,
                'z': z_values,
                'str': [s.__str__() for s in front]
            })
            self.doc.add_root(
                row(self.figure_xy, self.figure_xz, self.figure_yz))

        self.client.push(self.doc)

        if output:
            self.__save(output)
        if show:
            self.client.show()
Beispiel #28
0
def plot_process_tree(
    data: pd.DataFrame,
    schema: ProcSchema = None,
    output_var: str = None,
    legend_col: str = None,
    show_table: bool = False,
    **kwargs,
) -> Tuple[figure, LayoutDOM]:
    """
    Plot a Process Tree Visualization.

    Parameters
    ----------
    data : pd.DataFrame
        DataFrame containing one or more Process Trees
    schema : ProcSchema, optional
        The data schema to use for the data set, by default None
        (if None the schema is inferred)
    output_var : str, optional
        Output variable for selected items in the tree,
        by default None
    legend_col : str, optional
        The column used to color the tree items, by default None
    show_table: bool
        Set to True to show a data table, by default False.

    Other Parameters
    ----------------
    height : int, optional
        The height of the plot figure
        (the default is 700)
    width : int, optional
        The width of the plot figure (the default is 900)
    title : str, optional
        Title to display (the default is None)

    Returns
    -------
    Tuple[figure, LayoutDOM]:
        figure - The main bokeh.plotting.figure
        Layout - Bokeh layout structure.

    Raises
    ------
    ProcessTreeSchemaException
        If the data set schema is not valid for the plot.

    Notes
    -----
    The `output_var` variable will be overwritten with any selected
    values.

    """
    check_kwargs(kwargs, _DEFAULT_KWARGS)
    reset_output()
    output_notebook()

    data, schema, levels, n_rows = _pre_process_tree(data, schema)
    if schema is None:
        raise ProcessTreeSchemaException(
            "Could not infer schema from data set.")

    source = ColumnDataSource(data=data)
    # Get legend/color bar map
    fill_map, color_bar = _create_fill_map(source, legend_col)

    max_level = max(levels) + 3
    min_level = min(levels)
    plot_height: int = kwargs.pop("height", 700)
    plot_width: int = kwargs.pop("width", 900)
    title: str = kwargs.pop("title", "ProcessTree")

    if color_bar:
        title += " (color bar = {legend_col})"
    visible_range = int(plot_height / 35)
    y_start_range = (n_rows - visible_range, n_rows + 1)
    b_plot = figure(
        title=title,
        plot_width=plot_width,
        plot_height=plot_height,
        x_range=(min_level, max_level),
        y_range=y_start_range,
        tools=["ypan", "reset", "save", "tap"],
        toolbar_location="above",
    )

    hover = HoverTool(tooltips=_get_tool_tips(schema),
                      formatters={"TimeGenerated": "datetime"})
    b_plot.add_tools(hover)

    # dodge to align rectangle with grid
    rect_x = dodge("Level", 1.75, range=b_plot.x_range)
    rect_plot_params = dict(width=3.5,
                            height=0.95,
                            source=source,
                            fill_alpha=0.4,
                            fill_color=fill_map)

    if color_bar:
        b_plot.add_layout(color_bar, "right")
    elif legend_col:
        rect_plot_params["legend_field"] = legend_col
    rect_plot = b_plot.rect(x=rect_x, y="Row", **rect_plot_params)
    if legend_col and not color_bar:
        b_plot.legend.title = legend_col

    text_props = {
        "source": source,
        "text_align": "left",
        "text_baseline": "middle"
    }

    def x_dodge(x_offset):
        return dodge("Level", x_offset, range=b_plot.x_range)

    def y_dodge(y_offset):
        return dodge("Row", y_offset, range=b_plot.y_range)

    b_plot.text(x=x_dodge(0.1),
                y=y_dodge(-0.2),
                text="cmd",
                text_font_size="7pt",
                **text_props)
    b_plot.text(x=x_dodge(0.1),
                y=y_dodge(0.25),
                text="Exe",
                text_font_size="8pt",
                **text_props)
    b_plot.text(x=x_dodge(1.8),
                y=y_dodge(0.25),
                text="PID",
                text_font_size="8pt",
                **text_props)

    # Plot options
    _set_plot_option_defaults(b_plot)
    b_plot.xaxis.ticker = sorted(levels)
    b_plot.xgrid.ticker = sorted(levels)
    b_plot.hover.renderers = [rect_plot]  # only hover element boxes

    # Selection callback
    if output_var is not None:
        get_selected = _create_js_callback(source, output_var)
        b_plot.js_on_event("tap", get_selected)
        box_select = BoxSelectTool(callback=get_selected)
        b_plot.add_tools(box_select)

    range_tool = _create_vert_range_tool(
        data=source,
        min_y=0,
        max_y=n_rows,
        plot_range=b_plot.y_range,
        width=90,
        height=plot_height,
        x_col="Level",
        y_col="Row",
        fill_map=fill_map,
    )
    plot_elems = row(b_plot, range_tool)
    if show_table:
        data_table = _create_data_table(source, schema, legend_col)
        plot_elems = column(plot_elems, data_table)
    show(plot_elems)
    return b_plot, plot_elems
Beispiel #29
0
def _return_html_comps(plots):
    script, divs = components(plots)
    reset_output()
    return script, divs, get_bokeh_version()
Beispiel #30
0
def tradingRepartitionDay(portfolio, absGain, conf):
    reset_output()
    output_file(
        '../results/dae/portfolios/resultsAnalysis/tradingRepartitionDay_' +
        conf + '.html')

    count, sell, buyy, days = getMetricsDay(portfolio, absGain)

    colorRange = OrRd9[0:5] + list(reversed(YlGn9[0:5]))

    colors = []
    for day in absGain.index.values:
        if absGain[day] < -5000: colors.append(0)
        elif absGain[day] >= -5000 and absGain[day] <= -2500: colors.append(1)
        elif absGain[day] > -2500 and absGain[day] <= -1000: colors.append(2)
        elif absGain[day] > -1000 and absGain[day] <= -500: colors.append(3)
        elif absGain[day] > -500 and absGain[day] < 0: colors.append(4)
        elif absGain[day] == 0: colors.append(-1)
        elif absGain[day] > 0 and absGain[day] <= 500: colors.append(5)
        elif absGain[day] > 500 and absGain[day] <= 1000: colors.append(6)
        elif absGain[day] > 1000 and absGain[day] <= 2500: colors.append(7)
        elif absGain[day] > 2500 and absGain[day] <= 5000: colors.append(8)
        elif absGain[day] > 5000: colors.append(9)

    xRange = []
    yRange = []
    for line in range(int(np.floor(absGain.shape[0] / 20) + 1)):
        for column in range(20):
            xRange.append(str(column))
            yRange.append(str(line))

    type_color = []
    for i, color in enumerate(colors):
        if color != -1: type_color.append(colorRange[colors[i]])
        else: type_color.append('grey')

    source = ColumnDataSource(data=dict(xGroup=xRange,
                                        yGroup=yRange,
                                        absGain=absGain,
                                        days=days,
                                        count=count,
                                        sell=sell,
                                        buyy=buyy,
                                        type_color=type_color))

    p = figure(
        title="Trading repartition by day",
        tools=TOOLS,
        x_range=[str(x) for x in range(20)],
        y_range=list(
            reversed([
                str(x)
                for x in range(int(np.floor(portfolio.shape[0] / 20) + 1))
            ])))
    p.outline_line_color = None
    p.plot_width = 1800
    p.plot_height = 1800
    p.rect('xGroup',
           'yGroup',
           0.9,
           0.9,
           source=source,
           fill_alpha=0.6,
           color="type_color")

    text_props = {
        "source": source,
        "angle": 0,
        "color": "black",
        "text_align": "center",
        "text_baseline": "middle"
    }

    p.text(x='xGroup',
           y='yGroup',
           text='days',
           text_font_size="9pt",
           **text_props)
    p.axis.visible = None
    p.grid.grid_line_color = None

    p.select_one(HoverTool).tooltips = [("day", "@days"),
                                        ("% actions involved", "@count"),
                                        ("sold", "@sell"), ("bought", "@buyy"),
                                        ("absGain", "@absGain")]

    save(p)
    show(p)
    return True
Beispiel #31
0
def tradingRepartitionAction(portfolio, conf):
    reset_output()
    output_notebook()

    count, minn, maxx, sellPerc, buyyPerc, name = getMetricsAction(portfolio)

    colorRange = list(reversed(PuBu9))

    colors = []
    for stock in portfolio.columns.values:
        if count[stock] < 0.01: colors.append(0)
        elif count[stock] >= 0.01 and count[stock] <= 0.4: colors.append(1)
        elif count[stock] > 0.4 and count[stock] <= 0.5: colors.append(2)
        elif count[stock] > 0.5 and count[stock] <= 0.6: colors.append(3)
        elif count[stock] > 0.6 and count[stock] <= 0.7: colors.append(4)
        elif count[stock] > 0.7 and count[stock] <= 0.8: colors.append(5)
        elif count[stock] > 0.8 and count[stock] <= 0.9: colors.append(6)
        elif count[stock] > 0.9 and count[stock] <= 0.95: colors.append(7)
        elif count[stock] > 0.95: colors.append(8)

    xRange = []
    yRange = []
    for line in range(int(np.floor(portfolio.shape[1] / 10) + 1)):
        for column in range(10):
            xRange.append(str(column))
            yRange.append(str(line))

    source = ColumnDataSource(data=dict(
        xGroup=xRange,
        yGroup=yRange,
        name=name,
        count=count,
        minn=minn,
        maxx=maxx,
        sellPerc=sellPerc,
        buyyPerc=buyyPerc,
        type_color=[colorRange[colors[i]] for i in range(portfolio.shape[1])]))

    p = figure(
        title="Trading repartition by action",
        tools=TOOLS,
        x_range=[str(x) for x in range(10)],
        y_range=list(
            reversed([
                str(x)
                for x in range(int(np.floor(portfolio.shape[1] / 10) + 1))
            ])))
    p.outline_line_color = None
    p.plot_width = 1200
    p.rect('xGroup',
           'yGroup',
           0.9,
           0.9,
           source=source,
           fill_alpha=0.6,
           color="type_color")

    text_props = {
        "source": source,
        "angle": 0,
        "color": "black",
        "text_align": "center",
        "text_baseline": "middle"
    }

    p.text(x='xGroup',
           y='yGroup',
           text='name',
           text_font_size="9pt",
           **text_props)
    p.axis.visible = None
    p.grid.grid_line_color = None

    p.select_one(HoverTool).tooltips = [("name", "@name"),
                                        ("participation", "@count"),
                                        ("min weight", "@minn"),
                                        ("max weight", "@maxx"),
                                        ("sellPerc", "@sellPerc"),
                                        ("buyyPerc", "@buyyPerc")]

    show(p)
    output_file(
        '../results/dae/portfolios/resultsAnalysis/tradingRepartitionAction_' +
        conf + '.html')
    save(p)
    return True
Beispiel #32
0
def make_interactive_scatter_plot(df,
                                  x_col='coord_x',
                                  y_col='coord_y',
                                  color_col=None,
                                  size_col=None,
                                  hover_cols=[],
                                  palette_type='categorical',
                                  color_bar=False,
                                  plot_width=1200,
                                  plot_height=800,
                                  max_lvls=20,
                                  plot_title='Title',
                                  plot_name='test.html'):
    """
    Makes and saves interactive scatter plot to a file. 
        df: data frame for plotting 
        color_col: what column of df as the color label 
        x_col: what column of df to plot along x axis 
        y_col: what column of df to plot along y axis 
        hover_cols: what columns of df to come up when hovering over point 
        palette_type: either 'categorical' or 'sequential'
        plot_width: how wide is the output plot 
        plot_height: how high is the output plot 
        max_lvls: maximum number of categorical levels to plot. Chooses different palettes depending on the levels. Must be between 2 and 40 
        plot_title: Title of the output plot 
        plot_name: What name/path to save the output plot 
    """
    from bokeh.io import output_file, show, reset_output
    from bokeh.plotting import figure, show
    from bokeh.models import HoverTool, ColumnDataSource, CategoricalColorMapper, BasicTicker
    from bokeh.transform import transform
    reset_output()
    source = ColumnDataSource(data=df)
    if palette_type == 'categorical':
        if color_bar:
            raise ValueError(
                "Must set palette_type='sequential' if specifying a color bar")
        n_levels = len(df[color_col].unique())
        factors = sorted(list(df[color_col].unique()))
        colors = get_palette(min(n_levels, max_lvls),
                             palette_type=palette_type)
        color_mapper = CategoricalColorMapper(factors=factors, palette=colors)
    elif palette_type == 'sequential':
        colors = get_palette(9, palette_type=palette_type)
        color_mapper = LinearColorMapper(low=df[color_col].min(),
                                         high=df[color_col].max(),
                                         palette=colors)
    hover_tooltips = [(o, "@" + o) for o in hover_cols]
    hover = HoverTool(tooltips=hover_tooltips)
    p = figure(plot_width=plot_width,
               plot_height=plot_height,
               tools=[hover, 'pan', 'box_zoom', 'wheel_zoom', 'reset'],
               title=plot_title)
    if size_col is None:
        p.circle(x_col,
                 y_col,
                 size=10,
                 source=source,
                 fill_color=transform(color_col, color_mapper))
    else:
        p.circle(x_col,
                 y_col,
                 size=size_col,
                 source=source,
                 fill_color=transform(color_col, color_mapper))
    if color_bar:
        color_bar = ColorBar(color_mapper=color_mapper,
                             ticker=BasicTicker(),
                             location=(0, 0))
        p.add_layout(color_bar, 'right')
    output_file(plot_name)
    show(p)
def graph(gui, daysList, nightsList, masterBlock, plotVertices = True, showDragWarn = False):
	bulkvertices = []
	vertX        = []
	vertY        = []
	masterX      = []
	masterY      = []
	airX         = []
	airY         = []
	monResList   = []
	
	#Clears previous graphs from memory
	reset_output()
	
	if plotVertices:
		#Add daytime period delimiting lines if appropriate
		if gui.restrictSearchBV.get() is True:
			for day in daysList:
				if len(day.vertices) > 0:
					#Add vertex at 0 to mark begining of day
					bulkvertices.append(coreC.vert(day.vertices[0].index, np.nan, day.vertices[0].vertType))
					#Get all vertices from bouts
					bulkvertices += day.vertices
					#Add vertex at 0 to mark end of day
					bulkvertices.append(coreC.vert(day.vertices[-1].index, np.nan, day.vertices[-1].vertType))
		#Else compile vertices for entire input file
		else:
			bulkvertices = masterBlock.vertices
			
		#Compile index number and temperature of all vertices
		for vertex in bulkvertices:
			vertX.append(gui.masterList[vertex.index][gui.dataPointCol])
			vertY.append(vertex.temp)
			
		#Output to HTML file
		output_file(gui.graphName)
	else:
		output_file(os.path.join(gui.coreDir, "misc_files", "tempGraph.html"))
	
	#Create column data source for vertex data so it can be manipulated in the interactive plot
	boutsSourceData = ColumnDataSource(data = dict(x = vertX, y = vertY))
	
	#Compile index number and temperature for every line in input file
	for line in gui.masterList:
		masterX.append(int(line[gui.dataPointCol]))
		masterY.append(float(line[gui.tempsCol]))
		
		#Compile air temperature if a valid column is present
		if gui.airValid is True:
			airX.append(int(line[gui.dataPointCol]))
			airY.append(float(line[gui.airTempCol]))
		
	
	#Set plot dimensions
	if gui.plotDimVar.get() is 1:
		#Get monitor resolution if "auto" selected
		for monitor in get_monitors():
			monResList.append(str(monitor))	
		
		#Extract monitor width and height
		resRE = re.search("(\d+)x(\d+)", monResList[0])
		monX = int(resRE.group(1))
		monY = int(resRE.group(2))
		plotX = (monX - 200)
		plotY = (monY - 200)
	#Get user dimension values if "manual" selected
	else:
		plotX = int(gui.plotDimXE.get())
		plotY = int(gui.plotDimYE.get())
	
	#Initialize hover tool (provides information about individual datapoints)
	hover = HoverTool(tooltips=[("Data Point", "$x{int}"), ("Temperature", "$y")])

	#(flag) may not be neccessary
	# for y in masterY:
		# float(y)
	
	#Get name of plot
	if plotVertices:
		plotName = gui.graphName[:-5]
	else:
		#Get input file root name
		tempRE = re.search((".*\."), os.path.basename(os.path.normpath(gui.inputFileAdvE.get())))
		#Assign default plot name
		if tempRE:
			plotName = tempRE.group(0)[:-1]
		else:
			plotName = os.path.basename(os.path.normpath(gui.inputFileAdvE.get()))

	#Detect best y axis range
	if gui.airValid is True:
		yMin = (float(min(min(masterY), min(airY))) - 2)
		yMax = (float(max(max(masterY), max(airY))) + 2)
	else:
		yMin = (float(min(masterY)) - 2)
		yMax = (float(max(masterY)) + 2)
		
	#Create core plot
	plot  = figure(tools = [hover, "box_zoom, wheel_zoom, pan, reset, save"],
				   x_range = [int(masterX[0]), int(masterX[len(masterX) - 1])],
				   y_range = [yMin, yMax],
				   # y_range = [float(min((float(min(airY)), float(min(masterY)))) - 2), (max(max(airY), max(masterY)) + 2)], 
				   title = plotName,
				   x_axis_label= "Data Point", y_axis_label = "Temperature (C)", plot_width = plotX, plot_height = plotY)
				   
	
	#Add vertical lines delimiting days
	if gui.showDayDelimsBV.get() is True:
		for day in daysList:
			vline = Span(location = int(gui.masterList[day.start][gui.dataPointCol]), dimension = "height",
				    line_color = gui.dayMarkerColorVar.get(), line_width = float(gui.dayMarkerSize_E.get()), line_alpha = 0.6)
			plot.renderers.extend([vline])
	
	#Get size settings from GUI
	plot.title.text_font_size = (gui.titleFontSizeE.get() + "pt")
	plot.axis.axis_label_text_font_size  = (gui.axisTitleFontSizeE.get() + "pt")
	plot.axis.major_label_text_font_size  = (gui.axisLabelFontSizeE.get() + "pt")	
	plot.axis.major_tick_line_width = int(gui.axisTickSizeE.get())
	plot.axis.minor_tick_line_width = int(gui.axisTickSizeE.get())
	plot.axis.major_tick_out = int(gui.axisTickSizeE.get())
	plot.axis.minor_tick_out = int(gui.axisTickSizeE.get())

	#Append vertex x and y values to data dictionary
	data = {"x": [], "y": []}
	for x in vertX: data["x"].append(x)
	for y in vertY: data["y"].append(y)
	
	#Generate table with vertex information
	src = ColumnDataSource(data)
	columns = [
				TableColumn(field = "x", title = "Data Point"),
				TableColumn(field = "y", title = "Temperature")
			  ]
	data_table = DataTable(source = src, columns = columns, width = 500, height = 20000)
	
	if gui.editModeBV.get():
		#Show drag warning if applicable
		if showDragWarn:
			dragWarn(gui)
			
		#Plot air temperatures
		if gui.showAirTempBV.get() is True and gui.airValid:
			# plot.circle(airX, airY, size = 5, color = "black", alpha = 1, legend = "Air Temeprature")
			plot.line(airX, airY, line_width = float(gui.airTempSize_E.get()), color = gui.airTempColorVar.get(), line_alpha = 1, legend = "Air Temeprature")
		
		#Plot egg temperatures
		plot.circle(masterX, masterY, size = float(gui.eggTempPointSize_E.get()), color = gui.eggTempPointColorVar.get(), legend = "Egg Temperature")
		plot.line(masterX, masterY, line_width = float(gui.eggTempLineSize_E.get()), color = gui.eggTempLineColorVar.get())

		#Plot verticies as large circles if edit mode selected
		renderer = plot.circle  (
									"x",
									"y",
									size = (float(gui.boutSize_E.get()) * 3),
									color = "black",
									fill_color = gui.boutColorVar.get(),
									fill_alpha = 0.8,
									legend = "Prediced Incubation Shift",
									source = src
								)
		
		draw_tool = PointDrawTool(renderers = [renderer], empty_value = 1)
		plot.add_tools(draw_tool)
		plot.toolbar.active_drag = draw_tool
		
		
			
	#Else plot verticies as lines
	else:		
		#Plot air temperatures
		if gui.showAirTempBV.get() is True and gui.airValid:
			# plot.circle(airX, airY, size = 5, color = "black", alpha = 1, legend = "Air Temeprature")
			plot.line(airX, airY, line_width = float(gui.airTempSize_E.get()), color = gui.airTempColorVar.get(), line_alpha = 1, legend = "Air Temeprature")
			
		#Plot egg temperatures
		plot.circle(masterX, masterY, size = float(gui.eggTempPointSize_E.get()), color = gui.eggTempPointColorVar.get(), legend = "Egg Temperature")	
		plot.line(masterX, masterY, line_width = float(gui.eggTempLineSize_E.get()), color = gui.eggTempLineColorVar.get())
			
		#Add vertices
		plot.line("x", "y", line_width = float(gui.boutSize_E.get()), legend = "Detected Bout", line_color = gui.boutColorVar.get(), source = boutsSourceData)
	
	if gui.showGridBV.get() is False:
		plot.grid.visible = False
	
	plot.legend.label_text_font_size = (gui.legendFontSizeE.get() + "pt")
	plot.background_fill_color = None
	plot.border_fill_color = None
	# plot.outline_color = None
			
	#Generate plot
	show(column(plot, widgetbox(data_table)))
Beispiel #34
0
    return df


#path = r'D:/Documents/DataScience/Portfolio/criticalrole_webscraper_knowledgegraph/'
result_df = pd.read_csv('critrole_w2v_model_output.csv')
result_df = DataFrame_StringOfInts2ListOfInts(result_df,
                                              ['word_most_similar_indices'])
#result_df['word_most_similar_indices'] = result_df['word_most_similar_indices'].apply(ast.literal_eval)
result_df['coords'] = list(zip(result_df['dim0'], result_df['dim1']))

result_pos = result_df['coords'].to_dict()
result_links = result_df['word_most_similar_indices'].to_dict()
result_cds = ColumnDataSource(result_df)
G = nx.from_dict_of_lists(result_links)

reset_output()
#output_notebook()
#output_file('critrole_w2v_visualizer.html')

# We could use figure here but don't want all the axes and titles
plot = Plot(x_range=Range1d(-2, 2), y_range=Range1d(-2, 2))

# Create a Bokeh graph from the NetworkX input using nx.spring_layout
#nx.set_node_attributes(G, result_pos, 'coord')
graph = from_networkx(G, layout_function=result_pos)

graph.node_renderer.glyph = Circle(size=4, fill_color='#2b83ba', line_width=0)
graph.edge_renderer.glyph = MultiLine(line_color="#cccccc",
                                      line_alpha=0.8,
                                      line_width=0)
Beispiel #35
0
def modify_doc(doc):
    reset_output(state=None)
    print("Entering modify_doc ...")
    global ecg_idx, ecg_filtered, crr_idx

    df = pd.read_csv(os.path.join(
        app.config['UPLOAD_FOLDER'], app.config['CUR_FILE']))
    ecg_clean = df['P02S ECG']
    ecg_idx = [i for i in range(len(ecg_clean))]
    ecg_filtered = bandpass(ecg_clean, lf, hf, 250, 2, False)
    ecg_filtered = np.interp(
        ecg_filtered, (ecg_filtered.min(), ecg_filtered.max()), (-1, +1))
    # ecg_filtered = scale(ecg_filtered, axis=0, with_mean=True,
    #                     with_std=True, copy=True)
    min_g = min(ecg_filtered)
    max_g = max(ecg_filtered)

    file_name_div = Div(text='Processing on file: ' +
                        app.config['CUR_FILE'].split('/')[-1])

    div = Div(text="Quality")
    radio_group = RadioGroup(
        labels=["Good", "Boderline", "Poor"], active=2)
    button_nxt_unit = Button(label="Next Unit", button_type="success")
    skip_unit = RadioGroup(
        labels=["Save This Unit", "Skip This Unit"], active=0)

    marker_line_st = ColumnDataSource(data=dict(x=[0, 0], y=[min_g, max_g]))
    marker_line_en = ColumnDataSource(data=dict(x=[0, 0], y=[min_g, max_g]))
    s2 = ColumnDataSource(data=dict(x=[], y=[]))

    sx = figure(width=1300, height=300, title="ecg_filtered "+str(lf) +
                "-"+str(hf)+"Hz", x_axis_label='time', y_axis_label='acc')
    sx.line(ecg_idx, ecg_filtered, legend="ecg_filtered "+str(lf) +
            "-"+str(hf)+"Hz", line_color="red", line_width=1)
    sx.line('x', 'y', source=marker_line_st,
            legend="current unit ", line_color="blue", line_width=1)
    sx.line('x', 'y', source=marker_line_en,
            legend="current unit ", line_color="blue", line_width=1)
    sx1 = figure(width=800, height=300, title="ecg_filtered "+str(lf) +
                 "-"+str(hf)+"Hz", x_axis_label='time', y_axis_label='acc')
    sx1.line('x', 'y', source=s2, legend="ecg_filtered unit "+str(lf) +
             "-"+str(hf)+"Hz", line_color="red", line_width=1)

    peaks, _ = find_peaks(ecg_filtered, distance=150)

    crr_idx = 0
    marked_file = open(os.path.join(
        app.config['MARKED_FOLDER'], app.config['CUR_FILE'][:-4] + '_' + 
        str(datetime.datetime.now().strftime("%Y-%m-%d_%H:%M:%S")) + '.txt'), 'w')
    marked_file.write(app.config['CUR_FILE'] + '\n')
    marked_file.write('P02S ECG' + '\n')
    marked_file.write(str(len(peaks) - 1) + '\n')
    res_df = {}
    
    def my_nxt_unit_handler():
        global crr_idx, ecg_filtered

        if (crr_idx < len(peaks) - 1):

            #save unit data
            if(skip_unit.active == 0 and crr_idx > 0):
                out_fn = os.path.join(
                    app.config['MARKED_FOLDER'], radio_group.labels[radio_group.active],
                    app.config['CUR_FILE'][:-4]+'_P2SECG_'+str(crr_idx)+'.json')
                print(out_fn)
                with open(out_fn, "w") as write_file:
                    json.dump(res_df, write_file)

            st = int(max(0, peaks[crr_idx] -
                         (peaks[crr_idx + 1] - peaks[crr_idx]) / 2))
            en = int(
                min(peaks[crr_idx] + (peaks[crr_idx + 1] - peaks[crr_idx]) / 2, ecg_idx[-1]))
            unit_idx = ecg_idx[st:en]
            unit_data = ecg_filtered[st:en]
            s2.data['x'] = unit_idx
            s2.data['y'] = unit_data

            marker_line_st.data['x'] = [st, st]
            marker_line_en.data['x'] = [en, en]

            print("crr marked quality: ", radio_group.labels[radio_group.active])

            marked_file.write(str(st) + '\t' + str(en) + '\t' + str(en - st) + '\t' +
                              str(radio_group.labels[radio_group.active]) + '\n')
            
            
            res_df['file_name'] = app.config['CUR_FILE']
            res_df['unit_number'] = crr_idx
            res_df['size'] = en - st +1
            res_df['data'] = list(unit_data)
            res_df['signal_name'] = 'P02S ECG'
            res_df['quality'] = radio_group.labels[radio_group.active]

            crr_idx += 1

        if crr_idx == len(peaks)-1:
            marked_file.close()

    button_nxt_unit.on_click(my_nxt_unit_handler)
    graphs = column(
        list(itertools.chain.from_iterable([[sx, sx1, widgetbox(div)]])))

    doc.add_root(widgetbox(file_name_div))
    doc.add_root(graphs)
    doc.add_root(row(widgetbox(radio_group), widgetbox(
        button_nxt_unit), widgetbox(skip_unit)))
    doc.theme = Theme(filename=os.path.join(THIS_FILE_PATH, "theme.yaml"))