def layout(): date_select = DateRangeSlider( name="period", title="Period:", value=(start, end), bounds=(bounds_start, bounds_end), value_labels='show', range=(dict(days=1), None)) date_select.on_change('value', on_date_change) country_select = Select(title="Host Site Country:", value="World", options=country_choices) country_select.on_change('value', on_country_change) controls = VBoxModelForm(_children=[Paragraph(text="Date Range"), Paragraph(text=""), # spacing hack Paragraph(text=""), date_select, country_select]) vboxsmall = VBoxModelForm(_children=[controls, source_par]) #hbox1 = HBox(children=[job_loc_plot_builder(), vboxsmall]) #hbox2 = HBox(children=[weekday_builder(), jobtype_builder()]) #layout = VBox(children=[hbox1, hbox2]) layout = VBox(children=[vboxsmall]) return layout
def createControls(self): # Setup Select Panes and Input Widgets #Obr - Overburden rock #ResR - Reservoir rock #Obf - Oberburden fluid #Resf - Reservoir fluid self.selectObr = Select(value=self.odict_rocks.keyslist()[0], options=self.odict_rocks.keyslist(), title="Rock Model") self.selectResR = Select(value=self.odict_rocks.keyslist()[0], options=self.odict_rocks.keyslist(), title="Rock Model") self.selectObf = Select(value=self.odict_fluids.keyslist()[0], options=self.odict_fluids.keyslist(), title="Fluid Model") self.selectResf = Select(value=self.odict_fluids.keyslist()[0], options=self.odict_fluids.keyslist(), title="Fluid Model") self.selectPres = Select(value=self.odict_pres.keyslist()[0], options=self.odict_pres.keyslist(), title="Pressure Scenario") self.slideDepth = Slider(start=0, end=10000, value=self.init_depth, step=10, title='Depth (TVDSS)', callback_policy='mouseup') self.selectObr.on_change('value', self.on_selection_change) self.selectResR.on_change('value', self.on_selection_change) self.selectObf.on_change('value', self.on_selection_change) self.selectResf.on_change('value', self.on_selection_change) self.selectPres.on_change('value', self.on_selection_change) self.slideDepth.on_change('value', self.on_selection_change)
def set_input_selector(self): # print ("set_input_selector") """Creates and configures each selector (drop-down menu).""" col_names = [x['name'] for x in self.columns] col_names.append('None') self.plot_selector = Select.create( title="PlotType", name="plot_type", value=self.plot_type, options=["line", "scatter", "bar"], ) self.x_selector = Select.create( name="x", value=self.x, options=col_names, ) self.y_selector = Select.create( name="y", value=self.y, options=col_names, ) self.agg_selector = Select.create( name='agg', value=self.agg, options=['sum', 'mean', 'last', 'count', 'percent'], )
def make_sidebar(): global sidebar, master_selector master_selector = Select(title="MASTOR SELECTOR:", value="test", options=["test", 'sandbox', 'template', "timeseries", "mdr_history"]) def selector_func(attrname, old, new): new_master_plot( master_selector.value ) master_selector.on_change('value', selector_func ) sidebar = vform(master_selector)
def make_inputs(self): self.ticker1_select = Select( name='ticker1', title='Portfolio:', value='MSFT', options = ['INTC', 'Tech Basket', 'IBB', 'IGOV'] ) self.ticker2_select = Select( name='ticker2', title='Risk/Performance Metric:', value='Price', options=['Daily Prices', 'Daily Returns', 'Daily Cum Returns', 'Max DD Percentage', 'Percentage Up Days', 'Rolling 95% VaR', 'Rolling Ann. Volatility', 'Rolling Worst Dly. Loss', 'Ann. Sharpe Ratio'] ) self.ticker3_select = TextInput( name='ticker3', title='Window Size:', value='63' ) self.ticker4_select = TextInput( name='ticker4', title='Start Date:', value='2010-01-01' ) self.ticker5_select = TextInput( name='ticker5', title='End Date:', value='2015-08-01' )
def create_layout(self): # create figure self.x_range = Range1d(start=self.model.map_extent[0], end=self.model.map_extent[2], bounds=None) self.y_range = Range1d(start=self.model.map_extent[1], end=self.model.map_extent[3], bounds=None) self.fig = Figure(tools='wheel_zoom,pan', x_range=self.x_range, y_range=self.y_range) self.fig.plot_height = 660 self.fig.plot_width = 990 self.fig.axis.visible = False # add tiled basemap self.tile_source = WMTSTileSource(url=self.model.basemap) self.tile_renderer = TileRenderer(tile_source=self.tile_source) self.fig.renderers.append(self.tile_renderer) # add datashader layer self.image_source = ImageSource(url=self.model.service_url, extra_url_vars=self.model.shader_url_vars) self.image_renderer = DynamicImageRenderer(image_source=self.image_source) self.fig.renderers.append(self.image_renderer) # add ui components axes_select = Select.create(name='Axes', options=self.model.axes) axes_select.on_change('value', self.on_axes_change) field_select = Select.create(name='Field', options=self.model.fields) field_select.on_change('value', self.on_field_change) aggregate_select = Select.create(name='Aggregate', options=self.model.aggregate_functions) aggregate_select.on_change('value', self.on_aggregate_change) transfer_select = Select.create(name='Transfer Function', options=self.model.transfer_functions) transfer_select.on_change('value', self.on_transfer_function_change) basemap_select = Select.create(name='Basemap', value='Toner', options=self.model.basemaps) basemap_select.on_change('value', self.on_basemap_change) opacity_slider = Slider(title="Opacity", value=100, start=0, end=100, step=1) opacity_slider.on_change('value', self.on_opacity_slider_change) controls = [axes_select, field_select, aggregate_select, transfer_select, basemap_select, opacity_slider] self.controls = HBox(width=self.fig.plot_width, children=controls) self.layout = VBox(width=self.fig.plot_width, height=self.fig.plot_height, children=[self.controls, self.fig])
def make_inputs(self): self.ticker1_select = Select( name='ticker1', value='AAPL', options=['AAPL', 'GOOG', 'INTC', 'BRCM', 'YHOO'] ) self.ticker2_select = Select( name='ticker2', value='GOOG', options=['AAPL', 'GOOG', 'INTC', 'BRCM', 'YHOO'] )
def update_station_list(attrname, old, new): year_value= select_years.value partition_value= select_partition.value # key= station_partition_key_format.format(year_value, partition_value) stations_list= station_partitions[key] # new_select_station= Select(title='Station', value=stations_list[0], options=stations_list) new_select_station.options=stations_list # new_children= [select_years, select_partition, new_select_station] inputs.children= new_children
def make_inputs(self): # #please add any other stocks you have downloaded using stock_data.py # self.ticker1_select = Select( name='ticker1', value='Stock', options=['A','AES','AN','BAX','BRCM','CCL','CMS', 'CTSH', 'DISCA', 'ECL', 'EW']) self.ticker2_select = Select( name='ticker2', value='Algorithm', options=['Arima','Arma','Anfis'] )
def make_inputs(self): with open("states.csv") as f: states = [line.strip().split(',') for line in f.readlines()] self.selectr = Select( name='states', value='Choose A State', options=[s[1] for s in states] + ['Choose A State'], ) labels = ["County Averages", "Hotels"] self.check_group = CheckboxGroup(labels=labels, active=[0,1], inline=True) ##Filler plot x_range = Range1d(0, 300) y_range = Range1d(0, 12) self.filler = Plot( x_range=x_range, y_range=y_range, title="", plot_width=300, plot_height=12, min_border=0, **PLOT_FORMATS ) x_range = Range1d(0, 300) y_range = Range1d(0, 18) self.filler2 = Plot( x_range=x_range, y_range=y_range, title="", plot_width=300, plot_height=14, min_border=0, **PLOT_FORMATS )
def make_star_input(self): starnames = sorted(self._ccf_interface.list_stars()) self.star_select = Select( name='Star identifier', value=self.star, options=starnames )
def read_file_tab(self): """Lets the user choose a data file to read""" # Drop down list self.file_select = Select(name='Data files', value='', options=[], title='Data files') # Status text self.file_status = Div(text='', width=self.page_width) # Update the file_select and file_status controls with scan data self.scan_folder() # This line is here deliberately. The scan_folder would trigger # the on-change function and we don't want that first time around. self.file_select.on_change('value', self.file_changed) # Re-scan button file_rescan = Button(label="Rescan folder", button_type="success") file_rescan.on_click(self.scan_folder) # Layout c = column(self.file_select, self.file_status, file_rescan) return Panel(child=c, title="Read from file")
def init_input(self): # create input widgets only once self.min_excitation = Slider( title="Min Excitation", name="min_excitation", value=min_excitation, start=min_excitation, end=max_excitation, ) self.max_excitation = Slider( title="Max Excitation", name="max_excitation", value=max_excitation, start=min_excitation, end=max_excitation, ) self.min_emission = Slider( title="Min Emission", name="min_emission", value=min_emission, start=min_emission, end=max_emission, ) self.max_emission = Slider( title="Max Emission", name="max_emission", value=max_emission, start=min_emission, end=max_emission, ) self.chrom_class_select = Select( title="Chromophore", value='All', options=['All'] + CHROMOPHORES, )
def make_inputs(self): self.ticker1_select = TextInput( name='ticker1', title='Drift Function:', value='1.2*(1.1-x)', ) self.ticker1p_select = TextInput( name='ticker1p', title='Drift Derivative:', value='-1.2', ) self.ticker2_select = TextInput( name='ticker2', title='Volatility Function:', value='4.0', ) self.ticker2p_select = TextInput( name='ticker2p', title='Volatility Derivative:', value='0.0', ) self.ticker3_select = TextInput( name='ticker3', title='Number of Paths:', value='500' ) self.ticker3_1_select = TextInput( name='ticker3_1', title='Number of Points:', value='252' ) self.ticker3_2_select = TextInput( name='ticker3_2', title='Time Step:', value='0.01' ) self.ticker4_select = TextInput( name='ticker4', title='Histogram Line:', value='100' ) self.ticker4_1_select = TextInput( name='ticker4_1', title='Initial Value:', value='1.01' ) self.ticker4_2_select = Select( name='ticker4_2', title='MC Scheme:', value='Milstein', options=['Euler','Milstein', 'Pred/Corr'] ) self.button_select = TextInput( name='button', title='Type any word containing "run" to run Simulation ', value = '' )
def make_inputs(self): columns = [ TableColumn(field='x', title='x'), TableColumn(field='y', title='y'), TableColumn(field='device', title='device'), TableColumn(field='platform', title='platform') ] # obj.data_display0 = DataTable(source=obj.source2, columns=columns) # obj.data_display1 = DataTable(source=obj.source3, columns=columns) # setup user input self.x_axis_options = Select(title="X:", value='size', options=axis_options) self.y_axis_options = Select(title="Y:", value='throughput [1/sec]', options=axis_options) self.benchmarks = Select(title="Benchmark:", value=benchmark_names[0], options=benchmark_names) self.device_names = CheckboxGroup(labels=device_names, active=[0]) self.platform_names = CheckboxButtonGroup(labels=platform_names, active=[0])
def main(): state_xs, state_ys = get_us_state_outline() left, right = minmax(state_xs) bottom, top = minmax(state_ys) plot = Figure(title=TITLE, plot_width=1000, plot_height=700, tools="pan, wheel_zoom, box_zoom, reset", x_range=Range1d(left, right), y_range=Range1d(bottom, top), x_axis_label='Longitude', y_axis_label='Latitude') plot_state_outline(plot, state_xs, state_ys) density_overlay = DensityOverlay(plot, left, right, bottom, top) density_overlay.draw() grid_slider = Slider(title="Details", value=density_overlay.gridcount, start=10, end=100, step=10) grid_slider.on_change("value", density_overlay.grid_change_listener) radiance_slider = Slider(title="Min. Radiance", value=density_overlay.radiance, start=np.min(density_overlay.rad), end=np.max(density_overlay.rad), step=10) radiance_slider.on_change("value", density_overlay.radiance_change_listener) listener = ViewListener(plot, density_overlay, name="viewport") plot.x_range.on_change("start", listener) plot.x_range.on_change("end", listener) plot.y_range.on_change("start", listener) plot.y_range.on_change("end", listener) backends = ["CPU", "HSA"] default_value = backends[kde.USE_HSA] backend_select = Select(name="backend", value=default_value, options=backends) backend_select.on_change('value', density_overlay.backend_change_listener) doc = curdoc() doc.add(VBox(children=[plot, grid_slider, radiance_slider, backend_select])) doc.add_periodic_callback(density_overlay.periodic_callback, 0.5)
def create_layout(self): self.plot_selector = Select.create( title="PlotType", name="plot_type", value='area', options=['area', "line", "scatter", "bar"], ) self.btn_confirm = Button(label='add plot') self.layout = vplot(hplot(self.plot_selector, self.btn_confirm), self.plot)
class DataViewer(): def __init__(self): img = imageio.imread("gray.png").astype(np.uint8) self.target = np.empty(img.shape[:2], dtype=np.uint32) view = self.target.view(dtype=np.uint8).reshape((img.shape[0], img.shape[1], 4)) view[:, :, :3] = img view[:, :, 3] = 255 self.fig = self.define_figure('image') self.source = ColumnDataSource(data={"image": [self.target], "dh": [self.target.shape[0]], "dw": [self.target.shape[1]]}) self.regist_image(self.fig, self.source) self.select = Select(title="Option:", value="gray.png", options=["gray.png", "white.png", "black.png", "julia.jpeg"]) self.plot = column(self.select, self.fig) self.select.on_change("value", self.update_image) def define_figure(self, title): return figure(title=title, match_aspect=True, tools="",) def regist_image(self, fig, source): fig.image_rgba('image', x=0, y=0, dh="dh", dw="dw", source=source) def update_image(self, attr, old, new): print(attr, old, new) img = imageio.imread(new).astype(np.uint8) img_ = np.empty(img.shape[:2], dtype=np.uint32) view = img_.view(dtype=np.uint8).reshape((img.shape[0], img.shape[1], 4)) view[:, :, :3] = img view[:, :, 3] = 255 self.fig.title.text = new self.source.data = {"image": [img_], "dh": [img_.shape[0]], "dw": [img_.shape[1]]}
def make_inputs(self): with open("states.csv") as f: states = [line.strip().split(',') for line in f.readlines()] self.selectr = Select( name='states', value='Choose A State', options=[s[1] for s in states] + ['Choose A State'] ) labels = ["County Averages", "Hotels"] self.check_group = CheckboxGroup(labels=labels, active=[0,1])
def make_inst_date_input(self): observations = self._ccf_interface.get_observations(self.star) observations = ['/'.join(obs).ljust(20, ' ') for obs in observations] self.inst_date = observations[0] if isinstance(self.inst_date_select, Select): self.inst_date_select.update(value=observations[0], options=observations) else: self.inst_date_select = Select.create( name='Instrument/Date', value=observations[0], options=observations, )
def compose_layout(self): """Compose the layout ot the app, the main elements are the widgets to select the dataset, the metric, a div for the title, a plot and a table """ # Load metrics and datasets self.metrics = get_metrics(default='AM1') self.datasets = get_datasets(default='cfht') # Get args from the app URL or use defaults args = get_url_args(doc=curdoc, defaults={'metric': self.metrics['default']}) self.selected_dataset = args['ci_dataset'] self.selected_metric = args['metric'] # get specifications for the selected metric self.specs = get_specs(self.selected_metric) self.selected_window = args['window'] # dataset select widget dataset_select = Select(title="Data Set:", value=self.selected_dataset, options=self.datasets['datasets'], width=100) dataset_select.on_change("value", self.on_dataset_change) # thresholds are used to make plot annotations self.configure_thresholds() # metric select widget metric_select = Select(title="Metric:", value=self.selected_metric, options=self.metrics['metrics'], width=100) metric_select.on_change("value", self.on_metric_change) self.data = \ get_meas_by_dataset_and_metric(self.selected_dataset, self.selected_metric, self.selected_window) self.update_data_source() self.make_plot() self.make_table() if len(self.data['values']) < 1: self.loading.text = "No data to display" else: self.loading.text = "" self.layout = column(row(widgetbox(metric_select, width=150), widgetbox(dataset_select, width=150)), widgetbox(self.title, width=1000), self.plot, widgetbox(self.table_title, width=1000), self.table)
def make_inputs(self): x_range = Range1d(0, 30) y_range = Range1d(0, 12) self.selectr_filler = Plot( x_range=x_range, y_range=y_range, title="", plot_width=30, plot_height=12, min_border=0, **PLOT_FORMATS ) self.selectr = Select( name='brands', value='Select A Brand', options=pop_brands + ['Select A Brand'], )
def __init__(self): img = imageio.imread("gray.png").astype(np.uint8) self.target = np.empty(img.shape[:2], dtype=np.uint32) view = self.target.view(dtype=np.uint8).reshape((img.shape[0], img.shape[1], 4)) view[:, :, :3] = img view[:, :, 3] = 255 self.fig = self.define_figure('image') self.source = ColumnDataSource(data={"image": [self.target], "dh": [self.target.shape[0]], "dw": [self.target.shape[1]]}) self.regist_image(self.fig, self.source) self.select = Select(title="Option:", value="gray.png", options=["gray.png", "white.png", "black.png", "julia.jpeg"]) self.plot = column(self.select, self.fig) self.select.on_change("value", self.update_image)
def create_layout(): year_select = Select(title="Year:", value="2010", options=years) location_select = Select(title="Location:", value="World", options=locations) year_select.on_change('value', on_year_change) location_select.on_change('value', on_location_change) controls = HBox(children=[year_select, location_select]) layout = VBox(children=[controls, pyramid(), population()]) return layout
def create_layout(): year_select = Select(title="Year:", value="2010", options=years) location_select = Select(title="Location:", value="World", options=locations) year_select.on_change('value', on_year_change) location_select.on_change('value', on_location_change) controls = WidgetBox(children=[year_select, location_select],height=150,width=600) layout = Column(children=[controls, pyramid(), population()]) return layout
def create_layout(self): from bokeh.models.widgets import Select, HBox, VBox years = list(map(str, sorted(self.df.Year.unique()))) locations = sorted(self.df.Location.unique()) year_select = Select(title="Year:", value="2010", options=years) location_select = Select(title="Location:", value="World", options=locations) year_select.on_change('value', self.on_year_change) location_select.on_change('value', self.on_location_change) controls = HBox(year_select, location_select) self.layout = VBox(controls, self.plot)
def line_config(app, df): options = CheckboxGroup( labels=['Show peaks', 'Show Std. Dev.', 'Bolinger Bands'], name='new_chart_options' ) title = TextInput(title="Chart Title", name='new_chart_title', value="Line") new_chart_index = Select.create(options=['---']+list(df.columns), name='"new_chart_index"', title="Chart index") chart_values_select = MultiSelect.create(options=df.columns, name="chart_values", title="Values") chart_data = AppHBox(app=app, children=[new_chart_index, chart_values_select, options]) main_tab = AppVBox(app=app, children=[title, chart_data, 'select_palette']) confirm_chart_button = Button(label="Confirm", type="success", name='confirm_add_chart') return { 'dlg': Dialog(buttons=[confirm_chart_button], content=main_tab, visible=True), "new_chart_index": new_chart_index, 'new_chart_title': title, 'chart_values': chart_values_select, 'new_chart_options': options }
<p><a href="http://scikit-learn.org/stable/modules/generated/sklearn.gaussian_process.GaussianProcessClassifier.html#sklearn.gaussian_process.GaussianProcessClassifier" target="_blank">More information on the parameters</a></p> <br/> """) #obj = client.get_object(Bucket='my-bucket', Key='churn.csv') #df = pd.read_csv(obj['Body']) #df = pd.read_csv('/Users/adilkhan/Documents/CS Fall 16/CS297/Bokeh-Demo/EmbedWebsite/cancer.csv') df = pd.read_csv('http://s3.amazonaws.com/cs297-mlplayground/' + datasetname) y = df[df.columns[:1]].values.ravel() df1 = df.drop(df.columns[:1], axis=1) features = MultiSelect(title="Features", options=df.columns[1:].tolist()) warm_start = Select(title="Warm_start:", value="False", options=["True", "False"]) copy_X_train = Select(title="Copy_X_train:", value="True", options=["True", "False"]) ''' div = Div(text="""Your <a href="https://en.wikipedia.org/wiki/HTML">HTML</a>-supported text is initialized with the <b>text</b> argument. The remaining div arguments are <b>width</b> and <b>height</b>. For this example, those values are <i>200</i> and <i>100</i> respectively.""", width=200, height=100) ''' stats = Paragraph(text='', width=800, height=200, name='Selected Features:') #columns =['avg_dist', 'avg_rating_by_driver','avg_rating_of_driver','avg_surge','surge_pct','trips_in_first_30_days','luxury_car_user','weekday_pct','city_Astapor',"city_KingsLanding",'city_Winterfell','phone_Android','phone_no_phone'] #columns = ['luxury_car_user','avg_dist','city_Astapor',"city_KingsLanding",'phone_Android','phone_iPhone']
<h2 style="font-family="Arial"> Select the features to be included in the Multi Layer Perceptron Model </h2> <p><a href="http://scikit-learn.org/stable/modules/generated/sklearn.neural_network.MLPClassifier.html#sklearn.neural_network.MLPClassifier" target="_blank">Click here </a>for more information on the parameters </p> """, width=1100) df = pd.read_csv(datasetname) y = df[df.columns[:1]].values.ravel() df1 = df.drop(df.columns[:1], axis=1) features = MultiSelect(title="Features", options=df.columns[1:].tolist()) activation = Select(title="Activation:", value="relu", options=["relu", "identity", "logistic", "tanh"]) solver = Select(title="Solver:", value="adam", options=["adam", "sgd", "lbfgs"]) learning_rate = Select(title="Learning Rate:", value="constant", options=["constant", "invscaling", "adaptive"]) shuffle = Select(title="Shuffle:", value="True", options=["True", "False"]) verbose = Select(title="Verbose:", value="False", options=["True", "False"]) warm_start = Select(title="Warm_start:", value="False", options=["True", "False"]) early_stopping = Select(title="Early Stopping:", value="False", options=["True", "False"])
source=source, color=dict(field='region', transform=color_mapper), legend='region') # Set the legend.location attribute of the plot to 'top_right' plot.legend.location = 'top_right' # Make a slider object: slider slider = Slider(start=1970, end=2010, step=1, value=1970, title='Year') # Attach the callback to the 'value' property of slider slider.on_change('value', update_plot) # Create a dropdown Select widget for the x data: x_select x_select = Select(options=['fertility', 'life', 'child_mortality', 'gdp'], value='fertility', title='x-axis data') # Create a dropdown Select widget for the y data: y_select y_select = Select(options=['fertility', 'life', 'child_mortality', 'gdp'], value='life', title='y-axis data') # Create a HoverTool: hover hover = HoverTool(tooltips=[('Country', '@country')]) # Add the HoverTool to the plot plot.add_tools(hover) # Make a row layout of widgetbox(slider) and plot and add it to the current document layout = row(widgetbox(slider, x_select, y_select), plot)
def plot(): # FIGURES AND X-AXIS fig1 = Figure(title = 'Dive Profile', plot_width = WIDTH, plot_height = HEIGHT, tools = TOOLS) fig2 = Figure(title = 'Dive Controls', plot_width = WIDTH, plot_height = HEIGHT, tools = TOOLS, x_range=fig1.x_range) fig3 = Figure(title = 'Attitude', plot_width = WIDTH, plot_height = HEIGHT, tools = TOOLS, x_range=fig1.x_range) figs = gridplot([[fig1],[fig2],[fig3]]) # Formatting x-axis timeticks = DatetimeTickFormatter(formats=dict(seconds =["%b%d %H:%M:%S"], minutes =["%b%d %H:%M"], hourmin =["%b%d %H:%M"], hours =["%b%d %H:%M"], days =["%b%d %H:%M"], months=["%b%d %H:%M"], years =["%b%d %H:%M %Y"])) fig1.xaxis.formatter = timeticks fig2.xaxis.formatter = timeticks fig3.xaxis.formatter = timeticks # removing gridlines fig1.xgrid.grid_line_color = None fig1.ygrid.grid_line_color = None fig2.xgrid.grid_line_color = None fig2.ygrid.grid_line_color = None fig3.xgrid.grid_line_color = None fig3.ygrid.grid_line_color = None # INPUT WIDGETS collection_list = CONN[DB].collection_names(include_system_collections=False) gliders = sorted([platformID for platformID in collection_list if len(platformID)>2]) gliders = Select(title = 'PlatformID', value = gliders[0], options = gliders) prev_glider = Button(label = '<') next_glider = Button(label = '>') glider_controlbox = HBox(children = [gliders, prev_glider, next_glider], height=80) chunkations = Select(title = 'Chunkation', value = 'segment', options = ['segment', '24hr', '30days', '-ALL-']) chunk_indicator = TextInput(title = 'index', value = '0') prev_chunk = Button(label = '<') next_chunk = Button(label = '>') chunk_ID = PreText(height=80) chunk_controlbox = HBox(chunkations, HBox(chunk_indicator, width=25), prev_chunk, next_chunk, chunk_ID, height = 80) control_box = HBox(glider_controlbox, chunk_controlbox) # DATA VARS deadby_date = '' depth = ColumnDataSource(dict(x=[],y=[])) vert_vel = ColumnDataSource(dict(x=[],y=[])) mbpump = ColumnDataSource(dict(x=[],y=[])) battpos = ColumnDataSource(dict(x=[],y=[])) pitch = ColumnDataSource(dict(x=[],y=[])) mfin = ColumnDataSource(dict(x=[],y=[])) cfin = ColumnDataSource(dict(x=[],y=[])) mroll = ColumnDataSource(dict(x=[],y=[])) mheading = ColumnDataSource(dict(x=[],y=[])) cheading = ColumnDataSource(dict(x=[],y=[])) # AXIS setup colors = COLORS[:] fig1.y_range.flipped = True fig1.yaxis.axis_label = 'm_depth (m)' fig1.extra_y_ranges = {'vert_vel': Range1d(start=-50, end=50), 'dummy': Range1d(start=0, end=100)} fig1.add_layout(place = 'right', obj = LinearAxis(y_range_name = 'vert_vel', axis_label = 'vertical velocity (cm/s)')) fig1.add_layout(place = 'left', obj = LinearAxis(y_range_name = 'dummy', axis_label = ' ')) fig1.yaxis[1].visible = False fig1.yaxis[1].axis_line_alpha = 0 fig1.yaxis[1].major_label_text_alpha = 0 fig1.yaxis[1].major_tick_line_alpha = 0 fig1.yaxis[1].minor_tick_line_alpha = 0 fig2.yaxis.axis_label = 'pitch (deg)' fig2.y_range.start, fig2.y_range.end = -40,40 fig2.extra_y_ranges = {'battpos': Range1d(start=-1, end = 1), 'bpump': Range1d(start=-275, end=275)} fig2.add_layout(place = 'right', obj = LinearAxis(y_range_name = 'battpos', axis_label = 'battpos (in)')) fig2.add_layout(place = 'left', obj = LinearAxis(y_range_name = 'bpump', axis_label = 'bpump (cc)')) fig2.yaxis[1].visible = False # necessary for spacing. later gets set to true fig3.yaxis.axis_label = 'fin/roll (deg)' fig3.y_range.start, fig3.y_range.end = -30, 30 fig3.extra_y_ranges = {'heading': Range1d(start=0, end=360), #TODO dynamic avg centering 'dummy': Range1d(start=0, end=100)} fig3.add_layout(place = 'right', obj = LinearAxis(y_range_name = 'heading', axis_label = 'headings (deg)')) fig3.add_layout(place = 'left', obj = LinearAxis(y_range_name = 'dummy', axis_label = ' ')) fig3.yaxis[1].visible = False fig3.yaxis[1].axis_line_alpha = 0 fig3.yaxis[1].major_label_text_alpha = 0 fig3.yaxis[1].major_tick_line_alpha = 0 fig3.yaxis[1].minor_tick_line_alpha = 0 # PLOT OBJECTS fig1.line( 'x', 'y', source = depth, legend = 'm_depth', color = 'red') fig1.circle('x', 'y', source = depth, legend = 'm_depth', color = 'red') fig1.line( 'x', 'y', source = vert_vel, legend = 'vert_vel', color = 'green', y_range_name = 'vert_vel') fig1.circle('x', 'y', source = vert_vel, legend = 'vert_vel', color = 'green', y_range_name = 'vert_vel') fig1.renderers.append(Span(location = 0, dimension = 'width', y_range_name = 'vert_vel', line_color= 'green', line_dash='dashed', line_width=1)) fig2.line( 'x', 'y', source = pitch, legend = "m_pitch", color = 'indigo') fig2.circle('x', 'y', source = pitch, legend = "m_pitch", color = 'indigo') fig2.line( 'x', 'y', source = battpos, legend = 'm_battpos', color = 'magenta', y_range_name = 'battpos') fig2.circle('x', 'y', source = battpos, legend = 'm_battpos', color = 'magenta', y_range_name = 'battpos') fig2.line( 'x', 'y', source = mbpump, legend = "m_'bpump'", color = 'blue', y_range_name = 'bpump') fig2.circle('x', 'y', source = mbpump, legend = "m_'bpump'", color = 'blue', y_range_name = 'bpump') fig2.renderers.append(Span(location = 0, dimension = 'width', line_color= 'black', line_dash='dashed', line_width=1)) fig3.line( 'x', 'y', source = mfin, legend = 'm_fin', color = 'cyan') fig3.circle('x', 'y', source = mfin, legend = 'm_fin', color = 'cyan') fig3.line( 'x', 'y', source = cfin, legend = 'c_fin', color = 'orange') fig3.circle('x', 'y', source = cfin, legend = 'c_fin', color = 'orange') fig3.line( 'x', 'y', source = mroll, legend = 'm_roll', color = 'magenta') fig3.circle('x', 'y', source = mroll, legend = 'm_roll', color = 'magenta') fig3.line( 'x', 'y', source = mheading, legend = 'm_heading', color = 'blue', y_range_name = 'heading') fig3.circle('x', 'y', source = mheading, legend = 'm_heading', color = 'blue', y_range_name = 'heading') fig3.line( 'x', 'y', source = cheading, legend = 'c_heading', color = 'indigo', y_range_name = 'heading') fig3.circle('x', 'y', source = cheading, legend = 'c_heading', color = 'indigo', y_range_name = 'heading') fig3.renderers.append(Span(location = 0, dimension = 'width', y_range_name = 'default', line_color= 'black', line_dash='dashed', line_width=1)) # CALLBACK FUNCS def update_data(attrib,old,new): g = gliders.value chnk = chunkations.value chindex = abs(int(chunk_indicator.value)) depth.data = dict(x=[],y=[]) vert_vel.data = dict(x=[],y=[]) mbpump.data = dict(x=[],y=[]) battpos.data = dict(x=[],y=[]) pitch.data = dict(x=[],y=[]) mfin.data = dict(x=[],y=[]) cfin.data = dict(x=[],y=[]) mroll.data = dict(x=[],y=[]) mheading.data = dict(x=[],y=[]) cheading.data = dict(x=[],y=[]) depth.data,startend = load_sensor(g, 'm_depth', chnk, chindex) if chnk == 'segment': xbd = startend[2] chunk_ID.text = '{} {} \n{} ({}) \nSTART: {} \nEND: {}'.format(g, xbd['mission'], xbd['onboard_filename'], xbd['the8x3_filename'], e2ts(xbd['start']), e2ts(xbd['end'])) if len(set(depth.data['x']))<=1 and attrib == 'chunk': if old > new: next_chunk.clicks += 1 else: prev_chunk.clicks += 1 return elif len(set(depth.data['x']))<=1 and chunk_indicator.value == 0: chunk_indicator.value = 1 elif chnk in ['24hr', '30days']: chunk_ID.text = '{} \nSTART: {} \nEND: {}'.format(g, e2ts(startend[0]), e2ts(startend[1])) elif chnk == '-ALL-': chunk_ID.text = '{} \nSTART: {} \nEND: {}'.format(g,e2ts(depth.data['x'][0] /1000), e2ts(depth.data['x'][-1]/1000)) vert_vel.data = calc_vert_vel(depth.data) mbpump.data,_ = load_sensor(g, 'm_de_oil_vol', chnk, chindex) if len(mbpump.data['x']) > 1: #for yax in fig2.select('mbpump'): # yax.legend = 'm_de_oil_vol' pass else: mbpump.data,_ = load_sensor(g, 'm_ballast_pumped', chnk, chindex) #for yax in fig2.select('mbpump'): # yax.legend = 'm_ballast_pumped' battpos.data,_ = load_sensor(g, 'm_battpos', chnk, chindex) pitch.data,_ = load_sensor(g, 'm_pitch', chnk, chindex) pitch.data['y'] = [math.degrees(y) for y in pitch.data['y']] mfin.data,_ = load_sensor(g, 'm_fin', chnk, chindex) cfin.data,_ = load_sensor(g, 'c_fin', chnk, chindex) mroll.data,_ = load_sensor(g, 'm_roll', chnk, chindex) mheading.data,_ = load_sensor(g, 'm_heading', chnk, chindex) cheading.data,_ = load_sensor(g, 'c_heading', chnk, chindex) mfin.data['y'] = [math.degrees(y) for y in mfin.data['y']] cfin.data['y'] = [math.degrees(y) for y in cfin.data['y']] mheading.data['y'] = [math.degrees(y) for y in mheading.data['y']] cheading.data['y'] = [math.degrees(y) for y in cheading.data['y']] mroll.data['y'] = [math.degrees(y) for y in mroll.data['y']] fig1.yaxis[1].visible = True fig2.yaxis[1].visible = True fig3.yaxis[1].visible = True #GLIDER SELECTS def glider_buttons(increment): ops = gliders.options new_index = ops.index(gliders.value) + increment if new_index >= len(ops): new_index = 0 elif new_index < 0: new_index = len(ops)-1 gliders.value = ops[new_index] chunkation_update(None, None, None) #reset chunk indicator and clicks def next_glider_func(): glider_buttons(1) def prev_glider_func(): glider_buttons(-1) def update_glider(attrib,old,new): chunk_indicator.value = '0' #update_data(None,None,None) gliders.on_change('value', update_glider) next_glider.on_click(next_glider_func) prev_glider.on_click(prev_glider_func) #CHUNK SELECTS def chunkation_update(attrib,old,new): chunk_indicator.value = '0' prev_chunk.clicks = 0 next_chunk.clicks = 0 update_data(None,None,None) if new == '-ALL-': chunk_indicator.value = '-' def chunk_func(): chunkdiff = prev_chunk.clicks - next_chunk.clicks if chunkdiff < 0: prev_chunk.clicks = 0 next_chunk.clicks = 0 chunkdiff = 0 print (chunkdiff) chunk_indicator.value = str(chunkdiff) def chunk_indicator_update(attrib,old,new): try: if abs(int(old)-int(new))>1: #manual update, triggers new non-manual indicator update, ie else clause below prev_chunk.clicks = int(new) next_chunk.clicks = 0 else: update_data('chunk',int(old),int(new)) print("UPDATE", old, new) except Exception as e: print(type(e),e, old, new) chunkations.on_change('value', chunkation_update) chunk_indicator.on_change('value', chunk_indicator_update) next_chunk.on_click(chunk_func) prev_chunk.on_click(chunk_func) update_data(None,None,None) return vplot(control_box, figs)
tab1_nfreq_square, = tab1_freq_square.shape tab1_ntim_square, = tab1_tim_square.shape tim_map = ((np.tile(tab1_tim_square, tab1_nfreq_square).reshape(tab1_nfreq_square, tab1_ntim_square) / 3600. / 24. + 2400000.5)) * 86400. freq_map = np.tile(tab1_freq_square, tab1_ntim_square).reshape(tab1_ntim_square, tab1_nfreq_square).swapaxes(0, 1) xx = tim_map.flatten() yy = freq_map.flatten() tab1_dspecDF_square = pd.DataFrame({'time': xx - xx[0], 'freq': yy}) tab1_SRC_dspec_square = ColumnDataSource(tab1_dspecDF_square) '''make the plot lasso selectable''' tab1_render_square = tab1_p_dspec.square('time', 'freq', source=tab1_SRC_dspec_square, fill_color=None, fill_alpha=0.0, line_color=None, line_alpha=0.0, selection_fill_color='red', selection_fill_alpha=0.2, nonselection_fill_alpha=0.0, selection_line_alpha=0.0, nonselection_line_alpha=0.0) ## ----------------baseline & polarization selection------------------------ tab1_Select_bl = Select(title="Baseline:", value=tab1_bl[0], options=tab1_bl, width=150) tab1_Select_pol = Select(title="Polarization:", value="I", options=["RR", "LL", "I", "V"], width=150) tab1_Select_colormap = Select(title="Colormap:", value="linear", options=["linear", "log"], width=150) def tab1_update_dspec(attrname, old, new): global tab1_spec, tab1_dtim, tab1_freq, tab1_bl select_pol = tab1_Select_pol.value select_bl = tab1_Select_bl.value bl_index = tab1_bl.index(select_bl) if select_pol == 'RR': spec_plt = tab1_spec[0, bl_index, :, :] elif select_pol == 'LL': spec_plt = tab1_spec[1, bl_index, :, :] elif select_pol == 'I': spec_plt = (tab1_spec[0, bl_index, :, :] + tab1_spec[1, bl_index, :, :]) / 2.
if ct == 'United States': bg = ['United States of America'] new_savvy_dict[bg[0]] = new_savvy_dict.pop(ct) if ct == bg[0]: # this is dumb I don't actually add any new_savvy_dict[bg[0]] = new_savvy_dict.pop( ct) # replace the medium data key with the new country key else: if ct != 'United States': new_savvy_dict.pop(ct) # ---------------- made it to here on new savvy dict from bokeh.models.widgets import Select select = Select(title="Metric:", value="Fear Level", options=["Fear Level", "Tech Savviness"], width=300) ndf = pd.concat([df['entity'], df['code']], axis=1) ndf.drop_duplicates() # add a country code to medium data dict codes_list = [] year_list = [] dat_list = [] pop_list = [] entitiesL = list(new_data_dict.keys()) for v in range(len(new_data_dict.values())): if entitiesL[v] == 'United States of America': tmp = 'United States' else:
elif t1 == '16:20': # bathroom window open t1 = '140.0' t1 = float(t1) data['tmp'] = df[df['time'] == t1]['temperature'].values data['hum'] = df[df['time'] == t1]['humidity'].values data.sort_values(['vol'], inplace=True) return data # set up widgets stats = PreText(text='', width=500) ticker1 = Select(value='15:15', options=nix('13:00', DEFAULT_TICKERS)) ticker2 = Select(value='15:15', options=nix('13:00', DEFAULT_TICKERS)) # set up plots source = ColumnDataSource(data=dict(vol=[], tmp=[], hum=[])) source_static = ColumnDataSource(data=dict(vol=[], tmp=[], hum=[])) tools = 'pan,wheel_zoom,xbox_select,reset' corr = figure(plot_width=600, plot_height=600, tools='pan,wheel_zoom,box_select,reset') corr.scatter(x='tmp', y='hum', size=8, source=source) labels = LabelSet(x='tmp', y='hum', text='room', level='glyph',
def _getPlotPanel(self): """@brief Add tab that shows plot data updates.""" self._figTable.append([]) self._voltsSource = ColumnDataSource({'x': [], 'y': []}) self._ampsSource = ColumnDataSource({'x': [], 'y': []}) self._wattsSource = ColumnDataSource({'x': [], 'y': []}) fig = figure(toolbar_location='above', x_axis_type="datetime", x_axis_location="below") fig.line(source=self._voltsSource, line_color="blue", legend_label="Volts") fig.line(source=self._ampsSource, line_color="green", legend_label="Amps") fig.line(source=self._wattsSource, line_color="red", legend_label="Watts") fig.legend.location = 'top_left' self._figTable[-1].append(fig) self._grid = gridplot(children=self._figTable, sizing_mode='scale_both', toolbar_location='right') self.selectSerialPort = Select(title="Serial Port:") self.selectSerialPort.options = glob.glob('/dev/ttyU*') self.outputVoltageSpinner = Spinner(title="Output Voltage (Volts)", low=0, high=40, step=0.5, value=self._pconfig.getAttr( PSUGUI.VOLTS)) self.currentLimitSpinner = Spinner(title="Currnet Limit (Amps)", low=0, high=10, step=0.25, value=self._pconfig.getAttr( PSUGUI.AMPS)) self.plotHistorySpinner = Spinner(title="Plot History (Seconds)", low=1, high=10000, step=1, value=self._pconfig.getAttr( PSUGUI.PLOT_SECONDS)) self._setButton = Button(label="Set") self._setButton.on_click(self._setHandler) self._setButton.disabled = True self._onButton = Button(label="On") self._onButton.on_click(self._psuOnHandler) shutdownButtonWrapper = ShutdownButtonWrapper(self._quit) controlPanel = column([ self.selectSerialPort, self._onButton, self.outputVoltageSpinner, self.currentLimitSpinner, self._setButton, self.plotHistorySpinner, shutdownButtonWrapper.getWidget() ]) self._opTableWrapper = ReadOnlyTableWrapper(("volts", "amps", "watts"), heightPolicy="fixed", height=65, showLastRows=0) plotPanel = column([self._grid, self._opTableWrapper.getWidget()]) panel2 = row([controlPanel, plotPanel]) plotPanel = column([panel2, self.statusBarWrapper.getWidget()]) return plotPanel
location = select_location.value new_df = loc_group.get_group(location) new_hist_df = loc_group.get_group(location).groupby(['FullName']).count() new_hist_df.sort_values('location',ascending=0,inplace=True) stop_ind = min(new_hist_df.shape[0],7) new_hist_df = new_hist_df[0:stop_ind] new_hist_df['y'] = np.arange(0,stop_ind)[::-1] new_hist_df['full_name']=new_hist_df.index.tolist() new_source = ColumnDataSource(new_df) new_hist_source = ColumnDataSource(new_hist_df) circle_source.data = new_source.data hist_source.data = new_hist_source.data # p2.y_range = FactorRange(factors=list(new_hist_df['full_name'])) # Creating dropdown tool select_location = Select(title='Select Location :', value=location_list[0], options=location_list) select_location.on_change('value',update_by_location) # Creating Hover Tool for p p.add_tools(HoverTool(tooltips = [ ('First Name', '@FirstName') ,('Last Name', '@LastName') ,('Expense', '@price') ] ,renderers =[p.select('circ')[0]] )) # Creating Hover Tool for p2 p2.add_tools(HoverTool(tooltips = [ ('Full Name', '@full_name') ]
def __init__(self, model, resolution=50, doc=None): """ Initialize parameters. Parameters ---------- model : MetaModelComponent Reference to meta model component resolution : int Value used to calculate the size of contour plot meshgrid doc : Document The bokeh document to build. """ self.prob = Problem() self.resolution = resolution logging.getLogger("bokeh").setLevel(logging.ERROR) # If the surrogate model coming in is structured if isinstance(model, MetaModelUnStructuredComp): self.is_structured_meta_model = False # Create list of input names, check if it has more than one input, then create list # of outputs self.input_names = [name[0] for name in model._surrogate_input_names] if len(self.input_names) < 2: raise ValueError('Must have more than one input value') self.output_names = [name[0] for name in model._surrogate_output_names] # Create reference for untructured component self.meta_model = MetaModelUnStructuredComp( default_surrogate=model.options['default_surrogate']) # If the surrogate model coming in is unstructured elif isinstance(model, MetaModelStructuredComp): self.is_structured_meta_model = True self.input_names = [name for name in model._var_rel_names['input']] if len(self.input_names) < 2: raise ValueError('Must have more than one input value') self.output_names = [name for name in model._var_rel_names['output']] self.meta_model = MetaModelStructuredComp( distributed=model.options['distributed'], extrapolate=model.options['extrapolate'], method=model.options['method'], training_data_gradients=model.options['training_data_gradients'], vec_size=1) # Pair input list names with their respective data self.training_inputs = {} self._setup_empty_prob_comp(model) # Setup dropdown menus for x/y inputs and the output value self.x_input_select = Select(title="X Input:", value=[x for x in self.input_names][0], options=[x for x in self.input_names]) self.x_input_select.on_change('value', self._x_input_update) self.y_input_select = Select(title="Y Input:", value=[x for x in self.input_names][1], options=[x for x in self.input_names]) self.y_input_select.on_change('value', self._y_input_update) self.output_select = Select(title="Output:", value=[x for x in self.output_names][0], options=[x for x in self.output_names]) self.output_select.on_change('value', self._output_value_update) # Create sliders for each input self.slider_dict = {} self.predict_inputs = {} for title, values in self.training_inputs.items(): slider_data = np.linspace(min(values), max(values), self.resolution) self.predict_inputs[title] = slider_data # Calculates the distance between slider ticks slider_step = slider_data[1] - slider_data[0] slider_object = Slider(start=min(values), end=max(values), value=min(values), step=slider_step, title=str(title)) self.slider_dict[title] = slider_object self._slider_attrs() # Length of inputs and outputs self.num_inputs = len(self.input_names) self.num_outputs = len(self.output_names) # Precalculate the problem bounds. limits = np.array([[min(value), max(value)] for value in self.training_inputs.values()]) self.limit_range = limits[:, 1] - limits[:, 0] # Positional indicies self.x_index = 0 self.y_index = 1 self.output_variable = self.output_names.index(self.output_select.value) # Data sources are filled with initial values # Slider Column Data Source self.slider_source = ColumnDataSource(data=self.predict_inputs) # Contour plot Column Data Source self.contour_plot_source = ColumnDataSource(data=dict( z=np.random.rand(self.resolution, self.resolution))) self.contour_training_data_source = ColumnDataSource( data=dict(x=np.repeat(0, self.resolution), y=np.repeat(0, self.resolution))) # Bottom plot Column Data Source self.bottom_plot_source = ColumnDataSource(data=dict( x=np.repeat(0, self.resolution), y=np.repeat(0, self.resolution))) self.bottom_plot_scatter_source = ColumnDataSource(data=dict( bot_slice_x=np.repeat(0, self.resolution), bot_slice_y=np.repeat(0, self.resolution))) # Right plot Column Data Source self.right_plot_source = ColumnDataSource(data=dict( x=np.repeat(0, self.resolution), y=np.repeat(0, self.resolution))) self.right_plot_scatter_source = ColumnDataSource(data=dict( right_slice_x=np.repeat(0, self.resolution), right_slice_y=np.repeat(0, self.resolution))) # Text input to change the distance of reach when searching for nearest data points self.scatter_distance = TextInput(value="0.1", title="Scatter Distance") self.scatter_distance.on_change('value', self._scatter_input) self.dist_range = float(self.scatter_distance.value) # Grouping all of the sliders and dropdowns into one column sliders = [value for value in self.slider_dict.values()] sliders.extend( [self.x_input_select, self.y_input_select, self.output_select, self.scatter_distance]) self.sliders_and_selects = row( column(*sliders)) # Layout creation self.doc_layout = row(self._contour_data(), self._right_plot(), self.sliders_and_selects) self.doc_layout2 = row(self._bottom_plot()) if doc is None: doc = curdoc() doc.add_root(self.doc_layout) doc.add_root(self.doc_layout2) doc.title = 'Meta Model Visualization'
"surya": "AIzaSyAU2gGkynk36LibmjTwLKOKMHVTRKIM87k", "graham": "AIzaSyBRcJ-Oj88gvz0LWNaCKg42K0K9SQIFpfs" } gmaps = googlemaps.Client(key=google_api_keys["surya"]) with open('static/zip_coords.json') as data_file: zip_coords_json = json.load(data_file) large_palette = sns_to_bokeh_palette("GnBu", 20) medium_palette = sns_to_bokeh_palette("GnBu", 6) small_palette = sns_to_bokeh_palette("GnBu", 2) address = TextInput(title="Address:", value="Times Square") month = Select(title="Month:", value="January", options=[ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]) update = Button(label="Update", button_type="success") rides_df, full_address = search_address(address.value, 50) freq_dict = calc_zipcode_frequencies(zip_coords_json, rides_df) d_choropleth, d_patches = make_choropleth_plot(zip_coords_json, freq_dict, "dropoff_frequency", large_palette) p_choropleth, p_patches = make_choropleth_plot(zip_coords_json, freq_dict, "pickup_frequency", large_palette) type_stacked_bar = make_stacked_bar_chart("day", 'type', rides_df, small_palette)
def spectral_tab(ts): # Make line plot for raw and detrended data. def make_lineplot_q1(source): ts_list = source.column_names ts_list.remove('index') ts_list.remove('time') ttp = [("Time", "$x"), ("Value", "$y")] plot_q1 = figure(plot_height=400, plot_width=400, tooltips=ttp, title="Time Series", tools="hover, pan, zoom_in, zoom_out, reset, save") for i, name in enumerate(ts_list): plot_q1.line('time', name, source=source, line_width=3, line_color=ts_colors[i], legend_label=name) plot_q1.legend.location = "top_left" plot_q1.legend.click_policy = "hide" return plot_q1 # Make line and circle plot for power spectrum. def make_lineplot_q2(source): ttp = [("Frequency", "$x"), ("Power", "$y")] plot_q2 = figure(plot_height=400, plot_width=400, tooltips=ttp, title="Power Spectrum", tools="hover, pan, zoom_in, zoom_out, reset, save") plot_q2.line('frequency', 'power', source=source, line_width=3, line_color=ts_colors[10]) plot_q2.circle('frequency', 'power', source=source, fill_color="white", size=8) return plot_q2 # Make time series plot for fourier components. def make_lineplot_q3(source): ts_list = source.column_names ts_list.remove('index') ts_list.remove('times') ttp = [("Time", "$x"), ("Value", "$y")] plot_q3 = figure(plot_height=400, plot_width=400, tooltips=ttp, title="Top 5 Fourier Components", tools="hover, pan, zoom_in, zoom_out, reset, save") for i, name in enumerate(ts_list): plot_q3.line('times', name, source=source, line_width=3, line_color=ts_colors[i], legend_label=name) plot_q3.legend.location = "top_left" plot_q3.legend.click_policy = "hide" return plot_q3 # Make time series plot for fourier components. def make_lineplot_q4(source): ts_list = source.column_names ts_list.remove('index') ts_list.remove('time') ttp = [("Time", "$x"), ("Value", "$y")] plot_q4 = figure(plot_height=400, plot_width=400, tooltips=ttp, title="Fourier Residuals", tools="hover, pan, zoom_in, zoom_out, reset, save") for i, name in enumerate(ts_list): plot_q4.line('time', name, source=source, line_width=3, line_color=ts_colors[i], legend_label=name) plot_q4.legend.location = "top_left" plot_q4.legend.click_policy = "hide" return plot_q4 # Make Summary Table of top 5 fourier components. def make_summarytable(source): columns = [ TableColumn(field="frequency", title="Frequency"), TableColumn(field="amplitude", title="Amplitude"), TableColumn(field="power", title="Power") ] summary_table = DataTable(source=source, columns=columns, width=400, height=200) return summary_table # Make dataset function. def make_dataset(ts, col, N): ts_data = ts[['time', col]] ts_data.columns = ['time', 'raw_data'] if N == 0: ts_data['detrended'] = ts_data['raw_data'] return ts_data else: ts_data.columns = ['time', 'raw_data'] ts_detrended = remove_trend(ts_data, N) ts_data['detrended'] = ts_detrended.iloc[:, 1] return ts_data ############################################################## # Set up callbacks def update_data(attrname, old, new): # Update data with selected timeseries and detrend with order N polynomial. new_data = make_dataset(ts, ts_select.value, order_select.value) update_dataset(new_data=new_data) update_powerspectrum(new_data=new_data) update_fourier1(new_data=new_data) update_fourier2(new_data=new_data) update_summarytable(new_data=new_data) def update_dataset(new_data): # Store updated data in CDS format. new_source_q1 = ColumnDataSource(data=new_data) # Update CDS to plot_q1. source_q1.data.update(new_source_q1.data) def update_powerspectrum(new_data): # Compute power spectrum. # If timeseries has not been detrended (i.e. size == 2), use raw data in power spectrum. if len(new_data.columns) == 2: pspec_data = fourier_to_freq_spectrum(new_data.iloc[:, 1], new_data['time']) else: pspec_data = fourier_to_freq_spectrum(new_data.iloc[:, 2], new_data['time']) # Store update data in CDS format. new_source_q2 = ColumnDataSource(data=pspec_data) # Update CDS to plot_q2 source_q2.data.update(new_source_q2.data) print(source_q2.data) def update_fourier1(new_data): # Compute fourier components. if len(new_data.columns) == 2: alpha0, coef = dfs(new_data.iloc[:, 1]) top_components_for_approx, _, _ = calc_residuals(alpha0, coef, new_data.iloc[:, 1], new_data['time'], components=5) else: alpha0, coef = dfs(new_data.iloc[:, 2]) top_components_for_approx, _, _ = calc_residuals(alpha0, coef, new_data.iloc[:, 2], new_data['time'], components=5) new_source_q3 = ColumnDataSource(data=top_components_for_approx) source_q3.data.update(new_source_q3.data) print(source_q3.data) def update_fourier2(new_data): if len(new_data.columns) == 2: alpha0, coef = dfs(new_data.iloc[:, 1]) _, _, residual_df = calc_residuals( alpha0, coef, new_data.iloc[:, 1], new_data['time'], components=component_select.value) else: alpha0, coef = dfs(new_data.iloc[:, 2]) _, _, residual_df = calc_residuals( alpha0, coef, new_data.iloc[:, 2], new_data['time'], components=component_select.value) new_source_q4 = ColumnDataSource(data=residual_df) source_q4.data.update(new_source_q4.data) print(source_q4.data) def update_summarytable(new_data): if len(new_data.columns) == 2: alpha0, coef = dfs(new_data.iloc[:, 1]) _, summary_table, _ = calc_residuals(alpha0, coef, new_data.iloc[:, 1], new_data['time'], components=5) else: alpha0, coef = dfs(new_data.iloc[:, 2]) _, summary_table, _ = calc_residuals(alpha0, coef, new_data.iloc[:, 2], new_data['time'], components=5) new_source_summarytable = ColumnDataSource(data=summary_table) source_summarytable.data.update(new_source_summarytable.data) print(source_summarytable.data) # Set up widgets. # Select time series for Spectral Analysis. ts_available = ts.columns.tolist() ts_available.remove('time') ts_select = Select(value=ts_available[0], title='Time Series', options=ts_available) ts_select.on_change('value', update_data) # Select order of polynomial for removing trend. order_select = Slider(start=0, end=10, step=1, value=0, title='Detrending Polynomial Order') order_select.on_change('value', update_data) # Select no. components for spectral analysis. component_select = Slider(start=0, end=20, step=1, value=5, title='No. Fourier Components') component_select.on_change('value', update_data) ############################################################## # Initial state and plotting. # Ensuring ts dataframe has odd no. elements. if (len(ts) % 2) == 0: # Remove last row when even no. elements. ts.drop(ts.tail(1).index, inplace=True) else: pass # Make initial dataset with first time series column by default. initial_data = make_dataset(ts, ts_available[0], 0) source_q1 = ColumnDataSource(data=initial_data) # Compute initial power spectrum for default time series. initial_pspec = fourier_to_freq_spectrum(initial_data.iloc[:, 1], initial_data['time']) source_q2 = ColumnDataSource(data=initial_pspec) # Compute initial fourier components for default time series. alpha0, coef = dfs(initial_data.iloc[:, 1]) top_components_for_approx, summary_table, residual_df = calc_residuals( alpha0, coef, initial_data.iloc[:, 1], initial_data['time'], components=5) source_q3 = ColumnDataSource(data=top_components_for_approx) source_q4 = ColumnDataSource(data=residual_df) source_summarytable = ColumnDataSource(data=summary_table) plot_q1 = make_lineplot_q1(source_q1) plot_q2 = make_lineplot_q2(source_q2) plot_q3 = make_lineplot_q3(source_q3) plot_q4 = make_lineplot_q4(source_q4) summary_table = make_summarytable(source_summarytable) # Set up layouts and add to document. # Put controls in a single element. controls = WidgetBox(ts_select, order_select, component_select) # Create a row layout layout = row( controls, gridplot([[plot_q1, plot_q2], [plot_q3, plot_q4], [summary_table, None]])) # Make a tab with the layout. tab = Panel(child=layout, title='Spectral Analysis') return tab
def index(): # narr_erc_all = narr_erc.query.limit(5).all() # This returns a list of objects. Pass that list of objects to your template using jinga. # narr_erc_lat = narr_erc.query.filter_by(lat='39.2549').first() # narr_erc_date = narr_erc.query.filter(func.date(narr_erc.date) <= '1979-01-02').all() # df = pd.read_sql(session.query(narr_erc).filter(func.date(narr_erc.date) <= '1979-01-02').statement, session.bind) # df = pd.read_sql(session.query(narr_erc).filter(func.date(narr_erc.date) <= '1979-01-02').statement, session.bind) #db.session.add(narr_erc_lat_lon) #db.session.commit() # fetchall() is one way to get data from a cursor after a query # results = cur.fetchall() conn = connect_to_db() # ------------------------------------------- # # QUERY: TIME SERIES OF H500, ERC AT ONE LOCATION # # Reading data into a list object 'results' directly from postgres fire_weather_db: # cur = conn.cursor() # sql = 'select id, lat, lon, date, h500, erc from narr_erc \ # where lat = 39.2549 and lon = 236.314 \ # order by id' # df = pd.read_sql(sql, conn) # cur.close() # conn.close() # source = ColumnDataSource(df) # ------------------------------------------- # # PLOTTING H500 TIME SERIES # p = figure( # x_axis_type = 'datetime', # plot_width = 800, # plot_height = 600, # # y_range = h500_list, # title = 'H500 Time Series', # x_axis_label = 'Date', # y_axis_label = 'Geopotential height, gpm', # tools = 'pan,zoom_in,zoom_out,save,reset', # ) # # p.line( # source = source, # x = 'date', # y = 'h500', # line_color = 'green', # legend = 'H500', # line_width = 2 # ) # ------------------------------------------- # # PLOTTING ERC TIME SERIES # p = figure( # x_axis_type = 'datetime', # plot_width = 800, # plot_height = 600, # # y_range = h500_list, # title = 'ERC Time Series', # x_axis_label = 'Date', # y_axis_label = 'ERC, AU', # tools = 'pan,zoom_in,zoom_out,save,reset', # ) # # p.line( # source = source, # x = 'date', # y = 'erc', # line_color = 'red', # legend = 'ERC', # line_width=2 # ) # ------------------------------------------- # SQL QUERY: H500 CONTOUR SINGLE DATE # Reading data into a list object 'results' directly from postgres fire_weather_db: cur = conn.cursor() sql = "select id, lat, lon, date, h500, h500_grad_x, pmsl, pmsl_grad_x, pmsl_grad_y, erc from narr_erc \ where cast(date as date) = '1979-05-15' \ order by id" df = pd.read_sql(sql, conn) cur.close() conn.close() source = ColumnDataSource(df) # ------------------------------------------- # # PLOTTING NARR GRID # x = df['lon'] # y = df['lat'] # p = figure( # plot_width = 800, # plot_height = 600, # title = 'NARR Grid', # x_axis_label = 'Lon', # y_axis_label = 'Lat', # tools = 'pan,zoom_in,zoom_out,save,reset', # ) # p.circle(x, y, size=2, color="black", alpha=0.5) # ------------------------------------------- # PLOTTING H500 CONTOUR var = 'pmsl_grad_x' # e.g. 'h500', 'h500_grad_x', 'erc' var_title = 'PMSL - X Gradient' # e.g. 'H500', 'H500 - X Gradient', 'ERC' lon = df['lon'].drop_duplicates('first').to_numpy() lat = df['lat'].drop_duplicates('first').to_numpy() lonlon, latlat = np.meshgrid(lon, lat) mesh_shape = np.shape(lonlon) # Change -32767 to 0: if var == 'erc': criteria = df[df['erc'] == -32767].index df['erc'].loc[criteria] = 0 d = df[var].to_numpy().reshape(mesh_shape) var_list = df[var].values.tolist() var_min = min(var_list) var_max = max(var_list) lon_min = np.min(lon) lon_max = np.max(lon) dw = lon_max - lon_min lat_min = np.min(lat) lat_max = np.max(lat) dh = lat_max - lat_min p = figure( #toolbar_location="left", title=var_title, plot_width=580, plot_height=600, tooltips=[("lon", "$lon"), ("lat", "$lat"), ("value", "@image")], x_range=(lon_min, lon_max), y_range=(lat_min, lat_max), x_axis_label='Longitude, deg', y_axis_label='Latitude, deg') if var == 'h500_grad_x' or 'h500_grad_y' or 'pmsl_grad_x' or 'pmsl_grad_y': # Color maps that make 0 values clear color_mapper = LinearColorMapper(palette=cividis(256), low=var_min, high=var_max) else: color_mapper = LinearColorMapper(palette="Inferno256", low=var_min, high=var_max) # Decent color map: "Spectra11", "Viridis256" # Giving a vector of image data for image parameter (contour plot) p.image(image=[d], x=lon_min, y=lat_min, dw=dw, dh=dh, color_mapper=color_mapper) # p.x_range.range_padding = p.y_range.range_padding = 0 color_bar = ColorBar( color_mapper=color_mapper, ticker=BasicTicker(), label_standoff=12, border_line_color=None, location=(0, 0), ) p.add_layout(color_bar, 'right') # get state boundaries from state map data imported from Bokeh state_lats = [states[code]["lats"] for code in states] state_lons = [states[code]["lons"] for code in states] # add 360 to adjust lons to NARR grid state_lons = np.array([np.array(sublist) for sublist in state_lons]) state_lons += 360 p.patches(state_lons, state_lats, fill_alpha=0.0, line_color="black", line_width=2, line_alpha=0.3) select = Select(title="Weather Variable:", value="H500", options=[ "H500", "H500 X Gradient", "H500 Y Gradient", "PMSL", "PMSL X Gradient", "PMSL Y Gradient", "Energy Release Component" ]) # slider = Slider(start=DateTime(1979,1,2), end=DateTime(1979,12,31), value=DateTime(1979,1,2), step=1, title="Date") slider = Slider(start=1, end=365, step=10, title="Date") # def callback(attr, old, new): # points = slider.value # data_points.data = {'x': random(points), 'y': random(points)} # slider.on_change('value', callback) widget_layout = widgetbox(slider, select) layout = row(slider, p) curdoc().add_root(widget_layout) # To run on bokeh server: # bokeh serve --show fwp_app.py # # Limit the view to the min and max of the building data # p.x_range = DataRange1d(lon_min, lon_max) # p.y_range = DataRange1d(lat_min, lat_max) # p.xaxis.visible = False # p.yaxis.visible = False p.xgrid.grid_line_color = None p.ygrid.grid_line_color = None # show(p) # output_file("image.html", title="image.py example") # show(p) # open a browser script, div = components(p) # The data below is passed to add_user_fwp.html to run when localhost:5000/ is opened. # return render_template('add_user_fwp.html', narr_erc_all=narr_erc_all, narr_erc_lat=narr_erc_lat, narr_erc_date=narr_erc_date, script=script, div=div) return render_template('fwp_bokeh_render.html', script=script, div=div, widget_layout=widget_layout)
def create_widget(self, dim, holomap=None, editable=False): """" Given a Dimension creates bokeh widgets to select along that dimension. For numeric data a slider widget is created which may be either discrete, if a holomap is supplied or the Dimension.values are set, or a continuous widget for DynamicMaps. If the slider is discrete the returned mapping defines a mapping between values and labels making it possible sync the two slider and label widgets. For non-numeric data a simple dropdown selection widget is generated. """ label, mapping = None, None if holomap is None: if dim.values: if dim.default is None: default = dim.values[0] elif dim.default not in dim.values: raise ValueError("%s dimension default %r is not in dimension values: %s" % (dim, dim.default, dim.values)) else: default = dim.default value = dim.values.index(default) if all(isnumeric(v) for v in dim.values): values = sorted(dim.values) labels = [unicode(dim.pprint_value(v)) for v in values] if editable: label = AutocompleteInput(value=labels[value], completions=labels, title=dim.pprint_label) else: label = Div(text='<b>%s</b>' % dim.pprint_value_string(labels[value])) widget = Slider(value=value, start=0, end=len(dim.values)-1, title=None, step=1) mapping = list(enumerate(zip(values, labels))) else: values = [(v, dim.pprint_value(v)) for v in dim.values] widget = Select(title=dim.pprint_label, value=values[value][0], options=values) else: start = dim.soft_range[0] if dim.soft_range[0] else dim.range[0] end = dim.soft_range[1] if dim.soft_range[1] else dim.range[1] dim_range = end - start int_type = isinstance(dim.type, type) and issubclass(dim.type, int) if dim.step is not None: step = dim.step elif isinstance(dim_range, int) or int_type: step = 1 else: step = 10**((round(math.log10(dim_range))-3)) if dim.default is None: default = start elif (dim.default < start or dim.default > end): raise ValueError("%s dimension default %r is not in the provided range: %s" % (dim, dim.default, (start, end))) else: default = dim.default if editable: label = TextInput(value=str(default), title=dim.pprint_label) else: label = Div(text='<b>%s</b>' % dim.pprint_value_string(default)) widget = Slider(value=default, start=start, end=end, step=step, title=None) else: values = (dim.values if dim.values else list(unique_array(holomap.dimension_values(dim.name)))) if dim.default is None: default = values[0] elif dim.default not in values: raise ValueError("%s dimension default %r is not in dimension values: %s" % (dim, dim.default, values)) else: default = dim.default if isinstance(values[0], np.datetime64) or isnumeric(values[0]): values = sorted(values) labels = [dim.pprint_value(v) for v in values] value = values.index(default) if editable: label = AutocompleteInput(value=labels[value], completions=labels, title=dim.pprint_label) else: label = Div(text='<b>%s</b>' % (dim.pprint_value_string(labels[value]))) widget = Slider(value=value, start=0, end=len(values)-1, title=None, step=1) else: labels = [dim.pprint_value(v) for v in values] widget = Select(title=dim.pprint_label, value=default, options=list(zip(values, labels))) mapping = list(enumerate(zip(values, labels))) return widget, label, mapping
# TODO: Checkboxes to include what we want <-- # TODO: make legend its own figure # TODO: Hover Tool Tips # TODO: Update documentation & cleanup # TODO: Control & Calibrator Tools (date range slider?) # TODO: Live plotting & long term performance evaluation plots # Get List of Dates for dropdown menu masterdf = save_r1masterfile(True) masterdf = masterdf[masterdf.index > IGNORE_BEFORE] date_list = masterdf.index.format() # Create Controls cycle = Select(title='Cycle Date', value=date_list[-1], options=date_list) probes = CheckboxGroup(labels=PROBE_LABELS.keys(), active=DEFAULT_PROBES, disabled=False) measure = CheckboxGroup(labels=MEASURE_LABELS.keys(), active=DEFAULT_MEASURED) def get_to_plot(what_to_plot, probe): """ Loops through checked boxes and returns names of columns requested to plot :param what_to_plot: list of ints, taken from checkboxgroup.active :param probe: boolean, true to look at probe data false for measured. :return: List of strs, names of columns in dataframe """ to_plot = []
# Set up the striptool striptool = Striptool(output_variables_to_display, controller, "test") striptool.build_plot() # set up global pv current_striptool_pv = striptool.live_variable # set up selection def striptool_select_callback(attr, old, new): global current_striptool_pv current_striptool_pv = new striptool_select = Select( title="Variable to plot:", value=current_striptool_pv, options=list(striptool.pv_monitors.keys()), ) striptool_select.on_change("value", striptool_select_callback) # striptool data update callback def striptool_update_callback(): """ Calls striptool update with the current global process variable. """ global current_striptool_pv striptool.update(live_variable = current_striptool_pv) # add table
<td><b>Votes:</b></td> <td>{3:.0f}</td> </tr> <tr> <td><b>Overall ranking:</b></td> <td>{4:.0f}</td> </tr> <tr> <td><b>Generation ranking:</b></td> <td>{5:.0f}</td> </tr> </table> """.format(initial_number, initial_name, initial_generation, initial_votes, initial_ranking_overall, initial_ranking_generation), width=POKEMON_PANEL_WIDTH, height=int(PLOT_HEIGHT*.6)) # Create Select. select = Select(title="Pokemon:", value=df['name'].tolist()[0], options=df['name'].tolist()) # Create the "Overall" plot. source_overall = ColumnDataSource(df_ranked[['name', 'votes', 'generation', 'generation_color', 'ranking_overall', 'ranking_generation', 'sprite_source']]) pokemon_names = source_overall.data['name'] pokemon_votes = source_overall.data['votes'] # Notice that initializing the figure with y_range=pokemon_names # doesn't allow the option to bound the plot. p_overall = figure(y_range=FactorRange(factors=pokemon_names, bounds=(0, len(pokemon_names))), x_axis_label='Votes', plot_height=PLOT_HEIGHT, tools=tools) r_overall = p_overall.hbar(y='name', left=0, right='votes', height=1, color='generation_color', source=source_overall) p_overall.x_range = Range1d(0, max(pokemon_votes)*1.05, bounds=(0, max(pokemon_votes)*1.05)) p_overall.ygrid.grid_line_color = None y_coord = len(df_ranked) - initial_ranking_overall + 0.5 arrow_overall = Arrow(end=NormalHead(line_color='red', fill_color='red', line_width=0, size=10, line_alpha=0.75, fill_alpha=0.75),
Nitelikleri seçiniz: """) set_new_dataset("lens") radio_button_labels = ["gini", "gainRatio"] tree_mode_labels = ["Basit", "Detaylı"] arrow_list = {"current": [], "previous": []} current_label = "gini" selected_root = "" attribute_checkbox = CheckboxGroup(labels=[attr for attr in list(Instance().attr_list) if attr != Instance().attr_list[-1]], active=[i for i, attr in enumerate(list(Instance().attr_list))]) apply_changes_button = Button(label="Değişiklikleri uygula", button_type="success") decision_button = Toggle(label="Sonuç göster", button_type="warning") arrow_button = Toggle(label="Karar değerlerini göster", button_type="warning") root_select = Select(title="Kök niteliği seçiniz:", options=['Hiçbiri'] + [attr for attr in list(Instance().attr_list)[:-1]], value="Hiçbiri") method_select = Select(title="Metodu seçiniz:", options=radio_button_labels, value="gini") tree_select = Select(title="Ağacın görünümünü seçiniz:", options=tree_mode_labels, value="Basit") dataset_select = Select(title="Veri kümesini seç:", value="lens", options=["lens", "car"]) dataset_slider = Slider(start=0, end=50, value=0, step=1, title="Test verisi oranı") rect_width = 2 rect_height = 0.5 circle_radius = 5 TOOLTIPS = [ ("Metod Değeri", "@{nonLeafNodes_stat}"), ("Örnek Sayısı", "@{instances}"), ("Sonuç", "@{decision}") ] """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
def bokeh_plot(import_df): import pandas as pd import numpy as np from bokeh.plotting import figure, show from bokeh.layouts import layout, widgetbox, row, column, gridplot from bokeh.models import ColumnDataSource, HoverTool, BoxZoomTool, ResetTool, PanTool, CustomJS, PrintfTickFormatter, WheelZoomTool, SaveTool, LassoSelectTool, NumeralTickFormatter from bokeh.models.widgets import Slider, Select, TextInput, Div, Tabs, Panel, DataTable, DateFormatter, TableColumn, PreText, NumberFormatter, RangeSlider from bokeh.io import curdoc from functools import lru_cache from bokeh.transform import dodge from os.path import dirname, join from bokeh.core.properties import value #load plotting data here @lru_cache() def load_data(): df = import_df df.dropna(how='all', axis=0) #Northest=['3229','3277','3276','3230','3259','All_Stores_NE'] df.location_reference_id = df.location_reference_id.astype(str) #df['region'] = ['Northeast' if x in Northest else 'Midwest' for x in df['location_reference_id']] df['date'] = pd.to_datetime(df['date']) df[[ 'BOH_gt_Shelf_Capacity', 'OTL_gt_Shelf_Capacity', 'Ideal_BOH_gt_Shelf_Capacity', 'BOH_lt_Ideal', 'BOH_eq_Ideal', 'BOH_gt_Ideal', 'Demand_Fulfilled', 'Fill_Rate', 'Backroom_OH', 'Total_OH', 'Prop_OH_in_Backroom', 'Never_Q98_gt_POG', 'Never_Ideal_BOH_gt_POG', 'Sometimes_OTL_Casepack_1_gt_POG', 'Always_OTL_Casepack_1_le_POG', 'Non_POG' ]] = df[[ 'BOH > Shelf Capacity', 'OTL > Shelf Capacity', 'Ideal BOH > Shelf Capacity', 'BOH < Ideal', 'BOH = Ideal', 'BOH > Ideal', 'Demand Fulfilled', 'Fill Rate', 'Backroom_OH', 'Total OH', 'Prop OH in Backroom', 'Never: Q98 > POG', 'Never: Ideal BOH > POG', 'Sometimes: OTL+Casepack-1 > POG', 'Always: OTL+Casepack-1 <= POG', 'Non-POG' ]] df['date_bar'] = df['date'] df['date_bar'] = df['date_bar'].astype(str) return df #Filter data source for "All" stores OR data agrregation on DC level df_agg = load_data().groupby(['location_reference_id'], as_index=False).sum() source1 = ColumnDataSource(data=df_agg) sdate = min(load_data()['date']) edate = max(load_data()['date']) nodes = len(list(load_data().location_reference_id.unique())) days = len(list(load_data().date.unique())) policy = "Prod" #list of dates for vbar charts x_range_list = list(load_data().date_bar.unique()) #direct access to number of location_reference_idand region all_locations1 = list(load_data().location_reference_id.unique()) #agg_value=['All'] #all location_reference_idfrom csv file along with an option for agg data "All" #all_locations=all_locations1+agg_value #all_regions = ['Northeast', 'Midwest'] all_regions = list(load_data().region.unique()) desc = Div(text="All locations", width=230) pre = Div(text="_", width=230) location = Select(title="Location", options=all_locations1, value="All_Stores_NE") region = Select(title="Region", options=all_regions, value="NE") text_input = TextInput(value="default", title="Search Location:") #full data set from load_data(df=df_import) source = ColumnDataSource(data=load_data()) original_source = ColumnDataSource(data=load_data()) #plotting starts........... here are total 8 graphs for each Metric. #Back room on hand hover = HoverTool( tooltips=[("Location", "@location_reference_id"), ( "Date", "@date_bar"), ("Backroom_OH", "@Backroom_OH{0,0.00}")]) TOOLS = [ hover, BoxZoomTool(), LassoSelectTool(), WheelZoomTool(), PanTool(), ResetTool(), SaveTool() ] p = figure(x_range=x_range_list, plot_width=1000, plot_height=525, title="Backroom On hand by store", tools=TOOLS, toolbar_location='above', x_axis_label="Date", y_axis_label="Backroom OH") p.background_fill_color = "#e6e9ed" p.background_fill_alpha = 0.5 p.vbar(x=dodge('date_bar', -0.25, range=p.x_range), top='Backroom_OH', hover_alpha=0.5, hover_line_color='black', width=0.8, source=source, color="#718dbf") p.xaxis.major_label_orientation = 1 p.legend.border_line_width = 3 p.legend.border_line_color = None p.legend.border_line_alpha = 0.5 p.title.text_color = "olive" #inbound outbound hover_m = HoverTool( tooltips=[("Location", "@location_reference_id"), ( "Date", "@date_bar"), ( "Inbound", "@Inbound{0,0.00}"), ("Outbound", "@Outbound{0,0.00}")]) TOOLS_m = [ hover_m, BoxZoomTool(), LassoSelectTool(), WheelZoomTool(), PanTool(), ResetTool(), SaveTool() ] m = figure(plot_height=525, plot_width=1000, x_range=x_range_list, title="Inbound/Outbound by store", tools=TOOLS_m, toolbar_location='above', x_axis_label="Date", y_axis_label="Units") m.background_fill_color = "#e6e9ed" m.background_fill_alpha = 0.5 m.vbar(x=dodge('date_bar', -0.25, range=m.x_range), top='Inbound', hover_alpha=0.5, hover_line_color='black', width=0.4, source=source, color="#718dbf", legend=value("Inbound")) m.vbar(x=dodge('date_bar', 0.25, range=m.x_range), top='Outbound', hover_alpha=0.5, hover_line_color='black', width=0.4, source=source, color="#e84d60", legend=value("Outbound")) m.xaxis.major_label_orientation = 1 m.legend.border_line_width = 3 m.legend.border_line_color = None m.legend.border_line_alpha = 0.5 m.title.text_color = "olive" #Stockout hover_s = HoverTool( tooltips=[("Location", "@location_reference_id"), ( "Date", "@date_bar"), ( "BOH_OOS", "@BOH_OOS{0,0.000}"), ("EOH_OOS", "@EOH_OOS{0,0.000}")]) TOOLS_s = [ hover_s, BoxZoomTool(), LassoSelectTool(), WheelZoomTool(), PanTool(), ResetTool(), SaveTool() ] s = figure(plot_height=525, plot_width=1000, title="Stockouts by store", x_axis_type="datetime", toolbar_location='above', tools=TOOLS_s, x_axis_label="Date", y_axis_label="Prop Stockout") s.background_fill_color = "#e6e9ed" s.background_fill_alpha = 0.5 s.circle(x='date', y='EOH_OOS', source=source, fill_color=None, line_color="#4375c6") s.line(x='date', y='EOH_OOS', source=source, hover_alpha=0.5, hover_line_color='black', line_width=2, line_color='navy', legend=value("EOH OOS")) s.circle(x='date', y='BOH_OOS', source=source, fill_color=None, line_color="#4375c6") s.line(x='date', y='BOH_OOS', source=source, hover_alpha=0.5, hover_line_color='black', line_width=2, line_color='red', legend=value("BOH OOS")) s.legend.border_line_width = 3 s.legend.border_line_color = None s.legend.border_line_alpha = 0.5 s.title.text_color = "olive" #Fill rate hover_t = HoverTool( tooltips=[("Location", "@location_reference_id"), ( "Date", "@date_bar"), ("Fill Rate", "@Fill_Rate{0,0.00}")]) TOOLS_t = [ hover_t, BoxZoomTool(), LassoSelectTool(), WheelZoomTool(), PanTool(), ResetTool(), SaveTool() ] t = figure(plot_height=525, x_range=x_range_list, plot_width=1000, title="Fill rates by store", tools=TOOLS_t, toolbar_location='above', x_axis_label="Date", y_axis_label="Fill rate") t.background_fill_color = "#e6e9ed" t.background_fill_alpha = 0.5 t.vbar(x=dodge('date_bar', -0.25, range=t.x_range), top='Fill Rate', hover_alpha=0.5, hover_line_color='black', width=0.8, source=source, color="#718dbf") t.xaxis.major_label_orientation = 1 t.legend.border_line_width = 3 t.legend.border_line_color = None t.legend.border_line_alpha = 0.5 t.title.text_color = "olive" # % Backroom spillover hover_w = HoverTool( tooltips=[("Location", "@location_reference_id"), ("Date", "@date_bar"), ("Prop OH in Backroom", "@Prop_OH_in_Backroom{0,0.00}")]) TOOLS_w = [ hover_w, BoxZoomTool(), LassoSelectTool(), WheelZoomTool(), PanTool(), ResetTool(), SaveTool() ] w = figure(plot_height=525, plot_width=1000, title="Prop OH in Backroom by store", x_axis_type="datetime", tools=TOOLS_w, toolbar_location='above', x_axis_label="Date", y_axis_label=" % Backroom spillover") w.background_fill_color = "#e6e9ed" w.background_fill_alpha = 0.5 w.circle(x='date', y='Prop OH in Backroom', source=source, fill_color=None, line_color="#4375c6") w.line(x='date', y='Prop OH in Backroom', source=source, hover_alpha=0.5, hover_line_color='black', line_width=2, line_color='navy') w.title.text_font_style = "bold" w.title.text_color = "olive" w.legend.click_policy = "hide" w.yaxis[0].formatter = NumeralTickFormatter(format="0.0%") #BOH vs Ideal hover_f = HoverTool( tooltips=[("Location", "@location_reference_id"), ( "Date", "@date_bar"), ('BOH < Ideal', "@BOH_lt_Ideal{0,0.00}" ), ('BOH > Ideal', "@BOH_gt_Ideal{0,0.00}" ), ('BOH = Ideal', "@BOH_eq_Ideal{0,0.00}")]) TOOLS_f = [ hover_f, BoxZoomTool(), LassoSelectTool(), WheelZoomTool(), PanTool(), ResetTool(), SaveTool() ] colors = ["#c9d9d3", "#718dbf", "#e84d60"] BOH_vs_ideal = ['BOH < Ideal', 'BOH > Ideal', 'BOH = Ideal'] f = figure(x_range=x_range_list, plot_height=525, plot_width=1000, title="BOH vs Ideal by store", toolbar_location='above', x_axis_label="Date", y_axis_label="Prop", tools=TOOLS_f) f.vbar_stack(BOH_vs_ideal, x='date_bar', width=0.9, color=colors, source=source, legend=[value(x) for x in BOH_vs_ideal], name=BOH_vs_ideal) f.xaxis.major_label_orientation = 1 f.legend.border_line_width = 3 f.legend.border_line_color = None f.legend.border_line_alpha = 0.5 f.title.text_color = "olive" #Pog Fit hover_g = HoverTool( tooltips=[("Location", "@location_reference_id"), ( "Date", "@date_bar"), ('Never: Q98 > POG', "@Never_Q98_gt_POG{0,0.00}"), ("Never: Ideal BOH > POG", "@Never_Ideal_BOH_gt_POG{0,0.00}"), ("Sometimes: OTL+Casepack-1 > POG", "@Sometimes_OTL_Casepack_1_gt_POG{0,0.00}"), ("Always: OTL+Casepack-1 <= POG", "@Always_OTL_Casepack_1_le_POG{0,0.00}" ), ("Non-POG'", "@Non_POG{0,0.00}")]) TOOLS_g = [ hover_g, BoxZoomTool(), LassoSelectTool(), WheelZoomTool(), PanTool(), ResetTool(), SaveTool() ] colors2 = ['#79D151', "#718dbf", '#29788E', '#fc8d59', '#d53e4f'] pog_fit = [ 'Never: Q98 > POG', 'Never: Ideal BOH > POG', 'Sometimes: OTL+Casepack-1 > POG', 'Always: OTL+Casepack-1 <= POG', 'Non-POG' ] g = figure(x_range=x_range_list, plot_height=525, plot_width=1200, title="Pog Fit by store", toolbar_location='above', x_axis_label="Date", y_axis_label="Counts", tools=TOOLS_g) g.vbar_stack(pog_fit, x='date_bar', width=0.9, color=colors2, source=source, legend=[value(x) for x in pog_fit], name=pog_fit) g.xaxis.major_label_orientation = 1 g.legend.border_line_width = 3 g.legend.border_line_color = None g.legend.border_line_alpha = 0.5 g.title.text_color = "olive" g.legend.location = "top_right" # BOH vs Pog colors3 = ["#c9d9d3", "#718dbf", "#e84d60"] shelf = [ 'BOH > Shelf Capacity', 'OTL > Shelf Capacity', 'Ideal BOH > Shelf Capacity' ] hover_h = HoverTool( tooltips=[("Location", "@location_reference_id"), ("Date", "@date_bar"), ("OTL > Shelf Capacity", "@OTL_gt_Shelf_Capacity{0,0.00}" ), ("BOH > Shelf Capacity", "@BOH_gt_Shelf_Capacity{0,0.00}"), ("Ideal BOH > Shelf Capacity", "@Ideal_BOH_gt_Shelf_Capacity{0,0.00}")]) TOOLS_h = [ hover_h, BoxZoomTool(), LassoSelectTool(), WheelZoomTool(), PanTool(), ResetTool(), SaveTool() ] h = figure(plot_height=525, plot_width=1000, title="BOH vs Pog by store", x_axis_type="datetime", toolbar_location='above', tools=TOOLS_h, x_axis_label="Date", y_axis_label="Prop") h.background_fill_color = "#e6e9ed" h.background_fill_alpha = 0.5 h.circle(x='date', y='BOH > Shelf Capacity', source=source, fill_color=None, line_color="#4375c6") h.line(x='date', y='BOH > Shelf Capacity', source=source, hover_alpha=0.5, hover_line_color='black', line_width=2, line_color='navy', legend=value("BOH > Shelf Capacity")) h.circle(x='date', y='OTL > Shelf Capacity', source=source, fill_color=None, line_color="#4375c6") h.line(x='date', y='OTL > Shelf Capacity', source=source, hover_alpha=0.5, hover_line_color='black', line_width=2, line_color="green", legend=value("OTL > Shelf Capacity")) h.circle(x='date', y='Ideal BOH > Shelf Capacity', source=source, fill_color=None, line_color="#4375c6") h.line(x='date', y='Ideal BOH > Shelf Capacity', source=source, hover_alpha=0.5, hover_line_color='black', line_width=2, line_color="#e84d60", legend=value("Ideal BOH > Shelf Capacity")) h.legend.border_line_width = 3 h.legend.border_line_color = None h.legend.border_line_alpha = 0.5 h.title.text_color = "olive" h.legend.click_policy = "mute" # Inventory hover_j = HoverTool( tooltips=[("Location", "@location_reference_id"), ( "Date", "@date_bar"), ("DFE_Q98", "@DFE_Q98{0,0.00}"), ("OTL", "@OTL{0,0.00}"), ("EOH", "@EOH{0,0.00}"), ("BOH", "@BOH{0,0.00}")]) TOOLS_j = [ hover_j, BoxZoomTool(), LassoSelectTool(), WheelZoomTool(), PanTool(), ResetTool(), SaveTool() ] j = figure(plot_height=525, plot_width=1200, x_range=x_range_list, title="Inbound/Outbound by store", tools=TOOLS_j, toolbar_location='above', x_axis_label="Date", y_axis_label="Units") j.background_fill_color = "#e6e9ed" j.background_fill_alpha = 0.5 j.vbar(x=dodge('date_bar', -0.40, range=j.x_range), top='DFE_Q98', hover_alpha=0.3, hover_line_color='black', width=0.2, source=source, color="#FBA40A", legend=value("DFE_Q98")) j.vbar(x=dodge('date_bar', -0.20, range=j.x_range), top='OTL', hover_alpha=0.3, hover_line_color='black', width=0.2, source=source, color="#4292c6", legend=value("OTL")) j.vbar(x=dodge('date_bar', 0.00, range=j.x_range), top='EOH', hover_alpha=0.3, hover_line_color='black', width=0.2, source=source, color='#a1dab4', legend=value("EOH")) j.vbar(x=dodge('date_bar', 0.20, range=j.x_range), top='BOH', hover_alpha=0.3, hover_line_color='black', width=0.2, source=source, color="#DC5039", legend=value("BOH")) j.xaxis.major_label_orientation = 1 j.legend.border_line_width = 3 j.legend.border_line_color = None j.legend.border_line_alpha = 0.5 j.title.text_color = "olive" j.legend.location = "top_left" j.legend.click_policy = "mute" #desc.text = " <br > <b> Region:</b> <i> </i> <br /> " pre.text = " <b>Start date:</b> <i>{}</i> <br /> <b>End date:</b> <i>{}</i> <br /> <b>Time period:</b> <i>{}</i> days <br /> <b> Total Number of Nodes:</b> <i>{}</i> <br /> <b>Policy</b> = <i>{}</i><br /> ".format( sdate, edate, days, nodes, policy) #fuction to update data on selection callback = CustomJS(args=dict(source=source, original_source=original_source, location_select_obj=location, region_select_obj=region, div=desc, text_input=text_input), code=""" var data = source.data; var original_data = original_source.data; var loc = location_select_obj.value; var reg = region_select_obj.value; var line = " <br /> <b> Region:</b>"+ reg + "<br /> <b>Location:</b> " + loc; var text_input =text_input.value; div.text=line; for (var key in original_data) { data[key] = []; for (var i = 0; i < original_data['location_reference_id'].length; ++i) { if ((original_data['location_reference_id'][i] === loc) && (original_data['region'][i] === reg) ) { data[key].push(original_data[key][i]); } } } source.change.emit(); """) #controls = [location, region] #for control in controls: #control.js_on_change("value", callback) #source.js_on_change("value", callback) desc.js_on_event('event', callback) location.js_on_change('value', callback) region.js_on_change('value', callback) text_input.js_on_change('value', callback) #inputs = widgetbox(*controls, sizing_mode="fixed") #inputs = widgetbox(*controls,width=220,height=500) inputs = widgetbox(location, region, desc, pre, width=220, height=500) # controls number of tabs tab1 = Panel(child=p, title='Backroom OH') tab2 = Panel(child=s, title='Stockouts') tab3 = Panel(child=f, title='BOH vs Ideal') tab4 = Panel(child=g, title='Pog Fit') tab5 = Panel(child=m, title='Inbound/Outbound') tab6 = Panel(child=h, title='BOH vs POG') tab7 = Panel(child=t, title='Fill Rate') tab8 = Panel(child=j, title='Inventory') tab9 = Panel(child=w, title='Prop OH in Backroom') #data table columns to summarize data columns = [ TableColumn(field="location_reference_id", title="Location"), TableColumn(field="Backroom_OH", title="Backroom_OH", formatter=NumberFormatter(format="0,0")), TableColumn(field="Outbound", title="Outbound", formatter=NumberFormatter(format="0,0")), TableColumn(field="Inbound", title="Inbound", formatter=NumberFormatter(format="0,0")), TableColumn(field="OTL", title="OTL", formatter=NumberFormatter(format="0,0")), TableColumn(field="DFE_Q98", title="DFE_Q98", formatter=NumberFormatter(format="0,0")), TableColumn(field="BOH", title="BOH", formatter=NumberFormatter(format="0,0")), TableColumn(field="EOH", title="EOH", formatter=NumberFormatter(format="0,0")), TableColumn(field="BOH_OOS", title="BOH_OOS", formatter=NumberFormatter(format="0,0")), TableColumn(field="EOH_OOS", title="EOH_OOS", formatter=NumberFormatter(format="0,0")) ] data_table = DataTable(source=source1, columns=columns, width=1250) tab10 = Panel(child=data_table, title='Summary Table') view = Tabs( tabs=[tab1, tab2, tab5, tab8, tab6, tab3, tab7, tab4, tab9, tab10]) layout_text = column(inputs) layout1 = row(layout_text, view) #laying out plot layout2 = layout(children=[[layout_text, view]], sizing_mode='scale_height') #update plots return layout2
def bokeh_summary_plot(df, savepath=None): """Summary plot""" from bokeh.plotting import figure from bokeh.layouts import column from bokeh.models import ColumnDataSource, Range1d, HoverTool, TapTool, CustomJS, OpenURL TOOLS = "pan,wheel_zoom,hover,tap,reset,save" colors = get_bokeh_colors() df = df.rename(columns={'level_0': 'predictor'}) df['color'] = [colors[x] for x in df['predictor']] p = figure(title="Summary", tools=TOOLS, width=500, height=500) p.xaxis.axis_label = 'binder_density' p.yaxis.axis_label = 'binders' #make metric for point sizes #df['point_size'] = df.binder_density source = ColumnDataSource(data=df) p.circle(x='binder_density', y='binders', line_color='black', fill_color='color', fill_alpha=0.4, size=10, source=source, legend_group='predictor') hover = p.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([ ("name", "@name"), ("length", "@length"), ("binders", "@binders"), ("binder_density", "@binder_density"), ("top_peptide", "@top_peptide"), ("max_score", "@max_score"), ]) p.toolbar.logo = None if savepath != None: url = "http://*****:*****@name" % savepath taptool = p.select(type=TapTool) taptool.callback = OpenURL(url=url) callback = CustomJS(args=dict(source=source), code=""" var data = source.data; var f = cb_obj.value data['x'] = f source.trigger('change'); source.change.emit(); """) from bokeh.layouts import widgetbox from bokeh.models.widgets import Select menu = [(i, i) for i in df.columns] select = Select(title='X', value='A', options=list(df.columns), width=8) select.js_on_change('value', callback) #layout = column(p, select, sizing_mode='scale_width') return p
# Darstellung des Säulendiagramm bar.vbar(x='x', top='top', width=0.1, source=source_kat) # Allgemeinen Plot erstellen scatter = figure(plot_height=400, plot_width=400, title="Scatter-Plot", x_axis_label='total_bill', y_axis_label='tip') source_scatter = ColumnDataSource(data=dict(x=tips.total_bill, y=tips.tip)) # Darstellung des Scatter Diagramms scatter.scatter(x='x', y='y', source=source_scatter) # WIDGTS # Select Histogramm select_hist = Select(title="Histogramm", value="total_bill", options=list(tips.dtypes[tips.dtypes != 'category'].index)) select_cat = Select(title="Säulendiagramm", value="smoker", options=list(tips.dtypes[tips.dtypes == 'category'].index)) # Select Scatter select_x = Select(title="Scatter X", value="total_bill", options=['total_bill', 'size', 'tip']) select_y = Select(title="Scatter Y", value="tip", options=['total_bill', 'size', 'tip']) # Callbacks zum Aktualisieren der Visualisierungen def update_data(attrname, old, new): """Update der Daten sowie der Beschriftungen""" # Scatter Diagramm scatter.xaxis.axis_label = select_x.value scatter.yaxis.axis_label = select_y.value x = select_x.value
checkbox_group = CheckboxGroup(labels=["Option 1", "Option 2", "Option 3"], active=[0, 1]) radio_group = RadioGroup(labels=["Option 1", "Option 2", "Option 3"], active=0) checkbox_button_group = CheckboxButtonGroup( labels=["Option 1", "Option 2", "Option 3"], active=[0, 1]) radio_button_group = RadioButtonGroup( labels=["Option 1", "Option 2", "Option 3"], active=0) text_input = TextInput(placeholder="Enter value ...") autocomplete_input = AutocompleteInput() select = Select(options=["Option 1", "Option 2", "Option 3"]) multi_select = MultiSelect(options=["Option %d" % (i + 1) for i in range(16)], size=6) slider = Slider(value=10, start=0, end=100) range_slider = RangeSlider() #date_range_slider = DateRangeSlider(value=(date(2016, 1, 1), date(2016, 12, 31))) date_picker = DatePicker() paragraph = Paragraph(text="some text") div = Div(text="some <b>text</b>")
def get_data(t1, t2): df1 = load_ticker(t1) df2 = load_ticker(t2) data = pd.concat([df1, df2], axis=1) data = data.dropna() data['t1'] = data[t1] data['t2'] = data[t2] data['t1_returns'] = data[t1 + '_returns'] data['t2_returns'] = data[t2 + '_returns'] return data # set up widgets stats = PreText(text='', width=500) ticker1 = Select(value='AAPL', options=nix('GOOG', DEFAULT_TICKERS)) ticker2 = Select(value='GOOG', options=nix('AAPL', DEFAULT_TICKERS)) # set up plots source = ColumnDataSource( data=dict(date=[], t1=[], t2=[], t1_returns=[], t2_returns=[])) source_static = ColumnDataSource( data=dict(date=[], t1=[], t2=[], t1_returns=[], t2_returns=[])) tools = 'pan,wheel_zoom,xbox_select,reset' corr = figure(plot_width=350, plot_height=350, tools='pan,wheel_zoom,box_select,reset') corr.circle('t1_returns', 't2_returns',
class MetaModelVisualization(object): """ Top-level container for the Meta Model Visualization. Attributes ---------- prob : Problem Name of variable corresponding to Problem Component meta_model : MetaModel Name of empty Meta Model Component object reference resolution : int Number used to calculate width and height of contour plot is_structured_meta_model : Bool Boolean used to signal whether the meta model is structured or unstructured slider_source : ColumnDataSource Data source containing dictionary of sliders contour_training_data_source : ColumnDataSource Data source containing dictionary of training data points bottom_plot_source : ColumnDataSource Data source containing data for the bottom subplot bottom_plot_scatter_source : ColumnDataSource Data source containing scatter point data for the bottom subplot right_plot_source : ColumnDataSource Data source containing data for the right subplot right_plot_scatter_source : ColumnDataSource Data source containing scatter point data for the right subplot contour_plot_source : ColumnDataSource Data source containing data for the contour plot input_names : list List of input data titles as strings output_names : list List of output data titles as strings training_inputs : dict Dictionary of input training data x_input_select : Select Bokeh Select object containing a list of inputs for the x axis y_input_select : Select Bokeh Select object containing a list of inputs for the y axis output_select : Select Bokeh Select object containing a list of inputs for the outputs x_input_slider : Slider Bokeh Slider object containing a list of input values for the x axis y_input_slider : Slider Bokeh Slider object containing a list of input values for the y axis slider_dict : dict Dictionary of slider names and their respective slider objects predict_inputs : dict Dictionary containing training data points to predict at. num_inputs : int Number of inputs num_outputs : int Number of outputs limit_range : array Array containing the range of each input scatter_distance : TextInput Text input for user to enter custom value to calculate distance of training points around slice line right_alphas : array Array of points containing alpha values for right plot bottom_alphas : array Array of points containing alpha values for bottom plot dist_range : float Value taken from scatter_distance used for calculating distance of training points around slice line x_index : int Value of x axis column y_index : int Value of y axis column output_variable : int Value of output axis column sliders_and_selects : layout Layout containing the sliders and select elements doc_layout : layout Contains first row of plots doc_layout2 : layout Contains second row of plots Z : array A 2D array containing contour plot data """ def __init__(self, model, resolution=50, doc=None): """ Initialize parameters. Parameters ---------- model : MetaModelComponent Reference to meta model component resolution : int Value used to calculate the size of contour plot meshgrid doc : Document The bokeh document to build. """ self.prob = Problem() self.resolution = resolution logging.getLogger("bokeh").setLevel(logging.ERROR) # If the surrogate model coming in is structured if isinstance(model, MetaModelUnStructuredComp): self.is_structured_meta_model = False # Create list of input names, check if it has more than one input, then create list # of outputs self.input_names = [name[0] for name in model._surrogate_input_names] if len(self.input_names) < 2: raise ValueError('Must have more than one input value') self.output_names = [name[0] for name in model._surrogate_output_names] # Create reference for untructured component self.meta_model = MetaModelUnStructuredComp( default_surrogate=model.options['default_surrogate']) # If the surrogate model coming in is unstructured elif isinstance(model, MetaModelStructuredComp): self.is_structured_meta_model = True self.input_names = [name for name in model._var_rel_names['input']] if len(self.input_names) < 2: raise ValueError('Must have more than one input value') self.output_names = [name for name in model._var_rel_names['output']] self.meta_model = MetaModelStructuredComp( distributed=model.options['distributed'], extrapolate=model.options['extrapolate'], method=model.options['method'], training_data_gradients=model.options['training_data_gradients'], vec_size=1) # Pair input list names with their respective data self.training_inputs = {} self._setup_empty_prob_comp(model) # Setup dropdown menus for x/y inputs and the output value self.x_input_select = Select(title="X Input:", value=[x for x in self.input_names][0], options=[x for x in self.input_names]) self.x_input_select.on_change('value', self._x_input_update) self.y_input_select = Select(title="Y Input:", value=[x for x in self.input_names][1], options=[x for x in self.input_names]) self.y_input_select.on_change('value', self._y_input_update) self.output_select = Select(title="Output:", value=[x for x in self.output_names][0], options=[x for x in self.output_names]) self.output_select.on_change('value', self._output_value_update) # Create sliders for each input self.slider_dict = {} self.predict_inputs = {} for title, values in self.training_inputs.items(): slider_data = np.linspace(min(values), max(values), self.resolution) self.predict_inputs[title] = slider_data # Calculates the distance between slider ticks slider_step = slider_data[1] - slider_data[0] slider_object = Slider(start=min(values), end=max(values), value=min(values), step=slider_step, title=str(title)) self.slider_dict[title] = slider_object self._slider_attrs() # Length of inputs and outputs self.num_inputs = len(self.input_names) self.num_outputs = len(self.output_names) # Precalculate the problem bounds. limits = np.array([[min(value), max(value)] for value in self.training_inputs.values()]) self.limit_range = limits[:, 1] - limits[:, 0] # Positional indicies self.x_index = 0 self.y_index = 1 self.output_variable = self.output_names.index(self.output_select.value) # Data sources are filled with initial values # Slider Column Data Source self.slider_source = ColumnDataSource(data=self.predict_inputs) # Contour plot Column Data Source self.contour_plot_source = ColumnDataSource(data=dict( z=np.random.rand(self.resolution, self.resolution))) self.contour_training_data_source = ColumnDataSource( data=dict(x=np.repeat(0, self.resolution), y=np.repeat(0, self.resolution))) # Bottom plot Column Data Source self.bottom_plot_source = ColumnDataSource(data=dict( x=np.repeat(0, self.resolution), y=np.repeat(0, self.resolution))) self.bottom_plot_scatter_source = ColumnDataSource(data=dict( bot_slice_x=np.repeat(0, self.resolution), bot_slice_y=np.repeat(0, self.resolution))) # Right plot Column Data Source self.right_plot_source = ColumnDataSource(data=dict( x=np.repeat(0, self.resolution), y=np.repeat(0, self.resolution))) self.right_plot_scatter_source = ColumnDataSource(data=dict( right_slice_x=np.repeat(0, self.resolution), right_slice_y=np.repeat(0, self.resolution))) # Text input to change the distance of reach when searching for nearest data points self.scatter_distance = TextInput(value="0.1", title="Scatter Distance") self.scatter_distance.on_change('value', self._scatter_input) self.dist_range = float(self.scatter_distance.value) # Grouping all of the sliders and dropdowns into one column sliders = [value for value in self.slider_dict.values()] sliders.extend( [self.x_input_select, self.y_input_select, self.output_select, self.scatter_distance]) self.sliders_and_selects = row( column(*sliders)) # Layout creation self.doc_layout = row(self._contour_data(), self._right_plot(), self.sliders_and_selects) self.doc_layout2 = row(self._bottom_plot()) if doc is None: doc = curdoc() doc.add_root(self.doc_layout) doc.add_root(self.doc_layout2) doc.title = 'Meta Model Visualization' def _setup_empty_prob_comp(self, metamodel): """ Take data from surrogate ref and pass it into new surrogate model with empty Problem model. Parameters ---------- metamodel : MetaModelComponent Reference to meta model component """ # Check for structured or unstructured if self.is_structured_meta_model: # Loop through the input names for idx, name in enumerate(self.input_names): # Check for no training data try: # Append the input data/titles to a dictionary self.training_inputs[name] = metamodel.inputs[idx] # Also, append the data as an 'add_input' to the model reference self.meta_model.add_input(name, 0., training_data=metamodel.inputs[idx]) except TypeError: msg = "No training data present for one or more parameters" raise TypeError(msg) # Add the outputs to the model reference for idx, name in enumerate(self.output_names): self.meta_model.add_output( name, 0., training_data=metamodel.training_outputs[name]) else: for name in self.input_names: try: self.training_inputs[name] = { title for title in metamodel.options['train:' + str(name)]} self.meta_model.add_input( name, 0., training_data=[ title for title in metamodel.options['train:' + str(name)]]) except TypeError: msg = "No training data present for one or more parameters" raise TypeError(msg) for name in self.output_names: self.meta_model.add_output( name, 0., training_data=[ title for title in metamodel.options['train:' + str(name)]]) # Add the subsystem and setup self.prob.model.add_subsystem('interp', self.meta_model) self.prob.setup() def _slider_attrs(self): """ Assign data to slider objects and callback functions. Parameters ---------- None """ for name, slider_object in self.slider_dict.items(): # Checks if there is a callback previously assigned and then clears it if len(slider_object._callbacks) == 1: slider_object._callbacks.clear() # Check if the name matches the 'x input' title if name == self.x_input_select.value: # Set the object and add an event handler self.x_input_slider = slider_object self.x_input_slider.on_change('value', self._scatter_plots_update) # Check if the name matches the 'y input' title elif name == self.y_input_select.value: # Set the object and add an event handler self.y_input_slider = slider_object self.y_input_slider.on_change('value', self._scatter_plots_update) else: # If it is not an x or y input then just assign it the event handler slider_object.on_change('value', self._update) def _make_predictions(self, data): """ Run the data parameter through the surrogate model which is given in prob. Parameters ---------- data : dict Dictionary containing training points. Returns ------- array np.stack of predicted points. """ # Create dictionary with an empty list outputs = {name: [] for name in self.output_names} # Parse dict into shape [n**2, number of inputs] list inputs = np.empty([self.resolution**2, self.num_inputs]) for idx, values in enumerate(data.values()): inputs[:, idx] = values.flatten() # Check for structured or unstructured if self.is_structured_meta_model: # Assign each row of the data coming in to a tuple. Loop through the tuple, and append # the name of the input and value. for idx, tup in enumerate(inputs): for name, val in zip(data.keys(), tup): self.prob[self.meta_model.name + '.' + name] = val self.prob.run_model() # Append the predicted value(s) for title in self.output_names: outputs[title].append( np.array(self.prob[self.meta_model.name + '.' + title])) else: for idx, tup in enumerate(inputs): for name, val in zip(data.keys(), tup): self.prob[self.meta_model.name + '.' + name] = val self.prob.run_model() for title in self.output_names: outputs[title].append( float(self.prob[self.meta_model.name + '.' + title])) return stack_outputs(outputs) def _contour_data_calcs(self): """ Parse input data into a dictionary to be predicted at. Parameters ---------- None Returns ------- dict Dictionary of training data to be predicted at. """ # Create initial data array of training points resolution = self.resolution x_data = np.zeros((resolution, resolution, self.num_inputs)) self._slider_attrs() # Broadcast the inputs to every row of x_data array x_data[:, :, :] = np.array(self.input_point_list) # Find the x/y input titles and match their index positions for idx, (title, values) in enumerate(self.slider_source.data.items()): if title == self.x_input_select.value: self.xlins_mesh = values x_index_position = idx if title == self.y_input_select.value: self.ylins_mesh = values y_index_position = idx # Make meshgrid from the x/y inputs to be plotted X, Y = np.meshgrid(self.xlins_mesh, self.ylins_mesh) # Move the x/y inputs to their respective positions in x_data x_data[:, :, x_index_position] = X x_data[:, :, y_index_position] = Y pred_dict = {} for idx, title in enumerate(self.slider_source.data): pred_dict.update({title: x_data[:, :, idx]}) return pred_dict def _contour_data(self): """ Create a contour plot. Parameters ---------- None Returns ------- Bokeh Image Plot """ resolution = self.resolution # Output data array initialization y_data = np.zeros((resolution, resolution, self.num_outputs)) self.input_point_list = [point.value for point in self.slider_dict.values()] # Pass the dict to make predictions and then reshape the output to # (resolution, resolution, number of outputs) y_data[:, :, :] = self._make_predictions(self._contour_data_calcs()).reshape( (resolution, resolution, self.num_outputs)) # Use the output variable to pull the correct column of data from the predicted # data (y_data) self.Z = y_data[:, :, self.output_variable] # Reshape it to be 2D self.Z = self.Z.reshape(resolution, resolution) # Update the data source with new data self.contour_plot_source.data = dict(z=[self.Z]) # Min to max of training data self.contour_x_range = xlins = self.xlins_mesh self.contour_y_range = ylins = self.ylins_mesh # Color bar formatting color_mapper = LinearColorMapper( palette="Viridis11", low=np.amin(self.Z), high=np.amax(self.Z)) color_bar = ColorBar(color_mapper=color_mapper, ticker=BasicTicker(), label_standoff=12, location=(0, 0)) # Contour Plot self.contour_plot = contour_plot = figure( match_aspect=False, tooltips=[(self.x_input_select.value, "$x"), (self.y_input_select.value, "$y"), (self.output_select.value, "@z")], tools='') contour_plot.x_range.range_padding = 0 contour_plot.y_range.range_padding = 0 contour_plot.plot_width = 600 contour_plot.plot_height = 500 contour_plot.xaxis.axis_label = self.x_input_select.value contour_plot.yaxis.axis_label = self.y_input_select.value contour_plot.min_border_left = 0 contour_plot.add_layout(color_bar, 'right') contour_plot.x_range = Range1d(min(xlins), max(xlins)) contour_plot.y_range = Range1d(min(ylins), max(ylins)) contour_plot.image(image='z', source=self.contour_plot_source, x=min(xlins), y=min(ylins), dh=(max(ylins) - min(ylins)), dw=(max(xlins) - min(xlins)), palette="Viridis11") # Adding training data points overlay to contour plot if self.is_structured_meta_model: data = self._structured_training_points() else: data = self._unstructured_training_points() if len(data): # Add training data points overlay to contour plot data = np.array(data) if self.is_structured_meta_model: self.contour_training_data_source.data = dict(x=data[:, 0], y=data[:, 1], z=self.meta_model.training_outputs[ self.output_select.value].flatten()) else: self.contour_training_data_source.data = dict(x=data[:, 0], y=data[:, 1], z=self.meta_model._training_output[ self.output_select.value]) training_data_renderer = self.contour_plot.circle( x='x', y='y', source=self.contour_training_data_source, size=5, color='white', alpha=0.50) self.contour_plot.add_tools(HoverTool(renderers=[training_data_renderer], tooltips=[ (self.x_input_select.value + " (train)", '@x'), (self.y_input_select.value + " (train)", '@y'), (self.output_select.value + " (train)", '@z'), ])) return self.contour_plot def _right_plot(self): """ Create the right side subplot to view the projected slice. Parameters ---------- None Returns ------- Bokeh figure """ # List of the current positions of the sliders self.input_point_list = [point.value for point in self.slider_dict.values()] # Find the title of the y input and match it with the data y_idx = self.y_input_select.value y_data = self.predict_inputs[y_idx] # Find the position of the x_input slider x_value = self.x_input_slider.value # Rounds the x_data to match the predict_inputs value subplot_value_index = np.where( np.around(self.predict_inputs[self.x_input_select.value], 5) == np.around(x_value, 5))[0] # Make slice in Z data at the point calculated before and add it to the data source z_data = self.Z[:, subplot_value_index].flatten() x = z_data y = self.slider_source.data[y_idx] # Update the data source with new data self.right_plot_source.data = dict(x=x, y=y) # Create and format figure self.right_plot_fig = right_plot_fig = figure( plot_width=250, plot_height=500, title="{} vs {}".format(y_idx, self.output_select.value), tools="pan") right_plot_fig.xaxis.axis_label = self.output_select.value right_plot_fig.yaxis.axis_label = y_idx right_plot_fig.xaxis.major_label_orientation = math.pi / 9 right_plot_fig.line(x='x', y='y', source=self.right_plot_source) right_plot_fig.x_range.range_padding = 0.1 right_plot_fig.y_range.range_padding = 0.02 # Determine distance and alpha opacity of training points if self.is_structured_meta_model: data = self._structured_training_points(compute_distance=True, source='right') else: data = self._unstructured_training_points(compute_distance=True, source='right') self.right_alphas = 1.0 - data[:, 2] / self.dist_range # Training data scatter plot scatter_renderer = right_plot_fig.scatter(x=data[:, 3], y=data[:, 1], line_color=None, fill_color='#000000', fill_alpha=self.right_alphas.tolist()) right_plot_fig.add_tools(HoverTool(renderers=[scatter_renderer], tooltips=[ (self.output_select.value + " (train)", '@x'), (y_idx + " (train)", '@y'), ])) right_plot_fig.scatter(x=data[:, 3], y=data[:, 1], line_color=None, fill_color='#000000', fill_alpha=self.right_alphas.tolist()) span_width = self.dist_range * (max(y_data) - min(y_data)) # Set the right_plot data source to new values self.right_plot_scatter_source.data = dict( right_slice_x=np.repeat(x_value, self.resolution), right_slice_y=y_data, left_dashed=[i - span_width for i in np.repeat(x_value, self.resolution)], right_dashed=[i + span_width for i in np.repeat(x_value, self.resolution)]) self.contour_plot.line( 'right_slice_x', 'right_slice_y', source=self.right_plot_scatter_source, color='black', line_width=2) self.contour_plot.line( 'left_dashed', 'right_slice_y', line_dash='dashed', source=self.right_plot_scatter_source, color='black', line_width=2) self.contour_plot.line( 'right_dashed', 'right_slice_y', line_dash='dashed', source=self.right_plot_scatter_source, color='black', line_width=2) return self.right_plot_fig def _bottom_plot(self): """ Create the bottom subplot to view the projected slice. Parameters ---------- None Returns ------- Bokeh figure """ # List of the current positions of the sliders self.input_point_list = [point.value for point in self.slider_dict.values()] # Find the title of the x input and match it with the data x_idx = self.x_input_select.value x_data = self.predict_inputs[x_idx] # Find the position of the y_input slider y_value = self.y_input_slider.value # Rounds the y_data to match the predict_inputs value subplot_value_index = np.where( np.around(self.predict_inputs[self.y_input_select.value], 5) == np.around(y_value, 5))[0] # Make slice in Z data at the point calculated before and add it to the data source z_data = self.Z[subplot_value_index, :].flatten() x = self.slider_source.data[x_idx] y = z_data # Update the data source with new data self.bottom_plot_source.data = dict(x=x, y=y) # Create and format figure self.bottom_plot_fig = bottom_plot_fig = figure( plot_width=550, plot_height=250, title="{} vs {}".format(x_idx, self.output_select.value), tools="") bottom_plot_fig.xaxis.axis_label = x_idx bottom_plot_fig.yaxis.axis_label = self.output_select.value bottom_plot_fig.line(x='x', y='y', source=self.bottom_plot_source) bottom_plot_fig.x_range.range_padding = 0.02 bottom_plot_fig.y_range.range_padding = 0.1 # Determine distance and alpha opacity of training points if self.is_structured_meta_model: data = self._structured_training_points(compute_distance=True) else: data = self._unstructured_training_points(compute_distance=True) self.bottom_alphas = 1.0 - data[:, 2] / self.dist_range # Training data scatter plot scatter_renderer = bottom_plot_fig.scatter(x=data[:, 0], y=data[:, 3], line_color=None, fill_color='#000000', fill_alpha=self.bottom_alphas.tolist()) bottom_plot_fig.add_tools(HoverTool(renderers=[scatter_renderer], tooltips=[ (x_idx + " (train)", '@x'), (self.output_select.value + " (train)", '@y'), ])) span_width = self.dist_range * (max(x_data) - min(x_data)) # Set the right_plot data source to new values self.bottom_plot_scatter_source.data = dict( bot_slice_x=x_data, bot_slice_y=np.repeat(y_value, self.resolution), upper_dashed=[i + span_width for i in np.repeat(y_value, self.resolution)], lower_dashed=[i - span_width for i in np.repeat(y_value, self.resolution)]) self.contour_plot.line( 'bot_slice_x', 'bot_slice_y', source=self.bottom_plot_scatter_source, color='black', line_width=2) self.contour_plot.line( 'bot_slice_x', 'upper_dashed', line_dash='dashed', source=self.bottom_plot_scatter_source, color='black', line_width=2) self.contour_plot.line( 'bot_slice_x', 'lower_dashed', line_dash='dashed', source=self.bottom_plot_scatter_source, color='black', line_width=2) return self.bottom_plot_fig def _unstructured_training_points(self, compute_distance=False, source='bottom'): """ Calculate the training points and returns and array containing the position and alpha. Parameters ---------- compute_distance : bool If true, compute the distance of training points from surrogate line. source : str Which subplot the method is being called from. Returns ------- array The array of training points and their alpha opacity with respect to the surrogate line """ # Input training data and output training data x_training = self.meta_model._training_input training_output = np.squeeze(stack_outputs(self.meta_model._training_output), axis=1) # Index of input/output variables x_index = self.x_input_select.options.index(self.x_input_select.value) y_index = self.y_input_select.options.index(self.y_input_select.value) output_variable = self.output_names.index(self.output_select.value) # Vertically stack the x/y inputs and then transpose them infos = np.vstack((x_training[:, x_index], x_training[:, y_index])).transpose() if not compute_distance: return infos points = x_training.copy() # Normalize so each dimension spans [0, 1] points = np.divide(points, self.limit_range) dist_limit = np.linalg.norm(self.dist_range * self.limit_range) scaled_x0 = np.divide(self.input_point_list, self.limit_range) # Query the nearest neighbors tree for the closest points to the scaled x0 array # Nearest points to x slice if x_training.shape[1] < 3: tree = cKDTree(points) # Query the nearest neighbors tree for the closest points to the scaled x0 array dists, idxs = tree.query( scaled_x0, k=len(x_training), distance_upper_bound=self.dist_range) # kdtree query always returns requested k even if there are not enough valid points idx_finite = np.where(np.isfinite(dists)) dists = dists[idx_finite] idxs = idxs[idx_finite] else: dists, idxs = self._multidimension_input(scaled_x0, points, source=source) # data contains: # [x_value, y_value, ND-distance, func_value] data = np.zeros((len(idxs), 4)) for dist_index, j in enumerate(idxs): data[dist_index, 0:2] = infos[j, :] data[dist_index, 2] = dists[dist_index] data[dist_index, 3] = training_output[j, output_variable] return data def _structured_training_points(self, compute_distance=False, source='bottom'): """ Calculate the training points and return an array containing the position and alpha. Parameters ---------- compute_distance : bool If true, compute the distance of training points from surrogate line. source : str Which subplot the method is being called from. Returns ------- array The array of training points and their alpha opacity with respect to the surrogate line """ # Create tuple of the input parameters input_dimensions = tuple(self.meta_model.inputs) # Input training data and output training data x_training = np.array([z for z in product(*input_dimensions)]) training_output = self.meta_model.training_outputs[self.output_select.value].flatten() # Index of input/output variables x_index = self.x_input_select.options.index(self.x_input_select.value) y_index = self.y_input_select.options.index(self.y_input_select.value) # Vertically stack the x/y inputs and then transpose them infos = np.vstack((x_training[:, x_index], x_training[:, y_index])).transpose() if not compute_distance: return infos points = x_training.copy() # Normalize so each dimension spans [0, 1] points = np.divide(points, self.limit_range) self.dist_limit = np.linalg.norm(self.dist_range * self.limit_range) scaled_x0 = np.divide(self.input_point_list, self.limit_range) # Query the nearest neighbors tree for the closest points to the scaled x0 array # Nearest points to x slice if x_training.shape[1] < 3: x_tree, x_idx = self._two_dimension_input(scaled_x0, points, source=source) else: x_tree, x_idx = self._multidimension_input(scaled_x0, points, source=source) # format for 'data' # [x_value, y_value, ND-distance_(x or y), func_value] n = len(x_tree) data = np.zeros((n, 4)) for dist_index, j in enumerate(x_idx): data[dist_index, 0:2] = infos[j, :] data[dist_index, 2] = x_tree[dist_index] data[dist_index, 3] = training_output[j] return data def _two_dimension_input(self, scaled_points, training_points, source='bottom'): """ Calculate the distance of training points to the surrogate line. Parameters ---------- scaled_points : array Array of normalized slider positions. training_points : array Array of input training data. source : str Which subplot the method is being called from. Returns ------- idxs : array Index of closest points that are within the dist range. x_tree : array One dimentional array of points that are within the dist range. """ # Column of the input if source == 'right': col_idx = self.y_input_select.options.index(self.y_input_select.value) else: col_idx = self.x_input_select.options.index(self.x_input_select.value) # Delete the axis of input from source to predicted 1D distance x = np.delete(scaled_points, col_idx, axis=0) x_training_points = np.delete(training_points, col_idx, axis=1).flatten() # Tree of point distances x_tree = np.abs(x - x_training_points) # Only return points that are within our distance-viewing paramter. idx = np.where(x_tree <= self.dist_range) x_tree = x_tree[idx] return x_tree, idx[0] def _multidimension_input(self, scaled_points, training_points, source='bottom'): """ Calculate the distance of training points to the surrogate line. Parameters ---------- scaled_points : array Array of normalized slider positions. training_points : array Array of input training data. source : str Which subplot the method is being called from. Returns ------- idxs : array Index of closest points that are within the dist range. x_tree : array Array of points that are within the dist range. """ # Column of the input if source == 'right': col_idx = self.y_input_select.options.index(self.y_input_select.value) else: col_idx = self.x_input_select.options.index(self.x_input_select.value) # Delete the axis of input from source to predicted distance x = np.delete(scaled_points, col_idx, axis=0) x_training_points = np.delete(training_points, col_idx, axis=1) # Tree of point distances x_tree = cKDTree(x_training_points) # Query the nearest neighbors tree for the closest points to the scaled array dists, idx = x_tree.query(x, k=len(x_training_points), distance_upper_bound=self.dist_range) # kdtree query always returns requested k even if there are not enough valid points idx_finite = np.where(np.isfinite(dists)) dists_finite = dists[idx_finite] idx = idx[idx_finite] return dists_finite, idx # Event handler functions def _update_all_plots(self): self.doc_layout.children[0] = self._contour_data() self.doc_layout.children[1] = self._right_plot() self.doc_layout2.children[0] = self._bottom_plot() def _update_subplots(self): self.doc_layout.children[1] = self._right_plot() self.doc_layout2.children[0] = self._bottom_plot() def _update(self, attr, old, new): self._update_all_plots() def _scatter_plots_update(self, attr, old, new): self._update_subplots() def _scatter_input(self, attr, old, new): # Text input update function of dist range value self.dist_range = float(new) self._update_all_plots() def _x_input_update(self, attr, old, new): # Checks that x and y inputs are not equal to each other if new == self.y_input_select.value: raise ValueError("Inputs should not equal each other") else: self.x_input_select.value = new self._update_all_plots() def _y_input_update(self, attr, old, new): # Checks that x and y inputs are not equal to each other if new == self.x_input_select.value: raise ValueError("Inputs should not equal each other") else: self.y_input_select.value = new self._update_all_plots() def _output_value_update(self, attr, old, new): self.output_variable = self.output_names.index(new) self._update_all_plots()
class Market(): ########################################################################### # PLOT WIDGET ########################################################################### # +----------------------------+ # | +-----------+ +----------+ | # | | start_btn | | end_btn | | # | +-----------+ +----------+ | # +----------------------------+ # | | # | plot | # | | # +----------------------------+ ########################################################################### def __init__(self, start=(date.today() - relativedelta(years=3)), end=date.today()): self.start = str(start) self.end = str(end) self.sp500 = core.market.SP500('dataset/tickers.h5') symbols = self.sp500.store['info']['symbol'].values.tolist() # log.info("{}".format(symbols)) # Select tick button title = "Company tick" self.select_tick = Select(title=title, value=symbols[0], options=symbols) # Inputs buttons self.start_date = TextInput(value=self.start, title='start') self.end_date = TextInput(value=self.end, title='end') # layout self.plot_layout = self.candle_plot(symbols[0]) self.start_date.on_change('value', self.on_start_date_change) self.end_date.on_change('value', self.on_end_date_change) self.select_tick.on_change('value', self.on_tick_selection_change) self.layout = layout( [[self.select_tick, self.start_date, self.end_date], [self.plot_layout]]) def candle_plot(self, ticker, index_name='date'): self.df = self.sp500.get_ticker_stocks(ticker, self.start, self.end) self.df = plot.normalize_name(self.df) self.df = self.df.set_index(index_name) index = self.df.index.get_level_values(index_name) stock_data = {index_name: index} for val in self.df.columns.values: stock_data[val] = self.df[val] # log.info(stock_data) source = ColumnDataSource(data=dict(stock_data)) hover = HoverTool(tooltips=[('date', '@date{%F}'), ('adj close', '$@adj_close{%0.2f}'), ('adj open', '$@adj_open{%0.2f}'), ('volume', '@volume{0.00 a}'), ('open', '@open{%0.2f}'), ('close', '@close{%0.2f}')], formatters={ 'date': 'datetime', 'adj_close': 'printf', 'adj_open': 'printf', 'open': 'printf', 'close': 'printf' }, mode='vline') inc = self.df['close'] > self.df['open'] dec = self.df['open'] > self.df['close'] w = 12 * 60 * 60 * 1000 # half day in ms p = figure(x_axis_type="datetime", plot_width=1600, title=ticker, tools="xwheel_zoom, xpan, reset, save", active_drag="xpan") p.add_tools(hover) p.toolbar.logo = None p.grid.grid_line_alpha = 0.3 p.xaxis.major_label_orientation = pi / 4 p.xaxis.axis_label = 'Date' p.yaxis.axis_label = 'Price' p.line(index_name, 'adj_close', color='#A6CEE3', source=source) p.line(index_name, 'adj_open', color='#FB9A99', source=source) p.segment(index_name, 'high', index_name, 'low', color="white", source=source) p.vbar(index[inc], w, self.df['open'][inc], self.df['close'][inc], fill_color="#D5E1DD", line_color="white") p.vbar(index[dec], w, self.df['open'][dec], self.df['close'][dec], fill_color="#F2583E", line_color="white") p.legend.location = "top_left" p.background_fill_color = "black" columns = [ TableColumn(field="date", title="date", formatter=DateFormatter()) ] for key in stock_data.keys(): if key != 'date': columns.append(TableColumn(field=key, title=key)) # Layout return column(p, DataTable(source=source, columns=columns, width=1600)) # Callbacks def on_tick_selection_change(self, attr, old, new): log.debug('VALUE: old {} | new {}'.format(old, new)) self.layout.children[1] = self.candle_plot(new) def on_start_date_change(self, attr, old, new): log.debug('VALUE: old {} | new {}'.format(old, new)) self.start = new self.layout.children[1] = self.candle_plot(self.select_tick.value) def on_end_date_change(self, attr, old, new): log.debug('VALUE: old {} | new {}'.format(old, new)) self.end = new self.layout.children[1] = self.candle_plot(self.select_tick.value)
def Mosaic_Plot(clone_df, png=None, title="", top_clones=5000, count_col="Clustered", vgene_col="VGene", jgene_col="JGene", isotype_col="Isotype", vshm_col="V_SHM", jshm_col="J_SHM", vgene_colors=vgene_colors, vfamily_colors=vfamily_colors, jgene_colors=jgene_colors, isotype_colors=isotype_colors, line_width=0.3, figsize=(600, 600), hover_tooltip=True): figure_params = { "plot_width": figsize[0], "plot_height": figsize[1], #"sizing_mode": "scale_both", "x_range": Range1d(-0.1, 1.1, bounds=(-1.0, 2.0)), "y_range": Range1d(-0.1, 1.1, bounds=(-1.0, 2.0)), #"outline_line_alpha": 0.0, "title": title, "tools": "pan, wheel_zoom, box_zoom, save, reset, help", "active_scroll": "wheel_zoom", "toolbar_location": "right" } plot = figure(**figure_params) plot.grid.visible = False plot.axis.visible = False hover_tooltips = [("Clone ID", "@CloneID")] info_cols = [count_col] if vgene_col is not None: info_cols.append(vgene_col) hover_tooltips.append(("V Gene", "@" + vgene_col)) if jgene_col is not None: info_cols.append(jgene_col) hover_tooltips.append(("J Gene", "@" + jgene_col)) if isotype_col is not None: info_cols.append(isotype_col) hover_tooltips.append(("Isotype", "@" + isotype_col)) if vshm_col is not None: info_cols.append(vshm_col) hover_tooltips.append(("V Gene SHM", "@" + vshm_col + "{(0.00%)}")) if jshm_col is not None: info_cols.append(jshm_col) hover_tooltips.append(("J Gene SHM", "@" + jshm_col + "{(0.00%)}")) if hover_tooltip: hover_tool = HoverTool(point_policy="snap_to_data", tooltips=hover_tooltips) plot.add_tools(hover_tool) mosaic_df = clone_df[info_cols] mosaic_df = mosaic_df.sort_values([count_col], ascending=[False]) if top_clones: mosaic_df = mosaic_df.head(top_clones) total_area = float(mosaic_df[count_col].sum()) mosaic_df["Clone_Frequencies"] = mosaic_df[count_col].astype( float) / total_area hover_tooltips.append(("Clone Frequency", "@Clone_Frequencies{(0.00%)}")) mosaic_rects = squarify(mosaic_df["Clone_Frequencies"].tolist(), 0.0, 0.0, 1.0, 1.0) #Add half width/height to x/y position for center points mosaic_df["x"] = [rect["x"] + rect["dx"] / 2.0 for rect in mosaic_rects] mosaic_df["y"] = [rect["y"] + rect["dy"] / 2.0 for rect in mosaic_rects] mosaic_df["width"] = [rect["dx"] for rect in mosaic_rects] mosaic_df["height"] = [rect["dy"] for rect in mosaic_rects] #By default there is no legend text, since colors are alternating and non-informative mosaic_df["legend"] = "" mosaic_df["Empty_Legend"] = "" alternating_colors = [ RGB(102, 194, 165), RGB(252, 141, 98), RGB(141, 160, 203) ] alt2_color_cycle = cycle(alternating_colors[0:2]) alt3_color_cycle = cycle(alternating_colors) mosaic_df["alternating2_colors"] = [ next(alt2_color_cycle) for _ in mosaic_rects ] mosaic_df["alternating3_colors"] = [ next(alt3_color_cycle) for _ in mosaic_rects ] #Default color scheme is alternating 3 colors mosaic_df["fill_color"] = mosaic_df["alternating3_colors"] #Set up various mosaic coloring options and associated legends color_select_options = ["Alternating (2)", "Alternating (3)"] if vgene_col in mosaic_df.columns: mosaic_df["vgene_colors"] = mosaic_df[vgene_col].map(vgene_colors) vfamilies = mosaic_df[vgene_col].str.split("-").str[0] mosaic_df["vfamily_colors"] = vfamilies.map(vfamily_colors) color_select_options.append("V Gene") color_select_options.append("V Family") mosaic_df["VGene_Legend"] = mosaic_df[vgene_col] mosaic_df["VFamily_Legend"] = mosaic_df[vgene_col].str.split( "-").str[0] if jgene_col in mosaic_df.columns: mosaic_df["jgene_colors"] = mosaic_df[jgene_col].map(jgene_colors) color_select_options.append("J Gene") mosaic_df["JGene_Legend"] = mosaic_df[jgene_col] if isotype_col in mosaic_df.columns: mosaic_df["isotype_colors"] = mosaic_df[isotype_col].map( isotype_colors) color_select_options.append("Isotype") mosaic_df["Isotype_Legend"] = mosaic_df[isotype_col] #Using viridis as a quantitative heatmap color scheme for SHM values #The SHM values are binned into 180 groups; viridis in >180 bins uses some values twice, which pandas.cut can't use shm_viridis = list(viridis(180)) colorbar_tick_formatter = NumeralTickFormatter(format="0.00%") if vshm_col in mosaic_df.columns: vshm_min = mosaic_df[vshm_col].min() vshm_max = mosaic_df[vshm_col].max() #Use pandas.cut to bin the V gene SHM values into the heatmap colors mosaic_df["vshm_colors"] = pandas.cut(mosaic_df[vshm_col], bins=180, labels=shm_viridis) color_select_options.append("V Gene SHM") vshm_color_mapper = LinearColorMapper(palette=shm_viridis, low=vshm_min, high=vshm_max) vshm_ticks = FixedTicker(ticks=numpy.linspace(vshm_min, vshm_max, 8)) vshm_colorbar = ColorBar(color_mapper=vshm_color_mapper, location=(0, 0), name="vshm_colorbar", label_standoff=12, formatter=colorbar_tick_formatter, ticker=vshm_ticks) plot.add_layout(vshm_colorbar, "right") if jshm_col in mosaic_df.columns: jshm_min = mosaic_df[jshm_col].min() jshm_max = mosaic_df[jshm_col].max() #Use pandas.cut to bin the J gene SHM values into the heatmap colors mosaic_df["jshm_colors"] = pandas.cut(mosaic_df[jshm_col], bins=180, labels=shm_viridis) color_select_options.append("J Gene SHM") jshm_color_mapper = LinearColorMapper(palette=shm_viridis, low=jshm_min, high=jshm_max) jshm_ticks = FixedTicker(ticks=numpy.linspace(jshm_min, jshm_max, 8)) jshm_colorbar = ColorBar(color_mapper=jshm_color_mapper, location=(0, 0), name="jshm_colorbar", label_standoff=12, formatter=colorbar_tick_formatter, ticker=jshm_ticks) plot.add_layout(jshm_colorbar, "right") mosaic_source = ColumnDataSource(mosaic_df) plot.rect(x="x", y="y", width="width", height="height", fill_color="fill_color", legend="legend", line_color="black", line_width=line_width, source=mosaic_source) #By default, the plot legend and ColorBar should be turned off (since the color is repeating and uninformative) plot.legend[0].visible = False vshm_colorbar = plot.select("vshm_colorbar")[0] jshm_colorbar = plot.select("jshm_colorbar")[0] vshm_colorbar.visible = False jshm_colorbar.visible = False if png is not None: export_png(plot, png) change_args = { "source": mosaic_source, "legend_obj": plot.legend[0], "vshm_colorbar_obj": vshm_colorbar, "jshm_colorbar_obj": jshm_colorbar } change_rect_color = CustomJS(args=change_args, code=""" var selection = cb_obj.value.toLowerCase(); var new_color_array; var new_legend_array; if(selection.indexOf("v gene shm") !== -1) { new_color_array = source.data["vshm_colors"]; new_legend_array = source.data["Empty_Legend"]; legend_obj.visible = false; vshm_colorbar_obj.visible = true; jshm_colorbar_obj.visible = false; } else if(selection.indexOf("j gene shm") !== -1) { new_color_array = source.data["jshm_colors"]; new_legend_array = source.data["Empty_Legend"]; legend_obj.visible = false; vshm_colorbar_obj.visible = false; jshm_colorbar_obj.visible = true; } else if(selection.indexOf("v gene") !== -1) { new_color_array = source.data["vgene_colors"]; new_legend_array = source.data["VGene_Legend"]; legend_obj.visible = true; vshm_colorbar_obj.visible = false; jshm_colorbar_obj.visible = false; } else if(selection.indexOf("v family") !== -1) { new_color_array = source.data["vfamily_colors"]; new_legend_array = source.data["VFamily_Legend"]; legend_obj.visible = true; vshm_colorbar_obj.visible = false; jshm_colorbar_obj.visible = false; } else if(selection.indexOf("j gene") !== -1) { new_color_array = source.data["jgene_colors"]; new_legend_array = source.data["JGene_Legend"]; legend_obj.visible = true; vshm_colorbar_obj.visible = false; jshm_colorbar_obj.visible = false; } else if(selection.indexOf("isotype") !== -1) { new_color_array = source.data["isotype_colors"]; new_legend_array = source.data["Isotype_Legend"]; legend_obj.visible = true; vshm_colorbar_obj.visible = false; jshm_colorbar_obj.visible = false; } else if(selection.indexOf("2") !== -1) { new_color_array = source.data["alternating2_colors"]; new_legend_array = source.data["Empty_Legend"]; legend_obj.visible = false; vshm_colorbar_obj.visible = false; jshm_colorbar_obj.visible = false; } else { new_color_array = source.data["alternating3_colors"]; new_legend_array = source.data["Empty_Legend"]; legend_obj.visible = false; vshm_colorbar_obj.visible = false; jshm_colorbar_obj.visible = false; } var fill_color = source.data["fill_color"]; var legend = source.data["legend"]; for(idx = 0; idx < fill_color.length; idx++) { fill_color[idx] = new_color_array[idx]; legend[idx] = new_legend_array[idx]; } source.change.emit(); """) patch_coloring_select = Select(title="Color by:", options=color_select_options, value="Alternating (3)", callback=change_rect_color) plot_layout = column(patch_coloring_select, plot) return plot_layout
message += '<br>The image will be taken between %02d:%02d and %02d:%02d local time on December %dth'%(t0.hour, t0.minute, t1.hour, t1.minute, t1.day) message += '<br>We hope you\'ll join us in waving to Kepler!' messages.append([message]) print('Saving Results') cities['message'] = messages results = ColumnDataSource(cities.set_index('name').to_dict()['message']) args = dict(results=results) update = CustomJS(args=args, code=""" par.text = results.data[select.value][0]; """) p = Div(text=results.data['New York, USA'][0], width=400, height=100) select = Select(title='Nearest City:', value='New York, USA', options=sorted(cities['name'].values), callback=update) update.args['par'] = p update.args['select'] = select layout = column([select, p]) script, div = components(layout) name1 = 'web_app.script' name2 = 'web_app.div' print('Saving results as components: %s and %s'%(name1, name2)) with open(name1, 'w') as f: f.write(script) f.write('\n') with open(name2, 'w') as f: f.write(div)
def main(): df = pd.read_csv("nlp_entities.csv") per_df = pd.read_csv("Person.csv") pla_df = pd.read_csv("Place.csv") org_df = pd.read_csv("Org.csv") entities = ["Person", "Place", "Org"] relations = [["Person_Org","Person_Place","Person_Person"],["Place_Person","Place_Org","Place_Place"],["Org_Person","Org_Place","Org_Org"]] dfs = [per_df, pla_df,org_df] button_group = RadioButtonGroup(labels=entities, active=0) slider = Slider(title="by Index", value=0, start=0, end=len(per_df.index)-1, step=1) text = TextInput(title="by Name:", value='') select1 = Select(title="y axis:", value="Person", options=["Person", "Place", "Org"]) files = per_df.iloc[0]["Documents"] d = [] y_n = [] headlines = [] datestr = [] if files == "None": files = [] else: files = ast.literal_eval(files) for f in files: v_d = df.loc[df["filename"] == f]["date"].values datestr.append(v_d[0]) d.append(datetime.strptime(v_d[0],"%m/%d/%Y")) v_p = df.loc[df["filename"] == f]["Person"].values y_n.append(len(ast.literal_eval(v_p[0]))) v_h = df.loc[df["filename"] == f]["headlines"].values headlines.append(v_h[0]) dates = np.array(d) n = np.array(y_n) headlines = np.array(headlines) name = per_df.iloc[0]["Person"] # create the timeline plot for the searched entity s_source = ColumnDataSource(data=dict(x = dates, y = n, f = files, h =headlines, d = datestr)) TOOLS = "box_select,pan,box_zoom,reset" f1 = figure(tools=TOOLS, plot_width=600, plot_height=450, title="Timeline for '" + name + "'", x_axis_type='datetime', x_axis_label='Time', y_range=[-50,250], y_axis_label='number in total', output_backend="webgl") c1 = f1.circle('x', 'y', source=s_source, size=10, color="red", alpha=0.5) hover1 = HoverTool( tooltips=[ ("document: ", "@f"), ("date", "@d"), ("headline: ", "@h") ] ) f1.add_tools(hover1) f1.x_range = Range1d(datetime.strptime('01/01/2002',"%m/%d/%Y"),datetime.strptime('12/31/2004',"%m/%d/%Y")) def update_y(attr,old,new): y = select1.value new_y = [] for f in s_source.data["f"]: v = df.loc[df["filename"] == f][y].values new_y.append(len(ast.literal_eval(v[0]))) new_y = np.array(new_y) s_source.data["y"] = new_y return select1.on_change("value",update_y) # create interactive network graph for timeline plot s1 = ColumnDataSource(data=dict(xs=[], ys=[])) s2 = ColumnDataSource(data=dict(vx=[], vy=[], labels=[], color=[])) n1 = figure(plot_width=500, plot_height=500, x_axis_type=None, y_axis_type=None, outline_line_color=None, tools="pan,box_zoom,reset,save",title="Related Entities of Selected Files", output_backend="webgl") n1.multi_line('xs', 'ys', line_color="blue", source=s1, alpha=0.3) c2 = n1.circle('vx', 'vy', size=20, line_color="black", fill_color='color', source=s2, alpha=0.5) n1.text('vx', 'vy', text='labels', text_color="black", text_font_size="10px", text_align="center", text_baseline="middle", source=s2) # update network graph when the files are selected def update_network1(attr, old, new): inds = new['1d']['indices'] nodes = [] edges = [] t_t_edges = [] selected = [] if inds == []: s1.data = dict(xs=[], ys=[]) s2.data = dict(vx=[], vy=[], labels=[], color=[]) return for i in inds: f = s_source.data["f"][i] v = df.loc[df["filename"] == f]["all_nodes"].values n = ast.literal_eval(v[0]) selected.append(f) nodes = list(set(nodes).union(n)) e = ast.literal_eval(df.loc[df["filename"] == f]["all_edges"].values[0]) edges = list(set(edges).union(e)) t_t_e = ast.literal_eval(df.loc[df["filename"] == f]["file_neighbors_edge"].values[0]) t_t_edges = list(set(t_t_edges).union(t_t_e)) t_t = list(itertools.combinations(selected, 2)) for t in t_t: if (t in t_t_edges) or ((t[1],t[0])in t_t_edges): edges.append(t) new_dict = create_graph(nodes, edges, selected) s1.data = new_dict["s1"] s2.data = new_dict["s2"] return c1.data_source.on_change("selected", update_network1) # create person-place network graph v1 = per_df.at[0, "Person_Place"] edges_1 = ast.literal_eval(v1) nodes_1 = [name] for e in edges_1: nodes_1.append(e[1]) g1 = create_graph(nodes_1,edges_1,[name]) s3 = ColumnDataSource(data=g1["s1"]) s4 = ColumnDataSource(data=g1["s2"]) n2 = figure(plot_width=400, plot_height=400, x_axis_type=None, y_axis_type=None, outline_line_color=None, tools="pan,box_zoom,reset,save", title="Person_Place Relationships for Person: " + name, output_backend="webgl") n2.multi_line('xs', 'ys', line_color="blue", source=s3, alpha=0.3) c3 = n2.circle('vx', 'vy', size=20, line_color="black", fill_color='color', source=s4, alpha=0.5) n2.text('vx', 'vy', text='labels', text_color="black", text_font_size="8px", text_align="center", text_baseline="middle", source=s4) # create person-org network graph v2 = per_df.at[0, "Person_Org"] edges_2 = ast.literal_eval(v2) nodes_2 = [name] for e in edges_2: nodes_2.append(e[1]) g2 = create_graph(nodes_2, edges_2, [name]) s5 = ColumnDataSource(data=g2["s1"]) s6 = ColumnDataSource(data=g2["s2"]) n3 = figure(plot_width=400, plot_height=400, x_axis_type=None, y_axis_type=None, outline_line_color=None, tools="pan,box_zoom,reset,save", title="Person_Org Relationships for Person: " + name, output_backend="webgl") n3.multi_line('xs', 'ys', line_color="blue", source=s5, alpha=0.3) c4 = n3.circle('vx', 'vy', size=20, line_color="black", fill_color='color', source=s6, alpha=0.5) n3.text('vx', 'vy', text='labels', text_color="black", text_font_size="8px", text_align="center", text_baseline="middle", source=s6) # create person-org network graph v3 = per_df.at[0, "Person_Person"] edges_3 = ast.literal_eval(v3) nodes_3 = [name] for e in edges_3: nodes_3.append(e[1]) g3 = create_graph(nodes_3, edges_3, [name]) s7 = ColumnDataSource(data=g3["s1"]) s8 = ColumnDataSource(data=g3["s2"]) n4 = figure(plot_width=400, plot_height=400, x_axis_type=None, y_axis_type=None, outline_line_color=None, tools="pan,box_zoom,reset,save", title="Person_Person Relationships for Person: " + name, output_backend="webgl") n4.multi_line('xs', 'ys', line_color="blue", source=s7, alpha=0.3) c5 = n4.circle('vx', 'vy', size=20, line_color="black", fill_color='color', source=s8, alpha=0.5) n4.text('vx', 'vy', text='labels', text_color="black", text_font_size="8px", text_align="center", text_baseline="middle", source=s8) #update visualizations when button group changes def button_group_update(attr, old, new): b = button_group.active entity = entities[b] d = dfs[b] t = text.value #change slider slider.end = len(d.index) - 1 if (slider.value>slider.end): slider.value = slider.end if t == '': slider_update(attr, old, new) else: text_update(attr, old, new) button_group.on_change("active", button_group_update) def slider_update(attr,old,new): text.value = '' b = button_group.active entity = entities[b] d = dfs[b] s = slider.value # clear the visualizations sources = [[s1, s2], [s3, s4], [s5, s6], [s7, s8]] networks = [n1, n2, n3, n4] s_source.data = dict(x=[], y=[], f=[], h=[], d=[]) for i in range(4): sources[i][0].data = dict(xs=[], ys=[]) sources[i][1].data = dict(vx=[], vy=[], labels=[], color=[]) name = d.iloc[s][entity] # update cooccurence relationships for i in range(3): v = d.at[s, relations[b][i]] edges = ast.literal_eval(v) nodes = [name] for e in edges: nodes.append(e[1]) g = create_graph(nodes, edges, [name]) sources[i + 1][0].data = g["s1"] sources[i + 1][1].data = g["s2"] networks[i + 1].title.text = relations[b][i] + " Relationships for " + entity + ": " + name #update timeline plot files = d.iloc[s]["Documents"] dates = [] y_n = [] headlines = [] datestr = [] select = select1.value if files == "None": files = [] f1.title.text = "No files found for '" + name + "'" else: files = ast.literal_eval(files) for f in files: v_d = df.loc[df["filename"] == f]["date"].values datestr.append(v_d[0]) dates.append(datetime.strptime(v_d[0], "%m/%d/%Y")) v_p = df.loc[df["filename"] == f][select].values y_n.append(len(ast.literal_eval(v_p[0]))) v_h = df.loc[df["filename"] == f]["headlines"].values headlines.append(v_h[0]) dates = np.array(dates) n = np.array(y_n) headlines = np.array(headlines) s_source.data = dict(x=dates, y=n, f=files, h=headlines, d=datestr) f1.title.text = "Timeline for '" + name + "'" return slider.on_change("value", slider_update) def text_update(attr,old,new): b = button_group.active entity = entities[b] print(entity) d = dfs[b] t = text.value slider.value = 0 # clear the visualizations sources = [[s1, s2], [s3, s4], [s5, s6], [s7, s8]] networks = [n1, n2, n3, n4] s_source.data = dict(x=[], y=[], f=[], h=[], d=[]) for i in range(4): sources[i][0].data = dict(xs=[], ys=[]) sources[i][1].data = dict(vx=[], vy=[], labels=[], color=[]) name = t if t in np.array(d[entity]): s = d[d[entity] == t].index.tolist()[0] else: f1.title.text = "No such " + entity + " in the datasets" for x in networks: x.title.text = "No such " + entity + " in the datasets" return # update cooccurence relationships for i in range(3): v = d.at[s, relations[b][i]] edges = ast.literal_eval(v) nodes = [name] for e in edges: nodes.append(e[1]) g = create_graph(nodes, edges, [name]) sources[i + 1][0].data = g["s1"] sources[i + 1][1].data = g["s2"] networks[i + 1].title.text = relations[b][i] + " Relationships for " + entity + ": " + name # update timeline plot files = d.iloc[s]["Documents"] dates = [] y_n = [] headlines = [] datestr = [] select = select1.value if files == "None": files = [] f1.title.text = "No files found for '" + name + "'" else: files = ast.literal_eval(files) for f in files: v_d = df.loc[df["filename"] == f]["date"].values datestr.append(v_d[0]) dates.append(datetime.strptime(v_d[0], "%m/%d/%Y")) v_p = df.loc[df["filename"] == f][select].values y_n.append(len(ast.literal_eval(v_p[0]))) v_h = df.loc[df["filename"] == f]["headlines"].values headlines.append(v_h[0]) dates = np.array(dates) n = np.array(y_n) headlines = np.array(headlines) s_source.data = dict(x=dates, y=n, f=files, h=headlines, d=datestr) f1.title.text = "Timeline for '" + name + "'" return text.on_change("value",text_update) widgets = widgetbox(button_group,slider,text) layout = column(widgets,row(n2,n3,n4),select1,row(f1,n1)) curdoc().add_root(layout) show(layout)
def prepare(self): #Creating Sessions data set self.daily_sessions = self.data.groupby('start_date').agg({'start_date':'count'}).rename(columns = {'start_date':'Sessions'}) self.daily_sessions = self.daily_sessions.reset_index() self.daily_sessions['start_date'] = self.daily_sessions['start_date'].apply(lambda x:datetime.strptime(x, "%m/%d/%Y")) self.daily_sessions['Year'] = self.daily_sessions['start_date'].apply(lambda x:x.year) self.daily_sessions['Month'] = self.daily_sessions['start_date'].apply(lambda x:x.month) self.daily_sessions['Day'] = self.daily_sessions['start_date'].apply(lambda x:x.day) self.daily_sessions['Month_Abb'] = self.daily_sessions['Month'].apply(lambda x: calendar.month_abbr[x]) self.daily_sessions['Month_Abb'] = str(self.daily_sessions['Month_Abb']) self.daily_sessions = self.daily_sessions.sort_values(by = ['Year', 'Month', 'Day']) #print(self.daily_sessions.head(10)) #Creating Session Duration data set self.avg_time = self.data[['start_date', 'start_time', 'end_time']] self.avg_time['Delta'] = self.avg_time['end_time'] - self.avg_time['start_time'] self.avg_time['Delta'] = self.avg_time['Delta'].apply(lambda x:x.seconds) self.avg_time['Day'] = self.avg_time['start_time'].apply(lambda x:x.day) self.avg_time['Year'] = self.avg_time['start_time'].apply(lambda x:x.year) self.avg_time['Month'] = self.avg_time['start_time'].apply(lambda x:x.month) self.avg_time = self.avg_time.groupby(['start_date', 'Day', 'Month', 'Year']).mean().rename(columns = {'Delta':'Avg_Time'}) self.avg_time['Avg_Time'] = round(self.avg_time['Avg_Time']) self.avg_time = self.avg_time.reset_index() self.avg_time = self.avg_time.sort_values(by = ['Year', 'Month', 'Day']) #print(self.avg_time.head(10)) #Creating Users data set self.users = self.data.groupby(['start_date', 'device_id']).count().reset_index() self.users = self.users.groupby(['start_date', 'device_id']).device_id.count() self.users = pd.DataFrame(self.users).rename(columns = {'device_id': 'Users'}).reset_index() self.users_final = self.users.groupby('start_date').sum().reset_index() #print(self.users_final.head(10)) #Creating Final Data set for CDS self.daily_sessions = self.daily_sessions[['start_date', 'Year', 'Month', 'Day', 'Sessions']] self.avg_time = self.avg_time[['Year', 'Month', 'Day', 'Avg_Time']] self.data_final = self.avg_time self.data_final['Sessions'] = self.daily_sessions['Sessions'] self.data_final['Date'] = self.daily_sessions['start_date'] self.data_final['Users'] = self.users_final['Users'] self.data_final = self.data_final.rename(columns = {'Avg_Time':'Session Duration'}) #print(self.data_final.head(10)) #print(self.data_final.info()) ##Creating events data set for possibly a pie chart self.events = self.data.groupby(by = ['start_date', 'events_type']).agg({'events_type':'count'}).rename( columns = {'events_type': 'Sessions'} ) self.events = self.events.reset_index() self.events['start_date'] = self.events['start_date'].apply(lambda x:datetime.strptime(x, "%m/%d/%Y")) self.events.rename(columns = {'start_date':'Date'}, inplace = True) self.events['Year'] = self.events['Date'].apply(lambda x:x.year) self.events['Month'] = self.events['Date'].apply(lambda x:x.month) self.events['Day'] = self.events['Date'].apply(lambda x:x.day) #print(self.events.info()) ##Top Holograms self.holograms = self.data.groupby(by = ['start_date', 'hologram_loaded']).agg({'hologram_loaded':'count'}).rename( columns = {'hologram_loaded': 'Count'} ) self.holograms = self.holograms.reset_index() self.holograms['start_date'] = self.holograms['start_date'].apply(lambda x:datetime.strptime(x, "%m/%d/%Y")) self.holograms.rename(columns = {'start_date':'Date'}, inplace = True) self.holograms['Year'] = self.holograms['Date'].apply(lambda x:x.year) self.holograms['Month'] = self.holograms['Date'].apply(lambda x:x.month) self.holograms['Day'] = self.holograms['Date'].apply(lambda x:x.day) #print(self.holograms) #Adding Widgets self.options = Select(title="", options=["Last 7 days", "Last 30 days", "Last 90 days", "Custom.."], value="Custom..") self.radio_button_group = RadioButtonGroup(labels=["Users", "Sessions", "Session Duration"], active=1) self.date_from = DatePicker(title="From", min_date=DT.date(2017,8,29), max_date=DT.date(2018,8,29), value=DT.date(2018,1,1)) self.date_to = DatePicker(title="To", min_date=DT.date(2017,8,29), max_date=DT.date(2018,8,29), value=DT.date(2018,2,1)) self.hover1 = HoverTool( tooltips=[("Date", "@Date{%a, %d %B}"), ("Value", "@y")], formatters={ "@Date" : "datetime"}, mode = "vline" ) self.hover3 = HoverTool( tooltips=[("Hologram", "@hologram_loaded"), ("Count", "@Count")], mode = 'vline' ) self.source = ColumnDataSource(data=dict(x = [], y = [], Year=[], Month=[], Day=[], Date=[])) self.source_events = ColumnDataSource(data=dict(events_type = [], Sessions = [], Date=[], angle = [], color = [])) self.source_holograms = ColumnDataSource(data=dict(Rank = [], hologram_loaded = [], Count = [], color = [])) #Creating Plot self.plot = figure(plot_width=748, plot_height=525, tools = 'wheel_zoom,box_zoom,reset,save', x_axis_type="datetime", title = "How are your Users trending over time?") #x_axis_type="datetime" logo = None, self.plot.toolbar.logo = None self.plot.add_tools(self.hover1) self.plot.xgrid.visible = False self.plot.line(x = 'x', y = 'y', line_width = 3, source=self.source, color = "#2097BE") self.plot.circle(x = 'x', y = 'y', source = self.source, size=6, fill_color = "#2097BE", line_color = 'white') self.plot.xaxis.formatter=DatetimeTickFormatter(days=["%d %b"]) #self.plot.xaxis.major_label_orientation = 'vertical' self.plot.xaxis.minor_tick_line_color = None self.plot_events = figure(plot_width=748, plot_height=525, tools = 'wheel_zoom,box_zoom,reset,save', tooltips="@events_type: @Sessions", x_range=(-0.5, 1.0), title = "Breakdown of App Events") self.plot_events.wedge(x=0, y=1, radius=0.4, start_angle=cumsum('angle', include_zero=True), end_angle=cumsum('angle'), line_color="white", fill_color="color", source=self.source_events, legend = "events_type") self.plot_events.toolbar.logo = None self.plot_events.axis.axis_label=None self.plot_events.axis.visible=False self.plot_events.grid.grid_line_color = None self.plot_holograms = figure(plot_width=748, plot_height=525, tools = 'wheel_zoom,box_zoom,reset,save', title = "What are the Top Data Points people are interacting with?") self.plot_holograms.toolbar.logo = None self.plot_holograms.add_tools(self.hover1) self.plot_holograms.xgrid.visible = False self.plot_holograms.add_tools(self.hover3) self.plot_holograms.xaxis.minor_tick_line_color = None self.plot_holograms.vbar(x="Rank", top="Count", width = 0.95, source = self.source_holograms, color = "color") return self.plot, self.plot_events, self.plot_holograms, self.options, self.radio_button_group, self.date_from, self.date_to
from bokeh.plotting import figure x=[3,4,6,12,10,1,5,6,3,8] y=[7,1,3,4,1,6,10,4,10,3] label=['Red', 'Orange', 'Red', 'Orange','Red', 'Orange','Red', 'Orange','Red', 'Orange',] df=pd.DataFrame({'x':x,'y':y,'label':label}) source = ColumnDataSource(data=dict(x=df.x, y=df.y,label=df.label)) plot_figure = figure(title='Select',plot_height=450, plot_width=600, tools="save,reset", toolbar_location="below") plot_figure.scatter('x', 'y', source=source, size=10,color='label') select = Select(title="Filter plot by color:", value="All", options=["All", "Red", "Orange"]) def select_click(attr,old,new): active_select=select.value ##Getting radio button value # filter the dataframe with value in select if active_select!='All': selected_df=df[df['label']==active_select] else: selected_df=df.copy() source.data=dict(x=selected_df.x, y=selected_df.y,label=selected_df.label) select.on_change('value',select_click)
("MeSH Term", "@mesh_term"), ("Sample Size", "@sample_size"), ("Population", "@popu"), ("P Value", "@P"), ("BETA", "@beta"), ("SE", "@se"), ("PAINTOR PP", "@paintor"), ("CAVIARBF PP", "@caviarbf"), ("FINEMAP PP", "@finemap"), ] population_map = pd.Series({1: "EUR", 2: "AFR", 3: "AMR", 4: "EAS", 5: "SAS"}) # define widgets input_rsid = TextInput(title='rsID', value='rs6265') input_fm_tool = Select(title='Tool', value='FINEMAP', options=fm_tools) s1 = ColumnDataSource({ 'PP': [], 'P': [], 'color': [], 'size': [], 'mesh_term': [] }) p = figure(x_axis_label='-log10 (PP)', y_axis_label='-log10 (P)', plot_width=800, plot_height=400, tools=['pan', 'wheel_zoom', 'reset', 'hover'], active_scroll='wheel_zoom', toolbar_location=None)
product_names.insert(0, "All") # Get issue names cur.execute(ISSUE_NAMES) issue_names = sorted([x[0] for x in cur.fetchall()]) issue_names.insert(0, "All") # Create data source for state totals state_source = ColumnDataSource(data=dict()) zip_source = ColumnDataSource(data=dict(x=[], y=[])) table_source = ColumnDataSource(data=dict(labels=[], data=[])) ### Step 2: Build the UI # Build inputs state_widget = Select(title="State", value="All", options=states) issue_widget = Select(title="Issues", value="All", options=issue_names) product_widget = Select(title="Products", value="All", options=product_names) min_complaints_widget = Slider(title="Min # Complaints", value=0, start=0, end=100, step=10) # Helper Functions def generate_where_clause(state=None, product=None, issue=None, min_complaints=None): """ Generate a where clause given a set of filters """ where_inner = [] where_outer = [] if state and not state == "All":
values = quantities[q]['values'] filter.active = [filter.tags.index(v) for v in values] #inp_preset = Select( # title='Preset', # options=list(presets.keys()), # value=get_preset_label_from_url()) #inp_preset.on_change('value', load_preset) # quantities nq = len(quantities) # quantity selectors plot_options = [(q, quantities[q]['label']) for q in config.plot_quantities] inp_x = Select(title='X', options=plot_options) inp_y = Select(title='Y', options=plot_options) #inp_clr = Select(title='Color', options=plot_options) inp_clr = Select(title='Color', options=plot_options + [('adsorbaphore_label', 'Adsorbaphore')]) def on_filter_change(attr, old, new): # pylint: disable=unused-argument """Change color of plot button to blue""" btn_plot.button_type = 'primary' # range sliders # pylint: disable=redefined-builtin def get_slider(desc, range, default=None):