Esempio n. 1
0
def test_legacy_serialization(valid_simple_ml_predictor_data):
    """Ensure that a serialized SimplePredictor looks sane."""
    predictor = SimpleMLPredictor.build(valid_simple_ml_predictor_data)
    serialized = predictor.dump()
    serialized['id'] = valid_simple_ml_predictor_data['id']
    assert serialized == valid_serialization_output(
        valid_simple_ml_predictor_data)
Esempio n. 2
0
def test_register(valid_simple_ml_predictor_data, basic_predictor_report_data):
    session = mock.Mock()
    session.post_resource.return_value = valid_simple_ml_predictor_data
    session.get_resource.return_value = basic_predictor_report_data
    pc = PredictorCollection(uuid.uuid4(), session)
    predictor = SimpleMLPredictor.build(valid_simple_ml_predictor_data)
    registered = pc.register(predictor)
    assert registered.name == 'ML predictor'
Esempio n. 3
0
def test_failed_register(valid_simple_ml_data):
    session = mock.Mock()
    session.post_resource.side_effect = NotFound("/projects/uuid/not_found")
    pc = PredictorCollection(uuid.uuid4(), session)
    predictor = SimpleMLPredictor.build(valid_simple_ml_data)
    with pytest.raises(ModuleRegistrationFailedException) as e:
        pc.register(predictor)
    assert 'The "SimpleMLPredictor" failed to register. NotFound: /projects/uuid/not_found' in str(e.value)
Esempio n. 4
0
def simple_predictor() -> SimpleMLPredictor:
    """Build a SimpleMLPredictor for testing."""
    return SimpleMLPredictor(
        name='ML predictor',
        description='Predicts z from input x and latent variable y',
        inputs=[x],
        outputs=[z],
        latent_variables=[y],
        training_data=[data_source])
Esempio n. 5
0
def test_create_or_update_raise_error_multiple_found(predictor_collection):
    # test when there are multiple resources that exists with the same listed name and raise error
    pred = SimpleMLPredictor(
        name="resource 1",  #Not unique: two "resource 1" exists in collection
        description='I am updated!',
        inputs=[],
        outputs=[],
        latent_variables=[])
    with pytest.raises(ValueError):
        create_or_update(predictor_collection, pred)
Esempio n. 6
0
def predictor_collection() -> PredictorCollection:
    class SeedingTestPredictorCollection(PredictorCollection):
        predictors = []

        def register(self, model: SimpleMLPredictor) -> SimpleMLPredictor:
            self.predictors.append(model)
            return model

        def update(self, model):
            self.predictors = [
                r for r in self.predictors if r.uid != model.uid
            ]
            return self.register(model)

        def list(self, page: Optional[int] = None, per_page: int = 100):
            if page is None:
                return self.predictors
            else:
                return self.predictors[(page - 1) * per_page:page * per_page]

    predictors = SeedingTestPredictorCollection(
        UUID('6b608f78-e341-422c-8076-35adc8828545'), session)

    #Adding a few predictors in the collection to have something to update
    for i in range(0, 5):
        predictors.register(
            SimpleMLPredictor(name="resource " + str(i),
                              description='',
                              inputs=[],
                              outputs=[],
                              latent_variables=[]))

    #Adding a few predictors with the same name ("resource {0,1}" were made above)
    # this is used to test behavior if there are duplicates
    for i in range(0, 2):
        predictors.register(
            SimpleMLPredictor(name="resource " + str(i),
                              description='',
                              inputs=[],
                              outputs=[],
                              latent_variables=[]))
    return predictors
Esempio n. 7
0
def test_register(valid_simple_ml_data):
    session = mock.Mock()
    session.post_resource.return_value = valid_simple_ml_data
    session.get_resource.return_value = {
        'id': str(uuid.uuid4()),
        'status': 'VALID',
        'report': {}
    }
    pc = PredictorCollection(uuid.uuid4(), session)
    predictor = SimpleMLPredictor.build(valid_simple_ml_data)
    registered = pc.register(predictor)
    assert registered.name == 'ML predictor'
Esempio n. 8
0
def test_simple_legacy_deserialization(valid_simple_ml_data):
    """Ensure that a deserialized SimplePredictor looks sane."""
    predictor: SimpleMLPredictor = SimpleMLPredictor.build(
        valid_simple_ml_data)
    assert predictor.name == 'ML predictor'
    assert predictor.description == 'Predicts z from input x and latent variable y'
    assert len(predictor.inputs) == 1
    assert predictor.inputs[0] == x
    assert len(predictor.outputs) == 1
    assert predictor.outputs[0] == z
    assert len(predictor.latent_variables) == 1
    assert predictor.latent_variables[0] == y
    assert predictor.training_data == 'training_data_key'
Esempio n. 9
0
def test_register_experimental(valid_simple_ml_predictor_data, basic_predictor_report_data):
    session = mock.Mock()
    post_response = deepcopy(valid_simple_ml_predictor_data)
    post_response["experimental"] = True
    post_response["experimental_reasons"] = ["This is a test", "Of experimental reasons"]
    session.post_resource.return_value = post_response
    session.get_resource.return_value = basic_predictor_report_data
    pc = PredictorCollection(uuid.uuid4(), session)
    predictor = SimpleMLPredictor.build(valid_simple_ml_predictor_data)
    with pytest.warns(UserWarning) as record:
        pc.register(predictor)
    msg = str(record[0].message)
    assert "Predictor" in msg
    assert "This is a test" in msg
    assert "Of experimental reasons" in msg
Esempio n. 10
0
def test_simple_legacy_deserialization(valid_simple_ml_predictor_data):
    """Ensure that a deserialized SimplePredictor looks sane."""
    predictor: SimpleMLPredictor = SimpleMLPredictor.build(
        valid_simple_ml_predictor_data)
    assert predictor.name == 'ML predictor'
    assert predictor.description == 'Predicts z from input x and latent variable y'
    assert len(predictor.inputs) == 1
    assert predictor.inputs[0] == RealDescriptor("x", 0, 100, "")
    assert len(predictor.outputs) == 1
    assert predictor.outputs[0] == RealDescriptor("z", 0, 100, "")
    assert len(predictor.latent_variables) == 1
    assert predictor.latent_variables[0] == RealDescriptor("y", 0, 100, "")
    assert len(predictor.training_data) == 1
    assert predictor.training_data[0].table_id == UUID(
        'e5c51369-8e71-4ec6-b027-1f92bdc14762')
Esempio n. 11
0
def test_create_or_update_unique_found(predictor_collection):
    # test when there is a single unique resource that exists with the listed name and update
    pred = SimpleMLPredictor(
        name="resource 4",  #this is a unique name in the collection
        description='I am updated!',
        inputs=[],
        outputs=[],
        latent_variables=[])
    #verify that the returned object is updated
    returned_pred = create_or_update(predictor_collection, pred)
    assert returned_pred.uid == pred.uid
    assert returned_pred.name == pred.name
    assert returned_pred.description == pred.description
    #verify that the collection is also updated
    updated_pred = [
        r for r in list(predictor_collection.list()) if r.name == "resource 4"
    ][0]
    assert updated_pred.description == "I am updated!"
Esempio n. 12
0
def test_mark_predictor_invalid(valid_simple_ml_predictor_data, valid_predictor_report_data):
    # Given
    session = FakeSession()
    collection = PredictorCollection(uuid.uuid4(), session)
    predictor = SimpleMLPredictor.build(valid_simple_ml_predictor_data)
    session.set_responses(valid_simple_ml_predictor_data, valid_predictor_report_data)

    # When
    predictor.archived = False
    collection.update(predictor)

    # Then
    assert 1 == session.num_calls, session.calls

    first_call = session.calls[0]  # First call is the update
    assert first_call.method == 'PUT'
    assert first_call.path == '/projects/{}/modules/{}'.format(collection.project_id, predictor.uid)
    assert not first_call.json['archived']
Esempio n. 13
0
def test_create_or_update_none_found(predictor_collection):
    # test when resource doesn't exist with listed name and check if new one is created
    assert not [
        r for r in list(predictor_collection.list()) if r.name == absent_name
    ]
    pred = SimpleMLPredictor(name=absent_name,
                             description='',
                             inputs=[],
                             outputs=[],
                             latent_variables=[])
    #verify that the returned object is updated
    returned_pred = create_or_update(predictor_collection, pred)
    assert returned_pred.uid == pred.uid
    assert returned_pred.name == pred.name
    assert returned_pred.description == pred.description
    #verify that the collection is also updated
    assert any([
        r for r in list(predictor_collection.list()) if r.name == absent_name
    ])