Skip to main content

CompressedSparseData2

Struct CompressedSparseData2 

Source
pub struct CompressedSparseData2<T, U = T>
where T: Clone, U: Clone,
{ pub data: Vec<T>, pub indices: Vec<u32>, pub indptr: Vec<u32>, pub cs_type: CompressedSparseFormat, pub data_2: Option<Vec<U>>, pub shape: (usize, usize), }
Expand description

Structure to store compressed sparse data with two potential data layers. Think for example in single cell raw counts and normalised counts.

Fields§

§data: Vec<T>

The first data slot for this compressed sparse data format

§indices: Vec<u32>

The indices of the data points

§indptr: Vec<u32>

The indptr of the data points

§cs_type: CompressedSparseFormat

Enum defining if the data is stored in CSC or CSR, see CompressedSparseFormat

§data_2: Option<Vec<U>>

Optional second data slot for a different layer of the data (for example raw and normalised counts)

§shape: (usize, usize)

Shape of the data (rows, cols)

Implementations§

Source§

impl<T, U> CompressedSparseData2<T, U>

Source

pub fn to_bytes(&self) -> Result<Vec<u8>, SparseErrors>

Serialise the matrix to bytes.

§Returns

A blob that Self::from_bytes reads back.

Source

pub fn from_bytes(bytes: &[u8]) -> Result<Self, SparseErrors>

Read a matrix written by Self::to_bytes.

The decoded structure is validated, since the bytes may come from disk.

§Params
  • bytes - The blob
§Returns

The matrix, or a header, decode or validation error.

Source§

impl<T, U> CompressedSparseData2<T, U>

Source

pub fn new_csc( data: &[T], indices: &[u32], indptr: &[u32], data2: Option<&[U]>, shape: (usize, usize), ) -> Self

Generate a nes CSC version of the matrix

§Params
  • data - The underlying data
  • indices - The index positions (in this case row indices)
  • indptr - The index pointer (in this case the column index pointers)
  • data2 - An optional second layer
  • shape - (nrow, ncol).
§Returns

Self as a CSC-type matrix

Source

pub fn new_csr( data: &[T], indices: &[u32], indptr: &[u32], data2: Option<&[U]>, shape: (usize, usize), ) -> Self

Generate a nes CSR version of the matrix

§Params
  • data - The underlying data
  • indices - The index positions (in this case row indices)
  • indptr - The index pointer (in this case the column index pointers)
  • data2 - An optional second layer
  • shape - (nrow, ncol).
§Returns

Self as a CSR-type matrix

Source

pub fn from_parts( data: Vec<T>, indices: Vec<u32>, indptr: Vec<u32>, data2: Option<Vec<U>>, cs_type: CompressedSparseFormat, shape: (usize, usize), ) -> Self

Take ownership of buffers that were built for this matrix

CompressedSparseData2::new_csr and its CSC sibling copy all three buffers, which doubles peak memory for any caller that assembled them itself and then throws the originals away.

§Params
  • data - The underlying data, moved.
  • indices - The minor-axis indices, moved.
  • indptr - The major-axis pointers, moved.
  • data2 - An optional second layer, moved.
  • cs_type - Which orientation the buffers describe.
  • shape - (nrow, ncol).
§Returns

The matrix, with no buffer copied.

Source

pub fn transform(&self) -> Self

Transform from CSC to CSR or vice versa

§Returns

The transformed/transposed version

Source

pub fn transform_single_layer( &self, use_second_layer: bool, ) -> Result<Self, SparseErrors>

Transform from CSC to CSR or vice versa, keeping only one data layer.

§Params
  • use_second_layer - If true, the output keeps data_2 and data is empty. If false, the output keeps data and data_2 is None.
§Returns

The transformed/transposed version with only the requested layer.

Source

pub fn transpose_and_convert(&self) -> Self

Transpose and convert

§Returns

The transformed/transposed version

Source

pub fn from_upper_triangle_sym( upper_triangle: &[T], n: usize, include_diagonal: bool, format: CompressedSparseFormat, ) -> Self

Create a sparse matrix from an upper triangular matrix.

§Params
  • upper_triangle - The upper triangular matrix.
  • n - The number of rows and columns in the matrix.
  • include_diagonal - Whether to include the diagonal elements.
  • format - The format of the sparse matrix, see CompressedSparseFormat.
§Returns

A sparse matrix.

Source

pub fn shape(&self) -> (usize, usize)

Returns the shape of the matrix

§Returns

A tuple of (n_row, n_col)

Source

pub fn nrows(&self) -> usize

Return the number of rows

§Returns

Number of rows

Source

pub fn ncols(&self) -> usize

Return the number of columns

§Returns

Number of columns

Source

pub fn get_nnz(&self) -> usize

Returns the NNZ

§Returns

The number of NNZ

Source

pub fn get_data2_unsafe(&self) -> Vec<U>

Return the second layer

If this does not exist, the function will panic

§Returns

Vector of the second layer

Source

pub fn to_sparse_axes(&self) -> Vec<SparseAxis<T, U>>

Split into one owned SparseAxis per major-axis run.

Columns of a CSC or rows of a CSR. A layer that is empty (not populated) stays empty on every axis.

§Returns

The axes in order, each sized to the minor dimension.

Source

pub fn slice_rows(&self, rows: &[usize]) -> Result<Self, SparseErrors>

Slice the rows of the matrix

§Params
  • rows - The row indices to keep
§Returns

Self with the rows to keep

Source

pub fn slice_cols(&self, cols: &[usize]) -> Result<Self, SparseErrors>

Slice the columns of the matrix

§Params
  • cols - The column indices to keep
§Returns

Self with the cols to keep

Source

pub fn slice( &self, rows: &[usize], cols: &[usize], ) -> Result<Self, SparseErrors>

Slice across rows and columns

§Params
  • rows - The row indices to keep
  • cols - The column indices to keep
§Returns

Self with the rows and columns to keep

Source

pub fn from_entry_fn<G>( nrows: usize, ncols: usize, format: CompressedSparseFormat, get: G, ) -> Self
where T: BixverseNumeric + Float, G: Fn(usize, usize) -> T,

Build a matrix by scanning every (row, col) entry of a virtual dense matrix and storing the non-zeros.

§Params
  • nrows - Number of rows
  • ncols - Number of columns
  • format - Format to generate; the scan runs along its major axis
  • get - Value at (row, col)
§Returns

The sparse matrix, with no second layer.

Source

pub fn validate(&self) -> Result<(), SparseErrors>

Check every invariant that indexing this matrix relies on.

Self::assert_invariants is a debug_assert helper and compiles out in release, and it never checks the indices against the minor axis. This is the release-mode counterpart, for input that crossed an FFI boundary: the fields are public, so a caller can hand over an indptr that disagrees with shape or an index past the minor axis, and any consumer that scatters into a dense buffer sized from shape would then panic inside a worker thread or, worse, silently process fewer major runs than the shape declares.

A layer that is empty is treated as absent rather than as a length mismatch, since Self::transform_single_layer deliberately leaves the unused layer empty.

§Returns

Ok(()), or the first invariant that failed: SparseErrors::SparseIndptrInvalid for an indptr that disagrees with shape or with the number of stored values, SparseErrors::DimensionMisMatchSparse for a populated layer whose length does not match indices, and SparseErrors::SliceIndexOutOfBounds for an index past the minor axis.

Source

pub fn assert_invariants(&self)

Assertion helper for invariants

Trait Implementations§

Source§

impl<T, U> Clone for CompressedSparseData2<T, U>
where T: Clone + Clone, U: Clone + Clone,

Source§

fn clone(&self) -> Self

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, U> Debug for CompressedSparseData2<T, U>
where T: Clone + Debug, U: Clone + Debug,

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<'de, T, U> Deserialize<'de> for CompressedSparseData2<T, U>
where T: Clone + Deserialize<'de>, U: Clone + Deserialize<'de>,

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T, U> Serialize for CompressedSparseData2<T, U>
where T: Clone + Serialize, U: Clone + Serialize,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<T, U> Freeze for CompressedSparseData2<T, U>
where Vec<T>: Freeze, Option<Vec<U>>: Freeze,

§

impl<T, U> RefUnwindSafe for CompressedSparseData2<T, U>

§

impl<T, U> Send for CompressedSparseData2<T, U>
where Vec<T>: Send, Option<Vec<U>>: Send,

§

impl<T, U> Sync for CompressedSparseData2<T, U>
where Vec<T>: Sync, Option<Vec<U>>: Sync,

§

impl<T, U> Unpin for CompressedSparseData2<T, U>
where Vec<T>: Unpin, Option<Vec<U>>: Unpin,

§

impl<T, U> UnsafeUnpin for CompressedSparseData2<T, U>

§

impl<T, U> UnwindSafe for CompressedSparseData2<T, U>

Blanket Implementations§

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<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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.