How to use the sciunit.base.SciUnit function in sciunit

To help you get started, we’ve selected a few sciunit examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github scidash / sciunit / sciunit / unit_test / base_tests.py View on Github external
def test_SciUnit(self):
        from sciunit.base import SciUnit
        sciunitObj = SciUnit()
        self.assertIsInstance(sciunitObj.properties, dict)
        self.assertIsInstance(sciunitObj.dict_hash({'1':1, '2':2}), str)
        self.assertIsInstance(sciunitObj._id, int)
        self.assertIsInstance(sciunitObj.id, str)
        sciunitObj.json(string=False)
        self.assertIsInstance(sciunitObj._class, dict)
        sciunitObj.testState = "testState"
        print(sciunitObj._state(keys=["testState"]))
        sciunitObj.unpicklable.append("testState")
        
        self.assertFalse("testState" in sciunitObj.__getstate__())
github scidash / sciunit / sciunit / unit_test / base_tests.py View on Github external
def test_SciUnitEncoder(self):
        from sciunit.base import SciUnitEncoder, SciUnit
        encoderObj = SciUnitEncoder()
        

        d = {'col1': [1, 2], 'col2': [3, 4]}
        df = pd.DataFrame(data=d)
        self.assertIsInstance(encoderObj.default(df), dict)

        npArr = np.ndarray(shape=(2,2), dtype=int)
        self.assertIsInstance(encoderObj.default(npArr), list)

        sciunitObj = SciUnit()
        sciunitObj.testState = "testState"
        self.assertIsInstance(encoderObj.default(sciunitObj), dict)

        self.assertRaises(TypeError, encoderObj.default, "This is a string")
        
        class MyClass:
            pass
        self.assertIsInstance(encoderObj.default(MyClass()), str)
github scidash / sciunit / sciunit / unit_test / utils_tests.py View on Github external
def test_dict_hash(self):
        from sciunit.base import SciUnit

        d1 = {'a': 1, 'b': 2, 'c': 3}
        d2 = {'c': 3, 'a': 1, 'b': 2}
        dh1 = SciUnit.dict_hash(d1)
        dh2 = SciUnit.dict_hash(d2)
        self.assertTrue(type(dh1) is str)
        self.assertTrue(type(dh2) is str)
        self.assertEqual(d1, d2)
github scidash / sciunit / sciunit / unit_test / utils_tests.py View on Github external
def test_dict_hash(self):
        from sciunit.base import SciUnit

        d1 = {'a': 1, 'b': 2, 'c': 3}
        d2 = {'c': 3, 'a': 1, 'b': 2}
        dh1 = SciUnit.dict_hash(d1)
        dh2 = SciUnit.dict_hash(d2)
        self.assertTrue(type(dh1) is str)
        self.assertTrue(type(dh2) is str)
        self.assertEqual(d1, d2)
github scidash / sciunit / sciunit / models / base.py View on Github external
"""Base class for SciUnit models."""

import inspect
from fnmatch import fnmatchcase

from sciunit.base import SciUnit
from sciunit.capabilities import Capability


class Model(SciUnit):
    """Abstract base class for sciunit models."""

    def __init__(self, name=None, **params):
        """Instantiate model."""
        if name is None:
            name = self.__class__.__name__
        self.name = name
        self.params = params
        if params is None:
            params = {}
        super(Model, self).__init__()
        self.check_params()

    name = None
    """The name of the model. Defaults to the class name."""
github scidash / sciunit / sciunit / suites.py View on Github external
"""
Base class for SciUnit test suites.
"""

import random
from types import MethodType

from .base import SciUnit, TestWeighted
from .utils import log
from .tests import Test
from .models import Model
from .scores import NoneScore
from .scores.collections import ScoreMatrix


class TestSuite(SciUnit, TestWeighted):
    """A collection of tests."""

    def __init__(self, tests, name=None, weights=None, include_models=None,
                 skip_models=None, hooks=None, optimizer=None):
        """
        optimizer: a function to bind to self.optimize (first argument must be a testsuite)
        """
        self.name = name if name else "Suite_%d" % random.randint(0, 1e12)
        if isinstance(tests, dict):
            for key, value in tests.items():
                if not isinstance(value, Test):
                    setattr(self, key, value)
            tests = [test for test in tests.values() if isinstance(test, Test)]
        self.tests = self.assert_tests(tests)
        self.weights_ = [] if not weights else list(weights)
        self.include_models = include_models if include_models else []
github scidash / sciunit / sciunit / scores / collections.py View on Github external
Uses the `norm_score` attribute, since otherwise direct comparison
        across different kinds of scores would not be possible.
        """

        return np.dot(np.array(self.norm_scores), self.weights)

    def stature(self, test_or_model):
        """Compute the relative rank of a model on a test.

        Rank is against other models that were asked to take the test.
        """
        return self.norm_scores.rank(ascending=False)[test_or_model]


class ScoreMatrix(pd.DataFrame, SciUnit, TestWeighted):
    """
    Represents a matrix of scores derived from a test suite.
    Extends the pandas DataFrame such that tests are columns and models
    are the index.
    Also displays and compute score summaries in sciunit-specific ways.

    Can use like this, assuming n tests and m models:

    >>> sm[test]

    >>> sm[test]
    (score_1, ..., score_m)
    >>> sm[model]
    (score_1, ..., score_n)
    """
github scidash / sciunit / sciunit / utils.py View on Github external
def dict_hash(d):
    return SciUnit.dict_hash(d)
github scidash / sciunit / sciunit / scores / base.py View on Github external
"""Base class for SciUnit scores."""

from copy import copy

import numpy as np

from sciunit.base import SciUnit
from sciunit.utils import log, config_get
from sciunit.errors import InvalidScoreError


class Score(SciUnit):
    """Abstract base class for scores."""

    def __init__(self, score, related_data=None):
        """Abstract base class for scores.

        Args:
            score (int, float, bool): A raw value to wrap in a Score class.
            related_data (dict, optional): Artifacts to store with the score.
        """
        self.check_score(score)
        if related_data is None:
            related_data = {}
        self.score, self.related_data = score, related_data
        if isinstance(score, Exception):
            # Set to error score to use its summarize().
            self.__class__ = ErrorScore
github scidash / sciunit / sciunit / scores / collections.py View on Github external
from datetime import datetime
import warnings

import numpy as np
import pandas as pd
import bs4
from IPython.display import display, Javascript

from sciunit.base import SciUnit, TestWeighted
from sciunit.models import Model
from sciunit.tests import Test
from sciunit.scores import Score, NoneScore


class ScoreArray(pd.Series, SciUnit,TestWeighted):
    """Represents an array of scores derived from a test suite.

    Extends the pandas Series such that items are either
    models subject to a test or tests taken by a model.
    Also displays and compute score summaries in sciunit-specific ways.

    Can use like this, assuming n tests and m models:

    >>> sm[test]

    >>> sm[test]
    (score_1, ..., score_m)
    >>> sm[model]
    (score_1, ..., score_n)
    """