|
| 1 | +# spatial-graph |
| 2 | + |
| 3 | +`spatial-graph` provides a data structure for directed and undirected graphs, |
| 4 | +where each node has an nD position (in time or space). |
| 5 | + |
| 6 | +It leverages well-in-time compiled C++ code for efficient graph operations, |
| 7 | +coupled with an rtree implementation for fast spatial queries. |
| 8 | + |
| 9 | +## Goals |
| 10 | + |
| 11 | +- Support for arbitrary number of dimensions |
| 12 | +- Typed node identifiers and attributes |
| 13 | + - Any fixed-length type that is supported by `numpy` |
| 14 | +- Efficient node/edge queries by |
| 15 | + - ROI |
| 16 | + - kNN (by points / lines) |
| 17 | +- numpy-like interface for efficient: |
| 18 | + - Graph population and manipulation |
| 19 | + - Query results |
| 20 | + - Attribute access |
| 21 | +- Minimal memory footprint |
| 22 | +- Minimal dependencies |
| 23 | +- PYX API for graph algorithms in C/C++ |
| 24 | + |
| 25 | +## Basic Usage |
| 26 | + |
| 27 | +Graph creation: |
| 28 | + |
| 29 | +```python |
| 30 | +graph = sg.SpatialGraph( |
| 31 | + ndims=3, |
| 32 | + node_dtype="uint64", |
| 33 | + node_attr_dtypes={"position": "double[3]"}, |
| 34 | + edge_attr_dtypes={"score": "float32"}, |
| 35 | + position_attr="position", |
| 36 | + directed=False, |
| 37 | +) |
| 38 | +``` |
| 39 | + |
| 40 | +Adding nodes/edges: |
| 41 | + |
| 42 | +```python |
| 43 | +graph.add_nodes( |
| 44 | + np.array([1, 2, 3, 4, 5], dtype="uint64"), |
| 45 | + position=np.array( |
| 46 | + [ |
| 47 | + [0.1, 0.1, 0.1], |
| 48 | + [0.2, 0.2, 0.2], |
| 49 | + [0.3, 0.3, 0.3], |
| 50 | + [0.4, 0.4, 0.4], |
| 51 | + [0.5, 0.5, 0.5], |
| 52 | + ], |
| 53 | + dtype="double", |
| 54 | + ), |
| 55 | +) |
| 56 | + |
| 57 | +graph.add_edges( |
| 58 | + np.array([[1, 2], [3, 4], [5, 1]], dtype="uint64"), |
| 59 | + score=np.array([0.2, 0.3, 0.4], dtype="float32"), |
| 60 | +) |
| 61 | +``` |
| 62 | + |
| 63 | +Query nodes/edges in ROI: |
| 64 | + |
| 65 | +```python |
| 66 | +# nodes/edges will be numpy arrays of dtype uint64 and shape (n,)/(n, 2) |
| 67 | +nodes = graph.query_nodes_in_roi(np.array([[0.0, 0.0, 0.0], [0.25, 0.25, 0.25]])) |
| 68 | +edges = graph.query_edges_in_roi(np.array([[0.0, 0.0, 0.0], [0.25, 0.25, 0.25]])) |
| 69 | +``` |
| 70 | + |
| 71 | +Query nodes/edges by position: |
| 72 | + |
| 73 | +```python |
| 74 | +nodes = graph.query_nearest_nodes(np.array([0.3, 0.3, 0.3]), k=3) |
| 75 | +edges = graph.query_nearest_edges(np.array([0.3, 0.3, 0.3]), k=3) |
| 76 | +``` |
| 77 | + |
| 78 | +Access node/edge attributes: |
| 79 | + |
| 80 | +```python |
| 81 | +node_positions = graph.node_attrs[nodes].position |
| 82 | +edge_scores = graph.edge_attrs[edges].score |
| 83 | +``` |
| 84 | + |
| 85 | +Delete nodes/edges: |
| 86 | + |
| 87 | +```python |
| 88 | +graph.remove_nodes(nodes[:1000]) |
| 89 | +``` |
| 90 | + |
| 91 | +See the [API documentation](./reference) for more details. |
0 commit comments