Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# 1.
if not isinstance(models, (list, tuple, set)):
raise TypeError(("Models must be specified as a list, tuple or "
"set. For single model tests, use 'Test' class."))
else:
models = list(models)
# 2.
predictions = []
# If observation exists, store it as first element in predictions[]
if self.observation:
predictions.append(self.observation)
for model in models:
if not isinstance(model, Model):
raise TypeError(("TestM2M's judge method received a non-Model."
"Invalid model name: '%s'" % model))
else:
try:
# 3.
self.check_capabilities(model,
skip_incapable=skip_incapable)
# 4.
prediction = self.generate_prediction(model)
self.check_prediction(prediction)
predictions.append(prediction)
except CapabilityError as e:
raise CapabilityError(model, e.capability,
("TestM2M's judge method resulted in"
" error for '%s'. Error: '%s'" %
(model, str(e))))
in [a,b] as output."""
def __init__(self, a, b, name=None):
self.a, self.b = a, b
super(UniformModel, self).__init__(name=name, a=a, b=b)
def produce_number(self):
return random.uniform(self.a, self.b)
################################################################
# Here are several examples of caching and sharing can be used
# to reduce the computational load of testing.
################################################################
class UniqueRandomNumberModel(Model, ProducesNumber):
"""An example model to ProducesNumber."""
def produce_number(self):
"""Each call to this method will produce a different random number."""
return random.random()
class RepeatedRandomNumberModel(Model, ProducesNumber):
"""An example model to demonstrate ProducesNumber with cypy.lazy."""
@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()
from sciunit.capabilities import ProducesNumber
from sciunit.utils import class_intern, method_cache
class ConstModel(Model, ProducesNumber):
"""A model that always produces a constant number as output."""
def __init__(self, constant, name=None):
self.constant = constant
super(ConstModel, self).__init__(name=name, constant=constant)
def produce_number(self):
return self.constant
class UniformModel(Model, ProducesNumber):
"""A model that always produces a random uniformly distributed number
in [a,b] as output."""
def __init__(self, a, b, name=None):
self.a, self.b = a, b
super(UniformModel, self).__init__(name=name, a=a, b=b)
def produce_number(self):
return random.uniform(self.a, self.b)
################################################################
# Here are several examples of caching and sharing can be used
# to reduce the computational load of testing.
################################################################
"""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
class ConstModel(Model, ProducesNumber):
"""A model that always produces a constant number as output."""
def __init__(self, constant, name=None):
self.constant = constant
super(ConstModel, self).__init__(name=name, constant=constant)
def produce_number(self):
return self.constant
class UniformModel(Model, ProducesNumber):
"""A model that always produces a random uniformly distributed number
in [a,b] as output."""
def __init__(self, a, b, name=None):
self.a, self.b = a, b
################################################################
# Here are several examples of caching and sharing can be used
# to reduce the computational load of testing.
################################################################
class UniqueRandomNumberModel(Model, ProducesNumber):
"""An example model to ProducesNumber."""
def produce_number(self):
"""Each call to this method will produce a different random number."""
return random.random()
class RepeatedRandomNumberModel(Model, ProducesNumber):
"""An example model to demonstrate ProducesNumber with cypy.lazy."""
@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()
@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."""
def assert_models(self, models):
"""Check and in some cases fixes the list of models."""
if isinstance(models, Model):
models = (models,)
else:
try:
for model in models:
if not isinstance(model, Model):
raise TypeError(("The judge method of Test suite '%s'"
"provided an iterable containing a "
"non-Model '%s'.") % (self, model))
except TypeError:
raise TypeError(("Test suite's judge method not provided with "
"a model or iterable."))
return models
def get_group(self, x):
if isinstance(x[0], (Test, Model)) and isinstance(x[1], (Test, Model)):
return self.loc[x[0], x[1]]
elif isinstance(x[0], str):
return self.__getitem__(x[0]).__getitem__(x[1])
raise TypeError("Expected test/model pair")
return random.random()
class RepeatedRandomNumberModel(Model, ProducesNumber):
"""An example model to demonstrate ProducesNumber with cypy.lazy."""
@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()
@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
class PersistentUniformModel(UniformModel):
"""TODO"""
def run(self):
self._x = random.uniform(self.a, self.b)
def produce_number(self):
return self._x
def assert_models(self, models):
"""Check and in some cases fixes the list of models."""
if isinstance(models, Model):
models = (models,)
else:
try:
for model in models:
if not isinstance(model, Model):
raise TypeError(("The judge method of Test suite '%s'"
"provided an iterable containing a "
"non-Model '%s'.") % (self, model))
except TypeError:
raise TypeError(("Test suite's judge method not provided with "
"a model or iterable."))
return models