import param import numpy as np import holoviews as hv hv.extension('bokeh', 'matplotlib') from holoviews.operation import histogram hv.help(histogram) boxw = hv.BoxWhisker(np.random.randn(10000)) histop_instance = histogram.instance(num_bins=50) boxw + histop_instance(boxw).relabel('num_bins=50') + histogram(boxw, bin_range=(0, 3)).relabel('bin_range=(0, 3)') holomap = hv.HoloMap({(i*0.1+0.1): hv.BoxWhisker(np.random.randn(10000)*(i*0.1+0.1)) for i in range(5)}, kdims='Sigma') holomap + histogram(holomap) from holoviews.operation import timeseries def time_series(T = 1, N = 100, mu = 0.1, sigma = 0.1, S0 = 20): """Parameterized noisy time series""" dt = float(T)/N t = np.linspace(0, T, N) W = np.random.standard_normal(size = N) W = np.cumsum(W)*np.sqrt(dt) # standard brownian motion X = (mu-0.5*sigma**2)*t + sigma*W S = S0*np.exp(X) # geometric brownian motion return S curve = hv.Curve(time_series(N=1000)) %%opts Scatter [width=600] (color='black') smoothed = curve * timeseries.rolling(curve) * timeseries.rolling_outlier_std(curve) smoothed from holoviews.operation import Operation class residual(Operation): """ Subtracts two curves from one another. """ label = param.String(default='Residual', doc=""" Defines the label of the returned Element.""") def _process(self, element, key=None): # Get first and second Element in overlay el1, el2 = element.get(0), element.get(1) # Get x-values and y-values of curves xvals = el1.dimension_values(0) yvals = el1.dimension_values(1) yvals2 = el2.dimension_values(1) # Return new Element with subtracted y-values # and new label return el1.clone((xvals, yvals-yvals2), vdims=self.p.label) %%opts Curve [width=600] Overlay [xaxis=None] (smoothed + residual(smoothed)).cols(1) rolled = hv.HoloMap({(w, str(wt)): timeseries.rolling(curve, rolling_window=w, window_type=wt) for w in [10, 25, 50, 100, 200] for wt in [None, 'hamming', 'triang']}, kdims=['Window', 'Window Type']) rolled %%opts Curve [width=600] Overlay [legend_position='top_left'] (curve(style=dict(color='black')) * rolled + residual(curve * rolled)).cols(1) %%output backend="matplotlib" from scipy.misc import ascent stairs_image = hv.Image(ascent()[200:500, :], bounds=[0, 0, ascent().shape[1], 300], label="stairs") stairs_image %%output backend="matplotlib" from scipy import ndimage class image_filter(hv.Operation): sigma = param.Number(default=5) type_ = param.String(default="low-pass") def _process(self, element, key=None): xs = element.dimension_values(0, expanded=False) ys = element.dimension_values(1, expanded=False) # setting flat=False will preserve the matrix shape data = element.dimension_values(2, flat=False) if self.p.type_ == "high-pass": new_data = data - ndimage.gaussian_filter(data, self.p.sigma) else: new_data = ndimage.gaussian_filter(data, self.p.sigma) label = element.label + " ({} filtered)".format(self.p.type_) # make an exact copy of the element with all settings, just with different data and label: element = element.clone((xs, ys, new_data), label=label) return element stairs_map = hv.HoloMap({sigma: image_filter(stairs_image, sigma=sigma) for sigma in range(0, 12, 1)}, kdims="sigma") stairs_map %%output backend="matplotlib" image_filter(stairs_map, type_="high-pass")