def test_get_screenshot_as_png_with_glyph(): layout = Plot(x_range=Range1d(0, 1), y_range=Range1d(0, 1), plot_height=20, plot_width=20, toolbar_location=None, outline_line_color=None, background_fill_color=None, min_border=2, border_fill_color="blue", border_fill_alpha=1) glyph = Rect(x="x", y="y", width=2, height=2, fill_color="red", line_color="red") source = ColumnDataSource(data=dict(x=[0.5], y=[0.5])) layout.add_glyph(source, glyph) png = bie.get_screenshot_as_png(layout) assert png.size == (20, 20) # count 256 red pixels in center area (400 - 20*4 - 16*4) data = png.tobytes() count = 0 for x in range(400): if data[x * 4:x * 4 + 4] == b"\xff\x00\x00\xff": count += 1 assert count == 256 assert len(data) == 1600
def update_plot(self): today = Timestamp.today().strftime('%Y-%m-%d') today_date = to_datetime(today) df = self.counts_data weights = self.similarities_data.xs(self.school, level='school_name', axis=1) df_all = df[df['school_name'].eq(self.school)] present_academic_year = df['academic_year'].unique()[0] tooltips = ''' <div style="font-size: 12px;"> <span style="font-weight: bold;">@school_name</span> (@school_type, @school_age years old)<br> Grade @grade enrollment, as of @reference_date_string<br> <br> <span style="font-weight: bold;">Attrition</span>: @exit_count_present_cumm (@exit_percent_present_cumm{'0.0%'} change from October 1)<br> <span style="font-weight: bold;">Backfill</span>: @enter_count_present_cumm (@enter_percent_present_cumm{'0.0%'} change from October 1)<br> <span style="font-weight: bold;">Current total enrollment</span>: @total_enrollment </div> ''' ticker_ref = DataFrame({'month': date_range('2014-10-01', '2015-06-01', freq='MS')}) ticker_ref['marker'] = ticker_ref['month'] ticker_ref['month'] = ticker_ref['month'].apply(lambda x: x.strftime('%B')) ticker_ref = ticker_ref.set_index('marker')['month'].to_dict() x_range1d = Range1d(-0.05, 290.0) y_index = max( -df_all['exit_percent_present_cumm'].min(), df_all['enter_percent_present_cumm'].max(), -df_all['exit_percent_past_cumm'].min(), df_all['enter_percent_past_cumm'].max() ) y_range1d = Range1d(-y_index - 0.005, y_index + 0.005) plots = [] focus_cols = ['enter_percent_present_cumm', 'exit_percent_present_cumm'] unique_grades = sorted(df_all['grade'].unique().astype('int')) for grade in unique_grades: grade_weight = weights[grade] benchmark_df = DataFrame(columns=focus_cols) df_copy = df[df['academic_year'].eq(present_academic_year)].copy().set_index(['school_name', 'grade']) for rid, label in sorted(ticker_ref.items(), reverse=True): df_copy = df_copy[df_copy['reference_date'].le(rid)] df_ave = df_copy.groupby(level=['school_name', 'grade'])[focus_cols].tail(1).\ mul(grade_weight, axis=0).sum() / grade_weight.sum() df_ave.name = label benchmark_df = benchmark_df.append(df_ave) benchmark_df = benchmark_df.reset_index() benchmark_df['reference_date'] = benchmark_df['index'].map({y: x for x, y in ticker_ref.items()}) benchmark_df['reference_id'] = benchmark_df['reference_date'].apply(lambda x: x.toordinal()) - \ df_all['reference_date'].min().toordinal() source_df = df_all[df_all['grade'].eq(grade)] source_df_rev = source_df.sort_values('reference_id', ascending=False) source_df_trunc = source_df.loc[source_df['reference_date'].le(today_date), :] source = ColumnDataSource(source_df) source_trunc = ColumnDataSource(source_df_trunc) patch_source = ColumnDataSource(dict( x_past=source_df['reference_id'].tolist() + source_df_rev['reference_id'].tolist(), y_past=source_df['enter_percent_past_cumm'].tolist() + source_df_rev['exit_percent_past_cumm'].tolist() )) plot1 = Plot( x_range=x_range1d, y_range=y_range1d, min_border_bottom=5, min_border_top=10, min_border_right=10, plot_width=700, plot_height=150, title=None, title_text_font_size='0pt', title_text_color='grey', outline_line_alpha=0.0) plot1.add_layout( LinearAxis( axis_label='Grade ' + str(grade), axis_label_text_font_size='9pt', minor_tick_line_alpha=0.0, axis_label_text_color='grey', axis_line_alpha=0.1, major_tick_line_alpha=0.1, major_label_text_color='grey', major_label_text_font_size='7pt', formatter=NumeralTickFormatter(format='0%') ), 'left') patch = Patch(x='x_past', y='y_past', fill_color='#AFAFAD', fill_alpha=0.25, line_alpha=0.0) plot1.add_glyph(patch_source, patch) line1 = Line( x='reference_id', y='enter_percent_present_cumm', line_width=2, line_color='#f7910b', line_alpha=1.0) plot1.add_glyph(source_trunc, line1) line2 = Line( x='reference_id', y='exit_percent_present_cumm', line_width=2, line_color='#f7910b', line_alpha=1.0) plot1.add_glyph(source_trunc, line2) line_h = Line(x='reference_id', y=0, line_width=1, line_color='black', line_alpha=0.1) line_renderer = GlyphRenderer(data_source=source, glyph=line_h, name='line') plot1.add_glyph(source, line_h) for ind, series in benchmark_df.iterrows(): x = series['reference_id'] y_enter = series['enter_percent_present_cumm'] y_exit = series['exit_percent_present_cumm'] label = series['index'] line = Segment(x0=x, x1=x, y0=-y_index, y1=y_index, line_width=1, line_color='#165788', line_alpha=0.1) plot1.add_glyph(line) linec1 = Segment( x0=x - 3, x1=x + 3, y0=y_enter, y1=y_enter, line_width=1, line_color='#ed2939', line_alpha=1.0) plot1.add_glyph(linec1) linec2 = Segment( x0=x - 3, x1=x + 3, y0=y_exit, y1=y_exit, line_width=1, line_color='#ed2939', line_alpha=1.0) plot1.add_glyph(linec2) text = Text(x=x+3, y=-y_index, text=[label], text_font_size='8pt', text_color='grey', text_alpha=0.5) plot1.add_glyph(text) hover_tool = HoverTool( plot=plot1, renderers=[line_renderer], tooltips=tooltips, always_active=False, mode='vline', point_policy='follow_mouse', line_policy='prev') crosshair_tool = CrosshairTool(plot=plot1, dimensions=['height']) zoom_tool = BoxZoomTool(plot=plot1, dimensions=['width']) reset_tool = ResetTool(plot=plot1) save_tool = PreviewSaveTool(plot=plot1) pan_tool = PanTool(plot=plot1, dimensions=['width']) help_tool = HelpTool(plot=plot1, help_tooltip='App help page', redirect='http://data.successacademies.org/blog/') plot1.tools.extend([hover_tool, zoom_tool, pan_tool, reset_tool, save_tool, help_tool, crosshair_tool]) plot1.renderers.extend([line_renderer]) plots.append([plot1]) self.plot.children = plots