Skip to content

Commit

Permalink
Validate observation in judge() instead of __init__()
Browse files Browse the repository at this point in the history
  • Loading branch information
JustasB committed Sep 20, 2018
1 parent 9431a37 commit f8ab784
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions sciunit/tests.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ def __init__(self, observation, name=None, **params):
self.verbose = params.pop('verbose', 1)
self.params.update(params)

validated = self.validate_observation(observation)
self.observation = validated if validated is not None else observation
self.observation = observation

if self.score_type is None or not issubclass(self.score_type, Score):
raise Error(("The score type '%s' specified for Test '%s' "
Expand Down Expand Up @@ -71,6 +70,8 @@ def validate_observation(self, observation):
raise ObservationError("Observation is missing.")
if not isinstance(observation, dict):
raise ObservationError("Observation is not a dictionary.")
if "mean" in observation and observation["mean"] is None:
raise ObservationError("Observation mean cannot be 'None'.")
if self.observation_schema:
if isinstance(self.observation_schema, list):
schema = {'oneof_schema': self.observation_schema,
Expand Down Expand Up @@ -171,19 +172,28 @@ def _judge(self, model, skip_incapable=True):
"""Generate a score for the model (internal API use only)."""
# 1.
self.check_capabilities(model, skip_incapable=skip_incapable)

# 2.
prediction = self.generate_prediction(model)
self.check_prediction(prediction)
self.last_model = model
# 3.
observation = self.observation
score = self.compute_score(observation, prediction)

# 3. Validate observation and compute score
validated = self.validate_observation(self.observation)

if validated is not None:
self.observation = validated

score = self.compute_score(self.observation, prediction)

if self.converter:
score = self.converter.convert(score)

# 4.
self.check_score_type(score)

# 5.
self._bind_score(score, model, observation, prediction)
self._bind_score(score, model, self.observation, prediction)

return score

Expand Down

0 comments on commit f8ab784

Please sign in to comment.