# ============================================================================= # def update_title(attrname, old, new): # plot.title.text = text.value # # text.on_change('value', update_title) # ============================================================================= # ============================================================================= # group=gdf.groupby('county') # sample=group.agg({'cofiltered':'mean'}).reset_index(drop=False) # lnd_bor.join(sample.set_index('county'),on='name') # # pd.DataFrame(lnd_bor.name).join(sample.set_index('county'),on='name') # ============================================================================= def update_data(attrname, old, new): co_now = lnd_bor[['name', 'geometry', Date.value]] co_now = co_now.rename(columns={Date.value: 'Ave_CO'}) gjson = co_now.to_json() source.geojson = gjson Date.on_change('value', update_data) # Set up layouts and add to document inputs = widgetbox(Date) curdoc().add_root(row(inputs, plot)) curdoc().title = "London"
start=VARS['global_start'], step=1, value=( VARS['time_range'][0], VARS['time_range'][1], )) single_day_selector = DateSlider( title="Select Day", end=VARS['global_end'], start=VARS['global_start'], step=1, value=VARS['selected_day'], ) date_selector.on_change('value', update_time_range) single_day_selector.on_change('value', update_highlighted_day) ################################################################## # Bokeh Plots ################################################################## TOOLS = "pan,wheel_zoom,box_select,lasso_select,reset" ########## # Stage Time Series ########## hydrograph = figure(plot_width=1000, plot_height=350, tools=TOOLS + ',box_zoom,hover', toolbar_location="above", toolbar_sticky=False,
return { 'selected': ColumnDataSource(data=selected), 'unselected': ColumnDataSource(unselected) } def update_plot(): src = get_datasets(df) selected_data_source.data.update(src['selected'].data) unselected_data_source.data.update(src['unselected'].data) df0 = pd.read_pickle(join(dirname(__file__), 'data', 'tremors.pkl')) df = df0.sample(frac=0.1) date_slider = DateSlider(start=df['date'].min(), end=df['date'].max(), step=100, value=df['date'].min()) date_slider.on_change('value', lambda attr, old, new: update_plot()) slider_box = widgetbox(children=[date_slider], width=600) datasets_init = get_datasets(df) # get bokeh ColumnDataSource selected_data_source = datasets_init['selected'] unselected_data_source = datasets_init['unselected'] p = make_plot(selected_data_source, unselected_data_source) layout = layout(children=[[p], [slider_box]], sizing_mode='fixed') curdoc().add_root(layout)
1000.0) #date_slider.value #slider.value #geosource = GeoJSONDataSource(geojson = json_data(day)) new_data = json_data(day) geosource.geojson = new_data p.title.text = 'CovIndex, {:%d %b %Y}'.format(day) #'CovIndex, %d' %day date_slider = DateSlider( title="Date Range: ", start=date(2020, 3, 25), end=date(2020, 5, 14), value=date(2020, 3, 25), step=86400000 ) # https://discourse.bokeh.org/t/bokeh-dateslider-still-broken/2466 date_slider.on_change('value', update_plot) # Make a column layout of widgetbox(slider) and plot, and add it to the current document layout = column(p, column(date_slider)) curdoc().add_root(layout) #Display plot inline in Jupyter notebook output_notebook() #Display plot show(layout) # Activate callback function with Bokeh Server # (run the command below on Anaconda Prompt, from the corect directory) # (it will open a new browser page) # bokeh serve --show 02806_DateSliderMap.py
def scatter(df): #global df1 def make_dataset(df): return ColumnDataSource(df) def style(p): # Title p.title.align = 'center' p.title.text_font_size = '20pt' p.title.text_font = 'serif' # Axis titles p.xaxis.axis_label_text_font_size = '14pt' p.xaxis.axis_label_text_font_style = 'bold' p.yaxis.axis_label_text_font_size = '14pt' p.yaxis.axis_label_text_font_style = 'bold' # Tick labels p.xaxis.major_label_text_font_size = '12pt' p.yaxis.major_label_text_font_size = '12pt' return p def make_plot(src): # Blank plot with correct labels p = figure(plot_width = 1000, plot_height = 600, title = 'Confirmed Cases vs Recovered Cases (Size of glyph = Confirmed Death)', x_axis_label = 'Confirmed Cases', y_axis_label = 'Recovered Cases') p.circle('confirmed_cases', 'recovered_cases', source=src, fill_alpha=0.7, size='death1', hover_fill_color = 'purple', hover_fill_alpha = 0.7, color='color', legend_field = 'continent') hover = HoverTool(tooltips=[('As at', '@Date{%F}'), ('Country', '@Country'), ('Confirmed', '@confirm'), ('Recovered', '@recovered'), ('Death', '@death')], formatters={'@Date': 'datetime'}) p.add_tools(hover) p.legend.location = "center_right" p.legend.click_policy = 'hide' # Styling p = style(p) return p # Callback function def update(attr, old, new): continent_to_plot = [continent_selection.labels[i] for i in continent_selection.active] df1 = df.set_index(['continent']) df1 = df1.loc[continent_to_plot] a = day_slider.value_as_date date = pd.to_datetime(a) d = df1[df1['Date'] == date] new_src = make_dataset(d) src.data.update(new_src.data) def animate_update(): day = day_slider.value_as_date + timedelta(days=1) if day>df['Date'].max(): day = df['Date'].min() day_slider.value = day def animate(): global callback_id if button.label == '► Play': button.label = '❚❚ Pause' callback_id = curdoc().add_periodic_callback(animate_update, 200) else: button.label = '► Play' curdoc().remove_periodic_callback(callback_id) callback_id = None button = Button(label='► Play', width=60) button.on_click(animate) value = list(df['continent'].unique()) continent_selection = CheckboxGroup(labels=value, active = [0, 1]) continent_selection.on_change('active', update) day_slider = DateSlider(title="Date: ", start=df['Date'].min(), end=df['Date'].max(), value=df['Date'].max(), step=1) day_slider.on_change('value', update) controls = row(continent_selection, day_slider, button) initial = [continent_selection.labels[i] for i in continent_selection.active] df1 = df.set_index(['continent']) df1 = df1.loc[initial] dat = df1['Date'].max() d = df1[df1['Date'] == dat] src = make_dataset(d) p = make_plot(src) layout = column([controls, p]) tab = Panel(child = layout, title = 'Progression') return tab
def third_tab_create(filterData): #def arima(date = dummy_date, house = dummy_house , data = dummy_data,trainDays = dummy_trainDays): def arima(date, state, data, trainDays): houseData = filterData.groupby( filterData[filterData['state'] == state]['time']).sum() houseData['time'] = houseData.index #houseData['time'] = pd.to_datetime(houseData['time']) #houseData = houseData[['car1','grid','solar','time','load']] houseData = houseData[['time', data]] #houseData = filterData[filterData['dataid'] == house][['time',data]] #houseData = houseData.sort_values('time', ascending = True) houseData = houseData.sort_index() #houseData.index = houseData['time'] startDate = pd.to_datetime(date) + pd.DateOffset(days=-trainDays) endDate = pd.to_datetime(date) + pd.DateOffset(days=1) daterange = [startDate, endDate] houseData = houseData.loc[daterange[0]:daterange[1], :] houseData[data] = houseData[data] * 60 * 15 / 3600 # kWh #houseData[data] = ( houseData[data] > .01 ) * houseData[data] weekdays = houseData[houseData.index.dayofweek < 5] weekends = houseData[houseData.index.dayofweek > 4] weekdayProfile = weekdays.groupby(weekdays['time'].dt.time).mean() weekendProfile = weekends.groupby(weekends['time'].dt.time).mean() houseData['detrend'] = houseData[data] for i in range(0, len(houseData)): if houseData['time'][i].dayofweek > 4: houseData['detrend'][i] = houseData['detrend'][ i] - weekendProfile[weekendProfile.index == houseData['time'].dt.time[i]][data] else: houseData['detrend'][i] = houseData['detrend'][ i] - weekdayProfile[weekdayProfile.index == houseData['time'].dt.time[i]][data] trainData = houseData['detrend'] stepwise_model = auto_arima(trainData, start_p=1, start_q=1, max_p=3, max_q=3, m=7, start_P=0, seasonal=True, d=1, D=1, trace=True, error_action='ignore', suppress_warnings=True, stepwise=True) train = houseData[data].loc[startDate:date] test = pd.DataFrame(data=houseData.loc[date:endDate]) test = test.drop(columns='detrend') future_forecast = stepwise_model.predict(n_periods=len(test)) test['arima'] = future_forecast if pd.to_datetime(date).dayofweek > 4: aveProfile = weekendProfile else: aveProfile = weekdayProfile for i in range(0, len(test)): test['arima'][i] = test['arima'][i] + aveProfile[ aveProfile.index == test['time'].dt.time[i]][data] test['error'] = abs(test[data] - test['arima']) #test['error'] = ( test['error'] > .03 ) * test['error'] test = test.rename(columns={data: 'data'}) test = test.drop(columns='time') #mape = 100 * sum( abs( test['error'] / test['data'] ) ) / len (test) mape = 100 * sum(abs(test['error'] / test['data'].max())) / len(test) print(stepwise_model.summary()) return ColumnDataSource(test), mape def plot1_plot(src, mape): plot1 = figure( title='PV Generation + Battery(Discharge) forecasting of NY', x_axis_label='Time', y_axis_label='Generation [kWh]', x_axis_type="datetime") a = plot1.line('time', 'data', source=src, color='blue') b = plot1.line('time', 'arima', source=src, color='green') c = plot1.line('time', 'error', source=src, color='red') plot1.plot_width = 1300 legend = Legend(items=[ LegendItem(label="Raw Data", renderers=[a], index=0), LegendItem(label="Forecast", renderers=[b], index=1), LegendItem(label="Error", renderers=[c], index=2), ]) plot1.add_layout(legend) plot1.legend.title = f'Abs Error = {round(mape1,3)}%' return plot1 def update(attr, old, new): button.update(button_type='warning', label='Loading, please wait') def calculate(): global home_to_plot, state_dict data_selector = data_type_selector.labels[ data_type_selector.active] if data_selector == 'Net Load': data_to_plot = 'grid' plot1.yaxis.axis_label = 'Net Load [kWh]' if data_selector == 'Load + Battery(Charging)': data_to_plot = 'Load_+_Battery(Charging)' plot1.yaxis.axis_label = 'Load [kWh]' if data_selector == "Electric Vehicle Consumption": data_to_plot = 'car1' plot1.yaxis.axis_label = 'Consumption [kWh]' if data_selector == "PV Generation + Battery(Discharge)": data_to_plot = 'PV_+_Battery(Discharge)' plot1.yaxis.axis_label = 'Generation [kWh]' trainDays_to_plot = int(trainDays_input.value) new_home_to_plot = state_dict[community_selector.active] #new_home_to_plot = int(home_id_selector.value) ### plot1.title.text = f'{data_selector} forecasting of {new_home_to_plot} for date {date_slider.value}' if new_home_to_plot != home_to_plot: startDate = filterData[ filterData['state'] == new_home_to_plot]['time'].iloc[0].date() ##change endDate = filterData[filterData['state'] == new_home_to_plot]['time'].iloc[-1].date() middle = startDate + (endDate - startDate) / 1.5 date_slider.start = startDate date_slider.end = endDate date_slider.value = middle date_to_plot = str(middle) print(startDate, endDate, middle) #daterange_raw = list(date_slider.value_as_datetime) #daterange_to_plot = [daterange_raw[0].strftime("%Y-%m-%d"), daterange_raw[1].strftime("%Y-%m-%d")] date_to_plot = date_slider.value new_src1, new_mape1 = arima(date=date_to_plot, state=new_home_to_plot, data=data_to_plot, trainDays=trainDays_to_plot) src1.data.update(new_src1.data) plot1.legend.title = f'Abs Error = {round(new_mape1,3)}%' button.update(button_type='success', label='Done') home_to_plot = new_home_to_plot curdoc().add_next_tick_callback(calculate) ## Initialize src and plot src1, mape1 = arima(date='2019-07-20', state='NY', data='PV_+_Battery(Discharge)', trainDays=2) plot1 = plot1_plot(src1, mape1) ## Date Slider date_slider = DateSlider(title="Date: ", start=date(2019, 5, 1), end=date(2019, 8, 20), value=date(2019, 7, 20), step=1, callback_policy='mouseup', width=1300) date_slider.on_change("value_throttled", update) ## Text input trainDays_input = TextInput(value='2', title='Training Days', max_width=200, max_height=50) trainDays_input.on_change('value', update) ## Data Options data_type_selector = RadioGroup(labels=[ "PV Generation + Battery(Discharge)", "Load + Battery(Charging)", "Net Load", "Electric Vehicle Consumption" ], active=0) data_type_selector.on_change('active', update) ## Loading Status button = Button(label="Done", button_type="success") ## Home Selector #home_ids_available = np.unique(filterData['dataid']) #home_ids_available = list(map(str, home_ids_available)) #home_id_selector = Dropdown(label="Home ID", button_type="warning", menu=home_ids_available, value="5679", max_width = 200) #home_id_selector.on_change('value',update) ## Agg house selection community_selector = RadioGroup(labels=list(np.unique( filterData['state'])), active=6, max_width=200) community_selector.on_change('active', update) row1 = row( plot1, column(data_type_selector, trainDays_input, community_selector, button, sizing_mode="scale_width")) row2 = row(date_slider) ## Layout layout = column(row1, row2) tab = Panel(child=layout, title='Forecasting') return tab
class ForecastByGeography(): """Shows US map and electoral college votes by state.""" # %% def __init__(self, controller): """Initialize object. First part of two-part initialization. Put initialization code here that's very unlikely to fail. """ self.controller = controller self.state = None self.state_src = None # State maop of the US. self.stateusmap = figure( title="""Electoral college votes by time and geography""", x_axis_location=None, y_axis_location=None, x_axis_type="""linear""", sizing_mode="""stretch_both""") self.stateusmap.xgrid.visible = False self.stateusmap.ygrid.visible = False # The date for charting. self.choosethedatefordisplay = DateSlider( title="""Choose the date for display""", start="""2018-11-13T20:20:39+00:00""", end="""2025-11-13T20:20:39+00:00""", step=24 * 60 * 60 * 1000, value="""2018-11-13T20:20:39+00:00""", sizing_mode="stretch_width") # Layout the widgets row1 = row(children=[ Spacer(width=10), self.choosethedatefordisplay, Spacer(width=10) ], sizing_mode='stretch_width') self.layout = column(children=[ self.stateusmap, row1, Spacer(height=75, sizing_mode='scale_width') ], sizing_mode='stretch_both') self.panel = Panel(child=self.layout, title='Forecast by geography') # %% def setup(self): """Set up object. Second part of two-part initialization. Place initialization code here that's more likely to fail. """ # Load the files containing the state outlines and the Alaska/Hawaii # dividing lines _folder = os.path.dirname(os.path.realpath(__file__)) _state_j = json.load( open(os.path.join(_folder, MAP_FOLDER, "state.json"), 'r')) _state = pd.DataFrame(_state_j['data']) _state = _state.sort_values('State abbreviation') _state['color'] = random.choices(brewer['RdBu'][11], k=_state.shape[0]) _state['Democratic percentage'] = np.random.rand(_state.shape[0]) _state['Republican percentage'] = np.random.rand(_state.shape[0]) _frame_j = json.load( open(os.path.join(_folder, MAP_FOLDER, "frame.json"), 'r')) # Set up the sources self.state_src = ColumnDataSource(_state) frame_src = ColumnDataSource( data=dict(x=_frame_j['data']['x'], y=_frame_j['data']['y'])) # Draw the states and the lines states = self.stateusmap.patches(xs='x', ys='y', source=self.state_src, fill_alpha=0.5, fill_color='color', line_color="gray", line_width=0.5) # The frame that separates AK, HI from the rest of the US self.stateusmap.multi_line(xs='x', ys='y', source=frame_src, line_color="gray", line_width=1.0) # Now set up the hover tool - so the state name is given hover = HoverTool(point_policy="follow_mouse", renderers=[states], tooltips=[ ("State name", "@{State name}"), ("State abbreviation", "@{State abbreviation}"), ("Democratic", "@{Democratic percentage}{%0.1f}"), ("Republican", "@{Republican percentage}{%0.1f}") ]) self.stateusmap.add_tools(hover) # Setup the callbacks. self.choosethedatefordisplay.on_change( "value", self.callback_choosethedatefordisplay) # %% def update(self, state): """Update view object.""" # Make a copy of the state data and change the copy self.state = state.copy() self.state['color index'] = self.state['Spread D-R'] * 100 self.state['color index'] = pd.cut( self.state['color index'], [-100, -10, -5, -2, -1, -0.5, 0.5, 1, 2, 5, 10, 100], labels=[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]) self.state['color'] =\ self.state['color index'].map( {k: v for k, v in enumerate(brewer['RdBu'][11])}) self.choosethedatefordisplay.start = self.state['Date'].min() self.choosethedatefordisplay.value = self.state['Date'].max() self.choosethedatefordisplay.end = self.state['Date'].max() self._update_chart(self.choosethedatefordisplay.value_as_datetime) # %% def _update_chart(self, date): """Update chart based on date.""" _slice = self.state[self.state['Date'] == date] self.state_src.data['color'] = \ _slice[['State abbreviation', 'color']].sort_values( 'State abbreviation')['color'].to_list() self.state_src.data['Democratic percentage'] = \ _slice[['State abbreviation', 'Democratic proportion']].sort_values( 'State abbreviation')[ 'Democratic proportion'].to_list() self.state_src.data['Republican percentage'] = \ _slice[['State abbreviation', 'Republican proportion']].sort_values( 'State abbreviation')[ 'Republican proportion'].to_list() # %% def callback_choosethedatefordisplay(self, attrname, old, new): """Execute callback method for self.choosethedatefordisplay.""" # pylint: disable=W0613 self._update_chart(self.choosethedatefordisplay.value_as_datetime)