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 KMNIST_PACK: Pack = Pack {
	name: "kmnist",
	version: "1.0.0",
	license: "CC-BY-SA-4.0",
	files: &[
		PackFile {
			name: "train-images-idx3-ubyte.gz",
			size: 18_165_135,
			shake256: "514a63d19287b9363d1259a7c5a178a75b82322fbe90a9ac9bf240734250006b",
			urls: &["http://codh.rois.ac.jp/kmnist/dataset/kmnist/train-images-idx3-ubyte.gz"],
		},
		PackFile {
			name: "train-labels-idx1-ubyte.gz",
			size: 29_497,
			shake256: "271b9566abb706d1197a89a9940d3ea709ba49a9dc6a3dae7543f1ebf29b58b5",
			urls: &["http://codh.rois.ac.jp/kmnist/dataset/kmnist/train-labels-idx1-ubyte.gz"],
		},
		PackFile {
			name: "t10k-images-idx3-ubyte.gz",
			size: 3_041_136,
			shake256: "b8c5f3e82249027924131038bea598300a7a2639c411d3f2f79500a7667cf7bf",
			urls: &["http://codh.rois.ac.jp/kmnist/dataset/kmnist/t10k-images-idx3-ubyte.gz"],
		},
		PackFile {
			name: "t10k-labels-idx1-ubyte.gz",
			size: 5_120,
			shake256: "e834280bf6aefdd90293b34ead5e67b1e0c30cac93f1ed49ed500f6c7e3fa1d5",
			urls: &["http://codh.rois.ac.jp/kmnist/dataset/kmnist/t10k-labels-idx1-ubyte.gz"],
		},
	],
};

/// Official Kuzushiji-MNIST split selection.
pub type KMnistSplit = IdxSplit;

/// Load options for [`KMnist`][crate::image::kmnist::KMnist].
#[derive(Clone, Debug)]
pub struct KMnistConfig {
	/// Which official split to materialize.
	pub split: KMnistSplit,
	/// 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 KMnistConfig {
	fn default() -> Self {
		Self {
			split: KMnistSplit::Train,
			cache_dir: None,
			normalize: true,
		}
	}
}

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

impl super::KMnist {
	/// 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: &KMnistConfig) -> 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: &KMnistConfig) -> 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: &KMnistConfig,
		progress: &mut impl FnMut(FetchProgress) -> bool,
	) -> Result<Self> {
		Self::load_with_progress_on(&Engine::current()?, config, progress)
	}
}