示例#1
0
def print_scores(response_logs: List[pd.DataFrame], opts):
    joined = pd.concat(response_logs)  # type: pd.DataFrame
    binary_score = joined['score'] > 0.5
    print()
    print('Total number of pages: {:,}, relevant pages: {:,}, '
          'average binary score: {:.2f}, average score: {:.2f}'.format(
              len(joined), binary_score.sum(), binary_score.mean(),
              joined['score'].mean()))
    show_domain_stats(joined.copy(), output=opts.output, top=opts.top)
    joined.sort_values(by='time', inplace=True)
    joined.index = pd.to_datetime(joined.pop('time'), unit='s')
    if opts.smooth:
        crawl_time = (joined.index[-1] - joined.index[0]).total_seconds()
        avg_rps = len(joined) / crawl_time
        span = int(opts.smooth * opts.step * avg_rps)
        joined['score'] = joined['score'].ewm(span=span).mean()
    print_averages({'score': joined['score']}, opts.step, '{:.2f}')
    title = 'Page relevancy score'
    scores = joined['score'].resample('{}S'.format(opts.step)).mean()
    plot = TimeSeries(scores,
                      plot_width=1000,
                      xlabel='time',
                      ylabel='score',
                      title=title)
    plot.set(y_range=Range1d(0, 1))
    if not opts.no_show:
        save_plot(plot, title=title, suffix='score', output=opts.output)
示例#2
0
def plot_aboard_vs_fatalities_vs_year(air_data, method, save_fig=True):
    """
        乘客数量vs遇难数vs年份分析
    """

    grouped_year_sum_data = air_data.groupby('Year', as_index=False).sum()

    if method == 'sns':
        # Seaborn 绘图
        plt.figure(figsize=(15.0, 10.0))
        sns.barplot(x='Year',
                    y='Aboard',
                    data=grouped_year_sum_data,
                    color='green')
        sns.barplot(x='Year',
                    y='Fatalities',
                    data=grouped_year_sum_data,
                    color='red')

        # 解决matplotlib显示中文问题
        plt.rcParams['font.sans-serif'] = ['SimHei']  # 指定默认字体
        plt.rcParams['axes.unicode_minus'] = False  # 解决保存图像是负号'-'显示为方块的问题
        plt.title(u'乘客数量vs遇难数vs年份')
        plt.xlabel(u'年份')
        plt.ylabel(u'乘客数量vs遇难数')
        plt.xticks(rotation=90)

        if save_fig:
            plt.savefig('./output/aboard_fatalities_year.png')

        plt.show()

    elif method == 'bokeh':
        # Boken 绘图
        tsline = TimeSeries(data=grouped_year_sum_data,
                            x='Year',
                            y=['Aboard', 'Fatalities'],
                            color=['Aboard', 'Fatalities'],
                            dash=['Aboard', 'Fatalities'],
                            title='乘客数vs遇难数vs年份',
                            xlabel='年份',
                            ylabel='乘客数vs遇难数',
                            legend=True)
        tspoint = TimeSeries(data=grouped_year_sum_data,
                             x='Year',
                             y=['Aboard', 'Fatalities'],
                             color=['Aboard', 'Fatalities'],
                             dash=['Aboard', 'Fatalities'],
                             builder_type='point',
                             title='乘客数vs遇难数vs年份',
                             xlabel='年份',
                             ylabel='乘客数vs遇难数',
                             legend=True)
        output_file('./output/aboard_fatalities_year.html')
        show(column(tsline, tspoint))
    else:
        print '不支持的绘图方式!'
示例#3
0
def simple_chart(request,counter_name):
	counter_len = len(Counter.objects.values())
	Date = [Counter.objects.values()[i]["pub_date"] for i in range(counter_len)]
	name = counter_name
	y_values = Counter.objects.values_list("counter_value",flat=True).filter(counter_name=counter_name)
	
	ddict = OrderedDict({'Date':Date})
	ddict[name] = y_values

	plot = TimeSeries(ddict,index='Date',title=name,ylabel='Counter Values',plot_width=400,plot_height=500)
	plot.border_fill = "whitesmoke"
	plot.min_border = 350
	
	script, div = components(plot, CDN)
	context = RequestContext(request,{"the_script":script, "the_div":div})
	return render(request, "counters_app/simple_bokeh.html",context)
示例#4
0
def plot(ticker, list_of_metrics=['close']):
    with open('api.txt', 'r') as f:
        api_key = f.read()

    base = 'https://www.quandl.com/api/v3/datatables/WIKI/PRICES.json'
    today = datetime.date.today()
    date_gte = "".join(str(today - datetime.timedelta(days=30)).split('-'))

    response = requests.get(base,
                            params={
                                'api_key': api_key,
                                'ticker': ticker,
                                'date.gte': date_gte
                            })
    table_info = response.json()['datatable']

    df = pd.DataFrame(
        data=table_info['data'],
        columns=[col['name'] for col in table_info['columns']],
    )
    #dtype=[col['type'] for col in table_info['columns']])

    hover = HoverTool(tooltips=[('Date', '$x')])
    p = TimeSeries(data=df,
                   x='date',
                   y=list_of_metrics,
                   tools=[hover],
                   plot_width=600,
                   plot_height=400,
                   title=ticker)
    return p
示例#5
0
def plot_topic_popularity_over_time(topic):
    '''
    Plots the topic popularity as a function of time.
    Values on the y-axis represent daily article counts for that topic.
    Inputs:
        topic - integer
    Outputs:
        plot dictionary with script and div elements
    '''
    model = get_model()
    articles = model.articles
    article_counts = articles[articles['topic'] == topic].groupby(
        'date')['title'].count().sort_index()
    dates = articles['date'].unique()
    missing_dates = set(dates) - set(article_counts.index)
    for date in missing_dates:
        article_counts[date] = 0
    article_counts = article_counts.sort_index()
    dates = article_counts.index
    data = {'date': map(str, dates), 'count': article_counts}
    p = TimeSeries(data,
                   x='date',
                   y='count',
                   title='Topic Popularity Over Time')
    p.y_range.start = 0
    script, div = components(p)
    plot = {}
    plot['script'] = script
    plot['div'] = div
    return plot
示例#6
0
def index_stock():
    if request.method == 'GET':
        return render_template('index.html')
    else:
        #return render_template('Timeseries.html')
        app.vars['name'] = request.form['stock_name']
        r = requests.get('https://www.quandl.com/api/v3/datasets/WIKI/' +
                         app.vars['name'] +
                         '.json?api_key=pHsoz965RzenMwNwBsdS')
        #,auth=('*****@*****.**', 'yasigole20'), headers={'Authorization':'M3Lz2KmyPXch_zQyyS2o'})
        r.status_code
        json_data = json.loads(r.text)
        pd_apple = json_normalize(json_data['dataset'])
        l = pd_apple['data'][0][:9199]
        first = [e[0] for e in l if e[0].startswith('2017-05')]
        second = [e[4] for e in l if e[0].startswith('2017-05')]
        new_pd = pd.DataFrame(data={'Day': first, 'EOD': second})
        defaults.width = 600
        defaults.height = 350

        #output_file("templates/Timeseries.html")
        sorted_pd = new_pd.sort_values(["Day"])
        tsline = TimeSeries(sorted_pd,
                            x='Day',
                            y='EOD',
                            title="Timeseries",
                            ylabel='Stock Prices',
                            legend=True)

        #show(tsline)
        #return render_template('graphs.html',stock_name=len(new_pd))
        #return render_template('index.html')
        script, div = components(tsline)
        return render_template('graphs_tdi.html', script=script, div=div)
示例#7
0
def plot():
    year_prev_month = (datetime.datetime.now() -
                       datetime.timedelta(days=31)).strftime('%Y-%m')

    stock_ticker = request.args.get('stock_ticker', '')

    columns = map(str, request.args.getlist('column'))

    if len(columns) == 0 or stock_ticker == '':
        return render_template('/error.html')

    quandl = 'https://www.quandl.com/api/v3/datasets/WIKI/' + stock_ticker + '.json?start_date=' + year_prev_month + '-01'
    r = requests.get(quandl + '?api_key=_AnN7yvqekPoP7s1yPJ4')
    data = json.loads(r.text)
    stock = pd.DataFrame(data['dataset']['data'],
                         columns=map(str, data['dataset']['column_names']))
    stock['Date'] = stock['Date'].astype('datetime64')
    columns.append('Date')

    p = TimeSeries(stock[columns],
                   index='Date',
                   title=stock_ticker,
                   xlabel='Month/Day',
                   legend=True,
                   tools='pan,box_zoom,wheel_zoom,reset,save')

    script, div = embed.components(p, CDN)

    return render_template("timeseries.html", script=script, div=div)
示例#8
0
def index():
    error = None
    if request.method == 'POST':
        try:
            app.vars['stock_name'] = stock_to_caps(request.form['ticker'])
            app.vars['data_table'] = get_data(app.vars['stock_name'])
        except:
            app.vars['stock_name'] = 'GOOG'
            app.vars['data_table'] = get_data('GOOG')

    else:
        app.vars['stock_name'] = 'GOOG'
        app.vars['data_table'] = get_data('GOOG')
    fig = TimeSeries(app.vars['data_table'])

    plot_resources = RESOURCES.render(js_raw=INLINE.js_raw,
                                      css_raw=INLINE.css_raw,
                                      js_files=INLINE.js_files,
                                      css_files=INLINE.css_files)
    script, div = components(fig, INLINE)

    return render_template('index.html',
                           name=app.vars['stock_name'],
                           data=app.vars['data_table'],
                           plot_script=script,
                           plot_div=div,
                           plot_resources=plot_resources)
def analyzeAndPlot(tickerSymbol, typeOfData):
    #    tickerSymbol = 'PKI'
    #    typeOfData = 'Open'
    rjson = requests.get('https://www.quandl.com/api/v3/datasets/YAHOO/' +
                         tickerSymbol +
                         '.json?auth_token=L9jUoYTsWeow-DN_EraF')
    data = rjson.json()
    data = data['dataset']
    mydict = {
        data['column_names'][column]: np.array(data['data'])[:, column]
        for column in range(len(data['column_names']))
    }

    def newdate(mystring):
        yyyy, mm, dd = mystring.split('-')
        dt = datetime(int(yyyy), int(mm), int(dd))
        return dt

    df = DataFrame(mydict)
    df.Date = df.Date.apply(newdate)
    df = df.set_index('Date').sort_index(ascending=True)

    output_file("stocks.html", title=tickerSymbol + "Data")

    # p1 = figure(x_axis_type = "datetime")
    # # p1.title = datetime.now().strftime('%m/%d/%Y')
    # p1.line(df.index, df[typeOfData], color='red', legend=tickerSymbol)
    # p1.title = tickerSymbol + " Stock Prices : " + datetime.now().strftime('%m/%d/%Y %H:%M:%S')
    # p1.grid.grid_line_alpha=0.3
    # p1.xaxis.axis_label = 'Date'
    # p1.yaxis.axis_label = 'Price'

    # df2 = df.astype(float)[['Open','Close','Adjusted Close']]
    df2 = df.astype(float)[typeOfData]
    p1 = TimeSeries(df2.reset_index(),
                    index='Date',
                    legend=True,
                    title=tickerSymbol + ' Stock Prices',
                    xlabel='Date',
                    ylabel='Prices')

    outputOption = 2  # output options
    if outputOption == 1:
        # option 1: using bokeh.plotting.save to directly save to html file
        save(p1)  # need to import from bokeh.plotting!
    elif outputOption == 2:
        # option 2: using bokeh.embed.file_html to save to standalone html file
        html = file_html(p1, CDN, "my plot")
        f = open('./templates/_' + tickerSymbol + '.html', 'w')
        returnBtn = '<h1>Generated graph for ' + tickerSymbol + '<br><a href="/index">Back</a></h1>'
        html = html.replace('<body>', '<body>' + returnBtn)
        f.write('%s' % html)
        f.close()
    elif outputOption == 3:
        # option 2: using bokeh.embed.components to save to html components
        script, div = components(p1)
        f = open(tickerSymbol + '.html', 'w')
        f.write('%s' % script)
        f.close()
示例#10
0
def plot_aboard_vs_fatalities_vs_year(air_data, method, save_fig=True):
    group_year_data = air_data.groupby("Year", as_index=False).sum()
    print group_year_data

    if method == 'sns':
        plt.figure(figsize=(15.0, 10.0))
        sns.barplot(x="Year", y="Aboard", data=group_year_data, color='green')
        sns.barplot(x="Year",
                    y="Fatalities",
                    data=group_year_data,
                    color="red")
        # 瑙e喅matplotlib鏄剧ず涓枃闂

        plt.rcParams['font.sans-serif'] = ['SimHei']  # 鎸囧畾榛樿瀛椾綋
        plt.rcParams[
            'axes.unicode_minus'] = False  # 瑙e喅淇濆瓨鍥惧儚鏄礋鍙�-'鏄剧ず涓烘柟鍧楃殑闂
        plt.title(u'乘客数量vs遇难数vs年份')
        plt.xlabel(u'年份')
        plt.ylabel(u'乘客数量vs遇难数')
        plt.xticks(rotation=90)
        if save_fig:
            plt.savefig('aboard_fatalities_year.png')
        plt.show()
    elif method == 'bokeh':
        tsline = TimeSeries(data=group_year_data,
                            x='Year',
                            y=['Aboard', 'Fatalities'],
                            color=['Aboard', 'Fatalities'],
                            dash=['Aboard', 'Fatalities'],
                            title='乘客数vs遇难数vs年份',
                            xlabel='年份',
                            ylabel='乘客数vs遇难数',
                            legend=True)
        tspoint = TimeSeries(data=group_year_data,
                             x='Year',
                             y=['Aboard', 'Fatalities'],
                             color=['Aboard', 'Fatalities'],
                             dash=['Aboard', 'Fatalities'],
                             builder_type='point',
                             title='乘客数vs遇难数vs年份',
                             xlabel='年份',
                             ylabel='乘客数vs遇难数',
                             legend=True)
        output_file('aboard_fatalities_year.html')
        show(column(tsline, tspoint))
示例#11
0
def get_plot_ticker_components(ticker, df_data):
    plot_title = 'Last Month ' + ticker.upper() + ' Closing Price'

    p = TimeSeries(df_data.closing_price, legend=True, title=plot_title, xlabel='Date', ylabel='Stock Prices')
    p.title.text_font_size = '14pt'

    script, div = components(p)

    return script, div
示例#12
0
def index():

    if request.method == 'GET':
        return render_template('index.html')
    if request.method == 'POST':

        lag = 1
        ticker = request.form['ticker']
        keys = ['Close', 'Open', 'Adj. Open', 'Adj. Close', 'High', 'Low']
        series = [s for s in keys if s in request.form.keys()]
        if series == []:
            series = ['Close']

        def pad(s):
            s = str(s)
            if len(s) == 1:
                s = '0' + s
            return (s)

        now = datetime.now()
        dend = str(now.year) + '-' + pad(now.month) + '-' + pad(now.day)
        go = True
        f = 0
        while go:
            try:
                dstart = str(now.year) + '-' + pad(now.month -
                                                   1) + '-' + pad(now.day - f)
                dmp = datetime.strptime(dstart, '%Y-%m-%d')
                go = False
            except:
                f = f + 1
        drange = (dstart, dend)
        #drange = ('2016-07-01', '2016-08-01') #fix range

        url = 'https://www.quandl.com/api/v3/datasets/WIKI/{0}.json?trim_start={1}&trim_end={2}&api_key={3}'.format(
            ticker, str(drange[0]), str(drange[1]), app.api_key)
        try:
            jsondata = urlopen(url).read()
        except HTTPError:
            return render_template('index.html',
                                   ticker=ticker,
                                   error='Invalid ticker')
        jsondata = json.loads(jsondata)
        data = pd.DataFrame.from_dict(jsondata['dataset']['data'])
        data.columns = jsondata['dataset']['column_names']
        data.index = data.Date
        pd.to_datetime(data.index)
        plot = TimeSeries(data=data[::-1],
                          x='Date',
                          y=series,
                          legend=True,
                          xlabel='Date',
                          ylabel='$',
                          title=ticker)
        script, div = components(plot)

        return render_template('graph.html', script=script, div=div)
示例#13
0
def plt_all(df):
    TOOLS = 'pan,wheel_zoom,crosshair,resize,reset,save'
    ts_plt = TimeSeries(df,
                        title="time series",
                        xlabel='time',
                        ylabel='count',
                        plot_width=900,
                        plot_height=500,
                        tools=TOOLS)
    show(ts_plt)
示例#14
0
def main():
    stocks = request.args.get('stock')
    if stocks is None:
        stocks = 'goog, fb'
    checks = request.args.getlist('checks')
    if checks == []:
        checks = [ 'Low' ]

    df_builder={}
    error='' 
    data = []
    for stock in stocks.split(','):
        try:
            stock=stock.strip()
            response=requests.get("https://www.quandl.com/api/v3/datasets/WIKI/%s.json?api_key=VgV7E-Rpyyredyc2hFjs" % stock)        
            dataset = response.json()['dataset']
            date_column = dataset['column_names'].index('Date')
            index = pd.to_datetime( [item[ date_column ] for item in dataset['data']] )
            for check in checks:
                checked_column = dataset['column_names'].index(check)
                data = [item[checked_column] for item in dataset['data']]
                df_builder[ (stock+" "+check) ] = pd.Series( data, index=index)
        except Exception:
            error += stock + " could not be found. "
    try:
        df = pd.DataFrame( df_builder )
        plot=TimeSeries(df, legend=True)
    except Exception:
        error += "Could not build plot. "
        script=''
        div=''
    else:
        try:
            top = df.max().max()
            bottom = df.min().min()
            left = df.index.min()
            right = df.index.max()    
            plot.set( x_range=Range1d(left, right), y_range=Range1d(bottom,top) )
        except:
            pass
        script,div=components(plot)
    return render_template('index.html', script=script, div=div, code=stocks, checks=checks, error=error)
示例#15
0
def plt_one_ts(ts):
    plt.legend(loc="best")
    TOOLS = 'pan,wheel_zoom,crosshair,resize,reset,save'
    ts_plt = TimeSeries(ts,
                        title="time series",
                        xlabel='time',
                        ylabel='count',
                        plot_width=900,
                        plot_height=500,
                        tools=TOOLS)
    show(ts_plt)
示例#16
0
def plt_random_numpy(scaler_data, cols_index, rand_col_num):
    cols = random.sample(cols_index, rand_col_num)
    data = scaler_data[:, cols].T
    TOOLS = 'pan,wheel_zoom,crosshair,resize,reset,save'
    ts_plt = TimeSeries(data,
                        title="time series",
                        xlabel='time',
                        ylabel='count',
                        plot_width=900,
                        plot_height=500,
                        tools=TOOLS)
    show(ts_plt)
示例#17
0
def line_balance(data):
    #dat = get_balance("../sample_data.csv")
    dat = d.dataframer()
    data = dict(balance_x=dat['Balance_x'],
                balance_y=dat['Balance_y'],
                Date=dat['Date'])
    ser = TimeSeries(data,
                     x='Date',
                     ylabel='Balance/£',
                     legend=None,
                     width=1000)
    #line = Line(xyvalues, title="line", legend="top_left", ylabel='Languages')
    return ser
示例#18
0
def plt_random_cols(df, cols_index, rand_col_num):
    rand_col = random.sample(df.columns[cols_index].values.tolist(),
                             rand_col_num)
    data = df[rand_col]
    TOOLS = 'pan,wheel_zoom,crosshair,resize,reset,save'
    ts_plt = TimeSeries(data,
                        title="time series",
                        xlabel='time',
                        ylabel='count',
                        plot_width=900,
                        plot_height=500,
                        tools=TOOLS)
    show(ts_plt)
示例#19
0
def time_series_plot(dates, values, xlabel='', ylabel='', title=''):
    data = {'dates': map(str, dates), 'values': values}
    p = TimeSeries(data,
                   x='dates',
                   y='values',
                   xlabel=xlabel,
                   ylabel=ylabel,
                   title=title)
    p.y_range.start = 0
    script, div = components(p)
    plot = {}
    plot['script'] = script
    plot['div'] = div
    return plot
示例#20
0
def plot2():
    p = figure(title='Data from worldbank.org (SH.H2O.SAFE.ZS)',
               x_axis_label='date',
               x_axis_type='datetime')
    countries = ['AF', 'TZ', 'AO', 'MG', 'MZ', 'CG']
    indicators = {'SH.H2O.SAFE.ZS': 'Improved water source'}
    df1 = wbdata.get_dataframe(indicators,
                               country=countries,
                               convert_date=False)
    indicators2 = {'SP.POP.TOTL': 'Total Population'}
    df2 = wbdata.get_dataframe(indicators2,
                               country=countries,
                               convert_date=False)
    dfu1 = df1.unstack(level=0)
    dfu1 = dfu1['1990':]
    dfu2 = df2.unstack(level=0)
    dfu2 = dfu2['1990':]
    dfu = pd.DataFrame(dfu1.values * dfu2.values,
                       columns=dfu1.columns,
                       index=dfu1.index)
    range(dfu.shape[1])
    dfu.columns = range(dfu.shape[1])
    dfu['Date'] = dfu.index
    xyvalues = pd.DataFrame(
        dict(
            Afghanistan=dfu[0],
            Tanzania=dfu[1],
            Angola=dfu[2],
            Madagascar=dfu[3],
            Mozambique=dfu[4],
            Congo=dfu[5],
            # New_Guinea=dfu[6],
            # Saudi_Arabia=dfu[7],
            # Chad=dfu[8],
            # Mongolia=dfu[9],
            Date=dfu['Date']))

    output_file("stocks_timeseries.html")

    p = TimeSeries(
        xyvalues,
        x='Date',
        legend=True,
        title="",
        ylabel='Population with no access to improved source of water')

    script, div = components(p)
    return render_template('plot2.html', script=script, div=div)
示例#21
0
def time_series():
    try:
        if 'username' in session:
            users = mongo.db.users
            existing_user = users.find_one({"username": session["username"]})

            if existing_user:
                object = users.find_one({"username": session["username"]})
                user_session = object["user_session"]
                neu = []
                pos = []
                neg = []
                time = []
                data = {}
                for dic in user_session:
                    for k, v in dic.items():
                        if k != 'Algorithm':
                            time.append(k)
                            for k1, v1 in v[0].items():
                                if k1 == "neutral":
                                    neu.append(float(v1))
                                elif k1 == 'negative':
                                    neg.append(float(v1))
                                else:
                                    pos.append(float(v1))
                            data.update(Date=time,
                                        Neutral=neu,
                                        Positive=pos,
                                        Negative=neg)
                dataframe = pd.DataFrame(data)

                # output_file("timeseries.html")

                p = TimeSeries(dataframe,
                               x='Date',
                               y=['Neutral', 'Negative', 'Positive'],
                               color=['Neutral', 'Negative', 'Positive'],
                               dash=['Neutral', 'Negative', 'Positive'],
                               title="User History",
                               ylabel='Classes')
                script_p, div_p = components(p)
                script_p = Markup(script_p)
                div_p = Markup(div_p)
                page = [script_p, div_p]
                return page
    except Exception as e:
        return False
示例#22
0
def generateplot(data, desired_columns, stock):

    # plot = figure()
    # plot.circle([1,2], [3,4], radius=1.)

    note = ""
    if len(desired_columns) == 0:
        desired_columns = ['Close']
        note = "NOTE: No desired features selected!"

    desired_columns.append('Date')
    plot = TimeSeries(data.loc[:, desired_columns],
                      index='Date',
                      legend=True,
                      title=stock)
    script, div = components(plot)
    return script, div, note
示例#23
0
def index():
	# Pull POST data for 'ticker' into a local variable
	if request.method == 'POST':
		ticker = request.form['ticker']

		ticker_data = quandl.get('WIKI/' + ticker, collapse='daily')

		# Create title for Bokeh plot 
		title = ('Daily Closing Stock Price for ' + ticker + ', ' + str(prior_month) + '/' + str(year) + ' - ' + str(current_month) + '/' + str(year))

		# Create bokeh plotting variable
		ts_plot = TimeSeries(ticker_data.Close[-30:], title = title, xlabel = 'Date', ylabel = 'Price ($ USD)')

		script, div = components(ts_plot)

		return render_template('plot.html', script=script, div=div)
	else:
		return render_template('index.html')
示例#24
0
def output():
    myticker = request.form['ticker_symbol']
    quandlstr = "WIKI/" + myticker + ".11"
    startDate = "2016-11-30"
    endDate = "2016-12-30"
    try:
        df = quandl.get(quandlstr, start_date=startDate, end_date=endDate)
        #Fixed TimeSeries display https://github.com/bokeh/bokeh/issues/4344
        df['dates'] = df.index.to_datetime()
        fig = TimeSeries(df, x='dates')
        script, div = components(fig)
        title = 'Closing price for {} in the last month'.format(myticker)
        return render_template('plot.html',
                               script=script,
                               div=div,
                               title=title)
    except:
        return render_template('error.html')
示例#25
0
def stock_graph(ticker):

    quandl_apikey = "mWZdFiBfbyUzVK2xeec6"
    quandl_header = "https://www.quandl.com/api/v3/datasets/WIKI"

    today = datetime.date.today()
    lastmonth = today - datetime.timedelta(days=31)  # not so accurate...
    dateformat = "%Y-%m-%d"
    end_date = today.strftime(dateformat)
    start_date = lastmonth.strftime(dateformat)
    stock = ticker
    qurl='https://www.quandl.com/api/v3/datasets/WIKI/%s.json?column_index=4&'\
    'start_date=%s&end_date=%s&api_key=mWZdFiBfbyUzVK2xeec6'%(stock, start_date, end_date)

    r = requests.get(qurl)
    if r.status_code != requests.codes.ok: return 0
    print r.url
    print r.json()

    print(json.dumps(r.json(), sort_keys=True, indent=4 * ' '))
    #print json.loads(r.json())

    tsdata = r.json()['dataset']['data']

    print json.dumps(tsdata)

    # format data as pandas' time series data
    s = pd.Series()
    for price in tsdata:
        s.set_value(pd.Timestamp(price[0]), price[1])
    print s.is_time_series
    print s

    graph_title = "30 Day Stock Price of " + stock.encode('ascii', 'ignore')
    tsline = TimeSeries(s,
                        title=graph_title,
                        ylabel='Stock Prices',
                        xlabel='Date',
                        legend=True)
    #    output_file("timeseries.html", autosave=True)

    save(obj=tsline, filename='templates/timeseries.html')
    #    save(column(tsline))
    return 1
示例#26
0
def graph():

    #get POST data
    tickerSymbol = request.form['ticker'].upper()
    features = request.form.getlist('features')

    #get stock data from Quandl WIKI
    quandleKey = 'Hss7TYkWM9KUsB5wfvRb'
    quandleRequest = 'https://www.quandl.com/api/v3/datasets/WIKI/' + tickerSymbol + '.json?api_key=' + quandleKey
    r = requests.get(quandleRequest)

    #bad ticker
    if (r.status_code != 200):
        return render_template('errorPage.html')

    #convert to dataframe
    df = pd.DataFrame(r.json()['dataset']['data'],
                      columns=r.json()['dataset']['column_names'])
    df['Date'] = pd.to_datetime(df['Date'])
    df.rename(columns=lambda x: tickerSymbol + ': ' + x, inplace=True)

    #handle no features
    if len(features) == 0:
        p = figure(x_axis_type="datetime", title='Data from Quandl WIKI set')
    else:
        #pre-pend ticker name to column names
        columns = ['Date'] + features
        tickerText = tickerSymbol + ': '
        columns = [tickerText + s for s in columns]
        p = TimeSeries(df[columns],
                       legend=True,
                       index=tickerText + 'Date',
                       title='Data from Quandl WIKI set',
                       xlabel='date',
                       width=600,
                       height=600)

    #generate html plot
    htmlPlot = file_html(p, CDN, "my plot")

    return render_template('graph.html',
                           tickerSymbol=tickerSymbol,
                           features=features,
                           plottingData=htmlPlot)
示例#27
0
def index():
    if request.method == 'GET':
        return render_template('index.html')
    else:
        #request was a POST
        app.vars['ticker'] = request.form['ticker']
        app.vars['feature'] = request.form['features']

        url = 'https://www.quandl.com/api/v3/datasets/WIKI/' + str(
            app.vars['ticker']
        ) + '.json?order=asc&rows=31&start_date=2015-08-01&end_date=2015-08-31'

        r = requests.get(url)
        dat1 = json.loads(r.text)
        df = pd.DataFrame(dat1['dataset']['data'],
                          columns=dat1['dataset']['column_names'])
        df['Date'] = pd.to_datetime(df['Date'])

        if app.vars['feature'] == 'Close':
            ylab = "Closing price"
        if app.vars['feature'] == 'Adj. Close':
            ylab = "Adjusted closing price"
        if app.vars['feature'] == 'Volume':
            ylab = "Volume"

        fig = TimeSeries(df[str(app.vars['feature'])],
                         df['Date'],
                         title=str(app.vars['ticker']) + " August 2015",
                         ylabel=ylab)

    resources = RESOURCES.render(
        js_raw=INLINE.js_raw,
        css_raw=INLINE.css_raw,
        js_files=INLINE.js_files,
        css_files=INLINE.css_files,
    )

    script, div = components(fig, INLINE)
    html = render_template('index_post.html',
                           plot_script=script,
                           plot_div=div,
                           plot_resources=resources)
    return encode_utf8(html)
示例#28
0
def FrameViewer(df, title="EEG", ylabel='voltage (mV)', xlabel='time (s)'):

    x_min = df.index[0]
    x_max = df.index[-1]
    default_interval = 1
    ts = TimeSeries(df,
                    title=title,
                    legend='top_left',
                    ylabel=ylabel,
                    xlabel=xlabel,
                    x_range=Range1d(x_min, x_min + default_interval),
                    plot_height=400)

    # noinspection PyShadowingNames
    def update_in(x_min=x_min, x_interval=default_interval):
        ts.x_range.start = x_min
        ts.x_range.end = x_min + x_interval
        push_notebook()

    show(ts, notebook_handle=True)
    interact(update_in, x_min=(x_min, x_max, 0.5), x_interval=(0.2, 5, 0.2))
示例#29
0
def result():
    '''Gets prediction using the HTML form'''
    if request.method == 'POST':
        stock = request.form['query']

    col = ['Open', 'Close']
    rows = 300

    stock_code = "WIKI/" + stock

    mydata = quandl.get(stock_code, rows=rows)

    fig = TimeSeries(mydata,
                     y=col,
                     title="Stock Prices for " + stock,
                     ylabel='Stock Prices',
                     xlabel='Last ' + str(rows) + ' days',
                     legend='top_left')

    save(fig, filename="./templates/graph.html")
    show(fig)
    return (render_template('graph.html'))
示例#30
0
def make_plot(price_req, stock_symb):

    now = dt.datetime.now().date()
    then = now - dt.timedelta(days=30)
    then = "&start_date=" + then.strftime("%Y-%m-%d")
    now = "&end_date=" + now.strftime("%Y-%m-%d")

    symb = "WIKI/" + stock_symb

    mydata = quandl.get(symb, start_date=then, end_date=now)
    mydata = mydata[price_req]
    mydata.reset_index(inplace=True)

    TOOLS = [HoverTool()]
    p = TimeSeries(mydata,
                   x='Date',
                   ylabel='Stock Prices',
                   plot_height=300,
                   tools=TOOLS)
    script, div = components(p)

    return script, div
示例#31
0
def print_rpms(all_rpms: List[pd.DataFrame], opts):
    joined_rpms = pd.DataFrame()
    for df in all_rpms:
        joined_rpms = joined_rpms.join(df, how='outer')
    joined_rpms.fillna(0, inplace=True)

    all_name = '<all>'
    col_names = [df.columns[0] for df in all_rpms]
    joined_rpms[all_name] = joined_rpms[col_names[0]]
    for col_name in col_names[1:]:
        joined_rpms[all_name] += joined_rpms[col_name]

    print_averages(joined_rpms, opts.step)
    rpms_title = 'Requests per minute'
    rpms_plot = TimeSeries(joined_rpms,
                           plot_width=1000,
                           xlabel='time',
                           ylabel='rpm',
                           title=rpms_title)
    if not opts.no_show:
        save_plot(rpms_plot,
                  title=rpms_title,
                  suffix='rpms',
                  output=opts.output)
示例#32
0
def make_plot(stock_symb):
    # Build the Dataframe
    today = dt.date.today()
    today_str = "&end_date=" + today.strftime("%Y-%m-%d")
    month_ago = today - dt.timedelta(days=30)
    month_ago_str = "&start_date=" + month_ago.strftime("%Y-%m-%d")

    symb = "WIKI/" + stock_symb

    mydata = quandl.get(symb, start_date=month_ago_str, end_date=today_str)
    mydata.reset_index(inplace=True)
    mydata = mydata[["Date", "Close"]]

    TOOLS = [HoverTool()]

    p = TimeSeries(mydata,
                   x="Date",
                   ylabel="Stock Prices at Closing",
                   legend=True,
                   plot_height=300,
                   tools=TOOLS)
    script, div = components(p)

    return script, div
示例#33
0
    "http://ichart.yahoo.com/table.csv?s=AAPL&a=0&b=1&c=2000",
    parse_dates=['Date'])
MSFT = pd.read_csv(
    "http://ichart.yahoo.com/table.csv?s=MSFT&a=0&b=1&c=2000",
    parse_dates=['Date'])
IBM = pd.read_csv(
    "http://ichart.yahoo.com/table.csv?s=IBM&a=0&b=1&c=2000",
    parse_dates=['Date'])

xyvalues = OrderedDict(
    AAPL=AAPL['Adj Close'],
    Date=AAPL['Date'],
    MSFT=MSFT['Adj Close'],
    IBM=IBM['Adj Close'],
)

# any of the following commented are valid Bar inputs
#xyvalues = pd.DataFrame(xyvalues)
#lindex = xyvalues.pop('Date')
#lxyvalues = list(xyvalues.values())
#lxyvalues = np.array(xyvalues.values())

ts = TimeSeries(xyvalues, index='Date', title="timeseries, pd_input",
                ylabel='Stock Prices', filename="stocks_timeseries.html")

# usage with iterable index
#ts = TimeSeries(lxyvalues, index=lindex, title="timeseries, pd_input",
#                ylabel='Stock Prices', filename="stocks_timeseries.html")

ts.legend("top_left").show()