Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/a-r-j/graphtype
Type hinting for networkx Graphs
https://github.com/a-r-j/graphtype
data-science graph graph-algorithms graph-theory network-analysis networkx pydata python scientific-computing typehinting typehints
Last synced: 16 days ago
JSON representation
Type hinting for networkx Graphs
- Host: GitHub
- URL: https://github.com/a-r-j/graphtype
- Owner: a-r-j
- License: apache-2.0
- Created: 2022-07-12T10:31:12.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-07-13T11:51:43.000Z (over 2 years ago)
- Last Synced: 2024-10-10T22:16:49.239Z (about 1 month ago)
- Topics: data-science, graph, graph-algorithms, graph-theory, network-analysis, networkx, pydata, python, scientific-computing, typehinting, typehints
- Language: Jupyter Notebook
- Homepage: https://pypi.org/project/graphtype/
- Size: 20.5 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# graphtype
Type hinting for networkx Graphs
## Installation
```python
pip install graphtype
```## Usage
There are two parts in graphtype: the type-hinting part, and the validation. You can use type-hinting with the provided class to indicate attributes graphs should possess, and the validation decorator to additionally ensure the format is respected in every function call.
### Type-Hinting `Graph`
```python
from graphtype import Graphdef do_something_to_graph(g: Graph)
pass
```### Type-Hinting Graph Attributes `GraphData`
```python
from graphtype import Graph, GraphDatadef do_something_to_graph(g: Graph[GraphData["name"]])
pass# The graph must have a "name" attribute
def do_something_to_graph(g: Graph[GraphData["name": str]])
pass# type(g.graph["name"]) == str must be True
```### Type-Hinting Node Attributes `NodeData`
```python
from graphtype import Graph, NodeDatadef do_something_to_graph(g: Graph[NodeData["feature"]])
pass# Each node must have a "feature" attribute
def do_something_to_graph(g: Graph[NodeData["feature": np.ndarray]])
pass# for n, d in g.nodes(data=True):
# type(d["feature"]) == np.ndarray must be True
```### Type-Hinting Edge Attributes `EdgeData`
```python
from graphtype import Graph, EdgeDatadef do_something_to_graph(g: Graph[EdgeData["feature"]])
pass# Each edge must have a "feature" attribute
def do_something_to_graph(g: Graph[EdgeData["feature": pd.DataFrame]])
pass# for u, v, d in g.edges(data=True):
# type(d["feature"]) == pd.DataFrame must be True
```## Enforcing: `@validate`
The `@validate` decorator ensures that input `Graphs` have the right format when the function is called, otherwise raises `TypeError`.
```python
@validate
def func(g: Graph[NodeData["feature1": pd.DataFrame, "feature2": int],
EdgeData["length": float, "counts": np.ndarray],
GraphData["name": str]],
h: Graph[NodeData["feature1": pd.DataFrame, "feature2": int]],
):
pass
```This package is heavily inspired by [Dataenforce](https://github.com/CedricFR/dataenforce).