Ejemplo n.º 1
0
def _show_sunset_hour(df, geoloc):
    hover = HoverTool(tooltips=[
        ('Date', '@datetime{%m/%d}'),
        ('Hour of Sunset', '@hour{0.1f}'),
        ('Length of Day', '@daylight{0.1f}'),
    ],
                      formatters={
                          'datetime': 'datetime',
                      },
                      mode='vline')

    sunset_df = df.copy()
    sunset_df = df.loc[df['sun_down'] == False, :]
    sunset_df = sunset_df.assign(**{
        'hour': sunset_df['hour'] - 12,
        'hour_24': sunset_df['hour_24'] - 12
    })

    lat, lon = geoloc.latitude, geoloc.longitude
    address = geoloc.address
    sunset_curve = sunset_df.hvplot('datetime',
                                    'hour',
                                    hover_cols=['daylight'],
                                    responsive=True)
    sunset_curve = sunset_curve.opts(
        invert_yaxis=True,
        color='darkblue',
        xlabel='Date',
        ylabel='PM Hour of Sunset [Local Time]',
        title=f'Yearly Sunset Hour at {address} ({lat:.1f} N, {lon:.1f} E)',
        hooks=[_format_datetime_axis],
        show_grid=True,
        gridstyle={'ygrid_line_alpha': 0},
        tools=[hover],
        ylim=(4, 9))

    sun_up = hv.Area(df.loc[df['sun_down'] == False], 'datetime', 'hour')
    sun_up = sun_up.opts(color='tan', alpha=0.15, responsive=True)

    sun_down = hv.Area(sunset_df, 'datetime', ['hour', 'hour_24'])
    sun_down = sun_down.opts(color='darkblue', alpha=0.15, responsive=True)

    five_pm_line = hv.HLine(5).opts(color='black',
                                    alpha=0.1,
                                    line_dash='dotted',
                                    responsive=True)

    five_pm_txt = hv.Text(pd.datetime(2020, 7, 4), 5, '5 PM')
    five_pm_txt = five_pm_txt.opts(text_font_size='1.5em',
                                   text_alpha=0.2,
                                   text_baseline='bottom',
                                   text_align='left',
                                   responsive=True)

    overlay = (sunset_curve * sun_up * sun_down * five_pm_line * five_pm_txt)
    return overlay
def modify_doc(doc):
    X = np.linspace(0, 3, 200)
    Y = X**2 + 3
    Y2 = np.exp(X) + 2
    Y3 = np.cos(X)
    final = hv.Area((X, Y, Y2), vdims=['y', 'y2']) * hv.Area(
        (X, Y, Y3), vdims=['y', 'y3'])

    plot = renderer.get_plot(final)
    layout = row(plot.state)
    # renderer.server_doc(layout)

    doc.add_root(layout)
Ejemplo n.º 3
0
 def area(self, x, y):
     if x and y:
         return hv.Area(self.data, kdims=[x],
                        vdims=[y]).opts(plot=self._plot_opts)
     index = self.data.index.name or 'index'
     df = self.data.reset_index()
     areas = []
     for c in self.data.columns:
         areas.append(hv.Area(df, kdims=[index], vdims=[c], label=c))
     areas = hv.Overlay(areas)
     if self.stacked:
         return hv.Area.stack(areas)
     else:
         return areas
Ejemplo n.º 4
0
def plot_1d_min_max_with_region_bar(
    run_diags: RunDiagnostics,
    varfilter_min: str,
    varfilter_max: str,
) -> HVPlot:
    """Plot all diagnostics whose name includes varfilter. Plot is overlaid across runs.
    All matching diagnostics must be 1D."""
    p = hv.Cycle("Colorblind")
    hmap = hv.HoloMap(kdims=["variable", "region", "run"])

    variables_to_plot = run_diags.matching_variables(varfilter_min)

    for run in run_diags.runs:
        for min_var in variables_to_plot:
            max_var = min_var.replace(varfilter_min, varfilter_max)
            vmin = run_diags.get_variable(run, min_var).rename("min")
            vmax = run_diags.get_variable(run, max_var).rename("max")
            style = "solid" if run_diags.is_baseline(run) else "dashed"
            long_name = vmin.long_name
            region = min_var.split("_")[-1]
            # Area plot doesn't automatically add correct y label
            ylabel = f'{vmin.attrs["long_name"]} {vmin.attrs["units"]}'
            hmap[(long_name, region, run)] = hv.Area(
                (vmin.time, vmin, vmax), label="Min/max",
                vdims=["y", "y2"]).options(line_dash=style,
                                           color=p,
                                           alpha=0.6,
                                           ylabel=ylabel)
    return HVPlot(_set_opts_and_overlay(hmap))
Ejemplo n.º 5
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)
Ejemplo n.º 6
0
 def _make_area_static(self):
     """Создаем диаграммы с выбором диапазона дат"""
     for dim in self._ddims:
         df = self._get_count(dim)
         diagram = hv.Area(df, kdims=[dim], vdims=['count'])
         selector = (hv.streams.BoundsX(source=diagram).rename(boundsx=dim))
         self._diagrams[dim] = _Plot(selector, diagram)
Ejemplo n.º 7
0
    def select_series(boundsy):
        test = df.columns.values[0] == boundsy[0]

        def plot_series(df, i):
            data = {"x": df.index.values, "y": df.iloc[:, i].values}
            return hv.Area(data)  #* hv.Curve(data).opts(tools=["hover"])

        low, high = boundsy
        low, high = int(np.floor(low)), int(np.ceil(high))
        overlay = hv.NdOverlay({
            df.columns[i]: hv.Area(df.iloc[:, i].values)
            #* hv.Curve(df.iloc[:, i].values).opts(tools=["hover"]))
            for i in range(low, high)
        }).opts(legend_position='left', fontsize={'legend': '6pt'})
        ticks = list(
            zip(range(len(df.index)),
                [pd.to_datetime(x).strftime("%Y-%m-%d") for x in df.index]))
        return hv.Area.stack(
            overlay.opts(
                xticks=ticks[::len(df.index) // 15],
                ylim=(
                    0, None
                ),  #df.iloc[:, list(range(low,high))].values.max()*1.05),
                xlabel="Date",
                ylabel="Commits",
                xrotation=45))
def integral(limit_a, limit_b, y, time):
    limit_a = -3 if limit_a is None else np.clip(limit_a, -3, 3)
    limit_b = 3 if limit_b is None else np.clip(limit_b, -3, 3)
    curve = hv.Curve((xs, function(xs, time)))
    area = hv.Area((xs, function(xs, time)))[limit_a:limit_b]
    summed = area.dimension_values('y').sum() * 0.015  # Numeric approximation
    return (area * curve * hv.VLine(limit_a) * hv.VLine(limit_b) *
            hv.Text(limit_b - 0.8, 2.0, '%.2f' % summed))
Ejemplo n.º 9
0
def regression(df: DataFrame,
               x_val: str,
               y_val: str,
               vdims: List[str] = None,
               ci: int = 95,
               title: str = None) -> hv.NdOverlay:
    """
    Seaborn's regplot implemented in Holoviews

    Args:
        df: Input dataframe
        x_val: Column name for x-variable
        y_val: Column name for y-variable
        vdims: Additional vdims to include that will display using the hover tool (optional)
        ci: Confidence interval to calculate for regression [0, 100]
        title: Title for plot (optional)

    Returns:
        Combined plot of scatter points, regression line, and CI interval
    """
    # Define grid for x-axis
    xmin, xmax = df[x_val].min(), df[x_val].max()
    grid = np.linspace(xmin, xmax)
    # Seaborn Fit and CI via Bootstrapping
    yhat, yhat_boots = _fit_fast(grid, df[x_val], df[y_val])
    ci = sns.utils.ci(yhat_boots, which=ci, axis=0)
    # Define plot Elements
    vdims = [y_val] + vdims if vdims else [y_val]
    scatter = hv.Scatter(data=df, kdims=[x_val],
                         vdims=vdims).options(tools=['hover'],
                                              width=700,
                                              height=300,
                                              color='blue',
                                              size=5,
                                              alpha=0.50)
    regline = hv.Curve((grid, yhat)).options(color='red')
    lower = hv.Area((grid, yhat, ci[0]), vdims=['y',
                                                'y2']).options(color='red',
                                                               alpha=0.15)
    upper = hv.Area((grid, yhat, ci[1]), vdims=['y',
                                                'y2']).options(color='red',
                                                               alpha=0.15)

    return (scatter * regline * lower * upper).relabel(title)
Ejemplo n.º 10
0
def beta_plots(
        wins: Iterable,
        losses: Iterable,
        labels: Iterable,
        legend_position='right',
        alpha=.5,
        normed=False,
        xlabel=None,
        ylabel=None):
    """
    Make beta plots for win/loss type scenarios.  The wins/losses are provided in arrays.
    Each element of the array corresponds to a specific win/loss scenario you want plotted.

    So, to just determine the beta distirbution of a single scenario, these should be
    one-element arrays.

    Args:
        wins: the number of "wins"
        losses: the number of "losses"
        labels: The label for each scenario
        legend_postion: Where to put the legend
        alpha: the opacity of the area plots
        xlabel: The x label [default "Win Perdentage"]
        ylabel: The y label [default, "Density"]
    """
    from scipy.stats import beta
    import numpy as np
    import holoviews as hv

    if xlabel is None:
        xlabel = 'Win Percentage'

    if ylabel is None:
        ylabel = 'Density'

    c_list = []
    x = np.linspace(0, 1, 500)
    xpoints = []
    ypoints = []
    for (won, lost, label) in zip(wins, losses, labels):
        dist = beta(won + 1, lost + 1)
        y = dist.pdf(x)
        if normed:
            y_max = np.max(y)
        else:
            y_max = 1.
        y = y / y_max
        win_frac = won / (won + lost + 1e-12)
        xpoints.append(win_frac * 100)
        ypoints.append(dist.pdf(win_frac) / y_max)
        c = hv.Area((100 * x, y), xlabel, ylabel, label=label).options(alpha=alpha)
        c_list.append(c)

    c1 = hv.Overlay(c_list).options(legend_position='right')
    c2 = hv.Scatter((xpoints, ypoints), xlabel, ylabel).options(color='black', size=8, tools=['hover'])
    return (c1 * c2).options(legend_position=legend_position)
Ejemplo n.º 11
0
def test_template_can_use_non_panels():
    header = "[Header Link](https://panel.holoviz.org)"
    sidebar = "[Menu Item](https://panel.holoviz.org)<br/>" * 5

    xs = np.linspace(0, np.pi * 4, 40)
    main = hv.Area((xs, np.sin(xs)))
    template = VanillaTemplate(title="My App")
    template.header.append(header)
    template.sidebar.append(sidebar)
    template.main.append(main)
    template.show()
Ejemplo n.º 12
0
def isotonic(df, predict, target, calibrations_data=None):
    """Визуализация точности прогноза вероятности

    **Аргументы**

    df : pandas.DataFrame
        таблица с данными

    predict : str
        прогнозная вероятность

    target : str
        бинарная (0, 1) целевая переменная

    calibrations_data : pandas.DataFrame
        таблица с калибровками

    **Результат**

    area * curve * [curve] : holoviews.Overlay
    """

    df_agg = _aggregate_data_for_isitonic(df, predict, target)

    confident_intervals = hv.Area(df_agg,
                                  kdims=['predict'],
                                  vdims=['ci_l', 'ci_h'],
                                  group='Confident Intervals')
    curve = hv.Curve(df_agg,
                     kdims=['predict'],
                     vdims=['isotonic'],
                     group='Isotonic')

    if calibrations_data is not None and target in calibrations_data.columns:
        calibration = hv.Curve(data=calibrations_data[['predict',
                                                       target]].values,
                               kdims=['predict'],
                               vdims=['target'],
                               group='Calibration',
                               label='calibration')
        return hv.Overlay(items=[curve, confident_intervals, calibration],
                          group='Isotonic',
                          label=predict)
    return hv.Overlay(items=[curve, confident_intervals],
                      group='Isotonic',
                      label=predict)
Ejemplo n.º 13
0
def xarray_to_interactive_dispatch_plot(results, tune, producer, time):
    """
    Creates a tunable dispatch plot from an xarray.
    
    Parameters
    ----------
    results : 
    
    tune_dims
    
    producer
    
    time

    Returns
    -------
    dispatch_hmap : HoloMap
    """
    tune_coords = {k: results.coords[k].data for k in tune}
    tune_sample = it.product(*tune_coords.values())
    producer_coord = results.coords[producer]
    time_coord = results.coords[time]

    dispatch_hmap = hv.HoloMap({t:
                        hv.Area.stack(
                            hv.Overlay([hv.Area(results.loc[dict(zip(tune_coords.keys(), t))].loc[{producer: s}])
                                        for s in producer_coord]
                                      ))
                                for t in tune_sample},
                                kdims=list(tune_coords.keys()))\
                        .options(width=500, height=200)

    ## https://github.com/ioam/holoviews/issues/1819
    renderer = hv.renderer('bokeh')

    # Using renderer save
    renderer.save(dispatch_hmap, 'example_sampling_and_plotting')
Ejemplo n.º 14
0
def create_interactive_plot(data_dispatch, data_bars):
    """

    """
    dispatch_hmap = hv.HoloMap(
        {(i, k): hv.Area.stack(
            hv.Overlay(
                [hv.Area(data_dispatch[k, i, :, j]) for j in range(n_stack)]))
         for i in range(n_i) for k in range(n_k)},
        kdims=['price pv', 'price wind'])

    bar_hmap = hv.HoloMap(
        {(i, k): hv.Bars(data_bars, hv.Dimension('Technology'),
                         'Installed capacity')
         for i in range(n_i) for k in range(n_k)},
        kdims=['price pv', 'price wind'])

    layout = hv.Layout(dispatch_hmap + bar_hmap).cols(1)

    ## https://github.com/ioam/holoviews/issues/1819
    renderer = hv.renderer('bokeh')

    # Using renderer save
    renderer.save(dispatch_hmap + bar_hmap, 'interactive_modeling')
Ejemplo n.º 15
0
    def wiggle_plot(self, gather, time_slice, wiggle_buttons):
        """
        NAME
        ----
           wiggle_plot.
        
        DESCRIPTION
        -----------
            Plots the amplitudes of a single angle gather.

            The plot will vary according to the following parameters:
                - time_slice will set the time window for the plot.
                - wiggle_button value will modify the way the amplitudes are displayed. More
                  details in wiggle_buttons argument.

        ARGUMENTS
        ---------
            gather : (Numpy)ndarray
                Amplitude array given by Segyio's gather method. Can be given manually.
            
            time_slice : list
                Time slice of interest. Can be given manually or by Panel's range slider widget.
            
            wiggle_buttons : str
                Desired amplitude's plot type. Can be given manually or by Panel's radio button
                widget. "Wavelet" will plot amplitudes using only a sine curve, "Black wiggle"
                will fill with black the area between the sin curve and time_axis and "Colored
                wiggle", will fill with blue/red the positive/negative area between the sin curve
                and time_axis.
            
            WiggleMethod.interpolation : bool
                Whether the amplitudes will be interpolated to improve wiggle display or not. 
                False by default.
            
        RETURN
        ------   
            wiggle_display : Holviews element [Overlay]
                Compilation of traces within the angle gather.
                                                  
        """
        amp_df = WiggleModule.amp_dataframe(self, gather, time_slice,
                                            wiggle_buttons)

        # Initializing the plot
        wiggle_display = hv.Curve((0, 0))

        # Computing scale factor for the X axis
        WiggleModule.plot_xticks = [
            (angle_position * self.scaling_fac,
             Survey.angle_list[angle_position])
            for angle_position in range(len(Survey.angle_list))
        ]

        # making the data to plot according a scaled value
        for trace in range(gather.shape[0]):

            # Hover designation
            hover_w = HoverTool(
                tooltips=[('Time', '@time_axis'),
                          ('Amplitude',
                           f"@amplitude_{Survey.angle_list[trace]}"
                           ), ("Angle", f"{Survey.angle_list[trace]}")])

            # Plotting the wiggle
            wiggle = hv.Curve(
                amp_df,
                ["time_axis", f"s_amplitude_{Survey.angle_list[trace]}"],
                [f"amplitude_{Survey.angle_list[trace]}"],
                label="W")
            wiggle.opts(color="black", line_width=2, tools=[hover_w])

            if wiggle_buttons != "Wavelet":

                if wiggle_buttons == "Black wiggle":
                    WiggleModule.positive_amp, WiggleModule.negative_amp = "black", "black"
                else:
                    WiggleModule.positive_amp, WiggleModule.negative_amp = "blue", "red"

                # Making the area plot more comfortable
                x = amp_df["time_axis"]
                y = self.scaling_fac * trace
                y2 = amp_df[f"s_negative_amplitude_{Survey.angle_list[trace]}"]
                y3 = amp_df[f"s_positive_amplitude_{Survey.angle_list[trace]}"]

                # Fill in between: Holoviews Element
                negative = hv.Area((x, y, y2), vdims=['y', 'y2'],
                                   label="-").opts(color=self.negative_amp,
                                                   line_width=0)
                positive = hv.Area((x, y, y3), vdims=['y', 'y3'],
                                   label="+").opts(color=self.positive_amp,
                                                   line_width=0)

                # Overlying the colored areas +  the zero phase wavelet
                wiggle_display *= wiggle * negative * positive

            else:
                wiggle_display *= wiggle

            # Adding final customizations
            wiggle_display.opts(xaxis="top",
                                invert_axes=True,
                                invert_yaxis=True,
                                xlabel="Time [ms]",
                                ylabel=" ",
                                xticks=self.plot_xticks,
                                xlim=(time_slice[0], time_slice[-1]))

        return (wiggle_display)
Ejemplo n.º 16
0
    def get_individual_plot(current_file):
        ds = open_files[current_file]

        # get reference flowline for true values
        rgi_id = ds.attrs['rgi_id']
        # rgi_id = translate_name_rgi[glacier_select.value]
        for gdir in gdirs:
            if gdir.rgi_id == rgi_id:
                fl_ref = gdir.read_pickle('model_flowlines',
                                          filesuffix='_combine_true_init')[0]

        # now calculate data for delta bed_h and w0_m
        data_bed_h = []
        d_bed_h_lim = 0
        data_w0_m = []
        d_w0_m_lim = 0
        for i, fl in enumerate(ds.flowlines.values):
            x_all = ds.coords['x'][ds.ice_mask].values

            # bed_h
            d_bed_h = (fl.bed_h - fl_ref.bed_h)[ds.ice_mask]
            d_bed_h_lim = np.max([d_bed_h_lim, np.max(np.abs(d_bed_h))])
            for el in [(x, i, v) for x, v in zip(x_all, d_bed_h)]:
                data_bed_h.append(el)

            # w0_m
            d_w0_m = (fl._w0_m - fl_ref._w0_m)[ds.ice_mask]
            d_w0_m_lim = np.max([d_w0_m_lim, np.max(np.abs(d_w0_m))])
            for el in [(x, i, v) for x, v in zip(x_all, d_w0_m)]:
                data_w0_m.append(el)

        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'))

        # plots for delta bed_h and w0_m
        delta_bed_h_plot = get_heatmap(data_bed_h,
                                       d_bed_h_lim,
                                       'Delta bed_h',
                                       kdim='ice_mask_x',
                                       height=200)

        delta_w0_m_plot = get_heatmap(data_w0_m,
                                      d_w0_m_lim,
                                      'Delta w0_m',
                                      kdim='ice_mask_x',
                                      height=200)

        parameter_indices = ds.attrs['parameter_indices']
        # plot for height shift spinup if there
        if 'height_shift_spinup' in parameter_indices.keys():
            height_shift_data = []
            for i, unknown_p in enumerate(ds.unknown_parameters.values):
                height_shift_data.append(
                    (i, unknown_p[parameter_indices['height_shift_spinup']]))
            height_shift_spinup_plot = hv.Curve(
                height_shift_data,
                kdims='Iterations',
                vdims='shift (m)',
            ).opts(
                opts.Curve(title='spinup height shift',
                           tools=['hover'],
                           height=200))
        else:
            height_shift_spinup_plot = None

        # get gradients
        data_grad_bed_h = None
        data_grad_area_bed_h = None
        data_grad_w0_m = None
        data_grad_surface_h = None
        data_grad_height_shift_spinup = None

        if 'bed_h' in parameter_indices.keys():
            data_grad_bed_h = []
            grad_bed_h_lim = 0
        if 'area_bed_h' in parameter_indices.keys():
            data_grad_area_bed_h = []
            grad_area_bed_h_lim = 0
        if 'w0_m' in parameter_indices.keys():
            data_grad_w0_m = []
            grad_w0_m_lim = 0
        if 'surface_h' in parameter_indices.keys():
            data_grad_surface_h = []
            grad_surface_h_lim = 0
        if 'height_shift_spinup' in parameter_indices.keys():
            data_grad_height_shift_spinup = []

        for i, grad in enumerate(ds.grads.values):
            x_ice_mask = ds.coords['x'][ds.ice_mask].values
            x_all = ds.coords['x'].values

            # bed_h
            if 'bed_h' in parameter_indices.keys():
                grad_bed_h = grad[parameter_indices['bed_h']]
                grad_bed_h_lim = np.max(
                    [grad_bed_h_lim,
                     np.max(np.abs(grad_bed_h))])
                for el in [(x, i, v) for x, v in zip(x_ice_mask, grad_bed_h)]:
                    data_grad_bed_h.append(el)
            if 'area_bed_h' in parameter_indices.keys():
                grad_area_bed_h = grad[parameter_indices['area_bed_h']]
                grad_area_bed_h_lim = np.max(
                    [grad_area_bed_h_lim,
                     np.max(np.abs(grad_area_bed_h))])
                for el in [(x, i, v)
                           for x, v in zip(x_ice_mask, grad_area_bed_h)]:
                    data_grad_area_bed_h.append(el)
            if 'w0_m' in parameter_indices.keys():
                grad_w0_m = grad[parameter_indices['w0_m']]
                grad_w0_m_lim = np.max(
                    [grad_w0_m_lim, np.max(np.abs(grad_w0_m))])
                for el in [(x, i, v) for x, v in zip(x_ice_mask, grad_w0_m)]:
                    data_grad_w0_m.append(el)
            if 'surface_h' in parameter_indices.keys():
                grad_surface_h = grad[parameter_indices['surface_h']]
                grad_surface_h_lim = np.max(
                    [grad_surface_h_lim,
                     np.max(np.abs(grad_surface_h))])
                for el in [(x, i, v) for x, v in zip(x_all, grad_surface_h)]:
                    data_grad_surface_h.append(el)
            if 'height_shift_spinup' in parameter_indices.keys():
                data_grad_height_shift_spinup.append(
                    (i, grad[parameter_indices['height_shift_spinup']]))

        grad_plots = None
        if 'bed_h' in parameter_indices.keys():
            grad_plots = pn.Column(get_heatmap(data_grad_bed_h,
                                               grad_bed_h_lim,
                                               'Grad bed_h',
                                               kdim='ice_mask_x',
                                               height=200),
                                   sizing_mode='stretch_width')
        elif 'area_bed_h' in parameter_indices.keys():
            grad_plots = pn.Column(get_heatmap(data_grad_area_bed_h,
                                               grad_area_bed_h_lim,
                                               'Grad area_bed_h',
                                               kdim='ice_mask_x',
                                               height=200),
                                   sizing_mode='stretch_width')

        if 'w0_m' in parameter_indices.keys():
            grad_plots.append(
                get_heatmap(data_grad_w0_m,
                            grad_w0_m_lim,
                            'Grad w0_m',
                            kdim='ice_mask_x',
                            height=200))

        if 'surface_h' in parameter_indices.keys():
            grad_plots.append(
                get_heatmap(data_grad_surface_h,
                            grad_surface_h_lim,
                            'Grad surface_h',
                            kdim='total_distance_x',
                            height=200))

        if 'height_shift_spinup' in parameter_indices.keys():
            grad_plots.append(
                hv.Curve(
                    data_grad_height_shift_spinup,
                    kdims='Iterations',
                    vdims='gradient',
                ).opts(
                    opts.Curve(title='spinup height shift gradient',
                               tools=['hover'],
                               height=200)))

        # convert c_terms
        c_terms_conv = {}
        hover_height = 0
        for term in ds.c_terms_description.values:
            # term = ds.c_terms_description.values[0]
            for var in term.keys():
                var_use = var.replace(':', '_')
                var_use = var_use.replace('-', '')
                if type(term[var]) == dict:
                    yr = list(term[var].keys())[0]
                    yr_use = yr.replace('-', '_')
                    if var_use + '_' + yr_use not in c_terms_conv.keys():
                        c_terms_conv[var_use + '_' + yr_use] = []
                    c_terms_conv[var_use + '_' + yr_use].append(term[var][yr])
                    hover_height = np.max(
                        [hover_height, np.max(term[var][yr])])
                else:
                    if var_use not in c_terms_conv.keys():
                        c_terms_conv[var_use] = []
                    c_terms_conv[var_use].append(term[var])
                    hover_height = np.max([hover_height, np.max(term[var])])

        c_term_area = []
        for one_c_term in c_terms_conv.keys():
            c_term_area.append(
                hv.Area(
                    (ds.coords['iteration'].values, c_terms_conv[one_c_term]),
                    kdims='Iterations',
                    vdims='c_terms',
                    label=one_c_term))
        overlay_c_terms = hv.Overlay(c_term_area)
        stack_c_terms = hv.Area.stack(overlay_c_terms)

        df_c_terms = pd.DataFrame(c_terms_conv)
        df_c_terms['Iteration'] = ds.coords['iteration'].values
        df_c_terms['hover_height'] = np.repeat(
            hover_height / 2, len(ds.coords['iteration'].values))

        tooltips_c_terms = [('Iteration', '@{Iteration}')]
        tooltips_c_terms += [(key, '@{' + key + '}{%0.4f}')
                             for key in c_terms_conv.keys()]
        hover_c_terms = HoverTool(tooltips=tooltips_c_terms,
                                  formatters=dict([
                                      ('@{' + key + '}', 'printf')
                                      for key in c_terms_conv.keys()
                                  ]),
                                  mode='vline')
        vdims_curve_c_terms = ['hover_height']
        for key in c_terms_conv.keys():
            vdims_curve_c_terms.append(key)
        curve_c_terms = hv.Curve(df_c_terms,
                                 kdims='Iteration',
                                 vdims=vdims_curve_c_terms).opts(
                                     tools=[hover_c_terms], line_alpha=0)

        c_terms_plot = (stack_c_terms * curve_c_terms).opts(
            width=500, height=200, legend_position='left', title='Cost Terms')

        # calculate differences of surface height at start, rgi and end
        for gdir in gdirs:
            if gdir.rgi_id == rgi_id:
                fl_ref_rgi = gdir.read_pickle(
                    'model_flowlines', filesuffix='_combine_true_init')[0]
                fl_ref_start = gdir.read_pickle('model_flowlines',
                                                filesuffix='_spinup')[0]
                fl_ref_end = gdir.read_pickle(
                    'model_flowlines', filesuffix='_combine_true_end')[0]

        def get_performance_sfc_h_array(fct, data, ref_val):
            return [np.around(fct(val, ref_val), decimals=2) for val in data]

        def get_sfc_h_table(data, ref_val, title):
            df = pd.DataFrame({
                'RMSE':
                get_performance_sfc_h_array(RMSE, data, ref_val),
                'BIAS':
                get_performance_sfc_h_array(BIAS, data, ref_val),
                'DIFF':
                get_performance_sfc_h_array(DIFF, data, ref_val),
                'AERR':
                get_performance_sfc_h_array(AERR, data, ref_val),
            })
            return pn.Column(pn.pane.Markdown('Statistics ' + title),
                             pn.widgets.Tabulator(df,
                                                  titles={'index': 'I'},
                                                  height=200),
                             sizing_mode='stretch_width')

        # sfc_h_end
        d_sfc_h_end_lim = 0.
        data_sfc_h_end = []
        table_data_sfc_h_end = []
        for i, fl in enumerate(ds.flowlines.values):
            x_all = ds.coords['x'].values
            d_sfc_h_end = (fl.surface_h - fl_ref_end.surface_h)
            d_sfc_h_end_lim = np.max(
                [d_sfc_h_end_lim, np.max(np.abs(d_sfc_h_end))])
            table_data_sfc_h_end.append(fl.surface_h)
            for el in [(x, i, v) for x, v in zip(x_all, d_sfc_h_end)]:
                data_sfc_h_end.append(el)
        delta_sfc_h_end_plot = get_heatmap(data_sfc_h_end,
                                           d_sfc_h_end_lim,
                                           'Delta sfc_h_end',
                                           kdim='total_distance_x',
                                           height=150)
        delta_sfc_h_end_table = get_sfc_h_table(table_data_sfc_h_end,
                                                fl_ref_end.surface_h,
                                                'sfc_h_end')

        # sfc_h_rgi
        if 'fl_surface_h:m' in ds.observations_mdl.values[0].keys():
            d_sfc_h_rgi_lim = 0.
            data_sfc_h_rgi = []
            table_data_sfc_h_rgi = []
            for i, obs in enumerate(ds.observations_mdl.values):
                x_all = ds.coords['x'].values
                d_sfc_h_rgi = (list(obs['fl_surface_h:m'].values())[0] -
                               fl_ref_rgi.surface_h)
                d_sfc_h_rgi_lim = np.max(
                    [d_sfc_h_rgi_lim,
                     np.max(np.abs(d_sfc_h_rgi))])
                table_data_sfc_h_rgi.append(
                    list(obs['fl_surface_h:m'].values())[0])
                for el in [(x, i, v) for x, v in zip(x_all, d_sfc_h_rgi)]:
                    data_sfc_h_rgi.append(el)
            delta_sfc_h_rgi_plot = get_heatmap(data_sfc_h_rgi,
                                               d_sfc_h_rgi_lim,
                                               'Delta sfc_h_rgi',
                                               kdim='total_distance_x',
                                               height=150)
            delta_sfc_h_rgi_table = get_sfc_h_table(table_data_sfc_h_rgi,
                                                    fl_ref_rgi.surface_h,
                                                    'sfc_h_rgi')
        else:
            delta_sfc_h_rgi_plot = None
            delta_sfc_h_rgi_table = None

        # sfc_h_start
        d_sfc_h_start_lim = 0.
        data_sfc_h_start = []
        table_data_sfc_h_start = []
        for i, tmp_sfc_h in enumerate(ds.sfc_h_start.values):
            x_all = ds.coords['x'].values
            d_sfc_h_start = (tmp_sfc_h - fl_ref_start.surface_h)
            d_sfc_h_start_lim = np.max(
                [d_sfc_h_start_lim,
                 np.max(np.abs(d_sfc_h_start))])
            table_data_sfc_h_start.append(tmp_sfc_h)
            for el in [(x, i, v) for x, v in zip(x_all, d_sfc_h_start)]:
                data_sfc_h_start.append(el)
        delta_sfc_h_start_plot = get_heatmap(data_sfc_h_start,
                                             d_sfc_h_start_lim,
                                             'Delta sfc_h_start',
                                             kdim='total_distance_x',
                                             height=150)
        delta_sfc_h_start_table = get_sfc_h_table(table_data_sfc_h_start,
                                                  fl_ref_start.surface_h,
                                                  'sfc_h_start')

        # create Table with performance measures (bed_h, w0_m, sfc_h_start, sfc_h_end, sfc_h_rgi,
        # fct_calls, time, device)
        def get_performance_array(fct, attr):
            return [
                np.around(fct(val,
                              getattr(fl_ref, attr)[ds.ice_mask]),
                          decimals=2) for val in [
                              getattr(fl.values.item(), attr)[ds.ice_mask]
                              for fl in ds.flowlines
                          ]
            ]

        def get_performance_table(attr):
            df = pd.DataFrame({
                'RMSE': get_performance_array(RMSE, attr),
                'BIAS': get_performance_array(BIAS, attr),
                'DIFF': get_performance_array(DIFF, attr),
                'AERR': get_performance_array(AERR, attr),
            })
            return pn.Column(pn.pane.Markdown('Statistics ' + attr),
                             pn.widgets.Tabulator(df,
                                                  titles={'index': 'I'},
                                                  height=200),
                             sizing_mode='stretch_width')

        def get_minimise_performance_table():
            df = pd.DataFrame({
                'forward runs':
                ds.fct_calls.values,
                'computing time':
                ds.time_needed.values,
                'device':
                np.repeat(ds.attrs['device'], len(ds.time_needed.values))
            })
            return pn.widgets.Tabulator(df)

        performance_tables = \
            pn.Column(get_performance_table('bed_h'),
                      get_performance_table('_w0_m'),
                      get_minimise_performance_table(),
                      sizing_mode='stretch_width')

        # create plot for exploration of geometry
        # thickness at end time
        data_thick_end = []
        thick_end_lim = 0.
        for i, fl in enumerate(ds.flowlines.values):
            x_all = ds.coords['x'].values
            thick_end = fl.thick
            thick_end_lim = np.max([thick_end_lim, np.max(np.abs(thick_end))])
            for el in [(x, i, v) for x, v in zip(x_all, thick_end)]:
                data_thick_end.append(el)
        thick_end_plot = get_heatmap(data_thick_end,
                                     thick_end_lim,
                                     'Ice thickness at end time',
                                     kdim='total_distance_x',
                                     height=150)
        thick_end_true = fl_ref_end.thick
        thick_end_true_lim = np.max(np.abs(thick_end_true))
        x_all = ds.coords['x'].values
        data_thick_end_true = []
        for el in [(x, 0, v) for x, v in zip(x_all, thick_end_true)]:
            data_thick_end_true.append(el)
        thick_end_true_plot = get_heatmap(data_thick_end_true,
                                          thick_end_true_lim,
                                          'Ice thickness at end time TRUE',
                                          kdim='total_distance_x',
                                          vdim='true',
                                          height=100)

        # surface widths
        def get_width_curve(x, width, label, color, height=150):
            return (hv.Curve(
                (x, width / 2),
                kdims='total_distance_x',
                vdims='widths',
                label=label,
            ) * hv.Curve(
                (x, -width / 2),
                kdims='total_distance_x',
                vdims='widths',
                label=label,
            )).opts(opts.Curve(color=color, tools=['hover'], height=height))

        x_all = ds.coords['x'].values
        surface_widths_rgi_true_plot = get_width_curve(x_all,
                                                       fl_ref_rgi.widths_m,
                                                       'RGI', 'blue')
        surface_widths_start_true_plot = get_width_curve(
            x_all, fl_ref_start.widths_m, 'Start', 'red')
        surface_widths_end_true_plot = get_width_curve(x_all,
                                                       fl_ref_end.widths_m,
                                                       'End', 'gray')
        widths_plot = (surface_widths_start_true_plot *
                       surface_widths_rgi_true_plot *
                       surface_widths_end_true_plot).opts(
                           title='Surface widths',
                           # legend_position='right',
                           show_legend=False)

        # Surface_h with bed_h
        x_all = ds.coords['x'].values

        def get_curve(x, y, label, color, height=200):
            return hv.Curve((x, y),
                            kdims='total_distance_x',
                            vdims='heights',
                            label=label).opts(
                                opts.Curve(color=color,
                                           tools=['hover'],
                                           height=height))

        surface_height_start_true_plot = get_curve(x_all,
                                                   fl_ref_start.surface_h,
                                                   'Start', 'red')
        surface_height_rgi_true_plot = get_curve(x_all, fl_ref_rgi.surface_h,
                                                 'RGI', 'blue')
        surface_height_end_true_plot = get_curve(x_all, fl_ref_end.surface_h,
                                                 'End', 'gray')
        bed_height_true_plot = get_curve(x_all, fl_ref_rgi.bed_h, 'bed_h',
                                         'black')

        surface_height_plot = (bed_height_true_plot *
                               surface_height_start_true_plot *
                               surface_height_rgi_true_plot *
                               surface_height_end_true_plot).opts(
                                   title='Surface heights',
                                   legend_position='bottom',
                                   legend_cols=2)

        return pn.Column('## ' + current_file,
                         pn.Row(
                             pn.Column(
                                 pn.Row(
                                     pn.Column(delta_bed_h_plot,
                                               delta_w0_m_plot,
                                               height_shift_spinup_plot,
                                               sizing_mode='stretch_width'),
                                     grad_plots),
                                 pn.Row(c_terms_plot,
                                        sizing_mode='stretch_width'),
                             ),
                             pn.Column(delta_sfc_h_start_plot,
                                       delta_sfc_h_rgi_plot,
                                       delta_sfc_h_end_plot,
                                       delta_sfc_h_start_table,
                                       delta_sfc_h_rgi_table,
                                       delta_sfc_h_end_table,
                                       sizing_mode='stretch_width'),
                             pn.Column(thick_end_plot,
                                       thick_end_true_plot,
                                       widths_plot,
                                       surface_height_plot,
                                       performance_tables,
                                       sizing_mode='stretch_width'),
                         ),
                         sizing_mode='stretch_width')
Ejemplo n.º 17
0
production = production.redim.unit(Florida="MMcf")
production

# ### Layout
# Create a layout in holoviews is very easy. First, let's try a few other types of plots to visualise the same data.

print(production)

spikes = hv.Spikes(production)
spikes

scatter = hv.Scatter(production)
scatter

area = hv.Area(production)
area

# In holoviews, creating a layout is much easier. You just simply create a layout by adding up the plots!

production + spikes + scatter + area

# To make it look better we can specify the number of columns.

layout = production + spikes + scatter + area
layout.cols(2)

print(layout)

# The object `layout` has all the plots in it. This means we can easily access each plot and even create a new layout. Note the name of the elements in the layout.
Ejemplo n.º 18
0
def make_area(boundsy):
    return hv.Area(
        (xs, np.minimum(ys, boundsy[0]), np.minimum(ys, boundsy[1])),
        vdims=['min', 'max'])
Ejemplo n.º 19
0
def plot_pozos_tipo():


    dims_eur_baja = dict(kdims='mes', vdims=['EUR_baja_L','EUR_baja_H'])
    env_eur_baja = hv.Area(perfil, label='EUR Baja', **dims_eur_baja)
    env_eur_baja.opts(alpha=0.3,
                      color='red',
                      fontscale=1.5)

    linea_eur_baja = hv.Curve(perfil.EUR_baja_M)
    linea_eur_baja.opts(color='red',
                        line_dash='dotted')

    dims_eur_media = dict(kdims='mes', vdims=['EUR_media_L','EUR_media_H'])
    env_eur_media= hv.Area(perfil, label='EUR Media', **dims_eur_media)
    env_eur_media.opts(alpha=0.3,
                       color='blue',
                       fontscale=1.5)

    linea_eur_media = hv.Curve(perfil.EUR_media_M)
    linea_eur_media.opts(color='blue',
                         line_dash='dotted')

    dims_eur_alta = dict(kdims='mes', vdims=['EUR_alta_L','EUR_alta_H'])
    env_eur_alta = hv.Area(perfil, label='EUR Alta', **dims_eur_alta)
    env_eur_alta.opts(alpha=0.3,
                      color='green',
                      fontscale=1.5)

    linea_eur_alta = hv.Curve(perfil.EUR_alta_M)
    linea_eur_alta.opts(color='green',
                        line_dash='dotted')

    elementos_tabla=dict(indice=resumen.index[6:13], valores=resumen[6:13])

    tabla_resumen = hv.Table(elementos_tabla,'indice','valores')
    tabla_resumen.opts(height=500,fontscale=20)

    plot_eur = env_eur_baja * linea_eur_baja * env_eur_media * linea_eur_media * env_eur_alta * linea_eur_alta
    plot_eur.opts(legend_position='top_left')

    elementos_tipos = dict(tipo=pd.unique(tipos.tipo), numero=tipos.tipo.value_counts())
    plot_tipos = hv.Bars(elementos_tipos,'tipo','numero')
    plot_tipos.opts(color='tipo',
                    cmap='Set1',
                    fontscale=1.5)
                    #fill_color=factor_cmap('tipo', palette=Spectral6, factors=elementos_tipos['tipo']))

    layout = plot_eur + plot_tipos  + tabla_resumen

    fig1 = hv.render(layout)

    hv.output(layout, backend='bokeh', fig='html', size=200)
    hv.save(layout, 'resumen_produccion.html')


    dims_baja = dict(kdims='mes', vdims=['baja_L','baja_H'])
    env_baja = hv.Area(perfil, label='Envolvente', **dims_baja)
    env_baja.opts(alpha=0.3,color='red',fontscale=1.5,title='Perfil BAJA Qoi')

    linea_baja = hv.Curve(perfil.baja_M,label='P50')
    linea_baja.opts(color='red',line_dash='dotted')

    dims_media = dict(kdims='mes', vdims=['media_L','media_H'])
    env_media= hv.Area(perfil, label='Envolvente', **dims_media)
    env_media.opts(alpha=0.3,color='blue',fontscale=1.5,title='Perfil MEDIA Qoi')

    linea_media = hv.Curve(perfil.media_M,label='P50')
    linea_media.opts(color='blue',line_dash='dotted')

    dims_alta = dict(kdims='mes', vdims=['alta_L','alta_H'])
    env_alta = hv.Area(perfil, label='Envolvente', **dims_alta)
    env_alta.opts(alpha=0.3,color='green',fontscale=1.5,title='Perfil ALTA Qoi')

    linea_alta = hv.Curve(perfil.alta_M,label='P50')
    linea_alta.opts(color='green',line_dash='dotted')

    plots_perfiles = env_baja * linea_baja + env_media * linea_media + env_alta * linea_alta


    fig2 = hv.render(plots_perfiles)

    hv.output(plots_perfiles, backend='bokeh', fig='html', size=200)

    hv.save(plots_perfiles, 'curvas_tipo.html')

    return
Ejemplo n.º 20
0
opts_curve_1 = {'Curve': dict(color=col_2, width=350, height=400)}
opts_curve_2 = {'Curve': dict(color='red', width=350, height=400)}
opts_point = {'Points': dict(color='black', marker='+', size=11)}


# create rectangle from starting point with width and height
def hv_rectangle(x=0, y=0, width=.05, height=.05):
    return hv.Polygons([
        np.array([(x, y), (x + width, y), (x + width, y + height),
                  (x, y + height)])
    ]).options(opts_poly)


# Create curve and integral
hv_curve = hv.Curve((x, y), 'x', 'f(x)').options(opts_curve_1)
hv_int = hv.Area((x, y), 'x', 'f(x)').options(
    color=col_1)  # create integral polygone from input vector x and y

hv_rectangle_0 = hv_rectangle(x=0, y=0, width=10, height=(fct((0 + 10) / 2)))
int_approx_viz_0 = (hv_rectangle_0 * hv_curve).redim.range(y=(0, 1.08))

# number of steps to approximate the integral with
n_steps = np.arange(3, 31)

# create empty list to store all integral approximations for all n_steps
hv_int_box_approx = []
approx_int = []  # value of approximated integral for all n_steps

# calculate approximation for each step
for j in range(len(n_steps)):
    x_steps = np.linspace(0, 10, num=n_steps[j] + 1)
    hv_int_box_approx_j = [
def get_data(region, prediction_for=1, **kwargs):
    data = input_data[prediction_for -
                      1][input_data[prediction_for -
                                    1].region == region].sort_values("time")
    return hv.Area((data.time, data.trips), 'Time', 'Trips') * hv.Area(
        (data.time, data.predictions), 'Time', 'Pred')
Ejemplo n.º 22
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)
Ejemplo n.º 23
0
 def plot_series(df, i):
     data = {"x": df.index.values, "y": df.iloc[:, i].values}
     return hv.Area(data)  #* hv.Curve(data).opts(tools=["hover"])