コード例 #1
0
 def test_rasterize_points(self):
     points = Points([(0.2, 0.3), (0.4, 0.7), (0, 0.99)])
     img = rasterize(points, dynamic=False,  x_range=(0, 1), y_range=(0, 1),
                     width=2, height=2)
     expected = Image(([0.25, 0.75], [0.25, 0.75], [[1, 0], [2, 0]]),
                      vdims=['Count'])
     self.assertEqual(img, expected)
コード例 #2
0
 def test_rasterize_curve(self):
     curve = Curve([(0.2, 0.3), (0.4, 0.7), (0.8, 0.99)])
     expected = Image(([0.25, 0.75], [0.25, 0.75], [[1, 0], [1, 1]]),
                      vdims=['Count'])
     img = rasterize(curve, dynamic=False,  x_range=(0, 1), y_range=(0, 1),
                     width=2, height=2)
     self.assertEqual(img, expected)
コード例 #3
0
 def test_rasterize_path(self):
     path = Path([[(0.2, 0.3), (0.4, 0.7)], [(0.4, 0.7), (0.8, 0.99)]])
     expected = Image(([0.25, 0.75], [0.25, 0.75], [[1, 0], [2, 1]]),
                      vdims=['Count'])
     img = rasterize(path, dynamic=False,  x_range=(0, 1), y_range=(0, 1),
                     width=2, height=2)
     self.assertEqual(img, expected)
コード例 #4
0
 def test_rasterize_trimesh_no_vdims(self):
     simplices = [(0, 1, 2), (3, 2, 1)]
     vertices = [(0., 0.), (0., 1.), (1., 0), (1, 1)]
     trimesh = TriMesh((simplices, vertices))
     img = rasterize(trimesh, width=3, height=3, dynamic=False)
     image = Image(np.array([[True, True, True], [True, True, True], [True, True, True]]),
                   bounds=(0, 0, 1, 1), vdims='Any')
     self.assertEqual(img, image)
コード例 #5
0
 def test_rasterize_trimesh_with_vdims_as_wireframe(self):
     simplices = [(0, 1, 2, 0.5), (3, 2, 1, 1.5)]
     vertices = [(0., 0.), (0., 1.), (1., 0), (1, 1)]
     trimesh = TriMesh((simplices, vertices), vdims=['z'])
     img = rasterize(trimesh, width=3, height=3, aggregator='any', interpolation=None, dynamic=False)
     image = Image(np.array([[True, True, True], [True, True, True], [True, True, True]]),
                   bounds=(0, 0, 1, 1), vdims='Any')
     self.assertEqual(img, image)
コード例 #6
0
 def test_rasterize_trimesh_node_explicit_vdim(self):
     simplices = [(0, 1, 2, 0.5), (3, 2, 1, 1.5)]
     vertices = [(0., 0., 1), (0., 1., 2), (1., 0, 3), (1, 1, 4)]
     trimesh = TriMesh((simplices, Points(vertices, vdims=['node_z'])), vdims=['z'])
     img = rasterize(trimesh, width=3, height=3, dynamic=False, aggregator=ds.mean('z'))
     image = Image(np.array([[1.5, 1.5, np.NaN], [0.5, 1.5, np.NaN], [np.NaN, np.NaN, np.NaN]]),
                   bounds=(0, 0, 1, 1))
     self.assertEqual(img, image)
コード例 #7
0
 def test_rasterize_trimesh_zero_range(self):
     simplices = [(0, 1, 2, 0.5), (3, 2, 1, 1.5)]
     vertices = [(0., 0.), (0., 1.), (1., 0), (1, 1)]
     trimesh = TriMesh((simplices, vertices), vdims=['z'])
     img = rasterize(trimesh, x_range=(0, 0), height=2, dynamic=False)
     image = Image(([], [0.25, 0.75], np.zeros((2, 0))),
                   bounds=(0, 0, 0, 1), xdensity=1)
     self.assertEqual(img, image)
コード例 #8
0
 def test_rasterize_trimesh_vertex_vdims(self):
     simplices = [(0, 1, 2), (3, 2, 1)]
     vertices = [(0., 0., 1), (0., 1., 2), (1., 0., 3), (1., 1., 4)]
     trimesh = TriMesh((simplices, Points(vertices, vdims='z')))
     img = rasterize(trimesh, width=3, height=3, dynamic=False)
     image = Image(np.array([[2., 3., np.NaN], [1.5, 2.5, np.NaN], [np.NaN, np.NaN, np.NaN]]),
                   bounds=(0, 0, 1, 1), vdims='z')
     self.assertEqual(img, image)
コード例 #9
0
 def test_rasterize_ndoverlay(self):
     ds = Dataset([(0.2, 0.3, 0), (0.4, 0.7, 1), (0, 0.99, 2)], kdims=['x', 'y', 'z'])
     ndoverlay = ds.to(Points, ['x', 'y'], [], 'z').overlay()
     expected = Image(([0.25, 0.75], [0.25, 0.75], [[1, 0], [2, 0]]),
                      vdims=['Count'])
     img = rasterize(ndoverlay, dynamic=False, x_range=(0, 1), y_range=(0, 1),
                     width=2, height=2)
     self.assertEqual(img, expected)
コード例 #10
0
 def test_rasterize_trimesh_string_aggregator(self):
     simplices = [(0, 1, 2, 0.5), (3, 2, 1, 1.5)]
     vertices = [(0., 0.), (0., 1.), (1., 0), (1, 1)]
     trimesh = TriMesh((simplices, vertices), vdims=['z'])
     img = rasterize(trimesh, width=3, height=3, dynamic=False, aggregator='mean')
     image = Image(np.array([[1.5, 1.5, np.NaN], [0.5, 1.5, np.NaN], [np.NaN, np.NaN, np.NaN]]),
                   bounds=(0, 0, 1, 1))
     self.assertEqual(img, image)
コード例 #11
0
 def test_aggregate_dt_xaxis_constant_yaxis(self):
     df = pd.DataFrame({'y': np.ones(100)}, index=pd.date_range('1980-01-01', periods=100, freq='1T'))
     img = rasterize(Curve(df), dynamic=False)
     xs = np.array(['1980-01-01T00:16:30.000000', '1980-01-01T00:49:30.000000',
                    '1980-01-01T01:22:30.000000'], dtype='datetime64[us]')
     ys = np.array([])
     bounds = (np.datetime64('1980-01-01T00:00:00.000000'), 1.0,
               np.datetime64('1980-01-01T01:39:00.000000'), 1.0)
     expected = Image((xs, ys, np.empty((0, 3))), ['index', 'y'], 'Count',
                      xdensity=1, ydensity=1, bounds=bounds)
     self.assertEqual(img, expected)
コード例 #12
0
 def test_aggregate_dt_xaxis_constant_yaxis(self):
     df = pd.DataFrame({'y': np.ones(100)},
                       index=pd.date_range('1980-01-01',
                                           periods=100,
                                           freq='1T'))
     img = rasterize(Curve(df), dynamic=False, width=3)
     xs = np.array([
         '1980-01-01T00:16:30.000000', '1980-01-01T00:49:30.000000',
         '1980-01-01T01:22:30.000000'
     ],
                   dtype='datetime64[us]')
     ys = np.array([])
     bounds = (np.datetime64('1980-01-01T00:00:00.000000'), 1.0,
               np.datetime64('1980-01-01T01:39:00.000000'), 1.0)
     expected = Image((xs, ys, np.empty((0, 3))), ['index', 'y'],
                      'Count',
                      xdensity=1,
                      ydensity=1,
                      bounds=bounds)
     self.assertEqual(img, expected)
コード例 #13
0
    def generate(self, bounds_polygon: sg.Polygon, raster_shape: Tuple[int, int], from_cache: bool = False,
                 hour: int = 8, resolution: float = 20, **kwargs) -> \
            Tuple[Geometry, np.ndarray, gpd.GeoDataFrame]:
        from holoviews.operation.datashader import rasterize
        import geoviews as gv
        import datashader as ds
        import colorcet

        relative_variation = self.relative_variations_flat[hour]

        roads_gdf = self._interpolate_traffic_counts(bounds_polygon)
        roads_gdf['population_per_hour'] = roads_gdf[
            'population_per_hour'] * relative_variation
        roads_gdf['population'] = roads_gdf['population_per_hour'] / 3600
        roads_gdf['density'] = roads_gdf['population'] / (
            roads_gdf.geometry.area * 1e-6)  # km^2
        ln_mask = roads_gdf['density'] > 0
        roads_gdf.loc[ln_mask, 'ln_density'] = np.log(roads_gdf.loc[ln_mask,
                                                                    'density'])
        roads_gdf['ln_density'].fillna(0, inplace=True)
        roads_gdf = roads_gdf.set_crs('EPSG:27700').to_crs('EPSG:4326')

        points = gv.Polygons(
            roads_gdf,
            kdims=['Longitude', 'Latitude'],
            vdims=['population_per_hour', 'ln_density', 'density']).opts(
                # colorbar=True,
                cmap=colorcet.CET_L18,
                color='ln_density',
                line_color='ln_density')
        bounds = bounds_polygon.bounds
        raster = rasterize(points,
                           aggregator=ds.mean('density'),
                           width=raster_shape[0],
                           height=raster_shape[1],
                           x_range=(bounds[1], bounds[3]),
                           y_range=(bounds[0], bounds[2]),
                           dynamic=False)
        raster_grid = np.copy(
            list(raster.data.data_vars.items())[0][1].data.astype(np.float))
        return points, raster_grid, gpd.GeoDataFrame(roads_gdf)
コード例 #14
0
    def view_map(self):
        # print('view_map method')

        if self.adh_mod.mesh.elevation_toggle:
            elevation = rasterize(self.adh_mod.mesh.tri_mesh,
                                  aggregator=ds.mean('z'),
                                  precompute=True).apply.opts(
                                      opts.Image(
                                          cmap=self.cmap_opts.colormap,
                                          clim=self.display_range.color_range,
                                          height=self.map_height,
                                          width=self.map_width))
        else:
            elevation = Curve([]).opts(height=self.map_height,
                                       width=self.map_width)

        # return self.adh_mod.mesh.view_bathy() * self.adh_mod.mesh.view_elements(line_color='yellow') * base_map * self.view_scatter()

        return elevation * self.adh_mod.mesh.view_elements(
            line_color='yellow') * hv.DynamicMap(
                self.adh_mod.wmts.view) * self.view_scatter()
コード例 #15
0
 def test_segments_aggregate_sum(self, instance=False):
     segments = Segments([(0, 1, 4, 1, 2), (1, 0, 1, 4, 4)], vdims=['value'])
     if instance:
         agg = rasterize.instance(
             width=10, height=10, dynamic=False, aggregator='sum'
         )(segments, width=4, height=4)
     else:
         agg = rasterize(
             segments, width=4, height=4, dynamic=False, aggregator='sum'
         )
     xs = [0.5, 1.5, 2.5, 3.5]
     ys = [0.5, 1.5, 2.5, 3.5]
     na = np.nan
     arr = np.array([
         [na, 4, na, na],
         [2 , 6, 2 , 2 ],
         [na, 4, na, na],
         [na, 4, na, na]
     ])
     expected = Image((xs, ys, arr), vdims='value')
     self.assertEqual(agg, expected)
コード例 #16
0
    def test_rasterize_regrid_and_spikes_overlay(self):
        img = Image(([0.5, 1.5], [0.5, 1.5], [[0, 1], [2, 3]]))
        spikes = Spikes([(0.5, 0.2), (1.5, 0.8), ], vdims='y')

        expected_regrid = Image(([0.25, 0.75, 1.25, 1.75],
                                 [0.25, 0.75, 1.25, 1.75],
                                 [[0, 0, 1, 1],
                                  [0, 0, 1, 1],
                                  [2, 2, 3, 3],
                                  [2, 2, 3, 3]]))
        spikes_arr = np.array([[0, 1, 0, 1],
                               [0, 1, 0, 1],
                               [0, 0, 0, 0],
                               [0, 0, 0, 0]])
        expected_spikes = Image(([0.25, 0.75, 1.25, 1.75],
                                 [0.25, 0.75, 1.25, 1.75], spikes_arr), vdims='count')
        overlay = img * spikes
        agg = rasterize(overlay, width=4, height=4, x_range=(0, 2), y_range=(0, 2),
                        spike_length=0.5, upsample=True, dynamic=False)
        self.assertEqual(agg.Image.I, expected_regrid)
        self.assertEqual(agg.Spikes.I, expected_spikes)
コード例 #17
0
 def test_segments_aggregate_dt_count(self):
     segments = Segments([
         (0, dt.datetime(2016, 1, 2), 4, dt.datetime(2016, 1, 2)),
         (1, dt.datetime(2016, 1, 1), 1, dt.datetime(2016, 1, 5))
     ])
     agg = rasterize(segments, width=4, height=4, dynamic=False)
     xs = [0.5, 1.5, 2.5, 3.5]
     ys = [
         np.datetime64('2016-01-01T12:00:00'), np.datetime64('2016-01-02T12:00:00'),
         np.datetime64('2016-01-03T12:00:00'), np.datetime64('2016-01-04T12:00:00')
     ]
     arr = np.array([
         [0, 1, 0, 0],
         [1, 2, 1, 1],
         [0, 1, 0, 0],
         [0, 1, 0, 0]
     ])
     bounds = (0.0, np.datetime64('2016-01-01T00:00:00'),
               4.0, np.datetime64('2016-01-05T00:00:00'))
     expected = Image((xs, ys, arr), bounds=bounds, vdims='count')
     self.assertEqual(agg, expected)
コード例 #18
0
    def generate(self, bounds_polygon: sg.Polygon, raster_shape: Tuple[int, int], from_cache: bool = False, **kwargs) -> \
            Tuple[Geometry, np.ndarray, gpd.GeoDataFrame]:
        import geoviews as gv
        from holoviews.operation.datashader import rasterize

        bounds = bounds_polygon.bounds
        polys_df = self.query_osm_polygons(bounds_polygon)
        if polys_df.empty:
            return None
        if self.buffer_dist > 0:
            polys_df.geometry = polys_df.to_crs('EPSG:27700').buffer(self.buffer_dist).to_crs('EPSG:4326')
        polys = gv.Polygons(polys_df).opts(alpha=0.8, color=self._colour, line_color=self._colour)
        raster = rasterize(polys, width=raster_shape[0], height=raster_shape[1],
                           x_range=(bounds[1], bounds[3]), y_range=(bounds[0], bounds[2]), dynamic=False)
        raster_grid = np.copy(list(raster.data.data_vars.items())[0][1].data.astype(np.float))
        if self.blocking:
            raster_grid[raster_grid != 0] = -1
        else:
            raster_grid = None

        return polys, raster_grid, polys_df
コード例 #19
0
 def test_polygon_rasterize_mean_agg(self):
     poly = Polygons([{
         'x': [0, 1, 2],
         'y': [0, 1, 0],
         'z': 2.4
     }, {
         'x': [0, 0, 1],
         'y': [0, 1, 1],
         'z': 3.6
     }],
                     vdims='z')
     agg = rasterize(poly,
                     width=4,
                     height=4,
                     dynamic=False,
                     aggregator='mean')
     xs = [0.25, 0.75, 1.25, 1.75]
     ys = [0.125, 0.375, 0.625, 0.875]
     arr = np.array([[2.4, 2.4, 2.4, 2.4], [3.6, 2.4, 2.4, np.nan],
                     [3.6, 2.4, 2.4, np.nan], [3.6, 3.6, np.nan, np.nan]])
     expected = Image((xs, ys, arr), vdims='z')
     self.assertEqual(agg, expected)
コード例 #20
0
ファイル: viz.py プロジェクト: manuflores/time_machine
def make_gridplot_hv(df, col_list, color_by, n_samples, rasterize_ = True):
    
    """
    Returns a scatterplot gridmatrix for all pairwise combinations in col_list. 
    Example: http://holoviews.org/gallery/demos/bokeh/iris_density_grid.html

    Params 
    ------

    df (pd.DataFrame)
        DataFrame with samples 

    col_list (list)

    color_by (str)

    n_samples (int)

    rasterize_(bool)

    Returns 
    -------


    """

    df_ = df.sample(n_samples)[col_list + [color_by]]

    if color_by is not None: 
        ds = hv.Dataset(df_).groupby(color_by).overlay().opts(width = 150, height = 150)
    else: 
        ds = hv.Dataset(df_)
    

    if rasterize_: 
        return rasterize(gridmatrix(ds))
    else :
        return gridmatrix(ds, diagonal = hv.Distribution)
コード例 #21
0
def add_obstacles(bounds, obstacles, raster_shape):
    import geoviews as gv
    from holoviews.operation.datashader import rasterize
    import geopandas as gpd

    if type(obstacles) is gpd.GeoDataFrame:
        df = obstacles
    else:
        df = gpd.GeoDataFrame({'geometry': obstacles}).set_crs('EPSG:4326')
    bounded_df = df.cx[bounds[1]:bounds[3], bounds[0]:bounds[2]]
    bounded_df['z'] = np.inf

    polys = gv.Polygons(bounded_df, vdims=['z'])
    raster = rasterize(polys,
                       width=raster_shape[1],
                       height=raster_shape[0],
                       x_range=(bounds[1], bounds[3]),
                       y_range=(bounds[0], bounds[2]),
                       dynamic=False)
    raster_grid = np.flipud(
        np.copy(
            list(raster.data.data_vars.items())[0][1].data.astype(np.float)))
    return remove_raster_nans(raster_grid)
コード例 #22
0
def graph():
    for d in getattr(xrData, variable).dims:
        # WORKAROUND because Holoview is not working with a kdim with name "height"
        # See issue
        if d == "height":
            freedims.append("hi")
            continue
        if d != "ncells":
            freedims.append(d)

    ranges = {}
    for d in freedims:
        # WORKAROUND because Holoview is not working with a kdim with name "height"
        # See issue
        if d != "hi":
            ranges[d] = (0, len(getattr(getattr(xrData, variable), d)))
        else:
            ranges[d] = (0, len(getattr(getattr(xrData, variable), "height")))
    dm = hv.DynamicMap(triGraph, kdims=freedims).redim.range(**ranges)
    print("DynamicMap:" + str(dm))
    cm = "Magma"
    if slCMap is not None:
        cm = slCMap.value
    return rasterize(dm).opts(cmap=cm, colorbar=True)
コード例 #23
0
    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
コード例 #24
0
                '#004cff',
                '#0090ff',
                '#00d4ff',
                '#29ffce',
                '#60ff97',
                '#97ff60',
                '#ceff29',
                '#ffe600',
                '#ffa700',
                '#ff2900',
                '#cd0000',
                '#800000',
            ],
            clim=(0, 15),
            title="\t\t\t\t\t\t\t\t\t Mean Wave Period (s) ",
            fontsize={
                'title': 18,
                'xlabel': 15,
                'ylabel': 15,
                'ticks': 12
            })

tiles = gv.tile_sources.Wikipedia
hmap1 = hv.HoloMap(allplot, kdims=['Select D :', 'Select State'])

dd = df_div.opts(width=70, height=70)
finalplot = pn.Column(
    pn.Row(dd),
    tiles * rasterize(hmap1).options(**opts) *
    logo1.opts(hooks=[absolute_position], apply_ranges=False))
finalplot
コード例 #25
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)
コード例 #26
0
client = Client(cluster)

var = 'streamflow'

ds[var].nbytes / 1e9

var_mean = ds[var].mean(dim='time').persist()
progress(var_mean)

df = var_mean.to_pandas().to_frame()

df = df.assign(latitude=df['latitude'].values)
df = df.assign(longitude=df['longitude'].values)
df.rename(columns={0: "transport"}, inplace=True)

from holoviews.operation.datashader import datashade, shade, dynspread, rasterize, spread

import cartopy.crs as ccrs

p = df.hvplot.points('longitude',
                     'latitude',
                     crs=ccrs.PlateCarree(),
                     c='transport',
                     colorbar=True,
                     size=14)
g = rasterize(p,
              aggregator='mean',
              x_sampling=0.02,
              y_sampling=0.02,
              width=500).opts(tools=['hover'])
コード例 #27
0
    def frames(self, var="depth", tiles=False, **kwargs):

        x = kwargs.get("x", self._obj.SCHISM_hgrid_node_x[:].values)
        y = kwargs.get("y", self._obj.SCHISM_hgrid_node_y[:].values)
        try:
            t = kwargs.get("t", self._obj.time.values)
        except:
            pass
        tes = kwargs.get("tri3",
                         self._obj.SCHISM_hgrid_face_nodes.values[:, :4])

        # sort out quads
        try:
            mask = np.isnan(tes)[:, 3]
            tr3 = tes[mask][:, :3]
            tr3_ = quads_to_tris(tes[~mask])
            if tr3_:
                tri3 = np.append(tr3, tr3_, axis=0).astype(int)
            else:
                tri3 = tr3.astype(int)
        except:
            tri3 = tes.astype(int)

        times = kwargs.get("times", self._obj.time.values)

        z = kwargs.get("z", self._obj[var].values[0, :].flatten())

        zmin = self._obj[var].values.min()
        zmax = self._obj[var].values.max()

        nodes = pd.DataFrame({
            "longitude": x,
            "latitude": y,
            "{}".format(var): z
        })
        elems = pd.DataFrame(tri3, columns=["a", "b", "c"])

        width = kwargs.get("width", 800)
        height = kwargs.get("height", 600)
        opts.defaults(opts.WMTS(width=width, height=height))

        tile = gv.WMTS("https://b.tile.openstreetmap.org/{Z}/{X}/{Y}.png")

        points = gv.operation.project_points(
            gv.Points(nodes, vdims=["{}".format(var)]))

        trimesh = gv.TriMesh((elems, points))

        def time_mesh(time):
            points.data[var] = self._obj[var].sel(time=time).values
            return gv.TriMesh((elems, points))  # , crs=ccrs.GOOGLE_MERCATOR)

        meshes = hv.DynamicMap(time_mesh,
                               kdims=["Time"]).redim.values(Time=times)

        imesh = rasterize(meshes, aggregator="mean").opts(cmap="viridis",
                                                          colorbar=True,
                                                          padding=0.1,
                                                          tools=["hover"],
                                                          clim=(zmin, zmax))

        if tiles:
            return hv.output(tile * imesh, holomap="scrubber", fps=1)
        else:
            return hv.output(imesh.opts(width=width, height=height),
                             holomap="scrubber",
                             fps=1)
コード例 #28
0
def _scatter(adata,
             x,
             y,
             condition,
             by=None,
             subsample='datashade',
             steps=40,
             keep_frac=0.2,
             seed=None,
             legend_loc='top_right',
             size=4,
             xlabel=None,
             ylabel=None,
             title=None,
             use_raw=True,
             hover=None,
             hover_width=10,
             hover_height=10,
             kde=None,
             density=None,
             density_size=150,
             jitter=None,
             perc=None,
             xlim=None,
             ylim=None,
             cmap=None,
             show_legend=True,
             plot_height=400,
             plot_width=400):

    _sentinel = object()

    def create_density_plots(df, density, kdims, cmap):
        cm = {}
        if density == 'all':
            dfs = {_sentinel: df}
        elif density == 'group':
            if 'z' not in df.columns:
                warnings.warn(
                    f'`density=\'groups\' was specified, but no group found. Did you specify `color=...`?'
                )
                dfs = {_sentinel: df}
            elif not is_categorical(df['z']):
                warnings.warn(
                    f'`density=\'groups\' was specified, but column `{condition}` is not categorical.'
                )
                dfs = {_sentinel: df}
            else:
                dfs = {k: v for k, v in df.groupby('z')}
                cm = cmap
        else:
            raise ValueError(
                f'Invalid `density` type: \'`{density}`\'. Possible values are `\'all\'`, `\'group\'`.'
            )
        # assumes x, y order in kdims
        return [
            hv.Overlay([
                hv.Distribution(df, kdims=dim).opts(color=cm.get(k, 'black'),
                                                    framewise=True)
                for k, df in dfs.items()
            ]) for dim in kdims
        ]

    assert keep_frac >= 0 and keep_frac <= 1, f'`keep_perc` must be in interval `[0, 1]`, got `{keep_frac}`.'

    adata_mraw = get_mraw(adata, use_raw)

    if subsample == 'uniform':
        cb_kwargs = {'steps': steps}
    elif subsample == 'density':
        cb_kwargs = {'size': int(keep_frac * adata.n_obs), 'seed': seed}
    else:
        cb_kwargs = {}

    categorical = False
    if condition is None:
        cmap = ['black'] * len(x) if subsample == 'datashade' else 'black'
    elif is_categorical(condition):
        categorical = True
        cmap = Sets1to3 if cmap is None else cmap
        cmap = odict(
            zip(condition.cat.categories, adata.uns.get(f'{by}_colors', cmap)))
    else:
        cmap = Viridis256 if cmap is None else cmap

    jitter_x, jitter_y = None, None
    if isinstance(jitter, (tuple, list)):
        assert len(
            jitter
        ) == 2, f'`jitter` must be of length `2`, found `{len(jitter)}`.'
        jitter_x, jitter_y = jitter
    elif jitter is not None:
        jitter_x, jitter_y = jitter, jitter

    if jitter_x is not None:
        x += np.random.normal(0, jitter_x, size=x.shape)
    if jitter_y is not None:
        y += np.random.normal(0, jitter_y, size=y.shape)

    data = {'x': x, 'y': y, 'z': condition}
    vdims = ['z']

    hovertool = None
    if hover is not None:
        for k, dt in hover.items():
            vdims.append(k)
            data[k] = dt
        hovertool = HoverTool(
            tooltips=[(key.capitalize(), f'@{key}') for key in (
                ['index'] if subsample == 'datashade' else hover.keys())])

    data = pd.DataFrame(data)
    if categorical:
        data['z'] = data['z'].astype('category')

    if not vdims:
        vdims = None

    if xlim is None:
        xlim = pad(*minmax(x))
    if ylim is None:
        ylim = pad(*minmax(y))

    kdims = [('x', 'x' if xlabel is None else xlabel),
             ('y', 'y' if ylabel is None else ylabel)]

    scatter = hv.Scatter(data, kdims=kdims, vdims=vdims).sort('z')
    scatter = scatter.opts(size=size, xlim=xlim, ylim=ylim)

    kde_plot= None if kde is None else \
            hv.Bivariate(scatter).opts(bandwidth=kde, show_legend=False, line_width=2)
    xdist, ydist = (None, None) if density is None else create_density_plots(
        data, density, kdims, cmap)

    if categorical:
        scatter = scatter.opts(cmap=cmap,
                               color='z',
                               show_legend=show_legend,
                               legend_position=legend_loc)
    elif 'z' in data:
        scatter = scatter.opts(cmap=cmap,
                               color='z',
                               clim=tuple(map(float, minmax(data['z'], perc))),
                               colorbar=True,
                               colorbar_opts={'width': 20})
    else:
        scatter = scatter.opts(color='black')

    legend = None
    if subsample == 'datashade':
        subsampled = dynspread(datashade(
            scatter,
            aggregator=(ds.count_cat('z') if categorical else ds.mean('z'))
            if vdims is not None else None,
            color_key=cmap,
            cmap=cmap,
            streams=[hv.streams.RangeXY(transient=True), hv.streams.PlotSize],
            min_alpha=255).opts(axiswise=True, framewise=True),
                               threshold=0.8,
                               max_px=5)
        if show_legend and categorical:
            legend = hv.NdOverlay({
                k: hv.Points([0, 0], label=str(k)).opts(size=0, color=v)
                for k, v in cmap.items()
            })
        if hover is not None:
            t = hv.util.Dynamic(rasterize(scatter, width=hover_width, height=hover_height, streams=[hv.streams.RangeXY],
                                          aggregator=ds.reductions.min('index')), operation=hv.QuadMesh)\
                                              .opts(tools=[hovertool], axiswise=True, framewise=True,
                                                    alpha=0, hover_alpha=0.25,
                                                    height=plot_height, width=plot_width)
            scatter = t * subsampled
        else:
            scatter = subsampled

    elif subsample == 'decimate':
        scatter = decimate(scatter,
                           max_samples=int(adata.n_obs * keep_frac),
                           streams=[hv.streams.RangeXY(transient=True)],
                           random_seed=seed)

    if legend is not None:
        scatter = (scatter * legend).opts(legend_position=legend_loc)

    if kde_plot is not None:
        scatter *= kde_plot

    scatter = scatter.opts(height=plot_height, width=plot_width)
    scatter = scatter.opts(hv.opts.Scatter(
        tools=[hovertool])) if hovertool is not None else scatter

    if xdist is not None and ydist is not None:
        scatter = (scatter << ydist.opts(width=density_size)) << xdist.opts(
            height=density_size)

    return scatter.opts(title=title if title is not None else '')
コード例 #29
0
def graph():
    dm = hv.DynamicMap(triGraph)
    cm = "Magma"
    return rasterize(dm).opts(cmap=cm, colorbar=True)
コード例 #30
0
nc = netCDF4.Dataset(url)
# Use gridgeo to get mesh from UGRID compliant dataset
u = ugrid(nc)
tris = pd.DataFrame(u['faces'].astype('int'), columns=['v0', 'v1', 'v2'])

# In[7]:

dpdown.observe(on_change)

# In[8]:

tiles = gv.WMTS(
    'https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{Z}/{Y}/{X}.jpg'
)
dynamic_trimesh = hv.DynamicMap(gen_trimesh, streams=[value_stream])
plot = tiles * rasterize(
    dynamic_trimesh, aggregator=ds.mean('z'), precompute=True)

# In[9]:

get_ipython().run_line_magic(
    'opts',
    "Image [colorbar=True clipping_colors={'NaN': (0, 0, 0, 0)}] (cmap=palettable.cubehelix.perceptual_rainbow_16.mpl_colormap)"
)
get_ipython().run_line_magic('opts', 'WMTS [width=700 height=400]')
plot

# In[10]:

display(dpdown)

# In[11]:
コード例 #31
0
ファイル: testdatashader.py プロジェクト: JulianDekker/DOVAL
 def test_aggregate_contours_without_vdim(self):
     contours = Contours([[(0.2, 0.3), (0.4, 0.7)], [(0.4, 0.7), (0.8, 0.99)]])
     img = rasterize(contours, dynamic=False)
     self.assertEqual(img.vdims, ['Count'])
コード例 #32
0
 def test_aggregate_contours_with_vdim(self):
     contours = Contours([[(0.2, 0.3, 1), (0.4, 0.7, 1)], [(0.4, 0.7, 2), (0.8, 0.99, 2)]], vdims='z')
     img = rasterize(contours, dynamic=False)
     self.assertEqual(img.vdims, ['z'])
コード例 #33
0
 def test_aggregate_zero_range_points(self):
     p = Points([(0, 0), (1, 1)])
     agg = rasterize(p, x_range=(0, 0), y_range=(0, 1), expand=False, dynamic=False)
     img = Image(([], [0.25, 0.75], np.zeros((2, 0))), bounds=(0, 0, 0, 1), xdensity=1, vdims=['Count'])
     self.assertEqual(agg, img)
コード例 #34
0
def rs_fMRI_plots(SBJ, RUN, WL_sec, corr_range):
    # Load rs fMRI data
    # -----------------
    file_name = SBJ + '_fanaticor_Craddock_T2Level_0200_wl' + str(
        WL_sec).zfill(
            3) + 's_ws002s_' + RUN + '_PCA_vk97.5.swcorr.pkl'  # Data file name
    data_path = osp.join('/data/SFIM_Vigilance/PRJ_Vigilance_Smk02/PrcsData',
                         SBJ, 'D02_Preproc_fMRI', file_name)  # Path to data
    data_df = pd.read_pickle(data_path).T  # Read data into pandas data frame
    num_samp = data_df.shape[0]  # Save number of samples as a varable

    # Load sleep segmenting data
    # --------------------------
    seg_path = osp.join(PRJDIR, 'Data', 'Samika_DSet02', 'Sleep_Segments',
                        SBJ + '_' + RUN + '_WL_' + str(WL_sec) +
                        'sec_Sleep_Segments.pkl')  # Path to segment data
    seg_df = pd.read_pickle(seg_path)  # Load segment data

    # Compute correlation and distance matrix
    # ---------------------------------------
    data_corr = np.corrcoef(data_df)  # Correlation matrix
    data_dist = pairwise_distances(data_df,
                                   metric='euclidean')  # Distance matrix

    # Compute distribution of correlation and distance matrix
    # -------------------------------------------------------
    triangle = np.mask_indices(num_samp, np.triu,
                               k=1)  # Top triangle mask for matricies
    corr_freq, corr_edges = np.histogram(
        np.array(data_corr)[triangle], 100
    )  # Compute histogram of top triangle of correlation matrix (100 bars)
    dist_freq, dist_edges = np.histogram(
        np.array(data_dist)[triangle],
        100)  # Compute histogram of top triangle of distance matrix (100 bars)

    # Create sleep segments plots
    # ---------------------------
    sleep_color_map = {
        'Wake': 'orange',
        'Stage 1': 'yellow',
        'Stage 2': 'green',
        'Stage 3': 'blue',
        'Undetermined': 'gray'
    }  # Color key for sleep staging
    seg_x = hv.Segments(seg_df, [
        hv.Dimension('start', range=(-10, num_samp - 1.5)),
        hv.Dimension('start_event', range=(-5, num_samp - 1.5)), 'end',
        'end_event'
    ], 'stage').opts(color='stage',
                     cmap=sleep_color_map,
                     line_width=7,
                     show_legend=True)  # x axis segments
    seg_y = hv.Segments(seg_df, [
        hv.Dimension('start_event', range=(-10, num_samp - 1.5)),
        hv.Dimension('start', range=(-5, num_samp - 1.5)), 'end_event', 'end'
    ], 'stage').opts(color='stage',
                     cmap=sleep_color_map,
                     line_width=7,
                     show_legend=False)  # y axis segments
    seg_plot = (seg_x * seg_y).opts(xlabel=' ', ylabel=' ',
                                    show_legend=False)  # All segments

    # Create matrix and histogram plots
    # ---------------------------------
    # raterize() fucntion used for big data set
    corr_img = rasterize(
        hv.Image(np.rot90(data_corr),
                 bounds=(-0.5, -0.5, num_samp - 1.5, num_samp - 1.5)).opts(
                     cmap='viridis', colorbar=True,
                     title='Correlation Matrix')).redim.range(z=corr_range)
    dist_img = rasterize(
        hv.Image(np.rot90(data_dist),
                 bounds=(-0.5, -0.5, num_samp - 1.5,
                         num_samp - 1.5)).opts(cmap='viridis',
                                               colorbar=True,
                                               title='Distance Matrix'))
    corr_his = rasterize(
        hv.Histogram(
            (corr_edges, corr_freq)).opts(xlabel='Correlation',
                                          height=300,
                                          width=400,
                                          title='Correlation Histogram'))
    dist_his = rasterize(
        hv.Histogram((dist_edges, dist_freq)).opts(xlabel='Distance',
                                                   height=300,
                                                   width=400,
                                                   title='Distance Histogram'))

    corr_img_wseg = (corr_img * seg_plot).opts(
        width=600, height=300, legend_position='right'
    )  # Overlay sleep segemnt plot with correlation matrix
    dist_img_wseg = (dist_img * seg_plot).opts(
        width=600, height=300, legend_position='right'
    )  # Overlay sleep segemnt plot with distance matrix

    dash = (corr_img_wseg + corr_his + dist_img_wseg + dist_his).opts(
        opts.Layout(shared_axes=False)).cols(2)  # Dashboard of all plots

    return dash
コード例 #35
0
ファイル: hplot.py プロジェクト: brey/pyPoseidon-dev
    def mesh(self, tiles=False, **kwargs):

        x = kwargs.get("x", self._obj.SCHISM_hgrid_node_x[:].values)
        y = kwargs.get("y", self._obj.SCHISM_hgrid_node_y[:].values)
        tes = kwargs.get("tri3", self._obj.SCHISM_hgrid_face_nodes.values)

        width = kwargs.get("width", 800)
        height = kwargs.get("height", 600)
        opts.defaults(opts.WMTS(width=width, height=height))

        tile = gv.WMTS("https://b.tile.openstreetmap.org/{Z}/{X}/{Y}.png")

        nodes = pd.DataFrame({"longitude": x, "latitude": y})

        points = gv.operation.project_points(gv.Points(nodes),
                                             projection=ccrs.PlateCarree())

        if tes.shape[1] == 3:

            elems = pd.DataFrame(tes, columns=["a", "b", "c"])

            if elems.min().min() > 0:
                elems = elems - 1

            trimesh = gv.TriMesh((elems, points)).edgepaths

            if tiles:
                return tile * datashade(
                    trimesh, precompute=True, cmap=["black"])
            else:
                return datashade(trimesh, precompute=True,
                                 cmap=["green"]).opts(width=width,
                                                      height=height)

        else:  # there are quads

            elems = pd.DataFrame(tes, columns=["a", "b", "c", "d"])

            if elems.min().min() > 0:
                elems = elems - 1

            quads = elems.loc[~elems.d.isna()].copy()
            quads = quads.reset_index(drop=True)
            ap = nodes.loc[quads.a, ["longitude", "latitude"]]
            bp = nodes.loc[quads.b, ["longitude", "latitude"]]
            cp = nodes.loc[quads.c, ["longitude", "latitude"]]
            dp = nodes.loc[quads.d, ["longitude", "latitude"]]
            quads["ap"] = ap.values.tolist()
            quads["bp"] = bp.values.tolist()
            quads["cp"] = cp.values.tolist()
            quads["dp"] = dp.values.tolist()

            n = 2
            al = quads.ap + quads.bp + quads.cp + quads.dp + quads.ap
            coords = [[l[i:i + n] for i in range(0, len(l), n)] for l in al]

            quads["coordinates"] = coords

            qpolys = pygeos.polygons(quads.coordinates.to_list())

            df = gp.GeoDataFrame(geometry=qpolys)

            df_ = spatialpandas.GeoDataFrame(df)

            q = gv.Path(df_)

            qdf = dynspread(
                rasterize(q, precompute=True).options(cmap=["black"]))

            triangles = elems.loc[elems.d.isna()]

            trimesh = gv.TriMesh((triangles, points)).edgepaths

            wireframe = dynspread(
                rasterize(trimesh, precompute=True).opts(cmap=["black"]))

            if tiles:
                return tile * wireframe * qdf
            else:
                return wireframe.opts(cmap=["green"]) * qdf.opts(
                    cmap=["green"])
コード例 #36
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)
コード例 #37
0
 def test_aggregate_contours_without_vdim(self):
     contours = Contours([[(0.2, 0.3), (0.4, 0.7)], [(0.4, 0.7), (0.8, 0.99)]])
     img = rasterize(contours, dynamic=False)
     self.assertEqual(img.vdims, ['Count'])
コード例 #38
0
    def viz_elevation(self) -> (hv.DynamicMap, hv.Layout):
        """
        Visualize elevation requested from OpenAltimetry API using datashader based on cycles
        https://holoviz.org/tutorial/Large_Data.html

        Returns
        -------
        map_cycle, map_rgt + lineplot_rgt : Holoviews objects
            Holoviews data visualization elements
        """

        OA_da = self.parallel_request_OA()

        if OA_da is None:
            print("No data")
            return (None, ) * 2

        else:

            cols = (["lat", "lon", "elevation", "canopy", "rgt", "cycle"]
                    if self.product == "ATL08" else
                    ["lat", "lon", "elevation", "rgt", "cycle"])
            ddf = dd.io.from_dask_array(OA_da, columns=cols).astype({
                "lat":
                "float",
                "lon":
                "float",
                "elevation":
                "float",
                "rgt":
                "int",
                "cycle":
                "int",
            })

            print("Plot elevation, please wait...")

            x, y = ds.utils.lnglat_to_meters(ddf.lon, ddf.lat)
            ddf_new = ddf.assign(x=x, y=y).persist()
            dset = hv.Dataset(ddf_new)

            raster_cycle = dset.to(
                hv.Points,
                ["x", "y"],
                ["elevation"],
                groupby=["cycle"],
                dynamic=True,
            )
            raster_rgt = dset.to(hv.Points, ["x", "y"], ["elevation"],
                                 groupby=["rgt"],
                                 dynamic=True)
            curve_rgt = dset.to(hv.Scatter, ["lat"], ["elevation"],
                                groupby=["rgt"],
                                dynamic=True)

            tiles = hv.element.tiles.EsriImagery().opts(xaxis=None,
                                                        yaxis=None,
                                                        width=450,
                                                        height=450)
            map_cycle = tiles * rasterize(
                raster_cycle, aggregator=ds.mean("elevation")).opts(
                    colorbar=True, tools=["hover"])
            map_rgt = tiles * rasterize(raster_rgt,
                                        aggregator=ds.mean("elevation")).opts(
                                            colorbar=True, tools=["hover"])
            lineplot_rgt = rasterize(curve_rgt,
                                     aggregator=ds.mean("elevation")).opts(
                                         width=450, height=450, cmap=["blue"])

            return map_cycle, map_rgt + lineplot_rgt
コード例 #39
0
ファイル: datashader_ny.py プロジェクト: pyviz-dev/nei
# In[ ]
import holoviews as hv, datashader as ds
from holoviews import opts
import dask.dataframe as dd
from holoviews.operation.datashader import rasterize

hv.extension('bokeh')

# In[ ]
path = './nyc_taxi_wide.parq'
df = dd.read_parquet(path).persist()

points = hv.Points(df, ['dropoff_x', 'dropoff_y'],
                   ['dropoff_hour', 'pickup_hour'])

# In[ ]
options = opts.Image(data_aspect=1,
                     responsive=True,
                     logz=True,
                     tools=['hover'],
                     colorbar=True)
rasterize(points).options(options)

# In[ ]
import panel
panel.panel(rasterize(points).options(options))
コード例 #40
0
ファイル: testdatashader.py プロジェクト: JulianDekker/DOVAL
 def test_aggregate_contours_with_vdim(self):
     contours = Contours([[(0.2, 0.3, 1), (0.4, 0.7, 1)], [(0.4, 0.7, 2), (0.8, 0.99, 2)]], vdims='z')
     img = rasterize(contours, dynamic=False)
     self.assertEqual(img.vdims, ['z'])
コード例 #41
0
ファイル: testdatashader.py プロジェクト: JulianDekker/DOVAL
 def test_rasterize_quadmesh_string_aggregator(self):
     qmesh = QuadMesh(([0, 1], [0, 1], np.array([[0, 1], [2, 3]])))
     img = rasterize(qmesh, width=3, height=3, dynamic=False, aggregator='mean')
     image = Image(np.array([[2., 3., np.NaN], [0, 1, np.NaN], [np.NaN, np.NaN, np.NaN]]),
                   bounds=(-.5, -.5, 1.5, 1.5))
     self.assertEqual(img, image)
コード例 #42
0
ファイル: testdatashader.py プロジェクト: JulianDekker/DOVAL
 def test_aggregate_zero_range_points(self):
     p = Points([(0, 0), (1, 1)])
     agg = rasterize(p, x_range=(0, 0), y_range=(0, 1), expand=False, dynamic=False)
     img = Image(([], [0.25, 0.75], np.zeros((2, 0))), bounds=(0, 0, 0, 1), xdensity=1, vdims=['Count'])
     self.assertEqual(agg, img)
コード例 #43
0
ファイル: api.py プロジェクト: pmav99/Thalassa
def get_wireframe(trimesh: gv.TriMesh) -> hv.Layout:
    wireframe = dynspread(rasterize(trimesh.edgepaths, precompute=True))
    return wireframe
コード例 #44
0
ファイル: main.py プロジェクト: snowind/pyviz
 def view(self,**kwargs):
     points = hv.DynamicMap(self.points)
     agg = rasterize(points, x_sampling=1, y_sampling=1, width=600, height=400)
     stream = hv.streams.Params(self, ['cmap'])
     tiles = hv.DynamicMap(self.tiles)
     return tiles * shade(agg, streams=[stream])
コード例 #45
0
 def test_rasterize_quadmesh_string_aggregator(self):
     qmesh = QuadMesh(([0, 1], [0, 1], np.array([[0, 1], [2, 3]])))
     img = rasterize(qmesh, width=3, height=3, dynamic=False, aggregator='mean')
     image = Image(np.array([[2., 3., np.NaN], [0, 1, np.NaN], [np.NaN, np.NaN, np.NaN]]),
                   bounds=(-.5, -.5, 1.5, 1.5))
     self.assertEqual(img, image)