Exemplo n.º 1
0
    def create_widgets(self):
        def callback(attr, old, new):

            l_values = [o_wdg_check.labels[i] for i in o_wdg_check.active]
            o_source_sub = ColumnDataSource(self.create_data_points(l_values))
            o_source.data = o_source_sub.data

        ########## list with values

        l_chkgrp_values = list(self.df_data[self.s_col_check_grp].unique())

        ########## create widget table

        o_cols = [TableColumn(field=i, title=i) for i in self.df_data.columns]
        o_source = ColumnDataSource(self.create_data_points(l_chkgrp_values))
        o_wdg_table = DataTable(source=o_source,
                                columns=o_cols,
                                width=self.params['width'],
                                height=self.params['height'])

        ##### create widget checkbox

        o_wdg_check = CheckboxGroup(labels=l_chkgrp_values)
        o_wdg_check.on_change('active', callback)

        return o_wdg_table, o_wdg_check
Exemplo n.º 2
0
def create_checkboxes(cont):
    """
    Create checkboxes.
    Args:
        cont (str): Continent name.
    Returns:
        all_regions1 (list): List of regions corresponding to checkbox column1. 
        all_regions2 (list): List of regions corresponding to checkbox column2.
        region_selection1 (:obj:`CheckboxGroup`): Checkbox column1.
        region_selection2 (:obj:`CheckboxGroup`): Checkbox column2.
    """
    # Split into groups to make the checkboxes 2 columns
    all_regions = by_cont[cont]
    middle = int(np.floor(len(all_regions) / 2))
    all_regions1 = all_regions[:middle]
    all_regions2 = all_regions[middle:]
    region_selection1 = CheckboxGroup(labels=all_regions1,
                                      active=[0],
                                      css_classes=["custom_checkbox"])
    region_selection2 = CheckboxGroup(labels=all_regions2,
                                      active=[],
                                      css_classes=["custom_checkbox"])
    region_selection1.on_change('active', update_plot)
    region_selection2.on_change('active', update_plot)

    return all_regions1, all_regions2, region_selection1, region_selection2
Exemplo n.º 3
0
def heatmap_tab(controls):

	# the list of control domains will be colorcoded for the heat map
	controls_domains=list(set((dataFrame[dataFrame['root']==True])['control_name']))
	controls_domains.sort()
	controls_colors = Category20_16
	controls_colors.sort()

	controls_selection = CheckboxGroup(labels=controls_domains, 
									  active = [0, 1])
	range_select = RangeSlider(start = 0, end = 100, value = (80, 100),
							   step = 10, title = 'Compliance % Range')
	
	def form_controls_dataset(controls_list, range_start = 80, range_end = 100):

		# Dataframe to hold information
		heatmap_df = pd.DataFrame(columns=['impact', 'frequency', 'control'])
		range_extent = range_end - range_start

		# Iterate through all the carriers
		for i, control_name in enumerate(controls_list):

			# Add to the overall dataframe
			heatmap_df = heatmap_df.append(temp_df)

		# Overall dataframe
		heatmap_df = heatmap_df.sort_values(['impact', 'frequency'])

		return ColumnDataSource(heatmap_df)


	controls_dataset = form_controls_dataset(list(controls_domains[0]),
					   range_start = range_select.value[0],
					   range_end = range_select.value[1])

	def update(attr, old, new):
		controls_to_plot = []
		 
		for i in controls_selection.active:
			tmp_control_name=controls_selection.labels[i]
			tmp_control_id=

		
		new_controls_dataset = form_controls_dataset(controls_to_plot,
							   range_start = range_select.value[0],
							   range_end = range_select.value[1])
		
		

		controls_dataset.data.update(new_controls_dataset.data)

	controls_selection.on_change('active', update)
	range_select.on_change('value', update)


	# return tab
Exemplo n.º 4
0
    def create_widgets(self):
        def callback(attr, old, new):

            l_active = [o_wdg_check.labels[i] for i in o_wdg_check.active]
            df_sub_data = self.df_data[self.df_data[self.s_col_labels].isin(
                l_active)]
            l_values, l_time, l_labels = self.create_data_points(df_sub_data)
            o_source.data = {
                'x': l_time,
                'y': l_values,
                'labels': l_labels,
                'mypal': l_colors[0:len(l_values)]
            }

        ########## get data points

        l_values, l_time, l_labels = self.create_data_points(self.df_data)

        ########## create widget plot

        o_source = ColumnDataSource(
            data={
                'x': l_time,
                'y': l_values,
                'labels': l_labels,
                'mypal': l_colors[0:len(l_values)]
            })
        TOOLTIPS = [("item", "@labels"), ("value", "$y")]
        o_wdg_plot = figure(width=self.params['width'],
                            height=self.params['height'],
                            x_axis_type='datetime',
                            y_axis_type='linear',
                            tooltips=TOOLTIPS,
                            toolbar_location=None,
                            title=self.params['title'])

        if self.params['legend'] == True:
            o_wdg_plot.multi_line('x',
                                  'y',
                                  source=o_source,
                                  line_width=2,
                                  line_color='mypal',
                                  legend='labels')
        else:
            o_wdg_plot.multi_line('x',
                                  'y',
                                  source=o_source,
                                  line_width=2,
                                  line_color='mypal')

        ########## create widget checkbox

        o_wdg_check = CheckboxGroup(labels=l_labels)
        o_wdg_check.on_change('active', callback)

        return o_wdg_plot, o_wdg_check
Exemplo n.º 5
0
 def layout(self):
     # Build interactive user interface
     checkbox1 = CheckboxGroup(labels=[
                               "Start/Stop Sensor Thread", "Start/Stop Plotting"], active=[0, 1])  # Create checkboxes
     # Specify the action to be performed upon change in checkboxes' values
     checkbox1.on_change('active', self.checkbox1Handler)
     # Build presentation layout
     # Place the text at the top, followed by checkboxes and graphs in a row below
     layout = column(self.text1, row(checkbox1, self.pAll))
     self.doc.title = "Real Time Sensor Data Streaming"  # Name of internet browser tab
     self.doc.add_root(layout)  # Add the layout to the web document
Exemplo n.º 6
0
def emission_lines(fname):
    def line_update(attr, old, new):
        # TODO: make it faster by finding a method to only modify the changed object
        z_list = [(1 + float(z.value)) for z in z_in_list]
        for i in range(len(line_list)):
            for j in range(len(line_list[i])):
                # j: jth line for ith element
                line_list[i][j].visible = True if not (checkboxes[i].active
                                                       == []) else False
                line_list[i][j].location = emission_lines[i][j] * z_list[i]

    # variables
    emission_lines = np.genfromtxt(fname,
                                   skip_header=1,
                                   delimiter=',',
                                   unpack=True)
    linename_list = np.loadtxt(fname,
                               delimiter=',',
                               unpack=True,
                               max_rows=1,
                               dtype='str')
    line_list = []  # line_list[i][j] for jth line in ith element
    checkboxes = []  # checkboxes[i] for ith element
    z_in_list = []  # z_in_list[i]  for ith element
    v_in_list = []  # v_in_list[i]  for ith element

    # generate widgets
    for i in range(len(emission_lines)):
        element = linename_list[i]
        print('Setting lines for ', element, '...')
        b_tmp = CheckboxGroup(labels=[element], active=[])
        b_tmp.on_change('active', line_update)
        checkboxes.append(b_tmp)
        #z_tmp = TextInput(value='2',title=element,sizing_mode='scale_width')
        z_tmp = TextInput(value='0', sizing_mode='scale_width')
        z_tmp.on_change('value', line_update)
        z_in_list.append(z_tmp)
        lines_for_this_element = []
        for j in range(len(emission_lines[i])):
            wavelength = emission_lines[i][j]
            if not np.isnan(wavelength):
                print('\t* lambda = ', emission_lines[i][j])
                line_tmp = Span(location=wavelength,
                                dimension='height',
                                line_color='orange',
                                line_width=1)  # TODO: line color
                lines_for_this_element.append(line_tmp)
        line_list.append(lines_for_this_element)
    line_update('', '', '')
    return line_list, z_in_list, checkboxes
Exemplo n.º 7
0
def modify_doc(doc):

    status_selection = CheckboxGroup(
        labels=['viewed', 'explored', 'certified'], active=[0, 1])
    status_selection.on_change('active', update)
    initial_status = [
        status_selection.labels[i] for i in status_selection.active
    ]
    src = make_dataset(edX, initial_status)
    p = make_plot(src)
    controls = WidgetBox(status_selection)
    layout = row(controls, p)
    tab = Panel(child=layout, title='Events Histogram')
    tabs = Tabs(tabs=[tab])
    doc.add_root(tabs)
Exemplo n.º 8
0
def histogram_tab(flights):

    # Function to make a dataset for histogram based on a list of carriers
    # a minimum delay, maximum delay, and histogram bin width
    def make_dataset(carrier_list,
                     range_start=-60,
                     range_end=120,
                     bin_width=5):

        # Dataframe to hold information
        by_carrier = pd.DataFrame(columns=[
            'proportion', 'left', 'right', 'f_proportion', 'f_interval',
            'name', 'color'
        ])

        range_extent = range_end - range_start

        # Iterate through all the carriers
        for i, carrier_name in enumerate(carrier_list):

            # Subset to the carrier
            subset = flights[flights['name'] == carrier_name]

            # Create a histogram with 5 minute bins
            arr_hist, edges = np.histogram(subset['arr_delay'],
                                           bins=int(range_extent / bin_width),
                                           range=[range_start, range_end])

            # Divide the counts by the total to get a proportion
            arr_df = pd.DataFrame({
                'proportion': arr_hist / np.sum(arr_hist),
                'left': edges[:-1],
                'right': edges[1:]
            })

            # Format the proportion
            arr_df['f_proportion'] = [
                '%0.5f' % proportion for proportion in arr_df['proportion']
            ]

            # Format the interval
            arr_df['f_interval'] = [
                '%d to %d minutes' % (left, right)
                for left, right in zip(arr_df['left'], arr_df['right'])
            ]

            # Assign the carrier for labels
            arr_df['name'] = carrier_name

            # Color each carrier differently
            arr_df['color'] = Category20_16[i]

            # Add to the overall dataframe
            by_carrier = by_carrier.append(arr_df)

        # Overall dataframe
        by_carrier = by_carrier.sort_values(['name', 'left'])

        return ColumnDataSource(by_carrier)

    def style(p):
        # Title
        p.title.align = 'center'
        p.title.text_font_size = '20pt'
        p.title.text_font = 'serif'

        # Axis titles
        p.xaxis.axis_label_text_font_size = '14pt'
        p.xaxis.axis_label_text_font_style = 'bold'
        p.yaxis.axis_label_text_font_size = '14pt'
        p.yaxis.axis_label_text_font_style = 'bold'

        # Tick labels
        p.xaxis.major_label_text_font_size = '12pt'
        p.yaxis.major_label_text_font_size = '12pt'

        return p

    def make_plot(src):
        # Blank plot with correct labels
        p = figure(plot_width=700,
                   plot_height=700,
                   title='Histogram of Arrival Delays by Airline',
                   x_axis_label='Delay (min)',
                   y_axis_label='Proportion')

        # Quad glyphs to create a histogram
        p.quad(source=src,
               bottom=0,
               top='proportion',
               left='left',
               right='right',
               color='color',
               fill_alpha=0.7,
               hover_fill_color='color',
               legend='name',
               hover_fill_alpha=1.0,
               line_color='black')

        # Hover tool with vline mode
        hover = HoverTool(tooltips=[('Carrier', '@name'),
                                    ('Delay', '@f_interval'),
                                    ('Proportion', '@f_proportion')],
                          mode='vline')

        p.add_tools(hover)

        # Styling
        p = style(p)

        return p

    def update(attr, old, new):
        carriers_to_plot = [
            carrier_selection.labels[i] for i in carrier_selection.active
        ]

        new_src = make_dataset(carriers_to_plot,
                               range_start=range_select.value[0],
                               range_end=range_select.value[1],
                               bin_width=binwidth_select.value)

        src.data.update(new_src.data)

    # Carriers and colors
    available_carriers = list(set(flights['name']))
    available_carriers.sort()

    airline_colors = Category20_16
    airline_colors.sort()

    carrier_selection = CheckboxGroup(labels=available_carriers, active=[0, 1])
    carrier_selection.on_change('active', update)

    binwidth_select = Slider(start=1,
                             end=30,
                             step=1,
                             value=5,
                             title='Bin Width (min)')
    binwidth_select.on_change('value', update)

    range_select = RangeSlider(start=-60,
                               end=180,
                               value=(-60, 120),
                               step=5,
                               title='Range of Delays (min)')
    range_select.on_change('value', update)

    # Initial carriers and data source
    initial_carriers = [
        carrier_selection.labels[i] for i in carrier_selection.active
    ]

    src = make_dataset(initial_carriers,
                       range_start=range_select.value[0],
                       range_end=range_select.value[1],
                       bin_width=binwidth_select.value)
    p = make_plot(src)

    # Put controls in a single element
    controls = WidgetBox(carrier_selection, binwidth_select, range_select)

    # Create a row layout
    layout = row(controls, p)

    # Make a tab with the layout
    tab = Panel(child=layout, title='Histogram')

    return tab
Exemplo n.º 9
0
class HotelApp(VBox):
    extra_generated_classes = [["HotelApp", "HotelApp", "VBox"]]
    jsmodel = "VBox"

    # input
    selectr = Instance(Select)
    #check_group = Instance(RadioGroup)
    check_group = Instance(CheckboxGroup)

    # plots
    plot = Instance(GMapPlot)
    filler = Instance(Plot)
    filler2 = Instance(Plot)
    bar_plot = Instance(Plot)
    legend_plot = Instance(Plot)
    legend_filler = Instance(Plot)

    # data source
    source = Instance(ColumnDataSource)
    county_source = Instance(ColumnDataSource)

    # layout boxes
    checkbox = Instance(VBox)
    mainrow = Instance(HBox)
    statsbox = Instance(VBox)
    mapbox = Instance(VBox)
    legendbox = Instance(HBox)
    totalbox = Instance(VBox)

    # inputs
    #ticker1_select = Instance(Select)
    #ticker2_select = Instance(Select)
    #input_box = Instance(VBoxForm)

    def make_inputs(self):
        with open("states.csv") as f:
            states = [line.strip().split(',') for line in f.readlines()]

        self.selectr = Select(
            name='states',
            value='Choose A State',
            options=[s[1] for s in states] + ['Choose A State'],
        )
        labels = ["County Averages", "Hotels"]
        self.check_group = CheckboxGroup(labels=labels, active=[0,1], inline=True)
        ##Filler plot
        x_range = Range1d(0, 300)
        y_range = Range1d(0, 12)
        self.filler = Plot(
            x_range=x_range, y_range=y_range, title="", 
            plot_width=300, plot_height=12, min_border=0, 
            **PLOT_FORMATS
        )  
        x_range = Range1d(0, 300)
        y_range = Range1d(0, 18)
        self.filler2 = Plot(
            x_range=x_range, y_range=y_range, title="", 
            plot_width=300, plot_height=14, min_border=0, 
            **PLOT_FORMATS
        )  




    def make_outputs(self):
        pass
        #self.pretext = Paragraph(text="", width=800)

    def __init__(self, *args, **kwargs):
        super(HotelApp, self).__init__(*args, **kwargs)
        self._show_counties = True
        self._show_hotels = True

    @classmethod
    def create(cls):
        """
        This function is called once, and is responsible for
        creating all objects (plots, datasources, etc)
        """
        # create layout widgets
        obj = cls()
        obj.mainrow = HBox()
        obj.checkbox = VBox(height=50)
        obj.mapbox = VBox()
        #obj.bottomrow = HBox()
        obj.statsbox = VBox( width=500)
        obj.totalbox = VBox()
        obj.legendbox = HBox()

        labels = ["County Average Ratings", "Hotel Locations"]
        obj.make_inputs()
        obj.make_outputs()

        # outputs
        #obj.pretext = Paragraph(text="", width=500)
        obj.make_source()
        obj.make_county_source()
        lat=39.8282
        lng=-98.5795
        zoom=6
        xr = Range1d()
        yr = Range1d()
        #obj.make_plots(lat, lng, zoom, xr, yr)
        obj.make_plots()

        # layout
        obj.set_children()
        return obj

    @property
    def selected_df(self):
        pandas_df = self.df
        selected = self.source.selected
        print "seeing if selected!"
        if selected:
            idxs = selected['1d']['indices']
            pandas_df = pandas_df.iloc[idxs, :]
        else:
            pandas_df = pandas_df.iloc[0:0, :]
        return pandas_df


    def make_source(self):
        self.source = ColumnDataSource(data=self.df)
        self.source.callback = Callback(args=dict(), code="""

            var inds = cb_obj.get('selected')['1d'].indices;
            var theidx = inds[0];

            console.log("yep");
            console.log(theidx);
            $.get( "reviews", {id: theidx}, function( response ) {
                $( "#section2" ).html( response );
            }, "html");
            console.log("done");
        """)


    def update_source(self):
        if self.selectr.value is None or self.selectr.value == 'Choose A State':
            df = hdata
        else:
            df = hdata[hdata['state'] == self.selectr.value]

        new_data = {}
        for col in df:
            new_data[col] = df[col]

        self.source.data = new_data

    def make_county_source(self):
        self.county_source = ColumnDataSource(data=self.countydf)

    def update_county_source(self):
        if self.selectr.value is None or self.selectr.value == 'Choose A State':
            df = county_data
        else:
            df = county_data[county_data['state'] == self.selectr.value]

        new_data = {}
        for col in df:
            new_data[col] = df[col]

        self.county_source.data = new_data
        #for col in df:
        #    self.county_source.data[col] = df[col]


    def make_plots(self):
        self.create_map_plot()
        self.create_legend()
        self.populate_glyphs()
        self.make_bar_plot()

    def create_legend(self):

        x_range = Range1d(0, 185)
        y_range = Range1d(0, 130)

        text_box = Plot(
            x_range=x_range, y_range=y_range, title="", 
            plot_width=185, plot_height=130, min_border=0, 
            **PLOT_FORMATS
        )  

        FONT_PROPS_SM['text_font_size'] = '11pt'
        text_box.add_glyph(
            Text(x=35, y=9, text=['Low Average Rating'],  **FONT_PROPS_SM)
        )
        text_box.add_glyph(
            Rect(x=18, y=18, width=25, height=25, 
                fill_color='#ef4e4d', line_color=None)
        )
        text_box.add_glyph(
            Text(x=35, y=49, text=['Medium Average Rating'],  **FONT_PROPS_SM)
        )
        text_box.add_glyph(
            Rect(x=18, y=58, width=25, height=25, 
                fill_color='#14a1af', line_color=None)
        )

        text_box.add_glyph(
            Text(x=35, y=89, text=['High Average Rating'],  **FONT_PROPS_SM)
        )
        text_box.add_glyph(
            Rect(x=18, y=98, width=25, height=25, 
                fill_color='#743184', line_color=None)
        )

        self.legend_plot = text_box

        ##Filler plot
        x_range = Range1d(0, 40)
        y_range = Range1d(0, 100)
        self.legend_filler = Plot(
            x_range=x_range, y_range=y_range, title="", 
            plot_width=40, plot_height=100, min_border=0, 
            **PLOT_FORMATS
        )  
 


    def create_map_plot(self):
        lat=39.8282
        lng=-98.5795
        zoom=6
        xr = Range1d()
        yr = Range1d()
        x_range = xr
        y_range = yr
        #map_options = GMapOptions(lat=39.8282, lng=-98.5795, zoom=6)
        map_options = GMapOptions(lat=lat, lng=lng, zoom=zoom)
        #map_options = GMapOptions(lat=30.2861, lng=-97.7394, zoom=15)
        plot = GMapPlot(
            x_range=x_range, y_range=y_range,
            map_options=map_options,
            plot_width=680,
            plot_height=600,
            title=" "
        )
        plot.map_options.map_type="hybrid"
        xaxis = LinearAxis(axis_label="lon", major_tick_in=0, formatter=NumeralTickFormatter(format="0.000"))
        plot.add_layout(xaxis, 'below')
        yaxis = LinearAxis(axis_label="lat", major_tick_in=0, formatter=PrintfTickFormatter(format="%.3f"))
        plot.add_layout(yaxis, 'left')

        self.plot = plot

    def populate_glyphs(self):
        self.plot.renderers=[]
        self.plot.tools=[]
        if self._show_counties:
            print "showing you the counties"
            #datasource = ColumnDataSource(county_data)
            #apatch = Patches(xs=county_xs, ys=county_ys, fill_color='white')
            #apatch = Patches(xs='xs', ys='ys', fill_color='colors', fill_alpha="alpha")
            apatch = Patches(xs='xs', ys='ys', fill_color='thecolors', fill_alpha='alpha')
            self.plot.add_glyph(self.county_source, apatch, name='counties')

        if self._show_hotels:
            print "showing you the hotels"
            circle2 = Circle(x="lon", y="lat", size=10, fill_color="fill2", fill_alpha=1.0, line_color="black")
            circle = Circle(x="lon", y="lat", size=10, fill_color="fill", fill_alpha=1.0, line_color="black")
            #print "source is ", self.source['lon'], self.source['lat'], self.source['f1ll']
            self.plot.add_glyph(self.source, circle2, nonselection_glyph=circle, name='hotels')
            #county_xs, county_ys = get_some_counties()

        rndr = self.plot.renderers[-1]
        pan = PanTool()
        wheel_zoom = WheelZoomTool()
        box_select = BoxSelectTool()
        box_select.renderers = [rndr]
        tooltips = "@name"
        tooltips = "<span class='tooltip-text'>@names</span>\n<br>"
        tooltips += "<span class='tooltip-text'>Reviews: @num_reviews</span>"
        hover = HoverTool(tooltips=tooltips, names=['hotels'])
        tap = TapTool(names=['hotels'])
        self.plot.add_tools(pan, wheel_zoom, box_select, hover, tap)
        overlay = BoxSelectionOverlay(tool=box_select)
        self.plot.add_layout(overlay)

    def make_bar_plot(self):
        # create a new plot
        y_rr = Range1d(start=0.0, end=5.0)
        TOOLS = "box_select,lasso_select"

        #x's and y's based on selected_df
        sdf = self.selected_df[['names', 'ratings']]

        xvals = [1.0*i + 0.5 for i in range(0, len(sdf['names']))]
        rightvals = [1.0*i + 0.85 for i in range(0, len(sdf['names']))]
        ratings = [r for r in sdf['ratings']]
        centers = [0.5*r for r in ratings]
        bottoms = [0]*len(ratings)
        y_text = [y + 1.0 for y in ratings]
        width = [1.0] * len(ratings)
        all_names = []
        for n in sdf['names']:
            short_name = n[:20]
            idx = short_name.rfind(" ")
            all_names.append(short_name[:idx])

        while len(all_names) > 0 and len(all_names) < 5:
            all_names.append("   ")

        bar_plot = figure(tools=TOOLS, width=400, height=350, x_range=all_names, y_range=y_rr, title="Average Rating")
        bar_plot.title_text_color = "black"
        bar_plot.title_text_font_size='15pt'
        bar_plot.title_text_font='Avenir'
        bar_plot.title_text_align = "right"
        print "all_names ", all_names

        bar_colors = []
        for r in ratings:
            if r >= 4.0:
                bar_colors.append("#743184")
            elif r >= 3.0:
                bar_colors.append("#14a1af")
            else:
                bar_colors.append("#ef4e4d")

        bar_plot.xaxis.major_label_orientation = pi/2.3
        bar_plot.rect(x=all_names, y=centers, width=width, height=ratings, color=bar_colors, line_color="black")

        #glyph = Text(x=xvals, y=y_text, text=sdf['names'], angle=pi/4,
                    #text_align="left", text_baseline="middle")

        #glyphs = [Text(x=x, y=y, text=[n], angle=pi/4, text_align="left", text_baseline="middle") 
                  #for x, y, n in zip(xvals, y_text, all_names)]
                   

        bar_plot.xaxis.major_tick_line_color = None
        bar_plot.xaxis.minor_tick_line_color = None

        bar_plot.yaxis.minor_tick_line_color = None


        bar_plot.xgrid.grid_line_color = None
        bar_plot.ygrid.grid_line_color = None
        self.bar_plot = bar_plot


    def set_children(self):
        self.children = [self.totalbox]
        self.totalbox.children = [self.mainrow]
        self.mainrow.children = [self.statsbox, self.mapbox]
        self.mapbox.children = [self.plot]
        #self.checkbox.children = [self.filler, self.check_group, self.filler2]
        self.statsbox.children = [self.bar_plot, self.legendbox]
        self.legendbox.children = [self.legend_filler, self.legend_plot]
        #self.bottomrow.children = [self.pretext]


    def setup_events(self):
        super(HotelApp, self).setup_events()
        if self.source:
            self.source.on_change('selected', self, 'selection_change')
        if self.selectr:
            self.selectr.on_change('value', self, 'input_change')
        if self.check_group:
            self.check_group.on_change('active', self, 'check_change')

    @property
    def df(self):
        thedf = return_hotel_data()
        if self.selectr.value is None or self.selectr.value == 'Choose A State':
            return thedf
        else:
            newdf = thedf[thedf['state'] == self.selectr.value]
            return newdf

    @property
    def countydf(self):
        thedf = county_data
        if self.selectr.value is None or self.selectr.value == 'Choose A State':
            return thedf
        else:
            newdf = thedf[thedf['state'] == self.selectr.value]
            return newdf


    def selection_change(self, obj, attrname, old, new):
        #self.make_source()
        self.update_source()
        self.make_bar_plot()
        self.set_children()
        curdoc().add(self)

    def check_change(self, obj, attrname, old, new):
        #Turn on/off the counties/hotel data
        print "what the heck ", obj, attrname, old, new
        if 0 in new:
            self._show_counties = True
        else:
            self._show_counties = False

        if 1 in new:
            self._show_hotels = True
        else:
            self._show_hotels = False

        self.populate_glyphs()
        self.set_children()
        curdoc().add(self)

    def input_change(self, obj, attrname, old, new):
        #import pdb;pdb.set_trace()
        print "source len: ", len(self.source.data['state'])
        print "county len: ", len(self.county_source.data['names'])
        self.update_source()
        self.update_county_source()
        print "source len: ", len(self.source.data['state'])
        print "county len: ", len(self.county_source.data['names'])
        if self.selectr.value is None or self.selectr.value == 'Choose A State':
            pass
        else:
            self.plot.title = self.selectr.value
Exemplo n.º 10
0
def age_hist_tab(df_selected=None):
    def make_dataset(age_group_list,
                     range_start=0.076070,
                     range_end=0.271264,
                     bin_width=0.00001):
        by_age_group = pd.DataFrame(columns=[
            'proportion', 'left', 'right', 'f_proportion', 'f_interval',
            'name', 'color'
        ])
        range_extent = range_end - range_start

        # Iterate through all the carriers
        for i, age_group in enumerate(age_group_list):
            # Subset to the carrier
            subset = df_selected[df_selected['age_range'] == age_group]
            # Create a histogram with 5 minute bins
            arr_hist, edges = np.histogram(subset['meanfun'],
                                           bins=int(range_extent / bin_width),
                                           range=[range_start, range_end])

            # Divide the counts by the total to get a proportion
            arr_df = pd.DataFrame({
                'proportion': arr_hist / np.sum(arr_hist),
                'left': edges[:-1],
                'right': edges[1:]
            })

            # Format the proportion
            arr_df['f_proportion'] = [
                '%0.5f' % proportion for proportion in arr_df['proportion']
            ]

            # Format the interval
            arr_df['f_interval'] = [
                '%0.5f to %0.5f KHz' % (left, right)
                for left, right in zip(arr_df['left'], arr_df['right'])
            ]
            # Assign the carrier for labels
            arr_df['name'] = age_group

            # Color each carrier differently
            arr_df['color'] = Category20_16[i]

            # Add to the overall dataframe
            by_age_group = by_age_group.append(arr_df)

        # Overall dataframe
        by_age_group = by_age_group.sort_values(['name', 'left'])

        return ColumnDataSource(by_age_group)

    def style(p):
        # Title
        p.title.align = 'center'
        p.title.text_font_size = '20pt'
        p.title.text_font = 'serif'

        # Axis titles
        p.xaxis.axis_label_text_font_size = '14pt'
        p.xaxis.axis_label_text_font_style = 'bold'
        p.yaxis.axis_label_text_font_size = '14pt'
        p.yaxis.axis_label_text_font_style = 'bold'

        # Tick labels
        p.xaxis.major_label_text_font_size = '12pt'
        p.yaxis.major_label_text_font_size = '12pt'

        return p

    def make_plot(src):
        # Blank plot with correct labels
        p = figure(
            plot_width=700,
            plot_height=700,
            title='Histogram of Age Range and Mean Fundamental Frequeny',
            x_axis_label='Mean fundamental frequency (KHz)',
            y_axis_label='Proportion')

        # Quad glyphs to create a histogram
        p.quad(source=src,
               bottom=0,
               top='proportion',
               left='left',
               right='right',
               color='color',
               fill_alpha=0.7,
               hover_fill_color='color',
               legend='name',
               hover_fill_alpha=1.0,
               line_color='black')

        # Hover tool with vline mode
        hover = HoverTool(tooltips=[('Age Group', '@name'),
                                    ('Meanfund frequency', '@f_interval'),
                                    ('Proportion', '@f_proportion')],
                          mode='vline')

        p.add_tools(hover)

        # Styling
        p = style(p)

        return p

    def update(attr, old, new):
        age_group_to_plot = [
            age_group_selection.labels[i] for i in age_group_selection.active
        ]

        new_src = make_dataset(age_group_to_plot,
                               range_start=range_select.value[0],
                               range_end=range_select.value[1],
                               bin_width=binwidth_select.value)

        src.data.update(new_src.data)

    # This is the option on the side
    available_age_groups = list(df_selected['age_range'].unique())
    age_group_selection = CheckboxGroup(labels=available_age_groups,
                                        active=[0, 1])
    # What happens when you select it
    age_group_selection.on_change('active', update)

    # The slider on the side
    binwidth_select = Slider(start=0.001,
                             end=0.01,
                             step=0.001,
                             value=0.001,
                             title='Frequency Width (KHz)')
    #Something is updated
    binwidth_select.on_change('value', update)

    #The slider and the ranges for that one
    range_select = RangeSlider(start=0.076070,
                               end=0.271264,
                               value=(0.076070, 0.271264),
                               step=0.01,
                               title='Meanfun Frequency (KHz)')
    # Same here
    range_select.on_change('value', update)

    initial_age_grop = [
        age_group_selection.labels[i] for i in age_group_selection.active
    ]

    src = make_dataset(initial_age_grop,
                       range_start=range_select.value[0],
                       range_end=range_select.value[1],
                       bin_width=binwidth_select.value)

    p = make_plot(src)

    # Put controls in a single element
    controls = WidgetBox(age_group_selection, binwidth_select, range_select)

    # Create a row layout
    layout = row(controls, p)

    # Make a tab with the layout
    tab = Panel(child=layout, title='Age Group Histogram')
    return tab
Exemplo n.º 11
0
def emergence_tab(company_data, founder_data, gis_data):
    company_data.loc[:, 'IncorporationDate'] = pd.to_datetime(
        company_data['IncorporationDate'], format='%d/%m/%Y')
    company_data.loc[:,
                     'year'] = company_data['IncorporationDate'].dt.to_period(
                         'Y').astype(str).astype(int)

    founder_data = founder_data.dropna()
    founder_data.loc[:, 'IncorporationDate'] = pd.to_datetime(
        founder_data['IncorporationDate'], format='%d/%m/%Y')
    founder_data.loc[:,
                     'year'] = founder_data['IncorporationDate'].dt.to_period(
                         'Y').astype(str).astype(int)
    founder_data.loc[:, 'age'] = founder_data['year'] - founder_data[
        'date_of_birth.year']
    founder_data.loc[:, 'age'] = founder_data['age'].astype(int)

    ##############################################
    # def the function to prepare data for plots #
    ##############################################
    def make_dataset(plot_indicators,
                     map_indicators,
                     range_start=1980,
                     range_end=2020,
                     year_select=2020):
        # calculate the accumulated number of companies
        companies = company_data.copy()
        companies.loc[:, 'company_count'] = 1
        companies = companies.loc[:, ['year', 'company_count']]
        companies = pd.DataFrame(
            companies.groupby(['year'])['company_count'].sum())
        companies.loc[:, 'company_sum'] = companies['company_count'].cumsum()
        companies = companies.reset_index()
        data = companies.loc[:, ['year', 'company_sum']]

        # calculate the accumulated number of founders
        founders = founder_data.copy()
        founders.loc[:, 'founder_count'] = 1
        founders = founders.loc[:, ['year', 'founder_count']]
        founders = pd.DataFrame(
            founders.groupby(['year'])['founder_count'].sum())
        founders.loc[:, 'founder_sum'] = founders['founder_count'].cumsum()
        founders = founders.reset_index()
        data = pd.merge(data, founders, on='year')
        data = data.drop(columns=['founder_count'], axis=1)
        data = data[data['year'] >= range_start]
        data = data[data['year'] <= range_end]

        company_nodes = gis_data.copy()
        companies_1 = company_data.copy()

        companies_1 = companies_1.loc[:, ['RegAddress.PostCode', 'year']]
        companies_1 = companies_1[companies_1['year'] <= year_select]
        companies_1.loc[:, 'count'] = 1
        companies_1 = pd.DataFrame(
            companies_1.groupby(['RegAddress.PostCode'])['count'].sum())
        companies_1.loc[:, 'count'] = companies_1['count'].apply(np.log)
        companies_1.loc[:, 'count'] = companies_1['count'] * 30
        companies_1 = companies_1.reset_index()
        companies_1.loc[:, 'RegAddress.PostCode'] = companies_1[
            'RegAddress.PostCode'].str.replace(' ', '')
        companies_1 = pd.merge(companies_1,
                               company_nodes,
                               left_on='RegAddress.PostCode',
                               right_on='postcode')
        companies_1 = companies_1.loc[:, ['count', 'x', 'y']]

        founder_nodes = gis_data.copy()
        founder_1 = founder_data.copy()

        founder_1 = founder_1[founder_1['year'] <= year_select]
        founder_1.loc[:, 'count'] = 1
        founder_1 = pd.DataFrame(
            founder_1.groupby(['RegAddress.PostCode'])['count'].sum())
        founder_1.loc[:, 'count'] = founder_1['count'].apply(np.log)
        founder_1.loc[:, 'count'] = founder_1['count'] * 30
        founder_1 = founder_1.reset_index()
        founder_1.loc[:, 'RegAddress.PostCode'] = founder_1[
            'RegAddress.PostCode'].str.replace(' ', '')
        founder_1 = pd.merge(founder_1,
                             founder_nodes,
                             left_on='RegAddress.PostCode',
                             right_on='postcode')
        founder_1 = founder_1.loc[:, ['count', 'x', 'y']]

        if 'Num of Companies' not in plot_indicators:
            data.loc[:, 'company_sum'] = 'N/A'
        if 'Num of Founders' not in plot_indicators:
            data.loc[:, 'founder_sum'] = 'N/A'

        if map_indicators != 'Num of Companies':
            companies_1 = pd.DataFrame(columns=['count', 'x', 'y'])
        if map_indicators != 'Num of Founders':
            founder_1 = pd.DataFrame(columns=['count', 'x', 'y'])

        return (ColumnDataSource(data), ColumnDataSource(companies_1),
                ColumnDataSource(founder_1))

    #############################
    # plot the map of tech-city #
    #############################
    def make_map(company_src, founder_src):
        # set the range of axises
        x_min = -12000
        x_max = -8500
        y_min = 6714000
        y_max = 6716000

        # define the map tiles to use
        tile_provider = get_provider(Vendors.CARTODBPOSITRON_RETINA)

        # plot the map
        p_map = figure(match_aspect=True,
                       x_range=(x_min, x_max),
                       y_range=(y_min, y_max),
                       x_axis_type='mercator',
                       y_axis_type='mercator',
                       height=550)

        p_map.circle(x='x',
                     y='y',
                     color='darkslategray',
                     source=company_src,
                     size=10,
                     fill_alpha=0.4,
                     radius='count')
        p_map.circle(x='x',
                     y='y',
                     color='brown',
                     source=founder_src,
                     size=10,
                     fill_alpha=0.4,
                     radius='count')

        p_map.grid.visible = True

        # add map tiles
        map = p_map.add_tile(tile_provider)
        map.level = 'underlay'

        # formatting
        p_map.xaxis.visible = False
        p_map.yaxis.visible = False

        return p_map

    ####################################
    # define the function to plot data #
    ####################################
    def make_plot(src):
        p = figure(plot_width=1000,
                   plot_height=550,
                   x_axis_label='YEAR',
                   y_axis_label='NUM')
        p.line(source=src,
               x='year',
               y='company_sum',
               color='darkslategray',
               legend_label='Num of Companies')
        p.line(source=src,
               x='year',
               y='founder_sum',
               color='brown',
               legend_label='Num of Founders')
        p.legend.location = 'top_left'

        hover = HoverTool(tooltips=[(
            'Year',
            '@year'), ('Num of Companies',
                       '@company_sum'), ('Num of Founders', '@founder_sum')])
        p.add_tools(hover)

        return p

    ##############################
    # define the update function #
    ##############################
    def update(attr, old, new):
        plot_indicators = [
            plot_indicator_selection.labels[i]
            for i in plot_indicator_selection.active
        ]

        map_indicators = map_indicator_selection.value

        new_src, new_company_src, new_founder_src = make_dataset(
            plot_indicators,
            map_indicators,
            range_start=range_select.value[0],
            range_end=range_select.value[1],
            year_select=year_select.value)

        src.data.update(new_src.data)
        company_src.data.update(new_company_src.data)
        founder_src.data.update(new_founder_src.data)

    # %% set controllers
    available_indicators = ['Num of Companies', 'Num of Founders']

    plot_indicator_selection = CheckboxGroup(labels=available_indicators,
                                             active=[0, 1])
    plot_indicator_selection.on_change('active', update)

    map_indicator_selection = Select(title="Choose Indicator",
                                     value='Num of Companies',
                                     options=available_indicators)
    map_indicator_selection.on_change('value', update)

    year_select = Slider(start=1980,
                         end=2020,
                         value=2020,
                         step=1,
                         title='Choose the year')
    year_select.on_change('value', update)

    range_select = RangeSlider(start=1980,
                               end=2020,
                               value=(1980, 2020),
                               title='Time Period (year)')
    range_select.on_change('value', update)

    # %% initial indicators and data source
    initial_plot_indicators = [
        plot_indicator_selection.labels[i]
        for i in plot_indicator_selection.active
    ]

    initial_map_indicator = map_indicator_selection.value

    src, company_src, founder_src = make_dataset(
        initial_plot_indicators,
        initial_map_indicator,
        range_start=range_select.value[0],
        range_end=range_select.value[1],
        year_select=year_select.value)

    p1 = make_plot(src)
    p2 = make_map(company_src, founder_src)

    # %% put controls in a single element
    plot_controls = WidgetBox(plot_indicator_selection, range_select)
    map_controls = WidgetBox(map_indicator_selection, year_select)

    # text description:
    title_1 = Div(text='''
        <b>Emergence and Development of London Tech City</b>
        ''')

    description_1 = Div(text='''
        <b>In 2008</b>, Tech City - also known as East London Tech City or
        Silicon Roundabout - came to prominence, with a variety of
        companies operating in the area, including Last.FM, Poke and
        Trampoline System. Being located in Inner East London, Tech
        City presents a large number of advantages for firms: a central
        location, cheap rents, accessibility to the rest of London and
        proximity to other like-minded tech companies. Moreover, Tech City
        offers many nightlife activaties, which is highly attractive to
        the type of workers and employees that digital and tech business
        want to retain - young, urban, creative and tech-savvy.
        ''')

    description_2 = Div(text='''
        <b>In Nov 2010</b>, then UK Prime Minister made a speech to emphasize
        the importance of East London Tech firms in the UK Economy and set
        ambitious plans to further develop Tech City, promising a governmental
        support. The proposals involved building the profile of the area
        through government funding (£15 million in Government Funding) and
        support. Besides, Tech City Investment Organisation (TCIO) was created
        by the government aiming to make Tech City the leading digital hub in
        Europe.
        ''')

    description_3 = Div(
        text='<b>The Tech City strategy presented 3 clear objectives:</b>\n')

    description_4 = Div(
        text='1. Supporting the cluster of start-ups and small/midium-sized\
        businesses(SMES);')

    description_5 = Div(text='2. Attracting large international investers;')

    description_6 = Div(
        text='3. Using this momentum to steer high-tech activity further east,\
        including into a post-Games Olympic Park.')

    description_7 = Div(text='''
        <b>In May 2011</b>, TweetDeck, a social media dashboard, was
        launched by Twitter and gave another boost to the already growing
        repulation of Tech City.
        ''')

    reference_1 = Div(text='''
        <a href="https://www.mbymontcalm.co.uk/blog/history-behind-tech-city/
        ">Reference 1</a>
        ''')

    reference_2 = Div(text='''
        <a href="https://www.demos.co.uk/files/A_Tale_of_Tech_City_web.pdf
        ">Reference 2</a>
        ''')

    reference_3 = Div(text='''
        <a href="https://www.gov.uk/government/speeches/east-end-tech-city-speech">Reference 3</a>
        ''')

    reference_4 = Div(text='''
        <a href="https://olaonikoyi.medium.com/the-problems-at-east-london
        -tech-city-london-silicon-roundabout-d0bc2d4c7181">Reference 4</a>
        ''')

    title_2 = Div(text='''
        <b>Data Visual</b>
        ''')

    description_8 = Div(text='''
        The programme was a major success and resulted in an important
        multiplication of the number of tech companies operating in the
        region. In 2000, the number of companies in Tech City was 628.
        After Cameron's speech in 2011, the number of companies started
        increasing exponentially - indeed, the number of companies increased
        by 1204% between 2011 and 2020 (3,208 in 2011 vs. 41,841 in 2020).
        ''')

    # %% design the layout
    plot_layout = column(plot_controls, p1)
    map_layout = column(map_controls, p2)
    graph_layout = row(plot_layout, map_layout)
    layout = column(title_1, description_1, description_2, description_3,
                    description_4, description_5, description_6, description_7,
                    reference_1, reference_2, reference_3, reference_4,
                    title_2, description_8, graph_layout)

    # %% make a tab with the layout
    tab = Panel(child=layout, title='Emergence of Tech-City')

    return tab
Exemplo n.º 12
0
def priceOverTimeTab(priceDf):

    def findProductPriceLists(df, product, country):
        this_product = df.query("country==\"" + country + "\"")
        this_product = this_product.query("_product==\"" + product + "\"")
        this_product = this_product.sort_values(by=['year', 'month'])
        month_means = []
        year_months = []
        for year in this_product.year.unique():
            this_year = this_product.query("year==" + str(year))
            for month in this_year.month.unique():
                this_month = this_year.query("month==" + str(month))
                month_means.append(this_month.price.mean())
                year_months.append(dt.datetime(year=year, month=int(month), day=1))
        return [month_means, year_months]


    # Function to make a dataset for histogram based on a list of carriers
    # a minimum delay, maximum delay, and histogram bin width
    def make_dataset(products_list, country, df):

        # Dataframe to hold information
        by_product = pd.DataFrame(columns=['proportion',
                                           'f_proportion',
                                           'product', 'date',  'color'])

        # Iterate through all the carriers
        for i, product in enumerate(products_list):

            # Subset to the carrier
            lists = findProductPriceLists(df, product, country)
            prices = lists[0]
            dates = lists[1]

            # Divide the counts by the total to get a proportion
            arr_df = pd.DataFrame({'proportion': prices / np.mean(prices)})
            arr_df['date'] = dates
            # Format the proportion
            arr_df['f_proportion'] = ['%0.5f' % proportion for proportion in arr_df['proportion']]

            # Assign the carrier for labels
            arr_df['product'] = product

            # Color each carrier differently
            arr_df['color'] = Category20_16[i]

            # Add to the overall dataframe
            by_product = by_product.append(arr_df)

        # Overall dataframe
        by_product = by_product.sort_values(['product', 'date'])

        return ColumnDataSource(by_product)

    def style(p):
    	# Title
    	p.title.align = 'center'
    	p.title.text_font_size = '20pt'
    	p.title.text_font = 'serif'

    	# Axis titles
    	p.xaxis.axis_label_text_font_size = '14pt'
    	p.xaxis.axis_label_text_font_style = 'bold'
    	p.yaxis.axis_label_text_font_size = '14pt'
    	p.yaxis.axis_label_text_font_style = 'bold'

    	# Tick labels
    	p.xaxis.major_label_text_font_size = '12pt'
    	p.yaxis.major_label_text_font_size = '12pt'

    	return p

    def make_plot(src):
    	# Blank plot with correct labels
    	p = figure(plot_width = 700, plot_height = 700,
    			  title = 'Price of products in a country over time',
    			  x_axis_label = 'Time', y_axis_label = 'Price proportional to the mean',
                  x_axis_type="datetime")

    	# Quad glyphs to create a histogram
    	p.line(source = src, x = 'date', y = 'proportion',
    		   color = 'color', legend = 'product', line_color = 'black')

    	# Hover tool with vline mode
    	hover = HoverTool(tooltips=[('Product', '@product'),
    								('Proportion', '@f_proportion')],
    					  mode='vline')

    	p.add_tools(hover)

    	# Styling
    	p = style(p)

    	return p



    def update(attr, old, new):
    	products_to_plot = [products_selection.labels[i] for i in products_selection.active]

    	new_src = make_dataset(products_to_plot, 'India', df)



    	src.data.update(new_src.data)


    # load dataframe
    priceDf = pd.read_csv("data/only_complete_years_data.csv")
    # available products and colors
    priceCountryDf = priceDf[priceDf['country'] == 'India']
    available_products = list(priceCountryDf._product.unique())
    available_products.sort()


    product_colors = Category20_16
    product_colors.sort()

    products_selection = CheckboxGroup(labels=available_products,
    								  active = [0, 1])
    products_selection.on_change('active', update)

    # Initial carriers and data source
    initial_products = [products_selection.labels[i] for i in products_selection.active]

    src = make_dataset(initial_products, 'India', priceDf)

    p = make_plot(src)

    # Put controls in a single element
    controls = WidgetBox(products_selection)

    # Create a row layout
    layout = row(controls, p)

    # Make a tab with the layout
    tab = Panel(child=layout, title = 'Product price over time')

    return tab
Exemplo n.º 13
0
def histogram_tab(flights):

	# Function to make a dataset for histogram based on a list of carriers
	# a minimum delay, maximum delay, and histogram bin width
	def make_dataset(carrier_list, range_start = -60, range_end = 120, bin_width = 5):

		# Dataframe to hold information
		by_carrier = pd.DataFrame(columns=['proportion', 'left', 'right', 
										   'f_proportion', 'f_interval',
										   'name', 'color'])
		
		range_extent = range_end - range_start

		# Iterate through all the carriers
		for i, carrier_name in enumerate(carrier_list):

			# Subset to the carrier
			subset = flights[flights['name'] == carrier_name]

			# Create a histogram with 5 minute bins
			arr_hist, edges = np.histogram(subset['arr_delay'], 
										   bins = int(range_extent / bin_width), 
										   range = [range_start, range_end])

			# Divide the counts by the total to get a proportion
			arr_df = pd.DataFrame({'proportion': arr_hist / np.sum(arr_hist), 'left': edges[:-1], 'right': edges[1:] })

			# Format the proportion 
			arr_df['f_proportion'] = ['%0.5f' % proportion for proportion in arr_df['proportion']]

			# Format the interval
			arr_df['f_interval'] = ['%d to %d minutes' % (left, right) for left, right in zip(arr_df['left'], arr_df['right'])]

			# Assign the carrier for labels
			arr_df['name'] = carrier_name

			# Color each carrier differently
			arr_df['color'] = Category20_16[i]

			# Add to the overall dataframe
			by_carrier = by_carrier.append(arr_df)

		# Overall dataframe
		by_carrier = by_carrier.sort_values(['name', 'left'])

		return ColumnDataSource(by_carrier)

	def style(p):
		# Title 
		p.title.align = 'center'
		p.title.text_font_size = '20pt'
		p.title.text_font = 'serif'

		# Axis titles
		p.xaxis.axis_label_text_font_size = '14pt'
		p.xaxis.axis_label_text_font_style = 'bold'
		p.yaxis.axis_label_text_font_size = '14pt'
		p.yaxis.axis_label_text_font_style = 'bold'

		# Tick labels
		p.xaxis.major_label_text_font_size = '12pt'
		p.yaxis.major_label_text_font_size = '12pt'

		return p
	
	def make_plot(src):
		# Blank plot with correct labels
		p = figure(plot_width = 700, plot_height = 700, 
				  title = 'Histogram of Arrival Delays by Airline',
				  x_axis_label = 'Delay (min)', y_axis_label = 'Proportion')

		# Quad glyphs to create a histogram
		p.quad(source = src, bottom = 0, top = 'proportion', left = 'left', right = 'right',
			   color = 'color', fill_alpha = 0.7, hover_fill_color = 'color', legend = 'name',
			   hover_fill_alpha = 1.0, line_color = 'black')

		# Hover tool with vline mode
		hover = HoverTool(tooltips=[('Carrier', '@name'), 
									('Delay', '@f_interval'),
									('Proportion', '@f_proportion')],
						  mode='vline')

		p.add_tools(hover)

		# Styling
		p = style(p)

		return p
	
	
	
	def update(attr, old, new):
		carriers_to_plot = [carrier_selection.labels[i] for i in carrier_selection.active]
		
		new_src = make_dataset(carriers_to_plot,
							   range_start = range_select.value[0],
							   range_end = range_select.value[1],
							   bin_width = binwidth_select.value)
		
		

		src.data.update(new_src.data)
		
	# Carriers and colors
	available_carriers = list(set(flights['name']))
	available_carriers.sort()


	airline_colors = Category20_16
	airline_colors.sort()
		
	carrier_selection = CheckboxGroup(labels=available_carriers, 
									  active = [0, 1])
	carrier_selection.on_change('active', update)
	
	binwidth_select = Slider(start = 1, end = 30, 
							 step = 1, value = 5,
							 title = 'Bin Width (min)')
	binwidth_select.on_change('value', update)
	
	range_select = RangeSlider(start = -60, end = 180, value = (-60, 120),
							   step = 5, title = 'Range of Delays (min)')
	range_select.on_change('value', update)
	
	# Initial carriers and data source
	initial_carriers = [carrier_selection.labels[i] for i in carrier_selection.active]
	
	src = make_dataset(initial_carriers,
					   range_start = range_select.value[0],
					   range_end = range_select.value[1],
					   bin_width = binwidth_select.value)
	p = make_plot(src)
	
	# Put controls in a single element
	controls = WidgetBox(carrier_selection, binwidth_select, range_select)
	
	# Create a row layout
	layout = row(controls, p)
	
	# Make a tab with the layout 
	tab = Panel(child=layout, title = 'Histogram')

	return tab
Exemplo n.º 14
0
    p.yaxis.axis_label = 'Value'
    p.ygrid.band_fill_color = "olive"
    p.ygrid.band_fill_alpha = 0.1

    return p


def update(attr, old, new):
    #chosen cat
    cat_to_plot = [cat_selection.labels[i] for i in cat_selection.active]
    new_bar = make_dataset(cat_to_plot)
    src_bar.data.update(new_bar.data)


cat_selection = CheckboxGroup(labels=bar_cat, active=[1, 2, 3])
cat_selection.on_change('active', update)

init_cat = [cat_selection.labels[i] for i in cat_selection.active]

src_bar = make_dataset(init_cat)
p = make_plot(src_bar)

df2_cross_french = df2.merge(
    french[['hosp', 'rea',
            'dc']].groupby('jour').sum().reset_index().set_index('jour'),
    left_index=True,
    right_index=True,
    how='left').fillna(0).astype(int)


def make_plot_compare(cat):
Exemplo n.º 15
0

def clear():
    chkbx.active = [0]
    chkbx2.active = []


def sel_all():
    chkbx.active = [i for i in range(brk)]
    chkbx2.active = [i for i in range(len(qual_cols) - brk)]


# Make interactive stuff.
brk = 27
chkbx = CheckboxGroup(labels=qual_cols[0:brk], active=[0])
chkbx.on_change('active', update)

chkbx2 = CheckboxGroup(labels=qual_cols[brk:], active=[])
chkbx2.on_change('active', update)

clr_button = Button(label='Clear Selections', button_type='success')
clr_button.on_click(clear)

all_button = Button(label='Select All', button_type='success')
all_button.on_click(sel_all)

# Layout document.
# controls = widgetbox([chkbx], [chkbx2])
r1 = row(chkbx, chkbx2, make_bar())
r2 = row(clr_button, all_button)
Exemplo n.º 16
0
                          how='outer',
                          on=df.columns.values.tolist())
        df_new = df_new.sort_values(by=df_new.columns.values[6],
                                    ascending=False)
        df_new = df_new.drop_duplicates(df.columns.values[1], keep='first')
        df_new = df_new.sort_values(by=df_new.columns.values[0])
    df_new.drop(df_new.columns.values[0], axis=1, inplace=True)
    source.data = {
        df_new.columns.values[column]: df_new[df_new.columns.values[column]]
        for column in range(df_new.shape[1])
    }


labels = [' '.join(directions[i]) for i in range(len(directions))]
checkbox_group = CheckboxGroup(labels=labels)
checkbox_group.on_change('active', update)

df_initial = pd.read_excel(join(dirname(__file__), 'docs', files[0]),
                           skiprows=5,
                           usecols=[0, 1, 3, 4, 5, 6, 9],
                           nrows=0)
source = ColumnDataSource(data=dict())
columns = [
    TableColumn(field=df_initial.columns.values[column],
                title=df_initial.columns.values[column])
    for column in range(1, df_initial.shape[1])
]
data_table = DataTable(source=source, columns=columns, width=1000, height=2000)
'''
df_new = df_initial
for i in range(2):
Exemplo n.º 17
0
def slider_delay(attr, old, new):
    global dend_threshold
    dend_threshold = source_for_slider.data["value"][0]
    update_dend()


source_for_slider.on_change("data", slider_delay)

# calling call back function upon changes in interactive widgets
issues_list_input.on_change('value',
                            lambda attr, old, new: update_issue_list())
submit_button.on_click(update_issue_list)
#dend_threshold_slider.on_change('value', lambda attr, old, new: update_dend())
leaves_source.on_change('selected', update_clipboard_text)
universe_checkbox.on_change('active',
                            lambda attr, old, new: update_issue_list())
# =============================================================================
# Set up layouts and add to document
# =============================================================================
# making the descrption for the page
page_header = Div(text="""<h1>Data Quality Issue Clustering</h1>
                      Start creating your issues dendrogram by entering your issues
                      IDs. Fine tune your clusters by changing the <b>Dendrogram
                      Threshold</b>. (Click on the link for more information about
                      <a href="https://en.wikipedia.org/wiki/Dendrogram" target="_blank">dendrograms</a>).
                      The following DQ Direct fields were used for
                      this clustering: <b>Source System, Issue Detection Point,
                      Issue Summary, Detailed Description</b> and <b>Attribute Document
                      Header</b>. Check the tick box for <b>Universe issues
                      dendrogram</b> if you want to compare your issues list with
                      all other non-rejected and non-remediated issues.
Exemplo n.º 18
0
def map_tab(map_data, states):

	# Function to make a dataset for the map based on a list of carriers
	def make_dataset(carrier_list):
		
		# Subset to the carriers in the specified list
		subset = map_data[map_data['carrier']['Unnamed: 3_level_1'].isin(
														   carrier_list)]

		
		# Dictionary mapping carriers to colors
		color_dict = {carrier: color for carrier, color in zip(
			available_carriers, airline_colors)}
		
		# Lists of data for plotting
		flight_x = []
		flight_y = []
		colors = []
		carriers = []
		counts = []
		mean_delays = []
		min_delays = []
		max_delays = []
		dest_loc = []
		origin_x_loc = []
		origin_y_loc = []
		dest_x_loc = []
		dest_y_loc = []
		origins = []
		dests = []
		distances = []

		# Iterate through each carrier
		for carrier in carrier_list:

			# Subset to the carrier
			sub_carrier = subset[subset['carrier']['Unnamed: 3_level_1'] == carrier]

			# Iterate through each route (origin to destination) for the carrier
			for _, row in sub_carrier.iterrows():

				colors.append(color_dict[carrier])
				carriers.append(carrier)
				origins.append(row['origin']['Unnamed: 1_level_1'])
				dests.append(row['dest']['Unnamed: 2_level_1'])

				# Origin x (longitude) and y (latitude) location
				origin_x_loc.append(row['start_long']['Unnamed: 20_level_1'])
				origin_y_loc.append(row['start_lati']['Unnamed: 21_level_1'])

				# Destination x (longitude) and y latitude (location)
				dest_x_loc.append(row['end_long']['Unnamed: 22_level_1'])
				dest_y_loc.append(row['end_lati']['Unnamed: 23_level_1'])

				# Flight x (longitude) locations
				flight_x.append([row['start_long']['Unnamed: 20_level_1'], 
								 row['end_long']['Unnamed: 22_level_1']])

				# Flight y (latitude) locations
				flight_y.append([row['start_lati']['Unnamed: 21_level_1'], 
								 row['end_lati']['Unnamed: 23_level_1']])


				# Stats about the particular route
				counts.append(row['arr_delay']['count'])
				mean_delays.append(row['arr_delay']['mean'])
				min_delays.append(row['arr_delay']['min'])
				max_delays.append(row['arr_delay']['max'])
				distances.append(row['distance']['mean'])


		# Create a column data source from the lists of lists
		new_src = ColumnDataSource(data = {'carrier': carriers, 'flight_x': flight_x, 'flight_y': flight_y, 
											   'origin_x_loc': origin_x_loc, 'origin_y_loc': origin_y_loc,
											   'dest_x_loc': dest_x_loc, 'dest_y_loc': dest_y_loc,
											   'color': colors, 'count': counts, 'mean_delay': mean_delays,
											   'origin': origins, 'dest': dests, 'distance': distances,
											   'min_delay': min_delays, 'max_delay': max_delays})

		return new_src

	def make_plot(src, xs, ys):
		
		# Create the plot with no axes or grid
		p = figure(plot_width = 1100, plot_height = 700, title = 'Map of 2013 Flight Delays Departing NYC')
		p.xaxis.visible = False
		p.yaxis.visible = False
		p.grid.visible = False

		# States are drawn as patches
		patches_glyph = p.patches(xs, ys, fill_alpha=0.2, fill_color = 'lightgray', 
								  line_color="#884444", line_width=2, line_alpha=0.8)

		# Airline flights are drawn as lines
		lines_glyph = p.multi_line('flight_x', 'flight_y', color = 'color', line_width = 2, 
								   line_alpha = 0.8, hover_line_alpha = 1.0, hover_line_color = 'color',
								   legend = 'carrier', source = src)

		# Origins are drawn as squares (all in NYC)
		squares_glyph = p.square('origin_x_loc', 'origin_y_loc', color = 'color', size = 10, source = src, 
								 legend = 'carrier')

		# Destinations are drawn as circles
		circles_glyph = p.circle('dest_x_loc', 'dest_y_loc', color = 'color', size = 10, source = src, 
								 legend = 'carrier')

		# Add the glyphs to the plot using the renderers attribute
		p.renderers.append(patches_glyph)
		p.renderers.append(lines_glyph)
		p.renderers.append(squares_glyph)
		p.renderers.append(circles_glyph)

		# Hover tooltip for flight lines, assign only the line renderer
		hover_line = HoverTool(tooltips=[('Airline', '@carrier'),
									('Number of Flights', '@count'),
									('Average Delay', '@mean_delay{0.0}'),
									('Max Delay', '@max_delay{0.0}'),
									('Min Delay', '@min_delay{0.0}')],
							  line_policy = 'next',
							  renderers = [lines_glyph])
		
		# Hover tooltip for origin and destination, assign only the line renderer
		hover_circle = HoverTool(tooltips=[('Origin', '@origin'),
										   ('Dest', '@dest'),
										   ('Distance (miles)', '@distance')],
								renderers = [circles_glyph])

		# Position the location so it does not overlap plot
		p.legend.location = (10, 50)

		# Add the hovertools to the figure
		p.add_tools(hover_line)
		p.add_tools(hover_circle)

		p = style(p) 
		
		return p
	
	# Styling 
	def style(p):
			
		# Title 
		p.title.align = 'center'
		p.title.text_font_size = '20pt'
		p.title.text_font = 'serif'

		# Axis titles
		p.xaxis.axis_label_text_font_size = '14pt'
		p.xaxis.axis_label_text_font_style = 'bold'
		p.yaxis.axis_label_text_font_size = '14pt'
		p.yaxis.axis_label_text_font_style = 'bold'

		# Tick labels
		p.xaxis.major_label_text_font_size = '12pt'
		p.yaxis.major_label_text_font_size = '12pt'

		return p
		
	# Show selected carriers on map
	def update(attr, old, new):
		# Find list of carriers and make a new data set
		carrier_list = [carrier_selection.labels[i] for i in carrier_selection.active]
		new_src = make_dataset(carrier_list)

		src.data.update(new_src.data)
			
			
	available_carriers = list(set(map_data['carrier']['Unnamed: 3_level_1']))
	available_carriers.sort()

	airline_colors = Category20_16
	airline_colors.sort()

	# Remove Alaska and Hawaii from states
	if 'HI' in states: del states['HI']
	if 'AK' in states: del states['AK']

	# Put longitudes and latitudes in lists
	xs = [states[state]['lons'] for state in states]
	ys = [states[state]['lats'] for state in states]

	# CheckboxGroup to select carriers for plotting    
	carrier_selection = CheckboxGroup(labels=available_carriers, active = [0, 1])
	carrier_selection.on_change('active', update)

	# Initial carriers to plot
	initial_carriers = [carrier_selection.labels[i] for i in carrier_selection.active]

	# Initial source and plot
	src = make_dataset(initial_carriers)

	p = make_plot(src, xs, ys)

	# Layout setup
	layout = row(carrier_selection, p)
	tab = Panel(child = layout, title = 'Flight Map')

	return tab
# Update the plot based on selections
def update(attr, old, new):
    carriers_to_plot = [carrier_selection.labels[i] for i in carrier_selection.active]

    new_src = make_dataset(carriers_to_plot,
                           range_start = range_select.value[0],
                           range_end = range_select.value[1],
                           bin_width = binwidth_select.value)

    src.data.update(new_src.data)


# CheckboxGroup to select carrier to display
carrier_selection = CheckboxGroup(labels=available_carriers, active = [0, 1])
carrier_selection.on_change('active', update)

# Slider to select width of bin
binwidth_select = Slider(start = 1, end = 30, 
                     step = 1, value = 5,
                     title = 'Delay Width (min)')
binwidth_select.on_change('value', update)

# RangeSlider control to select start and end of plotted delays
range_select = RangeSlider(start = -60, end = 180, value = (-60, 120),
                           step = 5, title = 'Delay Range (min)')
range_select.on_change('value', update)


# Find the initially selected carrieres
initial_carriers = [carrier_selection.labels[i] for i in carrier_selection.active]
Exemplo n.º 20
0
plot_figure = figure(title='Checkbox',plot_height=450, plot_width=600,
              tools="save,reset",toolbar_location="below")

plot_figure.scatter('x', 'y', source=source, size=10)

checkbox = CheckboxGroup(labels=['Show x-axis label','Show y-axis label'])

def checkbox_click(attr,old,new):
    active_checkbox=checkbox.active ##Getting checkbox value in list

    ## Get first checkbox value and show x-axis label

    if len(active_checkbox)!=0 and (0 in active_checkbox):
        plot_figure.xaxis.axis_label='X-Axis'
    else:
        plot_figure.xaxis.axis_label = None

    ## Get second checkbox value and show y-axis label

    if len(active_checkbox)!=0 and (1 in active_checkbox):
        plot_figure.yaxis.axis_label='Y-Axis'
    else:
        plot_figure.yaxis.axis_label = None

checkbox.on_change('active',checkbox_click)

layout=row(checkbox, plot_figure)

curdoc().add_root(layout)
curdoc().title = "Checkbox Bokeh Server"
Exemplo n.º 21
0
    #     seebeck_arrays.append(df['Seebeck (V/K)'])
    #     color_array.append(colors[f])
    #     label_array.append(f"{f}")

    return ColumnDataSource(dict(temperature=temperature_arrays, seebeck=seebeck_arrays, color=color_array,
                                   label=label_array))


def update(attr, old, new):
    selected_samples = [sample_selection.labels[i] for i in sample_selection.active]
    new_source = get_data(selected_samples)
    source.data.update(new_source.data)
    print("Updated!")


sample_selection = CheckboxGroup(labels=available_file_names, active = [0])
sample_selection.on_change('active', update)

selected_samples = [sample_selection.labels[i] for i in sample_selection.active]
source = get_data(selected_samples)

p.multi_line('temperature', 'seebeck', line_width=2, source=source, color='color', legend='label')
p.xaxis.axis_label = 'Temperature (degC)'
p.yaxis.axis_label = 'Seebeck coefficient (V/K)'
p.legend.location = 'bottom_right'

columns = ['label', '']

curdoc().add_root(row(sample_selection, p))

Exemplo n.º 22
0

def choose_calculate(attr, old, new):
    VectorSum_glyph.visible = 0 in calculate_selection.active
    VectorSub_glyph.visible = 1 in calculate_selection.active
    VectorSum_label_glyph.visible = 0 in calculate_selection.active
    VectorSub_label_glyph.visible = 1 in calculate_selection.active
    line1_glyph.visible = 0 in calculate_selection.active
    line2_glyph.visible = 1 in calculate_selection.active
    VectorMul_glyph.visible = 2 in calculate_selection.active
    VectorDiv_glyph.visible = 3 in calculate_selection.active
    V5_label_glyph.visible = 2 in calculate_selection.active
    V6_label_glyph.visible = 3 in calculate_selection.active


calculate_selection.on_change('active', choose_calculate)


def changeVector1(attr, old, new):
    glob_Vector1.data = dict(val=[new])  #      /output
    updateVector1()
    updateSum()
    updateSub()
    updateLine()
    updatevalues()
    updateMul()
    updateDiv()


#Changing Vector2
def changeVector2(attr, old, new):
Exemplo n.º 23
0
                                                        random_state=0)
    clf = get_classifier(alg)
    clf.fit(X_train, y_train)
    y_hat_test = clf.predict(X_test)

    # Update the result plot
    xvals = list(range(1, len(X_test) + 1))
    s.data = dict(x=xvals, real_y=y_test, predict_y=y_hat_test)
    p.xaxis.ticker = xvals
    p.title.text = 'Predict {0} using {1}'.format(pred, alg)
    label.text = 'Accuracy: {0:.3f}'.format(clf.score(X_test, y_test))


pred_ticker.on_change('value', setting_change)
alg_ticker.on_change('value', setting_change)
feature_boxes.on_change('active', setting_change)

# Set up layout
fpath = join(dirname(__file__), 'description.html')
desc = Div(text=open(fpath).read(), width=700, height=140)

spacer = Spacer(width=sidebar_width, height=15)
text = Div(text="<h2>Features</h2>", height=10, width=sidebar_width)
feature_widget = column(text, feature_boxes)
sidebar = column(pred_ticker, alg_ticker, spacer, feature_widget)

layout = column(desc, row(p, sidebar))

# Initalize
update()
Exemplo n.º 24
0
def histo_tab(dataframe):
    """
    function called by main.py to show this tab.
    """
    def calc_totals_averages(name_list):
        """
        Simple calculations on columns of the dataframe
        """
        totals = {}
        means = {}
        for name in name_list:
            totals[name] = dataframe[name].sum()
            means[name] = int(dataframe[name].mean())
        return totals, means

    def make_dataset(name_list, x_min=30000, x_max=130000, n_bins=40):
        """
        return a column datasource
        """
        totals, means = calc_totals_averages(name_list)
        hist_df = pd.DataFrame(columns=[
            "name", "left", "right", "f_interval", "steps", "colour", "total",
            "average"
        ])
        for i, name in enumerate(name_list):

            step_hist, edges = np.histogram(dataframe[name],
                                            bins=n_bins,
                                            range=[x_min, x_max])
            tmp_df = pd.DataFrame({
                "steps": step_hist,
                "left": edges[:-1],
                "right": edges[1:]
            })
            tmp_df['f_interval'] = ['%d to %d steps' % (left, right) \
                                    for left, right in zip(tmp_df['left'],
                                                           tmp_df['right'])]
            tmp_df["name"] = name
            tmp_df["colour"] = Category20_16[i]
            tmp_df["total"] = totals[name]
            tmp_df["average"] = means[name]

            # add this persons data to the overall hist_df
            hist_df = hist_df.append(tmp_df)

        # convert to a Bokeh ColumnDataSource and return it
        source = ColumnDataSource(hist_df)
        return source

    def style(p):
        # Title
        p.title.align = 'center'
        p.title.text_font_size = '20pt'
        p.title.text_font = 'serif'

        # Axis titles
        p.xaxis.axis_label_text_font_size = '14pt'
        p.xaxis.axis_label_text_font_style = 'bold'
        p.yaxis.axis_label_text_font_size = '14pt'
        p.yaxis.axis_label_text_font_style = 'bold'

        # Tick labels
        p.xaxis.major_label_text_font_size = '12pt'
        p.yaxis.major_label_text_font_size = '12pt'

        return p

    def make_plot(source):
        """
        return the bokeh figure
        """
        print("In make_plot")
        p = figure(plot_height=600,
                   plot_width=600,
                   x_axis_label='steps',
                   y_axis_label="Count")
        p.quad(source=source,
               bottom=0,
               top='steps',
               left='left',
               right='right',
               fill_color='colour',
               alpha=0.5,
               line_color='black',
               legend='name')

        hover = HoverTool(tooltips=[("Name", "@name"), (
            "Total",
            "@total"), ("Average",
                        "@average"), ('Step range',
                                      '@f_interval'), ('Num of Weeks',
                                                       '@steps')])
        p.add_tools(hover)
        p = style(p)
        return p

    def update(attr, old, new):
        """
        Update the plot every time a change is made
        """
        hists_to_plot = [selection.labels[i] for i in selection.active]
        new_src = make_dataset(hists_to_plot,
                               x_min=range_select.value[0],
                               x_max=range_select.value[1],
                               n_bins=nbin_select.value)
        cds.data.update(new_src.data)

    ### back to the histo_tab function definition...
    names = list(dataframe.columns)
    names.sort()
    # widgets to allow user to configure the plot
    selection = CheckboxGroup(labels=names, active=list(range(len(names))))
    selection.on_change('active', update)
    nbin_select = Slider(start=1, end=100, step=1, value=40, title="Num bins")
    nbin_select.on_change('value', update)

    range_select = RangeSlider(start=20000,
                               end=200000,
                               value=(30000, 130000),
                               step=1000,
                               title="Range")
    range_select.on_change('value', update)

    # initial column data source and plot
    initial_names = [selection.labels[i] for i in selection.active]
    cds = make_dataset(initial_names)
    p = make_plot(cds)

    controls = WidgetBox(selection, nbin_select, range_select)
    # create row layout
    layout = row(controls, p)
    #turn this into a tab
    tab = Panel(child=layout, title="Histograms")
    return tab
Exemplo n.º 25
0
                                       active=[10, 11])

#########################
x, counts = create_x_and_counts(df=df, cities=cities)

source = ColumnDataSource(
    data=dict(x=x, counts=counts,
              colors=()))  # ,colors=(sum(zip(Spectral6, Spectral6), ())

p = figure(x_range=FactorRange(*source.data['x']),
           plot_width=1400,
           plot_height=850,
           title="Determinants Comparison",
           toolbar_location=None,
           tools="")

p.xaxis.major_label_orientation = "vertical"

city_selection.on_change('active', update_plot)
determinants_selection.on_change('active', update_plot)

checkbox_cities_column = column(city_selection)
checkbox_determinants_column = column(determinants_selection)

bokeh_doc = curdoc()
bokeh_doc.add_root(
    row(checkbox_cities_column, p, checkbox_determinants_column, width=1500))

# bokeh serve --show plot_bar_chart.py
# python -m notebook
def bkapp(dfile,
          pcol,
          app_name,
          server_static_root,
          title='Sketch-map',
          pointsize=10,
          jmol_settings=""):
    global cv, controls, selectsrc, columns, button, slider, n, xcol, ycol, ccol, rcol, plt_name, indx, ps, jmolsettings, appname, lay, server_prefix, periodic_checkbox, pss, frac, alphas, grid, marker
    appname = app_name
    server_prefix = server_static_root
    ps = pointsize
    jmolsettings = jmol_settings
    #initialise data
    datafile = join(appname, 'data', dfile)
    cv = smap(name=title)
    cv.read(datafile)
    n = len(cv.data)
    columns = [i for i in cv.columns]

    # set up selection options
    tcol = pcol[0] - 1
    xcol = Select(title='X-Axis',
                  value=columns[tcol],
                  options=columns,
                  width=50)
    xcol.on_change('value', update)
    tcol = pcol[1] - 1
    ycol = Select(title='Y-Axis',
                  value=columns[tcol],
                  options=columns,
                  width=50)
    ycol.on_change('value', update)
    roptions = ['None']
    for option in columns:
        roptions.append(option)
    rcol = Select(title='Size', value='None', options=roptions, width=50)
    rcol.on_change('value', update)
    if (len(pcol) > 2):
        tcol = pcol[2] - 1
        ccol = Select(title='Color',
                      value=columns[tcol],
                      options=roptions,
                      width=50)
    else:
        ccol = Select(title='Color', value='None', options=roptions, width=50)
    ccol.on_change('value', update)

    marker_options = [
        'circle', 'diamond', 'triangle', 'square', 'asterisk', 'cross',
        'inverted_triangle', 'variable'
    ]
    marker = Select(title='Marker',
                    value='circle',
                    options=marker_options,
                    width=50)
    marker.on_change('value', update)

    periodic_checkbox = CheckboxGroup(labels=["Periodic Palette"], active=[])
    periodic_checkbox.on_change('active', update)

    grid = CheckboxGroup(labels=["Show Axis"], active=[0])
    grid.on_change('active', update)

    plt_name = Select(title='Palette',
                      width=50,
                      value='Inferno256',
                      options=[
                          "Magma256", "Plasma256", "Spectral6", "Inferno256",
                          "Viridis256", "Greys256", "cosmo"
                      ])
    plt_name.on_change('value', update)

    pss = Slider(start=0,
                 end=50,
                 value=ps,
                 step=1,
                 callback_policy='mouseup',
                 title="Point Size",
                 width=150)
    pss.on_change('value', update)

    frac = Slider(start=0,
                  end=1,
                  value=min(1.0, round(3000. / n, 1)),
                  step=0.1,
                  callback_policy='mouseup',
                  title="Fraction Of Data Loaded",
                  width=200)
    frac.on_change('value', update)

    alphas = Slider(start=0,
                    end=1,
                    value=0.75,
                    step=0.1,
                    callback_policy='mouseup',
                    title="Point Alpha",
                    width=150)
    alphas.on_change('value', update)

    xm = widgetbox(xcol, width=170, sizing_mode='fixed')
    ym = widgetbox(ycol, width=170, sizing_mode='fixed')
    cm = widgetbox(ccol, width=170, sizing_mode='fixed')
    mm = widgetbox(marker, width=170, sizing_mode='fixed')
    cp = widgetbox(periodic_checkbox, width=100, sizing_mode='fixed')
    gc = widgetbox(grid, width=100, sizing_mode='fixed')
    rm = widgetbox(rcol, width=170, sizing_mode='fixed')
    pm = widgetbox(plt_name, width=170, sizing_mode='fixed')
    psw = widgetbox(pss, width=210, height=50, sizing_mode='fixed')
    asl = widgetbox(alphas, width=210, height=50, sizing_mode='fixed')
    fw = widgetbox(frac, width=270, height=50, sizing_mode='fixed')
    controls = Column(
        Row(xm, ym, cm, rm, pm, mm, width=1050, sizing_mode='scale_width'),
        Row(gc, fw, psw, asl, cp, width=1050, sizing_mode='fixed'))

    # create plot and slider

    plotpanel = create_plot()
    # full layout
    lay = layout([
        [controls],
        [plotpanel],
    ], sizing_mode='fixed')
    return lay
Exemplo n.º 27
0
    states_to_plot = [states_select1.labels[i] for i in states_select1.active] + \
                        [states_select2.labels[i] for i in states_select2.active] + \
                        [states_select3.labels[i] for i in states_select3.active]

    new_src = make_dataset(states_to_plot,
                           start=range_select.value_as_datetime[0],
                           end=range_select.value_as_datetime[1],
                           thresh=thresh_select.value)

    src.data.update(new_src.data)


# Create widgets
states_select1 = CheckboxGroup(labels=STATES[0:18], active=[0, 1])
states_select1.on_change('active', update)
states_select2 = CheckboxGroup(labels=STATES[18:36], active=[])
states_select2.on_change('active', update)
states_select3 = CheckboxGroup(labels=STATES[36:54], active=[])
states_select3.on_change('active', update)
#     states_selection = CheckboxGroup(labels=STATES, active = [0,1])
#     states_selection.on_change('active', update)

thresh_select = Slider(start=0,
                       end=1000,
                       step=1,
                       value=0,
                       title='Case Count Minimum')
thresh_select.on_change('value', update)

range_select = DateRangeSlider(start=dt.date(2020, 1, 21),
def signals(raw_data):
    
    # create dataset
    def make_dataset(signal_list):
        
        # get signal names and activities
        if not not signal_list:
            signal_list.insert(0, 'activity')

        # list for storing selected signals data
        x = []
        y = []
        colors = []
        labels = []

        # iterate through signals
        for i, signal in enumerate(signal_list):
            
            # get subset
            subset = raw_data[signal]
            
            # substract -9 from original values of activity
            # don't overlap it with signals values
            if signal=='activity':
                subset=subset-9

            
            # add the values/color/label
            y.append(list(subset))
            x.append(list(np.arange(len(subset))))
            colors.append(signal_colors[i])
            labels.append(signal)

            
        # create new data source
        new_data_source = ColumnDataSource(data={'x': x, 'y': y, 
                                   'color': colors, 'label': labels})

        return new_data_source
    
    
    # create plot 
    def make_plot(data_source):
        
        # init plot
        plt = figure(plot_width = 900, plot_height = 400,
                   title = 'Time series of different signals for a subject',
                   x_axis_label = 'f/t domain', y_axis_label = '')

        # add m-lines
        plt.multi_line('x', 'y',  color = 'color', legend = 'label', 
                     line_width = 3, source = data_source)


        # create hoover tools
        hover = HoverTool(tooltips=[('signal', '@label'), ('f/t domain', '$x'), ('values', '$y')], line_policy = 'next')
        plt.add_tools(hover)

        # do styling
        plt = style(plt)

        return plt
    
    
    # style plot
    def style(plt):
        
        # title 
        plt.title.align = 'center'
        plt.title.text_font_size = '20pt'

        # axis titles
        plt.xaxis.axis_label_text_font_size = '14pt'
        plt.xaxis.axis_label_text_font_style = 'bold'
        plt.yaxis.axis_label_text_font_size = '14pt'
        plt.yaxis.axis_label_text_font_style = 'bold'

        return plt
    
    
    # update plot if widgebox changes
    def update(attr, old, new):
        
        
        # get signals names from selector
        new_signals = [signal_selection.labels[i] for i in signal_selection.active]

        # get data for updated list
        new_data_source = make_dataset(new_signals)

        # update data and plot new data soruce
        data_source.data.update(new_data_source.data)
        

    # ---- MAIN ----
        
    # get unique signal names
    available_signals = list(raw_data.columns[1:])
    available_signals.sort()
    
    signal_colors = Category20_16
    signal_colors.sort()

    # create checkbox for signal names
    signal_selection = CheckboxGroup(labels=available_signals, active = [0])
    signal_selection.on_change('active', update)

    # initial selected data source
    initial_signals = [signal_selection.labels[i] for 
                        i in signal_selection.active]    
    
    # create data source
    data_source = make_dataset(initial_signals) 

    # plot aand style
    plot = make_plot(data_source)
    plot = style(plot)

    # place control in widgetbox
    controls = WidgetBox(signal_selection)
    
    # create row layout for controller and plot
    layout = row(controls, plot)
 
    # add layout to the tab
    tab = Panel(child=layout, title = 'Raw Data - Time Series Plot')

    return tab
Exemplo n.º 29
0
class Page():
    def __init__(self):
        self.WIDTH_MATRIX = 1
        self.HEIGHT_MATRIX = 1
        self.tabs = RadioButtonGroup(labels=['Page1', 'Page2'], active=0)
        self.tabs.on_change('active',
                            lambda attr, old, new: self.change_page())

        self.data_directory = 'data'
        self.data_files = os.listdir(self.data_directory)

        self.select_data_files1 = Select(title="Data files 1:",
                                         value="df1",
                                         options=["df1"] + self.data_files)
        self.select_data_files2 = Select(title="Data files 2:",
                                         value="df2",
                                         options=["df2"] + self.data_files)

        self.select_data_files1.on_change(
            'value', lambda attr, old, new: self.init_dataframes())
        self.select_data_files2.on_change(
            'value', lambda attr, old, new: self.init_dataframes())
        self.refresh_button = Button(label="Refresh",
                                     button_type="success",
                                     width=100)
        self.refresh_button.on_click(self.update_directory)

        self.layout = column(
            self.tabs, row(self.select_data_files1, self.select_data_files2),
            self.refresh_button)
        curdoc().add_root(self.layout)

    # show(self.layout)

    def change_page(self):
        if (self.select_data_files1.value == "df1"
                or self.select_data_files2.value == "df2"):
            self.tabs.active = 0
            return
        if (self.tabs.active == 0):
            self.layout.children = [
                self.tabs,
                row(self.select_data_files1, self.select_data_files2),
                self.refresh_button, self.slider_depth, self.plot_matrix
            ]
        elif (self.tabs.active == 1):
            self.layout.children = [
                self.tabs,
                # self.sel_well,self.pred_button,
                self.plots_rock,
                row(column(self.select_df1_corr, self.checkbox_df1),
                    column(self.select_df2_corr, self.checkbox_df2)),
                self.plot_correlation
            ]

    def init_dataframes(self):
        if (self.select_data_files1.value == "df1"
                or self.select_data_files2.value == "df2"):
            return
        if (not self.select_data_files1.value.endswith('.csv')
                or not self.select_data_files2.value.endswith('.csv')):
            print("incorrect data, expected format csv")
            return

        self.Y_COL = 'Depth'
        self.main_df1, self.main_df2 = self.read_dataframe()
        # self.change
        self.df1 = self.main_df1.copy()
        self.df2 = self.main_df2.copy()
        self.slider_depth = RangeSlider(
            start=self.min_total_depth(self.main_df1, self.main_df2),
            # изменить реализацию мин и мах
            end=self.max_total_depth(self.main_df1, self.main_df2),
            step=1,
            value=(self.min_total_depth(self.main_df1, self.main_df2),
                   self.max_total_depth(self.main_df1, self.main_df2)))
        self.slider_depth.on_change('value', self.change_depth)

        self.update_page()

    def update_page(self):
        print("1")
        self.columns_df1 = [
            col for col in self.df1.columns if self.is_number(col, self.df1)
        ]
        self.columns_df2 = [
            col for col in self.df2.columns if self.is_number(col, self.df2)
        ]
        # self.select_plots = [-1 for i in range(len(self.columns_df1) + len(self.columns_df1))]
        self.select_plots = []
        print("2")
        #	wells = self.df1['Well Name'].unique().tolist()
        #	wells = [well for well in wells if well not in ['Recruit F9']]

        # self.sel_well = Select(title="Well name:", value=wells[0], options=wells)
        # self.pred_button = Button(label="Predict", button_type="success", width=100)
        # self.pred_button.on_click(self.update_predict)

        # self.plots_rock = self.init_plots_rock()

        self.plots_rock = column()
        self.source_correlation_plot = None
        self.plot_correlation = self.init_corr()

        self.select_df1_corr = Select(title="Data frame 1:",
                                      value=self.columns_df1[0],
                                      options=self.columns_df1)
        self.select_df2_corr = Select(title="Data frame 2:",
                                      value=self.columns_df2[1],
                                      options=self.columns_df2)

        self.select_df1_corr.on_change(
            'value', lambda attr, old, new: self.update_set_data())
        self.select_df2_corr.on_change(
            'value', lambda attr, old, new: self.update_set_data())

        self.checkbox_df1 = CheckboxGroup(labels=["log10"], active=[])
        self.checkbox_df2 = CheckboxGroup(labels=["log10"], active=[])
        self.checkbox_df1.on_change(
            'active', lambda attr, old, new: self.update_set_data())
        self.checkbox_df2.on_change(
            'active', lambda attr, old, new: self.update_set_data())
        print("3")
        data_matrix = self.init_data_matrix(self.df1, self.df2)
        self.plot_matrix = self.draw_matrix(data_matrix)
        self.plot_matrix.on_event(Tap, self.update_plots)
        print("4")
        self.layout.children = [
            self.tabs,
            row(self.select_data_files1, self.select_data_files2),
            self.refresh_button, self.slider_depth, self.plot_matrix
        ]
        print("5")

    def change_depth(self, attr, old, new):
        self.df1 = self.df1[
            (self.df1[self.Y_COL] >= self.slider_depth.value[0])
            & (self.df1[self.Y_COL] <= self.slider_depth.value[1])]
        self.df2 = self.df2[
            (self.df2[self.Y_COL] >= self.slider_depth.value[0])
            & (self.df2[self.Y_COL] <= self.slider_depth.value[1])]
        self.update_page()

    def min_total_depth(self, df1, df2):
        return max(df1[self.Y_COL].min(), df2[self.Y_COL].min())

    def max_total_depth(self, df1, df2):
        return min(df1[self.Y_COL].max(), df2[self.Y_COL].max())

    def change_case_depth(self, df1, df2):
        ret_code = self.rename_depth(df1)
        # Обработать код возврата
        ret_code = self.rename_depth(df2)
        # if df1[self.Y_COL][1] - df1[self.Y_COL][0] > df2[self.Y_COL][1] - df2[self.Y_COL][0]:
        if (df1[self.Y_COL].size < df2[self.Y_COL].size):
            df2.set_index([self.Y_COL], inplace=True)
            df2 = df2.reindex(df1[self.Y_COL], method='nearest')
            column_values = pd.Series(df2.index, index=df2.index)
            df2.insert(loc=0, column=self.Y_COL, value=column_values)
            df2.set_index(df1.index, inplace=True)
        else:
            df1.set_index([self.Y_COL], inplace=True)
            df1 = df1.reindex(df2[self.Y_COL], method='nearest')
            column_values = pd.Series(df1.index, index=df1.index)
            df1.insert(loc=0, column=self.Y_COL, value=column_values)
            df1.set_index(df2.index, inplace=True)
        return df1, df2

    def rename_depth(self, df):
        is_renamed_df = False
        dept = "Dept"
        unnamed = "Unnamed: 0"
        exist = 0
        not_exist = 1
        for col in df.columns:
            if (col.lower() == self.Y_COL.lower()
                    or col.lower() == dept.lower()):
                df.rename(columns={col: self.Y_COL}, inplace=True)
                is_renamed_df = True
        if (not is_renamed_df):
            if (df.index.name == None and df.columns[0] == unnamed):
                df.rename(columns={unnamed: self.Y_COL}, inplace=True)
                return not_exist
            elif (df.index.name != None
                  and df.index.name.lower().find(dept.lower()) != -1):
                column_values = pd.Series(df.index, index=df.index)
                df.insert(loc=0, column=self.Y_COL, value=column_values)
                return not_exist
            elif ((df.columns)[0] != unnamed
                  and df.columns[0].lower().find(dept.lower()) != -1):
                df.rename(columns={unnamed: self.Y_COL}, inplace=True)
                return not_exist
            else:
                column_values = pd.Series(df.index, index=df.index)
                df.insert(loc=0, column=self.Y_COL, value=column_values)
                return not_exist
        return exist

    '''	if(df1)
        df_gis = pd.read_csv('gis.csv') 
        df_gis = df_gis.astype(np.float32) 

        df_git = pd.read_csv('git.csv') 
        df_git = df_git.rename(columns={'0':'DEPT'}) 

        df_gis = df_gis.set_index(['DEPT']) 
        df_gis.reindex(df_git['DEPT'], method='nearest')'''

    def is_number(self, col, df):
        if type(df[col][df[col].index[0]]).__name__ == "int64" or type(
                df[col][df[col].index[0]]).__name__ == "float64":
            return True
        return False

    def update_directory(self):
        self.data_directory = 'data'
        self.data_files = os.listdir(self.data_directory)
        self.select_data_files1 = Select(title="Data files 1:",
                                         value="df1",
                                         options=["df1"] + self.data_files)
        self.select_data_files2 = Select(title="Data files 2:",
                                         value="df2",
                                         options=["df2"] + self.data_files)
        self.layout.children[1] = row(self.select_data_files1,
                                      self.select_data_files2)

    def init_corr(self):
        plot_correlation = figure(plot_width=800,
                                  plot_height=500,
                                  title='correlation graph')
        data = pd.DataFrame(data={
            'x': self.df1[self.columns_df1[0]],
            'y': self.df2[self.columns_df2[1]]
        })
        self.source_correlation_plot = ColumnDataSource(data)
        plot_correlation.scatter(x='x',
                                 y='y',
                                 source=self.source_correlation_plot,
                                 line_color='red',
                                 size=2)

        return plot_correlation

    def toFixed(self, numObj, digits=0):
        return f"{numObj:.{digits}f}"

    def get_y_range(self, df):
        ymin = df[self.Y_COL].min()
        ymax = df[self.Y_COL].max()
        res = Range1d(ymin, ymax)
        return res

    def update_plots(self, event):
        length_columns_df1 = len(self.columns_df1)
        length_columns_df2 = len(self.columns_df2)
        x = int((event.x) / self.WIDTH_MATRIX)
        y = int((event.y) / self.HEIGHT_MATRIX)
        if (self.select_plots.count(
            ((1, self.columns_df1[x]),
             (2, self.columns_df2[length_columns_df2 - y - 1]))) == 1):
            self.select_plots.remove(
                ((1, self.columns_df1[x]),
                 (2, self.columns_df2[length_columns_df2 - y - 1])))
            self.delete_select_cell(
                name=self.columns_df1[x] +
                self.columns_df2[length_columns_df2 - y - 1])

        elif (event.x < 0 or event.x > length_columns_df1 or event.y < 0
              or event.y > length_columns_df2):
            return
        else:
            self.draw_select_cell(name=self.columns_df1[x] +
                                  self.columns_df2[length_columns_df2 - y - 1],
                                  x=x + 0.5,
                                  y=y + 0.5,
                                  width=1,
                                  height=1)
            self.select_plots.append(
                ((1, self.columns_df1[x]),
                 (2, self.columns_df2[length_columns_df2 - y - 1])))

        self.plots_rock.children = [
            self.draw_plot_rock(self.df1, self.df2, self.select_plots)
        ]

    def draw_select_cell(self, name, x, y, height, width):
        self.plot_matrix.rect(x=x,
                              y=y,
                              width=width,
                              height=height,
                              fill_alpha=0,
                              line_color="blue",
                              name=name)

    def delete_select_cell(self, name):
        self.plot_matrix.select(name=name).visible = False

    def update_set_data(self):
        r = self.df1[self.select_df1_corr.value]
        col = self.df2[self.select_df2_corr.value]
        if (self.checkbox_df2.active == [0]):
            col = col.apply(numpy.log10)
        if (self.checkbox_df1.active == [0]):
            r = r.apply(numpy.log10)
        data = {'x': r, 'y': col}
        self.source_correlation_plot.data = data

    def update_predict(self):
        return

    def draw_plot_rock(self, df1, df2, select_plots):
        list_plots = []
        source_df1 = ColumnDataSource(data=df1)
        source_df2 = ColumnDataSource(data=df2)
        yrange = self.get_y_range(df1)
        for pair_data in self.select_plots:
            list_plots.append(
                self.init_plot_rock(pair_data[0][1], pair_data[0][1],
                                    self.Y_COL, source_df1, yrange, '#0000ff'))

            list_plots.append(
                self.init_plot_rock(pair_data[1][1], pair_data[1][1],
                                    self.Y_COL, source_df2, yrange, '#008000'))
        return row(list_plots)

    def init_plot_rock(self, title, x, y, source, yrange, color_title):
        plot = figure(plot_width=200,
                      plot_height=400,
                      title=title,
                      toolbar_location=None,
                      tools='ywheel_zoom',
                      active_scroll='ywheel_zoom')
        plot.line(x, y, source=source, line_width=2)
        plot.y_range = yrange
        plot.title.text_color = color_title

        return plot

    def init_data_matrix(self, df1, df2):
        matrix = {col: [] for col in self.columns_df1}
        for r in self.columns_df1:
            for col in self.columns_df2:
                matrix[r].append(
                    float(
                        self.toFixed(
                            numpy.corrcoef(df1[r], df2[col])[0, 1], 3)))
        data = pd.DataFrame(data=matrix)
        data.index = list(self.columns_df2)
        data.index.name = 'rows'
        data.columns.name = 'columns'

        return data

    def draw_matrix(self, data):
        df = pd.DataFrame(data.stack(), columns=['rate']).reset_index()

        source = ColumnDataSource(df)
        colors = [
            "#75968f", "#a5bab7", "#c9d9d3", "#e2e2e2", "#dfccce", "#ddb7b1",
            "#cc7878", "#933b41", "#550b1d"
        ]
        mapper = LinearColorMapper(palette=colors,
                                   low=df.rate.min(),
                                   high=df.rate.max())

        plot_matrix = figure(plot_width=800,
                             plot_height=300,
                             title="",
                             x_range=list(self.columns_df2),
                             y_range=list(reversed(self.columns_df1)),
                             toolbar_location=None,
                             tools="",
                             x_axis_location="above")
        plot_matrix.rect(x="rows",
                         y="columns",
                         width=self.WIDTH_MATRIX,
                         height=self.HEIGHT_MATRIX,
                         source=source,
                         line_color=None,
                         fill_color=transform('rate', mapper))
        plot_matrix.xaxis.major_label_text_color = '#0000ff'  # blue
        plot_matrix.yaxis.major_label_text_color = '#008000'  # green
        self.r = plot_matrix.text(x=dodge("rows",
                                          -0.1,
                                          range=plot_matrix.x_range),
                                  y=dodge("columns",
                                          -0.2,
                                          range=plot_matrix.y_range),
                                  text="rate",
                                  **{"source": df})
        self.r.glyph.text_font_size = "9pt"

        plot_matrix.axis.axis_line_color = None
        plot_matrix.axis.major_tick_line_color = None
        return plot_matrix

    def read_dataframe(self):
        df1 = pd.read_csv('data/' + self.select_data_files1.value)
        df2 = pd.read_csv('data/' + self.select_data_files2.value)
        df1, df2 = self.change_case_depth(df1, df2)
        min = self.min_total_depth(df1, df2)
        max = self.max_total_depth(df1, df2)
        print("@@@")
        df1 = df1[(df1[self.Y_COL] >= min) & (df1[self.Y_COL] <= max)]
        df2 = df2[(df2[self.Y_COL] >= min) & (df2[self.Y_COL] <= max)]
        print("######")
        return (df1, df2)
Exemplo n.º 30
0
Arquivo: viz.py Projeto: arzwa/wgd
def histogram_bokeh(ks_distributions, labels):
    """
    Run an interactive bokeh application.
    This requires a running bokeh server! Use ``bokeh serve &`` to start a bokeh
    server in the background.

    :param ks_distributions: a list of Ks distributions (pandas data frames)
    :param labels: a list of labels for the corresponding distributions
    :return: bokeh app
    """
    from bokeh.io import curdoc
    from bokeh.layouts import widgetbox, layout
    from bokeh.models.widgets import Select, TextInput, Slider, Div
    from bokeh.models.widgets import CheckboxGroup, Toggle
    from bokeh.plotting import figure, output_file, show
    from bokeh.client import push_session
    from pylab import cm
    from .utils import gaussian_kde
    from .modeling import reflect

    # helper functions
    def get_colors(cmap_choice='binary'):
        too_light = [
            'binary', 'hot', 'copper', 'pink', 'summer', 'bone', 'Pastel1',
            'Pastel2', 'gist_ncar', 'nipy_spectral', 'Greens'
        ]
        cmap = cm.get_cmap(cmap_choice, len(ks_distributions))
        if cmap_choice in too_light:
            cmap = cm.get_cmap(cmap_choice, len(ks_distributions) * 4)
        c = []
        for i in range(cmap.N):
            rgb = cmap(i)[:3]  # will return rgba, but we need only rgb
            c.append(matplotlib.colors.rgb2hex(rgb))
        if cmap_choice in too_light:
            if len(ks_distributions) > 1:
                c = c[2:-2]
            else:
                c = [c[-1]]
        return c

    def get_data(df, var, scale, r1, r2, outliers_included):
        df = filter_group_data(df,
                               min_ks=r1,
                               max_ks=r2,
                               weights_outliers_included=outliers_included)
        data = df[var].dropna()
        if scale == 'log10':
            data = np.log10(data)
        return data, df

    # get the distributions
    dists = [pd.read_csv(x, sep='\t') for x in ks_distributions]
    if labels:
        labels = labels.split(',')
    else:
        labels = ks_distributions

    # basics
    c = get_colors()
    variables = ['Ks', 'Ka', 'Omega']
    scales = ['Normal', 'log10']

    # set up widgets
    div = Div(text=BOKEH_APP_DIV, width=800)
    var = Select(title='Variable', value='Ks', options=variables)
    scale = Select(title='Scale', value='Normal', options=scales)
    r1 = TextInput(title="Minimum", value='0.1')
    r2 = TextInput(title="Maximum", value='5')
    bins = TextInput(title="Bins", value='50')
    bandwidth = TextInput(title="Bandwidth", value='0.1')
    line = Slider(title="Lines", start=0, end=1, value=0.3, step=0.1)
    density = CheckboxGroup(labels=["Histogram", "KDE"], active=[0])
    density_alpha = Slider(title="Density alpha value",
                           start=0,
                           end=1,
                           value=0.6,
                           step=0.1)
    hist_alpha = Slider(title="Histogram alpha value",
                        start=0,
                        end=1,
                        value=0.6,
                        step=0.1)
    color_choice = Select(options=[
        'binary', 'hsv', 'hot', 'magma', 'viridis', 'Greens', 'spring',
        'autumn', 'copper', 'cool', 'winter', 'pink', 'summer', 'bone', 'RdBu',
        'RdYlGn', 'coolwarm', 'inferno', 'Pastel1', 'Pastel2', 'tab10',
        'gnuplot', 'brg', 'gist_ncar', 'jet', 'rainbow', 'nipy_spectral',
        'ocean', 'cubehelix'
    ],
                          value='binary',
                          title='Color map')
    no_reweight = Toggle(label="Don't adapt weights when filtering",
                         active=False)

    # set up figure
    p1 = figure(
        plot_width=1000,
        plot_height=700,  # output_backend="svg",
        tools='pan,wheel_zoom,xwheel_zoom,ywheel_zoom,save')
    p1.xgrid.grid_line_color = None
    p1.ygrid.grid_line_color = None
    p1.border_fill_color = 'white'
    p1.outline_line_color = None
    p1.yaxis.axis_label = 'Duplications'
    p1.xaxis.axis_label = 'Ks'

    # draw initial plot
    hist_dict = {}
    density_dict = {}
    all_data = []

    # set up callbacks
    def update(selected=None):
        redraw_plots()

    def redraw_plots():
        print(density.active)
        c = get_colors(color_choice.value)
        p1.legend.items = []

        all_data = []
        for i in range(len(dists)):
            df = dists[i]
            data, df = get_data(df, var.value, scale.value, float(r1.value),
                                float(r2.value), no_reweight.active)
            all_data.append(data)

        edges = np.histogram(np.hstack(tuple(all_data)),
                             bins=int(bins.value))[1]

        for i in range(len(dists)):
            if density.active == [0]:
                hist = np.histogram(all_data[i], bins=int(bins.value))[0]
                p1.yaxis.axis_label = 'Duplications'
            else:
                hist = np.histogram(all_data[i],
                                    bins=int(bins.value),
                                    density=True)[0]
                p1.yaxis.axis_label = 'density'

            # First histograms
            if i in hist_dict:
                remove_plot(hist_dict, i)

            if 0 in density.active:
                hist_dict[i] = p1.quad(top=hist,
                                       bottom=0,
                                       left=edges[:-1],
                                       right=edges[1:],
                                       fill_color=c[i],
                                       line_color="black",
                                       fill_alpha=hist_alpha.value,
                                       line_alpha=line.value,
                                       legend=labels[i])

            # Then KDEs
            if i in density_dict:
                density_dict[i].data_source.data['x'] = []
                density_dict[i].data_source.data['y'] = []

            if 1 in density.active:
                X = reflect(all_data[i])
                kde = gaussian_kde(X, bw_method=float(bandwidth.value))
                x = np.linspace(
                    float(r1.value) + 0.000001, float(r2.value), 1000)
                if scale.value == 'log10':
                    x = np.log10(x)
                pdf = np.array(kde(x)) * 2

                # add boundaries such that it is a nice curve!
                pdf = np.hstack([0, pdf, 0])
                if scale.value == 'log10':
                    x = np.hstack([
                        np.log10(float(r1.value) + 0.00000099), x,
                        np.log10(float(r2.value) + 0.000001)
                    ])
                else:
                    x = np.hstack(
                        [float(r1.value), x,
                         float(r2.value) + 0.000001])

                density_dict[i] = p1.patch(x=x,
                                           y=pdf,
                                           fill_color=c[i],
                                           line_width=2,
                                           line_color="black",
                                           line_alpha=line.value,
                                           alpha=density_alpha.value,
                                           legend=labels[i])

            p1.legend.label_text_font_style = "italic"
            p1.legend.click_policy = "hide"
            p1.legend.inactive_fill_alpha = 0.6
            v = var.value
            if v == "Omega":
                v = "Ka/Ks"
            if scale.value == 'log10':
                v = 'log10(' + v + ')'
            p1.xaxis.axis_label = v

    def remove_plot(hist_dict, i):
        hist_dict[i].data_source.data["top"] = []
        hist_dict[i].data_source.data["left"] = []
        hist_dict[i].data_source.data["right"] = []

    def change_update(attrname, old, new):
        update()

    def feat_change(attrname, old, new):
        c = get_colors(color_choice.value)
        for i, d in density_dict.items():
            d.glyph.fill_color = c[i]
            d.glyph.line_color = "black"
            d.glyph.fill_alpha = density_alpha.value
            d.glyph.line_alpha = line.value
        for i, d in hist_dict.items():
            d.glyph.fill_color = c[i]
            d.glyph.line_color = "black"
            d.glyph.fill_alpha = hist_alpha.value
            d.glyph.line_alpha = line.value

    def bins_update(attrname, old, new):
        update()

    var.on_change('value', bins_update)
    bandwidth.on_change('value', bins_update)
    scale.on_change('value', bins_update)
    r1.on_change('value', bins_update)
    r2.on_change('value', bins_update)
    line.on_change('value', feat_change)
    bins.on_change('value', bins_update)
    color_choice.on_change('value', feat_change)
    density.on_change('active', bins_update)
    density_alpha.on_change('value', feat_change)
    hist_alpha.on_change('value', feat_change)
    no_reweight.on_change("active", bins_update)

    # set up layout
    widgets1 = widgetbox(var,
                         scale,
                         color_choice,
                         density,
                         line,
                         hist_alpha,
                         density_alpha,
                         r1,
                         r2,
                         bins,
                         bandwidth,
                         no_reweight,
                         sizing_mode='fixed')
    l = layout([
        [div],
        [widgets1, p1],
    ], sizing_mode='fixed')

    # initialize
    update()

    session = push_session(curdoc())
    curdoc().add_root(l)
    session.show(l)  # open the document in a browser
    session.loop_until_closed()  # run forever
    return  # show(p1)
Exemplo n.º 31
0
class HotelApp(VBox):
    extra_generated_classes = [["HotelApp", "HotelApp", "VBox"]]
    jsmodel = "VBox"

    # input
    selectr = Instance(Select)
    #check_group = Instance(RadioGroup)
    check_group = Instance(CheckboxGroup)

    # plots
    plot = Instance(GMapPlot)
    filler = Instance(Plot)
    filler2 = Instance(Plot)
    bar_plot = Instance(Plot)
    legend_plot = Instance(Plot)
    legend_filler = Instance(Plot)

    # data source
    source = Instance(ColumnDataSource)
    county_source = Instance(ColumnDataSource)

    # layout boxes
    checkbox = Instance(VBox)
    mainrow = Instance(HBox)
    statsbox = Instance(VBox)
    mapbox = Instance(VBox)
    legendbox = Instance(HBox)
    totalbox = Instance(VBox)

    # inputs
    #ticker1_select = Instance(Select)
    #ticker2_select = Instance(Select)
    #input_box = Instance(VBoxForm)

    def make_inputs(self):
        with open("states.csv") as f:
            states = [line.strip().split(',') for line in f.readlines()]

        self.selectr = Select(
            name='states',
            value='Choose A State',
            options=[s[1] for s in states] + ['Choose A State'],
        )
        labels = ["County Averages", "Hotels"]
        self.check_group = CheckboxGroup(labels=labels,
                                         active=[0, 1],
                                         inline=True)
        ##Filler plot
        x_range = Range1d(0, 300)
        y_range = Range1d(0, 12)
        self.filler = Plot(x_range=x_range,
                           y_range=y_range,
                           title="",
                           plot_width=300,
                           plot_height=12,
                           min_border=0,
                           **PLOT_FORMATS)
        x_range = Range1d(0, 300)
        y_range = Range1d(0, 18)
        self.filler2 = Plot(x_range=x_range,
                            y_range=y_range,
                            title="",
                            plot_width=300,
                            plot_height=14,
                            min_border=0,
                            **PLOT_FORMATS)

    def make_outputs(self):
        pass
        #self.pretext = Paragraph(text="", width=800)

    def __init__(self, *args, **kwargs):
        super(HotelApp, self).__init__(*args, **kwargs)
        self._show_counties = True
        self._show_hotels = True

    @classmethod
    def create(cls):
        """
        This function is called once, and is responsible for
        creating all objects (plots, datasources, etc)
        """
        # create layout widgets
        obj = cls()
        obj.mainrow = HBox()
        obj.checkbox = VBox(height=50)
        obj.mapbox = VBox()
        #obj.bottomrow = HBox()
        obj.statsbox = VBox(width=500)
        obj.totalbox = VBox()
        obj.legendbox = HBox()

        labels = ["County Average Ratings", "Hotel Locations"]
        obj.make_inputs()
        obj.make_outputs()

        # outputs
        #obj.pretext = Paragraph(text="", width=500)
        obj.make_source()
        obj.make_county_source()
        lat = 39.8282
        lng = -98.5795
        zoom = 6
        xr = Range1d()
        yr = Range1d()
        #obj.make_plots(lat, lng, zoom, xr, yr)
        obj.make_plots()

        # layout
        obj.set_children()
        return obj

    @property
    def selected_df(self):
        pandas_df = self.df
        selected = self.source.selected
        print "seeing if selected!"
        if selected:
            idxs = selected['1d']['indices']
            pandas_df = pandas_df.iloc[idxs, :]
        else:
            pandas_df = pandas_df.iloc[0:0, :]
        return pandas_df

    def make_source(self):
        self.source = ColumnDataSource(data=self.df)
        self.source.callback = Callback(args=dict(),
                                        code="""

            var inds = cb_obj.get('selected')['1d'].indices;
            var theidx = inds[0];

            console.log("yep");
            console.log(theidx);
            $.get( "reviews", {id: theidx}, function( response ) {
                $( "#section2" ).html( response );
            }, "html");
            console.log("done");
        """)

    def update_source(self):
        if self.selectr.value is None or self.selectr.value == 'Choose A State':
            df = hdata
        else:
            df = hdata[hdata['state'] == self.selectr.value]

        new_data = {}
        for col in df:
            new_data[col] = df[col]

        self.source.data = new_data

    def make_county_source(self):
        self.county_source = ColumnDataSource(data=self.countydf)

    def update_county_source(self):
        if self.selectr.value is None or self.selectr.value == 'Choose A State':
            df = county_data
        else:
            df = county_data[county_data['state'] == self.selectr.value]

        new_data = {}
        for col in df:
            new_data[col] = df[col]

        self.county_source.data = new_data
        #for col in df:
        #    self.county_source.data[col] = df[col]

    def make_plots(self):
        self.create_map_plot()
        self.create_legend()
        self.populate_glyphs()
        self.make_bar_plot()

    def create_legend(self):

        x_range = Range1d(0, 185)
        y_range = Range1d(0, 130)

        text_box = Plot(x_range=x_range,
                        y_range=y_range,
                        title="",
                        plot_width=185,
                        plot_height=130,
                        min_border=0,
                        **PLOT_FORMATS)

        FONT_PROPS_SM['text_font_size'] = '11pt'
        text_box.add_glyph(
            Text(x=35, y=9, text=['Low Average Rating'], **FONT_PROPS_SM))
        text_box.add_glyph(
            Rect(x=18,
                 y=18,
                 width=25,
                 height=25,
                 fill_color='#ef4e4d',
                 line_color=None))
        text_box.add_glyph(
            Text(x=35, y=49, text=['Medium Average Rating'], **FONT_PROPS_SM))
        text_box.add_glyph(
            Rect(x=18,
                 y=58,
                 width=25,
                 height=25,
                 fill_color='#14a1af',
                 line_color=None))

        text_box.add_glyph(
            Text(x=35, y=89, text=['High Average Rating'], **FONT_PROPS_SM))
        text_box.add_glyph(
            Rect(x=18,
                 y=98,
                 width=25,
                 height=25,
                 fill_color='#743184',
                 line_color=None))

        self.legend_plot = text_box

        ##Filler plot
        x_range = Range1d(0, 40)
        y_range = Range1d(0, 100)
        self.legend_filler = Plot(x_range=x_range,
                                  y_range=y_range,
                                  title="",
                                  plot_width=40,
                                  plot_height=100,
                                  min_border=0,
                                  **PLOT_FORMATS)

    def create_map_plot(self):
        lat = 39.8282
        lng = -98.5795
        zoom = 6
        xr = Range1d()
        yr = Range1d()
        x_range = xr
        y_range = yr
        #map_options = GMapOptions(lat=39.8282, lng=-98.5795, zoom=6)
        map_options = GMapOptions(lat=lat, lng=lng, zoom=zoom)
        #map_options = GMapOptions(lat=30.2861, lng=-97.7394, zoom=15)
        plot = GMapPlot(x_range=x_range,
                        y_range=y_range,
                        map_options=map_options,
                        plot_width=680,
                        plot_height=600,
                        title=" ")
        plot.map_options.map_type = "hybrid"
        xaxis = LinearAxis(axis_label="lon",
                           major_tick_in=0,
                           formatter=NumeralTickFormatter(format="0.000"))
        plot.add_layout(xaxis, 'below')
        yaxis = LinearAxis(axis_label="lat",
                           major_tick_in=0,
                           formatter=PrintfTickFormatter(format="%.3f"))
        plot.add_layout(yaxis, 'left')

        self.plot = plot

    def populate_glyphs(self):
        self.plot.renderers = []
        self.plot.tools = []
        if self._show_counties:
            print "showing you the counties"
            #datasource = ColumnDataSource(county_data)
            #apatch = Patches(xs=county_xs, ys=county_ys, fill_color='white')
            #apatch = Patches(xs='xs', ys='ys', fill_color='colors', fill_alpha="alpha")
            apatch = Patches(xs='xs',
                             ys='ys',
                             fill_color='thecolors',
                             fill_alpha='alpha')
            self.plot.add_glyph(self.county_source, apatch, name='counties')

        if self._show_hotels:
            print "showing you the hotels"
            circle2 = Circle(x="lon",
                             y="lat",
                             size=10,
                             fill_color="fill2",
                             fill_alpha=1.0,
                             line_color="black")
            circle = Circle(x="lon",
                            y="lat",
                            size=10,
                            fill_color="fill",
                            fill_alpha=1.0,
                            line_color="black")
            #print "source is ", self.source['lon'], self.source['lat'], self.source['f1ll']
            self.plot.add_glyph(self.source,
                                circle2,
                                nonselection_glyph=circle,
                                name='hotels')
            #county_xs, county_ys = get_some_counties()

        rndr = self.plot.renderers[-1]
        pan = PanTool()
        wheel_zoom = WheelZoomTool()
        box_select = BoxSelectTool()
        box_select.renderers = [rndr]
        tooltips = "@name"
        tooltips = "<span class='tooltip-text'>@names</span>\n<br>"
        tooltips += "<span class='tooltip-text'>Reviews: @num_reviews</span>"
        hover = HoverTool(tooltips=tooltips, names=['hotels'])
        tap = TapTool(names=['hotels'])
        self.plot.add_tools(pan, wheel_zoom, box_select, hover, tap)
        overlay = BoxSelectionOverlay(tool=box_select)
        self.plot.add_layout(overlay)

    def make_bar_plot(self):
        # create a new plot
        y_rr = Range1d(start=0.0, end=5.0)
        TOOLS = "box_select,lasso_select"

        #x's and y's based on selected_df
        sdf = self.selected_df[['names', 'ratings']]

        xvals = [1.0 * i + 0.5 for i in range(0, len(sdf['names']))]
        rightvals = [1.0 * i + 0.85 for i in range(0, len(sdf['names']))]
        ratings = [r for r in sdf['ratings']]
        centers = [0.5 * r for r in ratings]
        bottoms = [0] * len(ratings)
        y_text = [y + 1.0 for y in ratings]
        width = [1.0] * len(ratings)
        all_names = []
        for n in sdf['names']:
            short_name = n[:20]
            idx = short_name.rfind(" ")
            all_names.append(short_name[:idx])

        while len(all_names) > 0 and len(all_names) < 5:
            all_names.append("   ")

        bar_plot = figure(tools=TOOLS,
                          width=400,
                          height=350,
                          x_range=all_names,
                          y_range=y_rr,
                          title="Average Rating")
        bar_plot.title_text_color = "black"
        bar_plot.title_text_font_size = '15pt'
        bar_plot.title_text_font = 'Avenir'
        bar_plot.title_text_align = "right"
        print "all_names ", all_names

        bar_colors = []
        for r in ratings:
            if r >= 4.0:
                bar_colors.append("#743184")
            elif r >= 3.0:
                bar_colors.append("#14a1af")
            else:
                bar_colors.append("#ef4e4d")

        bar_plot.xaxis.major_label_orientation = pi / 2.3
        bar_plot.rect(x=all_names,
                      y=centers,
                      width=width,
                      height=ratings,
                      color=bar_colors,
                      line_color="black")

        #glyph = Text(x=xvals, y=y_text, text=sdf['names'], angle=pi/4,
        #text_align="left", text_baseline="middle")

        #glyphs = [Text(x=x, y=y, text=[n], angle=pi/4, text_align="left", text_baseline="middle")
        #for x, y, n in zip(xvals, y_text, all_names)]

        bar_plot.xaxis.major_tick_line_color = None
        bar_plot.xaxis.minor_tick_line_color = None

        bar_plot.yaxis.minor_tick_line_color = None

        bar_plot.xgrid.grid_line_color = None
        bar_plot.ygrid.grid_line_color = None
        self.bar_plot = bar_plot

    def set_children(self):
        self.children = [self.totalbox]
        self.totalbox.children = [self.mainrow]
        self.mainrow.children = [self.statsbox, self.mapbox]
        self.mapbox.children = [self.plot]
        #self.checkbox.children = [self.filler, self.check_group, self.filler2]
        self.statsbox.children = [self.bar_plot, self.legendbox]
        self.legendbox.children = [self.legend_filler, self.legend_plot]
        #self.bottomrow.children = [self.pretext]

    def setup_events(self):
        super(HotelApp, self).setup_events()
        if self.source:
            self.source.on_change('selected', self, 'selection_change')
        if self.selectr:
            self.selectr.on_change('value', self, 'input_change')
        if self.check_group:
            self.check_group.on_change('active', self, 'check_change')

    @property
    def df(self):
        thedf = return_hotel_data()
        if self.selectr.value is None or self.selectr.value == 'Choose A State':
            return thedf
        else:
            newdf = thedf[thedf['state'] == self.selectr.value]
            return newdf

    @property
    def countydf(self):
        thedf = county_data
        if self.selectr.value is None or self.selectr.value == 'Choose A State':
            return thedf
        else:
            newdf = thedf[thedf['state'] == self.selectr.value]
            return newdf

    def selection_change(self, obj, attrname, old, new):
        #self.make_source()
        self.update_source()
        self.make_bar_plot()
        self.set_children()
        curdoc().add(self)

    def check_change(self, obj, attrname, old, new):
        #Turn on/off the counties/hotel data
        print "what the heck ", obj, attrname, old, new
        if 0 in new:
            self._show_counties = True
        else:
            self._show_counties = False

        if 1 in new:
            self._show_hotels = True
        else:
            self._show_hotels = False

        self.populate_glyphs()
        self.set_children()
        curdoc().add(self)

    def input_change(self, obj, attrname, old, new):
        #import pdb;pdb.set_trace()
        print "source len: ", len(self.source.data['state'])
        print "county len: ", len(self.county_source.data['names'])
        self.update_source()
        self.update_county_source()
        print "source len: ", len(self.source.data['state'])
        print "county len: ", len(self.county_source.data['names'])
        if self.selectr.value is None or self.selectr.value == 'Choose A State':
            pass
        else:
            self.plot.title = self.selectr.value
Exemplo n.º 32
0
# Update the plot based on selections
def update(attr, old, new):
    players_to_plot = [
        player_selection.labels[i] for i in player_selection.active
    ]

    new_src = make_dataset(players_to_plot)

    src.data = new_src.data


#    source.data = new_src.data

players = list(set(df_single_lines['Player']))
player_selection = CheckboxGroup(labels=players, active=[])
player_selection.on_change('active', lambda attr, old, new: update())

# Find the initially selected players
#initial_players = players
initial_players = [player_selection.labels[i] for i in player_selection.active]

src = make_dataset(initial_players)

p = make_plot(src)

# Put controls in a single element
controls = WidgetBox(player_selection)

# Create a row layout
layout = row(p, controls)
# drop unnecessary columns and simplify name
genes = genes[['start', 'end', 'name', 'strand']]
genes.name = [x.split(':')[1] for x in genes.name.tolist()]

# create colors for each condition and strand
colors = pd.DataFrame({
    'condition': ['LB', 'LB', 'M9', 'M9'],
    'strand': ['+', '-', '+', '-'],
    'color': ['#8daec9', '#edac80', '#528ecb', '#ef8137']
})

############################ Define widgets ####################################

condition_selection = CheckboxGroup(labels=['LB', 'M9'], active=[1])
# link change in selected buttons to update function
condition_selection.on_change('active', update_region_plot)

# # RangeSlider to change positions of region
# position_selection = RangeSlider(start = 2238500 - 300, end = 2238500 + 300,
#                           value = (2238500 - 300, 2238500 + 300),
#                           step = 100, title = 'genomic position')

# TextInput to define start and end position
# lacZ
# start = TextInput(value='362455', title='genome U00096.2 start position')
# end = TextInput(value='365529', title='genome end position')
start = TextInput(value='1232000', title='genome (U00096.2) start position')
end = TextInput(value='1238000', title='genome end position')

# update plot when value is changed
# position_selection.on_change('value', update_region_plot)
Exemplo n.º 34
0
class HotelApp(VBox):
    extra_generated_classes = [["HotelApp", "HotelApp", "VBox"]]
    jsmodel = "VBox"

    # input
    selectr = Instance(Select)
    #check_group = Instance(RadioGroup)
    check_group = Instance(CheckboxGroup)

    # plots
    plot = Instance(GMapPlot)
    bar_plot = Instance(Plot)

    # data source
    source = Instance(ColumnDataSource)
    county_source = Instance(ColumnDataSource)

    # layout boxes
    mainrow = Instance(HBox)
    #bottomrow = Instance(HBox)
    statsbox = Instance(VBox)
    totalbox = Instance(VBox)

    # inputs
    #ticker1_select = Instance(Select)
    #ticker2_select = Instance(Select)
    #input_box = Instance(VBoxForm)

    def make_inputs(self):
        with open("states.csv") as f:
            states = [line.strip().split(',') for line in f.readlines()]

        self.selectr = Select(
            name='states',
            value='Choose A State',
            options=[s[1] for s in states] + ['Choose A State']
        )
        labels = ["County Averages", "Hotels"]
        self.check_group = CheckboxGroup(labels=labels, active=[0,1])

    def make_outputs(self):
        pass
        #self.pretext = Paragraph(text="", width=800)

    def __init__(self, *args, **kwargs):
        super(HotelApp, self).__init__(*args, **kwargs)
        self._show_counties = True
        self._show_hotels = True

    @classmethod
    def create(cls):
        """
        This function is called once, and is responsible for
        creating all objects (plots, datasources, etc)
        """
        # create layout widgets
        obj = cls()
        obj.mainrow = HBox()
        #obj.bottomrow = HBox()
        obj.statsbox = VBox()
        obj.totalbox = VBox()

        labels = ["County Average Ratings", "Hotel Locations"]
        obj.make_inputs()
        obj.make_outputs()

        # outputs
        #obj.pretext = Paragraph(text="", width=500)
        obj.make_source()
        obj.make_county_source()
        lat=39.8282
        lng=-98.5795
        zoom=6
        xr = Range1d()
        yr = Range1d()
        #obj.make_plots(lat, lng, zoom, xr, yr)
        obj.make_plots()

        # layout
        obj.set_children()
        return obj

    @property
    def selected_df(self):
        pandas_df = self.df
        selected = self.source.selected
        print "seeing if selected!"
        if selected:
            idxs = selected['1d']['indices']
            pandas_df = pandas_df.iloc[idxs, :]
        else:
            pandas_df = pandas_df.iloc[0:0, :]
        return pandas_df


    def make_source(self):
        self.source = ColumnDataSource(data=self.df)
        self.source.callback = Callback(args=dict(), code="""

            var inds = cb_obj.get('selected')['1d'].indices;
            var theidx = inds[0];

            console.log("yep");
            console.log(theidx);
            $.get( "reviews", {id: theidx}, function( response ) {
                $( "#section2" ).html( response );
            }, "html");
            console.log("done");
        """)


    def update_source(self):
        if self.selectr.value is None or self.selectr.value == 'Choose A State':
            df = hdata
        else:
            df = hdata[hdata['state'] == self.selectr.value]

        for col in df:
            self.source.data[col] = df[col]

    def make_county_source(self):
        self.county_source = ColumnDataSource(data=self.countydf)

    def update_county_source(self):
        if self.selectr.value is None or self.selectr.value == 'Choose A State':
            df = county_data
        else:
            df = county_data[county_data['state'] == self.selectr.value]

        for col in df:
            self.county_source.data[col] = df[col]



    """def init_check_group(self):

        print "initing radio group"
        self.check_group.on_click(self.check_group_handler)

    def check_group_handler(self, active):
        print "radio group handler %s" % active"""

    def make_plots(self):
        self.create_map_plot()
        self.populate_glyphs()
        self.make_bar_plot()

    #def make_plots(self):
    def create_map_plot(self):
        lat=39.8282
        lng=-98.5795
        zoom=6
        xr = Range1d()
        yr = Range1d()
        x_range = xr
        y_range = yr
        #map_options = GMapOptions(lat=39.8282, lng=-98.5795, zoom=6)
        map_options = GMapOptions(lat=lat, lng=lng, zoom=zoom)
        #map_options = GMapOptions(lat=30.2861, lng=-97.7394, zoom=15)
        plot = GMapPlot(
            x_range=x_range, y_range=y_range,
            map_options=map_options,
            title = "Hotel Review Explorer",
            plot_width=680,
            plot_height=600
        )
        plot.map_options.map_type="hybrid"
        xaxis = LinearAxis(axis_label="lon", major_tick_in=0, formatter=NumeralTickFormatter(format="0.000"))
        plot.add_layout(xaxis, 'below')
        yaxis = LinearAxis(axis_label="lat", major_tick_in=0, formatter=PrintfTickFormatter(format="%.3f"))
        plot.add_layout(yaxis, 'left')

        #pan = PanTool()
        #wheel_zoom = WheelZoomTool()
        #box_select = BoxSelectTool()
        #box_select.renderers = [rndr]
        #tooltips = "@name"
        #tooltips = "<span class='tooltip-text'>@names</span>\n<br>"
        #tooltips += "<span class='tooltip-text'>Reviews: @num_reviews</span>"
        #hover = HoverTool(tooltips="@num_reviews")
        #hover = HoverTool(tooltips="@names")
        #hover = HoverTool(tooltips=tooltips)
        #tap = TapTool()
        #plot.add_tools(pan, wheel_zoom, box_select, hover, tap)
        #plot.add_tools(hover, tap)
        #overlay = BoxSelectionOverlay(tool=box_select)
        #plot.add_layout(overlay)
        #plot.add_glyph(self.source, circle)
        #county_xs, county_ys = get_some_counties()
        #apatch = Patch(x=county_xs, y=county_ys, fill_color=['white']*len(county_xs))
        #plot.add_glyph(apatch)
        self.plot = plot


    def populate_glyphs(self):
        self.plot.renderers=[]
        self.plot.tools=[]
        if self._show_counties:
            print "showing you the counties"
            #datasource = ColumnDataSource(county_data)
            #apatch = Patches(xs=county_xs, ys=county_ys, fill_color='white')
            #apatch = Patches(xs='xs', ys='ys', fill_color='colors', fill_alpha="alpha")
            apatch = Patches(xs='xs', ys='ys', fill_color='thecolors', fill_alpha='alpha')
            self.plot.add_glyph(self.county_source, apatch, name='counties')

        if self._show_hotels:
            print "showing you the hotels"
            circle2 = Circle(x="lon", y="lat", size=10, fill_color="fill2", fill_alpha=1.0, line_alpha=0.0)
            circle = Circle(x="lon", y="lat", size=10, fill_color="fill", fill_alpha=1.0, line_color="black")
            #print "source is ", self.source['lon'], self.source['lat'], self.source['f1ll']
            self.plot.add_glyph(self.source, circle, nonselection_glyph=circle2, name='hotels')
            #county_xs, county_ys = get_some_counties()

        rndr = self.plot.renderers[-1]
        pan = PanTool()
        wheel_zoom = WheelZoomTool()
        box_select = BoxSelectTool()
        box_select.renderers = [rndr]
        tooltips = "@name"
        tooltips = "<span class='tooltip-text'>@names</span>\n<br>"
        tooltips += "<span class='tooltip-text'>Reviews: @num_reviews</span>"
        hover = HoverTool(tooltips=tooltips, names=['hotels'])
        tap = TapTool(names=['hotels'])
        self.plot.add_tools(pan, wheel_zoom, box_select, hover, tap)
        overlay = BoxSelectionOverlay(tool=box_select)
        self.plot.add_layout(overlay)

    def make_bar_plot(self):
        # create a new plot
        x_rr = Range1d(start=0.0, end=6.0)
        y_rr = Range1d(start=0.0, end=10.0)
        TOOLS = "box_select,lasso_select"
        bar_plot = figure(tools=TOOLS, width=400, height=350, x_range=x_rr, y_range=y_rr, title="Average Rating")

        #x's and y's based on selected_df
        sdf = self.selected_df[['names', 'ratings']]

        xvals = [1.0*i + 0.5 for i in range(0, len(sdf['names']))]
        rightvals = [1.0*i + 0.85 for i in range(0, len(sdf['names']))]
        ratings = [r for r in sdf['ratings']]
        bottoms = [0]*len(ratings)
        y_text = [y + 1.0 for y in ratings]
        all_names = [n for n in sdf['names']]

        print "all_names ", all_names

        #bar_plot.circle(xvals, ratings, size=12)
        bar_plot.quad(xvals, rightvals, ratings, bottoms, fill="teal")

        #glyph = Text(x=xvals, y=y_text, text=sdf['names'], angle=pi/4,
                    #text_align="left", text_baseline="middle")

        glyphs = [Text(x=x, y=y, text=[n], angle=pi/4, text_align="left", text_baseline="middle") 
                  for x, y, n in zip(xvals, y_text, all_names)]
                   
        for g in glyphs:
            bar_plot.add_glyph(g)

        bar_plot.xaxis.major_tick_line_color = None
        bar_plot.xaxis.minor_tick_line_color = None
        #bar_plot.xaxis.major_tick_line_width = 3
        #bar_plot.xaxis.minor_tick_line_color = "orange"

        bar_plot.yaxis.minor_tick_line_color = None


        bar_plot.xgrid.grid_line_color = None
        bar_plot.ygrid.grid_line_color = None
        self.bar_plot = bar_plot

    def set_children(self):
        self.children = [self.totalbox]
        self.totalbox.children = [self.mainrow]
        self.mainrow.children = [self.statsbox, self.plot]
        self.statsbox.children = [self.selectr, self.check_group, self.bar_plot]
        #self.bottomrow.children = [self.pretext]


    def setup_events(self):
        super(HotelApp, self).setup_events()
        if self.source:
            self.source.on_change('selected', self, 'selection_change')
        if self.selectr:
            self.selectr.on_change('value', self, 'input_change')
        if self.check_group:
            self.check_group.on_change('active', self, 'check_change')

    @property
    def df(self):
        thedf = return_hotel_data()
        if self.selectr.value is None or self.selectr.value == 'Choose A State':
            return thedf
        else:
            newdf = thedf[thedf['state'] == self.selectr.value]
            return newdf

    @property
    def countydf(self):
        thedf = county_data
        if self.selectr.value is None or self.selectr.value == 'Choose A State':
            return thedf
        else:
            newdf = thedf[thedf['state'] == self.selectr.value]
            return newdf


    def selection_change(self, obj, attrname, old, new):
        #self.make_source()
        self.update_source()
        self.make_bar_plot()
        self.set_children()
        curdoc().add(self)

    def check_change(self, obj, attrname, old, new):
        #Turn on/off the counties/hotel data
        print "what the heck ", obj, attrname, old, new
        if 0 in new:
            self._show_counties = True
        else:
            self._show_counties = False

        if 1 in new:
            self._show_hotels = True
        else:
            self._show_hotels = False

        self.populate_glyphs()
        self.set_children()
        curdoc().add(self)

    def input_change(self, obj, attrname, old, new):
        self.make_source()
        self.make_county_source()
        print "source len: ", len(self.source.data['state'])
        print "county len: ", len(self.county_source.data['names'])
        if self.selectr.value is None or self.selectr.value == 'Choose A State':
            pass
        else:
            self.plot.title = self.selectr.value
        self.populate_glyphs()

        self.set_children()
        curdoc().add(self)
Exemplo n.º 35
0
def timeseries_tab(dataframe):
    """
    return a tab showing steps vs time for each person
    """
    def make_dataset(name_list,
                     range_start='2019-01-01',
                     range_end='2019-12-31'):
        """
        Filter the full dataset by name and by date range,
        and return it as a Bokeh ColumnDataSource
        """

        ## why do I have to do this? What is the point of a DateRangeSlider???
        if isinstance(range_start, int):
            range_start = datetime.fromtimestamp(range_start / 1000)
        if isinstance(range_end, int):
            range_end = datetime.fromtimestamp(range_end / 1000)

#        filtered_df = dataframe[name_list].loc[range_start: range_end]
        filtered_df = dataframe.loc[range_start:range_end]

        source = ColumnDataSource(filtered_df)
        return source

    def style(p):
        # Title
        p.title.align = 'center'
        p.title.text_font_size = '20pt'
        p.title.text_font = 'serif'

        # Axis titles
        p.xaxis.axis_label_text_font_size = '14pt'
        p.xaxis.axis_label_text_font_style = 'bold'
        p.yaxis.axis_label_text_font_size = '14pt'
        p.yaxis.axis_label_text_font_style = 'bold'

        # Tick labels
        p.xaxis.major_label_text_font_size = '12pt'
        p.yaxis.major_label_text_font_size = '12pt'

        return p

    def make_plot(source):
        """
        create a Bokeh figure with the selected data
        """
        names_to_plot = source.column_names[1:]

        time_series = figure(x_axis_type="datetime",
                             plot_width=700,
                             plot_height=550)
        hover = HoverTool(tooltips=[("Name", "$name"), ("Date", "@Date{%F}"),
                                    ("Steps", "$y")],
                          formatters={'Date': 'datetime'})
        time_series = style(time_series)
        time_series.add_tools(hover)
        time_series.legend.location = "top_left"
        for i, name in enumerate(names_to_plot):
            time_series.line("Date",
                             name,
                             source=source,
                             line_color=Category20_16[i],
                             line_width=2,
                             name=name,
                             legend_label=name)
        return time_series

    def update(attr, old, new):
        """
        Update data source when something is changed.
        """
        name_list = [name_selection.labels[i] for i in name_selection.active]
        new_src = make_dataset(name_list, date_slider.value[0],
                               date_slider.value[1])
        cds.data.update(new_src.data)

    def update_lines(attr, old, new):
        """
        Hide selected lines
        """
        names_to_plot = [
            name_selection.labels[i] for i in name_selection.active
        ]
        for name in names:
            if name in names_to_plot:
                p.select_one({"name": name}).visible = True
            else:
                p.select_one({"name": name}).visible = False

    ### back to the timeseries_tab function
    names = list(dataframe.columns)
    names.sort()

    # widgets to allow user to configure the plot
    name_selection = CheckboxGroup(labels=names,
                                   active=list(range(len(names))))

    name_selection.on_change('active', update_lines)

    date_slider = DateRangeSlider(title="Date range",
                                  start=date(2019, 1, 1),
                                  end=date(2019, 12, 31),
                                  value=(date(2019, 1, 1), date(2019, 12, 31)),
                                  step=1)
    date_slider.on_change('value', update)

    initial_names = [name_selection.labels[i] for i in name_selection.active]
    cds = make_dataset(initial_names, date_slider.value[0],
                       date_slider.value[1])
    p = make_plot(cds)

    controls = WidgetBox(name_selection, date_slider)
    layout = row(controls, p)

    tab = Panel(child=layout, title="Time series")
    return tab
	for attr in attributes_ord:

		if attributes_ord.index(attr) in checkbox_group_p.active:

			data["timestamp_"+attr] = darray[:,0]
			data[attr]              = darray[:,attributes.index(attr)]

		else:

			data["timestamp_"+attr] = darray[:,0]
			data[attr]              = [0 for _ in range(darray.shape[1])]

	source.data, task_source.data, x = query_events(current_run)

checkbox_group_p.on_change("active", checkbox)

# get a list of all scientific workflow runs with id, number of log messages and start timestamp.
run_map = query_runs()

# select the latest run by default
current_run = run_map.keys()[0]

general_info = {"tasks": 0, "start_time" : 0, "elapsed_time": 0}

task_types = {}

# active tasks history plot dimensions
PLOT_WIDTH = 1400
PLOT_HEIGHT = 600
Exemplo n.º 37
0
def mgstat_tab(db):
    def make_dataset(mgstat_list):
        newdf = mgstat[mgstat_list]
        # Convert dataframe to column data source
        return ColumnDataSource(newdf)

    def make_plot(src):
        # Blank plot with correct labels
        p = Figure(
            plot_width=1024,
            plot_height=768,
            x_axis_type="datetime",
            title="mgstat",
            output_backend="webgl",
        )
        cm = plt.get_cmap("gist_rainbow")

        numlines = len(mgstat.columns)
        mypal = [cm(1.0 * i / numlines) for i in range(numlines)]
        mypal = list(map(lambda x: colors.rgb2hex(x), mypal))
        col = 0
        for key in src.data.keys():
            if key == "datetime":
                continue
            l = key + " "
            col = col + 1
            p.line(
                mgstat.index.values,
                mgstat[key],
                line_width=1,
                alpha=0.8,
                name=key,
                legend=key,
                color=mypal[col],
            )
        p.legend.click_policy = "hide"
        return p

    def update(attr, old, new):
        print("update called")
        mgstats_to_plot = [
            mgstat_selection.labels[i] for i in mgstat_selection.active
        ]
        new_src = make_dataset(mgstats_to_plot)
        src.data = new_src.data
        plot = make_plot(src)
        layout.children[1] = plot

    # get data from DB, setup index
    mgstat = pd.read_sql_query("select * from mgstat", db)
    mgstat.index = pd.to_datetime(mgstat["datetime"])
    mgstat = mgstat.drop(["datetime"], axis=1)
    mgstat.index.name = "datetime"
    mgstat_selection = CheckboxGroup(labels=list(mgstat.columns),
                                     active=[0, 5])

    mgstat_list = [mgstat_selection.labels[i] for i in mgstat_selection.active]
    src = make_dataset(mgstat_list)
    plot = make_plot(src)

    mgstat_selection.on_change("active", update)
    controls = WidgetBox(mgstat_selection)
    layout = row(controls, plot)
    tab = Panel(child=layout, title="mgstat")
    return tab
Exemplo n.º 38
0
def density_tab(flights):
	
	# Dataset for density plot based on carriers, range of delays,
	# and bandwidth for density estimation
	def make_dataset(carrier_list, range_start, range_end, bandwidth):

		xs = []
		ys = []
		colors = []
		labels = []

		for i, carrier in enumerate(carrier_list):
			subset = flights[flights['name'] == carrier]
			subset = subset[subset['arr_delay'].between(range_start, 
														range_end)]

			kde = gaussian_kde(subset['arr_delay'], bw_method=bandwidth)
			
			# Evenly space x values
			x = np.linspace(range_start, range_end, 100)
			# Evaluate pdf at every value of x
			y = kde.pdf(x)

			# Append the values to plot
			xs.append(list(x))
			ys.append(list(y))

			# Append the colors and label
			colors.append(airline_colors[i])
			labels.append(carrier)

		new_src = ColumnDataSource(data={'x': xs, 'y': ys, 
								   'color': colors, 'label': labels})

		return new_src

	def make_plot(src):
		p = figure(plot_width = 700, plot_height = 700,
				   title = 'Density Plot of Arrival Delays by Airline',
				   x_axis_label = 'Delay (min)', y_axis_label = 'Density')


		p.multi_line('x', 'y', color = 'color', legend = 'label', 
					 line_width = 3,
					 source = src)

		# Hover tool with next line policy
		hover = HoverTool(tooltips=[('Carrier', '@label'), 
									('Delay', '$x'),
									('Density', '$y')],
						  line_policy = 'next')

		# Add the hover tool and styling
		p.add_tools(hover)

		p = style(p)

		return p
	
	def update(attr, old, new):
		# List of carriers to plot
		carriers_to_plot = [carrier_selection.labels[i] for i in 
							carrier_selection.active]
		
		# If no bandwidth is selected, use the default value
		if bandwidth_choose.active == []:
			bandwidth = None
		# If the bandwidth select is activated, use the specified bandwith
		else:
			bandwidth = bandwidth_select.value
			
		
		new_src = make_dataset(carriers_to_plot,
									range_start = range_select.value[0],
									range_end = range_select.value[1],
									bandwidth = bandwidth)
		
		src.data.update(new_src.data)
		
	def style(p):
		# Title 
		p.title.align = 'center'
		p.title.text_font_size = '20pt'
		p.title.text_font = 'serif'

		# Axis titles
		p.xaxis.axis_label_text_font_size = '14pt'
		p.xaxis.axis_label_text_font_style = 'bold'
		p.yaxis.axis_label_text_font_size = '14pt'
		p.yaxis.axis_label_text_font_style = 'bold'

		# Tick labels
		p.xaxis.major_label_text_font_size = '12pt'
		p.yaxis.major_label_text_font_size = '12pt'

		return p
	
	# Carriers and colors
	available_carriers = list(set(flights['name']))
	available_carriers.sort()

	airline_colors = Category20_16
	airline_colors.sort()

	# Carriers to plot
	carrier_selection = CheckboxGroup(labels=available_carriers, 
									   active = [0, 1])
	carrier_selection.on_change('active', update)
	
	range_select = RangeSlider(start = -60, end = 180, value = (-60, 120),
							   step = 5, title = 'Range of Delays (min)')
	range_select.on_change('value', update)
	
	# Initial carriers and data source
	initial_carriers = [carrier_selection.labels[i] for 
						i in carrier_selection.active]
	
	# Bandwidth of kernel
	bandwidth_select = Slider(start = 0.1, end = 5, 
							  step = 0.1, value = 0.5,
							  title = 'Bandwidth for Density Plot')
	bandwidth_select.on_change('value', update)
	
	# Whether to set the bandwidth or have it done automatically
	bandwidth_choose = CheckboxButtonGroup(
		labels=['Choose Bandwidth (Else Auto)'], active = [])
	bandwidth_choose.on_change('active', update)

	# Make the density data source
	src = make_dataset(initial_carriers, 
						range_start = range_select.value[0],
						range_end = range_select.value[1],
						bandwidth = bandwidth_select.value) 
	
	# Make the density plot
	p = make_plot(src)
	
	# Add style to the plot
	p = style(p)
	
	# Put controls in a single element
	controls = WidgetBox(carrier_selection, range_select, 
						 bandwidth_select, bandwidth_choose)
	
	# Create a row layout
	layout = row(controls, p)
	
	# Make a tab with the layout 
	tab = Panel(child=layout, title = 'Density Plot')

	return tab