"""Example SciUnit model classes."""
import random
from typing import Union
from sciunit.capabilities import ProducesNumber
from sciunit.models import Model
from sciunit.utils import ( # Decorator for caching of capability method results.
class_intern,
method_cache,
method_memoize,
)
[docs]class ConstModel(Model, ProducesNumber):
"""A model that always produces a constant number as output."""
[docs] def __init__(self, constant: Union[int, float], name: str = None):
self.constant = constant
super(ConstModel, self).__init__(name=name, constant=constant)
[docs] def produce_number(self) -> Union[int, float]:
return self.constant
################################################################
# Here are several examples of caching and sharing can be used
# to reduce the computational load of testing.
################################################################
[docs]class UniqueRandomNumberModel(Model, ProducesNumber):
"""An example model to ProducesNumber."""
[docs] def produce_number(self) -> float:
"""Each call to this method will produce a different random number.
Returns:
float: A random number produced.
"""
return random.random()
[docs]class RepeatedRandomNumberModel(Model, ProducesNumber):
"""An example model to demonstrate ProducesNumber with cypy.lazy."""
[docs] @method_memoize
def produce_number(self):
"""Each call to this method will produce the same random number as was returned in the first call, ensuring reproducibility and eliminating computational overhead.
Returns:
float: A random number produced.
"""
return random.random()
[docs]@class_intern
class SharedModel(Model):
"""A model that, each time it is instantiated with the same parameters,
will return the same instance at the same locaiton in memory.
Attributes should not be set post-instantiation
unless the goal is to set those attributes on all models of this class."""