MCPcopy Create free account
hub / github.com/bamler-lab/constriction / EncoderModel

Interface EncoderModel

src/stream/model.rs:317–386  ·  view source on GitHub ↗

A trait for [`EntropyModel`]s that can be used for encoding (compressing) data. As discussed in the [module level documentation](self), all stream codes in `constriction` use so-called [`EntropyModel`]s for encoding and/or decoding data. Some of these `EntropyModel`s may be used only for encoding, only for decoding, or for both, depending on their internal representation. This `EncoderModel` tra

Source from the content-addressed store, hash-verified

315/// [`AnsCoder::encode_symbols_reverse`]: super::stack::AnsCoder::encode_symbols_reverse
316/// [`Encode::encode_iid_symbols`]: super::Encode::encode_iid_symbols
317pub trait EncoderModel<const PRECISION: usize>: EntropyModel<PRECISION> {
318 /// Looks up a symbol in the entropy model.
319 ///
320 /// Takes a `symbol` either by value or by reference and looks it up in the entropy
321 /// model.
322 /// - If `symbol` has a nonzero probability under the model, then this method returns
323 /// `Some((left_sided_cumulative, probability))`, where `probability` is the
324 /// probability in fixed-point representation (see
325 /// [discussion](EntropyModel::Probability)) and `left_sided_cumulative` is the sum of
326 /// the probabilities of all symbols up to but not including `symbol` (also in
327 /// fixed-point representation). Both `left_sided_cumulative` and `probability` are
328 /// guaranteed to be strictly smaller than `1 << PRECISION` (which would semantically
329 /// represent "probability one") because `probability` is nonzero and because we don't
330 /// support degenerate entropy models that put all probability mass on a single
331 /// symbol.
332 /// - If `symbol` has zero probability under the model, then this method returns `None`.
333 fn left_cumulative_and_probability(
334 &self,
335 symbol: impl Borrow<Self::Symbol>,
336 ) -> Option<(Self::Probability, <Self::Probability as BitArray>::NonZero)>;
337
338 /// Returns the probability of the given symbol in floating point representation.
339 ///
340 /// The trait bound `Self::Probability: Into<F>` guarantees that no rounding occurs in
341 /// the conversion. You may have to specify the return type explicitly using "turbofish"
342 /// notation `::<f64>(...)` or `::<f32>(...)`, see example below.
343 ///
344 /// Returns `0.0` if `symbol` is not in the support of the entropy model.
345 ///
346 /// This method is provided mainly as a convenience for debugging.
347 ///
348 /// # Example
349 ///
350 /// ```
351 /// use constriction::stream::model::{EncoderModel, DefaultNonContiguousCategoricalEncoderModel};
352 ///
353 /// let symbols = vec!['a', 'b', 'c', 'd'];
354 /// let probabilities = vec![1u32 << 21, 1 << 23, 1 << 22, 1 << 21];
355 /// let model = DefaultNonContiguousCategoricalEncoderModel // "Default" uses `PRECISION = 24`
356 /// ::from_symbols_and_nonzero_fixed_point_probabilities(
357 /// symbols.iter().copied(), probabilities.iter().copied(), false)
358 /// .unwrap();
359 ///
360 /// assert_eq!(model.floating_point_probability::<f64>('a'), 0.125);
361 /// assert_eq!(model.floating_point_probability::<f64>('b'), 0.5);
362 /// assert_eq!(model.floating_point_probability::<f64>('c'), 0.25);
363 /// assert_eq!(model.floating_point_probability::<f64>('d'), 0.125);
364 /// assert_eq!(model.floating_point_probability::<f64>('x'), 0.0);
365 /// ```
366 ///
367 /// [`fixed_point_probabilities`]: #method.fixed_point_probabilities
368 /// [`floating_point_probabilities_lossy`]: #method.floating_point_probabilities_lossy
369 /// [`from_floating_point_probabilities`]: #method.from_floating_point_probabilities
370 /// [`from_nonzero_fixed_point_probabilities`]:
371 /// #method.from_nonzero_fixed_point_probabilities
372 #[inline]
373 fn floating_point_probability<F>(&self, symbol: Self::Symbol) -> F
374 where

Callers

nothing calls this directly

Implementers 7

internals.rssrc/pybindings/stream/model/internals.
model.rssrc/stream/model.rs
uniform.rssrc/stream/model/uniform.rs
quantize.rssrc/stream/model/quantize.rs
contiguous.rssrc/stream/model/categorical/contiguou
non_contiguous.rssrc/stream/model/categorical/non_conti
lazy_contiguous.rssrc/stream/model/categorical/lazy_cont

Calls

no outgoing calls

Tested by

no test coverage detected