コード例 #1
0
def test_deployment_categorical():
    predictions = {str(i): random.random() for i in range(10)}
    responses.add(responses.POST,
                  'http://deployment_url',
                  json={'rows': [{
                      'label': predictions
                  }]})
    deployment = Deployment(url='http://deployment_url',
                            token='deployment_token',
                            dtypes_in={
                                'feature_1': 'Float (1)',
                                'feature_2': 'Numpy (30)'
                            },
                            dtypes_out={'label': 'Categorical (10)'})

    # Single scalar value prediction
    prediction = deployment.predict(feature_1=1.0,
                                    feature_2=np.random.rand(30))
    assert prediction == {'label': predictions}

    # Send bad type
    with pytest.raises(TypeError):
        deployment.predict(feature_1=1.0, feature_2='foo')

    # Return bad shape
    deployment = Deployment(url='http://deployment_url',
                            token='deployment_token',
                            dtypes_in={
                                'feature_1': 'Float (1)',
                                'feature_2': 'Numpy (30)'
                            },
                            dtypes_out={'label': 'Categorical (5)'})
    with pytest.raises(ValueError):
        deployment.predict(feature_1=0.0, feature_2=np.random.rand(30))
コード例 #2
0
def test_deployment_numpy_autoencoder():
    arr = np.random.rand(100, 10, 3).astype(np.float32)
    encoded = sidekick.encode.NumpyEncoder().encode_json(arr)
    responses.add(responses.POST,
                  'http://deployment_url',
                  json={'rows': [{
                      'numpy_out': encoded
                  }]})
    deployment = Deployment(url='http://deployment_url',
                            token='deployment_token',
                            dtypes_in={'numpy_in': 'Numpy (100x10x3)'},
                            dtypes_out={'numpy_out': 'Numpy (100x10x3)'})

    # Single numpy prediction
    prediction = deployment.predict(numpy_in=arr)
    np.testing.assert_array_equal(prediction['numpy_out'], arr)

    # List of numpy predictions
    predictions = deployment.predict_many({'numpy_in': arr} for _ in range(10))
    for prediction in predictions:
        np.testing.assert_array_equal(prediction['numpy_out'], arr)

    # Generator of numpy predictions
    predictions = deployment.predict_lazy({'numpy_in': arr} for _ in range(10))
    for prediction in predictions:
        np.testing.assert_array_equal(prediction['numpy_out'], arr)

    # Send bad shape
    with pytest.raises(ValueError):
        deployment.predict(numpy_in=np.random.rand(100, 1, 1))
コード例 #3
0
def test_deployment_scalars():
    responses.add(responses.POST,
                  'http://deployment_url',
                  json={'rows': [{
                      'out': 1.0
                  }]})
    deployment = Deployment(url='http://deployment_url',
                            token='deployment_token',
                            dtypes_in={
                                'feature_1': 'Float (1)',
                                'feature_2': 'Int (1)'
                            },
                            dtypes_out={'out': 'Float (1)'})

    # Single scalar value prediction
    prediction = deployment.predict(feature_1=1.0, feature_2=1)
    assert prediction == {'out': 1.0}

    # List of scalar value prediction
    prediction = deployment.predict(feature_1=1.0, feature_2=1)
    assert prediction == {'out': 1.0}

    # Generator of scalar value predictions
    prediction = deployment.predict(feature_1=1.0, feature_2=1)
    assert prediction == {'out': 1.0}

    # Send bad type
    with pytest.raises(TypeError):
        deployment.predict(feature_1=1.0, feature_2='foo')
コード例 #4
0
def test_deployment_user_agent():
    features_in = [FeatureSpec('input', 'numeric', (1, ))]
    features_out = [FeatureSpec('output', 'numeric', (1, ))]
    prediction = 1

    responses.add(responses.POST,
                  'http://peltarion.com/deployment/forward',
                  json={'rows': [{
                      'output': prediction
                  }]})

    responses.add(
        responses.GET,
        'http://peltarion.com/deployment/openapi.json',
        json=mock_api_specs(features_in, features_out),
    )

    deployment = Deployment(
        url='http://peltarion.com/deployment/forward',
        token='deployment_token',
    )

    predictions = deployment.predict(input=1.0)
    request = responses.calls[0].request
    assert predictions == {'output': prediction}
    assert len(responses.calls) == 2
    assert 'sidekick' in request.headers['User-Agent'].lower()
コード例 #5
0
def test_deployment_instantiation():
    features_in = [FeatureSpec('input', 'image', (100, 100, 3))]
    features_out = [FeatureSpec('output', 'numeric', (1, ))]

    responses.add(
        responses.GET,
        'http://peltarion.com/deployment/openapi.json',
        json=mock_api_specs(features_in, features_out),
    )

    deployment = Deployment(
        url='http://peltarion.com/deployment/forward',
        token='deployment_token',
    )

    assert deployment.feature_specs_in[0].name == features_in[0].name
    assert deployment.feature_specs_in[0].dtype == features_in[0].dtype
    assert deployment.feature_specs_in[0].shape == tuple(features_in[0].shape)

    assert deployment.feature_specs_out[0].name == features_out[0].name
    assert deployment.feature_specs_out[0].dtype == features_out[0].dtype
    assert (deployment.feature_specs_out[0].shape == tuple(
        features_out[0].shape))

    # Ensure feature_specs cannot be modified
    deployment.feature_specs_in[0].shape = (0, 0, 0)
    deployment.feature_specs_out[0].name = 'string'
    assert deployment.feature_specs_in[0].shape == features_in[0].shape
    assert deployment.feature_specs_out[0].name == features_out[0].name
コード例 #6
0
def test_deployment_image_autoencoder():
    shape = (100, 10, 3)
    features_in = [FeatureSpec('input', 'image', shape)]
    features_out = [FeatureSpec('output', 'image', shape)]
    arr = np.uint8(np.random.rand(*shape) * 255)
    image = Image.fromarray(arr)
    image.format = 'png'

    encoded = sidekick.encode.ImageEncoder().encode_json(image)
    responses.add(responses.POST,
                  'http://peltarion.com/deployment/forward',
                  json={'rows': [{
                      'output': encoded
                  }]})

    responses.add(
        responses.GET,
        'http://peltarion.com/deployment/openapi.json',
        json=mock_api_specs(features_in, features_out),
    )

    deployment = Deployment(
        url='http://peltarion.com/deployment/forward',
        token='deployment_token',
    )

    prediction = deployment.predict(input=image)
    np.testing.assert_array_equal(np.array(prediction['output']), arr)

    # Send bad type
    with pytest.raises(TypeError):
        deployment.predict(input=arr)
コード例 #7
0
def test_deployment_text():
    categories = 10
    features_in = [FeatureSpec('input', 'text', (20, ))]
    features_out = [FeatureSpec('output', 'categorical', (categories, ))]
    predictions = {str(i): random.random() for i in range(categories)}

    responses.add(
        responses.POST,
        'http://peltarion.com/deployment/forward',
        json={'rows': [{
            'output': predictions
        }]},
    )

    responses.add(
        responses.GET,
        'http://peltarion.com/deployment/openapi.json',
        json=mock_api_specs(features_in, features_out),
    )

    deployment = Deployment(
        url='http://peltarion.com/deployment/forward',
        token='deployment_token',
    )

    prediction = deployment.predict(input='foo')
    assert prediction == {'output': predictions}
コード例 #8
0
def test_deployment_categorical():
    categories = 10
    features_in = [
        FeatureSpec('input_1', 'numeric', (1, )),
        FeatureSpec('input_2', 'numeric', (30, )),
    ]
    features_out = [FeatureSpec('output', 'categorical', (categories, ))]
    predictions = {str(i): random.random() for i in range(categories)}

    responses.add(
        responses.POST,
        'http://peltarion.com/deployment/forward',
        json={'rows': [{
            'output': predictions
        }]},
    )

    responses.add(
        responses.GET,
        'http://peltarion.com/deployment/openapi.json',
        json=mock_api_specs(features_in, features_out),
    )

    deployment = Deployment(
        url='http://peltarion.com/deployment/forward',
        token='deployment_token',
    )

    prediction = deployment.predict(input_1=1.0, input_2=np.random.rand(30))
    assert prediction == {'output': predictions}

    # Send bad type
    with pytest.raises(TypeError):
        deployment.predict(input_1=1.0, input_2='foo')

    # Return bad shape
    predictions = {str(i): random.random() for i in range(5)}
    responses.replace(
        responses.POST,
        'http://peltarion.com/deployment/forward',
        json={'rows': [{
            'output': predictions
        }]},
    )

    with pytest.raises(ValueError):
        deployment.predict(input_1=1.0, input_2=np.random.rand(30))
コード例 #9
0
def test_deployment_numpy_autoencoder():
    shape = (100, 10, 3)
    features_in = [FeatureSpec('input', 'numeric', shape)]
    features_out = [FeatureSpec('output', 'numeric', shape)]

    arr = np.random.rand(*shape).astype(np.float32)
    encoded = sidekick.encode.NumpyEncoder().encode_json(arr)

    responses.add(
        responses.POST,
        'http://peltarion.com/deployment/forward',
        json={'rows': [{
            'output': encoded
        }]},
    )

    responses.add(
        responses.GET,
        'http://peltarion.com/deployment/openapi.json',
        json=mock_api_specs(features_in, features_out),
    )

    deployment = Deployment(
        url='http://peltarion.com/deployment/forward',
        token='deployment_token',
    )

    # Single numpy prediction
    prediction = deployment.predict(input=arr)
    np.testing.assert_array_equal(prediction['output'], arr)

    # List of numpy predictions
    predictions = deployment.predict_many({'input': arr} for _ in range(10))
    for prediction in predictions:
        np.testing.assert_array_equal(prediction['output'], arr)

    # Generator of numpy predictions
    predictions = deployment.predict_lazy({'input': arr} for _ in range(10))
    for prediction in predictions:
        np.testing.assert_array_equal(prediction['output'], arr)

    # Send bad shape
    with pytest.raises(ValueError):
        deployment.predict(input=np.random.rand(100, 1, 1))
コード例 #10
0
def test_deployment_numeric_multiple_input():
    features_in = [
        FeatureSpec('input_1', 'numeric', (1, )),
        FeatureSpec('input_2', 'numeric', (1, )),
    ]
    features_out = [FeatureSpec('output', 'numeric', (1, ))]
    output = 1

    responses.add(responses.POST,
                  'http://peltarion.com/deployment/forward',
                  json={'rows': [{
                      'output': output
                  }]})

    responses.add(
        responses.GET,
        'http://peltarion.com/deployment/openapi.json',
        json=mock_api_specs(features_in, features_out),
    )

    deployment = Deployment(
        url='http://peltarion.com/deployment/forward',
        token='deployment_token',
    )

    # Single numeric prediction
    predictions = deployment.predict(input_1=1.0, input_2=2)
    assert predictions == {'output': output}

    inputs = [{'input_1': 1.0, 'input_2': 2} for _ in range(10)]

    # List of numeric predictions
    predictions = deployment.predict_many(inputs)
    for prediction in predictions:
        np.testing.assert_array_equal(prediction['output'], output)

    # Generator of numeric predictions
    predictions = deployment.predict_lazy(inputs)
    for prediction in predictions:
        np.testing.assert_array_equal(prediction['output'], output)

    # Incorrect type
    with pytest.raises(TypeError):
        deployment.predict(input_1=1.0, input_2='foo')
コード例 #11
0
def test_deployment_user_agent():
    responses.add(responses.POST,
                  'http://deployment_url',
                  json={'rows': [{
                      'out': 1.0
                  }]})
    deployment = Deployment(url='http://deployment_url',
                            token='deployment_token',
                            dtypes_in={'feature': 'Float (1)'},
                            dtypes_out={'out': 'Float (1)'})

    # Single scalar value prediction
    prediction = deployment.predict(feature=1.0)

    # Assert user agent and calls were correct
    request = responses.calls[0].request
    assert prediction == {'out': 1.0}
    assert len(responses.calls) == 1
    assert 'sidekick' in request.headers['User-Agent'].lower()
コード例 #12
0
def test_deployment_image_autoencoder():
    arr = np.uint8(np.random.rand(100, 10, 3) * 255)
    image = Image.fromarray(arr)
    image.format = 'png'

    encoded = sidekick.encode.ImageEncoder().encode_json(image)
    responses.add(responses.POST,
                  'http://deployment_url',
                  json={'rows': [{
                      'image_out': encoded
                  }]})
    deployment = Deployment(url='http://deployment_url',
                            token='deployment_token',
                            dtypes_in={'image_in': 'Image (100x10x3)'},
                            dtypes_out={'image_out': 'Image (100x10x3)'})

    # Single image prediction
    prediction = deployment.predict(image_in=image)
    np.testing.assert_array_equal(np.array(prediction['image_out']), arr)

    # Send bad type
    with pytest.raises(TypeError):
        deployment.predict(image_in=arr)
コード例 #13
0
def test_deployment_numeric_single_input():
    features_in = [FeatureSpec('input', 'numeric', (1, ))]
    features_out = [FeatureSpec('output', 'numeric', (1, ))]
    prediction = 1

    responses.add(responses.POST,
                  'http://peltarion.com/deployment/forward',
                  json={'rows': [{
                      'output': prediction
                  }]})

    responses.add(
        responses.GET,
        'http://peltarion.com/deployment/openapi.json',
        json=mock_api_specs(features_in, features_out),
    )

    deployment = Deployment(
        url='http://peltarion.com/deployment/forward',
        token='deployment_token',
    )

    predictions = deployment.predict(input=1.0)
    assert predictions == {'output': prediction}