def print_scores(response_logs: List[pd.DataFrame], opts):
joined = pd.concat(response_logs) # type: pd.DataFrame
binary_score = joined['score'] > 0.5
print()
print('Total number of pages: {:,}, relevant pages: {:,}, '
'average binary score: {:.2f}, average score: {:.2f}'.format(
len(joined), binary_score.sum(), binary_score.mean(),
joined['score'].mean()))
show_domain_stats(joined.copy(), output=opts.output, top=opts.top)
joined.sort_values(by='time', inplace=True)
joined.index = pd.to_datetime(joined.pop('time'), unit='s')
if opts.smooth:
crawl_time = (joined.index[-1] - joined.index[0]).total_seconds()
avg_rps = len(joined) / crawl_time
span = int(opts.smooth * opts.step * avg_rps)
joined['score'] = joined['score'].ewm(span=span).mean()
print_averages({'score': joined['score']}, opts.step, '{:.2f}')
title = 'Page relevancy score'
scores = joined['score'].resample('{}S'.format(opts.step)).mean()
plot = TimeSeries(scores, plot_width=1000,
xlabel='time', ylabel='score', title=title)
plot.set(y_range=Range1d(0, 1))
if not opts.no_show:
save_plot(plot, title=title, suffix='score', output=opts.output)
python类Range1d()的实例源码
def getPlot(title):
p = figure(plot_height=200, plot_width=400, min_border=40, toolbar_location=None, title=title)
p.background_fill_color = "#515052" # Background color
p.title.text_color = "#333138" # Title color
p.title.text_font = "helvetica" # Title font
p.x_range.follow = "end" # Only show most recent window of data
p.x_range.follow_interval = 60 # Moving window size
p.xaxis.major_tick_line_color = None # Turn off x-axis major ticks
p.xaxis.minor_tick_line_color = None # Turn off x-axis minor ticks
p.yaxis.major_tick_line_color = None # Turn off y-axis major ticks
p.yaxis.minor_tick_line_color = None # Turn off y-axis minor ticks
p.xgrid.grid_line_alpha = 0 # Hide X-Axis grid
p.ygrid.grid_line_alpha = 0 # Hide Y-Axis grid
p.xaxis.major_label_text_color = "#333138" # X-Axis color
p.yaxis.major_label_text_color = "#333138" # Y-Axis color
p.extra_y_ranges = {"sentiment": Range1d(start=-1, end=1)}
p.add_layout(LinearAxis(y_range_name="sentiment", major_tick_line_color = None, minor_tick_line_color = None), 'right')
return p
def plotCCI(p, df, plotwidth=800, upcolor='orange', downcolor='yellow'):
# create y axis for rsi
p.extra_y_ranges = {"cci": Range1d(start=min(df['cci'].values),
end=max(df['cci'].values))}
p.add_layout(LinearAxis(y_range_name="cci"), 'right')
candleWidth = (df.iloc[2]['date'].timestamp() -
df.iloc[1]['date'].timestamp()) * plotwidth
# plot green bars
inc = df.cci >= 0
p.vbar(x=df.date[inc],
width=candleWidth,
top=df.cci[inc],
bottom=0,
fill_color=upcolor,
line_color=upcolor,
alpha=0.5,
y_range_name="cci",
legend='cci')
# Plot red bars
dec = df.cci < 0
p.vbar(x=df.date[dec],
width=candleWidth,
top=0,
bottom=df.cci[dec],
fill_color=downcolor,
line_color=downcolor,
alpha=0.5,
y_range_name="cci",
legend='cci')
def plotVolume(p, df, plotwidth=800, upcolor='green',
downcolor='red', colname='volume'):
candleWidth = (df.iloc[2]['date'].timestamp() -
df.iloc[1]['date'].timestamp()) * plotwidth
# create new y axis for volume
p.extra_y_ranges = {colname: Range1d(start=min(df[colname].values),
end=max(df[colname].values))}
p.add_layout(LinearAxis(y_range_name=colname), 'right')
# Plot green candles
inc = df.close > df.open
p.vbar(x=df.date[inc],
width=candleWidth,
top=df[colname][inc],
bottom=0,
alpha=0.1,
fill_color=upcolor,
line_color=upcolor,
y_range_name=colname)
# Plot red candles
dec = df.open > df.close
p.vbar(x=df.date[dec],
width=candleWidth,
top=df[colname][dec],
bottom=0,
alpha=0.1,
fill_color=downcolor,
line_color=downcolor,
y_range_name=colname)
def nb_imshow(image, cmap='jet'):
'''
Interactive equivalent of imshow for ipython notebook
'''
colormap = cm.get_cmap(cmap) # choose any matplotlib colormap here
grayp = [mpl.colors.rgb2hex(m) for m in colormap(np.arange(colormap.N))]
xr = Range1d(start=0, end=image.shape[1])
yr = Range1d(start=image.shape[0], end=0)
p = bpl.figure(x_range=xr, y_range=yr)
# p = bpl.figure(x_range=[0,image.shape[1]], y_range=[0,image.shape[0]])
# p.image(image=[image], x=0, y=0, dw=image.shape[1], dh=image.shape[0], palette=grayp)
p.image(image=[image[::-1, :]], x=0, y=image.shape[0],
dw=image.shape[1], dh=image.shape[0], palette=grayp)
return p
def set_aspect(fig, x, y, aspect=1, margin=0.1):
"""Set the plot ranges to achieve a given aspect ratio.
https://stackoverflow.com/questions/26674779/bokeh-plot-with-equal-axes
Args:
fig (bokeh Figure): The figure object to modify.
x (iterable): The x-coordinates of the displayed data.
y (iterable): The y-coordinates of the displayed data.
aspect (float, optional): The desired aspect ratio. Defaults to 1.
Values larger than 1 mean the plot is squeezed horizontally.
margin (float, optional): The margin to add for glyphs (as a fraction
of the total plot range). Defaults to 0.1
"""
xmin, xmax = min(x), max(x)
ymin, ymax = min(y), max(y)
width = (xmax - xmin) * (1 + 2 * margin)
width = 1.0 if width <= 0 else width
height = (ymax - ymin) * (1 + 2 * margin)
height = 1.0 if height <= 0 else height
r = aspect * (fig.plot_width / fig.plot_height)
if width < r * height:
width = r * height
else:
height = width / r
xcenter = 0.5 * (xmax + xmin)
ycenter = 0.5 * (ymax + ymin)
fig.x_range = Range1d(xcenter - 0.5 * width, xcenter + 0.5 * width)
fig.y_range = Range1d(ycenter - 0.5 * height, ycenter + 0.5 * height)
def plotRSI(p, df, plotwidth=800, upcolor='green',
downcolor='red', yloc='right', limits=(30, 70)):
# create y axis for rsi
p.extra_y_ranges = {"rsi": Range1d(start=0, end=100)}
p.add_layout(LinearAxis(y_range_name="rsi"), yloc)
p.add_layout(Span(location=limits[0],
dimension='width',
line_color=upcolor,
line_dash='dashed',
line_width=2,
y_range_name="rsi"))
p.add_layout(Span(location=limits[1],
dimension='width',
line_color=downcolor,
line_dash='dashed',
line_width=2,
y_range_name="rsi"))
candleWidth = (df.iloc[2]['date'].timestamp() -
df.iloc[1]['date'].timestamp()) * plotwidth
# plot green bars
inc = df.rsi >= 50
p.vbar(x=df.date[inc],
width=candleWidth,
top=df.rsi[inc],
bottom=50,
fill_color=upcolor,
line_color=upcolor,
alpha=0.5,
y_range_name="rsi")
# Plot red bars
dec = df.rsi <= 50
p.vbar(x=df.date[dec],
width=candleWidth,
top=50,
bottom=df.rsi[dec],
fill_color=downcolor,
line_color=downcolor,
alpha=0.5,
y_range_name="rsi")