Skip to main content

Sample

Struct Sample 

Source
pub struct Sample { /* private fields */ }
Expand description

One training example: an insertion-ordered map from file extension to value.

Fields whose names start with __ are metadata and are skipped by decoding, encoding, and key extraction.

use webdataset_core::{Sample, Value};

let mut sample = Sample::with_key("image0001");
sample.insert("cls", Value::Int(7));
sample.insert("txt", Value::Text("a caption".into()));

assert_eq!(sample.key(), Some("image0001"));
assert_eq!(sample.get("cls").and_then(Value::as_i64), Some(7));
assert_eq!(sample.field_names(), vec!["cls", "txt"]);

Implementations§

Source§

impl Sample

Source

pub fn new() -> Sample

An empty sample with no fields at all.

Source

pub fn with_key(key: impl Into<String>) -> Sample

An empty sample carrying only its __key__.

Source

pub fn key(&self) -> Option<&str>

The sample’s __key__, if it has one.

Source

pub fn url(&self) -> Option<&str>

The URL of the shard this sample came from.

Source

pub fn local_path(&self) -> Option<&str>

The on-disk path of the shard, when it was read or cached locally.

Source

pub fn set_key(&mut self, key: impl Into<String>)

Set the __key__ field.

Source

pub fn set_url(&mut self, url: impl Into<String>)

Set the __url__ field.

Source

pub fn get(&self, name: &str) -> Option<&Value>

Look up a field.

Source

pub fn get_mut(&mut self, name: &str) -> Option<&mut Value>

Look up a field for modification.

Source

pub fn get_first(&self, names: &[&str]) -> Option<&Value>

Look up the first field that exists among several alternatives.

Alternatives are given either as a slice or, following the Python API, as a single ;-separated string (see Sample::get_first_spec).

Source

pub fn get_first_spec(&self, spec: &str) -> Option<&Value>

Look up the first field named by a ;-separated alternation such as "png;jpg;jpeg".

Source

pub fn require_first_spec(&self, spec: &str) -> Result<&Value>

Like Sample::get_first_spec but reports which alternatives were tried.

Source

pub fn insert( &mut self, name: impl Into<String>, value: impl Into<Value>, ) -> Option<Value>

Insert a field, returning the value it replaced.

Source

pub fn remove(&mut self, name: &str) -> Option<Value>

Remove a field, returning its value.

Source

pub fn contains_key(&self, name: &str) -> bool

Whether a field is present.

Source

pub fn len(&self) -> usize

The number of fields, metadata included.

Source

pub fn is_empty(&self) -> bool

Whether the sample has no fields at all.

Source

pub fn iter(&self) -> MapIter<'_, String, Value> ⓘ

Iterate over all fields in insertion order.

Source

pub fn keys(&self) -> impl Iterator<Item = &str>

All field names, metadata included.

Source

pub fn field_names(&self) -> Vec<&str>

The names of the data (non-__) fields.

Source

pub fn as_map(&self) -> &Fields

Borrow the underlying map.

Source

pub fn into_map(self) -> Fields

Consume the sample and return the underlying map.

Source

pub fn is_valid(&self) -> bool

Whether this sample should be passed downstream.

Mirrors valid_sample in the Python implementation: a sample is valid when it has at least one field and is not marked __bad__.

Source

pub fn rename(&mut self, from: &str, to: impl Into<String>) -> bool

Rename a field, preserving its position in the field order.

Trait Implementations§

Source§

impl Clone for Sample

Source§

fn clone(&self) -> Sample

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Sample

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Sample

Source§

fn default() -> Sample

Returns the “default value” for a type. Read more
Source§

impl From<IndexMap<String, Value, BuildHasherDefault<Fnv1a>>> for Sample

Source§

fn from(fields: Fields) -> Sample

Converts to this type from the input type.
Source§

impl FromIterator<(String, Value)> for Sample

Source§

fn from_iter<T: IntoIterator<Item = (String, Value)>>(iter: T) -> Sample

Creates a value from an iterator. Read more
Source§

impl Index<&str> for Sample

Source§

type Output = Value

The returned type after indexing.
Source§

fn index(&self, name: &str) -> &Value

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a> IntoIterator for &'a Sample

Source§

type Item = (&'a String, &'a Value)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, String, Value>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl IntoIterator for Sample

Source§

type Item = (String, Value)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<String, Value>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl PartialEq for Sample

Source§

fn eq(&self, other: &Sample) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Sample

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> CustomValue for T
where T: Any + Debug + Send + Sync,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Upcast so callers can downcast to the concrete type.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

Source§

fn read_from_little_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_little_endian().
Source§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
Source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.