A python library for graph manipulation and visualization
Project description
Graphnet
A lightweight python library for graph and networks manipulation and visualization
Installation
pip install graphnet
Usage
>>> from graphnet import Graph
>>> g = Graph()
>>> g.add_node(1)
>>> g.add_node(2)
>>> g.add_node(3)
>>> g.add_edge(1,2)
>>> g.add_edge(2, 3)
>>> g.graph_matrix
array([[0., 1., 0.],
[1., 0., 1.],
[0., 1., 0.]])
The node object can take any hashable object as input in this example we use integers for simplicity but you can use any object of your choice. The are two graph types supported for now, The are vector
and scalar
type graphs. Graph objects by default are scalar or undirected graph to change it's type assign the variable VECTOR
to the Graph type argument like so.
>>> from graphnet import VECTOR
>>> g = Graph(type=VECTOR)
Visualizing
Graphnet supports graph visualization through Matplotlib API. All the graph plot heavy-lifting is done by one method so the is no need to worry. customization can be done using the display method parameters.
>>> from graphnet import VECTOR
>>> import matplotlib.pyplot as plt
>>> g = Graph(type=VECTOR)
>>> g.add_node(1)
>>> g.add_node(2)
>>> g.add_node(3)
>>> g.add_node(4)
>>> g.add_edge(1,2,weight=2)
>>> g.add_edge(2,3,weight=6)
>>> g.add_edge(3,4,weight=0)
>>> g.add_edge(4,1,weight=4)
>>> g.display(weighted=True)
>>> plt.show()
Nodes and Edges can be added through iterable to reduce code lines and to load data from a huge iterable.
>>> g = Graph()
>>> g.add_nodes_from_iterable(range(5))
>>> edge_list = [(1,2,3), (3,4), (0,1,2), (2,4), (3,2,1), (4,2,0)]
>>> g.add_edges_from_iterable(edge_list)
>>> g.graph_matrix
array([[0., 2., 0., 0., 0.],
[2., 0., 3., 0., 0.],
[0., 3., 0., 1., 0.],
[0., 0., 1., 0., 1.],
[0., 0., 0., 1., 0.]])
Data can also be added to the graph from dictionaries.
>>> h = Graph(type=VECTOR)
>>> m = {1:[1,2], 2:[3], 3:[4,1], 4:[2]}
>>> h.from_dict(m)
>>> h.graph_matrix
array([[1., 1., 0., 0.],
[0., 0., 1., 0.],
[1., 0., 0., 1.],
[0., 1., 0., 0.]])
Using Custom Node Class
In some cases you might want to use a custom class as nodes in the graph to create networks. to use custom node class in the graph first we inherit from the Node class and then overwrite the __eq__
,__hash__
,__repr__
methods in other for the custom class to work well with the graph. then pass the new reference attribute to the graph or you can use the old reference value
.
class Person(Node):
def __init__(self, name, age, sex):
Node.__init__(self)
self.name = name
self.age = age
self.sex = sex
def __eq__(self, other):
return self.name == other.name
def __hash__(self):
return hash(self.name)
def __repr__(self):
return "Person(name=%s, age=%s, sex=%s)"%(self.name, self.age, self.sex)
g = Graph(ref="name")
Algorithms
The algorithms module supports only two algorithms for now.
from graphnet.algorithms import dijkstra
with Graph(type='vector') as g:
for i in range(6):
g.add_node(Node(i))
edges = [(0, 1, 2), (0, 2, 10), (1, 3, 10),
(1, 4, 8), (2, 5, 2), (3, 2, 7), (4, 5, 5)]
for e in edges:
g.add_edge(*e)
cost = dijkstra(g, 0, 5, path=True)
for i, c in enumerate(cost):
if i < len(cost)-1:
edge = g.connections[g.get_node_id(
cost[i])][g.get_node_id(cost[i+1])]
c.color = "#82ffa3"
edge.color= 'red'
c.radius = 0.2
g.display(weighted=True)
plt.show()
Dependencies
- Numpy
- Matplotlib
Contributing
Any pull requests to this projects will be reviewed and accepted as long as you follow the Contributing Guide.
License
This project is licensed under the MIT License, see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file graphnet-0.1.2-py2.py3-none-any.whl
.
File metadata
- Download URL: graphnet-0.1.2-py2.py3-none-any.whl
- Upload date:
- Size: 16.3 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | db133e556ba1aa777fb60d2de937c914c310d24675b2413bafc232b6e67b6a2e |
|
MD5 | 58878d17c0eae9793a34198950add3e3 |
|
BLAKE2b-256 | f21128152ddb03ad6c2609352916f03fbae8e9a29b16cf590c11880662df7264 |