read from file

This commit is contained in:
Niko Abeler 2022-11-23 21:15:03 +01:00
parent b6ab0f2144
commit 2ceb9a413f
7 changed files with 102 additions and 18 deletions

2
Cargo.lock generated
View File

@ -33,7 +33,7 @@ dependencies = [
[[package]] [[package]]
name = "graph_force" name = "graph_force"
version = "0.1.3" version = "0.2.0"
dependencies = [ dependencies = [
"pyo3", "pyo3",
"rand", "rand",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "graph_force" name = "graph_force"
version = "0.1.3" version = "0.2.0"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@ -9,6 +9,11 @@ pip install graph_force
``` ```
## Usage ## 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.
```python ```python
import graph_force import graph_force
@ -17,6 +22,9 @@ pos = graph_force.layout_from_edge_list(4, edges)
``` ```
### Example with networkx ### 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.
```python ```python
import networkx as nx import networkx as nx
import graph_force import graph_force
@ -34,6 +42,47 @@ 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) 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)
```python
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` are optional parameters, supported by `layout_from_edge_list` and `layout_from_edge_file`.
```python
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"
)
```
#### Available models
- `spring_model`: A simple spring model (my own implementation)
- `networkx_model`: Reimplementation of the [spring model from networkx](https://networkx.org/documentation/stable/reference/generated/networkx.drawing.layout.spring_layout.html)
## Contributing ## Contributing
- [Development](DEVELOPMENT.md) - [Development](DEVELOPMENT.md)

View File

@ -1,6 +1,7 @@
mod graph; mod graph;
mod model; mod model;
mod networkx_model; mod networkx_model;
mod reader;
mod runner; mod runner;
mod spring_model; mod spring_model;
mod utils; mod utils;
@ -8,6 +9,32 @@ mod utils;
use pyo3::exceptions; use pyo3::exceptions;
use pyo3::{prelude::*, types::PyIterator}; use pyo3::{prelude::*, types::PyIterator};
fn pick_model(model: &str) -> Result<Box<dyn model::ForceModel + Send + Sync>, PyErr> {
match model {
"spring_model" => Ok(Box::new(spring_model::SimpleSpringModel::new(1.0))),
"networkx_model" => Ok(Box::new(networkx_model::NetworkXModel::new())),
_ => {
return Err(PyErr::new::<exceptions::PyValueError, _>(
"model must be either 'spring_model' or 'networkx_model'",
))
}
}
}
#[pyfunction(file_path, "*", iter = 500, threads = 0, model = "\"spring_model\"")]
fn layout_from_edge_file(
file_path: &str,
iter: usize,
threads: usize,
model: &str,
) -> PyResult<Vec<(f32, f32)>> {
let (size, matrix) = reader::read_graph(file_path);
let model = pick_model(model)?;
let r = runner::Runner::new(iter, threads);
Ok(r.layout(size, matrix, model))
}
#[pyfunction( #[pyfunction(
number_of_nodes, number_of_nodes,
edges, edges,
@ -23,15 +50,7 @@ fn layout_from_edge_list(
threads: usize, threads: usize,
model: &str, model: &str,
) -> PyResult<Vec<(f32, f32)>> { ) -> PyResult<Vec<(f32, f32)>> {
let model: Box<dyn model::ForceModel + Send + Sync> = match model { let model: Box<dyn model::ForceModel + Send + Sync> = pick_model(model)?;
"spring_model" => Box::new(spring_model::SimpleSpringModel::new(1.0)),
"networkx_model" => Box::new(networkx_model::NetworkXModel::new()),
_ => {
return Err(PyErr::new::<exceptions::PyValueError, _>(
"model must be either 'spring_model' or 'networkx_model'",
))
}
};
let mut edge_matrix = graph::new_edge_matrix(number_of_nodes); let mut edge_matrix = graph::new_edge_matrix(number_of_nodes);
match edges.extract::<&PyIterator>() { match edges.extract::<&PyIterator>() {
@ -70,5 +89,6 @@ fn layout_from_edge_list(
#[pymodule] #[pymodule]
fn graph_force(_py: Python, m: &PyModule) -> PyResult<()> { fn graph_force(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(layout_from_edge_list, m)?)?; m.add_function(wrap_pyfunction!(layout_from_edge_list, m)?)?;
m.add_function(wrap_pyfunction!(layout_from_edge_file, m)?)?;
Ok(()) Ok(())
} }

View File

@ -3,6 +3,7 @@ use crate::model::ForceModel;
/** /**
* Translation of the NetworkX spring_layout function. * Translation of the NetworkX spring_layout function.
* https://networkx.org/documentation/stable/reference/generated/networkx.drawing.layout.spring_layout.html
*/ */
pub struct NetworkXModel { pub struct NetworkXModel {
edges: EdgeMatrix, edges: EdgeMatrix,

View File

@ -1,3 +1,7 @@
use std::{fs::File, io::Read};
use crate::graph::{add_edge, new_edge_matrix, EdgeMatrix};
/** /**
* Read Graph data from file * Read Graph data from file
* Format: * Format:
@ -5,22 +9,20 @@
* - 4 bytes: number of nodes(int) * - 4 bytes: number of nodes(int)
* - 12 bytes: nodeA(int), nodeB(int), weight(float) * - 12 bytes: nodeA(int), nodeB(int), weight(float)
*/ */
fn read_graph(file_name: &str) -> (usize, EdgeMatrix) { pub fn read_graph(file_name: &str) -> (usize, EdgeMatrix) {
let mut file = File::open(file_name).expect("file not found"); let mut file = File::open(file_name).expect("file not found");
let mut size_buffer = [0; 4]; let mut size_buffer = [0; 4];
file.read_exact(&mut size_buffer).expect("buffer overflow"); file.read_exact(&mut size_buffer).expect("buffer overflow");
let size = u32::from_le_bytes(size_buffer) as usize; let size = u32::from_le_bytes(size_buffer) as usize;
let matrix_ptr = connection_matrix(size); let mut matrix = new_edge_matrix(size);
{ {
let mut matrix = matrix_ptr.write().unwrap();
let mut buffer = [0; 12]; let mut buffer = [0; 12];
while file.read_exact(&mut buffer).is_ok() { while file.read_exact(&mut buffer).is_ok() {
let node_a = u32::from_le_bytes(buffer[0..4].try_into().unwrap()) as usize; let node_a = u32::from_le_bytes(buffer[0..4].try_into().unwrap()) as usize;
let node_b = u32::from_le_bytes(buffer[4..8].try_into().unwrap()) as usize; let node_b = u32::from_le_bytes(buffer[4..8].try_into().unwrap()) as usize;
let weight = f32::from_le_bytes(buffer[8..12].try_into().unwrap()); let _weight = f32::from_le_bytes(buffer[8..12].try_into().unwrap());
matrix[node_a][node_b].weight = weight; add_edge(&mut matrix, node_a, node_b);
matrix[node_b][node_a].weight = weight;
} }
} }
(size, matrix_ptr) (size, matrix)
} }

View File

@ -1,4 +1,5 @@
import graph_force import graph_force
import struct
def test_list_of_edges(): def test_list_of_edges():
edges = [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)] edges = [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]
@ -28,3 +29,14 @@ def test_model_selection():
pos = graph_force.layout_from_edge_list(7, edges, model='networkx_model') pos = graph_force.layout_from_edge_list(7, edges, model='networkx_model')
assert pos is not None assert pos is not None
assert len(pos) == 7 assert len(pos) == 7
def test_from_file():
with open("/tmp/edges.bin", "wb") 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('/tmp/edges.bin')
assert pos is not None
assert len(pos) == 10