Example #1
0
def dig_plots(corr_range):
    # Create matrix and histogram plots
    # ---------------------------------
    dig_corr_img = hv.Image(
        np.rot90(dig_corr),
        bounds=(-0.5, -0.5, dig_num_samp - 1.5, dig_num_samp - 1.5)).opts(
            cmap='viridis',
            colorbar=True,
            height=300,
            width=400,
            title='Correlation Matrix').redim.range(z=corr_range)
    dig_dist_img = hv.Image(np.rot90(dig_dist),
                            bounds=(-0.5, -0.5, dig_num_samp - 1.5,
                                    dig_num_samp - 1.5)).opts(
                                        cmap='viridis',
                                        colorbar=True,
                                        height=300,
                                        width=400,
                                        title='Distance Matrix')
    dig_corr_his = hv.Histogram(
        (dig_corr_edges, dig_corr_freq)).opts(xlabel='Correlation',
                                              height=300,
                                              width=400,
                                              title='Correlation Histogram')
    dig_dist_his = hv.Histogram(
        (dig_dist_edges, dig_dist_freq)).opts(xlabel='Distance',
                                              height=300,
                                              width=400,
                                              title='Distance Histogram')

    dash = (dig_corr_img + dig_corr_his + dig_dist_img + dig_dist_his).opts(
        opts.Layout(shared_axes=False)).cols(2)  # Dashboard of all plots

    return dash
Example #2
0
def dist_compare_plot(seq1, seq2, bins=None, max_bar_categories: int = 40):
    plot_object = None
    if (is_numeric_dtype(seq1) and is_numeric_dtype(seq2)
            and len(np.unique(np.concatenate((seq1, seq2)))) > 4):
        if bins is None:
            bins = bin_it(np.concatenate((seq1.values, seq2.values), axis=0))
        frequencies1, edges1 = np.histogram(seq1.dropna(), bins, density=True)
        frequencies2, edges2 = np.histogram(seq2.dropna(), bins, density=True)
        plot_object = hv.NdOverlay({
            "df1": hv.Histogram((edges1, frequencies1)),
            "df2": hv.Histogram((edges2, frequencies2)),
        })
        plot_object.opts("Histogram", fill_alpha=0.5).redim.label(x="Value")
    else:
        plot_df_1 = pd.DataFrame(seq1.value_counts() / len(seq1))
        plot_df_1["Data frame"] = "df1"
        plot_df_2 = pd.DataFrame(seq2.value_counts() / len(seq2))
        plot_df_2["Data frame"] = "df2"
        plot_df = pd.concat([plot_df_1, plot_df_2]).reset_index()
        plot_df.columns = ["Category", "Count", "Source"]
        if len(plot_df.Category.unique()) < max_bar_categories:
            plot_object = hv.Bars(plot_df, ["Category", "Source"], "Count")
        else:
            train_levels = plot_df_1.index.unique()
            test_levels = plot_df_2.index.unique()
            plot_object = hv.Bars((
                ["Only df1", "In both", "Only df2"],
                [
                    len([t for t in train_levels if t not in test_levels]),
                    len([t for t in train_levels if t in test_levels]),
                    len([t for t in test_levels if t not in train_levels]),
                ],
            )).opts(invert_axes=True)
    return plot_object
Example #3
0
def hist(vals, **kwargs):
    """
    Use with s.pipe(hist)
    """
    import holoviews as hv

    return hv.Histogram(np.histogram(vals, bins="auto"), **kwargs)
Example #4
0
    def _create_histogram(df: pandas.DataFrame, histogram_name: HistogramName, bins: int = 20) -> hv.Histogram:
        """
        ヒストグラムを生成する。
        Args:
            df:
            histogram_name:
            bins: ヒスグラムの棒の数

        Returns:
            ヒストグラムオブジェクト
        """
        mean = round(df[histogram_name.column].mean(), 3)
        std = round(df[histogram_name.column].std(), 3)
        title = f"{histogram_name.title}: μ={mean}, α={std}, N={len(df)}"

        data = df[histogram_name.column].values

        frequencies, edges = np.histogram(data, bins)
        hist = (
            hv.Histogram(
                (edges, frequencies), kdims=histogram_name.x_axis_label, vdims=histogram_name.y_axis_label, label=title
            )
            .options(width=500, title=title, fontsize={"title": 9, "labels": 9})
            .opts(hv.opts(tools=["hover"]))
        )
        return hist
Example #5
0
def histogram(data, bins, xlabel):

    return hv.Histogram(np.histogram(data, bins=bins,
                                     density=True)).opts(xlabel=xlabel,
                                                         width=500,
                                                         height=500,
                                                         shared_axes=False)
Example #6
0
def integrated_charge(limit_a, limit_b, y, iteration):
    # compute 1D histogram
    energy_hist, bin_edges, nbins = particle_energy_histogram(
        tseries=time_series,
        it=iteration,
        cutoff=np.inf,  # no cutoff
        energy_max=e_max,
    )

    histogram = hv.Histogram((bin_edges, energy_hist),
                             kdims=energy,
                             vdims=count)
    curve = hv.Curve(histogram)

    e_min = histogram.edges[0]

    limit_a = e_min if limit_a is None else np.clip(limit_a, e_min, e_max)
    limit_b = e_max if limit_b is None else np.clip(limit_b, e_min, e_max)

    area = hv.Area((curve.dimension_values('energy'),
                    curve.dimension_values('frequency')))[limit_a:limit_b]
    charge = np.sum(
        np.diff(histogram[limit_a:limit_b].edges) *
        histogram[limit_a:limit_b].values)

    return curve * area * hv.VLine(limit_a) * hv.VLine(limit_b) * hv.Text(
        limit_b - 2., 5, 'Q = %.0f pC' % charge)
def _create_price_and_volume_histogram(  # pylint: disable=too-many-arguments
        data,
        price="close",
        volume="volume",
        bins=150,
        color=GRAY,
        alpha=0.5):
    formatter = NumeralTickFormatter(format="0,0")
    hist = np.histogram(data[price], bins=bins, weights=data[volume])
    hist_data = zip(hist[1], hist[0])
    tooltips = [
        ("Price", "@{price}"),
        ("Volume", "@volume{0,0}"),
    ]
    hover = HoverTool(tooltips=tooltips)
    return (hv.Histogram(hist_data).redim(Frequency="volume", x="price").opts(
        xformatter=formatter,
        color=color,
        alpha=alpha,
        tools=["ybox_zoom", "reset", hover],
        default_tools=[],
        invert_axes=True,
        hooks=[set_toolbar_none],
        xticks=2,
    ))
Example #8
0
def SimplifiedBarChart(dfs,
                       measurement,
                       configs,
                       analysisType,
                       xaxis,
                       bins=50,
                       **addConfigs):
    """Generates a simplified bar chart with a simplified x axis, can be handy if you have lots of points """
    newConfigs = addConfigs
    log.info("Generating BarChart for measurement {}...".format(measurement))
    finalplots = None
    try:
        for key in dfs["keys"]:
            log.info("Generating histograms for measurement {} for file {}...".
                     format(measurement, key))
            # Sanatize data
            data = dfs[key]["data"][[measurement,
                                     xaxis]].dropna()  # Drop all nan
            invertedaxis = data.reset_index().set_index(measurement)
            data = np.histogram(data[measurement], bins=data[xaxis])

            plt = hv.Histogram(data,
                               label="BarChart: {}".format(measurement),
                               group="{}".format(key))

            try:
                xlabel = "{} [{}]".format(
                    measurement,
                    dfs[dfs["keys"][0]]["units"][dfs[
                        dfs["keys"][0]]["measurements"].index(measurement)],
                )
            except Exception as err:
                log.error(
                    "Label could not be generated for Histogram {}. Error: {}".
                    format(measurement, err))
                xlabel = "X-Axis"

            plt.opts(xlabel=xlabel)
            # Update the plot specific options if need be
            generalOptions = configs[analysisType].get("General", {})
            newConfigs.update(generalOptions.copy())
            data_options = (configs[analysisType].get(measurement, {}).get(
                "Single Histogram", {}).get("PlotOptions", {}))
            newConfigs.update(configs[analysisType].get(
                "{}Options".format("Histogram"), {}))
            newConfigs.update(data_options)
            plots = customize_plot(plt, "", configs[analysisType],
                                   **newConfigs)

            if finalplots:
                finalplots += plots
            else:
                finalplots = plots
    except Exception as err:
        log.error(
            "Unexpected error happened during Hist plot generation {}. Error: {}"
            .format(measurement, err))
        return None

    return finalplots
Example #9
0
 def plot(self):
     import holoviews as hv
     vals = [v for v in self.data.values() if v is not None]
     if not vals:
         return hv.Histogram([[], []])
     num_bins = int(max(5, sqrt(self.n)))
     vals = hv.Points(vals)
     return hv.operation.histogram(vals, num_bins=num_bins, dimension=1)
Example #10
0
def hist(x, logx=False, logy=False, label=None, color=None, **kwargs):
    """
    Creates holoviews histrogram object.
        logx=True: Create log spaced bins between min/max
        logy=True: Compute histogram of (1 + x) with y on db scale
        **kwargs are passed to numpy.histogram

    """
    # If logx was specified, create log spaced bins
    import numpy as np
    import holoviews as hv
    if logx:
        nbins = kwargs.get('bins', 10)
        if not isinstance(nbins, int):
            raise ValueError('Bins must be an integer when logx=True')

        range_vals = kwargs.get('range', None)
        if range_vals:
            minval, maxval = range_vals
        else:
            minval, maxval = np.min(x), np.max(x)

        bins = np.logspace(np.log10(minval), np.log10(maxval), nbins)
        kwargs.update(bins=bins)

    # Build up kwargs for the holoviews call
    hv_kwargs = {}
    if label is not None:
        hv_kwargs['label'] = label

    # If logy was specified, create a histogram of the db of (counts + 1)
    if logy:
        counts, edges = np.histogram(x, **kwargs)
        counts = 10 * np.log10(1 + counts)
        c = hv.Histogram((counts, edges), vdims='dB of (counts + 1)', **hv_kwargs)
    # If not logy, just to a histogram of counts
    else:
        c = hv.Histogram(np.histogram(x, **kwargs), **hv_kwargs)

    # Default the x axis to log if logx was specified
    if logx:
        c = c.options(logx=True)
    c = c.options(alpha=.3)
    if color is not None:
        c = c.options(color=color)
    return c
Example #11
0
 def test_histogram_ellipsis_slice_value_missing(self):
     frequencies, edges = np.histogram(range(20), 20)
     try:
         hv.Histogram(frequencies, edges)[..., 'Non-existent']
         raise AssertionError("No assertion raised")
     except Exception as e:
         if str(e) != repr(
                 "'Frequency' is the only selectable value dimension"):
             raise AssertionError("Incorrect exception raised.")
Example #12
0
def fash_plots(corr_range):
    # Create matrix and histogram plots
    # ---------------------------------
    # raterize() fucntion used for big data set
    fash_corr_img = rasterize(
        hv.Image(
            np.rot90(fash_corr),
            bounds=(-0.5, -0.5, fash_num_samp - 1.5,
                    fash_num_samp - 1.5)).opts(
                        cmap='viridis',
                        colorbar=True,
                        height=300,
                        width=400,
                        title='Correlation Matrix')).redim.range(z=corr_range)
    fash_dist_img = rasterize(
        hv.Image(np.rot90(fash_dist),
                 bounds=(-0.5, -0.5, fash_num_samp - 1.5,
                         fash_num_samp - 1.5)).opts(cmap='viridis',
                                                    colorbar=True,
                                                    height=300,
                                                    width=400,
                                                    title='Distance Matrix'))
    fash_corr_his = rasterize(
        hv.Histogram((fash_corr_edges,
                      fash_corr_freq)).opts(xlabel='Correlation',
                                            height=300,
                                            width=400,
                                            title='Correlation Histogram'))
    fash_dist_his = rasterize(
        hv.Histogram((fash_dist_edges,
                      fash_dist_freq)).opts(xlabel='Distance',
                                            height=300,
                                            width=400,
                                            title='Distance Histogram'))

    dash = (fash_corr_img + fash_corr_his + fash_dist_img +
            fash_dist_his).opts(opts.Layout(shared_axes=False)).cols(
                2)  # Dashboard of all plots

    return dash
Example #13
0
    def __init__(self, topics=None, terms=None, addr=None, **kwargs):
        super().__init__(topics, terms, addr, pipes=False, **kwargs)
        self.num_terms = int(len(terms) / 2) if terms else 0
        plots = []

        for i in range(0, self.num_terms):
            y = self.terms[f"Counts.{i}" if i > 0 else "Counts"]
            self.pipes[y] = hv.streams.Pipe(data=[])
            plots.append(
                hv.DynamicMap(lambda data: hv.Histogram(data),
                              streams=[self.pipes[y]]))

        self._plot = hv.Overlay(
            plots).collate() if len(plots) > 1 else plots[0]
Example #14
0
    def plot_histogram(data):
        """
        Plot the histogram.

        Args:
            data: Tuple containing (values, bins), xlim. xlim is a tuple \
                  containing two scalars that represent the limits of the x \
                  axis of the histogram.

        Returns:
            Histogram plot.

        """
        plot_data, xlim = data
        return holoviews.Histogram(plot_data).redim(x=holoviews.Dimension("x", range=xlim))
Example #15
0
def main(json_path: str, type_: str, out: str):
    assert out[-5:] == ".html"

    if type_ == "f":
        assert json_path[-5:] == ".json"
        list_json_paths = [Path(json_path)]
    else:
        list_json_paths = list(Path(json_path).glob("**/*.json"))

    all_data: list = []
    for pathfile in list_json_paths:
        with open(pathfile) as json_file:
            data = json.load(json_file)
            dictionary = {}
            for k, v in data.items():
                if type(v) == int or type(v) == float:
                    dictionary[k] = v

                elif type(v) == bool:
                    dictionary[k] = int(v)
            all_data += [dictionary]

    df = pd.DataFrame(all_data)
    widgets: list = []

    for column_name in df.columns:
        data = df[column_name].values
        frequencies, edges = np.histogram(data)
        hist = hv.Histogram((edges, frequencies))
        hist.opts(tools=["hover"], title=column_name, width=600)

        annotation = ("<h2>{}</h2>"
                      "<b>Overview</b></br>{}</br>{}</br>"
                      "<b>Describe</b></br>{}</br>").format(
                          column_name,
                          data.shape,
                          np.array_repr(data),
                          pformat(dict(describe(data)._asdict())),
                      )
        annotation = annotation.replace("\n", "</br>")
        html_annotation = Div(text=annotation)

        widgets.append(row(hv.render(hist), html_annotation))

    layout = column(*widgets)

    output_file(out, title=out)
    save(layout)
Example #16
0
def main(npz_filepath, out):
    assert out[-5:] == ".html", "Please enter an html output file"
    data = np.load(npz_filepath)

    widgets = []
    widgets += [
        Div(text="<h1>NPZ statistic</h1><b>Subfiles found</b></br>{}".format(
            data.files))
    ]
    output_file(out, title="NPZ statistic")
    for subfile in data.files:
        subdata = data[subfile].squeeze()
        array_repr = subdata.__str__()
        array_describe = pformat(dict(describe(subdata)._asdict()))
        if subdata.ndim == 1:

            hist, edges = np.histogram(subdata)

            hv_hist = hv.Histogram((edges, hist))
            hv_hist.opts(tools=["hover"],
                         title=subfile + " histogram",
                         width=600)
            fig = hv.render(hv_hist)

        elif subdata.ndim == 2:
            heatmap = hv.Image(subdata)
            heatmap.opts(colorbar=True,
                         width=600,
                         height=600,
                         tools=["hover"],
                         title=subfile)
            fig = hv.render(heatmap)

        else:
            fig = Div(text="To many dimension to visualize")

        annotation = ("<h2>{}</h2>"
                      "<b>Overview</b></br>{}</br>{}</br>"
                      "<b>Describe</b></br>{}</br>").format(
                          subfile, subdata.shape, array_repr, array_describe)
        annotation = annotation.replace("\n", "</br>")

        html_annotation = Div(text=annotation)

        widgets += [row([fig, html_annotation])]

    data.close()
    show(column(*widgets))
Example #17
0
    def plot_slack_histogram(self, task: TaskID, bins: int = 30):
        """
        Plot the slack histogram.

        :param task: the rt-app task to filter for
        :type task: int or str or lisa.trace.TaskID

        :param bins: number of bins for the histogram.
        :type bins: int

        .. seealso:: :meth:`plot_perf` for the slack definition.
        """
        task = self.trace.get_task_id(task)
        name = f'slack of {task} (us)'
        series = self.df_rtapp_stats(task)['slack']
        return hv.Histogram(np.histogram(series, bins=bins)).options(
            xlabel=name,
            title=name,
        )
Example #18
0
def plot_hist(
    label,
    bins=50,
    width=1000,
    height=500,
):
    frequencies, edges = numpy.histogram(
        [c[label] for c in cf.molecular_clouds],
        bins,
    )
    renderer = holoviews.renderer('bokeh')
    hist = holoviews.Histogram((edges, frequencies))
    hist = hist.options(
        width=width,
        height=height,
    )
    renderer.save(
        hist,
        os.path.join(PLOT_DIR, '{}_hist_{}'.format(cf.label, label)),
    )
Example #19
0
    def histogram_plot_hv(self,
                          df,
                          title,
                          var,
                          bin_size=30,
                          height=400,
                          width=1000):
        frequencies, edges = np.histogram(df.dropna()[var], bins=bin_size)
        fig = hv.Histogram((edges, frequencies),
                           label=title).opts(fontsize={
                               'title': 16,
                               'labels': 12,
                               'xticks': 10,
                               'yticks': 10
                           },
                                             height=height,
                                             width=width,
                                             fill_color="#34495e")

        return fig
def _create_normalized_price_and_volume_histogram(hist_data,
                                                  color=ACCENT_COLOR,
                                                  alpha=0.5):
    formatter = NumeralTickFormatter(format="0,0")
    tooltips = [
        ("Price", "@{Price}{0,0.000}"),
        ("Volume Density", "@{VolumeDensity}{0,0.000}"),
    ]
    hover = HoverTool(tooltips=tooltips)
    return (hv.Histogram(hist_data).redim(
        Frequency="VolumeDensity", x="Price").opts(
            xformatter=formatter,
            color=color,
            alpha=alpha,
            tools=["xbox_zoom", "reset", hover, "save"],
            default_tools=[],
            ylabel="Volume Density",
            # invert_axes=True,
            # hooks=[set_toolbar_none],
            # xticks=2,
        ))
def _create_time_and_volume_histogram(data,
                                      time="time",
                                      volume="volume",
                                      color=GRAY,
                                      alpha=0.5):
    formatter = NumeralTickFormatter(format="0,0")
    hist_data = zip(data[time], data[volume])
    tooltips = [
        ("Time", "@{time}{%F}"),
        ("Volume", "@volume{0,0}"),
    ]
    hover = HoverTool(tooltips=tooltips, formatters={"@{time}": "datetime"})
    return (hv.Histogram(hist_data).redim(Frequency="volume", x="time").opts(
        yformatter=formatter,
        color=color,
        alpha=alpha,
        tools=["xbox_zoom", "reset", hover],
        default_tools=[],
        hooks=[set_toolbar_none],
        yticks=4,
    ))
Example #22
0
    def plot_latencies_histogram(self, task: TaskID, wakeup: bool=True,
            preempt: bool=True, threshold_ms: float=1, bins: int=64):
        """
        Plot the latencies histogram of a task

        :param task: The task's name or PID
        :type task: int or str or tuple(int, str)

        :param wakeup: Whether to plot wakeup latencies
        :type wakeup: bool

        :param preempt: Whether to plot preemption latencies
        :type preempt: bool

        :param threshold_ms: The latency threshold to plot
        :type threshold_ms: int or float
        """

        df = self._get_latencies_df(task, wakeup, preempt)
        threshold_s = threshold_ms / 1e3
        name = f'Latencies histogram of task {task}'
        return (
            hv.Histogram(
                np.histogram(df['latency'], bins=bins),
                label=name,
            ) *
            hv.VSpan(
                0, threshold_s,
                label=f"{threshold_ms}ms threshold zone",
            ).options(
                color=self.LATENCY_THRESHOLD_ZONE_COLOR,
                alpha=0.5,
            )
        ).options(
            xlabel='Latency (s)',
            title=name,
        )
Example #23
0
#%%

### Histograms of PM 2.5 measurement distributions
hv.extension('bokeh', logo=False)
import numpy as np
import holoviews as hv

data = Paccar['PM2_5']
data = data.values
data = data[~np.isnan(data)]

frequencies, edges = np.histogram(data, 70)

p2 = figure(plot_width=1500, plot_height=700)

p2 = hv.Histogram((edges, frequencies))
p2 = p2.options(xlabel='PM 2.5 (ug/m3)', ylabel='Frequency', title='Paccar')

data = Reference['PM2_5']
data = data.values
data = data[~np.isnan(data)]

frequencies, edges = np.histogram(data, 70)

p3 = figure(plot_width=1500, plot_height=700)

p3 = hv.Histogram((edges, frequencies))
p3 = p3.options(xlabel='PM 2.5 (ug/m3)', ylabel='Frequency', title='Reference')

data = Augusta['PM2_5']
data = data.values
Example #24
0
def Histogram(dfs,
              measurement,
              configs,
              analysisType,
              bins=50,
              iqr=None,
              **addConfigs):
    """Generates a Points Plot with a corresponding Histogram"""
    newConfigs = addConfigs
    log.info("Generating histograms for measurement {}...".format(measurement))
    finalplots = None
    try:
        for key in dfs["keys"]:
            log.info("Generating histograms for measurement {} for file {}...".
                     format(measurement, key))
            # Sanatize data
            data = dfs[key]["data"][measurement].dropna()  # Drop all nan
            if iqr:
                log.info("Outliers correction with iqr: {}".format(iqr))
                data = reject_outliers(data, iqr)
            mean = np.round(np.mean(data), 2)
            rms = np.round(np.sqrt(np.mean(data**2)), 2)
            std = np.round(np.std(data), 2)
            median = np.round(np.median(data), 2)
            data = np.histogram(data, bins=bins)
            plt = hv.Histogram(data,
                               label="Histogram: {}".format(measurement),
                               group="Histogram: {}: {}".format(
                                   measurement, key))

            try:
                xlabel = "{} [{}]".format(
                    measurement, dfs[dfs["keys"][0]]["units"][dfs[
                        dfs["keys"][0]]["measurements"].index(measurement)])
            except Exception as err:
                log.error(
                    "Label could not be generated for Histogram {}. Error: {}".
                    format(measurement, err))
                xlabel = "X-Axis"

            plt.opts(xlabel=xlabel)
            # Update the plot specific options if need be
            generalOptions = configs[analysisType].get("General", {})
            newConfigs.update(generalOptions.copy())
            data_options = configs[analysisType].get(measurement, {}).get(
                "Single Histogram", {}).get("PlotOptions", {})
            newConfigs.update(configs[analysisType].get(
                "{}Options".format("Histogram"), {}))
            newConfigs.update(data_options)
            plots = customize_plot(plt, "", configs[analysisType],
                                   **newConfigs)

            # Add text
            text = '\nMean: {mean} \n' \
                   'Median: {median} \n' \
                   'RMS: {rms}\n' \
                   'std: {std}'.format(mean=mean,
                                       median=median,
                                       rms=rms,
                                       std=std)
            log.info(text)
            y = data[0].max()
            x = data[1][int(len(data[1]) * 0.9)]
            text = hv.Text(x, y, text).opts(fontsize=30)
            #text = text_box(text, x, y, boxsize= (100, 150))
            plots = plots * text

            if finalplots:
                finalplots += plots
            else:
                finalplots = plots
    except Exception as err:
        log.error(
            "Unexpected error happened during Hist plot generation {}. Error: {}"
            .format(measurement, err))
        return None

    return finalplots
Example #25
0
def concatHistogram(dfs,
                    measurement,
                    configs,
                    analysisType,
                    bins=50,
                    iqr=None,
                    **addConfigs):
    """Concatenates dataframes and generates a Histogram for all passed columns"""
    newConfigs = addConfigs
    log.info("Generating concat histograms for measurements {}...".format(
        measurement))
    try:
        df = dfs["All"]
        # Sanatize data
        data = df[measurement].dropna()  # Drop all nan
        if iqr:
            log.info("Outliers correction with iqr: {}".format(iqr))
            data = reject_outliers(data, iqr)
        mean = np.round(np.mean(data), 2)
        rms = np.round(np.sqrt(np.mean(data**2)), 2)
        std = np.round(np.std(data), 2)
        median = np.round(np.median(data), 2)
        data = np.histogram(data, bins=bins)

        plt = hv.Histogram(
            data,
            label="Concatenated Histogram: {}".format(measurement),
            group="Concatenated Histogram: {}".format(measurement))
        #plt = hv.Histogram(data, vdims=to_plot, group="Concatenated Histogram: {}".format(to_plot))

        try:
            xlabel = "{} [{}]".format(
                measurement, dfs[dfs["keys"][0]]["units"][dfs[
                    dfs["keys"][0]]["measurements"].index(measurement)])
        except Exception as err:
            log.error(
                "Label could not be genereated for concatonated Histogram {}. Error: {}"
                .format(measurement, err))
            xlabel = "X-Axis"

        plt.opts(xlabel=xlabel)
        # Update the plot specific options if need be
        generalOptions = configs[analysisType].get("General", {})
        newConfigs.update(generalOptions.copy())
        data_options = configs[analysisType].get(measurement, {}).get(
            "Concatenated Histogram", {}).get("PlotOptions", {})
        newConfigs.update(configs[analysisType].get(
            "{}Options".format("Histogram"), {}))
        newConfigs.update(data_options)
        #addConfigs.update({"xlabel": measurement})
        plots = customize_plot(plt, "", configs[analysisType], **newConfigs)

        # Add text
        text = '\nMean: {mean} \n' \
               'Median: {median} \n' \
               'RMS: {rms}\n' \
               'std: {std}'.format(mean=mean,
                                median=median,
                                rms=rms,
                                std=std)
        log.info(text)
        y = data[0].max()
        x = data[1][int(len(data[1]) * 0.9)]
        text = hv.Text(x, y, text).opts(fontsize=30)
        plots = plots * text

    except Exception as err:
        log.error(
            "Unexpected error happened during concatHist plot generation {}. Error: {}"
            .format(measurement, err))
        return None

    return plots
Example #26
0
 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)
Example #27
0
    def create_hist(self, df_sel, var):
        # keep histogram pairs consistent with the same xlim + ylim
        # since the pairs are likely to be min + max or somehow related
        # for more intuitive comparison between the pairs
        col_ind = list(df_sel.columns).index(var)
        if col_ind % 2 == 0:
            var_ref = df_sel.columns[col_ind + 1]
        else:
            var_ref = df_sel.columns[col_ind - 1]
        var_min = df_sel[[var, var_ref]].min().min()
        var_max = df_sel[[var, var_ref]].max().max()

        oom = self.order_of_mag(var_max) - 1
        scale = 10**oom
        if oom > 0:
            scale = np.log10(scale)
        base = scale * 5

        var_min = self.roundn(var_min, base=base, method='down')
        var_max = self.roundn(var_max, base=base)

        if var_max < 1:
            var_max = 1

        num_bins = (var_max - var_min) / base
        if num_bins <= 7:
            var_bins = np.arange(var_min, var_max, base / 3).tolist()
        elif num_bins <= 14:
            var_bins = np.arange(var_min, var_max, base / 2).tolist()
        else:
            var_bins = np.arange(var_min, var_max, base).tolist()

        if var_max == var_min:
            var_max += 0.01
        xlim = var_min - base / 3, var_max + base / 3

        var_freq, var_edge = np.histogram(df_sel[var].values, bins=var_bins)
        var_ref_freq, _ = np.histogram(df_sel[var_ref].values, bins=var_bins)
        ymax = max(var_freq.max(), var_ref_freq.max())
        ylim = (0, ymax + ymax / 5)

        var_split = var.split()
        var_field = ' '.join(var_split[:-1])
        var_fmt = '.2f' if var not in ['Precip In', 'Snow In'] else '.2f'
        var_units = var_split[-1]

        var_hist = hv.Histogram((var_edge, var_freq)).opts(
            xlim=xlim,
            ylim=ylim,
            xlabel='',
            ylabel='Number of Days',
        ).redim.label(x=var, Frequency=f'{var_field} Count')

        plot = var_hist
        # highlight selected date
        var_sel = df_sel.loc[self.datetime.strftime('%Y-%m-%d'), var]
        if np.isnan(var_sel):
            label = var
        else:
            var_ind = np.where(var_edge <= var_sel)[0][-1]
            if var_ind == len(var_edge) - 1:
                var_ind -= 1
            var_slice = slice(*var_edge[var_ind:var_ind + 2])
            var_hist_hlgt = var_hist[var_slice, :].opts(fill_color=RED)
            label = f'{var_field}: {var_sel:{var_fmt}} {var_units}'
            plot *= var_hist_hlgt

        if var_freq.max() == 0:
            plot *= hv.Text(xlim[1] / 2, ylim[-1] / 2, 'Data N/A', fontsize=18)

        try:
            var_climo = df_sel.iloc[0][f'Climo {var}']
            var_vline = hv.VLine(var_climo)
            plot *= var_vline
        except KeyError:
            pass

        return plot.opts(title=label)
Example #28
0
        upper_bound : float, optional
            the upper bound to truncate the underlying distribution at
        bins : int, optional
            number of bins for the histogram which should be around one percent
            of the number of samples, which is the default

    Returns
    -------

        hist : hv.Histogram
            the holoviews histogram of the samples
    """
    # We create a histogram based on Bokeh, but could exchange that simply by
    # "matplotlib".
    hv.extension("bokeh")

    if bins is None:
        bins = int(size / 100)

    # Draw the first set of samples.
    realizations = dist.rvs(size=size)
    # Now as long as there are samples outside the desired range, redraw and fill up
    # the sample size with valid samples.
    while np.count_nonzero(mask := (realizations < lower_bound)
                           | (realizations > upper_bound)):
        realizations[mask] = dist.rvs(size=np.count_nonzero(mask))

    # Produce the input format for the holoview histogram using the according numpy
    # routine.
    return hv.Histogram(np.histogram(realizations, bins))
Example #29
0
def histogram(x, bins=10, **kwargs):
    return hv.Histogram(np.histogram(x, bins=bins), **kwargs)
Example #30
0
 def test_histogram_ellipsis_slice_range(self):
     frequencies, edges = np.histogram(range(20), 20)
     sliced = hv.Histogram(frequencies, edges)[0:5, ...]
     self.assertEqual(len(sliced.data[0]), 5)