def test_buffer_dframe_patch_larger_than_length(self): data = pd.DataFrame({'x': np.array([0]), 'y': np.array([1])}) buff = Buffer(data, length=1, index=False) chunk = pd.DataFrame({'x': np.array([1, 2]), 'y': np.array([2, 3])}) buff.send(chunk) dframe = pd.DataFrame({'x': np.array([2]), 'y': np.array([3])}) self.assertEqual(buff.data.values, dframe.values)
def test_buffer_dframe_send_with_index(self): data = pd.DataFrame({'x': np.array([0]), 'y': np.array([1])}) buff = Buffer(data) buff.send(pd.DataFrame({'x': np.array([1]), 'y': np.array([2])})) dframe = pd.DataFrame({ 'x': np.array([0, 1]), 'y': np.array([1, 2]) }, index=[0, 0]) self.assertEqual(buff.data.values, dframe.reset_index().values)
def test_dataset_dataframe_init_hm_alias(self): "Tests support for homogeneous DataFrames" exception = "None of the available storage backends "\ "were able to support the supplied data format." with self.assertRaisesRegexp(Exception, exception): Dataset(pd.DataFrame({'x':self.xs, 'x2':self.xs_2}), kdims=['x'], vdims=['x2'])
def test_dataset_dataframe_init_hm_alias(self): with self.assertRaises(Exception): Dataset(pd.DataFrame({ 'x': self.xs, 'x2': self.xs_2 }), kdims=['x'], vdims=['x2'])
def test_bivariate_dframe_constructor(self): dist = Bivariate( pd.DataFrame({ 'x': [0, 1, 2], 'y': [0, 1, 2] }, columns=['x', 'y'])) self.assertEqual(dist.kdims, [Dimension('x'), Dimension('y')]) self.assertEqual(dist.vdims, [Dimension('Density')])
def test_bivariate_dframe_constructor(self): if pd is None: raise SkipTest("Test requires pandas, skipping.") dist = Bivariate( pd.DataFrame({ 'x': [0, 1, 2], 'y': [0, 1, 2] }, columns=['x', 'y'])) self.assertEqual(dist.kdims, [Dimension('x'), Dimension('y')]) self.assertEqual(dist.vdims, [Dimension('Density')])
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)
def test_df_dataset(self): if not pd: raise SkipTest('Pandas not available') dfs = [pd.DataFrame(np.column_stack([np.arange(i, i+2), np.arange(i, i+2)]), columns=['x', 'y']) for i in range(2)] mds = Path(dfs, kdims=['x', 'y'], datatype=[self.datatype]) self.assertIs(mds.interface, self.interface) for i, ds in enumerate(mds.split(datatype='dataframe')): ds['x'] = ds.x.astype(int) ds['y'] = ds.y.astype(int) self.assertEqual(ds, dfs[i])
def test_point_categorical_dtype_color_op(self): df = pd.DataFrame(dict(sample_id=['subject 1', 'subject 2', 'subject 3', 'subject 4'], category=['apple', 'pear', 'apple', 'pear'], value=[1, 2, 3, 4])) df['category'] = df['category'].astype('category') points = Points(df, ['sample_id', 'value']).opts(color='category') plot = bokeh_renderer.get_plot(points) cds = plot.handles['cds'] glyph = plot.handles['glyph'] cmapper = plot.handles['color_color_mapper'] self.assertTrue(cmapper, CategoricalColorMapper) self.assertEqual(cmapper.factors, ['apple', 'pear']) self.assertEqual(np.asarray(cds.data['color']), np.array(['apple', 'pear', 'apple', 'pear'])) self.assertEqual(glyph.fill_color, {'field': 'color', 'transform': cmapper}) self.assertEqual(glyph.line_color, {'field': 'color', 'transform': cmapper})
def test_aggregate_curve_datetimes_dask(self): df = pd.DataFrame( data=np.arange(1000), columns=['a'], index=pd.date_range('2019-01-01', freq='1T', periods=1000), ) ddf = dd.from_pandas(df, npartitions=4) curve = Curve(ddf, kdims=['index'], vdims=['a']) img = aggregate(curve, width=2, height=3, dynamic=False) bounds = (np.datetime64('2019-01-01T00:00:00.000000'), 0.0, np.datetime64('2019-01-01T16:39:00.000000'), 999.0) dates = [np.datetime64('2019-01-01T04:09:45.000000000'), np.datetime64('2019-01-01T12:29:15.000000000')] expected = Image((dates, [166.5, 499.5, 832.5], [[332, 0], [167, 166], [0, 334]]), ['index', 'a'], 'Count', datatype=['xarray'], bounds=bounds) self.assertEqual(img, expected)
def test_init_buffer_dframe_with_index(self): data = pd.DataFrame({'x': np.array([1]), 'y': np.array([2])}) buff = Buffer(data) self.assertEqual(buff.data, data.reset_index())
def test_distribution_dframe_constructor(self): if pd is None: raise SkipTest("Test requires pandas, skipping.") dist = Distribution(pd.DataFrame({'Value': [0, 1, 2]})) self.assertEqual(dist.kdims, [Dimension('Value')]) self.assertEqual(dist.vdims, [Dimension('Density')])
def test_distribution_dframe_constructor(self): dist = Distribution(pd.DataFrame({'Value': [0, 1, 2]})) self.assertEqual(dist.kdims, [Dimension('Value')]) self.assertEqual(dist.vdims, [Dimension('Density')])
def _process_element(self, element): if not bool(element): return element.clone(crs=self.p.projection) crs = element.crs proj = self.p.projection if (isinstance(crs, ccrs.PlateCarree) and not isinstance(proj, ccrs.PlateCarree) and crs.proj4_params['lon_0'] != 0): element = self.instance(projection=ccrs.PlateCarree())(element) if isinstance(proj, ccrs.CRS) and not isinstance(proj, ccrs.Projection): raise ValueError('invalid transform:' ' Spherical contouring is not supported - ' ' consider using PlateCarree/RotatedPole.') if isinstance(element, Polygons): geoms = polygons_to_geom_dicts(element, skip_invalid=False) else: geoms = path_to_geom_dicts(element, skip_invalid=False) projected = [] for path in geoms: geom = path['geometry'] # Ensure minimum area for polygons (precision issues cause errors) if isinstance(geom, Polygon) and geom.area < 1e-15: continue elif isinstance(geom, MultiPolygon): polys = [g for g in geom if g.area > 1e-15] if not polys: continue geom = MultiPolygon(polys) elif (not geom or isinstance(geom, GeometryCollection)): continue proj_geom = proj.project_geometry(geom, element.crs) # Attempt to fix geometry without being noisy about it logger = logging.getLogger() try: prev = logger.level logger.setLevel(logging.ERROR) if not proj_geom.is_valid: proj_geom = proj.project_geometry(geom.buffer(0), element.crs) except: continue finally: logger.setLevel(prev) if proj_geom.geom_type == 'GeometryCollection' and len(proj_geom) == 0: continue data = dict(path, geometry=proj_geom) if 'holes' in data: data.pop('holes') projected.append(data) if len(geoms) and len(projected) == 0: self.warning('While projecting a %s element from a %s coordinate ' 'reference system (crs) to a %s projection none of ' 'the projected paths were contained within the bounds ' 'specified by the projection. Ensure you have specified ' 'the correct coordinate system for your data.' % (type(element).__name__, type(element.crs).__name__, type(self.p.projection).__name__)) # Try casting back to original types if element.interface is GeoPandasInterface: import geopandas as gpd projected = gpd.GeoDataFrame(projected, columns=element.data.columns) elif element.interface is MultiInterface: x, y = element.kdims item = element.data[0] if element.data else None if item is None or (isinstance(item, dict) and 'geometry' in item): return element.clone(projected, crs=self.p.projection) projected = [geom_dict_to_array_dict(p, [x.name, y.name]) for p in projected] if any('holes' in p for p in projected): pass elif pd and isinstance(item, pd.DataFrame): projected = [pd.DataFrame(p, columns=item.columns) for p in projected] elif isinstance(item, np.ndarray): projected = [np.column_stack([p[d.name] for d in element.dimensions()]) for p in projected] return element.clone(projected, crs=self.p.projection)
def test_clear_buffer_dframe_with_index(self): data = pd.DataFrame({'a': [1, 2, 3]}) buff = Buffer(data) buff.clear() self.assertEqual(buff.data, data.iloc[:0, :].reset_index())
def test_buffer_dframe_send_verify_column_fail(self): data = pd.DataFrame({'x': np.array([0]), 'y': np.array([1])}) buff = Buffer(data, index=False) error = "Input expected to have columns \['x', 'y'\], got \['x'\]" with self.assertRaisesRegexp(IndexError, error): buff.send(pd.DataFrame({'x': np.array([2])}))
def _process_element(self, element): if element.crs == self.p.projection: return element elif not len(element): return element.clone(crs=self.p.projection) crs = element.crs cylindrical = isinstance(crs, ccrs._CylindricalProjection) proj = self.p.projection if isinstance(proj, ccrs.CRS) and not isinstance(proj, ccrs.Projection): raise ValueError('invalid transform:' ' Spherical contouring is not supported - ' ' consider using PlateCarree/RotatedPole.') boundary = Polygon(crs.boundary) bounds = [round(b, 10) for b in boundary.bounds] xoffset = round((boundary.bounds[2]-boundary.bounds[0])/2.) if isinstance(element, Polygons): geoms = polygons_to_geom_dicts(element, skip_invalid=False) else: geoms = path_to_geom_dicts(element, skip_invalid=False) data_bounds = max_extents([g['geometry'].bounds for g in geoms]) total_bounds = tuple(round(b, 10) for b in data_bounds) projected = [] for path in geoms: geom = path['geometry'] if (cylindrical and total_bounds[0] >= (bounds[0]+xoffset) and total_bounds[2] > (bounds[2]+xoffset//2)): # Offset if lon and not centered on 0 longitude # i.e. lon_min > 0 and lon_max > 270 geom = shapely.affinity.translate(geom, xoff=-xoffset) geom_bounds = [round(b, 10) for b in geom.bounds] if boundary and (geom_bounds[0] < bounds[0] or geom_bounds[2] > bounds[2]): try: geom = boundary.intersection(geom) except: pass # Ensure minimum area for polygons (precision issues cause errors) if isinstance(geom, Polygon) and geom.area < 1e-15: continue elif isinstance(geom, MultiPolygon): polys = [g for g in geom if g.area > 1e-15] if not polys: continue geom = MultiPolygon(polys) elif (not geom or isinstance(geom, GeometryCollection)): continue proj_geom = proj.project_geometry(geom, element.crs) # Attempt to fix geometry without being noisy about it logger = logging.getLogger() try: prev = logger.level logger.setLevel(logging.ERROR) if not proj_geom.is_valid: proj_geom = proj.project_geometry(geom.buffer(0), element.crs) except: continue finally: logger.setLevel(prev) data = dict(path, geometry=proj_geom) projected.append(data) if len(geoms) and len(projected) == 0: self.warning('While projecting a %s element from a %s coordinate ' 'reference system (crs) to a %s projection none of ' 'the projected paths were contained within the bounds ' 'specified by the projection. Ensure you have specified ' 'the correct coordinate system for your data.' % (type(element).__name__, type(element.crs).__name__, type(self.p.projection).__name__)) # Try casting back to original types if element.interface is GeoPandasInterface: import geopandas as gpd projected = gpd.GeoDataFrame(projected, columns=element.data.columns) elif element.interface is MultiInterface: x, y = element.kdims item = element.data[0] if element.data else None if item is None or (isinstance(item, dict) and 'geometry' in item): return element.clone(projected, crs=self.p.projection) projected = [geom_dict_to_array_dict(p, [x.name, y.name]) for p in projected] if any('holes' in p for p in projected): pass elif pd and isinstance(item, pd.DataFrame): projected = [pd.DataFrame(p, columns=item.columns) for p in projected] elif isinstance(item, np.ndarray): projected = [np.column_stack([p[d.name] for d in element.dimensions()]) for p in projected] return element.clone(projected, crs=self.p.projection)
def test_buffer_dframe_send(self): data = pd.DataFrame({'x': np.array([0]), 'y': np.array([1])}) buff = Buffer(data, index=False) buff.send(pd.DataFrame({'x': np.array([1]), 'y': np.array([2])})) dframe = pd.DataFrame({'x': np.array([0, 1]), 'y': np.array([1, 2])}) self.assertEqual(buff.data.values, dframe.values)
def test_init_buffer_dframe(self): data = pd.DataFrame({'x': np.array([1]), 'y': np.array([2])}) buff = Buffer(data, index=False) self.assertEqual(buff.data, data)