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))
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))
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')