ddn3.visualize2
Visualization functions of DDN, improved v2 version
These functions are still quite basic. For advanced plotting of networks, consider using specialized tools.
Module Contents
Functions
|
Draw networks for DDN |
|
Create NetworkX graph based on edge data frame. |
|
Draw the network |
|
Find positions of nodes with multiple isolated subgraphs. |
|
Find positions of nodes on a circle |
|
Add labels to a graph |
|
Find positions in an ellipse, and calculate the minimum distances between points |
|
Calculate angles of evenly spaced points in an ellipse |
- ddn3.visualize2.draw_network_for_ddn(edges_df, nodes_to_draw, mode='common', nodes_type=None, cen_lst=None, rad_lst=None, labels=None, fig_size=(16, 9), font_size_scale=1, node_size_scale=1, pdf_name='')
Draw networks for DDN
Support drawing any number of ellipses according to nodes_type. The positions and shapes are given in cen_lst and rad_lst. This makes the layout of the graphs more flexible.
The direction of the labels now points to the center of each ellipse. This allows showing larger fonts.
The node size, font size, edge weight are now automatically adjusted according to figure size and node number.
For common network, if two nodes in an edge have same type, draw grey line. If two nodes in an edge have different type, draw green line.
For differential network, if an edge comes from condition 1, draw blue line. If an edge comes from condition 2, draw red line.
- Parameters:
edges_df (pandas.DataFrame) – Edge information. First two columns for the two feature names. Third column for edge type (common=0, diff1=1, diff2=2) Fourth column for weight.
nodes_to_draw (list of str) – Name of nodes to draw
fig_size (tuple, optional) – Size of figure.
font_size_scale (int, optional) – Scale of fonts, by default 1 To make the font larger, set larger value. The default value is scaled according to the node number.
node_size_scale (int, optional) – Scale of node sizes, by default 2
nodes_type (None or dict, optional) – Node type (e.g., Gene=0, TF=1), by default None
cen_lst (None or ndarray, optional) – The center of ellipse for each type of node. For node type i, use cen_lst[i] Shape (k, 2), k is the number of types. 2 for (x, y) Assume the y-axis of the figure is in range [-1,1], so do not set too large values.
rad_lst (None or ndarray, optional) – The radius of ellipse for each type of node. For node type i, use rad_lst[i] Shape (k, 2), k is the number of types. 2 for shorter and longer axis length. Assume the y-axis of the figure is in range [-1,1], so do not set too large values.
labels (dict) – Alternative (e.g., simplified) names for nodes
mode (str, optional) – Draw common graph or differential graph, by default “common”
pdf_name (str, optional) – Name of the PDF file to export, by default “”. If set as “”, no pdf will be output.
- Returns:
Graph object
- Return type:
nx.Graph
- ddn3.visualize2.create_nx_graph(edges_df, nodes_show, min_alpha=0.2, max_alpha=1.0, mode='common', nodes_type=None)
Create NetworkX graph based on edge data frame.
Add nodes and edges. Provide visualization related properties to the nodes.
For common network, if two nodes in an edge have same type, draw grey line. If two nodes in an edge have different type, draw green line.
For differential network, if an edge comes from condition 1, draw blue line. If an edge comes from condition 2, draw red line.
- Parameters:
edges_df (pandas.DataFrame) – Edge information. First two columns for the two feature names. Third column for edge type (common=0, diff1=1, diff2=2) Fourth column for weight.
nodes_show (list of str) – Name of nodes to draw
min_alpha (float, optional) – Minimum alpha value of edges, by default 0.2 This is for the most light edges.
max_alpha (float, optional) – Maximum alpha value of edges, by default 1.0
mode (str, optional) – Draw common graph or differential graph, by default “common”
nodes_type (None or dict, optional) – Node type (e.g., Gene=0, TF=1), by default None
- Returns:
Generated graph
- Return type:
nx.Graph
- ddn3.visualize2.plot_network(G, pos, d_min, labels, node_type, cen_lst, fig_size, font_size_scale=1, node_size_scale=2, font_alpha_min=0.4)
Draw the network
- Parameters:
G (nx.Graph) – Graph to draw
pos (dict) – Position of each node
d_min (float) – Minimum distance between nodes. We use this to adjust node size, font size, etc.
labels (dict) – Alternative names for nodes
node_type (None or dict, optional) – Node type (e.g., Gene=0, TF=1), by default None
cen_lst (None or ndarray, optional) – The center of ellipse for each type of node. For node type i, use cen_lst[i] Shape (k, 2), k is the number of types. 2 for (x, y) Assume the y-axis of the figure is in range [-1,1], so do not set too large values.
fig_size (tuple) – Size of figure. The unit is inch.
font_size_scale (int, optional) – Scale of fonts, by default 1 To make the font larger, set larger value. The default value is scaled according to the node number.
node_size_scale (int, optional) – Scale of node sizes, by default 1
font_alpha_min (float, optional) – The smallest alpha value for fonts in labels, between 0 and 1
- ddn3.visualize2.get_pos_multi_parts(nodes_all, nodes_type, cen_lst=np.array([[-0.6, 0], [0.6, 0]]), rad_lst=np.array([[0.4, 1], [0.4, 1]]))
Find positions of nodes with multiple isolated subgraphs.
Each subgraph is an ellipse. Also provides the minimum distances between nodes.
- Parameters:
nodes_all (list of str) – Node names
nodes_type (dict) – feature type ID for each node
cen_lst (None or ndarray, optional) – The center of ellipse for each type of node. For node type i, use cen_lst[i] Shape (k, 2), k is the number of types. 2 for (x, y) Assume the y-axis of the figure is in range [-1,1], so do not set too large values.
rad_lst (None or ndarray, optional) – The radius of ellipse for each type of node. For node type i, use rad_lst[i] Shape (k, 2), k is the number of types. 2 for shorter and longer axis length. Assume the y-axis of the figure is in range [-1,1], so do not set too large values.
- Returns:
pos (dict) – The positions for each node
d_min (float) – Minimum distances between nodes
- ddn3.visualize2._get_pos_one_part(nodes_show, cen=(0.0, 0.0), rad=(1.0, 1.0), pos=None)
Find positions of nodes on a circle
Also provides the minimum distances between nodes.
- Parameters:
nodes_show (tuple of str) – Node names
cen (ndarray, optional) – The center of ellipse for each type of node. Shape (2, ) Assume the y-axis of the figure is in range [-1,1], so do not set too large values.
rad (ndarray, optional) – The radius of ellipse for each type of node. Shape (2, ) Assume the y-axis of the figure is in range [-1,1], so do not set too large values.
pos (dict) – NetworkX position dictionary
- Returns:
pos (dict) – The positions for each node
d_min (float) – Minimum distances between nodes
- ddn3.visualize2._draw_network_labels(ax, pos, d_min, node_type, cen_lst, labels, font_size_lst, font_alpha_lst)
Add labels to a graph
Modified from Networkx add label function
- Parameters:
ax (matplotlib.axes.Axes) – The axes of the figure
pos (dict) – NetworkX position dictionary
d_min (float) – Minimum distance between nodes
node_type (dict) – Feature type index of each node
cen_lst (None or ndarray, optional) – The center of ellipse for each type of node. For node type i, use cen_lst[i] Shape (k, 2), k is the number of types. 2 for (x, y) Assume the y-axis of the figure is in range [-1,1], so do not set too large values.
labels (dict) – Alternative names for each node, to be shown in the graph
font_size_lst (array_like) – Font size for each node, in points
font_alpha_lst (array_like) – Font alpha for each node, value from 0 to 1
- ddn3.visualize2._add_node_to_a_circle(pos, nodes, cen, rad, angles)
Find positions in an ellipse, and calculate the minimum distances between points
- Parameters:
pos (dict) – For saving the position results for NetworkX
nodes (list of str) – Name of nodes
cen (array_like) – Central position of this circle, shape (2,)
rad (array_like) – Length of two axes of the ellipse, shape (2,)
angles (array_like) – Angles of the points
- Returns:
Minimum distances between points
- Return type:
float
- ddn3.visualize2._angles_in_ellipse(num, a, b)
Calculate angles of evenly spaced points in an ellipse
Based on https://stackoverflow.com/a/52062369, which is from https://pypi.org/project/flyingcircus/
- Parameters:
num (int) – Sample number to get
a (float) – Length of shorter axis
b (float) – Length of longer axis
- Returns:
angles – Angles of sampled points
- Return type:
ndarray