def create_village_name_map(survey, pie_column): means = survey.groupby('village_name')['_gps_point_latitude', '_gps_point_longitude', pie_column].mean() source = bkm.ColumnDataSource( data=dict(vn=means.index, lat=means['_gps_point_latitude'], lon=means['_gps_point_longitude'], size=[10 for x in means[pie_column]], angle=means[pie_column] * 6.28)) wedges = bkg.Wedge(x='lon', y='lat', radius='size', start_angle=0, end_angle='angle', fill_color='green', fill_alpha=0.5, radius_units='screen') wedges2 = bkg.Wedge(x='lon', y='lat', radius='size', start_angle='angle', end_angle=6.28, fill_color='red', fill_alpha=0.5, radius_units='screen') text = bkg.Text(x='lon', y='lat', text='vn', text_color='000', text_font_size='12pt', x_offset=10) map_options = bkm.GMapOptions(lat=-2.588, lng=140.5170, zoom=11, map_type='terrain') plot = bkm.GMapPlot(x_range=bkm.Range1d(), y_range=bkm.Range1d(), map_options=map_options, title="Lake Sentani" + pie_column) plot.add_glyph(source, wedges) plot.add_glyph(source, wedges2) plot.add_glyph(source, text) #plot.add_tools(pan, wheel_zoom, box_zoom, resize, reset) plot.add_tools(bkm.BoxZoomTool(), bkm.PanTool(), bkm.ResetTool(), bkm.WheelZoomTool()) plot.add_layout( bkm.LinearAxis(axis_label='Longitude (deg)', major_tick_in=0), 'below') plot.add_layout( bkm.LinearAxis(axis_label='Latitude (deg)', major_tick_in=0), 'left') return plot
def plot_daily_gas_bkh(self, figsize=(650, 450), plot_temperature=False, colors=['black', 'darkturquoise']): p = bkh.figure(x_axis_type='datetime', plot_width=figsize[0], plot_height=figsize[1]) p.line(x=self.energy_average['Date_'], y=32 * np.ones(len(self.energy_average)), line_dash='dashed', line_color=colors[0], legend_label='Typical domestic use (medium)') p.line(x=self.energy_average['Date_'], y=self.energy_average['gas_daily_total'], line_color=colors[0], legend_label='Mean of 115,000 UK households') p.xaxis.axis_label = 'Date' p.xaxis[0].formatter = bkm.DatetimeTickFormatter(days=['%d/%m']) p.y_range = bkm.Range1d(15, 70) p.yaxis.axis_label = 'Gas Consumption (Corrected) (kWh/ALP)' p.yaxis.axis_label_text_color = colors[0] if plot_temperature and self.weather_file: p.extra_y_ranges = {'temperature': bkm.Range1d(start=6, end=24)} p.line(x=self.energy_average['Date_'], y=self.energy_average['temperature'], line_color=colors[1], legend_label='Temperature', y_range_name='temperature') p.add_layout( bkm.LinearAxis(y_range_name='temperature', axis_label='Mean Temperature (°C)', axis_label_text_color=colors[1], axis_line_color=colors[1], major_label_text_color=colors[1], major_tick_line_color=colors[1], minor_tick_line_color=colors[1]), 'right') r = self.energy_average[['gas_daily_total', 'temperature' ]].corr(method='pearson').values[1, 0] p.legend.title = f'R = {r:.3f}' p.legend.location = 'top_left' p.legend.background_fill_alpha = 1.0 #bkh.output_notebook() bkh.show(p)
def freqz(b, a=1, fs=2.0, worN=None, whole=False, degrees=True, style='solid', thickness=1, title=None, xlabel='Frequency (Hz)', xlim=None, ylim=None, width=None, height=None, hold=False, interactive=None): """Plot frequency response of a filter. This is a convenience function to plot frequency response, and internally uses :func:`scipy.signal.freqz` to estimate the response. For further details, see the documentation for :func:`scipy.signal.freqz`. :param b: numerator of a linear filter :param a: denominator of a linear filter :param fs: sampling rate in Hz (optional, normalized frequency if not specified) :param worN: see :func:`scipy.signal.freqz` :param whole: see :func:`scipy.signal.freqz` :param degrees: True to display phase in degrees, False for radians :param style: line style ('solid', 'dashed', 'dotted', 'dotdash', 'dashdot') :param thickness: line width in pixels :param title: figure title :param xlabel: x-axis label :param ylabel1: y-axis label for magnitude :param ylabel2: y-axis label for phase :param xlim: x-axis limits (min, max) :param ylim: y-axis limits (min, max) :param width: figure width in pixels :param height: figure height in pixels :param interactive: enable interactive tools (pan, zoom, etc) for plot :param hold: if set to True, output is not plotted immediately, but combined with the next plot >>> import arlpy >>> arlpy.plot.freqz([1,1,1,1,1], fs=120000); """ w, h = _sig.freqz(b, a, worN, whole) Hxx = 20 * _np.log10(abs(h) + _np.finfo(float).eps) f = w * fs / (2 * _np.pi) if xlim is None: xlim = (0, fs / 2) if ylim is None: ylim = (_np.max(Hxx) - 50, _np.max(Hxx) + 10) figure(title=title, xlabel=xlabel, ylabel='Amplitude (dB)', xlim=xlim, ylim=ylim, width=width, height=height, interactive=interactive) _hold_enable(True) plot(f, Hxx, color=color(0), style=style, thickness=thickness, legend='Magnitude') fig = gcf() units = 180 / _np.pi if degrees else 1 fig.extra_y_ranges = { 'phase': _bmodels.Range1d(start=-_np.pi * units, end=_np.pi * units) } fig.add_layout( _bmodels.LinearAxis( y_range_name='phase', axis_label='Phase (degrees)' if degrees else 'Phase (radians)'), 'right') phase = _np.angle(h) * units fig.line(f, phase, line_color=color(1), line_dash=style, line_width=thickness, legend='Phase', y_range_name='phase') _hold_enable(hold)
def make_multi_plot(merge): #s1 = figure(plot_width=400, plot_height=400, title=None) #s2 = figure(plot_width=400, plot_height=400, title=None) #merge = pd.DataFrame.from_csv('merge.csv') condition_1 = ((merge['bs_exp_offset'] >= 0) & (merge['bs_exp_offset'] <= 1)) condition_2 = ((merge['bs_exp_rsquared'] >= 0.8) & (merge['bs_exp_rsquared'] <= 1)) condition_3 = ((merge['select_line_aic_bs'] == False) & (merge['select_line_r_bs'] == False)) bs = merge[condition_1 & condition_2 & condition_3] X_bs = np.arange(-0.1, 13, 0.5) source = bkm.ColumnDataSource( dict( xs=[X_bs for i in bs.index.values], ys=[ model_deg(X_bs, bs.loc[n]['bs_exp_N0'], bs.loc[n]['bs_exp_tau'], bs.loc[n]['bs_exp_offset']) for n in bs.index.values ], )) xdr = bkm.DataRange1d() ydr = bkm.DataRange1d() s1 = bkm.Plot(title=None, x_range=xdr, y_range=ydr, plot_width=500, plot_height=500, toolbar_location='below') glyph = MultiLine(xs="xs", ys="ys", line_color="gray", line_width=1, line_alpha=0.1) s1.add_glyph(source, glyph) yaxis = bkm.LinearAxis() xaxis = bkm.LinearAxis() s1.add_layout(xaxis, 'below') s1.add_layout(yaxis, 'left') condition_1 = ((merge['pc_exp_offset'] >= 0) & (merge['pc_exp_offset'] <= 1)) condition_2 = ((merge['pc_exp_rsquared'] >= 0.8) & (merge['pc_exp_rsquared'] <= 1)) condition_3 = ((merge['select_line_aic_pc'] == False) & (merge['select_line_r_pc'] == False)) pc = merge[condition_1 & condition_2 & condition_3] X_pc = np.arange(-0.1, 28, 0.5) source2 = bkm.ColumnDataSource( dict( xs=[X_pc for i in pc.index.values], ys=[ model_deg(X_pc, pc.loc[n]['pc_exp_N0'], pc.loc[n]['pc_exp_tau'], pc.loc[n]['pc_exp_offset']) for n in pc.index.values ], )) xdr = bkm.DataRange1d() ydr = bkm.DataRange1d() s2 = bkm.Plot(title=None, x_range=xdr, y_range=ydr, plot_width=500, plot_height=500, toolbar_location='below') glyph = MultiLine(xs="xs", ys="ys", line_color="gray", line_width=1, line_alpha=0.1) s2.add_glyph(source2, glyph) yaxis = bkm.LinearAxis() xaxis = bkm.LinearAxis() s2.add_layout(xaxis, 'below') s2.add_layout(yaxis, 'left') #s2.add_layout(xaxis, 'below') #s2.add_layout(yaxis, 'left') s1.add_layout(bkm.Grid(dimension=0, ticker=xaxis.ticker)) s1.add_layout(bkm.Grid(dimension=1, ticker=yaxis.ticker)) s2.add_layout(bkm.Grid(dimension=0, ticker=xaxis.ticker)) s2.add_layout(bkm.Grid(dimension=1, ticker=yaxis.ticker)) return s1, s2
def botscore_hist(botscore_dict, data, use_cap=False): bins = { 'user_english': {}, 'tweet_english': {}, 'user_universal': {}, 'tweet_universal': {} } max_bin, score_type, score_mult = (101, 'cap', 100) if use_cap else (51, 'scores', 10) for key in bins: bins[key] = {start: 0 for start in range(0, max_bin)} grouped_tweets = pd.concat( [datum.df for datum in data]).groupby('UserId').size().to_frame('tweet_count') for user_id, scores in botscore_dict.items(): if user_id not in grouped_tweets.index: continue score_bin_english = int(scores[score_type]['english'] * score_mult) score_bin_universal = int(scores[score_type]['universal'] * score_mult) bins['user_english'][score_bin_english] += 1 bins['user_universal'][score_bin_universal] += 1 user_tweet_count = grouped_tweets.loc[user_id].tweet_count bins['tweet_english'][score_bin_english] -= user_tweet_count bins['tweet_universal'][score_bin_universal] -= user_tweet_count # Plot bins extra_y_start = min( it.chain(bins['tweet_english'].values(), bins['tweet_universal'].values())) - 1000 extra_y_end = max( it.chain(bins['user_english'].values(), bins['user_universal'].values())) + 5000 fig_english = bplt.figure(plot_width=500, plot_height=500, title='English Score Distr') fig_english.extra_y_ranges = { 'tweets': bmodels.Range1d(start=extra_y_start, end=extra_y_end) } fig_english.add_layout(bmodels.LinearAxis(y_range_name='tweets'), 'right') fig_english.vbar(x=list(bins['user_english'].keys()), width=0.95, bottom=0, top=list(bins['user_english'].values()), legend='Users') fig_english.vbar(x=list(bins['tweet_english'].keys()), width=0.95, bottom=0, top=list(bins['tweet_english'].values()), y_range_name='tweets', color='red', legend='Tweets') fig_universal = bplt.figure(plot_width=500, plot_height=500, title='Universal Score Distr') fig_universal.extra_y_ranges = { 'tweets': bmodels.Range1d(start=extra_y_start, end=extra_y_end) } fig_universal.add_layout(bmodels.LinearAxis(y_range_name='tweets'), 'right') fig_universal.vbar(x=list(bins['user_universal'].keys()), width=0.95, bottom=0, top=list(bins['user_universal'].values()), legend='Users') fig_universal.vbar(x=list(bins['tweet_universal'].keys()), width=0.95, bottom=0, top=list(bins['tweet_universal'].values()), y_range_name='tweets', color='red', legend='Tweets') bplt.show(blayouts.row(fig_english, fig_universal))
def generate_plot(data, df_all_prediction): COLORS = d3["Category10"][10] tooltips = f""" <div> <p> <span style="font-size: 12px; color: black;font-family:century gothic;">Nombre de cas : </span> <span style="font-size: 12px; color: {COLORS[0]}; font-weight: bold;font-family:century gothic;">@total_cases</span> <br> <span style="font-size: 12px; color: black;font-family:century gothic;">Nombre de nouveaux cas : </span> <span style="font-size: 12px; color: {COLORS[1]}; font-weight: bold;font-family:century gothic;">@new_cases</span> <br> <span style="font-size: 12px; color: black;font-family:century gothic;">Nombre de deces : </span> <span style="font-size: 12px; color: {COLORS[3]}; font-weight: bold;font-family:century gothic;">@total_deaths</span> <br> <span style="font-size: 12px; color: black;font-family:century gothic;">Nombre nouveaux deces : </span> <span style="font-size: 12px; color: {COLORS[5]}; font-weight: bold;font-family:century gothic;">@new_deaths</span> <br> <span style="font-size: 12px; color: black;font-family:century gothic;">Date : </span> <span style="font-size: 12px; color: black; font-weight: bold;font-family:century gothic;">@date_str</span> </p> </div> """ tooltips_predictions = (f""" <div> <p> <span style="font-size: 12px; color: black;font-family:century gothic;">Prédiction nombre de cas : </span> <span style="font-size: 12px; color: {COLORS[0]}; font-weight: bold;font-family:century gothic;">@median_display</span> <br> <span style="font-size: 12px; color: black;font-family:century gothic;">Date : </span> <span style="font-size: 12px; color: black; font-weight: bold;font-family:century gothic;">""" + """@date_str</span> </p> </div> """) hover = bkm.tools.HoverTool(names=["line_total"], tooltips=tooltips, mode="vline") hover_prediction = bkm.tools.HoverTool( names=["prediction"], tooltips=tooltips_predictions, ) # --- define all DataSource needed --- # source_all = bkm.ColumnDataSource(data) country = "World" source = bkm.ColumnDataSource(get_country(data, country)) source_all_prediction = bkm.ColumnDataSource(df_all_prediction) source_prediction = bkm.ColumnDataSource( get_country(df_all_prediction, country)) date_end_training = np.unique( get_country(df_all_prediction, country)["date_end_train"])[-1] source_prediction_end_date = bkm.ColumnDataSource( get_country(df_all_prediction, country)[get_country( df_all_prediction, country).date_end_train == date_end_training]) slider = bkm.Slider( start=0, end=len( np.unique( get_country(df_all_prediction, country)["date_end_train"])) - 1, value=0, step=1, title="Days dropped for prediction", ) # ----------- # p = bkp.figure( y_axis_type="linear", x_axis_type="datetime", sizing_mode="stretch_both", title=f"Covid 19 evolution: {country}", x_axis_label="date", y_axis_label="Total number of Covid 19 cases", tools=[hover, "pan", "wheel_zoom", "reset"], x_range=[ get_country(data, country).date.min(), get_country(data, country).date.max() + datetime.timedelta(days=1), ], y_range=[ -get_country(data, country).total_cases.max() * 0.05, get_country(data, country).total_cases.max() * 1.1, ], ) p.yaxis.formatter = bkm.formatters.NumeralTickFormatter(format="0,0") p.xaxis.formatter = bkm.formatters.DatetimeTickFormatter( days=["%d/%m", "%d%a"], months=["%m/%Y", "%b %Y"]) p.add_tools(hover_prediction) p.toolbar.active_drag = None # p.toolbar.active_scroll = p.select_one(bkm.WheelZoomTool) y_extra_range_max = np.max([ np.max(get_country(data, country).new_cases.values), np.max(get_country(data, country).total_deaths.values), ]) p.extra_y_ranges = { "Number of deaths": bkm.Range1d(start=-0.05 * y_extra_range_max, end=1.1 * y_extra_range_max) } p.add_layout( bkm.LinearAxis( y_range_name="Number of deaths", axis_label="New Covid 19 cases", formatter=bkm.formatters.NumeralTickFormatter(format="0,0"), ), "right", ) # --- plot total cases --- # p.line( source=source, x="date", y="total_cases", name="line_total", color=COLORS[0], legend_label="total cases", muted_alpha=0.1, ) p.circle(source=source, x="date", y="total_cases", color=COLORS[0], muted_alpha=0.1) # --- plot new cases --- # p.vbar( source=source, x="date", top="new_cases", color=COLORS[1], width=50e6, alpha=0.5, name="bar", y_range_name="Number of deaths", legend_label="new cases", muted_alpha=0.1, ) # --- plot total death --- # p.line( source=source, x="date", y="total_deaths", color=COLORS[3], y_range_name="Number of deaths", name="line_death", legend_label="total deaths", muted_alpha=0.1, ) p.circle( source=source, x="date", y="total_deaths", color=COLORS[3], y_range_name="Number of deaths", muted_alpha=0.1, ) # --- plot new death --- # p.vbar( source=source, x="date", top="new_deaths", color=COLORS[5], width=50e6, alpha=0.5, y_range_name="Number of deaths", legend_label="new deaths", muted_alpha=0.1, ) button_click_count = bkm.ColumnDataSource({"clicks": [0]}) select = bkm.Select(title="Country: ", value=country, options=list(data.location.unique())) button_log = bkm.Button(label="Log Scale", button_type="primary") # --- Predictions --- # median_prediction = p.line( source=source_prediction_end_date, x="date", y="median", line_color=COLORS[0], name="prediction", ) prediction_cases_line = p.line( source=source_prediction_end_date, x="date", y="derivative", color=COLORS[1], y_range_name="Number of deaths", ) band_low = bkm.Band( source=source_prediction_end_date, base="date", lower="25%", upper="median", fill_color=COLORS[0], level="underlay", fill_alpha=0.1, line_width=0.5, line_color="black", ) band_high = bkm.Band( source=source_prediction_end_date, base="date", lower="median", upper="75%", fill_color=COLORS[0], level="underlay", fill_alpha=0.1, line_width=0.5, line_color="black", ) median_prediction.visible = False prediction_cases_line.visible = False band_low.visible = False band_high.visible = False p.add_layout(band_low) p.add_layout(band_high) button_prediction = bkm.Button(label="Show predictions", button_type="primary") # -- Callback -- # callback = bkm.CustomJS( args=dict( source=source, source_all=source_all, select=select, x_range=p.x_range, y_range_left=p.y_range, y_range_right=p.extra_y_ranges["Number of deaths"], title=p.title, button_click_count=button_click_count, slider=slider, source_all_prediction=source_all_prediction, source_prediction=source_prediction, source_prediction_end_date=source_prediction_end_date, median_prediction=median_prediction, band_low=band_low, prediction_cases_line=prediction_cases_line, band_high=band_high, ), code=""" var country = select.value var date = source_all.data['date'] var date_str = source_all.data['date_str'] var location = source_all.data['location'] var total_cases = source_all.data['total_cases'] var new_cases = source_all.data['new_cases'] var total_deaths = source_all.data['total_deaths'] var new_deaths = source_all.data['new_deaths'] var new_date = [] var new_date_str = [] var new_total_cases = [] var new_new_cases = [] var new_total_deaths = [] var new_new_deaths = [] for(var i=0; i < date.length; i++){ if(location[i]==country){ new_date.push(date[i]); new_date_str.push(date_str[i]) new_total_cases.push(total_cases[i]); new_new_cases.push(new_cases[i]); new_total_deaths.push(total_deaths[i]); new_new_deaths.push(new_deaths[i]); } } source.data['date']=new_date; source.data['date_str']=new_date_str; source.data['total_cases']=new_total_cases; source.data['new_cases']=new_new_cases; source.data['total_deaths']=new_total_deaths; source.data['new_deaths']=new_new_deaths; const new_cases_no_Nan = new_new_cases.filter(function (value) { return !Number.isNaN(value); }); const cases_no_Nan = new_total_cases.filter(function (value) { return !Number.isNaN(value); }); y_range_right.setv({"start": -0.05*Math.max.apply(Math, new_cases_no_Nan.concat(new_total_deaths)), "end": 1.1*Math.max.apply(Math, new_cases_no_Nan.concat(new_total_deaths))}) y_range_left.setv({"start": -0.05*Math.max.apply(Math, cases_no_Nan), "end": 1.1*Math.max.apply(Math, cases_no_Nan)}) x_range.setv({"start": Math.min.apply(Math, new_date), "end": 1.0001*Math.max.apply(Math, new_date)}) title.text = "Evolution du nombre de cas en " + country source.change.emit(); // change value of predictions button_click_count.data.clicks = 0 median_prediction.visible = false band_low.visible = false band_high.visible = false prediction_cases_line.visble = false var date_end_prediction = source_all_prediction.data['date_end_train'] var location = source_all_prediction.data['location'] var date = source_all_prediction.data['date'] var date_str = source_all_prediction.data['date_str'] var quantile_1 = source_all_prediction.data['25%'] var quantile_2 = source_all_prediction.data['median'] var quantile_3 = source_all_prediction.data['75%'] var new_cases = source_all_prediction.data['derivative'] var median_prediction = source_all_prediction.data['median_display'] var new_date = [] var new_date_str = [] var new_date_end_prediction = [] var new_quantile_1 = [] var new_quantile_2 = [] var new_quantile_3 = [] var new_new_cases = [] var new_median_prediction = [] for(var i=0; i < quantile_1.length; i++){ if(location[i]==country){ new_date.push(date[i]) new_date_str.push(date_str[i]) new_date_end_prediction.push(date_end_prediction[i]) new_quantile_1.push(quantile_1[i]); new_quantile_2.push(quantile_2[i]); new_quantile_3.push(quantile_3[i]); new_new_cases.push(new_cases[i]); new_median_prediction.push(median_prediction[i]); } } source_prediction.data['date']=new_date source_prediction.data['date_str']=new_date_str source_prediction.data['date_end_train']=new_date_end_prediction source_prediction.data['25%']=new_quantile_1; source_prediction.data['median']=new_quantile_2; source_prediction.data['75%']=new_quantile_3; source_prediction.data['derivative']=new_new_cases; source_prediction.data['median_display']=new_median_prediction; var n = new_date.length var max_date = Math.max.apply(Math, new_date_end_prediction) var new_date_bis = [] var new_date_str_bis = [] var new_date_end_prediction_bis = [] var new_quantile_1_bis = [] var new_quantile_2_bis = [] var new_quantile_3_bis = [] var new_new_cases_bis = [] var new_median_prediction_bis = [] for(var i=0; i < n; i++){ if(new_date_end_prediction[i]==max_date){ new_date_bis.push(new_date[i]) new_date_str_bis.push(new_date_str[i]) new_date_end_prediction_bis.push(new_date_end_prediction[i]) new_quantile_1_bis.push(new_quantile_1[i]); new_quantile_2_bis.push(new_quantile_2[i]); new_quantile_3_bis.push(new_quantile_3[i]); new_new_cases_bis.push(new_new_cases[i]); new_median_prediction_bis.push(new_median_prediction[i]); } } var n = new_date_bis.length var max_date = Math.max.apply(Math, new_date_end_prediction_bis) source_prediction_end_date.data['date']=new_date_bis source_prediction_end_date.data['date_str']=new_date_str_bis source_prediction_end_date.data['date_end_train']=new_date_end_prediction_bis source_prediction_end_date.data['25%']=new_quantile_1_bis; source_prediction_end_date.data['median']=new_quantile_2_bis; source_prediction_end_date.data['75%']=new_quantile_3_bis; source_prediction_end_date.data['derivative']=new_new_cases_bis; source_prediction_end_date.data['median_display']=new_median_prediction_bis; source_prediction.change.emit(); source_prediction_end_date.change.emit() const unique = (value, index, self) => { return self.indexOf(value) === index } // change slider value slider.setv({"end": new_date_end_prediction.filter(unique).length - 1, "value": 0}) """, ) callback_button = bkm.CustomJS( args=dict(y_axis=p.left, title=p.title), code=""" console.log(y_axis) y_axis = LogAxis() """, ) select.js_on_change("value", callback) button_log.js_on_click(callback_button) callback_button = bkm.CustomJS( args=dict( source=source, source_prediction=source_prediction, source_all_prediction=source_all_prediction, source_prediction_end_date=source_prediction_end_date, select=select, button_prediction=button_prediction, median_prediction=median_prediction, band_low=band_low, prediction_cases_line=prediction_cases_line, band_high=band_high, button_click_count=button_click_count, x_range=p.x_range, y_range_left=p.y_range, y_range_right=p.extra_y_ranges["Number of deaths"], ), code=""" // function to get unique value of an array const unique = (value, index, self) => { return self.indexOf(value) === index } var date = source.data['date']; var total_cases = source.data['total_cases']; var new_cases = source.data['new_cases']; var total_deaths = source.data['total_deaths']; var date_prediction = source_prediction.data['date']; var total_cases_prediction = source_prediction.data['75%']; const new_cases_no_Nan = new_cases.filter(function (value) { return !Number.isNaN(value); }); const cases_no_Nan = total_cases.filter(function (value) { return !Number.isNaN(value); }); var country = select.value button_click_count.data.clicks ++ var show_prediction = (button_click_count.data.clicks % 2) == 1 var locations_predicted = source_all_prediction.data['location'].filter(unique) if (locations_predicted.includes(country) == false){ window.alert("This country doesn't have prediction: Available countries are: " + locations_predicted); } else{ if (show_prediction == true){ median_prediction.visible = true band_low.visible = true band_high.visible = true prediction_cases_line.visble = true const y_range_right_values = [].concat([].slice.call(new_cases_no_Nan), [].slice.call(total_deaths)) y_range_left.setv({"start": -0.05*Math.max.apply(Math, total_cases_prediction), "end": 1.1 * Math.max.apply(Math, total_cases_prediction)}) y_range_right.setv({"start": -0.05*Math.max.apply(Math, y_range_right_values) * Math.max.apply(Math, total_cases_prediction) / Math.max.apply(Math, cases_no_Nan), "end": 1.1*Math.max.apply(Math, y_range_right_values) * Math.max.apply(Math, total_cases_prediction) / Math.max.apply(Math, cases_no_Nan)}) x_range.setv({"start": Math.min.apply(Math, date_prediction), "end": 1.0001*Math.max.apply(Math, date_prediction)}) } else{ median_prediction.visible = false band_low.visible = false band_high.visible = false prediction_cases_line.visble = false const y_range_right_values = [].concat([].slice.call(new_cases_no_Nan), [].slice.call(total_deaths)) y_range_left.setv({"start": -0.05*Math.max.apply(Math, cases_no_Nan), "end": 1.1*Math.max.apply(Math, cases_no_Nan)}) y_range_right.setv({"start": -0.05*Math.max.apply(Math, y_range_right_values), "end": 1.1*Math.max.apply(Math, y_range_right_values)}) x_range.setv({"start": Math.min.apply(Math, date), "end": 1.0001*Math.max.apply(Math, date)}) } } """, ) button_prediction.js_on_click(callback_button) callback_slider = bkm.CustomJS( args=dict( source=source, source_prediction=source_prediction, source_all_prediction=source_all_prediction, source_prediction_end_date=source_prediction_end_date, select=select, prediction_cases_line=prediction_cases_line, slider=slider, button_click_count=button_click_count, x_range=p.x_range, y_range_left=p.y_range, y_range_right=p.extra_y_ranges["Number of deaths"], ), code=""" // function to get unique value of an array const unique = (value, index, self) => { return self.indexOf(value) === index } var slider_value = slider.value var country = select.value var date_prediction = source_prediction.data['date'] var date_str = source_prediction.data['date_str'] var date_end_prediction = source_prediction.data['date_end_train'] var quantile_1 = source_prediction.data['25%']; var quantile_2 = source_prediction.data['median'] var quantile_3 = source_prediction.data['75%'] var new_cases = source_prediction.data['derivative']; var median_prediction = source_prediction.data['median_display'] var unique_end_prediction = date_end_prediction.filter(unique) var show_prediction = (button_click_count.data.clicks % 2) == 1 var locations_predicted = source_all_prediction.data['location'].filter(unique) if (show_prediction == true && locations_predicted.includes(country)){ var new_date_prediction = [] var new_date_str = [] var new_date_end_prediction = [] var new_quantile_1 = [] var new_quantile_2 = [] var new_quantile_3 = [] var new_new_cases = [] var new_median_prediction = [] for(var i=0; i < quantile_1.length; i++){ if(date_end_prediction[i]==unique_end_prediction[slider.end - slider_value]){ new_date_prediction.push(date_prediction[i]) new_date_str.push(date_str[i]) new_date_end_prediction.push(date_end_prediction[i]) new_quantile_1.push(quantile_1[i]); new_quantile_2.push(quantile_2[i]); new_quantile_3.push(quantile_3[i]); new_new_cases.push(new_cases[i]); new_median_prediction.push(median_prediction[i]); } } source_prediction_end_date.data['date']=new_date_prediction source_prediction_end_date.data['date_str']=new_date_str source_prediction_end_date.data['date_end_train']=new_date_end_prediction source_prediction_end_date.data['25%']=new_quantile_1; source_prediction_end_date.data['median']=new_quantile_2; source_prediction_end_date.data['75%']=new_quantile_3; source_prediction_end_date.data['derivative']=new_new_cases; source_prediction_end_date.data['median_display']=new_median_prediction; source_prediction_end_date.change.emit(); var date_prediction = source_prediction_end_date.data['date']; var total_cases_prediction = source_prediction_end_date.data['75%']; const new_cases_no_Nan = new_cases.filter(function (value) { return !Number.isNaN(value); }); const cases_no_Nan = quantile_2.filter(function (value) { return !Number.isNaN(value); }); // y_range_left.setv({"start": -0.05*Math.max.apply(Math, total_cases_prediction), "end": 1.1*Math.max.apply(Math, total_cases_prediction)}) // x_range.setv({"start": Math.min.apply(Math, date_prediction), "end": 1.0001*Math.max.apply(Math, date_prediction)}) } """, ) slider.js_on_change("value", callback_slider) p.legend.location = "top_left" p.legend.click_policy = "mute" return select, button_prediction, slider, p
def print_html(): conso_elec, weather_stat = run() conso_elec['date_c'] = pd.to_datetime(conso_elec.date) conso_elec = conso_elec.sort_values(by='date_c') conso_elec = conso_elec.reset_index(drop=True) weather_stat['Dato'] = pd.to_datetime(weather_stat.Dato, format='%Y-%m-%d') if any(conso_elec.conso.diff() < 0): l = (conso_elec.conso.diff() < 0) ind = [i for i, l in enumerate(l) if l] # Give the index of the error line d = conso_elec.date[ind] print( "Your database contains an error, please check the values arround {d}" .format(d=d)) #Group by all conso in the same month, and take the last value to substract with previous month grp = conso_elec.groupby( [conso_elec.date_c.dt.year, conso_elec.date_c.dt.month]) res = grp.last() res.conso = res.conso.diff() #Group all the initial values #Conso x = res.date_c y = res.conso #Weather from Yr.no x_data = weather_stat.Dato y_min = weather_stat.Min_T y_max = weather_stat.Maks_T y_mean = weather_stat.Middel_T y_normal = weather_stat.Normal_T p = figure(plot_width=800, plot_height=500, y_axis_label="Energy consumed in kWh/month", x_axis_type="datetime", tools="resize,save", toolbar_location="above", title="Electrical consumption at Inkognito, Oslo") #Add second axis p.extra_y_ranges = { "Temp": bkm.Range1d(start=min(y_min - 5), end=max(y_max + 5)) } p.add_layout(bkm.LinearAxis(y_range_name="Temp"), 'right') #Source1 gets the conso values and print it with its hover method source1 = bkm.ColumnDataSource( data={ "x": x, "y": y, "bill": y * elec_price, "CO2": y * m_CO2, "time_tooltip": res.date }) g1 = bkm.VBar(x='x', top='y', width=1e9, bottom=0, fill_color='green') g1_r = p.add_glyph(source_or_glyph=source1, glyph=g1) g1_hover = bkm.HoverTool(renderers=[g1_r], tooltips={ "Conso": "@y kWh", "Bill": "±@bill{int} NOK", "CO2 prod.": "@CO2{int} Kg", "Date": "@time_tooltip" }) p.add_tools(g1_hover) #Creates a dictionnary with the weather data and ads a tooltip column for the date hover data = { "x_data": x_data, "y_min": y_min, "y_max": y_max, "y_mean": y_mean, "DJU": confort_T - y_mean } #Convert the date into a string to be read in the hover df = pd.DataFrame(data) df['tooltip'] = [x.strftime("%Y-%m-%d") for x in df['x_data']] #Source2 does the same as source1 with the temperatures source2 = bkm.ColumnDataSource(df) g2 = bkm.Line(x='x_data', y='y_mean', line_color='orange', name="Avg_temp", line_alpha=0.9) g2_r = p.add_glyph(source_or_glyph=source2, glyph=g2, y_range_name="Temp") g2_hover = bkm.HoverTool(renderers=[g2_r], tooltips={ "Avg. Temp.": "$y ˚C", "DJU": "@DJU{int}", "Date": "@tooltip" }) p.add_tools(g2_hover) p.line(x_data, y_max, y_range_name="Temp", legend="max_temp", color='red', line_alpha=0.5, line_dash='dashdot') p.line(x_data, y_min, y_range_name="Temp", legend="min_temp", color='blue', line_alpha=0.5, line_dash='dashdot') p.line(x_data, y_normal, y_range_name="Temp", legend="normal_temp", color='black', line_alpha=1, line_dash='dashed') output_file("conso_elec.html", title="Inkognito_conso") show(p)
def make_power_graph(output_file: str, data: typ.List, window: int) -> None: """グラフ作成. Args: output_file: 出力ファイル名 data: データ window: 移動平均のサンプル数 """ cols: typ.Tuple = ("time", "電力量", "電力", "電流R", "電流T", "MA電力", "MA電流R", "MA電流T") datadict: typ.Dict = {} for col in cols: datadict[col] = [] has_data: bool = len(data) > 0 for row in data: datadict["time"].append(row["created_at"]) datadict["電力量"].append(calc_電力量(row)) datadict["電力"].append(row["瞬時電力"]) datadict["電流R"].append(row["瞬時電流_r"] / 10.0) datadict["電流T"].append(row["瞬時電流_t"] / 10.0) datadict["MA電力"].append(statistics.mean(datadict["電力"][-window:])) datadict["MA電流T"].append(statistics.mean(datadict["電流T"][-window:])) datadict["MA電流R"].append(statistics.mean(datadict["電流R"][-window:])) source: bp.ColumnDataSource = bp.ColumnDataSource(datadict) tooltips: typ.List[typ.Tuple[str, str]] = [ ("time", "@time{%F %T}"), ("積算電力量", "@{電力量}{0,0.0}"), ("瞬時電力", "@{電力}{0,0}"), ("瞬時電流(R相)", "@{電流R}{0,0.0}"), ("瞬時電流(T相)", "@{電流T}{0,0.0}"), ] hover_tool: bm.HoverTool = bm.HoverTool(tooltips=tooltips, formatters={"@time": "datetime"}) bp.output_file(output_file, title="Power consumption") fig: bp.figure = bp.figure( title="Power consumption", x_axis_type="datetime", x_axis_label="時刻", y_axis_label="電力量[kWh]", sizing_mode="stretch_both", ) fig.add_tools(hover_tool) fmt: typ.List[str] = ["%H:%M"] fig.xaxis.formatter = bm.DatetimeTickFormatter(hours=fmt, hourmin=fmt, minutes=fmt) if has_data: 電力量_min: float = min(datadict["電力量"]) 電力量_max: float = max(datadict["電力量"]) 電力量_5p: float = (電力量_max - 電力量_min) * 0.05 fig.y_range = bm.Range1d(電力量_min - 電力量_5p, 電力量_max + 電力量_5p) fig.extra_y_ranges["W"] = bm.Range1d( 0, max(datadict["電力"]) * 1.05 if has_data else 0) fig.add_layout(bm.LinearAxis(y_range_name="W", axis_label="電力[W]"), "left") fig.extra_y_ranges["A"] = bm.Range1d( 0, max(*datadict["電流R"], *datadict["電流T"]) * 1.05 if has_data else 0) fig.add_layout(bm.LinearAxis(y_range_name="A", axis_label="電流[A]"), "right") fig.line("time", "電力量", legend_label="積算電力量", line_color="red", source=source) raw_data: typ.List = [ ("電力", "W", "瞬時電力", "orange"), ("電流R", "A", "瞬時電流(R相)", "blue"), ("電流T", "A", "瞬時電流(T相)", "green"), ] for col, range_name, legend_label, color in raw_data: fig.line( "time", col, y_range_name=range_name, legend_label=legend_label, line_color=color, line_alpha=0.8, source=source, ).visible = False ma_data: typ.List = [ ("MA電力", "W", "瞬時電力(移動平均)", "orange"), ("MA電流R", "A", "瞬時電流(R相)(移動平均)", "blue"), ("MA電流T", "A", "瞬時電流(T相)(移動平均)", "green"), ] for col, range_name, legend_label, color in ma_data: fig.line( "time", col, y_range_name=range_name, legend_label=legend_label, line_color=color, line_width=2, line_alpha=0.8, line_dash="dotted", source=source, ) fig.legend.click_policy = "hide" fig.legend.location = "top_left" bp.save(fig)
("astigmatism", "@astigmatism"), ("defocus U", "@defocusU"), ("defocus V", "@defocusV")]) phase_shift_fig.add_tools(phase_shift_hover) phase_shift_fig.xaxis.axis_label = "Micrograph number" phase_shift_fig.yaxis.axis_label = "Phase shift, degrees" ctf_fig = bplt.figure(plot_width=1200, title="CTF", tools="ywheel_zoom", y_range=(0, 1)) ctf_fig.extra_y_ranges = {"epa": bmodels.Range1d(start=10, end=20)} ctf_fig.add_layout(bmodels.LinearAxis(y_range_name="epa"), 'right') # print('Reading {}'.format(sys.argv[1])) # micrograph = mrcfile.open(sys.argv[1], 'r', permissive=True) # micrograph_ps = estimate_power_spectrum(micrograph.data, 512, 0.5) pixel_size = 0.66 # wys, wxs = np.meshgrid(np.linspace(-1/(2*pixel_size), 1/(2*pixel_size), 512, endpoint=False), # np.linspace(-1/(2*pixel_size), 1/(2*pixel_size), 512, endpoint=False), # indexing='ij') # mean = micrograph_ps[ np.sqrt(wys**2 + wxs**2) > 1/50 ].mean()
def make_temp_graph(output_file: str, temp_data: typ.List, co2_data: typ.List, bme280_data: typ.List) -> None: """グラフ作成. Args: output_file: 出力ファイル名 temp_data: 温度データ co2_data: CO2データ bme280_data: BME280のデータ """ tooltips: typ.List[typ.Tuple[str, str]] = [ ("time", "@time{%F %T}"), ] df: pd.DataFrame deg_max: int = 0 num_data: int = 0 y_axis_label: str = "温度[℃]" if len(temp_data) > 0: num_data += 1 if len(co2_data) > 0: num_data += 1 if len(bme280_data) > 0: num_data += 1 if len(temp_data) > 0: df1: pd.DataFrame = pd.DataFrame(temp_data, columns=list(temp_data[0].keys())) df1 = df1.rename(columns={"created_at": "time"}) if num_data > 1: df1["time"] = df1["time"].apply( lambda x: x.replace(second=0, microsecond=0)) df1["temp"] /= 1000 df1 = df1[["time", "temp"]].drop_duplicates(subset="time") df = df1 tooltips.append(("CPU温度", "@temp{0.0}")) deg_max = int(df["temp"].max()) + 10 if len(co2_data) > 0: df2: pd.DataFrame = pd.DataFrame(co2_data, columns=list(co2_data[0].keys())) df2 = df2.rename(columns={"temp": "temp2", "created_at": "time"}) if num_data > 1: df2["time"] = df2["time"].apply( lambda x: x.replace(second=0, microsecond=0)) df2 = df2[["time", "co2", "temp2"]].drop_duplicates(subset="time") if len(temp_data) > 0: df = pd.merge(df1, df2, on="time", how="outer").sort_values("time") else: df = df2 if len(bme280_data) == 0: tooltips.append(("気温", "@temp2")) tooltips.append(("CO₂", "@co2")) deg_max = max(deg_max, int(df["temp2"].max()) + 10) if len(bme280_data) > 0: df3: pd.DataFrame = pd.DataFrame(bme280_data, columns=list(bme280_data[0].keys())) df3 = df3.rename(columns={"temp": "temp3", "created_at": "time"}) if num_data > 1: df3["time"] = df3["time"].apply( lambda x: x.replace(second=0, microsecond=0)) df3 = df3[["time", "temp3", "pressure", "humidity"]].drop_duplicates(subset="time") if num_data > 1: df = pd.merge(df, df3, on="time", how="outer").sort_values("time") else: df = df3 tooltips.append(("気温", "@temp3{0.0}")) tooltips.append(("湿度", "@humidity{0.0}")) tooltips.append(("気圧", "@pressure{0,0.0}")) deg_max = max(deg_max, int(df["temp3"].max()) + 10, int(df["humidity"].max()) + 10) y_axis_label += "/湿度[%]" source: bp.ColumnDataSource = bp.ColumnDataSource(df) hover_tool: bm.HoverTool = bm.HoverTool(tooltips=tooltips, formatters={"@time": "datetime"}) bp.output_file(output_file, title="Temperature") fig: bp.figure = bp.figure( title="Temperature", x_axis_type="datetime", x_axis_label="時刻", y_axis_label=y_axis_label, sizing_mode="stretch_both", ) fig.add_tools(hover_tool) fmt: typ.List[str] = ["%H:%M"] fig.xaxis.formatter = bm.DatetimeTickFormatter(hours=fmt, hourmin=fmt, minutes=fmt) fig.y_range = bm.Range1d(0, deg_max) if len(temp_data) > 0: fig.line("time", "temp", legend_label="CPU温度", line_color="red", source=source) if len(co2_data) > 0: if len(bme280_data) == 0: fig.line("time", "temp2", legend_label="気温", line_color="darkorange", source=source) fig.extra_y_ranges["ppm"] = bm.Range1d( 0, max(2000, df["co2"].max() * 1.05)) fig.add_layout(bm.LinearAxis(y_range_name="ppm", axis_label="濃度[ppm]"), "right") fig.line("time", "co2", legend_label="CO₂", line_color="green", y_range_name="ppm", source=source) if len(bme280_data) > 0: fig.line("time", "temp3", legend_label="気温", line_color="darkorange", source=source) fig.line("time", "humidity", legend_label="湿度", line_color="blue", source=source) fig.extra_y_ranges["pressure"] = bm.Range1d( min(990, df["pressure"].min()), max(1020, df["pressure"].max())) fig.add_layout( bm.LinearAxis(y_range_name="pressure", axis_label="気圧[hPa]"), "right") fig.line("time", "pressure", legend_label="気圧", line_color="deeppink", y_range_name="pressure", source=source) fig.legend.click_policy = "hide" fig.legend.location = "top_left" bp.save(fig)