def update_figure(self, rnd_draws): # regenerate matplotlib plot self.ax1.cla() self.ax1.set_xlabel('r1') self.ax1.set_ylabel('Normalized Distribtuion') self.ax1.set_xlim(0, 1) self.ax1.set_ylim(0, 1.5) self.ax1.grid(True) self.ax1.hist([r[0] for r in rnd_draws], 50, normed=1, facecolor='green', alpha=0.75) self.ax2.cla() self.ax2.set_xlabel('r2') self.ax2.set_ylabel('Normalized Distribtuion') self.ax2.set_xlim(0, 1) self.ax2.set_ylim(0, 1.5) self.ax2.grid(True) self.ax2.hist([r[1] for r in rnd_draws], 50, normed=1, facecolor='blue', alpha=0.75) # send new matplotlib plots to frontend self.emit('mpld3canvas', mpld3.fig_to_dict(self.fig)) META = databench.Meta('mpld3pi', __name__, __doc__, Analysis)
for f in self.flowers] widths = [r-l for l, r in zip(lefts, rights)] self.flowers = [f for f, w in zip(self.flowers, widths) if w < self.max_width] for f in self.flowers: f['trunk'].generate() self.emit('update', self.output()) time.sleep(0.15) """Adjust parameters.""" def on_number_of_flowers(self, flowers): self.number_of_flowers = flowers def on_branch_angle(self, angle_in_degrees): self.config.branch_angle = angle_in_degrees/57.0 def on_init_size(self, size): self.config.init_size = size def on_init_length(self, length): self.config.init_length = length def on_init_color(self, color): self.config.init_color = color META = databench.Meta('flowers', __name__, __doc__, Analysis)
for j in finished_jobs: draws_count += j.result[2] inside_count += j.result[1] self.emit( 'log', { 'result': j.result, 'draws': draws_count, 'inside': inside_count, }) uncertainty = 4.0 * math.sqrt( draws_count * inside_count / draws_count * (1.0 - inside_count / draws_count)) / draws_count self.emit( 'status', { 'pi-estimate': 4.0 * inside_count / draws_count, 'pi-uncertainty': uncertainty, }) jobs.remove(j) time.sleep(0.1) self.emit('log', {'action': 'done'}) def on_disconnect(self): # terminate all workers self.on_workers(0) META = databench.Meta('fastpi', __name__, __doc__, Analysis)
import databench import time import datetime class Analysis(databench.Analysis): def on_connect(self): """Run as soon as a browser connects to this. CHANGEME """ self.emit('log', 'backend is connected and initialized') time.sleep(3) self.emit('ready', 'ready at ' + datetime.datetime.now().isoformat()) def on_got_ready_signal(self, msg): """This is the signal the frontend sends back to the backend once it received the 'ready' signal from the on_connect() function. CHANGEME """ time.sleep(3) self.emit( 'log', 'Backend received the confirmation from the frontend ' 'that the ready signal was received.') META = databench.Meta('scaffold', __name__, __doc__, Analysis)
"""Checks enironment for certatin redis providers and creates a redis client.""" rediscloudenv = os.environ.get('REDISCLOUD_URL') if rediscloudenv: url = urlparse.urlparse(rediscloudenv) r = redis.Redis(host=url.hostname, port=url.port, password=url.password) else: r = redis.StrictRedis() return r class Analysis(databench.Analysis): def __init__(self): self.redis_client = redis_creator() def on_connect(self): """Run as soon as a browser connects to this.""" self.emit('log', {'action': 'backend started'}) def on_stats(self, msg): """Listens for new messages from frontend and pushes to redis channel.""" self.redis_client.publish('someStatsProvider', msg) META = databench.Meta('redispub', __name__, __doc__, Analysis)
import matplotlib.pyplot as plt import databench class Analysis(databench.Analysis): def on_connect(self): """Run as soon as a browser connects to this.""" fig, ax = plt.subplots() points = ax.scatter( np.random.rand(40), np.random.rand(40), s=300, alpha=0.3, ) # use the mpld3 tooltop plugin labels = ["Point {0}".format(i) for i in range(40)] tooltip = mpld3.plugins.PointLabelTooltip(points, labels) mpld3.plugins.connect(fig, tooltip) # send the plot to the frontend self.emit('mpld3canvas', mpld3.fig_to_dict(fig)) # done self.emit('log', {'action': 'done'}) META = databench.Meta('mpld3PointLabel', __name__, __doc__, Analysis)
"""Providing "bag-of-chars" statistics.""" import databench class Analysis(databench.Analysis): def on_connect(self): """Run as soon as a browser connects to this.""" self.emit( 'log', {'backend': 'started and listening for signals from frontend'}) def on_sentence(self, sentence): """Takes a sentence and counts the characters.""" counts = {} for c in sentence.lower(): if ord(c) < ord('a') or \ ord(c) > ord('z'): continue if c not in counts: counts[c] = sentence.count(c) self.emit('log', counts) self.emit('counts', counts) META = databench.Meta('bagofcharsd3', __name__, __doc__, Analysis)
for i in xrange(10000): time.sleep(0.001) # generate points and check whether they are inside the unit circle r1 = random.random() r2 = random.random() if r1*r1 + r2*r2 < 1.0: inside += 1 # every 100 iterations, update status if (i+1) % 100 == 0: draws = i+1 # debug self.emit('log', {'draws': draws, 'inside': inside}) # calculate pi and its uncertainty given the current draws p = float(inside)/draws uncertainty = 4.0*math.sqrt(draws*p*(1.0 - p)) / draws # send status to frontend self.emit('status', { 'pi-estimate': 4.0*inside/draws, 'pi-uncertainty': uncertainty }) self.emit('log', {'action': 'done'}) META = databench.Meta('angular', __name__, __doc__, Analysis)
'draws': draws, 'inside': inside, 'r1': r1, 'r2': r2, }) p = float(inside) / draws uncertainty = 4.0 * math.sqrt(draws * p * (1.0 - p)) / draws self.emit( 'status', { 'pi-estimate': 4.0 * inside / draws, 'pi-uncertainty': uncertainty }) self.emit('log', {'action': 'done'}) def on_samples(self, value): """Sets the number of samples to generate per run.""" self.samples = value def on_test_fn(self, first_param, second_param=100): """Echo params.""" print(first_param, second_param) self.emit('test_fn', { 'first_param': first_param, 'second_param': second_param, }) META = databench.Meta('dummypi', __name__, __doc__, Analysis)
"""Hello World for Databench.""" import databench class Analysis(databench.Analysis): def on_connect(self): """Run as soon as a browser connects to this.""" self.emit('status', {'message': 'Hello World'}) META = databench.Meta('helloworld', __name__, __doc__, Analysis)