class NYCTaxiExplorer(hv.streams.Stream): alpha = param.Magnitude(default=0.75, doc="Alpha value for the map opacity") colormap = param.ObjectSelector( default=cm["fire"], objects=[cm[k] for k in cm.keys() if not '_' in k]) hour = param.Range(default=(0, 24), bounds=(0, 24)) location = param.ObjectSelector(default='dropoff', objects=['dropoff', 'pickup']) def make_view(self, x_range, y_range, **kwargs): map_tiles = tiles(style=dict(alpha=self.alpha), plot=tile_options) points = hv.Points(df, kdims=[self.location + '_x', self.location + '_y'], vdims=['dropoff_hour']) if self.hour != (0, 24): points = points.select(dropoff_hour=self.hour) taxi_trips = datashade(points, x_sampling=1, y_sampling=1, cmap=self.colormap, dynamic=False, x_range=x_range, y_range=y_range, width=1000, height=600) return map_tiles * taxi_trips
class NYCTaxiExplorer(hv.streams.Stream): alpha = param.Magnitude(default=0.8, doc='Alpha value for the map opacity') plot = param.ObjectSelector(default='pickup', objects=['pickup', 'dropoff']) passengers = param.Range( default=(0, 10), bounds=(0, 10), doc='Filter for Taxi Trips by Number of Passengers') output = parambokeh.view.Plot() def make_view(self, x_range=None, y_range=None, **kwargs): options = dict(width=800, height=475, xaxis=None, yaxis=None) map_tiles = gv.WMTS(tiles['ESRI']).opts(style=dict(alpha=self.alpha), plot=options) points = hv.Points(nyc_taxi, kdims=[self.plot + '_x', self.plot + '_y'], vdims=['passenger_count']) selected = points.select(passenger_count=self.passengers) taxi_trips = datashade(selected, x_sampling=1, y_sampling=1, width=800, height=475, cmap=inferno, dynamic=False) return map_tiles * taxi_trips
class NYCTaxiExplorer(param.Parameterized): alpha = param.Magnitude(default=0.75, doc="Alpha value for the map opacity") cmap = param.ObjectSelector(cm['fire'], objects={c: cm[c] for c in cmaps}) hour = param.Range(default=(0, 24), bounds=(0, 24)) location = param.ObjectSelector(default='dropoff', objects=['dropoff', 'pickup']) @param.depends('location', 'hour') def points(self): points = hv.Points(df, kdims=[self.location + '_x', self.location + '_y'], vdims=['dropoff_hour']) if self.hour != (0, 24): points = points.select(dropoff_hour=self.hour) return points @param.depends('alpha') def tiles(self): return tiles.options(alpha=self.alpha, **opts) 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])
class NYCTaxiExplorer(hv.streams.Stream): alpha = param.Magnitude(default=0.75, doc="Alpha value for the map opacity") colormap = param.ObjectSelector( default=cm["fire"], objects=[cm[k] for k in cm.keys() if not '_' in k]) plot = param.ObjectSelector(default="pickup", objects=["pickup", "dropoff"]) passengers = param.Range(default=passenger_counts, bounds=passenger_counts) output = parambokeh.view.Plot() def make_view(self, x_range, y_range, alpha, colormap, plot, passengers, **kwargs): map_tiles = tiles(style=dict(alpha=alpha), plot=tile_options) points = hv.Points(df, kdims=[plot + '_x', plot + '_y'], vdims=['passenger_count']) if passengers != passenger_counts: points = points.select(passenger_count=passengers) taxi_trips = datashade(points, x_sampling=1, y_sampling=1, cmap=colormap, dynamic=False, x_range=x_range, y_range=y_range) return map_tiles * taxi_trips
class NYCTaxiExplorer(hv.streams.Stream): alpha = param.Magnitude(default=0.75, doc="Alpha value for the map opacity") colormap = param.ObjectSelector(default=cm_n["fire"], objects=cm_n.values()) hour = param.Integer(default=None, bounds=(0, 23), doc="All hours by default; drag to select one hour") passengers = param.Range(default=(0, max_pass), bounds=(0, max_pass)) location = param.ObjectSelector(default='dropoff', objects=['dropoff', 'pickup']) def make_view(self, x_range, y_range, **kwargs): map_tiles = tiles.opts(style=dict(alpha=self.alpha), plot=options) points = hv.Points(df, [self.location + '_x', self.location + '_y'], self.location + '_hour') selection = { self.location + "_hour": self.hour if self.hour else (0, 24), "passenger_count": self.passengers } taxi_trips = datashade(points.select(**selection), x_sampling=1, y_sampling=1, cmap=self.colormap, dynamic=False, x_range=x_range, y_range=y_range, width=1000, height=600) return map_tiles * taxi_trips
class Style(Controls): cnorm = param.Selector(default='linear', objects=['linear', 'log', 'eq_hist']) color = param.String(default=None) cmap = param.Selector(default=None, label='Colormap', objects=COLORMAPS) alpha = param.Magnitude(default=1)
class NYCTaxi(param.Parameterized): alpha = param.Magnitude(default=0.75, doc="Map tile opacity") cmap = param.ObjectSelector( 'fire', objects=['fire', 'bgy', 'bgyw', 'bmy', 'gray', 'kbc']) location = param.ObjectSelector(default='dropoff', objects=['dropoff', 'pickup']) def make_view(self, **kwargs): print(kwargs) pts = hv.Points(taxi, [self.location + '_x', self.location + '_y']) trips = datashade(pts, cmap=palette[self.cmap], **dopts) return tiles.options(alpha=self.alpha) * trips
class OSMExplorer(hv.streams.Stream): alpha = param.Magnitude(default=0.75, doc="Map opacity") colormap = param.ObjectSelector(default=cm_n["fire"], objects=cm_n.values()) def make_view(self, x_range, y_range, **kwargs): tiles = map_tiles.options(alpha=self.alpha, **opts1) points = hv.Points(df, ['x', 'y']) return tiles * datashade(points, cmap=self.colormap, x_range=x_range, y_range=y_range, **opts2)
class _BigDumbParams(param.Parameterized): action = param.Action(default_action, allow_None=True) array = param.Array(np.array([1.0, 2.0])) boolean = param.Boolean(True, allow_None=True) callable = param.Callable(default_action, allow_None=True) class_selector = param.ClassSelector(int, is_instance=False, allow_None=True) color = param.Color("#FFFFFF", allow_None=True) composite = param.Composite(["action", "array"], allow_None=True) try: data_frame = param.DataFrame( pd.DataFrame({"A": 1.0, "B": np.arange(5)}), allow_None=True ) except TypeError: data_frame = param.DataFrame(pd.DataFrame({"A": 1.0, "B": np.arange(5)})) date = param.Date(datetime.now(), allow_None=True) date_range = param.DateRange((datetime.min, datetime.max), allow_None=True) dict_ = param.Dict({"foo": "bar"}, allow_None=True, doc="dict means dictionary") dynamic = param.Dynamic(default=default_action, allow_None=True) file_selector = param.FileSelector( os.path.join(FILE_DIR_DIR, "LICENSE"), path=os.path.join(FILE_DIR_DIR, "*"), allow_None=True, ) filename = param.Filename( os.path.join(FILE_DIR_DIR, "LICENSE"), allow_None=True ) foldername = param.Foldername(os.path.join(FILE_DIR_DIR), allow_None=True) hook_list = param.HookList( [CallableObject(), CallableObject()], class_=CallableObject, allow_None=True ) integer = param.Integer(10, allow_None=True) list_ = param.List([1, 2, 3], allow_None=True, class_=int) list_selector = param.ListSelector([2, 2], objects=[1, 2, 3], allow_None=True) magnitude = param.Magnitude(0.5, allow_None=True) multi_file_selector = param.MultiFileSelector( [], path=os.path.join(FILE_DIR_DIR, "*"), allow_None=True, check_on_set=True, ) number = param.Number(-10.0, allow_None=True, doc="here is a number") numeric_tuple = param.NumericTuple((5.0, 10.0), allow_None=True) object_selector = param.ObjectSelector( False, objects={"False": False, "True": 1}, allow_None=True ) path = param.Path(os.path.join(FILE_DIR_DIR, "LICENSE"), allow_None=True) range_ = param.Range((-1.0, 2.0), allow_None=True) series = param.Series(pd.Series(range(5)), allow_None=True) string = param.String("foo", allow_None=True, doc="this is a string") tuple_ = param.Tuple((3, 4, "fi"), allow_None=True) x_y_coordinates = param.XYCoordinates((1.0, 2.0), allow_None=True)
class OSM(param.Parameterized): alpha = param.Magnitude(default=0.75, doc="Map tile opacity") cmap = param.ObjectSelector(cm['fire'], objects={c: cm[c] for c in cmaps}) @param.depends('alpha') def tiles(self): return gv.tile_sources.EsriImagery.options(alpha=self.alpha, **topts) @param.depends() def view(self): points = hv.DynamicMap(hv.Points(df, kdims=['x', 'y'])) raster = rasterize(points, x_sampling=1, y_sampling=1, width=900, height=600) return hv.DynamicMap(self.tiles) * shade( raster, streams=[hv.streams.Params(self, ['cmap'])])
class MyParameterized(param.Parameterized): enable = param.Boolean(True, doc="A sample Boolean parameter", allow_None=True) what_proportion = param.Magnitude(default=0.9) age = param.Number(49, bounds=(0, 100), doc="Any Number between 0 to 100") how_many = param.Integer() favorite_quote = param.String(default="Hello, world!") choose_file_or_folder = param.Path(search_paths='./') choose_folder = param.Foldername(search_paths="./") choose_file = param.Filename(search_paths="./") select_a_file = param.FileSelector(path='./*') select_multiple_files = param.MultiFileSelector(path='./*') favorite_color = param.ObjectSelector( default="green", objects=["red", "yellow", "green"]) favorite_fruit = param.Selector(default="Apple", objects=["Orange", "Apple", "Mango"]) select_multiple = param.ListSelector(default=[3, 5], objects=[1, 2, 3, 4, 5]) birthday = param.CalendarDate(dt.date(2017, 1, 1), bounds=(dt.date(2017, 1, 1), dt.date(2017, 2, 1))) appointment = param.Date(dt.datetime(2017, 1, 1), bounds=(dt.datetime(2017, 1, 1), dt.datetime(2017, 2, 1))) least_favorite_color = param.Color(default='#FF0000') dataset = param.DataFrame(pd.util.testing.makeDataFrame().iloc[:3]) this_strange_thing = param.Tuple(default=(False, ), allow_None=True) some_numbers = param.NumericTuple(default=(1, 2, 3.0, 4.0)) home_city = param.XYCoordinates(default=(-111.65, 40.23)) bounds = param.Range(default=(-10, 10))
class GrabCutPanel(param.Parameterized): """ Defines a Panel for extracting contours from an Image. """ crs = param.ClassSelector(default=ccrs.PlateCarree(), class_=ccrs.Projection, precedence=-1, doc=""" Projection the inputs and output paths are defined in.""") image = param.ClassSelector(class_=gv.RGB, precedence=-1, doc=""" The Image to compute contours on""") path_type = param.ClassSelector(default=gv.Path, class_=hv.Path, precedence=-1, is_instance=False, doc=""" The element type to draw into.""") downsample = param.Magnitude(default=1, precedence=1, doc=""" Amount to downsample image by before applying grabcut.""") iterations = param.Integer(default=5, precedence=1, bounds=(0, 20), doc=""" Number of iterations to run the GrabCut algorithm for.""") clear = param.Action(default=lambda o: o._trigger_clear(), precedence=2, doc=""" Button to clear drawn annotations.""") update_contour = param.Action(default=lambda o: o.param.trigger('update_contour'), precedence=2, doc=""" Button triggering GrabCut.""") minimum_size = param.Integer(default=0, precedence=3) filter_contour = param.Action(default=lambda o: o.param.trigger('filter_contour'), precedence=4, doc=""" Button triggering filtering of contours.""") tolerance = param.Number(default=0, precedence=5) simplify_contour = param.Action(default=lambda o: o.param.trigger('simplify_contour'), precedence=6, doc=""" Simplifies contour.""" ) width = param.Integer(default=500, precedence=-1, doc=""" Width of the plot""") height = param.Integer(default=None, precedence=-1, doc=""" Height of the plot""") def __init__(self, image, fg_data=[], bg_data=[], **params): super(GrabCutPanel, self).__init__(image=image, **params) self._bg_data = bg_data self._fg_data = fg_data self.bg_paths = DynamicMap(self.bg_path_view) self.fg_paths = DynamicMap(self.fg_path_view) self.draw_bg = FreehandDraw(source=self.bg_paths) self.draw_fg = FreehandDraw(source=self.fg_paths) self._initialized = False self._clear = False def _trigger_clear(self): self._clear = True self.param.trigger('clear') self._clear = False @param.depends('clear') def bg_path_view(self): if self._clear: self._bg_data = [] elif self._initialized: self._bg_data = self.draw_bg.element.data else: self._bg_data = gv.project(self.path_type(self._bg_data, crs=self.crs), projection=self.image.crs) return self.path_type(self._bg_data, crs=self.image.crs) @param.depends('clear') def fg_path_view(self): if self._clear: self._fg_data = [] elif self._initialized: self._fg_data = self.draw_fg.element.data else: self._fg_data = gv.project(self.path_type(self._fg_data, crs=self.crs), projection=self.image.crs) return self.path_type(self._fg_data, crs=self.image.crs) @param.depends('update_contour', 'image') def extract_foreground(self, **kwargs): img = self.image bg, fg = self.bg_path_view(), self.fg_path_view() self._initialized = True if not len(bg) or not len(fg): return gv.Path([], img.kdims, crs=img.crs) if self.downsample != 1: kwargs = {'dynamic': False} h, w = img.interface.shape(img, gridded=True) kwargs['width'] = int(w*self.downsample) kwargs['height'] = int(h*self.downsample) img = regrid(img, **kwargs) foreground = extract_foreground(img, background=bg, foreground=fg, iterations=self.iterations) foreground = gv.Path([contours(foreground, filled=True, levels=1).split()[0].data], kdims=foreground.kdims, crs=foreground.crs) self.result = gv.project(foreground, projection=self.crs) return foreground @param.depends('filter_contour') def _filter_contours(self, obj, **kwargs): if self.minimum_size > 0: obj = filter_polygons(obj, minimum_size=self.minimum_size) return obj @param.depends('simplify_contour') def _simplify_contours(self, obj, **kwargs): if self.tolerance > 0: obj = simplify_paths(obj, tolerance=self.tolerance) self.result = gv.project(obj, projection=self.crs) return obj def view(self): height = self.height if height is None: h, w = self.image.dimension_values(2, flat=False).shape[:2] height = int(self.width*(h/w)) options = dict(width=self.width, height=height, xaxis=None, yaxis=None, projection=self.image.crs) dmap = hv.DynamicMap(self.extract_foreground) dmap = hv.util.Dynamic(dmap, operation=self._filter_contours) dmap = hv.util.Dynamic(dmap, operation=self._simplify_contours) return (regrid(self.image).options(**options) * self.bg_paths * self.fg_paths + dmap.options(**options)) @param.output(polys=hv.Path) def output(self): return self.result def panel(self): return pn.Row(self.param, self.view())
class link_selections(_base_link_selections): """ Operation which automatically links selections between elements in the supplied HoloViews object. Can be used a single time or be used as an instance to apply the linked selections across multiple objects. """ cross_filter_mode = param.Selector(['overwrite', 'intersect'], default='intersect', doc=""" Determines how to combine selections across different elements.""") index_cols = param.List(default=None, doc=""" If provided, selection switches to index mode where all queries are expressed solely in terms of discrete values along the index_cols. All Elements given to link_selections must define the index_cols, either as explicit dimensions or by sharing an underlying Dataset that defines them.""" ) selection_expr = param.Parameter(default=None, doc=""" dim expression of the current selection or None to indicate that everything is selected.""") selected_color = param.Color(default=None, allow_None=True, doc=""" Color of selected data, or None to use the original color of each element.""") selection_mode = param.Selector( ['overwrite', 'intersect', 'union', 'inverse'], default='overwrite', doc=""" Determines how to combine successive selections on the same element.""") unselected_alpha = param.Magnitude(default=0.1, doc=""" Alpha of unselected data.""") unselected_color = param.Color(default=None, doc=""" Color of unselected data.""") @bothmethod def instance(self_or_cls, **params): inst = super(link_selections, self_or_cls).instance(**params) # Initialize private properties inst._obj_selections = {} inst._obj_regions = {} inst._reset_regions = True # _datasets caches inst._datasets = [] inst._cache = {} self_or_cls._install_param_callbacks(inst) return inst @param.depends('selection_expr', watch=True) def _update_pipes(self): sel_expr = self.selection_expr for pipe, ds, raw in self._datasets: ref = ds._plot_id self._cache[ref] = ds_cache = self._cache.get(ref, {}) if sel_expr in ds_cache: data = ds_cache[sel_expr] return pipe.event(data=data.data) else: ds_cache.clear() sel_ds = SelectionDisplay._select(ds, sel_expr, self._cache) ds_cache[sel_expr] = sel_ds pipe.event(data=sel_ds.data if raw else sel_ds) def selection_param(self, data): """ Returns a parameter which reflects the current selection when applied to the supplied data, making it easy to create a callback which depends on the current selection. Args: data: A Dataset type or data which can be cast to a Dataset Returns: A parameter which reflects the current selection """ raw = False if not isinstance(data, Dataset): raw = True data = Dataset(data) pipe = Pipe(data=data.data) self._datasets.append((pipe, data, raw)) return pipe.param.data @bothmethod def _install_param_callbacks(self_or_cls, inst): def update_selection_mode(*_): # Reset selection state of streams for stream in inst._selection_expr_streams.values(): stream.reset() stream.mode = inst.selection_mode inst.param.watch(update_selection_mode, ['selection_mode']) def update_cross_filter_mode(*_): inst._cross_filter_stream.reset() inst._cross_filter_stream.mode = inst.cross_filter_mode inst.param.watch(update_cross_filter_mode, ['cross_filter_mode']) def update_show_region(*_): for stream in inst._selection_expr_streams.values(): stream.include_region = inst.show_regions stream.event() inst.param.watch(update_show_region, ['show_regions']) def update_selection_expr(*_): new_selection_expr = inst.selection_expr current_selection_expr = inst._cross_filter_stream.selection_expr if repr(new_selection_expr) != repr(current_selection_expr): # Disable regions if setting selection_expr directly if inst.show_regions: inst.show_regions = False inst._selection_override.event( selection_expr=new_selection_expr) inst.param.watch(update_selection_expr, ['selection_expr']) def selection_expr_changed(*_): new_selection_expr = inst._cross_filter_stream.selection_expr if repr(inst.selection_expr) != repr(new_selection_expr): inst.selection_expr = new_selection_expr inst._cross_filter_stream.param.watch(selection_expr_changed, ['selection_expr']) # Clear selection expr sequence history on plot reset for stream in inst._selection_expr_streams.values(): def clear_stream_history(resetting, stream=stream): if resetting: stream.clear_history() print("registering reset for ", stream) stream.plot_reset_stream.param.watch(clear_stream_history, ['resetting']) @classmethod def _build_selection_streams(cls, inst): # Colors stream style_stream = _Styles( colors=[inst.unselected_color, inst.selected_color], alpha=inst.unselected_alpha) # Cmap streams cmap_streams = [ _Cmap(cmap=inst.unselected_cmap), _Cmap(cmap=inst.selected_cmap), ] def update_colors(*_): colors = [inst.unselected_color, inst.selected_color] style_stream.event(colors=colors, alpha=inst.unselected_alpha) cmap_streams[0].event(cmap=inst.unselected_cmap) if cmap_streams[1] is not None: cmap_streams[1].event(cmap=inst.selected_cmap) inst.param.watch( update_colors, ['unselected_color', 'selected_color', 'unselected_alpha']) # Exprs stream exprs_stream = _SelectionExprLayers(inst._selection_override, inst._cross_filter_stream) return _SelectionStreams( style_stream=style_stream, exprs_stream=exprs_stream, cmap_streams=cmap_streams, ) @property def unselected_cmap(self): """ The datashader colormap for unselected data """ if self.unselected_color is None: return None return _color_to_cmap(self.unselected_color) @property def selected_cmap(self): """ The datashader colormap for selected data """ return None if self.selected_color is None else _color_to_cmap( self.selected_color)
class MyParamMagnitude(param.Parameterized): r_squared = param.Magnitude(default=0.9)
class SagSwellExplorer(hv.streams.Stream): alpha = param.Magnitude(default=0.75, doc="Alpha value for the map opacity") plot = param.ObjectSelector(default="Sag", objects=["Sag", "Swell"]) colormap = param.ObjectSelector(default=cm["fire"], objects=cm.values()) numEvents = param.Range(default=(1, 300), bounds=(1, 300), doc="""Filter for event count""") ByDay = param.Boolean(False, doc="Filter By Day") DayNum = param.Integer( 1, bounds=(1, 15)) # Stop at 15 since that's all the data we have loaded ByFeeder = param.Boolean(False, doc="Filter By Feeder") Feeder = param.ObjectSelector( default="28GM012002", objects=df.FEEDER_ID.sort_values().unique().tolist()) BySUB = param.Boolean(False, doc="Filter By SUB") Substations = param.ObjectSelector( default="28GM", objects=df.SUB.sort_values().unique().tolist()) maxpix = param.Integer(12) threshhold = param.Number(0.6, bounds=(0.1, 1.0)) def make_view(self, x_range=None, y_range=None, **kwargs): #map_tiles = tiles.opts(style=dict(alpha=self.alpha), plot=options) points = hv.Points( df, kdims=['X_CORD', 'Y_CORD'], vdims=['EVENT_COUNT', 'EventType', 'SUB', 'day', 'FEEDER_ID']) if (self.BySUB & self.ByFeeder & self.ByDay): selected = points.select(EventType=self.plot, EVENT_COUNT=self.numEvents, SUB=self.Substations, day=self.DayNum, FEEDER_ID=self.Feeder) elif (self.BySUB & self.ByFeeder & (not self.ByDay)): selected = points.select(EventType=self.plot, EVENT_COUNT=self.numEvents, SUB=self.Substations, FEEDER_ID=self.Feeder) elif (self.BySUB & (not self.ByFeeder) & self.ByDay): selected = points.select(EventType=self.plot, EVENT_COUNT=self.numEvents, SUB=self.Substations, day=self.DayNum) elif (self.BySUB & (not self.ByFeeder) & (not self.ByDay)): selected = points.select(EventType=self.plot, EVENT_COUNT=self.numEvents, SUB=self.Substations) elif ((not self.BySUB) & self.ByFeeder & self.ByDay): selected = points.select(EventType=self.plot, EVENT_COUNT=self.numEvents, day=self.DayNum, FEEDER_ID=self.Feeder) elif ((not self.BySUB) & self.ByFeeder & (not self.ByDay)): selected = points.select(EventType=self.plot, EVENT_COUNT=self.numEvents, FEEDER_ID=self.Feeder) elif ((not self.BySUB) & (not self.ByFeeder) & self.ByDay): selected = points.select(EventType=self.plot, EVENT_COUNT=self.numEvents, day=self.DayNum) else: selected = points.select(EventType=self.plot, EVENT_COUNT=self.numEvents) SagSwellPts = datashade(selected, x_sampling=1, y_sampling=1, cmap=self.colormap, dynamic=False, x_range=x_range, y_range=y_range, width=640, height=380) dsss = dynspread(SagSwellPts, shape='circle', max_px=self.maxpix, threshold=self.threshhold) #return map_tiles * dsss return dsss def jtdp(self, x_range, y_range, **kwargs): pointdec = hv.Points(df, kdims=['X_CORD', 'Y_CORD'], vdims=['EVENT_COUNT', 'FEEDER_ID']) selecteddec = pointdec.select(EventType=self.plot, EVENT_COUNT=self.numEvents) dm2 = decimate( selecteddec, x_range=x_range, y_range=y_range, dynamic=False ).opts( style={'Points': dict(alpha=0.0, color='blue', size=self.maxpix)}) return dm2 def dec_tab(self, x_range, y_range, bounds, **kwargs): #%opts Table [ fig_size=550 width=600 height=380] b0 = bounds[0] b2 = bounds[2] b1 = bounds[1] b3 = bounds[3] xr = bounds[2] - bounds[0] yr = bounds[3] - bounds[1] if (not ((xr < 50000) & (yr < 50000))): b0 = b2 = b1 = b3 = 0.0 win32api.MessageBox(0, "SELECTED AREA TOO LARGE! ", 'dec_tab', 0x00001000) pointdec = hv.Points(df, kdims=['X_CORD', 'Y_CORD'], vdims=[ 'EVENT_COUNT', 'EventType', 'SUB', 'day', 'FEEDER_ID', 'XFMR', 'Phase' ]) if (self.BySUB & self.ByFeeder & self.ByDay): selected = pointdec.select(X_CORD=(b0, b2), Y_CORD=(b1, b3), EventType=self.plot, EVENT_COUNT=self.numEvents, SUB=self.Substations, day=self.DayNum, FEEDER_ID=self.Feeder) elif (self.BySUB & self.ByFeeder & (not self.ByDay)): selected = pointdec.select(X_CORD=(b0, b2), Y_CORD=(b1, b3), EventType=self.plot, EVENT_COUNT=self.numEvents, SUB=self.Substations, FEEDER_ID=self.Feeder) elif (self.BySUB & (not self.ByFeeder) & self.ByDay): selected = pointdec.select(X_CORD=(b0, b2), Y_CORD=(b1, b3), EventType=self.plot, EVENT_COUNT=self.numEvents, SUB=self.Substations, day=self.DayNum) elif (self.BySUB & (not self.ByFeeder) & (not self.ByDay)): selected = pointdec.select(X_CORD=(b0, b2), Y_CORD=(b1, b3), EventType=self.plot, EVENT_COUNT=self.numEvents, SUB=self.Substations) elif ((not self.BySUB) & self.ByFeeder & self.ByDay): selected = pointdec.select(X_CORD=(b0, b2), Y_CORD=(b1, b3), EventType=self.plot, EVENT_COUNT=self.numEvents, day=self.DayNum, FEEDER_ID=self.Feeder) elif ((not self.BySUB) & self.ByFeeder & (not self.ByDay)): selected = pointdec.select(X_CORD=(b0, b2), Y_CORD=(b1, b3), EventType=self.plot, EVENT_COUNT=self.numEvents, FEEDER_ID=self.Feeder) elif ((not self.BySUB) & (not self.ByFeeder) & self.ByDay): selected = pointdec.select(X_CORD=(b0, b2), Y_CORD=(b1, b3), EventType=self.plot, EVENT_COUNT=self.numEvents, day=self.DayNum) else: selected = pointdec.select(X_CORD=(b0, b2), Y_CORD=(b1, b3), EventType=self.plot, EVENT_COUNT=self.numEvents) #bp=selected.select( X_CORD=(b0, b2),Y_CORD=(b1, b3) ) tab = hv.Table( selected, kdims=[], vdims=['EventType', 'SUB', 'FEEDER_ID', 'XFMR', 'Phase']) return tab
class GrabCutDashboard(Stream): """ Defines a Dashboard for extracting contours from an Image. """ crs = param.ClassSelector(default=ccrs.PlateCarree(), class_=ccrs.Projection, precedence=-1, doc=""" Projection the inputs and output paths are defined in.""") path_type = param.ClassSelector(default=gv.Path, class_=hv.Path, precedence=-1, is_instance=False, doc=""" The element type to draw into.""") update_contour = param.Action(default=lambda self: self.event(), precedence=1, doc="""Button triggering GrabCut.""") filter_contour = param.Action( default=lambda self: self.filter_stream.event(filter=True), precedence=1, doc="""Button triggering GrabCut.""") width = param.Integer(default=500, precedence=-1, doc=""" Width of the plot""") height = param.Integer(default=500, precedence=-1, doc=""" Height of the plot""") downsample = param.Magnitude(default=1., doc=""" Amount to downsample image by before applying grabcut.""") iterations = param.Integer(default=5, bounds=(0, 20), doc=""" Number of iterations to run the GrabCut algorithm for.""") minimum_size = param.Integer(default=10) def __init__(self, image, fg_data=[], bg_data=[], **params): self.image = image super(GrabCutDashboard, self).__init__(transient=True, **params) self.bg_paths = gv.project(self.path_type(bg_data, crs=self.crs), projection=image.crs) self.fg_paths = gv.project(self.path_type(fg_data, crs=self.crs), projection=image.crs) self.draw_bg = FreehandDraw(source=self.bg_paths) self.draw_fg = FreehandDraw(source=self.fg_paths) self.filter_stream = hv.streams.Stream.define( 'Filter', filter=False)(transient=True) self._initialized = False self._filter = False def extract_foreground(self, **kwargs): img = self.image if self._initialized: bg, fg = self.draw_bg.element, self.draw_fg.element else: self._initialized = True bg, fg = self.bg_paths, self.fg_paths bg, fg = (gv.project(g, projection=img.crs) for g in (bg, fg)) if not len(bg) or not len(fg): return gv.Path([], img.kdims, crs=img.crs) if self.downsample != 1: kwargs = {'dynamic': False} h, w = img.interface.shape(img, gridded=True) kwargs['width'] = int(w * self.downsample) kwargs['height'] = int(h * self.downsample) img = regrid(img, **kwargs) foreground = extract_foreground(img, background=bg, foreground=fg, iterations=self.iterations) with warnings.catch_warnings(): warnings.filterwarnings('ignore') foreground = gv.Path( [contours(foreground, filled=True, levels=1).split()[0].data], kdims=foreground.kdims, crs=foreground.crs) self.result = gv.project(foreground, projection=self.crs) return foreground def filter_contours(self, obj, **kwargs): if kwargs.get('filter'): obj = filter_polygons(obj, minimum_size=self.minimum_size) self.result = obj return obj def view(self): options = dict(width=self.width, height=self.height, xaxis=None, yaxis=None, projection=self.image.crs) dmap = hv.DynamicMap(self.extract_foreground, streams=[self]) dmap = hv.util.Dynamic(dmap, operation=self.filter_contours, streams=[self.filter_stream]) return (regrid(self.image).options(**options) * self.bg_paths * self.fg_paths + dmap.options(**options)).options( merge_tools=False, clone=False)
class link_selections(_base_link_selections): """ Operation which automatically links selections between elements in the supplied HoloViews object. Can be used a single time or be used as an instance to apply the linked selections across multiple objects. """ cross_filter_mode = param.Selector(['overwrite', 'intersect'], default='intersect', doc=""" Determines how to combine selections across different elements.""") index_cols = param.List(default=None, doc=""" If provided, selection switches to index mode where all queries are expressed solely in terms of discrete values along the index_cols. All Elements given to link_selections must define the index_cols, either as explicit dimensions or by sharing an underlying Dataset that defines them.""" ) selection_expr = param.Parameter(default=None, doc=""" dim expression of the current selection or None to indicate that everything is selected.""") selected_color = param.Color(default=None, allow_None=True, doc=""" Color of selected data, or None to use the original color of each element.""") selection_mode = param.Selector( ['overwrite', 'intersect', 'union', 'inverse'], default='overwrite', doc=""" Determines how to combine successive selections on the same element.""") show_regions = param.Boolean(default=True, doc=""" Whether to highlight the selected regions.""") unselected_alpha = param.Magnitude(default=0.1, doc=""" Alpha of unselected data.""") unselected_color = param.Color(default=None, doc=""" Color of unselected data.""") @bothmethod def instance(self_or_cls, **params): inst = super(link_selections, self_or_cls).instance(**params) # Initialize private properties inst._obj_selections = {} inst._obj_regions = {} inst._reset_regions = True return inst @classmethod def _build_selection_streams(cls, inst): # Colors stream style_stream = _Styles( colors=[inst.unselected_color, inst.selected_color], alpha=inst.unselected_alpha) # Cmap streams cmap_streams = [ _Cmap(cmap=inst.unselected_cmap), _Cmap(cmap=inst.selected_cmap), ] def update_colors(*_): colors = [inst.unselected_color, inst.selected_color] style_stream.event(colors=colors, alpha=inst.unselected_alpha) cmap_streams[0].event(cmap=inst.unselected_cmap) if cmap_streams[1] is not None: cmap_streams[1].event(cmap=inst.selected_cmap) inst.param.watch( update_colors, ['unselected_color', 'selected_color', 'unselected_alpha']) # Exprs stream exprs_stream = _Exprs(exprs=[True, None]) def update_exprs(*_): exprs_stream.event(exprs=[True, inst.selection_expr]) # Reset regions if inst._reset_regions: for k, v in inst._region_streams.items(): inst._region_streams[k].event(region_element=None) inst._obj_selections.clear() inst._obj_regions.clear() inst.param.watch(update_exprs, ['selection_expr']) return _SelectionStreams( style_stream=style_stream, exprs_stream=exprs_stream, cmap_streams=cmap_streams, ) @property def unselected_cmap(self): """ The datashader colormap for unselected data """ if self.unselected_color is None: return None return _color_to_cmap(self.unselected_color) @property def selected_cmap(self): """ The datashader colormap for selected data """ return None if self.selected_color is None else _color_to_cmap( self.selected_color) def _expr_stream_updated(self, hvobj, selection_expr, bbox, region_element, **kwargs): if selection_expr: if self.cross_filter_mode == "overwrite": # clear other regions and selections for k, v in self._region_streams.items(): if k is not hvobj: self._region_streams[k].event(region_element=None) self._obj_regions.pop(k, None) self._obj_selections.pop(k, None) # Update selection expression if hvobj not in self._obj_selections or self.selection_mode == "overwrite": if self.selection_mode == "inverse": self._obj_selections[hvobj] = ~selection_expr else: self._obj_selections[hvobj] = selection_expr else: if self.selection_mode == "intersect": self._obj_selections[hvobj] &= selection_expr elif self.selection_mode == "union": self._obj_selections[hvobj] |= selection_expr else: # inverse self._obj_selections[hvobj] &= ~selection_expr # Update region if self.show_regions: if isinstance(hvobj, DynamicMap): el_type = hvobj.type else: el_type = hvobj region_element = el_type._merge_regions( self._obj_regions.get(hvobj, None), region_element, self.selection_mode) self._obj_regions[hvobj] = region_element else: region_element = None # build combined selection selection_exprs = list(self._obj_selections.values()) if self.index_cols: if len(selection_exprs) > 1: vals = set.intersection(*(set(expr.ops[2]['args'][0]) for expr in selection_exprs)) old = selection_exprs[0] selection_expr = dim('new') selection_expr.dimension = old.dimension selection_expr.ops = list(old.ops) selection_expr.ops[2] = dict(selection_expr.ops[2], args=(list(vals), )) else: selection_expr = selection_exprs[0] for expr in selection_exprs[1:]: selection_expr = selection_expr & expr # Set _reset_regions to False so that plot regions aren't automatically # cleared when self.selection_expr is set. self._reset_regions = False self.selection_expr = selection_expr self._reset_regions = True # update this region stream if self._region_streams.get(hvobj, None) is not None: self._region_streams[hvobj].event( region_element=region_element)
class TrainingStateParams(param.Parameterized): """Parameters controlling a TrainingStateController This class implements the :class:`pydrobert.param.optuna.TunableParameterized` interface """ num_epochs = param.Integer( None, bounds=(1, None), softbounds=(10, 100), doc="Total number of epochs to run for. If unspecified, runs " "until the early stopping criterion (or infinitely if disabled) ", ) log10_learning_rate = param.Number( None, softbounds=(-10, -2), doc="Initial optimizer log-learning rate. If unspecified, the initial " "learning rate of the optimizer instance remains unchanged", ) early_stopping_threshold = param.Number( 0.0, bounds=(0, None), softbounds=(0, 1.0), doc="Minimum magnitude decrease in validation metric from the last " "best that resets the early stopping clock. If zero, early stopping " "will never be performed", ) early_stopping_patience = param.Integer( 1, bounds=(1, None), softbounds=(1, 30), doc="Number of epochs after which, if the classifier has failed to " "decrease its validation metric by a threshold, training is " "halted", ) early_stopping_burnin = param.Integer( 0, bounds=(0, None), softbounds=(0, 10), doc="Number of epochs before the early stopping criterion kicks in", ) reduce_lr_threshold = param.Number( 0.0, bounds=(0, None), softbounds=(0, 1.0), doc="Minimum magnitude decrease in validation metric from the last " "best that resets the clock for reducing the learning rate. If zero, " "the learning rate will never be reduced", ) reduce_lr_factor = param.Magnitude( 0.1, softbounds=(0.1, 0.5), inclusive_bounds=(False, False), doc="Factor by which to multiply the learning rate if there has " 'been no improvement in the after "reduce_lr_patience" ' "epochs", ) reduce_lr_patience = param.Integer( 1, bounds=(1, None), softbounds=(1, 30), doc="Number of epochs after which, if the classifier has failed to " "decrease its validation metric by a threshold, the learning rate is " "reduced", ) reduce_lr_cooldown = param.Integer( 0, bounds=(0, None), softbounds=(0, 10), doc="Number of epochs after reducing the learning rate before we " "resume checking improvements", ) reduce_lr_log10_epsilon = param.Number( -8, bounds=(None, 0), doc="The log10 absolute difference between learning rates that, " "below which, reducing the learning rate is considered meaningless", ) reduce_lr_burnin = param.Integer( 0, bounds=(0, None), softbounds=(0, 10), doc="Number of epochs before the criterion for reducing the learning " "rate kicks in", ) seed = param.Integer( None, doc="Seed used for training procedures (e.g. dropout). If " "unset, will not touch torch's seeding", ) keep_last_and_best_only = param.Boolean( True, doc="If the model is being saved, keep only the model and optimizer " "parameters for the last and best epoch (in terms of validation loss)." ' If False, save every epoch. See also "saved_model_fmt" and ' '"saved_optimizer_fmt"', ) saved_model_fmt = param.String( "model_{epoch:03d}.pt", doc="The file name format string used to save model state information." " Entries from the state csv are used to format this string (see " "TrainingStateController)", ) saved_optimizer_fmt = param.String( "optim_{epoch:03d}.pt", doc="The file name format string used to save optimizer state " "information. Entries from the state csv are used to format this " "string (see TrainingStateController)", ) @classmethod def get_tunable(cls): """Returns a set of tunable parameters""" return { "num_epochs", "log10_learning_rate", "early_stopping_threshold", "early_stopping_patience", "early_stopping_burnin", "reduce_lr_factor", "reduce_lr_threshold", "reduce_lr_patience", "reduce_lr_cooldown", "reduce_lr_burnin", } @classmethod def suggest_params(cls, trial, base=None, only=None, prefix=""): """Populate a parameterized instance with values from trial""" if only is None: only = cls.get_tunable() params = cls() if base is None else base pdict = params.param.params() if "log10_learning_rate" in only: softbounds = pdict["log10_learning_rate"].get_soft_bounds() params.log10_learning_rate = trial.suggest_uniform( prefix + "log10_learning_rate", *softbounds) if "num_epochs" in only: softbounds = pdict["num_epochs"].get_soft_bounds() params.num_epochs = trial.suggest_int(prefix + "num_epochs", *softbounds) if params.num_epochs is None: num_epochs = float("inf") else: num_epochs = params.num_epochs # if we sample patience and burnin so that their collective total # reaches or exceeds the number of epochs, they are effectively # disabled. Rather than allowing vast sums above the number of epochs, # we only allow the sum to reach the remaining epochs remaining_epochs = num_epochs if "early_stopping_patience" not in only: remaining_epochs -= params.early_stopping_patience if "early_stopping_burnin" not in only: remaining_epochs -= params.early_stopping_burnin remaining_epochs = max(0, remaining_epochs) if remaining_epochs and "early_stopping_threshold" in only: softbounds = pdict["early_stopping_threshold"].get_soft_bounds() params.early_stopping_threshold = trial.suggest_uniform( prefix + "early_stopping_threshold", *softbounds) if not params.early_stopping_threshold: remaining_epochs = 0 if remaining_epochs and "early_stopping_patience" in only: softbounds = pdict["early_stopping_patience"].get_soft_bounds() softbounds = tuple(min(x, remaining_epochs) for x in softbounds) params.early_stopping_patience = trial.suggest_int( prefix + "early_stopping_patience", *softbounds) remaining_epochs -= params.early_stopping_patience assert remaining_epochs >= 0 if remaining_epochs and "early_stopping_burnin" in only: softbounds = pdict["early_stopping_burnin"].get_soft_bounds() softbounds = tuple(min(x, remaining_epochs) for x in softbounds) params.early_stopping_burnin = trial.suggest_int( prefix + "early_stopping_burnin", *softbounds) remaining_epochs -= params.early_stopping_burnin assert remaining_epochs >= 0 # we do the same thing, but for the learning rate scheduler remaining_epochs = num_epochs if "reduce_lr_patience" not in only: remaining_epochs -= params.reduce_lr_patience if "reduce_lr_burnin" not in only: remaining_epochs -= params.reduce_lr_burnin remaining_epochs = max(0, remaining_epochs) if remaining_epochs and "reduce_lr_threshold" in only: softbounds = pdict["reduce_lr_threshold"].get_soft_bounds() params.reduce_lr_threshold = trial.suggest_uniform( prefix + "reduce_lr_threshold", *softbounds) if not params.reduce_lr_threshold: remaining_epochs = 0 if remaining_epochs and "reduce_lr_patience" in only: softbounds = pdict["reduce_lr_patience"].get_soft_bounds() softbounds = tuple(min(x, remaining_epochs) for x in softbounds) params.reduce_lr_patience = trial.suggest_int( prefix + "reduce_lr_patience", *softbounds) remaining_epochs -= params.reduce_lr_patience if remaining_epochs and "reduce_lr_burnin" in only: softbounds = pdict["reduce_lr_burnin"].get_soft_bounds() softbounds = tuple(min(x, remaining_epochs) for x in softbounds) params.reduce_lr_burnin = trial.suggest_int( prefix + "reduce_lr_burnin", *softbounds) if remaining_epochs and "reduce_lr_factor" in only: softbounds = pdict["reduce_lr_factor"].get_soft_bounds() params.reduce_lr_factor = trial.suggest_uniform( prefix + "reduce_lr_factor", *softbounds) if remaining_epochs and "reduce_lr_cooldown" in only: softbounds = pdict["reduce_lr_cooldown"].get_soft_bounds() params.reduce_lr_cooldown = trial.suggest_int( prefix + "reduce_lr_cooldown", *softbounds) return params
class MyParamMagnitude(param.Parameterized): magnitude = param.Magnitude(default=0.9)
class OSMExplorer(hv.streams.Stream): alpha = param.Magnitude(default=0.75, doc="Alpha value for the map opacity") cmap = param.ObjectSelector(default=cm["fire"], objects=cm.values()) min_count = param.Number(default=0, bounds=(0, 100)) output = parambokeh.view.Plot()