Skip to content

Commit 964b320

Browse files
committed
Refactor documentation
1 parent 6b19b6d commit 964b320

File tree

4 files changed

+83
-17
lines changed

4 files changed

+83
-17
lines changed

src/array.rs

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Safe interface for NumPy ndarray
22
use ndarray::*;
3-
use npyffi::{self, npy_intp, PY_ARRAY_API};
3+
use npyffi::{self, npy_intp, PY_ARRAY_API, NPY_ORDER};
44
use num_traits::AsPrimitive;
55
use pyo3::*;
66
use std::iter::ExactSizeIterator;
@@ -11,18 +11,82 @@ use std::ptr;
1111

1212
use convert::{NpyIndex, ToNpyDims};
1313
use error::{ErrorKind, IntoPyErr};
14-
use types::{NpyDataType, TypeNum, NPY_ORDER};
15-
16-
/// Interface for [NumPy ndarray](https://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html).
14+
use types::{NpyDataType, TypeNum};
15+
16+
/// A safe, static-typed interface for
17+
/// [NumPy ndarray](https://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html).
18+
///
19+
/// # Memory location
20+
/// Numpy api allows to use a memory area allocated outside Pyhton.
21+
///
22+
/// However, we designed `PyArray` to always **owns a memory area allocated in Python's private
23+
/// heap**, where all memories are managed by GC.
24+
///
25+
/// This means you always need to pay allocation cost when you create a `PyArray`, but don't need
26+
/// to fear memory leak.
27+
///
28+
/// # Reference
29+
///
30+
/// Like [`new`](#method.new), most constractor methods of this type returns `&PyArray`.
31+
///
32+
/// See [pyo3's document](https://pyo3.rs/master/doc/pyo3/index.html#ownership-and-lifetimes)
33+
/// for the reason.
34+
///
35+
/// # Mutation
36+
/// You can do destructive changes to `PyArray` via &self methods like [`move_to`](#method.move_to).
37+
///
38+
/// About this design, see
39+
/// [pyo3's document](https://pyo3.rs/master/doc/pyo3/index.html#ownership-and-lifetimes), too.
40+
///
41+
/// # Dimension
42+
/// `PyArray` has 2 type parametes `T` and `D`. `T` represents its data type like `f32`, and `D`
43+
/// represents its dimension.
44+
///
45+
/// To specify the dimension, you can use types which implements
46+
/// [Dimension](https://docs.rs/ndarray/0.12/ndarray/trait.Dimension.html).
47+
///
48+
/// Typically, you can use `Ix1, Ix2, ..` for fixed size arrays, and use `IxDyn` for dynamic
49+
/// dimensioned arrays. They're re-exported from `ndarray` crate.
50+
///
51+
/// You can also use various type aliases we provide, like [`PyArray1`](./type.PyArray1.html)
52+
/// or [`PyArrayDyn`](./type.PyArrayDyn.html).
53+
///
54+
/// Many constructor methods takes a type which implements
55+
/// [`IntoDimension`](https://docs.rs/ndarray/0.12/ndarray/dimension/conversion/trait.IntoDimension.html)
56+
/// trait. Typically, you can use array(e.g. `[3, 4, 5]`) or tuple(e.g. `(3, 4, 5)`) as a dimension.
57+
/// # Example
58+
/// ```
59+
/// # #[macro_use] extern crate ndarray; extern crate pyo3; extern crate numpy; fn main() {
60+
/// use pyo3::{GILGuard, Python};
61+
/// use numpy::PyArray;
62+
/// use ndarray::Array;
63+
/// let gil = Python::acquire_gil();
64+
/// let pyarray = PyArray::arange(gil.python(), 0., 4., 1.).reshape([2, 2]).unwrap();
65+
/// let array = array![[3., 4.], [5., 6.]];
66+
/// assert_eq!(
67+
/// array.dot(&pyarray.as_array().unwrap()),
68+
/// array![[8., 15.], [12., 23.]]
69+
/// );
70+
/// # }
71+
/// ```
1772
pub struct PyArray<T, D>(PyObject, PhantomData<T>, PhantomData<D>);
73+
74+
/// one-dimensional array
1875
pub type PyArray1<T> = PyArray<T, Ix1>;
76+
/// two-dimensional array
1977
pub type PyArray2<T> = PyArray<T, Ix2>;
78+
/// three-dimensional array
2079
pub type PyArray3<T> = PyArray<T, Ix3>;
80+
/// four-dimensional array
2181
pub type PyArray4<T> = PyArray<T, Ix4>;
82+
/// five-dimensional array
2283
pub type PyArray5<T> = PyArray<T, Ix5>;
84+
/// six-dimensional array
2385
pub type PyArray6<T> = PyArray<T, Ix6>;
86+
/// dynamic-dimensional array
2487
pub type PyArrayDyn<T> = PyArray<T, IxDyn>;
2588

89+
/// Returns a array module.
2690
pub fn get_array_module(py: Python) -> PyResult<&PyModule> {
2791
PyModule::import(py, npyffi::array::MOD_NAME)
2892
}
@@ -348,7 +412,7 @@ impl<T: TypeNum, D: Dimension> PyArray<T, D> {
348412
unsafe { Ok(ArrayView::from_shape_ptr(self.ndarray_shape(), self.data())) }
349413
}
350414

351-
/// Get the mmutable view of the internal data of `PyArray`, as `ndarray::ArrayViewMut`.
415+
/// Almost same as [`as_array`](#method.as_array), but returns `ArrayViewMut`.
352416
pub fn as_array_mut(&self) -> Result<ArrayViewMut<T, D>, ErrorKind> {
353417
self.type_check()?;
354418
unsafe {
@@ -387,13 +451,13 @@ impl<T: TypeNum, D: Dimension> PyArray<T, D> {
387451
/// # }
388452
/// ```
389453
/// But, for dinamic errors too long/short index returns `None`.
390-
/// ```compile_fail
454+
/// ```
391455
/// # extern crate pyo3; extern crate numpy; fn main() {
392456
/// use numpy::PyArray;
393457
/// let gil = pyo3::Python::acquire_gil();
394458
/// let arr = PyArray::arange(gil.python(), 0, 16, 1).reshape([2, 2, 4]).unwrap();
395459
/// let arr = arr.into_dyn();
396-
/// assert!(arr.get([1, 2]).is_none());
460+
/// assert!(arr.get([1, 2].as_ref()).is_none());
397461
/// # }
398462
/// ```
399463
#[inline(always)]

src/convert.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ pub trait ToNpyDims: Dimension {
5353
fn ndim_cint(&self) -> c_int {
5454
self.ndim() as c_int
5555
}
56-
fn as_dims_ptr(&self) -> *mut npy_intp {
57-
self.slice().as_ptr() as *mut npy_intp
56+
fn as_dims_ptr(&self) -> *mut npyffi::npy_intp {
57+
self.slice().as_ptr() as *mut npyffi::npy_intp
5858
}
5959
fn to_npy_dims(&self) -> npyffi::PyArray_Dims {
6060
npyffi::PyArray_Dims {
@@ -73,7 +73,8 @@ impl<D: Dimension> ToNpyDims for D {
7373

7474
/// Types that can be used to index an array.
7575
///
76-
/// See[IntoDimension](https://docs.rs/ndarray/0.12/ndarray/dimension/conversion/trait.IntoDimension.html)
76+
/// See
77+
/// [IntoDimension](https://docs.rs/ndarray/0.12/ndarray/dimension/conversion/trait.IntoDimension.html)
7778
/// for what types you can use as `NpyIndex`.
7879
///
7980
/// But basically, you can use

src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
//! installed by `pip install numpy` or other ways in your python environment.
99
//! You can use both system environment and `virtualenv`.
1010
//!
11+
//! This library loads numpy module automatically. So if numpy is not installed, it simply panics,
12+
//! instead of returing a result.
13+
//!
1114
//! # Example
1215
//!
1316
//! ```
@@ -48,6 +51,7 @@ pub use array::{
4851
PyArrayDyn,
4952
};
5053
pub use convert::{NpyIndex, ToNpyDims, ToPyArray};
51-
pub use error::*;
54+
pub use error::{IntoPyErr, IntoPyResult, ArrayFormat, ErrorKind};
5255
pub use npyffi::{PY_ARRAY_API, PY_UFUNC_API};
53-
pub use types::*;
56+
pub use types::{c32, c64, NpyDataType, TypeNum};
57+
pub use ndarray::{Ix1, Ix2, Ix3, Ix4, Ix5, Ix6, IxDyn};

src/types.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
//! Implements conversion utitlities.
2-
2+
/// alias of Complex32
33
pub use num_complex::Complex32 as c32;
4+
/// alias of Complex64
45
pub use num_complex::Complex64 as c64;
56

6-
pub use super::npyffi::npy_intp;
7-
pub use super::npyffi::NPY_ORDER;
8-
pub use super::npyffi::NPY_ORDER::{NPY_CORDER, NPY_FORTRANORDER};
9-
107
use super::npyffi::NPY_TYPES;
118

129
/// An enum type represents numpy data type.

0 commit comments

Comments
 (0)