示例#1
0
def valid_opener():
    script = """
import json
from substratools import Opener

class FakeOpener(Opener):
    def get_X(self, folder):
        return 'X'

    def get_y(self, folder):
        return list(range(0, 3))

    def fake_X(self):
        return 'Xfake'

    def fake_y(self):
        return [0] * 3

    def get_pred(self, path):
        with open(path, 'r') as f:
            return json.load(f)

    def save_pred(self, pred, path):
        with open(path, 'w') as f:
            json.dump(pred, f)
"""
    import_module('opener', script)
    yield
    del sys.modules['opener']
示例#2
0
def dummy_opener():
    script = """
import json
from substratools import Opener

class DummyOpener(Opener):
    def get_X(self, folder):
        return None

    def get_y(self, folder):
        return None

    def fake_X(self):
        raise NotImplementedError

    def fake_y(self):
        raise NotImplementedError

    def get_pred(self, path):
        with open(path, 'r') as f:
            return json.load(f)

    def save_pred(self, pred, path):
        with open(path, 'w') as f:
            json.dump(pred, f)
"""
    import_module('opener', script)
示例#3
0
def test_opener_check_folders(tmp_cwd):
    script = """
from substratools import Opener
class MyOpener(Opener):
    def get_X(self, folders):
        assert len(folders) == 5
        return 'Xclass'
    def get_y(self, folders):
        return 'yclass'
    def fake_X(self):
        return 'fakeX'
    def fake_y(self):
        return 'fakey'
    def get_pred(self, path):
        return 'pred'
    def save_pred(self, y_pred, path):
        return 'pred'
"""

    import_module('opener', script)

    o = load_from_module()

    # create some data folders
    data_root_path = o._workspace.data_folder
    data_paths = [os.path.join(data_root_path, str(i)) for i in range(5)]
    [os.makedirs(p) for p in data_paths]

    o.data_folders = data_paths
    assert o.get_X() == 'Xclass'
def test_predictions_check(tmp_cwd, save_predictions_method_body):
    script = f"""
import json
from substratools import Opener

class MyOpener(Opener):
    def get_X(self, folder):
        return 'X'

    def get_y(self, folder):
        return list(range(0, 3))

    def fake_X(self, n_samples):
        return 'Xfake'

    def fake_y(self, n_samples):
        return [0] * 3

    def get_predictions(self, path):
        with open(path, 'r') as f:
            return json.load(f)

    def save_predictions(self, pred, path):
        {save_predictions_method_body}
"""
    import_module('opener', script)

    o = load_from_module()

    with pytest.raises(exceptions.MissingFileError):
        o.save_predictions({'foo': 'bar'})
示例#5
0
def test_invalid_interface():
    code = """
def score():
    pass
"""
    import_module('score', code)
    with pytest.raises(exceptions.InvalidInterface):
        load_interface_from_module('score', interface_class=Metrics)
示例#6
0
def load_metrics_module():
    code = """
from substratools import Metrics
class DummyMetrics(Metrics):
    def score(self, y_true, y_pred):
        return sum(y_true) + sum(y_pred)
"""
    import_module('metrics', code)
    yield
    del sys.modules['metrics']
示例#7
0
def test_load_invalid_opener(tmp_cwd):
    invalid_script = """
def get_X():
    raise NotImplementedError
def get_y():
    raise NotImplementedError
"""

    import_module('opener', invalid_script)

    with pytest.raises(exceptions.InvalidInterface):
        load_from_module()
示例#8
0
def test_load_opener_as_class(tmp_cwd):
    script = """
from substratools import Opener
class MyOpener(Opener):
    def get_X(self, folders):
        return 'Xclass'
    def get_y(self, folders):
        return 'yclass'
    def fake_X(self):
        return 'fakeX'
    def fake_y(self):
        return 'fakey'
    def get_pred(self, path):
        return 'pred'
    def save_pred(self, y_pred, path):
        return 'pred'
"""

    import_module('opener', script)

    o = load_from_module()
    assert o.get_X() == 'Xclass'
示例#9
0
def test_load_opener_as_module(tmp_cwd):
    script = """
def _helper():
    pass
def get_X(folders):
    return 'X'
def get_y(folders):
    return 'y'
def fake_X():
    return 'fakeX'
def fake_y():
    return 'fakey'
def get_pred(path):
    return 'pred'
def save_pred(y_pred, path):
    return 'pred'
"""

    import_module('opener', script)

    o = load_from_module()
    assert o.get_X() == 'X'
示例#10
0
def valid_opener(valid_opener_code):
    import_module('opener', valid_opener_code)
    yield
    del sys.modules['opener']