exceptions
exceptions
Custom exceptions and warnings for spotforecast2.
This module contains all the custom warnings and error classes used across spotforecast2.
Examples
Using custom warnings::
import warnings
from spotforecast2_safe.exceptions import MissingValuesWarning
# Raise a warning
warnings.warn(
"Missing values detected in input data.",
MissingValuesWarning
)
# Suppress a specific warning
warnings.simplefilter('ignore', category=MissingValuesWarning)
Classes
| Name | Description |
|---|---|
| DataTransformationWarning | Warning for output data in transformed space. |
| DataTypeWarning | Warning for incompatible data types in exogenous data. |
| ExogenousInterpretationWarning | Warning about implications when using exogenous variables. |
| FeatureOutOfRangeWarning | Warning for features out of training range. |
| IgnoredArgumentWarning | Warning for ignored arguments. |
| InputTypeWarning | Warning for inefficient input format. |
| LongTrainingWarning | Warning for potentially long training processes. |
| MissingExogWarning | Warning for missing exogenous variables. |
| MissingValuesWarning | Warning for missing values in data. |
| NotFittedError | Exception class to raise if estimator is used before fitting. |
| OneStepAheadValidationWarning | Warning for one-step-ahead validation usage. |
| ResidualsUsageWarning | Warning for incorrect residuals usage. |
| SaveLoadSkforecastWarning | Warning for save/load operations. |
| SpotforecastVersionWarning | Warning for version mismatch. |
| UnknownLevelWarning | Warning for unknown levels in prediction. |
DataTransformationWarning
exceptions.DataTransformationWarning(message)Warning for output data in transformed space.
Used to notify that the output data is in the transformed space.
Examples
>>> import warnings
>>> warnings.warn(
... "Output is in transformed space.",
... DataTransformationWarning
... )DataTypeWarning
exceptions.DataTypeWarning(message)Warning for incompatible data types in exogenous data.
Used to notify there are dtypes in the exogenous data that are not ‘int’, ‘float’, ‘bool’ or ‘category’. Most machine learning models do not accept other data types, therefore the forecaster fit and predict may fail.
Examples
>>> import warnings
>>> warnings.warn(
... "Exogenous data contains unsupported dtypes.",
... DataTypeWarning
... )ExogenousInterpretationWarning
exceptions.ExogenousInterpretationWarning(message)Warning about implications when using exogenous variables.
Used to notify about important implications when using exogenous variables with models that use a two-step approach (e.g., regression + ARAR).
Examples
>>> import warnings
>>> warnings.warn(
... "Exogenous variables may not be used as expected.",
... ExogenousInterpretationWarning
... )FeatureOutOfRangeWarning
exceptions.FeatureOutOfRangeWarning(message)Warning for features out of training range.
Used to notify that a feature is out of the range seen during training.
Examples
>>> import warnings
>>> warnings.warn(
... "Feature value exceeds training range.",
... FeatureOutOfRangeWarning
... )IgnoredArgumentWarning
exceptions.IgnoredArgumentWarning(message)Warning for ignored arguments.
Used to notify that an argument is ignored when using a method or a function.
Examples
>>> import warnings
>>> warnings.warn(
... "Argument 'x' is ignored in this context.",
... IgnoredArgumentWarning
... )InputTypeWarning
exceptions.InputTypeWarning(message)Warning for inefficient input format.
Used to notify that input format is not the most efficient or recommended for the forecaster.
Examples
>>> import warnings
>>> warnings.warn(
... "Input format is not optimal for this forecaster.",
... InputTypeWarning
... )LongTrainingWarning
exceptions.LongTrainingWarning(message)Warning for potentially long training processes.
Used to notify that a large number of models will be trained and the the process may take a while to run.
Examples
>>> import warnings
>>> warnings.warn(
... "Training may take a long time.",
... LongTrainingWarning
... )MissingExogWarning
exceptions.MissingExogWarning(message)Warning for missing exogenous variables.
Used to indicate that there are missing exogenous variables in the data. Most machine learning models do not accept missing values, so the Forecaster’s fit' andpredict’ methods may fail.
Examples
>>> import warnings
>>> warnings.warn(
... "Missing exogenous variables detected.",
... MissingExogWarning
... )MissingValuesWarning
exceptions.MissingValuesWarning(message)Warning for missing values in data.
Used to indicate that there are missing values in the data. This warning occurs when the input data contains missing values, or the training matrix generates missing values. Most machine learning models do not accept missing values, so the Forecaster’s fit' andpredict’ methods may fail.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| message | str | The message to display. | required |
Examples
>>> import warnings
>>> from spotforecast2_safe.exceptions import MissingValuesWarning
>>> warnings.warn(
... "Missing values detected in input data.",
... MissingValuesWarning
... )NotFittedError
exceptions.NotFittedError()Exception class to raise if estimator is used before fitting.
This class inherits from both ValueError and AttributeError to help with exception handling and backward compatibility.
Examples
>>> from spotforecast2_safe.exceptions import NotFittedError
>>> try:
... raise NotFittedError("Forecaster not fitted")
... except NotFittedError as e:
... print(e)
Forecaster not fittedOneStepAheadValidationWarning
exceptions.OneStepAheadValidationWarning(message)Warning for one-step-ahead validation usage.
Used to notify that the one-step-ahead validation is being used.
Examples
>>> import warnings
>>> warnings.warn(
... "Using one-step-ahead validation.",
... OneStepAheadValidationWarning
... )ResidualsUsageWarning
exceptions.ResidualsUsageWarning(message)Warning for incorrect residuals usage.
Used to notify that a residual are not correctly used in the probabilistic forecasting process.
Examples
>>> import warnings
>>> warnings.warn(
... "Residuals are not properly used.",
... ResidualsUsageWarning
... )SaveLoadSkforecastWarning
exceptions.SaveLoadSkforecastWarning(message)Warning for save/load operations.
Used to notify any issues that may arise when saving or loading a forecaster.
Examples
>>> import warnings
>>> warnings.warn(
... "Issues detected when saving forecaster.",
... SaveLoadSkforecastWarning
... )SpotforecastVersionWarning
exceptions.SpotforecastVersionWarning(message)Warning for version mismatch.
Used to notify that the version installed in the environment differs from the version used to initialize the forecaster.
Examples
>>> import warnings
>>> warnings.warn(
... "Version mismatch detected.",
... SpotforecastVersionWarning
... )UnknownLevelWarning
exceptions.UnknownLevelWarning(message)Warning for unknown levels in prediction.
Used to notify that a level being predicted was not part of the training data.
Examples
>>> import warnings
>>> warnings.warn(
... "Predicting for an unknown level.",
... UnknownLevelWarning
... )Functions
| Name | Description |
|---|---|
| format_warning_handler | Custom warning handler to format warnings in a box. |
| rich_warning_handler | Custom handler for warnings that uses rich to display formatted panels. |
| runtime_deprecated | Decorator to mark functions or classes as deprecated. |
| set_skforecast_warnings | Suppress spotforecast warnings. |
| set_warnings_style | Set the warning handler based on the provided style. |
format_warning_handler
exceptions.format_warning_handler(
message,
category,
filename,
lineno,
file=None,
line=None,
)Custom warning handler to format warnings in a box.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| message | str | Warning message. | required |
| category | str | Warning category. | required |
| filename | str | Filename where the warning was raised. | required |
| lineno | str | Line number where the warning was raised. | required |
| file | object | File where the warning was raised. | None |
| line | str | Line where the warning was raised. | None |
Returns
| Name | Type | Description |
|---|---|---|
| None | None |
Examples
>>> # This is used internally by the warnings module
>>> set_warnings_style('skforecast')rich_warning_handler
exceptions.rich_warning_handler(
message,
category,
filename,
lineno,
file=None,
line=None,
)Custom handler for warnings that uses rich to display formatted panels.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| message | str | Warning message. | required |
| category | str | Warning category. | required |
| filename | str | Filename where the warning was raised. | required |
| lineno | str | Line number where the warning was raised. | required |
| file | object | File where the warning was raised. | None |
| line | str | Line where the warning was raised. | None |
Returns
| Name | Type | Description |
|---|---|---|
| None | None |
Examples
>>> # This is used internally when rich is available
>>> set_warnings_style('skforecast')runtime_deprecated
exceptions.runtime_deprecated(
replacement=None,
version=None,
removal=None,
category=FutureWarning,
)Decorator to mark functions or classes as deprecated.
Works for both function and class targets, and ensures warnings are visible even inside Jupyter notebooks.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| replacement | str | Name of the replacement function/class to use instead. | None |
| version | str | Version in which the function/class was deprecated. | None |
| removal | str | Version in which the function/class will be removed. | None |
| category | type[Warning] | Warning category to use. Default is FutureWarning. | FutureWarning |
Returns
| Name | Type | Description |
|---|---|---|
| object | Decorator function. |
Examples
>>> @runtime_deprecated(replacement='new_function', version='0.5', removal='1.0')
... def old_function():
... pass
>>> old_function()
FutureWarning: old_function() is deprecated since version 0.5; use new_function instead...set_skforecast_warnings
exceptions.set_skforecast_warnings(suppress_warnings, action='ignore')Suppress spotforecast warnings.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| suppress_warnings | bool | bool If True, spotforecast warnings will be suppressed. | required |
| action | str | str, default ‘ignore’ Action to take regarding the warnings. | 'ignore' |
set_warnings_style
exceptions.set_warnings_style(style='skforecast')Set the warning handler based on the provided style.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| style | str | The style of the warning handler. Either ‘skforecast’ or ‘default’. | 'skforecast' |
Returns
| Name | Type | Description |
|---|---|---|
| None | None |
Examples
>>> set_warnings_style('skforecast')
>>> # Now warnings will be displayed with formatting
>>> set_warnings_style('default')
>>> # Back to default Python warning format