Encoding Mixins#

Mixins that provide encode() interfaces for inference.

BasicEncodingMixin#

Simple encoding for models that produce single-resolution representations.

Lightweight shared encoding mixin for fixed-length sequence models.

Provides a uniform encode(data, batch_size, num_workers) API for models that process whole sequences in a single forward pass (TST, TimeVAE, TimeNet, MCL, TS-TCC, Series2Vec). Models that need sliding-window inference, multi-scale pooling, or mask-mode handling (the dilated trio: TS2Vec, AutoTCL, CoST) should use the heavier mixins under convolutional/dilated/_mixin/ instead.

Subclasses expose their encoder via BasicEncodingMixin._get_encoder() and, when needed, customize input preparation and output post-processing via _prepare_inputs() and _postprocess(). The mixin itself owns the DataLoader iteration, eval/inference mode handling, device placement, encoder invocation, and result concatenation.

class chronocratic.models._mixin.encoding.BasicEncodingMixin#

Bases: ABC

Uniform encode() API for fixed-length sequence models.

Designed to be mixed into a lightning.pytorch.LightningModule. The LightningModule is expected to provide self.device so that batches can be moved onto the model’s device.

Subclasses implement _get_encoder() (required) and may override _get_encoder_module(), _prepare_inputs(), and _postprocess() to adapt the contract to encoders that take extra arguments or return non-tensor structures.

The mixin manages the training / eval state of the encoder module (not the whole LightningModule) so that encode() does not perturb submodules that are unrelated to inference (e.g. contrastive heads, auxiliary loss modules).

encode(data: Tensor, batch_size: int, num_workers: int = 0) Tensor#

Extract representations for data in mini-batches.

Iterates data through a DataLoader, moves each batch to self.device, invokes the encoder via the _get_encoder / _prepare_inputs / _postprocess hooks under inference_mode, and concatenates the per-batch outputs on dim 0. The model’s prior training-mode state is preserved across the call.

Parameters:
  • data – Input tensor of shape (N, ...) — leading dim is the sample dimension, the rest is whatever the model expects.

  • batch_size – Mini-batch size for inference.

  • num_workers – Number of DataLoader workers.

Returns:

CPU tensor of shape (N, ...) — concatenation of per-batch representations along dim 0.

PoolingEncodingMixin#

Multi-resolution encoding with sliding window and pooling. Used by dilated convolutional models.

class chronocratic.models.convolutional.dilated._mixin.encoding.BaseEncodingMixin#

Bases: ABC

Base mixin providing shared encoding logic for time series models.

Contains the public encode() entry point, sliding window computation, and default strategy methods. Subclasses override _get_encoder(), _get_eval_method(), and _get_slice() to provide model-specific behavior.

Attributes are intentionally declared here but implemented by subclasses: TS2Vec, AutoTCL (pooling), CoST (decomposition).

encode(data: Tensor, batch_size: int, num_workers: int, mask: MaskMode | None = None, encoding_window: str | int | None = None, *, causal: bool = False, sliding_length: int | None = None, sliding_padding: int = 0) Tensor#

Compute representations using the model.

Parameters:
  • data – Shape (n_instance, n_timestamps, n_features). Missing data set to NaN.

  • batch_size – Batch size used for inference.

  • num_workers – Number of workers used for data loading.

  • mask – Mask for the encoder. One of ‘binomial’, ‘continuous’, ‘all_true’, ‘all_false’, or ‘mask_last’.

  • encoding_window – Pooling strategy. ‘full_series’, ‘multiscale’, or an integer for the pooling kernel size.

  • causal – If True, future information is not encoded.

  • sliding_length – Sliding window length. If set, sliding inference is applied.

  • sliding_padding – Contextual data length for each sliding window.

Returns:

The representations for data.

class chronocratic.models.convolutional.dilated._mixin.encoding.DecompositionEncodingMixin#

Bases: BaseEncodingMixin

Mixin for decomposition-based encoding (CoST).

Extends BaseEncodingMixin with trend+seasonality feature concatenation evaluation. Models inheriting this mixin encode decomposed components and concatenate their final-step features rather than using pooling slices.

class chronocratic.models.convolutional.dilated._mixin.encoding.PoolingEncodingMixin#

Bases: BaseEncodingMixin

Mixin for pooling-based encoding (TS2Vec, AutoTCL).

Extends BaseEncodingMixin with slice computation and pooling evaluation strategies. Models inheriting this mixin use multi-scale or integer pooling to produce fixed-length representations.