예제 #1
0
def plot_asweep(df, plot_stress=False):
    """ 
    Function to plot amplitude sweeps

    ARGS : 
    - df [PANDAS.DATAFRAME] : your sliced rheology data
    - steps [LIST] : your step(s) you want to plot (can be only one step)

    OUTPUT :
    - a figure (d'uh !)
    """

    steps = np.unique(df['step'])
    f1 = figure(x_axis_type='log',
                y_axis_type='log',
                title='Amplitude Sweep',
                tooltips=[('x', '$x'), ('y', '$y')])
    cmap, cmap_line = magma(np.size(steps)), _darken(magma(np.size(steps)))

    for no, step in enumerate(steps):
        # Check that step makes sense
        amp_sweep = df[df['step'] == step]
        if amp_sweep.iloc[0]['steptype'] != 'Amplitudesweep':
            print('plot_asweep > Warning : Cannot confirm that step ' +
                  str(step) + ' is an amplitude sweep')
        f1.scatter(x='strain',
                   y='gprime',
                   marker='square',
                   line_color=cmap[no],
                   fill_color=cmap_line[no],
                   source=amp_sweep,
                   legend_label="G' , step " + str(step))
        f1.scatter(x='strain',
                   y='gsecond',
                   marker='o',
                   line_color=cmap[no],
                   fill_color='white',
                   source=amp_sweep,
                   legend_label="G'' , step " + str(step))
        if plot_stress:
            f1.scatter(x='strain',
                       y='stress',
                       marker='triangle',
                       line_color=cmap_line[no],
                       fill_color=cmap[no],
                       source=amp_sweep,
                       legend_label='σ , step ' + str(step))

    f1.xaxis.axis_label, f1.yaxis.axis_label = 'γ (%)', 'G, σ (Pa)'
    f1.legend.location = "bottom"

    show(f1)
    time.sleep(0.5)

    return f1
예제 #2
0
def plot_tsweep(df, plot_stress=False):
    """ 
    Function to plot time sweeps

    ARGS : 
    - df [PANDAS.DATAFRAME] : your sliced rheology data
    - plot_stress [BOOL] [OPTIONAL] : add the global oscillatory stress to the plot

    OUTPUT :
    - a figure (d'uh !)
    """

    steps = np.unique(df['step'])
    f1 = figure(x_axis_type='log',
                y_axis_type='log',
                title='Time Sweep',
                tooltips=[('x', '$x'), ('y', '$y')])
    cmap, cmap_line = magma(np.size(steps)), _darken(magma(np.size(steps)))

    for no, step in enumerate(steps):
        # Check that step makes sense
        time_sweep = df[df['step'] == step]
        if time_sweep.iloc[0]['steptype'] != 'Timesweep':
            print('plot_tsweep > Cannot confirm that step ' + str(step) +
                  ' is a time sweep')
        f1.scatter(x='time',
                   y='gprime',
                   marker='square',
                   line_color=cmap_line[no],
                   fill_color=cmap[no],
                   source=time_sweep,
                   legend_label="G' , step " + str(step))
        f1.scatter(x='time',
                   y='gsecond',
                   marker='o',
                   line_color=cmap_line[no],
                   fill_color='white',
                   source=time_sweep,
                   legend_label="G'' , step " + str(step))
        if plot_stress:
            f1.scatter(x='frequency',
                       y='stress',
                       marker='triangle',
                       line_color=cmap_line[no],
                       fill_color=cmap[no],
                       source=time_sweep,
                       legend_label='σ , step ' + str(step))

    f1.xaxis.axis_label, f1.yaxis.axis_label = 't (s)', 'G, σ (Pa)'
    f1.legend.location = "left"

    show(f1)
    time.sleep(0.5)
    return f1
예제 #3
0
def plot_flowcurve(df, fit_up_to=500):
    """ 
    Function to plot flow curve steps

    ARGS : 
    - df [PANDAS.DATAFRAME] : your sliced rheology data
    - fit_up_to [FLOAT] [OPTIONAL] : fit the flow curves up to some specific shear rate value 

    OUTPUT :
    - a figure (d'uh !)
    - FIT [Nx4 list] : the fit to your steps, with [step, sigma_Y, K, exponent]
    """
    steps = np.unique(df['step'])
    fits_all = np.zeros((len(steps), 4))

    # Create and format figure
    f1 = figure(x_axis_type='log',
                y_axis_type='log',
                title='Flow Curve',
                tooltips=[('x', '$x'), ('y', '$y')])
    f1.xaxis.axis_label, f1.yaxis.axis_label = 'γ (1/s)', 'τ (Pa)'
    cmap, cmap_line = magma(np.size(steps)), _darken(magma(np.size(steps)))

    for no, step in enumerate(steps):
        # Check that step makes sense
        flow_curve = df[df['step'] == step]
        if flow_curve.iloc[0]['steptype'] != 'Flowcurve':
            print('plot_flowcurve >  Warning : Cannot confirm that step ' +
                  str(step) + ' is a flow curve.')
        ys, K, exponent = _fit_HB(flow_curve['shearrate'],
                                  flow_curve['stress'],
                                  fit_up_to=fit_up_to)
        fits_all[no, :] = [step, ys, K, exponent]

        # Plotting + Display + producing the fitted curve
        shearrates = flow_curve['shearrate']
        fitted_stress = ys + K * shearrates**exponent
        f1.line(shearrates, fitted_stress, line_color=cmap[no])
        f1.scatter(x='shearrate',
                   y='stress',
                   marker='square',
                   source=flow_curve,
                   line_color=cmap_line[no],
                   fill_color=cmap[no],
                   legend_label="Flow curve, step " + str(step))
        print('plot_flowcurve > fit for step ' + str(step) + ' : τ = ' +
              '{:3.2f}'.format(ys) + ' + ' + '{:3.2f}'.format(K) + ' γ^(' +
              '{:3.2f}'.format(exponent) + ')')
    show(f1)
    time.sleep(0.5)

    return fits_all, f1
예제 #4
0
 def update_colors(self, use_gradient):
     if use_gradient:
         mx = np.max(self.display_df['total'])
         if mx > 1:
             mx = np.ceil(10 * np.log2(mx)) / 10
             if not np.isnan(mx):
                 self.display_df['color'] = [
                     magma(256)[int(np.log2(tot) * 255 / mx)]
                     for tot in self.display_df['total']
                 ]
     else:
         self.display_df['color'] = magma(4)[0]
     if self.built:
         self.source.data = self.display_df.to_dict(orient='list')
예제 #5
0
def plot_normalforce(df):
    """ 
    Function to plot flow normal forces (as a function of gamma_dot by default, but gamma also possible)

    ARGS : 
    - df [PANDAS.DATAFRAME] : your sliced rheology data
    - steps [LIST] : your step(s) you want to plot (can be only one step)

    OUTPUT :
    - a figure (d'uh !)
    """
    steps = np.unique(df['step'])

    # Create and format figure
    f1 = figure(x_axis_type='log',
                y_axis_type='linear',
                title='Normal Force',
                tooltips=[('x', '$x'), ('y', '$y')])
    f1.y_range.start, f1.y_range.end = -1, 1
    cmap, cmap_line = magma(np.size(steps)), _darken(magma(np.size(steps)))

    for no, step in enumerate(steps):
        # Check that step makes sense
        df_now = df[df['step'] == step]

        if df_now.iloc[0]['steptype'] == 'Flowcurve':
            print('plot_normalforce > Step ' + str(step) +
                  ' is a flow curve. Plotting N = f(gamma_dot)')
            f1.scatter(df_now['shearrate'],
                       df_now['normalforce'],
                       line_color=cmap_line[no],
                       fill_color=cmap[no],
                       marker='o',
                       legend_label='Step ' + str(step))
            f1.xaxis.axis_label, f1.yaxis.axis_label = 'γ^dot (1/s)', 'F_N (N)'
        else:
            print('plot_normalforce > Step ' + str(step) +
                  ' not a flow curve. Plotting N = f(gamma)')
            f1.scatter(df_now['strain'],
                       df_now['normalforce'],
                       line_color=cmap[no],
                       fill_color=cmap[no],
                       marker='diamond',
                       legend_label='Step ' + str(step))
            f1.xaxis.axis_label, f1.yaxis.axis_label = 'γ (1)', 'F_N (N)'

    show(f1)
    time.sleep(0.5)

    return f1
예제 #6
0
    def prep_palette(self, pname, binverse=False):
        """
        Prepares a palette based on a name
        :param pname:
        :return:
        """
        res = palettes.grey(256)

        if pname == 'Greys256':
            res = palettes.grey(256)
        elif pname == 'Inferno256':
            res = palettes.inferno(256)
        elif pname == 'Magma256':
            res = palettes.magma(256)
        elif pname == 'Plasma256':
            res = palettes.plasma(256)
        elif pname == 'Viridis256':
            res = palettes.viridis(256)
        elif pname == 'Cividis256':
            res = palettes.cividis(256)
        elif pname == 'Turbo256':
            res = palettes.turbo(256)
        elif pname == 'Bokeh8':
            res = palettes.small_palettes['Bokeh'][8]
        elif pname == 'Spectral11':
            res = palettes.small_palettes['Spectral'][11]
        elif pname == 'RdGy11':
            res = palettes.small_palettes['RdGy'][11]
        elif pname == 'PiYG11':
            res = palettes.small_palettes['PiYG'][11]

        if binverse:
            res = res[::-1]
        return res
예제 #7
0
    def make_dataset(Music):
        #con = sql.connect('/Users/aaronwestmoreland/Documents/Data.Projects/Music/Metacritic.db')

        #Music = psql.read_sql('SELECT * FROM Music;', con)

        Mus_split = Music["Day"].str.split(' ', expand=True)
        Music["Month"] = Mus_split[0]
        Music["Date"] = Mus_split[1]

        c_map = bp.magma(len(np.unique(Music["Score"])))
        c_dict = zip(np.unique(Music["Score"]), c_map)
        Col_MAP = dict(c_dict)
        Music["color"] = Music["Score"].apply(lambda x: Col_MAP[x])

        #Music["Month"]

        Music["DateTime"] = pd.to_datetime(Music["Release"],
                                           format="%b %d, %Y")

        Music['alpha'] = np.where(Music["Score"].astype(float) >= 90, 0.9,
                                  0.25)

        mu_dict = dict(
            x=Music["Release"],
            y=Music["Score"].astype(float),
            album=Music["Album"],
            release=Music["Release"],
            artist=Music["Artist"],
            alpha=Music["alpha"],
            color=Music['color'],
        )
        return ColumnDataSource(mu_dict)
def test_cmap_generator_function():
    assert pal.viridis(256) == pal.Viridis256
    assert pal.magma(256) == pal.Magma256
    assert pal.plasma(256) == pal.Plasma256
    assert pal.inferno(256) == pal.Inferno256
    assert pal.gray(256) == pal.Greys256
    assert pal.grey(256) == pal.Greys256
예제 #9
0
def bar(x, y, x_label, y_label,bar_width):
    plot = figure(x_range = x,
                  title = y_label + ' vs. ' + x_label,
                  plot_width = 600,
                  plot_height = 600)
    plot.vbar(x = x, top = y, width = bar_width, color = palettes.magma(len(x)))
    return components(plot)
예제 #10
0
def test_cmap_generator_function():
    assert pal.viridis(256) == pal.Viridis256
    assert pal.magma(256) == pal.Magma256
    assert pal.plasma(256) == pal.Plasma256
    assert pal.inferno(256) == pal.Inferno256
    assert pal.gray(256) == pal.Greys256
    assert pal.grey(256) == pal.Greys256
    assert pal.turbo(256) == pal.Turbo256
    assert pal.diverging_palette(pal.Reds9, pal.Greys9, n=18, midpoint=0.5) == pal.Reds9 + pal.Greys9[::-1]
예제 #11
0
def create_kw_freq_bar_graphs(raw_dataframe,
                              start_date=0,
                              end_date=0,
                              freq_threshold=3):
    """
    Creates a keyword frequency bar graph from dataframe and exports it in html with name of newspaper and creation
    timestamp
    Default keyword frequency threshold set at 3
    Can select time scope with start_date and end_date variables, if not set defaults to include all given data
    """
    # Slicing Dataframe to begin at start_date
    if start_date == 0:
        start_date = raw_dataframe['Datetime'].min()
    else:
        start_date = start_date
    raw_dataframe = raw_dataframe[raw_dataframe['Datetime'] >= start_date]
    # Slicing Dataframe to end at end_date
    if end_date == 0:
        end_date = raw_dataframe['Datetime'].min()
    else:
        end_date = end_date
    raw_dataframe = raw_dataframe[raw_dataframe['Datetime'] <= end_date]

    newspapers = raw_dataframe['Newspaper'].unique().tolist()
    for np_name in newspapers:
        df = create_keyword_ranking_df(
            raw_dataframe[raw_dataframe['Newspaper'] == np_name],
            freq_threshold)
        # Creating figure
        p1 = figure(title=f'Keyword ranking {np_name}',
                    x_range=df['keyword'],
                    width=1000,
                    height=300,
                    sizing_mode='scale_width')

        # Setting axis names and properties
        p1.xaxis.major_label_orientation = 4 / 3
        p1.yaxis.axis_label = 'Frequency'
        p1.xaxis.axis_label = 'Keyword'
        p1.xaxis.axis_label_text_font_size = '18pt'
        palette = magma(len(df))
        # Creating bar elements
        p1.vbar(x=df['keyword'],
                top=df['frequency'],
                width=0.5,
                fill_color=palette,
                line_color='black')

        # Adding hover tools
        p1.add_tools(HoverTool(tooltips=[('Count', '@top{0}')]))

        # Setting file name and path
        filename = f'Kw_freq_{np_name.lower()}_{start_date}_to_{end_date}.html'
        path = 'html_graphs/' + filename
        html = file_html(p1, CDN, 'kw_frequency_plot')
        with open(path, 'w+') as file:
            file.write(html)
    def default_palette(self):
        from bokeh import palettes

        palette_options = {
            'viridis': palettes.viridis(256),
            'magma': palettes.magma(256),
            'coolwarm': cc.coolwarm,
        }

        return palette_options[self.settings.get('palette', 'viridis')]
예제 #13
0
def plot_startup(df, reversal=False, malvern=True):
    """
    Function that helps plot shear startup and reversals

    ARGS : 
    - df [PANDAS.DATAFRAME] : your sliced rheology data
    - reversal [BOOL] [DEFAULT : FALSE] : bunch your startup data two-by-two and 
    consider that every odd (even) step is forward (reverse) done consecutively
    - malvern [BOOL] [DEFAULT : TRUE] : specify this to avoid issues with the shear
    stress not being negative during shear reversal tests.

    OUTPUT :
    - a figure (d'uh !)
    """

    f1 = figure(title='Shear Startup / Reversal')
    steps = np.unique(df['step'])
    strain_offset = np.zeros(len(steps) + 1)
    cmap = magma(np.size(steps))

    # Fix things that are messed up first ...
    if reversal:
        steps_forward = steps[::2]
        steps_reverse = steps[1::2]
        if malvern:
            df = fix_stress_malvern_reversal(df, steps_reverse)
        else:
            df = fix_strain_antonpaar(df, steps)
    else:
        steps_forward = steps

    # Plot things, be careful with strain offsets
    for no, s in enumerate(steps):
        strain, stress = df[df['step'] == s]['strain'], df[df['step'] ==
                                                           s]['stress']
        avgrate = np.mean(df[df['step'] == s]['shearrate'])
        if s in steps_forward and reversal:  # No need for strain offsets if no reversal ...
            strain_offset[no + 1] = df[df['step'] == s]['strain'].iloc[-1]

        f1.line(strain + strain_offset[no],
                stress,
                line_color=cmap[no],
                line_width=1,
                legend_label='gdot = ' + '{:3.2e}'.format(avgrate))

    strslim, strnlim = np.max(np.abs(df['stress'])), np.max(
        np.abs(df['strain']))
    f1.line([0, strnlim], [0, 0], line_dash='dotted', line_color='black')
    f1.line([strnlim / 2, strnlim / 2], [-strslim, strslim],
            line_dash='dotted',
            line_color='black')
    f1.xaxis.axis_label, f1.yaxis.axis_label = 'gamma (%)', 'sigma (Pa)'

    show(f1)
    return f1
예제 #14
0
def prepare_plots3(target_vals,epoch, METRICSPATH):

    cfm_file = f"{METRICSPATH}/confusion-{epoch}.png"
    bok_file = f"{METRICSPATH}/ranking_{epoch}.html"
   

    classes, rankings, preds, pred_rank = [],[],[],[]
    
    for t_ in target_vals:
        cl_,ra_,pr_,em_ = t_
        classes.append(cl_)
        rankings.append(ra_)
        preds.append(pr_)
        pred_rank.append(em_)



    classes = np.squeeze(np.concatenate(classes))
    rankings = np.squeeze(np.concatenate(rankings))
    predictions = np.concatenate([softmax(p,axis=0) for p in preds])
    pred_rank = np.concatenate(pred_rank)
    
    activations = np.argmax(predictions,axis=1) 
    conf_mat = confusion_matrix(classes,activations)
    fig = plt.figure(figsize=[10,8])
    plot_confusion_matrix(conf_mat, classes=class_names, normalize=False,
                      title=f'Confusion matrix epoch {epoch}')
    plt.savefig(cfm_file,format="png")
    pil_image = fig2pil(fig)
    neptune.send_image('conf_mat', pil_image)


    df = pd.DataFrame(data={ 'tar': rankings, 'pred': pred_rank, 'class': classes})

    
    palette = magma(num_of_classes + 1)
    p = figure(plot_width=600, plot_height=800, title=f"Ranking by exercise, epoch {epoch}")
    p.xgrid.grid_line_color = None
    p.xaxis.axis_label = 'Target ranking'
    p.yaxis.axis_label = 'Predicted ranking'
    

    
    for cl in range(num_of_classes):
        if cl == 6:
            continue
        df2 = df.loc[df['class']==cl]
        p.circle(x=jitter('tar',0.3), y='pred', size=8, alpha=0.1, color=palette[cl], legend=class_names[cl], source=df2 )
        p.line(x='tar', y='pred', line_width=2, alpha=0.5, color=palette[cl],legend=class_names[cl], source=df2.groupby(by="tar").mean())
    p.legend.location = "top_left"
    p.legend.click_policy="hide"
    output_file(bok_file, title="Ranking by exercise")
    save(p)
    pil_image2 = get_screenshot_as_png(p)
    neptune.send_image('rank_distances', pil_image2)
예제 #15
0
def make_earning_plot(data, symbol=''):
    num_of_year = len(data['year'].unique())
    colors = magma(num_of_year)
    mapper = LinearColorMapper(palette=colors,
                               low=data.year.max(),
                               high=data.year.min())

    source = ColumnDataSource(data)
    plot = figure(
        x_range=Range1d(0, 5),
        plot_height=500,
        plot_width=1000,
    )
    reported = plot.circle(
        x='quarter',
        y='reported',
        source=source,
        name='reported',
        legend='Reported',
        size=20,
        color=transform('year', mapper),
        fill_color=transform('year', mapper),
    )
    estimated = plot.circle(
        x='quarter',
        y='estimate',
        source=source,
        name='estimated',
        legend='Estimated',
        size=15,
        color=transform('year', mapper),
        fill_alpha=0.4,
        muted_alpha=0.1,
    )
    price_hover = HoverTool(renderers=[reported],
                            tooltips=[
                                ("year: ", "@year"),
                                ("reported: ", "@reported"),
                                ("estimate: ", "@estimate"),
                                ("surprise: ", "@surprise{0.2f}"),
                                ("surprise_per: ", "@surprise_per"),
                            ])

    plot.add_tools(price_hover)
    plot.title.text = f"{symbol.upper()} Earning" if symbol else "Earning"
    plot.legend.location = "top_left"
    plot.xaxis.axis_label = 'Quarter'
    plot.yaxis.axis_label = 'Price'
    plot.ygrid.band_fill_color = "olive"
    plot.ygrid.band_fill_alpha = 0.1
    plot.legend.click_policy = "mute"
    plot.xaxis.ticker = [1, 2, 3, 4]
    return plot
예제 #16
0
def genColors(n, ptype='magma'):
    """
    """
    from bokeh.palettes import magma, inferno, plasma, viridis
    if ptype == 'magma':
        return magma(n)
    elif ptype == 'inferno':
        return inferno(n)
    elif ptype == 'plasma':
        return plasma(n)
    else:
        return viridis(n)
예제 #17
0
 def colorPalette(self, size=None):
     # https://bokeh.pydata.org/en/latest/docs/reference/palettes.html
     # https://bokeh.pydata.org/en/latest/docs/reference/colors.html
     color = list(d3['Category10'][10]) # [ 'orangered', 'cornflowerblue',  ]
     # color = list(Spectral9).reverse()
     if size is None:
         return color
     elif size <= len(color):
         return color[0:size]
     elif size <= 256:
         return linear_palette(plasma(256), size)
     else:
         return linear_palette(plasma(256) + viridis(256) + magma(256), size)
예제 #18
0
def get_colormap(num_of_items):
    """
    Project:         'ICOS Carbon Portal'
    Created:          Fri Apr 04 17:00:00 2019
    Last Changed:     Fri Apr 04 17:00:00 2019
    Version:          1.0.0
    Author(s):        Karolina Pantazatou
    
    Description:      Function that takes an integer representing the total number of
                      items that should receive a sepparate color and  returns a colormap
                      (i.e. a list of strings, where every string represents a different
                      color in hexadecimal code) with the same amount of colors.
                      The function uses color palettes (namely Colorblind & magma) from
                      the Bokeh visualization library and can return colormaps for
                      1 - 256 items.
                      
    Input parameters: Number of items to be colored in a sepparate color
                      (var_name: 'num_of_items', var_type: Integer)

    Output:           List of strings (colormap)
    
    """

    #Check input:
    if (isinstance(num_of_items, int)):

        #import module:
        from bokeh.palettes import all_palettes, Colorblind, magma

        #Check the number of items to be colored (1-2 items):
        if ((num_of_items > 0) and (num_of_items < 3)):
            return ['#2b83ba', '#fdae61']  #return colormap selection

        #Check the number of items to be colored (3-8 items):
        elif ((num_of_items > 2) and (num_of_items < 9)):
            return all_palettes['Colorblind'][
                num_of_items]  #return colormap selection

        #Check the number of items to be colored (9-256 items):
        elif ((num_of_items > 8) and (num_of_items < 257)):
            return magma(num_of_items)  #return colormap selection

        #If the number of items exceeds the number 256:
        else:
            print(
                'Error! Number of items to be colored is zero or higher than 256.'
            )

    #If the input parameter is not an integer:
    else:
        print('Input is not an integer.')
예제 #19
0
def plot_creep(df, x_axis_type='log', y_axis_type='log', plot_shearrate=False):
    """
    Function that plots creep data

    ARGS : 
    - df [PANDAS.DATAFRAME] : your sliced rheology data
    - y_axis_type [STR] [DEFAULT : 'log'] : whether y axis is log or lin
    - x_axis_type [STR] [DEFAULT : 'log'] : whether x axis is log or lin
    - plot_shearrate [BOOL] [DEFAULT : False] : whether to plot gamma dot instead of gamma

    OUTPUT :
    - a figure (d'uh !)
    """

    f1 = figure(title='Creep',
                x_axis_type=x_axis_type,
                y_axis_type=y_axis_type)
    steps = np.unique(df['step'])
    cmap = magma(np.size(steps))

    for no, cr in enumerate(steps):
        strn = df.loc[df['step'] == cr, 'strain']
        sr = df.loc[df['step'] == cr, 'shearrate']
        tm = df.loc[df['step'] == cr, 'time']
        strs = df.loc[df['step'] == cr, 'stress'].to_numpy().mean()

        if plot_shearrate:
            f1.line(tm,
                    sr,
                    line_color=cmap[no],
                    line_width=1,
                    legend_label='Step ' + str(cr) + ', sigma = ' +
                    '{:4.2f}'.format(strs) + ' Pa')
            f1.yaxis.axis_label = 'gamma^dot (1/s)'
            f1.legend.location = 'bottom_left'
        else:
            f1.line(tm,
                    strn,
                    line_color=cmap[no],
                    line_width=1,
                    legend_label='Step ' + str(cr) + ', sigma = ' +
                    '{:4.2f}'.format(strs) + ' Pa')
            f1.line([1e-3, np.max(strn)], [1e-3, np.max(strn)],
                    line_dash='dotted',
                    line_color='black')
            f1.yaxis.axis_label = 'gamma (%)'
            f1.legend.location = 'top_left'

    f1.xaxis.axis_label = 'time (s)'
    show(f1)
    return f1
예제 #20
0
def create_figure(n):

    mass_spec = defaultdict(list)
    int_fict = np.arange(50)
    order_loc = np.ones(50)

    n = int(n)
    choices = ['Todos', 'Ninguno', 'Retroexcavdora, Camion']
    lista_zonas = [(1.0, 'Zona ' + str(k), np.random.choice(choices))
                   for k in range(n)]

    for scale, mz, evt_list in lista_zonas:
        mass_spec["RT"].append(order_loc)
        order_loc = order_loc + 1
        mass_spec["RT_intensity"].append(int_fict *
                                         scale)  #norm(loc=120.4).pdf(RT_x) *
        mass_spec['Eventos_permitidos'].append(evt_list)
        mass_spec['Intensity_tip'].append(mz)
        mass_spec['rel_int'].append(np.random.normal(loc=0.9, scale=0.01))
    mass_spec['color'] = magma(n)

    source = ColumnDataSource(mass_spec)

    x_limit_left = n + 0.1

    p = figure(plot_width=800, plot_height=600, x_range=(0, x_limit_left))
    hover_t = HoverTool(show_arrow=False,
                        tooltips=[('Eventos permitidos',
                                   '@Eventos_permitidos'),
                                  ('Intensidad relativa', '@rel_int')])
    p.add_tools(hover_t)
    p.image_url(url=['static/mapa.png'],
                x=0,
                y=0,
                w=x_limit_left,
                h=49,
                anchor='bottom_left')
    ml = p.multi_line(xs='RT',
                      ys='RT_intensity',
                      legend="Intensity_tip",
                      line_width=5,
                      line_color='color',
                      line_alpha=0.6,
                      hover_line_color='pink',
                      hover_line_alpha=1.0,
                      source=source)
    p.xaxis.bounds = (0, n)
    p.yaxis.visible = False

    return p
예제 #21
0
    def aggregate(self):
        agg = self.df.groupby(['F', 'A']).agg(['count', 'max', 'min'])
        self.display_df = pd.DataFrame({
            'F': agg['fer']['max'],
            'A': agg['agin']['min'],
            'total': agg['fer']['count']
        })

        date_strings = [
            generate_tooltip_data(x)
            for x in zip(agg['Date']['min'], agg['Date']['max'])
        ]
        self.display_df['first'] = [x[0] for x in date_strings]
        self.display_df['last'] = [x[1] for x in date_strings]
        self.display_df['color'] = magma(4)[0]
예제 #22
0
def get_color(min_val, max_val, n, val):
    '''
    Gets the background and text color for rendering a cell with value val in a
    matrix with n values and min and max values min_val and max_val,
    respectively.

    Args:
        min_val (float) Smallest value in the matrix.
        max_val (float) Largest value in the matrix.
        n (int) Number of values in the matrix.
        val (float|int) Value of cell in question.

    Returns:
        (str, str) The background and text color
    '''
    # The result should be memoized so we don't have to keep computing the full
    # range of values, but oh well. Choose from: magma, inferno, plasma,
    # viridis.
    top = float(max_val - min_val)
    black = magma(2)[0]
    white = 'white'

    # If the matrix is basically all the same value just return black.
    if np.isclose(0.0, top):
        return black, white

    # Compute which color to return.
    idx = int(((val - min_val) / top) * n)
    bg_color = magma(n + 1)[idx]
    text_color = white
    # This is specific to magma. To make the text more readable, change its
    # color to black if we're more than 90% of the way through the magma
    # colors.
    if ALL_MAGMA.index(bg_color) >= 0.9 * len(ALL_MAGMA):
        text_color = black
    return bg_color, text_color
예제 #23
0
파일: main.py 프로젝트: shernee/SF_Crime
def make_data(dist, cat):

    df_distcat = df_data[(df_data['PdDistrict'].isin(dist))
                         & (df_data['Category'].isin(cat))]
    df_grouped = df_distcat.groupby('DayOfWeek')[['PdId']].count().sort_values(
        'PdId', ascending=False)

    df_hour_dist = df_distcat.groupby([df_distcat.index.hour, 'PdDistrict'
                                       ])['PdId'].count().unstack()

    bar_palette = magma(len(df_grouped))
    bar_source = ColumnDataSource(
        dict(x=df_grouped.index.tolist(),
             top=df_grouped['PdId'],
             color=bar_palette))

    numlines = df_hour_dist.shape[1]
    line_source = ColumnDataSource(
        dict(x=[df_hour_dist.index.values] * numlines,
             y=[df_hour_dist[name].values for name in df_hour_dist],
             color=magma(numlines),
             label=[name for name in df_hour_dist]))

    return bar_source, line_source
예제 #24
0
def plot_control_startup(df, malvern=True, strain_as_x=True):
    """
        A companion function to check if your shear startup / reversals make sense
        Plots the strain rate as a function of time. Since I don't have the target
        I can't do just an "error plot" 

        ARGS : 
        - df [PANDAS.DATAFRAME] : your sliced rheology data. Note : you need to run "

        OUTPUT :
        - a figure (d'uh !)
    """

    f1 = figure(title='Strain rate control for reversal ...',
                y_axis_type='log')
    steps = np.unique(df['step'])
    cmap = magma(np.size(steps))

    # Fix things that are messed up first ...
    if not malvern:
        df = fix_strain_antonpaar(df, steps)

    # Plot things, be careful with strain offsets
    for no, s in enumerate(steps):
        if strain_as_x:
            xdata = np.abs(df[df['step'] == s]['strain'])
            f1.xaxis.axis_label = '|gamma| (%)'
        else:
            xdata = df[df['step'] == s]['time']
            f1.xaxis.axis_label = 't (s)'

        rate = np.convolve(np.abs(df[df['step'] == s]['shearrate']),
                           np.ones(5) / 5,
                           mode='same')
        est_rate = np.abs(np.mean(rate[-30:]))

        f1.line(xdata, rate, line_color=cmap[no], line_width=1)
        f1.line([0, np.max(xdata)], [est_rate, est_rate],
                line_color=cmap[no],
                line_dash='dashed')

    show(f1)
    return f1
예제 #25
0
def getdormcaseschart(transdata):
    # # Area chart in plotly
    from bokeh.palettes import magma
    acol = magma(transdata.drop(['Date', 'tooltip'], axis=1).shape[1])[::-1]
    chart2 = go.Figure()
    # Area chart for each series
    for i in reversed(range(transdata.shape[1] - 2)):
        yname = transdata.drop(['Date', 'tooltip'], axis=1).columns[i]
        y = transdata.drop(['Date', 'tooltip'], axis=1)[yname]
        chart2.add_trace(
            go.Scatter(
                x=transdata['Date'],
                y=y,
                mode='lines',
                line=dict(width=0.5, color='lightgray'),
                fillcolor=acol[i],
                stackgroup='one',
                name=yname,
                hovertemplate='%{y}',
            ))
    chart2.update_layout(
        title_text="Number of cases over time",
        height=500,
        width=1150,
        template="plotly_white",
        hovermode='x unified',
        xaxis_showgrid=True,
        yaxis_showgrid=True,
        margin={'b': 0},
    )
    chart2.update_xaxes(tickangle=-45,
                        tickmode='linear',
                        ticks="outside",
                        showline=True,
                        rangemode="tozero")
    chart2.update_yaxes(ticks="outside", showline=True, rangemode="tozero")

    return chart2
예제 #26
0
    def make_dataset(Movies):

        Mov_split = Movies["Day"].str.split(' ', expand=True)
        Movies["Month"] = Mov_split[0]
        Movies["Date"] = Mov_split[1]
        c_map = bp.magma(len(np.unique(Movies["Score"])))
        c_dict = zip(np.unique(Movies["Score"]), c_map)
        Col_MAP = dict(c_dict)
        Movies["color"] = Movies["Score"].apply(lambda x: Col_MAP[x])

        Movies["DateTime"] = pd.to_datetime(Movies["Release"],
                                            format="%B %d, %Y")

        Movies['alpha'] = np.where(Movies["Score"].astype(float) >= 90, 0.9,
                                   0.25)

        mov_dict = dict(Movie=Movies["Movie"],
                        Score=Movies["Score"],
                        Release=Movies["Release"],
                        alpha=Movies["alpha"],
                        color=Movies["color"])

        return ColumnDataSource(mov_dict)
예제 #27
0
def box_plot(x,y,cl_ar,bok_file,epoch):
    class_names = ['squat', 'deadlift', 'pushups', 'pullups', 'wallpushups', 'lunges', 'other', 'cleanandjerk']
    num_of_classes = len(class_names)
    df = pd.DataFrame(data={ 'tar': x, 'pred': y, 'class': cl_ar})
    palette = magma(num_of_classes + 1)
    p = figure(plot_width=600, plot_height=800, title=f"Ranking by exercise, epoch {epoch}")
    p.xgrid.grid_line_color = None
    p.xaxis.axis_label = 'Target ranking'
    p.yaxis.axis_label = 'Predicted ranking'
    

    
    for cl in range(num_of_classes):
        if cl == 6:
            continue
        df2 = df.loc[df['class']==cl]
        p.circle(x=jitter('tar', 0.5), y='pred', size=8, alpha=0.1, color=palette[cl], legend=class_names[cl], source=df2 )
        p.line(x='tar', y='pred', line_width=2, alpha=0.5, color=palette[cl], source=df2.groupby(by="tar").mean())
    p.legend.location = "top_left"
    p.legend.click_policy="hide"
    output_file(bok_file, title="Ranking by exercise")
    save(p)
    pil_image2 = get_screenshot_as_png(p)
    neptune.send_image('rank_distances', pil_image2)
예제 #28
0
def corr_molecular(atm, filename_db, wavenumber, residual, max_plot,
                   threshold):

    #get available data and grab needed cross sections
    mols, pt_pairs = molecular_avail(filename_db)
    Ts = []
    Ps = []
    for i in np.unique(atm.layer['pt_opa_index']):
        Ps += [pt_pairs[i - 1][1]]
        Ts += [pt_pairs[i - 1][2]]

    #GET UNIQUE TEMPERATURES SO WE JUST HAVE ON PRESSURE FOR EACH TEMP
    Ts_u, Ps_u = [], []
    for i, j in zip(Ts, Ps):
        if i not in Ts_u:
            Ts_u += [i]
            Ps_u += [j]
    Ts = Ts_u
    Ps = Ps_u

    grab_m = get_molecular(filename_db, list(mols), Ts, Ps)

    #rebin contimuum and compute correlations

    cont = []
    mol_t_pairs = []
    counts = []
    for i in mols:
        for jt, jp in zip(Ts, Ps):
            cx = grab_m[i][jt][jp]
            x, cx_bi = mean_regrid(grab_m['wavenumber'], cx, newx=wavenumber)
            grab_m[i][jt][jp] = cx_bi
            mol_t_pairs += [(str(i), str(jt))]
            pr = pearsonr(cx_bi, residual)[0]
            if np.isnan(pr):
                counts += [0]
            else:
                counts += [pr]
    grab_m['wavenumber'] = x

    #reformat the data to be in dictionary form so that we can tack in bokeh chart

    #POSITIVE CORRELATIONS
    data_p = {i[1]: [] for i in mol_t_pairs}

    #NEGATIVE CORRELATIONS
    data_n = {i[1]: [] for i in mol_t_pairs}

    temp_strs = [str(i) for i in Ts]
    i = 0
    for ii in mols:
        for jj in temp_strs:
            if counts[i] >= 0:
                data_p[jj] += [counts[i]]
                data_n[jj] += [0]
            else:
                data_p[jj] += [0]
                data_n[jj] += [counts[i]]
            i += 1
    data_p['molecules'] = mols
    data_n['molecules'] = mols

    #get molecules pairs with highest correlations for the negative (df_n) and
    #the positive (df_c) correlations

    df_n = pd.DataFrame(data_n, index=range(len(mols)))
    cols = list(df_n.keys())
    cols.pop(cols.index('molecules'))
    df_n = df_n[(df_n[cols].T < -threshold).any()]

    if df_n.shape[0] > 0:
        mol_to_plot_n = df_n.sum(axis=1).sort_values(ascending=True)
        mol_to_plot_n = mol_to_plot_n.loc[
            mol_to_plot_n != 0].index.values[0:max_plot]

        t_to_plot_n = {}
        for i in mol_to_plot_n:
            top_ts_n = df_n.loc[i, cols].sort_values().index.values[0]
            t_to_plot_n[mols[i]] = top_ts_n
    else:
        t_to_plot_n = "none"

    df_p = pd.DataFrame(data_p, index=range(len(mols)))
    cols = list(df_p.keys())
    cols.pop(cols.index('molecules'))
    df_p = df_p[(df_p[cols].T > threshold).any()]

    if df_p.shape[0] > 0:
        mol_to_plot_p = df_p.sum(axis=1).sort_values(ascending=False)
        mol_to_plot_p = mol_to_plot_p.loc[
            mol_to_plot_p != 0].index.values[0:max_plot]

        t_to_plot_p = {}
        for i in mol_to_plot_p:
            top_ts_p = df_p.loc[i, cols].sort_values(
                ascending=False).index.values[0]
            t_to_plot_p[mols[i]] = top_ts_p
    else:
        t_to_plot_p = "none"

    #finally MAKE BIG ASS double PLOT

    #first stack of correlations
    color_mapper = LinearColorMapper(palette="Magma256",
                                     low=min(Ts),
                                     high=max(Ts))

    p = figure(y_range=mols,
               plot_height=300,
               plot_width=600,
               x_range=(-1 * len(temp_strs), len(temp_strs)),
               title="Correlations with Molecules")

    p.hbar_stack(temp_strs,
                 y='molecules',
                 height=0.9,
                 color=magma(len(temp_strs)),
                 source=ColumnDataSource(data_p))

    p.hbar_stack(temp_strs,
                 y='molecules',
                 height=0.9,
                 color=magma(len(temp_strs)),
                 source=ColumnDataSource(data_n))

    color_bar = ColorBar(color_mapper=color_mapper,
                         ticker=BasicTicker(),
                         label_standoff=12,
                         border_line_color=None,
                         location=(0, 0))

    p.add_layout(color_bar, 'right')

    #second take top correlated and plot it with the residuals
    spec = figure(plot_height=300,
                  plot_width=650,
                  x_axis_type='log',
                  title="Residuals w/ Molecular Opacity",
                  y_range=[min(residual), max(residual)])
    legend_it = []

    spec.extra_y_ranges = {"cxs": Range1d()}
    new_range = [0, -100]
    spec.add_layout(LinearAxis(y_range_name="cxs"), 'right')

    spec.line(1e4 / grab_m['wavenumber'],
              residual,
              color='black',
              line_width=3)

    icolor = 0

    if t_to_plot_n != "none":
        i = 0
        for im in t_to_plot_n.keys():
            ip = Ps[Ts.index(float(t_to_plot_n[im]))]
            cx = grab_m[im][float(t_to_plot_n[im])][ip]
            loc = np.where(cx != 0)
            logcx = np.log10(cx[loc])
            c = spec.line(1e4 / grab_m['wavenumber'][loc],
                          logcx,
                          color=Colorblind8[icolor],
                          y_range_name="cxs",
                          line_width=3)
            new_range[0] = np.min([new_range[0], min(logcx)])
            new_range[1] = np.max([new_range[1], max(logcx)])
            legend_it.append((im + "_" + t_to_plot_n[im], [c]))
            i += 1
            icolor += 1

    if t_to_plot_p != "none":
        i = -1
        for im in list(t_to_plot_p.keys()):
            ip = Ps[Ts.index(float(t_to_plot_p[im]))]
            cx = grab_m[im][float(t_to_plot_p[im])][ip]
            loc = np.where(cx != 0)
            logcx = np.log10(cx[loc])
            c = spec.line(1e4 / grab_m['wavenumber'][loc],
                          logcx,
                          color=Colorblind8[icolor],
                          y_range_name="cxs",
                          line_width=3)
            new_range[0] = np.min([new_range[0], min(logcx)])
            new_range[1] = np.max([new_range[1], max(logcx)])
            legend_it.append((im + "_" + t_to_plot_p[im], [c]))
            i -= 1
            icolor += 1

    if ((t_to_plot_n != "none") | (t_to_plot_p != "none")):
        legend = Legend(items=legend_it, location=(0, 0))
        legend.click_policy = "mute"
        spec.add_layout(legend, 'right')

    spec.extra_y_ranges = {
        "cxs": Range1d(start=new_range[0] * 1.1, end=new_range[1] * .9)
    }  #

    p.y_range.range_padding = 0.1
    p.ygrid.grid_line_color = None
    p.axis.minor_tick_line_color = None
    p.outline_line_color = None

    return column(p, spec)
예제 #29
0
def calendar():

    '''
    # Calendar
    '''

    '''
    $contents
    '''

    s = session('-v2')
    output_file(filename='/dev/null')

    '''
    ## Distance

    Larger distances have larger symbols.
    
    Place the cursor over the symbol for more information.
    '''

    df = Statistics(s). \
        by_name(ActivityTopic, N.NAME). \
        by_name(ActivityCalculator, N.ACTIVE_DISTANCE, N.ACTIVE_TIME, N.TOTAL_CLIMB).with_. \
        copy({N.ACTIVE_DISTANCE: N.ACTIVE_DISTANCE_KM}).add_times().df
    df['Duration'] = df[N.ACTIVE_TIME].map(format_seconds)
    if present(df, N.TOTAL_CLIMB):
        df.loc[df[N.TOTAL_CLIMB].isna(), [N.TOTAL_CLIMB]] = 0

    calendar = Calendar(df, title=N.DISTANCE, not_hover=[N.ACTIVE_DISTANCE, N.ACTIVE_TIME])
    calendar.std_distance()

    '''
    ## Work Done and Fatigue

    Larger increases in Fitness have larger symbols.  Higher fatigue (relative to fitness) is redder.
    
    Place the cursor over the symbol for more information.
    '''

    df = Statistics(s). \
        by_name(ActivityTopic, N.NAME). \
        by_name(ActivityCalculator, N.ACTIVE_DISTANCE, N.ACTIVE_TIME, N.TOTAL_CLIMB). \
        by_name(ActivityCalculator, N._delta(N.FITNESS_ANY), like=True).df
    df = Statistics(s). \
        by_name(ResponseCalculator, N.ANY_FATIGUE_ANY, N.ANY_FITNESS_ANY, like=True). \
        with_.drop_prefix(N.DEFAULT).into(df, tolerance='30m')
    df['Duration'] = df[N.ACTIVE_TIME].map(format_seconds)
    df.loc[df[N.TOTAL_CLIMB].isna(), N.TOTAL_CLIMB] = 0
    work_done = sorted_numeric_labels(df.columns, N._delta(N.FITNESS))[0]
    fitness = sorted_numeric_labels(df.columns, N.FITNESS)[0]
    fatigue = sorted_numeric_labels(df.columns, N.FATIGUE)[0]
    print(fatigue, fitness)
    df['FF Ratio'] = df[fatigue] / df[fitness]

    calendar = Calendar(df, title='Work Done and Fatigue',
                        not_hover=[N.ACTIVE_DISTANCE, N.ACTIVE_TIME] +
                                  [column for column in df.columns if ':' in column])
    calendar.background('square', fill_alpha=0, line_alpha=1, color='lightgrey')
    calendar.set_palette('FF Ratio', K2R, lo=0.5, hi=2)
    calendar.set_size(work_done, min=0.1, gamma=0.5)
    calendar.foreground('square', fill_alpha=1, line_alpha=0)
    calendar.show()

    '''
    ## Distance, Climb and Direction

    Larger distances have larger symbols.  Higher climbs are redder.  
    The arc indicates the general direction relative to the start.
    
    Place the cursor over the symbol for more information.
    '''

    df = Statistics(s). \
        by_name(ActivityTopic, N.NAME). \
        by_name(ActivityCalculator, N.ACTIVE_DISTANCE, N.ACTIVE_TIME, N.TOTAL_CLIMB, N.DIRECTION,
                N.ASPECT_RATIO).df
    df[N.DISTANCE_KM] = df[N.ACTIVE_DISTANCE]
    df['Duration'] = df[N.ACTIVE_TIME].map(format_seconds)
    if present(df, N.TOTAL_CLIMB):
        df.loc[df[N.TOTAL_CLIMB].isna(), N.TOTAL_CLIMB] = 0

    calendar = Calendar(df, title='Distance, Climb and Direction',
                        not_hover=[N.ACTIVE_DISTANCE, N.ACTIVE_TIME] +
                                  [column for column in df.columns if ':' in column])
    calendar.std_distance_climb_direction()

    '''
    ## Distance, Work Done and Direction

    Larger distances have larger symbols.  Larger gains in fitness are redder.  
    The arc indicates the general direction relative to the start.
    
    Place the cursor over the symbol for more information.
    '''

    df = Statistics(s). \
        by_name(ActivityTopic, N.NAME). \
        by_name(ActivityCalculator, N.ACTIVE_DISTANCE, N.ACTIVE_TIME, N.TOTAL_CLIMB, N.DIRECTION,
                N.ASPECT_RATIO). \
        by_name(ActivityCalculator, N._delta(N.FITNESS_ANY), like=True).df
    df[N.DISTANCE_KM] = df[N.ACTIVE_DISTANCE]
    df['Duration'] = df[N.ACTIVE_TIME].map(format_seconds)
    df.loc[df[N.TOTAL_CLIMB].isna(), N.TOTAL_CLIMB] = 0

    calendar = Calendar(df, title='Distance, Fitness and Direction',
                        not_hover=[N.ACTIVE_DISTANCE, N.ACTIVE_TIME] +
                                  [column for column in df.columns if ':' in column])
    calendar.std_distance_fitness_direction()

    '''
    ## Fitness and Fatigue

    Better fitness has larger symbols.  When fatigue is higher symbols have "hotter" colours.
    
    Place the cursor over the symbol for more information.
    '''

    df = Statistics(s). \
        by_name(ActivityTopic, N.NAME). \
        by_name(ActivityCalculator, N.ACTIVE_DISTANCE, N.ACTIVE_TIME). \
        by_name(ActivityCalculator, N._delta(N.FITNESS_ANY), like=True).df
    df = Statistics(s). \
        by_name(ResponseCalculator, N.ANY_FATIGUE_ANY, N.ANY_FITNESS_ANY, like=True). \
        with_.drop_prefix(N.DEFAULT).into(df, tolerance='30m')
    fitness = sorted_numeric_labels(df.columns, N.FITNESS)[0]
    fatigue = sorted_numeric_labels(df.columns, N.FATIGUE)[0]
    df['FF Ratio'] = df[fatigue] / df[fitness]

    calendar = Calendar(df, title='Fitness and Fatigue', scale=18, border_month=0, border_day=0)
    calendar.set_size(fitness, min=0.1, gamma=0.5)
    calendar.set_palette('FF Ratio', magma(256), lo=0.5, hi=2, min=0)
    calendar.foreground('square', fill_alpha=1, line_alpha=0)
    calendar.show()

    '''
    ## Groups, Distance, Climb and Direction

    Larger distances have larger symbols.  Higher climbs are lighter.  
    The arc indicates the general direction relative to the start.
    Pastel backgrounds group similar rides.
    
    Place the cursor over the symbol for more information.
    '''

    dfa = Statistics(s). \
        by_name(ActivityTopic, N.NAME). \
        by_name(ActivityCalculator, N.ACTIVE_DISTANCE, N.ACTIVE_TIME, N.TOTAL_CLIMB, N.DIRECTION,
                N.ASPECT_RATIO). \
        by_name(ActivityCalculator, N._delta(N.FITNESS_ANY), like=True).df
    dfa[N.DISTANCE_KM] = df[N.ACTIVE_DISTANCE]
    dfa['Duration'] = df[N.ACTIVE_TIME].map(format_seconds)
    dfa.loc[dfa[N.TOTAL_CLIMB].isna(), N.TOTAL_CLIMB] = 0
    dfb = groups_by_time(s)
    if present(dfb, N.GROUP):
        dfb.loc[dfb[N.GROUP].isna(), N.GROUP] = -1
        df = dfa.join(dfb)

        calendar = Calendar(df, scale=15, border_day=0.1,
                            not_hover=[N.ACTIVE_DISTANCE, N.ACTIVE_TIME] +
                                      [column for column in df.columns if ':' in column])
        calendar.std_group_distance_climb_direction()
예제 #30
0
def make_plot(df, corp, color_palette):

    palette_list = [
        palettes.viridis(100),
        palettes.inferno(100),
        palettes.magma(100),
        palettes.plasma(100),
    ]
    colors = list(reversed(palette_list[color_palette]))
    mapper = LinearColorMapper(palette=colors, low=0, high=100)

    TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom"
    TOOLTIPS = """
                <div>
                    <div>
                    <span style="font-size: 20px; font-weight: bold;">score: @c%</span>
                    </div>
                    <div>
                        <span style="font-size: 16px; font-style: italic;">@a & @b</span>
                    </div>
                </div>
    """
    # tooltips=[('score','@c%'), ('doc_1', '@a'), ('doc_2', '@b')])

    hm = figure(
        x_range=corp,
        y_range=list(reversed(corp)),
        x_axis_location="above",
        plot_width=900,
        plot_height=900,
        tools=TOOLS,
        toolbar_location="below",
        tooltips=TOOLTIPS,
    )

    hm.grid.grid_line_color = None
    hm.axis.axis_line_color = None
    hm.axis.major_tick_line_color = None
    hm.axis.major_label_text_font_size = "8pt"
    hm.axis.major_label_standoff = 0
    hm.xaxis.major_label_orientation = pi / 3

    hm.rect(
        x="a",
        y="b",
        source=df,
        width=1,
        height=1,
        line_color="#ffffff",
        fill_color={
            "field": "c",
            "transform": mapper
        },
    )

    color_bar = ColorBar(
        color_mapper=mapper,
        formatter=PrintfTickFormatter(format="%d%%"),
        major_label_text_font_size="10pt",
        label_standoff=10,
        border_line_color=None,
        location=(0, 0),
    )

    hm.add_layout(color_bar, "right")

    js_resources = INLINE.render_js()
    css_resources = INLINE.render_css()
    script, div = components(hm)

    return js_resources, css_resources, script, div
예제 #31
0
def render_mask(img_path, document_objs, document_name='Default_Document', output_path='./outputs/', save_html=False):
    img = load_img(img_path)
    bokeh.plotting.reset_output()
    hover = HoverTool(
        tooltips=[
            ("Name", "@name"),
            ("(x,y)", "($x, $y)"),
        ]
    )
    fig = figure(tools=[hover,
                        bokeh.models.WheelZoomTool(),
                        bokeh.models.PanTool()])
    fig.hover.point_policy = "follow_mouse"
    fig.sizing_mode='scale_width'
    fig.image_url(url=[os.path.basename(img_path)], x=0, y=img.shape[0], w=img.shape[1], h=img.shape[0])
    
    script = "active = cb_obj.active;"
    labels = list(document_objs.keys())
    color_map = bp.magma(len(labels))
    
    args = dict()
    total_objs = 0
    for key_id, key in enumerate(labels):
        xs = []
        ys = []
        for box in document_objs[key]:
            _ = np.array(box).reshape(-1,2).T
            xs.append(_[0])
            ys.append(_[1])
        ys=doccoor2planecoor(ys, img.shape[0])
        data_source = dict(x=xs, y=ys, 
                          name=["%s %d"%(key, idx) for idx in range(len(xs))])
        
        falpha = 0.5*int('table' not in key.lower())
        lcolor = 'blue' if 'table' in key.lower() else 'red'
        lwidth = 3 if 'table' in key.lower() else 1
        total_objs += len(document_objs[key])
        args[key] = fig.patches('x', 'y', source=data_source, fill_color=color_map[key_id], 
                                fill_alpha=falpha, line_color=lcolor, line_width=lwidth, 
                                legend=key+': %d'%len(document_objs[key]))
        r = args[key]
        script += "\n%s.visible = active.includes(%d);"%(key, key_id)
    fig.patch([],[], fill_color='red', fill_alpha=0, line_color='white', legend='Total region: %d'%total_objs)
    fig.legend.click_policy="hide"
    fig.legend.location = "top_left"
    checkbox = CheckboxGroup(labels=labels, active=[])#active=[*range(len(labels))])
    checkbox.callback = CustomJS(args=args, code=script)
    plt_obj = [fig]

    
    html = file_html(plt_obj, CDN, title=document_name)
    if save_html:
        base = os.path.join(output_path, document_name)
        if os.path.exists(base):
            shutil.rmtree(base)
        os.makedirs(base)
        shutil.copy(img_path, base)
        with open(os.path.join(base, 'main.html'),'w') as g:
            g.write(html)
            
    return html