Skip to main content

TensorView

Struct TensorView 

Source
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,

Source

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::TensorShapeOverflow when shape’s dimensions multiply out to more elements than usize can represent — checked before anything else, so this never panics on a debug build the way an unchecked shape.iter().product() would.
  • DataError::TensorShapeMismatch when array.len() is not the product of shape’s dimensions.
Source

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.

Source

pub fn shape(&self) -> &[usize]

Extent of each dimension, outermost first.

Source

pub fn strides(&self) -> &[usize]

Element stride of each dimension.

Source

pub fn ndim(&self) -> usize

Number of dimensions.

Source

pub fn len(&self) -> usize

Total number of logical elements — the product of TensorView::shape, 1 for a zero-dimensional (scalar) tensor.

Source

pub fn is_empty(&self) -> bool

Returns true when any dimension is zero.

Source

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.

Source

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);
Source

pub fn flat_index(&self, index: &[usize]) -> Result<usize, DataError>

The flat element offset index names, checked against the shape.

§Errors
Source

pub fn get(&self, index: &[usize]) -> Result<T, DataError>

The value at index.

§Errors

Whatever TensorView::flat_index reports.

Source

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().

Source

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>

Source§

fn clone(&self) -> TensorView<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for TensorView<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> !Freeze for TensorView<T>

§

impl<T> RefUnwindSafe for TensorView<T>

§

impl<T> Send for TensorView<T>
where PrimitiveArray<T>: Send,

§

impl<T> Sync for TensorView<T>
where PrimitiveArray<T>: Sync,

§

impl<T> Unpin for TensorView<T>

§

impl<T> UnsafeUnpin for TensorView<T>

§

impl<T> UnwindSafe for TensorView<T>

Blanket Implementations§

Source§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> MaybeDebug for T

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more