Skip to content

Commit

Permalink
Merge pull request #58 from scidash/master
Browse files Browse the repository at this point in the history
Add master commits to shailesh branch
  • Loading branch information
rgerkin committed Nov 3, 2017
2 parents 20c7b07 + efb34d0 commit 01cf6ac
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 16 deletions.
46 changes: 32 additions & 14 deletions sciunit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,37 @@ def __getstate__(self):
del state[key]
return state

def _state(self, state=None, keys=None, exclude=None):
if state is None:
state = self.__getstate__()
if keys:
state = {key:state[key] for key in keys if key in state.keys()}
if exclude:
state = {key:state[key] for key in state.keys() if key not in exclude}
return state

@property
def state(self):
return self.__getstate__()
return self._state()

@property
def hash(self):
"""A unique numeric identifier of the current model state"""
state = self.state
result = dict_hash(state)
return result
return dict_hash(self.state)

def serialize(self):
state = self.state
result = json.dumps(state)
return result
@property
def json(self):
def serialize(obj):
try:
s = json.dumps(obj)
except:
s = json.dumps(obj.state, default=serialize)
return json.loads(s)
return serialize(self)

@property
def id(self):
return str(self.json)


class Model(SciUnit):
Expand Down Expand Up @@ -393,6 +409,10 @@ def describe(self):
result = '\n'.join(s)
return result

@property
def state(self):
return self._state(exclude=['last_model'])

def __str__(self):
return '%s' % self.name

Expand Down Expand Up @@ -964,7 +984,7 @@ def __str__(self):
return 'N/A'


class ScoreArray(pd.Series):
class ScoreArray(pd.Series,SciUnit):
"""
Represents an array of scores derived from a test suite.
Extends the pandas Series such that items are either
Expand Down Expand Up @@ -1032,7 +1052,7 @@ def stature(self, test_or_model):
# return self


class ScoreMatrix(pd.DataFrame):
class ScoreMatrix(pd.DataFrame,SciUnit):
"""
Represents a matrix of scores derived from a test suite.
Extends the pandas DataFrame such that tests are columns and models
Expand Down Expand Up @@ -1165,7 +1185,6 @@ def to_html(self, show_mean=None, sortable=None, colorize=True, *args,
# html = self.to_html(*args, **kwargs)
# return HTML(html)


class ScoreArrayM2M(pd.Series):
"""
Represents an array of scores derived from TestM2M.
Expand Down Expand Up @@ -1245,8 +1264,7 @@ def __getattr__(self, name):
def sort_keys(self):
return self.applymap(lambda x: x.sort_key)


class ScorePanel(pd.Panel):
class ScorePanel(pd.Panel,SciUnit):
def __getitem__(self, item):
df = super(ScorePanel,self).__getitem__(item)
assert isinstance(df,pd.DataFrame), \
Expand All @@ -1255,7 +1273,7 @@ def __getitem__(self, item):
return score_matrix


class Error(Exception):
class Error(Exception,SciUnit):
"""Base class for errors in sciunit's core."""
pass

Expand Down
10 changes: 10 additions & 0 deletions sciunit/unit_test/core_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,16 @@ def test_import_module_from_path(self):
module = import_module_from_path(temp_file)
self.assertEqual(module.value,42)

def test_versioned(self):
from sciunit.utils import Versioned
from sciunit.models import ConstModel
class VersionedModel(ConstModel,Versioned):
pass
m = VersionedModel(37)
print("Commit hash is %s" % m.version)
print("Remote URL is %s" % m.remote_url)
self.assertTrue('sciunit' in m.remote_url)


class CommandLineTestCase(unittest.TestCase):
"""Unit tests for command line tools"""
Expand Down
33 changes: 33 additions & 0 deletions sciunit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from quantities.dimensionality import Dimensionality
from quantities.quantity import Quantity
import cypy
import git

PRINT_DEBUG_STATE = False # printd does nothing by default.

Expand Down Expand Up @@ -293,3 +294,35 @@ def decorate(*args, **kwargs):

method_memoize = cypy.memoize


class Versioned(object):
"""
A Mixin class for SciUnit model instances, which provides a version string
based on the Git repository where the model is tracked.
Provided by Andrew Davison in issue #53.
"""

def get_repo(self):
module = sys.modules[self.__module__]
# We use module.__file__ instead of module.__path__[0]
# to include modules without a __path__ attribute.
path = os.path.realpath(module.__file__)
repo = git.Repo(path, search_parent_directories=True)
return repo

def get_version(self):
repo = self.get_repo()
head = repo.head
version = head.commit.hexsha
if repo.is_dirty():
version += "*"
return version
version = property(get_version)

def get_remote_url(self, remote='origin'):
repo = self.get_repo()
remotes = {r.name:r for r in repo.remotes}
r = repo.remotes[0] if remote not in remotes else remotes[remote]
url = list(r.urls)[0]
return url
remote_url = property(get_remote_url)
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
version='0.19',
author='Rick Gerkin',
author_email='rgerkin@asu.edu',
packages=['sciunit',
packages=['sciunit',
'sciunit.tests',
'sciunit.unit_test'],
url='http://sciunit.scidash.org',
Expand All @@ -34,7 +34,8 @@
'lxml',
'nbconvert',
'ipykernel',
'nbformat',],
'nbformat',
'gitpython'],
#dependency_links = ['git+https://github.com/python-quantities/python-quantities.git@master#egg=quantities-999'],
entry_points={
'console_scripts': [
Expand Down

0 comments on commit 01cf6ac

Please sign in to comment.