Skip to content

Commit

Permalink
Style changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rgerkin committed Jun 17, 2019
1 parent 95661d7 commit 4b1915a
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 61 deletions.
10 changes: 8 additions & 2 deletions sciunit/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,15 +578,21 @@ def compute_score(self, observation, prediction):

class ProtocolToFeaturesTest(Test):
"""Assume that generating a prediction consists of:
1) Setting up a simulation experiment protocol,
1) Setting up a simulation experiment protocol.
Depending on the backend, this could include editing simulation parameters
in memory or editing a model file. It could include any kind of
experimental protocol, such as a perturbation.
2) Running a model (using e.g. RunnableModel)
3) Extract features from the results
Developers should not need to manually implement `generate_prediction`, and
instead should focus on the other three methods here.
"""

def generate_prediction(self, model):
run_method = getattr(model, "run", None)
assert callable(run_method), \
"Model must have a `run` method to use a ProtocolToFeatureTest"
"Model must have a `run` method to use a ProtocolToFeaturesTest"
self.setup_protocol(model)
result = self.get_result(model)
prediction = self.extract_features(model, result)
Expand Down
13 changes: 7 additions & 6 deletions sciunit/unit_test/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from sciunit.tests import RangeTest
from sciunit.models.examples import UniformModel


class SuiteBase(object):
"""Abstract base class for testing suites and scores"""

Expand All @@ -12,9 +13,9 @@ def setUp(self):
self.T = RangeTest

def prep_models_and_tests(self):
t1 = self.T([2,3],name='test1')
t2 = self.T([5,6])
m1 = self.M(2,3)
m2 = self.M(5,6)
ts = TestSuite([t1,t2],name="MySuite")
return (ts,t1,t2,m1,m2)
t1 = self.T([2, 3], name='test1')
t2 = self.T([5, 6])
m1 = self.M(2, 3)
m2 = self.M(5, 6)
ts = TestSuite([t1, t2], name="MySuite")
return (ts, t1, t2, m1, m2)
4 changes: 4 additions & 0 deletions sciunit/unit_test/command_line_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@ def test_sciunit_4make_nb(self):
"run-nb not supported on Python < 3.3")
def test_sciunit_5run_nb(self):
self.main('--directory', self.cosmosuite_path, 'run-nb')


if __name__ == '__main__':
test_program = unittest.main(verbosity=0, buffer=True, exit=False)
9 changes: 4 additions & 5 deletions sciunit/unit_test/import_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@

import unittest

from .base import *

class ImportTestCase(unittest.TestCase):
"""Unit tests for imports"""

def test_quantities(self):
import quantities as pq
pq.Quantity([10,20,30], pq.pA)
pq.Quantity([10, 20, 30], pq.pA)

def test_import_everything(self):
import sciunit
from sciunit.utils import import_all_modules

# Recursively import all submodules
import_all_modules(sciunit)


if __name__ == '__main__':
unittest.main()
unittest.main()
90 changes: 48 additions & 42 deletions sciunit/unit_test/score_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,84 +5,90 @@
from IPython.display import display
import numpy as np

from sciunit import TestSuite, ScoreMatrix, ScoreArray, ScorePanel
from sciunit.scores import ZScore,CohenDScore,PercentScore,BooleanScore,FloatScore,RatioScore
from sciunit.scores import ErrorScore,NAScore,TBDScore,NoneScore, InsufficientDataScore
from sciunit import ScoreMatrix, ScoreArray, ScorePanel
from sciunit.scores import ZScore, CohenDScore, PercentScore, BooleanScore,\
FloatScore, RatioScore
from sciunit.scores import ErrorScore, NAScore, TBDScore, NoneScore,\
InsufficientDataScore
from sciunit.tests import RangeTest

from .base import SuiteBase
from sciunit.unit_test.base import SuiteBase

class ScoresTestCase(SuiteBase,unittest.TestCase):

class ScoresTestCase(SuiteBase, unittest.TestCase):
def test_score_matrix(self):
t,t1,t2,m1,m2 = self.prep_models_and_tests()
t, t1, t2, m1, m2 = self.prep_models_and_tests()
sm = t.judge(m1)
self.assertTrue(type(sm) is ScoreMatrix)
self.assertTrue(sm[t1][m1].score)
self.assertTrue(sm['test1'][m1].score)
self.assertTrue(sm[m1]['test1'].score)
self.assertFalse(sm[t2][m1].score)
self.assertEqual(sm[(m1,t1)].score,True)
self.assertEqual(sm[(m1,t2)].score,False)
sm = t.judge([m1,m2])
self.assertEqual(sm.stature(t1,m1),1)
self.assertEqual(sm.stature(t1,m2),2)
self.assertFalse(sm[t2][m1].score)
self.assertEqual(sm[(m1, t1)].score, True)
self.assertEqual(sm[(m1, t2)].score, False)
sm = t.judge([m1, m2])
self.assertEqual(sm.stature(t1, m1), 1)
self.assertEqual(sm.stature(t1, m2), 2)
display(sm)

def test_score_arrays(self):
t,t1,t2,m1,m2 = self.prep_models_and_tests()
t, t1, t2, m1, m2 = self.prep_models_and_tests()
sm = t.judge(m1)
sa = sm[m1]
self.assertTrue(type(sa) is ScoreArray)
self.assertEqual(list(sa.norm_scores.values),[1.0,0.0])
self.assertEqual(sa.stature(t1),1)
self.assertEqual(sa.stature(t2),2)
self.assertEqual(sa.stature(t1),1)
self.assertEqual(list(sa.norm_scores.values), [1.0, 0.0])
self.assertEqual(sa.stature(t1), 1)
self.assertEqual(sa.stature(t2), 2)
self.assertEqual(sa.stature(t1), 1)
display(sa)

@unittest.skip("Currently failing because ScorePanel just stores sm1 twice")
@unittest.skip(("Currently failing because ScorePanel "
"just stores sm1 twice"))
def test_score_panel(self):
t,t1,t2,m1,m2 = self.prep_models_and_tests()
sm1 = t.judge([m1,m2])
sm2 = t.judge([m2,m1])
sp = ScorePanel(data={1:sm1,2:sm2})
t, t1, t2, m1, m2 = self.prep_models_and_tests()
sm1 = t.judge([m1, m2])
sm2 = t.judge([m2, m1])
sp = ScorePanel(data={1: sm1, 2: sm2})
self.assertTrue(sp[1].equals(sm1))
self.assertTrue(sp[2].equals(sm2))
self.assertTrue(sp[2].equals(sm2))

def test_regular_score_types_1(self):
score = PercentScore(42)
self.assertEqual(score.norm_score,0.42)
self.assertEqual(score.norm_score, 0.42)

ZScore(0.7)
score = ZScore.compute({'mean':3.,'std':1.},{'value':2.})
self.assertEqual(score.score,-1.)
score = ZScore.compute({'mean': 3., 'std': 1.},
{'value': 2.})
self.assertEqual(score.score, -1.)

CohenDScore(-0.3)
score = CohenDScore.compute({'mean':3.,'std':1.},{'mean':2.,'std':1.})
score = CohenDScore.compute({'mean': 3., 'std': 1.},
{'mean': 2., 'std': 1.})
self.assertTrue(-0.708 < score.score < -0.707)

def test_regular_score_types_2(self):
BooleanScore(True)
BooleanScore(False)
score = BooleanScore.compute(5,5)
self.assertEqual(score.norm_score,1)
score = BooleanScore.compute(4,5)
self.assertEqual(score.norm_score,0)
t = RangeTest([2,3])
score = BooleanScore.compute(5, 5)
self.assertEqual(score.norm_score, 1)
score = BooleanScore.compute(4, 5)
self.assertEqual(score.norm_score, 0)

t = RangeTest([2, 3])
score.test = t
score.describe()
score.description = "Lorem Ipsum"
score.describe()

score = FloatScore(3.14)
obs = np.array([1.0,2.0,3.0])
pred = np.array([1.0,2.0,4.0])
score = FloatScore.compute_ssd(obs,pred)
self.assertEqual(score.score,1.0)
obs = np.array([1.0, 2.0, 3.0])
pred = np.array([1.0, 2.0, 4.0])
score = FloatScore.compute_ssd(obs, pred)
self.assertEqual(score.score, 1.0)

RatioScore(1.2)
score = RatioScore.compute({'mean':4.,'std':1.},{'value':2.})
self.assertEqual(score.score,0.5)
score = RatioScore.compute({'mean': 4., 'std': 1.}, {'value': 2.})
self.assertEqual(score.score, 0.5)

def test_irregular_score_types(self):
e = Exception("This is an error")
Expand All @@ -91,4 +97,4 @@ def test_irregular_score_types(self):
score = TBDScore(None)
score = NoneScore(None)
score = InsufficientDataScore(None)
self.assertEqual(score.norm_score,None)
self.assertEqual(score.norm_score, None)
12 changes: 6 additions & 6 deletions sciunit/unit_test/test_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from sciunit.scores import FloatScore
from sciunit.capabilities import ProducesNumber

from .base import SuiteBase
from sciunit.unit_test.base import SuiteBase

class TestsTestCase(unittest.TestCase):
"""Unit tests for the sciunit module"""
Expand All @@ -19,7 +19,7 @@ def setUp(self):
self.T = RangeTest

def test_get_test_description(self):
t = self.T([2,3])
t = self.T([2, 3])
t.describe()
t.description = "Lorem Ipsum"
t.describe()
Expand All @@ -28,17 +28,17 @@ class MyTest(self.T):
"""Lorem Ipsum"""
pass

t = MyTest([2,3])
t = MyTest([2, 3])
t.description = None
self.assertEqual(t.describe(),"Lorem Ipsum")

def test_check_model_capabilities(self):
t = self.T([2,3])
m = self.M(2,3)
t = self.T([2, 3])
m = self.M(2, 3)
t.check(m)

def test_rangetest(self):
range_2_3_test = RangeTest(observation=[2,3])
range_2_3_test = RangeTest(observation=[2, 3])
one_model = ConstModel(2.5)
self.assertTrue(range_2_3_test.check_capabilities(one_model))
score = range_2_3_test.judge(one_model)
Expand Down
7 changes: 7 additions & 0 deletions sciunit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,3 +548,10 @@ def config_get(key, default=None):
else:
raise e
return value


def path_escape(path):
"""Escape a path by placing backslashes in front of disallowed characters"""
for char in [' ', '(', ')']:
path = path.replace(char, '\%s' % char)
return path

0 comments on commit 4b1915a

Please sign in to comment.