oadsrust 0.2.0

Content-addressed dataset packs and loaders for OA
// @generated by tool/gen/ds/generate.py — edit tool/gen/ds/schema/packs.json instead.
use crate::idx::IdxSplit;
use crate::pack::{FetchProgress, Pack, PackFile};
use oa::{Engine, Result};
use std::path::PathBuf;

pub(crate) const EMNIST_PACK: Pack = Pack {
	name: "emnist",
	version: "1.0.0",
	license: "CC-BY-SA-3.0",
	files: &[PackFile {
		name: "gzip.zip",
		size: 561_753_746,
		shake256: "1c1360c74f9a37e9e9af8db6a63ebbae9afc377e1b7bb1204f7f169289c4dc5c",
		urls: &["https://biometrics.nist.gov/cs_links/EMNIST/gzip.zip"],
	}],
};

/// Official EMNIST split selection.
pub type EmnistSplit = IdxSplit;

/// Load options for [`Emnist`][crate::image::emnist::Emnist].
#[derive(Clone, Debug)]
pub struct EmnistConfig {
	/// Which official split to materialize.
	pub split: EmnistSplit,
	/// Class grouping to decode from the shared zip; see
	/// [`EmnistVariant`][super::EmnistVariant].
	pub variant: super::EmnistVariant,
	/// Cache root override. `None` resolves `OA_DATA_DIR`, then the platform
	/// cache directory; see [`data_root`](crate::pack::data_root).
	pub cache_dir: Option<PathBuf>,
	/// Scale pixels into `[0, 1]`. `false` keeps raw `0..=255` values.
	pub normalize: bool,
}

impl Default for EmnistConfig {
	fn default() -> Self {
		Self {
			split: EmnistSplit::Train,
			variant: super::EmnistVariant::Balanced,
			cache_dir: None,
			normalize: true,
		}
	}
}

impl EmnistConfig {
	/// Construct the record with its default state.
	#[inline]
	#[must_use]
	pub fn new() -> Self {
		Self::default()
	}
}

impl super::Emnist {
	/// Decode the selected split and upload it on this thread's
	/// default engine.
	///
	/// # Errors
	///
	/// Returns an error when the default engine cannot be
	/// initialized, plus the errors of
	/// [`load_on`](Self::load_on).
	#[inline]
	pub fn load(config: &EmnistConfig) -> Result<Self> {
		Self::load_on(&Engine::current()?, config)
	}

	/// Decode the selected split and upload it on `engine`.
	///
	/// This is the [`load`](Self::load) contract evaluated on
	/// `engine` instead of this thread's default engine.
	///
	/// # Errors
	///
	/// Returns the errors of
	/// [`load_with_progress_on`](Self::load_with_progress_on).
	#[inline]
	pub fn load_on(engine: &Engine, config: &EmnistConfig) -> Result<Self> {
		Self::load_with_progress_on(engine, config, &mut |_| true)
	}

	/// [`load`](Self::load) with a fetch progress callback.
	///
	/// # Errors
	///
	/// Returns an error when the default engine cannot be
	/// initialized, plus the errors of
	/// [`load_with_progress_on`](Self::load_with_progress_on).
	#[inline]
	pub fn load_with_progress(
		config: &EmnistConfig,
		progress: &mut impl FnMut(FetchProgress) -> bool,
	) -> Result<Self> {
		Self::load_with_progress_on(&Engine::current()?, config, progress)
	}
}