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

Method encode_reverse

src/pybindings/stream/chain.rs:368–429  ·  view source on GitHub ↗
(
        &mut self,
        py: Python<'_>,
        symbols: &Bound<'_, PyAny>,
        model: &Model,
        optional_model_params: &Bound<'_, PyTuple>,
    )

Source from the content-addressed store, hash-verified

366 /// content of the encoded symbol under the employed entropy model).
367 #[pyo3(signature = (symbols, model, *optional_model_params))]
368 pub fn encode_reverse(
369 &mut self,
370 py: Python<'_>,
371 symbols: &Bound<'_, PyAny>,
372 model: &Model,
373 optional_model_params: &Bound<'_, PyTuple>,
374 ) -> PyResult<()> {
375 if let Ok(symbol) = symbols.extract::<i32>() {
376 if !optional_model_params.is_empty() {
377 return Err(pyo3::exceptions::PyValueError::new_err(
378 "To encode a single symbol, use a concrete model, i.e., pass the\n\
379 model parameters directly to the constructor of the model and not to the\n\
380 `encode` method of the entropy coder. Delaying the specification of model\n\
381 parameters until calling `encode_reverse` is only useful if you want to encode
382 several symbols in a row with individual model parameters for each symbol. If\n\
383 this is what you're trying to do then the `symbols` argument should be a numpy\n\
384 array, not a scalar.",
385 ));
386 }
387 return model.0.as_parameterized(py, &mut |model| {
388 self.inner
389 .encode_symbol(symbol, EncoderDecoderModel(model))?;
390 Ok(())
391 });
392 }
393
394 // Don't use an `else` branch here because, if the following `extract` fails, the returned
395 // error message is actually pretty user friendly.
396 let symbols = symbols.extract::<PyReadonlyArray1<'_, i32>>()?;
397 let symbols = symbols.as_array();
398
399 if optional_model_params.is_empty() {
400 model.0.as_parameterized(py, &mut |model| {
401 self.inner
402 .encode_iid_symbols_reverse(symbols, EncoderDecoderModel(model))?;
403 Ok(())
404 })?;
405 } else {
406 if symbols.len()
407 != model.0.len(
408 optional_model_params
409 .get_borrowed_item(0)
410 .expect("len checked above"),
411 )?
412 {
413 return Err(pyo3::exceptions::PyValueError::new_err(
414 "`symbols` argument has wrong length.",
415 ));
416 }
417 let mut symbol_iter = symbols.iter().rev();
418 model
419 .0
420 .parameterize(py, optional_model_params, true, &mut |model| {
421 let symbol = symbol_iter.next().expect("TODO");
422 self.inner
423 .encode_symbol(*symbol, EncoderDecoderModel(model))?;
424 Ok(())
425 })?;

Callers 9

run_decoder_partFunction · 0.95
fixed_model_paramsFunction · 0.95
variable_model_paramsFunction · 0.95
discrete_distributionFunction · 0.95
test_chain_gaussianFunction · 0.95
run_decoder_partFunction · 0.95
fixed_model_paramsFunction · 0.95
variable_model_paramsFunction · 0.95
discrete_distributionFunction · 0.95

Calls 9

EncoderDecoderModelClass · 0.85
iterMethod · 0.80
is_emptyMethod · 0.45
as_parameterizedMethod · 0.45
encode_symbolMethod · 0.45
lenMethod · 0.45
parameterizeMethod · 0.45
nextMethod · 0.45

Tested by 9

run_decoder_partFunction · 0.76
fixed_model_paramsFunction · 0.76
variable_model_paramsFunction · 0.76
discrete_distributionFunction · 0.76
test_chain_gaussianFunction · 0.76
run_decoder_partFunction · 0.76
fixed_model_paramsFunction · 0.76
variable_model_paramsFunction · 0.76
discrete_distributionFunction · 0.76