-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace numpy-Python comparison with dtype (#210)
* fix: Replace numpy-Python comparison with dtype * test: Add test
- Loading branch information
Showing
3 changed files
with
48 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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,4 @@ | ||
- bump: patch | ||
changes: | ||
changed: | ||
- Replaced unsafe numpy-Python comparison with use of numpy dtype to convert byte-string arrays to Unicode ones within enums |
This file contains 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 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,39 @@ | ||
import pytest | ||
import numpy as np | ||
from policyengine_core.enums.enum import Enum | ||
from policyengine_core.enums.enum_array import EnumArray | ||
|
||
|
||
def test_enum_creation(): | ||
""" | ||
Test to make sure that various types of numpy arrays | ||
are correctly encoded to int-typed EnumArray instances; | ||
check enum_array.py to see why int-typed | ||
""" | ||
|
||
test_simple_array = ["MAXWELL", "DWORKIN", "MAXWELL"] | ||
|
||
class Sample(Enum): | ||
MAXWELL = "maxwell" | ||
DWORKIN = "dworkin" | ||
|
||
sample_string_array = np.array(test_simple_array) | ||
sample_item_array = np.array( | ||
[Sample.MAXWELL, Sample.DWORKIN, Sample.MAXWELL] | ||
) | ||
explicit_s_array = np.array(test_simple_array, "S") | ||
|
||
encoded_array = Sample.encode(sample_string_array) | ||
assert len(encoded_array) == 3 | ||
assert isinstance(encoded_array, EnumArray) | ||
assert encoded_array.dtype.kind == "i" | ||
|
||
encoded_array = Sample.encode(sample_item_array) | ||
assert len(encoded_array) == 3 | ||
assert isinstance(encoded_array, EnumArray) | ||
assert encoded_array.dtype.kind == "i" | ||
|
||
encoded_array = Sample.encode(explicit_s_array) | ||
assert len(encoded_array) == 3 | ||
assert isinstance(encoded_array, EnumArray) | ||
assert encoded_array.dtype.kind == "i" |