wepy.analysis.network_layouts.layout_graph module

Base class for generating general purpose graphs to external formats.

The primary use for this will be generating GEXF files for use in visualization.

Routines

LayoutGraph

class wepy.analysis.network_layouts.layout_graph.LayoutGraph(graph)[source]

Bases: object

Base class for networks that will be given layouts for visualization.

For the graph it wraps it will create a mirror graph which only has visualization specific node and edge attributes. This avoids namespace clashes between graphs.

Creates a wrapper for a graph that allows for setting of visualization properties and export to external formats without modifying the source graph.

This is done by keeping a mirrored copy of the graph, known as the viz_graph, which has all node and edge attributes removed but keeps the topology.

Parameters

graph (any networkx graph) –

GRAPHVIZ_COLOR = 'color'
GRAPHVIZ_POSITION = 'pos'
GRAPHVIZ_SHAPE = 'shape'
GRAPHVIZ_SIZE = 'size'
GEXF_VIZ = 'viz'
GEXF_VIZ_COLOR = 'color'
GEXF_VIZ_COLOR_RED = 'r'
GEXF_VIZ_COLOR_GREEN = 'g'
GEXF_VIZ_COLOR_BLUE = 'b'
GEXF_VIZ_COLOR_ALPHA = 'a'
GEXF_VIZ_POSITION = 'position'
GEXF_VIZ_POSITION_X = 'x'
GEXF_VIZ_POSITION_Y = 'y'
GEXF_VIZ_POSITION_Z = 'z'
GEXF_VIZ_SIZE = 'size'
GEXF_VIZ_SHAPE = 'shape'
GEXF_EDGE_THICKNESS = 'thickness'
GEXF_EDGE_SHAPE = 'shape'
COLOR = 'color'
POSITION = 'pos'
SHAPE = 'shape'
SIZE = 'size'
property graph

The source data graph the class is wrapping.

property viz_graph

The mirror graph in which visualization attributes are set.

get_node_attributes(attribute_key)[source]

Gets attributes from the data graph and returns as a dictionary which is compatible with the set_node_viz_attributes methods.

For instance could be used to get a ‘weight’ value and set as the color.

Parameters

attribute_key (str) –

Returns

node_attributes – Dictionary mapping of data graph nodes to the value specified by the attribute key.

Return type

dict of node_id: value

set_node_viz_attributes(attribute_key, node_attribute_dict)[source]

Sets attributes of the viz_graph in a format agnostic way.

Parameters
  • attribute_key (str) – Key to set node attribute values to.

  • node_attribute_dict (dict of node_id: value) – Dictionary mapping node_ids to the value that will be set under the attribute key for each node.

set_node_positions(node_positions_dict)[source]

Set positions of the nodes in the viz graph in a format agnostic way.

Parameters

node_positions_dict (dict of node_id: position) –

set_node_colors(node_colors_dict)[source]

Set colors of the nodes in the viz graph in a format agnostic way.

Parameters

node_colors_dict (dict of node_id: color) –

set_node_sizes(node_sizes_dict)[source]

Set sizes of the nodes in the viz graph in a format agnostic way.

Parameters

node_sizes_dict (dict of node_id: int) –

set_node_shapes(node_shapes_dict)[source]

Set the shapes of the nodes in the viz graph in a format agnostic way.

Parameters

node_shapes_dict (dict of node_id: shape_spec) –

get_node_viz_attributes(attribute_key)[source]

Get the format agnostic attributes of nodes in the viz graph.

Parameters

attribute_key (str) –

Returns

node_attributes

Return type

dict of node_id: value

get_node_positions()[source]

Get the format agnostic positions of the nodes in the viz graph.

Returns

node_positions

Return type

dict of node_id: numpy.ndarray of float

get_node_colors()[source]

Get the format agnostic colors of the nodes in the viz graph.

Returns

node_colors

Return type

dict of node_id: color_spec

get_node_sizes()[source]

Get the format agnostic size of the nodes in the viz graph.

Returns

node_sizes

Return type

dict of node_id: float

get_node_shapes()[source]

Get the format agnostic shapes of the nodes in the viz graph.

Returns

node_shapes

Return type

dict of node_id: shape_spec

classmethod RGBA_to_hex(color_vec)[source]

Convert an RGBA color tuple to the hex representation.

Parameters

color_vec (array_like of int) – Valid RGB values between 0 and 256

Returns

hex_color – String of the hex value of the color.

Return type

str

classmethod feature_vector_to_gexf_viz_position(coord_vec)[source]

Converts an array_like of positions to the corresponding x, y, z representation for GEXF representation.

Parameters

coord_vec (array_like of float of dim (3,)) –

Returns

gexf_position_spec – The node attribute for specifying a position of a node in gexf.

Return type

dict of str: float

classmethod feature_vector_to_gexf_viz_color_RGB(color_vec)[source]

Converts an array_like of positions to the corresponding RGB representation for GEXF representation.

Parameters

color_vec (array_like of int of dim (3,)) – Should be valid RGB values between 0 and 256.

Returns

gexf_color_spec – The node attribute for specifying a color of a node in gexf.

Return type

dict of str: int

classmethod feature_vector_to_gexf_viz_color_RGBA(color_vec)[source]

Converts an array_like of positions to the corresponding RGBA representation for GEXF representation.

Parameters

color_vec (array_like of int of dim (4,)) – Should be valid RGB values between 0 and 256.

Returns

gexf_color_spec – The node attribute for specifying a color of a node in gexf.

Return type

dict of str: int

set_node_gexf_viz(viz_key, node_dict)[source]

Sets GEXF formatted visualization attributes for all nodes.

GEXF has a structure where each node has a special compound ‘viz’ attribute which can have a number of special attributes for shape, position, color, size, etc.

This method just makes it easy to set one of those attributes under all nodes ‘viz’ attribute.

Parameters
  • viz_key (str) – Valid GEXF ‘viz’ attribute

  • node_dict (dict of node_id: value) –

set_node_gexf_positions(node_positions_dict)[source]

Sets GEXF specific positions attribute for visualization to nodes.

Parameters

node_positions_dict (dict of node_id: array_like of float of dim (3,)) – Positions for each node.

set_node_gexf_colors_rgb(node_colors_dict)[source]

Sets GEXF specific RGB color attribute for visualization to nodes.

Parameters

node_rgb_dict (dict of node_id: array_like of int of dim (3,)) – GEXF RGB color spec for each node.

set_node_gexf_colors_rgba(node_colors_dict)[source]

Sets GEXF specific RGBA color attribute for visualization to nodes.

Parameters

node_rgba_dict (dict of node_id: array_like of int of dim (4,)) – GEXF RGBA color spec for each node.

set_node_gexf_sizes(node_sizes_dict)[source]

Sets GEXF specific node size attribute for visualization to nodes.

Parameters

node_sizes_dict (dict of node_id: float) – GEXF size spec for each node.

set_node_gexf_shape(node_shape_dict)[source]

Sets GEXF specific node shape attribute for visualization to nodes.

Parameters

node_shapes_dict (dict of node_id: str) – GEXF shape spec for each node.