Пример #1
0
def myGraphTestSpringLayout2():
    hv.extension('bokeh')
    renderer = hv.renderer('bokeh')

    #EXAMPLE GRAPH EDGE LIST
    path = "/u/alfonsda/Documents/DOCTORAT_TAL/004projetOntologie/holoviews-examples/myGraphEdgeList.tsv"
    #100 000 PROFILES GRAPH EDGE LIST
    #path = "/u/alfonsda/Documents/DOCTORAT_TAL/004projetOntologie/002data/candidats/2016-09-15/fr/anglophone/sample100milFunctions/edgeListNoWeight.tsv"

    g = nx.read_edgelist(
        path,
        delimiter='\t',
        nodetype=str,
        data=(('weight', float),
              ('index', int)))  #the bigger the edgelist weight appears shorter

    #set colors for the nodes
    nodeColorsDict = getNodeColors(path)
    nx.set_node_attributes(g, name='color', values=nodeColorsDict)
    #position nodes using Fruchterman-Reingold force-directed algorithm
    #pos = nx.spring_layout(g, weight='weight', iterations=50, scale=2, center=(0,0))

    #nodes = hv.Nodes((x, y, node_indices, node_labels), vdims='Type')
    myGraph = hv.Graph.from_networkx(
        g,
        nx.spring_layout,
        nodes=None,
        weight='weight',
        iterations=50,
        scale=2,
        center=(0, 0)
    )  #the spring layout uses Fruchterman-Reingold force-directed algorithm to lay out the graph
    myGraph = hv.Graph(
        myGraph,
        label=
        'Graphe echantillon (EDGES: 795046, VERTICES : JobTitles(3827) - Skills(7529)'
    )
    #nodes = hv.Nodes((x, y, node_indices, node_labels), vdims='Type')
    #print(myGraph.nodes['index'])

    padding = dict(x=(-1.2, 1.2),
                   y=(-1.2, 1.2))  #X and Y showed scale on the cartesian table
    #myGraph = myGraph.redim.range(**padding).opts(plot=dict(height=800, width=1000, color_index='Type', edge_color_index='Weight'),
    #                                    style=dict(node_fill_color='red', cmap=['green', 'red']))
    myGraph = myGraph.redim.range(**padding).opts(
        plot=dict(height=800,
                  width=1000,
                  node_fill_color='color',
                  edge_color_index='Weight'),
        style=dict(cmap=nodeColorsDict))

    interactImg = InteractiveImage(myGraph, create_image())

    #myGraph = rasterize(myGraph)
    renderer.save(interactImg, 'out')
Пример #2
0
def plot_filtered(col, fdf, background = 'black', Toner=True, x_range = (1373757.1102773394, 1412506.1502695908), y_range = (7478418.9895278225, 7520786.118694777)):
    """
    
    """
    p = base_plot(x_range = x_range, y_range = y_range)
    if Toner:
        url="http://tile.stamen.com/toner-background/{Z}/{X}/{Y}.png"
    else:
        url="https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{Z}/{Y}/{X}.png"
    tile_renderer = p.add_tile(WMTSTileSource(url=url))
    tile_renderer.alpha=1.0 if background == "black" else 0.15
    return InteractiveImage(p, create_image_wrap(col, fdf, x_range = x_range, y_range = y_range))
Пример #3
0
def test_interactive_image_initialize():
    p = figure(x_range=(0, 1), y_range=(0, 1), plot_width=2, plot_height=2)
    img = InteractiveImage(p, create_image)
    out = np.array([[4287299584, 4287299584], [4287299584, 4287299584]],
                   dtype=np.uint32)

    assert img.ds.data['x'] == [0]
    assert img.ds.data['y'] == [0]
    assert img.ds.data['dh'] == [1]
    assert img.ds.data['dw'] == [1]
    assert np.array_equal(img.ds.data['image'][0], out)

    assert img.renderer.glyph.x == 'x'
    assert img.renderer.glyph.y == 'y'
    assert img.renderer.glyph.dh == 'dh'
    assert img.renderer.glyph.dw == 'dw'
    assert img.renderer.glyph.image == 'image'
def ds_lines(ds_df, plot=None):
    """
    plot many lines using datashader

    Parameters
    ----------
    ds_df: see np_to_ds_format

    Returns
    -------
    img: datashader image object
    """

    x_range = ds_df['x'].min(), ds_df['x'].max()
    y_range = ds_df['y'].min(), ds_df['y'].max()

    if plot is not None:
        plot_width = plot.plot_width
        plot_height = plot.plot_height
    else:
        plot_width = 400
        plot_height = 300

    def plot_data(x_range=x_range,
                  y_range=y_range,
                  plot_height=plot_height,
                  plot_width=plot_width,
                  aggregator=datashader.count()):
        canvas = datashader.Canvas(x_range=x_range,
                                   y_range=y_range,
                                   plot_height=plot_height,
                                   plot_width=plot_width)
        agg = canvas.line(ds_df, 'x', 'y', aggregator)
        img = datashader.transfer_functions.shade(agg, how='eq_hist')
        return img

    if plot is not None:
        plot.x_range = bokeh.models.Range1d(*x_range)
        plot.y_range = bokeh.models.Range1d(*y_range)
        InteractiveImage(plot, plot_data, aggregator=datashader.count())
    else:
        return plot_data(x_range, y_range)
Пример #5
0
def test_interactive_image_update():
    # jbednar: This test uses 1x1 images that are not actually supported
    # (as they have infinite resolution), but as InteractiveImage is deprecated
    # anyway the tests have not been updated.
    p = figure(x_range=(0, 1), y_range=(0, 1), plot_width=2, plot_height=2)
    img = InteractiveImage(p, create_image)

    # Ensure bokeh Document is instantiated
    img._repr_html_()
    assert isinstance(img.doc, Document)

    # Ensure image is updated
    img.update_image({
        'xmin': 0.5,
        'xmax': 1,
        'ymin': 0.5,
        'ymax': 1,
        'w': 1,
        'h': 1
    })
    out = np.array([[4287299584]], dtype=np.uint32)
    assert img.ds.data['x'] == [0.5]
    assert img.ds.data['y'] == [0.5]
    assert img.ds.data['dh'] == [0.5]
    assert img.ds.data['dw'] == [0.5]
    assert np.array_equal(img.ds.data['image'][0], out)

    # Ensure patch message is correct
    msg = img.get_update_event()
    event = msg.content['events'][0]
    assert event['kind'] == 'ColumnDataChanged'
    assert event['column_source'] == img.ds.ref
    assert sorted(event['cols']) == ['dh', 'dw', 'image', 'x', 'y']
    new = event['new']
    assert new['dh'] == [0.5]
    assert new['dw'] == [0.5]
    assert new['x'] == [0.5]
    assert new['y'] == [0.5]
    image = new['image'][0]
    assert image['dtype'] == 'uint32'
    assert image['shape'] == [1, 1]

    # Ensure events are cleared after update
    assert img.doc._held_events == []
Пример #6
0
    wm_minlong, wm_minlat = web_mercator(min_long, min_lat)
    wm_maxlong, wm_maxlat = web_mercator(max_long, max_lat)
    FRAME = x_range, y_range = ((wm_minlong, wm_maxlong), (wm_minlat,
                                                           wm_maxlat))

    def create_image(x_range, y_range, w=plot_width, h=plot_height):
        cvs = ds.Canvas(plot_width=w,
                        plot_height=h,
                        x_range=x_range,
                        y_range=y_range)
        agg = cvs.points(data, 'wm_lon', 'wm_lat', ds.count('velo'))
        img = tf.shade(agg, cmap=viridis, how='eq_hist')
        return tf.dynspread(img, threshold=0.5, max_px=4)

    def create_image90(x_range, y_range, w=plot_width, h=plot_height):
        cvs = ds.Canvas(plot_width=w,
                        plot_height=h,
                        x_range=x_range,
                        y_range=y_range)
        agg = cvs.points(data, 'wm_lon', 'wm_lat', ds.count('velo'))
        img = tf.shade(agg.where(agg > np.percentile(agg, 90)),
                       cmap=viridis,
                       how='eq_hist')
        return tf.dynspread(img, threshold=0.3, max_px=4)

    p = base_plot()
    export = partial(export_image, export_path="export", background=background)
    p.add_tile(STAMEN_TERRAIN)
    export(create_image90(*FRAME), "NYCT_90th")
    InteractiveImage(p, create_image90)
Пример #7
0
 def interactive_plot(self):
     bp.output_notebook()
     p = bp.figure(tools='pan,wheel_zoom,reset', x_range=(-5, 5), y_range=(-5, 5), \
         plot_width=self.plot_width, plot_height=self.plot_height,background_fill_color='black')
     return InteractiveImage(p, self.image_callback())
Пример #8
0
	def __init__(self,
			model=PointImageModel(),
			x_range=(0,1), # datashader cannot handle 0-sized range
			y_range=(0,1), # datashader cannot handle 0-sized range
			customize_ranges=customize_ranges,
			doc=None,
			log=None,
		):
		self.customize_ranges = customize_ranges
		self.height_textinput = TextInput(
			# value="100",
			width_policy="min",
			# height_policy="min",
			sizing_mode='stretch_width',
		)
		self.query = ''
		self.model = model
		fig = figure(
			x_range=x_range,
			y_range=y_range,
			reset_policy="event_only",
			sizing_mode='stretch_both',
		)
		legend = Div(
			visible=True,
			height_policy='max',
		)
		# fig.add_layout(Legend(click_policy='hide'))
		query_textinput = TextInput(
			title="query",
			sizing_mode="stretch_width",
			value='',
			width=100
		)
		options_dropdown = Dropdown(
			label='Options',
			sizing_mode='fixed',
			menu=[('Show/Hide Legend','legend'),('Enable/Disable Auto Update','auto')]
		)
		actions_dropdown = Dropdown(
			label='Actions',
			sizing_mode='fixed',
			menu=[('Fit Window','fit')],
		)
		status_button = Button(
			label='Auto Update',
			sizing_mode='fixed',
			#sizing_mode='stretch_width',
			width_policy='min',
		)
		view = column(
			row(
				self.height_textinput,
				options_dropdown,
				actions_dropdown,
				Spacer(sizing_mode='stretch_width', width_policy='max'),
				status_button, sizing_mode='stretch_width',
			),
			row(legend, fig, sizing_mode='stretch_both',),
			query_textinput,
			sizing_mode='stretch_both',
		)
		super(FigureViewController, self).__init__(view, doc, log)
		self.height_textinput.on_change('value', self.on_change_height_textinput)
		self.auto_update_image = True
		self.options_dropdown = options_dropdown
		self.options_dropdown.on_click(self.on_click_options_dropdown)
		self.actions_dropdown = actions_dropdown
		self.actions_dropdown.on_click(self.on_click_actions_dropdown)
		self.status_button = status_button
		self.status_button.on_click(self.on_click_status_button)
		self.fig = fig
		self.legend = legend
		self.query_textinput = query_textinput
		# Has to be executed before inserting fig in doc
		self.fig.on_event(LODEnd_event, self.callback_LODEnd)
		# Has to be executed before inserting fig in doc
		self.fig.on_event(Reset_event, self.callback_Reset)
		# Has to be executed before inserting fig in doc
		# self.color_key = datashader_color
		self.img = Queue(maxsize=1)
		self.interactiveImage = InteractiveImage(self.fig, self.callback_InteractiveImage)
		self.user_lock = Lock()
		# Forbid widget changes when busy
		self.user_widgets = [
			self.query_textinput,
			self.status_button,
			self.options_dropdown,
			self.actions_dropdown,
		]
		self.query_textinput.on_change('value', self.on_change_query_textinput)
		assert(len(self.fig.renderers) == 1)
		self.datashader = self.fig.renderers[0]
		self.source = ColumnDataSource({})
		self.hovertool = None
		self.hide_hovertool_for_category = None
		self.table = None
Пример #9
0
class FigureViewController(ViewController):
	"""docstring for FigureViewController"""
	def __init__(self,
			model=PointImageModel(),
			x_range=(0,1), # datashader cannot handle 0-sized range
			y_range=(0,1), # datashader cannot handle 0-sized range
			customize_ranges=customize_ranges,
			doc=None,
			log=None,
		):
		self.customize_ranges = customize_ranges
		self.height_textinput = TextInput(
			# value="100",
			width_policy="min",
			# height_policy="min",
			sizing_mode='stretch_width',
		)
		self.query = ''
		self.model = model
		fig = figure(
			x_range=x_range,
			y_range=y_range,
			reset_policy="event_only",
			sizing_mode='stretch_both',
		)
		legend = Div(
			visible=True,
			height_policy='max',
		)
		# fig.add_layout(Legend(click_policy='hide'))
		query_textinput = TextInput(
			title="query",
			sizing_mode="stretch_width",
			value='',
			width=100
		)
		options_dropdown = Dropdown(
			label='Options',
			sizing_mode='fixed',
			menu=[('Show/Hide Legend','legend'),('Enable/Disable Auto Update','auto')]
		)
		actions_dropdown = Dropdown(
			label='Actions',
			sizing_mode='fixed',
			menu=[('Fit Window','fit')],
		)
		status_button = Button(
			label='Auto Update',
			sizing_mode='fixed',
			#sizing_mode='stretch_width',
			width_policy='min',
		)
		view = column(
			row(
				self.height_textinput,
				options_dropdown,
				actions_dropdown,
				Spacer(sizing_mode='stretch_width', width_policy='max'),
				status_button, sizing_mode='stretch_width',
			),
			row(legend, fig, sizing_mode='stretch_both',),
			query_textinput,
			sizing_mode='stretch_both',
		)
		super(FigureViewController, self).__init__(view, doc, log)
		self.height_textinput.on_change('value', self.on_change_height_textinput)
		self.auto_update_image = True
		self.options_dropdown = options_dropdown
		self.options_dropdown.on_click(self.on_click_options_dropdown)
		self.actions_dropdown = actions_dropdown
		self.actions_dropdown.on_click(self.on_click_actions_dropdown)
		self.status_button = status_button
		self.status_button.on_click(self.on_click_status_button)
		self.fig = fig
		self.legend = legend
		self.query_textinput = query_textinput
		# Has to be executed before inserting fig in doc
		self.fig.on_event(LODEnd_event, self.callback_LODEnd)
		# Has to be executed before inserting fig in doc
		self.fig.on_event(Reset_event, self.callback_Reset)
		# Has to be executed before inserting fig in doc
		# self.color_key = datashader_color
		self.img = Queue(maxsize=1)
		self.interactiveImage = InteractiveImage(self.fig, self.callback_InteractiveImage)
		self.user_lock = Lock()
		# Forbid widget changes when busy
		self.user_widgets = [
			self.query_textinput,
			self.status_button,
			self.options_dropdown,
			self.actions_dropdown,
		]
		self.query_textinput.on_change('value', self.on_change_query_textinput)
		assert(len(self.fig.renderers) == 1)
		self.datashader = self.fig.renderers[0]
		self.source = ColumnDataSource({})
		self.hovertool = None
		self.hide_hovertool_for_category = None
		self.table = None

	#######################################
	# Functions triggered by User actions #
	#######################################

	def on_change_height_textinput(self, attr, old, new):
		fname = self.on_change_height_textinput.__name__
		try:
			self.fig.plot_height = int(self.height_textinput.value)
		except Exception as e:
			self.log('Exception({}) in {}:{}'.format(type(e), fname, e))
			self.log(traceback.format_exc())

	def on_click_status_button(self, new):
		fname = self.on_click_status_button.__name__
		if not self.user_lock.acquire(False):
			self.log('Could not acquire user_lock in {}'.format(fname))
			return
		def target():
			try:
				self.set_busy()
				self.update_image()
				self.set_update()
			except Exception as e:
				self.log('Exception({}) in {}:{}'.format(type(e), fname, e))
				self.log(traceback.format_exc())
				self.set_failed()
			else:
				self.user_lock.release()
		Thread(target=target).start()

	def on_click_actions_dropdown(self, new):
		if new.item == 'fit':
			self.action_fit_window()
			pass
		else:
			raise Exception('Exception in on_click_actions_dropdown: {}'.format(new.item))

	def on_click_options_dropdown(self, new):
		# Very short, no need to spawn a Thread
		if new.item == 'legend':
			self.legend.visible = not self.legend.visible
		elif new.item == 'auto':
			self.auto_update_image = not self.auto_update_image
		else:
			raise Exception('Exception in on_click_options_dropdown: {}'.format(new.item))
		pass

	def action_fit_window(self):
		fname = self.fit_window.__name__
		if not self.user_lock.acquire(False):
			self.log('Could not acquire user_lock in {}'.format(fname))
			return
		def target():
			try:
				self.set_busy()
				xmin, xmax, ymin, ymax = self.model.result_ranges()
				self.fit_window(xmin, xmax, ymin, ymax)
				self.set_update()
			except Exception as e:
				self.log('Exception({}) in {}:{}'.format(type(e), fname, e))
				self.log(traceback.format_exc())
				self.set_failed()
			else:
				self.user_lock.release()
		Thread(target=target).start()


	def plot(self, **kwargs):
		fname = self.plot.__name__
		if not self.user_lock.acquire(False):
			self.log('Could not acquire user_lock in {}'.format(fname))
			return
		def target(model=None, config=None, width=None, height=None):
			try:
				self.set_busy()
				self.model = model
				xmin, xmax, ymin, ymax = self.model.data_ranges()
				self._plot(config, width, height, xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax)
				self.set_update()
			except Exception as e:
				self.log('Exception({}) in {}:{}'.format(type(e), fname, e))
				self.log(traceback.format_exc())
				self.set_failed()
			else:
				self.user_lock.release()
		Thread(target=target, kwargs=kwargs).start()

	def on_change_query_textinput(self, attr, old, new):
		fname = self.on_change_query_textinput.__name__
		if not self.user_lock.acquire(False):
			self.log('Could not acquire user_lock in {}'.format(fname))
			return
		def target():
			try:
				self.set_busy()
				self.query = self.query_textinput.value
				if self.auto_update_image:
					self.update_image()
				self.set_update()
			except Exception as e:
				self.log('Exception({}) in {}:{}'.format(type(e), fname, e))
				self.log(traceback.format_exc())
				self.set_failed()
			else:
				self.user_lock.release()
		Thread(target=target).start()

	def callback_LODEnd(self, event):
		fname = self.callback_LODEnd.__name__
		if not self.auto_update_image:
			return
		if not self.user_lock.acquire(False):
			self.log('Could not acquire user_lock in {}'.format(fname))
			return
		def target():
			try:
				self.set_busy()
				self.update_image()
				self.set_update()
			except Exception as e:
				self.log('Exception({}) in {}:{}'.format(type(e), fname, e))
				self.log(traceback.format_exc())
				self.set_failed()
			else:
				self.user_lock.release()
		Thread(target=target).start()

	def callback_Reset(self, event):
		fname = self.callback_Reset.__name__
		if not self.user_lock.acquire(False):
			self.log('Could not acquire user_lock in {}'.format(fname))
			return
		def target():
			try:
				self.set_busy()
				xmin, xmax, ymin, ymax = self.model.data_ranges()
				self.fit_window(xmin, xmax, ymin, ymax)
				self.update_image(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax)
				self.set_update()
			except Exception as e:
				self.log('Exception({}) in {}:{}'.format(type(e), fname, e))
				self.log(traceback.format_exc())
				self.set_failed()
			else:
				self.user_lock.release()
		Thread(target=target).start()

	####################################
	# Functions modifying the document #
	####################################

	# Try to avoid intensive computation
	# Must use coroutine

	def fit_window(self, xmin, xmax, ymin, ymax):
		@gen.coroutine
		def coroutine(xmin, xmax, ymin, ymax):
			self.fig.x_range.start = xmin
			self.fig.x_range.end = xmax
			self.fig.y_range.start = ymin
			self.fig.y_range.end = ymax
		if self.doc:
			self.doc.add_next_tick_callback(partial(coroutine, xmin, xmax, ymin, ymax))

	def set_failed(self):
		@gen.coroutine
		def coroutine():
			for e in self.user_widgets:
				e.disabled= False
			self.visible = True
			self.status_button.label = "Failed"
			self.status_button.button_type = "failure"
		if self.doc:
			self.doc.add_next_tick_callback(partial(coroutine))

	def set_busy(self):
		@gen.coroutine
		def coroutine():
			for e in self.user_widgets:
				e.disabled= True
			self.visible = True
			self.status_button.label = "Busy"
			self.status_button.button_type = "warning"
		if self.doc:
			self.doc.add_next_tick_callback(partial(coroutine))

	def set_update(self):
		@gen.coroutine
		def coroutine():
			for e in self.user_widgets:
				e.disabled= False
			self.visible = True
			if self.auto_update_image:
				self.status_button.label = "Auto Update"
			else:
				self.status_button.label = "Update"
			self.status_button.button_type = "success"
		if self.doc:
			self.doc.add_next_tick_callback(partial(coroutine))

	@ViewController.logFunctionCall
	def _plot(self, config, width, height, xmin, xmax, ymin, ymax):
		@gen.coroutine
		def coroutine(config, width, height, xmin, xmax, ymin, ymax):
			self.fig.x_range.start = xmin
			self.fig.x_range.end = xmax
			self.fig.y_range.start = ymin
			self.fig.y_range.end = ymax
			self.fig.plot_width = width
			self.fig.plot_height = height
			category = config['c']
			self.legend.text = '\n'.join(
				['Categories:<ul style="list-style: none;padding-left: 0;">']+
				[
					'<li><span style="color: {};">◼</span>c[{}]={}</li>'.format(
						category[i]['color'],
						i,
						category[i]['label']
					)
					for i in range(len(category))
					if category[i]['len'] > 0
				]+
				["</ul>"]
			)
			# self.color_key = [c['color'] for c in category if c['len'] > 0]
			self.hide_hovertool_for_category = [
				i
				for i in range(len(category))
				if 'hide_hovertool' in category[i]
				if category[i]['hide_hovertool']
			]
			df = self.model.data
			_df = pd.DataFrame({k:[] for k in df.columns})
			self.source.data = ColumnDataSource.from_df(_df)
			if self.table is not None:
				self.table.columns = [TableColumn(field=c, title=c) for c in _df.columns]
			glyph = self.model.bokeh_glyph()
			renderer = self.fig.add_glyph(self.source, glyph)
			if self.hovertool is None:
				tooltips = [
					("(x,y)","($x, $y)"),
				]
				for k in df.columns:
					tooltips.append((k,"@"+str(k)))
				self.hovertool = HoverTool(tooltips = tooltips)
				self.fig.add_tools(self.hovertool)
			self.update_image()
		if self.doc:
			self.doc.add_next_tick_callback(partial(coroutine, config, width, height, xmin, xmax, ymin, ymax))

	@ViewController.logFunctionCall
	def update_source(self, df):
		@gen.coroutine
		def coroutine(df):
			self.source.data = ColumnDataSource.from_df(df)
			if self.table is not None:
				self.table.columns = [TableColumn(field=c, title=c) for c in df.columns]
		if self.doc is not None:
			self.doc.add_next_tick_callback(partial(coroutine, df))

	@ViewController.logFunctionCall
	def callback_InteractiveImage(self, x_range, y_range, plot_width, plot_height, name=None):
		fname = self.callback_InteractiveImage.__name__
		try:
			img = self.img.get(block=False)
		except Exception as e:
			self.log('Exception({}) in {}: {}'.format(type(e), fname, e))
			self.log(traceback.format_exc())
			img = self._callback_InteractiveImage(x_range, y_range, plot_width, plot_height, name)
		return img

	@ViewController.logFunctionCall
	def fit_figure(self, ranges):
		@gen.coroutine
		def coroutine(ranges):
			self.fig.x_range.start = ranges['xmin']
			self.fig.x_range.end = ranges['xmax']
			self.fig.y_range.start = ranges['ymin']
			self.fig.y_range.end = ranges['ymax']
			self.fig.plot_width = ranges['w']
			self.set_fig_height(ranges['h'])
		if self.doc is not None:
			self.doc.add_next_tick_callback(partial(coroutine, ranges))

	def set_fig_height(self, height):
		height = int(height)
		self.height_textinput.value = str(height)

	###############################
	# Compute intensive functions #
	###############################

	# Should not be ran in the interactive thread

	@ViewController.logFunctionCall
	def apply_query(self):
		fname = self.apply_query.__name__
		if self.mode == 'lines':
			no_query = self.lines
		elif self.mode == 'points':
			no_query = self.points
		else:
			raise Exception('Not Yet Implemented')
		try:
			if self.query.strip() == '':
				return no_query
			self.log('Applying query {}'.format(self.query))
			query = no_query.query(self.query)
			if len(query) == 0:
				raise Exception(
					'QUERY ERROR',
					'{} => len(lines) == 0'.format(self.query)
				)
			return query
		except Exception as e:
			self.log('Exception({}) in {}: {}'.format(type(e), fname, e))
			self.log(traceback.format_exc())
		return no_query

	@ViewController.logFunctionCall
	def _callback_InteractiveImage(self, *args, **kwargs):
		return self.model.callback_InteractiveImage(*args, **kwargs)

	@ViewController.logFunctionCall
	def compute_hovertool(self, ranges):
		MAX = 100000.
		xmin = ranges['xmin']
		xmax = ranges['xmax']
		ymin = ranges['ymin']
		ymax = ranges['ymax']
		intersection = self.model.generate_intersection_query(xmin, xmax, ymin, ymax)
		if len(self.hide_hovertool_for_category)==0:
			query = intersection
		else:
			hide_hovertool = "&".join([
				"(c!={})".format(c)
				for c in self.hide_hovertool_for_category
			])
			query = "({})&({})".format(intersection, hide_hovertool)
		self.log("HoverTool query={}".format(query))
		result = self.model.result.query(query)
		n = len(result)
		if n > MAX:
			frac = MAX/n
			self.log('Sampling hovertool frac={}'.format(frac))
			result = result.sample(frac=frac)
		else:
			self.log('Full hovertool')
		df = dask.compute(result)[0]
		self.update_source(df)

	@ViewController.logFunctionCall
	def update_image(self, **kwargs):
		ranges = {
			'xmin' : self.fig.x_range.start,
			'xmax' : self.fig.x_range.end,
			'ymin' : self.fig.y_range.start,
			'ymax' : self.fig.y_range.end,
			'w' : self.fig.plot_width,
			'h' : self.fig.plot_height,
		}
		for k in ['xmin', 'xmax', 'ymin', 'ymax', 'w', 'h']:
			if k in kwargs:
				ranges[k] = kwargs[k]
		ranges = self.customize_ranges(ranges)
		self.model.apply_query(self.query)
		def target0():
			try:
				self.compute_hovertool(ranges)
			except Exception as e:
				self.log('Exception({}) in {}:{}'.format(type(e),target0.__name__,e))
				self.log(traceback.format_exc())
		def target1():
			try:
				xmin, xmax, ymin, ymax, w, h = [
					ranges[k] for k in ['xmin', 'xmax', 'ymin', 'ymax', 'w', 'h']
				]
				self.log(ranges) # debug
				self.img.put(self._callback_InteractiveImage((xmin,xmax), (ymin,ymax), w, h))
				@gen.coroutine
				def coroutine():
					self.interactiveImage.update_image(ranges)
				if self.doc:
					self.doc.add_next_tick_callback(partial(coroutine))
			except Exception as e:
				self.log('Exception({}) in {}:{}'.format(type(e),target1.__name__,e))
				self.log(traceback.format_exc())
		threads = [Thread(target=target0), Thread(target=target1)]
		for t in threads:
			t.start()
		for t in threads:
			t.join()
		self.fit_figure(ranges)
def overlay_image(map, callback, **kwargs):
    callback = decorate_callback(callback)
    return InteractiveImage(map, callback, **kwargs)
Пример #11
0
def main():
    args = sys.argv[1:]
    if not args:
        print('usage description to come later!')

    if '--windingnumbertest' in args:
        print('Running only the test section of code!')

        output_file("test.html", title="test")

        seta(.615)
        setb(.4)

        ys = np.linspace(-.3, .2, 100)

        print('starting calculation of winding numbers')
        omegas = [windingNumber(y) for y in ys]
        print('finished with that! Now to plot!')

        p = figure(title='winding number test',
                   x_range=(-.3, .2),
                   y_range=(.57, .605))
        p.line(ys, omegas)
        show(p)

        sys.exit()
    """
	Below is the code where I was working on datashader stuff.  Where I left it it
	it was saving the image very quickly.  However, I was not getting the zooming 
	functionality of InteractiveImage.  This is because InteractiveImage is set up 
	to work in a jupyter notebook and not in bokeh server mode.  There were promising 
	things I found on google that might resolve this.  
	"""
    if '--dstest' in args:

        seta(.615)
        setb(.4)

        #output_file("datashadertest.html", title="DStest")
        output_server('hover')

        initconds = np.random.rand(5e2, 2) - .5
        orbitarray = orbit(initconds, int(1e4))

        x_range, y_range = ((-.5, .5), (-.5, .5))

        plot_width = int(750)
        plot_height = plot_width // 1.3

        background = 'black'
        export = functools.partial(ds.utils.export_image,
                                   export_path="export",
                                   background=background)

        points = np.array([[], []])
        print('putting the orbits into arrays for plotting...')
        for i in range(len(orbitarray[:, 0, 0])):
            points = np.append(points, orbitarray[i, :, :], axis=1)
        points[0, :] = points[0, :] % 1 - .5

        df = pd.DataFrame(points.T, columns=['x', 'y'])
        print('finished. Here are the last three rows:', df.tail(3))

        def create_image(x_range, y_range, w=plot_width, h=plot_height):
            canvas = ds.Canvas(plot_width=plot_width,
                               plot_height=plot_height,
                               x_range=x_range,
                               y_range=y_range)
            agg = canvas.points(df, 'x', 'y')
            img = tf.interpolate(agg, cmap=ds.colors.Hot, how='log')
            return tf.dynspread(img, threshold=0.5, max_px=4)

        def base_plot(tools='pan,wheel_zoom,reset',
                      plot_width=plot_width,
                      plot_height=plot_height,
                      **plot_args):
            p = figure(tools=tools,
                       plot_width=plot_width,
                       plot_height=plot_height,
                       x_range=x_range,
                       y_range=y_range,
                       outline_line_color=None,
                       min_border=0,
                       min_border_left=0,
                       min_border_right=0,
                       min_border_top=0,
                       min_border_bottom=0,
                       **plot_args)
            p.axis.visible = True
            p.xgrid.grid_line_color = None
            p.ygrid.grid_line_color = None
            return p

        print('making the image now...')
        session = bokeh.client.push_session(curdoc())

        p = base_plot(background_fill_color=background)
        export(create_image(x_range, y_range), 'testexport')
        print('image saved.')

        #curdoc().add_periodic_callback(create_image, 50)
        session.show()
        InteractiveImage(p, create_image, throttle=200)
        session.loop_until_closed()

    if '--a' in args:
        index = args.index('--a')
        del args[index]
        a = args.pop(index)
        seta(a)

    if '--b' in args:
        index = args.index('--b')
        del args[index]
        b = args.pop(index)
        setb(b)

    if '--orbitLength' in args:
        index = args.index('--orbitLength')
        del args[index]
        orbitLength = args.pop(index)
        orbitLength = int(orbitLength)
        initconds = np.random.rand(200, 2)
        orbitarray = orbit(initconds, orbitLength)

    if '--plot' in args:
        if not orbitLength:
            print(
                'please specify orbit length using command line flag --orbitLength'
            )
            sys.exit()
        #output_file("test.html", title="SNTM plots!!!")
        output_server('hover')
        p = figure(title='testplot!',
                   x_range=(0, 1),
                   y_range=(-.5, .5),
                   webgl=True)
        for i in range(len(orbitarray[:, 0, 0])):
            p.circle(orbitarray[i, 0, :], orbitarray[i, 1, :], size=.1)
        print('showing plot...')
        show(p)
        print('plot should be showing')

    return