Exemplo n.º 1
0
def make_plots_tab2(source_heat_map_temp, source_events_temp, source_boundaries, source_rolling_temp, source_data_aggregated_day_temp, dates_list, hour_list):

    colors_heat_map = ['#fff7fb', '#ece7f2', '#d0d1e6', '#a6bddb', '#74a9cf', '#3690c0', '#0570b0', '#045a8d', '#023858']
    mapper_heat_map = LogColorMapper(palette=colors_heat_map, low= 0, high=1000000)

    TOOLS_heat_map = "save,pan ,reset, xwheel_zoom"

    p_heat_map = figure(title="Water consumption in Log(Litres)",x_axis_type="datetime", x_range = dates_list, y_range = list(reversed(hour_list)),
                        tools=TOOLS_heat_map)

    heat_map = p_heat_map.rect(x="date", y="hour", width=1, height=1, source = source_heat_map_temp, fill_color={'field': 'rate', 'transform': mapper_heat_map}, line_color=None)
    p_events = p_heat_map.triangle(x = 'date', y = 'Hour', legend= "Events", source = source_events_temp, color = 'color', size= 12, line_color="black")

    #boundaries
    p_boundary_start = p_heat_map.line(x = 'start_date', y = 'hour', source =source_boundaries, line_dash = 'dashed', color = "firebrick", line_width = 4 )
    p_boundary_end = p_heat_map.line(x = 'end_date', y = 'hour', source =source_boundaries, line_dash = 'dashed', color = "firebrick", line_width = 4 )


    color_bar = ColorBar(color_mapper=mapper_heat_map, border_line_color=None,label_standoff=12, location=(0, 0))
    color_bar.formatter = NumeralTickFormatter(format='0.0')
    p_heat_map.add_layout(color_bar, 'right')

    heat_map_hover = HoverTool(renderers=[heat_map],
                        tooltips=OrderedDict([('Water Consumption (Litres)', '@rate'),
                                            ('date hour', '@date'),
                                             ('hour', '@hour'),
                                           ]))

    events_hover = HoverTool(renderers=[p_events],
            tooltips=OrderedDict([('Event description',
            '@{Hoofdtype Melding}'),
            ]))

    p_heat_map.grid.grid_line_color = None
    p_heat_map.axis.axis_line_color = None
    p_heat_map.axis.major_tick_line_color = None
    p_heat_map.xaxis.major_label_text_font_size = '0pt'  # turn off x-axis tick labels
    p_heat_map.yaxis.axis_label = 'Hour'
    p_heat_map.xaxis.axis_label = None
    p_heat_map.axis.major_label_standoff = 0

    p_heat_map.legend.location = "top_left"
    p_heat_map.legend.click_policy= "hide"
    p_heat_map.add_tools(heat_map_hover)
    p_heat_map.add_tools(events_hover)

    p_outliers = figure(title="Daily water consumptions in Litres", x_axis_type="datetime", tools=TOOLS_heat_map, x_range = dates_list)
    p_circle = p_outliers.circle(x = 'date', y = 'delta_total', size='s', color= 'c', alpha='a', legend= "Consumption in L",
                                 source = source_data_aggregated_day_temp)

    p_ub = p_outliers.line(x='date', y='ub', legend='upper_bound (2 sigma)', line_dash = 'dashed', line_width = 4, color = '#984ea3',
                           source = source_rolling_temp)
    p_mean = p_outliers.line(x='date', y='y_mean', source = source_rolling_temp, line_dash = 'dashed', line_width = 3,
                             legend='moving_average', color = '#4daf4a')

    #Dynamic boundaries
    p_boundary_start = p_outliers.line(x = 'start_date', y = 'y', source =source_boundaries, line_dash = 'dashed', color = "firebrick", line_width = 4 )
    p_boundary_end = p_outliers.line(x = 'end_date', y = 'y', source =source_boundaries, line_dash = 'dashed', color = "firebrick", line_width = 4 )


    p_outliers.legend.location = "top_left"
    p_outliers.legend.orientation = "horizontal"
    p_outliers.legend.click_policy= "hide"
    p_outliers.ygrid.band_fill_color = "olive"
    p_outliers.ygrid.band_fill_alpha = 0.1
    p_outliers.xaxis.axis_label = None
    p_outliers.yaxis.axis_label = 'Litres'
    p_outliers.xaxis.major_label_orientation = 3.1416 / 3
    p_outliers.x_range = p_heat_map.x_range# Same axes as the heatMap

    p_outliers.xaxis.formatter = FuncTickFormatter(code=""" var labels = %s; return labels[tick];""" % dates_list)


    circle_hover = HoverTool(renderers=[p_circle],
                        tooltips=OrderedDict([('date', '@date'),
                                              ('Water Consumption (L)', '@delta_total'),
                                             ]))

    p_ub_hover = HoverTool(renderers=[p_ub],
                        tooltips=OrderedDict([('date', '@date'),
                                              ('UpperBound water consumption (L)', '@ub'),
                                             ]))

    p_mean_hover = HoverTool(renderers=[p_mean],
                        tooltips=OrderedDict([('date', '@date'),
                                              ('Mean water consumption (L)', '@y_mean'),
                                             ]))

    p_outliers.add_tools(circle_hover)
    p_outliers.add_tools(p_ub_hover)
    p_outliers.add_tools(p_mean_hover)

    return p_heat_map, p_outliers
Exemplo n.º 2
0
from bokeh.models import ColorBar, LogColorMapper, LogTicker
from bokeh.plotting import figure, output_file, show

output_file('color_bar.html')


def normal2d(X, Y, sigx=1.0, sigy=1.0, mux=0.0, muy=0.0):
    z = (X - mux)**2 / sigx**2 + (Y - muy)**2 / sigy**2
    return np.exp(-z / 2) / (2 * np.pi * sigx * sigy)


X, Y = np.mgrid[-3:3:100j, -2:2:100j]
Z = normal2d(X, Y, 0.1, 0.2, 1.0, 1.0) + 0.1 * normal2d(X, Y, 1.0, 1.0)
image = Z * 1e6

color_mapper = LogColorMapper(palette="Viridis256", low=1, high=1e7)

plot = figure(x_range=(0, 1), y_range=(0, 1), toolbar_location=None)
plot.image(image=[image],
           color_mapper=color_mapper,
           dh=[1.0],
           dw=[1.0],
           x=[0],
           y=[0])

color_bar = ColorBar(color_mapper=color_mapper,
                     ticker=LogTicker(),
                     label_standoff=12,
                     border_line_color=None,
                     location=(0, 0))
Exemplo n.º 3
0
    def display(self, savefile='image.html', limits=None, show=True, log=True):
        if self.unit.__str__() == 'R_Mercury':
            ustr = 'R_M'
        else:
            ustr = 'R_obj'

        if self.quantity == 'radiance':
            runit = 'kR'
            rname = 'Radiance'
        elif self.quantity == 'column':
            runit = 'cm-2'
            rname = 'Column'
        else:
            assert 0

        tooltips = [('x', '$x{0.1f} ' + ustr), ('y', '$y{0.1f} ' + ustr),
                    (rname, '@image ' + runit)]

        curdoc().theme = Theme(
            os.path.join(os.path.dirname(__file__), 'data', 'bokeh.yml'))

        if log:
            if limits is None:
                limits = (self.image[self.image > 0].min(), self.image.max())
            else:
                pass
            color_mapper = LogColorMapper(palette=Inferno256,
                                          low=limits[0],
                                          high=limits[1])
        else:
            if limits is None:
                limits = (0, self.image.max())
            else:
                pass
            color_mapper = LinearColorMapper(palette=Inferno256,
                                             low=limits[0],
                                             high=limits[1])

        x0 = np.min(self.xaxis.value)
        y0 = np.min(self.zaxis.value)
        dw = np.max(self.xaxis.value) - np.min(self.xaxis.value)
        dh = np.max(self.zaxis.value) - np.min(self.zaxis.value)

        fig = bkp.figure(
            plot_width=1000,
            plot_height=1000,
            title=f'{self.inputs.options.species} {rname}',
            x_axis_label=f'Distance ({ustr})',
            y_axis_label=f'Distance ({ustr})',
            x_range=[np.min(self.xaxis.value),
                     np.max(self.xaxis.value)],
            y_range=[np.min(self.zaxis.value),
                     np.max(self.zaxis.value)],
            tooltips=tooltips)

        fig.image(image=[self.image.T],
                  x=x0,
                  y=y0,
                  dw=dw,
                  dh=dh,
                  color_mapper=color_mapper)
        xc = np.cos(np.linspace(0, 2 * np.pi, 1000))
        yc = np.sin(np.linspace(0, 2 * np.pi, 1000))
        fig.patch(xc, yc, fill_color='yellow')

        bkp.output_file(savefile)
        export_png(fig, filename=savefile.replace('.html', '.png'))
        if show:
            bkp.show(fig)
        else:
            bkp.save(fig)

        #
        # # Determine limits if none given
        # if limits is None:
        #     interval = PercentileInterval(95)
        #     self.blimits = interval.get_limits(self.image[self.image > 0])
        # elif len(limits) == 2:
        #     self.blimits = limits
        # else:
        #     assert 0, 'Problem with the display limits'

#        norm = ImageNormalize(self.image, stretch=LogStretch(),
#                              vmin=self.blimits[0], vmax=self.blimits[1])

# # Make the colorbar
# if self.quantity == 'column':
#     clabel = f'$N_{{ {self.inputs.options.species} }}\ cm^{{-2}}$'
# else:
#     clabel = f'$I_{{ {self.inputs.options.species} }} R$'
# cbar = fig.colorbar(im, shrink=0.7, label=clabel)

# Put Planet's disk in the middle
# xc, yc = (np.cos(np.linspace(0, 2*np.pi, 1000)),
#           np.sin(np.linspace(0, 2*np.pi, 1000)))
# ax.fill(xc, yc, 'y')

        return fig
Exemplo n.º 4
0
def create_plot():

    s1 = ColumnDataSource(data=dict(x=['Male', 'Female'],
                                    y=[0, 0]))  # Gender of Providers
    s2 = ColumnDataSource(data=dict(x=['Male', 'Female'],
                                    y=[0, 0]))  # Gender of Beneficiaries
    # s3 = ColumnDataSource()  # Type of Physicians

    callback = CustomJS(args=dict(source=data, s1=s1, s2=s2),
                        code="""
        var i = source.selected.indices
        console.log(cb_data.source.data['male_prov'][i])
        console.log(cb_data.source.data['male_benif'][i])
        
        var d1 = s1.data;
        d1['x'] = []
        d1['y'] = []
        d1['x'].push('Male')
        d1['x'].push('Female')
        d1['y'].push(cb_data.source.data['male_prov'][i])
        d1['y'].push(cb_data.source.data['female_pro'][i])
        
        s1.change.emit();
        
        var d2 = s2.data;
        d2['x'] = []
        d2['y'] = []
        d2['x'].push('Male')
        d2['x'].push('Female')
        d2['y'].push(cb_data.source.data['male_benif'][i])
        d2['y'].push(cb_data.source.data['female_benif'][i])
        
        s2.change.emit();
        """)

    color_mapper = LogColorMapper(palette=palette)

    p = figure(title="Medicare Data",
               toolbar_location="left",
               plot_width=810,
               plot_height=484,
               match_aspect=True,
               aspect_scale=0.7,
               x_range=[-128, -64],
               y_range=[23, 50])
    p.patches('lons',
              'lats',
              source=data,
              fill_alpha=1,
              line_color="#FF9200",
              line_width=1.25,
              fill_color={
                  'field': 'male_prov',
                  'transform': color_mapper
              })
    p.add_tools(TapTool(callback=callback))

    q1 = figure(title="Providers Gender",
                x_range=['Male', 'Female'],
                plot_width=250,
                plot_height=250,
                y_axis_label='Number of Providers',
                x_axis_label="Gender")
    q1.vbar(x='x', top='y', width=0.5, source=s1)

    q2 = figure(title="Beneficiaries Gender",
                x_range=['Male', 'Female'],
                plot_width=250,
                plot_height=250,
                y_axis_label='Number of Beneficiaries',
                x_axis_label="Gender")
    q2.vbar(x='x', top='y', width=0.5, source=s2)

    return row(p, column(q1, q2))
Exemplo n.º 5
0
country_keys = [item for sublist in temp_list for item in sublist]
temp_list = [[country.get_veges() for unit in country.get_coordinate_list()]
             for country in countries]
country_veges = [item for sublist in temp_list for item in sublist]

df = pd.DataFrame(data=dict(x=country_xs,
                            y=country_ys,
                            name=country_names,
                            area=country_areas,
                            key=country_keys,
                            veges=country_veges))
print(df.tail())

source = ColumnDataSource(df)

color_mapper = LogColorMapper(palette=palette)
print("col_map: ", color_mapper)
print("palette: ", palette)

TOOLS = "pan,wheel_zoom,reset,hover,save,tap"
p = figure(title="First Test Map ",
           tools=TOOLS,
           x_axis_location=None,
           y_axis_location=None,
           plot_width=1600,
           plot_height=800)
p.grid.grid_line_color = None
p.background_fill_color = "#33b5ff"
p.background_fill_alpha = 0.2

renderer = p.patches('x',
Exemplo n.º 6
0
us_states_datasource["lons"] = us_states_df.lons.values.tolist()
us_states_datasource["lats"] = us_states_df.lats.values.tolist()
us_states_datasource["name"] = us_states_df.name.values.tolist()
us_states_datasource["StateCodes"] = us_states_df.index.values.tolist()
us_states_datasource["StarbuckStores"] = us_states_df.Count.values.tolist()

fig = figure(
    plot_width=900,
    plot_height=600,
    title="United States Starbucks Store Count Per State Choropleth Map",
    x_axis_location=None,
    y_axis_location=None,
    tooltips=[("Name", "@name"), ("StarbuckStores", "@StarbuckStores"),
              ("(Long, Lat)", "($x, $y)")])

fig.grid.grid_line_color = None

fig.patches("lons",
            "lats",
            source=us_states_datasource,
            fill_color={
                'field': 'StarbuckStores',
                'transform': LogColorMapper(palette=oranges[::-1])
            },
            fill_alpha=0.7,
            line_color="white",
            line_width=0.5)

output_file('index.html')

show(fig)
Exemplo n.º 7
0
def world_map(variable='confirmed', logscale=False):
	"""
	Interactive world map in bokeh figure
	"""

	shapefile = os.path.join(ws.folders['data/misc'], 'countries/ne_110m_admin_0_countries.shp')

	# Read shapefile using Geopandas
	gdf = gpd.read_file(shapefile)[['ADMIN', 'ADM0_A3', 'geometry']]

	# Rename columns
	gdf.columns = ['country', 'country_code', 'geometry']

	# Drop row corresponding to 'Antarctica'
	gdf = gdf.drop(gdf.index[159])

	# Folder containing daily reports
	folder = os.path.join(ws.folders['data/covid'], 'csse_covid_19_data/csse_covid_19_daily_reports/')

	# Point to the global dataframe (no provinces)
	df = ws.data_countries_only

	# Merge the data frames
	df_last_date = df[df['date_key'] == ws.dates_keys[-1]][['country_region', variable]]
	merged = gdf.merge(df_last_date, left_on='country', right_on='country_region', how='left')
	
	# Read data to json
	merged_json = json.loads(merged.to_json())

	# Convert to String like object.
	json_data = json.dumps(merged_json)


	# Input GeoJSON source that contains features for plotting.
	geosource = GeoJSONDataSource(geojson=json_data)

	
	# Define a sequential multi-hue color palette.
	order = int(np.log10(df[variable].max()))
	max_value = df[variable].max()
	max_rounded = utl.round_up_order(max_value, order)
	nlinticks = max_rounded // (10 ** order) + 1
	if logscale:
		palette = brewer['Reds'][order + 1]
	else:
		palette = brewer['Reds'][nlinticks - 1]
	# Reverse color order so that dark blue is highest obesity.
	palette = palette[::-1]
	
	# Instantiate LinearColorMapper that linearly maps numbers in a range, into a sequence of colors. Input nan_color.
	max_value = df_last_date[variable].max()
	max_rounded = utl.round_up_order(max_value, order)
	color_mapper = LinearColorMapper(palette=palette, low=0, high=max_rounded)
	color_mapper_log = LogColorMapper(palette=palette, low=1, high=10**(order+1))

	# Define custom tick labels for color bar.
	tick_labels = dict([('%i'%i, '%i'%i) for i in np.linspace(0, max_rounded, nlinticks)])
	# Make tick labels numbers
	tick_labels = [int(v) for v in tick_labels.values()]
	tick_labels_log = dict([('%i'%i, '%i'%i) for i in np.logspace(0, order, order + 1)])

	# Add hover tool
	hover = HoverTool(tooltips=[('País', '@country'), ('Casos', '@active')])

	#Create color bar. 
	color_bar = ColorBar(
			color_mapper=color_mapper, 
			label_standoff=8, 
			width = 450, 
			height = 20,
			border_line_color=None, 
			location = (0,0), 
			orientation = 'horizontal', 
			ticker=FixedTicker(ticks=tick_labels), 
		)
	color_bar_log = ColorBar(
			color_mapper=color_mapper_log, 
			label_standoff=8, 
			width = 450, 
			height = 10,
			border_line_color=None, 
			location = (0,0), 
			orientation = 'horizontal', 
			ticker=LogTicker(), 
		)

	# Create figure object.
	p = figure(
			title = 'Casos reportados como activos en el mundo, %s'%ws.dates_keys[-1], 
			plot_height = 500, 
			plot_width = 800, 
			toolbar_location = None, 
			# tools = [hover]
		)
	p.add_tools(hover)

	p.xgrid.grid_line_color = None
	p.ygrid.grid_line_color = None

	# Add patch renderer to figure. 
	p.patches(
			'xs', 
			'ys', 
			source = geosource, 
			fill_color = {'field' :variable, 'transform' : color_mapper_log}, 
			line_color = 'black', 
			line_width = 0.25, 
			fill_alpha = 1
		)

	#Specify layout
	if logscale:
		p.add_layout(color_bar_log, 'below')
	else:
		p.add_layout(color_bar, 'below')

	# Display plot

	# Save file
	if logscale:
		output_file(os.path.join(ws.folders["website/static/images"], "world_map_log.html"))
	else:
		output_file(os.path.join(ws.folders["website/static/images"], "world_map.html"))
	# show(p)
	save(p)
def investigate_fsed(directory, metal, distance, fsed, plot_file='plot.html'):
    """
      This functionw as created to investigate CLD output from the grid created in Batalha+2018. 
      The CLD files are not included in the SQLite database so these file names refer to the 
      specific naming function and directory. These files are available upon request. 
      Email [email protected] 

      The plot itselfs plots maps of the wavelength dependent single scattering albedo 
      and cloud opacity for up to three different fseds at a single metallicty and distance. 
      It was created becaues there are several times when fsed does funky numerical things 
      in the upper layers of the atmosphere. 

      Parameters
      ----------
      directory : str 
            Directory which points to the fortney grid of cloud output from Ackerman Marley code 
      metal : str 
            Metallicity: Options are m0.0 m0.5  m1.0  m1.5  m1.7  m2.0
      distance : str 
            Distance to G-type star. Options are d0.5 d0.6  d0.7  d0.85 d1.0  d1.5  d2.0  d3.0  d4.0  d5.0
      fsed : list of str 
            Up to three sedimentation efficiencies. Options are f0.01 f0.03 f0.3 f1 f3 f6
      plot_file : str 
            (Optional)This is the html plotting file Default = 'plot.html'
      """
    cols = colfun1(200)
    color_mapper = LinearColorMapper(palette=cols, low=0, high=1)

    dat01 = pd.read_csv(os.path.join(
        directory, metal, distance,
        metal + 'x_rfacv0.5-nc_tint150-' + fsed[0] + '-' + distance + '.cld'),
                        header=None,
                        delim_whitespace=True)
    dat1 = pd.read_csv(os.path.join(
        directory, metal, distance,
        metal + 'x_rfacv0.5-nc_tint150-' + fsed[1] + '-' + distance + '.cld'),
                       header=None,
                       delim_whitespace=True)
    dat6 = pd.read_csv(os.path.join(
        directory, metal, distance,
        metal + 'x_rfacv0.5-nc_tint150-' + fsed[2] + '-' + distance + '.cld'),
                       header=None,
                       delim_whitespace=True)

    scat01 = np.flip(np.reshape(dat01[4], (60, 196)), 0)  #[0:10,:]
    scat1 = np.flip(np.reshape(dat1[4], (60, 196)), 0)  #[0:10,:]
    scat6 = np.flip(np.reshape(dat6[4], (60, 196)), 0)  #[0:10,:]

    xr, yr = scat01.shape

    f01a = figure(x_range=[150, yr],
                  y_range=[0, xr],
                  x_axis_label='Wavelength Grid',
                  y_axis_label='Pressure Grid, TOA ->',
                  title="Scattering Fsed = 0.01",
                  plot_width=300,
                  plot_height=300)

    f1a = figure(x_range=[150, yr],
                 y_range=[0, xr],
                 x_axis_label='Wavelength Grid',
                 y_axis_label='Pressure Grid, TOA ->',
                 title="Scattering Fsed = 0.3",
                 plot_width=300,
                 plot_height=300)

    f6a = figure(x_range=[150, yr],
                 y_range=[0, xr],
                 x_axis_label='Wavelength Grid',
                 y_axis_label='Pressure Grid, TOA ->',
                 title="Scattering Fsed = 1",
                 plot_width=300,
                 plot_height=300)

    f01a.image(image=[scat01],
               color_mapper=color_mapper,
               x=0,
               y=0,
               dh=xr,
               dw=yr)
    f1a.image(image=[scat1], color_mapper=color_mapper, x=0, y=0, dh=xr, dw=yr)
    f6a.image(image=[scat6], color_mapper=color_mapper, x=0, y=0, dh=xr, dw=yr)

    color_bar = ColorBar(
        color_mapper=color_mapper,  #ticker=LogTicker(),
        label_standoff=12,
        border_line_color=None,
        location=(0, 0))

    f01a.add_layout(color_bar, 'right')

    #PLOT OPD
    scat01 = np.flip(np.reshape(dat01[2] + 1e-60, (60, 196)), 0)
    scat1 = np.flip(np.reshape(dat1[2] + 1e-60, (60, 196)), 0)
    scat6 = np.flip(np.reshape(dat6[2] + 1e-60, (60, 196)), 0)
    xr, yr = scat01.shape
    cols = colfun2(200)[::-1]
    color_mapper = LogColorMapper(palette=cols, low=1e-3, high=10)

    f01 = figure(x_range=[150, yr],
                 y_range=[0, xr],
                 x_axis_label='Wavelength Grid',
                 y_axis_label='Pressure Grid, TOA ->',
                 title="Optical Depth Fsed = 0.01",
                 plot_width=300,
                 plot_height=300)

    f1 = figure(x_range=[150, yr],
                y_range=[0, xr],
                x_axis_label='Wavelength Grid',
                y_axis_label='Pressure Grid, TOA ->',
                title="Optical Depth Fsed = 0.3",
                plot_width=300,
                plot_height=300)

    f6 = figure(x_range=[150, yr],
                y_range=[0, xr],
                x_axis_label='Wavelength Grid',
                y_axis_label='Pressure Grid, TOA ->',
                title="Optical Depth Fsed = 1",
                plot_width=300,
                plot_height=300)

    f01.image(image=[scat01],
              color_mapper=color_mapper,
              x=0,
              y=0,
              dh=xr,
              dw=yr)
    f1.image(image=[scat1], color_mapper=color_mapper, x=0, y=0, dh=xr, dw=yr)
    f6.image(image=[scat6], color_mapper=color_mapper, x=0, y=0, dh=xr, dw=yr)

    color_bar = ColorBar(color_mapper=color_mapper,
                         ticker=LogTicker(),
                         label_standoff=12,
                         border_line_color=None,
                         location=(0, 0))

    f01.add_layout(color_bar, 'right')
    output_file(plot_file)
    show(row(column(f01a, f1a, f6a), column(f01, f1, f6)))
Exemplo n.º 9
0
def bigScatter(data,
               precomputed=False,
               logscale=False,
               features=False,
               title="BigScatter",
               binsize=0.1,
               folder="",
               showpoint=False):
    """
    uses a binning method to display millions of points at the same time and showcase density.

    Does this in an interactive fashion

    Args:
    -----
      data: array like. the array containing the point location x,y or their location and density of bins (q,r,c)
      precomputed: bool whether or not the array has aleady been hexbined
      logscale: bool, whether or not the data is logscaled
      features: list[] if the matrix contains a feature column with feature information (names, values. for each bin/dot)
      title: str the title of the plot
      binsize: float the size of the bins
      showpoint: bool whether or not to display them as points or hexes.
      folder: str of location where to save the plot, won't save if empty

    Returns:
    ------
      the bokeh object
    """
    TOOLS = "wheel_zoom,zoom_in,zoom_out,box_zoom,undo,redo,reset,save,box_select,lasso_select,"
    names = [("count", "@c")]
    if features:
        names.append(('features', '@features'))
    if precomputed:
        TOOLS = "hover," + TOOLS
    p = figure(title=title,
               tools=TOOLS,
               tooltips=names if precomputed else None,
               match_aspect=True,
               background_fill_color='#440154')
    if precomputed:
        p.hex_tile(q="q",
                   r="r",
                   size=binsize,
                   line_color=None,
                   source=data,
                   hover_color="pink",
                   hover_alpha=0.8,
                   fill_color=linear_cmap('c', 'Viridis256', 0, max(data.c))
                   if not logscale else {
                       'field': 'c',
                       'transform': LogColorMapper('Viridis256')
                   })
    else:
        if features:
            print("we cannot yet process features on non precomputed version")
        r, bins = p.hexbin(data[:, 0],
                           data[:, 1],
                           line_color=None,
                           size=binsize,
                           hover_color="pink",
                           hover_alpha=0.8,
                           fill_color=linear_cmap('c', 'Viridis256', 0, None)
                           if not logscale else {
                               'field': 'c',
                               'transform': LogColorMapper('Viridis256')
                           })
    p.grid.visible = False
    if showpoint:
        p.circle(data[:, 0], data[:, 1], color="white", size=1)

    if not precomputed:
        p.add_tools(
            HoverTool(tooltips=names,
                      mode="mouse",
                      point_policy="follow_mouse",
                      renderers=[r] if not precomputed else None))

    try:
        show(p)
    except:
        show(p)
    if folder:
        save(p, folder + title.replace(' ', "_") + "_scatter.html")
        #export_png(p, filename=folder + title.replace(' ', "_") + "_scatter.png")

    return p
Exemplo n.º 10
0
'''Create Affordable Housing by Neighborhood Map'''

#read dataframe as geodataframe

gdf = gpd.GeoDataFrame(housing_nbhoods, geometry='geometry')

#convert geodataframe to geojson
geosource = GeoJSONDataSource(geojson=gdf.to_json())

# Define color palettes
palette = brewer['BuGn'][6]
palette = palette[::
                  -1]  # reverse order of colors so higher values have darker colors

# Instantiate LogColorMapper that exponentially maps numbers in a range, into a sequence of colors.
color_mapper = LogColorMapper(palette=palette, low=0, high=40)

# Define custom tick labels for color bar.
tick_labels = {
    1.35: '0',
    2.5: '1-2',
    4.6: '3-5',
    8.5: '6-10',
    16: '11-19',
    30: '20+'
}

# Create color bar
color_bar = ColorBar(title='Number of Housing Units',
                     color_mapper=color_mapper,
                     label_standoff=6,
Exemplo n.º 11
0
    def show_pdf_heatmap_compute_pdf_fun(self,
                                         compute_pdf_fun,
                                         title,
                                         paper=False,
                                         x_fixed=None,
                                         y_fixed=None,
                                         x_lim=None,
                                         y_lim=None,
                                         pdf_percentile_cut_off=None):
        dl = self.data_loader

        grid_size = 100
        grids = []
        for i, val in enumerate(x_fixed):
            if val is None:
                grids.append(np.linspace(dl.min_x[i], dl.max_x[i], grid_size))

        for i, val in enumerate(y_fixed):
            if val is None:
                grids.append(np.linspace(dl.min_y[i], dl.max_y[i], grid_size))

        mesh = np.meshgrid(*(grids))
        x_min = np.min(mesh[0])
        x_max = np.max(mesh[0])
        y_min = np.min(mesh[1])
        y_max = np.max(mesh[1])

        xs = []
        ys = []
        mesh_id = 0
        for i, val in enumerate(x_fixed):
            if val is None:
                xs.append(mesh[mesh_id].reshape(-1, 1))
                mesh_id += 1
            else:
                xs.append(np.ones((grid_size * grid_size, 1)) * val)

        for i, val in enumerate(y_fixed):
            if val is None:
                ys.append(mesh[mesh_id].reshape(-1, 1))
                mesh_id += 1
            else:
                ys.append(np.ones((grid_size * grid_size, 1)) * val)

        pdf = compute_pdf_fun(np.concatenate(xs, axis=1),
                              np.concatenate(ys, axis=1))

        print("integral: %f" % (np.sum(pdf) / (grid_size * grid_size)))
        if pdf_percentile_cut_off is None:
            high = np.max(pdf)
        else:
            high = np.percentile(pdf, 95)

        color_mapper = LogColorMapper(palette=gray(256),
                                      low=np.min(pdf),
                                      high=high)
        # color_mapper = LinearColorMapper(palette=gray(256),low=np.min(pdf), high=high)

        if paper:
            p = figure(x_range=(x_min, x_max),
                       y_range=(y_min, y_max),
                       tooltips=[("x", "$x"), ("y", "$y"),
                                 ("value", "@image")],
                       toolbar_location=None)
        else:
            p = figure(x_range=(x_min, x_max),
                       y_range=(y_min, y_max),
                       title=title,
                       tooltips=[("x", "$x"), ("y", "$y"),
                                 ("value", "@image")])

        # p.xaxis.axis_label = 'x%d' % x_i
        # p.yaxis.axis_label = 'y%d' % y_i

        p.xaxis.axis_label_text_font_size = "20pt"
        p.yaxis.axis_label_text_font_size = "20pt"

        p.xaxis.major_label_text_font_size = "20pt"
        p.yaxis.major_label_text_font_size = "20pt"

        p.image(image=[pdf.reshape(grid_size, grid_size)],
                x=x_min,
                y=y_min,
                dw=x_max - x_min,
                dh=y_max - y_min,
                color_mapper=color_mapper)

        # color_bar = ColorBar(color_mapper=color_mapper, ticker=LogTicker(),
        #                      label_standoff=12, border_line_color=None, location=(0, 0))

        # p.add_layout(color_bar, 'right')

        show(p)
        return p
Exemplo n.º 12
0
                         value=[opts[0]],
                         options=opts,
                         size=4,
                         name=column_name)

    multi_selector = MultiSelect(**selector_params)
    selectors.append(multi_selector)

source_json = geojson.loads(geo_source.geojson)

data_vals = [source_json[i]['properties']['data_val'] for i in range(len(source_json['features']))]
min_val = min(data_vals)
max_val = max(data_vals)

palette = sns.cubehelix_palette(128, dark=0).as_hex()
color_mapper = LogColorMapper(palette, low=min_val, high=max_val)

geomap_tooltips = [("Ticket Count", "@data_val")]

##create map
geomap_opts = {
    'background_fill_color': None,
    'plot_width': 800,
    'tooltips': geomap_tooltips,
    'tools': '',
    'x_axis_type': "mercator",
    'y_axis_type': "mercator",
    'x_range': (-9789724.66045665, -9742970.474323519),
    'y_range': (5107551.543942757, 5164699.247119262),
    'output_backend': 'webgl'
}
Exemplo n.º 13
0
def interactive_map(YKR_ID, travel_mode, output_folder=''):
    '''
    Creates interactive map based on YKR_ID and travel mode.
    :param YKR_ID: grid id
    :param travel_mode: walk, public or car
    :param output_folder: user defined folder
    :return: None
    '''
    data = to_geodataframe(files_by_YKR_ID([YKR_ID])[0])

    grid = metrop_access
    mode = {'car': 'car_r_t', 'public': 'pt_m_tt', 'walk': 'walk_t'}
    travel_m = mode[travel_mode]

    def get_poly_coords(row, geom, coord_type):
        """Returns the coordinates ('x' or 'y') of edges of a Polygon exterior"""

        # Parse the exterior of the coordinate
        exterior = row[geom].exterior

        if coord_type == 'x':
            # Get the x coordinates of the exterior
            return list(exterior.coords.xy[0])
        elif coord_type == 'y':
            # Get the y coordinates of the exterior
            return list(exterior.coords.xy[1])

    grid['x'] = grid.apply(get_poly_coords,
                           geom='geometry',
                           coord_type='x',
                           axis=1)
    grid['y'] = grid.apply(get_poly_coords,
                           geom='geometry',
                           coord_type='y',
                           axis=1)

    # Replace No Data values (-1) with large number (999)
    grid = grid.replace(-1, 999)

    # Classify our travel times into 5 minute classes until 200 minutes
    # Create a list of values where minumum value is 5, maximum value is 200 and step is 5.
    breaks = [x for x in range(5, 200, 5)]

    # Initialize the classifier and apply it
    classifier = ps.User_Defined.make(bins=breaks)
    pt_classif = data[[travel_m]].apply(classifier)

    # Rename the classified column
    pt_classif.columns = [travel_m + '_ud']

    # Join it back to the grid layer
    grid = grid.join(pt_classif)

    # Make a copy, drop the geometry column and create ColumnDataSource
    g_df = grid.drop('geometry', axis=1).copy()
    gsource = ColumnDataSource(g_df)
    # Initialize our figure
    p = figure(title="Travel times to " + YKR_ID + " by " + travel_mode)

    # Create the color mapper
    color_mapper = LogColorMapper(palette=palette)
    # Plot grid
    p.patches('x',
              'y',
              source=gsource,
              fill_color={
                  'field': travel_m + '_ud',
                  'transform': color_mapper
              },
              fill_alpha=1.0,
              line_color="black",
              line_width=0.05)

    # Save the figure
    outfp = output_folder + YKR_ID + '_interactive.html'
    save(p, outfp)
    return ''
Exemplo n.º 14
0
def visualize_matrix(rotor, matrix=None, frequency=None):
    """Visualize global matrix.

    This function gives some visualization of a given matrix, displaying
    values on a heatmap.

    Parameters
    ----------
    rotor: rs.Rotor
    
    matrix: str
        String for the desired matrix.
    frequency: float, optional
        Excitation frequency. Defaults to rotor speed.
    
    Returns
    -------
    fig: bokeh.figure
    """
    if frequency is None:
        frequency = rotor.w

    A = np.zeros((rotor.ndof, rotor.ndof))
    # E will store element's names and contributions to the global matrix
    E = np.zeros((rotor.ndof, rotor.ndof), dtype=np.object)

    M, N = E.shape
    for i in range(M):
        for j in range(N):
            E[i, j] = []

    for elm in rotor.elements:
        g_dofs = elm.dof_global_index()
        l_dofs = elm.dof_local_index()
        try:
            elm_matrix = getattr(elm, matrix)(frequency)
        except TypeError:
            elm_matrix = getattr(elm, matrix)()

        A[np.ix_(g_dofs, g_dofs)] += elm_matrix

        for l0, g0 in zip(l_dofs, g_dofs):
            for l1, g1 in zip(l_dofs, g_dofs):
                if elm_matrix[l0, l1] != 0:
                    E[g0, g1].append("\n" + elm.__class__.__name__ +
                                     f"(n={elm.n})" +
                                     f": {elm_matrix[l0, l1]:.2E}")

    # list for dofs -> ['x0', 'y0', 'alpha0', 'beta0', 'x1'...]
    dof_list = [0 for i in range(rotor.ndof)]

    for elm in rotor.elements:
        for k, v in elm.dof_global_index()._asdict().items():
            dof_list[v] = k

    data = {"row": [], "col": [], "value": [], "pos_value": [], "elements": []}
    for i, dof_row in enumerate(dof_list):
        for j, dof_col in enumerate(dof_list):
            data["row"].append(i)
            data["col"].append(j)
            data["value"].append(A[i, j])
            data["pos_value"].append(abs(A[i, j]))
            data["elements"].append(E[i, j])

    TOOLTIPS = """
        <div style="width:150px;">
            <span style="font-size: 10px;">Value: @value</span>
            <br>
            <span style="font-size: 10px;">Elements: </span>
            <br>
            <span style="font-size: 10px;">@elements</span>
        </div>
    """
    fig = figure(
        tools="hover",
        tooltips=TOOLTIPS,
        x_range=(-0.5, len(A) - 0.5),
        y_range=(len(A) - 0.5, -0.5),
        x_axis_location="above",
    )
    fig.plot_width = 500
    fig.plot_height = 500
    fig.grid.grid_line_color = None
    fig.axis.axis_line_color = None
    fig.axis.major_tick_line_color = None
    fig.axis.major_label_text_font_size = "7pt"
    fig.axis.major_label_standoff = 0
    fig.xaxis.ticker = [i for i in range(len(dof_list))]
    fig.yaxis.ticker = [i for i in range(len(dof_list))]

    dof_string_list = []
    sub = str.maketrans("0123456789", "₀₁₂₃₄₅₆₇₈₉")
    for d in dof_list:
        d = d.replace("alpha", "α")
        d = d.replace("beta", "β")
        d = d.replace("_", "")
        d = d.translate(sub)
        dof_string_list.append(d)

    fig.xaxis.major_label_overrides = {
        k: v
        for k, v in enumerate(dof_string_list)
    }
    fig.yaxis.major_label_overrides = {
        k: v
        for k, v in enumerate(dof_string_list)
    }

    mapper = LogColorMapper(palette=Viridis256, low=0, high=A.max())

    fig.rect(
        "row",
        "col",
        0.95,
        0.95,
        source=data,
        fill_color={
            "field": "pos_value",
            "transform": mapper
        },
        line_color=None,
    )

    return fig
Exemplo n.º 15
0
def plot_counties(df,
                  variable_to_distribute,
                  variables_to_display,
                  state=None,
                  logcolor=False):
    """Plots the distribution of a given variable across the given sttate
    
    Params
    ------
    df
        df is a data frame containing the county level data
    variable_to_distribute
        variable_to_distribute is the variable that you want to see across the state
    variables_to_display
        Variables to display on hovering over each county
    
    output: Bokeh plotting object
    """
    from bokeh.sampledata.us_counties import data as counties

    counties = {
        code: county
        for code, county in counties.items()
        if county["state"] == state.lower()
    }

    county_xs = [county["lons"] for county in counties.values()]
    county_ys = [county["lats"] for county in counties.values()]

    if variable_to_distribute in variables_to_display:
        variables_to_display.remove(variable_to_distribute)

    colors = palettes.RdBu11  #(n_colors)
    min_value = df[variable_to_distribute].min()
    max_value = df[variable_to_distribute].max()
    gran = (max_value - min_value) / float(len(colors))
    #print variable_to_distribute,state,min_value,max_value
    index_range = [min_value + x * gran for x in range(len(colors))]
    county_colors = []
    variable_dictionary = {}
    variable_dictionary["county_names"] = [
        county['name'] for county in counties.values()
    ]
    variable_dictionary["x"] = county_xs
    variable_dictionary["y"] = county_ys
    variable_dictionary[re.sub("[^\w]", "", variable_to_distribute)] = []
    for vd in variables_to_display:
        variable_dictionary[re.sub("[^\w]", "", vd)] = []
    for county_id in counties:
        StateCountyID = str(county_id[0]).zfill(2) + str(county_id[1]).zfill(3)
        if StateCountyID in list(df["Header-FIPSStandCtyCode"].values):
            temp_var = df[df["Header-FIPSStandCtyCode"] ==
                          StateCountyID][variable_to_distribute].values[0]
            #             if temp_var > 0.0:
            variable_dictionary[re.sub(
                "[^\w]", "", variable_to_distribute)].append(temp_var)
            for vd in variables_to_display:
                variable_dictionary[re.sub("[^\w]", "", vd)].append(
                    round(
                        float(df[df["Header-FIPSStandCtyCode"] ==
                                 StateCountyID][vd].values), 2))
            color_idx = list(temp_var - np.array(index_range)).index(
                min(x for x in list(temp_var - np.array(index_range))
                    if x >= 0))
            county_colors.append(colors[color_idx])
            '''
            else:
                variable_dictionary[re.sub("[^\w]","",variable_to_distribute)].append(0.0)
                county_colors.append("#A9A9A9")
                for vd in variables_to_display:
                    variable_dictionary[re.sub("[^\w]","",vd)].append(0.0)
            '''
        else:
            variable_dictionary[re.sub("[^\w]", "",
                                       variable_to_distribute)].append(0.0)
            county_colors.append("#A9A9A9")
            for vd in variables_to_display:
                variable_dictionary[re.sub("[^\w]", "", vd)].append(0.0)
        #print temp_var,counties[county_id]["name"]
    variable_dictionary["color"] = county_colors
    source = ColumnDataSource(data=variable_dictionary)
    TOOLS = "pan,wheel_zoom,box_zoom,reset,hover,save"

    if logcolor:
        mapper = LogColorMapper(palette=colors, low=min_value, high=max_value)
    else:
        mapper = LinearColorMapper(palette=colors,
                                   low=min_value,
                                   high=max_value)

    color_bar = ColorBar(color_mapper=mapper,
                         location=(0, 0),
                         orientation='horizontal',
                         title=variable_to_distribute,
                         ticker=FixedTicker(ticks=index_range))

    p = figure(title=variable_to_distribute,
               toolbar_location="left",
               tools=TOOLS,
               plot_width=1100,
               plot_height=700,
               x_axis_location=None,
               y_axis_location=None)

    p.patches('x',
              'y',
              source=source,
              fill_alpha=0.7,
              fill_color='color',
              line_color="#884444",
              line_width=2)

    hover = p.select_one(HoverTool)
    hover.point_policy = "follow_mouse"
    tool_tips = [("County ", "@county_names")]
    for key in variable_dictionary.keys():
        if key not in ["x", "y", "color", "county_names"]:
            tool_tips.append((key, "@" + re.sub("[^\w]", "", key) + "{1.11}"))
    hover.tooltips = tool_tips

    p.add_layout(color_bar, 'below')

    return p
Exemplo n.º 16
0
def geoplot(gdf_in,
            figure=None,
            figsize=None,
            title="",
            xlabel="Longitude",
            ylabel="Latitude",
            xlim=None,
            ylim=None,
            color="blue",
            colormap=None,
            colormap_uselog=False,
            colormap_range=None,
            category=None,
            dropdown=None,
            slider=None,
            slider_range=None,
            slider_name="",
            show_colorbar=True,
            xrange=None,
            yrange=None,
            hovertool=True,
            hovertool_columns=[],
            hovertool_string=None,
            simplify_shapes=None,
            tile_provider="CARTODBPOSITRON_RETINA",
            tile_provider_url=None,
            tile_attribution="",
            tile_alpha=1,
            panning=True,
            zooming=True,
            toolbar_location="right",
            show_figure=True,
            return_figure=True,
            return_html=False,
            legend=True,
            webgl=True,
            **kwargs):
    """Doc-String: TODO"""

    # Imports:
    import bokeh.plotting
    from bokeh.plotting import show
    from bokeh.models import (
        HoverTool,
        LogColorMapper,
        LinearColorMapper,
        GeoJSONDataSource,
        WheelZoomTool,
        ColorBar,
        BasicTicker,
        LogTicker,
        Dropdown,
        Slider,
        ColumnDataSource,
    )
    from bokeh.models.callbacks import CustomJS
    from bokeh.models.widgets import Dropdown
    from bokeh.palettes import all_palettes
    from bokeh.layouts import row, column

    # Make a copy of the input geodataframe:
    gdf = gdf_in.copy()

    # Check layertypes:
    if type(gdf) != pd.DataFrame:
        layertypes = []
        if "Point" in str(gdf.geom_type.unique()):
            layertypes.append("Point")
        if "Line" in str(gdf.geom_type.unique()):
            layertypes.append("Line")
        if "Polygon" in str(gdf.geom_type.unique()):
            layertypes.append("Polygon")
        if len(layertypes) > 1:
            raise Exception(
                "Can only plot GeoDataFrames/Series with single type of geometry (either Point, Line or Polygon). Provided is a GeoDataFrame/Series with types: %s"
                % layertypes)
    else:
        layertypes = ["Point"]

    # Get and check provided parameters for geoplot:
    figure_options = {
        "title": title,
        "x_axis_label": xlabel,
        "y_axis_label": ylabel,
        "plot_width": 600,
        "plot_height": 400,
        "toolbar_location": toolbar_location,
        "active_scroll": "wheel_zoom",
        "x_axis_type": "mercator",
        "y_axis_type": "mercator",
    }
    if not figsize is None:
        width, height = figsize
        figure_options["plot_width"] = width
        figure_options["plot_height"] = height
    if webgl:
        figure_options["output_backend"] = "webgl"

    if type(gdf) != pd.DataFrame:
        # Convert GeoDataFrame to Web Mercator Projection:
        gdf.to_crs({"init": "epsg:3857"}, inplace=True)

        # Simplify shapes if wanted:
        if isinstance(simplify_shapes, numbers.Number):
            if layertypes[0] in ["Line", "Polygon"]:
                gdf["geometry"] = gdf["geometry"].simplify(simplify_shapes)
        elif not simplify_shapes is None:
            raise ValueError(
                "<simplify_shapes> parameter only accepts numbers or None.")

    # Check for category, dropdown or slider (choropleth map column):
    category_options = 0
    if not category is None:
        category_options += 1
        category_columns = [category]
    if not dropdown is None:
        category_options += 1
        category_columns = dropdown
    if not slider is None:
        category_options += 1
        category_columns = slider
    if category_options > 1:
        raise ValueError(
            "Only one of <category>, <dropdown> or <slider> parameters is allowed to be used at once."
        )

    # Check for category (single choropleth plot):
    if category is None:
        pass
    elif isinstance(category, (list, tuple)):
        raise ValueError(
            "For <category>, please provide an existing single column of the GeoDataFrame."
        )
    elif category in gdf.columns:
        pass
    else:
        raise ValueError(
            "Could not find column '%s' in GeoDataFrame. For <category>, please provide an existing single column of the GeoDataFrame."
            % category)

    # Check for dropdown (multiple choropleth plots via dropdown selection):
    if dropdown is None:
        pass
    elif not isinstance(dropdown, (list, tuple)):
        raise ValueError(
            "For <dropdown>, please provide a list/tuple of existing columns of the GeoDataFrame."
        )
    else:
        for col in dropdown:
            if col not in gdf.columns:
                raise ValueError(
                    "Could not find column '%s' for <dropdown> in GeoDataFrame. "
                    % col)

    # Check for slider (multiple choropleth plots via slider selection):
    if slider is None:
        pass
    elif not isinstance(slider, (list, tuple)):
        raise ValueError(
            "For <slider>, please provide a list/tuple of existing columns of the GeoDataFrame."
        )
    else:
        for col in slider:
            if col not in gdf.columns:
                raise ValueError(
                    "Could not find column '%s' for <slider> in GeoDataFrame. "
                    % col)

        if not slider_range is None:
            if not isinstance(slider_range, Iterable):
                raise ValueError(
                    "<slider_range> has to be a type that is iterable like list, tuple, range, ..."
                )
            else:
                slider_range = list(slider_range)
                if len(slider_range) != len(slider):
                    raise ValueError(
                        "The number of elements in <slider_range> has to be the same as in <slider>."
                    )
                steps = []
                for i in range(len(slider_range) - 1):
                    steps.append(slider_range[i + 1] - slider_range[i])

                if len(set(steps)) > 1:
                    raise ValueError(
                        "<slider_range> has to have equal step size between each elements (like a range-object)."
                    )
                else:
                    slider_step = steps[0]
                    slider_start = slider_range[0]
                    slider_end = slider_range[-1]

    # Check colormap if either <category>, <dropdown> or <slider> is choosen:
    if category_options == 1:
        if colormap is None:
            colormap = blue_colormap
        elif isinstance(colormap, (tuple, list)):
            if len(colormap) > 1:
                pass
            else:
                raise ValueError(
                    "<colormap> only accepts a list/tuple of at least two colors or the name of one of the following predefined colormaps (see also https://bokeh.pydata.org/en/latest/docs/reference/palettes.html ): %s"
                    % (list(all_palettes.keys())))
        elif isinstance(colormap, str):
            if colormap in all_palettes:
                colormap = all_palettes[colormap]
                colormap = colormap[max(colormap.keys())]
            else:
                raise ValueError(
                    "Could not find <colormap> with name %s. The following predefined colormaps are supported (see also https://bokeh.pydata.org/en/latest/docs/reference/palettes.html ): %s"
                    % (colormap, list(all_palettes.keys())))
        else:
            raise ValueError(
                "<colormap> only accepts a list/tuple of at least two colors or the name of one of the following predefined colormaps (see also https://bokeh.pydata.org/en/latest/docs/reference/palettes.html ): %s"
                % (list(all_palettes.keys())))
    else:
        if isinstance(color, str):
            colormap = [color]
        else:
            raise ValueError(
                "<color> has to be a string specifying the fill_color of the map glyph."
            )

    # Check xlim & ylim:
    if xlim is not None:
        if isinstance(xlim, (tuple, list)):
            if len(xlim) == 2:
                from pyproj import Proj, transform

                inProj = Proj(init="epsg:4326")
                outProj = Proj(init="epsg:3857")
                xmin, xmax = xlim
                for _ in [xmin, xmax]:
                    if not -180 < _ <= 180:
                        raise ValueError(
                            "Limits for x-axis (=Longitude) have to be between -180 and 180."
                        )
                if not xmin < xmax:
                    raise ValueError("xmin has to be smaller than xmax.")
                xmin = transform(inProj, outProj, xmin, 0)[0]
                xmax = transform(inProj, outProj, xmax, 0)[0]
                figure_options["x_range"] = (xmin, xmax)
            else:
                raise ValueError(
                    "Limits for x-axis (=Longitude) have to be of form [xmin, xmax] with values between -180 and 180."
                )
        else:
            raise ValueError(
                "Limits for x-axis (=Longitude) have to be of form [xmin, xmax] with values between -180 and 180."
            )
    if ylim is not None:
        if isinstance(ylim, (tuple, list)):
            if len(ylim) == 2:
                from pyproj import Proj, transform

                inProj = Proj(init="epsg:4326")
                outProj = Proj(init="epsg:3857")
                ymin, ymax = ylim
                for _ in [ymin, ymax]:
                    if not -90 < _ <= 90:
                        raise ValueError(
                            "Limits for y-axis (=Latitude) have to be between -90 and 90."
                        )
                if not ymin < ymax:
                    raise ValueError("ymin has to be smaller than ymax.")
                ymin = transform(inProj, outProj, 0, ymin)[1]
                ymax = transform(inProj, outProj, 0, ymax)[1]
                figure_options["y_range"] = (ymin, ymax)
            else:
                raise ValueError(
                    "Limits for y-axis (=Latitude) have to be of form [ymin, ymax] with values between -90 and 90."
                )
        else:
            raise ValueError(
                "Limits for y-axis (=Latitude) have to be of form [ymin, ymax] with values between -90 and 90."
            )

    # Create Figure to draw:
    old_layout = None
    if figure is None:
        p = bokeh.plotting.figure(**figure_options)

        # Add Tile Source as Background:
        p = _add_backgroundtile(p, tile_provider, tile_provider_url,
                                tile_attribution, tile_alpha)

    elif isinstance(figure, type(bokeh.plotting.figure())):
        p = figure
    elif isinstance(figure, type(column())):
        old_layout = figure
        p = _get_figure(old_layout)
    else:
        raise ValueError(
            "Parameter <figure> has to be of type bokeh.plotting.figure or bokeh.layouts.column."
        )

    # Get ridd of zoom on axes:
    for t in p.tools:
        if type(t) == WheelZoomTool:
            t.zoom_on_axis = False

    # Hide legend if wanted:
    legend_input = legend
    if isinstance(legend, str):
        pass
    else:
        legend = "GeoLayer"

    # Define colormapper:
    if len(colormap) == 1:
        kwargs["fill_color"] = colormap[0]

    elif not category is None:
        # Check if category column is numerical:
        if not issubclass(gdf[category].dtype.type, np.number):
            raise NotImplementedError(
                "<category> plot only yet implemented for numerical columns. Column '%s' is not numerical."
                % category)

        field = category
        colormapper_options = {"palette": colormap}
        if not colormap_range is None:
            if not isinstance(colormap_range, (tuple, list)):
                raise ValueError(
                    "<colormap_range> can only be 'None' or a tuple/list of form (min, max)."
                )
            elif len(colormap_range) == 2:
                colormapper_options["low"] = colormap_range[0]
                colormapper_options["high"] = colormap_range[1]
        else:
            colormapper_options["low"] = gdf[field].min()
            colormapper_options["high"] = gdf[field].max()
        if colormap_uselog:
            colormapper = LogColorMapper(**colormapper_options)
        else:
            colormapper = LinearColorMapper(**colormapper_options)
        kwargs["fill_color"] = {"field": "Colormap", "transform": colormapper}
        if not isinstance(legend, str):
            legend = str(field)

    elif not dropdown is None:
        # Check if all columns in dropdown selection are numerical:
        for col in dropdown:
            if not issubclass(gdf[col].dtype.type, np.number):
                raise NotImplementedError(
                    "<dropdown> plot only yet implemented for numerical columns. Column '%s' is not numerical."
                    % col)

        field = dropdown[0]
        colormapper_options = {"palette": colormap}
        if not colormap_range is None:
            if not isinstance(colormap_range, (tuple, list)):
                raise ValueError(
                    "<colormap_range> can only be 'None' or a tuple/list of form (min, max)."
                )
            elif len(colormap_range) == 2:
                colormapper_options["low"] = colormap_range[0]
                colormapper_options["high"] = colormap_range[1]
        else:
            colormapper_options["low"] = gdf[dropdown].min().min()
            colormapper_options["high"] = gdf[dropdown].max().max()
        if colormap_uselog:
            colormapper = LogColorMapper(**colormapper_options)
        else:
            colormapper = LinearColorMapper(**colormapper_options)
        kwargs["fill_color"] = {"field": "Colormap", "transform": colormapper}
        legend = " " + field

    elif not slider is None:
        # Check if all columns in dropdown selection are numerical:
        for col in slider:
            if not issubclass(gdf[col].dtype.type, np.number):
                raise NotImplementedError(
                    "<slider> plot only yet implemented for numerical columns. Column '%s' is not numerical."
                    % col)

        field = slider[0]
        colormapper_options = {"palette": colormap}
        if not colormap_range is None:
            if not isinstance(colormap_range, (tuple, list)):
                raise ValueError(
                    "<colormap_range> can only be 'None' or a tuple/list of form (min, max)."
                )
            elif len(colormap_range) == 2:
                colormapper_options["low"] = colormap_range[0]
                colormapper_options["high"] = colormap_range[1]
        else:
            colormapper_options["low"] = gdf[slider].min().min()
            colormapper_options["high"] = gdf[slider].max().max()
        if colormap_uselog:
            colormapper = LogColorMapper(**colormapper_options)
        else:
            colormapper = LinearColorMapper(**colormapper_options)
        kwargs["fill_color"] = {"field": "Colormap", "transform": colormapper}
        if not isinstance(legend, str):
            legend = "Geolayer"

    # Check that only hovertool_columns or hovertool_string is used:
    if isinstance(hovertool_columns, (list, tuple, str)):
        if len(hovertool_columns) > 0 and hovertool_string is not None:
            raise ValueError(
                "Either <hovertool_columns> or <hovertool_string> can be used, but not both at the same time."
            )
    else:
        raise ValueError(
            "<hovertool_columns> has to be a list of columns of the GeoDataFrame or the string 'all'."
        )

    if hovertool_string is not None:
        hovertool_columns = "all"

    # Check for Hovertool columns:
    if hovertool:
        if not isinstance(hovertool_columns, (list, tuple)):
            if hovertool_columns == "all":
                hovertool_columns = list(
                    filter(lambda col: col != "geometry", gdf.columns))
            else:
                raise ValueError(
                    "<hovertool_columns> has to be a list of columns of the GeoDataFrame or the string 'all'."
                )
        elif len(hovertool_columns) == 0:
            if not category is None:
                hovertool_columns = [category]
            elif not dropdown is None:
                hovertool_columns = dropdown
            elif not slider is None:
                hovertool_columns = slider
            else:
                hovertool_columns = []
        else:
            for col in hovertool_columns:
                if col not in gdf.columns:
                    raise ValueError(
                        "Could not find columns '%s' in GeoDataFrame. <hovertool_columns> has to be a list of columns of the GeoDataFrame or the string 'all'."
                        % col)
    else:
        if category is None:
            hovertool_columns = []
        else:
            hovertool_columns = [category]

    # Reduce DataFrame to needed columns:
    if type(gdf) == pd.DataFrame:
        gdf["Geometry"] = 0
        additional_columns = ["x", "y"]
    else:
        additional_columns = ["geometry"]
    for kwarg, value in kwargs.items():
        if isinstance(value, Hashable):
            if value in gdf.columns:
                additional_columns.append(value)
    if category_options == 0:
        gdf = gdf[list(set(hovertool_columns) | set(additional_columns))]
    else:
        gdf = gdf[list(
            set(hovertool_columns) | set(category_columns)
            | set(additional_columns))]
        gdf["Colormap"] = gdf[field]
        field = "Colormap"

    # Create GeoJSON DataSource for Plot:
    if type(gdf) != pd.DataFrame:
        geo_source = GeoJSONDataSource(geojson=gdf.to_json())
    else:
        geo_source = gdf

    # Draw Glyph on Figure:
    layout = None
    if "Point" in layertypes:
        if "line_color" not in kwargs:
            kwargs["line_color"] = kwargs["fill_color"]
        glyph = p.scatter(x="x",
                          y="y",
                          source=geo_source,
                          legend=legend,
                          **kwargs)

    if "Line" in layertypes:
        if "line_color" not in kwargs:
            kwargs["line_color"] = kwargs["fill_color"]
            del kwargs["fill_color"]
        glyph = p.multi_line(xs="xs",
                             ys="ys",
                             source=geo_source,
                             legend=legend,
                             **kwargs)

    if "Polygon" in layertypes:

        if "line_color" not in kwargs:
            kwargs["line_color"] = "black"

        # Creates from a geoDataFrame with Polygons and Multipolygons a Pandas DataFrame
        # with x any y columns specifying the geometry of the Polygons:
        geo_source = ColumnDataSource(convert_geoDataFrame_to_patches(gdf))

        # Plot polygons:
        glyph = p.patches(xs="__x__",
                          ys="__y__",
                          source=geo_source,
                          legend=legend,
                          **kwargs)

    # Add hovertool:
    if hovertool and (category_options == 1 or len(hovertool_columns) > 0):
        my_hover = HoverTool(renderers=[glyph])
        if hovertool_string is None:
            my_hover.tooltips = [(str(col), "@{%s}" % col)
                                 for col in hovertool_columns]
        else:
            my_hover.tooltips = hovertool_string
        p.add_tools(my_hover)

    # Add colorbar:
    if show_colorbar and category_options == 1:
        colorbar_options = {
            "color_mapper": colormapper,
            "label_standoff": 12,
            "border_line_color": None,
            "location": (0, 0),
        }
        if colormap_uselog:
            colorbar_options["ticker"] = LogTicker()

        colorbar = ColorBar(**colorbar_options)

        p.add_layout(colorbar, "right")

    # Add Dropdown Widget:
    if not dropdown is None:
        # Define Dropdown widget:
        dropdown_widget = Dropdown(label="Select Choropleth Layer",
                                   menu=list(zip(dropdown, dropdown)))

        # Define Callback for Dropdown widget:
        callback = CustomJS(
            args=dict(
                dropdown_widget=dropdown_widget,
                geo_source=geo_source,
                legend=p.legend[0].items[0],
            ),
            code="""

                //Change selection of field for Colormapper for choropleth plot:
                geo_source.data["Colormap"] = geo_source.data[dropdown_widget.value];
                geo_source.change.emit();

                //Change label of Legend:
                legend.label["value"] = " " + dropdown_widget.value;

                            """,
        )
        dropdown_widget.js_on_change("value", callback)

        # Add Dropdown widget above the plot:
        if old_layout is None:
            layout = column(dropdown_widget, p)
        else:
            layout = column(dropdown_widget, old_layout)

    # Add Slider Widget:
    if not slider is None:

        if slider_range is None:
            slider_start = 0
            slider_end = len(slider) - 1
            slider_step = 1

        value2name = ColumnDataSource({
            "Values":
            np.arange(slider_start, slider_end + slider_step, slider_step),
            "Names":
            slider,
        })

        # Define Slider widget:
        slider_widget = Slider(
            start=slider_start,
            end=slider_end,
            value=slider_start,
            step=slider_step,
            title=slider_name,
        )

        # Define Callback for Slider widget:
        callback = CustomJS(
            args=dict(
                slider_widget=slider_widget,
                geo_source=geo_source,
                value2name=value2name,
            ),
            code="""

                //Change selection of field for Colormapper for choropleth plot:
                var slider_value = slider_widget.value;
                for(i=0; i<value2name.data["Names"].length; i++)
                    {
                    if (value2name.data["Values"][i] == slider_value)
                        {
                         var name = value2name.data["Names"][i];
                         }

                    }
                geo_source.data["Colormap"] = geo_source.data[name];
                geo_source.change.emit();

                            """,
        )
        slider_widget.js_on_change("value", callback)

        # Add Slider widget above the plot:
        if old_layout is None:
            layout = column(slider_widget, p)
        else:
            layout = column(slider_widget, old_layout)

    # Hide legend if user wants:
    if legend_input is False:
        p.legend.visible = False

    # Set click policy for legend:
    p.legend.click_policy = "hide"

    # Set panning option:
    if panning is False:
        p.toolbar.active_drag = None

    # Set zooming option:
    if zooming is False:
        p.toolbar.active_scroll = None

    # Display plot and if wanted return plot:
    if layout is None:
        if old_layout is None:
            layout = p
        else:
            layout = old_layout

    # Display plot if wanted
    if show_figure:
        show(layout)

    # Return as (embeddable) HTML if wanted:
    if return_html:
        return embedded_html(layout)

    # Return plot:
    if return_figure:
        return layout
def selection_tool(df_pipes, df_users):
    # Create the color mapper
    color_mapper = LogColorMapper(palette=palette)

    df_pipes['x'] = df_pipes.apply(getLineCoords,
                                   geom='geometry',
                                   coord_type='x',
                                   axis=1)
    df_pipes['y'] = df_pipes.apply(getLineCoords,
                                   geom='geometry',
                                   coord_type='y',
                                   axis=1)
    df_nodes = df_pipes[["ID", "node_ups"]].copy()
    df_nodes['x'] = df_pipes.apply(getLineCoords,
                                   geom='node_ups',
                                   coord_type='x',
                                   axis=1)
    df_nodes['y'] = df_pipes.apply(getLineCoords,
                                   geom='node_ups',
                                   coord_type='y',
                                   axis=1)

    df_users['x'] = df_users.apply(getLineCoords,
                                   geom='geometry',
                                   coord_type='x',
                                   axis=1)
    df_users['y'] = df_users.apply(getLineCoords,
                                   geom='geometry',
                                   coord_type='y',
                                   axis=1)

    #pipes source
    m_df = df_pipes[["ID", "x", "y", "BUILD_DATE", "DIMENSION"]].copy()
    msource = ColumnDataSource(m_df)

    #defining x,y and ID for upstream nodes and source
    n_df = df_nodes[['ID', "x", "y"]].copy()

    def node_coords(n_df):
        n_df['x'] = n_df['x'].str.get(0)
        n_df['y'] = n_df['y'].str.get(0)
        #n_df['ID'] = n_df['ID'].str.get(0)
        n_df['x'] = n_df['x'].astype(float)
        n_df['y'] = n_df['y'].astype(float)
        n_df['x'] = n_df['x'].apply(lambda x: '{:.2f}'.format(x))
        n_df['y'] = n_df['y'].apply(lambda x: '{:.2f}'.format(x))
        return (n_df)

    n_df = node_coords(n_df)

    x = list(n_df['x'])
    y = list(n_df['y'])
    ID = list(n_df['ID'])
    s1 = ColumnDataSource(data=dict(x=x, y=y))

    #uporabniki
    u_df = df_users[["x", "y"]].copy()
    u_df['x'] = u_df['x'].str.get(0)
    u_df['y'] = u_df['y'].str.get(0)
    usource = ColumnDataSource(u_df)

    #figure
    p1 = figure(plot_width=1000,
                plot_height=1200,
                tools=["lasso_select", 'wheel_zoom', 'box_zoom'],
                title="District heating")

    #ploting upstream nodes
    p1.circle('x', 'y', source=usource, alpha=0.6, size=10)
    p1.circle('x', 'y', source=s1, alpha=0.6)

    #second figure
    s2 = ColumnDataSource(data=dict(
        x=[],
        y=[],
    ))
    p2 = figure(plot_width=1000,
                plot_height=1200,
                x_range=(0, 1),
                y_range=(0, 1),
                tools="",
                title="Selected pipes")
    p2.circle('x', 'y', source=s2, alpha=0.6)

    columns = [
        TableColumn(field="x", title="X axis"),
        TableColumn(field="y", title="Y axis"),
    ]

    table = DataTable(source=s2, columns=columns, width=300, height=500)

    s1.selected.js_on_change(
        'indices',
        CustomJS(args=dict(s1=s1, s2=s2, table=table),
                 code="""
            var inds = cb_obj.indices;
            var d1 = s1.data;
            var d2 = s2.data;
            d2['x'] = []
            d2['y'] = []
            var text = "";
            for (var i = 0; i < inds.length; i++) {
                d2['x'].push(d1['x'][inds[i]])
                d2['y'].push(d1['y'][inds[i]])
                text = text  + d1['x'][inds[i]] + "," + d1['y'][inds[i]] + '\\n'

            }


            var filename = "SelectedPipes.txt";

            var blob = new Blob([text], {type:'text/plain'});

            var elementExists = document.getElementById("link");

            if(elementExists){
                elementExists.href = window.URL.createObjectURL(blob);
            }
            else{
                var link = document.createElement("a");
                link.setAttribute("id", "link");
                link.download = filename;
                link.innerHTML = "Select Pipes";
                link.href = window.URL.createObjectURL(blob);
                document.body.appendChild(link);
            }




            s2.change.
            emit();
            table.change.emit();
        """))

    p1.multi_line('x', 'y', source=msource, color='gray', line_width=3)

    my_hover = HoverTool()
    my_hover.tooltips = [('ID', '@ID'), ('Diameter', '@DIMENSION'),
                         ('Year', '@BUILD_DATE'), ('Next', '@PipeNext'),
                         ('Previous', '@Previous')]
    p1.add_tools(my_hover)

    layout = row(p1, table)

    show(layout, browser=None, new='tab', notebook_handle=False)
Exemplo n.º 18
0
def make_tpf_figure_elements(tpf,
                             tpf_source,
                             pedestal=0,
                             fiducial_frame=None,
                             plot_width=370,
                             plot_height=340):
    """Returns the lightcurve figure elements.

    Parameters
    ----------
    tpf : TargetPixelFile
        TPF to show.
    tpf_source : bokeh.plotting.ColumnDataSource
        TPF data source.
    fiducial_frame: int
        The tpf slice to start with by default, it is assumed the WCS
        is exact for this frame.
    pedestal: float
        A scalar value to be added to the TPF flux values, often to avoid
        taking the log of a negative number in colorbars


    Returns
    -------
    fig, stretch_slider : bokeh.plotting.figure.Figure, RangeSlider
    """
    if tpf.mission in ['Kepler', 'K2']:
        title = 'Pixel data (CCD {}.{})'.format(tpf.module, tpf.output)
    elif tpf.mission == 'TESS':
        title = 'Pixel data (Camera {}.{})'.format(tpf.camera, tpf.ccd)
    else:
        title = "Pixel data"

    fig = figure(plot_width=plot_width,
                 plot_height=plot_height,
                 x_range=(tpf.column, tpf.column + tpf.shape[2]),
                 y_range=(tpf.row, tpf.row + tpf.shape[1]),
                 title=title,
                 tools='tap,box_select,wheel_zoom,reset',
                 toolbar_location="below",
                 border_fill_color="whitesmoke")

    fig.yaxis.axis_label = 'Pixel Row Number'
    fig.xaxis.axis_label = 'Pixel Column Number'

    vlo, lo, hi, vhi = np.nanpercentile(tpf.flux - pedestal,
                                        [0.2, 1, 95, 99.8])
    vstep = (np.log10(vhi) - np.log10(vlo)) / 300.0  # assumes counts >> 1.0!
    color_mapper = LogColorMapper(palette="Viridis256", low=lo, high=hi)

    fig.image([tpf.flux[fiducial_frame, :, :] - pedestal],
              x=tpf.column,
              y=tpf.row,
              dw=tpf.shape[2],
              dh=tpf.shape[1],
              dilate=True,
              color_mapper=color_mapper,
              name="tpfimg")

    # The colorbar will update with the screen stretch slider
    # The colorbar margin increases as the length of the tick labels grows.
    # This colorbar share of the plot window grows, shrinking plot area.
    # This effect is known, some workarounds might work to fix the plot area:
    # https://github.com/bokeh/bokeh/issues/5186
    color_bar = ColorBar(color_mapper=color_mapper,
                         ticker=LogTicker(desired_num_ticks=8),
                         label_standoff=-10,
                         border_line_color=None,
                         location=(0, 0),
                         background_fill_color='whitesmoke',
                         major_label_text_align='left',
                         major_label_text_baseline='middle',
                         title='e/s',
                         margin=0)
    fig.add_layout(color_bar, 'right')

    color_bar.formatter = PrintfTickFormatter(format="%14u")

    if tpf_source is not None:
        fig.rect('xx',
                 'yy',
                 1,
                 1,
                 source=tpf_source,
                 fill_color='gray',
                 fill_alpha=0.4,
                 line_color='white')

    # Configure the stretch slider and its callback function
    stretch_slider = RangeSlider(start=np.log10(vlo),
                                 end=np.log10(vhi),
                                 step=vstep,
                                 title='Screen Stretch (log)',
                                 value=(np.log10(lo), np.log10(hi)),
                                 orientation='horizontal',
                                 width=200,
                                 direction='ltr',
                                 show_value=True,
                                 sizing_mode='fixed',
                                 name='tpfstretch')

    def stretch_change_callback(attr, old, new):
        """TPF stretch slider callback."""
        fig.select('tpfimg')[0].glyph.color_mapper.high = 10**new[1]
        fig.select('tpfimg')[0].glyph.color_mapper.low = 10**new[0]

    stretch_slider.on_change('value', stretch_change_callback)

    return fig, stretch_slider
Exemplo n.º 19
0
def scatter_foreigners_totalMarketValue():
    df = pandas_get_teams()
    '''
    Columns
    ['Id', 'Name', 'Nickname', 'Squad', 'AverageAge', 'NumberofForeigners', 'TotalMarketValue', 'AverageMarketValue', 'League']
    '''

    palette = [
        "#053061", "#2166ac", "#4393c3", "#92c5de", "#d1e5f0", "#f7f7f7",
        "#fddbc7", "#f4a582", "#d6604d", "#b2182b", "#67001f"
    ]
    AverageAge = df["AverageAge"]
    low = min(AverageAge)
    high = max(AverageAge)
    AverageAge_inds = [int(10 * (x - low) / (high - low)) for x in AverageAge
                       ]  #gives items in colors a value from 0-10

    df['age_colors'] = [palette[i] for i in AverageAge_inds]

    color_mapper = LogColorMapper(palette=palette, low=low, high=high)

    source = ColumnDataSource(df)

    #Create Figure
    p = figure(width=900)

    #Configure Circles
    p.circle(x='TotalMarketValue',
             y='NumberofForeigners',
             source=source,
             size=10,
             color='age_colors',
             line_color="black",
             fill_alpha=0.8)

    #Configure Labels

    #Configure Color of Average Ages
    color_bar = ColorBar(color_mapper=color_mapper,
                         ticker=LogTicker(),
                         label_standoff=12,
                         border_line_color=None,
                         location=(0, 0))
    p.add_layout(color_bar, 'right')

    #Configure Graph Titles and Axises
    p.title.text = 'Number of Foreign League Players and Total Market Value (Colored By Average Age)'
    p.xaxis.axis_label = 'Total Market Value'
    p.yaxis.axis_label = 'Number of Foreign Players Per Squad'

    #Hover Tools
    hover = HoverTool()
    hover.tooltips = [('Name', '@Name'), ('Average Age', '@AverageAge'),
                      ('Number of Foreign Players', '@NumberofForeigners'),
                      ('League', '@League')]

    p.add_tools(hover)

    output_file("docs/scatterPlot.html")

    show(p)
Exemplo n.º 20
0
def xrayvis_app(doc):
    def load_wav_cb(attr, old, new):
        '''Handle selection of audio file to be loaded.'''
        if new == '':
            return
        global wavname
        global snd
        spkr, fname = os.path.split(new)
        wavname = get_cached_fname(
            fname,
            f'https://linguistics.berkeley.edu/phonapps/xray_microbeam_database/{spkr}',
            spkr)
        #        wavname = new
        if not wavname.endswith('.wav'):
            return
        snd = parselmouth.Sound(wavname)
        srcdf = pd.DataFrame(
            dict(
                seconds=snd.ts().astype(np.float32),
                ch0=snd.values[0, :].astype(np.float32),
            ))
        #! TODO: do file caching
        phdf = allphdf.loc[allphdf.wavpath == new, :].copy()
        phdf['t1'] = phdf['t1'].astype(np.float32)
        wddf = allwddf.loc[allwddf.wavpath == new, :].copy()
        wddf['t1'] = wddf['t1'].astype(np.float32)
        uttdiv.text = '<b>Utterance:</b> ' + ' '.join(
            wddf.word.str.replace('sp', '')).strip()
        phwddf = pd.merge_asof(phdf[['t1', 'phone']],
                               wddf[['t1', 'word']],
                               on='t1',
                               suffixes=['_ph', '_wd'])
        # TODO: separate t1_ph and t1_wd columns
        srcdf = pd.merge_asof(srcdf, phwddf, left_on='seconds', right_on='t1')
        srcdf[['phone', 'word']] = srcdf[['phone', 'word']].fillna('')
        srcdf = srcdf.drop('t1', axis='columns')
        dfs['srcdf'] = srcdf
        source.data = srcdf
        tngsource.data = {'x': [], 'y': []}
        othsource.data = {'x': [], 'y': []}
        timesource.data = {k: [] for k in timesource.data.keys()}
        lasttngtimesource.data = {'x': [], 'y': []}
        lastothtimesource.data = {'x': [], 'y': []}
        playvisbtn.channels = channels
        playvisbtn.disabled = False
        playselbtn.channels = channels
        playselbtn.disabled = False
        playvisbtn.fs = snd.sampling_frequency
        playvisbtn.start = snd.start_time
        playvisbtn.end = snd.end_time
        playselbtn.fs = snd.sampling_frequency
        playselbtn.start = 0.0
        playselbtn.end = 0.0
        selbox.left = 0.0
        selbox.right = 0.0
        selbox.visible = False
        cursor.location = 0.0
        cursor.visible = False
        ch0.visible = True
        update_sgram()
        load_artic()
        set_limits(0.0, srcdf['seconds'].max())

    def load_artic():
        '''Load articulation data.'''
        trace.title.text = 'Static trace'
        traj.title.text = 'Trajectories'
        tngfile = os.path.splitext(wavname)[0] + '.txy'
        palfile = os.path.join(os.path.dirname(wavname), 'PAL.DAT')
        phafile = os.path.join(os.path.dirname(wavname), 'PHA.DAT')
        tngdf = pd.read_csv(tngfile,
                            sep='\t',
                            names=[
                                'sec', 'ULx', 'ULy', 'LLx', 'LLy', 'T1x',
                                'T1y', 'T2x', 'T2y', 'T3x', 'T3y', 'T4x',
                                'T4y', 'MIx', 'MIy', 'MMx', 'MMy'
                            ])
        # Convert to seconds
        tngdf['sec'] = tngdf['sec'] / 1e6
        tngdf = tngdf.set_index(['sec'])
        # Convert to mm
        tngdf[[
            'ULx', 'ULy', 'LLx', 'LLy', 'T1x', 'T1y', 'T2x', 'T2y', 'T3x',
            'T3y', 'T4x', 'T4y', 'MIx', 'MIy', 'MMx', 'MMy'
        ]] = tngdf[[
            'ULx', 'ULy', 'LLx', 'LLy', 'T1x', 'T1y', 'T2x', 'T2y', 'T3x',
            'T3y', 'T4x', 'T4y', 'MIx', 'MIy', 'MMx', 'MMy'
        ]] * 1e-3
        # Find global x/y max/min in this recording to set axis limits.
        # Exclude bad values (1000000 in data file; 1000 mm in scaled dataframe).
        cmpdf = tngdf[tngdf < badval]
        xmax = np.max(
            np.max(
                cmpdf[['ULx', 'LLx', 'T1x', 'T2x', 'T3x', 'T4x', 'MIx',
                       'MMx']]))
        xmin = np.min(
            np.min(
                cmpdf[['ULx', 'LLx', 'T1x', 'T2x', 'T3x', 'T4x', 'MIx',
                       'MMx']]))
        ymax = np.max(
            np.max(
                cmpdf[['ULy', 'LLy', 'T1y', 'T2y', 'T3y', 'T4y', 'MIy',
                       'MMy']]))
        ymin = np.min(
            np.min(
                cmpdf[['ULy', 'LLy', 'T1y', 'T2y', 'T3y', 'T4y', 'MIy',
                       'MMy']]))

        paldf = pd.read_csv(palfile, sep='\s+', header=None, names=['x', 'y'])
        paldf = paldf * 1e-3
        palsource.data = {'x': paldf['x'], 'y': paldf['y']}
        phadf = pd.read_csv(phafile, sep='\s+', header=None, names=['x', 'y'])
        phadf = phadf * 1e-3
        phasource.data = {'x': phadf['x'], 'y': phadf['y']}

        xmin = np.min([xmin, np.min(paldf['x']), np.min(phadf['x'])])
        xmax = np.max([xmax, np.max(paldf['x']), np.max(phadf['x'])])
        ymin = np.min([ymin, np.min(paldf['y']), np.min(phadf['y'])])
        ymax = np.max([ymax, np.max(paldf['y']), np.max(phadf['y'])])
        xsz = xmax - xmin
        ysz = ymax - ymin
        xrng = [xmin - (xsz * 0.05), xmax + (xsz * 0.05)]
        yrng = [ymin - (ysz * 0.05), ymax + (ysz * 0.05)]
        dfs['tngdf'] = tngdf
        dfs['paldf'] = paldf
        dfs['phadf'] = phadf

    def update_sgram():
        '''Update spectrogram based on current values.'''
        if snd.end_time < 15:
            sgrams[0] = snd2specgram(snd, 0.005)
            specsource.data = dict(
                sgram0=[sgrams[0].values.astype(np.float32)])
            spec0img.glyph.dw = sgrams[0].x_grid().max()
            spec0img.glyph.dh = sgrams[0].y_grid().max()
            spec0cmap.low = _low_thresh()
            spec0.visible = True
        else:
            specsource.data = dict(sgram0=[])
            spec0.visible = False

    def update_trace():
        '''Update the static trace at the cursor time.'''
        trace.title.text = f'Static trace ({cursor.location:0.4f})'
        tidx = dfs['tngdf'].index.get_loc(cursor.location, method='nearest')
        row = dfs['tngdf'].iloc[tidx]
        tngsource.data = {
            'x': [row.T1x, row.T2x, row.T3x, row.T4x],
            'y': [row.T1y, row.T2y, row.T3y, row.T4y]
        }
        othsource.data = {
            'x': [row.ULx, row.LLx, row.MIx, row.MMx],
            'y': [row.ULy, row.LLy, row.MIy, row.MMy]
        }

    def update_traj():
        '''Update the trajectories during the selected time range.'''
        traj.title.text = f'Trajectories ({selbox.left:0.4f} - {selbox.right:0.4f})'
        seldf = dfs['tngdf'].loc[(dfs['tngdf'].index >= selbox.left)
                                 & (dfs['tngdf'].index <= selbox.right)]
        dfs['seldf'] = seldf
        pts = ('T1x', 'T1y', 'T2x', 'T2y', 'T3x', 'T3y', 'T4x', 'T4y', 'ULx',
               'ULy', 'LLx', 'LLy', 'MIx', 'MIy', 'MMx', 'MMy')
        # Create a list of line segments for each tracked element.
        newdata = {
            pt: list(np.squeeze(np.dstack((seldf[pt].iloc[:-1], seldf[pt].iloc[1:])))) \
                for pt in pts
        }
        newdata['color'] = np.arange(1, len(seldf))
        newdata['sec'] = seldf.index[1:]
        timesource.data = newdata
        anim_slider.start = seldf.index[0]
        anim_slider.end = seldf.index[-1]
        anim_slider.step = np.diff(newdata['sec']).mean()
        anim_slider.value = anim_slider.end
        anim_slider.disabled = False
        anim_btn.disabled = False
        lastrow = seldf.iloc[-1]
        lasttngtimesource.data = {
            'x': [lastrow.T1x, lastrow.T2x, lastrow.T3x, lastrow.T4x],
            'y': [lastrow.T1y, lastrow.T2y, lastrow.T3y, lastrow.T4y]
        }
        lastothtimesource.data = {
            'x': [lastrow.ULx, lastrow.LLx, lastrow.MIx, lastrow.MMx],
            'y': [lastrow.ULy, lastrow.LLy, lastrow.MIy, lastrow.MMy]
        }

    # TODO: this is a workaround until we can set x_range, y_range directly
    # See https://github.com/bokeh/bokeh/issues/4014
    def set_limits(xstart, xend):
        '''Set axis limits.'''
        ch0.x_range.start = xstart
        ch0.x_range.end = xend
        ch0.axis[0].bounds = (xstart, xend)

    def update_select_widgets(clicked_x=None):
        '''Update widgets based on current selection. Use the clicked_x param to
        designate the cursor location if this function is called as the result of
        a Tap event. If clicked_x is None, then use the existing cursor location
        to set the center of the selection.'''
        mode = selmodebtn.labels[selmodebtn.active]
        if clicked_x is None and cursor.visible:
            x_loc = cursor.location
        elif clicked_x is not None:
            x_loc = clicked_x
        else:
            return
        if mode == '200ms':
            start = x_loc - 0.100
            end = x_loc + 0.100
            cursor.location = x_loc
        else:  # 'word' or 'phone'
            idx = np.abs(source.data['seconds'] - x_loc).argmin()
            # TODO: clean up the redundancy
            fld = {'word': 'word', 'phone': 'phone'}[mode]
            label = source.data[fld][idx]
            indexes = nonzero_groups(source.data[fld] == label,
                                     include_any=idx)
            secs = source.data['seconds'][indexes]
            start = secs.min()
            end = secs.max()
            cursor.location = secs.mean()
        playselbtn.start = start
        playselbtn.end = end
        selbox.left = start
        selbox.right = end
        selbox.visible = True
        cursor.visible = True

    def spkr_select_cb(attr, old, new):
        '''Respond to changes in speaker multiselect.'''
        try:
            spkrs = demo[
                (demo.sex.isin(sex_select.value) \
                & demo.dialect_base_state.isin(state_select.value) \
                & (demo.dialect_base_city.isin(city_select.value)))
            ].subject.unique()
            new_opts = [''] + [(f.value, f.label) for f in fileoptsdf[
                fileoptsdf.speaker.isin(spkrs)].itertuples()]
            fselect.options = new_opts
            fselect.value = ''
        except NameError as e:
            pass  # Values not set yet, so ignore

    def cursor_cb(e):
        '''Handle cursor mouse click in the waveform.'''
        update_select_widgets(clicked_x=e.x)
        update_trace()
        update_traj()

    def x_range_cb(attr, old, new):
        '''Handle change of x range in waveform/spectrogram.'''
        if attr == 'start':
            playvisbtn.start = new
        elif attr == 'end':
            playvisbtn.end = new

    def selection_cb(e):
        '''Handle data range selection event.'''
        #! TODO: handle situation in which selection is too short, i.e. len(seldf) <= 1
        cursor.location = (e.geometry['x0'] + e.geometry['x1']) / 2
        cursor.visible = True
        playselbtn.start = e.geometry['x0']
        playselbtn.end = e.geometry['x1']
        selbox.left = e.geometry['x0']
        selbox.right = e.geometry['x1']
        selbox.visible = True
        update_trace()
        update_traj()

    def selmode_cb(attr, old, new):
        '''Handle change in click selection value.'''
        update_select_widgets(clicked_x=None)

    def anim_cb(attr, old, new):
        '''Handle change in the animation slider.'''
        idx = np.argmin(np.abs(timesource.data['sec'] - new))
        n = len(timesource.data['color'])
        active = np.arange(n - idx, n + 1)
        timesource.data['color'] = np.pad(active, (0, n - len(active)),
                                          constant_values=0)
        anim_cmap = LinearColorMapper(palette=r_Greys256,
                                      low=1,
                                      high=n + 1,
                                      low_color='white')
        for tag, palette in (('anim_tng', r_Reds9), ('anim_oth', r_Greens9)):
            for a in find(traj.references(), {'tags': tag}):
                a.line_color = linear_cmap('color',
                                           palette,
                                           low=1,
                                           high=n + 1,
                                           low_color='white')
        lasttngtimesource.data = {
            'x': [
                timesource.data[pt][idx][1]
                for pt in ('T1x', 'T2x', 'T3x', 'T4x')
            ],
            'y': [
                timesource.data[pt][idx][1]
                for pt in ('T1y', 'T2y', 'T3y', 'T4y')
            ]
        }
        lastothtimesource.data = {
            'x': [
                timesource.data[pt][idx][1]
                for pt in ('ULx', 'LLx', 'MIx', 'MMx')
            ],
            'y': [
                timesource.data[pt][idx][1]
                for pt in ('ULy', 'LLy', 'MIy', 'MMy')
            ]
        }

    def anim_btn_cb():
        '''Handle click of anim_btn animate trajectories of selected audio.'''
        values = np.linspace(anim_slider.start, anim_slider.end,
                             len(timesource.data['T1x']))
        for v in values:
            anim_slider.value = v

    def low_thresh_cb(attr, old, new):
        '''Handle change in threshold slider to fade out low spectrogram values.'''
        params['low_thresh_power'] = new
        lt = _low_thresh()
        spec0cmap.low = lt

    def _low_thresh():
        return sgrams[0].values.min() \
               + sgrams[0].values.std()**params['low_thresh_power']

    step = None
    rate = orig_rate = None
    #    dfs = {}
    xrng = []
    yrng = []
    width = 1000
    height = 200
    cutoff = 50
    order = 3
    tngcolor = 'DarkRed'
    othcolor = 'Indigo'
    fselect = Select(options=[], value='')
    fselect.on_change('value', load_wav_cb)
    sex_select = MultiSelect(options=[('F', 'female'), ('M', 'male')],
                             value=['F', 'M'])
    state_select = MultiSelect(
        options=list(demo.dialect_base_state.cat.categories),
        value=list(demo.dialect_base_state.cat.categories))
    city_select = MultiSelect(
        options=list(demo.dialect_base_city.cat.categories),
        value=list(demo.dialect_base_city.cat.categories))
    sex_select.on_change('value', spkr_select_cb)
    state_select.on_change('value', spkr_select_cb)
    city_select.on_change('value', spkr_select_cb)
    spkr_select_cb('', '', '')

    source = ColumnDataSource(data=dict(seconds=[], ch0=[]))
    channels = ['ch0']

    playvisbtn = AudioButton(label='Play visible signal',
                             source=source,
                             channels=channels,
                             width=120,
                             disabled=True)
    playselbtn = AudioButton(label='Play selected signal',
                             source=source,
                             channels=channels,
                             width=120,
                             disabled=True)
    selmodebtn = RadioButtonGroup(labels=['200ms', 'word', 'phone'], active=1)
    selmodebtn.on_change('active', selmode_cb)
    # Instantiate and share specific select/zoom tools so that
    # highlighting is synchronized on all plots.
    boxsel = BoxSelectTool(dimensions='width')
    spboxsel = BoxSelectTool(dimensions='width')
    boxzoom = BoxZoomTool(dimensions='width')
    zoomin = ZoomInTool(dimensions='width')
    zoomout = ZoomOutTool(dimensions='width')
    crosshair = CrosshairTool(dimensions='height')
    shared_tools = [
        'xpan', boxzoom, boxsel, crosshair, zoomin, zoomout, 'reset'
    ]

    uttdiv = Div(text='')

    figargs = dict(tools=shared_tools, )
    cursor = Span(dimension='height',
                  line_color='red',
                  line_dash='dashed',
                  line_width=1)
    wavspec_height = 280
    ch0 = figure(name='ch0',
                 tooltips=[('time', '$x{0.0000}'), ('word', '@word'),
                           ('phone', '@phone')],
                 height=wavspec_height,
                 **figargs)
    ch0.toolbar.logo = None
    ch0.line(x='seconds', y='ch0', source=source, nonselection_line_alpha=0.6)
    # Link pan, zoom events for plots with x_range.
    ch0.x_range.on_change('start', x_range_cb)
    ch0.x_range.on_change('end', x_range_cb)
    ch0.on_event(SelectionGeometry, selection_cb)
    ch0.on_event(Tap, cursor_cb)
    ch0.add_layout(cursor)
    wavtab = Panel(child=ch0, title='Waveform')
    selbox = BoxAnnotation(name='selbox',
                           left=None,
                           right=None,
                           fill_color='green',
                           fill_alpha=0.1,
                           line_color='green',
                           line_width=1.5,
                           line_dash='dashed',
                           visible=False)
    ch0.add_layout(selbox)
    sgrams = [np.ones((1, 1))]
    specsource = ColumnDataSource(data=dict(sgram0=[sgrams[0]]))
    spec0 = figure(
        name='spec0',
        x_range=ch0.x_range,  # Keep times synchronized
        tooltips=[("time", "$x{0.0000}"), ("freq", "$y{0.0000}"),
                  ("value", "@sgram0{0.000000}")],
        height=wavspec_height,
        **figargs)
    spec0.toolbar.logo = None
    spec0.x_range.on_change('start', x_range_cb)
    spec0.x_range.on_change('end', x_range_cb)
    spec0.on_event(SelectionGeometry, selection_cb)
    spec0.on_event(Tap, cursor_cb)
    spec0.add_layout(cursor)
    spec0.x_range.range_padding = spec0.y_range.range_padding = 0
    spec0cmap = LogColorMapper(palette=r_Greys256,
                               low_color=params['low_thresh_color'])
    low_thresh_slider = Slider(start=1.0,
                               end=12.0,
                               step=0.03125,
                               value=params['low_thresh_power'],
                               title='Spectrogram threshold')
    low_thresh_slider.on_change('value', low_thresh_cb)
    spec0img = spec0.image(image='sgram0',
                           x=0,
                           y=0,
                           color_mapper=spec0cmap,
                           level='image',
                           source=specsource)
    spec0.grid.grid_line_width = 0.0
    spec0.add_layout(selbox)
    sgramtab = Panel(child=spec0, title='Spectrogram')

    tngsource = ColumnDataSource(data={'x': [], 'y': []})
    othsource = ColumnDataSource(data={'x': [], 'y': []})
    timesource = ColumnDataSource({
        'T1x': [],
        'T1y': [],
        'T2x': [],
        'T2y': [],
        'T3x': [],
        'T3y': [],
        'T4x': [],
        'T4y': [],
        'ULx': [],
        'ULy': [],
        'LLx': [],
        'LLy': [],
        'MIx': [],
        'MIy': [],
        'MMx': [],
        'MMy': [],
        'color': [],
        'sec': []
    })
    lasttngtimesource = ColumnDataSource(data={'x': [], 'y': []})
    lastothtimesource = ColumnDataSource(data={'x': [], 'y': []})
    palsource = ColumnDataSource(pd.DataFrame({'x': [], 'y': []}))
    phasource = ColumnDataSource(pd.DataFrame({'x': [], 'y': []}))

    trace = figure(width=500,
                   height=300,
                   title='Static trace',
                   x_range=(-100.0, 25.0),
                   y_range=(-37.650, 37.650),
                   tools=[],
                   tags=['xray', 'static_fig'])
    trace.toolbar.logo = None
    trace.circle('x',
                 'y',
                 source=tngsource,
                 size=3,
                 color=tngcolor,
                 tags=['update_xray'])
    trace.circle('x',
                 'y',
                 source=othsource,
                 size=3,
                 color=othcolor,
                 tags=['update_xray'])
    trace.line('x',
               'y',
               source=tngsource,
               color=tngcolor,
               tags=['update_xray'])
    trace.line('x', 'y', source=palsource, color='black')
    trace.line('x', 'y', source=phasource, color='black')

    traj = figure(width=500,
                  height=300,
                  title='Trajectories',
                  x_range=(-100.0, 25.0),
                  y_range=(-37.650, 37.650),
                  tools=[],
                  tags=['xray', 'trajectory_fig'])
    traj.toolbar.logo = None
    traj.multi_line('T1x', 'T1y', source=timesource, tags=['anim_tng'])
    traj.multi_line('T2x', 'T2y', source=timesource, tags=['anim_tng'])
    traj.multi_line('T3x', 'T3y', source=timesource, tags=['anim_tng'])
    traj.multi_line('T4x', 'T4y', source=timesource, tags=['anim_tng'])
    traj.multi_line('ULx', 'ULy', source=timesource, tags=['anim_oth'])
    traj.multi_line('LLx', 'LLy', source=timesource, tags=['anim_oth'])
    traj.multi_line('MIx', 'MIy', source=timesource, tags=['anim_oth'])
    traj.multi_line('MMx', 'MMy', source=timesource, tags=['anim_oth'])
    traj.circle('x', 'y', source=lasttngtimesource, color=tngcolor)
    traj.circle('x', 'y', source=lastothtimesource, color=othcolor)
    traj.line('x', 'y', source=lasttngtimesource, color='lightgray')
    traj.line('x', 'y', source=palsource, color='black')
    traj.line('x', 'y', source=phasource, color='black')

    anim_slider = Slider(start=0,
                         end=100,
                         step=1,
                         value=0,
                         width=240,
                         format='0.000f',
                         title='Selected trajectory',
                         orientation='horizontal',
                         disabled=True)
    anim_slider.on_change('value', anim_cb)
    anim_btn = Button(label='Animate', width=120, disabled=True)
    anim_btn.on_click(anim_btn_cb)

    audtabs = Tabs(tabs=[wavtab, sgramtab])
    mainLayout = column(
        row(sex_select, state_select, city_select),
        row(fselect),
        row(
            column(uttdiv, audtabs),
            column(
                #!                row(anim_slider, anim_btn),
                column(Div(text='Click selection mode:'), selmodebtn,
                       low_thresh_slider),
                row(playvisbtn, playselbtn))),
        row(trace, traj),
        name='mainLayout')
    doc.add_root(mainLayout)
    return doc
Exemplo n.º 21
0
def colombia_map(variable='confirmed', logscale=False):
	"""
	Interactive Colombian map in bokeh figure
	"""

	shapefile = os.path.join(ws.folders['data/misc'], 'departamentos_colombia/departamentos_colombia.shp')

	# Read shapefile using Geopandas
	gdf = gpd.read_file(shapefile)

	gdf = gdf[['cartodb_id', 'departamen', 'geometry']]

	# Remove 'None' values
	gdf = gdf[~gdf['geometry'].isnull()]

	# Rename columns
	gdf.columns = ['cartodb_id', 'departamento', 'geometry']

	# Make columns with lowered and 'normalized' values
	gdf['departamento_normalized'] = gdf['departamento'].str.lower().str.normalize('NFKD').str.encode('ascii', errors='ignore').str.decode('utf-8').str.replace(' ', '').str.replace('.', '')

	# Lower the strings in 'departamento'

	# Point to the Colombian COVID-19 dataframe
	df = ws.data_specific['Colombia']['last_date']

	# Change cartodb_id to int so that the dataframes can be merged
	df.cartodb_id = df.cartodb_id.astype(int)
	gdf.cartodb_id = gdf.cartodb_id.astype(int)

	# For merging, leave only the relevant columns
	df = df[['cartodb_id', 'iso', 'departamento_correcto', 'confirmed']]

	# Merge the dataframes
	merged = gdf.merge(df, left_on='cartodb_id', right_on='cartodb_id', how='left')
	
	# Read data to json
	merged_json = json.loads(merged.to_json())

	# Convert to String like object.
	json_data = json.dumps(merged_json)

	# Input GeoJSON source that contains features for plotting.
	geosource = GeoJSONDataSource(geojson=json_data)
	
	# Define a sequential multi-hue color palette.
	order = int(np.log10(df[variable].max()))
	max_value = df[variable].max()
	max_rounded = utl.round_up_order(max_value, order)
	nlinticks = max_rounded // (10 ** order) + 1
	# If max_rounded is too low (so there are too little nlinticks), just force 11 ticks
	if nlinticks <= 5:
		max_rounded = utl.round_up_order(max_value, order - 1)
		nlinticks = max_rounded // (10 ** (order - 1)) + 1
	# Now, if nlinticks is greater than 11, tick labels may look too tight, so go crop it
	if nlinticks > 11:
		nlinticks = 11

	if logscale:
		palette = brewer['Reds'][order + 1][::-1]
	else:
		try:
			palette = brewer['Reds'][nlinticks - 1]
			# Reverse color order so that dark blue is highest value.
			palette = palette[::-1]
		except KeyError:
			# nlinticks is too large; use a matplotlib colormap
			cmap = plt.cm.hot
			cmap = plt.cm.Reds
			cm = cmap(mpl.colors.Normalize()(np.arange(nlinticks - 1))) * 255
			palette = ["#%02x%02x%02x"%(int(r), int(g), int(b)) for r, g, b, _ in cm]

	# Instantiate LinearColorMapper that linearly maps numbers in a range, into a sequence of colors. Input nan_color.
	color_mapper = LinearColorMapper(palette=palette, low=0, high=max_rounded)
	color_mapper_log = LogColorMapper(palette=palette, low=1, high=10**(order+1))

	# Define custom tick labels for color bar.
	tick_labels = dict([('%i'%i, '%i'%i) for i in np.linspace(0, max_rounded, nlinticks)])
	# Make tick labels numbers
	tick_labels = [int(v) for v in tick_labels.values()]
	tick_labels_log = dict([('%i'%i, '%i'%i) for i in np.logspace(0, order, order + 1)])

	# Add hover tool
	hover = HoverTool(tooltips=[('Departamento', '@departamento_correcto'), ('Casos', '@confirmed')])

	#Create color bar. 
	color_bar = ColorBar(
			color_mapper=color_mapper, 
			label_standoff=8, 
			width = 350, 
			height = 10,
			border_line_color=None, 
			location = (0,0), 
			orientation = 'horizontal', 
			ticker=FixedTicker(ticks=tick_labels), 
		)
	color_bar_log = ColorBar(
			color_mapper=color_mapper_log, 
			label_standoff=8, 
			width = 350, 
			height = 10,
			border_line_color=None, 
			location = (0,0), 
			orientation = 'horizontal', 
			ticker=LogTicker(), 
		)

	# Last date for Colombia (to put in the title)
	df_ = ws.data_specific['Colombia']['time_series']
	last_date = df_['fecha_obj'].max()
	last_date = df_[df_['fecha_obj'] == last_date]['fecha'].tolist()[0]
	# Now 'delete' df_
	del(df_)
	# Create figure object.
	p = figure(
			title = 'Casos confirmados en Colombia, %s'%last_date, 
			plot_height = 500, 
			plot_width = 400, 
			# aspect_ratio="auto", 
			# aspect_scale=1, 
			toolbar_location = None, 
			# tools = [hover]
		)
	p.add_tools(hover)

	p.xgrid.grid_line_color = None
	p.ygrid.grid_line_color = None


	#Specify layout
	if logscale:
		# Add patch renderer to figure. 
		p.patches(
				'xs', 
				'ys', 
				source = geosource, 
				fill_color = {'field' :variable, 'transform' : color_mapper_log}, 
				line_color = 'black', 
				line_width = 1, 
				fill_alpha = 1
			)
		p.add_layout(color_bar_log, 'below')
	else:
		# Add patch renderer to figure. 
		p.patches(
				'xs', 
				'ys', 
				source = geosource, 
				fill_color = {'field' :variable, 'transform' : color_mapper}, 
				line_color = 'black', 
				line_width = 1, 
				fill_alpha = 1
			)
		p.add_layout(color_bar, 'below')

	# Save file
	if logscale:
		output_file(os.path.join(ws.folders["website/static/images"], "colombia_map_log.html"))
	else:
		output_file(os.path.join(ws.folders["website/static/images"], "colombia_map.html"))
	# show(p)
	save(p)
Exemplo n.º 22
0
def main():
    name = ''
    sex = ''

    years = [i for i in range(1880, 2018)]

    if "HI" in us_states.keys():
        del us_states["HI"]

    if "AK" in us_states.keys():
        del us_states["AK"]

    state_xs = [us_states[code]["lons"] for code in us_states]
    state_ys = [us_states[code]["lats"] for code in us_states]
    states = [state for state in us_states.keys()]
    """Begin formatting data for popularity trend model.
       Once data is formatted, build the model and add labels"""

    data_sources = create_dataset(name, sex, years, states, state_xs, state_ys)

    #adding layout tools and color pallete
    name_field = TextInput(value="", title="Name")
    sex_selection = RadioGroup(labels=["Male", "Female"], active=0)
    sub_button = Button(label="Submit", button_type="success", disabled=False)
    directions = Paragraph(
        text="""Type in any name and select the sex you would like to query.
        Then press Submit to display your results.""",
        width=250,
        height=60)

    palette.reverse()
    color_mapper = LogColorMapper(palette=palette)

    #create popularity trend model
    pop_plt = figure(title="Popularity Trend",
                     x_axis_label='Year',
                     y_axis_label="Quantity")
    pop_plt.line('x',
                 'y',
                 source=data_sources['line_source'],
                 legend="Qty",
                 line_width=2)
    pop_plt.circle('year',
                   'qty',
                   source=data_sources['circ_source'],
                   size=14,
                   color="navy",
                   alpha=0.5)
    labels = LabelSet(x='year',
                      y='qty',
                      text='value',
                      level='glyph',
                      x_offset=5,
                      y_offset=5,
                      source=data_sources['label_source'])
    pop_plt.add_layout(labels)

    #create the choropleth model
    TOOLS = "pan,wheel_zoom,reset,hover,save"

    choro_plt = figure(title="US Name Distribution",
                       tools=TOOLS,
                       x_axis_location=None,
                       plot_width=850,
                       y_axis_location=None,
                       plot_height=565,
                       tooltips=[("State", "@state_names"),
                                 ("Quantity", "@rate")])

    choro_plt.grid.grid_line_color = None
    choro_plt.hover.point_policy = "follow_mouse"

    choro_plt.patches('x',
                      'y',
                      source=data_sources['choropleth_source'],
                      fill_color={
                          'field': 'rate',
                          'transform': color_mapper
                      },
                      line_color="white",
                      fill_alpha=.8,
                      line_width=0.5)

    year_slider = Slider(start=1880,
                         end=2017,
                         value=1880,
                         step=1,
                         title="Year")

    color_bar = ColorBar(color_mapper=color_mapper,
                         ticker=LogTicker(),
                         label_standoff=12,
                         border_line_color=None,
                         location=(0, 0))
    choro_plt.add_layout(color_bar, 'right')

    #handlers and callbacks
    def slider_callback(attr, old, new):
        """Called by year_slider to cause interactive changes
        on the popularity trend and choropleth models"""

        yr = str(year_slider.value)

        #x is a pandas series
        xlist = data_sources['line_source'].data['x'].tolist()

        # x~year y~qty
        if year_slider.value in xlist:

            new_circ_data = dict(
                year=[
                    data_sources['line_source'].data['x'][xlist.index(
                        year_slider.value)]
                ],
                qty=[
                    data_sources['line_source'].data['y'][xlist.index(
                        year_slider.value)]
                ])

            new_label_data = dict(year=new_circ_data['year'],
                                  qty=new_circ_data['qty'],
                                  value=new_circ_data['qty'])

            data_sources['label_source'].data = new_label_data
            data_sources['circ_source'].data = new_circ_data

        choro_data = dict(x=state_xs,
                          y=state_ys,
                          state_names=states,
                          rate=data_sources['matrix'][str(yr)])
        data_sources['choropleth_source'].data = choro_data

    def text_input_handler(attr, old, new):

        new_name = new.strip()

        try:
            name_validation(new_name)

            try:
                new_name = new_name.capitalize()
                check_tables(new_name)

                new_query_data = dict(
                    name=[new_name],
                    sex=[data_sources["query_source"].data["sex"][0]])
                query_data = ColumnDataSource(new_query_data)
                data_sources['query_source'] = query_data

            except ValueError:
                name_field.value = f"Sorry, {new_name} doesn't exist in the database"

        except ValueError:
            name_field.value = "Enter a name with valid letters only."

    def radio_handler(new):
        #value based on radio button index: 0-Male 1-Female
        if new == 0:
            sex = 'M'
        else:
            sex = 'F'

        new_query_data = dict(
            name=[data_sources["query_source"].data["name"][0]], sex=[sex])

        query_data = ColumnDataSource(new_query_data)
        data_sources['query_source'] = query_data

    def replot():

        new_name = str(name_field.value).strip()
        new_name = new_name.capitalize()

        if sex_selection.active == 0:
            new_sex = 'M'
        else:
            new_sex = 'F'

        data_dict = get_data(new_name, new_sex, years, states)

        new_query_data = dict(name=[new_name], sex=[new_sex])
        data_sources['query_source'].data = new_query_data

        new_line_data = dict(x=data_dict['pop_df']['Year'],
                             y=data_dict['pop_df']['Qty'])
        data_sources['line_source'].data = new_line_data
        xlist = data_sources['line_source'].data['x'].tolist()

        if year_slider.value not in xlist:

            new_circ_data = dict(
                year=[data_sources['line_source'].data['x'][0]],
                qty=[data_sources['line_source'].data['y'][0]])
        else:
            new_circ_data = dict(
                year=[
                    data_sources['line_source'].data['x'][xlist.index(
                        year_slider.value)]
                ],
                qty=[
                    data_sources['line_source'].data['y'][xlist.index(
                        year_slider.value)]
                ])

        new_label_data = dict(year=new_circ_data['year'],
                              qty=new_circ_data['qty'],
                              value=new_circ_data['qty'])

        new_choropleth_data = dict(x=state_xs,
                                   y=state_ys,
                                   state_names=states,
                                   rate=data_dict['matrix'][str(
                                       year_slider.value)])

        data_sources['circ_source'].data = new_circ_data
        data_sources['label_source'].data = new_label_data
        data_sources['choropleth_source'].data = new_choropleth_data
        data_sources['matrix'] = data_dict['matrix']

    year_slider.on_change('value', slider_callback)
    sex_selection.on_click(radio_handler)
    name_field.on_change('value', text_input_handler)
    sub_button.on_click(replot)

    layout = row(column(directions, name_field, sex_selection, sub_button),
                 row(pop_plt, column(choro_plt, widgetbox(year_slider))))

    curdoc().add_root(layout)
Exemplo n.º 23
0
    def _make_echelle_elements(
        self,
        deltanu,
        cmap="viridis",
        minimum_frequency=None,
        maximum_frequency=None,
        smooth_filter_width=0.1,
        scale="linear",
        plot_width=490,
        plot_height=340,
        title="Echelle",
    ):
        """Helper function to make the elements of the echelle diagram for bokeh plotting."""
        if not hasattr(deltanu, "unit"):
            deltanu = deltanu * self.periodogram.frequency.unit

        if smooth_filter_width:
            pgsmooth = self.periodogram.smooth(filter_width=smooth_filter_width)
            freq = pgsmooth.frequency  # Makes code below more readable below
        else:
            freq = self.periodogram.frequency  # Makes code below more readable

        ep, x_f, y_f = self._clean_echelle(
            deltanu=deltanu,
            minimum_frequency=minimum_frequency,
            maximum_frequency=maximum_frequency,
            smooth_filter_width=smooth_filter_width,
            scale=scale,
        )

        fig = figure(
            plot_width=plot_width,
            plot_height=plot_height,
            x_range=(0, 1),
            y_range=(y_f[0].value, y_f[-1].value),
            title=title,
            tools="pan,box_zoom,reset",
            toolbar_location="above",
            border_fill_color="white",
        )

        fig.yaxis.axis_label = r"Frequency [{}]".format(freq.unit.to_string())
        fig.xaxis.axis_label = r"Frequency / {:.3f} Mod. 1".format(deltanu)

        lo, hi = np.nanpercentile(ep.value, [0.1, 99.9])
        vlo, vhi = 0.3 * lo, 1.7 * hi
        vstep = (lo - hi) / 500
        color_mapper = LogColorMapper(palette="RdYlGn10", low=lo, high=hi)

        fig.image(
            image=[ep.value],
            x=0,
            y=y_f[0].value,
            dw=1,
            dh=y_f[-1].value,
            color_mapper=color_mapper,
            name="img",
        )

        stretch_slider = RangeSlider(
            start=vlo,
            end=vhi,
            step=vstep,
            title="",
            value=(lo, hi),
            orientation="vertical",
            width=10,
            height=230,
            direction="rtl",
            show_value=False,
            sizing_mode="fixed",
            name="stretch",
        )

        def stretch_change_callback(attr, old, new):
            """TPF stretch slider callback."""
            fig.select("img")[0].glyph.color_mapper.high = new[1]
            fig.select("img")[0].glyph.color_mapper.low = new[0]

        stretch_slider.on_change("value", stretch_change_callback)
        return fig, stretch_slider
Exemplo n.º 24
0
def build_state_plot():
    from bokeh.sampledata.us_counties import data as counties

    print('building data sets...')
    alum_points = create_dictionary_data('./local/alum_lat_lon.csv')
    cm_points = create_dictionary_data('./local/cm_lat_lon.csv')
    school_points = create_dictionary_data('./local/school_lat_lon.csv')
    supporter_points = create_dictionary_data('./local/supporters_lat_lon.csv')

    print('school data...')
    cm_data, cm_counties = open_cm_data()

    for cm in cm_data:
        students, county = int(cm['num']), cm['county']
        cm_counties[county]['num'] += students
        cm_counties[county]['cms'] += 1

    print('creating county info...')
    counties = {
        code: county
        for code, county in counties.items()
        if (county['state'] == 'ms' or county['state'] == 'ar')
    }
    student_numbers = []
    for code in counties.keys():
        if counties[code]['name'].replace(
                ' County', '') in cm_counties.keys() and cm_counties.get(
                    counties[code]['name'], {
                        'state': ''
                    }).get('state', '') == counties[code]['state']:
            counties[code]['students'] = cm_counties[counties[code]
                                                     ['name']].get('num', 0)
            student_numbers.append(cm_counties[counties[code]['name']].get(
                'num', 0))
            counties[code]['cms'] = cm_counties[counties[code]['name']].get(
                'cms', 0)
        else:
            counties[code]['students'] = 0
            counties[code]['cms'] = 0
    max_students = max(student_numbers)

    print('building plot...')
    output_file('./map.html')

    county_xs = [county["lons"] for county in counties.values()]
    county_ys = [county["lats"] for county in counties.values()]

    county_names = [
        '{} County'.format(county['name']) for county in counties.values()
    ]
    county_place = [bool(county['students']) for county in counties.values()]
    county_students = [
        '{} students'.format(county['students'])
        for county in counties.values()
    ]
    county_cms = [
        '{} Corps Members'.format(county['cms'])
        for county in counties.values()
    ]

    palette = [CUSTOM_COLORS['Dark Blue'], CUSTOM_COLORS['Teal']]
    color_mapper = LogColorMapper(palette=palette)

    source = ColumnDataSource(data=dict(x=county_xs,
                                        y=county_ys,
                                        county=county_names,
                                        has_cm=county_place,
                                        students=county_students))

    TOOLS = "pan,wheel_zoom,reset,save"

    p = figure(tools=TOOLS,
               active_drag="pan",
               active_scroll=None,
               active_tap=None,
               active_inspect=None,
               x_axis_location=None,
               y_axis_location=None,
               toolbar_location="left")
    p.grid.grid_line_color = None
    p.sizing_mode = 'scale_width'
    # p.sizing_mode = 'scale_height'

    p.patches('x',
              'y',
              source=source,
              fill_color={
                  'field': 'has_cm',
                  'transform': color_mapper
              },
              fill_alpha=1,
              line_color="white",
              line_width=2)

    # school data points
    source = ColumnDataSource(
        dict(
            x=school_points['lon'],
            y=school_points['lat'],
            city=school_points['city'],
            name=school_points['name'],
            type=school_points['type'],
            cms=school_points['number'],
        ))
    g = p.square(x="x",
                 y="y",
                 source=source,
                 size=8,
                 color=CUSTOM_COLORS['Purple'],
                 alpha=1,
                 legend='Schools')
    hover1 = HoverTool(renderers=[g],
                       tooltips=[
                           ("School Name", "@name"),
                           ("Location", "@city"),
                           ("Type of Partner", "@type"),
                           ("Number of CMs", "@cms"),
                       ])
    p.add_tools(hover1)

    # donor data points
    source = ColumnDataSource(
        dict(
            x=supporter_points['lon'],
            y=supporter_points['lat'],
            city=supporter_points['city'],
            name=supporter_points['name'],
            donate=supporter_points['last_donate'],
        ))
    g = p.circle(x="x",
                 y="y",
                 source=source,
                 size=8,
                 color=CUSTOM_COLORS['Blue'],
                 alpha=1,
                 legend='Supporters')
    hover2 = HoverTool(renderers=[g],
                       tooltips=[
                           ("Donor Name", "@name"),
                           ("Location", "@city"),
                           ("Latest Donation", "@donate"),
                       ])
    p.add_tools(hover2)

    # alumni data points
    source = ColumnDataSource(
        dict(
            x=alum_points['lon'],
            y=alum_points['lat'],
            city=alum_points['city'],
            name=alum_points['name'],
            corps=alum_points['corps'],
            title=alum_points['title'],
            employer=alum_points['employer'],
            profession=alum_points['profession'],
            last_survey=alum_points['last_survey'],
            nps=alum_points['nps'],
        ))
    g = p.circle(x="x",
                 y="y",
                 source=source,
                 size=8,
                 color=CUSTOM_COLORS['Red'],
                 alpha=1,
                 legend='Alumni')
    hover3 = HoverTool(renderers=[g],
                       tooltips=[
                           ("Alum Name", "@name"),
                           ("Corps", "@corps"),
                           ("Location", "@city"),
                           ("Title", "@title"),
                           ("Employer", "@employer"),
                           ("profession", "@profession"),
                           ("Last Alum Survey", "@last_survey"),
                           ("Latest NPS", "@nps"),
                       ])
    p.add_tools(hover3)

    # cm data points
    source = ColumnDataSource(
        dict(
            x=cm_points['lon'],
            y=cm_points['lat'],
            city=cm_points['city'],
            name=cm_points['name'],
            school=cm_points['school'],
            coach=cm_points['coach'],
            content=cm_points['content'],
            num=cm_points['num'],
            yol=cm_points['yol'],
        ))
    g = p.circle(x="x",
                 y="y",
                 source=source,
                 size=8,
                 color=CUSTOM_COLORS['Orange'],
                 alpha=1,
                 legend='Corps Members')
    hover4 = HoverTool(renderers=[g],
                       tooltips=[
                           ("CM Name", "@name"),
                           ("Location", "@city"),
                           ("School", "@school"),
                           ("Coach", "@coach"),
                           ("Content", "@content"),
                           ("Number of Students", "@num"),
                           ("Expected Growth", "@yol"),
                       ])
    p.add_tools(hover4)

    p.legend.location = "top_right"
    p.legend.click_policy = "hide"

    p.toolbar.active_inspect = [hover2, hover3, hover4]
    # p.toolbar.active_inspect = []

    show(p)
Exemplo n.º 25
0
def make_tpf_figure_elements(tpf,
                             tpf_source,
                             pedestal=None,
                             fiducial_frame=None,
                             plot_width=370,
                             plot_height=340,
                             scale='log',
                             vmin=None,
                             vmax=None,
                             cmap='Viridis256',
                             tools='tap,box_select,wheel_zoom,reset'):
    """Returns the lightcurve figure elements.

    Parameters
    ----------
    tpf : TargetPixelFile
        TPF to show.
    tpf_source : bokeh.plotting.ColumnDataSource
        TPF data source.
    pedestal: float
        A scalar value to be added to the TPF flux values, often to avoid
        taking the log of a negative number in colorbars.
        Defaults to `-min(tpf.flux) + 1`
    fiducial_frame: int
        The tpf slice to start with by default, it is assumed the WCS
        is exact for this frame.
    scale: str
        Color scale for tpf figure. Default is 'log'
    vmin: int [optional]
        Minimum color scale for tpf figure
    vmax: int [optional]
        Maximum color scale for tpf figure
    cmap: str
        Colormap to use for tpf plot. Default is 'Viridis256'
    tools: str
        Bokeh tool list
    Returns
    -------
    fig, stretch_slider : bokeh.plotting.figure.Figure, RangeSlider
    """
    if pedestal is None:
        pedestal = -np.nanmin(tpf.flux.value) + 1
    if scale == 'linear':
        pedestal = 0

    if tpf.mission in ['Kepler', 'K2']:
        title = 'Pixel data (CCD {}.{})'.format(tpf.module, tpf.output)
    elif tpf.mission == 'TESS':
        title = 'Pixel data (Camera {}.{})'.format(tpf.camera, tpf.ccd)
    else:
        title = "Pixel data"

    # We subtract 0.5 from the range below because pixel coordinates refer to
    # the middle of a pixel, e.g. (col, row) = (10.0, 20.0) is a pixel center.
    fig = figure(plot_width=plot_width,
                 plot_height=plot_height,
                 x_range=(tpf.column - 0.5, tpf.column + tpf.shape[2] - 0.5),
                 y_range=(tpf.row - 0.5, tpf.row + tpf.shape[1] - 0.5),
                 title=title,
                 tools=tools,
                 toolbar_location="below",
                 border_fill_color="whitesmoke")

    fig.yaxis.axis_label = 'Pixel Row Number'
    fig.xaxis.axis_label = 'Pixel Column Number'

    vlo, lo, hi, vhi = np.nanpercentile(tpf.flux.value + pedestal,
                                        [0.2, 1, 95, 99.8])
    if vmin is not None:
        vlo, lo = vmin, vmin
    if vmax is not None:
        vhi, hi = vmax, vmax

    if scale == 'log':
        vstep = (np.log10(vhi) -
                 np.log10(vlo)) / 300.0  # assumes counts >> 1.0!
    if scale == 'linear':
        vstep = (vhi - vlo) / 300.0  # assumes counts >> 1.0!

    if scale == 'log':
        color_mapper = LogColorMapper(palette=cmap, low=lo, high=hi)
    elif scale == 'linear':
        color_mapper = LinearColorMapper(palette=cmap, low=lo, high=hi)
    else:
        raise ValueError(
            'Please specify either `linear` or `log` scale for color.')

    fig.image([tpf.flux.value[fiducial_frame, :, :] + pedestal],
              x=tpf.column - 0.5,
              y=tpf.row - 0.5,
              dw=tpf.shape[2],
              dh=tpf.shape[1],
              dilate=True,
              color_mapper=color_mapper,
              name="tpfimg")

    # The colorbar will update with the screen stretch slider
    # The colorbar margin increases as the length of the tick labels grows.
    # This colorbar share of the plot window grows, shrinking plot area.
    # This effect is known, some workarounds might work to fix the plot area:
    # https://github.com/bokeh/bokeh/issues/5186

    if scale == 'log':
        ticker = LogTicker(desired_num_ticks=8)
    elif scale == 'linear':
        ticker = BasicTicker(desired_num_ticks=8)

    color_bar = ColorBar(color_mapper=color_mapper,
                         ticker=ticker,
                         label_standoff=-10,
                         border_line_color=None,
                         location=(0, 0),
                         background_fill_color='whitesmoke',
                         major_label_text_align='left',
                         major_label_text_baseline='middle',
                         title='e/s',
                         margin=0)
    fig.add_layout(color_bar, 'right')

    color_bar.formatter = PrintfTickFormatter(format="%14i")

    if tpf_source is not None:
        fig.rect('xx',
                 'yy',
                 1,
                 1,
                 source=tpf_source,
                 fill_color='gray',
                 fill_alpha=0.4,
                 line_color='white')

    # Configure the stretch slider and its callback function
    if scale == 'log':
        start, end = np.log10(vlo), np.log10(vhi)
        values = (np.log10(lo), np.log10(hi))
    elif scale == 'linear':
        start, end = vlo, vhi
        values = (lo, hi)

    stretch_slider = RangeSlider(start=start,
                                 end=end,
                                 step=vstep,
                                 title='Screen Stretch ({})'.format(scale),
                                 value=values,
                                 orientation='horizontal',
                                 width=200,
                                 direction='ltr',
                                 show_value=True,
                                 sizing_mode='fixed',
                                 height=15,
                                 name='tpfstretch')

    def stretch_change_callback_log(attr, old, new):
        """TPF stretch slider callback."""
        fig.select('tpfimg')[0].glyph.color_mapper.high = 10**new[1]
        fig.select('tpfimg')[0].glyph.color_mapper.low = 10**new[0]

    def stretch_change_callback_linear(attr, old, new):
        """TPF stretch slider callback."""
        fig.select('tpfimg')[0].glyph.color_mapper.high = new[1]
        fig.select('tpfimg')[0].glyph.color_mapper.low = new[0]

    if scale == 'log':
        stretch_slider.on_change('value', stretch_change_callback_log)
    if scale == 'linear':
        stretch_slider.on_change('value', stretch_change_callback_linear)

    return fig, stretch_slider
Exemplo n.º 26
0
def create_bokeh_choro(ff, prop=0):
    """Creates Interactive Bokeh Choropleth for US counties transportationdata.

    Arguments:
        ff {dict} -- Dictionary containing filled dataframes

    Keyword Arguments:
        prop {int} -- Select the property for which choropleth needs to be created (default: {0})
    """
    year = 0
    # Very Important Function
    assert isinstance(prop, int)
    assert isinstance(year, int)
    assert len(ff['CA'][0].columns) > prop >= 0
    assert len(ff['CA'][0].index) > year >= 0
    try:
        # del states["HI"]
        del states["AK"]
    except:
        pass
    nyears = len(ff['CA'][0].index)
    state_xs = [states[code]["lons"] for code in states]
    state_ys = [states[code]["lats"] for code in states]
    county_xs = []
    county_ys = []
    district_name = []
    for cs in bokeh_counties.values():
        for dname in cs:
            county_xs.append([counties[code]["lons"]
                              for code in counties if counties[code]["detailed name"] == dname][0])
            county_ys.append([counties[code]["lats"]
                              for code in counties if counties[code]["detailed name"] == dname][0])
            district_name.append(dname)
    if isinstance(palette, dict):
        color_mapper = LogColorMapper(
            palette=palette[list(palette.keys())[-1]])
    else:
        color_mapper = LogColorMapper(palette=palette)
    pvalues = []

    for yx in range(nyears):
        yvalues = []
        for state in ff.keys():
            for cs in ff[state]:
                yvalues.append(cs.iloc[yx, prop])
        pvalues.append(yvalues)
    alldat = {}
    syear = ff['CA'][0].index[0]
    for ix, yy in enumerate(range(syear, syear + nyears)):
        alldat[str(yy)] = pvalues[ix]
    # alldat = {str(i): v for i, v in enumerate(pvalues)}
    source = ColumnDataSource(data=dict(
        x=county_xs, y=county_ys,
        name=district_name, pvalue=pvalues[0], **alldat))
    TOOLS = "pan,wheel_zoom,reset,hover,save"
    p = figure(title=f"{ff['CA'][0].columns[prop]} across Counties", tools=TOOLS, plot_width=1800,
               plot_height=700, x_axis_location=None, y_axis_location=None)
    p.toolbar.active_scroll = "auto"
    p.toolbar.active_drag = 'auto'
    p.background_fill_color = "#B0E0E6"
    p.patches(state_xs, state_ys, fill_alpha=1.0, fill_color='#FFFFE0',
              line_color="#884444", line_width=2, line_alpha=0.3)
    p.patches('x', 'y', source=source,
              fill_color={'field': 'pvalue', 'transform': color_mapper},
              fill_alpha=0.8, line_color="white", line_width=0.3)
    hover = p.select_one(HoverTool)
    hover.point_policy = "follow_mouse"
    property = ff['CA'][0].columns[prop]
    hover.tooltips = [("County", "@name"), (property,
                                            "@pvalue"), ("(Long, Lat)", "($x, $y)")]

    output_file("US_transport.html", title="US Public Transport")
    slider = Slider(start=int(ff['CA'][0].index[0]), end=int(ff['CA'][0].index[-1]),
                    value=int(ff['CA'][0].index[0]), step=1, title="Start Year")

    def update(source=source, slider=slider, window=None):
        """ Update the map: change the bike density measure according to slider
            will be translated to JavaScript and Called in Browser """
        data = source.data
        v = cb_obj.getv('value')
        print(data[v])
        data['pvalue'] = [x for x in data[v]]
        source.trigger('change')
        # source.change.emit()
    slider.js_on_change('value', CustomJS.from_py_func(update))
    show(column(p, widgetbox(slider),))
Exemplo n.º 27
0
from COVID.extract import COVID_counts
import pandas as pd
import pickle

[stateBorders,
 countyBorders] = pickle.load(open("./COVID/extract/regionBorders.p", "rb"))
[usPopulation, statePopulations, countyPopulations
 ] = pickle.load(open("./COVID/extract/regionPopulations.p", "rb"))
[countyDF, stateDF_NYT, stateDF_CT, usDF_NYT, usDF_CT,
 lastUpdated] = pickle.load(open("./COVID/extract/CovidCounts.p", "rb"))
print(lastUpdated)

# palette = tuple(palette)
palette = tuple([all_palettes['Turbo'][256][idx] for idx in range(50, 256)])
# color_mapper = LinearColorMapper(palette=palette)
color_mapper = LogColorMapper(palette=palette, low=1, high=200000)

us_TOOLS = [
    BoxZoomTool(),
    PanTool(),
    WheelZoomTool(),
    TapTool(),
    HoverTool(),
    ResetTool()
]
state_TOOLS = [
    BoxZoomTool(),
    PanTool(),
    WheelZoomTool(),
    TapTool(),
    HoverTool(),
Exemplo n.º 28
0
        elements.group[i] = str(count + 4)
        count += 1

#Define matplotlib and bokeh color map
if log_scale == 0:
    color_mapper = LinearColorMapper(palette=bokeh_palette,
                                     low=min(data),
                                     high=max(data))
    norm = Normalize(vmin=min(data), vmax=max(data))
elif log_scale == 1:
    for datum in data:
        if datum < 0:
            raise ValueError('Entry for element ' + datum + ' is negative but'
                             ' log-scale is selected')
    color_mapper = LogColorMapper(palette=bokeh_palette,
                                  low=min(data),
                                  high=max(data))
    norm = LogNorm(vmin=min(data), vmax=max(data))
color_scale = ScalarMappable(norm=norm, cmap=cmap).to_rgba(data, alpha=None)

#Define color for blank entries
blank_color = '#c4c4c4'
color_list = []
for i in range(len(elements)):
    color_list.append(blank_color)

#Compare elements in dataset with elements in periodic table
for i, data_element in enumerate(data_elements):
    element_entry = elements.symbol[elements.symbol.str.lower() ==
                                    data_element.lower()]
    if element_entry.empty == False: