コード例 #1
0
ファイル: models.py プロジェクト: timothydmorton/isochrones
    def view_eep_fit(self, mass, feh, plot_fit=True, order=5, p0=None, plot_p0=False):
        import holoviews as hv
        hv.extension('bokeh')
        subdf = self.df.xs((mass, feh), level=('initial_mass', 'initial_feh'))

        ds = hv.Dataset(subdf)
        pts = hv.Points(ds, kdims=['age', 'eep'], vdims=['phase', 'interpolated']).options(tools=['hover'], width=800, height=400, marker='+')
        primary_eeps = self.primary_eeps
        primary_ages = [subdf.loc[e].age for e in primary_eeps if e < subdf.eep.max()]

        from isochrones.eep import eep_fn, eep_jac, eep_fn_p0
        from scipy.optimize import curve_fit
        if p0 is None:
            p0 = eep_fn_p0(subdf.age.values, subdf.eep.values, order=order)

        m = subdf.eep < 808
        if plot_fit:
            pfit, _ = curve_fit(partial(eep_fn, order=order), subdf.age.values[m], subdf.eep.values[m], p0, jac=partial(eep_jac, order=order))
            fit = hv.Points([(a, eep_fn(a, *pfit)) for a in subdf.age])
        if plot_p0:
            p0_fit = hv.Points([(a, eep_fn(a, *p0)) for a in subdf.age])

        olay = pts * hv.Points([(a, e) for a, e in zip(primary_ages, primary_eeps)]).options(size=8)
        if plot_fit:
            olay = olay * fit
        if plot_p0:
            olay = olay * p0_fit
        return olay
コード例 #2
0
__all__ = [
    'KELVIN', 'CrankNicolsonSolver', 'four_minutes_a_side', 'every_15_secs',
    'sear_then_cook_low', 'sous_vide_liquid_nitrogen', 'slow_roast', 'hv',
    'np', 'cook_recipe', 'snapshots_to_protein_state', 'PROTEIN_STATES'
]

import holoviews as hv
hv.extension('bokeh', logo=False)

import numpy as np
from enum import Enum

KELVIN = 273.15


class BC(Enum):
    """A class for holding boundary conditions."""
    HEATING_LEFT = 0
    HEATING_RIGHT = 1
    HEATING_BOTH = 2
    NO_HEATING = 3


def assemble_A_matrix(N, D, dx, dt, boundary_condition):
    sigma = D * dt / (2 * dx**2)
    A = np.zeros((N + 1, N + 1))
    for i in range(N + 1):
        if i == 0:
            # imposed temperature
            if boundary_condition == BC.HEATING_LEFT or boundary_condition == BC.HEATING_BOTH:
                A[i, i] = 1
コード例 #3
0
def init_renderer():
    hv.extension('matplotlib')
    hv.output(backend='matplotlib')
    pyplot.switch_backend(RENDERER_NAME)
コード例 #4
0
ファイル: dash.py プロジェクト: Anhmike/holoviews
from holoviews.streams import Derived, History
from holoviews.plotting.plotly.callbacks import (
    Selection1DCallback, RangeXYCallback, RangeXCallback, RangeYCallback,
    BoundsXYCallback, BoundsXCallback, BoundsYCallback)

# Dash imports
import dash_core_components as dcc
import dash_html_components as html
from dash import callback_context
from dash.dependencies import Output, Input, State

# plotly.py imports
import plotly.graph_objects as go

# Activate plotly as current HoloViews extension
hv.extension("plotly")

# Named tuples definitions
StreamCallback = namedtuple("StreamCallback", ["input_ids", "fn", "output_id"])
DashComponents = namedtuple("DashComponents",
                            ["graphs", "kdims", "store", "resets", "children"])
HoloViewsFunctionSpec = namedtuple("HoloViewsFunctionSpec",
                                   ["fn", "kdims", "streams"])


def get_layout_ranges(plot):
    layout_ranges = {}
    fig_dict = plot.state
    for k in fig_dict['layout']:
        if k.startswith('xaxis') or k.startswith('yaxis'):
            if "range" in fig_dict['layout'][k]:
コード例 #5
0
## preliminaries ##

import holoviews as hv
import hvplot.pandas
import numpy as np
import pandas as pd
import panel as pn
from holoviews.operation.datashader import rasterize, shade
from holoviews.selection import link_selections
from holoviews.util.transform import dim

hv.extension("bokeh", width=100)
pn.extension(comms="vscode")

## create random walks (one location) ##
data_df = pd.DataFrame()
npoints = 15000
np.random.seed(71)
x = np.arange(npoints)
y1 = 1300 + 2.5 * np.random.randn(npoints).cumsum()
y2 = 1500 + 2 * np.random.randn(npoints).cumsum()
y3 = 3 + np.random.randn(npoints).cumsum()
data_df.loc[:, "x"] = x
data_df.loc[:, "rand1"] = y1
data_df.loc[:, "rand2"] = y2
data_df.loc[:, "rand3"] = y3

colors = hv.Cycle("Category10").values
dims = ["rand1", "rand2", "rand3"]
items = []
for c, dim in zip(colors, dims):
コード例 #6
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
コード例 #7
0
ファイル: plot.py プロジェクト: sunny1205124/umap-1
def interactive(
    umap_object,
    labels=None,
    values=None,
    hover_data=None,
    theme=None,
    cmap="Blues",
    color_key=None,
    color_key_cmap="Spectral",
    background="white",
    width=800,
    height=800,
    point_size=None,
    subset_points=None,
):
    """Create an interactive bokeh plot of a UMAP embedding.
    While static plots are useful, sometimes a plot that
    supports interactive zooming, and hover tooltips for
    individual points is much more desireable. This function
    provides a simple interface for creating such plots. The
    result is a bokeh plot that will be displayed in a notebook.

    Note that more complex tooltips etc. will require custom
    code -- this is merely meant to provide fast and easy
    access to interactive plotting.

    Parameters
    ----------
    umap_object: trained UMAP object
        A trained UMAP object that has a 2D embedding.

    labels: array, shape (n_samples,) (optional, default None)
        An array of labels (assumed integer or categorical),
        one for each data sample.
        This will be used for coloring the points in
        the plot according to their label. Note that
        this option is mutually exclusive to the ``values``
        option.

    values: array, shape (n_samples,) (optional, default None)
        An array of values (assumed float or continuous),
        one for each sample.
        This will be used for coloring the points in
        the plot according to a colorscale associated
        to the total range of values. Note that this
        option is mutually exclusive to the ``labels``
        option.

    hover_data: DataFrame, shape (n_samples, n_tooltip_features)
    (optional, default None)
        A dataframe of tooltip data. Each column of the dataframe
        should be a Series of length ``n_samples`` providing a value
        for each data point. Column names will be used for
        identifying information within the tooltip.

    theme: string (optional, default None)
        A color theme to use for plotting. A small set of
        predefined themes are provided which have relatively
        good aesthetics. Available themes are:
           * 'blue'
           * 'red'
           * 'green'
           * 'inferno'
           * 'fire'
           * 'viridis'
           * 'darkblue'
           * 'darkred'
           * 'darkgreen'

    cmap: string (optional, default 'Blues')
        The name of a matplotlib colormap to use for coloring
        or shading points. If no labels or values are passed
        this will be used for shading points according to
        density (largely only of relevance for very large
        datasets). If values are passed this will be used for
        shading according the value. Note that if theme
        is passed then this value will be overridden by the
        corresponding option of the theme.

    color_key: dict or array, shape (n_categories) (optional, default None)
        A way to assign colors to categoricals. This can either be
        an explicit dict mapping labels to colors (as strings of form
        '#RRGGBB'), or an array like object providing one color for
        each distinct category being provided in ``labels``. Either
        way this mapping will be used to color points according to
        the label. Note that if theme
        is passed then this value will be overridden by the
        corresponding option of the theme.

    color_key_cmap: string (optional, default 'Spectral')
        The name of a matplotlib colormap to use for categorical coloring.
        If an explicit ``color_key`` is not given a color mapping for
        categories can be generated from the label list and selecting
        a matching list of colors from the given colormap. Note
        that if theme
        is passed then this value will be overridden by the
        corresponding option of the theme.

    background: string (optional, default 'white')
        The color of the background. Usually this will be either
        'white' or 'black', but any color name will work. Ideally
        one wants to match this appropriately to the colors being
        used for points etc. This is one of the things that themes
        handle for you. Note that if theme
        is passed then this value will be overridden by the
        corresponding option of the theme.

    width: int (optional, default 800)
        The desired width of the plot in pixels.

    height: int (optional, default 800)
        The desired height of the plot in pixels

    point_size: int (optional, default None)
        The size of each point marker

    subset_points: array, shape (n_samples,) (optional, default None)
        A way to select a subset of points based on an array of boolean
        values.

    Returns
    -------

    """
    if theme is not None:
        cmap = _themes[theme]["cmap"]
        color_key_cmap = _themes[theme]["color_key_cmap"]
        background = _themes[theme]["background"]

    if labels is not None and values is not None:
        raise ValueError(
            "Conflicting options; only one of labels or values should be set")

    points = umap_object.embedding_
    if subset_points is not None:
        if len(subset_points) != points.shape[0]:
            raise ValueError(
                "Size of subset points ({}) does not match number of input points ({})"
                .format(len(subset_points), points.shape[0]))
        points = points[subset_points]

    if points.shape[1] != 2:
        raise ValueError(
            "Plotting is currently only implemented for 2D embeddings")

    if point_size is None:
        point_size = 100.0 / np.sqrt(points.shape[0])

    data = pd.DataFrame(umap_object.embedding_, columns=("x", "y"))

    if labels is not None:
        data["label"] = labels

        if color_key is None:
            unique_labels = np.unique(labels)
            num_labels = unique_labels.shape[0]
            color_key = _to_hex(
                plt.get_cmap(color_key_cmap)(np.linspace(0, 1, num_labels)))

        if isinstance(color_key, dict):
            data["color"] = pd.Series(labels).map(color_key)
        else:
            unique_labels = np.unique(labels)
            if len(color_key) < unique_labels.shape[0]:
                raise ValueError(
                    "Color key must have enough colors for the number of labels"
                )

            new_color_key = {
                k: color_key[i]
                for i, k in enumerate(unique_labels)
            }
            data["color"] = pd.Series(labels).map(new_color_key)

        colors = "color"

    elif values is not None:
        data["value"] = values
        palette = _to_hex(plt.get_cmap(cmap)(np.linspace(0, 1, 256)))
        colors = btr.linear_cmap("value",
                                 palette,
                                 low=np.min(values),
                                 high=np.max(values))

    else:
        colors = matplotlib.colors.rgb2hex(plt.get_cmap(cmap)(0.5))

    if subset_points is not None:
        data = data[subset_points]
        if hover_data is not None:
            hover_data = hover_data[subset_points]

    if points.shape[0] <= width * height // 10:

        if hover_data is not None:
            tooltip_dict = {}
            for col_name in hover_data:
                data[col_name] = hover_data[col_name]
                tooltip_dict[col_name] = "@" + col_name
            tooltips = list(tooltip_dict.items())
        else:
            tooltips = None

        # bpl.output_notebook(hide_banner=True) # this doesn't work for non-notebook use
        data_source = bpl.ColumnDataSource(data)

        plot = bpl.figure(
            width=width,
            height=height,
            tooltips=tooltips,
            background_fill_color=background,
        )
        plot.circle(x="x",
                    y="y",
                    source=data_source,
                    color=colors,
                    size=point_size)

        plot.grid.visible = False
        plot.axis.visible = False

        # bpl.show(plot)
    else:
        if hover_data is not None:
            warn("Too many points for hover data -- tooltips will not"
                 "be displayed. Sorry; try subssampling your data.")
        hv.extension("bokeh")
        hv.output(size=300)
        hv.opts(
            'RGB [bgcolor="{}", xaxis=None, yaxis=None]'.format(background))
        if labels is not None:
            point_plot = hv.Points(data, kdims=["x", "y"])
            plot = hd.datashade(
                point_plot,
                aggregator=ds.count_cat("color"),
                color_key=color_key,
                cmap=plt.get_cmap(cmap),
                width=width,
                height=height,
            )
        elif values is not None:
            min_val = data.values.min()
            val_range = data.values.max() - min_val
            data["val_cat"] = pd.Categorical(
                (data.values - min_val) // (val_range // 256))
            point_plot = hv.Points(data, kdims=["x", "y"], vdims=["val_cat"])
            plot = hd.datashade(
                point_plot,
                aggregator=ds.count_cat("val_cat"),
                cmap=plt.get_cmap(cmap),
                width=width,
                height=height,
            )
        else:
            point_plot = hv.Points(data, kdims=["x", "y"])
            plot = hd.datashade(
                point_plot,
                aggregator=ds.count(),
                cmap=plt.get_cmap(cmap),
                width=width,
                height=height,
            )

    return plot
コード例 #8
0
def sankey_plot_main():

    config_params(font_size=4)

    hv.extension('matplotlib')
    hv.output(fig='svg')

    forbidden = ['RADIATION', 'Miscellanious', 'Unknown', 'TopoII', 'TOPOII']
    out, dic_t = create_matrix_treatments_plot()
    order_ttypes = [
        'Breast',
        'Colon-Rectum',
        'Prostate',
        'Lung',
        'Skin',
        'Bone-Soft-tissue',
        'Ovary',
        'Esophagus',
        'Urinary-tract',
        'NET',
        'Kidney',
        'Nervous-system',
        'Biliary',
        'Pancreas',
        'Unknown',
        'Uterus',
        'Head-and-neck',
        'Liver',
        'Stomach',
        'Mesothelioma',
    ]

    all_rows = []
    for ttype in order_ttypes:
        samples = dic_t[ttype]
        subs = out.loc[samples]
        for col in subs:
            if col not in forbidden:
                all_rows.append((ttype, col, int(subs[col].sum())))

    matrix_df = pd.DataFrame(all_rows)
    matrix_df.columns = ['target', 'source', 'value']
    matrix_df = matrix_df[(matrix_df['target'] != 'Unknown')]
    matrix_df = matrix_df.fillna(0)
    matrix_df['value'] = matrix_df['value'].astype(int)

    good_source = set()
    for source, data in matrix_df.groupby(by='source'):
        tot = data['value'].sum()
        if tot > 30:
            if source != 'Unknown':
                good_source.add(source)
    matrix_df = matrix_df[matrix_df['source'].isin(good_source)]
    out = hv.Sankey(matrix_df.sort_values(
        by='source',
        ascending=True,
    ),
                    label='').opts(label_position='left',
                                   edge_color='target',
                                   node_color='index',
                                   cmap='Set1')  # color=total_colors)

    fig = hv.render(out)
    fig.set_figwidth(10)
    fig.savefig('figures/2A.svg')
    fig.savefig('figures/2A.png', dpi=600)
コード例 #9
0
ファイル: plots.py プロジェクト: rynge/straxen
def show_time_range(st, run_id, t0, dt=10):
    from functools import partial

    import numpy as np
    import pandas as pd

    import holoviews as hv
    from holoviews.operation.datashader import datashade, dynspread
    hv.extension('bokeh')

    import strax

    import gc
    # Somebody thought it was a good idea to call gc.collect explicitly somewhere in holoviews
    # This makes dynamic PMT maps super slow
    # Until I trace the offender:
    gc.collect = lambda *args, **kwargs: None

    # Custom wheel zoom tool that only zooms in time
    from bokeh.models import WheelZoomTool
    time_zoom = WheelZoomTool(dimensions='width')

    # Get ADC->pe multiplicative conversion factor
    from pax.configuration import load_configuration
    from pax.dsputils import adc_to_pe
    pax_config = load_configuration('XENON1T')["DEFAULT"]
    to_pe = np.array(
        [adc_to_pe(pax_config, ch) for ch in range(pax_config['n_channels'])])

    tpc_r = pax_config['tpc_radius']

    # Get locations of PMTs
    r = []
    for q in pax_config['pmts']:
        r.append(
            dict(x=q['position']['x'],
                 y=q['position']['y'],
                 i=q['pmt_position'],
                 array=q.get('array', 'other')))
    f = 1.08
    pmt_locs = pd.DataFrame(r)

    records = st.get_array(run_id,
                           'raw_records',
                           time_range=(t0, t0 + int(1e10)))

    # TOOD: don't reprocess, just load...
    hits = strax.find_hits(records)
    peaks = strax.find_peaks(hits,
                             to_pe,
                             gap_threshold=300,
                             min_hits=3,
                             result_dtype=strax.peak_dtype(n_channels=260))
    strax.sum_waveform(peaks, records, to_pe)
    # Integral in pe
    areas = records['data'].sum(axis=1) * to_pe[records['channel']]

    def normalize_time(t):
        return (t - records[0]['time']) / 1e9

    # Create dataframe with record metadata
    df = pd.DataFrame(
        dict(area=areas,
             time=normalize_time(records['time']),
             channel=records['channel']))

    # Convert to holoviews Points
    points = hv.Points(
        df,
        kdims=[
            hv.Dimension('time', label='Time', unit='sec'),
            hv.Dimension('channel', label='PMT number', range=(0, 260))
        ],
        vdims=[
            hv.Dimension(
                'area',
                label='Area',
                unit='pe',
                # range=(0, 1000)
            )
        ])

    def pmt_map(t_0, t_1, array='top', **kwargs):
        # Compute the PMT pattern (fast)
        ps = points[(t_0 <= points['time']) & (points['time'] < t_1)]
        areas = np.bincount(ps['channel'],
                            weights=ps['area'],
                            minlength=len(pmt_locs))

        # Which PMTs should we include?
        pmt_mask = pmt_locs['array'] == array
        d = pmt_locs[pmt_mask].copy()
        d['area'] = areas[pmt_mask]

        # Convert to holoviews points
        d = hv.Dataset(d,
                       kdims=[
                           hv.Dimension('x',
                                        unit='cm',
                                        range=(-tpc_r * f, tpc_r * f)),
                           hv.Dimension('y',
                                        unit='cm',
                                        range=(-tpc_r * f, tpc_r * f)),
                           hv.Dimension('i', label='PMT number'),
                           hv.Dimension('area', label='Area', unit='PE')
                       ])

        return d.to(hv.Points,
                    vdims=['area', 'i'],
                    group='PMTPattern',
                    label=array.capitalize(),
                    **kwargs).opts(plot=dict(color_index=2,
                                             tools=['hover'],
                                             show_grid=False),
                                   style=dict(size=17, cmap='magma'))

    def pmt_map_range(x_range, array='top', **kwargs):
        # For use in dynamicmap with streams
        if x_range is None:
            x_range = (0, 0)
        return pmt_map(x_range[0], x_range[1], array=array, **kwargs)

    xrange_stream = hv.streams.RangeX(source=points)

    # TODO: weigh by area

    def channel_map():
        return dynspread(
            datashade(
                points, y_range=(0, 260),
                streams=[xrange_stream])).opts(plot=dict(
                    width=600,
                    tools=[time_zoom, 'xpan'],
                    default_tools=['save', 'pan', 'box_zoom', 'save', 'reset'],
                    show_grid=False))

    def plot_peak(p):
        # It's better to plot amplitude /time than per bin, since
        # sampling times are now variable
        y = p['data'][:p['length']] / p['dt']
        t_edges = np.arange(p['length'] + 1, dtype=np.int64)
        t_edges = t_edges * p['dt'] + p['time']
        t_edges = normalize_time(t_edges)

        # Correct step plotting from Knut
        t_ = np.zeros(2 * len(y))
        y_ = np.zeros(2 * len(y))
        t_[0::2] = t_edges[0:-1]
        t_[1::2] = t_edges[1::]
        y_[0::2] = y
        y_[1::2] = y

        c = hv.Curve(dict(time=t_, amplitude=y_),
                     kdims=points.kdims[0],
                     vdims=hv.Dimension('amplitude',
                                        label='Amplitude',
                                        unit='PE/ns'),
                     group='PeakSumWaveform')
        return c.opts(
            plot=dict(  # interpolation='steps-mid',
                # default_tools=['save', 'pan', 'box_zoom', 'save', 'reset'],
                # tools=[time_zoom, 'xpan'],
                width=600,
                shared_axes=False,
                show_grid=True),
            style=dict(color='b')
            # norm=dict(framewise=True)
        )

    def peaks_in(t_0, t_1):
        return peaks[(normalize_time(peaks['time'] +
                                     peaks['length'] * peaks['dt']) > t_0)
                     & (normalize_time(peaks['time']) < t_1)]

    def plot_peaks(t_0, t_1, n_max=10):
        # Find peaks in this range
        ps = peaks_in(t_0, t_1)
        # Show only the largest n_max peaks
        if len(ps) > n_max:
            areas = ps['area']
            max_area = np.sort(areas)[-n_max]
            ps = ps[areas >= max_area]

        return hv.Overlay(items=[plot_peak(p) for p in ps])

    def plot_peak_range(x_range, **kwargs):
        # For use in dynamicmap with streams
        if x_range is None:
            x_range = (0, 10)
        return plot_peaks(x_range[0], x_range[1], **kwargs)

    top_map = hv.DynamicMap(partial(pmt_map_range, array='top'),
                            streams=[xrange_stream])
    bot_map = hv.DynamicMap(partial(pmt_map_range, array='bottom'),
                            streams=[xrange_stream])
    waveform = hv.DynamicMap(plot_peak_range, streams=[xrange_stream])
    layout = waveform + top_map + channel_map() + bot_map
    return layout.cols(2)
コード例 #10
0
ファイル: GeoViews.py プロジェクト: folmerkrikken/knmi-python
# Geoviews needs to be loaded as well (quite heavy to load if not used)
import geoviews as gv

# Features are often used, shortcut is handy
import geoviews.feature as gf

# The packages is based on Xarray, so this is kinda handy
import xarray as xr

# Cartopy is used to make the maps
from cartopy import crs


# There is a tab-completion issue, but that can be fixed
hv.extension(case_sensitive_completion=True)
# Holoviews uses an ~/.holoviews.rc file at start-up, this line can be placed there


# In[2]:


#%% Loading some data (Multi-File data loading is used here as an example)
ds_psl = xr.open_mfdataset('/nobackup_3/users/stoop/7A_weather_data/psl_d_ECEarth_2C_s16r09_2064_world.nc')
ds_rsds = xr.open_mfdataset('/nobackup_3/users/stoop/7A_weather_data/rsds_d_ECEarth_2C_s16r09_2064_world.nc')
ds_wind = xr.open_mfdataset('/nobackup_3/users/stoop/7A_weather_data/sfcwind_d_ECEarth_2C_s16r09_2064_world.nc')
ds_temp = xr.open_mfdataset('/nobackup_3/users/stoop/7A_weather_data/tas_d_ECEarth_2C_s16r09_2064_world.nc')


# In[3]:
コード例 #11
0
# -*- coding: utf-8 -*-
# !/usr/bin/env python

# Importing libraries
import holoviews as hv
from holoviews import opts, dim
import pandas as pd
import numpy as np

hv.extension("matplotlib")
hv.output(fig='pdf', size=250)


def chord_diagram(df_flow_MN, n_cycles, dir_path):
    '''
    Function to plot chord diagram for flows across industry sectors
    '''
    df_flow_MN = df_flow_MN.loc[df_flow_MN['Option'] == 'Industrial']
    df_flow_MN = df_flow_MN[['Cycle', 'Generator Industry Sector',
                             'Flow transferred', 'RETDF Industry Sector',
                             'Recycled flow', 'Industry sector']]

    Flows = {'waste': {'Generator Industry Sector': 'source',
                       'RETDF Industry Sector': 'target',
                       'Flow transferred': 'value'},
             'recyled': {'RETDF Industry Sector': 'source',
                         'Industry sector': 'target',
                         'Recycled flow': 'value'}}

    df_links = pd.DataFrame()
    for Flow, Link in Flows.items():
コード例 #12
0
This example uses the *Iris* dataset.
"""
from typing import Any, Dict, Tuple

import holoviews as hv
import panel as pn
import param
import plotly.io as pio
from holoviews import opts
from panel.template import FastGridTemplate
from plotly.data import iris

from application.config import site

hv.extension("bokeh", "plotly")

APPLICATION = site.create_application(
    url="holoviews-linked-brushing",
    name="HoloViews Linked Brushing",
    author="Marc Skov Madsen",
    introduction=
    "A demonstration of HoloViews linked brushing for Bokeh and Plotly backends",
    description=__doc__,
    thumbnail_url="holoviews-linked-brushing.png",
    code_url="holoviews_linked_brushing.py",
    mp4_url="holoviews-linked-brushing.mp4",
    tags=[
        "Panel", "Bokeh", "Plotly", "HoloViews", "Linked Brushing",
        "Cross Filter"
    ],
コード例 #13
0
def chord_diagram(ds, agg='mean', minimum_quantile=0,
                  groups=None, size=200, pallette='Category20',
                  fig_inches=4):
    """
    Build a chord diagram on the base of holoviews [1].

    It visualizes allocated peer-to-peer flows for all buses given in
    the data. As for compatibility with ipython shell the rendering of
    the image is passed to matplotlib however to the disfavour of
    interactivity. Note that the plot becomes only meaningful for networks
    with N > 5, because of sparse flows otherwise.


    [1] http://holoviews.org/reference/elements/bokeh/Chord.html

    Parameters
    ----------
    allocation : xarray.Dataset
        Dataset with 'peer_to_peer' variable.
    lower_bound : int, default is 0
        filter small power flows by a lower bound
    groups : pd.Series, default is None
        Specify the groups of your buses, which are then used for coloring.
        The series must contain values for all allocated buses.
    size : int, default is 300
        Set the size of the holoview figure
    save_path : str, default is '/tmp/chord_diagram_pypsa'
        set the saving path of your figure

    """
    from holoviews.plotting.mpl import Layout, LayoutPlot
    import holoviews as hv
    hv.extension('matplotlib')

    allocation = filter_null(
        as_dense(
            ds.peer_to_peer.mean('snapshot')),
        'source') .to_series().dropna()
    if groups is not None:
        allocation = allocation.rename(groups).sum(level=['sink', 'source'])
    allocated_buses = allocation.index.levels[0] \
        .append(allocation.index.levels[1]).unique()
    bus_map = pd.Series(range(len(allocated_buses)), index=allocated_buses)

    links = allocation.to_frame('value').reset_index() .replace(
        {
            'source': bus_map,
            'sink': bus_map}) .sort_values('source').reset_index(
        drop=True)[
                lambda df: df.value >= df.value.quantile(minimum_quantile)]

    nodes = pd.DataFrame({'bus': bus_map.index})
    cindex = 'index'
    ecindex = 'source'

    nodes = hv.Dataset(nodes, 'index')
    diagram = hv.Chord((links, nodes))
    diagram = diagram.opts(style={'cmap': pallette,
                                  'edge_cmap': pallette,
                                  'tight': True},
                           plot={'label_index': 'bus',
                                 'color_index': cindex,
                                 'edge_color_index': ecindex})
    # fig = hv.render(diagram, size=size, dpi=300)
    fig = LayoutPlot(Layout([diagram]), dpi=300, fig_size=size,
                     fig_inches=fig_inches,
                     tight=True, tight_padding=0,
                     fig_bounds=(-.15, -.15, 1.15, 1.15),
                     hspace=0, vspace=0, fontsize=15)\
        .initialize_plot()
    return fig, fig.axes
コード例 #14
0
import dask.dataframe as dd
import holoviews as hv
import geoviews as gv

from bokeh.models import Slider, Button
from bokeh.layouts import layout
from bokeh.io import curdoc
from bokeh.models import WMTSTileSource

from holoviews.operation.datashader import datashade, aggregate, shade
from holoviews.plotting.util import fire
shade.cmap = fire

hv.extension('bokeh')
renderer = hv.renderer('bokeh').instance(mode='server')

# Load data
usecols = ['tpep_pickup_datetime', 'dropoff_x', 'dropoff_y']
ddf = dd.read_csv('../data/nyc_taxi.csv', parse_dates=['tpep_pickup_datetime'], usecols=usecols)
ddf['hour'] = ddf.tpep_pickup_datetime.dt.hour
ddf = ddf.persist()

from bokeh.models import WMTSTileSource
url = 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{Z}/{Y}/{X}.jpg'
wmts = gv.WMTS(WMTSTileSource(url=url))

stream = hv.streams.Stream.define('HourSelect', hour=0)()
points = hv.Points(ddf, kdims=['dropoff_x', 'dropoff_y'])
dmap = hv.util.Dynamic(points, operation=lambda obj, hour: obj.select(hour=hour),
                       streams=[stream])
コード例 #15
0
        def wrapped_f(context: strax.Context, run_id: str, **kwargs):
            # Validate arguments
            known_kwargs = (
                'time_range seconds_range time_within time_selection '
                'ignore_time_warning '
                'selection_str t_reference to_pe config').split()
            for k in kwargs:
                if k not in known_kwargs and k not in parameters:
                    # Python itself also raises TypeError for invalid kwargs
                    raise TypeError(f"Unknown argument {k} for {f.__name__}")

            if 'config' in kwargs:
                context = context.new_context(config=kwargs['config'])

            # Say magic words to enable holoviews
            if hv_bokeh:
                global _hv_bokeh_initialized
                if not _hv_bokeh_initialized:
                    import holoviews
                    holoviews.extension('bokeh')
                    _hv_bokeh_initialized = True

            # TODO: This is a placeholder until the corrections system
            # is more fully developed
            if 'to_pe' in parameters and 'to_pe' not in kwargs:
                kwargs['to_pe'] = straxen.get_to_pe(
                    run_id, 'https://raw.githubusercontent.com/XENONnT/'
                    'strax_auxiliary_files/master/to_pe.npy')

            # Prepare selection arguments
            kwargs['time_range'] = context.to_absolute_time_range(
                run_id,
                targets=requires,
                **{
                    k: kwargs.get(k)
                    for k in ('time_range seconds_range time_within'.split())
                })
            kwargs.setdefault('time_selection', default_time_selection)
            kwargs.setdefault('selection_str', None)

            kwargs['t_reference'] = context.estimate_run_start(
                run_id, requires)

            if warn_beyond_sec is not None and not kwargs.get(
                    'ignore_time_warning'):
                tr = kwargs['time_range']
                if tr is None:
                    sec_requested = float('inf')
                else:
                    sec_requested = (tr[1] - tr[0]) / int(1e9)
                if sec_requested > warn_beyond_sec:
                    tr_str = "the entire run" if tr is None else f"{sec_requested} seconds"
                    raise ValueError(
                        f"The author of this mini analysis recommends "
                        f"not requesting more than {warn_beyond_sec} seconds. "
                        f"You are requesting {tr_str}. If you wish to proceed, "
                        "pass ignore_time_warning = True.")

            # Load required data, if any
            if len(requires):
                deps_by_kind = strax.group_by_kind(requires,
                                                   context=context,
                                                   require_time=False)
                for dkind, dtypes in deps_by_kind.items():
                    if dkind in kwargs:
                        # Already have data, just apply cuts
                        kwargs[dkind] = context.apply_selection(
                            kwargs[dkind],
                            selection_str=kwargs['selection_str'],
                            time_range=kwargs['time_range'],
                            time_selection=kwargs['time_selection'])
                    else:
                        kwargs[dkind] = context.get_array(
                            run_id,
                            dtypes,
                            selection_str=kwargs['selection_str'],
                            time_range=kwargs['time_range'],
                            time_selection=kwargs['time_selection'],
                            # Arguments for new context, if needed
                            config=kwargs.get('config'),
                            register=kwargs.get('register'),
                            storage=kwargs.get('storage', tuple()))

                # If user did not give time kwargs, but the function expects
                # a time_range, try to add one based on the time range of the data
                base_dkind = list(deps_by_kind.keys())[0]
                x = kwargs[base_dkind]
                if len(x) and kwargs.get('time_range') is None:
                    x0 = x.iloc[0] if isinstance(x, pd.DataFrame) else x[0]
                    try:
                        kwargs.setdefault('time_range',
                                          (x0['time'], strax.endtime(x).max()))

                    except AttributeError:
                        # If x is a holoviews dataset, this will fail.
                        pass

            if 'seconds_range' in parameters:
                if kwargs.get('time_range') is None:
                    scr = None
                else:
                    scr = tuple([(t - kwargs['t_reference']) / int(1e9)
                                 for t in kwargs['time_range']])
                kwargs.setdefault('seconds_range', scr)

            kwargs.setdefault('run_id', run_id)
            kwargs.setdefault('context', context)

            if 'kwargs' in parameters:
                # Likely this will be passed to another mini-analysis
                to_pass = kwargs
                # Do not pass time_range and seconds_range both (unless explicitly requested)
                # strax does not like that
                if 'seconds_range' in to_pass and not 'seconds_range' in parameters:
                    del to_pass['seconds_range']
                if 'time_within' in to_pass and not 'time_within' in parameters:
                    del to_pass['time_within']
            else:
                # Pass only arguments the function wants
                to_pass = {k: v for k, v in kwargs.items() if k in parameters}
            return f(**to_pass)
コード例 #16
0
ファイル: storm_typing.py プロジェクト: hydrologie/sefm
    def plot_stations(self):
        """

        Returns
        -------

        """
        hv.extension('bokeh')

        opts.defaults(opts.Overlay(active_tools=['wheel_zoom', 'pan']))

        return (gf.ocean*gf.land*gf.ocean* gf.borders) *\
            self.metadata.hvplot.points('longitude', 'latitude',
                                        hover_cols=['id','name'],
                                        grid=True, width=600, height=400,
                                        project=True,  alpha=0.8,
                                        xlim=(-81, -74), ylim=(44, 49.2), tiles='EsriUSATopo')


# def get_metadata_stations(bucket,
#                           latlngbox,
#                           storage_options):
#     df = dd.read_csv(bucket, storage_options=storage_options)
#     return df[df['longitude'].between(latlngbox[0], latlngbox[1]) & df['latitude'].between(latlngbox[2],
#                                                                                            latlngbox[3])].compute()
#
#
# def get_metadata_stations_all(bucket,
#                               storage_options):
#     return dd.read_csv(bucket, storage_options=storage_options)
#
#
# def get_data_stations(bucket,
#                       metadata_stations,
#                       element,
#                       storage_options):
#     df = dd.read_csv(bucket, storage_options=storage_options)
#     return df.loc[df.id.isin(metadata_stations.id) & df.element.isin(element)].compute()
#
#
# def crop_zone(df, latlngbox):
#     gdf = gpd.GeoDataFrame(
#         df, geometry=gpd.points_from_xy(x=df['Longitude (Decimal Degrees)'], y=df['Latitude (Decimal Degrees)'])
#     )
#     return gdf[gdf['Longitude (Decimal Degrees)'].between(latlngbox[0], latlngbox[1])
#            & gdf['Latitude (Decimal Degrees)'].between(latlngbox[2], latlngbox[3])]
#
#
# def visualisation_stations(gdf, df_stations, latlngbox):
#     chro = px.choropleth(gdf.reset_index().drop(columns=['geometry']),
#                         geojson=json.loads(gdf.to_json()), locations='index',
#                         hover_name="NOM",
#                         title='Bassin Outaouais',
#                         )
#
#     sc = go.Scattergeo(
#             lon = df_stations['longitude'],
#             lat = df_stations['latitude'],
#             text = df_stations['name'],
#             mode = 'markers',
#             marker_color ='red',
#             name = 'Stations météo'
#             )
#
#     fig = go.Figure(data=[sc, chro.data[0]])
#
#
#     fig.update_layout(
#             title = 'Station météorologiques contenant des données de précipitations (PRCP)',
#             geo = dict(
#                 showland = True,
#                 showsubunits = True,
#                 showcountries = True,
#                 resolution = 50,
#                 showlakes = True,
#                 lonaxis = dict(
#                     showgrid = True,
#                     gridwidth = 0.5,
#                     range= [ latlngbox[0] - 1, latlngbox[1] + 1],
#                     dtick = 5
#                 ),
#                 lataxis = dict (
#                     showgrid = True,
#                     gridwidth = 0.5,
#                     range= [ latlngbox[2] - 1, latlngbox[3] + 1],
#                     dtick = 5
#                 )
#             )
#     )
#     return fig.show()
コード例 #17
0
import numpy as np
import pandas as pd
import cartopy.crs as ccrs
from cartopy import crs
import geoviews as gv
import holoviews as hv
import geoviews.tile_sources as gts
import xarray as xr
import panel as pn
import os, sys, shutil, time, datetime
from collections import defaultdict

pn.extension()
gv.extension("bokeh", logo=False)
hv.extension("bokeh", logo=False)
hv.Dimension.type_formatters[np.datetime64] = '%Y-%m-%d'
gv.Dimension.type_formatters[np.datetime64] = '%Y-%m-%d'


#### Header Object #####
def make_header_html(header_html_path=None):
    logo = "https://www.kyushu-u.ac.jp/img/common_en/logo.png"
    title = '<p style="font-size:30px">Passive Seismic Monitoring System</p>'
    #     creator = 'Created by: Fernando Lawrens Hutapea, Takeshi Tsuji, and Tatsunori Ikeda <br>'
    info = "(Ambient noise data is provided by National Research Institute for Earth Science and Disaster Prevention (NIED) Japan)"
    creator = 'Presently the monitoring result is updated weekly (Saturday) <br> Please use Firefox or Google Chrome or Microsoft Edge <br>'
    current = np.datetime64(datetime.datetime.now(), "s").astype(str)
    last_update = "Last update: {} <br>".format(current)  # today's date
    header = pn.Row(
        logo, pn.Spacer(width=30),
        pn.Column(pn.Spacer(height=0), pn.Pane(title, height=16, width=900),
コード例 #18
0
def plot_frames(data, backend='matplotlib', mode='mosaic', rows=1, vmax=None,
                vmin=None, circle=None, circle_alpha=0.8, circle_color='white',
                circle_linestyle='-', circle_radius=6, circle_label=False, circle_label_color='white',
                arrow=None, arrow_alpha=0.8, arrow_length=10, arrow_shiftx=5, 
                arrow_label=None, label=None, label_pad=5, label_size=12, 
                label_color='white',grid=False, grid_alpha=0.4,  grid_color='#f7f7f7', 
                grid_spacing=None, cross=None, cross_alpha=0.4, lab_fontsize=8,
                cross_color='white', ang_scale=False, ang_ticksep=50, ndec=1, 
                pxscale=0.01, auscale=1., ang_legend=False, au_legend=False, 
                axis=True, show_center=False, cmap=None, log=False, 
                colorbar=True, colorbar_ticks=None, dpi=100, size_factor=6, 
                horsp=0.4, versp=0.2, width=400, height=400, title=None, 
                tit_size=16, sampling=1, save=None, transparent=False):
    """ Plot a 2d array or a tuple of 2d arrays. Supports the ``matplotlib`` and
    ``bokeh`` backends. When having a tuple of 2d arrays, the plot turns into a
    mosaic. For ``matplotlib``, instead of a mosaic of images, we can create a
    mosaic of surface plots. Also, when using the ``matplotlib`` backend, this
    function allows to annotate and customize the plot and produce publication
    quality figures.

    Parameters
    ----------
    data : numpy.ndarray or tuple
        A single 2d array or a tuple of 2d arrays.
    backend : {'matplotlib', 'bokeh'}, str optional
        Selects the backend used to display the plots. ``Matplotlib`` plots
        are static and allow customization (leading to publication quality
        figures). ``Bokeh`` plots are interactive, allowing the used to zoom,
        pan, inspect pixel values, etc.
    mode : {'mosaic', 'surface'}, str optional
        [backend='matplotlib'] Controls whether to turn the images into surface
        plots.
    rows : int, optional
        How many rows (subplots in a grid) in the case ``data`` is a tuple of
        2d arrays.
    vmax : None, float/int or tuple of float/int, optional
        For defining the data range that the colormap covers. When set to None,
        the colormap covers the complete value range of the supplied data.
    vmin : None, float/int or tuple of float/int, optional
        For stretching the displayed pixels values. When set to None,
        the colormap covers the complete value range of the supplied data.
    circle : None, tuple or tuple of tuples, optional
        [backend='matplotlib'] To show a circle at the given px coordinates. The
        circles are shown on all subplots.
    circle_alpha : float or tuple of floats, optional
        [backend='matplotlib'] Alpha transparency for each circle.
    circle_color : str, optional
        [backend='matplotlib'] Color of the circles. White by default.
    circle_radius : int, optional
        [backend='matplotlib'] Radius of the circles, 6 px by default.
    circle_label : bool or string, optional
        [backend='matplotlib'] Whether to show the coordinates next to each
        circle. If a string: the string to be printed. If a tuple, should be 
        a tuple of strings with same length as 'circle'.
    circle_label_color : str, optional
        [backend='matplotlib'] Default 'white'. Sets the color of the circle
        label
    arrow : None or tuple of floats, optional
        [backend='matplotlib'] To show an arrow pointing to the given pixel
        coordinates.
    arrow_alpha : float, optional
        [backend='matplotlib'] Alpha transparency for the arrow.
    arrow_length : int, optional
        [backend='matplotlib'] Length of the arrow, 10 px by default.
    arrow_shiftx : int, optional
        [backend='matplotlib'] Shift in x of the arrow pointing position, 5 px
        by default.
    arrow_label : bool or string, optional
        [backend='matplotlib'] Label to be printed next to the arrow.
    label : None, str or tuple of str/None, optional
        [backend='matplotlib'] Text for labeling each subplot. The label is
        shown at the bottom-left corner if each subplot.
    label_pad : int or tuple of int, optional
        [backend='matplotlib'] Padding of the label from the left bottom corner.
        5 by default. If a tuple, sets the padding in x and y.
    label_size : int, optional
        [backend='matplotlib'] Size of the labels font.
    grid : bool or tuple of bools, optional
        [backend='matplotlib'] If True, a grid is displayed over the image, off
        by default.
    grid_alpha : None, float/int or tuple of None/float/int, optional
        [backend='matplotlib'] Alpha transparency of the grid.
    grid_color : str, optional
        [backend='matplotlib'] Color of the grid lines.
    grid_spacing : None, float/int or tuple of None/float/int, optional
        [backend='matplotlib'] Separation of the grid lines in pixels.
    cross : None or tuple of floats, optional
        [backend='matplotlib'] If provided, a crosshair is displayed at given
        pixel coordinates.
    cross_alpha : float, optional
        [backend='matplotlib'] Alpha transparency of the crosshair.
    cross_color : string, optional
        [backend='matplotlib'] Color of the crosshair.
    ang_scale : bool or tuple of bools, optional
        [backend='matplotlib'] If True, the axes are displayed in angular scale
        (arcsecs).
    ang_ticksep : int, optional
        [backend='matplotlib'] Separation for the ticks when using axis in
        angular scale.
    ndec : int, optional
        [backend='matplotlib'] Number of decimals for axes labels.
    pxscale : float, optional
        [backend='matplotlib'] Pixel scale in arcseconds/px. Default 0.01
        (Keck/NIRC2, SPHERE-IRDIS).
    auscale : float, optional
        [backend='matplotlib'] Pixel scale in au/px. Default 1.
    ang_legend : bool or tuple of bools, optional
        [backend='matplotlib'] If True a scaling bar (1 arcsec or 500 mas) will
        be added on the bottom-right corner of the subplots.
    au_legend : bool or tuple of bools, optional
        [backend='matplotlib'] If True (and ang_legend is False) a scaling bar 
        (10 au, 20 au or 50 au) will be added on the top-right corner of the 
        subplots.
    axis : bool, optional
        [backend='matplotlib'] Show the axis, on by default.
    show_center : bool or tuple of bools, optional
        [backend='matplotlib'] To show a cross at the center of the frame.
    cmap : None, str or tuple of str, optional
        Colormap to be used. When None, the value of the global variable
        ``default_cmap`` will be used. Any string corresponding to a valid
        ``matplotlib`` colormap can be used. Additionally, 'ds9cool', 'ds9heat'
        and 'binary' (for binary maps) are valid colormaps for this function.
    log : bool or tuple of bool, optional
        [backend='matplotlib'] Log color scale.
    colorbar : bool or tuple of bool, optional
        To attach a colorbar, on by default.
    colorbar_ticks : None, tuple or tuple of tuples, optional
        [backend='matplotlib'] Custom ticks for the colorbar of each plot.
    dpi : int, optional
        [backend='matplotlib'] Dots per inch, determines how many pixels the
        figure comprises (which affects the plot quality).
    size_factor : int, optional
        [backend='matplotlib'] Determines the size of the plot by setting the
        figsize parameter (width x height [inches]) as size_factor * ncols,
        size_factor * nrows.
    horsp : float, optional
        [backend='matplotlib'] Horizontal gap between subplots.
    versp : float, optional
        [backend='matplotlib'] Vertical gap between subplots.
    width : int, optional
        [backend='bokeh'] Controls the width of each subplot.
    height : int, optional
        [backend='bokeh'] Controls the height of each subplot.
    title : None or str, optional
        [backend='matplotlib'] Title of the whole figure, None by default.
    tit_size: int, optional
        Size of the title font.
    sampling : int, optional
        [mode='surface'] Sets the stride used to sample the input data to
        generate the surface graph.
    save : None or str, optional
        If a string is provided the plot is saved using ``save`` as the
        path/filename.
    transparent : bool, optional
        [save=True] Whether to have a transparent background between subplots.
        If False, then a white background is shown.

    """
    def check_bool_param(param, name):
        msg_type = '`' + name + '` must be a bool or tuple of bools'
        if isinstance(param, bool):
            param = [param] * num_plots
        elif isinstance(param, tuple):
            if not num_plots == len(param):
                msg = 'The len of `' + name + '` ({}) does not match the ' + \
                      'number of plots ({})'
                raise ValueError(msg.format(len(param), num_plots))
            else:
                for elem in param:
                    if not isinstance(elem, bool):
                        raise TypeError(msg_type)
        else:
            raise TypeError(msg_type)
        return param

    def check_numeric_param(param, name):
        msg_type = '`' + name + '` must be a None, float/int or tuple of ' + \
                   'None/float/ints'
        if param is None:
            param = [None] * num_plots
        elif isinstance(param, (int, float)):
            param = [param] * num_plots
        elif isinstance(param, tuple):
            if not num_plots == len(param):
                msg = 'The len of `' + name + '` ({}) does not match the ' + \
                      'number of plots ({})'
                raise ValueError(msg.format(len(param), num_plots))
            else:
                for elem in param:
                    if elem and not isinstance(elem, (float, int)):
                        raise TypeError(msg_type)
        else:
            raise TypeError(msg_type)
        return param

    def check_str_param(param, name, default_value=None):
        msg_type = '`' + name + '` must be a None, str or tuple of ' + \
                   'None/str'
        if param is None:
            param = [default_value] * num_plots
        elif isinstance(param, str):
            param = [param] * num_plots
        elif isinstance(param, tuple):
            if not num_plots == len(param):
                msg = 'The len of `' + name + '` ({}) does not match the ' + \
                      'number of plots ({})'
                raise ValueError(msg.format(len(param), num_plots))
            else:
                for elem in param:
                    if elem and not isinstance(elem, str):
                        raise TypeError(msg_type)
        else:
            raise TypeError(msg_type)
        return param
    # --------------------------------------------------------------------------

    # Checking inputs: a frame (1 or 3 channels) or tuple of them
    msg_data_type = "`data` must be a frame (2d array) or tuple of frames"
    if isinstance(data, np.ndarray):
        if data.ndim == 2:
            data = [data]
        elif data.ndim == 3:
            raise TypeError(msg_data_type)
    elif isinstance(data, tuple):
        for i in range(len(data)):
            # checking the elements are 2d (excepting the case of 3 channels)
            if not data[i].ndim == 2:# and data[i].shape[2] != 3:
                raise ValueError(msg_data_type)
    else:
        raise ValueError(msg_data_type)

    if not isinstance(backend, str):
        raise TypeError('`backend` must be a string. ' + msg_data_type)

    if backend == 'bokeh':
        if mode == 'surface':
            raise ValueError('Surface plotting only supported with matplotlib '
                             'backend')
        if save is not None:
            raise ValueError('Saving is only supported with matplotlib backend')

    if isinstance(label_pad, tuple):
        label_pad_x, label_pad_y = label_pad
    else:
        label_pad_x = label_pad
        label_pad_y = label_pad

    num_plots = len(data)

    if rows == 0:
        raise ValueError('Rows must be a positive integer')
    if num_plots % rows == 0:
        cols = int(num_plots / rows)
    else:
        cols = int((num_plots / rows) + 1)

    # CIRCLE -------------------------------------------------------------------
    if circle is not None:
        if isinstance(circle, tuple):
            show_circle = True
            if isinstance(circle[0], tuple):
                n_circ = len(circle)
                coor_circle = circle
            elif isinstance(circle[0], (float, int)):
                n_circ = 1
                coor_circle = [circle] * n_circ
        else:
            print("`circle` must be a tuple (X,Y) or tuple of tuples (X,Y)")
            show_circle = False
    else:
        show_circle = False

    if show_circle:
        if isinstance(circle_radius, (float, int)):
            # single value is provided, used for all circles
            circle_radius = [circle_radius] * n_circ
        elif isinstance(circle_radius, tuple):
            # a different value for each circle
            if not n_circ == len(circle_radius):
                msg = '`circle_radius` must have the same len as `circle`'
                raise ValueError(msg)
        else:
            raise TypeError("`circle_rad` must be a float or tuple of floats")

    if show_circle:
        if isinstance(circle_alpha, (float, int)):
            circle_alpha = [circle_alpha] * n_circ
        elif isinstance(circle_alpha, tuple):
            # a different value for each circle
            if not n_circ == len(circle_alpha):
                msg = '`circle_alpha` must have the same len as `circle`'
                raise ValueError(msg)

    # ARROW --------------------------------------------------------------------
    if arrow is not None:
        if isinstance(arrow, tuple):
            show_arrow = True
        else:
            raise ValueError("`arrow` must be a tuple (X,Y)")
    else:
        show_arrow = False

    # VMAX-VMIN ----------------------------------------------------------------
    vmin = check_numeric_param(vmin, 'vmin')
    vmax = check_numeric_param(vmax, 'vmax')

    # CROSS --------------------------------------------------------------------
    if cross is not None:
        if not isinstance(cross, tuple):
            raise ValueError("`crosshair` must be a tuple (X,Y)")
        else:
            coor_cross = cross
            show_cross = True
    else:
        show_cross = False

    # AXIS, GRID, ANG_SCALE ----------------------------------------------------
    axis = check_bool_param(axis, 'axis')
    grid = check_bool_param(grid, 'grid')
    grid_alpha = check_numeric_param(grid_alpha, 'grid_alpha')
    grid_spacing = check_numeric_param(grid_spacing, 'grid_spacing')
    show_center = check_bool_param(show_center, 'show_center')
    ang_scale = check_bool_param(ang_scale, 'ang_scale')
    ang_legend = check_bool_param(ang_legend, 'ang_legend')
    au_legend = check_bool_param(au_legend, 'au_legend')

    if isinstance(grid_color, str):
        grid_color = [grid_color] * num_plots

    if any(ang_scale) and save is not None:
        print("`Pixel scale set to {}`".format(pxscale))

    # LABEL --------------------------------------------------------------------
    label = check_str_param(label, 'label')

    # CMAP ---------------------------------------------------------------------
    custom_cmap = check_str_param(cmap, 'cmap', default_cmap)

    # COLORBAR -----------------------------------------------------------------
    colorbar = check_bool_param(colorbar, 'colorbar')

    if colorbar_ticks is not None:
        cbar_ticks = colorbar_ticks
        # must be a tuple
        if isinstance(cbar_ticks, tuple):
            # tuple of tuples
            if isinstance(cbar_ticks[0], tuple):
                if not num_plots == len(cbar_ticks):
                    raise ValueError('`colorbar_ticks` does not contain enough '
                                     'items')
            # single tuple
            elif isinstance(cbar_ticks[0], (float, int)):
                cbar_ticks = [colorbar_ticks] * num_plots
        else:
            raise TypeError('`colorbar_ticks` must be a tuple or tuple of '
                            'tuples')
    else:
        cbar_ticks = [None] * num_plots

    # LOG ----------------------------------------------------------------------
    logscale = check_bool_param(log, 'log')
#    if any(logscale):
#        # Showing bad/nan pixels with the darkest color in current colormap
#        current_cmap = mplcm.get_cmap()
#        current_cmap.set_bad(current_cmap.colors[0])

    # --------------------------------------------------------------------------
    if backend == 'matplotlib':
        # Creating the figure --------------------------------------------------
        fig = figure(figsize=(cols * size_factor, rows * size_factor), dpi=dpi)

        if title is not None:
            fig.suptitle(title, fontsize=tit_size, va='center', x=0.51, 
                         #y=1-0.08*(28/tit_size)**(0.5))
                         y=1-0.1*(16/tit_size))

        if mode == 'surface':
            plot_mosaic = False
        elif mode == 'mosaic':
            plot_mosaic = True
        else:
            raise ValueError("`mode` value was not recognized")

        for i, v in enumerate(range(num_plots)):
            image = data[i].copy()
            frame_size = image.shape[0]  # assuming square frames
            cy = image.shape[0] / 2 - 0.5
            cx = image.shape[1] / 2 - 0.5
            v += 1

            if plot_mosaic:
                ax = subplot(rows, cols, v)
                ax.set_aspect('auto')

                if logscale[i]:
                    image += np.abs(np.nanmin(image))
                    if vmin[i] is None:
                        linthresh = 1e-2
                    else:
                        linthresh = vmin[i]
                    norm = colors.SymLogNorm(linthresh, base=10)
                else:
                    norm = None

                if image.dtype == bool:
                    image = image.astype(int)

                if custom_cmap[i] == 'binary' and image.max() == 1 and \
                   image.min() == 0:
                    cucmap = cmap_binary
                    cbticks = (0, 0.5, 1)

                else:
                    cucmap = custom_cmap[i]
                    cbticks = cbar_ticks[i]

                im = ax.imshow(image, cmap=cucmap, origin='lower', norm=norm,
                               interpolation='nearest', vmin=vmin[i],
                               vmax=vmax[i])

                if colorbar[i]:
                    divider = make_axes_locatable(ax)
                    # the width of cax is 5% of ax and the padding between cax
                    # and ax wis fixed at 0.05 inch
                    cax = divider.append_axes("right", size="5%", pad=0.05)
                    cb = plt_colorbar(im, ax=ax, cax=cax, drawedges=False,
                                      ticks=cbticks)
                    cb.outline.set_linewidth(0.1)
                    cb.ax.tick_params(labelsize=lab_fontsize)

            else:
                # Leave the import to make porjection='3d' work
                #from mpl_toolkits.mplot3d import Axes3D
                x = np.outer(np.arange(0, frame_size, 1), np.ones(frame_size))
                y = x.copy().T
                ax = subplot(rows, cols, v, projection='3d')
                ax.set_aspect('auto')
                surf = ax.plot_surface(x, y, image, rstride=sampling,
                                       cstride=sampling, linewidth=2,
                                       cmap=custom_cmap[i], antialiased=True,
                                       vmin=vmin[i], vmax=vmax[i])
                ax.set_xlabel('x')
                ax.set_ylabel('y')
                ax.dist = 10
                if title is not None:
                    ax.set_title(title)

                if colorbar[i]:
                    fig.colorbar(surf, aspect=10, pad=0.05, fraction=0.04)

            if ang_legend[i] and plot_mosaic:
                scaleng = 1. / pxscale
                scalab = '1 arcsec'
                scalabloc = scaleng / 2. - 8
                if scaleng > frame_size / 2.:
                    scaleng /= 2.
                    scalab = '500 mas'
                    scalabloc = scaleng / 2. - 8
                scapad = 4
                xma = frame_size - scapad
                xmi = xma - scaleng
                hlines(y=scapad, xmin=xmi, xmax=xma, colors='white', lw=1.,
                       linestyles='solid')
                annotate(scalab, (xmi + scalabloc, scapad + 2), color='white', 
                         size=label_size)
            elif au_legend[i] and plot_mosaic:
                pxsc_fac = (0.012265/pxscale)
                labsz_fac = (label_size/12)
                scaleng = 50. / auscale
                scalab = '50 au'
                scalabloc = scaleng / 2. - 6.4*pxsc_fac*labsz_fac
                if scaleng > frame_size / 3.:
                    scaleng = 20. / auscale
                    scalab = '20 au'
                    scalabloc = scaleng / 2. - 6.4*pxsc_fac*labsz_fac
                    if scaleng > frame_size / 3.:
                        scaleng = 10. / auscale
                        scalab = '10 au'
                        scalabloc = scaleng / 2. - 6.4*pxsc_fac*labsz_fac
                scapad = 5*pxsc_fac*labsz_fac
                xma = frame_size - scapad
                xmi = xma - scaleng
                hlines(y=xma-scapad, xmin=xmi, xmax=xma, colors='white', lw=1.,
                       linestyles='solid')
                annotate(scalab, (xmi + scalabloc, xma-0.5*scapad), 
                         color='white', size=label_size)
                
            if show_circle and plot_mosaic:
                if isinstance(circle_linestyle,tuple):
                    c_offset = circle_linestyle[0]
                    circle_linestyle = circle_linestyle[1]
                else:
                    c_offset = lab_fontsize+1  # vertical offset is equal to the font size + 1, was 2
                for j in range(n_circ):
                    if isinstance(circle_color, (list, tuple)):
                        circle_color_tmp = circle_color[j]
                    else:
                        circle_color_tmp = circle_color
                    if isinstance(circle_linestyle, (list, tuple)):
                        circle_linestyle_tmp = circle_linestyle[j]
                    else:
                        circle_linestyle_tmp = circle_linestyle
                    circ = Circle(coor_circle[j], radius=circle_radius[j],
                                  fill=False, color=circle_color_tmp,
                                  alpha=circle_alpha[j], ls=circle_linestyle_tmp)
                    ax.add_artist(circ)
                    if circle_label:                  
                        x = coor_circle[j][0]
                        y = coor_circle[j][1]
                        if isinstance(circle_label,str):
                            cirlabel = circle_label
                        elif isinstance(circle_label,tuple):
                            cirlabel = circle_label[j]
                        else:
                            cirlabel = str(int(x))+','+str(int(y))
                        ax.text(x, y + circle_radius[j] + c_offset, cirlabel,
                                fontsize=lab_fontsize, color=circle_label_color, family='monospace',
                                ha='center', va='center', weight='bold',
                                alpha=circle_alpha[j])

            if show_cross and plot_mosaic:
                ax.axhline(coor_cross[0], xmin=0, xmax=frame_size, alpha=cross_alpha, lw=0.6,
                           linestyle='dashed', color='white')
                ax.axvline(coor_cross[1], ymin=0, ymax=frame_size, alpha=cross_alpha, lw=0.6,
                           linestyle='dashed', color='white')

            if show_center[i] and plot_mosaic:
                ax.scatter([cy], [cx], marker='+',
                           color=cross_color, alpha=cross_alpha)

            if show_arrow and plot_mosaic:
                ax.arrow(arrow[0] + arrow_length + arrow_shiftx, arrow[1],
                         -arrow_length, 0, color='white', head_width=6,
                         head_length=4, width=2, length_includes_head=True,
                         alpha=arrow_alpha)
                if arrow_label:                  
                    x = arrow[0]
                    y = arrow[1]
                    if isinstance(arrow_label,str):
                        arrlabel = arrow_label
                    else:
                        arrlabel = str(int(x))+','+str(int(y))
                    if len(arrlabel) < 5:
                        arr_fontsize=14
                    else:
                        arr_fontsize=lab_fontsize
                    ax.text(x + arrow_length + 1.3*arrow_shiftx, y, arrlabel,
                            fontsize=arr_fontsize, color='white', family='monospace',
                            ha='left', va='center', weight='bold',
                            alpha=arrow_alpha)
                                
            if label[i] is not None and plot_mosaic:
                ax.annotate(label[i], xy=(label_pad_x, label_pad_y), color=label_color,
                            xycoords='axes pixels', weight='bold',
                            size=label_size)

            if grid[i] and plot_mosaic:
                if grid_spacing[i] is None:
                    if cy < 10:
                        gridspa = 1
                    elif cy >= 10:
                        if cy % 2 == 0:
                            gridspa = 4
                        else:
                            gridspa = 5
                else:
                    gridspa = grid_spacing[i]

                ax.tick_params(axis='both', which='minor')
                minor_ticks = np.arange(0, data[i].shape[0], gridspa)
                ax.set_xticks(minor_ticks, minor=True)
                ax.set_yticks(minor_ticks, minor=True)
                ax.grid(True, which='minor', color=grid_color[i], linewidth=0.5,
                        alpha=grid_alpha[i], linestyle='dashed')
            else:
                ax.grid(False)

            if ang_scale[i] and plot_mosaic:
                # Converting axes from pixels to arcseconds
                half_num_ticks = int(np.round(cy // ang_ticksep))

                # Calculate the pixel locations at which to put ticks
                ticks = []
                for t in range(half_num_ticks, -half_num_ticks-1, -1):
                    # Avoid ticks not showing on the last pixel
                    if not cy - t * ang_ticksep == frame_size:
                        ticks.append(cy - t * ang_ticksep)
                    else:
                        ticks.append((cy - t * ang_ticksep)-1)
                ax.set_xticks(ticks)
                ax.set_yticks(ticks)

                # Corresponding distance in arcseconds, measured from the center
                labels_y = []
                labels_x = []
                for t in range(half_num_ticks, -half_num_ticks-1, -1):
                    labels_y.append(round(Decimal(-t * (ang_ticksep * pxscale)),ndec))
                    labels_x.append(round(Decimal(t * (ang_ticksep * pxscale)),ndec))
                ax.set_xticklabels(labels_x)
                ax.set_yticklabels(labels_y)
                ax.set_xlabel('\u0394RA["]', fontsize=label_size)
                ax.set_ylabel('\u0394Dec["]', fontsize=label_size)
                ax.tick_params(axis='both', which='major', labelsize=label_size)
            else:
                ax.set_xlabel("x", fontsize=label_size)
                ax.set_ylabel("y", fontsize=label_size)

            if not axis[i]:
                ax.set_axis_off()

        fig.subplots_adjust(wspace=horsp, hspace=versp)
        if save is not None and isinstance(save, str):
            savefig(save, dpi=dpi, bbox_inches='tight', pad_inches=0,
                    transparent=transparent)
            close()
        else:
            show()

    elif backend == 'bokeh':
        hv.extension(backend)
        subplots = []
        # options = "Image (cmap='" + custom_cmap[0] + "')"  # taking first item
        # hv.opts(options)

        for i, v in enumerate(range(num_plots)):
            image = data[i].copy()
            if vmin[i] is None:
                vmin[i] = image.min()
            if vmax[i] is None:
                vmax[i] = image.max()
            im = hv.Image((range(image.shape[1]), range(image.shape[0]), image))
            subplots.append(im.opts(tools=['hover'], colorbar=colorbar[i],
                                    colorbar_opts={'width': 15},
                                    width=width, height=height,
                                    clim=(vmin[i], vmax[i]),
                                    cmap=custom_cmap[0]))

        return hv.Layout(subplots).cols(cols)

    else:
        raise ValueError('`backend` not supported')
コード例 #19
0
def plot():
    #################################################################333333
    # Final plot.
    # High Significant

    from shapely.geometry import Point, Polygon
    import pandas as pd

    import numpy as np
    import geoviews as gv

    import bokeh

    import panel as pn

    import holoviews as hv
    from holoviews import opts
    from holoviews.operation.datashader import datashade, rasterize
    hv.extension("bokeh")

    import math

    from bokeh.models import HoverTool

    from scipy.interpolate import griddata
    from scipy.io import loadmat

    import datetime

    from dateutil.parser import parse

    from datetime import timedelta
    import time
    strtt = time.time()

    datamat = loadmat('IOEC_ECM_noDA_20190703_masked.mat')
    # datamat = loadmat('IOEC_ECM_DA_20191121_masked.mat')

    Xp = datamat['Xp']
    Yp = datamat['Yp']

    # Xp=np.where(((Xp>= 85) & (Xp<=88) & (Yp>=19) & (Yp<=22)),Xp,88)
    # Yp=np.where(((Xp>= 85) & (Xp<=88) & (Yp>=19) & (Yp<=22)),Yp,22)

    strt = datetime.datetime(2019, 7, 4, 0, 0)
    end = datetime.datetime(2019, 1, 13, 0, 0)
    from bokeh.models.callbacks import CustomJS

    def perdelta(strt, end, delta):
        curr = strt
        while curr < end:
            yield curr
            curr += delta

    # Read element file

    tri_new = pd.read_csv('fort.ele',
                          delim_whitespace=True,
                          names=('A', 'B', 'C', 'D'),
                          usecols=[1, 2, 3],
                          skiprows=1,
                          dtype={'D': np.int})

    dateList = []

    def plotsecond(datetime, regions='Whole_East_Coast'):
        dat = datetime
        # datetostr=result.strftime("%Y%b%d%H")
        dt = parse(str(dat))
        yr = dt.year
        mn = dt.month
        d = dt.day
        hr = dt.hour
        mi = dt.minute

        if hr < 10:

            hr = '0' + str(hr)
        else:
            d = str(d)
            hr = str(hr)
        if int(d) < 10:
            d = '0' + str(d)
        else:
            d = str(d)
        #     varname = 'Hsig_' + str(yr) + '0' + str(mn) + str(d) + '_' + hr + '0000'

        Xp = datamat['Xp']
        Yp = datamat['Yp']
        x = Xp.flatten()
        y = Yp.flatten()

        Longitude = x
        Latitude = y

        pkname = 'PkDir_' + str(yr) + '0' + str(mn) + str(
            d) + '_' + hr + '0000'

        pkvalue = datamat[pkname]

        if regions is 'Odisha':
            pkvalue = np.where(
                ((Xp >= 85) & (Xp <= 88) & (Yp >= 19) & (Yp <= 22)), pkvalue,
                np.nan).flatten()
        elif regions is 'Andra_Pradesh':
            pkvalue = np.where(
                ((Xp >= 79) & (Xp <= 85) & (Yp >= 13) & (Yp <= 19)), pkvalue,
                np.nan).flatten()
        elif regions is 'Tamil_Nadu':
            pkvalue = np.where(
                ((Xp >= 77) & (Xp <= 83) & (Yp >= 7) & (Yp <= 14)), pkvalue,
                np.nan).flatten()
        elif regions is 'Whole_East_Coast':
            pkvalue = pkvalue.flatten()
        else:
            #         data = get_data4region(data,**odisha)
            pkvalue = pkvalue.flatten()

        pkvalue = pkvalue.flatten()
        d = pkvalue * (math.pi / 180)

        # target grid to interpolate to
        xt = np.arange(76.937, 92.008, 0.1)
        yt = np.arange(1.482, 22.461, 0.1)
        xi, yi = np.meshgrid(xt, yt)
        di = griddata((Longitude, Latitude), d, (xi, yi))

        dfcoast = pd.read_csv('ECpolygonTwoDegreeOffsetBorder.txt',
                              delim_whitespace=True,
                              names=('X', 'Y'))
        dfcoast['geometry'] = dfcoast.apply(lambda row: Point(row.X, row.Y),
                                            axis=1)
        poly = Polygon([(p.x, p.y) for p in dfcoast.geometry])

        arr = np.zeros((len(yt), len(xt)))
        for i in range(len(xt)):
            for j in range(len(yt)):
                point = Point(xt[i], yt[j])
                arr[j, i] = poly.contains(point)

        mask = (xi > 79.7817) & (xi < 81.2718) & (yi > 7.6951) & (yi < 9.7406)
        di[mask] = np.nan
        di[arr == False] = np.nan
        U = np.cos(di)
        V = np.sin(di)

        mag = np.sqrt(U**2 + V**2)
        angle = (np.pi / 2.) - np.arctan2(U / mag, V / mag)

        vec = gv.VectorField(
            (xi[::5, ::5], yi[::5, ::5], angle[::5, ::5], mag[::5, ::5]))
        return vec

    def plotthis(datetime, regions='Whole_East_Coast'):
        dat = datetime
        # datetostr=result.strftime("%Y%b%d%H")
        dt = parse(str(dat))
        yr = dt.year
        mn = dt.month
        d = dt.day
        hr = dt.hour
        mi = dt.minute

        if hr < 10:

            hr = '0' + str(hr)
        else:
            d = str(d)
            hr = str(hr)
        if int(d) < 10:
            d = '0' + str(d)
        else:
            d = str(d)
        varname = 'Hsig_' + str(yr) + '0' + str(mn) + str(
            d) + '_' + hr + '0000'

        x = Xp.flatten()
        y = Yp.flatten()
        z = datamat[varname]

        if regions is 'Odisha':
            z = np.where(((Xp >= 85) & (Xp <= 88) & (Yp >= 19) & (Yp <= 22)),
                         z, np.nan).flatten()
        elif regions is 'Andra_Pradesh':
            z = np.where(((Xp >= 79) & (Xp <= 85) & (Yp >= 13) & (Yp <= 19)),
                         z, np.nan).flatten()
        elif regions is 'Tamil_Nadu':
            z = np.where(((Xp >= 77) & (Xp <= 83) & (Yp >= 7) & (Yp <= 14)), z,
                         np.nan).flatten()
        elif regions is 'Whole_East_Coast':
            z = z.flatten()

        else:
            #         data = get_data4region(data,**odisha)
            z = z.flatten()

        # z = z.flatten()
        Longitude = x
        Latitude = y
        High_Significant = z

        pts = np.stack((Longitude, Latitude, High_Significant)).T
        verts = pd.DataFrame(
            np.stack((Longitude, Latitude, High_Significant)).T,
            columns=['Longitude', 'Latitude', ' High_Significant'])

        # openStreet Background.
        tri_sub = tri_new.apply(lambda x: x - 1)
        ggpoints = gv.Points(verts, vdims=[' High_Significant'])
        ggsubraster = rasterize(gv.TriMesh((tri_sub, gv.Points(verts))))

        tri = gv.TriMesh((tri_sub, gv.Points(verts)))

        return tri

    allplot = {
        (k.strftime("%Y-%m-%d %H:%M:%S"), r): plotthis(k, r)
        for k in perdelta(strt, strt + timedelta(days=2), timedelta(hours=18))
        for r in ['Odisha', 'Andra_Pradesh', 'Whole_East_Coast', 'Tamil_Nadu']
    }
    allplot2 = {
        (k.strftime("%Y-%m-%d %H:%M:%S"), r): plotsecond(k, r)
        for k in perdelta(strt, strt + timedelta(days=2), timedelta(hours=18))
        for r in ['Odisha', 'Andra_Pradesh', 'Whole_East_Coast', 'Tamil_Nadu']
    }

    df_div = hv.Div("""
        <figure>
        <img src="https://i.ibb.co/S0t5GWb/imglogo.png" height='80' width='90' vspace='-10'>
        """)

    df_div1 = hv.Div("""
        &nbsp<center><b><p style="color:#B22222";font-size:80px;font-family:Times new roman><h1 style=font-size:20px;margin-left:2.5em;margin-top:-1em;color:#B22222>Indian National Center for Ocean Information Services<br />
        (INCOIS)</h1></p></b></center>

        """)

    colorbar_opts = {
        'major_label_overrides': {
            0: '0',
            0.5: '0.5',
            1: '1',
            1.5: '1.5',
            2: '2',
            2.5: '2.5',
            3: '3',
            3.5: '>3.5',
            3.8: '>4 ',
            3.9: '>3.9',
        },
        'major_label_text_align': 'left',
        'major_label_text_font_style': 'bold',
    }
    levels = [
        0,
        0.2,
        0.4,
        0.6,
        0.8,
        1,
        1.2,
        1.4,
        1.6,
        1.8,
        2,
        2.2,
        2.5,
        3,
    ]

    def disable_logo(plot, element):
        plot.state.toolbar.logo = None

    hv.plotting.bokeh.ElementPlot.finalize_hooks.append(disable_logo)

    # logo1 = hv.RGB.load_image("imglogo.png")

    logo1 = hv.RGB.load_image("https://i.ibb.co/7VXRPCS/logo1.png")

    def absolute_position(plot, element):
        glyph = plot.handles['glyph']
        x_range, y_range = plot.handles['x_range'], plot.handles['y_range']
        glyph.dh_units = 'screen'
        glyph.dw_units = 'screen'
        glyph.dh = 60
        glyph.dw = 90
        glyph.x = x_range.start
        glyph.y = y_range.start
        xcode = CustomJS(code="glyph.x = cb_obj.start", args={'glyph': glyph})
        plot.handles['x_range'].js_on_change('start', xcode)
        ycode = CustomJS(code="glyph.y = cb_obj.start", args={'glyph': glyph})
        plot.handles['y_range'].js_on_change('start', ycode)

    def plot_limits(plot, element):
        plot.handles['x_range'].min_interval = 100
        plot.handles['x_range'].max_interval = 55000000
        # 3000000
        plot.handles['y_range'].min_interval = 500
        plot.handles['y_range'].max_interval = 900000

    opts = dict(
        width=700,
        height=700,
        tools=['hover', 'save', 'wheel_zoom'],
        active_tools=['wheel_zoom'],
        hooks=[plot_limits, disable_logo],
        colorbar=True,
        color_levels=15,
        colorbar_opts=colorbar_opts,
        cmap=[
            '#000080',
            '#0000cd',
            '#0008ff',
            '#004cff',
            '#0090ff',
            '#00d4ff',
            '#29ffce',
            '#60ff97',
            '#97ff60',
            '#ceff29',
            '#ffe600',
            '#ffa700',
            # '#ff6800',
            '#ff2900',
            '#cd0000',
            '#800000',
        ],
        clim=(0, 3.76),
        title="\tSignificant Wave Height (m) and Direction (°) ",
        fontsize={
            'title': 18,
            'xlabel': 15,
            'ylabel': 15,
            'ticks': 12
        })

    tiles = gv.tile_sources.Wikipedia
    hmap1 = hv.HoloMap(allplot,
                       kdims=['Select Date and Time :', 'Select Indian State'])
    hmap2 = hv.HoloMap(allplot2,
                       kdims=['Select Date and Time :', 'Select Indian State'])

    dd = df_div.opts(width=70, height=70)
    dd1 = df_div1.opts(width=600, height=90)

    finalplot = pn.Column(
        pn.Row(dd, dd1),
        tiles * rasterize(hmap1).options(**opts) * hmap2 *
        logo1.opts(hooks=[absolute_position], apply_ranges=False)).servable()
    # print("--- %s seconds ---" % (time.time() - strtt))
    from bokeh.embed import components
    from bokeh.resources import CDN
    from bokeh.io import curdoc
    doc = curdoc()
    script, div = components(finalplot.get_root(doc))
    cdn_js0 = CDN.js_files[0]
    cdn_js = CDN.js_files[1]
    cdn_css = CDN.css_files
    print("cdn_js:", cdn_js)
    print("cdn_css", cdn_css)

    return render_template("plot.html",
                           script=script,
                           div=div,
                           cdn_css=cdn_css,
                           cdn_js=cdn_js,
                           cdn_js0=cdn_js0)
コード例 #20
0
def plot_cubes(cube, mode='slider', backend='matplotlib', dpi=100,
               figtype='png', vmin=None, vmax=None, size=100, width=360,
               height=360, cmap=None, colorbar=True, dynamic=True,
               anim_path=None, data_step_range=None, label=None,
               label_step_range=None, delay=50, anim_format='gif',
               delete_anim_cache=True, **kwargs):
    """ Plot multi-dimensional high-contrast imaging datacubes (3d and 4d
    ``numpy`` arrays). It allows to visualize in-memory ``numpy`` arrays on
    ``Jupyterlab`` by leveraging the ``HoloViews`` library. It can also generate
    and save animations from a 3d ``numpy`` array with ``matplotlib``.

    Parameters
    ----------
    cube : np.ndarray
        Input 3d or 4d cube.
    mode : {'slider', 'animation'}, str optional
        Whether to plot the 3d array as a widget with a slider or to save an
        animation of the 3d array. The animation is saved to disk using
        ImageMagick's convert command (it must be installed otherwise a
        ``FileNotFoundError`` will be raised).
    backend : {'matplotlib', 'bokeh'}, str optional
        Selects the backend used to display the plots. ``Bokeh`` plots are
        interactive, allowing the used to zoom, pan, inspect pixel values, etc.
        ``Matplotlib`` can lead to some flickering when using the slider and
        ``dynamic`` is True.
    dpi : int, optional
        [backend='matplotlib'] The rendered dpi of the figure.
    figtype : {'png', 'svg'}, str optional
        [backend='matplotlib'] Type of output.
    vmin : None, float or int, optional
        For defining the data range that the colormap covers. When set to None,
        the colormap covers the complete value range of the supplied data.
    vmax : None, float or int, optional
        For defining the data range that the colormap covers. When set to None,
        the colormap covers the complete value range of the supplied data.
    size : int, optional
        [backend='matplotlib'] Sets the size of the plot.
    width : int, optional
        [backend='bokeh'] Sets the width of the plot.
    height : int, optional
        [backend='bokeh'] Sets the height of the plot.
    cmap : None or str, optional
        Colormap. When None, the value of the global variable ``default_cmap``
        will be used.
    colorbar : bool, optional
        If True, a colorbar is shown.
    dynamic : bool, optional
        [mode='slider'] When False, a ``HoloViews.HoloMap`` is created (slower
        and will take up a lot of RAM for large datasets). If True, a
        ``HoloViews.DynamicMap`` is created instead.
    anim_path : str, optional
        [mode='animation'] The animation path/filename. If None then the
        animation will be called ``animation``.``anim_format`` and will be saved
        in the current directory.
    data_step_range : tuple, optional
        [mode='animation'] Tuple of 1, 2 or 3 values that creates a range for
        slicing the ``data`` cube.
    label : str, optional
        [mode='animation'] Label to be overlaid on top of each frame of the
        animation. If None, then ``frame <#>`` will be used.
    label_step_range : tuple, optional
        [mode='animation'] Tuple of 1, 2 or 3 values that creates a range for
        customizing the label overlaid on top of each frame of the animation.
    delay : int, optional
        [mode='animation'] Delay for displaying the frames in the animation
        sequence.
    anim_format : str, optional
        [mode='animation'] Format of the saved animation. By default 'gif' is
        used. Other formats supported by ImageMagick are valid, such as 'mp4'.
    delete_anim_cache : str, optional
        [mode='animation'] If True, the cache folder is deleted once the
        animation file is saved to disk.
    **kwargs : dictionary, optional
        [mode='animation'] Arguments to be passed to ``plot_frames`` to
        customize each frame of the animation (adding markers, using a log
        scale, etc).

    Notes
    -----
    http://holoviews.org/getting_started/Gridded_Datasets.html
    http://holoviews.org/user_guide/Gridded_Datasets.html
    http://holoviews.org/user_guide/Applying_Customizations.html
    """
    hv.extension(backend)

    if not isinstance(cube, np.ndarray):
        raise TypeError('`cube` must be a numpy.ndarray')

    if cmap is None:
        cmap = default_cmap

    if mode == 'slider':
        if cube.ndim not in (3, 4):
            raise ValueError('`cube` must be a 3 or 4 array when `mode` set to '
                             'slider')

        if cube.ndim == 3:
            # Dataset((X, Y, Z), Data), where
            # X is a 1D array of shape M ,
            # Y is a 1D array of shape N and
            # Z is a 1D array of shape O
            # Data is a ND array of shape NxMxO
            ds = hv.Dataset((range(cube.shape[2]), range(cube.shape[1]),
                             range(cube.shape[0]), cube), ['x', 'y', 'time'],
                            'flux')
            max_frames = cube.shape[0]
        elif cube.ndim == 4:
            # adding a lambda dimension
            ds = hv.Dataset((range(cube.shape[3]), range(cube.shape[2]),
                             range(cube.shape[1]), range(cube.shape[0]), cube),
                            ['x', 'y', 'time', 'lambda'], 'flux')
            max_frames = cube.shape[0] * cube.shape[1]

        # Matplotlib takes None but not Bokeh. We take global min & max instead
        if vmin is None:
            vmin = cube.min()
        if vmax is None:
            vmax = cube.max()

        print(ds)
        print(":Cube_shape\t{}".format(list(cube.shape[::-1])))

        # not working for bokeh: dpi
        image_stack = ds.to(hv.Image, kdims=['x', 'y'], dynamic=dynamic)
        hv.output(backend=backend, size=size, dpi=dpi, fig=figtype,
                  max_frames=max_frames)

        if backend == 'matplotlib':
            # keywords in the currently active 'matplotlib' renderer are:
            # 'alpha', 'clims', 'cmap', 'filterrad', 'interpolation', 'norm',
            # 'visible'
            #options = "Image (cmap='" + cmap + "', interpolation='nearest',"
            #options += " clims=("+str(vmin)+','+str(vmax)+")"+")"
            #opts(options, image_stack)
            return image_stack.opts(opts.Image(colorbar=colorbar,
                                               cmap=cmap,
                                               clim=(vmin, vmax)))
            # hv.save(image_stack, 'holomap.gif', fps=5)

        elif backend == 'bokeh':
            #options = "Image (cmap='" + cmap + "')"
            #opts(options, image_stack)
            # Compensating the width to accommodate the colorbar
            if colorbar:
                cb_wid = 15
                cb_pad = 3
                tick_len = len(str(int(cube.max())))
                if tick_len < 4:
                    cb_tick = 25
                elif tick_len == 4:
                    cb_tick = 35
                elif tick_len > 4:
                    cb_tick = 45
                width_ = width + cb_pad + cb_wid + cb_tick
            else:
                width_ = width

            return image_stack.opts(opts.Image(colorbar=colorbar,
                                               colorbar_opts={'width': 15,
                                                              'padding': 3},
                                               width=width_, height=height,
                                               clim=(vmin, vmax),
                                               cmap=cmap,
                                               tools=['hover']))

    elif mode == 'animation':
        if cube.ndim != 3:
            raise ValueError('`cube` must be a 3 array when `mode` set to '
                             'animation')

        if backend == 'bokeh':
            print('Creating animations works with the matplotlib backend')

        dir_path = './animation_temp/'
        if anim_path is None:
            anim_path = './animation'

        if data_step_range is None:
            data_step_range = range(0, cube.shape[0], 1)
        else:
            if not isinstance(data_step_range, tuple):
                msg = '`data_step_range` must be a tuple with 1, 2 or 3 values'
                raise ValueError(msg)
            if len(data_step_range) == 1:
                data_step_range = range(data_step_range)
            elif len(data_step_range) == 2:
                data_step_range = range(data_step_range[0], data_step_range[1])
            elif len(data_step_range) == 3:
                data_step_range = range(data_step_range[0],
                                        data_step_range[1],
                                        data_step_range[2])

        if label_step_range is None:
            label_step_range = data_step_range
        else:
            if not isinstance(label_step_range, tuple):
                msg = '`label_step_range` must be a tuple with 1, 2 or 3 values'
                raise ValueError(msg)
            if len(label_step_range) == 1:
                label_step_range = range(label_step_range)
            elif len(label_step_range) == 2:
                label_step_range = range(label_step_range[0],
                                         label_step_range[1])
            elif len(label_step_range) == 3:
                label_step_range = range(label_step_range[0],
                                         label_step_range[1],
                                         label_step_range[2])

        if os.path.exists(dir_path):
            shutil.rmtree(dir_path)
            print('Replacing ' + dir_path)
        os.mkdir(dir_path)

        print('Producing each animation frame...')
        for i, labstep in zip(data_step_range, list(label_step_range)):
            if label is None:
                label = 'frame '
            savelabel = dir_path + label + str(i + 100)
            plot_frames(cube[i], backend='matplotlib', mode='mosaic',
                        save=savelabel, dpi=dpi, vmin=vmin, vmax=vmax,
                        colorbar=colorbar, cmap=cmap,
                        label=[label + str(labstep + 1)], **kwargs)
        try:
            filename = anim_path + '.' + anim_format
            call(['convert', '-delay', str(delay), dir_path + '*.png',
                  filename])
            if os.path.exists(filename):
                print('Animation successfully saved to disk as ' + filename)
                if delete_anim_cache:
                    shutil.rmtree(dir_path)
                    print('Temp directory deleted ' + dir_path)

        except FileNotFoundError:
            print('ImageMagick `convert` command could not be found')

    else:
        raise ValueError("`mode` is not recognized")
コード例 #21
0
ファイル: util.py プロジェクト: wsheffel/hvplot
 def wrapper(*args, **kwargs):
     if extension and not getattr(hv.extension, '_loaded', False):
         hv.extension(extension, logo=logo)
     return func(*args, **kwargs)
コード例 #22
0
from bokeh.sampledata import gapminder
from holoviews import dim
from holoviews import opts

from holoviews.plotting.bokeh import BokehRenderer

# In[5]:

l = ["#40fe10", "#fffe10", "#f25000", "#ff0f00"]

# ### Gapminder test

# In[6]:

renderer = hv.renderer('bokeh')
hv.extension('plotly')
hv.extension('bokeh')

# In[7]:

df = pd.read_csv("/home/atez/Scrivania/web valley/milano_cleaned.csv")

# In[24]:

#print(df.columns.values)

# In[9]:

df_medical = df[[
    'cod_pz', 'visit:visit', 'ult_tsa:imt_cc_average_right',
    'lab:total_cholesterol', 'lab:calculated_ldl', 'ScoreClass'
コード例 #23
0
def plot_policy_gantt_chart(
    policies,
    effects=False,
    colors="categorical",
    fig_kwargs=None,
):
    """Plot a Gantt chart of the policies."""
    if fig_kwargs is None:
        fig_kwargs = {}
    fig_kwargs = {**DEFAULT_FIGURE_KWARGS, **fig_kwargs}

    if isinstance(policies, dict):
        df = (pd.DataFrame(policies).T.reset_index().rename(columns={
            "index": "name"
        }).astype({
            "start": "datetime64",
            "end": "datetime64"
        }).drop(columns="policy"))
    elif isinstance(policies, pd.DataFrame):
        df = policies
    else:
        raise ValueError(
            "'policies' should be either a dict or pandas.DataFrame.")

    if effects:
        effect_kwargs = effects if isinstance(effects, dict) else {}
        effects = compute_pseudo_effect_sizes_of_policies(policies=policies,
                                                          **effect_kwargs)
        effects_s = pd.DataFrame([{
            "policy": name,
            "effect": effects[name]["mean"]
        } for name in effects]).set_index("policy")["effect"]
        df = df.merge(effects_s, left_on="name", right_index=True)
        df["alpha"] = (1 - df["effect"] + 0.1) / 1.1
    else:
        df["alpha"] = 1

    df = df.reset_index()
    df = _complete_dates(df)
    df = _add_color_to_gantt_groups(df, colors)
    df = _add_positions(df)

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

    segments = hv.Segments(
        df,
        [
            hv.Dimension("start", label="Date"),
            hv.Dimension("position", label="Affected contact model"),
            "end",
            "position",
        ],
    )
    y_ticks_and_labels = list(zip(*_create_y_ticks_and_labels(df)))

    tooltips = [("Name", "@name")]
    if effects:
        tooltips.append(("Effect", "@effect"))
    hover = HoverTool(tooltips=tooltips)

    gantt = segments.opts(
        color="color",
        alpha="alpha",
        tools=[hover],
        yticks=y_ticks_and_labels,
        **fig_kwargs,
    )

    return gantt
コード例 #24
0
import glob
import argparse
import os.path

import xarray

import sonar.lowrance_log_parser
import sonar.map
import sonar.normalized_depth
import sonar.all
import sonar.merged_position

DEBUG_EXCEPTIONS = False

# Configure the default renderer
holoviews.extension('bokeh')
renderer = holoviews.renderer('bokeh').instance(mode='server')


def GenMapImage(sonar_data):
    try:
        holoviews_map = sonar.map.GenMap(sonar_data)

        # @todo There is a bug somehow where the map tiles dont load in time and renders a white square
        time.sleep(10)
        map_file_name = sonar_data.attrs['gen_name'] + '.map.png'
        logger.info('Saving map image: %s', map_file_name)
        holoviews.save(holoviews_map, map_file_name)
    except:
        logger.exception('Failed to produce map image for: %s',
                         sonar_data.attrs['file_name'])
コード例 #25
0
def interactive_map(v1, options):
    """
        Creates and interactive map of the variable v1 on the cruise track contained in options['gps_file']

        :param v1: dataframe / series containing the variable to be visualized
        :param options: 
            - options['plot_size'] : global figure scaling (make all of them larger of smaller by this multiplier), except the interactive plots\n",
            - options['figsize'] : width, height for the interactive plot\n",
            - options['scatter_markersize'] :  size of the markers in the scatterplot\n",
            - options['map_scatter_markersize'] : size of the markers in the static geographical map\n",
            - options['map_temporal_aggregation'] : Hours to aggregate in the static and interactive geographical map.\n",
            - options['resampling_operation'] : resample points on map temporally,
            - options['colormap'] : colormap from plt.cm
            - options['gps_file'] : file containing the gps coordinates

        :returns: interactive figure object
    """

    import cartopy.crs as ccrs
    import holoviews as hv
    from holoviews import opts, dim
    import geoviews as gv
    import geoviews.feature as gf
    import simplekml

    hv.extension("bokeh", "matplotlib")

    vname = v1.columns.tolist()[0]

    stretch = [0, 100]

    tres = np.median(np.diff(v1.index.tolist()))
    tres = 1 * int(tres.total_seconds() / 60)
    # vf1 = dataset.ts_aggregate_timebins(v1.to_frame(), time_bin=tres, operations={'': np.nanmean}, index_position='middle')

    #  come up with leg coloring
    leg_series = dataset.add_legs_index(v1)["leg"]

    vcol = dataset.ts_aggregate_timebins(
        v1,
        time_bin=tres,
        operations={"": options["resampling_operation"]},
        index_position="initial",
    )

    vcol.columns = ["color"]
    min_ = np.percentile(vcol.dropna(), stretch[0])
    max_ = np.percentile(vcol.dropna(), stretch[1])
    # print(min_,max_)
    vcol[vcol < min_] = min_
    vcol[vcol > max_] = max_

    coordinates_raw = dataset.read_standard_dataframe(
        options["gps_file"])[["latitude", "longitude"]]

    # mode_ = lambda x : stats.mode(x)[0]
    #  Deal with coordinates
    coordinates = dataset.ts_aggregate_timebins(
        coordinates_raw,
        time_bin=int(np.floor(options["map_temporal_aggregation"] * 60)),
        operations={"": np.nanmedian},
        index_position="initial",
    )

    #  Resample and merge coordinates + data
    to_plot = pd.merge(coordinates, v1, left_index=True, right_index=True)
    to_plot = pd.merge(
        to_plot,
        pd.DataFrame(data=to_plot.index.tolist(),
                     columns=["date"],
                     index=to_plot.index),
        left_index=True,
        right_index=True,
    )
    to_plot = to_plot.dropna()

    if options["kml_file"]:
        kml_ = simplekml.Kml()
        fol = kml_.newfolder(name="LV_KML")

        colz = (to_plot.loc[:, vname] - to_plot.loc[:, vname].min()) / (
            to_plot.loc[:, vname].max() - to_plot.loc[:, vname].min())
        colz = np.floor(255 * plt.cm.Spectral_r(colz.values)).astype(int)
        c = 0
        for lat, lon, val, date in to_plot.values:
            #     print(lat, lon, val, date)
            #     print(row[1].loc['LV#11'])
            pnt = fol.newpoint(name=str(date), coords=[(lon, lat)])
            pnt.style.iconstyle.icon.href = (
                "http://maps.google.com/mapfiles/kml/shapes/shaded_dot.png")
            pnt.style.iconstyle.scale = 1  # Icon thrice as big
            pnt.style.iconstyle.color = simplekml.Color.rgb(
                colz[c, 0], colz[c, 1], colz[c, 2], 255)
            pnt.style.labelstyle.scale = 0.5
            c += 1

        kml_.save(options["kml_file"])

    #  colorbar limits
    min_v1 = min_  # np.percentile(to_plot.loc[:,vname].dropna(), stretch[0])
    max_v1 = max_  # np.percentile(to_plot.loc[:,vname].dropna(), stretch[1])

    #  Create geoviews datasets
    v1_tomap = hv.Dataset(
        to_plot.loc[:, ["longitude", "latitude", "date", vname]],
        kdims=["longitude", "latitude"],
        vdims=[hv.Dimension(vname, range=(min_v1, max_v1))],
        group=vname,
    )

    points_v1 = v1_tomap.to(gv.Points, kdims=["longitude", "latitude"])

    gps_track = gv.Dataset(coordinates_raw)
    track = gv.Path(gps_track, kdims=["longitude", "latitude"])
    # land_ = gf.land#.options(facecolor='red')

    point_map_v1 = points_v1.opts(
        projection=ccrs.SouthPolarStereo(),
        cmap=options["colormap"],
        size=5,
        tools=["hover"],  #  ['hover'],
        width=500,
        height=400,
        color_index=2,
        colorbar=True,
    )

    track_map = track.opts(projection=ccrs.SouthPolarStereo()).opts(
        color="black")

    return (gf.land * gf.coastline * track_map * point_map_v1).opts(
        title=vname, width=options["figsize"][0], height=options["figsize"][1])
コード例 #26
0
# segmentation Mariangela's data
import os
import time
import pickle
import warnings
import scipy.stats
import numpy as np
import pandas as pd
import caiman as cm
import seaborn as sns
import holoviews as hv
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import utils_mari as ut
hv.extension('matplotlib')

# Do the segmentation with agonia
data_path = '/media/pedro/DATAPART1/Mariangela/20200313/area02_0002-1561'  #folder where the tif movie and the median projection are
data_name, median_projection, boxes_path = ut.get_files_names(data_path)
ut.agonia_detect(data_path, data_name, median_projection,
                 multiplier=.7)  #boxes are saved as pickle files

# extract and save traces
agonia_th = .2  #choose a threshold of confidence for the detected boxes
traces = ut.traces_extraction_AGONIA(data_path, agonia_th)
#save results
df = pd.DataFrame(traces)
df.to_hdf(os.path.join(data_path,
                       os.path.splitext(data_name[0])[0] + '_traces.hdf5'),
          key='df',
          mode='w')
コード例 #27
0
# %%
from fractions import Fraction
from importlib import reload

import holoviews as hv
import numpy as np
from holoviews import opts

import hw2

hv.extension('bokeh', 'matplotlib')
opts.defaults(opts.Curve(width=650))

conf = np.loadtxt('60.txt')
conf100, conf200 = np.loadtxt('100.txt'), np.loadtxt('200.txt')

# %%
deltas = [Fraction(1, 6), Fraction(1, 3), 1, 2, 3]
E, E_corr, err, acc_rate = hw2.point_b(conf, np.array(deltas, np.float))
# yapf: disable
print('delta values (in units of d):', [str(d) for d in deltas],
      'means and errors:', E.mean(1), err, 'acceptance ratios:', acc_rate,
      sep='\n')
E_plot = hv.NdOverlay({delta: hv.Curve(E[i, ::40])
                       for i, delta in enumerate(deltas)}).redim(x='t', y='E')
acc_plot = hv.Curve((np.array(deltas, np.float), acc_rate))
E_corr_plot = hv.NdOverlay({delta: hv.Curve(E_corr[i, :300])
                            for i, delta in enumerate(deltas)})
err_plot = hv.Curve((list(map(float, deltas)), err))
# yapf: enable
コード例 #28
0
def test_holoview():
    import holoviews as hv
    hv.extension('bokeh')

    model = Generate()
    alloc = Amounts() + Simulation()
    exec = sim.engine.BasicExecution(model, alloc)

    tr = model['grow', 'const', 'alt']

    ht = tr.take(6).to(sim.trace.Holotrace)

    assert 'Holotrace' in str(ht)
    assert str(ht.trace) in str(ht)
    assert repr(ht.trace) in repr(ht)

    assert list(ht.data.columns) == ['grow', 'const', 'alt']
    assert list(ht.data.index.names) == ['trace', 'sample']

    dm = ht.plot(hv.Curve)
    hv.render(dm)  # trigger rendering, so dynamic map gets filled

    over, = dm
    assert len(over) == 6 * 3
    crv, *_ = over

    assert isinstance(dm, hv.DynamicMap)
    assert isinstance(over, hv.NdOverlay)
    assert isinstance(crv, hv.Curve)
    assert crv.kdims == [hv.Dimension('trace')]
    assert crv.vdims == [hv.Dimension('value')]

    bf = ht.buffer
    assert len(bf.data) == 6
    with exec.trace(ht):
        exec.run(n_steps=2)
    assert ht.buffer == bf
    assert len(bf.data) == 3 * 6

    ht = tr.take(6).to(sim.trace.Holotrace, skip=4, batch=5)
    with exec.trace(ht):
        exec.run()
        print(ht.traces)
        print(ht.data)
        assert ht.data.shape == (6 * 5, 3)

    assert ht.data.shape == (6 * 6, 3)

    ht = tr.take(6).to(sim.trace.Holotrace, skip=4, batch=100, timeout=10)
    with exec.trace(ht):
        exec.run()
        assert ht.data.shape == (0, 3)

    assert ht.data.shape == (6 * 6, 3)

    ht = tr.take(6).to(sim.trace.Holotrace, skip=4, batch=100, timeout=.001)
    with exec.trace(ht):
        exec.run()
        assert ht.data.shape[0] > 0

    assert ht.data.shape == (6 * 6, 3)
コード例 #29
0
import pandas as pd
import hvplot.pandas
import holoviews as hv
from holoviews import opts
from holoviews import dim

hv.extension("bokeh")


def plot_stats_distribution(data: pd.DataFrame, plot_cols: list) -> hv.Layout:
    hist_plots = {}
    for col in plot_cols:
        hist_plots[col] = data[col].hvplot.hist(
            normed=True) * data[col].hvplot.kde()
    stats_distribution = hv.Layout(list(hist_plots.values())).cols(1)
    return stats_distribution


def plot_player_ratings_scatter(
    player_ratings: pd.DataFrame,
    x_col: str,
    y_col: str,
    color_col: str = "overall",
    size_col: str = "overall",
    **plot_opts,
) -> hv.Overlay:
    scatter_base = (player_ratings.reset_index().hvplot.scatter(
        x=x_col,
        y=y_col,
        c=color_col,
        hover_cols=["name", "overall"],
コード例 #30
0
ファイル: notebook.py プロジェクト: douglas-raillard-arm/lisa
_backends = ['plotly', 'bokeh', 'matplotlib']

# If the user selected a backend already, use it as defaults by activating it
# last
_curr_backend = hv.Store.current_backend
if _curr_backend:
    _backends.remove(_curr_backend)
    _backends.append(_curr_backend)

for backend in _backends:
    try:
        importlib.import_module(backend)
    except Exception:
        pass
    else:
        hv.extension(backend)

COLOR_CYCLE = [
    '#377eb8', '#ff7f00', '#4daf4a', '#f781bf', '#a65628', '#984ea3',
    '#999999', '#e41a1c', '#dede00'
]
"""
Colorblind-friendly cycle, see https://gist.github.com/thriveth/8560036
"""

plt.rcParams['axes.prop_cycle'] = make_cycler(color=COLOR_CYCLE)


class WrappingHBox(widgets.HBox):
    """
    HBox that will overflow on multiple lines if the content is too large to
コード例 #31
0
ファイル: main.py プロジェクト: snowind/pyviz
import parambokeh
import param

from colorcet import cm_n, fire

from bokeh.models import Slider, Button
from bokeh.layouts import layout
from bokeh.io import curdoc
from bokeh.models import WMTSTileSource

from holoviews.operation.datashader import datashade, aggregate, shade


shade.cmap = fire

hv.extension('bokeh')
renderer = hv.renderer('bokeh').instance(mode='server')

# Load data
ddf = dd.read_parquet(os.path.join(os.path.dirname(__file__),'..','..','..','data','nyc_taxi_wide.parq')).persist()

from bokeh.models import WMTSTileSource
url = 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{Z}/{Y}/{X}.jpg'
wmts = gv.WMTS(WMTSTileSource(url=url))

stream = hv.streams.Stream.define('HourSelect', hour=0)()
points = hv.Points(ddf, kdims=['dropoff_x', 'dropoff_y'])
dmap = hv.util.Dynamic(points, operation=lambda obj, hour: obj.select(dropoff_hour=hour).relabel('Hour of Day: %d' % hour),
                       streams=[stream])

# Apply aggregation
コード例 #32
0
def plot():
    #################################################################333333
    # Final plot.
    # High Significant

    from shapely.geometry import Point, Polygon
    import pandas as pd

    import numpy as np
    import geoviews as gv

    import bokeh

    import panel as pn

    import holoviews as hv
    from holoviews import opts
    from holoviews.operation.datashader import datashade, rasterize
    hv.extension("bokeh")

    import math

    from bokeh.models import HoverTool

    from scipy.interpolate import griddata
    from scipy.io import loadmat

    import datetime

    from dateutil.parser import parse

    from datetime import timedelta
    import time
    strtt = time.time()

    datamat = loadmat('IOEC_ECM_noDA_20190703_masked.mat')
    # datamat = loadmat('IOEC_ECM_DA_20191121_masked.mat')

    Xp = datamat['Xp']
    Yp = datamat['Yp']

    # Xp=np.where(((Xp>= 85) & (Xp<=88) & (Yp>=19) & (Yp<=22)),Xp,88)
    # Yp=np.where(((Xp>= 85) & (Xp<=88) & (Yp>=19) & (Yp<=22)),Yp,22)

    strt = datetime.datetime(2019, 7, 4, 0, 0)
    end = datetime.datetime(2019, 1, 13, 0, 0)
    from bokeh.models.callbacks import CustomJS

    def perdelta(strt, end, delta):
        curr = strt
        while curr < end:
            yield curr
            curr += delta

    # Read element file

    tri_new = pd.read_csv('fort.ele',
                          delim_whitespace=True,
                          names=('A', 'B', 'C', 'D'),
                          usecols=[1, 2, 3],
                          skiprows=1,
                          dtype={'D': np.int})

    dateList = []

    def plotthis(datetime, regions='Whole_East_Coast'):
        dat = datetime
        # datetostr=result.strftime("%Y%b%d%H")
        dt = parse(str(dat))
        yr = dt.year
        mn = dt.month
        d = dt.day
        hr = dt.hour
        mi = dt.minute

        if hr < 10:

            hr = '0' + str(hr)
        else:
            d = str(d)
            hr = str(hr)
        if int(d) < 10:
            d = '0' + str(d)
        else:
            d = str(d)
        varname = 'Steepn_' + str(yr) + '0' + str(mn) + str(
            d) + '_' + hr + '0000'

        x = Xp.flatten()
        y = Yp.flatten()
        z = datamat[varname]

        if regions is 'Odisha':
            z = np.where(((Xp >= 85) & (Xp <= 88) & (Yp >= 19) & (Yp <= 22)),
                         z, np.nan).flatten()
        elif regions is 'Andra_Pradesh':
            z = np.where(((Xp >= 79) & (Xp <= 85) & (Yp >= 13) & (Yp <= 19)),
                         z, np.nan).flatten()
        elif regions is 'Tamil_Nadu':
            z = np.where(((Xp >= 77) & (Xp <= 83) & (Yp >= 7) & (Yp <= 14)), z,
                         np.nan).flatten()
        elif regions is 'Whole_East_Coast':
            z = z.flatten()

        else:
            #         data = get_data4region(data,**odisha)
            z = z.flatten()

        # z = z.flatten()
        Longitude = x
        Latitude = y
        HS = z

        pts = np.stack((Longitude, Latitude, HS)).T
        verts = pd.DataFrame(np.stack((Longitude, Latitude, HS)).T,
                             columns=['Longitude', 'Latitude', 'HS'])

        # openStreet Background.
        tri_sub = tri_new.apply(lambda x: x - 1)
        ggpoints = gv.Points(verts, vdims=['HS'])
        ggsubraster = rasterize(gv.TriMesh((tri_sub, gv.Points(verts))))

        tri = gv.TriMesh((tri_sub, gv.Points(verts)))

        return tri

    allplot = {
        (k.strftime("%Y-%m-%d %H:%M:%S"), r): plotthis(k, r)
        for k in perdelta(strt, strt + timedelta(days=9), timedelta(hours=3))
        for r in ['Odisha', 'Andra_Pradesh', 'Whole_East_Coast', 'Tamil_Nadu']
    }
    # allplot2={(k.strftime("%Y-%m-%d %H:%M:%S"),r):plotsecond(k,r)for k in perdelta(strt, strt + timedelta(days=1), timedelta(hours=18)) for r in ['Odisha','Andra_Pradesh','Whole_East_Coast','Tamil_Nadu']}

    df_div = hv.Div("""
        <figure>
        <img src="https://i.ibb.co/S0t5GWb/imglogo.png" height='80' width='90' vspace='-10'>
        """)

    df_div1 = hv.Div("""
        &nbsp<center><b><p style="color:#B22222";font-size:80px;font-family:Times new roman><h1 style=font-size:20px;margin-left:2.5em;margin-top:-1em;color:#B22222>Indian National Center for Ocean Information Services<br />
        (INCOIS)</h1></p></b></center>

        """)
    df_div2 = hv.Div("""
            <html>
            <head>
            <style>
            input[type=button], input[type=submit], input[type=reset] {
              background-color: #C6E2FF;
              color: DARKVIOLET;
              padding: 10px 22px;
              text-decoration: none;
              margin: 4px 2px;
              cursor: pointer;
              font-weight: bold;
              font-size: 15px;
              border: 2px solid light slateblue
            }
            </style>
            </head>
            <body>

            <input type="button" value=" PRINT " onClick="window.print()">


            </body>
            </html>

            """)

    colorbar_opts = {
        'major_label_overrides': {
            0.00: '0.00',
            0.02: '0.02',
            0.04: '0.04',
            0.06: '0.06',
            0.08: '0.08',
            0.10: '0.10',
            0.12: '0.12',
            0.14: '> 0.14',
            0.15: '0.15',
            0.16: ' ',
            0.17: '>0.17',
        },
        'major_label_text_align': 'left',
        'major_label_text_font_style': 'bold',
    }
    levels = [0.00, 0.02, 0.04, 0.06, 0.08, 0.10, 0.12, 0.14, 0.15, 0.16, 0.17]

    def disable_logo(plot, element):
        plot.state.toolbar.logo = None

    hv.plotting.bokeh.ElementPlot.finalize_hooks.append(disable_logo)

    def plot_limits(plot, element):
        plot.handles['x_range'].min_interval = 100
        plot.handles['x_range'].max_interval = 55000000
        # 3000000
        plot.handles['y_range'].min_interval = 500
        plot.handles['y_range'].max_interval = 900000

    opts = dict(
        width=700,
        height=700,
        logz=False,
        logx=False,
        logy=False,
        responsive=True,
        active_tools=['wheel_zoom'],
        tools=['save', 'wheel_zoom', 'hover'],
        hooks=[plot_limits, disable_logo],
        colorbar=True,
        color_levels=15,
        colorbar_opts=colorbar_opts,
        cmap=[
            '#000080',
            '#0000cd',
            '#0008ff',
            '#004cff',
            '#0090ff',
            '#00d4ff',
            '#29ffce',
            '#60ff97',
            '#97ff60',
            '#ceff29',
            '#ffe600',
            '#ffa700',
            # '#ff6800',
            '#ff2900',
            '#cd0000',
            '#800000',
        ],
        clim=(0.00, 0.15),
        title="\t\t\t\t\t\t\t\t Wave Steepness  . ",
        fontsize={
            'title': 18,
            'xlabel': 15,
            'ylabel': 15,
            'ticks': 12
        })

    tiles = gv.tile_sources.Wikipedia
    tiles = gv.tile_sources.Wikipedia
    hmap1 = hv.HoloMap(
        allplot, kdims=['Select Date and Time :', 'Select Indian States'])
    # hmap2 = hv.HoloMap(allplot2, kdims=['Date and Time :','region'])
    logo1 = hv.RGB.load_image("https://i.ibb.co/7VXRPCS/logo1.png")

    def absolute_position(plot, element):
        glyph = plot.handles['glyph']
        x_range, y_range = plot.handles['x_range'], plot.handles['y_range']
        glyph.dh_units = 'screen'
        glyph.dw_units = 'screen'
        glyph.dh = 60
        glyph.dw = 90
        #     x_range.start=+85
        #     y_range.start=+20
        glyph.x = x_range.start
        glyph.y = y_range.start
        xcode = CustomJS(code="glyph.x = cb_obj.start", args={'glyph': glyph})
        plot.handles['x_range'].js_on_change('start', xcode)
        ycode = CustomJS(code="glyph.y = cb_obj.start", args={'glyph': glyph})
        plot.handles['y_range'].js_on_change('start', ycode)

    # finalplot=tiles*rasterize(hmap1.redim.range(Latitude=(13, 19), Longitude=(79, 85))).options(**opts)*hmap2

    dd = df_div.opts(width=90, height=80)
    dd1 = df_div1.opts(width=600, height=90)
    dd2 = df_div2.opts(width=100, height=10)

    finalplot = pn.Column(
        pn.Row(dd, dd1),
        tiles * rasterize(hmap1).options(**opts) *
        logo1.opts(hooks=[absolute_position], apply_ranges=False))
    # print("--- %s seconds ---" % (time.time() - strtt))
    from bokeh.embed import components
    from bokeh.resources import CDN
    from bokeh.io import curdoc
    doc = curdoc()
    script, div = components(finalplot.get_root(doc))
    cdn_js0 = CDN.js_files[0]
    cdn_js = CDN.js_files[1]
    cdn_css = CDN.css_files
    print("cdn_js:", cdn_js)
    print("cdn_css", cdn_css)

    return render_template("plot.html",
                           script=script,
                           div=div,
                           cdn_css=cdn_css,
                           cdn_js=cdn_js,
                           cdn_js0=cdn_js0)