schrodinger.ui.qt.network_visualizer module

network_visualizer.py

Description: This package is meant to help with the visualization of network- connection data, in conjunction with network_views.py. A good example of the type of data this is meant to visualize is at: http://networkx.lanl.gov/index.html

The Graph class is meant as a wrapper for networkx.Graph objects, which can then act as a model for AbstractNetworkView and the associated network view classes defined in network_views.py.

Copyright Schrodinger, LLC. All rights reserved.

class schrodinger.ui.qt.network_visualizer.GraphSignals[source]

Bases: PyQt5.QtCore.QObject

selectionChanged
positionChanged
nodesChanged
nodesAdded
nodesDeleted
edgesChanged
graphChanged
undoPointSet
__init__(*args, **kwargs)
blockSignals(self, bool) bool
childEvent(self, QChildEvent)
children(self) List[QObject]
connectNotify(self, QMetaMethod)
customEvent(self, QEvent)
deleteLater(self)
destroyed

destroyed(self, object: QObject = None) [signal]

disconnect(QMetaObject.Connection) bool
disconnect(self) None
disconnectNotify(self, QMetaMethod)
dumpObjectInfo(self)
dumpObjectTree(self)
dynamicPropertyNames(self) List[QByteArray]
event(self, QEvent) bool
eventFilter(self, QObject, QEvent) bool
findChild(self, type, name: str = '', options: Union[Qt.FindChildOptions, Qt.FindChildOption] = Qt.FindChildrenRecursively) QObject
findChild(self, Tuple, name: str = '', options: Union[Qt.FindChildOptions, Qt.FindChildOption] = Qt.FindChildrenRecursively) QObject
findChildren(self, type, name: str = '', options: Union[Qt.FindChildOptions, Qt.FindChildOption] = Qt.FindChildrenRecursively) List[QObject]
findChildren(self, Tuple, name: str = '', options: Union[Qt.FindChildOptions, Qt.FindChildOption] = Qt.FindChildrenRecursively) List[QObject]
findChildren(self, type, QRegExp, options: Union[Qt.FindChildOptions, Qt.FindChildOption] = Qt.FindChildrenRecursively) List[QObject]
findChildren(self, Tuple, QRegExp, options: Union[Qt.FindChildOptions, Qt.FindChildOption] = Qt.FindChildrenRecursively) List[QObject]
findChildren(self, type, QRegularExpression, options: Union[Qt.FindChildOptions, Qt.FindChildOption] = Qt.FindChildrenRecursively) List[QObject]
findChildren(self, Tuple, QRegularExpression, options: Union[Qt.FindChildOptions, Qt.FindChildOption] = Qt.FindChildrenRecursively) List[QObject]
inherits(self, str) bool
installEventFilter(self, QObject)
isSignalConnected(self, QMetaMethod) bool
isWidgetType(self) bool
isWindowType(self) bool
killTimer(self, int)
metaObject(self) QMetaObject
moveToThread(self, QThread)
objectName(self) str
objectNameChanged

objectNameChanged(self, str) [signal]

parent(self) QObject
property(self, str) Any
pyqtConfigure(...)

Each keyword argument is either the name of a Qt property or a Qt signal. For properties the property is set to the given value which should be of an appropriate type. For signals the signal is connected to the given value which should be a callable.

receivers(self, PYQT_SIGNAL) int
removeEventFilter(self, QObject)
sender(self) QObject
senderSignalIndex(self) int
setObjectName(self, str)
setParent(self, QObject)
setProperty(self, str, Any) bool
signalsBlocked(self) bool
startTimer(self, int, timerType: Qt.TimerType = Qt.CoarseTimer) int
staticMetaObject = <PyQt5.QtCore.QMetaObject object>
thread(self) QThread
timerEvent(self, QTimerEvent)
tr(self, str, disambiguation: str = None, n: int = - 1) str
class schrodinger.ui.qt.network_visualizer.Graph(ggraph=None, node_class=None, edge_class=None)[source]

Bases: object

A model class for an undirected graph. This wraps around the NetworkX Graph class and provides QT signals, a easier-to-use API, and access control.

All persistent data should be stored in self._ggraph.

Note that Graph itself cannot be pickled; Graph has Graph.signals, which is a QObject and cannot be pickled. For this reason selection information (which contains references to Graph) is not placed in self._ggraph, so that self._ggraph can be pickled.

__init__(ggraph=None, node_class=None, edge_class=None)[source]

Constructs a new Graph object

Parameters
  • ggraph (networkx.Graph) – The graph underlying this graph.

  • node_class (class) – The class to represent the graph’s nodes (should be subclass of Node)

  • edge_class (class) – The class to represent the graph’s edges (should be subclass of Edge)

property ggraph
update()[source]

Update any derived aspects of the graph after changes.

setEdgeValidator(validator)[source]

Set an edge validator that will be run when adding edges between nodes. :param validator: the validator :type validator: ConnectionValidator

toNetworkX()[source]

Return a copy of the underlying NetworkX graph.

getData(key)[source]

Return the requested item from the graph’s data dictionary. Returns None if the key is not found.

setData(key, value, signal=True)[source]

Set the value of an item in the graph’s data dictionary.

isConnected()[source]

Checks whether the graph is connected, that is, whether every node is connected by some path to every other node.

Returns

Whether the graph is connected rtype: bool

nodeCount()[source]
Returns

the number of nodes in the graph

Return type

int

getIsolates()[source]
Returns

a complete set of nodes in the graph that have degree 0

Return type

set(Node)

getConnectedComponents(nodes=None)[source]

Return a set of nodes for each connected component in the graph.

Parameters

nodes (set(Node) or NoneType) – optionally, a set of nodes to filter the returned components. If provided, this method will only return components for which at least one node is in nodes

Returns

a generater over each connected component in the graph

Return type

typing.Generator[set[Node], None, None]

getNodeConnectedComponent(node)[source]

Return a set of nodes that are part of the same connected component as node.

Parameters

node (Node) – a node

Returns

a set of nodes connected to node through any path of edges

Return type

set(Node)

getNode(node_key)[source]

Retrieve a node via its name. Retrieved nodes are cached, so getting the same Node again will return the same instance. Returns None if no matching Node exists.

Parameters

node_key (object) – a node, gnode, or string that corresponds to the desired node

Returns

a node if found, else None

Return type

Node or NoneType

getNodes(node_keys=None)[source]

Retrieve a set of nodes optionally indicated by a list of keys. If none is provided, return all nodes.

Parameters

node_keys (list(object) or NoneType) – optionally, a list of nodes, gnodes, or strings that correspond to the desired nodes

Returns

a set of nodes

Return type

set(Node)

getNeighbors(node)[source]

Return a set of all nodes connected to a specified node

Parameters

node (Node) – center node

Returns

neighboring nodes

Return type

set of Node

addNodes(nodes, signal=True)[source]

Add a list of nodes to this graph. The nodes argument can either be a list of Node objects or a list of hashable objects that can be used as new gnodes.

Note that any time a new gnode is created for use in this graph, its string representation must be unique among the other nodes in this graph: nodes are keyed in the node_objects dictionary by the string representation of their corresponding gnode.

Parameters
  • nodes (list(object) or list(Node)) – list of gnodes or nodes

  • signal (bool) – whether the addNodes signal should be emitted when done

Returns

a set of added nodes

Return type

set(Node)

addNode(node, signal=True)[source]

Convenience method for adding a single node to the graph. See addNodes() for full documentation.

Parameters
  • node (hashable or Node) – gnode or node

  • signal (bool) – whether the addNodes signal should be emitted when done

Returns

the added node

Return type

Node

removeNodes(nodes, signal=True)[source]

Remove specified nodes from the graph and optionally emit a signal.

Parameters
  • nodes (list(Node)) – a list of nodes to be removed

  • signal (bool) – whether to emit a nodesDeleted signal when done

removeNode(node, signal=True)[source]

Convenience function for removing a single node. See removeNode() for full documentation.

Parameters
  • node (object or Node) – a gnode or node to remove

  • signal (bool) – whether to emit a nodesDeleted signal when done

setMultipleNodePos(pos_dict, signal=True)[source]

Set the positions of nodes from a dictionary.

Parameters

pos_dict (dict {Node : (int, int)}) – A dictionary mapping nodes to (x,y) tuples.

edgeCount()[source]
Returns

the number of edges in the graph

Return type

int

hasEdge(node1, node2)[source]

Return whether there is an edge between the supplied nodes.

Parameters
  • node1 (Node) – a node from this graph

  • node2 (Node) – a node from this graph

Returns

whether there exists an edge between the two supplied nodes

Return type

bool

getGEdge(node0, node1)[source]

Return the underlying gedge object corresponding to two supplied nodes. This can be overwritten in subclasses, but the returned class should define a consistent edge ordering that is independent of the order of the supplied node parameters.

Parameters
  • node0 (Node) – a node

  • node1 (Node) – a node

Returns

the underlying gedge between the two nodes, if it exists

Return type

tuple(networkx.Node) or NoneType

getEdge(node0, node1)[source]

Given two nodes, return the corresponding edge if it exists.

Parameters
  • node0 (Node) – a node

  • node1 (Node) – a node

Returns

the edge connecting the two nodes if it exists

Return type

Edge or NoneType

getEdges(nodes=None)[source]

Return all edges connected to a node or set of nodes. If no node is specified, all the edges in the graph are returned.

Parameters

nodes (iterable(Node), Node, or None) – optionally a node or iterable of nodes

Returns

a set of edges connected to at least one of the supplied nodes, or a set of all edges if nodes is not specified

Return type

set(Edge)

addEdges(edge_tuples, signal=True)[source]

Add edges to graph.

Parameters
  • edge_tuples (list(tuple(Node, Node, dict)) or list(tuple(Node, Node, None))) – list of tuples indicating the edges to add, containing two gnodes or nodes and an edge attribute dictionary (or None)

  • signal (bool) – whether edgesChanged signal should be emitted when done

addEdge(node1, node2, signal=True, data=None)[source]

Convenience function to add a single edge to the graph given two nodes. The order of the nodes does not matter.

Parameters
  • node1 (object or Node) – a gnode or node connected by the edge

  • node2 (object or Node) – a gnode or node connected by the edge

  • signal (bool) – whether edgesChanged signal should be emitted when done

removeEdges(edges, signal=True)[source]

Removes specified edges from the graph.

Parameters
  • edges (list(Edge)) – a list of edges

  • signal (bool) – whether edgesChanged signal should be emitted when done

removeEdge(edge, signal=True)[source]

Convenience function to remove a single edge from the graph.

Parameters
  • edge (Edge) – an edge

  • signal (bool) – whether edgesChanged signal should be emitted when done

getEdgeApproval(node1, node2)[source]

Test whether a new edge can be added between two nodes. Doesn’t actually add an edge, just returns whether it is allowable to add.

selectedNodes()[source]

Return the currently selected nodes.

Return type

set of Nodes

selectedEdges()[source]
Returns

the set of selected edges

Return type

set(Edge)

setSelectedObjs(objs, source=None, signal=True)[source]

Specify the current selection.

Parameters
  • objs (list(Node or Edge)) – a list of objects (nodes or edges) to be selected

  • source (object) – the class instance calling this method (used to avoid infinite recursion when updating selection state)

  • signal (bool) – whether to emit a signal when changing selection state

springLayout(signal=True)[source]

Performs a spring layout on the current graph.

minCrossingSpringLayout(num_iterations=100, fixed_nodes=None, fraction=1.0)[source]

Perform multiple spring layouts and keep the one with the fewest edge intersections, keeping the original positions if the layout could not be improved.

Parameters
  • num_iterations (int) – number of spring layouts to try

  • fixed (iterable of Node) – nodes for which the position should be fixed

  • signal (bool) – whether to emit the positionChanged signal

  • fraction (float) – stop iterating if no reduction in crossings is found within this fraction of num_iterations

hasPositions(accept_partial=False)[source]

Determines whether the nodes in this graph have x-y coordinates.

Parameters

accept_partial (bool) – if set to True, the method will check whether at least one node has coordinates. Otherwise it requires that all nodes have coordinates.

getState()[source]

Get the current state of the Graph

setState(state)[source]

Set the current state of the Graph

setUndoPoint(signal=True)[source]

Store the current state to the undo stack. Also wipes out the redo stack.

undo()[source]

Revert to the last state on the undo stack.

redo()[source]

Undo the undo

clearUndoHistory()[source]

Clears both undo and redo stacks

merge(g)[source]

Merge data from another graph into this graph. Nodes with duplicate names will be considered to be the same ligand.

Parameters

g (Graph) – graph from which data is being merged.

deleteSelectedItems(include_edges=True, include_nodes=True)[source]

Delete selected nodes and/or selected edges.

Parameters
  • include_edges (bool) – whether selected edges should be deleted

  • include_nodes (bool) – whether selected nodes should be deleted

deleteItems(nodes=None, edges=None)[source]

Delete specified nodes and edges from the FEP map.

Parameters
  • nodes (Set[Node]) – nodes to delete

  • edges (Set[Tuple[Node, Node]]) – edges to delete

class schrodinger.ui.qt.network_visualizer.Node(name, graph=None)[source]

Bases: object

Model class for Node. Wraps the NetworkX Graph.node dictionary.

x_key = 'storedX'
y_key = 'storedY'
__init__(name, graph=None)[source]

Construct a Node object. Most of the time, this will be constructed around an existing NetworkX node (i.e. an entry in the networkx.Graph.node dict). If a graph is specified, a node of the same name must exist in the graph, or a ValueError will result.

QT signals will only be emitted if a graph is specified.

Parameters
  • name (hashable) – a unique identifier for this node

  • graph (Graph) – the graph object to which this node belongs

Variables
  • _gnode – the underlying graph node that this node wraps. In this class, we use the node name as the graph node, but any hashable object can be used.

  • _gdata – dictionary that stores data belonging to the underlying graph node.

property gnode

Return the underlying graph node object wrapped by this Node instance (not the data dictionary _gdata).

property name

Return unique string associated with this node. Convert to string for subclasses which do not necessarily use strings as graph nodes.

x()[source]
y()[source]
pos()[source]

Returns the Node’s current position coordinates. Returns None if there are no coordinates.

Return type

tuple (float, float)

setX(x, signal=True)[source]
setY(y, signal=True)[source]
setPos(x, y, signal=True)[source]

Set the node’s position coordinates

Parameters
  • x (float) – x coordinate

  • y (float) – y coordinate

gdata()[source]

Directly access the node data dictionary. Use this object carefully, as directly altering its contents can lead to internal inconsistencies.

This may be wrapped to restrict access.

getData(key)[source]

Return the requested item from the node’s data dictionary. Returns None if the key is not found.

setData(key, value, signal=True)[source]

Set the value of an item in the node’s data dictionary.

property degree
Returns

the degree (number of edges) of the node

Return type

int

class schrodinger.ui.qt.network_visualizer.Edge(gedge, graph)[source]

Bases: object

__init__(gedge, graph)[source]
Parameters
  • gedge (object) – the underlying edge object wrapped by this object

  • graph (Graph) – the graph object to which this edge belongs

property gedge
Returns

the underlying edge object wrapped by this object

Return type

fep.graph.Edge

property graph
Returns

the graph to which this edge belongs

Return type

Graph

property nodes
Returns

the nodes connected by this edge in a consistent order, as determined by the underlying graph edge

Return type

tuple(Node, Node)

data()[source]
Returns

the data dictionary associated with this edge

Return type

dict(str, object)

getData(key)[source]

Return the requested item from the edge’s data dictionary. Returns None if the key is not found.

Parameters

key (str) – the data item key

Returns

the value stored under the specified key in the edge’s data dictionary, or None if it is not found

Return type

object

setData(key, value, signal=True)[source]

Set the specified item in the edge’s data dictionary.

Parameters
  • key (str) – the data item key

  • value (object) – the value to set for the data item

property name
Returns

the name of the edge, a composite of the connected node names

Return type

str

class schrodinger.ui.qt.network_visualizer.ConnectionValidator[source]

Bases: object

Create a subclass of this and assign it using NetworkViewer.setConnectionValidator( ) to do extra work making sure node’s are compatible to connect. val1 and val2 are node1.val and node2.val

__init__()[source]
validate(node1, node2)[source]
firstNode()[source]
setFirstNode(node)[source]
validateSecondVal(val)[source]
class schrodinger.ui.qt.network_visualizer.AbstractNetworkView[source]

Bases: object

A base class for views on Graph models. Use setModel to replace the model object. Signals from the model are automatically connected to appropriate synchronization slots.

The abstract view does not provide any built-in support for effecting changes back into the model (ex. deleting nodes, changing selection). Any such operations should be implemented in the subclass by making calls directly to the model. These changes will then be automatically synchronized forward to all views.

self.nodes is a dictionary mapping model node objects to view node objects.

self.edges is a dictionary mapping pairs of model node objects to view edge objects. There is no such thing as a edge model object.

Note that all references to the word node and edge in method names refer to view objects. For example, makeNode() will make a view node, addEdge() will add an edge view object to the view.

Variables
  • MODEL_CLASS (Graph or subclass of Graph) – an instance of this class will be created as the default model when setModel

  • _sync_with_model (bool) – whether to automatically synchronize this view (and its subviews) with the model

MODEL_CLASS

alias of schrodinger.ui.qt.network_visualizer.Graph

__init__()[source]
syncAll()[source]

Synchronize the full model and selection state.

syncRecursive()[source]

Synchronize the full model and selection state on this view and all subviews.

setModelSyncEnabled(enable)[source]

Enable or disable automatic synchronization with the model for this view and all subviews.

setModel(model)[source]

Set the model for this view and synchronize to it. Any subviews will have the model set on them as well.

Parameters

model (Graph) – the graph model

getSignalsAndSlots(model)[source]

Get a list of signal/slot pairs for a model. This list will be used when setting a new model to disconnect the old model signals from their slots and connect the new model’s signals to those slots.

Override this method to modify or extend signals/slots in derived classes.

Parameters

model (Graph) – the graph model

addSubview(subview)[source]

Add a subview to this view. A subview is another AbstractNetworkView that should always have the same model as its parent view (this view).

Adding will automatically set its model to the current model. Changing the model on this view will result in all its subviews getting the new model set

Parameters

subview (AbstractNetworkView) – the new subview to add to this view

removeSubview(subview)[source]

Removes the specified subview. The subview is not deleted or altered, and the model remains set.

Parameters

subview

syncModel()[source]
syncNodes()[source]
syncNodesDeleted(nodes)[source]
syncNodesAdded(nodes)[source]
syncNodesChanged(nodes)[source]
syncEdges()[source]
syncSelection(selection, source)[source]
makeNodes(nodes)[source]

Create new view nodes and return a dictionary mapping supplied model nodes to corresponding view nodes. Do not add new view nodes to the view.

By default this method returns an “identity dictionary” that maps nodes to themselves. Subclasses should override this method to implement their own view nodes.

Parameters

nodes (list(Node)) – model nodes

Returns

a dictionary mapping supplied nodes to view nodes

Return type

dict(Node, object)

makeNode(node)[source]

Convenience method for calling makeNodes() with a single node. Rather than returning a dictionary mapping nodes to view nodes, returns the view node corresponding to the supplied node.

Parameters

node (Node) – the model node

Returns

the view node

Return type

object

addNode(viewnode)[source]

A convenience function for calling addNodes() for a single node.

Parameters

viewnode (object) – a view node

removeNode(viewnode)[source]

Convenience method for calling removeNode() for a single node.

Parameters

viewnode (object) – a view node

updateNode(node)[source]

Convenience method for calling updateNodes() for a single node.

Parameters

node (Node) – the model node to update to

getModelNodes(node_keys=None)[source]

Retrieve a set of model nodes optionally indicated by a list of keys. If none is provided, return all nodes.

Parameters

node_keys (list(object) or NoneType) – optionally, a list of nodes, gnodes, or strings that correspond to the desired model nodes

Returns

a set of nodes

Return type

set(Node)

getNode(node)[source]
Parameters

node (Node) – a model node

Returns

corresponding view node, if available

Return type

object or None

makeEdges(edges)[source]

Given a list of model edges, return a dictionary mapping them to corresponding view edges. Does not add view edges to the view.

By default this method returns an identity dictionary, mapping model edges to themselves. Subclasses should override this method if they want to implement their own view edges.

Parameters

edges – a list model nodes

Returns

a dictionary mapping model edges to view edges

Return type

dict(Edge, object)

makeEdge(edge)[source]

Convenience method for calling makeEdges() for a single edge. Rather than return a dictionary mapping model edges to view edges, returns a singe view edge. Does not add a view edge to the view.

Parameters

edge (Edge) – a model edge

Returns

a view edge

Return type

object

addEdge(viewedge)[source]

Convenience method for calling addEdges() for a single edge.

Parameters

viewedge (object) – the view edge to add to the view

removeEdge(viewedge)[source]

Convenience method for calling removeEdges() for a single edge.

Parameters

viewedge (object) – the view edge to remove from the view

updateEdge(edge)[source]

A convenience method for calling updateEdges() for a single edge.

Parameters

edge (Edge) – the model edge corresponding to the view edge to update

getModelEdges(nodes=None)[source]

Return all model edges connected to a model node or set of model nodes. If no node is specified, all the edges in the graph are returned. This method acts like Graph.getEdges(), but it filters for model edges that are available in this view.

Parameters

nodes (list(Node), Node, or None) – optionally a node or list of nodes

Returns

a list of model edges

Return type

list(Edge)

getEdge(edge)[source]

Return the view edge corresponding to the supplied model edge.

Parameters

edge (Edge) – a model edge

Returns

the corresponding view edge if available

Return type

object or None

getEdges(nodes=None)[source]

Return a list of view edges, filtering the list so that the edges are connected to the optionally-supplied node or iterable of nodes.

Parameters

nodes (iterable[Node] or Node or NoneType) – a node or iterable of nodes

Returns

list of view edges

Return type

list[NetworkEdge or NoneType]

addNodes(viewnodes)[source]

Takes view nodes and adds them to the view if that makes sense (eg. add graphics items to scene, add rows to table, etc.) It should not add the view node to self.nodes; that is handled in _addNodes().

Parameters

viewnodes (list(object)) – view nodes to add to the view

removeNodes(viewnodes)[source]

Removes view nodes from the view if that makes sense (eg. remove graphics items from scene, remove table rows, etc.) It should not remove view nodes from self.nodes; that is handled in _removeNodes().

Parameters

viewnodes (list(object)) – a list of view nodes

updateNodes(nodes)[source]

Performs any operations necessary to update the view to the current model state. Note that this method takes model nodes, not view nodes.

Parameters

nodes (list(Node)) – model nodes which must have their views updated

addEdges(viewedges)[source]

Adds view edges to the view. Does not add view edges to self.edges.

Parameters

viewedges (list(object)) – view edges to add to the view

removeEdges(viewedges)[source]

Removes view edges from the view. Does not remove view edges from self.edges.

Parameters

viewedges (list(object)) – view edges to remove from the view

updateEdges(edges)[source]

Performs any operations necessary to update the view to the current model state.

Parameters

edges (list(Edge)) – a list of model edges corresponding to view edges that should be updated

selectItems(selected_view_objects)[source]

Selects view objects in the view. Currently only view nodes will be requested, but may be expanded to allow a combination of nodes and edges to be selected.

Parameters

selected_view_objects (list(object)) – a list of view objects to be selected

schrodinger.ui.qt.network_visualizer.perp(a)[source]
schrodinger.ui.qt.network_visualizer.seg_intersect(a1, a2, b1, b2)[source]

Checks whether two line segments cross each other.

Parameters
  • a1 (numpy.array) – first endpoint of line segment a

  • a2 (numpy.array) – second endpoint of line segment a

  • b1 (numpy.array) – first endpoint of line segment b

  • b2 (numpy.array) – second endpoint of line segment b

Returns

whether the line segments intersect

Return type

bool

schrodinger.ui.qt.network_visualizer.fruchterman_reingold_layout(G, dim=2, pos=None, fixed=None, iterations=50, weight='weight', scale=1)[source]

Position nodes using Fruchterman-Reingold force-directed algorithm.

Parameters
  • G – NetworkX graph

  • dim (int) – Dimension of layout

  • pos (dict) – Initial positions for nodes as a dictionary with node as keys and values as a list or tuple. If None, then use random initial positions.

  • fixed (list) – Nodes to keep fixed at initial position. optional

  • iterations (int) – Number of iterations of spring-force relaxation

  • weight (str or None) – The edge attribute that holds the numerical value used for the edge weight. If None, then all edge weights are 1.

  • scale (float) – Scale factor for positions

Return type

dict

Returns

A dictionary of positions keyed by gnode

Examples:

>>> G=nx.path_graph(4)
>>> pos=nx.spring_layout(G)

# The same using longer function name
>>> pos=nx.fruchterman_reingold_layout(G)
schrodinger.ui.qt.network_visualizer.spring_layout(G, dim=2, pos=None, fixed=None, iterations=50, weight='weight', scale=1)

Position nodes using Fruchterman-Reingold force-directed algorithm.

Parameters
  • G – NetworkX graph

  • dim (int) – Dimension of layout

  • pos (dict) – Initial positions for nodes as a dictionary with node as keys and values as a list or tuple. If None, then use random initial positions.

  • fixed (list) – Nodes to keep fixed at initial position. optional

  • iterations (int) – Number of iterations of spring-force relaxation

  • weight (str or None) – The edge attribute that holds the numerical value used for the edge weight. If None, then all edge weights are 1.

  • scale (float) – Scale factor for positions

Return type

dict

Returns

A dictionary of positions keyed by gnode

Examples:

>>> G=nx.path_graph(4)
>>> pos=nx.spring_layout(G)

# The same using longer function name
>>> pos=nx.fruchterman_reingold_layout(G)