예제 #1
0
def makeHoverCarpet(carpets, collabels, rowlabels, maxval, extralabels):
    """
    makes a 3 carpet heatmap
    """
    if (len(rowlabels) < 1):
        print('no wells to show')
        return
    hv.extension('bokeh', width=90)
    sprowlabels = [
        rowlabels[i] + ',' + extralabels[i] for i in range(len(rowlabels))
    ]
    heatmap1 = hv.HeatMap(
        (collabels, sprowlabels, carpets[0])).opts(title=targets[0] + ' dRn')
    heatmap2 = hv.HeatMap(
        (collabels, sprowlabels, carpets[1])).opts(title=targets[1] + ' dRn')
    heatmap3 = hv.HeatMap(
        (collabels, sprowlabels, carpets[2])).opts(title=targets[2] + ' dRn')
    tooltips = [('Well', '@y'), ('Cycle', '@x'), ('dRn', '@z')]
    hover = HoverTool(tooltips=tooltips)
    layout = heatmap1 + heatmap2 + heatmap3
    layout.opts(
        opts.HeatMap(tools=[hover],
                     cmap='jet',
                     colorbar=True,
                     invert_yaxis=True,
                     clim=(0.0, maxval),
                     width=400,
                     height=800,
                     xlabel='Cycle',
                     ylabel='Wells'))
    return layout
예제 #2
0
def selected_points(index):
    global data, ids
    if len(ids) == 1 and index == []:
        index = ids
    if index and len(index) <= 1000:
        for i in index:
            for j in range(len(features)):
                data.append(
                    (features[j], str(i), temp_feats[features[j]].iloc[i] *
                     models_feats_imp[choice2.value][j]))
        b = temp_feats.iloc[index][features].mean()
        c = temp_feats.iloc[0:len(loc_feats)][features].mean()
        for j in range(len(features)):
            data.append((features[j], 'Mean_value',
                         b[features[j]] * models_feats_imp[choice2.value][j]))
        if ids == []:
            ids = index
        elif ids != index:
            ids += index
    else:
        data = []
        ids = []
    return hv.HeatMap(data, kdims=['Points', 'Features'],
                      vdims=['Values']).options(colorbar=True,
                                                width=950,
                                                height=800,
                                                tools=TOOLS,
                                                xrotation=45).sort()
예제 #3
0
    def correlation_heatmap(num_df, figsize=(800, 600)):
        """Correlation Heat Map
        Parameters
        ----------
        num_df : Numerical df
        figsize : tuple
            Ex. (width,height)

        Returns
        -------
        Holoview Object
            Return hv object that will be printed out

        """
        num_corr = num_df.corr()
        num_corr = pd.DataFrame(np.triu(num_corr),
                                index=num_corr.index,
                                columns=num_corr.columns)
        num_corr = pd.melt(num_corr.reset_index(),
                           'index').rename(columns={
                               'index': 'x',
                               'variable': 'y'
                           })
        plot = hv.HeatMap(num_corr, ['x', 'y'], 'value')
        plot = plot.opts(
            plot={
                'width': figsize[0],
                'height': figsize[1],
                'xrotation': 90,
                'tools': ['hover'],
                'colorbar': True
            })
        return plot
예제 #4
0
def plot_commit_heatmap(commits,
                        width=900,
                        height=400,
                        resample=None,
                        xlabel=None,
                        title="Seriated heatmap",
                        cmap="viridis"):
    xlabel = xlabel if xlabel is not None else commits.index.name
    xlabel = "index" if xlabel is None else xlabel
    commits = commits if resample is None else commits.resample(resample).sum()
    commits.index = [x.strftime("%D") for x in commits.index]
    mat = commits.values.T
    mat = mat / mat.max(axis=1)[:, None]
    heatmap = hv.HeatMap(
        {
            'x': commits.index,
            'y': format_columns(commits.columns),
            'z': mat.round(3) * 100
        },
        kdims=[('x', xlabel), ('y', 'Identity')],
        vdims=('z', "% With respect to max"))
    heatmap = heatmap.opts(cmap="viridis",
                           width=width,
                           height=width,
                           fontsize={'xticks': '2pt'},
                           tools=["hover"],
                           xrotation=90,
                           title=title)
    return heatmap
예제 #5
0
def generar_heatmap(cluster, indice):
    """Genera el heatmap de accidentes de cada cluster"""
    df_heat = (df_points_in_clusters.loc[df_points_in_clusters.cluster ==
                                         cluster].groupby(
                                             ['dia_semana', 'hora'],
                                             as_index=False).cluster.count())
    df_heat = df_heat.pivot(index='dia_semana',
                            columns='hora',
                            values='cluster').fillna(0)
    df_heat = df_heat.reindex(DIAS).reset_index()
    df_heat = pd.melt(df_heat, id_vars='dia_semana')
    df_heat = df_heat.assign(value=df_heat['value'].astype(int))
    heatmap_opts = dict(width=600,
                        height=300,
                        cmap='viridis',
                        colorbar=True,
                        show_title=True,
                        tools=['hover'])
    heatmap = hv.HeatMap(
        df_heat,
        kdims=['hora', 'dia_semana'],
        vdims=['value'],
        label=f'Incidentes por día y hora en el cluster: {indice}').options(
            **heatmap_opts)
    return heatmap
def cluster_viz(geocode, clusters):
    data, group = get_cluster_data(geocode=geocode, clusters=clusters,
                                   data_types=DATA_TYPES, cols=['casos'])

    city_names = dict(get_city_names(group))
    df_hm = data.reset_index().rename(columns={'index': 'week'})
    df_hm = pd.melt(df_hm, id_vars=['week'], var_name='city', value_name='incidence')
    df_hm['city'] = [int(re.sub('casos_', '', i)) for i in df_hm.city]
    df_hm['city'] = [city_names[i] for i in df_hm.city]

#     return df_hm
    curve_opts = dict(line_width=10, line_alpha=0.4,tools=[])
    overlay_opts = dict(width=900, height=200,tools=[])
    hm_opts = dict(width=900, height=500, tools=[], logz=True, invert_yaxis=False, xrotation=90,
                   labelled=[], toolbar=None, xaxis=None)

    heatmap = hv.HeatMap(df_hm)
    heatmap.toolbar_location = None
    graphs = [hv.Curve((data.index, data[i]), 'Time', 'Incidence') for i in data.columns]
    final = graphs[0]
    for i in graphs[1:]:
        final = final * i

    opts = {'HeatMap': {'plot': hm_opts}, 'Overlay': {'plot': overlay_opts},
            'Curve': {'plot': curve_opts,
                      'style': dict(color='blue', line_alpha=0.2)}}
    return (heatmap + final).opts(opts).cols(1)
예제 #7
0
def _heatmap_holoviews(heatmap_data, x, y, z):
    if hv is None:
        raise ImportError(
            "holoviews module could not be imported. use a different engine")

    heatmap = hv.HeatMap(heatmap_data.reset_index(), [f'{x}_bin', f'{y}_bin'],
                         z)
    return heatmap
예제 #8
0
def dictionary_pair_overlap(alpha, beta):
    overlap_dim = hv.Dimension("Overlap Count", value_format=lambda x: f"{x}")

    data = [(f"{ak}: {len(av)}", f"{bk}: {len(bv)}", len(set.intersection(set(av), set(bv))))
            for (ak, av), (bk, bv) in itertools.product(alpha.items(), beta.items())]

    heatmap = hv.HeatMap(data, vdims=overlap_dim).opts(labelled=[], colorbar=True)
    return heatmap * hv.Labels(heatmap)
예제 #9
0
def grid_plot_experiments(comet_api: comet_ml.API, experiments: APIExperiments, p1_name: str, p2_name: str,
                          metrics: List[str] = ['train_acc', 'dev_acc', 'test_acc'], parameters: List[str] = [],
                          fig_size: int = 220) -> hv.core.layout.Layout:
    targets_data = _download_data(comet_api, experiments, p1_name, p2_name, metrics, parameters)

    layout = hv.Layout()
    for k, v in targets_data.items():
        layout += hv.HeatMap(v, kdims=[p1_name, p2_name], vdims=k).sort().opts(title=k, cmap='greens').redim.range(z=(0, None))
    return layout.opts(fig_size=fig_size, framewise=True)
예제 #10
0
 def display(self,
             data: Union[TrackQC, pd.DataFrame],
             tracks: List[str] = None,
             beads: List[int] = None) -> pd.DataFrame:
     "Outputs heatmap with the status of the beads per track"
     return hv.HeatMap(self.dataframe(data, tracks, beads),
                       kdims=['bead', 'track'],
                       vdims=['errorid', 'mostcommonerror',
                              'modification']).options(**self.styleopts)
예제 #11
0
    def gpu_map(data):
        data_algo1 = [d.split(",") for d in data['algo-1']]
        data_algo2 = [d.split(",") for d in data['algo-2']]

        data_algo1 = clean(data_algo1)
        data_algo2 = clean(data_algo2)

        algo1_map = hv.HeatMap(data_algo1).options(cmap='coolwarm',
                                                   title="GPU Heatmap (algo1)",
                                                   width=400,
                                                   colorbar=True).sort()
        algo2_map = hv.HeatMap(data_algo2).options(cmap='coolwarm',
                                                   title="GPU Heatmap (algo2)",
                                                   width=400,
                                                   colorbar=True).sort()

        full_plot = algo1_map + algo2_map.opts(shared_axes=False)
        return full_plot
예제 #12
0
def model_weights(model):
    """
    Takes a model and returns the weights as a grid
    of heatmaps using Holoviews.
    """
    # get the weights out of the model
    weights = model.get_weights_topo()

    # make heatmaps using holoviews
    heatmaps = None
    for w in weights:
        w = {(i, j): w[i, j][0]
             for i in range(w.shape[0]) for j in range(w.shape[1])}
        if heatmaps == None:
            heatmaps = hl.HeatMap(w)
        else:
            heatmaps += hl.HeatMap(w)
    return heatmaps
예제 #13
0
    def plot_cov(self, **kwargs):
        data = [(f"{li}: {self.data.x[i]}", f"{lj}: {self.data.x[j]}",
                 self.cov[i, j])
                for i, li in zip(range(len(self.data)), self.labels)
                for j, lj in zip(range(len(self.data)), self.labels)]

        return hv.HeatMap(data).opts(tools=["hover"],
                                     width=800,
                                     height=800,
                                     invert_yaxis=True,
                                     xrotation=90)
예제 #14
0
def empty_heatmap():
    """Crea un heatmap con datos nulos"""
    heatmap_opts = dict(width=600,
                        height=300,
                        cmap='viridis',
                        colorbar=True,
                        show_title=False,
                        tools=['hover'])
    heatmap = hv.HeatMap(df_empty,
                         kdims=['hora', 'dia_semana'],
                         vdims=['value']).options(**heatmap_opts)
    return heatmap
예제 #15
0
 def create_figure(self):
     data = self.solve()
     heatmap = hv.HeatMap(data,
                          label='Optimal Ads Served',
                          kdims=['hour', 'Advertisement id'],
                          vdims=['count'])
     heatmap.opts(
         tools=['hover'],
         colorbar=True,
         toolbar=None,
         invert_yaxis=True,
         width=self.width - self.panel_width,
     )
     return renderer.get_plot(heatmap).state
예제 #16
0
def plot_adjacency_matrix(G,
                          save=False,
                          filename=None,
                          weight="weighted_score",
                          log=False,
                          width=200,
                          height=200,
                          fontsize=10,
                          fontscale=1.5):
    """Flattens and plots an adjecency matric for a given graph.
    If a split graph is provided, plots adjacency matrix for each interaction.

    Weight can be "weighted_score" or "score".
    Returns: Holoviews heatmap
    """
    import numpy as np
    import holoviews as hv
    from holoviews import opts
    import networkx as nx
    hv.extension('bokeh')

    # Create adjecency matrix and sort index and columns by name
    matrix = nx.to_pandas_adjacency(G, weight=weight)

    if log:
        matrix = np.log10(matrix)

    matrix = matrix.sort_index(ascending=True)
    matrix = matrix[matrix.index.values]
    matrix = matrix.stack()
    matrix = matrix.reset_index(name="values")
    matrix.columns = ["source", "target", "values"]

    # Plot the matrix as a heatmap
    heatmap = hv.HeatMap(matrix, kdims=["target", "source"])

    # Rotate X lables and show the color bar
    heatmap.opts(
        opts.HeatMap(tools=["hover"],
                     xrotation=90,
                     colorbar=True,
                     width=width,
                     height=height,
                     fontsize=fontsize,
                     fontscale=fontscale))

    if save is True:
        hv.save(heatmap, filename)
    return heatmap
 def get_heatmap(data,
                 lim,
                 title,
                 kdim='x',
                 vdim='Iteration',
                 height=200):
     return hv.HeatMap(data, kdims=[kdim, vdim]).opts(
         opts.HeatMap(tools=['hover'],
                      colorbar=True,
                      width=350,
                      height=height,
                      invert_yaxis=True,
                      ylabel='Iteration',
                      title=title,
                      clim=(-lim, lim),
                      cmap='RdBu'))
예제 #18
0
def heat_hv(df, id_prop="Compound_Id", cmap="bwr", invert_y=False):
    if not HOLOVIEWS:
        raise ImportError("# holoviews library could not be imported")
    df_parm = df[[id_prop] + ACT_PROF_PARAMETERS].copy()
    df_len = len(df_parm)
    col_bar = False if df_len < 3 else True
    values = list(df_parm.drop(id_prop, axis=1).values.flatten())
    max_val = max(values)
    min_val = min(values)
    max_val = max(abs(min_val), max_val)
    hm_opts = dict(width=950, height=40 + 30 * df_len, tools=['hover'], invert_yaxis=invert_y,
                   xrotation=90, labelled=[], toolbar='above', colorbar=col_bar, xaxis=None,
                   colorbar_opts={"width": 10})
    hm_style = {"cmap": cmap}
    opts = {'HeatMap': {'plot': hm_opts, "style": hm_style}}
    df_heat = cpt.melt(df_parm, id_prop=id_prop)
    heatmap = hv.HeatMap(df_heat).redim.range(Value=(-max_val, max_val))
    return heatmap(opts)
def genesetcollection_overlap_heatmap(alpha: dict, beta: dict):
    """
    View the overlap (as a heatmap) of two GeneSetCollection dictionaries,
    as provided by gsc.as_dict().

    :param alpha:
    :param beta:
    :return:
    """
    overlap_dim = hv.Dimension("Overlap Count", value_format=lambda x: f"{x}")

    data = [(f"{ak}: {len(av)}", f"{bk}: {len(bv)}",
             len(set.intersection(set(av), set(bv))))
            for (ak,
                 av), (bk,
                       bv) in itertools.product(alpha.items(), beta.items())]

    heatmap = hv.HeatMap(data, vdims=overlap_dim)
    return heatmap * hv.Labels(heatmap)
예제 #20
0
def showidentifiedpeaks(
        data,  # pylint: disable=too-many-arguments
        missingvalue=20,
        dynmap=False,
        adjoint=True,
        width=600,
        height=100):
    """
    Show expected peaks in a heatmap
    """
    # pylint: disable=too-many-locals
    if dynmap:
        fcn = lambda x: showidentifiedpeaks(data.loc[[x]], missingvalue, None,
                                            False)
        return (hv.DynamicMap(fcn, kdims=[
            'track'
        ]).redim.values(track=sorted(data.index.levels[0].unique())))
    vals = (data.reset_index(level=[0, 2]).loc[list(
        data.groupby(level=1).hfsigma.mean().sort_values().index)].sort_values(
            ['reference']).assign(dist=lambda x: np.abs(x.delta)))

    if len(vals.track.unique()) > 1:
        ref = 'ref'
        vals['ref'] = vals.reference.astype(str) + " " + vals.track
    else:
        ref = 'reference'

    inds = vals.delta.isna().sum(level=0).sort_values().index
    vals = pd.concat([vals.loc[[i]] for i in inds])

    opts = dict(xrotation=90, width=width)
    hmap = hv.HeatMap(vals.fillna(missingvalue), [ref, "bead"], "dist")
    if not adjoint:
        opts.pop('width')
        return hmap.options(**opts)

    beads = (hv.Bars(vals.delta.isna().sum(level=0)[::-1]).redim(
        x="bead", y="missbeads"))
    pos = (hv.Bars(vals.reset_index().set_index(ref).delta.isna().sum(
        level=0)).redim(x=ref, y="misspos"))

    return (hmap.options(**opts) << beads.options(width=height, yaxis=None) <<
            pos.options(height=height, width=width, xaxis=None))
예제 #21
0
def get_ccaa_heatmap(complete_df):
    df = complete_df[['Comunidad Autónoma', 'Fecha', 'Casos']]
    df = df.sort_values(by=['Comunidad Autónoma', 'Fecha'],
                        ascending=[False, True])
    df.Fecha = df.loc[:, 'Fecha'].dt.strftime('%d-%m')

    heatmap = hv.HeatMap(df,
                         kdims=['Fecha', 'Comunidad Autónoma'],
                         vdims='Casos')
    heatmap.opts(width=width,
                 height=450,
                 tools=['hover'],
                 logz=True,
                 title='Casos acumulados',
                 toolbar='above',
                 xlabel='Fecha',
                 ylabel='',
                 colorbar=True,
                 clim=(1, np.nan),
                 xrotation=90)
    return heatmap
예제 #22
0
def makeHoverHeatMap(df, labelcol, hovercols, vmin=25, vmax=45):
    """
    this makes a holoviews heatmap from the dataframe
    x and y and z (column and row and Ct) columns should be the first two columns
    labelcol values are shown on the heatmap
    hovercols values will be shown on hover
    """
    hv.extension('bokeh', width=90)
    heatmap = hv.HeatMap(df)
    #tooltips=[('Row','@Row'),('Col','@Column'),('JCT','@JCT'),('CT','@CT'),('JCategory','@JCategory'),('Category','@Category'),('Sample','@Sample')]
    tooltips = [(hovercols[i], '@' + hovercols[i])
                for i in range(len(hovercols))]
    hover = HoverTool(tooltips=tooltips)
    heatmap.opts(tools=[hover],
                 colorbar=True,
                 invert_yaxis=True,
                 width=1500,
                 height=800,
                 clim=(vmin, vmax),
                 xlim=(0.5, 24.5))
    return heatmap * hv.Labels(heatmap, vdims=[labelcol])
예제 #23
0
    def _plot_cpu_heatmap(self, event, bins, xbins, cmap):
        """
        Plot some data in a heatmap-style 2d histogram
        """
        df = self.trace.df_event(event)
        df = df_window(df, window=self.trace.window, method='exclusive', clip_window=False)
        x = df.index
        y = df['target_cpu']

        if xbins:
            warnings.warn('"xbins" parameter is deprecated and will be removed, use "bins" instead', DeprecationWarning)
            bins = xbins

        nr_cpus = self.trace.cpus_count
        hist = np.histogram2d(y, x, bins=[nr_cpus, bins])
        z, _, x = hist
        y = list(range(nr_cpus))
        return hv.HeatMap(
            (x, y, z),
            kdims=[
                # Manually set dimension name/label so that shared_axes works
                # properly.
                # Also makes hover tooltip better.
                hv.Dimension('Time'),
                hv.Dimension('CPU'),
            ],
            vdims=[
                hv.Dimension(event),
            ]
        ).options(
            colorbar=True,
            xlabel='Time (s)',
            ylabel='CPU',
            # Viridis works both on bokeh and matplotlib
            cmap=cmap or 'Viridis',
            yticks=[
                (cpu, f'CPU{cpu}')
                for cpu in y
            ]
        )
예제 #24
0
def generate_single_plot(comet_api,
                         SOPT,
                         STR="r",
                         COPT="sgd",
                         E=1,
                         project="emnist-s"):
    clr = "Client Learning Rate (log10)"
    slr = "Server Learning Rate (log10)"
    exps = get_experiments(comet_api, SOPT, STR, COPT, E, project)
    slrv = exp_params2list(exps, "SERVER_LEARNING_RATE", float)
    clrv = exp_params2list(exps, "CLIENT_LEARNING_RATE", float)
    acc = exp_metrics2list(exps, "last_avg_acc", float)
    df = pd.DataFrame({
        slr: np.log10(slrv),
        clr: np.log10(clrv),
        "Accuracy": acc
    })
    i = df["Accuracy"].idxmax()
    m = df.iloc[i]
    return (
        hv.HeatMap(df, kdims=[clr, slr]).opts(colorbar=True, cmap="viridis") *
        hv.Ellipse(m[clr], m[slr], 0.5)).opts(fig_inches=10)
예제 #25
0
    def display(self,
                trackqc: TrackQC,
                tracks: List[str] = None,
                normalize=True) -> hv.Layout:
        """
        Outputs a heatmap. Columns are types of Error and rows are tracks. Each
        cell presents the percentage of appearance of the specific error at the
        specific track.
        """
        disc = self.dataframe(trackqc, tracks, normalize)
        value = self.value(normalize)
        nbeads = len(trackqc.status.index)
        hmap = (hv.HeatMap(disc[~disc['error'].isna()],
                           kdims=['error', 'track'],
                           vdims=[value, 'beads']).redim.range(**{
                               value: (-100, 100) if normalize else (-nbeads,
                                                                     nbeads)
                           }).redim.label(error=" ").options(**self.styleopts))

        fmt = (lambda x: f'{abs(x):.01f}') if normalize else (
            lambda x: f'{abs(x):.1f}')
        return ((hmap *
                 hv.Labels(hmap).redim.value_format(**{value: fmt})).clone(
                     label=self.title))
예제 #26
0
def geneset_overlap_heatmap(lineament_collection, keys=None, mode="overlap"):
    lineament_dict = lineament_collection.as_dict(keys)

    if mode == "overlap":
        overlap_dim = hv.Dimension("Overlap Count", value_format=lambda x: f"{x}")
        data = [(ak, bk, len(set.intersection(set(av), set(bv))))
                for (ak, av), (bk, bv) in itertools.permutations(lineament_dict.items(), 2)
                if ak != bk]

    elif mode == "percent":
        overlap_dim = hv.Dimension("Overlap Percent", value_format=lambda x: f"{x:.0%}")

        zero_filtered_dict = {k: v for k, v in lineament_dict.items()
                              if len(v) > 0}
        data = [(ak, bk, len(set.intersection(set(av), set(bv))) / len(set(av)))
                for (ak, av), (bk, bv) in itertools.permutations(zero_filtered_dict.items(), 2)
                if ak != bk]

    else:
        raise ValueError(f"{mode} is not a valid mode. Select from 'overlap' or 'percent'.)")

    heatmap = hv.HeatMap(data, vdims=overlap_dim)  # .options(xrotation=45, width=450, height=450, labelled=[],
    #         colorbar=True)
    return heatmap * hv.Labels(heatmap)
예제 #27
0
파일: bokeh.py 프로젝트: synw/dataswim
 def _get_bokeh_chart(self,
                      x_field,
                      y_field,
                      chart_type,
                      label,
                      opts,
                      style,
                      options={},
                      **kwargs):
     """
     Get a Bokeh chart object
     """
     if isinstance(x_field, list):
         kdims = x_field
     else:
         kdims = [x_field]
     if isinstance(y_field, list):
         vdims = y_field
     else:
         vdims = [y_field]
     args = kwargs
     args["data"] = self.df
     args["kdims"] = kdims
     args["vdims"] = vdims
     if label is not None:
         args["label"] = label
     else:
         if self.label is not None:
             args["label"] = self.label
     chart = None
     try:
         if chart_type == "line":
             chart = hv.Curve(**args)
         if chart_type == "hline":
             chart = self._hline_bokeh_(y_field)
         elif chart_type == "point":
             chart = hv.Scatter(**args)
         elif chart_type == "area":
             chart = hv.Area(**args)
         elif chart_type == "bar":
             chart = hv.Bars(**args)
         elif chart_type == "hist":
             chart = hv.Histogram(**args)
         elif chart_type == "errorBar":
             chart = hv.ErrorBars(**args)
         elif chart_type == "heatmap":
             chart = hv.HeatMap(**args)
         elif chart_type == "lreg":
             chart = self._lreg_bokeh(**args)
         elif chart_type == "sline":
             window_size, y_label = options["window_size"],
             options["y_label"]
             chart = self._sline_bokeh(window_size, y_label)
         if chart is None:
             self.err("Chart type " + chart_type + " unknown",
                      self._get_bokeh_chart)
             return
         endchart = chart(plot=opts, style=style)
         return endchart
     except DataError as e:
         msg = "Column not found in " + x_field + " and " + y_field
         self.err(e, self._get_bokeh_chart, msg)
     except Exception as e:
         self.err(e)
예제 #28
0
def plot_confussion_matrix(
    y_test,
    y_pred,
    target_names: list = None,
    cmap: str = "YlGnBu",
    width=500,
    height: int = 400,
    title: str = "Confusion matrix",
    normalize: bool = False,
):
    value_label = "examples"
    target_label = "true_label"
    pred_label = "predicted_label"
    label_color = "color"

    def melt_distances_to_heatmap(distances: pd.DataFrame) -> pd.DataFrame:
        dist_melt = pd.melt(distances.reset_index(),
                            value_name=value_label,
                            id_vars="index")
        dist_melt = dist_melt.rename(columns={
            "index": target_label,
            "variable": pred_label
        })
        dist_melt[target_label] = pd.Categorical(dist_melt[target_label])
        dist_melt[pred_label] = pd.Categorical(dist_melt[pred_label])
        coords = dist_melt.copy()
        coords[target_label] = dist_melt[target_label].values.codes
        coords[pred_label] = dist_melt[pred_label].values.codes
        return coords[[pred_label, target_label, value_label]]

    conf_matrix = confusion_matrix(y_test, y_pred)
    if normalize:
        conf_matrix = np.round(
            conf_matrix.astype("float") /
            conf_matrix.sum(axis=1)[:, np.newaxis], 3)
    # Adjust label color to make them readable when displayed on top of any colormap
    df = melt_distances_to_heatmap(pd.DataFrame(conf_matrix))
    mean = df[value_label].mean()
    df[label_color] = -df[value_label].apply(lambda x: int(x > mean))
    if target_names is not None:
        df[target_label] = df[target_label].apply(lambda x: target_names[x])
        df[pred_label] = df[pred_label].apply(lambda x: target_names[x])
    true_label_name = "Actual label"
    pred_label_name = "Predicted label"

    tooltip = [
        (true_label_name, "@{%s}" % target_label),
        (pred_label_name, "@{%s}" % pred_label),
        ("Examples", "@{%s}" % value_label),
    ]
    hover = HoverTool(tooltips=tooltip)
    heatmap = hv.HeatMap(df, kdims=[pred_label, target_label])
    heatmap.opts(title=title,
                 colorbar=True,
                 cmap=cmap,
                 width=width,
                 height=height,
                 tools=[hover])
    labeled = heatmap * hv.Labels(heatmap).opts(text_color=label_color,
                                                cmap=cmap)
    return labeled.options(xlabel=pred_label_name,
                           ylabel=true_label_name,
                           invert_yaxis=True)
예제 #29
0
def plot_infection_rates_by_contact_models(
    df_or_time_series: Union[pd.DataFrame, dd.core.DataFrame],
    show_reported_cases: bool = False,
    unit: str = "share",
    fig_kwargs: Optional[Dict[str, Any]] = None,
) -> hv.HeatMap:
    """Plot infection rates by contact models.

    Parameters
    ----------
    df_or_time_series : Union[pandas.DataFrame, dask.dataframe.core.DataFrame]
        The input can be one of the following two.

        1. It is a :class:`dask.dataframe.core.DataFrame` which holds the time series
           from a simulation.
        2. It can be a :class:`pandas.DataFrame` which is created with
           :func:`prepare_data_for_infection_rates_by_contact_models`. It allows to
           compute the data for various simulations with different seeds and use the
           average over all seeds.

    show_reported_cases : bool, optional
        A boolean to select between reported or real cases of infections. Reported cases
        are identified via testing mechanisms.
    unit : str
        The arguments specifies the unit shown in the figure.

        - ``"share"`` means that daily units represent the share of infection caused
          by a contact model among all infections on the same day.

        - ``"population_share"`` means that daily units represent the share of
          infection caused by a contact model among all people on the same day.

        - ``"incidence"`` means that the daily units represent incidence levels per
          100,000 individuals.
    fig_kwargs : Optional[Dict[str, Any]], optional
        Additional keyword arguments which are passed to ``heatmap.opts`` to style the
        plot. The keyword arguments overwrite or extend the default arguments.

    Returns
    -------
    heatmap : hv.HeatMap
        The heatmap object.

    """
    fig_kwargs = (DEFAULT_IR_PER_CM_KWARGS if fig_kwargs is None else {
        **DEFAULT_IR_PER_CM_KWARGS,
        **fig_kwargs
    })

    if _is_data_prepared_for_heatmap(df_or_time_series):
        df = df_or_time_series
    else:
        df = prepare_data_for_infection_rates_by_contact_models(
            df_or_time_series, show_reported_cases, unit)

    hv.extension("bokeh", logo=False)

    heatmap = hv.HeatMap(df)
    plot = heatmap.opts(**fig_kwargs)

    return plot
예제 #30
0
def plot_connectivity_matrix(connectivity_matrix,
                             atlas_labels,
                             threshold=None,
                             dst_dir=None,
                             filename=None):
    '''Plot a connectivity matrix. This function is very similar to nilearn.plotting.plot_matrix
    but uses the bokeh backend and is therefore interactive
    
    Parameters
    ----------
    connectivity_matrix : np.array
        A symmetric connectivity matrix.
    atlas_labels : pd.Series or list
        A list-like object providing names of each atlas region.
    threshold : float or int, optional
        Apply a threshold to the connectivity matrix before plotting. Values 
        lower than this threshold will be set to 0.
    dst_dir : str, optional
        Name of the output directory. The default is None.
    filename : str, optional
        Name of the file (must be provided including the extenstion). 
        The default is None.


    Returns
    -------
    connectogram_plot : holoviews.element.raster.HeatMap
        The the connectivity matrix plot object.


    '''

    # copy matrix
    connectivity_matrix = connectivity_matrix.copy()

    # convert to pd.DataFrame for further processing
    connectivity_matrix_df = pd.DataFrame(data=connectivity_matrix,
                                          columns=atlas_labels,
                                          index=atlas_labels)

    # Ensure that index name has the default name 'Index'
    if connectivity_matrix_df.index.name:
        connectivity_matrix_df.index.name = None

    # stack connectivity_matrix
    connectivity_matrix_stacked = connectivity_matrix_df.stack().reset_index()
    connectivity_matrix_stacked.columns = ['source', 'target', 'value']

    if threshold:
        connectivity_matrix_stacked['value'].where(
            connectivity_matrix_stacked['value'] >= threshold, 0, inplace=True)

    connectivity_matrix_stacked_ds = hv.Dataset(connectivity_matrix_stacked,
                                                ['source', 'target'])
    heatmap = hv.HeatMap(connectivity_matrix_stacked_ds)
    heatmap.opts(
        opts.HeatMap(tools=['hover'],
                     colorbar=True,
                     xaxis='bare',
                     yaxis='bare',
                     cmap='blues_r'))

    # save plot
    if dst_dir:
        if not filename:
            raise ValueError('Please provide a filename')

        dst_path = dst_dir + filename
        hv.save(heatmap, dst_path)

    # FIXME: this doesn't work for me in Spyder
    show(hv.render(heatmap))

    return heatmap