pub struct TensorView<T>where
T: ArrowNativeType,{ /* private fields */ }Expand description
The read-only views Payload::view decodes into (blueprint §9.1’s
let img: ImageView = data.view()?;).
They belong to astrs-data — a view is a checked window over columns
somebody else built, and the layout rules it checks are that crate’s — but
the line that names them is a node’s event loop, so a node reaches them
here rather than through a second crate. ImageView has a
FromPayload implementation (see message::media); TensorView is
the general n-dimensional form the same module builds on.
A checked, zero-copy N-dimensional view over a PrimitiveArray’s flat
values.
Owns a clone of the source PrimitiveArray rather than borrowing it —
cloning one is an Arc bump (see PrimitiveArray’s own layout doc), so
this is still zero-copy, and it is what lets ImageView
build one from a value it downcast out of a row’s Arc<dyn Array>
without fighting a borrow that does not outlive the match arm it came
from.
See the module documentation for the shape it expects and what it deliberately does not check (validity).
Implementations§
Source§impl<T> TensorView<T>where
T: ArrowNativeType,
impl<T> TensorView<T>where
T: ArrowNativeType,
Sourcepub fn from_primitive(
array: &PrimitiveArray<T>,
shape: impl Into<Vec<usize>>,
) -> Result<TensorView<T>, DataError>
pub fn from_primitive( array: &PrimitiveArray<T>, shape: impl Into<Vec<usize>>, ) -> Result<TensorView<T>, DataError>
Builds a view over array’s values with the given shape.
§Errors
DataError::TensorShapeOverflowwhenshape’s dimensions multiply out to more elements thanusizecan represent — checked before anything else, so this never panics on a debug build the way an uncheckedshape.iter().product()would.DataError::TensorShapeMismatchwhenarray.len()is not the product ofshape’s dimensions.
Sourcepub fn from_values(
values: impl IntoIterator<Item = T>,
shape: impl Into<Vec<usize>>,
) -> Result<TensorView<T>, DataError>
pub fn from_values( values: impl IntoIterator<Item = T>, shape: impl Into<Vec<usize>>, ) -> Result<TensorView<T>, DataError>
Builds a view over freshly supplied values — mainly for tests and synthetic tensors that are not already backed by a decoded column.
§Errors
Whatever TensorView::from_primitive reports.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Total number of logical elements — the product of TensorView::shape,
1 for a zero-dimensional (scalar) tensor.
Sourcepub fn is_contiguous(&self) -> bool
pub fn is_contiguous(&self) -> bool
Returns true when this view’s memory is one contiguous run in
row-major order — true for a freshly built tensor and for any
TensorView::slice_axis result on axis 0, false once a slice on
any other axis has narrowed a dimension with extent greater than one
that is not the outermost.
A size-one dimension’s stride never contributes to which memory a
read touches (there is only one valid index into it, 0), so — like
NumPy’s own C-contiguity check — this walks the axes from innermost
out, skipping any dimension whose extent is 1, rather than
comparing every stride against the naive row_major_strides verbatim; the
naive comparison would call a size-one axis “non-contiguous” for
carrying a stride left over from before it was narrowed to 1, even
though no read the shape allows can tell the difference.
Sourcepub fn as_slice(&self) -> Option<&[T]>
pub fn as_slice(&self) -> Option<&[T]>
The whole view as one flat slice, when TensorView::is_contiguous.
use astrs_data::array::Int32Array;
use astrs_data::tensor::TensorView;
let values = Int32Array::from_values(0..12);
let tensor = TensorView::from_primitive(&values, [3, 4])?;
assert_eq!(tensor.as_slice().map(<[i32]>::len), Some(12));
// Slicing a non-outermost axis breaks contiguity.
let column = tensor.slice_axis(1, 1, 1)?;
assert!(!column.is_contiguous());
assert_eq!(column.as_slice(), None);Sourcepub fn flat_index(&self, index: &[usize]) -> Result<usize, DataError>
pub fn flat_index(&self, index: &[usize]) -> Result<usize, DataError>
The flat element offset index names, checked against the shape.
§Errors
DataError::TensorRankMismatchwhenindex.len() != self.ndim().DataError::TensorIndexOutOfBoundswhen a component is not smaller than its axis’s extent.
Sourcepub fn slice_axis(
&self,
axis: usize,
start: usize,
len: usize,
) -> Result<TensorView<T>, DataError>
pub fn slice_axis( &self, axis: usize, start: usize, len: usize, ) -> Result<TensorView<T>, DataError>
A zero-copy sub-view: axis narrowed to [start, start + len),
clamped to the axis’s extent (the crate-wide slicing convention —
see Array::slice).
§Errors
DataError::TensorAxisOutOfBounds when axis >= self.ndim().
Sourcepub fn try_slice_axis(
&self,
axis: usize,
start: usize,
len: usize,
) -> Result<TensorView<T>, DataError>
pub fn try_slice_axis( &self, axis: usize, start: usize, len: usize, ) -> Result<TensorView<T>, DataError>
Checked TensorView::slice_axis.
§Errors
DataError::TensorAxisOutOfBounds when axis >= self.ndim(), or
DataError::SliceOutOfBounds when [start, start + len) leaves the
axis’s extent.
Trait Implementations§
Source§impl<T> Clone for TensorView<T>where
T: Clone + ArrowNativeType,
impl<T> Clone for TensorView<T>where
T: Clone + ArrowNativeType,
Source§fn clone(&self) -> TensorView<T>
fn clone(&self) -> TensorView<T>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more