Fork me on GitHub

pyhrf.graph module

Module to handle graphs. Base structures : - undirected, unweighted graph: a list of neighbours index (list of numpy array).

pyhrf.graph.bfs_set_label(g, root, data, value, radius)
pyhrf.graph.bfs_sub_graph(g, root, radius)

Traverses a graph in breadth-first order.

The first argument should be the tree root; visitable should be an iterable with all searchable nodes;

pyhrf.graph.center_mask_at(mask, pos, indexes, toroidal=False)
pyhrf.graph.center_mask_at_v01(mask, pos, shape)
pyhrf.graph.center_mask_at_v02(mask, pos, shape)
pyhrf.graph.connected_components(g)
pyhrf.graph.connected_components_iter(g)
pyhrf.graph.flatten_and_graph(data, mask=None, kerMask=None, depth=1, toroidal=False)
pyhrf.graph.graph_from_lattice(mask, kerMask=None, depth=1, toroidal=False)

Creates a graph from a n-dimensional lattice ‘mask’ define valid positions to build the graph over. ‘kerMask’ is numpy array mask (tuple of arrays) which defines the neighbourhood system, ie the relative positions of neighbours for a given position in the lattice.

pyhrf.graph.graph_from_lattice3D(mask, kerMask=None, depth=1, toroidal=False)

Creates a graph from a n-dimensional lattice ‘mask’ define valid positions to build the graph over. ‘kerMask’ is numpy array mask (tuple of arrays) which defines the neighbourhood system, ie the relative positions of neighbours for a given position in the lattice.

pyhrf.graph.graph_from_mesh(polygonList)

Return the list of neighbours indexes for each position, from a list of polygons. Each polygon is a triplet.

pyhrf.graph.graph_is_sane(g, toroidal=False)

Check the structure of graph ‘g’, which is a list of neighbours index. Return True if check is ok, else False. -> every nid in g[id] should verify id in g[nid] -> any neighbours list must have unique elements -> no isolated node

pyhrf.graph.graph_nb_cliques(graph)
pyhrf.graph.graph_pool_indexes(g)
pyhrf.graph.graph_pygraph(g)
pyhrf.graph.graph_to_sparse_matrix(graph)

Creates a connectivity sparse matrix from the adjacency graph (list of neighbors list)

pyhrf.graph.parcels_to_graphs(parcellation, kerMask, toKeep=None, toDiscard=None)

Compute graphs for each parcel in parcels. A graph is simply defined as a list of neighbour indexes.

Parameters:
  • parcellation – is a n-ary numpy array.
  • kerMask – defines the connectivity
  • toKeep
  • toDiscard
Returns:

Return type:

a dictionary mapping a roi ID to its graph

pyhrf.graph.split_mask_into_cc_iter(mask, min_size=0, kerMask=None)

Return an iterator over all connected components (CC) within input mask. CC which are smaller than min_size are discarded. kerMask defines the connectivity, e.g., kerMask3D_6n for 6-neighbours in 3D.

Examples

vol = np.array( [[1,1,0,1,1],
                [1,1,0,1,1],
                [0,0,0,0,0],
                [1,0,1,1,0],
                [0,0,1,1,0]], dtype=int )
for cc in split_mask_into_cc_iter(vol):
    print cc

Should output:

np.array( [[1,1,0,0,0],
           [1,1,0,0,0],
           [0,0,0,0,0],
           [0,0,0,0,0],
           [0,0,0,0,0]]
np.array( [[0,0,0,1,1],
           [0,0,0,1,1],
           [0,0,0,0,0],
           [0,0,0,0,0],
           [0,0,0,0,0]]
pyhrf.graph.sub_graph(graph, nodes)