def MonthGraph(request): if request.method =='GET' : if request.user.is_authenticated(): now = datetime.now() now = formats.date_format(now,"SHORT_DATETIME_FORMAT") #data = DeviceUsage.objects.values('date').annotate(sumusage=Sum('usage'))# Group by date y = [0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0] print "Don't select data" head="" plot = Bar(y,title=head, xlabel='date', ylabel='Unit', width=800, height=400) plot.toolbar_location = None plot.outline_line_color = None script, div = components(plot, CDN) return render(request, "monthgraph.html", {"the_script": script, "the_div": div,'devices': DeviceProfile.objects.filter(owner=request.user),"date":str(now)}) else: return redirect('/authen/') elif request.method == 'POST': now = datetime.now() now = formats.date_format(now,"SHORT_DATETIME_FORMAT") #data = DeviceUsage.objects.values('date').annotate(sumusage=Sum('usage'))# Group by date y = [0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0] head="" if request.POST.get('deviceid') is not None: id = request.POST.get('deviceid') if request.POST.get('month') is not None: month = request.POST.get('month') do = DeviceProfile.objects.get(device_id=id) data = DeviceUsage.objects.filter(device_id=do,date__month=month).values('date').annotate(sumusage=Sum('usage'),sumtime=Sum('time')) #print data # data[0].get('date').strftime("%d") get only date from year-month-date for d in data: hr = (float(d.get('sumtime'))/3600) kw = float(d.get('sumusage')) y[int(d.get('date').strftime("%d"))] = kw * hr head = "usage of device name: "+ do.device_name +" at "+request.POST.get('month')+"th Month" else: print "Don't select data" head="" plot = Bar(y,title=head, xlabel='date', ylabel='Unit', width=800, height=400) plot.toolbar_location = None plot.outline_line_color = None script, div = components(plot, CDN) return render(request, "monthgraph.html", {"the_script": script, "the_div": div,'devices': DeviceProfile.objects.filter(owner=request.user),"date":str(now)})
def generate_chart(year): data = get_data(year) barchart = Bar(data, values=blend(*SCORED_EVENTS, labels_name='event'), label=cat(columns='Team', sort=False), stack=cat(columns='event', sort=False), color=color(columns='event', palette=Spectral9, sort=False), xgrid=False, ygrid=False, legend='top_right', plot_width=1000, plot_height=625, tools="pan,wheel_zoom,box_zoom,reset,resize") barchart.title = "Formula SAE Michigan " + str(year) + " Total Scores by Place" barchart._xaxis.axis_label = "Teams" barchart._xaxis.axis_line_color = None barchart._xaxis.major_tick_line_color = None barchart._xaxis.minor_tick_line_color = None barchart._xaxis.major_label_text_font_size = '0.6em' barchart._yaxis.axis_label = "Total Score" barchart._yaxis.axis_line_color = None barchart._yaxis.major_tick_line_color = None barchart._yaxis.minor_tick_line_color = None barchart.outline_line_color = None barchart.toolbar_location = 'right' barchart.logo = None return barchart
def systemEvolutionBarPlot(df, yLabel, values): with Timer(key='systemEvolutionBarPlot', verbose=True): p = Bar(df, label='snapshot', values=values, agg='sum', stack='software', legend='bottom_left', bar_width=0.5, xlabel="Snapshots", ylabel=yLabel, responsive=True, height=200,tools='hover') glyph_renderers = p.select(GlyphRenderer) bar_source = [glyph_renderers[i].data_source for i in range(len(glyph_renderers))] hover = p.select(HoverTool) hover.tooltips = [ ('software',' @software'), ('value', '@height'), ] p.xaxis.formatter=FuncTickFormatter.from_py_func(getWeekStringTick) p.axis.minor_tick_line_color = None p.background_fill_color = "#fafafa" p.legend.location = "top_left" p.toolbar.logo = None p.toolbar_location = None legend=p.legend[0].legends p.legend[0].legends=[] l = Legend( location=(0, -30)) l.items=legend p.add_layout(l, 'right') return p
def plotbar(events, userid): """ Arch: I tried to install bokeh 0.10.0 ver and that was giving a lot of errors in the Bar(method args)- StopIteration and also while trying to load the latest bokeh.min.js from CDN So I am going back to the bokeh 0.8.1 ver- at least it works """ xr = [x.title for x in events] y1values = [y1.prefearfactor for y1 in events] y2values = [y2.postfearfactor for y2 in events] yr = OrderedDict(PredictedFear=y1values, ActualFear=y2values) """ height=400 width=600 bar = figure(plot_width=width, plot_height=height, title="Imagined vs real") # use the `rect` renderer to display stacked bars of the medal results. Note # that we set y_range explicitly on the first renderer bar.rect(xr, y1values, height=0.5, width=0.4, color="red", alpha=0.6) bar.rect(xr, y2values, height=0.5, width=0.4, color="blue", alpha=0.6) """ bar = Bar(yr, xr, xlabel="Events", ylabel="Fear factor", title="Imagined vs real fear", width=800, height=600, legend=True, palette=['#c1ff55', '#b7feea'], stacked=True, tools=None, xgrid=None, ygrid=None) bar.toolbar_location = None #output_file("stacked_bar.html", autosave=True) #show(bar) - commented out so it doesnt try to open display in linux #generating unique filenames by- concatenating userid to a rolledindex- #we dont expect a user to generate more than """ global rollingindex userfilepath = filepath+str(userid)+str(rollingindex.next())+'.js' """ userfilepath = filepath + str(userid) + '.js' js, tag = autoload_static( bar, CDN, '/' + userfilepath + '?id=' + str(randint(1, 1000000))) with open(userfilepath, 'w') as f: f.write(js) return js, tag
def generate_chart(event): data = get_data(TIMED_EVENTS[event]) # Bokeh doesn't let me control the order of the grouping! This is # frustrating since it will be different on every server launch barchart = Bar(data, values='percentage_dnf', label='year', color="FireBrick", xgrid=False, ygrid=False, plot_width=800, plot_height=500, tools="pan,wheel_zoom,box_zoom,reset,resize") barchart.title = "Formula SAE Michigan DNFs - " + event barchart._xaxis.axis_label = "Year" barchart._xaxis.axis_line_color = None barchart._xaxis.major_tick_line_color = None barchart._xaxis.minor_tick_line_color = None barchart._yaxis.axis_label = "Percentage DNF" barchart._yaxis.axis_line_color = None barchart._yaxis.major_tick_line_color = None barchart._yaxis.minor_tick_line_color = None barchart._yaxis.formatter = NumeralTickFormatter(format="0%") barchart.y_range = Range1d(0, 1) barchart.outline_line_color = None barchart.toolbar_location = 'right' barchart.logo = None for renderer in barchart.select(GlyphRenderer): if renderer.data_source.data['height'] != [0]: year = renderer.data_source.data['year'] num_dnf = data['dnfs'].loc[data['year'] == year] num_entries = data['entries'].loc[data['year'] == year] percent_dnf = data['percentage_dnf'].loc[data['year'] == year] hover = HoverTool(renderers=[renderer], tooltips=[ ("# DNFs", '%d' % num_dnf.values[0]), ("# Entries", '%d' % num_entries.values[0]), ("% DNF", '%.2f%%' % (100 * percent_dnf.values[0])) ]) barchart.add_tools(hover) return barchart
def generate_chart(team): data = generate_data(team) selectable_years = list(map(str, data.index.unique())) # Generate the chart UNFORTUNATELY using the high level plots. Streaming # a bunch of quads resulted in lots of graphics corruption when switching # teams. I would rather have the plots work all the time but really slow # then have the plot show the wrong information. barchart = Bar(data, values=blend(*SCORED_EVENTS, labels_name='event'), label=cat(columns='Year', sort=False), stack=cat(columns='event', sort=False), color=color(columns='event', palette=Spectral9, sort=False), xgrid=False, ygrid=False, plot_width=1000, plot_height=625, tools="pan,wheel_zoom,box_zoom,reset,resize") barchart.title = "Formula SAE Michigan - " + team barchart._xaxis.axis_label = "Teams" barchart._xaxis.axis_line_color = None barchart._xaxis.major_tick_line_color = None barchart._xaxis.minor_tick_line_color = None barchart._yaxis.axis_label = "Total Score" barchart._yaxis.axis_line_color = None barchart._yaxis.major_tick_line_color = None barchart._yaxis.minor_tick_line_color = None barchart.outline_line_color = None barchart.toolbar_location = 'right' barchart.logo = None # Hacky tooltips for renderer in barchart.select(GlyphRenderer): if renderer.data_source.data['height'] != [0]: year = renderer.data_source.data['Year'] place = data['Place'].loc[data['Year'] == year] score = data['Total Score'].loc[data['Year'] == year] hover = HoverTool(renderers=[renderer], tooltips=[("Year", '@Year'), ("Selection", '@event'), ("Event Score", '@height'), ("Total Score", '%.2f' % score.values[0]), ("Overall Place", '%d' % place.values[0])]) barchart.add_tools(hover) return barchart
def output_chart(issues_df, output_mode='static'): import datetime import bokeh from bokeh.models import HoverTool # Add timestamp to title issues_chart = Bar(issues_df, label='value_delivered', values='status', agg='count', stack='status', title=ISSUES_TITLE + " (Updated " + datetime.datetime.now().strftime('%m/%d/%Y') + ")", xlabel="Value Delivered", ylabel="Number of Use Cases", legend='top_right', tools='hover', color=brewer["GnBu"][3]) issues_chart.plot_width = DESTINATION_FRAME_WIDTH - (HTML_BODY_MARGIN * 2) issues_chart.plot_height = DESTINATION_FRAME_HEIGHT - (HTML_BODY_MARGIN * 2) issues_chart.logo = None issues_chart.toolbar_location = None hover = issues_chart.select(dict(type=HoverTool)) hover.tooltips = [("Value Delivered", "$x")] #--- Configure output --- reset_output() if output_mode == 'static': # Static file. CDN is most space efficient output_file(ISSUES_FILE, title=ISSUES_TITLE, autosave=False, mode='cdn', root_dir=None) # Generate file save(issues_chart, filename=ISSUES_FILE) elif output_mode == 'notebook': output_notebook() # Show inline show(issues_chart) else: # Server (using internal server IP, rather than localhost or external) session = bokeh.session.Session(root_url=BOKEH_SERVER_IP, load_from_config=False) output_server("ddod_chart", session=session) show(issues_chart)
def generate_chart(event): data = get_data(TIMED_EVENTS[event]) # Bokeh doesn't let me control the order of the grouping! This is # frustrating since it will be different on every server launch barchart = Bar(data, values='percentage_dnf', label='year', color="FireBrick", xgrid=False, ygrid=False, plot_width=800, plot_height=500, tools="pan,wheel_zoom,box_zoom,reset,resize") barchart.title = "Formula SAE Michigan DNFs - " + event barchart._xaxis.axis_label = "Year" barchart._xaxis.axis_line_color = None barchart._xaxis.major_tick_line_color = None barchart._xaxis.minor_tick_line_color = None barchart._yaxis.axis_label = "Percentage DNF" barchart._yaxis.axis_line_color = None barchart._yaxis.major_tick_line_color = None barchart._yaxis.minor_tick_line_color = None barchart._yaxis.formatter = NumeralTickFormatter(format="0%") barchart.y_range = Range1d(0, 1) barchart.outline_line_color = None barchart.toolbar_location = 'right' barchart.logo = None for renderer in barchart.select(GlyphRenderer): if renderer.data_source.data['height'] != [0]: year = renderer.data_source.data['year'] num_dnf = data['dnfs'].loc[data['year'] == year] num_entries = data['entries'].loc[data['year'] == year] percent_dnf = data['percentage_dnf'].loc[data['year'] == year] hover = HoverTool(renderers=[renderer], tooltips=[("# DNFs", '%d' % num_dnf.values[0]), ("# Entries", '%d' % num_entries.values[0]), ("% DNF", '%.2f%%' % (100 * percent_dnf.values[0]))]) barchart.add_tools(hover) return barchart
def output_chart(issues_df,output_mode='static'): import datetime import bokeh from bokeh.models import HoverTool # Add timestamp to title issues_chart = Bar(issues_df, label='value_delivered', values='status', agg='count', stack='status', title=ISSUES_TITLE+" (Updated "+datetime.datetime.now().strftime('%m/%d/%Y')+")", xlabel="Value Delivered",ylabel="Number of Use Cases", legend='top_right', tools='hover', color=brewer["GnBu"][3] ) issues_chart.plot_width = DESTINATION_FRAME_WIDTH - (HTML_BODY_MARGIN * 2) issues_chart.plot_height = DESTINATION_FRAME_HEIGHT - (HTML_BODY_MARGIN * 2) issues_chart.logo = None issues_chart.toolbar_location = None hover = issues_chart.select(dict(type=HoverTool)) hover.tooltips = [ ("Value Delivered", "$x")] #--- Configure output --- reset_output() if output_mode == 'static': # Static file. CDN is most space efficient output_file(ISSUES_FILE, title=ISSUES_TITLE, autosave=False, mode='cdn', root_dir=None ) # Generate file save(issues_chart,filename=ISSUES_FILE) elif output_mode == 'notebook': output_notebook() # Show inline show(issues_chart) else: # Server (using internal server IP, rather than localhost or external) session = bokeh.session.Session(root_url = BOKEH_SERVER_IP, load_from_config=False) output_server("ddod_chart", session=session) show(issues_chart)
def generate_chart(year): data = get_data(year) plot_data = { 'country': data.index.tolist(), 'count': [float(i) for i in data.values.tolist()] } barchart = Bar(plot_data, label='country', values='count', color="red", xgrid=False, ygrid=False, plot_width=800, plot_height=500, tools="pan,wheel_zoom,box_zoom,reset,resize") barchart.title = "Formula SAE Michigan " + year + " Countries" barchart.x_range = FactorRange(factors=data.index.tolist()) barchart._xaxis.axis_label = "Country" barchart._xaxis.axis_line_color = None barchart._xaxis.major_tick_line_color = None barchart._xaxis.minor_tick_line_color = None barchart._yaxis.axis_label = "Number of Teams" barchart._yaxis.axis_line_color = None barchart._yaxis.major_tick_line_color = None barchart._yaxis.minor_tick_line_color = None barchart.outline_line_color = None barchart.toolbar_location = 'right' barchart.logo = None hover = HoverTool(tooltips=[("Country", '@x'), ("# Teams", '@height')]) barchart.add_tools(hover) return barchart
def generate_chart(): data = get_data() # Bokeh doesn't let me control the order of the grouping! This is # frustrating since it will be different on every server launch barchart = Bar(data, label='year', values='count', group=cat(columns='size', ascending=True, sort=True), color=color(columns='size', palette=Spectral4), legend='top_left', xgrid=False, ygrid=False, plot_width=800, plot_height=500, tools="pan,wheel_zoom,box_zoom,reset,resize") barchart.title = "Formula SAE Michigan Engine Cylinders" barchart._xaxis.axis_label = "Year" barchart._xaxis.axis_line_color = None barchart._xaxis.major_tick_line_color = None barchart._xaxis.minor_tick_line_color = None barchart._yaxis.axis_label = "Frequency" barchart._yaxis.axis_line_color = None barchart._yaxis.major_tick_line_color = None barchart._yaxis.minor_tick_line_color = None barchart.outline_line_color = None barchart.toolbar_location = 'right' barchart.logo = None hover = HoverTool(tooltips=[("Engine", '@size'), ("# Teams", '@height')]) barchart.add_tools(hover) return barchart
def generate_chart(year): data = get_data(year) plot_data = {'country': data.index.tolist(), 'count': [float(i) for i in data.values.tolist()]} barchart = Bar(plot_data, label='country', values='count', color="red", xgrid=False, ygrid=False, plot_width=800, plot_height=500, tools="pan,wheel_zoom,box_zoom,reset,resize") barchart.title = "Formula SAE Michigan " + year + " Countries" barchart.x_range = FactorRange(factors=data.index.tolist()) barchart._xaxis.axis_label = "Country" barchart._xaxis.axis_line_color = None barchart._xaxis.major_tick_line_color = None barchart._xaxis.minor_tick_line_color = None barchart._yaxis.axis_label = "Number of Teams" barchart._yaxis.axis_line_color = None barchart._yaxis.major_tick_line_color = None barchart._yaxis.minor_tick_line_color = None barchart.outline_line_color = None barchart.toolbar_location = 'right' barchart.logo = None hover = HoverTool(tooltips=[("Country", '@x'), ("# Teams", '@height')]) barchart.add_tools(hover) return barchart
def generate_chart(year): data = get_data(year) barchart = Bar(data, values=blend(*SCORED_EVENTS, labels_name='event'), label=cat(columns='Team', sort=False), stack=cat(columns='event', sort=False), color=color(columns='event', palette=Spectral9, sort=False), xgrid=False, ygrid=False, legend='top_right', plot_width=1000, plot_height=625, tools="pan,wheel_zoom,box_zoom,reset,resize") barchart.title = "Formula SAE Michigan " + str( year) + " Total Scores by Place" barchart._xaxis.axis_label = "Teams" barchart._xaxis.axis_line_color = None barchart._xaxis.major_tick_line_color = None barchart._xaxis.minor_tick_line_color = None barchart._xaxis.major_label_text_font_size = '0.6em' barchart._yaxis.axis_label = "Total Score" barchart._yaxis.axis_line_color = None barchart._yaxis.major_tick_line_color = None barchart._yaxis.minor_tick_line_color = None barchart.outline_line_color = None barchart.toolbar_location = 'right' barchart.logo = None return barchart
def MonthGraph(request): if request.method == 'GET': if request.user.is_authenticated(): now = datetime.now() now = formats.date_format(now, "SHORT_DATETIME_FORMAT") #data = DeviceUsage.objects.values('date').annotate(sumusage=Sum('usage'))# Group by date y = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] print "Don't select data" head = "" plot = Bar(y, title=head, xlabel='date', ylabel='Unit', width=800, height=400) plot.toolbar_location = None plot.outline_line_color = None script, div = components(plot, CDN) return render( request, "monthgraph.html", { "the_script": script, "the_div": div, 'devices': DeviceProfile.objects.filter(owner=request.user), "date": str(now) }) else: return redirect('/authen/') elif request.method == 'POST': now = datetime.now() now = formats.date_format(now, "SHORT_DATETIME_FORMAT") #data = DeviceUsage.objects.values('date').annotate(sumusage=Sum('usage'))# Group by date y = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] head = "" if request.POST.get('deviceid') is not None: id = request.POST.get('deviceid') if request.POST.get('month') is not None: month = request.POST.get('month') do = DeviceProfile.objects.get(device_id=id) data = DeviceUsage.objects.filter( device_id=do, date__month=month).values('date').annotate( sumusage=Sum('usage'), sumtime=Sum('time')) #print data # data[0].get('date').strftime("%d") get only date from year-month-date for d in data: hr = (float(d.get('sumtime')) / 3600) kw = float(d.get('sumusage')) y[int(d.get('date').strftime("%d"))] = kw * hr head = "usage of device name: " + do.device_name + " at " + request.POST.get( 'month') + "th Month" else: print "Don't select data" head = "" plot = Bar(y, title=head, xlabel='date', ylabel='Unit', width=800, height=400) plot.toolbar_location = None plot.outline_line_color = None script, div = components(plot, CDN) return render( request, "monthgraph.html", { "the_script": script, "the_div": div, 'devices': DeviceProfile.objects.filter(owner=request.user), "date": str(now) })
def portalDynamicity(df): def getWeekString(yearweek): if yearweek is None or len(str(yearweek)) == 0: return '' year = "'" + str(yearweek)[:2] week = int(str(yearweek)[2:]) # d = d - timedelta(d.weekday()) # dd=(week)*7 # dlt = timedelta(days = dd) # first= d + dlt # dlt = timedelta(days = (week)*7) # last= d + dlt + timedelta(days=6) return 'W' + str(week) + '-' + str(year) bp = figure(plot_width=600, plot_height=300, y_axis_type="datetime", responsive=True, tools='') # ,toolbar_location=None bp.toolbar.logo = None bp.toolbar_location = None label_dict={} for i, s in enumerate(df['snapshot']): label_dict[i] = getWeekString1(s) bp.yaxis[0].formatter = NumeralTickFormatter(format="0.0%") bp.xaxis[0].axis_label = 'Snapshots' bp.yaxis[0].axis_label = '% of portals' li = bp.line(df.index.values.tolist(), df['dyratio'], line_width=2, line_color='red', legend="dyratio") c = bp.circle(df.index.values.tolist(), df['dyratio'], line_width=2, line_color='red', legend="dyratio") li1 = bp.line(df.index.values.tolist(), df['adddelratio'], line_width=2, line_color='blue', legend="adddelratio") c = bp.circle(df.index.values.tolist(), df['adddelratio'], line_width=2, line_color='blue', legend="adddelratio") legend = bp.legend[0].legends bp.legend[0].legends = [] l = Legend(location=(0, -30)) l.items = legend bp.add_layout(l, 'right') labels=["staticRatio","updatedRatio","addRatio","delRatio"] #for l in labels: # df[l]= df[l]*100 print brewer.keys() colors = brewer["Pastel2"][len(labels)] bar = Bar(df, values=blend("staticRatio","updatedRatio","addRatio","delRatio", name='medals', labels_name='medal'), label=cat(columns='snapshot', sort=False), stack=cat(columns='medal', sort=False), color=color(columns='medal', palette=colors, sort=False), legend='top_right', bar_width=0.5, responsive=True, tooltips=[('ratio', '@medal'), ('snapshot', '@snapshot'),('Value of Total',' @height{0.00%}')]) legend = bar.legend[0].legends bar.legend[0].legends = [] l = Legend(location=(0, -30)) l.items = legend bar.add_layout(l, 'right') bar.xaxis[0].axis_label = 'Snapshots' bar.yaxis[0].axis_label = '% of datasets' bar.width=600 bar.height=300 bar.xaxis[0].formatter = FuncTickFormatter.from_py_func(getWeekStringTick) bar.toolbar.logo = None bar.toolbar_location = None bar.yaxis[0].formatter = NumeralTickFormatter(format="0.0%") return {'bar':bar,'lines':bp}
# Make the chart TOOLS = '' plt2 = Bar(portions_df, label="Name", values="Portion", stack = "SaleStatus", legend="bottom_right", color=['Green','Red'], xlabel="Listing feature", ylabel="Portion sold or unsold", title="Impact of listing features on likelihood of sale", tools=TOOLS) plt2.logo = None plt2.toolbar_location = None plt2.y_range = Range1d(start=0,end=1) # Save the plot output_file("./templates/plot3_new.html") # show(plt2) ####################################################### # Look at dependence of sale outcome on seller feedback score ####################################################### # hist = Histogram(data_sold, values='feedbackScore', color='listingType', # title="Distribution of feedback scores for sold items", legend='top_left') #
def summarize_plans(state, age, npi): todaysdate = str(datetime.now()) age = str(age) if age > '65': age = '65' elif age < '20': age = '20' # filter based on what plans are current (not expired) and age filteredplans = pd.read_hdf( 'webapp/data/plan-rates.h5', state, where=[ '(Age==age) & (RateExpirationDate > todaysdate) & (RateEffectiveDate < todaysdate)' ], columns=[ 'IndividualRate', 'MetalLevel', 'Age', 'URLForSummaryofBenefitsCoverage', 'PlanMarketingName' ]) stateave = filteredplans.IndividualRate.mean() myave = stateave # plot it #filteredplans.groupby('MetalLevel').IndividualRate.mean().plot(kind='bar') #statebardf = filteredplans.groupby('MetalLevel').IndividualRate.mean() statebardf = filteredplans.groupby('MetalLevel', as_index=False).mean() #p = mpl.to_bokeh() if npi == '': p = Bar(filteredplans.groupby('MetalLevel').IndividualRate.mean(), values='IndividualRate', xlabel="", ylabel="Montly Premium ($)") else: print 'input npi is ' + npi # read hdf5 of provider-plan pairings provnplanpd = pd.read_hdf('webapp/data/plan_providers.h5', state) # make a list of planids for the input npi planlst = provnplanpd[provnplanpd.npi == npi].plan_id.values print planlst # read in hdf5 of plan info planinfo = pd.read_hdf( 'webapp/data/plan-rates.h5', state, where=[ '(Age==age) & (RateExpirationDate > todaysdate) & (RateEffectiveDate < todaysdate)' ]) # for each planid, get the first entry and concatenate them together. filteredplans = planinfo[planinfo.PlanId == planlst[0]].groupby( 'PlanId', as_index=False).first() filteredplans.head() #pd.concat([filteredplans, filteredplans]) for ii in range(1, len(planlst)): filteredplans = pd.concat([ filteredplans, planinfo[planinfo.PlanId == planlst[ii]].groupby( 'PlanId', as_index=False).first() ]) provbardf = filteredplans.groupby('MetalLevel', as_index=False).mean() provbardf['average'] = ['provider rates'] * len( provbardf.MetalLevel.values) statebardf['average'] = ['state average'] * len( provbardf.MetalLevel.values) myave = statebardf.IndividualRate.mean() #provbardf['average'] = [1, 1] #statebardf['average'] = [2, 2] #plotdf = pd.merge(statebardf, provbardf, on='MetalLevel') plotdf = pd.concat([provbardf, statebardf]) #plotdf['state average'] = statebardf['IndividualRate'] #plotdf['provider rates'] = provbardf['IndividualRate'] print plotdf p = Bar(plotdf, label='MetalLevel', values='IndividualRate', group='average', legend='top_right', xlabel="", ylabel="Montly Premium ($)") p.logo = None p.plot_height = 400 p.toolbar_location = None script, div = components(p) print filteredplans.to_dict(orient='records') return { 'num_plans': len(filteredplans), 'script': script, 'plot_div': div, 'national_comp': format_price_comp(float(age2nationalAverage(age)) - myave), 'state_comp': format_price_comp(float(stateave) - myave), 'plans': render_template('plans.html', plans=filteredplans.sort_values( by='IndividualRate').to_dict(orient='records')) }
def index(): if request.method == 'GET': return render_template('index.html') else: # request was a POST # store the city code # all processing is based on filtering summary tables on city code app.vars['citycode'] = request.form['city'] citycode = app.vars['citycode'] # app.vars['features'] = request.form.getlist('features') ####################### # Summary table loads # ####################### # coll_inj = pd.read_csv('1_coll_inj_ann.csv', encoding='utf-8') # top_10 = pd.read_csv('2_top_10.csv', encoding='utf-8') # surf_vs_high = pd.read_csv('3_surf_vs_high.csv', encoding='utf-8') # inj_causes = pd.read_csv('4_inj_causes.csv', encoding='utf-8') # weather = pd.read_csv('5_weather.csv', encoding='utf-8') # hour = pd.read_csv('6_hour.csv', encoding='utf-8') # city_stats14 = pd.read_csv('7_2014_city_stats.csv', encoding='utf-8') # ins_est13 = pd.read_csv('8_2013_ins_est.csv', encoding='utf-8') # kfinal = pd.read_csv('9_kmeans_and_pop.csv', encoding='utf-8') ################################ # Hardcoded summary statistics # ################################ # California collision stats capop2014 = 38357121 cacol2014 = 384646 caco1k = np.round(1000.0 * cacol2014 / capop2014, 1) cainj2014 = 225044 cainj1k = np.round(1000.0 * cainj2014 / capop2014, 1) cakill2014 = 3068 cak1m = np.round(1000000.0 * cakill2014 / capop2014, 1) # Califonia insurance stats human_claims_2013 = 4207773612 prop_claims_2013 = 7422643259 inj_2013 = 223128 sev_inj_2013 = 10664 kill_2013 = 3104 parties_2013 = 763086 # cost per X insurance estimates # crude! cost_per_inj = np.round(human_claims_2013 / \ (1.0 * inj_2013 + 9.0 * sev_inj_2013 + 19.0 * kill_2013), 1) cost_per_party = np.round(prop_claims_2013 / (1.0 * parties_2013), 1) cacpc = np.round(1.0 * (human_claims_2013 + prop_claims_2013) / 37965755, 2) # City collision stats citystats = city_stats14[city_stats14['cccode'] == int(citycode)] cityname = citystats['COUNTY/CITY'].values[0] collper1k = np.round(citystats['collper1k'].values[0],1) injper1k = np.round(citystats['injper1k'].values[0],1) killper1m = np.round(citystats['killper1m'].values[0],1) # City insurance Stats insstats = ins_est13[ins_est13['cccode'] == int(citycode)] citycpc = np.round(insstats['estpercapita'].values[0], 2) tools='save' # Other City Stats citypop = kfinal[kfinal['cccode'] == int(citycode)]['pop2014'].values[0] citypop = locale.format("%d", citypop, grouping=True) cluster = kfinal[kfinal['cccode'] == int(citycode)]['cluster'].values[0] ################ # Annual Trend # ################ df = coll_inj[coll_inj['cccode'] == int(citycode)] print 'before bar definition' p1 = Bar(df, label='year', values='count', title="Collisions & Injuries by Year", group='type', xlabel='Year', ylabel='Collisions/Injuries', height=400, # width=600, # bar_width=0.1, tools=tools, legend='bottom_left' ) p1.toolbar_location=None p1.responsive=True print 'end p1 definition' ############################## # Top 10 Worst Intersections # ############################## columns = ['inter', 'numcoll', 'SUM(NUMBER_INJURED)', 'SUM(NUMBER_KILLED)'] temp = top_10[top_10['CNTY_CITY_LOC'] == int(citycode)][columns] newcolumns = ['Intersection', 'Collisions', 'Injuries', 'Fatalities'] temp.columns = newcolumns interhtml = temp.to_html(index=False).replace('border="1"', 'border="0"') interhtml = interhtml.replace('\n', '') ############################### # Surface Streets vs Highways # ############################### df3 = surf_vs_high[surf_vs_high['cccode'] == int(citycode)] df3 = df3[['type', 'count', 'dtype']] p2 = Bar(df3, label='dtype', values='count', stack='type', title="Surface Streets vs Freeways and Highways", xlabel='', ylabel='Proportion', height=400, # width=600, bar_width=0.9, tools=tools, legend='bottom_right' ) p2.toolbar_location=None p2.responsive=True print 'end p2 definition' ############################################## # Primary Causes of Collisions with Injuries # ############################################## df4 = inj_causes[inj_causes['CNTY_CITY_LOC'] == int(citycode)] p3 = Bar(df4, label='pcf', values='SUM(NUMBER_INJURED)', title="Primary Violations Leading to Injuries", xlabel='', ylabel='Injuries', height=400, # width=600, bar_width=0.9, tools=tools, color='black' ) p3.toolbar_location=None p3.responsive=True print 'end p3 definition' ################################ # Weather at time of collision # ################################ df5 = weather[weather['CNTY_CITY_LOC'] == int(citycode)] p4 = Bar(df5, label='weather', values='numcoll', title="Weather Conditions for Collisions", xlabel='', ylabel='Collisions', height=400, # width=600, bar_width=0.9, tools=tools, color='yellow' ) p4.toolbar_location=None p4.responsive=True print 'end p4 definition' ############### # Time of Day # ############### df6 = hour[hour['CNTY_CITY_LOC'] == int(citycode)] p5 = Bar(df6, label='hour', values='numcoll', title="Time of Day", xlabel='Hour', ylabel='Collisions', height=400, # width=600, bar_width=0.9, tools=tools, color='black' ) p5.toolbar_location=None p5.responsive=True ################# # Plot encoding # ################# plots = {'trend': p1, 'streettypes': p2, 'injurycauses': p3, 'weather': p4, 'hour': p5} script, div = components(plots, INLINE) # script1 = encode_utf8(script1) ############# # Rendering # ############# print 'before render' html = render_template('index_post.html', citycode=citycode, cityname=cityname, collper1k=collper1k, caco1k=caco1k, injper1k=injper1k, cainj1k=cainj1k, killper1m=killper1m, cak1m=cak1m, citycpc=citycpc, cacpc=cacpc, plot_scripts=script, div=div, interhtml=interhtml, citypop=citypop, cluster=cluster) print 'after render' return encode_utf8(html)
columns=["Name", "Portion", "HasFeature", "SaleStatus", "Feature"]) # Make the chart TOOLS = '' plt2 = Bar(portions_df, label="Name", values="Portion", stack="SaleStatus", legend="bottom_right", color=['Green', 'Red'], xlabel="Listing feature", ylabel="Portion sold or unsold", title="Impact of listing features on likelihood of sale", tools=TOOLS) plt2.logo = None plt2.toolbar_location = None plt2.y_range = Range1d(start=0, end=1) # Save the plot output_file("./templates/plot3_new.html") # show(plt2) ####################################################### # Look at dependence of sale outcome on seller feedback score ####################################################### # hist = Histogram(data_sold, values='feedbackScore', color='listingType', # title="Distribution of feedback scores for sold items", legend='top_left') # # # Save the plot # output_file("./templates/data_exploration.html")
def summarize_plans(state, age, npi): todaysdate = str(datetime.now()) age = str(age) if age > '65': age = '65' elif age < '20': age = '20' # filter based on what plans are current (not expired) and age filteredplans = pd.read_hdf('webapp/data/plan-rates.h5', state, where=['(Age==age) & (RateExpirationDate > todaysdate) & (RateEffectiveDate < todaysdate)'], columns = ['IndividualRate', 'MetalLevel', 'Age','URLForSummaryofBenefitsCoverage','PlanMarketingName']) stateave = filteredplans.IndividualRate.mean() myave = stateave # plot it #filteredplans.groupby('MetalLevel').IndividualRate.mean().plot(kind='bar') #statebardf = filteredplans.groupby('MetalLevel').IndividualRate.mean() statebardf = filteredplans.groupby('MetalLevel', as_index=False).mean() #p = mpl.to_bokeh() if npi == '': p = Bar(filteredplans.groupby('MetalLevel').IndividualRate.mean(), values='IndividualRate', xlabel="", ylabel="Montly Premium ($)") else: print 'input npi is ' + npi # read hdf5 of provider-plan pairings provnplanpd = pd.read_hdf('webapp/data/plan_providers.h5', state) # make a list of planids for the input npi planlst = provnplanpd[provnplanpd.npi == npi].plan_id.values print planlst # read in hdf5 of plan info planinfo = pd.read_hdf('webapp/data/plan-rates.h5', state, where=['(Age==age) & (RateExpirationDate > todaysdate) & (RateEffectiveDate < todaysdate)']) # for each planid, get the first entry and concatenate them together. filteredplans = planinfo[planinfo.PlanId==planlst[0]].groupby('PlanId', as_index=False).first() filteredplans.head() #pd.concat([filteredplans, filteredplans]) for ii in range(1, len(planlst)): filteredplans = pd.concat([filteredplans, planinfo[planinfo.PlanId==planlst[ii]].groupby('PlanId', as_index=False).first()]) provbardf = filteredplans.groupby('MetalLevel', as_index=False).mean() provbardf['average'] = ['provider rates']*len(provbardf.MetalLevel.values) statebardf['average'] = ['state average']*len(provbardf.MetalLevel.values) myave = statebardf.IndividualRate.mean() #provbardf['average'] = [1, 1] #statebardf['average'] = [2, 2] #plotdf = pd.merge(statebardf, provbardf, on='MetalLevel') plotdf = pd.concat([provbardf, statebardf]) #plotdf['state average'] = statebardf['IndividualRate'] #plotdf['provider rates'] = provbardf['IndividualRate'] print plotdf p = Bar(plotdf, label='MetalLevel', values='IndividualRate', group='average', legend='top_right', xlabel="", ylabel="Montly Premium ($)") p.logo = None p.plot_height=400 p.toolbar_location = None script,div = components(p) print filteredplans.to_dict(orient='records') return {'num_plans':len(filteredplans), 'script': script, 'plot_div': div, 'national_comp': format_price_comp(float(age2nationalAverage(age))-myave), 'state_comp': format_price_comp(float(stateave)-myave), 'plans': render_template('plans.html', plans=filteredplans.sort_values(by='IndividualRate').to_dict(orient='records'))}