Note

This documentation is for a development version. Click here for the latest stable release (v0.5.0).

Visualization tools

GraphViz diagrams

nengo_extras.graphviz.net_diagram(net)

Create a .dot file showing nodes, ensmbles, and connections.

nengo_extras.graphviz.obj_conn_diagram(objs, …)

Create a .dot file showing nodes, ensmbles, and connections.

nengo_extras.graphviz.net_diagram(net)[source]

Create a .dot file showing nodes, ensmbles, and connections.

This can be useful for debugging and testing builders that manipulate the model graph before construction.

Parameters
netNetwork

A network from which objects and connections will be extracted.

Returns
textstring

Text content of the desired .dot file.

nengo_extras.graphviz.obj_conn_diagram(objs, connections)[source]

Create a .dot file showing nodes, ensmbles, and connections.

This can be useful for debugging and testing builders that manipulate the model graph before construction.

Parameters
objslist of Nodes and Ensembles

All the nodes and ensembles in the model.

connectionslist of Connections

All the connections in the model.

Returns
textstring

Text content of the desired .dot file.

Image manipulation

nengo_extras.gui.preprocess_display(x[, …])

Basic preprocessing that reshapes, transposes, and scales an image

nengo_extras.gui.image_function(image_shape)

Make a function to turn an array into a PIL Image.

nengo_extras.gui.image_string_function(…)

Make a function to turn an array into an image string.

nengo_extras.gui.image_html_function(image_shape)

Make a function to turn an image into HTML to display as an SVG.

nengo_extras.gui.image_display_function(…)

Make a function to display images in Nengo GUI.

nengo_extras.gui.preprocess_display(x, transpose=(1, 2, 0), scale=255.0, offset=0.0)[source]

Basic preprocessing that reshapes, transposes, and scales an image

nengo_extras.gui.image_function(image_shape, preprocess=<function preprocess_display>, **preprocess_args)[source]

Make a function to turn an array into a PIL Image.

Parameters
image_shapearray_like (3,)

The shape of the image: (channels, height, width)

preprocesscallable

Callable that takes an image and preprocesses it to be displayed.

preprocess_argsdict

Optional dictionary of keyword arguments for preprocess.

Returns
to_pilcallable (x)

A function that takes a (flattened) image array, returning a PIL Image.

nengo_extras.gui.image_string_function(image_shape, format='PNG', preprocess=<function preprocess_display>, **preprocess_args)[source]

Make a function to turn an array into an image string.

Parameters
image_shapearray_like (3,)

The shape of the image: (channels, height, width)

formatstring

A format string for the PIL.Image.save function.

preprocesscallable

Callable that takes an image and preprocesses it to be displayed.

preprocess_argsdict

Optional dictionary of keyword arguments for preprocess.

Returns
string_functioncallable (x)

A function that takes a (flattened) image array, and returns a base64 string representation of the image of the requested format.

See also

image_function
nengo_extras.gui.image_html_function(image_shape, preprocess=<function preprocess_display>, **preprocess_args)[source]

Make a function to turn an image into HTML to display as an SVG.

Parameters
image_shapearray_like (3,)

The shape of the image: (channels, height, width)

preprocesscallable

Callable that takes an image and preprocesses it to be displayed.

preprocess_argsdict

Optional dictionary of keyword arguments for preprocess.

Returns
html_functioncallable (x)

A function that takes a (flattened) image array, and returns a string that defines an SVG object in HTML to display the image.

See also

image_function
nengo_extras.gui.image_display_function(image_shape, preprocess=<function preprocess_display>, **preprocess_args)[source]

Make a function to display images in Nengo GUI.

Examples

Displaying images from a PresentInput process in NengoGUI.

from nengo_extras.gui import image_display_function

with nengo.Network() as net:
    u = nengo.Node(nengo.processes.PresentInput([[0], [0.5], [1.0]], 0.1))
    display_f = image_display_function((1, 1, 1))
    display_node = nengo.Node(display_f, size_in=u.size_out)
    nengo.Connection(u, display_node, synapse=None)

Matplotlib plots

nengo_extras.matplotlib.imshow(image[, ax, …])

Nicer version of Matplotlib’s imshow.

nengo_extras.matplotlib.tile(images[, ax, …])

Plot a grid of images

nengo_extras.matplotlib.compare(image_sets)

Compare sets of images in a grid.

nengo_extras.plot_spikes.plot_spikes(t, spikes)

Plots a spike raster.

nengo_extras.plot_spikes.preprocess_spikes(t, …)

Applies a default preprocessing to spike data for plotting.

nengo_extras.plot_spikes.cluster(t, spikes, …)

Change order of spike trains to have similar ones close together.

nengo_extras.plot_spikes.merge(t, spikes, num)

nengo_extras.plot_spikes.sample_by_variance(t, …)

Samples the spike trains with the highest variance.

nengo_extras.plot_spikes.sample_by_activity(t, …)

Samples the spike trains with the highest spiking activity.

nengo_extras.matplotlib.imshow(image, ax=None, vmin=None, vmax=None, invert=False, interpolation='none', axes=False)[source]

Nicer version of Matplotlib’s imshow.

  • By default, show the raw image with no interpolation.

  • If the image is greyscale, use grey colormap.

nengo_extras.matplotlib.tile(images, ax=None, rows=9, cols=12, grid=False, gridwidth=1, gridcolor='r', **show_params)[source]

Plot a grid of images

Parameters
imagesndarray (n_images, height, width, channels)

Array of images to display.

nengo_extras.matplotlib.compare(image_sets, ax=None, rows=4, cols=12, grid=True, gridwidth=1, gridcolor='r', **show_params)[source]

Compare sets of images in a grid.

Parameters
image_setslist of (n_images, height, width, channels) ndarray

List of the sets of images to compare. Each set of images must be the same size.

nengo_extras.plot_spikes.plot_spikes(t, spikes, contrast_scale=1.0, ax=None, **kwargs)[source]

Plots a spike raster.

Will use an alpha channel by default which allows to plot colored regions below the spike raster to add highlights. You can set the cmap keyword argument to a different color map like matplotlib.cm.gray_r to get an opaque spike raster.

Utilizes Matplotlib’s imshow.

Parameters
t(n,) array

Time indices of spike matrix. The indices are assumed to be equidistant.

spikes(n, m) array

Spike data for m neurons at n time points.

contrast_scalefloat, optional

Scales the contrst of the spike raster. This value multiplied with the maximum value in spikes will determine the minimum spike value to appear black (or the corresponding color of the chosen colormap).

axmatplotlib.axes.Axes, optional

Axes to plot onto. Uses the current axes by default.

kwargsdict

Additional keyword arguments will be passed on to imshow.

Returns
matplotlib.image.AxesImage

The spikeraster.

nengo_extras.plot_spikes.preprocess_spikes(t, spikes, num=50, sample_size=200, sample_filter_width=0.02, cluster_filter_width=0.002)[source]

Applies a default preprocessing to spike data for plotting.

This will first sample by variance, then cluster the spike trains, and finally merge them. See sample_by_variance, cluster, and merge for details.

Parameters
t(n,) array

Time indices of spike matrix. The indices are assumed to be equidistant.

spikes(n, m) array

Spike data for m neurons at n time points.

numint, optional

Number of spike trains to return after merging.

sample_sizeint, optional

Number of spike trains to sample by variance.

sample_filter_widthfloat, optional

Gaussian filter width in seconds for sampling by variance.

cluster_filter_widthfloat, optional

Gaussian filter width in seconds for clustering.

Returns
tuple (t, selected_spikes)

Returns the time indices t and the preprocessed spike trains spikes.

nengo_extras.plot_spikes.cluster(t, spikes, filter_width)[source]

Change order of spike trains to have similar ones close together.

Requires SciPy.

Parameters
t(n,) array

Time indices of spike matrix. The indices are assumed to be equidistant.

spikes(n, m) array

Spike data for m neurons at n time points.

filter_widthfloat

Gaussian filter width in seconds, controls the time scale the clustering is sensitive to.

Returns
tuple (t, selected_spikes)

Returns the time indices t and the selected spike trains spikes.

nengo_extras.plot_spikes.merge(t, spikes, num)[source]
nengo_extras.plot_spikes.sample_by_variance(t, spikes, num, filter_width)[source]

Samples the spike trains with the highest variance.

Requires SciPy.

Parameters
t(n,) array

Time indices of spike matrix. The indices are assumed to be equidistant.

spikes(n, m) array

Spike data for m neurons at n time points.

numint

Number of spike trains to return.

filter_widthfloat

Gaussian filter width in seconds, controls the time scale the variance calculation is sensitive to.

Returns
tuple (t, selected_spikes)

Returns the time indices t and the selected spike trains spikes.

nengo_extras.plot_spikes.sample_by_activity(t, spikes, num, blocksize=None)[source]

Samples the spike trains with the highest spiking activity.

Parameters
t(n,) array

Time indices of spike matrix. The indices are assumed to be equidistant.

spikes(n, m) array

Spike data for m neurons at n time points.

numint

Number of spike trains to return.

blocksizeint, optional

If not None, the spike trains will be divided into blocks of this size and the highest activity spike trains are obtained for each block individually.

Returns
tuple (t, selected_spikes)

Returns the time indices t and the selected spike trains spikes.

TkInter GUIs

nengo_extras.deepview.ImageSelector(parent, …)

Choose between images with left and right arrows

nengo_extras.deepview.ScrollCanvasFrame(parent)

Construct a frame widget with the parent MASTER.

nengo_extras.deepview.ScrollWindow(*args, …)

Scroll a single window

nengo_extras.deepview.VerticalImageFrame(…)

Construct a frame widget with the parent MASTER.

nengo_extras.deepview.Viewer(images, …)

Return a new Toplevel widget on screen SCREENNAME.

class nengo_extras.deepview.ImageSelector(parent, **kwargs)[source]

Choose between images with left and right arrows

Attributes
image_functioncallable

Turn an array into a PIL.Image. Can be generated by nengo_extras.gui.image_function.

resampleint

Resampling mode for PIL.Image.resize.

Construct a frame widget with the parent MASTER.

Valid resource names: background, bd, bg, borderwidth, class, colormap, container, cursor, height, highlightbackground, highlightcolor, highlightthickness, relief, takefocus, visual, width.

class nengo_extras.deepview.ScrollCanvasFrame(parent, vertical=False, horizontal=False, **kwargs)[source]

Construct a frame widget with the parent MASTER.

Valid resource names: background, bd, bg, borderwidth, class, colormap, container, cursor, height, highlightbackground, highlightcolor, highlightthickness, relief, takefocus, visual, width.

class nengo_extras.deepview.ScrollWindow(*args, **kwargs)[source]

Scroll a single window

Construct a frame widget with the parent MASTER.

Valid resource names: background, bd, bg, borderwidth, class, colormap, container, cursor, height, highlightbackground, highlightcolor, highlightthickness, relief, takefocus, visual, width.

class nengo_extras.deepview.VerticalImageFrame(parent, **kwargs)[source]

Construct a frame widget with the parent MASTER.

Valid resource names: background, bd, bg, borderwidth, class, colormap, container, cursor, height, highlightbackground, highlightcolor, highlightthickness, relief, takefocus, visual, width.

class Column(width, height, image_function)[source]

Create new instance of Column(width, height, image_function)

height

Alias for field number 1

image_function

Alias for field number 2

width

Alias for field number 0

class nengo_extras.deepview.Viewer(images, image_function, *args, **kwargs)[source]

Return a new Toplevel widget on screen SCREENNAME. A new Tcl interpreter will be created. BASENAME will be used for the identification of the profile file (see readprofile). It is constructed from sys.argv[0] without extensions if None is given. CLASSNAME is the name of the widget class.

Gephi visualization

nengo_extras.gexf.DispatchTable([parent])

A descriptor to dispatch to other methods depending on argument type.

nengo_extras.gexf.HierarchicalLabeler()

Obtains labels for objects in a Nengo network.

nengo_extras.gexf.GexfConverter([labeler, …])

Converts Nengo models into GEXF files.

nengo_extras.gexf.CollapsingGexfConverter([…])

Converts Nengo models into GEXF files with some collapsed networks.

nengo_extras.gexf.Attr(id, type, default)

class nengo_extras.gexf.DispatchTable(parent=None)[source]

A descriptor to dispatch to other methods depending on argument type.

How to use: assign the descriptor to a class attribute and use the register decorator to declare which functions to dispatch to for specific types:

class MyClass(object):
    dispatch = DispatchTable()

    @dispatch.register(TypeA)
    def handle_type_a(self, obj_of_type_a):
        # ...

    @dispatch.register(TypeB)
    def handle_type_b(self, obj_of_type_b):
        # ...

To then call the method for the appropriate type:

inst = MyClass()
inst.dispatch(obj_of_type_a_or_b)

If multiple methods would match (e.g. if TypeB inherits from TypeA), the most specific method will be used (to be precise: the first type in the method resolution order with a registered method will be used).

The DispatchTable descriptor accepts another DispatchTable as argument which will be used as a fallback. This allows to inherit the dispatch table and selectively overwrite methods like so:

class Inherited(MyClass):
    dispatch = DispatchTable(MyClass.dispatch)

    @dispatch.register(TypeA)
    def alternate_type_a_handler(self, obj_of_type_a):
        # ...

Finally, dispatch methods can also be changed on a per-instance basis:

inst.dispatch.register(TypeA, inst_type_a_handler)
class InstDispatch(param, inst, owner)[source]

Return value when accessing the dispatch table on an instance.

register(type_, fn)[source]
class nengo_extras.gexf.HierarchicalLabeler[source]

Obtains labels for objects in a Nengo network.

The names will include the network hierarchy.

Usage example:

labels = HierarchicalLabeler().get_labels(model)
class nengo_extras.gexf.GexfConverter(labeler=None, hierarchical=False)[source]

Converts Nengo models into GEXF files.

This can be loaded in Gephi for visualization of the model graph.

Links:

This class can be inherited from to customize the conversion or alternatively the dispatch table can be changed on a per-instance basis.

Note that probes are currently not included in the graph.

The following attributes will be stored on graph nodes:

  • type: type of the Nengo object (e.g., nengo.ensemble.Ensemble),

  • net: unique ID of the containing network,

  • net_label: (possibly non-unique) label of the containing network,

  • size_in: input size,

  • size_out: output_size,

  • radius: ensemble radius (unset for other nodes),

  • n_neurons: number of neurons (0 for non-ensembles),

  • neuron_type: string representation of the neuron type (unset for non-ensembles).

The following attributes will be stored on graph edges:

  • pre_type: type of the connection’s pre object (e.g., nengo.ensemble.Neurons),

  • post_type: type of the connection’s post object (e.g., nengo.ensemble.Neurons),

  • synapse: string representation of the synapse,

  • tau: the tau parameter of the synapse if existent,

  • function: string representation of the connection’s function,

  • transform: string representation of the connection’s transform,

  • scalar_transform: float representation of the transform if it is a scalar,

  • learning_rule_type: string representation of the connection’s learning rule type.

Parameters
labeleroptional

Object with a get_labels method that returns a dictionary mapping model objects to labels. If not given, a new HierarchicalLabeler will be used.

hierarchicalbool, optional (default: False)

Whether to include information of the network hierarchy in the file. Support for hierarchical graphs was removed in Gephi 0.9 and hierarchical networks will be automatically flattened which leaves an unconnected node for every network.

Examples

Basic usage to write a GEXF file:

GexfConverter().convert(model).write('model.gexf')
convert(model)[source]

Convert a model to GEXF format.

Returns
xml.etree.ElementTree.ElementTree

Converted model.

make_document(model)[source]

Create the GEXF XML document from model.

This method is exposed so it can be overwritten in inheriting classes. Invoke convert instead of this method to convert a model.

Returns
xml.etree.ElementTree.ElementTree

Converted model.

make_attr_defs(cls, defs)[source]

Generate an attribute definition block.

Parameters
clsstr

Class the attribute definitions are for (‘node’ or ‘edge’).

defsdict

Attribute definitions. Maps attribute names to Attr instances.

Returns
xml.etree.ElementTree.Element
make_attrs(defs, attrs)[source]

Generates a block of attribute values.

Parameters
defsdict

Attribute definitions. Maps attribute names to Attr instances.

attrsdict

Mapping of attribute names to assigned values.

Returns
xml.etree.ElementTree.Element
make_node(obj, **attrs)[source]

Generate a node for obj with attributes attrs.

make_edge(obj, source, target, **attrs)[source]

Edge for obj from source to target with attributes attrs.

get_node_obj(obj)[source]

Get an object with a corresponding graph node related to obj.

For certain objects like nengo.ensemble.Neurons or nengo.connection.LearningRule no graph node will be created. This function will resolve such an object to a related object that has a corresponding graph node (e.g., the ensemble for a neurons object or the pre object for a learning rule).

In GexfConverter this is used to make sure connections are between the correct nodes and do not introduce unrelated dangling nodes.

class nengo_extras.gexf.CollapsingGexfConverter(to_collapse=None, labeler=None, hierarchical=False)[source]

Converts Nengo models into GEXF files with some collapsed networks.

See GexfConverter for general information on conversion to GEXF files. This class will collapse certain networks to a single node in the conversion.

Parameters
to_collapsesequence, optional

Network types to collapse, if not given the networks listed in NENGO_NETS and SPA_NETS will be collapsed. Note that SPA_NETS currently only contains networks from nengo_spa, but not the spa module in core nengo.

labeleroptional

Object with a get_labels method that returns a dictionary mapping model objects to labels. If not given, a new HierarchicalLabeler will be used.

hierarchicalbool, optional (default: False)

Whether to include information of the network hierarchy in the file. Support for hierarchical graphs was removed in Gephi 0.9 and hierarchical networks will be automatically flattened which leaves an unconnected node for every network.

convert_collapsed(net)[source]

Used to convert a network into a collapsed graph node.

get_node_obj(obj)[source]

Get an object with a corresponding graph node related to obj.

For certain objects like nengo.ensemble.Neurons or nengo.connection.LearningRule no graph node will be created. This function will resolve such an object to a related object that has a corresponding graph node (e.g., the ensemble for a neurons object or the pre object for a learning rule).

In GexfConverter this is used to make sure connections are between the correct nodes and do not introduce unrelated dangling nodes.

class nengo_extras.gexf.Attr(id, type, default)[source]

Create new instance of Attr(id, type, default)

default

Alias for field number 2

id

Alias for field number 0

type

Alias for field number 1