def __draw_compare_tables(self):
        """ Compare multiple tables """

        # Sync X and Y range
        max_xaxis = 0
        for table in self.__table:
            max_xaxis = max(max_xaxis, self.__figures.get_max(table, 'cycle'))

        max_yaxis = {}
        for yaxis in self.__yaxis:
            max_val = 0
            for table in self.__table:
                max_val = max(max_val, self.__figures.get_max(table, yaxis))
            max_yaxis[yaxis] = max_val

        for yaxis in self.__yaxis:
            figures = []
            num_tables = str(len(self.__table)) + '_tables'
            output_name = '_'.join(
                [self.__trace, num_tables, self.__xaxis, yaxis])
            output_file(output_name + '.html', title=output_name)

            for table in self.__table:
                xaxis = self.__xaxis
                plot, plot_hist = self.__figures.plot_t_x_y(FIGURE_WIDTH,
                                                            FIGURE_HEIGHT,
                                                            table,
                                                            xaxis,
                                                            yaxis,
                                                            max_xaxis,
                                                            max_yaxis[yaxis])
                figures.append([plot, plot_hist])

            plot = gridplot(figures)
            show(plot)
Exemple #2
0
def _create_grid_plot_of_trends(df, X, col_list, filename):

    width  = 600
    height = 400
        
    color_palette = [ 'Black', 'Red', 'Purple', 'Green', 'Brown', 'Yellow', 'Cyan', 'Blue', 'Orange', 'Pink']
    i = 0
    #2 columns, so number of rows is total /2 
    row_index = 0
    row_list = []
    row = []
    for col in col_list[1:]: #skip the date column
        # create a new plot
        s1 = figure(x_axis_type = 'datetime', width=width, plot_height=height, title=col + ' trend')
        #seasonal decompae to extract seasonal trends
        decomposition = seasonal_decompose(np.array(df[col]), model='additive', freq=15)  
        s1.line(X, decomposition.trend, color=color_palette[i % len(color_palette)], alpha=0.5, line_width=2)

        row.append(s1)
        if len(row) == 2:
            row_copy = copy.deepcopy(row)
            row_list.append(row_copy)
            row = []
            i = 0
        i += 1
        

    # put all the plots in a grid layout
    p = gridplot(row_list)

    save(vplot(p), filename=filename, title='trends')  
Exemple #3
0
def plot_true_and_estimated_channel_with_bokeh_all_antennas(
    true_channel, estimated_channel, title=''):
    p0 = plot_true_and_estimated_channel_with_bokeh(true_channel,
                                                    estimated_channel,
                                                    'Antenna 1', 0)
    p1 = plot_true_and_estimated_channel_with_bokeh(true_channel,
                                                    estimated_channel,
                                                    'Antenna 2', 1)
    p2 = plot_true_and_estimated_channel_with_bokeh(true_channel,
                                                    estimated_channel,
                                                    'Antenna 3', 2)
    p3 = plot_true_and_estimated_channel_with_bokeh(true_channel,
                                                    estimated_channel,
                                                    'Antenna 4', 3)
    p1.x_range = p0.x_range
    p2.x_range = p0.x_range
    p3.x_range = p0.x_range
    p1.y_range = p0.y_range
    p2.y_range = p0.y_range
    p3.y_range = p0.y_range

    p = gridplot([[p0, p1], [p2, p3]])

    par = bw.Paragraph(text=title)
    vbox = bw.VBox(children=[par, p])
    return vbox
Exemple #4
0
    def plot_memory_hist(self, mode='overview'):
        # Output to static HTML file
        output_file_name = self.get_output_file_name()
        output_file_name += "_memory_hist_" + mode
        output_file(output_file_name + '.html', title=output_file_name)

        # List of histogram figures
        figures = []
        data_df = []
        for trace in self.traces:
            mem_info = trace.get_memory_hists(mode)
            figures.append(mem_info['hist'])
            data_df.append(mem_info['info'])

        # Plot mean and median comparison bar charts
        bars = []
        for access_type_index in range(len(data_df[0])):
            data_df_list = []
            for index, item in enumerate(data_df):
                data_df_list.append(data_df[index][access_type_index])
            data_dfs = pd.concat(data_df_list)
            access_location = data_dfs['location'][0]
            fig_bar = Bar(data_dfs, values='value', group='sched',
                          color='color', legend='center', label='type',
                          title=access_location + ': Mean and Median',
                          ylabel='Cycles')
            bars.append(fig_bar)
        figures.append(bars)

        # Plot all figures
        plot = gridplot(figures)
        show(plot)
def simpleLine():
    x = list(range(11))
    y0 = x
    y1 = [10 - i for i in x]
    y2 = [abs(i - 5) for i in x]

    # create a new plot
    s1 = figure(width=500, plot_height=500, title=None)
    s1.circle(x, y0, size=10, color="navy", alpha=0.5)

    # create another one
    s2 = figure(width=500, height=500, title=None)
    s2.triangle(x, y1, size=10, color="firebrick", alpha=0.5)

    # create and another
    s3 = figure(width=500, height=500, title=None)
    s3.square(x, y2, size=10, color="olive", alpha=0.5)

    # create and another
    s4 = figure(width=500, height=500, title=None)
    s4.line(x, y2, size=10, color="olive", line_width=2)
    # p.line([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], line_width=2)

    # put all the plots in a grid layout
    p = gridplot([[s1, s2], [s3, s4]])
    script, div = components(p)
    return render_template('simpleline.html', div=div,
                           script=script)  #this line matters!
    output_file("simpleline.html")
    show(p)
Exemple #6
0
    def add_plot(self, new_plot, linked_x_axis = True):
        self.document.clear()
        new_plot.x_range.bounds = None
        new_plot.y_range.bounds = None
        for key, plot in enumerate(self.plots):
            if new_plot.title == plot.title:
                self.plots.pop(key)
                self.plots.append(new_plot)
                break
        else:
            self.plots.append(new_plot)
        if linked_x_axis:
            for plot in self.plots[1:]:
                plot.x_range = self.plots[0].x_range
                plot.x_range.bounds = None
                plot.y_range.bounds = None
                
        if len(self.plots) > 1:
            number_of_rows = int(round(len(self.plots) / 2))
            rows = []
            number_of_columns = 2
            i = 0
            for each in range(number_of_rows):
                rows.append(list(plot for plot in self.plots[i:i+number_of_columns]))
                i += number_of_columns
            layout = gridplot(rows)
        else:
            print('Layout...')
            layout = VBox(children=self.plots)            

        self.document.add_root(layout)
Exemple #7
0
    def add_plot(self, new_plot, linked_x_axis=True):
        self.document.clear()
        new_plot.x_range.bounds = None
        new_plot.y_range.bounds = None
        for key, plot in enumerate(self.plots):
            if new_plot.title == plot.title:
                self.plots.pop(key)
                self.plots.append(new_plot)
                break
        else:
            self.plots.append(new_plot)
        if linked_x_axis:
            for plot in self.plots[1:]:
                plot.x_range = self.plots[0].x_range
                plot.x_range.bounds = None
                plot.y_range.bounds = None

        if len(self.plots) > 1:
            number_of_rows = int(round(len(self.plots) / 2))
            rows = []
            number_of_columns = 2
            i = 0
            for each in range(number_of_rows):
                rows.append(
                    list(plot for plot in self.plots[i:i + number_of_columns]))
                i += number_of_columns
            layout = gridplot(rows)
        else:
            print('Layout...')
            layout = VBox(children=self.plots)

        self.document.add_root(layout)
def plotPreds(prediction, test, outputDir, parametersSet):
    reset_output()
    stocks = test.columns.values
    
    dataTest = test.reset_index()
    output_file(outputDir + '_'.join(parametersSet) + '_predPerf.html')
    colors_list = ['green', 'red']
    
    grid = []
    subGrid = []
    for i, stock in enumerate(sorted(stocks)):
        if i % 3 == 0 and i != 0:
            grid.append(subGrid)
            subGrid = []
        legends_list = [stock, 'reconstruction']
        xs = [dataTest['Date'], dataTest['Date']]
        ys = [dataTest[stock], prediction[stock]]
        
        p = figure(x_axis_type="datetime",
                   y_axis_label = "Log-return")
        for (colr, leg, x, y ) in zip(colors_list, legends_list, xs, ys):
            p.line(x, y, color=colr, legend=leg)
        subGrid.append(p)
    p = gridplot(grid)
    save(p)
    return True
def plotResiduals(residuals, outputDir, parametersSet, who):
    reset_output()
    stocks = residuals.columns.values
    
    res = residuals.reset_index()
    output_file(outputDir + '_'.join(parametersSet)  + '_residuals_' + who + '.html')
    
    grid = []
    subGrid = []
    for i, stock in enumerate(sorted(stocks)):
        if i % 3 == 0 and i != 0:
            grid.append(subGrid)
            subGrid = []
        p1 = figure(title=stock + ' ' + who + ' residuals', background_fill_color="#E8DDCB", x_axis_label='r - r_hat')
        p1.yaxis.visible = None
        p1.legend.location = "top_left"
        hist, edges = np.histogram(res[stock], density=True, bins=25)
        p1.quad(top=hist,
                bottom=0,
                left=edges[:-1],
                right=edges[1:],
                fill_color="#036564",
                line_color="#033649")
        subGrid.append(p1)
    p = gridplot(grid)
    save(p)
    return True
    def plot_memory_hist(self, mode='overview'):
        # Output to static HTML file
        prefix = self.trace_files[0].split('_')
        output_file_name = prefix[0] + "_memory_hist_" + mode + "_"
        output_file_name += prefix[4]
        output_file(output_file_name + '.html', title=output_file_name)

        # List of histogram figures
        figures = []
        data_df = []
        for trace in self.traces:
            mem_info = trace.get_memory_hists(mode)
            figures.append(mem_info['hist'])
            data_df.append(mem_info['info'])

        # Plot mean and median comparison bar charts
        bars = []
        for access_type_index in range(len(data_df[0])):
            data_df_list = []
            for index, item in enumerate(data_df):
                data_df_list.append(data_df[index][access_type_index])
            data_dfs = pd.concat(data_df_list)
            access_location = data_dfs['location'][0]
            fig_bar = Bar(data_dfs, values='value', group='sched',
                          color='color', legend='center', label='type',
                          title=access_location + ': Mean and Median',
                          ylabel='Cycles')
            bars.append(fig_bar)
        figures.append(bars)

        # Plot all figures
        plot = gridplot(figures)
        show(plot)
Exemple #11
0
def plotDateGrid(date):
    # Assert given date is available.
    assert date in authorKey(df, 'Capella',
                             'date'), 'No entries for this date.'
    # Create list of fitting dataframes
    tempList = [
        biblioList[s] for s in range(len(biblioList))
        if biblioList[s]['date'][0] in [date]
    ]
    # sort by occuring diagrams
    dfDATE = sorted(tempList,
                    key=lambda tempList: tempList['Count'].sum(),
                    reverse=True)
    plotListDATE = []
    for x in range(len(dfDATE)):
        titleS = dfDATE[x]['biblio'][0] + '; ' + str(
            dfDATE[x]['date'][0]) + ' CE'
        b0 = Bar(dfDATE[x],
                 title=titleS,
                 label='biblio',
                 values='Count',
                 group='diaTyp',
                 bar_width=1,
                 ylabel='Diagrams',
                 palette=viridis(14),
                 width=250,
                 height=250,
                 legend=False)
        b0.xaxis.major_label_orientation = "horizontal"
        b0.xaxis.axis_label = ''
        plotListDATE.append(b0)
    plotGrid = gridplot(plotListDATE, ncols=3)
    show(plotGrid)
Exemple #12
0
    def go(self):
        """

        :return:
        """

        print("\n\n" + '#' * 75)
        print("Server Engaged")
        print('#' * 75 + '\n\n')


        plots = self.make_all_plots()
        controls = self.make_all_controls()
        dt = self.make_data_table()
        dp = self.dist_plot()

        dp = gridplot(dp, plot_width=300, plot_height=200, ncols=2, height=400)
        dp = Panel(child=dp, title="Distribution Plot", width=400, height=400)

        dt = Panel(child=dt, title="Data Table", width=400, height=400)

        dtdp = Tabs(tabs=[dp,dt],width=400,height=400)


        self.layout =  column(children=[plots,row(controls,dtdp)])
        curdoc().add_root(self.layout)
        curdoc().title = 'Graph Projection Explorer'
Exemple #13
0
def poverty_health():
  page_title = "Poverty and health"
  xl_file = 'DataDownload.xls'
  data = pd.ExcelFile(xl_file)
  sheet_names = data.sheet_names
  all_dfs = {sheet: data.parse(sheet) for sheet in sheet_names}
  
  socioec_df = all_dfs['SOCIOECONOMIC']
  socioec_df = socioec_df[['State','County','POVRATE10']]
  health_df = all_dfs['HEALTH']
  health_df = health_df[['State','County','PCT_DIABETES_ADULTS10','PCT_OBESE_ADULTS10']]
  health_se_df = pd.merge(socioec_df,health_df,on=['State','County'])
  health_se_df.columns = ['State','County','Poverty Rate','Adult Diabetes Rate','Adult Obesity Rate']
  
  rm_lines = list()
  for i in range(len(health_se_df['Poverty Rate'])):
      item1 = health_se_df['Poverty Rate'][i]
      item2 = health_se_df['Adult Diabetes Rate'][i]
      item3 = health_se_df['Adult Obesity Rate'][i]
      if type(item1)==str:
          rm_lines.append(np.int(i))
      elif type(item1)==int:
          continue
      elif any([np.isnan(item1),np.isnan(item2),np.isnan(item3)]):
          rm_lines.append(np.int(i))
  health_se_df = health_se_df.drop(rm_lines)

  health_se_df['Poverty Rate'] = np.float64(health_se_df['Poverty Rate'])
  plot = figure(plot_width=500, plot_height=400,x_axis_label='Poverty Rate',y_axis_label='Adult Obesity Rate',title='Obesity increases with poverty rate - US counties 2010',title_text_font_size='12pt')
  plot.scatter(health_se_df['Poverty Rate'],health_se_df['Adult Obesity Rate'],marker='+',color='DarkSlateGray')
  plot.xaxis.axis_label_text_font_size='9pt'
  plot.yaxis.axis_label_text_font_size='9pt'
  plot.text(0,50,['A 10% increase in poverty rate corresponds to a 3% increase in obesity rate'],text_font_size='8pt')

  regr = np.polyfit(health_se_df['Poverty Rate'],health_se_df['Adult Obesity Rate'],1)

  mx_X = max(health_se_df['Poverty Rate'])
  vals_X = range(np.int(round(mx_X)))
  vals_Y = regr[0]*np.float64(vals_X) + regr[1]

  plot.line(vals_X,vals_Y,color='red',line_width=1.5)

  plot2 = figure(plot_width=500, plot_height=400,x_axis_label='Poverty Rate', y_axis_label='Adult Diabetes Rate', title='Diabetes increases with poverty rate - US counties 2010', title_text_font_size='12pt')
  plot2.scatter(health_se_df['Poverty Rate'],health_se_df['Adult Diabetes Rate'], marker='+',color='DarkSlateBlue')
  plot2.xaxis.axis_label_text_font_size='9pt'
  plot2.yaxis.axis_label_text_font_size='9pt'
  plot2.text(0,20,['a 10% increase in poverty rate corresponds to a 2% increase in diabetes rate'],text_font_size='8pt')
  
  regr2 = np.polyfit(health_se_df['Poverty Rate'],health_se_df['Adult Diabetes Rate'],1)
  mx_X2 = max(health_se_df['Poverty Rate'])
  vals_X2 = range(np.int(round(mx_X2)))
  vals_Y2 = regr2[0]*np.float64(vals_X2) + regr2[1]
  plot2.line(vals_X2,vals_Y2,color='red',line_width=1.5)

  p=gridplot([[plot,plot2]])
  script, div = components(p,CDN)
  script=script.strip()
  return render_template('myplot.html', page_title=page_title, script=script, div=div)
Exemple #14
0
    def draw(self):
        """ Draw memory figures """
        output_name = self.__trace + '_instruction_memory'
        output_file(output_name + '.html', title=output_name)

        figures = self.__figures.plot_modules()

        plot = gridplot(figures)
        show(plot)
    def draw(self):
        """ Draw pipeline """
        output_name = self.__trace + '_instruction_timeline'
        output_file(output_name + '.html', title=output_name)

        figures = self.__figures.plot_timeline_all_cu()

        plot = gridplot(figures)
        show(plot)
Exemple #16
0
def make_html_plots(G,outdir):

    px = plot_distance_distrib(G)
    py = plot_sem_distrib(G)
    pz = plot_node_degrees(G)
    p = gridplot([[px, py], [pz, None]])
    html = file_html(p, CDN, "Distance distribution")
    out = open(outdir+'/plots.html','w')
    out.write(html)
    out.close()
Exemple #17
0
def make_html_plots(G, outdir):

    px = plot_distance_distrib(G)
    py = plot_sem_distrib(G)
    pz = plot_node_degrees(G)
    p = gridplot([[px, py], [pz, None]])
    html = file_html(p, CDN, "Distance distribution")
    out = open(outdir + '/plots.html', 'w')
    out.write(html)
    out.close()
def show_multiple_practice_session_plots(paths, titles):
    bkp.output_file("analysis/plots/simulation-last.html",
                    title="Practice simulations")
    sessions = [pd.read_csv(path) for path in paths]
    figs = [plot_practice_session(session, title)
            for session, title in zip(sessions, titles)]
    if len(figs) % 2 == 1:
        figs += [None]
    grid = gridplot([[figs[i], figs[i+1]] for i in range(0, len(figs), 2)])
    bkp.show(grid)
Exemple #19
0
 def plot_all_topic_evolutions(self):
     model = self.model
     plots = []
     for i in range(model.num_topics):
         score, p = self.topic_evolution_by_year(i, sp=False)
         plots.append(p)
     plots = np.array(plots)
     plots = plots.reshape((3, 3))
     p = gridplot(plots.tolist())
     show(p)
     return plots
Exemple #20
0
def explore_vars(x,y, schedule):
    output_file("scatter.html")

    p1=create_scatter(x,y,schedule, 'Home EFG')
    p2=create_scatter(x,y,schedule, 'Home TOV')
    p3=create_scatter(x,y,schedule, 'Home ORB')
    p4=create_scatter(x,y,schedule, 'Home FTFGA')
    
    p=gridplot([[p1,p2],[p3,p4]])
    
    show(p)
Exemple #21
0
def plot_im_grid(imarray, dims):
    x, y, z  = dims
    plot_list = []
    for layer, depth in zip(range(imarray.shape[-1]), z):
        p = figure(x_range=[x[0], x[-1]], y_range=[y[0], y[-1]],
                title="Depth %.0f mm"%(depth))
        p.image(image=[np.squeeze(imarray[:,:,layer])], x=[0], y=[0],
                dw=x[-1], dh=y[-1], palette="Greys9")
        plot_list.append(p)
    grid = gridplot([plot_list[:6],plot_list[6:12],plot_list[12:]])
    show(grid)
    return components(grid)
def show_multiple_practice_session_plots(paths, titles):
    bkp.output_file("analysis/plots/simulation-last.html",
                    title="Practice simulations")
    sessions = [pd.read_csv(path) for path in paths]
    figs = [
        plot_practice_session(session, title)
        for session, title in zip(sessions, titles)
    ]
    if len(figs) % 2 == 1:
        figs += [None]
    grid = gridplot([[figs[i], figs[i + 1]] for i in range(0, len(figs), 2)])
    bkp.show(grid)
Exemple #23
0
    def create_topic_dist_for_all_years(self, sp=False):
        model = self.model
        doc_vecs = self.doc_vecs
        topic_labels = self.topic_labels
        df = self.df_papers

        sparse_vecs = np.array(
            [matutils.sparse2full(vec, model.num_topics) for vec in doc_vecs])
        years = [i for i in range(df['year'].min(), df['year'].max() + 1)]
        df_sp = pd.DataFrame(sparse_vecs)
        year_dist = []
        plots = []
        for year in years:
            df_dist = df_sp[df['year'] == year]
            top_topic = df_dist.sum().idxmax()
            value = df_dist.sum()[top_topic]
            print('Top topic: %s' % topic_labels[top_topic])
            print('Value: %f' % value)
            #data = df[df['year']<=year]
            #            ax=df_dist.sum().plot(kind='bar')
            #            ax.set_xticklabels(topic_labels, rotation=90)
            #            plt.title('Topic Scores for the year:%d' %year)
            #            plt.show()
            #

            df_f = pd.DataFrame({
                'Topic': self.topic_labels,
                'TopicScore': df_dist.sum().values
            })

            output_file("year_dist.html")

            p = Bar(df_f,
                    'Topic',
                    values='TopicScore',
                    title="Bar Plot of Topic Scores for the year %d" % year,
                    color='blue',
                    legend=False)
            plots.append(p)
        plots = np.array(plots)
        plots = plots.reshape((3, 10))

        year_dist.append(df_dist)
        pickle.dump(year_dist, open(self.year_dist_name, "wb"))
        self.year_dist = year_dist

        if (sp):
            show(gridplot(plots.tolist()))

        return year_dist
Exemple #24
0
    def set_data(self):
        d = self.data
        rows = []
        tools = 'pan,box_zoom,wheel_zoom,save,reset'

        # parameter estimations
        for param in d['parameter_names']:

            # kernel and histogram
            fig1 = figure(tools=tools,
                          width=400,
                          plot_height=200,
                          title="Parameter {}".format(param))

            hist, edges = np.histogram(d['parameters'][param],
                                       density=True,
                                       bins=25)
            fig1.quad(top=hist,
                      bottom=0,
                      left=edges[:-1],
                      right=edges[1:],
                      fill_color="#036564",
                      line_color="#033649")
            fig1.line(d['kernels'][param]['x'],
                      d['kernels'][param]['y'],
                      line_color="#D95B43",
                      line_width=4,
                      alpha=0.8)
            fig1.title.text_font_size = "12pt"

            # parameter variance w/ time-course
            source = ColumnDataSource(
                data=dict(x=range(len(d['parameters'][param])),
                          y=d['parameters'][param]))
            fig2 = figure(tools=tools,
                          width=500,
                          plot_height=200,
                          title="Parameter {}".format(param))
            fig2.line('x', 'y', color="#036564", line_width=1, source=source)
            fig2.title.text_font_size = "12pt"

            rows.append([fig1, fig2])

        self.plot = gridplot(rows,
                             toolbar_options={
                                 'logo': None,
                                 'merge_tools': True,
                                 'toolbar_location': 'above',
                             })
def plot_nodecounts_bokeh_line():
    datasets_csv = read_datasets_csv()
    datasets = sorted([dataset['name'] for dataset in datasets_csv])
    plots = []
    for dataset in datasets:
        output_file(PNG_FOLDER + dataset + '_nodes.html')
        x,y=get_node_counts_for_cutoffs(dataset=dataset)
        p = figure(title='node counts for ' + dataset, tools="pan,wheel_zoom,box_zoom,previewsave,reset")
        p.xaxis.axis_label='cut-off'
        p.yaxis.axis_label='# nodes'
        p.line(x,y, color="green")
        save(p)
        plots.append(p)
    output_file(TEMPLATE_FOLDER + 'bokeh_nodecount.html')
    gp = gridplot(plots, ncols=2, tools="pan,wheel_zoom,box_zoom,save,reset")
    save(gp)
Exemple #26
0
 def showMainPlots(self, data_MCT, data_avg, data_lsi, data_contr):
     output_file('output\\standard.html')
     p = plots.hbarMCT(data_MCT[0], data_MCT[1])
     q = plots.hbarLength(data_avg)
     donutList = []
     for topic in data_lsi:
         donutList.append(plots.donutLsi(topic))
     barList = []
     for topic in data_contr:
         barList.append(plots.barDocLsi(topic))
     lsiList = []
     for i in range(len(donutList)):
         lsiList.append(barList[i])
         lsiList.append(donutList[i])
     g = gridplot([[p, q], [d for d in lsiList]])
     show(g)
Exemple #27
0
    def plot_author_evolution_plot(self, a_id, plot=True):
        'Plots and returns the topic Score of the author by year'
        df_papers = self.df_papers
        years = [
            i for i in range(df_papers['year'].min(), df_papers['year'].max() +
                             1)
        ]
        df = pd.concat([
            pd.DataFrame({
                year:
                self.get_author_topic_dist_for_year(a_id, year, verbose=plot)
            }) for year in years
        ],
                       axis=1)
        plots = []
        if (plot):

            for y in years:
                if (df[y].sum() == 0):
                    print('Author didn\'t publish any papers in the year:%d' %
                          y)
                else:
                    #                    ax=df[y].plot(kind='bar')
                    #                    ax.set_xticklabels(self.topic_labels, rotation=90)
                    #                    plt.title('Topic Score of Author in the year: %d'%y)
                    #                    plt.show()
                    #
                    df_f = pd.DataFrame({
                        'Topic': self.topic_labels,
                        'TopicScore': df[y].values
                    })

                    output_file("authorscorebyyear.html")

                    p = Bar(
                        df_f,
                        'Topic',
                        values='TopicScore',
                        title="Bar Plot of Topic Distributions for the year %d"
                        % y,
                        color='blue',
                        legend=False)
                    plots.append([p])

            show(gridplot(plots))
        return df
Exemple #28
0
def plot_im_grid(imarray, dims):
    x, y, z = dims
    plot_list = []
    for layer, depth in zip(range(imarray.shape[-1]), z):
        p = figure(x_range=[x[0], x[-1]],
                   y_range=[y[0], y[-1]],
                   title="Depth %.0f mm" % (depth))
        p.image(image=[np.squeeze(imarray[:, :, layer])],
                x=[0],
                y=[0],
                dw=x[-1],
                dh=y[-1],
                palette="Greys9")
        plot_list.append(p)
    grid = gridplot([plot_list[:6], plot_list[6:12], plot_list[12:]])
    show(grid)
    return components(grid)
Exemple #29
0
    def set_data(self):
        # set same x-range for all models
        self.x_range = DataRange1d(start=0, end=10)  # end is initial guess

        weighted = self.data['weighted']
        if 'kernel' in weighted:
            rows, x_max = self.get_single_plot()
        elif 'added_kernel' in weighted and 'extra_kernel' in weighted:
            rows, x_max = self.get_dual_plot()

        self.x_range.end = x_max
        self.plot = gridplot(rows,
                             toolbar_options={
                                 'logo': None,
                                 'merge_tools': True,
                                 'toolbar_location': 'above',
                             })
Exemple #30
0
def plot_nodecounts_bokeh_line():
    datasets_csv = read_datasets_csv()
    datasets = sorted([dataset['name'] for dataset in datasets_csv])
    plots = []
    for dataset in datasets:
        output_file(PNG_FOLDER + dataset + '_nodes.html')
        x, y = get_node_counts_for_cutoffs(dataset=dataset)
        p = figure(title='node counts for ' + dataset,
                   tools="pan,wheel_zoom,box_zoom,previewsave,reset")
        p.xaxis.axis_label = 'cut-off'
        p.yaxis.axis_label = '# nodes'
        p.line(x, y, color="green")
        save(p)
        plots.append(p)
    output_file(TEMPLATE_FOLDER + 'bokeh_nodecount.html')
    gp = gridplot(plots, ncols=2, tools="pan,wheel_zoom,box_zoom,save,reset")
    save(gp)
    def __draw_single_table(self):
        """ Show figures of multiple columns in a table """

        # Output file
        prefix = '_'.join([self.__trace, self.__table, self.__xaxis])
        if len(self.__yaxis) > 1:
            last = str(len(self.__yaxis)) + '_cols'
        else:
            last = self.__yaxis[0]
        output_name = '_'.join([prefix, last])
        output_file(output_name + '.html', title=output_name)

        # Get figures
        figures = self.__figures.plot_t_x_multi(FIGURE_WIDTH, FIGURE_HEIGHT,
                                                self.__table,
                                                self.__xaxis, self.__yaxis)
        plot = gridplot(figures)
        show(plot)
Exemple #32
0
    def initialize_plot(self, ranges=None, plots=[]):
        ranges = self.compute_ranges(self.layout, self.keys[-1], None)
        plots = [[] for r in range(self.cols)]
        passed_plots = list(plots)
        for i, coord in enumerate(self.layout.keys(full_grid=True)):
            r = i % self.cols
            subplot = self.subplots.get(coord, None)
            if subplot is not None:
                plot = subplot.initialize_plot(ranges=ranges, plots=passed_plots)
                plots[r].append(plot)
                passed_plots.append(plots[r][-1])
            else:
                plots[r].append(None)
                passed_plots.append(None)
        self.handles['plot'] = gridplot(plots[::-1])
        self.handles['plots'] = plots
        self.drawn = True

        return self.handles['plot']
Exemple #33
0
    def dist_plot(self):
        """

        :return:
        """
        dist_plots = []

        self.dist_dict = {}

        for col in self.true_cols[0:8]:

            x = self.data_dict[col]

            # create the horizontal histogram
            hhist, hedges = np.histogram(x, bins=20)
            hzeros = np.zeros(len(hedges) - 1)
            hmax = max(hhist) * 1.1

            LINE_ARGS = dict(color="#3A5785", line_color=None)

            ph = figure(toolbar_location=None, plot_width=200, plot_height=100,title=col,
                        y_range=(0, hmax), y_axis_location="left")
            ph.xgrid.grid_line_color = None
            ph.yaxis.major_label_orientation = np.pi / 4
            ph.background_fill_color = "#fafafa"

            ph.quad(bottom=0, left=hedges[:-1], right=hedges[1:], top=hhist, color="white", line_color="#3A5785")
            hh1 = ph.quad(bottom=0, left=hedges[:-1], right=hedges[1:], top=hzeros, alpha=0.5, **LINE_ARGS)


            # Add to dist dict
            self.dist_dict[col] = {'plot':ph,
                                   'hh1':hh1,
                                   'hedges':hedges}


            # Add to grid plot
            dist_plots.append(ph)

        gp = gridplot(dist_plots, plot_width=300, plot_height=200, ncols=4, height=400,toolbar_location=None,)


        return gp
Exemple #34
0
    def make_all_plots(self):
        """

        :return:
        """
        self.tab_list = []
        self.plot_list = []
        self.circle_list = []
        self.plot_control_dict = {}

        self.n_plots = len(self.maps_dict.keys())

        # Make Each Plot
        for f in self.maps_dict.keys():
            xs = self.maps_dict[f][0]
            ys = self.maps_dict[f][1]
            self.make_plot(f, xs, ys)

        #if self.verbose: print("Grid of plots: "+str(self.tab_list))
        return gridplot(self.tab_list,ncols=self.max_row_width,plot_width=250, plot_height=250)
Exemple #35
0
    def initialize_plot(self, ranges=None, plots=[]):
        ranges = self.compute_ranges(self.layout, self.keys[-1], None)
        plots = [[] for r in range(self.cols)]
        passed_plots = list(plots)
        for i, coord in enumerate(self.layout.keys(full_grid=True)):
            r = i % self.cols
            subplot = self.subplots.get(coord, None)
            if subplot is not None:
                plot = subplot.initialize_plot(ranges=ranges,
                                               plots=passed_plots)
                plots[r].append(plot)
                passed_plots.append(plots[r][-1])
            else:
                plots[r].append(None)
                passed_plots.append(None)
        self.handles['plot'] = gridplot(plots[::-1])
        self.handles['plots'] = plots
        self.drawn = True

        return self.handles['plot']
Exemple #36
0
def predict_obesity():
  page_title = "Predict Obesity"
  scores = np.load('scores_file.npy')
  lowci = np.percentile(scores,2.5,axis=0)
  highci = np.percentile(scores,97.5,axis=0)
  CIs = np.matrix([lowci,highci])
  
  scores_svm = np.load('scores_svm.npy')
  lowci_svm = np.percentile(scores_svm,2.5,axis=0)
  highci_svm = np.percentile(scores_svm,97.5,axis=0)
  CIs_svm = np.matrix([lowci_svm,highci_svm])

  p1 = figure(title='Predicting obesity from poverty, food access, food availability (logistic)',title_text_font_size='12pt')
  p1.quad(top=.7,bottom=.5,left=[.55,1.55,2.55,3.55],right=[1.45,2.45,3.45,4.45],alpha=.2,color=['red','blue','blue','blue'])
  p1.multi_line(xs=[[1,1],[2,2],[3,3],[4,4]],ys=[CIs[:,0],CIs[:,1],CIs[:,2],CIs[:,3]],line_width = 4)
  p1.circle(x=[1,2,3,4],y=scores.mean(axis=0),size=10,color='red')
  p1.xgrid.grid_line_color = None
  p1.ygrid.grid_line_color = None
  p1.xaxis.major_label_text_color = None
  p1.yaxis.axis_label = "Classifier accuracy"
  p1.text(.7,.51,['All variables'],text_font_size='10pt')
  p1.text(1.55,.51,['Drop poverty rate'],text_font_size='10pt')
  p1.text(2.7,.51,['Drop access'],text_font_size='10pt')
  p1.text(3.6,.51,['Drop availablity'],text_font_size='10pt')

  p2 = figure(title='Predict obesity from poverty, food access, food availability (SVM)',title_text_font_size = '12pt')
  p2.quad(top=.7,bottom=.5,left=[.55,1.55,2.55,3.55],right=[1.45,2.45,3.45,4.45],alpha=.2,color=['red','blue','blue','blue'])
  p2.multi_line(xs=[[1,1],[2,2],[3,3],[4,4]],ys=[CIs_svm[:,0],CIs_svm[:,1],CIs_svm[:,2],CIs_svm[:,3]],line_width=4)
  p2.circle(x=[1,2,3,4],y=scores_svm.mean(axis=0),size=10,color='red')
  p2.xgrid.grid_line_color = None
  p2.ygrid.grid_line_color = None
  p2.xaxis.major_label_text_color = None
  p2.yaxis.axis_label = "Classifier accuracy"
  p2.text(.7,.51,['All variables'],text_font_size='10pt')
  p2.text(1.55,.51,['Drop poverty rate'],text_font_size='10pt')
  p2.text(2.7,.51,['Drop access'],text_font_size='10pt')
  p2.text(3.6,.51,['Drop availability'], text_font_size='10pt')

  p = gridplot([[p1,p2]])
  script, div = components(p,CDN)
  return render_template('myplot.html', page_title=page_title, script=script, div=div)
Exemple #37
0
def _draw_decomposition_plot(filename, X, decomposition, legend, x_axis_type, x_axis_label, width=800, height=400):

    # create a new plot
    s1 = figure(x_axis_type = x_axis_type, width=width, plot_height=height, title='observed')
    s1.line(X, decomposition.observed, color="navy", alpha=0.5)

    # create another one
    s2 = figure(x_axis_type = x_axis_type, width=width, plot_height=height, title='trend')
    s2.line(X, decomposition.trend, color="navy", alpha=0.5)

    # create and another
    s3 = figure(x_axis_type = x_axis_type, width=width, plot_height=height, title='residual')
    s3.line(X, decomposition.resid, color="navy", alpha=0.5)

    s4 = figure(x_axis_type = x_axis_type, width=width, plot_height=height, title='seasonal')
    s4.line(X, decomposition.seasonal, color="navy", alpha=0.5)

    # put all the plots in a grid layout
    p = gridplot([[s1, s2], [s3, s4]])

    save(vplot(p), filename=filename, title='seasonal_decompose')  
Exemple #38
0
def usage():
    import numpy as np
    from datetime import timedelta
    from bokeh.io import gridplot

    output_file("test.html", mode="cdn")

    d_start = datetime(2016, 6, 1)
    d_step = timedelta(days=1)

    t = [d_start + (i * d_step) for i in range(0, 12)]
    s1 = np.random.randint(2, 10, 12)
    s2 = np.random.randint(2, 10, 12)
    source = ColumnDataSource({'t': t, 's1': s1, 's2': s2})

    p1 = timeline_figure()
    p1.triangle(x='t', y='s1', source=source, size=10, color="blue")
    p2 = timeline_figure()
    p2.square(x='t', y='s2', source=source, size=10, color="red")

    p = gridplot([[p1], [p2]])
    save(p)
Exemple #39
0
    def build_plot(self):

        prot_list = set(self._df_sum['prot1'])
        sorted_prot_list = sorted(prot_list.intersection(self._df_fasta['prot']))

        numprot = len(sorted_prot_list)
        rows = [[None for r in range(numprot+1)] for s in range(numprot)]
        rows[0][-1]=splotch.makelegend(self._df_sum)
        (xr, yr) = (Range1d(0,self._df_sum['res1'].max()), Range1d(0,self._df_sum['res2'].max()))
        i=numprot-1
        for prot2 in sorted_prot_list:
            j=0
            for prot1 in sorted_prot_list:
                subdf = self._df_sum[(self._df_sum['prot1']==prot1) & (self._df_sum['prot2']==prot2)]
                if self.unjoin:
                    (xr, yr) = ((0,self._df_fasta[self._df_fasta['prot']==prot1]['pos'].max()), 
                                (0,self._df_fasta[self._df_fasta['prot']==prot2]['pos'].max()))
                rows[i][j] = splotch.splotch_df(subdf, prot1, prot2, xr, yr, numprot, i==numprot-1, j==0)
                j+=1
            i-=1

        self.proscatter = gridplot(rows)
Exemple #40
0
def usage():
    import numpy as np
    from datetime import timedelta
    from bokeh.io import gridplot

    output_file("test.html", mode="cdn")

    d_start = datetime(2016, 6, 1)
    d_step = timedelta(days=1)

    t = [d_start + (i * d_step) for i in range(0, 12)]
    s1 = np.random. randint(2, 10, 12)
    s2 = np.random.randint(2, 10, 12)
    source = ColumnDataSource({'t': t, 's1': s1, 's2': s2})

    p1 = timeline_figure()
    p1.triangle(x='t', y='s1', source=source, size=10, color="blue")
    p2 = timeline_figure()
    p2.square(x='t', y='s2', source=source, size=10, color="red")

    p = gridplot([[p1], [p2]])
    save(p)
def plot_true_and_estimated_channel_with_bokeh_all_antennas(
        true_channel, estimated_channel, title=''):
    p0 = plot_true_and_estimated_channel_with_bokeh(
        true_channel, estimated_channel, 'Antenna 1', 0)
    p1 = plot_true_and_estimated_channel_with_bokeh(
        true_channel, estimated_channel, 'Antenna 2', 1)
    p2 = plot_true_and_estimated_channel_with_bokeh(
        true_channel, estimated_channel, 'Antenna 3', 2)
    p3 = plot_true_and_estimated_channel_with_bokeh(
        true_channel, estimated_channel, 'Antenna 4', 3)
    p1.x_range = p0.x_range
    p2.x_range = p0.x_range
    p3.x_range = p0.x_range
    p1.y_range = p0.y_range
    p2.y_range = p0.y_range
    p3.y_range = p0.y_range

    p = gridplot([[p0, p1], [p2, p3]])

    par = bw.Paragraph(text=title)
    vbox = bw.VBox(children=[par, p])
    return vbox
    def plot(self):
        """ Plot """
        # Create html file
        self.__save_html()

        # Create figures
        figures = collections.OrderedDict()
        for report in self.__reports:
            figure_dict = report.get_info()['figures']
            for key, value in figure_dict.iteritems():
                if key not in figures.keys():
                    figures[key] = []
                figures[key].append(value)

        # Plot all figures by tabs
        tabs_list = []
        for key, value in figures.iteritems():
            if value[0]:  # Ignore blank
                plot = gridplot(value)
                tabs_list.append(Panel(child=plot, title=key))

        tabs = Tabs(tabs=tabs_list)

        show(tabs)
Exemple #43
0
    def _draw_plots(self, scaffolder):
        '''Setup all plots.'''
        self.contig_read_src = ColumnDataSource(
            dict(reads=[scaffolder.nrReads],
                 contigs=[scaffolder.nrContigs],
                 n50=[scaffolder.N50]))

        # Calculate data for contig circle plot
        circle = self._calculate_circle(scaffolder)
        self.contig_dist_src = ColumnDataSource(
            dict(start=circle[0],
                 stop=circle[1],
                 colors=circle[2],
                 contigs=circle[3]))

        self.read_src = ColumnDataSource(
            dict(nrReads=[], nrPassReads=[], nrFailReads=[], readTime=[]))

        self.read_hist_src = ColumnDataSource(
            dict(readLength=[], left=[], right=[]))

        # Draw plots
        contigNrPlot = self._draw_contigNrPlot(scaffolder)
        n50Plot = self._draw_n50Plot()
        contigCirclePlot = self._draw_contigCirclePlot()
        readPlot = self._draw_readCountPlot()
        #readHist = self._draw_readLenHistPlot()

        # Position plots
        layout = gridplot([[n50Plot, contigNrPlot],
                           [contigCirclePlot, readPlot]])
        try:
            session = push_session(curdoc())
            session.show(layout)
        except IOError:
            sys.exit("No bokeh server is running on this host")
    def plot(self):
        """ Plot """
        # Create html file
        self.__save_html()

        # Create figures
        figures = collections.OrderedDict()
        for report in self.__reports:
            figure_dict = report.get_info()['figures']
            for key, value in figure_dict.iteritems():
                if key not in figures.keys():
                    figures[key] = []
                figures[key].append(value)

        # Plot all figures by tabs
        tabs_list = []
        for key, value in figures.iteritems():
            if value[0]:  # Ignore blank
                plot = gridplot(value)
                tabs_list.append(Panel(child=plot, title=key))

        tabs = Tabs(tabs=tabs_list)

        show(tabs)
Exemple #45
0
def displayParameterEvolution(
        groupedData,
        metrics,
        parameter,
        configuration='concatParams',
        outputDir='../results/dae/neuralNetwork/resultsAnalysis/'):
    reset_output()
    output_notebook()

    grid = []
    subGrid = []
    for metric in metrics:
        p = figure(tools=TOOLS)
        p.xaxis.axis_label = parameter
        p.yaxis.axis_label = metric

        for name, group in groupedData:
            tmp = group.sort(columns=parameter)
            source = ColumnDataSource({
                parameter: tmp[parameter],
                metric: tmp[metric],
                'conf': tmp[configuration]
            })
            p.line(parameter, metric, source=source)

        p.select_one(HoverTool).tooltips = [('conf', '@conf')]

        subGrid.append(p)

    grid.append(subGrid)
    p = gridplot(grid)

    show(p)
    output_file(outputDir + parameter + '.html')
    save(p)
    return True
               group='sexo',
               title="Sex vs ind_viv_fin_ult1",
               legend=False,
               plot_width=200)
df_bar23 = Bar(df_na,
               label='ind_recibo_ult1',
               values='age',
               group='sexo',
               title="Sex vs ind_recibo_ult1",
               legend=False,
               plot_width=200)

show(df_bar)

#fig=figure();

#fig.add_glyphs(df_bar.get_glyphs())
#fig.add_glyphs(bar_renta.get_glyphs())
#fig.add_glyphs(p_age.get_glyphs())

output_file('visulize.html')
p = gridplot([df_bar, df_bar1, p_age], [df_bar2, df_bar3, df_bar4],
             [df_bar5, df_bar6, df_bar7])
show(p)

df['ind_ahor_fin_ult1'].isnull().sum()
df['ind_nom_pens_ult1'].isnull().sum()
df['ind_nomina_ult1'].isnull().sum()

df['ind_recibo_ult1'].isnull().sum()
            plot_data.append(point)
        p = plot_graph(plot_data, title='Against shorter teams({0}, away)'.format(key))
        figures.append(p)

    return figures


def plot_graph(data_points, title):
    data_points.sort(key=lambda x: x['val'], reverse=True)
    # print data_points
    order = range(1, 20)
    x_vals = [get_abbr_name(i['team']) for i in data_points]
    y_vals = [i['val'] for i in data_points]
    yr = Range1d(0, max(y_vals) + 20)
    f = figure(x_range=x_vals, y_range=yr, width=800, title=title)
    f.rect(x=order, y=[val/2.0 for val in y_vals], width=0.25, height=y_vals, color="#ff1200")
    return f


if __name__ == '__main__':
    data = bake_data_for_graphs()
    # print data
    output_file('plot.html')
    figures = plot_graph_against_shorter_teams(data)
    figures.extend(plot_graph_against_taller_teams(data))

    # print figures
    print [[figures[i], figures[i+1]] for i in range(0, len(figures), 2)]
    plot = gridplot([[figures[i], figures[i+1]] for i in range(0, len(figures), 2)])
    show(plot)
def sync_pixel_clock(mwk_path, oe_path, oe_channels=[0, 1]):

    # 1. read in ephys binary data and timestamps

    #for open ephys format data, use:
    #oe_events = open_ephys.loadEvents(oe_path)

    # unpack the channels
    #channels = oe_events['channel']
    #directions = oe_events['eventId'] # 0: 1 -> 0, 1: 0 -> 1
    #times = oe_events['timestamps']
    #event_types = oe_events['eventType']

    # for KWIK format, use:
   
    # 2. send those files to extract relevant info and concatenate:
    #all_times,all_channels,all_directions = concatenate(raw_files)
    
            #[relevant,channels,directions,times],experiment_length = concatenateKWIKfiles.concatenate(oe_path)
   
    # make path to the Ch_0 and Ch_1 files:
    if oe_path[-1] == '/':
        ttls_path = oe_path + 'Ch_'
    else:
        ttls_path = oe_path + '/Ch_'

    [times,channels,directions] = titanspikes_ttl_extract.get_TTL_info(ttls_path)
    # times are in seconds.microseconds
    ephys_fs = 1

    experiment_length=[]


    print 'Experiment length = ', experiment_length    


    # duplicate every element of times and directions

    rel_times_0 = times[channels==0]
    rel_times_1 = times[channels==1]
    rel_directions_0 = directions[channels==0]
    rel_directions_1 = directions[channels==1]

    plot_times_0 = np.array(list(itertools.chain(*zip(rel_times_0,rel_times_0[1:]))))
    plot_times_1 = np.array(list(itertools.chain(*zip(rel_times_1,rel_times_1[1:]))))
    plot_directions_0 = np.array(list(itertools.chain(*zip(rel_directions_0,rel_directions_0[:-1]))))
    plot_directions_1 = np.array(list(itertools.chain(*zip(rel_directions_1,rel_directions_1[:-1]))))

    # plot the up/down transitions as recorded on OE:
    # fig1 = plt.figure()
    # ax1 = plt.subplot(2, 1, 1)
    # plt.plot(plot_times_0,plot_directions_0)
    # plt.ylabel('OE Ch 0')

    # ax2 = plt.subplot(2, 1, 2,sharex=ax1)
    # plt.plot(plot_times_1,plot_directions_1)
    # plt.ylabel('OE Ch 1')

    #plt.show()
    #print 'some oe ttl times: ', times(0)

    oe_codes, latencies = pixelclock.events_to_codes(np.vstack((times, channels, directions)).T, len(oe_channels), 0.01,swap_12_codes = 1,swap_03_codes=0)
    # the pixel clock should change once per frame, or at ~16ms max. This is 16ms * 30samples/ms = 480 samples. If a code is shorter than that, it's probably a fluke.
    # if oe_code times are in 636... format, use 10e4 as min code length
    #if in seconds.microseconds, min code time = 0.01

    print 'Number of ephys codes = ', len(oe_codes)


    # !! assuming there's just one mworks file, take the first element in the list mwk_path:
    mwk_path = os.path.abspath(mwk_path[0])

    mwk = mw.MWKFile(mwk_path)
    mwk.open()



    # Start by getting the pixel clock / bit code data
    stimulus_announces = mwk.get_events(codes=['#announceStimulus'])

    # bit_codes is a list of (time, code) tuples
    mw_codes = [(e.time, e.value['bit_code']) for e in stimulus_announces if isiterable(e.value) and 'bit_code' in e.value]

    print 'Number of mworks codes = ', len(mw_codes)
    ## for mw_codes and oe_codes - if one code persists for too long a time (>thresh), get rid of it (keep only the fast-changing codes that come from the grating stimulus):

    #oe_codes = lowpass_codetimes(oe_codes,fs=ephys_fs,thresh_samples = 0.02) #0.2
    #mw_codes = lowpass_codetimes(mw_codes,fs=1e6,thresh_samples = 0.02)



    print 'Number of oe codes after lowpass = '******'Number of mworks codes after lowpass = '******'win max is ',int(len(oe_codes)/win_size)
    for win in range(0,int(len(oe_codes)/win_size),25): #range(int(round(len(oe_codes)/win_size)))
        print 'win = ', win
        if win*win_size+win_size < len(oe_codes):
            tmp_match = pixelclock.match_codes(
                [evt[0] for evt in oe_codes[win*win_size:(win+1)*win_size]], # oe times
                [evt[1] for evt in oe_codes[win*win_size:(win+1)*win_size]], # oe codes
                [evt[0] for evt in mw_codes], # mw times
                [evt[1] for evt in mw_codes], # mw codes
                minMatch = 20,
                maxErr = 0) 
            print 'temp matches = ', tmp_match
            matches.extend(tmp_match)
        else:
            print '!!win = ', win
            tmp_match = pixelclock.match_codes(
                    [evt[0] for evt in oe_codes[win*win_size:-1]], # oe times
                    [evt[1] for evt in oe_codes[win*win_size:-1]], # oe codes
                    [evt[0] for evt in mw_codes], # mw times
                    [evt[1] for evt in mw_codes], # mw codes
                    minMatch = 9,
                    maxErr = 0)
                    
            matches.extend(tmp_match)
    
    
    print 'matches = ', matches
    print 'type = ', type(matches)
    #matches = pixelclock.match_codes(
    #    [evt[0] for evt in oe_codes], # oe times
    #    [evt[1] for evt in oe_codes], # oe codes
    #    [evt[0] for evt in mw_codes], # mw times
    #    [evt[1] for evt in mw_codes], # mw codes
    #    minMatch = 5,
    #    maxErr = 0) # from 0 to 1 == from 23 to 27 matches, but in the wrong places. 


    #print "OE Code sequence:"
    #print [evt[1] for evt in oe_codes]

    #print "MW Code sequence:"
    #print [evt[1] for evt in mw_codes]

    #print "MATCHES:"
    #print matches

    mw_times = [item[0] for item in mw_codes] #[e.time for e in stimulus_announces if isiterable(e.value)]
    oe_times = [item[0] for item in oe_codes]

       

    # condition the data to plot square pulses:
    tmp_mw_codes = [evt[1] for evt in mw_codes]
    tmp_mw_codetimes = [evt[0] for evt in mw_codes]
    plot_mw_codes = np.array(list(itertools.chain(*zip(tmp_mw_codes,tmp_mw_codes[:-1])))) 
    plot_mw_codetimes = np.array(list(itertools.chain(*zip(tmp_mw_codetimes,tmp_mw_codetimes[1:])))) 

    tmp_oe_codes = [evt[1] for evt in oe_codes]
    tmp_oe_codetimes = [evt[0] for evt in oe_codes]
    plot_oe_codes = np.array(list(itertools.chain(*zip(tmp_oe_codes,tmp_oe_codes[:-1])))) 
    plot_oe_codetimes = np.array(list(itertools.chain(*zip(tmp_oe_codetimes,tmp_oe_codetimes[1:])))) 



    # Bokeh:
    save_folder = './pixel_clock/'
    if not os.path.exists(save_folder):
        os.makedirs(save_folder)
    ####### FIGURE 1 ###########

    colors = []
    #col = np.matlib.repmat(rgb,10,1)

    for i in range(len(matches)):
        r = np.random.randint(255)
        g = np.random.randint(255)
        b = np.random.randint(255)
        
        colors.append(RGB(r,g,b)) 
    match_idx = [idx for idx,match in enumerate(matches)]    
    #TOOLS = [HoverTool(),'box_zoom','reset','box_select']
    TOOLS="pan,wheel_zoom,box_zoom,reset,hover,previewsave"
    s1 = figure(width=1000, plot_height=500, title='MWorks and OpenEhys Pixel Clock Codes') # ,tools = TOOLS
    s1.line(plot_mw_codetimes/1e6,plot_mw_codes)
    mw_match_circles = [mat[1]/1e6 for mat in matches]
    mw_match_circles_samples = [mat[1] for mat in matches]

    s1.circle(mw_match_circles,match_idx,color=colors,size=20)
    
    #s1.circle(mw_match_circles,np.ones(len(matches)),color=colors,size=20)
    s1.yaxis.axis_label = 'MW Codes'

    #tap = s1.select(dict(type=TapTool))
    

    s2 = figure(width=1000, plot_height=500, title=None) #,tools = TOOLS
    s2.line(plot_oe_codetimes/ephys_fs,plot_oe_codes)
    oe_match_circles = [mat[0]/ephys_fs for mat in matches]
    oe_match_circles_samples = [mat[0] for mat in matches]
    
    
    s2.circle(oe_match_circles,match_idx,color=colors,size=20) # ,tags = match_idx
    #s2.circle(oe_match_circles,np.ones(len(matches)),color=colors,size=20) # ,tags = match_idx
    s2.xaxis.axis_label = 'Time (sec)'
    s2.yaxis.axis_label = 'OE Codes'

    


    p = gridplot([[s1], [s2]])
    output_file(save_folder + "pc_codes_match.html")
    # show the results
    show(p)
    
    #plt.savefig('pc_code_matches.pdf')
    #plt.show()


    
    #m,c = fit_line(oe_match_circles_samples,mw_match_circles_samples)
    m,c = fit_ridge(oe_match_circles_samples,mw_match_circles_samples)
    
    # tb object lets you go back and forth between oe and mw timezones
    tb = timebase.TimeBase(matches,tmp_oe_codetimes,tmp_mw_codetimes)
    

    ## to test quality of match, plot OE codes in MW time

    print 'len plot_oe_codetimes = ', len(plot_oe_codetimes)



    


    #### want: take MW time (e.g. stim time) and get oe time:
    mw2oe_time = []
    for mw_time in plot_mw_codetimes:
        oe_tmp = mw_to_oe_time(mw_time,m,c) ### take MW time and convert to OE time 
        #oe_tmp = tb.mw_to_oe_time(mw_time)
        mw2oe_time.append(oe_tmp)

    mw2oe_time = np.squeeze(np.array(mw2oe_time))

    oe2mw_time = []
    for oe_time in plot_oe_codetimes:
        mw_tmp = oe_to_mw_time(oe_time,m,c)  ### take OE and convert to MW time!
        #mw_tmp = tb.oe_to_mw_time(oe_time)
        oe2mw_time.append(mw_tmp)

    oe2mw_time = np.squeeze(np.array(oe2mw_time))

    print 'oe2mw_time.shape ==== ', oe2mw_time.shape
    print 'plot_oe_codes.shape === ', plot_oe_codes.shape
    ####### FIGURE 2 ###########


    ####### PLOT the codes on the same time axis: e.g. everything on MW.
    
    

    tmp_oeMWconv_codetimes = [tb.audio_to_mworks(evt[0]/ephys_fs)* 1e6 for evt in oe_codes]
    plot_oeMWconv_codetimes = np.array(list(itertools.chain(*zip(tmp_oeMWconv_codetimes,tmp_oeMWconv_codetimes[1:])))) 

    

    s1 = figure(width=1000, plot_height=500, title='OE Codes Plotted in MW Time')
    s1.line(plot_mw_codetimes/1e6,plot_mw_codes)
    
    #match_circles = [mat[1] for mat in matches]
    #s1.circle(match_circles,np.ones(len(matches)),color=colors,size=20)

    s1.yaxis.axis_label = 'MW Codes in MW Time'

    s2 = figure(width=1000, plot_height=500, title=None,x_range=s1.x_range,y_range=s1.y_range)
    s2.line(oe2mw_time/1e6,plot_oe_codes)
    s2.xaxis.axis_label = 'Time (sec)'
    s2.yaxis.axis_label = 'OE Codes in MW Time'
    
    p = gridplot([[s1], [s2]])
    output_file(save_folder + "oe_codes_in_MWtime.html")
    # show the results
    show(p)
    
    ####### FIGURE 3 ###########

    s1 = figure(width=1000, plot_height=500, title='MW Codes Plotted in OE Time')
    s1.line(mw2oe_time/ephys_fs,plot_mw_codes)
    
    #match_circles = [mat[1] for mat in matches]
    #s1.circle(match_circles,np.ones(len(matches)),color=colors,size=20)

    s1.yaxis.axis_label = 'MW Codes in OE Time'

    s2 = figure(width=1000, plot_height=500, title=None,x_range=s1.x_range,y_range=s1.y_range)
    s2.line(plot_oe_codetimes/ephys_fs,plot_oe_codes)
    s2.xaxis.axis_label = 'Time (sec)'
    s2.yaxis.axis_label = 'OE Codes in OE Time'
    
    p = gridplot([[s1], [s2]])
    output_file(save_folder + "mw_codes_in_OEtime.html")
    # show the results
    show(p)
    

    ####### FIGURE 4 ###########
    ####### PLOT the LINE fit:
    
    pp = figure(width=1000, plot_height=500, title='Line Fit for MW and OE Time')
    
    #pp.line(oe2mw_time/1e6,plot_oe_codes)
    pp.line(plot_oe_codetimes,m*plot_oe_codetimes+c,color='red')
    #pp.circle(plot_oe_codetimes[0:len(matches)],plot_mw_codetimes[0:len(matches)])
    pp.circle(oe_match_circles_samples,mw_match_circles_samples)
    pp.xaxis.axis_label = 'oe codetimes'
    pp.yaxis.axis_label = 'mw codetimes'
    output_file(save_folder + "pc_line_fit.html")
    show(pp)


    print "number of MW events:"
    print len(mw_times)

    print "number of OE events:"
    print len(oe_times)

    print "number of matches: " + str(len(matches))
    

    


    return matches,mwk,m,c,experiment_length
Exemple #49
0
def score_grid_plot(scores, R2, n_pcs=2, obs_labels=None, obs_class_labels=None,
                    height=500, width=500):
    """ Plot `n_pcs` scores against each other in a all against all-fashion.

    Arrange plot in grid.

    Parameters
    ----------
    scores : array_like
        Array with all model scores as columns.
    R2 : array_like
        Array with R2-values for each model component.
    n_pcs : int
        Number of components to use.
    obs_labels : list[str], optional
        Observation labels.
    obs_class_labels : list[str], optional
        Observation classification.
    height : int
        Height of each subplot, default 500.
    width : int
        Width of each subplot, default 500.

    Returns
    -------
    bokeh.plotting.figure
    """
    cols = ['C{}'.format(i + 1) for i in range(scores.shape[1])]
    obs_labels = obs_labels or list(map(str, range(len(scores))))

    score_df = pd.DataFrame(scores, index=obs_labels, columns=cols)
    score_df['labels'] = obs_labels

    if obs_class_labels is not None:
        score_df['class'] = obs_class_labels

    score_color_map = make_color_mapping(obs_class_labels, BREWER12plus12)
    score_source = ColumnDataSource(data=score_df)

    TOOLS = "box_select,lasso_select,pan,help,box_zoom,wheel_zoom,reset"

    plots = [[None for i in range(n_pcs - 1)] for j in range(n_pcs - 1)]

    for i, j in itertools.combinations_with_replacement(range(n_pcs), 2):
        if i == j:
            continue
        PC1 = 'C{}'.format(i + 1)
        PC2 = 'C{}'.format(j + 1)

        # Score plot.
        score_title = 't[{}] vs t[{}]'.format(i + 1, j + 1)
        t1_lab = 't[{}] ({:.2f} %)'.format(i + 1, R2[i])
        t2_lab = 't[{}] ({:.2f} %)'.format(j + 1, R2[j])

        score_plot = scatter_plot(PC1, PC2, score_source, obs_class_labels,
                                  score_color_map, tools=TOOLS, height=height,
                                  title=score_title, x_axis_label=t1_lab,
                                  y_axis_label=t2_lab, width=width)
        plots[j - 1][i] = score_plot

    fig = gridplot(plots)
    return fig
Exemple #50
0
def plot_score_loading(scores, loadings, R2, n_pcs=2,
                       obs_labels=None, var_labels=None,
                       obs_class_labels=None, var_class_labels=None,
                       height=500, width=500):
    """ For each component combination of `n_pcs` first component
    plot score and loading pairs.

    Parameters
    ----------
    scores : array_like
        Array with all model scores as columns.
    loadings : array_like
        Array with all model loadings as columns.
    R2 : array_like
        Array with R2-values for each model component.
    n_pcs : int
        Number of components to use.
    obs_labels : list[str], optional
        Observation labels.
    var_labels : list[str], optional
        Variable labels.
    obs_class_labels : list[str], optional
        Observation classification.
    var_class_labels : list[str], optional
        Variable classification.
    height : int
        Height of each subplot, default 500.
    width : int
        Width of each subplot, default 500.

    Returns
    -------
    bokeh.plotting.figure
    """
    cols = ['C{}'.format(i + 1) for i in range(scores.shape[1])]

    obs_labels = obs_labels or list(map(str, range(len(scores))))
    var_labels = var_labels or list(map(str, range(len(loadings))))

    score_df = pd.DataFrame(scores, index=obs_labels, columns=cols)
    loading_df = pd.DataFrame(loadings, index=var_labels, columns=cols)

    score_df['labels'] = obs_labels
    loading_df['labels'] = var_labels

    if obs_class_labels is not None:
        score_df['class'] = obs_class_labels
    if var_class_labels is not None:
        loading_df['class'] = var_class_labels

    score_color_map = make_color_mapping(obs_class_labels, BREWER12plus12)
    loading_color_map = make_color_mapping(var_class_labels,
                                           BREWER12plus12[len(score_color_map):])

    score_source = ColumnDataSource(data=score_df)
    loading_source = ColumnDataSource(data=loading_df)

    plot_pairs = list()
    TOOLS = "box_select,lasso_select,pan,help,box_zoom,wheel_zoom,reset"

    for i, j in itertools.combinations_with_replacement(range(n_pcs), 2):
        if i == j:
            continue
        PC1 = 'C{}'.format(i + 1)
        PC2 = 'C{}'.format(j + 1)

        # Score plot.
        score_title = 't[{}] vs t[{}]'.format(i + 1, j + 1)
        t1_lab = 't[{}] ({:.2f} %)'.format(i + 1, R2[i])
        t2_lab = 't[{}] ({:.2f} %)'.format(j + 1, R2[j])

        score_plot = scatter_plot(PC1, PC2, score_source, obs_class_labels,
                                  score_color_map, tools=TOOLS, height=height,
                                  title=score_title, x_axis_label=t1_lab,
                                  y_axis_label=t2_lab, width=width)

        # Loading plot.
        p1_lab = 'p[{}]'.format(i + 1)
        p2_lab = 'p[{}]'.format(j + 1)
        loading_title = '{} vs {}'.format(p1_lab, p2_lab)

        loading_plot = scatter_plot(PC1, PC2, loading_source, var_class_labels,
                                    loading_color_map, tools=TOOLS,
                                    height=height, title=loading_title,
                                    x_axis_label=p1_lab, y_axis_label=p2_lab,
                                    width=width)

        plot_pairs.append([score_plot, loading_plot])

    fig = gridplot(plot_pairs)
    return fig
Exemple #51
0
def main():
    def cmd(a):
        p = (a / (1.0 + math.fabs(a)) + 1.0) / 2.0
        if random.random() > p:
            return -1.0
        else:
            return 1.0

    #def react(a):
    #    return a / (1.0 + math.fabs(a))
    
    random.seed()

    p = figure(x_range=(-120, 120), y_range=(-120, 120), webgl=False, plot_width=750, plot_height=750)
    #, toolbar_location=None
    p.border_fill_color = '#eeeeee'
    p.background_fill_color = 'white'
    p.outline_line_color = None
    p.grid.grid_line_color = None
  
    world = Polygon(p)

    minmax = MinMax([[0.0, 100.0]] * 36 + [[-1.0, 1.0], [-math.pi/4, math.pi/4]])
    learner = CACLA([[-1.0, 1.0]] * 38, dim_actions=2, hidden = 100, sigma=0.1,
            alpha=0.01)
   
    norm_reward = MinMax([[-4.0, 1.0]])

    brain_dir = 'car-brain16'
    try:
        learner.load(brain_dir)
        print "Saved networks have been successfully loaded"
    except Exception as e:
        print e, e.__dict__
        print 'No saved networks'

    #drawer_V = NetDrawer(learner.V.net)
    #drawer_Ac = NetDrawer(learner.Ac.net)

    session = push_session(curdoc())
    #num_layers = len(drawer_V.figures)
    #print 'Num layers:', num_layers
    pq = gridplot([]) #drawer_V.figures, drawer_Ac.figures])
    curdoc().add_root(pq)
    session.show()

    avgrwd = 0.0

    kp = KeyPoints([(-110.0, 0), (110.0, 0)], brain_dir)

    N = 100000 #!!!
    states = np.empty((N, len(world.state()))) #!!!
    for i in xrange(N):
        s = minmax.norm(world.state())
        
        a = learner.getAction(s)
        
        #world.act([cmd(a[0]), cmd(a[1])])
        world.act(a)
        r = world.reward()
        kp.update(world.car.center, r)
        #print world.car.path().points
        if i % 1000 == 0:
            #states[i, :] = s #!!!
            print a, world.car.speed, world.car.wheels_angle, '[', r, ']'
            world.draw()
        #if i % 10000 == 0:
        #    drawer_V.draw()
        #    drawer_Ac.draw()
        if i % 10000 == 0:
            print i, a, world.car.speed, world.car.wheels_angle, 'avgrwd:', avgrwd / 10000.0, '||', kp.full_circle, '||'
            avgrwd = 0.0
        new_s = minmax.norm(world.state())
        avgrwd = avgrwd + r
        nr = norm_reward.norm([r])[0]
        learner.step(s, new_s, a, nr)
        if (i != 0) and (i % 10000 == 0):
            learner.save(brain_dir)
def show_sw_kvm_heatmap(df, task_re, label, show_ctx_switches, show_kvm):
    gb = get_groupby(df, task_re)

    chart_list = []
    # these are the 2 main events to show for kvm events
    legend_map_kvm = {
        'kvm_exit': (BLUE, 'vcpu running (y=vcpu run time)', False),
        'kvm_entry': (ORANGE, 'vcpu not running (y=kvm+sleep time)', False)
    }
    # context switch events
    legend_map_ctx_sw = {
        'sched__sched_stat_sleep': (RED, 'wakeup from sleep (y=sleep time)', True),
        'sched__sched_switch': (GREEN, 'switched out from cpu (y=run time)', True)
    }
    if show_kvm and show_ctx_switches:
        legend_map = dict(legend_map_kvm.items() + legend_map_ctx_sw.items())
        title = "Scheduler and KVM events"
        prefix = 'swkvm'
    elif show_kvm:
        legend_map = legend_map_kvm
        title = "KVM events"
        prefix = 'kvm'
    else:
        legend_map = legend_map_ctx_sw
        title = "Scheduler events"
        prefix = 'sw'
    width = 1000
    height = 800
    show_legend = True
    nb_charts = len(gb.groups)
    if nb_charts == 0:
        print 'No selection matching: ' + task_re
        return
    if nb_charts > 1:
        width /= 2
        height /= 2
        tstyle = grid_title_style
    else:
        tstyle = title_style
    task_list = gb.groups.keys()
    task_list.sort()
    show_legend = True
    duration_max = usecs_max = -1
    duration_min = usecs_min = sys.maxint
    event_list = legend_map.keys()
    event_list.sort()

    for task in task_list:
        p = figure(plot_width=width, plot_height=height, y_axis_type="log", **tstyle)
        p.xaxis.axis_label = 'time (usecs)'
        p.yaxis.axis_label = 'duration (usecs)'
        p.legend.orientation = "bottom_right"
        p.xaxis.axis_label_text_font_size = "10pt"
        p.yaxis.axis_label_text_font_size = "10pt"
        if label:
            p.title = "%s for %s (%s)" % (title, task, label)
            label = None
        else:
            p.title = task
        p.ygrid.minor_grid_line_color = 'navy'
        p.ygrid.minor_grid_line_alpha = 0.1
        accumulated_time = {}
        total_time = 0

        dfg = gb.get_group(task)
        # remove any row with zero duration as it confuses the chart library
        dfg = dfg[dfg['duration'] > 0]

        for event in event_list:
            dfe = dfg[dfg.event == event]
            duration_min = min(duration_min, dfe['duration'].min())
            duration_max = max(duration_max, dfe['duration'].max())
            usecs_min = min(usecs_min, dfe['usecs'].min())
            usecs_max = max(usecs_max, dfe['usecs'].max())
            count = len(dfe)
            color, legend_text, cx_sw = legend_map[event]
            if show_legend:
                legend_text = '%s (%d)' % (legend_text, count)
            elif color == GREEN:
                legend_text = '(%d)' % (count)
            else:
                legend_text = None
            # there is bug in bokeh when there are too many circles to draw, nothing is visible
            if len(dfe) > 50000:
                dfe = dfe[:50000]
                print 'Series for %s display truncated to 50000 events' % (event)
            if cx_sw:
                draw_shape = p.circle
                size = get_disc_size(count)
            else:
                draw_shape = p.diamond
                size = get_disc_size(count) + 4

            draw_shape('usecs', 'duration', source=ColumnDataSource(dfe),
                       size=size,
                       color=color,
                       alpha=0.3,
                       legend=legend_text)
            event_duration = dfe['duration'].sum()
            accumulated_time[event] = event_duration
            total_time += event_duration
        chart_list.append(p)
        show_legend = False

    shared_x_range = Range1d(usecs_min, usecs_max)
    shared_y_range = Range1d(duration_min, duration_max)

    for p in chart_list:
        p.x_range = shared_x_range
        p.y_range = shared_y_range

    # specify how to output the plot(s)
    output_html(prefix, task_re)

    # display the figure
    if len(chart_list) == 1:
        show(chart_list[0])
    else:
        # split the list into an array of rows with 2 charts per row
        gp = gridplot(split_list(chart_list, 2))
        show(gp)
Exemple #53
0
EventDates = [dt.date(2010,01,28), dt.date(2010,03,02), dt.date(2010,03,26)]
# EventDates = [dt.date(2010, 02, 9), dt.date(2010, 3, 10), dt.date(2010,3,30)]
# ticker = "ALME"

# ticker = "PSEC"

start = min(EventDates) + relativedelta(years=-1)
end = max(EventDates) + relativedelta(years=1)


data = wb.DataReader(ticker, 'yahoo', start, end).reset_index()



output_file("graph.html")

price = figure(title=ticker, x_axis_type='datetime', y_axis_label="Price", height=250, width=800)
volume = figure(x_axis_type='datetime', y_axis_label="Volume", height=250, width=800)


price.line(data["Date"], data["Adj Close"], line_width=2)
volume.line(data["Date"], data["Volume"]/1000., line_width=2, color='green')

price.ray(x=EventDates, y=0, length=0, angle=3.14159/2, color='red')
price.ray(x=EventDates, y=0, length=0, angle=-3.14159/2, color='red')
volume.ray(x=EventDates, y=0, length=0, angle=3.14159/2, color='red')
volume.ray(x=EventDates, y=0, length=0, angle=-3.14159/2, color='red')

show(gridplot([[price], [volume]]))

Exemple #54
0
def index():
	reload(sys) #解决中文编码
	
	e_location = "127.0.0.1:9203"
	e_index = "logstash-*"
	analysis =action.LogAnalysis(e_location, e_index)
	
	c = pycurl.Curl()
	buf =BytesIO()
	c.setopt(c.URL, 'http://' + e_location + '/' + e_index + '/_search')
	c.setopt(pycurl.CUSTOMREQUEST,"GET")
	c.setopt(c.WRITEFUNCTION, buf.write)
	c.perform()
	results = buf.getvalue()
	results = json.loads(results.decode('utf-8'))
	c.close
	if results["hits"]["total"] == 0:
		flash('you have got no data!')
		return render_template('auth/upload.html')
	else:
		
		#bar1
		actionlist = analysis.actionAgg("action")
		df=pd.DataFrame(actionlist)
		bar1 = Bar(df, label='action', values='size', agg='max', color="green", title="sshd-invalid-passwd_IP", plot_width=600, plot_height=322, legend=False)
		
		script, div = components(bar1)
		'''
		#dount
		label,value,res = analysis.USERAgg("action")
		data = pd.Series(value, index = label)
		pie_chart = Donut(data, plot_width=400, plot_height=300)
		
		script2, div2 = components(pie_chart)
		'''
		#user_ip
		useriplist = analysis.USERIPAgg("action")
		a = list()
		for i in useriplist:
				data = pd.Series(i['value'], index = i['ip'])
				pie_chart = Donut(data, title = i['user'] +'\n'+ ",IP總數:" + str(len(i['value'])), plot_width=190, plot_height=190)
				a.append(pie_chart)
		b=[]
		for i in range(0,len(a),5):
			b.append(a[i:i+5])
		p = gridplot(b)
		
		script3, div3 = components(p)
		
		#INLINE_config
		js_resources = INLINE.render_js()
		css_resources = INLINE.render_css()
		
		#piechart
		labels,values,res = analysis.USERAgg("action")
		colors = []
		for i in labels:
			colors.append("#%06x" % random.randint(0, 0xFFFFFF))
		#colors = [ "#F7464A", "#46BFBD", "#FDB45C", "#FEDCBA","#ABCDEF", "#DDDDDD", "#ABCABC", "#AABEEF", "#EFAAED", "#bebece"]
		count = 0
		for a in res:
			a["color"]=colors[count]
			if count<9:
				count+=1
		#TableAgg
		Usertable = analysis.TableAgg("USERNAME","SUPERUSER")
		
		
		return render_template('index.html',
			plot_script=script,
			plot_div=div,
			pies_script=script3,
			pies_div=div3,
			js_resources=js_resources,
			css_resources=css_resources,
			set=zip(values, labels, colors),
			res=res,
			Usertable = Usertable
		)
    def plot_pairs(self):
        """
        Creates a scatter plot matrix for the specified data
        """

        html_tags = self.html_tags
        prod_dict = OrderedDict()

        txts = self.txt

        for p in product(txts, repeat=2):
            prod_dict[str(p)] = pair_data(html_tags[p[0]],
                                          html_tags[p[1]],
                                          p,
                                          1995)

        n = len(prod_dict)
        k2 = len(txts)

        fig_dict = OrderedDict()
        cds_dict = OrderedDict()

        for k, v in prod_dict.items():

            v[-1][0] = v[-1][0].replace(".txt", "").replace("_", " ")
            v[-1][1] = v[-1][1].replace(".txt", "").replace("_", " ")
            fig_dict[k] = figure(width=220, plot_height=220, title=str(v[-1][0]) + " vs "+str(v[-1][1]),
                                 title_text_font_size='8pt',
                                 tools="reset,hover",
                                 x_axis_label=v[-1][0],
                                 y_axis_label=v[-1][1])

            fig_dict[k].xaxis.axis_label_text_font_size = '8pt'
            fig_dict[k].yaxis.axis_label_text_font_size = '8pt'

            # Data sources are required for all 3 colors
            # in order to display labels and dynamically
            # update with notebook widgets
            cds_dict[k+'1'] = ColumnDataSource(
                data=dict(
                    x=np.array(v[0])[:, 1],
                    y=np.array(v[1])[:, 1],
                    desc=np.array(v[1])[:, 0]
                )
            )
            cds_dict[k+'2'] = ColumnDataSource(
                data=dict(
                    x=np.array(v[2])[:, 1],
                    y=np.array(v[3])[:, 1],
                    desc=np.array(v[3])[:, 0]
                )
            )
            cds_dict[k+'3'] = ColumnDataSource(
                data=dict(
                    x=np.array(v[4])[:, 1],
                    y=np.array(v[5])[:, 1],
                    desc=np.array(v[5])[:, 0]
                )
            )

            # All 3 plots
            s1 = fig_dict[k].scatter(np.array(v[0])[:, 1], np.array(v[1])[:, 1],
                                     fill_color='red', size=13, source=cds_dict[k + '1'])

            s2 = fig_dict[k].scatter(np.array(v[2])[:, 1], np.array(v[3])[:, 1],
                                     fill_color='green', size=10, source=cds_dict[k + '2'])
            
            s3 = fig_dict[k].scatter(np.array(v[4])[:, 1], np.array(v[5])[:, 1],
                                     fill_color='blue', size=7, source=cds_dict[k+'3'])
            
            fig_dict[k].select(HoverTool).tooltips = {
                "x": "$x", "y": "$y", "year": "@desc"}

        # List of figures
        f_vals = list(fig_dict.values())

        # Creates gird for k2 x k2 grid plot by default k2 should be 3
        g = gridplot([([None]*(k2 - 1 - round((i + 1) / k2)) + f_vals[i: i + 1
                                                                      + round((i + 1) / k2)][::-1])
                      for i in range(0, n, k2)][::-1])
        return g
def plot():

    # FIGURES AND X-AXIS
    fig1 = Figure(title = 'Dive Profile',  plot_width = WIDTH, plot_height = HEIGHT, tools = TOOLS)
    fig2 = Figure(title = 'Dive Controls', plot_width = WIDTH, plot_height = HEIGHT, tools = TOOLS, x_range=fig1.x_range)
    fig3 = Figure(title = 'Attitude',      plot_width = WIDTH, plot_height = HEIGHT, tools = TOOLS, x_range=fig1.x_range)
    figs = gridplot([[fig1],[fig2],[fig3]])

    # Formatting x-axis
    timeticks = DatetimeTickFormatter(formats=dict(seconds =["%b%d %H:%M:%S"],
                                                   minutes =["%b%d %H:%M"],
                                                   hourmin =["%b%d %H:%M"],
                                                   hours =["%b%d %H:%M"],
                                                   days  =["%b%d %H:%M"],
                                                   months=["%b%d %H:%M"],
                                                   years =["%b%d %H:%M %Y"]))
    fig1.xaxis.formatter = timeticks
    fig2.xaxis.formatter = timeticks
    fig3.xaxis.formatter = timeticks

    # removing gridlines
    fig1.xgrid.grid_line_color = None
    fig1.ygrid.grid_line_color = None
    fig2.xgrid.grid_line_color = None
    fig2.ygrid.grid_line_color = None
    fig3.xgrid.grid_line_color = None
    fig3.ygrid.grid_line_color = None

    # INPUT WIDGETS
    collection_list = CONN[DB].collection_names(include_system_collections=False)
    gliders = sorted([platformID for platformID in collection_list if len(platformID)>2])
    gliders = Select(title = 'PlatformID', value = gliders[0], options = gliders)
    prev_glider = Button(label = '<')
    next_glider = Button(label = '>')
    glider_controlbox = HBox(children = [gliders, prev_glider, next_glider], height=80)

    chunkations = Select(title = 'Chunkation', value = 'segment', options = ['segment', '24hr', '30days', '-ALL-'])
    chunk_indicator = TextInput(title = 'index', value = '0')
    prev_chunk = Button(label = '<')
    next_chunk = Button(label = '>')
    chunk_ID   = PreText(height=80)
    chunk_controlbox = HBox(chunkations,
                            HBox(chunk_indicator, width=25),
                            prev_chunk, next_chunk,
                            chunk_ID,
                            height = 80)

    control_box = HBox(glider_controlbox,
                        chunk_controlbox)

    # DATA VARS
    deadby_date = ''
    depth    = ColumnDataSource(dict(x=[],y=[]))
    vert_vel = ColumnDataSource(dict(x=[],y=[]))

    mbpump   = ColumnDataSource(dict(x=[],y=[]))
    battpos  = ColumnDataSource(dict(x=[],y=[]))
    pitch    = ColumnDataSource(dict(x=[],y=[]))

    mfin      = ColumnDataSource(dict(x=[],y=[]))
    cfin      = ColumnDataSource(dict(x=[],y=[]))
    mroll     = ColumnDataSource(dict(x=[],y=[]))
    mheading = ColumnDataSource(dict(x=[],y=[]))
    cheading = ColumnDataSource(dict(x=[],y=[]))

    # AXIS setup
    colors = COLORS[:]

    fig1.y_range.flipped = True
    fig1.yaxis.axis_label = 'm_depth (m)'
    fig1.extra_y_ranges = {'vert_vel': Range1d(start=-50, end=50),
                           'dummy':    Range1d(start=0, end=100)}
    fig1.add_layout(place = 'right',
                    obj = LinearAxis(y_range_name = 'vert_vel',
                                     axis_label   = 'vertical velocity (cm/s)'))
    fig1.add_layout(place = 'left',
                    obj = LinearAxis(y_range_name = 'dummy',
                                     axis_label   = ' '))
    fig1.yaxis[1].visible = False
    fig1.yaxis[1].axis_line_alpha = 0
    fig1.yaxis[1].major_label_text_alpha = 0
    fig1.yaxis[1].major_tick_line_alpha = 0
    fig1.yaxis[1].minor_tick_line_alpha = 0


    fig2.yaxis.axis_label = 'pitch (deg)'
    fig2.y_range.start, fig2.y_range.end = -40,40
    fig2.extra_y_ranges = {'battpos': Range1d(start=-1, end = 1),
                           'bpump':   Range1d(start=-275, end=275)}
    fig2.add_layout(place = 'right',
                    obj = LinearAxis(y_range_name = 'battpos',
                                     axis_label = 'battpos (in)'))
    fig2.add_layout(place = 'left',
                    obj = LinearAxis(y_range_name = 'bpump',
                                     axis_label   = 'bpump (cc)'))
    fig2.yaxis[1].visible = False # necessary for spacing. later gets set to true


    fig3.yaxis.axis_label = 'fin/roll (deg)'
    fig3.y_range.start, fig3.y_range.end = -30, 30
    fig3.extra_y_ranges = {'heading': Range1d(start=0, end=360), #TODO dynamic avg centering
                           'dummy':   Range1d(start=0, end=100)}
    fig3.add_layout(place = 'right',
                    obj = LinearAxis(y_range_name = 'heading',
                                     axis_label   = 'headings (deg)'))
    fig3.add_layout(place = 'left',
                    obj = LinearAxis(y_range_name = 'dummy',
                                     axis_label   = ' '))
    fig3.yaxis[1].visible = False
    fig3.yaxis[1].axis_line_alpha = 0
    fig3.yaxis[1].major_label_text_alpha = 0
    fig3.yaxis[1].major_tick_line_alpha = 0
    fig3.yaxis[1].minor_tick_line_alpha = 0

    # PLOT OBJECTS
    fig1.line(  'x', 'y', source = depth,    legend = 'm_depth',     color = 'red')
    fig1.circle('x', 'y', source = depth,    legend = 'm_depth',     color = 'red')
    fig1.line(  'x', 'y', source = vert_vel, legend = 'vert_vel',    color = 'green',     y_range_name = 'vert_vel')
    fig1.circle('x', 'y', source = vert_vel, legend = 'vert_vel',    color = 'green',     y_range_name = 'vert_vel')
    fig1.renderers.append(Span(location = 0, dimension = 'width',    y_range_name = 'vert_vel',
                               line_color= 'green', line_dash='dashed', line_width=1))

    fig2.line(  'x', 'y', source = pitch,   legend = "m_pitch",    color = 'indigo')
    fig2.circle('x', 'y', source = pitch,   legend = "m_pitch",    color = 'indigo')
    fig2.line(  'x', 'y', source = battpos, legend = 'm_battpos',  color = 'magenta',   y_range_name = 'battpos')
    fig2.circle('x', 'y', source = battpos, legend = 'm_battpos',  color = 'magenta',   y_range_name = 'battpos')
    fig2.line(  'x', 'y', source = mbpump,  legend = "m_'bpump'",  color = 'blue',      y_range_name = 'bpump')
    fig2.circle('x', 'y', source = mbpump,  legend = "m_'bpump'",  color = 'blue',      y_range_name = 'bpump')
    fig2.renderers.append(Span(location = 0, dimension = 'width',
                               line_color= 'black', line_dash='dashed', line_width=1))
    fig3.line(  'x', 'y', source = mfin,       legend = 'm_fin',     color = 'cyan')
    fig3.circle('x', 'y', source = mfin,       legend = 'm_fin',     color = 'cyan')
    fig3.line(  'x', 'y', source = cfin,       legend = 'c_fin',     color = 'orange')
    fig3.circle('x', 'y', source = cfin,       legend = 'c_fin',     color = 'orange')
    fig3.line(  'x', 'y', source = mroll,      legend = 'm_roll',    color = 'magenta')
    fig3.circle('x', 'y', source = mroll,      legend = 'm_roll',    color = 'magenta')
    fig3.line(  'x', 'y', source = mheading,   legend = 'm_heading', color = 'blue',    y_range_name = 'heading')
    fig3.circle('x', 'y', source = mheading,   legend = 'm_heading', color = 'blue',    y_range_name = 'heading')
    fig3.line(  'x', 'y', source = cheading,   legend = 'c_heading', color = 'indigo',  y_range_name = 'heading')
    fig3.circle('x', 'y', source = cheading,   legend = 'c_heading', color = 'indigo',  y_range_name = 'heading')
    fig3.renderers.append(Span(location = 0, dimension = 'width',    y_range_name = 'default',
                               line_color= 'black', line_dash='dashed', line_width=1))

    # CALLBACK FUNCS
    def update_data(attrib,old,new):
        g = gliders.value
        chnk = chunkations.value
        chindex = abs(int(chunk_indicator.value))

        depth.data    = dict(x=[],y=[])
        vert_vel.data = dict(x=[],y=[])
        mbpump.data   = dict(x=[],y=[])
        battpos.data  = dict(x=[],y=[])
        pitch.data    = dict(x=[],y=[])

        mfin.data     = dict(x=[],y=[])
        cfin.data     = dict(x=[],y=[])
        mroll.data    = dict(x=[],y=[])
        mheading.data = dict(x=[],y=[])
        cheading.data = dict(x=[],y=[])


        depth.data,startend   = load_sensor(g, 'm_depth', chnk, chindex)

        if chnk == 'segment':
            xbd = startend[2]
            chunk_ID.text = '{} {} \n{} ({}) \nSTART: {} \nEND:   {}'.format(g, xbd['mission'],
                                                                             xbd['onboard_filename'], xbd['the8x3_filename'],
                                                                             e2ts(xbd['start']), e2ts(xbd['end']))
            if len(set(depth.data['x']))<=1 and attrib == 'chunk':
                if old > new:
                    next_chunk.clicks += 1
                else:
                    prev_chunk.clicks += 1
                return
            elif len(set(depth.data['x']))<=1 and chunk_indicator.value == 0:
                chunk_indicator.value = 1

        elif chnk in ['24hr', '30days']:
            chunk_ID.text = '{} \nSTART: {} \nEND:   {}'.format(g, e2ts(startend[0]), e2ts(startend[1]))
        elif chnk == '-ALL-':
            chunk_ID.text = '{} \nSTART: {} \nEND:   {}'.format(g,e2ts(depth.data['x'][0] /1000),
                                                                  e2ts(depth.data['x'][-1]/1000))


        vert_vel.data  = calc_vert_vel(depth.data)

        mbpump.data,_     = load_sensor(g, 'm_de_oil_vol', chnk, chindex)
        if len(mbpump.data['x']) > 1:
            #for yax in fig2.select('mbpump'):
            #    yax.legend = 'm_de_oil_vol'
            pass
        else:
            mbpump.data,_     = load_sensor(g, 'm_ballast_pumped', chnk, chindex)
            #for yax in fig2.select('mbpump'):
            #    yax.legend = 'm_ballast_pumped'
        battpos.data,_ = load_sensor(g, 'm_battpos',    chnk, chindex)
        pitch.data,_   = load_sensor(g, 'm_pitch',      chnk, chindex)
        pitch.data['y'] = [math.degrees(y) for y in pitch.data['y']]

        mfin.data,_     = load_sensor(g, 'm_fin',     chnk, chindex)
        cfin.data,_     = load_sensor(g, 'c_fin',     chnk, chindex)
        mroll.data,_    = load_sensor(g, 'm_roll',    chnk, chindex)
        mheading.data,_ = load_sensor(g, 'm_heading', chnk, chindex)
        cheading.data,_ = load_sensor(g, 'c_heading', chnk, chindex)
        mfin.data['y']     = [math.degrees(y) for y in mfin.data['y']]
        cfin.data['y']     = [math.degrees(y) for y in cfin.data['y']]
        mheading.data['y'] = [math.degrees(y) for y in mheading.data['y']]
        cheading.data['y'] = [math.degrees(y) for y in cheading.data['y']]
        mroll.data['y']    = [math.degrees(y) for y in mroll.data['y']]

        fig1.yaxis[1].visible = True
        fig2.yaxis[1].visible = True
        fig3.yaxis[1].visible = True


    #GLIDER SELECTS
    def glider_buttons(increment):
        ops = gliders.options
        new_index = ops.index(gliders.value) + increment
        if new_index >= len(ops):
            new_index = 0
        elif new_index < 0:
            new_index = len(ops)-1
        gliders.value = ops[new_index]
        chunkation_update(None, None, None) #reset chunk indicator and clicks
    def next_glider_func():
        glider_buttons(1)
    def prev_glider_func():
        glider_buttons(-1)
    def update_glider(attrib,old,new):
        chunk_indicator.value = '0'
        #update_data(None,None,None)


    gliders.on_change('value', update_glider)
    next_glider.on_click(next_glider_func)
    prev_glider.on_click(prev_glider_func)


        #CHUNK SELECTS
    def chunkation_update(attrib,old,new):
        chunk_indicator.value = '0'
        prev_chunk.clicks = 0
        next_chunk.clicks = 0
        update_data(None,None,None)
        if new == '-ALL-':
            chunk_indicator.value = '-'

    def chunk_func():
        chunkdiff = prev_chunk.clicks - next_chunk.clicks
        if chunkdiff < 0:
            prev_chunk.clicks = 0
            next_chunk.clicks = 0
            chunkdiff = 0
        print (chunkdiff)
        chunk_indicator.value = str(chunkdiff)

    def chunk_indicator_update(attrib,old,new):
        try:
            if abs(int(old)-int(new))>1: #manual update, triggers new non-manual indicator update, ie else clause below
                prev_chunk.clicks = int(new)
                next_chunk.clicks = 0
            else:
                update_data('chunk',int(old),int(new))
            print("UPDATE", old, new)
        except Exception as e:
            print(type(e),e, old, new)

    chunkations.on_change('value', chunkation_update)
    chunk_indicator.on_change('value', chunk_indicator_update)
    next_chunk.on_click(chunk_func)
    prev_chunk.on_click(chunk_func)

    update_data(None,None,None)

    return vplot(control_box, figs)
Exemple #57
0
def plot_raw_traces(traces, time, stim_name, ch, save_folder, stim_dict,
                    stim_data):
    print 'shape traces[:,0] = ', traces[:, 0].shape
    print 'time shape = ', time.shape

    #################### BOKEH FIGURE #########################
    fig = plt.figure(figsize=(20, 10))
    num_trials = traces.shape[1]
    gs = gridspec.GridSpec(num_trials, 1,
                           width_ratios=[1, 1])  ## a subplot for every trial
    ax = range(num_trials)
    all_spike_times = []
    raster = figure(width=1000,
                    height=400,
                    y_axis_label='Trial Number',
                    title='Raster + Histogram Channel %d, Orientation %s' %
                    (ch + 1, stim_name))
    spike_vec = np.zeros([len(time), 1])

    time_vec = np.linspace(0, len(time), len(time))
    print 'stim_dict[stim_name] = ', stim_dict[stim_name]
    print 'stim_data.times[stim_dict[stim_name]] = ', stim_data.times[
        stim_dict[stim_name]]
    print 'stim_name = ', stim_name

    spike_mat = get_sua_times(stim_data.times[
        stim_dict[stim_name]])  ### this will need times of the stimuli...
    # spike_times === time (30e3) x trials

    for trial in range(num_trials):
        print 'trial # ', trial
        peaks, times, ifr_vec = run_spike_detect(traces[:, trial])

        # for each trial, get spike times. Times = samples that have spikes, between -15e3 and +15e3?

        all_spike_times.append(times)

        spike_vec[times] += 1

        trial_time = [t / 30e3 - 0.5 for t in times]
        #ax = plt.subplot(gs[trial])
        #sns.tsplot(downsample(traces[:,trial],10),time=time,value='Voltage (uV)',color='black',linewidth=0.1)
        ax[trial] = figure(width=1000, plot_height=500)
        #s1 = figure(width=1000, plot_height=500, title='Spikes')
        ax[trial].line(time, traces[:,
                                    trial])  ## (time is already downsampled)
        ax[trial].circle(
            trial_time, peaks, color='red'
        )  ## convert to seconds and subtract 0.5 b/c plotting data on time from -0.5 to +0.5 seconds

        #ax.set_ylim([-1000,1000])
        #ax.set(y_range=Range1d(-1000, 1000))
        #axes.append[ax]

        raster.segment(x0=trial_time,
                       y0=np.repeat(trial, len(times)),
                       x1=trial_time,
                       y1=np.repeat(trial + 1, len(times)),
                       color="black",
                       line_width=0.5)

    p = gridplot([[s1] for s1 in ax])  #gridplot([[s1] for s1 in axes])

    #fig.savefig(save_folder + '/raw_psth_'+str(stim_name)+'.pdf')

    output_file(save_folder + '/raw_psth_' + str(stim_name) + '.html')
    # show the results
    save(p)
    ############## SPIKE HISTOGRAM FIGURE ############################
    histo_fig = figure(width=1000,
                       plot_height=500,
                       y_axis_label='Firing Rate (Hz)',
                       x_axis_label='Time (sec)',
                       x_range=raster.x_range)
    print 'len all_spike_times = ', len(all_spike_times)
    num_bins = 50
    hist, edges = np.histogram(flatten(all_spike_times), bins=num_bins)
    bin_width = np.diff(edges)[0] / fs  # in seconds.
    edges = edges / fs - 0.5  ## plot x-axis in seconds.
    histo_fig.quad(
        top=hist / bin_width / num_trials,
        bottom=0,
        left=edges[:-1],
        right=edges[1:],  ## hist/bin_width = firing rate in Hz
        fill_color="#036564",
        line_color="#033649")

    #time
    # pass this to the sliding window - get sum of spikes in each 100ms window.
    win_size = 2**8
    win_step = 2**2

    ##################!!!!!!!!!!!###### -- should "spike_vec" here be the a sorted "all_spike_times"? yes...
    win_x, windowd_spike_vec = windowed_histogram(spike_vec, time_vec,
                                                  win_size, win_step)
    ##################!!

    histo_fig.line([t / fs - 0.5 for t in win_x],
                   [w / win_size * fs / num_trials for w in windowd_spike_vec],
                   color='magenta')

    output_file(save_folder + '/spike_histogram_' + str(stim_name) + '.html')
    grid = gridplot([[raster], [histo_fig]])

    save(grid)
Exemple #58
0
p3.xaxis.minor_tick_line_color = None
p3.line(temp_graph['date_t'], temp_graph['abs_two_year'], color='red', line_width=3, legend="Two-year absolute revision")

p3.legend.location = "bottom_left"

p4 = figure(width=600, height=300, title=temp_graph['description'].iloc[0], x_axis_type="datetime", outline_line_color = None)
p4.xgrid.grid_line_color = None
p4.ygrid.grid_line_color = None
p4.yaxis.minor_tick_line_color = None
p4.xaxis.minor_tick_line_color = None
p4.line(temp_graph['date_t'], temp_graph['adv_less_current'], color='red', line_width=3, legend="Advance less current")

p4.legend.location = "bottom_left"

# put all the plots in a grid layout
p = gridplot([[p1, p2], [p3, p4]])

# show the results
#show(p)


########## BUILD FIGURES ################

source = ColumnDataSource(temp_graph)
columns = [
        TableColumn(field='code', title = "BEA - Code", width = temp_graph['code'].map(len).max()),
        TableColumn(field='description', title = "Description", width = temp_graph['description'].map(len).max()),
        TableColumn(field='ADVANCE', title = "Advanced Est", width = 5),
        TableColumn(field='SECOND', title = "Second Est", width = 5),
        TableColumn(field='THIRD', title = "Third Est", width = 5),
        TableColumn(field='adv_less_third', title = "Revision (advance est less third est)", width = 10),
Exemple #59
0
s1.circle(x, y0, size=10, color="navy", alpha=0.5)
gList = []
#plots are put into a 2x2 grid format. 0-10 means 22 plots
#to test other numbers of plots, simply change the end range to 1/2 of the desired number
#for i in range (1):
#    if i == 0:
#    	#each plot seems to need to be named differently to be graphed properly
#    	#exec calls are a method for procedurally generating variable names 
#        exec("g%d = s1" % i)
#        exec("g%d = s1" % (i+1))
#        exec("g%d.circle(x, y0, size=10, color=\"navy\", alpha=0.5)" % i)
#        exec("g%d.circle(x, y0, size=10, color=\"navy\", alpha=0.5)" % (i+1))
#        exec("gList.append([g%d, g%d])" % (i , (i+1)))
#    else:
#        exec("g%d = s1" % (i+2))
#        exec("g%d = s1" % (i+3))
#        exec("g%d.circle(x, y0, size=10, color=\"navy\", alpha=0.5)" % (i+2))
#        exec("g%d.circle(x, y0, size=10, color=\"navy\", alpha=0.5)" % (i+3))
#        exec("gList.append([g%d, g%d])" % ((i+2) , (i+3)))
# put all the plots in a VBox
i = 1
exec("g%d = s1" % i)
exec("g%d = s1" % (i+1))
exec("g%d.circle(x, y0, size=10, color=\"navy\", alpha=0.5)" % i)
exec("g%d.circle(x, y0, size=10, color=\"navy\", alpha=0.5)" % (i+1))
exec("gList.append([g%d, g%d])" % (i , (i+1)))
p = gridplot(gList)

# show the results
show(p)
#code is a mixture of my own + tutorials on the website.