From 804c26a4b7ab167957b4efcf8fa427ebbb691bed Mon Sep 17 00:00:00 2001 From: Thomas Robitaille Date: Wed, 24 Apr 2019 22:29:24 +0100 Subject: [PATCH 1/2] Fix compatibility with Numpy 1.16.x by ensuring that we don't save/load object arrays from session files. --- CHANGES.md | 2 ++ glue/utils/array.py | 4 ++-- glue/utils/tests/test_array.py | 8 ++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 13b8b6cc8..db92cb1c0 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -40,6 +40,8 @@ v0.15.0 (unreleased) v0.14.3 (unreleased) -------------------- +* Fixed compatibility with Numpy 1.16.x. [#1989] + * Improve tab-completion of attribute names in Data to not include non-relevant items. [#1971] diff --git a/glue/utils/array.py b/glue/utils/array.py index f3fc3be62..553851a45 100644 --- a/glue/utils/array.py +++ b/glue/utils/array.py @@ -54,8 +54,8 @@ def unique(array): """ # numpy.unique doesn't handle mixed-types on python3, # so we use pandas - U, I = pd.factorize(array, sort=True) - return I, U + I, U = pd.factorize(array, sort=True) + return U.astype(array.dtype), I def shape_to_string(shape): diff --git a/glue/utils/tests/test_array.py b/glue/utils/tests/test_array.py index 50bf747d5..270c2583b 100644 --- a/glue/utils/tests/test_array.py +++ b/glue/utils/tests/test_array.py @@ -25,6 +25,14 @@ def test_unique(before, ref_after, ref_indices): np.testing.assert_array_equal(indices, ref_indices) +def test_unique_dtype(): + # Regression test to make sure that when working with strings, we don't + # get an object array back from the unique function + array = np.array(['a', 'b', 'c']) + U, I = unique(array) + assert U.dtype.kind in 'SU' + + def test_shape_to_string(): assert shape_to_string((1, 4, 3)) == "(1, 4, 3)" From c5b76783f4162e279be4d9fd5118e62db3dfa9f9 Mon Sep 17 00:00:00 2001 From: Thomas Robitaille Date: Wed, 24 Apr 2019 22:46:47 +0100 Subject: [PATCH 2/2] Fix unique with lists --- glue/utils/array.py | 1 + glue/utils/tests/test_array.py | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/glue/utils/array.py b/glue/utils/array.py index 553851a45..962b3bb66 100644 --- a/glue/utils/array.py +++ b/glue/utils/array.py @@ -54,6 +54,7 @@ def unique(array): """ # numpy.unique doesn't handle mixed-types on python3, # so we use pandas + array = np.asarray(array) I, U = pd.factorize(array, sort=True) return U.astype(array.dtype), I diff --git a/glue/utils/tests/test_array.py b/glue/utils/tests/test_array.py index 270c2583b..1eac4cc5d 100644 --- a/glue/utils/tests/test_array.py +++ b/glue/utils/tests/test_array.py @@ -26,12 +26,18 @@ def test_unique(before, ref_after, ref_indices): def test_unique_dtype(): + # Regression test to make sure that when working with strings, we don't # get an object array back from the unique function + array = np.array(['a', 'b', 'c']) U, I = unique(array) assert U.dtype.kind in 'SU' + li = ['a', 'b', 'c'] + U, I = unique(li) + assert U.dtype.kind in 'SU' + def test_shape_to_string(): assert shape_to_string((1, 4, 3)) == "(1, 4, 3)"