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

median raises assertion error for an empty array #9498

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

adityav1810
Copy link

This is my first time contributing to an opensource project.
Solving issue #9433
It would be great if someone could help me figure out where can i add some test cases to support this solution.

@esc esc requested a review from kc611 March 19, 2024 14:38
@kc611 kc611 self-assigned this Mar 19, 2024
Copy link
Collaborator

@kc611 kc611 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @adityav1810, Thank you for your contribution to Numba. 😄

I think the tests you're looking for is in numba/tests/test_array_reductions.py::TestArrayReductions::check_median_basic, the actual test itself (on line 343 of same file) is quite confusing, so you'd want to simply add a empty array case in this particular function and run the check function on it.

Also do remember to run flake8 on the file that you change.

Feel free to ask questions if any.

numba/np/arraymath.py Outdated Show resolved Hide resolved
@adityav1810
Copy link
Author

Hi @kc611 . Thanks for the help. I added the test and ran flake8 as well. However, it seems like the tests fail. Anyidea what I am missing?

numba/tests/test_array_reductions.py Outdated Show resolved Hide resolved
Comment on lines 1598 to 1601
if n != 0:
return _median_inner(temp_arry, n)
else:
return np.nan
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You cannot call np.median when the array dtype is datetime* and it is empty:

>>> import numpy as np
>>> a = np.array([]).astype(np.datetime64)
>>> np.median(a)
/Users/guilhermeleobas/micromamba/envs/numba/lib/python3.9/site-packages/numpy/core/fromnumeric.py:3474: RuntimeWarning: Mean of empty slice.
  return _methods._mean(a, axis=axis, dtype=dtype,
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<__array_function__ internals>", line 180, in median
  File "/Users/guilhermeleobas/micromamba/envs/numba/lib/python3.9/site-packages/numpy/lib/function_base.py", line 3793, in median
    r, k = _ureduce(a, func=_median, axis=axis, out=out,
  File "/Users/guilhermeleobas/micromamba/envs/numba/lib/python3.9/site-packages/numpy/lib/function_base.py", line 3702, in _ureduce
    r = func(a, **kwargs)
  File "/Users/guilhermeleobas/micromamba/envs/numba/lib/python3.9/site-packages/numpy/lib/function_base.py", line 3847, in _median
    rout = mean(part[indexer], axis=axis, out=out)
  File "<__array_function__ internals>", line 180, in mean
  File "/Users/guilhermeleobas/micromamba/envs/numba/lib/python3.9/site-packages/numpy/core/fromnumeric.py", line 3474, in mean
    return _methods._mean(a, axis=axis, dtype=dtype,
  File "/Users/guilhermeleobas/micromamba/envs/numba/lib/python3.9/site-packages/numpy/core/_methods.py", line 179, in _mean
    ret = umr_sum(arr, axis, dtype, out, keepdims, where=where)
numpy.core._exceptions._UFuncBinaryResolutionError: ufunc 'add' cannot use operands with types dtype('<M8') and dtype('<M8')

Changing your code to the one below should fix the issue:

@overload(np.median)
def np_median(a):
    if not isinstance(a, types.Array):
        return

    is_datetime = as_dtype(a.dtype).char in 'mM'

    def median_impl(a):
        # np.median() works on the flattened array, and we need a temporary
        # workspace anyway
        temp_arry = a.flatten()
        n = temp_arry.shape[0]
        if not is_datetime and n == 0:
            return np.nan
        return _median_inner(temp_arry, n)
    return median_impl

adityav1810 and others added 2 commits April 4, 2024 21:04
Co-authored-by: Guilherme Leobas <guilhermeleobas@gmail.com>
@guilhermeleobas
Copy link
Collaborator

Regarding the datetime64 failure, I think this may be a bug on NumPy:
numpy/numpy#26216

@guilhermeleobas
Copy link
Collaborator

@adityav1810 Can you add a release notes file to your PR?
https://numba.readthedocs.io/en/latest/developer/contributing.html#release-notes

@gmarkall gmarkall added 4 - Waiting on author Waiting for author to respond to review and removed 2 - In Progress labels Apr 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4 - Waiting on author Waiting for author to respond to review Effort - short Short size effort needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants