Source code for sciunit.models.examples
"""Example SciUnit model classes."""
import random
from cypy import memoize # Decorator for caching of capability method results.
from sciunit.models import Model
from sciunit.capabilities import ProducesNumber
from sciunit.utils import class_intern, method_cache
[docs]class ConstModel(Model, ProducesNumber):
"""A model that always produces a constant number as output."""
[docs] def __init__(self, constant, name=None):
self.constant = constant
super(ConstModel, self).__init__(name=name, constant=constant)
[docs] def produce_number(self):
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):
"""Each call to this method will produce a different random number."""
return random.random()
[docs]class RepeatedRandomNumberModel(Model, ProducesNumber):
"""An example model to demonstrate ProducesNumber with cypy.lazy."""
[docs] @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."""
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."""
pass