A spatial graph datastructure for python.
Project description
spatial-graph
spatial_graph provides a data structure for directed and undirected graphs,
where each node has an nD position (in time or space).
Design Principles
Goals
- support for arbitrary number of dimensions
- typed node identifiers and attributes
- any fixed-length type that is supported by
numpy
- any fixed-length type that is supported by
- efficient node/edge queries by
- ROI
- kNN (by points / lines)
- numpy-like interface for efficient:
- graph population and manipulation
- query results
- attribute access
- minimal memory footprint
- minimal dependencies
cython/witty/cheetah3for runtime compilation- numpy for array interfaces
- PYX API for graph algorithms in C/C++
Non-Goals
- graph algorithms
- I/O
- non-typed arguments
- non-spatial graphs
- out-of-memory support
- networkx compatibility
Python API
Graph creation:
graph = sg.SpatialGraph(
ndims=3,
node_dtype="uint64",
node_attr_dtypes={"position": "double[3]"},
edge_attr_dtypes={"score": "float32"},
position_attr="position",
directed=False,
)
Adding nodes/edges:
graph.add_nodes(
np.array([1, 2, 3, 4, 5], dtype="uint64"),
position=np.array(
[
[0.1, 0.1, 0.1],
[0.2, 0.2, 0.2],
[0.3, 0.3, 0.3],
[0.4, 0.4, 0.4],
[0.5, 0.5, 0.5],
],
dtype="double",
),
)
graph.add_edges(
np.array([[1, 2], [3, 4], [5, 1]], dtype="uint64"),
score=np.array([0.2, 0.3, 0.4], dtype="float32"),
)
Query nodes/edges in ROI:
# nodes/edges will be numpy arrays of dtype uint64 and shape (n,)/(n, 2)
nodes = graph.query_nodes_in_roi(np.array([[0.0, 0.0, 0.0], [0.25, 0.25, 0.25]]))
edges = graph.query_edges_in_roi(np.array([[0.0, 0.0, 0.0], [0.25, 0.25, 0.25]]))
Query nodes/edges by position:
nodes = graph.query_nearest_nodes(np.array([0.3, 0.3, 0.3]), k=3)
edges = graph.query_nearest_edges(np.array([0.3, 0.3, 0.3]), k=3)
Access node/edge attributes:
node_positions = graph.node_attrs[nodes].position
edge_scores = graph.edge_attrs[edges].score
Delete nodes/edges:
graph.remove_nodes(nodes[:1000])
Implementation Details
A SpatialGraph consists of three data structures:
- The
Graphitself, holding nodes, edges, and their attributes (graphlite). - Two R-trees for spatial node and edge queries (based on rtree.c).
For Developers
To create a new release, tag the current commit with a
version number and push it to the upstream remote:
git tag -a "vX.Y.Z" -m "vX.Y.Z"
git push upstream --follow-tags
This will trigger the CI workflow, which will build the package and upload it to PyPI.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file spatial_graph-0.0.2.tar.gz.
File metadata
- Download URL: spatial_graph-0.0.2.tar.gz
- Upload date:
- Size: 42.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fbd72c77393772bdc1bb902e3e605d9fc14064e0b28e80487ca73defc03ba756
|
|
| MD5 |
21025a32c21151d6d5f3267c4655e682
|
|
| BLAKE2b-256 |
7289cab0754999f16190791d088494967eaf0277de2a77d3927481ca352b1e59
|
File details
Details for the file spatial_graph-0.0.2-py3-none-any.whl.
File metadata
- Download URL: spatial_graph-0.0.2-py3-none-any.whl
- Upload date:
- Size: 41.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8b14d58370f2a4af1d839cd6371fd9e6a45157567a3dc0dca561fc4354ea9f9c
|
|
| MD5 |
8b715ff66357b2054aff078e4007214c
|
|
| BLAKE2b-256 |
65b5e28c1d27ca7046d985a28455f68b6b4a265d0dadfee7906c2adeb539b8a1
|