Skip to content
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

add support for numpy 1.18.1 #2448

Merged
merged 10 commits into from
Feb 11, 2020
26 changes: 6 additions & 20 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,17 @@ install:
# install dependencies yt
if [[ $TRAVIS_BUILD_STAGE_NAME != "Lint" ]]; then
if [[ $MINIMAL == 1 ]]; then
$PIP install numpy==1.10.4 cython==0.24
# The first numpy to support py3.6 is 1.12, but numpy 1.13 matches
# unyt so we'll match it here.
$PIP install numpy==1.13.3 cython==0.24
$PIP install -r tests/test_minimal_requirements.txt
else
# Getting cartopy installed requires getting cython and numpy installed
# first; this is potentially going to be fixed with the inclusion of
# pyproject.toml in cartopy.
# These versions are pinned, so we will need to update/remove them when
# the hack is no longer necessary.
$PIP install numpy==1.16.2 cython==0.29.6
$PIP install numpy==1.18.1 cython==0.29.6
$PIP install -r tests/test_requirements.txt
fi
$PIP install -e .
Expand All @@ -78,21 +80,12 @@ jobs:
python: 3.6
script: flake8 yt/

- stage: lint
python: 2.7
script: flake8 yt/

- stage: tests
name: "Python: 2.7 Unit Tests"
python: 2.7
name: "Python: 3.6 Minimal Dependency Unit Tests"
python: 3.6
env: MINIMAL=1
script: coverage run $(which nosetests) -c nose_unit.cfg

- stage: tests
name: "Python: 2.7 Unit Tests"
python: 2.7
script: coverage run $(which nosetests) -c nose_unit.cfg

- stage: tests
name: "Python: 3.5 Unit Tests"
python: 3.5
Expand All @@ -103,13 +96,6 @@ jobs:
python: 3.6
script: coverage run $(which nosetests) -c nose_unit.cfg

- stage: tests
name: "Python: 2.7 Minimal Dependency Answer Tests"
python: 2.7
env: MINIMAL=1
script: coverage run $(which nosetests) -c nose_answer.cfg
after_failure: python tests/report_failed_answers.py -f -m --xunit-file "answer_nosetests.xml"

- stage: tests
name: "Python: 3.6 Answer Tests"
python: 3.6
Expand Down
10 changes: 8 additions & 2 deletions yt/units/tests/test_ytarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,10 @@ def unary_ufunc_comparison(ufunc, a):


def binary_ufunc_comparison(ufunc, a, b):
out = a.copy()
if ufunc in [np.divmod]:
munkm marked this conversation as resolved.
Show resolved Hide resolved
out = (a.copy(), a.copy())
else:
out = a.copy()
if ufunc in yield_np_ufuncs([
'add', 'subtract', 'remainder', 'fmod', 'mod', 'arctan2', 'hypot',
'greater', 'greater_equal', 'less', 'less_equal', 'equal',
Expand Down Expand Up @@ -860,7 +863,10 @@ def binary_ufunc_comparison(ufunc, a, b):
assert_true(not isinstance(ret, YTArray) and
isinstance(ret, np.ndarray))
if isinstance(ret, tuple):
assert_array_equal(ret[0], out)
assert isinstance(out, tuple)
assert len(out) == len(ret)
for o, r in zip(out, ret):
assert_array_equal(o, r)
else:
assert_array_equal(ret, out)
if (ufunc in (np.divide, np.true_divide, np.arctan2) and
Expand Down
31 changes: 25 additions & 6 deletions yt/units/yt_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,8 @@ def _unit_repr_check_same(my_units, other_units):
sin, cos, tan,
)

multiple_output_operators = {modf: 2, frexp: 2, divmod_: 2}

class YTArray(np.ndarray):
"""
An ndarray subclass that attaches a symbolic unit object to the array data.
Expand Down Expand Up @@ -1370,9 +1372,25 @@ def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
func = getattr(ufunc, method)
if 'out' in kwargs:
out_orig = kwargs.pop('out')
out = np.asarray(out_orig[0])
if ufunc in multiple_output_operators:
outs = []
for arr in out_orig:
outs.append(arr.view(np.ndarray))
out = tuple(outs)
else:
out_element = out_orig[0]
if out_element.dtype.kind in ("u", "i"):
munkm marked this conversation as resolved.
Show resolved Hide resolved
new_dtype = "f" + str(out_element.dtype.itemsize)
float_values = out_element.astype(new_dtype)
out_element.dtype = new_dtype
np.copyto(out_element, float_values)
out = out_element.view(np.ndarray)
else:
out = None
if ufunc in multiple_output_operators:
num_outputs = multiple_output_operators[ufunc]
out = (None,) * num_outputs
else:
out = None
if len(inputs) == 1:
_, inp, u = get_inp_u_unary(ufunc, inputs)
out_arr = func(np.asarray(inp), out=out, **kwargs)
Expand Down Expand Up @@ -1439,14 +1457,15 @@ def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
else:
out_arr = ret_class(np.asarray(out_arr), unit)
if out is not None:
out_orig[0].flat[:] = out.flat[:]
if isinstance(out_orig[0], YTArray):
out_orig[0].units = unit
if ufunc not in multiple_output_operators:
out_orig[0].flat[:] = out.flat[:]
if isinstance(out_orig[0], YTArray):
out_orig[0].units = unit
return out_arr

def copy(self, order='C'):
return type(self)(np.copy(np.asarray(self)), self.units)

def __array_finalize__(self, obj):
if obj is None and hasattr(self, 'units'):
return
Expand Down
4 changes: 2 additions & 2 deletions yt/visualization/color_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,10 +548,10 @@ def make_colormap(ctuple_list, name=None, interpolate=True):

# Interpolate the R, G, and B channels from one color to the next
# Use np.round to make sure you're on a discrete index
interval = np.round(next_index)-np.round(rolling_index)
interval = int(np.round(next_index)-np.round(rolling_index))
neutrinoceros marked this conversation as resolved.
Show resolved Hide resolved
for j in np.arange(3):
cmap[int(np.rint(rolling_index)):int(np.rint(next_index)), j] = \
np.linspace(color[j], next_color[j], interval)
np.linspace(color[j], next_color[j], num=interval)

rolling_index = next_index

Expand Down