API References
Make interactive network in D3 javascript.
- d3graph.d3graph.adjmat2dict(adjmat, min_weight=0, edge_distance_minmax=[1, 10])
Convert adjacency matrix into vector with source and target.
- Parameters
adjmat (pd.DataFrame()) – Adjacency matrix.
min_weight (float) – edges are returned with a minimum weight.
edge_distance_minmax (tuple(int,int), (default: [None,None])) – Weights are normalized between minimum and maximum * [1, 20]
- Returns
edge_properties –
- key: (source, target)
’weight’: weight of the edge ‘weight_scaled’: scaled weight of the edge ‘color’: color of the edge
- Return type
dict
- d3graph.d3graph.adjmat2vec(adjmat, min_weight=1)
Convert adjacency matrix into vector with source and target.
- Parameters
adjmat (pd.DataFrame()) – Adjacency matrix.
min_weight (float) – edges are returned with a minimum weight.
- Returns
nodes that are connected based on source and target
- Return type
pd.DataFrame()
Examples
>>> source=['Cloudy','Cloudy','Sprinkler','Rain'] >>> target=['Sprinkler','Rain','Wet_Grass','Wet_Grass'] >>> adjmat = vec2adjmat(source, target, weight=[1,2,1,3]) >>> vector = adjmat2vec(adjmat)
- class d3graph.d3graph.d3graph(collision=0.5, charge=350, slider=[None, None], verbose=20)
Make interactive network in D3 javascript.
- get_cluster_color(node_names)
Clustering of graph labels.
- Parameters
df (Pandas DataFrame with columns) – ‘source’ ‘target’ ‘weight’
- Return type
dict cluster_label.
- graph(adjmat)
Process the adjacency matrix and set all properties to default.
This function processes the adjacency matrix. The nodes are the column and index names. An connect edge is seen in case vertices has has values larger than 0. The strenght of the edge is based on the vertices values.
- Parameters
adjmat (pd.DataFrame()) – Adjacency matrix (symmetric). Values > 0 are edges.
Examples
>>> from d3graph import d3graph >>> >>> # Initialize >>> d3 = d3graph() >>> >>> # Load karate example >>> adjmat, df = d3.import_example('karate') >>> >>> # Initialize >>> d3.graph(adjmat) >>> >>> # Node properties >>> d3.set_node_properties(label=df['label'].values, color=df['label'].values, size=df['degree'].values, edge_size=df['degree'].values, cmap='Set1') >>> >>> # Edge properties >>> d3.set_edge_properties(directed=True) >>> >>> # Plot >>> d3.show()
- Return type
None
- import_example(network='small')
Import example.
- Parameters
network (str, optional) – Import example adjacency matrix. The default is ‘small’.
- Returns
adjmat
- Return type
pd.DataFrame()
- set_edge_properties(edge_distance=None, edge_distance_minmax=[1, 20], directed=False)
Edge properties.
- Parameters
edge_distance (Int (default: 30)) – Distance of nodes on the edges. * 0: Weighted approach using edge weights in the adjacancy matrix. Weights are normalized between the edge_distance_minmax * 80: Constant edge distance
edge_distance_minmax (tuple(int,int), (default: [None,None])) – Weights are normalized between minimum and maximum * [1, 20]
directed (Bool, (default: False)) – True: Directed edges (with arrow), False: Undirected edges (without arrow)
- Returns
edge_properties –
- key: (source, target)
’weight’: weight of the edge ‘weight_scaled’: scaled weight of the edge ‘color’: color of the edge
- Return type
dict
- set_node_properties(label=None, hover=None, color='#000080', size=10, edge_color='#000000', edge_size=1, cmap='Set1')
Node properties.
- Parameters
label (list of names (default: None)) – The text that is shown on the Node. If not specified, the label text will be inherited from the adjacency matrix column-names. * [‘label 1’,’label 2’,’label 3’, …]
hover (list of names (default: None)) – The text that is shown when hovering over the Node. If not specified, the text will inherited from the label. * [‘hover 1’,’hover 2’,’hover 3’, …]
color (list of strings (default: '#000080')) – Color of the node. * ‘cluster’ : Colours are based on the community distance clusters. * None: All nodes will be have the same color (auto generated). * [‘#000000’]: All nodes will have the same hex color. * [‘#377eb8’,’#ffffff’,’#000000’,…]: Hex colors are directly used. * [‘A’]: All nodes will have hte same color. Color is generated on CMAP and the unique labels. * [‘A’,’A’,’B’,…]: Colors are generated using cmap and the unique labels recordingly colored.
size (array of integers (default: 5)) – Size of the nodes. * 10: all nodes sizes are set to 10 * [10, 5, 3, 1, …]: Specify node sizes
edge_color (list of strings (default: '#000080')) – Edge color of the node. * [‘#377eb8’,’#ffffff’,’#000000’,…]: Hex colors are directly used. * [‘A’]: All nodes will have hte same color. Color is generated on CMAP and the unique labels. * [‘A’,’A’,’B’,…]: Colors are generated using cmap and the unique labels recordingly colored.
edge_size (array of integers (default: 1)) – Size of the node edge. Note that node edge sizes are automatically scaled between [0.1 - 4]. * 1: All nodes will be set on this size, * [2,5,1,…] Specify per node the edge size.
cmap (String, (default: 'Set1')) – All colors can be reversed with ‘_r’, e.g. ‘binary’ to ‘binary_r’ ‘Set1’, ‘Set2’, ‘rainbow’, ‘bwr’, ‘binary’, ‘seismic’, ‘Blues’, ‘Reds’, ‘Pastel1’, ‘Paired’
- Returns
node_properties –
- key: node_name
’label’: Label of the node ‘color’: color of the node ‘size’: size of the node ‘edge_size’: edge_size of the node ‘edge_color’: edge_color of the node
- Return type
dict
- set_path(filepath='d3graph.html')
Set the file path.
- Parameters
filepath (str) – filename and or full pathname. * ‘d3graph.html’ * ‘c://temp/’ * ‘c://temp/d3graph.html’
- Returns
filepath – Path to graph.
- Return type
str
- setup_slider()
Mininum maximum range of the slider.
- Returns
tuple
- Return type
[min, max].
- show(figsize=(1500, 800), title='d3graph', filepath='d3graph.html', showfig=True, overwrite=True)
Build and show the graph.
- Parameters
figsize (tuple, (default: (1500, 800))) – Size of the figure in the browser, [height, width].
title (String, (default: None)) – Title of the figure.
filepath (String, (Default: user temp directory)) – File path to save the output
showfig (bool, (default: True)) – Open the window to show the network.
overwrite (bool, (default: True)) – Overwrite the existing html file.
- Return type
None.
- write_html(json_data, overwrite=True)
Write html.
- Parameters
json_data (json file) –
- Return type
None.
- d3graph.d3graph.data_checks(adjmat)
Check input Adjacency matrix.
- Parameters
adjmat (pd.DataFrame()) – Adjacency matrix (symmetric). Values > 0 are edges.
- Returns
adjmat
- Return type
pd.DataFrame()
- d3graph.d3graph.edges2G(edge_properties, G=None)
Convert edges to Graph.
- Parameters
edge_properties (dictionary) – Dictionary containing edge properties
G (Networkx object) – Graph G.
- Return type
Graph G
- d3graph.d3graph.json_create(G)
Create json from Graph.
- Parameters
G (Networkx object) – Graph G
- Return type
json dump
- d3graph.d3graph.library_compatibility_checks()
Library compatibiliy checks.
- Return type
None.
- d3graph.d3graph.make_graph(node_properties, edge_properties)
Make graph from node and edge properties.
- Parameters
node_properties (dictionary) – Dictionary containing node properties
edge_properties (dictionary) – Dictionary containing edge properties
- Return type
dict containing Graph G and dataframe.
- d3graph.d3graph.nodes2G(node_properties, G=None)
Convert nodes to Graph.
- Parameters
node_properties (dictionary) – Dictionary containing node properties
G (Networkx object) – Graph G.
- Return type
Graph G
- d3graph.d3graph.remove_special_chars(adjmat)
Remove special characters.
- Parameters
adjmat (pd.DataFrame()) – Adjacency matrix (symmetric). Values > 0 are edges.
- Returns
adjmat
- Return type
pd.DataFrame()
- d3graph.d3graph.set_logger(verbose=20)
Set the logger for verbosity messages.
- d3graph.d3graph.vec2adjmat(source, target, weight=None, symmetric=True)
Convert source and target into adjacency matrix.
- Parameters
source (list) – The source node.
target (list) – The target node.
weight (list of int) – The Weights between the source-target values
symmetric (bool, optional) – Make the adjacency matrix symmetric with the same number of rows as columns. The default is True.
- Returns
adjacency matrix.
- Return type
pd.DataFrame
Examples
>>> source=['Cloudy','Cloudy','Sprinkler','Rain'] >>> target=['Sprinkler','Rain','Wet_Grass','Wet_Grass'] >>> vec2adjmat(source, target) >>> >>> weight=[1,2,1,3] >>> vec2adjmat(source, target, weight=weight)