Beispiel #1
0
def update_market_data(attr, old, new):
    """update market data"""
    label.text = "Loading Market Data..."
    source_ind.data["color"] = ["red"]
    (nodes, g_by_fuel, demand, inj, f_dc, t_first, t_last,
     lines, dclines, lodf_matrix, n_0_flows, n_1_flows) = init_market_data(select_market_db.value)
    ## Update Data Sources
    source_nodes_data = ColumnDataSource.from_df(nodes)
    source_g_fuel_data.data = ColumnDataSource.from_df(g_by_fuel)
    source_d_data.data = ColumnDataSource.from_df(demand)
    source_inj_data.data = ColumnDataSource.from_df(inj)
    source_f_dc_data.data = ColumnDataSource.from_df(f_dc)
    source_n_0_flows_data.data = ColumnDataSource.from_df(n_0_flows)
    source_n_1_flows_data.data = ColumnDataSource.from_df(n_1_flows)
    source_lines_data.data = ColumnDataSource.from_df(lines)
    source_dclines_data.data = ColumnDataSource.from_df(dclines)
    source_lodf_data.data = ColumnDataSource.from_df(lodf_matrix)

    ### Update Data Sources for the plots
    nodes_dict = create_nodes_source(nodes)
    source_nodes.data = nodes_dict
    line_dict = create_line_source(lines, nodes)
    source_lines.data = line_dict
    dcline_dict = create_dc_line_source(dclines, nodes)
    source_dclines.data = dcline_dict

    slider.start = t_first
    slider.end = t_last
    slider.value = t_first
    label.text = "Data is Loaded!"
    source_ind.data["color"] = ["green"]
Beispiel #2
0
def update_market_data(attr, old, new):
    """update market data"""

    (nodes, g_by_fuel, demand, inj, f_dc, t_first, t_last, lines, dclines,
     lodf_matrix, n_0_flows,
     n_1_flows) = init_market_data(select_market_db.value)
    ## Update Data Sources
    # source_nodes_data = ColumnDataSource.from_df(nodes)
    source_g_fuel_data.data = ColumnDataSource.from_df(g_by_fuel)
    source_d_data.data = ColumnDataSource.from_df(demand)
    source_inj_data.data = ColumnDataSource.from_df(inj)
    source_f_dc_data.data = ColumnDataSource.from_df(f_dc)
    source_n_0_flows_data.data = ColumnDataSource.from_df(n_0_flows)
    source_n_1_flows_data.data = ColumnDataSource.from_df(n_1_flows)
    source_lines_data.data = ColumnDataSource.from_df(lines)
    source_dclines_data.data = ColumnDataSource.from_df(dclines)
    source_lodf_data.data = ColumnDataSource.from_df(lodf_matrix)

    ### Update Data Sources for the plots
    nodes_dict = create_nodes_source(nodes)
    source_nodes.data = nodes_dict
    line_dict = create_line_source(lines, nodes)
    source_lines.data = line_dict
    dcline_dict = create_dc_line_source(dclines, nodes)
    source_dclines.data = dcline_dict

    slider.start = t_first
    slider.end = t_last
    slider.value = t_first
Beispiel #3
0
 def calculate_mag_dec(attr, old, new, start_ie=None):
     ''' Is being call from the callback for the experiment chooser
         loads selected experiment visualize in plot p1 and p2 
         gets experiment number from the slider
         writes source_fid.data from the fid from the polymer object
         writes source_mag_dec.data from the dataframe
         '''
     ie = experiment_slider.value   #get expermient number from the slider
     logger.debug('calculate mag_dec for ie={}'.format(ie))
     fid, df = get_data_frames(ie)
     source_fid.data=ColumnDataSource.from_df(fid) #convert fid to bokeh format
     source_mag_dec.data = ColumnDataSource.from_df(df)
Beispiel #4
0
def update_market_data(attr, old, new):
    """update market data"""
    label.text = "Loading..."
    source_ind.data["color"] = ["red"]
    g_by_fuel, demand, inj, f_dc, t_first, t_last = init_market_result(select_market_db.value)

    source_g_fuel.data = ColumnDataSource.from_df(g_by_fuel)
    source_d.data = ColumnDataSource.from_df(demand)
    source_inj.data = ColumnDataSource.from_df(inj)
    source_dc.data = ColumnDataSource.from_df(f_dc)
    slider.start = t_first
    slider.end = t_last
    slider.value = t_first
    label.text = "Data is Loaded!"
    source_ind.data["color"] = ["green"]
Beispiel #5
0
    def on_view_cluster(self, attr, old, new):
        self.current_segment = new
        self.current_segment_clients = (
            self.df_rfm[self.cfc] == self.current_segment).sum()

        cluster_column_kind = self.df_rfm[self.cfc].dtype.kind
        if cluster_column_kind == 'i':
            sel_val = int(self.current_segment)
        elif cluster_column_kind == 'f':
            sel_val = float(self.current_segment)
        else:
            sel_val = self.current_segment

        self._logger("Viewing cluster selection {} (kind:{}) prep...".format(
            sel_val, cluster_column_kind))

        temp_df = self.df_rfm[self.df_rfm[self.cfc] == sel_val].head(
            self.nr_shown_records)
        self._logger("Viewed table head/tail:\n{}\n{}".format(
            temp_df.head(3), temp_df.tail(3)))
        self.cds_select.data = ColumnDataSource.from_df(temp_df)
        self.update_texts()
        if self.FULL_DEBUG:
            self._logger("Done view cluster prep.")
        return
Beispiel #6
0
 def on_update_table(self, attr, old, new):
     self.nr_shown_records = new
     self._logger("Refreshing data for table view: {}".format(
         self.df_rfm.head(self.nr_shown_records).shape))
     self.cds_select.data = ColumnDataSource.from_df(
         self.df_rfm.head(self.nr_shown_records))
     return
Beispiel #7
0
def fit_mag_decay_all(polymer, par_df):
    logger.debug('fitting all mag decay')
    p3 = figure(plot_width=400, plot_height=400,
            title='normalized phi vs normalized tau', webgl = True,
                y_axis_type = 'log',
                x_axis_type = 'linear')

    nr_experiments = polymer.get_number_of_experiments()
    r1=np.zeros(nr_experiments)
    MANY_COLORS = 0
    p3_line_glyph=[]
    for i in range(1, nr_experiments):
        try:
            par=polymer.getparameter(i)
            fid=polymer.getfid(i)
            tau= polymer.get_tau_axis(i) # tau axis could be parameter created right after import
            try:
                startpoint=polymer.getparvalue(i,'fid_range')[0]
                endpoint=polymer.getparvalue(i,'fid_range')[1] 
            except:
                startpoint=int(0.05*polymer.getparvalue(i,'BS'))
                endpoint = int(0.1*polymer.getparvalue(i,'BS'))
                logger.warning('using preset range (from {startpoint} to {endpoint}) in experiment {i}'.format(**locals()))
                polymer.addparameter(i,'fid_range',(startpoint,endpoint))
            phi = get_mag_amplitude(fid, startpoint, endpoint,
                                    polymer.getparameter(i)['NBLK'], polymer.getparameter(i)['BS'])
            df = pd.DataFrame(data=np.c_[tau, phi], columns=['tau', 'phi'])
            df['phi_normalized']=(df['phi'] - df['phi'].iloc[0] ) / (df['phi'].iloc[-1] - df['phi'].iloc[1] )
            polymer.addparameter(i,'df_magnetization',df)
            
            # initial values for the fit
            p0 = [1, 2 * polymer.getparvalue(i,'T1MX'), 0]
            df, popt = magnetization_fit(df,p0, fit_option=2)
            polymer.addparameter(i,'amp',popt[0])
            polymer.addparameter(i,'r1',popt[1])
            polymer.addparameter(i,'noise',popt[2])
            r1[i] = popt[1]
            tau = popt[1]*df.tau
            phi = popt[0]**-1*(df.phi_normalized - popt[2])
            p3_df=pd.DataFrame(data=np.c_[ tau, phi ], columns=['tau', 'phi'])
            source_p3=ColumnDataSource(data=ColumnDataSource.from_df(p3_df))
            p3_line_glyph.append(p3.line('tau', 'phi', source=source_p3))
            MANY_COLORS+=1
        except KeyError:
            logger.warning('no relaxation experiment found')
            polymer.addparameter(i,'amp',float('NaN'))
            polymer.addparameter(i,'r1',float('NaN'))
            polymer.addparameter(i,'noise',float('NaN'))
    COLORS=viridis(MANY_COLORS)
    for ic in range(MANY_COLORS):
        p3_line_glyph[ic].glyph.line_color=COLORS[ic]
    par_df['r1']=r1
    return p3
def candle_stix(dataframe):
    '''
    This function takes in dataframes and makes a candlesticks plot using bokeh
    Parameters
    ----------
    dataframe Stock dataframe
        This frame has elements: Open, Volume, Close, High, Low, Symbol and Date
    '''
    # Develop Aspects and Calculations
    mids = (dataframe["Open"] + dataframe["Close"])/2
    spans = abs(dataframe.Close-dataframe.Open)
    inc = dataframe.Close > dataframe.Open
    dec = dataframe.Open > dataframe.Close
    w = 12*60*60*1000 #width about a half day in width

    # Define Hover tool tips
    Gain = ColumnDataSource(ColumnDataSource.from_df(dataframe[inc]))
    Loss = ColumnDataSource(ColumnDataSource.from_df(dataframe[dec]))
    hover = HoverTool(
        tooltips=[
            ("Price","$y"),
            ("High","@High"),
            ("Low","@Low"),
            ("Open","@Open"),
            ("Close","@Close"),
            ("Volume(M)","@Volume")
        ]
    )

    # Build Plot

    output_file("candlestix.html", title="Candlestix")
    TOOLS = "pan,wheel_zoom,box_zoom"
    p = figure(x_axis_type="datetime",tools=[TOOLS,hover],plot_width=800,toolbar_location="left")
    p.segment(dataframe.Time, dataframe.Low ,dataframe.Time, dataframe.High , color="black")
    p.rect(dataframe.Time[inc], mids[inc], w, spans[inc], source=Gain, fill_color="white", line_color="black")
    p.rect(dataframe.Time[dec], mids[dec], w, spans[dec], source=Loss, fill_color="black", line_color="black")
    p.title = str(str(dataframe["Symbol"][1]))
    show(p)
Beispiel #9
0
def create_scatter(df_orig, met, xaxis, **kwargs):
    """
    Creates and returns a scatter plot as a bokeh figure object with circle glyphs
    """
    if 'cato' in kwargs:
        cato = kwargs['cato']
    else:
        cato = False
    if 'col' in kwargs:
        col = kwargs['colors']
    else:
        col = Set1[9]
    if 'metric_attrib' in kwargs:
        metric_attrib = kwargs['metric_attrib']
    else:
        metric_attrib = False

    df = df_orig

    p = scatter_template(df, met, xaxis, cato)

    #Finds the unique list of the category and counts them
    if cato:
        uniq_cat = df[cato].unique()
        cat_cnt = len(uniq_cat)

        #Groups by the category and assigns each group to a ColumnDataSource
        indv_cat = df.groupby(cato)

        source = []
        for item in indv_cat:
            sub_source = ColumnDataSource(ColumnDataSource.from_df(item[1]))
            source.append(sub_source)

        i = 0
        while i < cat_cnt:
            p.circle(xaxis,
                     met,
                     size=10,
                     color=col[i % len(col)],
                     source=source[i])
            i += 1

    else:
        source = ColumnDataSource(df)
        p.circle(xaxis, met, size=10, source=source)

    if metric_attrib:
        p = create_limits(p, df, xaxis, metric_attrib)

    return p
Beispiel #10
0
def _dict_from_da(da, unstack=None):
    """"""
    df = da.drop_vars([c for c in da.coords
                       if c not in da.dims]).to_dataframe()

    if unstack is not None:
        df = df.unstack(unstack)

    # convert column multi-index levels to str
    if isinstance(df.columns, pd.MultiIndex):
        df.columns = df.columns.set_levels(
            [l.astype(str) for l in df.columns.levels])

    return ColumnDataSource.from_df(df)
Beispiel #11
0
 def callback_load_more_data(attr,old,new):
     ''' callback for loading of data '''
     # TODO: implement
     logger.debug('callback for loading of data ')
     logger.error('Not implemented!')
     path=pathbox.value.strip()
     file=filebox.value.strip()
     if file=="*.sdf":
         logger.info('callback for loading data. filename: {}'.format(file))
         allsdf=filter(lambda x: x.endswith('.sdf'),os.listdir(path))
         for f in allsdf:
             sdf_list.append(sdf.StelarDataFile(f,path))
     else:
         sdf_list.append(sdf.StelarDataFile(file,path))
     
     filenames=[x.file() for x in sdf_list]
     filenames_df=pd.DataFrame(data=filenames,columns=['file'])
     table_source.data=ColumnDataSource.from_df(filenames_df)
Beispiel #12
0
def make_figure3():
    plot = figure(tools=TOOLS,
                  width=750,
                  height=450,
                  title='the Steep Climb',
                  x_axis_label='Percentage of Total Books',
                  y_axis_label='Percentage of Total Ratings')
    plot.line(df_sorted['perc_books'],
              df_sorted['cum_perc_ratings'],
              color='Orange')

    source = ColumnDataSource(ColumnDataSource.from_df(df_sorted))

    hover = plot.select(dict(type=HoverTool))
    hover.tooltips = OrderedDict([('Percentage of Total Books', '@perc_books'),
                                  ('Percentage of Total Ratings',
                                   '@cum_perc_ratings')])

    return plot
Beispiel #13
0
def make_figure1():
    plot = figure(tools=TOOLS,
                  width=750,
                  height=450,
                  title='the Long Tail',
                  x_axis_label='Number of Books',
                  y_axis_label='Number of Ratings',
                  x_range=(-50, 1080))
    plot.patch(
        pd.Series([0]).append(df_sorted['cum_sum'].append(pd.Series([0]))),
        pd.Series([0]).append(df_sorted['no_ratings'].append(pd.Series([0]))),
        color="#99d8c9")
    plot.line(df_sorted['cum_sum'], df_sorted['no_ratings'], color='Orange')

    source = ColumnDataSource(ColumnDataSource.from_df(df_sorted))

    hover = plot.select(dict(type=HoverTool))
    hover.tooltips = OrderedDict([('Number of books', '@cum_sum'),
                                  ('Ratings', '@no_ratings')])

    return plot
Beispiel #14
0
def _heatmap_summary(pvals, coefs, plot_width=1200, plot_height=400):
    """ Plots heatmap of coefficients colored by pvalues

    Parameters
    ----------
    pvals : pd.DataFrame
        Table of pvalues where rows are balances and columns are
        covariates.
    coefs : pd.DataFrame
        Table of coefficients where rows are balances and columns are
        covariates.
    plot_width : int, optional
        Width of plot.
    plot_height : int, optional
        Height of plot.

    Returns
    -------
    bokeh.charts.Heatmap
        Heatmap summarizing the regression statistics.
    """
    c = coefs.reset_index()
    c = c.rename(columns={'index': 'balance'})

    # fix alpha in fdr to account for the number of covariates
    def fdr(x):
        return multipletests(x, method='fdr_bh',
                             alpha=0.05 / pvals.shape[1])[1]
    cpvals = pvals.apply(fdr, axis=0)

    # log scale for coloring
    log_p = -np.log10(cpvals+1e-200)
    log_p = log_p.reset_index()
    log_p = log_p.rename(columns={'index': 'balance'})
    p = pvals.reset_index()
    p = p.rename(columns={'index': 'balance'})

    cp = cpvals.reset_index()
    cp = cp.rename(columns={'index': 'balance'})

    cm = pd.melt(c, id_vars='balance', var_name='Covariate',
                 value_name='Coefficient')
    pm = pd.melt(p, id_vars='balance', var_name='Covariate',
                 value_name='Pvalue')
    cpm = pd.melt(cp, id_vars='balance', var_name='Covariate',
                  value_name='Corrected_Pvalue')
    logpm = pd.melt(log_p, id_vars='balance', var_name='Covariate',
                    value_name='log_Pvalue')
    m = pd.merge(cm, pm,
                 left_on=['balance', 'Covariate'],
                 right_on=['balance', 'Covariate'])
    m = pd.merge(m, logpm,
                 left_on=['balance', 'Covariate'],
                 right_on=['balance', 'Covariate'])
    m = pd.merge(m, cpm,
                 left_on=['balance', 'Covariate'],
                 right_on=['balance', 'Covariate'])

    hover = HoverTool(
        tooltips=[("Pvalue", "@Pvalue"),
                  ("Corrected Pvalue", "@Corrected_Pvalue"),
                  ("Coefficient", "@Coefficient")]
    )

    N, _min, _max = len(palette), m.log_Pvalue.min(), m.log_Pvalue.max()
    X = pd.Series(np.arange(len(pvals.index)), index=pvals.index)
    Y = pd.Series(np.arange(len(pvals.columns)), index=pvals.columns)
    m['X'] = [X.loc[i] for i in m.balance]
    m['Y'] = [Y.loc[i] for i in m.Covariate]

    # fill in nans with zero.  Sometimes the pvalue calculation fails.
    m = m.fillna(0)
    for i in m.index:
        x = m.loc[i, 'log_Pvalue']
        ind = int(np.floor((x - _min) / (_max - _min) * (N - 1)))
        m.loc[i, 'color'] = palette[ind]
    source = ColumnDataSource(ColumnDataSource.from_df(m))
    hm = figure(title='Regression Coefficients Summary',
                plot_width=1200, plot_height=400,
                tools=[hover, PanTool(), BoxZoomTool(),
                       WheelZoomTool(), ResetTool(),
                       SaveTool()])
    hm.rect(x='X', y='Y', width=1, height=1,
            fill_color='color', line_color="white", source=source)
    Xlabels = pd.Series(pvals.index, index=np.arange(len(pvals.index)))
    Ylabels = pd.Series(pvals.columns, index=np.arange(len(pvals.columns)), )

    hm.xaxis[0].ticker = FixedTicker(ticks=Xlabels.index)
    hm.xaxis.formatter = FuncTickFormatter(code="""
    var labels = %s;
    return labels[tick];
    """ % Xlabels.to_dict())

    hm.yaxis[0].ticker = FixedTicker(ticks=Ylabels.index)
    hm.yaxis.formatter = FuncTickFormatter(code="""
    var labels = %s;
    return labels[tick];
    """ % Ylabels.to_dict())

    return hm
Beispiel #15
0
def candle(ticker, days=365, zoom=20):
    PATH = os.path.join(os.getcwd() + './Stocks/' + ticker + ".csv")
    df = pd.read_csv(PATH).iloc[-days:]
    df["Date"] = pd.to_datetime(df["Date"])

    # Setting Axis
    max_high = df.iloc[-zoom:].High.max()
    min_low = df.iloc[-zoom:].Low.min()

    inc = df.Close > df.Open
    dec = df.Open > df.Close
    shift = df["18EMA"] < df["50EMA"]

    long_pivot = (df.Low < df.Low.shift(1)) & (df.Low < df.Low.shift(-1)) & (
        df.High.shift(-1) > df.High.shift(1)) & (df.High.shift(-2) >
                                                 df.High.shift(1))

    long_reversal = (df.Open > df["18EMA"]) & (df.Close > df["18EMA"]) & (
        df.Low < df["18EMA"]) & (df.Low < df.Low.shift(1))
    w = 24 * 60 * 60 * 1000  # full day in ms

    sourceInc = ColumnDataSource.from_df(df.loc[inc])
    sourceDec = ColumnDataSource.from_df(df.loc[dec])
    hover = HoverTool(
        names=["dot"],
        tooltips=[
            ("open", "@Open"),
            ("close", "@Close"),
            ("high", "@High"),
            ("low", "@Low"),
            ("18EMA", "@18EMA"),
            ("50EMA", "@50EMA"),
            ("100EMA", "@100EMA"),
            ("200EMA", "@200EMA"),
        ],
    )
    TOOLS = "pan,wheel_zoom,box_zoom,reset,save"

    p = figure(
        title=ticker,
        x_axis_type="datetime",
        tools=[TOOLS, hover],
        plot_width=475,
        plot_height=475,
        background_fill_color="white",
        y_range=(min_low - 5, max_high + 5),
        x_range=(df.iloc[-zoom].Date, df.iloc[-1].Date),
    )
    p.xaxis.major_label_overrides = {
        i: date.strftime("%b %d")
        for i, date in enumerate(pd.to_datetime(df["Date"]))
    }
    p.xaxis.major_label_orientation = pi / 4
    p.grid.grid_line_alpha = 0.3

    p.segment(df.Date, df.High, df.Date, df.Low, color="black")
    # p.vbar
    p.vbar(
        df.Date[inc],
        w,
        df.Open[inc],
        df.Close[inc],
        fill_color="green",
        line_color="black",
    )
    p.vbar(
        df.Date[dec],
        w,
        df.Open[dec],
        df.Close[dec],
        fill_color="red",
        line_color="black",
    )
    p.vbar(
        df.Date[shift],
        w,
        df.Open[shift],
        df.Close[shift],
        fill_color="purple",
        line_color="black",
    )
    p.vbar(
        df.Date[long_pivot],
        w,
        df.Open[long_pivot],
        df.Close[long_pivot],
        fill_color="orange",
        line_color="black",
    )
    p.vbar(
        df.Date[long_reversal],
        w,
        df.Open[long_reversal],
        df.Close[long_reversal],
        fill_color="blue",
        line_color="black",
    )
    p.line(df.Date, df["18EMA"], line_width=2, line_color="red")
    p.line(df.Date, df["50EMA"], line_width=2, line_color="green")
    p.line(df.Date, df["100EMA"], line_width=2, line_color="blue")
    p.line(df.Date, df["200EMA"], line_width=2, line_color="purple")

    p.circle("Date",
             "Close",
             name="dot",
             size=7,
             source=sourceDec,
             color="grey")
    p.circle("Date",
             "Close",
             name="dot",
             size=7,
             source=sourceInc,
             color="grey")

    return p
Beispiel #16
0
def plot():
    if request.method == 'POST':
        symbol = request.form['symbol']
        # Get Stock DataFrame
        # msft = yf.Ticker("MSFT")
        msft = yf.Ticker(symbol)
        hist = msft.history(period='max')

        # Define constants
        W_PLOT = 1000
        H_PLOT = 400
        TOOLS = 'pan,wheel_zoom,hover,reset'

        VBAR_WIDTH = 0.2
        RED = Category20[7][6]
        GREEN = Category20[5][4]

        BLUE = Category20[3][0]
        BLUE_LIGHT = Category20[3][1]

        ORANGE = Category20[3][2]
        PURPLE = Category20[9][8]
        BROWN = Category20[11][10]

        def get_symbol_df(symbol=None):
            df = pd.DataFrame(hist)[-50:]
            df.reset_index(inplace=True)
            df["Date"] = pd.to_datetime(df["Date"])
            return df

        def plot_stock_price(stock):

            p = figure(plot_width=W_PLOT,
                       plot_height=H_PLOT,
                       tools=TOOLS,
                       title="Stock price",
                       toolbar_location='above')

            inc = stock.data['Close'] > stock.data['Open']
            dec = stock.data['Open'] > stock.data['Close']
            view_inc = CDSView(source=stock, filters=[BooleanFilter(inc)])
            view_dec = CDSView(source=stock, filters=[BooleanFilter(dec)])

            # map dataframe indices to date strings and use as label overrides
            p.xaxis.major_label_overrides = {
                i + int(stock.data['index'][0]): date.strftime('%b %d')
                for i, date in enumerate(pd.to_datetime(stock.data["Date"]))
            }
            p.xaxis.bounds = (stock.data['index'][0], stock.data['index'][-1])

            p.segment(x0='index',
                      x1='index',
                      y0='Low',
                      y1='High',
                      color=RED,
                      source=stock,
                      view=view_inc)
            p.segment(x0='index',
                      x1='index',
                      y0='Low',
                      y1='High',
                      color=GREEN,
                      source=stock,
                      view=view_dec)

            p.vbar(x='index',
                   width=VBAR_WIDTH,
                   top='Open',
                   bottom='Close',
                   fill_color=BLUE,
                   line_color=BLUE,
                   source=stock,
                   view=view_inc,
                   name="price")
            p.vbar(x='index',
                   width=VBAR_WIDTH,
                   top='Open',
                   bottom='Close',
                   fill_color=RED,
                   line_color=RED,
                   source=stock,
                   view=view_dec,
                   name="price")

            p.legend.location = "top_left"
            p.legend.border_line_alpha = 0
            p.legend.background_fill_alpha = 0
            p.legend.click_policy = "mute"

            p.yaxis.formatter = NumeralTickFormatter(format='$ 0,0[.]000')
            p.x_range.range_padding = 0.05
            p.xaxis.ticker.desired_num_ticks = 40
            p.xaxis.major_label_orientation = 3.14 / 4

            # Select specific tool for the plot
            price_hover = p.select(dict(type=HoverTool))

            # Choose, which glyphs are active by glyph name
            price_hover.names = ["price"]
            # Creating tooltips
            price_hover.tooltips = [("Datetime", "@Date{%Y-%m-%d}"),
                                    ("Open", "@Open{$0,0.00}"),
                                    ("Close", "@Close{$0,0.00}"),
                                    ("Volume", "@Volume{($ 0.00 a)}")]
            price_hover.formatters = {"Date": 'datetime'}

            return p

        stock = ColumnDataSource(
            data=dict(Date=[], Open=[], Close=[], High=[], Low=[], index=[]))
        #stock = AjaxDataSource(data=dict(Date=[], Open=[], Close=[], High=[], Low=[],index=[]),data_url='http://127.0.0.1:5000/plot',polling_interval=1000,mode='append')
        #symbol = 'msft'
        df = get_symbol_df(symbol)
        stock.data = stock.from_df(df)
        elements = list()

        # update_plot()
        p_stock = plot_stock_price(stock)

        elements.append(p_stock)

        curdoc().add_root(column(elements))
        curdoc().title = 'Bokeh stocks historical prices'

        #show(p_stock)

        script, div = components(p_stock)
        kwargs = {'script': script, 'div': div}
        #kwargs['title'] = 'bokeh-with-flask'
        return render_template('index.html', **kwargs)

        #return redirect(url_for('index', **kwargs))
        #return kwargs
        #return json.dumps(json_item(p_stock,"myplot"))

    #return redirect(url_for('index'))
    return "OK"
def create_figure():
    fig_df = df

    fig_df = fig_df[-int(text.value):]

    #Reverse lookup original column name if name if part of the conversion dict.
    #This will not be needed in the general Bokeh_Select
    if metric.value in conv.values():
        metric_name = [k for (k, v) in conv.iteritems()
                       if v == metric.value][0]

    else:
        metric_name = metric.value

    #Gets all the metric attributes from the xml file
    metric_attrib = param_get(metric_name)

    cycle_filt = lambda x: x['Cycles Completed'] >= int(metric_attrib['cycles']
                                                        )
    fig_df = fig_df.loc[cycle_filt]

    #Finds the unique list of the category and counts them
    uniq_cat = list(set(fig_df[cat.value].dropna()))
    color_cnt = len(uniq_cat)

    #Groups by the category and assigns each group to a ColumnDataSource
    indv_cat = fig_df.groupby(cat.value)
    main_source = []
    for item in indv_cat:
        source = ColumnDataSource(ColumnDataSource.from_df(item[1]))
        main_source.append(source)

    #Creates the tooltip and assign plot parameters
    hover = HoverTool(tooltips=[(
        'Serial Number',
        '@SerialNumber'), (cat.value, '@' +
                           cat.value), (metric.value, '@' +
                                        metric.value), ('Date',
                                                        '@StartDateStr')])

    p = figure(plot_height=625,
               plot_width=950,
               x_axis_type='datetime',
               tools=['pan, wheel_zoom', 'reset', 'save', hover],
               active_scroll='wheel_zoom',
               title="{0} by {1}".format(metric.value, cat.value))

    #Creates the specification and control limits
    time_range = [min(fig_df['StartDate']), max(fig_df['StartDate'])]

    if metric_attrib['usl'] != "NA":
        usl = metric_attrib['usl']
        p.line(time_range,
               2 * [usl],
               color='red',
               legend='Upper Specification: ' + usl)
    if metric_attrib['lsl'] != "NA":
        lsl = metric_attrib['lsl']
        p.line(time_range,
               2 * [lsl],
               color='red',
               legend='Lower Specification: ' + lsl)

    upl = metric_attrib['upl']
    lpl = metric_attrib['lpl']
    p.line(time_range,
           2 * [upl],
           color='orange',
           legend='Upper Process Limit: ' + upl)
    p.line(time_range,
           2 * [lpl],
           color='orange',
           legend='Lower Process Limit: ' + lpl)

    #Plot from each ColumnDataSource with a different color
    i = 0
    while i < color_cnt:
        p.circle('StartDate',
                 metric.value,
                 size=10,
                 color=color_set[i % len(color_set)],
                 source=main_source[i])
        i += 1

    return p
Beispiel #18
0
def _heatmap_summary(pvals, coefs, plot_width=1200, plot_height=400):
    """ Plots heatmap of coefficients colored by pvalues

    Parameters
    ----------
    pvals : pd.DataFrame
        Table of pvalues where rows are balances and columns are
        covariates.
    coefs : pd.DataFrame
        Table of coefficients where rows are balances and columns are
        covariates.
    plot_width : int, optional
        Width of plot.
    plot_height : int, optional
        Height of plot.

    Returns
    -------
    bokeh.charts.Heatmap
        Heatmap summarizing the regression statistics.
    """
    c = coefs.reset_index()
    c = c.rename(columns={'index': 'balance'})

    # fix alpha in fdr to account for the number of covariates
    def fdr(x):
        return multipletests(x, method='fdr_bh',
                             alpha=0.05 / pvals.shape[1])[1]

    cpvals = pvals.apply(fdr, axis=0)

    # log scale for coloring
    log_p = -np.log10(cpvals + 1e-200)
    log_p = log_p.reset_index()
    log_p = log_p.rename(columns={'index': 'balance'})
    p = pvals.reset_index()
    p = p.rename(columns={'index': 'balance'})

    cp = cpvals.reset_index()
    cp = cp.rename(columns={'index': 'balance'})

    cm = pd.melt(c,
                 id_vars='balance',
                 var_name='Covariate',
                 value_name='Coefficient')
    pm = pd.melt(p,
                 id_vars='balance',
                 var_name='Covariate',
                 value_name='Pvalue')
    cpm = pd.melt(cp,
                  id_vars='balance',
                  var_name='Covariate',
                  value_name='Corrected_Pvalue')
    logpm = pd.melt(log_p,
                    id_vars='balance',
                    var_name='Covariate',
                    value_name='log_Pvalue')
    m = pd.merge(cm,
                 pm,
                 left_on=['balance', 'Covariate'],
                 right_on=['balance', 'Covariate'])
    m = pd.merge(m,
                 logpm,
                 left_on=['balance', 'Covariate'],
                 right_on=['balance', 'Covariate'])
    m = pd.merge(m,
                 cpm,
                 left_on=['balance', 'Covariate'],
                 right_on=['balance', 'Covariate'])

    hover = HoverTool(tooltips=[(
        "Pvalue",
        "@Pvalue"), ("Corrected Pvalue",
                     "@Corrected_Pvalue"), ("Coefficient", "@Coefficient")])

    N, _min, _max = len(palette), m.log_Pvalue.min(), m.log_Pvalue.max()
    X = pd.Series(np.arange(len(pvals.index)), index=pvals.index)
    Y = pd.Series(np.arange(len(pvals.columns)), index=pvals.columns)
    m['X'] = [X.loc[i] for i in m.balance]
    m['Y'] = [Y.loc[i] for i in m.Covariate]

    # fill in nans with zero.  Sometimes the pvalue calculation fails.
    m = m.fillna(0)
    for i in m.index:
        x = m.loc[i, 'log_Pvalue']
        ind = int(np.floor((x - _min) / (_max - _min) * (N - 1)))
        m.loc[i, 'color'] = palette[ind]
    source = ColumnDataSource(ColumnDataSource.from_df(m))
    hm = figure(title='Regression Coefficients Summary',
                plot_width=1200,
                plot_height=400,
                tools=[
                    hover,
                    PanTool(),
                    BoxZoomTool(),
                    WheelZoomTool(),
                    ResetTool(),
                    SaveTool()
                ])
    hm.rect(x='X',
            y='Y',
            width=1,
            height=1,
            fill_color='color',
            line_color="white",
            source=source)
    Xlabels = pd.Series(pvals.index, index=np.arange(len(pvals.index)))
    Ylabels = pd.Series(
        pvals.columns,
        index=np.arange(len(pvals.columns)),
    )

    hm.xaxis[0].ticker = FixedTicker(ticks=Xlabels.index)
    hm.xaxis.formatter = FuncTickFormatter(code="""
    var labels = %s;
    return labels[tick];
    """ % Xlabels.to_dict())

    hm.yaxis[0].ticker = FixedTicker(ticks=Ylabels.index)
    hm.yaxis.formatter = FuncTickFormatter(code="""
    var labels = %s;
    return labels[tick];
    """ % Ylabels.to_dict())

    return hm
    elif symbol == 'EBAY':
        lengthOfTime = '1y'
        break
    else:
        print("Oops you typed it in wrong, try again... \n")

df = get_symbol_df(symbol, lengthOfTime)
stock = ColumnDataSource(data=dict(date=[],
                                   Open=[],
                                   Close=[],
                                   high=[],
                                   low=[],
                                   volume=[],
                                   volHeight=[],
                                   index=[]))
stock.data = stock.from_df(df)

p_stock = plot_stock_price(stock, symbol, lengthOfTime)

while True:
    fifty = input("An orange 50 period moving average? Y/N: ").upper()
    if fifty == 'Y':
        plot_fifty_ma(df, p_stock)
        break
    elif fifty == 'N':
        break
    else:
        print("Oops you typed it in wrong, try again... \n")

while True:
    bollinger = input(
Beispiel #20
0
def modify_doc(doc):
    """ Contains the application, including all callbacks
        TODO: could the callbacks be outsourced?
    :param doc:
    :type doc:
    """
    logger.debug('modify_doc has been called')
    def get_data_frames(ie,):
        """ Called one time initially, and then every time the experiment number is changed by the slider
        :param ie: experiment number
        :type ie: int
        :returns: dataframe from stella datafile and dataframe with tau and phi and fitted values
        :rtype: list of 2 pandas dataframes
        """
        logger.debug('get_dataframe with ie={}'.format(ie))
        fid = polymer.getfid(ie) #read FID or series of FIDs for selected experiment
        try:
            tau = polymer.get_tau_axis(ie) #numpy array containing the taus for experiment ie
            try:
                startpoint=fid_slider.range[0] #lower integration bound
                endpoint = fid_slider.range[1] #upper integration bound
            except NameError:
                # fid_slider not initialized for first plot. Use default values:
                startpoint=int(0.05*polymer.getparvalue(ie,'BS'))
                endpoint = int(0.1*polymer.getparvalue(ie,'BS'))
                logger.debug('fid_slider not initialized for first plot. Use default values {} and {}.'.format(startpoint, endpoint))
                
            polymer.addparameter(ie,'fid_range',(startpoint,endpoint)) #add integration range to parameters to make it accesible
            phi = get_mag_amplitude(fid, startpoint, endpoint,
                                    polymer.getparvalue(ie,'NBLK'),
                                    polymer.getparvalue(ie,'BS')) # list containing averaged fid amplitudes (which is proportional to a magnetization phi)
            df = pd.DataFrame(data=np.c_[tau, phi], columns=['tau', 'phi']) # DataFrames are nice
            df['phi_normalized'] = (df['phi'] - df['phi'].iloc[0] ) / (df['phi'].iloc[-1] - df['phi'].iloc[1] ) #Normalize magnetization,
            #Note: in the normalized magnetization the magnetization build-up curves and magnetization decay curves look alike
            #Note: this makes it easier for fitting as everything looks like 1 * exp(-R/time) in first order
            polymer.addparameter(ie,'df_magnetization',df) # make the magnetization dataframe accesible as parameter
            fit_option = 2 #mono exponential, 3 parameter fit
            p0=[1.0, polymer.getparvalue(ie,'T1MX')**-1*2, 0] #choose startparameters for fitting an exponential decay
            df, popt = magnetization_fit(df, p0, fit_option) # use leastsq to find optimal parameters
            polymer.addparameter(ie,'popt(mono_exp)',popt) # add fitting parameters for later access
            logger.info('fitfunction(t) = {} * exp(- {} * t) + {}'.format(*popt)) # print the fitting parameters to console (for convenience)
        except KeyError:
            logger.warning('no relaxation experiment found')
            tau=np.zeros(1)
            phi=np.zeros(1)
            df = pd.DataFrame(data=np.c_[tau, phi], columns=['tau', 'phi'])
            df['phi_normalized'] = np.zeros(1)
            df['fit_phi'] = np.zeros(1)
        return fid, df

    def calculate_mag_dec(attr, old, new, start_ie=None):
        ''' Is being call from the callback for the experiment chooser
            loads selected experiment visualize in plot p1 and p2 
            gets experiment number from the slider
            writes source_fid.data from the fid from the polymer object
            writes source_mag_dec.data from the dataframe
            '''
        ie = experiment_slider.value   #get expermient number from the slider
        logger.debug('calculate mag_dec for ie={}'.format(ie))
        fid, df = get_data_frames(ie)
        source_fid.data=ColumnDataSource.from_df(fid) #convert fid to bokeh format
        source_mag_dec.data = ColumnDataSource.from_df(df)

    def plot_par():
        ''' Creates plot for the parameters 
            Called with every update from the callback'''
        logger.debug('creating plot for the parameters')

        # read data due to selection of select_x/y
        xs = par_df[select_xaxis.value ].values
        ys = par_df[select_yaxis.value].values
        # read titles due to name of select_x/y
        x_title = select_xaxis.value.title()
        y_title = select_yaxis.value.title()

        # remark: many attributes in a bokeh plot cannot be modified after initialization
        #         for example p4.x_axis_type='datetime' does not work. keywords are a
        #         workaround to pass all optional arguments initially
        # set optional keyword arguments, kw, for figure()
        kw = dict() #initialize
        if select_xaxis.value in discrete:
            kw['x_range'] = sorted(set(xs))
        if select_yaxis.value in discrete:
            kw['y_range'] = sorted(set(ys))
        if select_yaxis.value in time:
            kw['y_axis_type'] = 'datetime'
        if select_xaxis.value in time:
            kw['x_axis_type'] = 'datetime'
        kw['title']="%s vs %s" % (x_title, y_title)
        # create figure using optional keywords kw
        p4 = figure(plot_height=300, plot_width=600, tools='pan,box_zoom,reset',
                    **kw)
        # set axis label
        p4.xaxis.axis_label = x_title
        p4.yaxis.axis_label = y_title

        # strings at x axis ticks need a lot of space. solution: rotate label orientation
        if select_xaxis.value in discrete:
            p4.xaxis.major_label_orientation = pd.np.pi / 4 # rotates labels...

        # standard size of symbols
        sz = 9
        # custom size of symbols due to select_size
        if select_size.value != 'None':
            groups = pd.qcut(pd.to_numeric(par_df[select_size.value].values), len(SIZES))
            sz = [SIZES[xx] for xx in groups.codes]

        # standard color
        c = "#31AADE"        
        # custom color due to select_color
        if select_color.value != 'None':
            groups = pd.qcut(pd.to_numeric(par_df[select_color.value]).values, len(COLORS))
            c = [COLORS[xx] for xx in groups.codes]

        # create the plot using circles
        p4.circle(x=xs, y=ys, color=c, size=sz, line_color="white", alpha=0.6, hover_color='white', hover_alpha=0.5)
        return p4 #return the plot
    
    def callback_update_plot_1(attr, old, new):
        ''' Callback for update of figure 1 in parameters tab '''
        tabs.tabs[1].child.children[1] = plot_par()
        print(tabs.tabs[1].child.children[1])
        logger.debug('Parameter plot updated')
#        p4 = plot_par()

    def callback_update_p3():
        logger.debug('update plot 3')
        p3 = fit_mag_decay_all(polymer,par_df)
        return p3

    def callback_update_experiment(attr, old, new):
        """ Callback for the experiment chooser
        """
        ie = experiment_slider.value
        logger.debug('Callback experiment update, ie={}'.format(ie))
        fid_slider.end = polymer.getparvalue(ie,'BS')
        try:
            fid_slider.range=polymer.getparvalue(ie,'fid_range')
        except:
            startpoint = int(0.05 * polymer.getparvalue(ie,'BS'))
            endpoint = int(0.1 * polymer.getparvalue(ie,'BS'))
            fid_slider.range=(startpoint,endpoint)
        calculate_mag_dec(attr,old,new)
        
    def callback_load_more_data(attr,old,new):
        ''' callback for loading of data '''
        # TODO: implement
        logger.debug('callback for loading of data ')
        logger.error('Not implemented!')
        path=pathbox.value.strip()
        file=filebox.value.strip()
        if file=="*.sdf":
            logger.info('callback for loading data. filename: {}'.format(file))
            allsdf=filter(lambda x: x.endswith('.sdf'),os.listdir(path))
            for f in allsdf:
                sdf_list.append(sdf.StelarDataFile(f,path))
        else:
            sdf_list.append(sdf.StelarDataFile(file,path))
        
        filenames=[x.file() for x in sdf_list]
        filenames_df=pd.DataFrame(data=filenames,columns=['file'])
        table_source.data=ColumnDataSource.from_df(filenames_df)

    def callback_export_data(attr,old,new):
        logger.debug('callback_export_data has been called ')
        logger.error('Not implemented!')
        pass
    
    def callback_write_table_to_file(attr,old,new): ##FIXME
        logger.debug('callback_write_table_to_file has been called ')
        logger.error('Not implemented!')
        pass
#        path=export_text.value.strip()
#        exportdata=export_source.data
#        CustomJS(args=dict(source=export_source),
#                 code=open(join(dirname(__file__), "export_csv.js")).read())

    def callback_update_parameters():
        ''' callback for button
            function to call when button is clicked
            for updates parameters of polymer, since they can change during evaluation '''
        logger.debug('callback for button (update parameter).')
        par_df, columns, discrete, continuous, time, quantileable = polymer.scan_parameters()
        select_xaxis.options=columns
        select_yaxis.options=columns
        select_size.options=['None']+quantileable
        select_color.options=['None']+quantileable

    logger.info('Starting the script')
    ### This is the start of the script ###
    ### The callbacks are above ###

    #load data:
    # TODO: how to handle multiple datafiles?
    # New Tab for each datafile?
    # dropdown selection to choose datafile
    # complete new start of process? (probably not prefered)

    polymer = load_data('glyzerin_d3_300K.sdf')
    nr_experiments = polymer.get_number_of_experiments()
    start_ie = 1     # initially set ie = 1
    par_df, columns, discrete, continuous, time, quantileable = polymer.scan_parameters(20)
    # for the initial call get the dataframes without callback
    # they are being updated in following callbacks
    fid, df = get_data_frames(start_ie)
    source_fid = ColumnDataSource(data=ColumnDataSource.from_df(fid))
    source_mag_dec = ColumnDataSource(data=ColumnDataSource.from_df(df))
    # initialy creates the plots p1 and p2
    p1, p2 = create_plot_1_and_2(source_fid, source_mag_dec)
    
    ### initiates widgets, which will call the callback on change ###
    # initiate slider to choose experiment
    experiment_slider = Slider(start=1, end=nr_experiments, value=1, step=1,callback_policy='mouseup', width=800) #select experiment by value
    # initiate slider for the range in which fid shall be calculated
    # select the intervall from which magneitization is calculated from fid
    fid_slider = RangeSlider(start=1,end=polymer.getparvalue(start_ie,'BS'),
                             range=polymer.getparvalue(start_ie,'fid_range'),
                             step=1,callback_policy='mouseup', width=400)

    # fit magnetization decay for all experiments
    p3 = fit_mag_decay_all(polymer, par_df)
    # refit mag dec with updated ranges after button push
    button_refit = Button(label='Update',button_type="success")
    button_refit.on_click(callback_update_p3)

    # initialize empty source for experiment slider
    source = ColumnDataSource(data=dict(value=[]))
    # 'data' is the attribute. it's a field in source, which is a ColumnDataSource
    # initiate callback_update_experiment which is the callback
    source.on_change('data',callback_update_experiment) #source for experiment_slider
    experiment_slider.callback = CustomJS(args=dict(source=source),code="""
        source.data = { value: [cb_obj.value] }
    """)#unfortunately this customjs is needed to throttle the callback in current version of bokeh

    # initialize empty source for fid slider, same as above
    source2 = ColumnDataSource(data=dict(range=[], ie=[]))
    source2.on_change('data',calculate_mag_dec)
    fid_slider.callback=CustomJS(args=dict(source=source2),code="""
        source.data = { range: cb_obj.range }
    """)#unfortunately this customjs is needed to throttle the callback in current version of bokeh

    # same for the update button
    button_scan = Button(label='Scan Parameters',button_type="success")
    button_scan.on_click(callback_update_parameters)
    
    # here comes for callbacks for x, y, size, color
    select_xaxis = Select(title='X-Axis', value='ZONE', options=columns)
    select_xaxis.on_change('value', callback_update_plot_1)

    select_yaxis = Select(title='Y-Axis', value='TIME', options=columns)
    select_yaxis.on_change('value', callback_update_plot_1)

    select_size = Select(title='Size', value='None', options=['None'] + quantileable)
    select_size.on_change('value', callback_update_plot_1)

    select_color = Select(title='Color', value='None', options=['None'] + quantileable)
    select_color.on_change('value', callback_update_plot_1)

    controls_p4 = widgetbox([button_scan, select_xaxis,select_yaxis,select_color,select_size], width=150)
    #p4 = plot_par()
    layout_p4 = row(controls_p4,plot_par())
    logger.debug('layout for parameter plot created')

    ####
    #### TODO: write file input
    #### TODO: select files to import
    #### TODO: discard imported files
    ####

    # load more data:
    table_source=ColumnDataSource(data=dict())
    sdf_list=[polymer]
    # TODO: This is current plan, to save the different dataframes in a list, right?
    filenames=[x.file() for x in sdf_list]
    files_df=pd.DataFrame(data=filenames,columns=['file'])
    table_source.data=ColumnDataSource.from_df(files_df)
    t_columns = [
        TableColumn(field='file', title='Path / Filename'),
        #TableColumn(field='file', title='Filename'),
        ]
    table=DataTable(source=table_source,columns=t_columns)
    pathbox=TextInput(title="Path",value=os.path.curdir)
    filebox=TextInput(title="Filename",value="*.sdf")
    pathbox.on_change('value',callback_load_more_data)
    filebox.on_change('value',callback_load_more_data)
    layout_input=column(pathbox,filebox,table)

    # Data Out: export data from figures
    #         & export parameters

    export_source=ColumnDataSource(data=dict())
    export_columns=[]
    output_table=DataTable(source=export_source,columns=export_columns)
    export_slider = Slider(start=1, end=4, value=3, step=1,callback_policy='mouseup', width=200) #do we need mouseup on this?
    export_slider.on_change('value',callback_export_data)
    export_text = TextInput(title="Path",value=os.path.curdir)
    export_button = Button(label='Export to csv',button_type="success") # FIXME Callback  doesn't work yet
    export_button.on_click(callback_write_table_to_file)
 
    layout_output=row(column(export_slider,export_text,export_button),output_table)
    print('after layout_output')
    

    # set the layout of the tabs
    layout_p1 = column(experiment_slider, p1,
                       row(
                           column(fid_slider,p2),
                           column(button_refit, p3)
                           ),
                       )
    tab_relaxation = Panel(child = layout_p1, title = 'Relaxation')
    tab_parameters = Panel(child = layout_p4, title = 'Parameters')
    tab_input = Panel(child = layout_input, title = 'Data In')
    tab_output = Panel(child = layout_output, title = 'Data Out')

    # initialize tabs object with 3 tabs
    tabs = Tabs(tabs = [tab_relaxation, tab_parameters,
                        tab_input, tab_output])
    print('tabs')
    doc.add_root(tabs)
    doc.add_root(source) # i need to add source to detect changes
    doc.add_root(source2)
    print('tab tab')
Beispiel #21
0
    price_hover = p.select(dict(type=HoverTool))

    # Choose, which glyphs are active by glyph name
    price_hover.names = ["price"]
    # Creating tooltips
    price_hover.tooltips = [("Datetime", "@Date{%Y-%m-%d}"),
                            ("Open", "@Open{$0,0.00}"),
                            ("Close", "@Close{$0,0.00}"),
                            ("Volume", "@Volume{($ 0.00 a)}")]
    price_hover.formatters = {"Date": 'datetime'}

    return p


stock = ColumnDataSource(
    data=dict(Date=[], Open=[], Close=[], High=[], Low=[], index=[]))

myPortfolio = Portfolio(['CSCO', 'DTE.DE', 'NOK'], [1, 1, 2], [1, 1, 1],
                        [1, 1, 1])
sample_df = pd.DataFrame(data=myPortfolio.share_data['CSCO']).dropna()
sample_df.reset_index(inplace=True)

stock.data = stock.from_df(sample_df)
elements = list()

p_stock = plot_stock_price(stock)
elements.append(p_stock)

curdoc().add_root(column(elements))
curdoc().title = 'CSCO Data'
Beispiel #22
0
def dashboard(request):
    current_user = request.user
    user = Profile.objects.get(user = current_user)
    if request.user.is_authenticated:
        all_stocks = []
        all_portfolios_of_current_users = Portfolio.objects.filter(user = user)
        for indv in all_portfolios_of_current_users:
            all_stocks.append(indv.stock)
    else:
        return redirect(landingpage)
    if not all_stocks:
        all_stocks = ["MMM", "ABT", "ABBV", "ABMD", "ACN", "ATVI", "AMZN", "FB", "GOOGL", "GOOG", "JNJ" ]

    if request.method == 'POST':
        symbol = request.POST.get('symbol')
        # Get Stock DataFrame
        # msft = yf.Ticker("MSFT")
        msft = yf.Ticker(symbol)
        hist = msft.history(period='max')

        # Define constants
        W_PLOT = 1000
        H_PLOT = 400
        TOOLS = 'pan,wheel_zoom,hover,reset'

        VBAR_WIDTH = 0.2
        RED = Category20[7][6]
        GREEN = Category20[5][4]

        BLUE = Category20[3][0]
        BLUE_LIGHT = Category20[3][1]

        ORANGE = Category20[3][2]
        PURPLE = Category20[9][8]
        BROWN = Category20[11][10]

        def get_symbol_df(symbol=None):
            df = pd.DataFrame(hist)[-50:]
            df.reset_index(inplace=True)
            df["Date"] = pd.to_datetime(df["Date"])
            return df

        def plot_stock_price(stock):
        
            p = figure(plot_width=W_PLOT, plot_height=H_PLOT, tools=TOOLS,
                    title="Stock price", toolbar_location='above')

            inc = stock.data['Close'] > stock.data['Open']
            dec = stock.data['Open'] > stock.data['Close']
            view_inc = CDSView(source=stock, filters=[BooleanFilter(inc)])
            view_dec = CDSView(source=stock, filters=[BooleanFilter(dec)])

            # map dataframe indices to date strings and use as label overrides
            p.xaxis.major_label_overrides = {i+int(stock.data['index'][0]): date.strftime('%b %d') for i, date in enumerate(pd.to_datetime(stock.data["Date"]))}
            p.xaxis.bounds = (stock.data['index'][0], stock.data['index'][-1])


            p.segment(x0='index', x1='index', y0='Low', y1='High', color=RED, source=stock, view=view_inc)
            p.segment(x0='index', x1='index', y0='Low', y1='High', color=GREEN, source=stock, view=view_dec)

            p.vbar(x='index', width=VBAR_WIDTH, top='Open', bottom='Close', fill_color=BLUE, line_color=BLUE,source=stock,view=view_inc, name="price")
            p.vbar(x='index', width=VBAR_WIDTH, top='Open', bottom='Close', fill_color=RED, line_color=RED,source=stock,view=view_dec, name="price")

            p.legend.location = "top_left"
            p.legend.border_line_alpha = 0
            p.legend.background_fill_alpha = 0
            p.legend.click_policy = "mute"

            p.yaxis.formatter = NumeralTickFormatter(format='$ 0,0[.]000')
            p.x_range.range_padding = 0.05
            p.xaxis.ticker.desired_num_ticks = 40
            p.xaxis.major_label_orientation = 3.14/4
            
            # Select specific tool for the plot
            price_hover = p.select(dict(type=HoverTool))

            # Choose, which glyphs are active by glyph name
            price_hover.names = ["price"]
            # Creating tooltips
            price_hover.tooltips = [("Datetime", "@Date{%Y-%m-%d}"),
                                    ("Open", "@Open{$0,0.00}"),
                                    ("Close", "@Close{$0,0.00}"),("Volume", "@Volume{($ 0.00 a)}")]
            price_hover.formatters={"Date": 'datetime'}

            return p

        
        stock = ColumnDataSource(data=dict(Date=[], Open=[], Close=[], High=[], Low=[],index=[]))
        df = get_symbol_df(symbol)
        stock.data = stock.from_df(df)
        elements = list()

        # update_plot()
        p_stock = plot_stock_price(stock)

        elements.append(p_stock)

        curdoc().add_root(column(elements))
        curdoc().title = 'Bokeh stocks historical prices'

        #show(p_stock)

        script, div = components(p_stock)
        kwargs = {'script': script, 'div': div}
        
        stock_list = all_stocks
        er = EventRegistry(apiKey = '3a7a023b-6280-4476-bec0-5c9ff41770bd')
        q = QueryArticlesIter(
            keywords = QueryItems.OR(stock_list),
            dataType = ["news"],
            lang = 'eng'
        )
        news_list = q.execQuery(er, sortBy = ["rel","date","sourceImportance"], maxItems = 5, )

        url = "https://apidojo-yahoo-finance-v1.p.rapidapi.com/stock/get-detail"

        querystring = {"region":"US","lang":"en","symbol":symbol}

        headers = {
            'x-rapidapi-host': "apidojo-yahoo-finance-v1.p.rapidapi.com",
            'x-rapidapi-key': "ff4cff81d5msh596a92309f2f43ap13412cjsn002bc2a74b4d"
            }

        response = requests.request("GET", url, headers=headers, params=querystring)
        json_dict  = json.loads(response.text)


        for key in json_dict.keys():
            if key == "summaryDetail":
                try:
                    stock_close = json_dict[key]['previousClose']['raw']
                except KeyError:
                    stock_close = ''
                    
                try:
                    stock_open = json_dict[key]['open']['raw']
                except KeyError:
                    stock_open = ''
                try:
                    stock_bid = json_dict[key]['bid']['raw']
                except KeyError:
                    stock_bid = ''
                try:
                    stock_ask = json_dict[key]['ask']['raw']
                except KeyError:
                    stock_ask = ''
                try:
                    stock_volume = json_dict[key]['volume']['fmt']
                except KeyError:
                    stock_volume = ''
                try:
                    stock_averageVolume = json_dict[key]['averageVolume']['fmt']
                except KeyError:
                    stock_averageVolume = ''
                try:
                    stock_marketCap = json_dict[key]['marketCap']['fmt']    
                except KeyError:  
                    stock_marketCap = ''
            if key == "defaultKeyStatistics":
                try:
                    stock_weekChange = json_dict[key]['52WeekChange']['fmt']
                except KeyError:
                    stock_weekChange = ''
                try:
                    stock_beta = json_dict[key]['beta']['fmt']
                except KeyError:
                    stock_beta = ''
                try:
                    stock_EPS = json_dict[key]['trailingEps']['raw']
                except KeyError:
                    stock_EPS = ''
                try:
                    stock_PE = json_dict[key]['forwardPE']['fmt']
                except KeyError:
                    stock_PE = ''

        return render(request, 'dashboard.html', {**kwargs, 'news_list' : news_list,'stock_close':stock_close,'stock_open':stock_open,'stock_bid':stock_bid,'stock_ask':stock_ask,'stock_volume':stock_volume,'stock_averageVolume':stock_averageVolume,'stock_marketCap':stock_marketCap,'stock_weekChange':stock_weekChange,'stock_beta':stock_beta,'stock_EPS':stock_EPS,'stock_PE':stock_PE,'stock_list':stock_list,'symbol':symbol}) 
    else:
        
        symbol = all_stocks[0]
        stock_list = all_stocks

        msft = yf.Ticker(symbol)
        hist = msft.history(period='max')

        # Define constants
        W_PLOT = 1000
        H_PLOT = 400
        TOOLS = 'pan,wheel_zoom,hover,reset'

        VBAR_WIDTH = 0.2
        RED = Category20[7][6]
        GREEN = Category20[5][4]

        BLUE = Category20[3][0]
        BLUE_LIGHT = Category20[3][1]

        ORANGE = Category20[3][2]
        PURPLE = Category20[9][8]
        BROWN = Category20[11][10]

        def get_symbol_df(symbol=None):
            df = pd.DataFrame(hist)[-50:]
            df.reset_index(inplace=True)
            df["Date"] = pd.to_datetime(df["Date"])
            return df

        def plot_stock_price(stock):
        
            p = figure(plot_width=W_PLOT, plot_height=H_PLOT, tools=TOOLS,
                    title="Stock price", toolbar_location='above')

            inc = stock.data['Close'] > stock.data['Open']
            dec = stock.data['Open'] > stock.data['Close']
            view_inc = CDSView(source=stock, filters=[BooleanFilter(inc)])
            view_dec = CDSView(source=stock, filters=[BooleanFilter(dec)])

            # map dataframe indices to date strings and use as label overrides
            p.xaxis.major_label_overrides = {i+int(stock.data['index'][0]): date.strftime('%b %d') for i, date in enumerate(pd.to_datetime(stock.data["Date"]))}
            p.xaxis.bounds = (stock.data['index'][0], stock.data['index'][-1])


            p.segment(x0='index', x1='index', y0='Low', y1='High', color=RED, source=stock, view=view_inc)
            p.segment(x0='index', x1='index', y0='Low', y1='High', color=GREEN, source=stock, view=view_dec)

            p.vbar(x='index', width=VBAR_WIDTH, top='Open', bottom='Close', fill_color=BLUE, line_color=BLUE,source=stock,view=view_inc, name="price")
            p.vbar(x='index', width=VBAR_WIDTH, top='Open', bottom='Close', fill_color=RED, line_color=RED,source=stock,view=view_dec, name="price")

            p.legend.location = "top_left"
            p.legend.border_line_alpha = 0
            p.legend.background_fill_alpha = 0
            p.legend.click_policy = "mute"

            p.yaxis.formatter = NumeralTickFormatter(format='$ 0,0[.]000')
            p.x_range.range_padding = 0.05
            p.xaxis.ticker.desired_num_ticks = 40
            p.xaxis.major_label_orientation = 3.14/4
            
            # Select specific tool for the plot
            price_hover = p.select(dict(type=HoverTool))

            # Choose, which glyphs are active by glyph name
            price_hover.names = ["price"]
            # Creating tooltips
            price_hover.tooltips = [("Datetime", "@Date{%Y-%m-%d}"),
                                    ("Open", "@Open{$0,0.00}"),
                                    ("Close", "@Close{$0,0.00}"),("Volume", "@Volume{($ 0.00 a)}")]
            price_hover.formatters={"Date": 'datetime'}

            return p

        
        stock = ColumnDataSource(data=dict(Date=[], Open=[], Close=[], High=[], Low=[],index=[]))
        df = get_symbol_df(symbol)
        stock.data = stock.from_df(df)
        elements = list()

        # update_plot()
        p_stock = plot_stock_price(stock)

        elements.append(p_stock)

        curdoc().add_root(column(elements))
        curdoc().title = 'Bokeh stocks historical prices'

        #show(p_stock)

        script, div = components(p_stock)
        kwargs = {'script': script, 'div': div}

        
        er = EventRegistry(apiKey = '3a7a023b-6280-4476-bec0-5c9ff41770bd')
        q = QueryArticlesIter(
            keywords = QueryItems.OR(stock_list),
            dataType = ["news"],
            lang = 'eng'
        )
        news_list = q.execQuery(er, sortBy = ["rel","date","sourceImportance"], maxItems = 5, )

        url = "https://apidojo-yahoo-finance-v1.p.rapidapi.com/stock/get-detail"

        querystring = {"region":"US","lang":"en","symbol":symbol}

        headers = {
            'x-rapidapi-host': "apidojo-yahoo-finance-v1.p.rapidapi.com",
            'x-rapidapi-key': "ff4cff81d5msh596a92309f2f43ap13412cjsn002bc2a74b4d"
            }

        response = requests.request("GET", url, headers=headers, params=querystring)
        json_dict  = json.loads(response.text)


        for key in json_dict.keys():
            if key == "summaryDetail":
                try:
                    stock_close = json_dict[key]['previousClose']['raw']
                except KeyError:
                    stock_close = ''
                    
                try:
                    stock_open = json_dict[key]['open']['raw']
                except KeyError:
                    stock_open = ''
                try:
                    stock_bid = json_dict[key]['bid']['raw']
                except KeyError:
                    stock_bid = ''
                try:
                    stock_ask = json_dict[key]['ask']['raw']
                except KeyError:
                    stock_ask = ''
                try:
                    stock_volume = json_dict[key]['volume']['fmt']
                except KeyError:
                    stock_volume = ''
                try:
                    stock_averageVolume = json_dict[key]['averageVolume']['fmt']
                except KeyError:
                    stock_averageVolume = ''
                try:
                    stock_marketCap = json_dict[key]['marketCap']['fmt']    
                except KeyError:  
                    stock_marketCap = ''
            if key == "defaultKeyStatistics":
                try:
                    stock_weekChange = json_dict[key]['52WeekChange']['fmt']
                except KeyError:
                    stock_weekChange = ''
                try:
                    stock_beta = json_dict[key]['beta']['fmt']
                except KeyError:
                    stock_beta = ''
                try:
                    stock_EPS = json_dict[key]['trailingEps']['raw']
                except KeyError:
                    stock_EPS = ''
                try:
                    stock_PE = json_dict[key]['forwardPE']['fmt']
                except KeyError:
                    stock_PE = ''

        return render(request, 'dashboard.html', {**kwargs, 'news_list' : news_list,'stock_close':stock_close,'stock_open':stock_open,'stock_bid':stock_bid,'stock_ask':stock_ask,'stock_volume':stock_volume,'stock_averageVolume':stock_averageVolume,'stock_marketCap':stock_marketCap,'stock_weekChange':stock_weekChange,'stock_beta':stock_beta,'stock_EPS':stock_EPS,'stock_PE':stock_PE,'stock_list':stock_list,'symbol':symbol})