ddn3.visualize

Visualization functions of DDN

These functions are quite basic. For advanced plotting of networks, consider using specialized tools.

Module Contents

Functions

draw_network_for_ddn(edges_df, nodes_to_draw[, ...])

Draw networks for DDN

_add_node_to_a_circle(pos, nodes, cen, rad, angles)

Find positions in an ellipse, and calculate the minimum distances between points

_angles_in_ellipse(num, a, b)

Calculate angles of evenly spaced points in an ellipse

_get_pos_one_part(nodes_show)

Find positions of nodes on a circle

_get_pos_two_parts(nodes_show, nodes_type)

Find positions of nodes with two groups.

_create_nx_graph(edges_df, nodes_show[, min_alpha, ...])

Create NetworkX graph based on edge data frame.

_plot_network_helper(G, pos, d_min, labels[, ...])

Draw the network

ddn3.visualize.draw_network_for_ddn(edges_df, nodes_to_draw, fig_size=18, font_size_scale=1, node_size_scale=2, part_number=1, nodes_type=None, labels=None, mode='common', export_pdf=True, pdf_name='')

Draw networks for DDN

By default, we draw all nodes to a circle. It is possible for names of features to overlap with each other, especially at the top or bottom of the circle. If this happens, consider setting a smaller font_size_scale.

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.

We support drawing nodes in one or two types (like Gene vs. TF). If two types of nodes are present, we draw two ellipses. See part_number and nodes_type parameters.

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 (int, optional) – Size of figure, by default 18 We draw in a square figure, so width=height=`fig_size`.

  • 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

  • part_number (int, optional) – Number of ellipse to draw, by default 1 If set to 1, all nodes are in the same condition, draw a single circle. If set to 2, nodes are in two conditions (like Gene and TF), we draw two ellipses. If set to 2, nodes_type is needed.

  • nodes_type (None or dict, optional) – Node type (e.g., Gene=0, TF=1), by default None

  • labels (dict) – Alternative (e.g., simplfied) names for nodes

  • mode (str, optional) – Draw common graph or differential graph, by default “common”

  • export_pdf (bool, optional) – Set to true want to export the graph as a PDF file, by default True

  • pdf_name (str, optional) – Name of the PDF file to export, by default “”

Returns:

Graph object

Return type:

nx.Graph

ddn3.visualize._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.visualize._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

ddn3.visualize._get_pos_one_part(nodes_show)

Find positions of nodes on a circle

Also provides the minimum distances between nodes.

Parameters:

nodes_show (list of str) – Node names

Returns:

  • pos (dict) – The positions for each node

  • d_min (float) – Minimum distances between nodes

ddn3.visualize._get_pos_two_parts(nodes_show, nodes_type)

Find positions of nodes with two groups.

Group 1 on the left ellipse, group 2 on the right. Also provides the minimum distances between nodes.

Parameters:
  • nodes_show (list of str) – Node names

  • nodes_type (dict) – Group ID for each node

Returns:

  • pos (dict) – The positions for each node

  • d_min (float) – Minimum distances between nodes

ddn3.visualize._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.visualize._plot_network_helper(G, pos, d_min, labels, fig_size=18, font_size_scale=1, node_size_scale=2)

Draw the network

The graph elements are optimized for figure size 18 x 18. For smaller figures, users may need to change font_size_scale and node_size_scale.

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

  • fig_size (int, optional) – Size of figure, by default 18 We draw in a square figure, so width=height=`fig_size`.

  • 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