Appearance
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:
| Row | Col 0 | Col 1 | Col 2 |
|---|---|---|---|
| 0 | 0 | 2 | 0 |
| 1 | 1 | 0 | 3 |
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/quickstartExpected output:
text
round-trip: 2 rows, 3 columnsHow it works
- Convert values to little-endian bytes for
DenseRef. The codec accepts a byte view;value_widthmust agree with the file's dtype. CpkWriter::createdeclares the columns, dtype, source layout, and encoding options.appendadds rows in order.finishflushes the tail chunk, writes the directory, and finally publishes the valid superblock. Hand the file to readers only after it succeeds.prepare_readprepares metadata and output dimensions.read_dense_intoperforms the record reads and writes the typed output.ReadWorkspaceretains 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.