Graph Force
A python/rust library for embedding graphs in 2D space, using force-directed layouts.
Installation
pip install graph_force
Usage
The first parameter defines the number of nodes in graph. The second parameter is an iterable of edges, where each edge is a tuple of two integers representing the nodes it connects. Node ids start at 0.
import graph_force
edges = [(0, 1), (1, 2), (2, 3), (3, 0)]
pos = graph_force.layout_from_edge_list(4, edges)
Example with networkx
This library does not have a function to consume a networkx graph directly, but it is easy to convert it to an edge list.
import networkx as nx
import graph_force
G = nx.grid_2d_graph(10, 10)
# we have to map the names to integers
# as graph_force only supports integers as node ids at the moment
edges = []
mapping = {n: i for i, n in enumerate(G.nodes)}
i = 0
for edge in G.edges:
edges.append((mapping[edge[0]], mapping[edge[1]]))
pos = graph_force.layout_from_edge_list(len(G.nodes), edges, iter=1000)
nx.draw(G, {n: pos[i] for n, i in mapping.items()}, node_size=2, width=0.1)
Example with edge file
This methods can be used with large graphs, where the edge list does not fit into memory.
Format of the file:
- Little endian
- 4 bytes: number of nodes(int)
- 12 bytes: nodeA(int), nodeB(int), weight(float)
import graph_force
import struct
with open("edges.bin", "rb") as f:
n = 10
f.write(struct.pack("i", n))
for x in range(n-1):
f.write(struct.pack("iif", x, x+1, 1))
pos = graph_force.layout_from_edge_file("edges.bin", iter=50)
Options
iter, threads and model, initial_pos are optional parameters, supported by layout_from_edge_list and layout_from_edge_file.
pos = graph_force.layout_from_edge_list(
number_of_nodes,
edges,
iter=500, # number of iterations, default 500
threads=0, # number of threads, default 0 (all available)
model="spring_model", # model to use, default "spring_model", other option is "networkx_model"
initial_pos=[(0.4, 0.7), (0.7, 0.2), ...], # initial positions, default None (random)
)
Available models
spring_model: A simple spring model (my own implementation)networkx_model: Reimplementation of the spring model from networkx
Contributing
Metadata
Release files for graph-force 0.2.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| graph_force-0.2.2.tar.gz | 11.1 kB | Without provenance | Details for graph_force-0.2.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| graph_force-0.2.2-cp311-cp311-manylinux_2_28_x86_64.whl | CPython 3.11 | CPython 3.11 | Linux glibc 2.28+ x86-64 | Details for graph_force-0.2.2-cp311-cp311-manylinux_2_28_x86_64.whl |
Total release size: 231.7 kB
Release files / graph_force-0.2.2.tar.gz
| Download URL | graph_force-0.2.2.tar.gz |
|---|---|
| Size | 11.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
35fbf8d815001c709d1f27ff72e6b797703b51265f6e10a7e37cb3c8ae1fb741
|
|
BLAKE2b-256 checksum How to use checksums |
a4b9176345460b36287b1ed4b6424a0ece1a304e51a0fc3f4ec388cf71abd1b3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/0.14.1
|
Release files / graph_force-0.2.2-cp311-cp311-manylinux_2_28_x86_64.whl
| Download URL | graph_force-0.2.2-cp311-cp311-manylinux_2_28_x86_64.whl |
|---|---|
| Size | 220.6 kB |
| Tags | CPython 3.11 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
9bc194d65b3916903240fac58951e08c032ec08557337ad88e601df73395ea3e
|
|
BLAKE2b-256 checksum How to use checksums |
aaa1716d09f4cebd4a794a82b98e4b7c02ba5e1d54d01c26f5becd57f0bc1d8c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/0.14.1
|
Security for graph-force 0.2.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.