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

Method encode_reverse

src/pybindings/stream/stack.rs:530–591  ·  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

528 /// ```
529 #[pyo3(signature = (symbols, model, *optional_model_params))]
530 pub fn encode_reverse(
531 &mut self,
532 py: Python<'_>,
533 symbols: &Bound<'_, PyAny>,
534 model: &Model,
535 optional_model_params: &Bound<'_, PyTuple>,
536 ) -> PyResult<()> {
537 if let Ok(symbol) = symbols.extract::<i32>() {
538 if !optional_model_params.is_empty() {
539 return Err(pyo3::exceptions::PyValueError::new_err(
540 "To encode a single symbol, use a concrete model, i.e., pass the\n\
541 model parameters directly to the constructor of the model and not to the\n\
542 `encode` method of the entropy coder. Delaying the specification of model\n\
543 parameters until calling `encode_reverse` is only useful if you want to encode
544 several symbols in a row with individual model parameters for each symbol. If\n\
545 this is what you're trying to do then the `symbols` argument should be a numpy\n\
546 array, not a scalar.",
547 ));
548 }
549 return model.0.as_parameterized(py, &mut |model| {
550 self.inner
551 .encode_symbol(symbol, EncoderDecoderModel(model))?;
552 Ok(())
553 });
554 }
555
556 // Don't use an `else` branch here because, if the following `extract` fails, the returned
557 // error message is actually pretty user friendly.
558 let symbols = symbols.extract::<PyReadonlyArray1<'_, i32>>()?;
559 let symbols = symbols.as_array();
560
561 if optional_model_params.is_empty() {
562 model.0.as_parameterized(py, &mut |model| {
563 self.inner
564 .encode_iid_symbols_reverse(symbols, EncoderDecoderModel(model))?;
565 Ok(())
566 })?;
567 } else {
568 if symbols.len()
569 != model.0.len(
570 optional_model_params
571 .get_borrowed_item(0)
572 .expect("len checked above"),
573 )?
574 {
575 return Err(pyo3::exceptions::PyValueError::new_err(
576 "`symbols` argument has wrong length.",
577 ));
578 }
579 let mut symbol_iter = symbols.iter().rev();
580 model
581 .0
582 .parameterize(py, optional_model_params, true, &mut |model| {
583 let symbol = symbol_iter.next().expect("TODO");
584 self.inner
585 .encode_symbol(*symbol, EncoderDecoderModel(model))?;
586 Ok(())
587 })?;

Callers 15

test_module_example1Function · 0.95
test_old_module_example1Function · 0.95
test_stack1Function · 0.95
test_stack2Function · 0.95
test_ans_encode_reverse1Function · 0.95
test_ans_encode_reverse2Function · 0.95
test_ans_encode_reverse3Function · 0.95
test_ans_encode_reverse4Function · 0.95
test_ans_seekFunction · 0.95
test_ans_exampleFunction · 0.95
fixed_model_paramsFunction · 0.95
variable_model_paramsFunction · 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 15

test_module_example1Function · 0.76
test_old_module_example1Function · 0.76
test_stack1Function · 0.76
test_stack2Function · 0.76
test_ans_encode_reverse1Function · 0.76
test_ans_encode_reverse2Function · 0.76
test_ans_encode_reverse3Function · 0.76
test_ans_encode_reverse4Function · 0.76
test_ans_seekFunction · 0.76
test_ans_exampleFunction · 0.76
fixed_model_paramsFunction · 0.76
variable_model_paramsFunction · 0.76