neural_lam.models.modules.deterministic
=======================================

.. py:module:: neural_lam.models.modules.deterministic

.. autoapi-nested-parse::

   Lightning module evaluating forecasters through a single deterministic
   forecast per batch.



Classes
-------

.. autoapisummary::

   neural_lam.models.modules.deterministic.DeterministicForecastingModule


Module Contents
---------------

.. py:class:: DeterministicForecastingModule(forecaster: neural_lam.models.forecasters.deterministic.BaseDeterministicForecaster, config: neural_lam.config.NeuralLAMConfig, datastore: neural_lam.datastore.base.BaseDatastore, datastore_boundary: neural_lam.datastore.base.BaseDatastore | None = None, lr: float = 0.001, restore_opt: bool = False, n_example_pred: int = 1, create_gif: bool = False, val_steps_to_log: list[int] | None = None, train_steps_to_log: list[int] | None = None, metrics_watch: list[str] | None = None, var_leads_metrics_watch: dict[int, list[int]] | None = None, args: Any | None = None)

   Bases: :py:obj:`neural_lam.models.modules.base.BaseForecastingModule`


   Lightning module for a single deterministic forecast per batch.

   Validation and testing evaluate the forecaster's own single prediction,
   as opposed to ``ProbabilisticForecastingModule``, which samples and scores
   an ensemble. Training is shared with that module unchanged (see
   ``BaseForecastingModule.training_step``).

   The reported loss comes from ``forecaster.compute_loss_from_forecast``,
   since only the forecaster knows its objective. The reported metrics
   (mse, mae) are
   computed here from ``neural_lam.metrics``: they are fixed regardless of
   what the forecaster trains on, so routing them through it would add a
   layer without adding meaning.

   Initialize the module and its deterministic evaluation metrics.

   :param forecaster: The forecaster to evaluate. Must supply
                      ``compute_loss_from_forecast``, i.e. carry the deterministic
                      objective, since validation and testing score a single
                      prediction through it.
   :type forecaster: BaseDeterministicForecaster
   :param config: Configuration object for the neural LAM model.
   :type config: NeuralLAMConfig
   :param datastore: Datastore providing grid metadata and data access.
   :type datastore: BaseDatastore
   :param datastore_boundary: Boundary forcing datastore, used for its standardization
                              statistics.
   :type datastore_boundary: BaseDatastore, optional
   :param lr: Learning rate for the optimizer.
   :type lr: float, default 1e-3
   :param restore_opt: Whether to restore optimizer state from checkpoint.
   :type restore_opt: bool, default False
   :param n_example_pred: Number of example predictions to plot during testing.
   :type n_example_pred: int, default 1
   :param create_gif: Whether to create GIFs of example predictions.
   :type create_gif: bool, default False
   :param val_steps_to_log: Specific predicted steps to log during validation/testing.
   :type val_steps_to_log: list of int, optional
   :param train_steps_to_log: Specific predicted steps to log during training, reported as
                              ``train_loss_unroll{i}`` (see ``training_step``).
   :type train_steps_to_log: list of int, optional
   :param metrics_watch: List of metrics to watch and log specifically.
   :type metrics_watch: list of str, optional
   :param var_leads_metrics_watch: Mapping from variable index to a list of predicted steps to log
                                   individually for the configured metrics.
   :type var_leads_metrics_watch: dict of {int: list of int}, optional
   :param args: Pre-refactor ``ARModel`` checkpoint hyperparameters; see
                ``BaseForecastingModule.__init__``.
   :type args: argparse.Namespace, optional


   .. py:method:: on_test_epoch_end() -> None

      Perform actions at the end of the test epoch.
      Aggregates and plots test metrics and spatial loss maps.



   .. py:method:: test_step(batch: neural_lam.models.modules.base.Batch, batch_idx: int) -> None

      Perform a single test step.

      :param batch: The batch of data.
      :type batch: tuple
      :param batch_idx: The index of the batch.
      :type batch_idx: int



   .. py:method:: training_step(batch: neural_lam.models.modules.base.Batch) -> torch.Tensor

      Perform a single training step, logging the per-step breakdown.

      Overrides ``BaseForecastingModule.training_step``, which logs only the
      scalar objective because a forecaster's training loss need not
      decompose over predicted steps. The deterministic objective does, so
      ``--train_steps_to_log`` can report individual steps as
      ``train_loss_unroll{i}`` and the logged ``train_loss`` is their mean,
      equal to ``forecaster.compute_training_loss``.

      :param batch: The batch of data.
      :type batch: tuple

      :returns: The computed loss for the training step.
      :rtype: torch.Tensor



   .. py:method:: validation_step(batch: neural_lam.models.modules.base.Batch, batch_idx: int) -> None

      Perform a single validation step.

      :param batch: The batch of data.
      :type batch: tuple
      :param batch_idx: The index of the batch.
      :type batch_idx: int



   .. py:attribute:: forecaster
      :type:  neural_lam.models.forecasters.deterministic.BaseDeterministicForecaster


   .. py:attribute:: spatial_loss_maps
      :type:  list[torch.Tensor]
      :value: []



   .. py:attribute:: test_metrics


   .. py:attribute:: val_metrics


