def data_retrieval(): conn = lite.connect('/Users/shanekenny/PycharmProjects/WiFinder/app/website/WiFinderDBv02.db') with conn: df = pd.read_sql_query("SELECT W.Log_Count, W.Time, W.Hour, W.Datetime, R.RoomID, R.Capacity, C.ClassID, C.Module, C.Reg_Students, O.Occupancy, O.OccID FROM WIFI_LOGS W JOIN CLASS C ON W.ClassID = C.ClassID JOIN ROOM R ON C.Room = R.RoomID JOIN OCCUPANCY O ON C.ClassID = O.ClassID WHERE R.RoomID = 'B002' AND W.Datetime = '2015-11-12' GROUP BY W.LogID;", conn) df['Time'] = df['Time'].apply(pd.to_datetime) p = figure(width=800, height=250, x_axis_type="datetime", ) p.extra_y_ranges = {"foo": Range1d(start=0, end=1)} p.line(df['Time'], df['Log_Count'], color='red',legend='Log Count') p.line(df['Time'], df['Reg_Students'], color='green',legend='Registered Students') p.line(df['Time'], df['Capacity'], color='blue', legend='Capacity') p.line(df['Time'], df['Occupancy']*100, color='orange', legend='Occupancy') p.add_layout(LinearAxis(y_range_name="foo"), 'left') p2 = figure(width=800, height=250, x_axis_type="datetime", x_range=p.x_range,) p2.line(df['Time'], df['Log_Count'], color='red', legend='Log Count') r= gridplot([[p, p2]], toolbar_location=None) js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(r) return flask.render_template( 'explore.html', script=script, div=div, js_resources=js_resources, css_resources=css_resources,)
def showbusscore(): c0 = request.args['country0'] c1 = request.args['country1'] c2 = request.args['country2'] c3 = request.args['country3'] c4 = request.args['country4'] countries = [c0, c1, c2, c3, c4] i0 = "IC.BUS.EASE.XQ" indicators = [i0] dataframes = create_dataframes(countries,indicators) score = get_mean(dataframes) chartsco = plot_score(score) script,div = components(chartsco) return render_template('showscore.html', js_resources=INLINE.render_js(), css_resources=INLINE.render_css(), script=script, div=div)
def index(request): plot = figure(responsive=True, tools=[], plot_width=500, plot_height=250, x_range=(-180, 180), y_range=(-90, 90)) plot.toolbar_location = None plot.axis.visible = None source = AjaxDataSource(method='GET', data_url='http://localhost:8000/view2d/data/', polling_interval=1000) source.data = dict(x=[], y=[]) # Workaround to initialize the plot img = load_image("static/images/earth.png") plot.image_rgba(image=[img], x=[-180], y=[-90], dw=[360], dh=[180]) plot.cross(source=source, x='x', y='y', size=22, line_width=4, color='Orange') # CLU1 script, div = components(plot, INLINE) js_resources = INLINE.render_js() css_resources = INLINE.render_css() context = { 'bokeh_script' : script, 'bokeh_div' : div, 'js_resources' : js_resources, 'css_resources' : css_resources } return render(request, 'view2d/index.html', context)
def polynomial(): """ Very simple embedding of a polynomial chart """ # Grab the inputs arguments from the URL args = flask.request.args # Get all the form arguments in the url with defaults color = getitem(args, 'color', 'Black') _from = int(getitem(args, '_from', 0)) to = int(getitem(args, 'to', 10)) # Create a polynomial line graph with those arguments x = list(range(_from, to + 1)) fig = figure(title="Polynomial") fig.line(x, [i ** 2 for i in x], color=colors[color], line_width=2) js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(fig) html = flask.render_template( 'embed.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, color=color, _from=_from, to=to ) return encode_utf8(html)
def second_stock(): n = app_stock.vars['name'] ss = "WIKI/" + n + ".4" mydata = quandl.get(ss, encoding='latin1', parse_dates=['Date'], dayfirst=True, index_col='Date', trim_start="2016-05-05", trim_end="2016-06-05", returns = "numpy", authtoken="ZemsPswo-xM16GFxuKP2") mydata = pd.DataFrame(mydata) #mydata['Date'] = mydata['Date'].astype('datetime64[ns]') x = mydata['Date'] y = mydata['Close'] p = figure(title="Stock close price", x_axis_label='Date', y_axis_label='close price', plot_height = 300, plot_width = 550) p.line(x, y, legend="Price in USD", line_width=3, color = "#2222aa") # Configure resources to include BokehJS inline in the document. # For more details see: # http://bokeh.pydata.org/en/latest/docs/reference/resources_embedding.html#bokeh-embed js_resources = INLINE.render_js() css_resources = INLINE.render_css() # For more details see: # http://bokeh.pydata.org/en/latest/docs/user_guide/embedding.html#components script, div = components(p, INLINE) html = flask.render_template( 'stockgraph.html', ticker = app_stock.vars['name'], plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, ) return encode_utf8(html)
def showind(): c0 = request.args['country0'] c1 = request.args['country1'] c2 = request.args['country2'] c3 = request.args['country3'] c4 = request.args['country4'] countries = [c0, c1, c2, c3, c4] i0 = request.args['indicator'] indicator = [i0] performance = get_indicator(countries,indicator) chartind = plot_ind(performance) script,div = components(chartind) return render_template('showind.html', js_resources=INLINE.render_js(), css_resources=INLINE.render_css(), script=script, div=div)
def dataframe_to_linegraph(dataframe, show_columns, title='graph'): '''Convert a dataframe to a bokeh line graph''' thisplot = figure( tools="pan,box_zoom,reset,save", title=title, x_axis_type="datetime", x_axis_label='Month', y_axis_label='Count', plot_width=1200, ) yvals = [x for x in dataframe.index.tolist()] idx = 0 for col, show in show_columns: if not show: continue width = 2 try: color = Paired[12][idx] except IndexError: color = Paired[12][-1] kwargs = { 'legend': col, 'line_width': width, 'line_color': color } idx += 1 xvals = dataframe[col].tolist() line = thisplot.line( yvals, xvals, **kwargs ) thisplot.add_tools( HoverTool( renderers=[line], tooltips=[ ('Name', col), ('Color', '<span class="bk-tooltip-color-block" ' 'style="background-color:{}"> </span>'. format(kwargs['line_color'])), ('count', "@y{int}"), ] ) ) thisplot.legend.location = "top_left" plot_script, plot_div = components(thisplot) js_resources = INLINE.render_js() css_resources = INLINE.render_css() return (plot_script, plot_div, js_resources, css_resources)
def save(fig): ''' Saves bokeh plots to HTML in ./Bokeh_plots ''' ## Set up directory for the plot files plots_dir = os.path.join('.','Bokeh_plots') if not os.path.exists(plots_dir): os.makedirs(plots_dir) filename = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')+'.html' figfile = os.path.join(plots_dir, filename) ## Save and show figure from jinja2 import Template from bokeh.embed import components from bokeh.resources import INLINE plots = {'fig':fig} script, div = components(plots) template = Template('''<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Bokeh Scatter Plots</title> {{ js_resources }} {{ css_resources }} {{ script }} <style> .embed-wrapper { width: 50%; height: 400px; margin: auto; } </style> </head> <body> {% for key in div.keys() %} <div class="embed-wrapper"> {{ div[key] }} </div> {% endfor %} </body> </html> ''') js_resources = INLINE.render_js() css_resources = INLINE.render_css() bokeh_html = template.render(js_resources=js_resources, css_resources=css_resources, script=script, div=div) with open(figfile, 'w') as f: f.write(bokeh_html)
def app_parse(): ticker_input = app.vars['ticker'] # ticker_input = raw_input('Please enter a stock ticker code (e.g., AAPL): ') quandl_url = 'https://www.quandl.com/api/v3/datasets/WIKI/' api_key = '_Dvc2yU_qTfyYtTzsqFd' csv_ext = '.csv' json_ext = '.json' input_url = quandl_url + ticker_input + json_ext input_csv = quandl_url + ticker_input + csv_ext r = requests.get(input_url) data = json.loads(r.text) pd_data = pd.read_csv(input_csv,parse_dates=['Date']) x = data["dataset"]["data"] data_rows = len(x) data_rows_good = np.zeros(data_rows) start_date = datetime.now().date() + timedelta(-30) booleans = [] for date_row in range(0,len(pd_data.Date)): # ticker_date = datetime.strptime(pd_data.Date[date_row],'%Y-%m-%d').date() ticker_date = pd_data.Date[date_row].date() if start_date < ticker_date: booleans.append(True) else: booleans.append(False) closing_price = pd_data.Close[booleans] # output_file("datetime.html") # create a new plot with a datetime axis type p = figure(width=500, height=500, x_axis_type="datetime") p.line(pd_data.Date[booleans], pd_data.Close[booleans], color='red', alpha=0.5) js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(p, INLINE) html = flask.render_template( 'datetime.html', ticker = app_stock.vars['name'], plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, ) return encode_utf8(html)
def polynomial(): """ Very simple embedding of a polynomial chart """ # connect to google spreadsheet and get sheet scope = ['https://spreadsheets.google.com/feeds'] credentials = ServiceAccountCredentials.from_json_keyfile_name('oauth-keyfile.json', scopes=scope) gc = gspread.authorize(credentials) responses = gc.open("Homework Histogram (Responses)") responses = responses.worksheet("Form Responses 1") # create plot html elements data = pd.DataFrame(responses.get_all_records()) # hist = Histogram(data, values='Question 1') # create a list of plots scripts = [] divs = [] for question in ['Question 1', 'Question 2']: hist = Histogram(data, values=question) script, div = components(hist, INLINE) scripts.append(script) divs.append(div) # Configure resources to include BokehJS inline in the document. # For more details see: # http://bokeh.pydata.org/en/latest/docs/reference/resources_embedding.html#bokeh-embed js_resources = INLINE.render_js() css_resources = INLINE.render_css() # script, div = components(hist, INLINE) # For more details see: # http://bokeh.pydata.org/en/latest/docs/user_guide/embedding.html#components html = flask.render_template( 'embed.html', scripts = scripts, divs=divs, js_resources=js_resources, css_resources=css_resources, ) return encode_utf8(html)
def index(): if request.method == 'GET': return render_template('index.html') else: app.vars['Company_Name'] = request.form['ticker'] if not app.vars['Company_Name']: return render_template('error.html') app.vars['features'] = request.form.getlist('features') Company_Name=app.vars['Company_Name'] API_url='https://www.quandl.com/api/v3/datasets/WIKI/%s.csv?api_key=a5-JLQBhNfxLnwxfXoUE' % Company_Name r = requests.get(API_url) if r.status_code == 404: return render_template('error.html') data = pd.read_csv(API_url,parse_dates=['Date']) Colors=["blue","green","yellow","red"] Color_index=0 target_data=data.ix[:,['Open','Adj. Open','Close','Adj. Close']] p=figure(x_axis_type="datetime") p.xaxis.axis_label = 'Date' p.title = 'Data from Quandle WIKI set' if 'Close' in app.vars['features']: p.line(x=data['Date'],y=target_data['Close'],legend="%s:Close" % Company_Name, line_color=Colors[Color_index]) Color_index = Color_index +1 if 'Adj. Close' in app.vars['features']: p.line(x=data['Date'],y=target_data['Adj. Close'],legend="%s:Adj. Close" % Company_Name, line_color=Colors[Color_index]) Color_index = Color_index +1 if 'Open' in app.vars['features']: p.line(x=data['Date'],y=target_data['Open'],legend="%s:Open" % Company_Name, line_color=Colors[Color_index]) Color_index = Color_index +1 if 'Adj. Open' in app.vars['features']: p.line(x=data['Date'],y=target_data['Adj. Open'],legend="%s:Adj. Open" % Company_Name, line_color=Colors[Color_index]) js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(p, INLINE) html = flask.render_template( 'embed.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, Company_Name= Company_Name ) return encode_utf8(html)
def index(): if request.method == 'POST': # Get the entered keywords keywords = request.form["keywords"] #keywords = [keyword.strip() for keyword in keywords.split(',') if len(keyword) > 0] keywords = [keyword.strip() for keyword in keywords.split(',') if len(keyword.strip()) > 0] keywords = keywords[:len(COLORS)] # prevent too many keywords # Get the location data user_location = (request.form['latitude'], request.form['longitude']) logger.info('{} | {}'.format(user_location, keywords)) fig = make_fig(keywords) # Build the bokeh plot resources # https://github.com/bokeh/bokeh/tree/master/examples/embed/simple js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(fig, INLINE) # Get recent comments matching the keywords recent_comments = get_matching_comments_2(keywords, user_location) # special case if no keywords entered - show 'All' comment counts if not keywords: keywords = ['All'] # Build the web page html = render_template( 'index.html', keywords = ', '.join(keyword.title() for keyword in keywords), plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, jobs=recent_comments, ) return html return render_template('index.html')
def plot(content, data=[]): x = list(range(0, 100 + 1)) fig = figure(title="Polynomial") print(dir(fig)) #fig.line(x, [i ** 2 for i in x], line_width=2) #fig.hbar(data['requested_by_name'], data['usage']) p=Bar(data, 'requested_by_name', values='usage') js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(p) html = render_template( 'overview.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, content=content, ) return encode_utf8(html)
def showlegscore(): c0 = request.args['country0'] c1 = request.args['country1'] c2 = request.args['country2'] c3 = request.args['country3'] c4 = request.args['country4'] countries = [c0, c1, c2, c3, c4] i0 = ['CC.EST'] i1 = ['GE.EST'] i2 = ['RQ.EST'] i3 = ['VA.EST'] i4 = ['RL.EST'] indicators = [i0, i1, i2, i3, i4] dataframes = create_dataframes(countries,indicators) score = get_mean(dataframes) chartsco = plot_score(score) script,div = components(chartsco) return render_template('showscore.html', js_resources=INLINE.render_js(), css_resources=INLINE.render_css(), script=script, div=div)
def predict(): input_sequence = request.form['sequence'] seq = standardize_sequence(to_numeric_rep(input_sequence, 'mw'), 'protease').reshape(1, -1) preds = predictions(drugs, models, seq) TOOLS = [PanTool(), ResetTool(), WheelZoomTool(), SaveTool()] plot = BoxPlot(data=preds, values='log10(DR)', label='drug', color='drug', title="protease drug resistance", plot_width=600, plot_height=400, legend=False, tools=TOOLS) js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(plot, INLINE) return render_template('predictor/predictions.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources,)
def polynomial(): """ Very simple embedding of a polynomial chart """ # Grab the inputs arguments from the URL # This is automated by the button args = flask.request.args # Get all the form arguments in the url with defaults color = colors[getitem(args, 'color', 'Black')] _from = int(getitem(args, '_from', 0)) to = int(getitem(args, 'to', 10)) # Create a polynomial line graph x = list(range(_from, to + 1)) fig = figure(title="Polynomial") fig.line(x, [i ** 2 for i in x], color=color, line_width=2) # Configure resources to include BokehJS inline in the document. # For more details see: # http://bokeh.pydata.org/en/latest/docs/reference/resources_embedding.html#bokeh-embed js_resources = INLINE.render_js() css_resources = INLINE.render_css() # For more details see: # http://bokeh.pydata.org/en/latest/docs/user_guide/embedding.html#components script, div = components(fig, INLINE) html = flask.render_template( 'embed.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, color=color, _from=_from, to=to ) return encode_utf8(html)
def bokeh_bild(range=4800): data = bk_plot_timeline( LoadFromSQL( range, app.config['DATABASE_LOCATION'], 'VS1_GT1', 'VS1_GT3', 'VS1_GT2', 'VS1_Setpoint' ) ) print(data) plot_script, plot_div = components(data, INLINE) js_resources = INLINE.render_js() css_resources = INLINE.render_css() return render_template( 'bild.html', script=plot_script, div=plot_div, js_resources=js_resources, css_resources=css_resources)
def index(): args = flask.request.args _input_material = str(args.get('_input_material', DEFAULT_MATERIAL)) _input_thickness = str(args.get('_input_thickness', DEFAULT_THICKNESS)) mat = Material(_input_material, float(_input_thickness) * u.micron) energy = u.Quantity(np.arange(1, 100), 'keV') source = ColumnDataSource(data={'x': energy.value, 'y': mat.absorption(energy).value}) source_static = ColumnDataSource(data={'x': energy.value, 'y': mat.absorption(energy).value}) fig = figure(title="Absorption", tools=TOOLS, plot_height=PLOT_HEIGHT, width=PLOT_WIDTH, toolbar_location="right", x_axis_label='Energy [keV]') fig.line('x', 'y', source=source_static, color='red', line_width=2, legend=_input_material) hover = HoverTool() #hover.tooltips = [ # ("x", "@time_str"), # ("y", "@xrsb") #] fig.add_tools(hover) js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(fig, INLINE) html = flask.render_template( 'index.html', plot_script=script, plot_div=div, material_list=list(roentgen.elements['name']), js_resources=js_resources, css_resources=css_resources, _input_material=_input_material, _input_thickness=_input_thickness, ) return encode_utf8(html)
def portalDynamicy(snapshot, portalid): Session = current_app.config['dbsession'] q = Session.query(PortalSnapshotDynamicity).filter( PortalSnapshotDynamicity.portalid == portalid).filter( PortalSnapshotDynamicity.snapshot <= snapshot) data = [] keys = [ 'dindex', 'changefrequ', 'adddelratio', 'dyratio', 'staticRatio', 'addRatio', 'delRatio', 'updatedRatio' ] for psd in q: d = row2dict(psd) for k in keys: d[k] = psd.__getattribute__(k) data.append(d) df = pd.DataFrame(data) with Timer(key="dynPlot", verbose=True) as t: p = portalDynamicity(df) script, div = components(p) js_resources = INLINE.render_js() css_resources = INLINE.render_css() data = getPortalInfos(Session, portalid, snapshot) data['portals'] = [row2dict(r) for r in Session.query(Portal).all()] return render("odpw_portal_dynamicity.jinja", plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, snapshot=snapshot, portalid=portalid, data=data)
async def get_market_limitstocks( request: Request): # async加了就支持异步 把Request赋值给request js_res = INLINE.render_js() css_res = INLINE.render_css() limits_data = get_data_limits() p1 = draw_line_data_datetime_avg_width(limits_data['df_limit_u_5_list'], limits_data['tradedate_5_list'], 'blue', 'black', 900) script1, div1 = components(p1) p2 = draw_line_data_datetime_avg_width(limits_data['df_limit_d_5_list'], limits_data['tradedate_5_list'], 'green', 'black', 900) script2, div2 = components(p2) return tmp.TemplateResponse( 'market_limits.html', { 'request': request, 'limits_dict': limits_data, "p_script1": script1, "p_div1": div1, "p_script2": script2, "p_div2": div2, "js_res": js_res, "css_res": css_res })
def portalssize(): with Timer(key="get_portalsstats", verbose=True): Session = current_app.config['dbsession'] with Timer(key="query_portalsstats", verbose=True): results = [ row2dict(r) for r in Session.query(Portal, Portal.snapshot_count, Portal.first_snapshot, Portal.last_snapshot, Portal.datasetcount, Portal.resourcecount) ] df = pd.DataFrame(results) with Timer(key="plot_portalsstats", verbose=True): p = portalsScatter(df) script, div = components(p) js_resources = INLINE.render_js() css_resources = INLINE.render_css() return render("odpw_portals_stats.jinja", plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources)
def portalEvolution(snapshot, portalid): db=current_app.config['db'] data={} portal = db.get_portal(portalid) snapshots = db.get_portal_snapshots(portalid) for sn in snapshots: portal.update(db.get_portal_info(portal['uri'], sn)) quality = db.get_portal_quality(portal['uri'], sn) for d in qa: for m in d['metrics']: m = m.lower() if m in quality: portal[m] = quality[m]['measurement'] portal['snapshot'] = sn # TODO query activity metadata portal['end'] = toLastdayinisoweek(sn) data[portalid + str(sn)] = portal.copy() df=pd.DataFrame([v for k,v in data.items()]) p=evolutionCharts(df) script, div= components(p) js_resources = INLINE.render_js() css_resources = INLINE.render_css() data = getPortalInfos(db,portalid,snapshot) return render("odpw_portal_evolution.jinja", plot_script=script ,plot_div=div ,js_resources=js_resources ,css_resources=css_resources ,snapshot=snapshot , portalid=portalid , data=data )
width: 50%; height: 400px; margin: auto; } </style> </head> <body> {% for key in div.keys() %} <div class="embed-wrapper"> {{ div[key] }} </div> {% endfor %} </body> </html> ''') js_resources = INLINE.render_js() css_resources = INLINE.render_css() filename = 'embed_multiple.html' html = template.render(js_resources=js_resources, css_resources=css_resources, script=script, div=div) with io.open(filename, mode='w', encoding='utf-8') as f: f.write(html) view(filename)
def plot_page(df, columns, request, page): """Render the Bokeh plot. Inputs ------ df : pd.DataFrame The DataFrame with the data columns : list Which columns to use for choices in the plot request : flask The request object from flask page : str ('plot', 'plot_exo') Which page to render (for combined ['plot_exo'] or just SWEET-Cat ['plot']) Output ------ The rendered page with the plot """ colorscheme = 'Plasma' if request.method == 'POST': # Something is being submitted color = request.form['color'] x = str(request.form['x']) y = str(request.form['y']) z = str(request.form['z']) z = None if z == 'None' else z if (x not in columns) or (y not in columns): return redirect(url_for(page)) if (z is not None) and (z not in columns): return redirect(url_for(page)) colorscheme = str(request.form.get('colorscheme', colorscheme)) if colorscheme not in colorschemes.keys(): return redirect(url_for(page)) checkboxes = request.form.getlist("checkboxes") if not checkboxes: # When [] is returned checkboxes = [''] if checkboxes[0] not in [[], '', 'h**o']: return redirect(url_for(page)) df, x, y, z = extract(df, x, y, z, checkboxes) # Setting the limits x1, x2, y1, y2 = get_limits(request.form, x, y) if x.name != session.get('x', None): x1 = min(x) x2 = max(x) session['x'] = x.name if y.name != session.get('y', None): y1 = min(y) y2 = max(y) session['y'] = y.name xscale = str(request.form['xscale']) yscale = str(request.form['yscale']) if xscale not in ['linear', 'log']: return redirect(url_for(page)) if yscale not in ['linear', 'log']: return redirect(url_for(page)) else: if page == "plot_exo": x = 'discovered' y = 'plMass' z = None x1, x2 = 1985, 2020 y1, y2 = 0.0001, 200 session['x'] = 'discovered' session['y'] = 'plMass' session['z'] = 'None' else: x = 'teff' y = 'lum' z = 'logg' x1, x2 = 8000, 2500 y1, y2 = 1e-3, 3000 session['x'] = 'teff' session['y'] = 'lum' session['z'] = 'logg' yscale = 'log' color = 'Orange' xscale = 'linear' checkboxes = [] df, x, y, z = extract(df, x, y, z, checkboxes) # Check scale xscale, yscale, error = check_scale(x, y, xscale, yscale) stars = df['Star'] stars = list(stars.values) # Turn series into list. hover = HoverTool(tooltips=[ ("{}".format(x.name), "$x"), ("{}".format(y.name), "$y"), ("Star", "@star"), ]) num_points = count(x, y, [x1, x2], [y1, y2]) title = '{} vs. {}:\tNumber of objects in plot: {}'.format( x.name, y.name, num_points) tools = "crosshair,pan,wheel_zoom,box_zoom,reset,box_select,lasso_select,save".split( ',') fig = figure(title=title, tools=tools + [hover], plot_width=800, plot_height=400, toolbar_location='above', x_range=[x1, x2], y_range=[y1, y2], x_axis_type=xscale, y_axis_type=yscale) if z is not None: # Add colours and a colorbar COLORS, pallete = colorschemes[colorscheme] groups = pd.qcut(z.values, len(COLORS), duplicates="drop") c = [COLORS[xx] for xx in groups.codes] source = ColumnDataSource(data=dict(x=x, y=y, c=c, star=stars)) color_mapper = LinearColorMapper(palette=pallete, low=z.min(), high=z.max()) color_bar = ColorBar(color_mapper=color_mapper, label_standoff=12, border_line_color=None, location=(0, 0)) fig.add_layout(color_bar, 'right') fig.circle('x', 'y', source=source, size=10, color='c', fill_alpha=0.2, line_color=None) z = z.name fig.xaxis.axis_label = x.name fig.yaxis.axis_label = y.name color_bar.title = z else: # Simple colorbar source = ColumnDataSource(data=dict(x=x, y=y, star=stars)) fig.circle('x', 'y', source=source, size=10, color=colors[color], fill_alpha=0.2, line_color=None) fig.xaxis.axis_label = x.name fig.yaxis.axis_label = y.name # Horizontal histogram hhist, hedges, hmax = scaled_histogram(x, num_points, xscale) ph = figure(toolbar_location=None, plot_width=fig.plot_width, plot_height=200, x_range=fig.x_range, y_range=(-hmax * 0.1, hmax), min_border=10, min_border_left=50, y_axis_location="right", x_axis_type=xscale) ph.xgrid.grid_line_color = None ph.yaxis.major_label_orientation = np.pi / 4 ph.background_fill_color = "#fafafa" ph.quad(bottom=0, left=hedges[:-1], right=hedges[1:], top=hhist, color="white", line_color=colors[color]) # Vertical histogram vhist, vedges, vmax = scaled_histogram(y, num_points, yscale) pv = figure(toolbar_location=None, plot_width=200, plot_height=fig.plot_height, x_range=(-vmax * 0.1, vmax), y_range=fig.y_range, min_border=10, y_axis_location="right", y_axis_type=yscale) pv.ygrid.grid_line_color = None pv.xaxis.major_label_orientation = np.pi / 4 pv.background_fill_color = "#fafafa" pv.quad(left=0, bottom=vedges[:-1], top=vedges[1:], right=vhist, color="white", line_color=colors[color]) layout = column(row(fig, pv), row(ph, Spacer(width=200, height=200))) js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(layout) if error is not None: flash('Scale was changed from log to linear') html = render_template('plot.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, color=color, colors=colors, x=x.name, y=y.name, z=z, x1=x1, x2=x2, y1=y1, y2=y2, xscale=xscale, yscale=yscale, checkboxes=checkboxes, columns=columns, colorschemes=colorschemes, colorscheme=colorscheme) return encode_utf8(html)
def index(): form = InputForm(request.form) if request.method == 'POST' and form.validate(): veg, fylke, hpfra, hptil, mfra, mtil = form.veg.data, form.fylke.data, form.hpfra.data, form.hptil.data, form.mfra.data, form.mtil.data viseside = True else: viseside = False veg = 'fv241' fylke = '14' hpfra = 1 hptil = 10 mfra = 0 mtil = 10000 if hpfra == hptil: vegref = str(fylke) + '00' + veg + 'hp' + str(hpfra) + 'm' + str( mfra) + '-' + str(mtil) else: vegref = str(fylke) + '00' + veg + 'hp' + str(hpfra) + 'm' + str( mfra) + '-' + 'hp' + str(hptil) + 'm' + str(mtil) df = data.hentData(vegref) fig_skredtype = plot.plot_skredtype(df, vegref) fig_maaned = plot.plot_maaned(df, vegref) fig_losneområde = plot.plot_losneomrade(df, vegref) fig_steinmaaned = plot.plot_steinmaaned(df, vegref) fig_snomaaned = plot.plot_snomaaned(df, vegref) #fig_steinm = plot.plot_steinmaaned(df, vegref) fig_aar = plot.plot_aar(df, vegref) fig_volumomraade = plot.plot_volumomraade(df, vegref) fig_steinlosneomrade = plot.plot_steinlosneomrade(df, vegref) #grid_oversikt = gridplot([fig_skredtype, fig_maaned, fig_losneområde, fig_aar], ncols=1, plot_width=500) #grid_skredtype = gridplot([fig_steinmaaned, fig_snomaaned], ncols=1, plot_width=500) #gird_samlaplot = gridplot([], ncols=1, plot_width=500, plot_height=500) # grab the static resources js_resources = INLINE.render_js() css_resources = INLINE.render_css() # render template script_skredtype, div_skredtype = components(fig_skredtype) script_maaned, div_maaned = components(fig_maaned) script_aar, div_aar = components(fig_aar) script_steinmaaned, div_steinmaaned = components(fig_steinmaaned) script_snomaaned, div_snomaaned = components(fig_snomaaned) script_losneområde, div_losneområde = components(fig_losneområde) script_volumomraade, div_volumomraade = components(fig_volumomraade) script_steinlosneområde, div_steinlosneområde = components( fig_steinlosneomrade) html = render_template('index.html', script_skredtype=script_skredtype, div_skredtype=div_skredtype, script_maaned=script_maaned, div_maaned=div_maaned, script_aar=script_aar, div_aar=div_aar, script_losneområde=script_losneområde, div_losneområde=div_losneområde, script_steinmaaned=script_steinmaaned, div_steinmaaned=div_steinmaaned, script_steinlosneområde=script_steinlosneområde, div_steinlosneområde=div_steinlosneområde, script_snomaaned=script_snomaaned, div_snomaaned=div_snomaaned, script_volumomraade=script_volumomraade, div_volumomraade=div_volumomraade, js_resources=js_resources, css_resources=css_resources, form=form, vegref=vegref, viseside=viseside) return encode_utf8(html)
def Ope(): if not request.method in ['POST']: return redirect(url_for('main.index')) files = request.files.getlist("ope[]") app.logger.debug('files={}'.format(files)) opes = [] del_files = [] upload_dir = current_app.config['APP_UPLOAD'] for f in files: filename = secure_filename(f.filename) ope = os.path.join(upload_dir, filename) f.save(ope) del_files.append(ope) if filename.lower().endswith(".ope"): opes.append(ope) app.logger.debug('opes={}'.format(opes)) try: targets = helper.ope(opes, upload_dir, app.logger) #errors = helper.format_error(targets, app.logger) except Exception as e: app.logger.error('Error: invalid ope file. {}'.format(e)) err_msg = "Plot Error: {}".format(e) #errors.append(err_msg) delete_files(del_files) return render_template('target_visibility.html', errors=[err_msg]) #filepath = tempfile.NamedTemporaryFile().name #filepath = os.path.join(current_app.config['APP_UPLOAD'], filename) mysite = helper.site(request.form.get('site')) mydate = request.form.get('date') #app.logger.debug('targets={}'.format(targets)) #app.logger.debug('filepath={}'.format(filepath)) app.logger.debug('mydate={}'.format(mydate)) delete_files(del_files) try: #fig = helper.populate_target2(targets, mysite, mydate, filepath, app.logger) fig = helper.populate_interactive_target(target_list=targets, mysite=mysite, mydate=mydate, logger=app.logger) except Exception as e: app.logger.error('Error: failed to populate ope plot. {}'.format(e)) err_msg = "Plot Error: {}".format(e) #errors.append(err_msg) return render_template('target_visibility.html', errors=[err_msg]) else: # Grab the static resources js_resources = INLINE.render_js() css_resources = INLINE.render_css() # render template script, div = components(fig) html = render_template('target_visibility.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, errors=None) return encode_utf8(html)
def plotDataset(ds,template='dataset.html',title='Dataset',color=None,*args, **kwargs): ds = ds.copy() ds.data.columns = ds.data.columns.astype(str) # ts = TimeSeries(ds.data) numlines=len(ds.data.columns) if color is None: color = ds.meta[ds.meta.columns[0]] label = color color = colorby(color) # print color # source = ColumnDataSource(data=ds.data) source = ColumnDataSource(dict(xs=[ds.data.index.values]*ds.data.shape[1], ys = [ds.data[name].values for name in ds.data], yslog = [np.log2(ds.data[name].values) for name in ds.data], color=color, label=label)) labelsource = ColumnDataSource(ds.meta) colorsource = ColumnDataSource({k:colorby(ds.meta[k]) for k in ds.meta.columns.tolist()}) # if color is None: # # color = viridis(numlines) # color = colorby(range(numlines)) # else: # # v = viridis(max(color)+1) # # color = [v[c] for c in color] # color = colorby(color) fig = figure(title=title,plot_width=97*8,) # plot = Plot() # fig.line(ds.data.index.values, ds.data, line_width=2) # fig.multi_line(xs=[ds.data.index.values]*ds.data.shape[1], # ys = [ds.data[name].values for name in ds.data], # line_color=color, # line_width=5) fig.multi_line('xs', 'ys', color='color', legend='label', source=source) fig.legend.location = "top_left" # glyph = MultiLine(xs="xs", ys="ys", line_color="", line_width=2) # fig.add_glyph(source, glyph) # plot.add_glyph(source, glyph) callback = CustomJS(args=dict(source=source,colorsource=colorsource,labelsource=labelsource), code=""" var data = source.get('data'); var data2 = colorsource.get('data'); var data3 = labelsource.get('data'); var f = cb_obj.get('value') color = data['color'] color2 = data2[f] label = data['label'] label2 = data3[f] for (i = 0; i < color.length; i++) { color[i] = color2[i] label[i] = label2[i] } source.trigger('change'); """) logcallback = CustomJS(args=dict(source=source), code=""" var data = source.get('data'); data['ys'] = data['yslog'] source.trigger('change'); """) menu = [(c,c) for c in ds.meta.columns] dropdown = Dropdown(label="Color by", button_type="warning", menu=menu, callback=callback) # layout = vform(dropdown, fig) layout = column(dropdown, fig) js_resources = INLINE.render_js() css_resources = INLINE.render_css() # script, div = components(ts) script, div = components(layout) # script, div = components(fig) html = render_template( template, plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, *args, **kwargs ) return encode_utf8(html)
data: {"index": active_button}, dataType: 'json', success: function (json_from_server) { var test = json_from_server[active_button] fig.y_range.start = test[0]; fig.y_range.end = test[1]; fig.x_range.start = date_ints[active_button] fig.x_range.end = date_ints[6] }, error: function() { alert("Oh no, something went wrong. Search for an error " + "message in Flask log and browser developer tools."); } }); """ % (date_ints, stock_ticker)) p.js_on_event('tap', tap_callback) button2.js_on_event(ButtonClick, button_callback) radio_button_group.callback = radio_button_callback lay_out = column(row(text_input, button2), radio_button_group, output, row(p,div)) curdoc().add_root(lay_out) js,div=components(lay_out, INLINE) cdn_js=INLINE.render_js() cdn_css=INLINE.render_css()
def order(): ''' Render order page ''' date_start = request.form.get('date_start', '2018-01-01') date_end = request.form.get('date_end', '2018-01-31') if request.form.get('time_frame') is None: time_frame = 'date' else: time_frame = request.form.get('time_frame') # render template best_product = get_best_product(date_start, date_end) avg_price = formatter(get_avg_price(date_start, date_end), 'dollar') # Revenue by categories rev_data = get_rev_by_cat(date_start, date_end) rev_js, rev_div = vbar(rev_data, 'category', 'revenue', 'dollar') # Order number by categories order_num_data = get_num_order_by_cat(date_start, date_end) order_num_js, order_num_div = vbar(order_num_data, 'category', 'order_number') time_dict = {'date': 'date', 'ww': 'week', 'mon': 'month', 'q': 'quarter'} # Top 5 revenue category trend rev_top5 = rev_data.loc[:5, 'category'].tolist() rev_trend_data = get_cat_trend(date_start, date_end, time_frame, rev_top5, 'revenue') rev_trend_js, rev_trend_div = multiline(rev_trend_data, time_dict[time_frame], 'revenue', 'dollar', rev_top5[0], rev_top5[1], rev_top5[2], rev_top5[3], rev_top5[4]) # top 5 order number category trend num_top5 = order_num_data.loc[:5, 'category'].tolist() num_trend_data = get_cat_trend(date_start, date_end, time_frame, num_top5, 'order_number') num_trend_js, num_trend_div = multiline( num_trend_data, time_dict[time_frame], 'order_number', 'number', num_top5[0], num_top5[1], num_top5[2], num_top5[3], num_top5[4]) # Max, avg, min price for top 5 categories price_data = get_max_avg_min_price_by_cat(date_start, date_end) price_js, price_div = multiline(price_data, 'category', 'price', 'dollar', 'max_price', 'avg_price', 'min_price') # grab the static resources js_resources = INLINE.render_js() css_resources = INLINE.render_css() html = render_template( 'order.html', rev_js=rev_js, rev_div=rev_div, order_num_js=order_num_js, order_num_div=order_num_div, price_js=price_js, price_div=price_div, rev_trend_js=rev_trend_js, rev_trend_div=rev_trend_div, num_trend_js=num_trend_js, num_trend_div=num_trend_div, js_resources=js_resources, css_resources=css_resources, best_product=best_product, avg_price=avg_price, date_start=date_start, date_end=date_end, ) return html
def user_response_times(username): df = MongoTools.get_user_response_time(username, usecache=True) ''' df['x_min'] = df['x_min'] / ( 24.0 * 60.0 * 60.0) df['x_max'] = df['x_max'] / ( 24.0 * 60.0 * 60.0) df['x_mean'] = df['x_mean'] / ( 24.0 * 60.0 * 60.0) ''' TOOLS = "pan,wheel_zoom,box_zoom,reset,save" w = 1000 * 10 p = figure( x_axis_type="datetime", tools=TOOLS, plot_width=1000, title="%s response times" % username, x_axis_label='month', y_axis_label='total days to respond after mention', ) p.xaxis.major_label_orientation = pi/4 p.grid.grid_line_alpha = 0.3 p.segment(df.index, df.x_max, df.index, df.x_min, color="black") ''' p.vbar( x=df.index, top=df.x_std, bottom=df.x_std + 1, width=100, fill_color="green", line_color="black" ) ''' # AttributeError: unexpected attribute 'height' to VBar, possible attributes are # bottom, fill_alpha, fill_color, js_callbacks, line_alpha, line_cap, line_color, # line_dash, line_dash_offset, line_join, line_width, name, tags, top, visible, # width or x p.vbar( x=df.index, top=df.x_mean, bottom=df.x_std, width=1000, fill_color="red", line_color="black" ) line = p.line( df.index, df.x_std, legend='stdev' ) line = p.line( df.index, df.x_mean, legend='mean', color='green' ) p.legend.location = "top_left" plot_script, plot_div = components(p) js_resources = INLINE.render_js() css_resources = INLINE.render_css() return render_template( 'user_response_times.html', data={ 'username': username, }, js_resources=js_resources, css_resources=css_resources, plot_script=plot_script, plot_div=plot_div, )
def show_simulation(): rounds = 0 try: if gtruncate_initial_rounds == 0: ignore_initial_rounds = int(session.get('ignore_initial_rounds', 50)) else: ignore_initial_rounds = 0 except NameError: ignore_initial_rounds = int(session.get('ignore_initial_rounds', 50)) gtruncate_initial_rounds = 0 plots = {} filenames = [] path = request.args.get('subdir') if path is None: path = newest_subdirectory('./result') try: with open(path + 'description.txt') as desc_file: desc = desc_file.read() except IOError: desc = '' plots = {} for filename in os.listdir(path): if not filename.endswith('.csv'): continue elif filename.startswith('#'): continue df = pd.read_csv(path + filename).ix[gtruncate_initial_rounds:] try: rounds = max(df['round']) except KeyError: rounds = max(df['index']) if ignore_initial_rounds >= rounds: ignore_initial_rounds = 0 print('kill') df = df.where((pd.notnull(df)), None) df.dropna(1, how='all', inplace=True) if filename.startswith('aggregate_'): plots.update(make_aggregate_graphs(df, filename, ignore_initial_rounds)) else: try: if max(df.get('id', [0])) == 0: plots.update(make_simple_graphs(df, filename, ignore_initial_rounds)) else: plots.update(make_panel_graphs(df, filename, ignore_initial_rounds)) except ValueError: print((filename, 'not displayable: ValueError')) script, div = components(plots) output = [] for idname_title, graph in div.items(): idname, title = json.loads(idname_title) output.append({'idname': idname, # can not stay i otherwise the cookie minimizing does not work 'title': title, 'graph': graph}) output.extend(load_text(path)) output.extend(load_text(path + '/../../')) output = sorted(output, key=lambda x: x['title']) return render_template('show_outcome.html', entries=output, desc=desc, setup=setup_dialog(rounds), script=script, js_resources=INLINE.render_js(), css_resources=INLINE.render_css())
def status_newDimensions(): """ Very simple embedding of a polynomial chart """ # Grab the inputs arguments from the URL # This is automated by the button args = flask.request.args # Get all the form arguments in the url with defaults device = str(getitem(args, 'device', 'compute-2-29')) time_frame = int(getitem(args, 'time_frame', 60)) #device='compute-2-29' # Create a polynomial line graph n_times=getData_N_Min_device(time_frame) #n_times=cursor if n_times.count()==0: return "No Data" #exit(1) data=dict() t=0 data=[] choose_machine=device x=[] y=[] label=[] time_label=[] i=1 for t1 in n_times: device_data=t1['data'][choose_machine] x.append(device_data[0][0]) y.append(device_data[1][0]) label.append(choose_machine) time_label.append(i) i+=1 #output_file("trace_new_dims.html") TOOLS="resize,crosshair,pan,wheel_zoom,box_zoom,reset,tap,previewsave,box_select,poly_select,lasso_select" source = ColumnDataSource( data=dict( x=x, y=y, desc=time_label, # colors=color_src, ) ) hover = HoverTool( tooltips=""" <div> <div> <span style="font-size: 17px; font-weight: bold;">@desc</span> <span style="font-size: 15px; color: #966;">[$index]</span> </div> <div> <span style="font-size: 15px;">Location</span> <span style="font-size: 10px; color: #696;">($x, $y)</span> </div> </div> """ ) TOOLS=["pan,wheel_zoom,box_zoom,reset,resize",hover] p = figure(plot_width=600, plot_height=600, tools=TOOLS, title="Mouse over the dots") p.line('x', 'y', source=source) # Configure resources to include BokehJS inline in the document. # For more details see: # http://bokeh.pydata.org/en/latest/docs/reference/resources_embedding.html#bokeh-embed js_resources = INLINE.render_js() css_resources = INLINE.render_css() # For more details see: # http://bokeh.pydata.org/en/latest/docs/user_guide/embedding.html#components script, div = components(p, INLINE) html = flask.render_template( 'embed.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, ) return encode_utf8(html)
def cluster_snapshot(): #args = flask.request.args # Get all the form arguments in the url with defaults #new_att = str(getitem(args, 'device_button','G_Swap_Used')) request_att=[] if request.method == "POST": #checked = 'att' in request.form request_att = request.form.getlist('att') #any_selected = bool(selected) request_att=[x.encode('UTF8') for x in request_att] #print(type(request_att)) #return str(request_att) #print (new_att) n_times=getData_N_Min_cluster(5) #n_times=cursor if n_times.count()==0: print ("No Data") #exit(1) data=dict() t=0 data=[] #Target only swap space #'compute-2-28' removed machine=['compute-2-29', 'compute-9-30', 'compute-9-36', 'compute-9-35','compute-9-34','compute-2-23', 'compute-2-25', 'compute-2-24', 'compute-2-27', 'compute-2-26', 'compute-6-29', 'compute-6-28', 'compute-6-25', 'compute-6-24', 'compute-6-27', 'compute-6-26', 'compute-6-23', 'compute-9-33', 'compute-9-32', 'compute-22-17', 'compute-22-16', 'compute-22-15', 'compute-22-14', 'compute-22-13', 'compute-22-12', 'compute-22-11', 'compute-22-18', 'compute-7-39', 'compute-7-38', 'compute-21-29', 'compute-21-28', 'compute-21-27', 'compute-21-26', 'compute-21-25', 'compute-21-24', 'compute-21-23', 'compute-5-1', 'compute-14-1', 'compute-14-2', 'compute-14-3', 'compute-14-4', 'compute-14-6', 'compute-14-7', 'compute-14-8', 'compute-13-6', 'compute-13-5', 'compute-13-4', 'compute-7-40', 'compute-13-2', 'compute-13-1', 'compute-14-30', 'compute-5-8', 'compute-14-32', 'compute-14-33', 'compute-14-34', 'compute-14-35', 'compute-18-18', 'compute-14-37', 'compute-14-38', 'compute-18-17', 'compute-5-3', 'compute-18-15', 'compute-5-5', 'compute-18-13', 'compute-5-7', 'compute-5-6', 'compute-6-2', 'compute-3-41', 'compute-6-1', 'compute-6-6', 'compute-6-7', 'compute-6-4', 'compute-6-5', 'compute-6-8', 'compute-13-28', 'compute-13-29', 'compute-13-26', 'compute-13-27', 'compute-13-24', 'compute-13-25', 'compute-13-23', 'compute-2-10', 'compute-2-11', 'compute-2-12', 'compute-2-14', 'compute-2-15', 'compute-2-16', 'compute-2-17', 'compute-2-18', 'compute-14-40', 'compute-2-8', 'compute-2-9', 'compute-2-7', 'compute-20-40', 'compute-1-9', 'compute-1-8', 'compute-6-11', 'compute-8-40', 'compute-6-14', 'compute-6-15', 'compute-6-16', 'compute-6-17', 'compute-6-10', 'compute-6-12', 'compute-6-13', 'compute-6-18', 'compute-4-29', 'compute-4-28', 'compute-23-38', 'compute-22-2', 'compute-23-36', 'compute-23-37', 'compute-23-34', 'compute-23-35', 'compute-4-27', 'compute-23-33', 'compute-4-25', 'compute-11-18', 'compute-8-38', 'compute-8-39', 'compute-11-17', 'compute-11-16', 'compute-22-40', 'compute-1-11', 'compute-1-10', 'compute-1-13', 'compute-1-12', 'compute-1-15', 'compute-1-14', 'compute-1-17', 'compute-1-16', 'compute-5-15', 'compute-5-14', 'compute-12-8', 'compute-5-16', 'compute-5-11', 'compute-5-10', 'compute-5-13', 'compute-5-12', 'compute-12-2', 'compute-12-3', 'compute-12-1', 'compute-12-6', 'compute-12-7', 'compute-12-4', 'compute-12-5', 'compute-12-27', 'compute-12-26', 'compute-12-18', 'compute-19-37', 'compute-19-36', 'compute-12-10', 'compute-12-11', 'compute-12-12', 'compute-12-13', 'compute-12-14', 'compute-12-15', 'compute-12-16', 'compute-12-17', 'compute-20-37', 'compute-20-36', 'compute-20-35', 'compute-20-39', 'compute-20-38', 'compute-23-39', 'compute-23-32', 'compute-4-26', 'compute-23-30', 'compute-12-23', 'compute-12-22', 'compute-12-25', 'compute-12-24', 'compute-19-39', 'compute-19-38', 'compute-12-29', 'compute-12-28', 'compute-19-35', 'compute-9-27', 'compute-21-40', 'compute-9-28', 'compute-9-29', 'compute-11-40', 'compute-21-38', 'compute-21-39', 'compute-21-30', 'compute-21-33', 'compute-21-34', 'compute-21-35', 'compute-21-36', 'compute-21-37', 'compute-5-28', 'compute-5-29', 'compute-5-24', 'compute-5-25', 'compute-5-26', 'compute-5-27', 'compute-5-23', 'compute-13-18', 'compute-13-13', 'compute-13-12', 'compute-13-11', 'compute-13-10', 'compute-13-17', 'compute-13-16', 'compute-13-15', 'compute-13-14', 'compute-14-15', 'compute-22-39', 'compute-22-38', 'compute-22-30', 'compute-22-33', 'compute-22-35', 'compute-22-34', 'compute-22-37', 'compute-22-36', 'compute-7-17', 'compute-7-16', 'compute-7-18', 'compute-5-17', 'compute-14-18', 'compute-14-12', 'compute-14-13', 'compute-14-10', 'compute-14-11', 'compute-14-16', 'compute-14-17', 'compute-14-14', 'compute-5-18', 'compute-21-8', 'compute-21-1', 'compute-21-2', 'compute-21-3', 'compute-21-4', 'compute-21-5', 'compute-21-6', 'compute-21-7', 'compute-4-30', 'compute-18-14', 'compute-23-27', 'compute-23-26', 'compute-12-37', 'compute-12-38', 'compute-21-11', 'compute-5-39', 'compute-5-38', 'compute-5-37', 'compute-5-36', 'compute-5-35', 'compute-5-34', 'compute-5-33', 'compute-5-32', 'compute-5-30', 'compute-22-3', 'compute-18-35', 'compute-22-1', 'compute-18-37', 'compute-22-7', 'compute-22-6', 'compute-22-5', 'compute-22-4', 'compute-22-8', 'compute-18-38', 'compute-18-39', 'compute-20-15', 'compute-20-17', 'compute-20-16', 'compute-20-18', 'compute-9-25', 'compute-2-38', 'compute-2-39', 'compute-2-36', 'compute-2-37', 'compute-2-34', 'compute-2-35', 'compute-2-32', 'compute-2-33', 'compute-2-30', 'compute-6-32', 'compute-6-33', 'compute-6-30', 'compute-6-36', 'compute-6-37', 'compute-6-34', 'compute-6-35', 'compute-6-38', 'compute-6-39', 'compute-9-26', 'compute-21-12', 'compute-21-13', 'compute-23-16', 'compute-23-17', 'compute-21-16', 'compute-21-17', 'compute-21-14', 'compute-21-15', 'compute-21-18', 'compute-23-18', 'compute-8-18', 'compute-8-16', 'compute-8-17', 'compute-11-39', 'compute-11-38', 'compute-22-28', 'compute-22-29', 'compute-22-23', 'compute-22-26', 'compute-22-27', 'compute-22-24', 'compute-22-25', 'compute-13-8', 'compute-13-7', 'compute-19-13', 'compute-19-15', 'compute-19-14', 'compute-19-17', 'compute-19-16', 'compute-14-27', 'compute-14-26', 'compute-14-25', 'compute-14-24', 'compute-14-23', 'compute-14-36', 'compute-14-29', 'compute-14-28', 'compute-18-16', 'compute-14-39', 'compute-3-39', 'compute-3-38', 'compute-5-2', 'compute-13-39', 'compute-13-38', 'compute-13-35', 'compute-13-34', 'compute-13-37', 'compute-13-36', 'compute-13-30', 'compute-13-33', 'compute-13-32', 'compute-3-40', 'compute-6-3', 'compute-13-40', 'compute-18-36', 'compute-23-29', 'compute-23-28', 'compute-23-25', 'compute-4-32', 'compute-4-33', 'compute-4-34', 'compute-4-35', 'compute-19-40', 'compute-18-40'] for t1 in n_times: devices=t1['data'].keys() for d in machine: lst=[] lst.append(d) for x in xrange(0,39): lst.append(t1['data'][d][x][1]) data.append(lst) t=t+1 res=['Device','G_Swap_Total', 'G_Swap_Free', 'G_Swap_Used', 'G_Proc_Run', 'G_Cpu_User', 'G_Cpu_Wio', 'G_Load_One', 'G_Load', 'G_Five', 'G_Load_Fifteen', 'G_Mem_Cached', 'G_Mem_Total', 'T_State', 'T_Slots', 'T_SlotsUsed', 'T_AvailMem(MB)', 'T_TotalMem(MB)/Swap', 'T_Time_Last_Rec', 'T_LoadAve', 'T_NetLoad(MB)', 'N_Status', 'N_Swap_Service', 'N_Swap_State', 'N_Swap_Info', 'N_IPMI_Service', 'N_IPMI_State', 'N_IPMI_Info', 'N_FreeSpace_Service', 'N_FreeSpace_State', 'N_FreeSpace_Info', 'N_CVMFS-OSG_Service', 'N_CVMFS-OSG_State', 'N_CVMFS-OSG_Info', 'N_CVMFS-CERN_Service', 'N_CVMFS-CERN_State', 'N_CVMFS-CERN_Info', 'N_CVMFS-CONDB_Service', 'N_CVMFS-CONDB_State', 'N_CVMFS-CONDB_Info'] att=['G_Swap_Used','G_Cpu_User', 'G_Cpu_Wio', 'G_Load_One', 'G_Load', 'G_Five', 'G_Load_Fifteen', 'G_Mem_Cached', 'T_AvailMem(MB)', 'T_LoadAve', 'T_NetLoad(MB)'] new_att=['Device','G_Swap_Used', 'G_Proc_Run', 'G_Cpu_User', 'G_Cpu_Wio', 'G_Load_One', 'G_Load', 'G_Five', 'G_Load_Fifteen', 'G_Mem_Cached', 'T_State', 'T_Slots', 'T_SlotsUsed','T_AvailMem(MB)', 'T_Time_Last_Rec', 'T_LoadAve','N_Status', 'N_Swap_State','N_IPMI_State','N_IPMI_Info','N_FreeSpace_State', 'N_CVMFS-OSG_State', 'N_CVMFS-CERN_State', 'N_CVMFS-CONDB_State'] #new_att=['Device','G_Swap_Used','T_AvailMem(MB)','G_Five','G_Cpu_Wio'] if request_att !=[]: new_att=request_att print (request_att) new_index=[] full_index=[] for i in new_att: full_index.append(res.index(i)) for a in att: new_index.append(res.index(a)) new_data=[] for d in data: core_count=int(d[14]) if core_count!=0: for i in new_index: d[i]=round(float(d[i])/core_count,2) d[i]=unicode(d[i]) tmp=[] for i in full_index: if i==res.index('N_IPMI_Info'): code_in_IPMI=re.findall(r'\d+',str(d[i])) if code_in_IPMI==[]: d[i]='0' else: d[i]=code_in_IPMI[0] tmp.append(d[i]) # tmp=[d[i] for i in full_index] new_data.append(tmp) df=pd.DataFrame(new_data) df.columns=new_att X = df.ix[:,1:len(df.columns)].values y = df.ix[:,0].values from sklearn.preprocessing import StandardScaler X_std = StandardScaler().fit_transform(X) from sklearn.decomposition import PCA as sklearnPCA sklearn_pca = sklearnPCA(n_components=2) Y_sklearn = sklearn_pca.fit_transform(X_std) x_corr=[] y_corr=[] label=[] x_l=[] y_l=[] new_dim_data=dict() for lab in machine: x_fact = Y_sklearn[y==lab, 0].tolist() y_fact = Y_sklearn[y==lab, 1].tolist() new_dim_data[lab] =[x_fact,y_fact] x_l.append(x_fact) y_l.append(y_fact) # Store new dimensions in database post={"date":datetime.datetime.utcnow(),"data":new_dim_data} d_var=db_new_dim.data post_id=d_var.insert_one(post).inserted_id for x in x_l: for x1 in x: x_corr.append(x1) for y in y_l: for y1 in y: y_corr.append(y1) l=len(x_l[0]) for lab in machine: for i in [lab for x in range(0,l)]: label.append(i) new_arr=np.array(zip(x_corr,y_corr)) k_means=KMeans(n_clusters=4) k_means.fit(new_arr) centroid=k_means.cluster_centers_ labels=k_means.labels_ colors=["green","red","cyan","yellow","blue"] color_src=[] for i in range(len(x_corr)): color_src.append(colors[labels[i]]) #output_file("toolbar.html") TOOLS="resize,crosshair,pan,wheel_zoom,box_zoom,reset,tap,previewsave,box_select,poly_select,lasso_select" source = ColumnDataSource( data=dict( x=x_corr, y=y_corr, desc=label, # colors=color_src, ) ) hover = HoverTool( tooltips=""" <div> <div> <span style="font-size: 17px; font-weight: bold;">@desc</span> <span style="font-size: 15px; color: #966;">[$index]</span> </div> <div> <span style="font-size: 15px;">Location</span> <span style="font-size: 10px; color: #696;">($x, $y)</span> </div> </div> """ ) #TOOLS= [BoxZoomTool(), ResetTool(),hover,ResizeTool(),WheelZoomTool()] TOOLS=["pan,wheel_zoom,box_zoom,reset,resize",hover] p = figure(plot_width=600, plot_height=600, tools=TOOLS, title="Mouse over the dots") p.circle('x', 'y', size=30, source=source,fill_color=color_src) p.scatter(centroid[:,0],centroid[:,1], color='black')#,s=200,linewidths=5,zorder=10) js_resources = INLINE.render_js() css_resources = INLINE.render_css() # For more details see: # http://bokeh.pydata.org/en/latest/docs/user_guide/embedding.html#components script, div = components(p, INLINE) html = flask.render_template( 'cluster_snapshot.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, ) return encode_utf8(html)
def graphs_input(): if request.method == 'POST': topic = request.form['topic'] sentence = request.form['sentence'] if sentence != '': sentence = str(sentence) with open('../pickles/lda_model_' + identifier + '.pkl', 'rb') as f: lda_model = pickle.load(f) bow = lda_model.id2word.doc2bow(sentence.split(' ')) topics = lda_model[bow] topic = 0 prob = 0 for topic_and_prob in topics: temp_topic = topic_and_prob[0] temp_prob = topic_and_prob[1] if temp_prob > prob: topic = temp_topic prob = temp_prob topic += 1 else: topic = int(topic) script, div = get_components(topic=topic) with open('../pickles/topic_dict_' + identifier + '.pkl', 'rb') as f: topic_dict = pickle.load(f) dates = topic_dict[topic]['date_published'] df = pd.DataFrame({'date_published': pd.Series(dates)}) df = df[df['date_published'] > date(2017, 5, 18)] df = df['date_published'].dt.date.value_counts() max_date = df.index[np.argmax(df.values)] # script, div = make_bokeh_plot(topic_dict, topic) anger_tones = topic_dict[topic]['Anger'] disgust_tones = topic_dict[topic]['Disgust'] fear_tones = topic_dict[topic]['Fear'] joy_tones = topic_dict[topic]['Joy'] sadness_tones = topic_dict[topic]['Sadness'] analytical_score = topic_dict[topic]['Analytical'] tone_mean = [] for i in range(5): tone_mean.append(np.mean(anger_tones)) tone_mean.append(np.mean(disgust_tones)) tone_mean.append(np.mean(fear_tones)) tone_mean.append(np.mean(joy_tones)) tone_mean.append(np.mean(sadness_tones)) colors = ['red', 'green', 'purple', 'yellow', 'blue'] tone = ['Anger', 'Disgust', 'Fear', 'Joy', 'Sadness'] idx = np.argmax(tone_mean) js_resources = INLINE.render_js() css_resources = INLINE.render_css() html = render_template( 'graphs_input.html', js_resources=js_resources, css_resources=css_resources, script=script, div=div, topic_num=topic, word_cloud='src="../static/img/wordclouds/wordcloud_topic' + str(topic) + '_' + identifier + '.png"', mood_plot='src="../static/img/mood_plots/mood_by_site_plot_by_topic' + str(topic) + '_' + identifier + '.png"', pos_neg_plot= 'src="../static/img/pos_neg_plots/pos_neg_by_site_plot_by_topic' + str(topic) + '_' + identifier + '.png"', tone_mean="{0:.1f}".format(tone_mean[idx] * 100), tone=tone[idx], color='color="' + colors[idx] + '"', max_date=max_date, date_plot='src="../static/img/coverage_plots/coverage_plot_by_topic' + str(topic) + '_' + identifier + '.png"') return encode_utf8(html)
</div> <HR> <H1>2nd stage</H1> <div class="embed-wrapper"> {{ div2 }} </div> <HR> <H1>3rd stage</H1> <div class="embed-wrapper"> {{ div3 }} </div> </body> </html> ''') js_resources = INLINE.render_js() css_resources = INLINE.render_css() html = template.render(js_resources=js_resources, css_resources=css_resources, script1=script1, script2=script2, script3=script3, div1=div1, div2=div2, div3=div3) with io.open(filename, mode='w', encoding='utf-8') as f: f.write(html) view(filename)
def embedding(): from bokeh.plotting import figure from bokeh.resources import INLINE from bokeh.models import HoverTool, ColumnDataSource from bokeh.embed import components # from sklearn.manifold import Isomap articles = list(db.get_articles()) vect = vectorizer.load(config.vectorizer_filename) documents = map(article_to_document, articles) vects = vect.transform(documents) if issparse(vects): vects = vects.toarray() #E = PCA(n_components=2) E = make_pipeline( PCA(n_components=50), TSNE(n_components=2, perplexity=30, early_exaggeration=4, verbose=1) ) embed = E.fit_transform(vects) _, pca = E.steps[0] print(pca.explained_variance_ratio_) titles = map(lambda article: article.title, articles) titles = map(lambda title: to_multiline(title, max_line_length=30), titles) titles = map(lambda title: "".join(title), titles) links = [article.link for article in articles] authors = map(lambda article: article.authors, articles) authors = map(lambda author: [a.name for a in author], authors) authors = map(lambda author: ",".join(author), authors) authors = map(lambda author: to_multiline(author, max_line_length=30), authors) authors = map(lambda author: "".join(author)[0:40]+"[...]", authors) ds = ColumnDataSource( dict(x=embed[:, 0], y=embed[:, 1], title=titles, link=links, author=authors) ) tools = "resize, hover, save, pan,wheel_zoom,box_zoom,reset,resize" fig = figure(title="paper embedding", tools=tools, width=1400, height=800) fig.scatter("x", "y", source=ds, size=5, marker='cross') hover = fig.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([ ("title", "@title"), ("author", "@author"), ("link", "@link"), ]) app.logger.info(INLINE.__dict__.keys()) js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(fig, INLINE) html = render_template( 'figure.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, ) return html
def polynomial(): """ Very simple embedding of a polynomial chart """ # Grab the inputs arguments from the URL # This is automated by the button args = flask.request.args # Get all the form arguments in the url with defaults _source=getitem(args,'_source',url_default) _size=int(getitem(args,'size','5')) favcolor=getitem(args,'favcolor','#1F77B4') bcolor=getitem(args,'bcolor','#F5F5DC') _shape=getitem(args,'_shape','circle') lcolor=getitem(args,'lcolor','#FCB938') toggle=bool(getitem(args,'toggle','')) #Parsing json DEFAULT_TICKERS = ['TOAs','RawProfiles', 'Period', 'PeriodDerivative', 'DM', 'RMS', 'Binary'] TOOLS = "tap,resize,crosshair,pan,wheel_zoom,box_zoom,reset,box_select,lasso_select,previewsave,hover" Pulsar,TOAs,RawProfiles, Period, PeriodDerivative, DM, RMS, Binary=[],[],[],[],[],[],[],[] url_location=urllib.urlopen(_source) for item in ijson.items(url_location,"item"): Pulsar.append(str(item["Pulsar"])) TOAs.append(float(item["TOAs"])) RawProfiles.append(int(item["Raw Profiles"])) Period.append(float(item["Period"])) PeriodDerivative.append(float(item["Period Derivative"])) DM.append(str(item["DM"])) RMS.append(str(item["RMS"])) if (item["Binary"]=="Y"): Binary.append("Yes") else: Binary.append("No") #Create a plot periodvs period derivative s1 = ColumnDataSource( data=dict( Pulsar=Pulsar, TOAs=TOAs, RawProfiles=RawProfiles, Period=Period, PeriodDerivative=PeriodDerivative, DM=DM, RMS=RMS, Binary=Binary, ) ) p1 = figure(plot_width=600, plot_height=600, title="Period vs Period Derivative", y_axis_type="log" ,y_range=[min(PeriodDerivative)-min(PeriodDerivative)/5, max(PeriodDerivative)+max(PeriodDerivative)/5],x_range=[min(Period)-min(Period)/5, max(Period)+max(Period)/5],x_axis_label='Period[s]', y_axis_label='Period Derivative[s/s]',tools=TOOLS) p1.background_fill_color = bcolor p1.background_fill_alpha = "0.5" getattr(p1,_shape)('Period', 'PeriodDerivative', legend="period deri",color=favcolor,size=_size, source=s1) #p1.circle('Period', 'PeriodDerivative', legend="period deri",color=favcolor,size=_size, source=s1) p1.xaxis.axis_label_text_font_size = "15pt" p1.yaxis.axis_label_text_font_size = "15pt" #p1.xaxis[0].formatter = PrintfTickFormatter(format="s") #p1.yaxis[0].formatter = PrintfTickFormatter(format=" s/s") #Toggle Line #if getattr(p1,line,None): # getattr(p1,line)('Period', 'PeriodDerivative',legend="period deri",line_dash=[4, 4], line_color=lcolor, line_width=1,source=s1) p1.line('Period', 'PeriodDerivative',legend="period deri",line_dash=[4, 4], line_color=lcolor, line_width=1,source=s1,visible=toggle) # Custom data source for selected points s2 = ColumnDataSource( data=dict( Pulsar=[], TOAs=[], RawProfiles=[], Period=[], PeriodDerivative=[], DM=[], RMS=[], Binary=[], ) ) p2= figure(plot_width=600, plot_height=600, title=" Selected points from Period vs Period Derivative", y_axis_type="log" ,y_range=[min(PeriodDerivative)-min(PeriodDerivative)/10, max(PeriodDerivative)+max(PeriodDerivative)/10],x_range=[min(Period)-min(Period)/10, max(Period)+max(Period)/10],x_axis_label='Period[s]', y_axis_label='Period Derivative[s/s]',tools=TOOLS) p2.xaxis.axis_label_text_font_size = "15pt" p2.yaxis.axis_label_text_font_size = "15pt" p2.circle('Period', 'PeriodDerivative', legend="period deri",alpha=1.2, source=s2) s1.callback = CustomJS(args=dict(s2=s2), code=""" var inds = cb_obj.get('selected')['1d'].indices; var d1 = cb_obj.get('data'); var d2 = s2.get('data'); d2['Pulsar'] = [] d2['TOAs'] = [] d2['RawProfiles'] = [] d2['Period'] = [] d2['PeriodDerivative'] = [] d2['DM'] = [] d2['RMS'] = [] d2['Binary'] = [] for (i = 0; i < inds.length; i++) { d2['Pulsar'].push(d1['Pulsar'][inds[i]]) d2['TOAs'].push(d1['TOAs'][inds[i]]) d2['RawProfiles'].push(d1['RawProfiles'][inds[i]]) d2['Period'].push(d1['Period'][inds[i]]) d2['PeriodDerivative'].push(d1['PeriodDerivative'][inds[i]]) d2['DM'].push(d1['DM'][inds[i]]) d2['RMS'].push(d1['RMS'][inds[i]]) d2['Binary'].push(d1['Binary'][inds[i]]) } s2.trigger('change'); """ ) hover = p1.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([ ("Pulsar's Name", '@Pulsar'), ('TOAs', '@TOAs'), ('RawProfiles', '@RawProfiles'), ('Period[s]', '@Period'), ('PeriodDerivative[s/s]', '@PeriodDerivative'), ('DM[pc/cc]', '@DM'), ('RMS[us]', '@RMS'), ('Binary', '@Binary'), ]) js_resources = INLINE.render_js() css_resources = INLINE.render_css() #Setting up layout layout = hplot(p1, p2) script, div = components(layout,INLINE) html = flask.render_template( 'embed.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, _source=_source, Pulsar=Pulsar, _shape=_shape, favcolor=favcolor, bcolor=bcolor, _size=_size, lcolor=lcolor, toggle=toggle ) return encode_utf8(html)
def limb_darkening(): """The limb darkening form page. """ # Load default form form = fv.LimbDarkeningForm() # Reload page with stellar data from ExoMAST if form.resolve_submit.data: if form.targname.data.strip() != '': try: # Resolve the target in exoMAST form.targname.data = get_canonical_name(form.targname.data) data, target_url = get_target_data(form.targname.data) # Update the form data form.feh.data = data.get('Fe/H') form.teff.data = data.get('Teff') form.logg.data = data.get('stellar_gravity') form.target_url.data = str(target_url) except: form.target_url.data = '' form.targname.errors = [ "Sorry, could not resolve '{}' in exoMAST.".format( form.targname.data) ] # Send it back to the main page return render_template('limb_darkening.html', form=form) # Reload page with appropriate filter data if form.filter_submit.data: kwargs = {} if form.bandpass.data == 'tophat': kwargs['n_bins'] = 1 kwargs['pixels_per_bin'] = 100 kwargs['wave_min'] = 1 * u.um kwargs['wave_max'] = 2 * u.um # Get the filter bandpass = svo.Filter(form.bandpass.data, **kwargs) # Update the form data form.wave_min.data = bandpass.wave_min.value form.wave_max.data = bandpass.wave_max.value # Send it back to the main page return render_template('limb_darkening.html', form=form) # Update validation values after a model grid is selected if form.modelgrid_submit.data: # Load the modelgrid mg = ModelGrid(form.modeldir.data, resolution=500) teff_rng = mg.Teff_vals.min(), mg.Teff_vals.max() logg_rng = mg.logg_vals.min(), mg.logg_vals.max() feh_rng = mg.FeH_vals.min(), mg.FeH_vals.max() # Update the validation parameters by setting validator attributes setattr(form.teff.validators[1], 'min', teff_rng[0]) setattr(form.teff.validators[1], 'max', teff_rng[1]) setattr( form.teff.validators[1], 'message', 'Effective temperature must be between {} and {}'.format( *teff_rng)) setattr(form.logg.validators[1], 'min', logg_rng[0]) setattr(form.logg.validators[1], 'max', logg_rng[1]) setattr(form.logg.validators[1], 'message', 'Surface gravity must be between {} and {}'.format(*logg_rng)) setattr(form.feh.validators[1], 'min', feh_rng[0]) setattr(form.feh.validators[1], 'max', feh_rng[1]) setattr(form.feh.validators[1], 'message', 'Metallicity must be between {} and {}'.format(*feh_rng)) # Send it back to the main page return render_template('limb_darkening.html', form=form) # Validate form and submit for results if form.validate_on_submit() and form.calculate_submit.data: # Get the stellar parameters star_params = [ float(form.teff.data), float(form.logg.data), float(form.feh.data) ] # Log the form inputs try: log_exoctk.log_form_input(request.form, 'limb_darkening', DB) except: pass # Load the model grid model_grid = ModelGrid(form.modeldir.data, resolution=500) form.modeldir.data = [ j for i, j in form.modeldir.choices if i == form.modeldir.data ][0] # Grism details if '.G' in form.bandpass.data.upper( ) and 'GAIA' not in form.bandpass.data.upper(): kwargs = { 'n_bins': form.n_bins.data, 'pixels_per_bin': form.n_pix.data, 'wl_min': form.wave_min.data * u.um, 'wl_max': form.wave_max.data * u.um } else: kwargs = {} # Make filter object and plot bandpass = svo.Filter(form.bandpass.data, **kwargs) bp_name = bandpass.name bk_plot = bandpass.plot(draw=False) bk_plot.plot_width = 580 bk_plot.plot_height = 280 js_resources = INLINE.render_js() css_resources = INLINE.render_css() filt_script, filt_plot = components(bk_plot) # Trim the grid to nearby grid points to speed up calculation full_rng = [ model_grid.Teff_vals, model_grid.logg_vals, model_grid.FeH_vals ] trim_rng = find_closest(full_rng, star_params, n=1, values=True) # Calculate the coefficients for each profile ld = lf.LDC(model_grid) for prof in form.profiles.data: ld.calculate(*star_params, prof, mu_min=float(form.mu_min.data), bandpass=bandpass) # Draw a figure for each wavelength bin tabs = [] for wav in np.unique(ld.results['wave_eff']): # Plot it TOOLS = 'box_zoom, box_select, crosshair, reset, hover' fig = figure(tools=TOOLS, x_range=Range1d(0, 1), y_range=Range1d(0, 1), plot_width=800, plot_height=400) ld.plot(wave_eff=wav, fig=fig) # Plot formatting fig.legend.location = 'bottom_right' fig.xaxis.axis_label = 'mu' fig.yaxis.axis_label = 'Intensity' tabs.append(Panel(child=fig, title=str(wav))) final = Tabs(tabs=tabs) # Get HTML script, div = components(final) # Store the tables as a string file_as_string = str(ld.results[[ c for c in ld.results.dtype.names if ld.results.dtype[c] != object ]]) # Make a table for each profile with a row for each wavelength bin profile_tables = [] for profile in form.profiles.data: # Make LaTeX for polynomials latex = lf.ld_profile(profile, latex=True) poly = '\({}\)'.format(latex).replace('*', '\cdot').replace('\e', 'e') # Make the table into LaTeX table = filter_table(ld.results, profile=profile) co_cols = [ c for c in ld.results.colnames if (c.startswith('c') or c.startswith('e')) and len(c) == 2 and not np.all([np.isnan(i) for i in table[c]]) ] table = table[['wave_min', 'wave_max'] + co_cols] table.rename_column('wave_min', '\(\lambda_\mbox{min}\hspace{5px}(\mu m)\)') table.rename_column('wave_max', '\(\lambda_\mbox{max}\hspace{5px}(\mu m)\)') # Add the results to the lists html_table = '\n'.join(table.pformat(max_width=500, html=True))\ .replace('<table', '<table id="myTable" class="table table-striped table-hover"') # Add the table title header = '<br></br><strong>{}</strong><br><p>\(I(\mu)/I(\mu=1)\) = {}</p>'.format( profile, poly) html_table = header + html_table profile_tables.append(html_table) return render_template('limb_darkening_results.html', form=form, table=profile_tables, script=script, plot=div, file_as_string=repr(file_as_string), filt_plot=filt_plot, filt_script=filt_script, js=js_resources, css=css_resources) return render_template('limb_darkening.html', form=form)
def hello(): df = pd.read_csv('./TABLE_LOS.csv') df2 = pd.read_csv('./people.csv') SIZES = list(range(6, 22, 3)) COLORS = Spectral5 N_SIZES = len(SIZES) N_COLORS = len(COLORS) # data cleanup df.INSURANCE = df.INSURANCE.astype(str) df.RACE = df.RACE.astype(str) df.LANGUAGE = df.LANGUAGE.astype(str) df.MARITAL = df.MARITAL.astype(str) df2.INSURANCE = df2.INSURANCE.astype(str) df2.RACE = df2.RACE.astype(str) df2.LANGUAGE = df2.LANGUAGE.astype(str) df2.MARITAL = df2.MARITAL.astype(str) df['xPredict_INSURANCE'] = df2['INSURANCE'] df['xPredict_RACE'] = df2['RACE'] df['xPredict_LANGUAGE'] = df2['LANGUAGE'] df['xPredict_MARITAL'] = df2['MARITAL'] df['xPredict_LOS'] = df2['LOS'] del df['PATIENT_ID'] columns = sorted(df.columns) discrete = [x for x in columns if df[x].dtype == object] continuous = [x for x in columns if x not in discrete] def create_figure(): xs = df[x.value].values ys = df[y.value].values x_title = x.value.title() y_title = y.value.title() kw = dict() if x.value in discrete: kw['x_range'] = sorted(set(xs)) if y.value in discrete: kw['y_range'] = sorted(set(ys)) kw['title'] = "%s vs %s" % (x_title, y_title) p = figure(plot_height=600, plot_width=800, tools='pan,box_zoom,hover,reset', **kw) p.xaxis.axis_label = x_title p.yaxis.axis_label = y_title if x.value in discrete: p.xaxis.major_label_orientation = pd.np.pi / 4 sz = 9 if size.value != 'None': if len(set(df[size.value])) > N_SIZES: groups = pd.qcut(df[size.value].values, N_SIZES, duplicates='drop') else: groups = pd.Categorical(df[size.value]) sz = [SIZES[xx] for xx in groups.codes] c = "#680000" if color.value != 'None': if len(set(df[color.value])) > N_SIZES: groups = pd.qcut(df[color.value].values, N_COLORS, duplicates='drop') else: groups = pd.Categorical(df[color.value]) c = [COLORS[xx] for xx in groups.codes] p.circle(x=xs, y=ys, color=c, size=sz, line_color="black", alpha=0.6, hover_color='black', hover_alpha=0.5) return p def update(attr, old, new): layout.children[1] = create_figure() x = Select(title='X-Axis', value='INSURANCE', options=columns) x.on_change('value', update) y = Select(title='Y-Axis', value='RACE', options=columns) y.on_change('value', update) size = Select(title='Size', value='None', options=['None'] + continuous) size.on_change('value', update) color = Select(title='Color', value='None', options=['None'] + continuous) color.on_change('value', update) controls = column([x, y, color, size], width=200) layout = row(controls, create_figure()) curdoc().add_root(layout) curdoc().title = "LOS_Visualizer" curdoc().theme = Theme(filename="./theme.yaml") # layout1 = layout.children[1] # grab the static resources js_resources = INLINE.render_js() css_resources = INLINE.render_css() # render template script, div = components(layout) return render_template( 'index.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, )
def package_bokeh_figure(fig): js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(fig) return script, div, js_resources, css_resources
def correlation(): "the scatter plot" args = flask.request.args xattr = args.get("x", "atomic_number") yattr = args.get("y", "covalent_radius_pyykko") categ = args.get("categ", "name_series") data = get_data() properties = get_property_names(data) categories = get_category_names() fig = Figure(title="{} vs {}".format(properties[xattr], properties[yattr]), plot_width=PLOT_WIDTH, plot_height=PLOT_HEIGHT, tools="box_zoom,pan,resize,save,reset", toolbar_location="above", toolbar_sticky=False, ) fig.xaxis.axis_label = properties[xattr] fig.yaxis.axis_label = properties[yattr] ccm = get_color_mapper(categ, data) if categ == "None": legend = None color_dict = "#1F77B4" else: legend = categ color_dict = {"field": categ, "transform": ccm} fig.circle(x=xattr, y=yattr, fill_alpha=0.7, size=10, source=ColumnDataSource(data=data), fill_color=color_dict, line_color=color_dict, legend=legend) if categ != "None": fig.legend.location = (0, 0) fig.legend.plot = None fig.add_layout(fig.legend[0], "right") hover = HoverTool(tooltips=HOVER_TOOLTIPS) fig.add_tools(hover) script, div = components(fig) js_resources = INLINE.render_js() css_resources = INLINE.render_css() html = render_template( "correlations.html", plot_script=script, plot_div=div, properties=properties, categories=categories, xselected=xattr, yselected=yattr, catselected=categ, js_resources=js_resources, css_resources=css_resources, ) return encode_utf8(html)
def member_stats(): """ This function carries out knn, and all the other relevant statistics + graphs produced by this toool. Some optmixations should be carried out and the plots should be abstracted in to their own seperate methods """ # Panda parsers pandifycv2 = lambda x: pd.DataFrame(x.__dict__, index=[0])[mlp.cat_vec2] pandifynv2 = lambda x: list(pd.DataFrame(x.__dict__, index=[0])[mlp.num_vec2].iloc[0]) out = db.session.query(Product).join(Products).join(User).filter(User.email == session["user"]) num_per_vec = np.mean(map(pandifynv2, out.all()), axis=0) # Parsing the user data in to a feature vector for k, ind in enumerate(map(pandifycv2, out.all())): cat_per_vec = deepcopy(mlp.cat_matrix) for i,d in enumerate(dict(ind).values()): cat_per_vec[i][int(list(d)[0])] = 1 # Averaging out to project the person in to product space. per_vec = list(np.sum(cat_per_vec, axis=0)) + list(num_per_vec) # for plotting purposes xn= per_vec[-4] yn =per_vec[-3] # Taking a random slice of size 700 out of our products to use as training # for the recomender model rand = random.randrange(700,5400) out2 = db.session.query(Product).outerjoin(Products).filter(Products.email == None)[rand-700:rand] # Ser up variables for parsing the trainign set cat_train = deepcopy(mlp.cat_matrix) num_train = map(pandifynv2, out2) train = list() end = enumerate(map(pandifycv2, out2) ) # Cur dictionary which is later on used as a data structure for creating # plots that sumarize costs per Manufacturer. # This is slow. Another aditional thing that takes place here is the collapse # of the cathegorical variables in to a binary representation. cur = dict() for k, ind in end: cat_train = deepcopy(mlp.cat_matrix) for i,d in enumerate(dict(ind).values()): cat_train[i][int(list(d)[0])] = 1 if mlp.cat_dics["manu"][int(list(ind["manu"])[0])] in cur: cur[mlp.cat_dics["manu"][int(list(ind["manu"])[0])]].append(num_train[k][1]) else: cur[mlp.cat_dics["manu"][int(list(ind["manu"])[0])]] =[ num_train[k][1]] train.append(list(chain(*cat_train)) + num_train[k]) # K-neares neighbors object fitting on the training set (products from # data base) nbrs = NearestNeighbors(n_neighbors=5, algorithm='ball_tree').fit(train) # predicting on current user who are his 5 closest prducts distances, indices = nbrs.kneighbors(per_vec) # Formatting the prediction in to a useful html renderable form prediction = [(out2[ind].__dict__["name"], out2[ind].__dict__["price"], out2[ind].__dict__["url"]) for ind in indices[0]] # variable used by bokeh for tooltip lables labl = [out2[ind].__dict__["name"] for ind in range(len(out2))] # Yes this is a crime getitem = lambda obj, item, default: default if item not in obj else obj[item] # price dimension of the training set # max number of delivery days of the training set trainprice = np.array(train)[:,-4] trainmax = np.array(train)[:,-3] # Get all the form arguments in the url with defaults args = request.args _from = int(getitem(args, '_from', 0)) to = int(getitem(args, 'to', 10)) # color dict for space plot ahead colors = [ "#%02x%02x%02x" % (int(r), int(g), 150) for r, g in zip(50+2*trainprice, 30+2*trainmax) ] TOOLS="resize,hover,crosshair,pan,wheel_zoom,box_zoom,reset,box_select,lasso_select" # Data sources for the first plot source = ColumnDataSource( data=dict( x=trainprice, y=trainmax, label=labl ) ) source2 = ColumnDataSource( data=dict( x=[xn], y=[yn], label=["YOU"] ) ) fig = figure(title="Price vs Discount", tools=TOOLS, x_axis_label = "Price", y_axis_label = "Discount") fig.circle(x='x',y='y', radius=150, fill_color=colors, fill_alpha=0.6, line_color=None, source=source) fig.square(x='x',y='y', fill_color="yellow",size=20, line_color="green", source=source2) # fig.xgrid.grid_line_color = None fig.axis.major_tick_line_color = None fig.axis[0].ticker.num_minor_ticks = 0 fig.axis[1].ticker.num_minor_ticks = 0 fig.outline_line_color = "white" fig.xaxis.axis_line_color = "white" fig.yaxis.axis_line_color = "white" hover =fig.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([ ("label", "@label"), ]) # Generation of the second plot plt_list = list() for k, v in cur.items(): plt_list.append((k, np.mean(v), np.std(v))) # Sorce data for tthe second plot plt_list.sort(key=lambda x: x[1]) source3 = ColumnDataSource( data=dict( x=range(len(np.array(plt_list)[:,0])), y= np.array(plt_list)[:,1], label=np.array(plt_list)[:,0] ) ) xr = range(len(np.array(plt_list)[:,0])) + list(reversed(range(len(np.array(plt_list)[:,0])))) yr = list((np.array(plt_list)[:,1].astype(float))) + list(reversed(np.array(plt_list)[:,1].astype(float) + np.array(plt_list)[:,2].astype(float))) source4 = ColumnDataSource( data=dict( x=xr, y= yr, label=list(np.array(plt_list)[:,0])*2 ) ) yr2 = list((np.array(plt_list)[:,1].astype(float))) + list(reversed(np.array(plt_list)[:,1].astype(float) - np.array(plt_list)[:,2].astype(float))) source5 = ColumnDataSource( data=dict( x=xr, y= yr2, label=list(np.array(plt_list)[:,0])*2 ) ) fig2 = figure(title="Manufacturer and their average cost", tools=TOOLS, x_axis_label = "Manufacturer", y_axis_label = "Averag Price") # fig2.circle(x='x', y='y' , source=source3) fig2.patch(x='x', y='y', color="#99d8c9" , source=source4) fig2.patch(x='x', y='y', color="#99d8c9" , source=source5) fig2.line(x='x' , y='y' , source=source3) fig2.xgrid.grid_line_color = None fig2.axis[0].major_label_text_font_size = "0pt" fig2.axis.major_tick_line_color = None fig2.axis[0].ticker.num_minor_ticks = 0 fig2.axis[1].ticker.num_minor_ticks = 0 fig2.outline_line_color = "white" fig2.xaxis.axis_line_color = "white" fig2.yaxis.axis_line_color = "white" hover2 =fig2.select(dict(type=HoverTool)) hover2.tooltips = OrderedDict([ ("label", "@label"), ]) js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(fig, INLINE) script2, div2 = components(fig2, INLINE) return render_template('user/member_stats.html', plot_script=script, plot_div=div, plot_script2=script2, plot_div2=div2, js_resources=js_resources, css_resources=css_resources, _from=_from, to=to, prediction=prediction)
def map_ptsd(): # Grab the inputs arguments from the URL args = flask.request.args # Get all the form arguments in the url with defaults metric = getitem(args, 'metric', 'PTSD Rate') year = getitem(args, 'year', '2014') # connect to database eng = create_engine("mysql://%s:%s@localhost/va_open" % (os.getenv("MYSQL_USER"), os.getenv("MYSQL_PASS"))) # execute query with open('ptsd_rate_by_state.sql', 'r') as fid: ptsd_table = pd.read_sql_query(fid.read(), eng) # separate years p14 = ptsd_table.query('year == 2014').reset_index(drop=True) p15 = ptsd_table.query('year == 2015').reset_index(drop=True) col_add = ['total_served', 'total_ptsd'] if year == '2014': ptsd_table = p14 elif year == '2015': ptsd_table = p15 elif year == '2015+2014': ptsd_table = p15 ptsd_table[col_add] += p14[col_add] # get the state boundaries state_table = pd.DataFrame(us_states.data).T # merge tables ptsd_table = pd.merge(state_table, ptsd_table, how='inner', left_index=True, right_on='state') ptsd_table['ptsd_rate'] = (100 * ptsd_table.total_ptsd / ptsd_table.total_served) # create data source for map src = ColumnDataSource({ 'lons': ptsd_table['lons'].tolist(), 'lats': ptsd_table['lats'].tolist(), 'ptsd_rate': ptsd_table['ptsd_rate'], 'total_served': ptsd_table['total_served'], 'total_ptsd': ptsd_table['total_ptsd'], 'name': ptsd_table['name'] }) # generate color map cmap = LinearColorMapper(palette=palette) # create figure us_map = figure(width=1200, x_axis_location=None, y_axis_location=None, tools="hover", title="Rates of Post Traumatic Stress (2015)") us_map.grid.grid_line_color = None us_map.patches('lons', 'lats', source=src, line_color='black', fill_color={ 'field': metric, 'transform': cmap }) us_map.x_range = Range1d(-130, -60) us_map.y_range = Range1d(20, 55) # program hover tool hover = us_map.select_one(HoverTool) hover.tooltips = [("State", "@name"), ("Total Veterans Enrolled", "@total_served"), ("Total PTSD", "@total_ptsd (@ptsd_rate{int}%)")] js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(us_map) html = flask.render_template('embed.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, metric=metric, year=year) return encode_utf8(html)
def index(): """ Very simple embedding of a lightcurve chart """ # FLASK # Grab the inputs arguments from the URL # This is automated by the button args = flask.request.args _from = str(args.get('_from', str(DEFAULT_TR.start))) _to = str(args.get('_to', str(DEFAULT_TR.end))) tr = TimeRange(parse_time(_from), parse_time(_to)) if 'next' in args: tr = tr.next() if 'prev' in args: tr = tr.previous() if 'next_hour' in args: tr = TimeRange(tr.start + ONE_HOUR, tr.end + ONE_HOUR) if 'next_day' in args: tr = TimeRange(tr.start + ONE_DAY, tr.end + ONE_DAY) if 'prev_hour' in args: tr = TimeRange(tr.start - ONE_HOUR, tr.end - ONE_HOUR) if 'prev_day' in args: tr = TimeRange(tr.start - ONE_DAY, tr.end - ONE_DAY) _from = str(tr.start) _to = str(tr.end) # get the data goes = lc.GOESLightCurve.create(tr) # resample to reduce the number of points for debugging goes.data = goes.data.resample("1T").mean() # add time string for display of hover tool goes.data['time_str'] = goes.data.index.strftime('%F %H:%M:%S') source = ColumnDataSource(data=goes.data) source_static = ColumnDataSource(data=goes.data) # now create the bokeh plots # XRS-B Plot fig1 = figure(title="GOES", tools=TOOLS, plot_height=PLOT_HEIGHT, width=PLOT_WIDTH, x_axis_type='datetime', y_axis_type="log", y_range=(10**-9, 10**-2), toolbar_location="right") fig1.xaxis.formatter = formatter fig1.line('index', 'xrsb', source=source_static, color='red', line_width=2, legend="xrsa 1-8 Angstrom") fig2 = figure(title="GOES", tools=TOOLS, plot_height=PLOT_HEIGHT, width=PLOT_WIDTH, x_axis_type='datetime', y_axis_type="log", y_range=(10**-9, 10**-2)) fig2.xaxis.formatter = formatter fig2.line('index', 'xrsa', source=source_static, color='blue', line_width=2, legend="xrsa 0.5-4.0 Angstrom") # link the x-range for common panning fig2.x_range = fig1.x_range fig = Column(fig1, fig2) source_static.callback = CustomJS(code=""" var inds = cb_obj.selected['1d'].indices; var d1 = cb_obj.data; var m = 0; if (inds.length == 0) { return; } for (i = 0; i < inds.length; i++) { d1['color'][inds[i]] = "red" if (d1['y'][inds[i]] > m) { m = d1['y'][inds[i]] } } console.log(m); cb_obj.trigger('change'); """) hover = HoverTool() hover.tooltips = [("time", "@time_str"), ("xrsb", "@xrsb"), ("xrsa", "@xrsa")] fig1.add_tools(hover) hover2 = HoverTool() hover2.tooltips = [("time", "@time_str"), ("xrsb", "@xrsb"), ("xrsa", "@xrsa")] fig2.add_tools(hover2) # Configure resources to include BokehJS inline in the document. # For more details see: # http://bokeh.pydata.org/en/latest/docs/reference/resources_embedding.html#bokeh-embed js_resources = INLINE.render_js() css_resources = INLINE.render_css() # For more details see: # http://bokeh.pydata.org/en/latest/docs/user_guide/embedding.html#components script, div = components(fig, INLINE) html = flask.render_template( 'embed.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, _from=_from, _to=_to, ) return encode_utf8(html)
def show_simulation(): rounds = 0 try: if gtruncate_initial_rounds == 0: ignore_initial_rounds = int( session.get('ignore_initial_rounds', 50)) else: ignore_initial_rounds = 0 except NameError: ignore_initial_rounds = int(session.get('ignore_initial_rounds', 50)) gtruncate_initial_rounds = 0 plots = {} filenames = [] path = request.args.get('subdir') if path is None: path = newest_subdirectory('./result') try: with open(path + 'description.txt') as desc_file: desc = desc_file.read() except IOError: desc = '' plots = {} for filename in os.listdir(path): if not filename.endswith('.csv'): continue elif filename.startswith('#'): continue df = pd.read_csv(path + filename).ix[gtruncate_initial_rounds:] try: rounds = max(df['round']) except KeyError: rounds = max(df['index']) if ignore_initial_rounds >= rounds: ignore_initial_rounds = 0 print('kill') df = df.where((pd.notnull(df)), None) df.dropna(1, how='all', inplace=True) if filename.startswith('aggregate_'): plots.update( make_aggregate_graphs(df, filename, ignore_initial_rounds)) else: try: if max(df.get('id', [0])) == 0: plots.update( make_simple_graphs(df, filename, ignore_initial_rounds)) else: plots.update( make_panel_graphs(df, filename, ignore_initial_rounds)) except ValueError: print((filename, 'not displayable: ValueError')) script, div = components(plots) output = [] for idname_title, graph in div.items(): idname, title = json.loads(idname_title) output.append({ 'idname': idname, # can not stay i otherwise the cookie minimizing does not work 'title': title, 'graph': graph }) output.extend(load_text(path)) output.extend(load_text(path + '/../../')) output = sorted(output, key=lambda x: x['title']) return render_template('show_outcome.html', entries=output, desc=desc, setup=setup_dialog(rounds), script=script, js_resources=INLINE.render_js(), css_resources=INLINE.render_css())
async def get_stock_daily_amnountvol( stockcode: str, request: Request): # async加了就支持异步 把Request赋值给request stock_daily_df = get_data_df('daily_qfq_macd_' + stockcode) stock_daily_moneyflow_df = get_col_df('daily_moneyflow_' + stockcode) stock_daily_moneyflow_df['buy_elg_lg_amount_ratio'] = round( (stock_daily_moneyflow_df['buy_elg_amount'] + stock_daily_moneyflow_df['buy_lg_amount']) / (stock_daily_moneyflow_df['buy_sm_amount'] + stock_daily_moneyflow_df['buy_md_amount'] + stock_daily_moneyflow_df['buy_lg_amount'] + stock_daily_moneyflow_df['buy_elg_amount']), 2) stock_daily_moneyflow_df['buy_lg_amount_ratio'] = round( stock_daily_moneyflow_df['buy_lg_amount'] / (stock_daily_moneyflow_df['buy_sm_amount'] + stock_daily_moneyflow_df['buy_md_amount'] + stock_daily_moneyflow_df['buy_lg_amount'] + stock_daily_moneyflow_df['buy_elg_amount']), 2) stock_daily_df['amount_vol'] = round( stock_daily_df['amount'] / stock_daily_df['vol'], 3) stock_daily_dict = stock_daily_df.to_dict('records') js_res = INLINE.render_js() css_res = INLINE.render_css() p1 = draw_candlestick('daily_qfq_macd_' + stockcode) script1, div1 = components(p1) p2 = draw_line_df_param_n(stock_daily_df, 'close', 'red', 60) script2, div2 = components(p2) p0 = draw_line_df_param_n(stock_daily_df, 'amount', 'black', 60) script0, div0 = components(p0) p3 = draw_line_df_param_n_line(stock_daily_moneyflow_df, 'buy_elg_lg_amount_ratio', 'green', 60) script3, div3 = components(p3) p4 = draw_line_df_param_n_line(stock_daily_moneyflow_df, 'buy_lg_amount_ratio', 'blue', 60) script4, div4 = components(p4) p5 = draw_line_df_param_n(stock_daily_moneyflow_df, 'buy_lg_amount', 'red', 60) script5, div5 = components(p5) p6 = draw_line_df_param_n(stock_daily_moneyflow_df, 'buy_elg_amount', 'black', 60) script6, div6 = components(p6) #p4 = draw_line_df_param_n(stock_daily_df,'amount_vol','blue',60) #script4,div4 = components(p4) return tmp.TemplateResponse( 'stock_daily_amount_vol.html', { 'request': request, # 一定要返回request 'stockcode': stockcode, 'stockdailycount': len(stock_daily_dict), 'stockdaily': stock_daily_dict, "p_script0": script0, "p_div0": div0, "p_script1": script1, "p_div1": div1, "p_script2": script2, "p_div2": div2, "p_script3": script3, "p_div3": div3, "p_script4": script4, "p_div4": div4, "p_script5": script5, "p_div5": div5, "p_script6": script6, "p_div6": div6, "js_res": js_res, "css_res": css_res })
def make_plot(df, corp, color_palette): palette_list = [ palettes.viridis(100), palettes.inferno(100), palettes.magma(100), palettes.plasma(100), ] colors = list(reversed(palette_list[color_palette])) mapper = LinearColorMapper(palette=colors, low=0, high=100) TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom" TOOLTIPS = """ <div> <div> <span style="font-size: 20px; font-weight: bold;">score: @c%</span> </div> <div> <span style="font-size: 16px; font-style: italic;">@a & @b</span> </div> </div> """ # tooltips=[('score','@c%'), ('doc_1', '@a'), ('doc_2', '@b')]) hm = figure( x_range=corp, y_range=list(reversed(corp)), x_axis_location="above", plot_width=900, plot_height=900, tools=TOOLS, toolbar_location="below", tooltips=TOOLTIPS, ) hm.grid.grid_line_color = None hm.axis.axis_line_color = None hm.axis.major_tick_line_color = None hm.axis.major_label_text_font_size = "8pt" hm.axis.major_label_standoff = 0 hm.xaxis.major_label_orientation = pi / 3 hm.rect( x="a", y="b", source=df, width=1, height=1, line_color="#ffffff", fill_color={ "field": "c", "transform": mapper }, ) color_bar = ColorBar( color_mapper=mapper, formatter=PrintfTickFormatter(format="%d%%"), major_label_text_font_size="10pt", label_standoff=10, border_line_color=None, location=(0, 0), ) hm.add_layout(color_bar, "right") js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(hm) return js_resources, css_resources, script, div
def ngramgraph(): pwidth = 550 pheight = 400 # Grab the inputs arguments from the URL # This is automated by the button args = request.args # Get all the form arguments in the url with defaults #default to obama query = getitem(args, 'input', 'obama') #splittign the query splitstr = query.split(",") #Fetching data from the query XX = [] YY = [] YY2 = [] lgnd = [] cnt = 0 source = [] for strs in splitstr: ngram = strs.rstrip().lstrip().lower() #print "ngram = ", ngram subreddits = get_subreddit_data(ngram) #print "subreddits", subreddits if len(subreddits) > 0: mainsr = subreddits[0]['subreddit'] lgndstr = "{0}::{1}".format(ngram, mainsr) data = get_timeseries_data(ngram, mainsr) x = [ k['date'] for k in data] y = [k['count'] for k in data] y2 = [k['percentage'] for k in data] else: x = Xtime y = [0] * len(Xtime) y2 = y lgndstr = ngram lgnd.append(lgndstr) XX.append(x) YY.append(y) YY2.append(y2) cnt +=1 source.append(dict(xx = [datetime.strftime(kk, "%Y-%m") for kk in x], yy = y, desc = [lgndstr] * len(x))) #Creating figure box fig = figure(title="Trends (absolute count)", x_axis_label = "Time", y_axis_label = "Word Count", width=pwidth, height=pheight, x_axis_type="datetime" ) #Plot the lines for k in range(cnt): fig.line(XX[k], YY[k], color=random.choice(colors), legend = lgnd[k], line_width=2) fig.legend.orientation = "top_left" fig2 = figure(title="Trends (ratio)", x_axis_label = "Time", y_axis_label = "Ratio", width=pwidth, height=pheight, x_axis_type="datetime" ) #Plot the lines for k in range(cnt): fig2.line(XX[k], YY2[k], color=random.choice(colors), legend = lgnd[k], line_width=2) fig2.legend.location = "top_left" p = hplot(fig, fig2) # Configure resources to include BokehJS inline in the document. # For more details see: js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components(p, INLINE) #script2, div2 = components(fig2, INLINE) html = render_template( 'Ngram.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources ) return encode_utf8(html)
def predict(): if request.method == 'POST': url = str(request.form['url']) worked, result = get_article(url) if worked: (article_text, headline, author, date_published) = result summary = get_summary(article_text) data = {'article_text': article_text, 'headline': headline} df = pd.DataFrame(data, index=[0]) topic_texts, sentiment_texts, quote_texts, tweet_texts = process_articles( df) with open('../pickles/lda_model_' + identifier + '.pkl', 'rb') as f: lda_model = pickle.load(f) pos, neg, obj = get_article_sentiment(lda_model, sentiment_texts) score = (pos + neg) * (1 - obj) article_bow = lda_model.id2word.doc2bow(topic_texts[0]) article_topics = lda_model[article_bow] topic = 0 prob = 0 for topic_and_prob in article_topics: temp_topic = topic_and_prob[0] temp_prob = topic_and_prob[1] if temp_prob > prob: topic = temp_topic prob = temp_prob topic += 1 with open('../pickles/topic_dict_' + identifier + '.pkl', 'rb') as f: topic_dict = pickle.load(f) analytical = -1 name = uuid.uuid4() try: json_response_sentiment = tone_analyzer.tone(text=' '.join( sentiment_texts[0]), sentences=False) tones = parse_toneanalyzer_response(json_response_sentiment) make_mood_plot(tones, str(name)) analytical = tones[1]['Analytical'] script, div = make_bokeh_plot(topic_dict, topic, new_article=[score, analytical]) except Exception as e: print(e) script, div = get_components(topic=topic) pos_all = [] neg_all = [] obj_all = [] score_all = [] analytical_all = [] for pos_score, neg_score, obj_score, analytical_score in zip( topic_dict[topic]['pos'], topic_dict[topic]['neg'], topic_dict[topic]['obj'], topic_dict[topic]['Analytical']): pos_all.append(pos_score) neg_all.append(neg_score) obj_all.append(obj_score) score_all.append((pos_score + neg_score) * (1 - obj_score)) analytical_all.append(analytical_score) pos_mean, neg_mean, obj_mean, score_mean, analytical_mean = np.mean( pos_all), np.mean(neg_all), np.mean(obj_all), np.mean( score_all), np.mean(analytical_all) anger_tones = topic_dict[topic]['Anger'] disgust_tones = topic_dict[topic]['Disgust'] fear_tones = topic_dict[topic]['Fear'] joy_tones = topic_dict[topic]['Joy'] sadness_tones = topic_dict[topic]['Sadness'] analytical_score = topic_dict[topic]['Analytical'] tone_mean = [] for i in range(5): tone_mean.append(np.mean(anger_tones)) tone_mean.append(np.mean(disgust_tones)) tone_mean.append(np.mean(fear_tones)) tone_mean.append(np.mean(joy_tones)) tone_mean.append(np.mean(sadness_tones)) colors = ['red', 'green', 'purple', 'yellow', 'blue'] tone = ['Anger', 'Disgust', 'Fear', 'Joy', 'Sadness'] idx = np.argmax(tone_mean) my_tone = [ tones[0]['Anger'], tones[0]['Disgust'], tones[0]['Fear'], tones[0]['Joy'], tones[0]['Sadness'] ] my_idx = np.argmax(my_tone) js_resources = INLINE.render_js() css_resources = INLINE.render_css() return render_template( 'prediction_worked.html', article_text=article_text, headline=headline, author=author, date_published=date_published, summary=summary, pos="{0:.3f}".format(pos), neg="{0:.3f}".format(neg), obj="{0:.3f}".format(obj), score="{0:.3f}".format(score), analytical="{0:.3f}".format(analytical), pos_mean="{0:.3f}".format(pos_mean), neg_mean="{0:.3f}".format(neg_mean), obj_mean="{0:.3f}".format(obj_mean), score_mean="{0:.3f}".format(score_mean), analytical_mean="{0:.3f}".format(analytical_mean), topic=topic, topic_prob="{0:.3f}".format(prob * 100), tone_mean="{0:.1f}".format(tone_mean[idx] * 100), tone=tone[idx], color='color="' + colors[idx] + '"', my_tone_mean="{0:.1f}".format(my_tone[my_idx] * 100), my_tone=tone[my_idx], my_color='color="' + colors[my_idx] + '"', js_resources=js_resources, css_resources=css_resources, script=script, div=div, mood_plot='src="static/img/mood_plots/predicted_article_' + str(name) + '.png"') else: return render_template('prediction_failed.html')
def server(): html_components = [[], []] # components container js_resources = INLINE.render_js() # recources for bokeh plot css_resources = INLINE.render_css() try: # get the stuff to display on Webside script1, div1 = components( get_api( "CPU - Usage in %", "cpu", [100, 0], "red", "", 2000, "server", )) script2, div2 = components( get_api( "Memory - Usage in %", "memory", [100, 0], "blue", "", 2000, "server", )) script3, div3 = components( get_api( "Disk - Usage in %", "disk", [100, 0], "blue", "", 2000, "server", )) except Exception as e: flask_log( "SERVER", "Could get Server data! Error: " + str(e), "yellow", "404", "Serverinformationen konnten nicht geladen werden!", "danger", "home", ) # fill container html_components[0].extend([script1, script2, script3]) html_components[1].extend([div1, div2, div3]) # get logs lines = [] with open(logpath, "r") as f: for line in f: lines.append(line) return render_template( "server.html", outlines=lines[-20:], plot_script=html_components[0], plot_div=html_components[1], js_resources=js_resources, css_resources=css_resources, )
def index(): reload(sys) #解决中文编码 e_location = "127.0.0.1:9203" e_index = "logstash-*" analysis =action.LogAnalysis(e_location, e_index) c = pycurl.Curl() buf =BytesIO() c.setopt(c.URL, 'http://' + e_location + '/' + e_index + '/_search') c.setopt(pycurl.CUSTOMREQUEST,"GET") c.setopt(c.WRITEFUNCTION, buf.write) c.perform() results = buf.getvalue() results = json.loads(results.decode('utf-8')) c.close if results["hits"]["total"] == 0: flash('you have got no data!') return render_template('auth/upload.html') else: #bar1 actionlist = analysis.actionAgg("action") df=pd.DataFrame(actionlist) bar1 = Bar(df, label='action', values='size', agg='max', color="green", title="sshd-invalid-passwd_IP", plot_width=600, plot_height=322, legend=False) script, div = components(bar1) ''' #dount label,value,res = analysis.USERAgg("action") data = pd.Series(value, index = label) pie_chart = Donut(data, plot_width=400, plot_height=300) script2, div2 = components(pie_chart) ''' #user_ip useriplist = analysis.USERIPAgg("action") a = list() for i in useriplist: data = pd.Series(i['value'], index = i['ip']) pie_chart = Donut(data, title = i['user'] +'\n'+ ",IP總數:" + str(len(i['value'])), plot_width=190, plot_height=190) a.append(pie_chart) b=[] for i in range(0,len(a),5): b.append(a[i:i+5]) p = gridplot(b) script3, div3 = components(p) #INLINE_config js_resources = INLINE.render_js() css_resources = INLINE.render_css() #piechart labels,values,res = analysis.USERAgg("action") colors = [] for i in labels: colors.append("#%06x" % random.randint(0, 0xFFFFFF)) #colors = [ "#F7464A", "#46BFBD", "#FDB45C", "#FEDCBA","#ABCDEF", "#DDDDDD", "#ABCABC", "#AABEEF", "#EFAAED", "#bebece"] count = 0 for a in res: a["color"]=colors[count] if count<9: count+=1 #TableAgg Usertable = analysis.TableAgg("USERNAME","SUPERUSER") return render_template('index.html', plot_script=script, plot_div=div, pies_script=script3, pies_div=div3, js_resources=js_resources, css_resources=css_resources, set=zip(values, labels, colors), res=res, Usertable = Usertable )
def alaska(candidate): if candidate == '': candidate = 'All' else: candidate = candidate cands = db.get_candidates() max_sent, min_sent = db.get_scaled_sent() shapefile = app.config["PATH"] gdf = gpd.read_file(shapefile) df_states = db.get_states() gdf = gdf.merge(df_states, left_on='NAME', right_on='state') gdf_us = gdf[['abbr', 'state', 'geometry']].copy() gdf_us.columns = ['abbr', 'name', 'geometry'] gdf_us = gdf_us[gdf_us['abbr'].isin(['AK'])] print(gdf_us) last_update, tweets, df = db.get_candidate_data(candidate, 0) df['sent'] = round(df['sent'] * 1, 2) merged = gdf_us.merge(df, how='left', left_on='abbr', right_on='state') merged = merged[merged['user_count'] > 0].copy() merged_json = json.loads(merged.to_json()) json_data = json.dumps(merged_json) geosource = GeoJSONDataSource(geojson=json_data) palette = brewer['RdBu'][8] palette = palette[::-1] bar_range = max(abs(max_sent), abs(min_sent)) color_mapper = LinearColorMapper(palette=palette, low=-bar_range, high=bar_range) color_bar = ColorBar(color_mapper=color_mapper, label_standoff=8, width=20, border_line_color='black', location=(0, 0)) p = figure(title=None, plot_height=550, plot_width=1000, toolbar_location=None) p.xgrid.grid_line_color = None p.ygrid.grid_line_color = None p.add_tools( HoverTool(tooltips=[ ("State", "@name"), ("#Tweeters", "@user_count"), ("Sentiment", "@sent"), ])) p.axis.visible = False p.add_layout(color_bar, 'right') p.patches('xs', 'ys', source=geosource, fill_color={ 'field': 'sent', 'transform': color_mapper }, line_color='black', line_width=0.25, fill_alpha=.75) js_resources = INLINE.render_js() css_resources = INLINE.render_css() layout = column(p) script, div = components(layout) html = render_template('map.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, last_update=last_update, tweets=tweets, selected_cand=candidate, cands=cands) return encode_utf8(html)
def contam_visibility(): """The contamination and visibility form page""" # Load default form form = fv.ContamVisForm() form.calculate_contam_submit.disabled = False if request.method == 'GET': # http://0.0.0.0:5000/contam_visibility?ra=24.354208334287005&dec=-45.677930555343636&target=WASP-18%20b target_name = request.args.get('target') form.targname.data = target_name ra = request.args.get('ra') form.ra.data = ra dec = request.args.get('dec') form.dec.data = dec return render_template('contam_visibility.html', form=form) # Reload page with stellar data from ExoMAST if form.resolve_submit.data: if form.targname.data.strip() != '': # Resolve the target in exoMAST try: form.targname.data = get_canonical_name(form.targname.data) data, url = get_target_data(form.targname.data) # Update the coordinates ra_deg = data.get('RA') dec_deg = data.get('DEC') # Set the form values form.ra.data = ra_deg form.dec.data = dec_deg form.target_url.data = url except Exception: form.target_url.data = '' form.targname.errors = [ "Sorry, could not resolve '{}' in exoMAST.".format( form.targname.data) ] # Send it back to the main page return render_template('contam_visibility.html', form=form) # Reload page with appropriate mode data if form.mode_submit.data: # Update the button if (form.inst.data == 'MIRI') or (form.inst.data == 'NIRSpec'): form.calculate_contam_submit.disabled = True else: form.calculate_contam_submit.disabled = False # Send it back to the main page return render_template('contam_visibility.html', form=form) if form.validate_on_submit() and (form.calculate_submit.data or form.calculate_contam_submit.data): try: # Log the form inputs try: log_exoctk.log_form_input(request.form, 'contam_visibility', DB) except Exception: pass # Make plot title = form.targname.data or ', '.join( [str(form.ra.data), str(form.dec.data)]) pG, pB, dates, vis_plot, table, badPAs = vpa.using_gtvt( str(form.ra.data), str(form.dec.data), form.inst.data.split(' ')[0], targetName=str(title)) # Make output table fh = StringIO() table.write(fh, format='csv', delimiter=',') visib_table = fh.getvalue() # Get scripts vis_js = INLINE.render_js() vis_css = INLINE.render_css() vis_script, vis_div = components(vis_plot) # Contamination plot too if form.calculate_contam_submit.data: # First convert ra and dec to HH:MM:SS ra_deg, dec_deg = float(form.ra.data), float(form.dec.data) sc = SkyCoord(ra_deg, dec_deg, unit='deg') ra_dec = sc.to_string('hmsdms') ra_hms, dec_dms = ra_dec.split(' ')[0], ra_dec.split(' ')[1] # Make field simulation contam_cube = fs.fieldSim(ra_hms, dec_dms, form.inst.data, binComp=form.companion.data) contam_plot = cf.contam( contam_cube, form.inst.data, targetName=str(title), paRange=[int(form.pa_min.data), int(form.pa_max.data)], badPAs=badPAs, fig='bokeh') # Get scripts contam_js = INLINE.render_js() contam_css = INLINE.render_css() contam_script, contam_div = components(contam_plot) else: contam_script = contam_div = contam_js = contam_css = '' return render_template('contam_visibility_results.html', form=form, vis_plot=vis_div, vis_table=visib_table, vis_script=vis_script, vis_js=vis_js, vis_css=vis_css, contam_plot=contam_div, contam_script=contam_script, contam_js=contam_js, contam_css=contam_css) except Exception as e: err = 'The following error occurred: ' + str(e) return render_template('groups_integrations_error.html', err=err) return render_template('contam_visibility.html', form=form)
def index(): df, last_update = db.get_grid_data_db() df['jpeg'] = 'static/images/' + df['jpeg'] max_sent = abs(df['avg_sent']).max() df['avg_sent'] = df['avg_sent'] / max_sent cands = db.get_candidates() xdr = Range1d(start=-0, end=df['user_count'].max() + 1000) ydr = Range1d(start=-1.2, end=1.2) source = ColumnDataSource(df) p = figure(plot_height=600, plot_width=800, x_range=xdr, y_range=ydr, toolbar_location=None) #p = figure(sizing_mode="scale_both", x_range=xdr, y_range=ydr, toolbar_location=None) p.circle(x="user_count", y="avg_sent", size=50, source=source, alpha=.05) image = ImageURL(url="jpeg", x="user_count", y="avg_sent", w=None, h=None, anchor="center", global_alpha=.75) p.add_glyph(source, image) p.add_tools( HoverTool(tooltips=[ ("Candidate", "@fullname"), ("Sentiment", "@avg_sent"), ("#Tweeters", "@user_count"), ])) p.toolbar.active_drag = None p.sizing_mode = 'scale_width' xaxis = LinearAxis() p.xaxis.fixed_location = 0 p.xaxis.axis_line_width = 2 p.xaxis.axis_label = 'Number of Tweeters' p.xaxis.axis_label_text_font_style = 'bold' p.xaxis.axis_label_text_font_size = '12pt' p.xaxis.major_label_text_font_style = 'bold' p.xaxis.major_label_text_font_size = '12pt' label = Label(x=df['user_count'].max() / 2, y=-.2, text='Number of Tweeters', level='glyph', render_mode='canvas', text_font_style='bold', text_font_size='12pt') yaxis = LinearAxis() p.yaxis.axis_line_width = 2 p.yaxis.axis_label = 'Sentiment' p.yaxis.axis_label_text_font_style = 'bold' p.yaxis.axis_label_text_font_size = '12pt' p.yaxis.major_label_text_font_style = 'bold' p.yaxis.major_label_text_font_size = '12pt' p.add_layout(Grid(dimension=0, ticker=xaxis.ticker)) p.add_layout(Grid(dimension=1, ticker=yaxis.ticker)) p.add_layout(label) js_resources = INLINE.render_js() css_resources = INLINE.render_css() layout = column(p) script, div = components(layout) html = render_template('main.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, last_update=last_update, cands=cands) return encode_utf8(html)
def contam_visibility(): """The contamination and visibility form page""" # Load default form form = fv.ContamVisForm() form.calculate_contam_submit.disabled = False if request.method == 'GET': # http://0.0.0.0:5000/contam_visibility?ra=24.354208334287005&dec=-45.677930555343636&target=WASP-18%20b target_name = request.args.get('target') form.targname.data = target_name ra = request.args.get('ra') form.ra.data = ra dec = request.args.get('dec') form.dec.data = dec return render_template('contam_visibility.html', form=form) # Reload page with stellar data from ExoMAST if form.resolve_submit.data: if form.targname.data.strip() != '': # Resolve the target in exoMAST try: form.targname.data = get_canonical_name(form.targname.data) data, url = get_target_data(form.targname.data) # Update the coordinates ra_deg = data.get('RA') dec_deg = data.get('DEC') # Set the form values form.ra.data = ra_deg form.dec.data = dec_deg form.target_url.data = url except: form.target_url.data = '' form.targname.errors = [ "Sorry, could not resolve '{}' in exoMAST.".format( form.targname.data) ] # Send it back to the main page return render_template('contam_visibility.html', form=form) # Reload page with appropriate mode data if form.mode_submit.data: # Update the button if form.inst.data != 'NIRISS': form.calculate_contam_submit.disabled = True else: form.calculate_contam_submit.disabled = False # Send it back to the main page return render_template('contam_visibility.html', form=form) if form.validate_on_submit() and (form.calculate_submit.data or form.calculate_contam_submit.data): try: # Log the form inputs try: log_exoctk.log_form_input(request.form, 'contam_visibility', DB) except: pass # Make plot title = form.targname.data or ', '.join( [form.ra.data, form.dec.data]) pG, pB, dates, vis_plot, table = vpa.using_gtvt( str(form.ra.data), str(form.dec.data), form.inst.data.split(' ')[0]) # Make output table vers = '0.3' today = datetime.datetime.now() fh = StringIO() fh.write('# Hi! This is your Visibility output file for... \n') fh.write('# Target: {} \n'.format(form.targname.data)) fh.write('# Instrument: {} \n'.format(form.inst.data)) fh.write('# \n') fh.write( '# This file was generated using ExoCTK v{} on {} \n'.format( vers, today)) fh.write('# Visit our GitHub: https://github.com/ExoCTK/exoctk \n') fh.write('# \n') table.write(fh, format='csv', delimiter=',') visib_table = fh.getvalue() # Format x axis day0 = datetime.date(2019, 6, 1) dtm = datetime.timedelta(days=367) #vis_plot.x_range = Range1d(day0, day0 + dtm) # TODO: Fix this so bad PAs are included pB = [] # Make plot TOOLS = 'crosshair, reset, hover, save' fig = figure(tools=TOOLS, plot_width=800, plot_height=400, x_axis_type='datetime', title=title) fh = StringIO() table.write(fh, format='ascii') visib_table = fh.getvalue() # Format x axis day0 = datetime.date(2019, 6, 1) dtm = datetime.timedelta(days=367) # Get scripts vis_js = INLINE.render_js() vis_css = INLINE.render_css() vis_script, vis_div = components(vis_plot) # Contamination plot too if form.calculate_contam_submit.data: # First convert ra and dec to HH:MM:SS ra_deg, dec_deg = float(form.ra.data), float(form.dec.data) sc = SkyCoord(ra_deg, dec_deg, unit='deg') ra_dec = sc.to_string('hmsdms') ra_hms, dec_dms = ra_dec.split(' ')[0], ra_dec.split(' ')[1] # Make field simulation contam_cube = fs.sossFieldSim(ra_hms, dec_dms, binComp=form.companion.data) contam_plot = cf.contam( contam_cube, title, paRange=[int(form.pa_min.data), int(form.pa_max.data)], badPA=pB, fig='bokeh') # Get scripts contam_js = INLINE.render_js() contam_css = INLINE.render_css() contam_script, contam_div = components(contam_plot) else: contam_script = contam_div = contam_js = contam_css = '' return render_template('contam_visibility_results.html', form=form, vis_plot=vis_div, vis_table=visib_table, vis_script=vis_script, vis_js=vis_js, vis_css=vis_css, contam_plot=contam_div, contam_script=contam_script, contam_js=contam_js, contam_css=contam_css) except IOError: #Exception as e: err = 'The following error occurred: ' + str(e) return render_template('groups_integrations_error.html', err=err) return render_template('contam_visibility.html', form=form)
def render_main_page(self, portability, plus = None): """ Renders the main page template for a plot and association expression information: gene/transcript list, annotations, (GO/KEGG) enrichment results. """ # TODO: make css customizing accessible csstables = ''' <style type="text/css"> .etables { dir: ltr; width: 1200px; } </style> ''' # Hardcoded, but (for the time being) it is for the best. # NOTE: if changed in the future, remember to keep paths relative. if portability == "batch": css_resources = '<link rel="stylesheet" href="static/bokeh-0.11.1.min.css" type="text/css" />' js_resources = ''' <script type="text/javascript" src="static/bokeh-0.11.1.min.js"></script> <script type="text/javascript"> Bokeh.set_log_level("info"); </script> ''' elif portability == "web": css_resources = ''' <link rel="stylesheet" href="https://cdn.pydata.org/bokeh/release/bokeh-0.11.1.min.css" type="text/css" />'''+csstables js_resources = ''' <script type="text/javascript" src="https://cdn.pydata.org/bokeh/release/bokeh-0.11.1.min.js"></script> <script type="text/javascript"> Bokeh.set_log_level("info"); </script> ''' # or "full", meaning full portability, but at a cost of increased filesize else: js_resources = INLINE.render_js() css_resources = INLINE.render_css() # ---------------------------------------------------------------------------- if plus is not None: bp = plus['bp'] mf = plus['mf'] cc = plus['cc'] kegg = plus['kegg'] # a small hack to pass along the significance # level (filter for GO enrichments) alpha = plus['alpha'] else: bp = None mf = None cc = None kegg = None alpha = 0.05 t_bp, t_mf, t_cc, t_kegg = self.process_enrichment_dict(bp, mf, cc, kegg, alpha) if bp is not None or mf is not None or cc is not None: goheader = '''<p style="font-size:20px; font-weight: bold;">GO term enrichment</p>''' else: goheader = "" if bp is not None: bp_info = ''' <span style="font-size:16px; font-weight: bold;">Biological Process</span> {% block table1a %} {{ tablegobp }} {% endblock %} <br/> ''' else: bp_info = "" if mf is not None: mf_info = ''' <span style="font-size:16px; font-weight: bold;">Molecular Function</span> {% block table1b %} {{ tablegomf }} {% endblock %} <br/> ''' else: mf_info = "" if cc is not None: cc_info = ''' <span style="font-size:16px; font-weight: bold;">Cellular Component</span> {% block table1c %} {{ tablegocc }} {% endblock %} <br/> <br/> ''' else: cc_info = "" if kegg is not None: kegg_info = ''' <span style="font-size:20px; font-weight: bold;">KEGG pathways enrichment</span> {% block table2 %} {{ tablekegg }} {% endblock %} <br/> ''' else: kegg_info = "" # ---------------------------------------------------------------------------- template = Template('''<!DOCTYPE html> <html lang="en"> <head> <meta charset="latin-1"> <title>{{ title }}</title> {{ js_resources }} {{ css_resources }} {{ script }} </head> <body> {{ div }} <br/> <br/> '''+goheader+bp_info+mf_info+cc_info+kegg_info+ ''' {% block table3 %} {{ annots }} {% endblock %} </body> </html> ''') html = template.render(js_resources=js_resources, css_resources=css_resources, script=self.script, div=self.div, title = self.title, tablegobp = t_bp, tablegomf = t_mf, tablegocc = t_cc, tablekegg = t_kegg, annots = self.render_gene_table(), ) return html
def graph(): ajax_input = dict(x_time=[], x1_time=[], y=[], y_female=[], y_male=[], x_female_proj=[], y_female_proj=[], x_male_proj=[], y_male_proj=[], y_female_avg_viewing=[], y_male_avg_viewing=[]) source = AjaxDataSource(data=ajax_input, data_url='http://127.0.0.1:5000/data', polling_interval=ajax_refresh_inter * 1000) #adapter=adapter) p = figure(plot_height=300, plot_width=800, x_axis_type="datetime", tools="wheel_zoom,reset", title="People Viewing Statistics") p.line(x='x_time', y='y', line_dash="4 4", line_width=3, color='gray', source=source) p.vbar(x='x_time', top='y_female', width=200, alpha=0.5, color='red', legend='female', source=source) #female p.vbar(x='x1_time', top='y_male', width=200, alpha=0.5, color='blue', legend='male', source=source) #male p.xaxis.formatter = DatetimeTickFormatter(milliseconds=["%X"], seconds=["%X"], minutes=["%X"], hours=["%X"]) p.y_range = DataRange1d(start=0, range_padding=5) #padding leave margin on the top p.legend.orientation = "horizontal" #legend horizontal p.xaxis.axis_label = 'Time' p.yaxis.axis_label = 'Number' # p.x_range.follow = "end" # p.x_range.follow_interval = timedelta(seconds=30) p2 = figure(plot_height=300, plot_width=800, tools="wheel_zoom,reset", title="Project Viewing Statistics") p2.vbar(x='x_female_proj', top='y_female_proj', width=f2_vbar_interval, alpha=0.5, color='red', legend='female', source=source) #female p2.vbar(x='x_male_proj', top='y_male_proj', width=f2_vbar_interval, alpha=0.5, color='blue', legend='male', source=source) #male p2.xaxis.ticker = [2, 6, 10, 14] p2.xaxis.major_label_overrides = {2: 'P1', 6: 'P2', 10: 'P3', 14: 'P4'} # avg_view_male = LabelSet(x='x_male_proj', y='y_male_proj', text='y_male_avg_viewing', level='glyph', # x_offset=0, y_offset=5, source=source, render_mode='canvas') # avg_view_female = LabelSet(x='x_female_proj', y='y_female_proj', text='y_female_avg_viewing', level='glyph', # x_offset=0, y_offset=5, source=source, render_mode='canvas') # # p2.add_layout(avg_view_male) # p2.add_layout(avg_view_female) p2.x_range = DataRange1d(start=0, end=16) #padding leave margin on the top p2.y_range = DataRange1d(start=0, range_padding=5) #padding leave margin on the top p2.legend.orientation = "horizontal" p2.xaxis.axis_label = 'Project' p2.yaxis.axis_label = 'Number' js_resources = INLINE.render_js() css_resources = INLINE.render_css() script, div = components((p, p2)) html = render_template( 'embed.html', plot_script=script, plot_div=div[0], plot_div2=div[1], js_resources=js_resources, css_resources=css_resources, ) return encode_utf8(html)
def show_graph(): if request.method == 'POST': # Do stuff here if needed return redirect(url_for('show_map')) # init a basic bar chart: # http://bokeh.pydata.org/en/latest/docs/user_guide/plotting.html#bars # create a new plot with a title and axis labels # SAMPLE1 IS FOR ALCOHOL DATA sample1 = send_alcohol_data()[0][1][currhb] sam1 = [] for i in range(1, len(sample1)): sam1.append(sample1[i][1]) years1 = [] for i in range(1, len(sample1)): years1.append(sample1[i][0]) sample2 = return_mental_graph()[currhb] sam2 = [] for i in range(1, len(sample2)): sam2.append(sample2[i][1]) years2 = [] for i in range(1, len(sample2)): years2.append(sample2[i][0]) largest = max(sam1) if max(sam2) > largest: largest = max(sam2) left = figure( title= "Patients / 10000 Population with Alcohol Related Health Conditions in:", x_axis_label='Year', y_axis_label='Patients', y_range=(0, largest + 10)) left.add_layout(Title(text=sample1[0]), 'above') # add a line renderer with legend and line thickness left.line(years1, sam1, legend="Patients", line_width=2) # add a circle renderer with a size, colour and alpha left.circle(years1, sam1, size=10, color="navy", alpha=0.5, legend="Patients") right = figure( title="Patients / 10000 Population with Mental Health Conditions in:", x_axis_label='Year', y_axis_label='Patients', y_range=(0, largest + 10)) right.add_layout(Title(text=sample2[0]), 'above') # add a line renderer with legend and line thickness right.line(years2, sam2, legend="Patients", color="red", line_width=2) # add a circle renderer with a size, colour and alpha right.circle(years2, sam2, size=10, color="red", alpha=0.5, legend="Patients") # grab the static resources js_resources = INLINE.render_js() css_resources = INLINE.render_css() p = gridplot([[left, right]]) # render template script, div = components(p) html = render_template( 'graph.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, ) return encode_utf8(html)