Skip to content

Commit

Permalink
Revert "MAINT: Minimal changes to get working with numpy 2.0"
Browse files Browse the repository at this point in the history
This reverts commit 64c597a.
  • Loading branch information
WarrenWeckesser committed Jun 28, 2024
1 parent 64c597a commit cad58c1
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 68 deletions.
68 changes: 34 additions & 34 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
strategy:
matrix:
python-version: ['3.12', '3.11', '3.10', '3.9']
numpy-version: ['1.20.3', '1.21.6', '1.22.4', '1.23.5', '1.24.4', '1.25.2', '1.26.4', '2.0.0']
numpy-version: ['1.20.3', '1.21.6', '1.22.4', '1.23.5', '1.24.4', '1.25.2', '1.26.4']
exclude:
- python-version: '3.10'
numpy-version: '1.20.3'
Expand Down Expand Up @@ -59,7 +59,7 @@ jobs:
strategy:
matrix:
python-version: ['3.12', '3.11', '3.10', '3.9']
numpy-version: ['1.20.3', '1.21.6', '1.22.4', '1.23.5', '1.24.4', '1.25.2', '1.26.4', '2.0.0']
numpy-version: ['1.20.3', '1.21.6', '1.22.4', '1.23.5', '1.24.4', '1.25.2', '1.26.4']
exclude:
- python-version: '3.10'
numpy-version: '1.20.3'
Expand Down Expand Up @@ -107,38 +107,38 @@ jobs:
run: |
pytest --pyargs ufunclab
# main-numpy:
# strategy:
# matrix:
# python-version: ['3.10', '3.11', '3.12']
# os: [ubuntu-latest]

# runs-on: ${{ matrix.os }}

# steps:
# - uses: actions/checkout@v4
# - name: Set up Python
# uses: actions/setup-python@v4
# with:
# python-version: ${{ matrix.python-version }}
# - name: Install dependencies
# run: |
# sudo apt-get install libopenblas-dev
# python -m pip install --upgrade pip wheel
# python -m pip install --upgrade setuptools==59.2.0
# python -m pip install ninja meson-python toml pytest
# pushd .
# cd ..
# git clone --shallow-submodules --recurse-submodules https://github.com/numpy/numpy.git
# cd numpy
# python -m pip install .
# popd
# - name: Install ufunclab
# run: |
# python -m pip install --no-build-isolation .
# - name: Test with pytest
# run: |
# pytest --pyargs ufunclab
main-numpy:
strategy:
matrix:
python-version: ['3.10', '3.11', '3.12']
os: [ubuntu-latest]

runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
sudo apt-get install libopenblas-dev
python -m pip install --upgrade pip wheel
python -m pip install --upgrade setuptools==59.2.0
python -m pip install ninja meson-python toml pytest
pushd .
cd ..
git clone --shallow-submodules --recurse-submodules https://github.com/numpy/numpy.git
cd numpy
python -m pip install .
popd
- name: Install ufunclab
run: |
python -m pip install --no-build-isolation .
- name: Test with pytest
run: |
pytest --pyargs ufunclab
windows-msvc:

Expand Down
39 changes: 34 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -993,11 +993,11 @@ of a NumPy array, but when the input is signed integers, the output
is an unsigned integer with the same bit width.

The function handles the standard integer and floating point types,
and object arrays. The function does not accept complex arrays. Also,
the function does not implement any special handling of `nan`, so the
behavior of this function with arrays containing `nan` is undefined
(i.e. it might not do what you want, and the behavior might change in
the next update of the software).
`datetime64`, `timedelta64`, and object arrays. The function does not
accept complex arrays. Also, the function does not implement any special
handling of `nan`, so the behavior of this function with arrays containing
`nan` is undefined (i.e. it might not do what you want, and the behavior
might change in the next update of the software).

```
>>> x = np.array([85, 125, 0, -75, -50], dtype=np.int8)
Expand Down Expand Up @@ -1033,6 +1033,35 @@ array([Fraction(59, 21), Fraction(8, 9)], dtype=object)
```

`dates` is an array of `datetime64`.

```
>>> dates = np.array([np.datetime64('2015-11-02T12:34:50'),
... np.datetime64('2016-03-01T16:00:00'),
... np.datetime64('2015-07-02T21:20:19'),
... np.datetime64('2016-05-01T19:25:00')])
>>> dates
array(['2015-11-02T12:34:50', '2016-03-01T16:00:00',
'2015-07-02T21:20:19', '2016-05-01T19:25:00'],
dtype='datetime64[s]')
>>> timespan = peaktopeak(dates)
>>> timespan
numpy.timedelta64(26258681,'s')
>>> timespan / np.timedelta64(1, 'D') # Convert to number of days.
303.9199189814815
```

Casting works when the `out` argument is an array with dtype `timedelta64`.
For example,

```
>>> out = np.empty((), dtype='timedelta64[D]')
>>> peaktopeak(dates, out=out)
array(303, dtype='timedelta64[D]')
```


#### `all_same`

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ requires = [

[project]
name = 'ufunclab'
version = '0.0.8.dev10'
version = '0.0.8.dev9'
description = 'NumPy ufuncs and utilities.'
readme = 'README.md'
requires-python = '>=3.9'
Expand Down
21 changes: 5 additions & 16 deletions src/peaktopeak/peaktopeak_gufunc.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,6 @@ peaktopeak_@typename@_loop(char **args, const npy_intp *dimensions,
// typical use-cases for these calculations do not encounter this situation.
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

#ifdef UNDEFINED

Handling of datetime64 and timedelta64 is disabled. This code needs to
be updated for NumPy 2.0

static void peaktopeak_int64_signed_loop(char **args, const npy_intp *dimensions,
const npy_intp* steps, void* data)
{
Expand Down Expand Up @@ -176,8 +171,6 @@ static void peaktopeak_int64_signed_loop(char **args, const npy_intp *dimensions
}
}

#endif

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// ufunc inner loop for object arrays.
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand Down Expand Up @@ -259,8 +252,8 @@ static char peaktopeak_typecodes[] = {
NPY_FLOAT, NPY_FLOAT,
NPY_DOUBLE, NPY_DOUBLE,
NPY_LONGDOUBLE, NPY_LONGDOUBLE,
// NPY_DATETIME, NPY_TIMEDELTA,
// NPY_TIMEDELTA, NPY_TIMEDELTA,
NPY_DATETIME, NPY_TIMEDELTA,
NPY_TIMEDELTA, NPY_TIMEDELTA,
NPY_OBJECT, NPY_OBJECT
};

Expand All @@ -276,16 +269,14 @@ static PyUFuncGenericFunction peaktopeak_funcs[] = {
(PyUFuncGenericFunction) &peaktopeak_float_loop,
(PyUFuncGenericFunction) &peaktopeak_double_loop,
(PyUFuncGenericFunction) &peaktopeak_longdouble_loop,
// (PyUFuncGenericFunction) &peaktopeak_int64_signed_loop,
// (PyUFuncGenericFunction) &peaktopeak_int64_signed_loop,
(PyUFuncGenericFunction) &peaktopeak_int64_signed_loop,
(PyUFuncGenericFunction) &peaktopeak_int64_signed_loop,
(PyUFuncGenericFunction) &peaktopeak_object_loop
};

#define PEAKTOPEAK_NTYPES (sizeof(peaktopeak_funcs)/sizeof(peaktopeak_funcs[0]))
static void *peaktopeak_data[PEAKTOPEAK_NTYPES];

#ifdef UNDEFINED

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// peaktopeak type resolver
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand Down Expand Up @@ -419,8 +410,6 @@ PeakToPeakTypeResolver(PyUFuncObject *ufunc,
return 0;
}

#endif

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Python extension module definitions.
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand Down Expand Up @@ -477,7 +466,7 @@ PyMODINIT_FUNC PyInit__peaktopeak(void)
Py_DECREF(module);
return NULL;
}
// peaktopeak_gufunc->type_resolver = PeakToPeakTypeResolver;
peaktopeak_gufunc->type_resolver = PeakToPeakTypeResolver;

status = PyModule_AddObject(module, "peaktopeak", (PyObject *) peaktopeak_gufunc);
if (status == -1) {
Expand Down
2 changes: 1 addition & 1 deletion src/searchsorted/searchsorted_gufunc.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ SearchSortedTypeResolver(PyUFuncObject *ufunc,
*/
type_num1 = PyArray_DESCR(operands[0])->type_num;
type_num2 = PyArray_DESCR(operands[1])->type_num;
if (type_num1 >= NPY_NTYPES_LEGACY || type_num2 >= NPY_NTYPES_LEGACY ||
if (type_num1 >= NPY_NTYPES || type_num2 >= NPY_NTYPES ||
type_num1 == NPY_OBJECT || type_num2 == NPY_OBJECT) {
return PyUFunc_DefaultTypeResolver(ufunc, casting, operands,
type_tup, out_dtypes);
Expand Down
19 changes: 8 additions & 11 deletions ufunclab/tests/test_peaktopeak.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,11 @@ def test_fractions_2d():
assert_array_equal(p1, [Fraction(23, 9), Fraction(0)])


# Removed until datetime64 and timedelta64 handling is restored
# in ufunclab.peaktopeak.
#
# def test_dates():
# dates = np.array([np.datetime64('2015-11-02T12:34:50'),
# np.datetime64('2015-11-02T10:00:00'),
# np.datetime64('2015-11-02T21:20:19'),
# np.datetime64('2015-11-02T19:25:00')])
# timespan = peaktopeak(dates)
# assert_equal(timespan.dtype, np.dtype('m8[s]'))
# assert_equal(timespan, np.timedelta64(40819, 's'))
def test_dates():
dates = np.array([np.datetime64('2015-11-02T12:34:50'),
np.datetime64('2015-11-02T10:00:00'),
np.datetime64('2015-11-02T21:20:19'),
np.datetime64('2015-11-02T19:25:00')])
timespan = peaktopeak(dates)
assert_equal(timespan.dtype, np.dtype('m8[s]'))
assert_equal(timespan, np.timedelta64(40819, 's'))

0 comments on commit cad58c1

Please sign in to comment.