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 MNIST_PACK: Pack = Pack {
	name: "mnist",
	version: "1.0.0",
	license: "CC-BY-SA-3.0",
	files: &[
		PackFile {
			name: "train-images-idx3-ubyte.gz",
			size: 9_912_422,
			shake256: "1314ff889cdcbd8edcfad0916f94202767b7258e93ab75534affc2ccbb6fa575",
			urls: &[
				"https://storage.googleapis.com/cvdf-datasets/mnist/train-images-idx3-ubyte.gz",
				"https://ossci-datasets.s3.amazonaws.com/mnist/train-images-idx3-ubyte.gz",
			],
		},
		PackFile {
			name: "train-labels-idx1-ubyte.gz",
			size: 28_881,
			shake256: "6bcc237fd21a81c96ff200bdae886930ad9b9576fcd16bc49038f5ae7e275c01",
			urls: &[
				"https://storage.googleapis.com/cvdf-datasets/mnist/train-labels-idx1-ubyte.gz",
				"https://ossci-datasets.s3.amazonaws.com/mnist/train-labels-idx1-ubyte.gz",
			],
		},
		PackFile {
			name: "t10k-images-idx3-ubyte.gz",
			size: 1_648_877,
			shake256: "fb5dd7142cba2dad6230d7b75b31917b34294280b67b60d7b95bc2e1e1dcd671",
			urls: &[
				"https://storage.googleapis.com/cvdf-datasets/mnist/t10k-images-idx3-ubyte.gz",
				"https://ossci-datasets.s3.amazonaws.com/mnist/t10k-images-idx3-ubyte.gz",
			],
		},
		PackFile {
			name: "t10k-labels-idx1-ubyte.gz",
			size: 4_542,
			shake256: "105e631874f55e52bafd7c85ab4be9152f8296fc7aaa475815d8f56f654faea7",
			urls: &[
				"https://storage.googleapis.com/cvdf-datasets/mnist/t10k-labels-idx1-ubyte.gz",
				"https://ossci-datasets.s3.amazonaws.com/mnist/t10k-labels-idx1-ubyte.gz",
			],
		},
	],
};

/// Official MNIST split selection.
pub type MnistSplit = IdxSplit;

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

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

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