#![allow(dead_code)]
use std::sync::Arc;
#[cfg(feature = "hdf5")]
use crate::sparse_backend::hdf5 as sparse_matrix_hdf5;
use crate::sparse_backend::zarr as sparse_matrix_zarr;
use super::{Array2, DMatrix, SparseIo, SparseIoBackend};
#[cfg(not(feature = "hdf5"))]
fn hdf5_disabled<T>() -> anyhow::Result<T> {
anyhow::bail!(
"HDF5 backend selected but data-beans was built without the `hdf5` feature. \
Reinstall with `--features hdf5` (requires libhdf5) or use the Zarr backend."
)
}
pub fn open_sparse_matrix(
backend_file: &str,
backend: &SparseIoBackend,
) -> anyhow::Result<Box<dyn SparseIo<IndexIter = Vec<usize>>>> {
match backend {
SparseIoBackend::Zarr => Ok(Box::new(sparse_matrix_zarr::SparseMtxData::open(
backend_file,
)?)),
#[cfg(feature = "hdf5")]
SparseIoBackend::HDF5 => Ok(Box::new(sparse_matrix_hdf5::SparseMtxData::open(
backend_file,
)?)),
#[cfg(not(feature = "hdf5"))]
SparseIoBackend::HDF5 => hdf5_disabled(),
}
}
pub fn open_sparse_matrix_by_path(
file_path: &str,
) -> anyhow::Result<Box<dyn SparseIo<IndexIter = Vec<usize>>>> {
let (backend, backend_file) = crate::hdf5_io::resolve_backend_file(file_path, None)?;
open_sparse_matrix(&backend_file, &backend)
}
pub fn create_sparse_from_triplets(
triplets: &[(u64, u64, f32)],
mtx_shape: (usize, usize, usize),
backend_file: Option<&str>,
backend: Option<&SparseIoBackend>,
) -> anyhow::Result<Box<dyn SparseIo<IndexIter = Vec<usize>>>> {
create_sparse_from_triplets_owned(triplets.to_vec(), mtx_shape, backend_file, backend)
}
pub fn create_sparse_from_triplets_owned(
mut triplets: Vec<(u64, u64, f32)>,
mtx_shape: (usize, usize, usize),
backend_file: Option<&str>,
backend: Option<&SparseIoBackend>,
) -> anyhow::Result<Box<dyn SparseIo<IndexIter = Vec<usize>>>> {
match backend {
#[cfg(feature = "hdf5")]
Some(SparseIoBackend::HDF5) => {
let mut ret = Box::new(sparse_matrix_hdf5::SparseMtxData::new(backend_file)?);
ret.record_mtx_shape(Some(mtx_shape))?;
ret.record_triplets_by_col(&mut triplets)?;
ret.record_triplets_by_row(&mut triplets)?;
ret.read_column_indptr()?;
ret.read_row_indptr()?;
Ok(ret)
}
#[cfg(not(feature = "hdf5"))]
Some(SparseIoBackend::HDF5) => hdf5_disabled(),
Some(SparseIoBackend::Zarr) | None => {
let mut ret = Box::new(sparse_matrix_zarr::SparseMtxData::new(backend_file)?);
ret.record_mtx_shape(Some(mtx_shape))?;
ret.record_triplets_by_col(&mut triplets)?;
ret.record_triplets_by_row(&mut triplets)?;
ret.read_column_indptr()?;
ret.read_row_indptr()?;
Ok(ret)
}
}
}
pub fn create_sparse_streaming_empty(
backend_file: Option<&str>,
backend: Option<&SparseIoBackend>,
) -> anyhow::Result<Box<dyn SparseIo<IndexIter = Vec<usize>>>> {
match backend {
#[cfg(feature = "hdf5")]
Some(SparseIoBackend::HDF5) => Ok(Box::new(sparse_matrix_hdf5::SparseMtxData::new(
backend_file,
)?)),
#[cfg(not(feature = "hdf5"))]
Some(SparseIoBackend::HDF5) => hdf5_disabled(),
Some(SparseIoBackend::Zarr) | None => Ok(Box::new(sparse_matrix_zarr::SparseMtxData::new(
backend_file,
)?)),
}
}
pub fn create_sparse_from_mtx_file(
mtx_file: &str,
backend_file: Option<&str>,
backend: Option<&SparseIoBackend>,
) -> anyhow::Result<Box<dyn SparseIo<IndexIter = Vec<usize>>>> {
match backend {
#[cfg(feature = "hdf5")]
Some(SparseIoBackend::HDF5) => Ok(Box::new(
sparse_matrix_hdf5::SparseMtxData::from_mtx_file(mtx_file, backend_file, Some(true))?,
)),
#[cfg(not(feature = "hdf5"))]
Some(SparseIoBackend::HDF5) => hdf5_disabled(),
Some(SparseIoBackend::Zarr) | None => Ok(Box::new(
sparse_matrix_zarr::SparseMtxData::from_mtx_file(mtx_file, backend_file, Some(true))?,
)),
}
}
pub fn create_sparse_from_ndarray(
data: &Array2<f32>,
backend_file: Option<&str>,
backend: Option<&SparseIoBackend>,
) -> anyhow::Result<Box<dyn SparseIo<IndexIter = Vec<usize>>>> {
match backend {
#[cfg(feature = "hdf5")]
Some(SparseIoBackend::HDF5) => Ok(Box::new(
sparse_matrix_hdf5::SparseMtxData::from_ndarray(data, backend_file, Some(true))?,
)),
#[cfg(not(feature = "hdf5"))]
Some(SparseIoBackend::HDF5) => hdf5_disabled(),
Some(SparseIoBackend::Zarr) | None => Ok(Box::new(
sparse_matrix_zarr::SparseMtxData::from_ndarray(data, backend_file, Some(true))?,
)),
}
}
pub fn create_sparse_from_dmatrix(
data: &DMatrix<f32>,
backend_file: Option<&str>,
backend: Option<&SparseIoBackend>,
) -> anyhow::Result<Box<dyn SparseIo<IndexIter = Vec<usize>>>> {
match backend {
#[cfg(feature = "hdf5")]
Some(SparseIoBackend::HDF5) => Ok(Box::new(
sparse_matrix_hdf5::SparseMtxData::from_dmatrix(data, backend_file, Some(true))?,
)),
#[cfg(not(feature = "hdf5"))]
Some(SparseIoBackend::HDF5) => hdf5_disabled(),
Some(SparseIoBackend::Zarr) | None => Ok(Box::new(
sparse_matrix_zarr::SparseMtxData::from_dmatrix(data, backend_file, Some(true))?,
)),
}
}
pub fn sparse_io_box_to_arc<T>(
boxed: Box<dyn SparseIo<IndexIter = T>>,
) -> Arc<dyn SparseIo<IndexIter = T>> {
Arc::from(boxed)
}