-
Notifications
You must be signed in to change notification settings - Fork 1.2k
breaking: Move _NPYSerializer to sagemaker.serializers and rename to NumpySerializer #1691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1f7f103
Move _NPYSerializer
71cf738
Address review comments
091a7ca
Merge branch 'zwei' into add-numpy-serializer
bveeramani 7c1762c
Merge branch 'zwei' into add-numpy-serializer
bveeramani 50b3d39
Apease lint
60f3721
Resolve merge conflicts
58bf4a1
Merge branch 'zwei' into add-numpy-serializer
bveeramani 52e189c
Update serializers.py
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
# Copyright 2017-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). You | ||
# may not use this file except in compliance with the License. A copy of | ||
# the License is located at | ||
# | ||
# http://aws.amazon.com/apache2.0/ | ||
# | ||
# or in the "license" file accompanying this file. This file is | ||
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF | ||
# ANY KIND, either express or implied. See the License for the specific | ||
# language governing permissions and limitations under the License. | ||
from __future__ import absolute_import | ||
|
||
import io | ||
|
||
import numpy as np | ||
import pytest | ||
|
||
from sagemaker.serializers import NumpySerializer | ||
|
||
|
||
@pytest.fixture | ||
def numpy_serializer(): | ||
return NumpySerializer() | ||
|
||
|
||
def test_numpy_serializer_python_array(numpy_serializer): | ||
array = [1, 2, 3] | ||
result = numpy_serializer.serialize(array) | ||
|
||
assert np.array_equal(array, np.load(io.BytesIO(result))) | ||
|
||
|
||
def test_numpy_serializer_python_array_with_dtype(): | ||
numpy_serializer = NumpySerializer(dtype="float16") | ||
array = [1, 2, 3] | ||
|
||
result = numpy_serializer.serialize(array) | ||
|
||
deserialized = np.load(io.BytesIO(result)) | ||
assert np.array_equal(array, deserialized) | ||
assert deserialized.dtype == "float16" | ||
|
||
|
||
def test_numpy_serializer_numpy_valid_2_dimensional(numpy_serializer): | ||
array = np.array([[1, 2, 3], [3, 4, 5]]) | ||
result = numpy_serializer.serialize(array) | ||
|
||
assert np.array_equal(array, np.load(io.BytesIO(result))) | ||
|
||
|
||
def test_numpy_serializer_numpy_valid_multidimensional(numpy_serializer): | ||
array = np.ones((10, 10, 10, 10)) | ||
result = numpy_serializer.serialize(array) | ||
|
||
assert np.array_equal(array, np.load(io.BytesIO(result))) | ||
|
||
|
||
def test_numpy_serializer_numpy_valid_list_of_strings(numpy_serializer): | ||
array = np.array(["one", "two", "three"]) | ||
result = numpy_serializer.serialize(array) | ||
|
||
assert np.array_equal(array, np.load(io.BytesIO(result))) | ||
|
||
|
||
def test_numpy_serializer_from_buffer_or_file(numpy_serializer): | ||
array = np.ones((2, 3)) | ||
stream = io.BytesIO() | ||
np.save(stream, array) | ||
stream.seek(0) | ||
|
||
result = numpy_serializer.serialize(stream) | ||
|
||
assert np.array_equal(array, np.load(io.BytesIO(result))) | ||
|
||
|
||
def test_numpy_serializer_object(numpy_serializer): | ||
object = {1, 2, 3} | ||
|
||
result = numpy_serializer.serialize(object) | ||
|
||
assert np.array_equal(np.array(object), np.load(io.BytesIO(result), allow_pickle=True)) | ||
|
||
|
||
def test_numpy_serializer_list_of_empty(numpy_serializer): | ||
with pytest.raises(ValueError) as invalid_input: | ||
numpy_serializer.serialize(np.array([[], []])) | ||
|
||
assert "empty array" in str(invalid_input) | ||
|
||
|
||
def test_numpy_serializer_numpy_invalid_empty(numpy_serializer): | ||
with pytest.raises(ValueError) as invalid_input: | ||
numpy_serializer.serialize(np.array([])) | ||
|
||
assert "empty array" in str(invalid_input) | ||
|
||
|
||
def test_numpy_serializer_python_invalid_empty(numpy_serializer): | ||
with pytest.raises(ValueError) as error: | ||
numpy_serializer.serialize([]) | ||
assert "empty array" in str(error) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.