Skip to content

Quickstart ​

Write a two-row, three-column f32 matrix, read it into a caller-owned dense buffer, and inspect its CSR row offsets and column indices:

Input matrix · 2 rows × 3 columns
RowCol 0Col 1Col 2
0020
1103

Complete example ​

rs
use std::error::Error;
use std::path::PathBuf;

use cellpack::format::{Dtype, SourceLayout};
use cellpack::scans::*;
use cellpack::store::{CpkFile, CpkOptions, CpkWriter, ReadWorkspace};

fn main() -> Result<(), Box<dyn Error>> {
    let directory = PathBuf::from(
        std::env::args_os()
            .nth(1)
            .ok_or("pass an output directory")?,
    );
    std::fs::create_dir_all(&directory)?;
    let path = directory.join("matrix.cpk");
    let values = [0.0f32, 2.0, 0.0, 1.0, 0.0, 3.0];
    let bytes: Vec<u8> = values.iter().flat_map(|v| v.to_le_bytes()).collect();

    let options = CpkOptions::new(EncodeOptions {
        renumber: true,
        thresholds: Thresholds {
            indices: DEFAULT_MU_INDICES,
            values: DEFAULT_MU_VALUES,
        },
        max_lanes: DEFAULT_MAX_LANES,
        lane_min: DEFAULT_LANE_MIN,
        column_modes: Some(DEFAULT_MODE_SHARE),
        layout: LayoutChoice::Auto,
        values: ValueChoice::Auto,
    });
    let mut writer = CpkWriter::create(&path, 3, Dtype::F32, SourceLayout::Dense, options)?;
    writer.append(DenseRef {
        n_genes: 3,
        value_width: 4,
        data: &bytes,
    })?;
    writer.finish()?;

    let file = CpkFile::open(&path)?;
    assert_eq!((file.n_rows(), file.n_cols()), (2, 3));
    let selection = file.prepare_read(0..2)?;
    let mut workspace = ReadWorkspace::default();
    let mut dense = vec![0.0f32; selection.spec().dense_len()?];
    selection.read_dense_into(&mut dense, &mut workspace, 1)?;
    assert_eq!(dense, values);

    let csr = file.read_csr(0..2)?;
    assert_eq!(csr.indptr, [0, 1, 3]);
    assert_eq!(csr.indices, [1, 0, 2]);
    println!(
        "round-trip: {} rows, {} columns",
        file.n_rows(),
        file.n_cols()
    );
    Ok(())
}

Follow installation to create a Rust application with the crates.io dependency. Save the complete source as src/main.rs, then run from your application's directory:

sh
cargo run -- output/quickstart

Expected output:

text
round-trip: 2 rows, 3 columns

How it works ​

  1. Convert values to little-endian bytes for DenseRef. The codec accepts a byte view; value_width must agree with the file's dtype.
  2. CpkWriter::create declares the columns, dtype, source layout, and encoding options. append adds rows in order.
  3. finish flushes the tail chunk, writes the directory, and finally publishes the valid superblock. Hand the file to readers only after it succeeds.
  4. prepare_read prepares metadata and output dimensions. read_dense_into performs the record reads and writes the typed output.
  5. ReadWorkspace retains reusable scratch. The application owns the output.

CpkOptions::new targets 4096-row chunks and 4096-byte pages. This matrix is smaller than a chunk, so finish writes its two remaining rows. LayoutChoice::Auto lets each record choose a suitable layout; SourceLayout::Dense describes the input rather than forcing dense records.

Continue with storage for CSR input, gathers, and column mapping, or loading for repeated batch delivery.