Skip to content

MatthieuBizien/pyo3_numpy.rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Py03_Numpy

Efficient bindings between Rust and Numpy with PyO3

Usage

To use Py03_Numpy, add to Cargo.toml

[dependencies]
pyo3_numpy = "0.2"

Example for saving a csv

extern create pyo3_numpy;
use pyo3_numpy::pyo3::Python;

fn main() {
    save_csv(vec![1, 2, 3], "/tmp/myfile.csv").unwrap();
}

fn save_csv(mut v: Vec<u32>, path: &str) -> PyResult<()> {
    // We create and populate the namespace
    let gil = Python::acquire_gil();
    let py = gil.python();
    let locals = PyDict::new(py);
    let numpy = py.import("numpy")?;
    locals.set_item("np", numpy)?;

    // Use to_numpy to send an array to numpy
    let myarray = v.to_numpy(py)?;
    locals.set_item("myarray", myarray)?;

    // The dtype is adapted
    let dtype = py.eval("str(myarray.dtype)", None, Some(locals))?;
    let dtype: String = dtype.extract().unwrap_or("NOT A STRING".to_string());
    assert_eq!(dtype, "uint32");

    // We can use python and numpy functions
    locals.set_item("path", path)?;
    py.run("np.savetxt(path, myarray)", None, Some(locals))?;
    Ok(())
}

About

Efficient bindings between Rust and Numpy with PyO3

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages