def set_data(self, data, column=None): """Set data properties and update all dependent properties.""" if isinstance(data, pd.DataFrame): data = ColumnDataSource(data) if isinstance(data, ColumnDataSource): self.source = data if column is not None: self.column = column else: self.values = data self.update() self.calculate()
def test_data_accepts_dataframe_column_categoricalindex(self, pd): columns = pd.CategoricalIndex(['a', 'b']) data = [[0, 2], [1, 3]] df = pd.DataFrame(columns=columns, data=data) ds = ColumnDataSource() assert ds.data == {} ds.data = df assert set(df.columns).issubset(set(ds.column_names)) for key in columns: assert isinstance(ds.data[key], np.ndarray) assert list(df[key]) == list(ds.data[key]) assert isinstance(ds.data['index'], np.ndarray) assert [0, 1] == list(ds.data['index']) assert set(ds.column_names) - set(df.columns) == set(["index"])
def build_source(self): """Calculate stats and builds and returns source for whiskers.""" self.calc_quartiles() x_label = self.get_dodge_label() x_w0_label = self.get_dodge_label(shift=(self.whisker_width / 2.0)) x_w1_label = self.get_dodge_label(shift=-(self.whisker_width / 2.0)) # span0, whisker bar0, span1, whisker bar1 x0s = [x_label, x_w0_label, x_label, x_w0_label] y0s = [self.w0, self.w0, self.q3, self.w1] x1s = [x_label, x_w1_label, x_label, x_w1_label] y1s = [self.q1, self.w0, self.w1, self.w1] return ColumnDataSource(dict(x0s=x0s, y0s=y0s, x1s=x1s, y1s=y1s))
def _teamsize_figure(p, team_size, entry_count_color, author_count_color): p.circle( "date", "entry_count", source=ColumnDataSource(team_size), color=entry_count_color, fill_alpha=0.1, line_alpha=0.2, ) p.circle( "date", "author_count", source=ColumnDataSource(team_size), y_range_name="team_range", color=author_count_color, fill_alpha=0.1, line_alpha=0.2, ) p.line( "date", "entry_count_lowess", source=ColumnDataSource(team_size), line_width=2, color=entry_count_color, legend="{}: Entry Count".format(team_size.name), ) p.line( "date", "author_count_lowess", source=ColumnDataSource(team_size), y_range_name="team_range", line_width=2, color=author_count_color, legend="{}: Team Size".format(team_size.name), ) return p
def __init__(self, columns=None, df=None, iterable=None, default=None, items=None, **properties): """Create a lazy evaluated attribute specification. Args: columns: a list of column labels df(:class:`~pandas.DataFrame`): the data source for the attribute spec. iterable: an iterable of distinct attribute values default: a value to use as the default attribute when no columns are passed items: the distinct values in columns. If items is provided as input, then the values provided are used instead of being calculated. This can be used to force a specific order for assignment. **properties: other properties to pass to parent :class:`HasProps` """ properties['columns'] = self._ensure_list(columns) if df is not None: properties['data'] = ColumnDataSource(df) if default is None and iterable is not None: default_iter = copy(iterable) properties['default'] = next(iter(default_iter)) elif default is not None: properties['default'] = default if iterable is not None: properties['iterable'] = iterable if items is not None: properties['items'] = items super(AttrSpec, self).__init__(**properties) if self.default is None and self.iterable is not None: self.default = next(iter(copy(self.iterable))) if self.data is not None and self.columns is not None: if df is None: df = self.data.to_df() self._generate_items(df, columns=self.columns) if self.items is not None and self.iterable is not None: self.attr_map = self._create_attr_map()
def test_stream_series_to_ds_created_from_df(self): data = pd.DataFrame(dict(a=[10], b=[20], c=[30])).set_index('c') ds = ColumnDataSource(data) ds._document = "doc" notify_owners_stuff = {} def notify_owners_mock(*args, **kw): notify_owners_stuff['args'] = args notify_owners_stuff['kw'] = kw ds.data._notify_owners = notify_owners_mock stream_stuff = {} data_stream = ds.data._stream def stream_wrapper(*args, **kwargs): stream_stuff['args'] = args stream_stuff['kwargs'] = kwargs data_stream(*args, **kwargs) ds.data._stream = stream_wrapper ds._stream(pd.Series([11, 21, 31], index=list('abc')), 7) self.assertEqual(len(stream_stuff['args']), 5) expected_stream_args = ("doc", ds, dict(a=np.array([11]), b=np.array([21]), c=np.array([31])), 7, None) for i, (arg, ex_arg) in enumerate(zip(stream_stuff['args'], expected_stream_args)): if i == 2: arg = {k: v.values for k, v in arg.to_dict('series').items()} self._assert_equal_dicts_of_arrays(arg, ex_arg) else: self.assertEqual(arg, ex_arg) self.assertEqual(stream_stuff['kwargs'], {}) self.assertEqual(len(notify_owners_stuff['args']), 1) self._assert_equal_dicts_of_arrays(notify_owners_stuff['args'][0], dict(a=np.array([10]), b=np.array([20]), c=np.array([30]))) self._assert_equal_dicts_of_arrays(dict(ds.data), dict(a=np.array([10, 11]), b=np.array([20, 21]), c=np.array([30, 31])))
def check_widget_box_children_prop(layout_callable): ## component subclasses are layouts, widgets and plots components = [Slider()] # Test layout accepts splatted components layout1 = layout_callable(*components) assert layout1.children == components # Test layout accepts children argument layout2 = layout_callable(children=components) assert layout2.children == components # Test value error raised when non-layout is provided as children with pytest.raises(ValueError): layout_callable(children=[ColumnDataSource()])
def test_stream_good_data(self): ds = ColumnDataSource(data=dict(a=[10], b=[20])) ds._document = "doc" stuff = {} def mock(*args, **kw): stuff['args'] = args stuff['kw'] = kw ds.data._stream = mock # public implementation of stream ds._stream(dict(a=[11, 12], b=[21, 22]), "foo") assert stuff['args'] == ("doc", ds, dict(a=[11, 12], b=[21, 22]), "foo", None) assert stuff['kw'] == {}
def generate_timeline_desc_data_source(xs: list, ys: list, font_size: int = 18 ) -> ColumnDataSource: """ :param xs: :param ys: :param font_size: :return: """ texts = ['', ''] font_size = 14 # debug++ sizes = ['{}pt'.format(font_size)] * len(xs) return ColumnDataSource(dict(x=xs, y=ys, text=texts, size=sizes))
def __init__(self, logger, spectrumId, dic, udic, pdata, dataSource, reference): Observer.__init__(self, logger) self.logger = logger self.id = spectrumId self.dic = dic self.udic = udic self.pdata = pdata self.mpdata = np.array(map(lambda x: -x, pdata)) self.dataSource = dataSource reference.addObserver(lambda n: referenceObserver(self, n)) self.sources = dict() self.sources['peaks'] = ColumnDataSource(data=dict(x=[], y=[]))
def test_init_groupby_with_None_subindex_name(self): df = pd.DataFrame({ "A": [1, 2, 3, 4] * 2, "B": [10, 20, 30, 40] * 2, "C": range(8) }) group = df.groupby(['A', [10, 20, 30, 40] * 2]) ds = ColumnDataSource(data=group) s = group.describe() self.assertTrue(len(ds.column_names)) == 41 self.assertIsInstance(ds.data['index'], np.ndarray) for key in s.columns.values: k2 = "_".join(key) self.assertIsInstance(ds.data[k2], pd.Series) self.assertEquals(list(s[key]), list(ds.data[k2]))
def test_stream_bad_data(self): ds = ColumnDataSource(data=dict(a=[10], b=[20])) with pytest.raises(ValueError, match=r"Must stream updates to all existing columns \(missing: a, b\)"): ds.stream(dict()) with pytest.raises(ValueError, match=r"Must stream updates to all existing columns \(missing: b\)"): ds.stream(dict(a=[10])) with pytest.raises(ValueError, match=r"Must stream updates to all existing columns \(extra: x\)"): ds.stream(dict(a=[10], b=[10], x=[10])) with pytest.raises(ValueError, match=r"Must stream updates to all existing columns \(missing: b, extra: x\)"): ds.stream(dict(a=[10], x=[10])) with pytest.raises(ValueError, match=r"All streaming column updates must be the same length"): ds.stream(dict(a=[10], b=[10, 20])) with pytest.raises(ValueError, match=r"stream\(...\) only supports 1d sequences, got ndarray with size \(.*"): ds.stream(dict(a=[10], b=np.ones((1,1))))
def test_init_groupby_with_None_subindex_name(self, pd): df = pd.DataFrame({ "A": [1, 2, 3, 4] * 2, "B": [10, 20, 30, 40] * 2, "C": range(8) }) group = df.groupby(['A', [10, 20, 30, 40] * 2]) ds = ColumnDataSource(data=group) s = group.describe() assert len(ds.column_names) == 17 assert isinstance(ds.data['index'], np.ndarray) for key in s.columns.values: k2 = "_".join(key) assert isinstance(ds.data[k2], np.ndarray) assert list(s[key]) == list(ds.data[k2])
def __init__(self, logger, spectrumId, pdata, dataSource, reference): Observer.__init__(self, logger) self.logger = logger self.id = spectrumId self.pdata = pdata self.dataSource = dataSource reference.addObserver(lambda n: referenceObserver(self, n)) self.sources = dict() self.sources['integration'] = ColumnDataSource( data=dict(x=[], y=[], width=[], height=[])) self.initIntegral = None
def make_ajax_query_plot(data): source_query = ColumnDataSource() source_query.data = data plot = figure(plot_height=300, sizing_mode='scale_width') plot.line('x', 'y', source=source_query, line_width=4) plot.circle('x', 'y', source=source_query, size=8, fill_color="white", color="red") script, div = components(plot) return script, div
def lines(fig, x, y, df=None, source=None, groups=None, **kwargs): """lines: add lines to a figure Args: fig (:py:class:`~bokeh.plotting.Plot`): bokeh Plot object x (str): string for x component y (str): string for y component df (:py:class:`~pandas.DataFrame`): pandas DataFram source (:py:class:`~bokeh.models.ColumnDataSource`): bokeh ColumnDataSource object groups (str, list(str)): string or list of strings for columns to group by kwargs: keyword arguments to pass to fig.line Example: .. bokeh-plot:: :source-position: above import pandas as pd from bokeh.plotting import figure, show, hplot from bokehutils.geom import lines df = pd.DataFrame([[1,2], [2,5], [3,9]], columns=["x", "y"]) f = figure(title="Line plot", plot_width=400, plot_height=400) lines(f, "x", "y", df, legend="y") lines(f, "x", "x", df, legend="x", color="red") show(f) """ logger.debug("Adding points to figure {}".format(fig)) if groups is None: fig.line(x=x, y=y, source=source, **kwargs) else: try: grouped = df.groupby(groups) except: raise colors = colorbrewer(datalen=len(grouped.groups.keys())) for k, color in zip(grouped.groups.keys(), colors): name = k group = grouped.get_group(name) source = ColumnDataSource(group) if 'legend' in kwargs: kwargs['legend'] = name if 'color' in kwargs: kwargs['color'] = color fig.line(x=x, y=y, source=source, **kwargs)
def generate_reference_figure(n=5): ''' Generate dummy scatter with N dots as a bokeh figure ''' p1 = figure(plot_width=400, plot_height=400, tools="tap", title="Click the dots to switch images") ### in this case, "source origin" will refer to the active data of the ### interactive plot. We will need this later to pass to our JS code if we ### want to access it in the callback source_origin = ColumnDataSource( data=dict(x=np.arange(n), y=np.random.randint(0, 20, 5))) p1.circle('x', 'y', size=20, source=source_origin) return p1, source_origin
def test__stream_good_datetime64_data(self): now = dt.datetime.now() dates = np.array([now+dt.timedelta(i) for i in range(1, 10)], dtype='datetime64') ds = ColumnDataSource(data=dict(index=dates, b=list(range(1, 10)))) ds._document = "doc" stuff = {} mock_setter = object() def mock(*args, **kw): stuff['args'] = args stuff['kw'] = kw ds.data._stream = mock # internal implementation of stream new_date = np.array([now+dt.timedelta(10)], dtype='datetime64') ds._stream(dict(index=new_date, b=[10]), "foo", mock_setter) self.assertTrue(np.array_equal(stuff['args'][2]['index'], new_date))
def _init_bathymetric_map_data(self): x_wm, y_wm = self._epsg4326_to_epsg3857( self.env.cds_df.LONGITUDE.as_matrix(), self.env.cds_df.LATITUDE.as_matrix()) aux_df = pd.DataFrame( dict(X_WMTS=x_wm, Y_WMTS=y_wm, STNNBR=list(self.env.cds_df[STNNBR]))) aux_df.drop_duplicates(subset=STNNBR, keep='first', inplace=True) lg.info('>> AUX DF LEN: {}'.format(aux_df.index.size)) new_index_column = list(range(aux_df.index.size)) lg.info('>> AUX DF new_index_column: {}'.format(len(new_index_column))) aux_df = aux_df.assign(NEW_INDEX=new_index_column) aux_df.set_index(keys='NEW_INDEX', inplace=True) self.env.wmts_map_df = aux_df.copy(deep=True) self.env.wmts_map_source = ColumnDataSource(self.env.wmts_map_df)
def create_bar_chart(data, title, x_name, y_name, width=1200, height=300): source = ColumnDataSource(data) xdr = FactorRange(factors=data[x_name]) ydr = Range1d(start=0, end=max(data[y_name]) * 1.5) plot = figure(title=title, x_range=xdr, y_range=ydr) glyph = VBar(x=x_name, top=y_name, bottom=0, width=.8) plot.add_glyph(source, glyph) xaxis = LinearAxis() yaxis = LinearAxis() plot.add_layout(Grid(dimension=0, ticker=xaxis.ticker)) plot.add_layout(Grid(dimension=1, ticker=yaxis.ticker)) return plot
def test_patch_good_simple_indices(self): ds = ColumnDataSource(data=dict(a=[10, 11], b=[20, 21])) ds._document = "doc" stuff = {} mock_setter = object() def mock(*args, **kw): stuff['args'] = args stuff['kw'] = kw ds.data._patch = mock ds.patch(dict(a=[(0, 100), (1, 101)], b=[(0, 200)]), mock_setter) assert stuff['args'] == ("doc", ds, dict(a=[(0, 100), (1, 101)], b=[(0, 200)]), mock_setter) assert stuff['kw'] == {}
def generate_timeline_label_data_source( source_graph: Graph) -> ColumnDataSource: """ :param source_graph: :return: """ df = convert_node_attribute2df(source_graph) df = df.reset_index() df['pep_id'] = df['index'] df = df.set_index('index') df = df[['pep_id', 'Created_dt']] df['displayed_text'] = [""] * len(df.index) # 初期状態では表示しないので空文字を格納しておく df['y'] = 1.01 return ColumnDataSource(df)
def test_stream_good_data(self): ds = ColumnDataSource(data=dict(a=[10], b=[20])) ds._document = "doc" stuff = {} mock_setter = object() def mock(*args, **kw): stuff['args'] = args stuff['kw'] = kw ds.data._stream = mock ds.stream(dict(a=[11, 12], b=[21, 22]), "foo", mock_setter) self.assertEqual( stuff['args'], ("doc", ds, dict(a=[11, 12], b=[21, 22]), "foo", mock_setter)) self.assertEqual(stuff['kw'], {})
def test__stream_good_data(self): ds = ColumnDataSource(data=dict(a=[10], b=[20])) ds._document = "doc" stuff = {} mock_setter = object() def mock(*args, **kw): stuff['args'] = args stuff['kw'] = kw ds.data._stream = mock # internal implementation of stream ds._stream(dict(a=[11, 12], b=[21, 22]), "foo", mock_setter) assert stuff['args'] == ("doc", ds, dict(a=[11, 12], b=[21, 22]), "foo", mock_setter) assert stuff['kw'] == {}
def _apply_inferred_index(self): """Configure chart when labels are provided as index instead of as kwarg.""" # try to infer grouping vs stacking labels if (self.attributes['label'].columns is None and self.values.selection is not None): if self.attributes['stack'].columns is not None: special_column = 'unity' else: special_column = 'index' self._data['label'] = special_column self.attributes['label'].setup(data=ColumnDataSource(self._data.df), columns=special_column) self.xlabel = ''
def compareCoinPlot(request,xkey,ykey,coinX = False, coinY = False,begin = False, end = False): print("you are in the compare component view") baseURL = "http://18.220.161.116/ajax/compare/" ajaxRoute = baseURL + str(xkey) + "/" + str(ykey) + "/" baseNameX = "doggo" baseNameY = "doggo" if(coinX): ajaxRoute += (str(coinX) + "/") baseNameX = str(coinX).capitalize() if(coinY): ajaxRoute += (str(coinY) + "/") baseNameY = str(coinY).capitalize() if(begin): ajaxRoute += (str(begin) + "/") if(end): ajaxRoute += str(end) #originally ajaxDataSource, now is a get route into a column data source. ajaxDataSource is good for real time data jsonArr = AWSsign.amazonCall(ajaxRoute).json() # ["x"][0],jsonDict["y"][0] x, y, date = parseArr(jsonArr) df = pd.DataFrame({'x': x, 'y' : y , 'date' : date, 'xLabel' : jsonArr[0]['xName'],'yLabel' : jsonArr[0]['yName']}) # df = df.fillna(0) # print("dataframe:\n",df) titleStr = baseNameX + " " + str(xkey) + " vs " + baseNameY + " " + str(ykey) TOOLTIPS = [ ("Price " + baseNameX, "$@x{0,0.00}"), ("Price " + baseNameY, "$@y{0,0.00}"), ("Date" , "@date") ] FORMAT = {"Date" : "datetime"} plot = figure(plot_width=1000, plot_height=700, x_axis_label = str(xkey), y_axis_label = str(ykey), title = titleStr) plot.toolbar.logo = None plot.toolbar_location = None hover = HoverTool(tooltips=TOOLTIPS, mode = 'vline', formatters = FORMAT) plot.add_tools(hover) source = ColumnDataSource(df) #print("CDSx::",source.data['x'],"\nCDSy::",source.data['y'],"\nCDScols::",source.column_names) plot.scatter(x='x',y='y', source=source) #print("plot complete:",plot.select(dict(type=HoverTool))[0].tooltips) script, div = components(plot) context = { "script" : script, "div" : div } #print("context:", context) template = loader.get_template("bokehGraphs/ajaxGraph.html") return HttpResponse(template.render(context = context, request = request))
def to_bokeh(self, columns: Optional[List[str]] = None): """Convert the dataset to a bokeh ColumnDataSource """ if columns is None: columns = [ col.name if col.name is not None else "column_{}".format(col.identifier) for col in self.columns ] return ColumnDataSource({ column.name: [ row.get_value(column.identifier if column.identifier >= 0 else column.name) for row in self.rows ] for column in self.columns })
def create_features_charts(self): # create charts for visualising features cds_feats_data = {"x": [], "y": []} for i in range(self.channels_number): cds_feats_data["value_" + str(i)] = [] self.cds_feats = ColumnDataSource(data=cds_feats_data) colors = [ "#75968f", "#a5bab7", "#c9d9d3", "#e2e2e2", "#dfccce", "#ddb7b1", "#cc7878", "#933b41", "#550b1d", ] mapper = LinearColorMapper(palette=colors, low=10, high=70) self.figs_feats = [ figure(plot_width=400, plot_height=120, toolbar_location=None) for i in range(self.channels_number) ] for f in self.figs_feats: f.axis.visible = False f.xgrid.grid_line_color = None f.ygrid.grid_line_color = None [ self.figs_feats[i].rect( x="x", y="y", width=1, height=1, source=self.cds_feats, line_color=None, fill_color={ "field": "value_" + str(i), "transform": mapper }, ) for i in range(self.channels_number) ]
def plot_with_hover(plot, x, y, f, colors, entities, title): ''' Add hover over individual datapoints. We use dirty trick of plotting invisible scatter points so that we can use exisitng plot. doc, TBA ''' # Compute distance from current article in terms of first two principal components centroid = (x[0], y[0]) R = np.sqrt((x - centroid[0])**2 + (y - centroid[1])**2) maxR = np.amax(R) dists = ["{:0.2f}".format(x / maxR) for x in R] # Get number of entities from for each article = datapoint numEnts = [len(en) for en in entities] # Create dataframe holding all the data that we want to appear on the final plot, including hover d = { 'x': x, 'y': y, 'f': f, 'ents': entities, 'numEnts': numEnts, 'dists': dists, 'tit': title } # works also for series of different length plot_df = dict([(k, pd.Series(v)) for k, v in d.items()]) # Plot empty circles and specify source. This enables us to add hover. plot.circle('x', 'y', fill_color=colors, fill_alpha=0.6, radius=0.025, line_color=None, source=ColumnDataSource(data=plot_df)) # Add Hover tooltips hover = gimmeHover() plot.add_tools(hover) plot = recommendopposite(R, title, plot) return plot
def plot_map(df2): from bokeh.sampledata import us_states from bokeh.models.sources import ColumnDataSource from bokeh.plotting import * us_states = us_states.data.copy() del us_states["HI"] del us_states["AK"] states = [a['name'] for a in us_states.values()] rates = [df2.ix[state_name]['Larceny Theft'] for state_name in states] cm = plt.get_cmap('YlOrRd') c_map = plt.cm.ScalarMappable(cmap=cm) c_map.set_clim(df2['Larceny Theft'].min(), df2['Larceny Theft'].max()) state_colors0 = [c_map.to_rgba(rate) for rate in rates] state_colors = [(c[0] * 255, c[1] * 255, c[2] * 255) for c in state_colors0] state_hex = [('#%02x%02x%02x' % c) for c in state_colors] state_xs = [us_states[code]["lons"] for code in us_states] state_ys = [us_states[code]["lats"] for code in us_states] TOOLS = "pan,wheel_zoom,box_zoom,reset,hover,save" source = ColumnDataSource(data=dict(rate=rates, state=states)) p = figure(title="State Crime Rates", toolbar_location="left", plot_width=1100, plot_height=700, tools=TOOLS) p.patches(state_xs, state_ys, fill_color=state_hex, line_color="#884444", line_width=2, source=source) hover = p.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([ ("State", "@state"), ('rate', '@rate'), ("(x,y)", "($x, $y)"), ]) show(p)