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

Closed
wants to merge 9 commits into 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
Contributor

@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
Contributor

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

@guilhermeleobas
Copy link
Contributor

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

@guilhermeleobas
Copy link
Contributor

@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
Copy link

github-actions bot commented Jul 8, 2024

This pull request is marked as stale as it has had no activity in the past 3 months. Please respond to this comment if you're still interested in working on this. Many thanks!

@github-actions github-actions bot added the stale Marker label for stale issues. label Jul 8, 2024
@guilhermeleobas guilhermeleobas added 2 - In Progress and removed stale Marker label for stale issues. labels Jul 9, 2024
Copy link

github-actions bot commented Oct 8, 2024

This pull request is marked as stale as it has had no activity in the past 3 months. Please respond to this comment if you're still interested in working on this. Many thanks!

@github-actions github-actions bot added the stale Marker label for stale issues. label Oct 8, 2024
@github-actions github-actions bot added the abandoned - stale PRs automatically closed due to stale status label Oct 15, 2024
@github-actions github-actions bot closed this Oct 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2 - In Progress 4 - Waiting on author Waiting for author to respond to review abandoned - stale PRs automatically closed due to stale status Effort - short Short size effort needed stale Marker label for stale issues.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants