Graph classesΒΆ

class nngt.Graph(nodes=0, name='Graph', weighted=True, directed=True, from_graph=None, **kwargs)[source]

The basic class that contains a graph_tool.Graph and some of is properties or methods to easily access them.

Initialize Graph instance

Parameters:
  • nodes (int, optional (default: 0)) – Number of nodes in the graph.
  • name (string, optional (default: “Graph”)) – The name of this Graph instance.
  • weighted (bool, optional (default: True)) – Whether the graph edges have weight properties.
  • directed (bool, optional (default: True)) – Whether the graph is directed or undirected.
  • from_graph (GraphObject, optional) – An optional GraphObject to serve as base.
  • kwargs (optional keywords arguments) – Optional arguments that can be passed to the graph, e.g. a dict containing information on the synaptic weights (weights={"distribution": "constant", "value": 2.3} which is equivalent to weights=2.3), the synaptic delays, or a type information.
Returns:

self (Graph)

attributes(edge=None, name=None)[source]

Attributes of the graph’s edges.

Parameters:
  • edge (tuple, optional (default: None)) – Edge whose attribute should be displayed.
  • name (str, optional (default: None)) – Name of the desired attribute.
Returns:

  • List containing the names of the graph’s attributes (synaptic weights,
  • delays...) if edge is None, else a dict containing the
  • attributes of the edge (or the value of attribute name if it is not
  • None).

copy()[source]

Returns a deepcopy of the current Graph instance

edge_attribute

Access edge attributes

static from_file(filename, format='auto', delimiter=' ', secondary=';', attributes=None, notifier='@', ignore='#', from_string=False)[source]

Import a saved graph from a file. @todo: implement population and shape loading, implement gml, dot, xml, gt

Parameters:
  • filename (str) – The path to the file.
  • format (str, optional (default: “neighbour”)) – The format used to save the graph. Supported formats are: “neighbour” (neighbour list, default if format cannot be deduced automatically), “ssp” (scipy.sparse), “edge_list” (list of all the edges in the graph, one edge per line, represented by a source target-pair), “gml” (gml format, default if filename ends with ‘.gml’), “graphml” (graphml format, default if filename ends with ‘.graphml’ or ‘.xml’), “dot” (dot format, default if filename ends with ‘.dot’), “gt” (only when using graph_tool`<http://graph-tool.skewed.de/>_ as library, detected if `filename ends with ‘.gt’).
  • delimiter (str, optional (default ” ”)) – Delimiter used to separate inputs in the case of custom formats (namely “neighbour” and “edge_list”)
  • secondary (str, optional (default: ”;”)) – Secondary delimiter used to separate attributes in the case of custom formats.
  • attributes (list, optional (default: [])) – List of names for the attributes present in the file. If a notifier is present in the file, names will be deduced from it; otherwise the attributes will be numbered.
  • notifier (str, optional (default: “@”)) – Symbol specifying the following as meaningfull information. Relevant information is formatted @info_name=info_value, where info_name is in (“attributes”, “directed”, “name”, “size”) and associated info_value``s are of type (``list, bool, str, int). Additional notifiers are @type=SpatialGraph/Network/ SpatialNetwork, which must be followed by the relevant notifiers among @shape, @population, and @graph.
  • from_string (bool, optional (default: False)) – Load from a string instead of a file.
Returns:

graph (Graph or subclass) – Loaded graph.

classmethod from_library(library_graph, weighted=True, directed=True, **kwargs)[source]
classmethod from_matrix(matrix, weighted=True, directed=True)[source]

Creates a Graph from a scipy.sparse matrix or a dense matrix.

Parameters:
  • matrix (scipy.sparse matrix or numpy.array) – Adjacency matrix.
  • weighted (bool, optional (default: True)) – Whether the graph edges have weight properties.
  • directed (bool, optional (default: True)) – Whether the graph is directed or undirected.
Returns:

Graph

get_attribute_type(attribute_name)[source]

Return the type of an attribute

get_betweenness(btype='both', use_weights=False)[source]

Betweenness centrality sequence of all nodes and edges.

Parameters:
  • btype (str, optional (default: "both")) – Type of betweenness to return ("edge", "node"-betweenness, or "both").
  • use_weights (bool, optional (default: False)) – Whether to use weighted (True) or simple degrees (False).
Returns:

  • node_betweenness (numpy.array) – Betweenness of the nodes (if btype is "node" or "both").
  • edge_betweenness (numpy.array) – Betweenness of the edges (if btype is "edge" or "both").

get_degrees(deg_type='total', node_list=None, use_weights=False)[source]

Degree sequence of all the nodes.

Parameters:
  • deg_type (string, optional (default: “total”)) – Degree type (among ‘in’, ‘out’ or ‘total’).
  • node_list (list, optional (default: None)) – List of the nodes which degree should be returned
  • use_weights (bool, optional (default: False)) – Whether to use weighted (True) or simple degrees (False).
Returns:

numpy.array or None (if an invalid type is asked).

get_delays()[source]

Returns the delay adjacency matrix as a scipy.sparse.lil_matrix if delays are present; else raises an error.

get_density()[source]

Density of the graph: \(\frac{E}{N^2}\), where E is the number of edges and N the number of nodes.

get_edge_types()[source]
get_graph_type()[source]

Return the type of the graph (see nngt.generation)

get_name()[source]

Get the name of the graph

get_weights()[source]

Returns the weighted adjacency matrix as a scipy.sparse.lil_matrix.

graph_id

Unique int identifying the instance.

is_directed()[source]

Whether the graph is directed or not

is_network()[source]

Whether the graph is a subclass of Network (i.e. if it has a NeuralPop attribute).

is_spatial()[source]

Whether the graph is embedded in space (i.e. if it has a Shape attribute). Returns True is the graph is a subclass of SpatialGraph.

is_weighted()[source]

Whether the edges have weights

static make_network(graph, neural_pop, copy=False, **kwargs)[source]

Turn a Graph object into a Network, or a SpatialGraph into a SpatialNetwork.

Parameters:
  • graph (Graph or SpatialGraph) – Graph to convert
  • neural_pop (NeuralPop) – Population to associate to the new Network
  • copy (bool, optional (default: False)) – Whether the operation should be made in-place on the object or if a new object should be returned.

Notes

In-place operation that directly converts the original graph if copy is False, else returns the copied Graph turned into a Network.

static make_spatial(graph, shape, positions=None, copy=False)[source]

Turn a Graph object into a SpatialGraph, or a Network into a SpatialNetwork.

Parameters:
  • graph (Graph or SpatialGraph) – Graph to convert.
  • shape (Shape, optional) – Shape to associate to the new SpatialGraph.
  • positions ((N, 2) array) – Positions, in a 2D space, of the N neurons.
  • copy (bool, optional (default: False)) – Whether the operation should be made in-place on the object or if a new object should be returned.

Notes

In-place operation that directly converts the original graph if copy is False, else returns the copied Graph turned into a SpatialGraph.

name

Name of the graph.

node_attributes

Access node attributes

classmethod num_graphs()[source]

Returns the number of alive instances.

set_delays(delay=None, elist=None, distribution=None, parameters=None, noise_scale=None)[source]

Set the delay for spike propagation between neurons. ..todo :: take elist into account in Connections.delays

Parameters:
  • delay (float or class:numpy.array, optional (default: None)) – Value or list of delays (for user defined delays).
  • elist (class:numpy.array, optional (default: None)) – List of the edges (for user defined delays).
  • distribution (class:string, optional (default: None)) – Type of distribution (choose among “constant”, “uniform”, “gaussian”, “lognormal”, “lin_corr”, “log_corr”).
  • parameters (dict, optional (default: {})) – Dictionary containing the properties of the delay distribution.
  • noise_scale (class:int, optional (default: None)) – Scale of the multiplicative Gaussian noise that should be applied on the delays.
set_edge_attribute(attribute, values=None, val=None, value_type=None, edges=None)[source]

Set attributes to the connections between neurons.

Warning

The special “type” attribute cannot be modified when using graphs that inherit from the Network class. This is because for biological networks, neurons make only one kind of synapse, which is determined by the nngt.NeuralGroup they belong to.

set_name(name='')[source]

set graph name

set_types(syn_type, nodes=None, fraction=None)[source]

Set the synaptic/connection types.

Warning

The special “type” attribute cannot be modified when using graphs that inherit from the Network class. This is because for biological networks, neurons make only one kind of synapse, which is determined by the nngt.NeuralGroup they belong to.

Parameters:
  • syn_type (int or string) – Type of the connection among ‘excitatory’ (also 1) or ‘inhibitory’ (also -1).
  • nodes (int, float or list, optional (default: None)) – If nodes is an int, number of nodes of the required type that will be created in the graph (all connections from inhibitory nodes are inhibitory); if it is a float, ratio of syn_type nodes in the graph; if it is a list, ids of the syn_type nodes.
  • fraction (float, optional (default: None)) – Fraction of the selected edges that will be set as syn_type (if nodes is not None, it is the fraction of the specified nodes’ edges, otherwise it is the fraction of all edges in the graph).
Returns:

t_list (numpy.ndarray) – List of the types in an order that matches the edges attribute of the graph.

set_weights(weight=None, elist=None, distribution=None, parameters=None, noise_scale=None)[source]

Set the synaptic weights.

..todo ::
take elist into account in Connections.weights
Parameters:
  • weight (float or class:numpy.array, optional (default: None)) – Value or list of the weights (for user defined weights).
  • elist (class:numpy.array, optional (default: None)) – List of the edges (for user defined weights).
  • distribution (class:string, optional (default: None)) – Type of distribution (choose among “constant”, “uniform”, “gaussian”, “lognormal”, “lin_corr”, “log_corr”).
  • parameters (dict, optional (default: {})) – Dictionary containing the properties of the weight distribution.
  • noise_scale (class:int, optional (default: None)) – Scale of the multiplicative Gaussian noise that should be applied on the weights.
to_file(filename, format='auto', delimiter=' ', secondary=';', attributes=None, notifier='@')[source]

Save graph to file; options detailed below.

See also

nngt.lib.save_to_file() function for options.

type

Type of the graph.

class nngt.SpatialGraph(nodes=0, name='Graph', weighted=True, directed=True, from_graph=None, shape=None, positions=None, **kwargs)[source]

The detailed class that inherits from Graph and implements additional properties to describe spatial graphs (i.e. graph where the structure is embedded in space.

Initialize SpatialClass instance. .. todo:: see what we do with the from_graph argument

Parameters:
  • nodes (int, optional (default: 0)) – Number of nodes in the graph.
  • name (string, optional (default: “Graph”)) – The name of this Graph instance.
  • weighted (bool, optional (default: True)) – Whether the graph edges have weight properties.
  • directed (bool, optional (default: True)) – Whether the graph is directed or undirected.
  • shape (Shape, optional (default: None)) – Shape of the neurons’ environment (None leads to a square of side 1 cm)
  • positions (numpy.array (N, 2), optional (default: None)) – Positions of the neurons; if not specified and nodes is not 0, then neurons will be reparted at random inside the Shape object of the instance.
Returns:

self (SpatialGraph)

get_positions(neurons=None)[source]

Returns the neurons’ positions as a (N, 2) array.

Parameters:neurons (int or array-like, optional (default: all neurons)) – List of the neurons for which the position should be returned.
shape
class nngt.Network(name='Network', weighted=True, directed=True, from_graph=None, population=None, **kwargs)[source]

The detailed class that inherits from Graph and implements additional properties to describe various biological functions and interact with the NEST simulator.

Initializes Network instance.

Parameters:
  • nodes (int, optional (default: 0)) – Number of nodes in the graph.
  • name (string, optional (default: “Graph”)) – The name of this Graph instance.
  • weighted (bool, optional (default: True)) – Whether the graph edges have weight properties.
  • directed (bool, optional (default: True)) – Whether the graph is directed or undirected.
  • from_graph (GraphObject, optional (default: None)) – An optional GraphObject to serve as base.
  • population (nngt.NeuralPop, (default: None)) – An object containing the neural groups and their properties: model(s) to use in NEST to simulate the neurons as well as their parameters.
Returns:

self (Network)

classmethod ei_network(size, ei_ratio=0.2, en_model='aeif_cond_alpha', en_param=None, es_model='static_synapse', es_param=None, in_model='aeif_cond_alpha', in_param=None, is_model='static_synapse', is_param=None)[source]

Generate a network containing a population of two neural groups: inhibitory and excitatory neurons.

Parameters:
  • size (int) – Number of neurons in the network.
  • ei_ratio (double, optional (default: 0.2)) – Ratio of inhibitory neurons: \(\frac{N_i}{N_e+N_i}\).
  • en_model (string, optional (default: ‘aeif_cond_alpha’)) – Nest model for the excitatory neuron.
  • en_param (dict, optional (default: {})) – Dictionary of parameters for the the excitatory neuron.
  • es_model (string, optional (default: ‘static_synapse’)) – NEST model for the excitatory synapse.
  • es_param (dict, optional (default: {})) – Dictionary containing the excitatory synaptic parameters.
  • in_model (string, optional (default: ‘aeif_cond_alpha’)) – Nest model for the inhibitory neuron.
  • in_param (dict, optional (default: {})) – Dictionary of parameters for the the inhibitory neuron.
  • is_model (string, optional (default: ‘static_synapse’)) – NEST model for the inhibitory synapse.
  • is_param (dict, optional (default: {})) – Dictionary containing the inhibitory synaptic parameters.
Returns:

net (Network or subclass) – Network of disconnected excitatory and inhibitory neurons.

get_neuron_type(neuron_ids)[source]

Return the type of the neurons (+1 for excitatory, -1 for inhibitory).

Parameters:neuron_ids (int or tuple) – NEST gids.
Returns:ids (int or tuple) – Ids in the network. Same type as the requested gids type.
id_from_nest_gid(gids)[source]

Return the ids of the nodes in the nngt.Network instance from the corresponding NEST gids.

Parameters:gids (int or tuple) – NEST gids.
Returns:ids (int or tuple) – Ids in the network. Same type as the requested gids type.
nest_gid
neuron_properties(idx_neuron)[source]

Properties of a neuron in the graph.

Parameters:idx_neuron (int) – Index of a neuron in the graph.
Returns:dict of the neuron’s properties.
classmethod num_networks()[source]

Returns the number of alive instances.

population

NeuralPop that divides the neurons into groups with specific properties.

set_types(syn_type, nodes=None, fraction=None)[source]
to_nest(use_weights=True)[source]

Send the network to NEST.

See also

make_nest_network() for parameters

classmethod uniform_network(size, neuron_model='aeif_cond_alpha', neuron_param=None, syn_model='static_synapse', syn_param=None)[source]

Generate a network containing only one type of neurons.

Parameters:
  • size (int) – Number of neurons in the network.
  • neuron_model (string, optional (default: ‘aief_cond_alpha’)) – Name of the NEST neural model to use when simulating the activity.
  • neuron_param (dict, optional (default: {})) – Dictionary containing the neural parameters; the default value will make NEST use the default parameters of the model.
  • syn_model (string, optional (default: ‘static_synapse’)) – NEST synaptic model to use when simulating the activity.
  • syn_param (dict, optional (default: {})) – Dictionary containing the synaptic parameters; the default value will make NEST use the default parameters of the model.
Returns:

net (Network or subclass) – Uniform network of disconnected neurons.

class nngt.SpatialNetwork(population, name='Graph', weighted=True, directed=True, shape=None, from_graph=None, positions=None, **kwargs)[source]

Class that inherits from Network and SpatialGraph to provide a detailed description of a real neural network in space, i.e. with positions and biological properties to interact with NEST.

Initialize Graph instance

Parameters:
  • name (string, optional (default: “Graph”)) – The name of this Graph instance.
  • weighted (bool, optional (default: True)) – Whether the graph edges have weight properties.
  • directed (bool, optional (default: True)) – Whether the graph is directed or undirected.
  • shape (Shape, optional (default: None)) – Shape of the neurons’ environment (None leads to a square of side 1 cm)
  • positions (numpy.array, optional (default: None)) – Positions of the neurons; if not specified and nodes != 0, then neurons will be reparted at random inside the Shape object of the instance.
  • population (class:~nngt.NeuralPop, optional (default: None))
Returns:

self (SpatialNetwork)

set_types(syn_type, nodes=None, fraction=None)[source]