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

ak.type does not understand numpy.<type> style dtypes #1840

Closed
lgray opened this issue Oct 26, 2022 · 3 comments · Fixed by #1841
Closed

ak.type does not understand numpy.<type> style dtypes #1840

lgray opened this issue Oct 26, 2022 · 3 comments · Fixed by #1841
Labels
bug The problem described is something that must be fixed

Comments

@lgray
Copy link
Contributor

lgray commented Oct 26, 2022

Version of Awkward Array

2.0.0rc1

Description and code to reproduce

numpy = 1.23.4

>>> import awkward as ak
>>> import numpy as np
>>> x = np.random.normal(size=100)
>>> ak.type(x)

results in:

Traceback (most recent call last):
  File "/Users/lgray/miniforge3/envs/coffea-dev/lib/python3.8/site-packages/awkward/operations/ak_type.py", line 99, in _impl
    out = ak.types.numpytype._dtype_to_primitive_dict[array.dtype.type]
KeyError: <class 'numpy.float64'>

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/lgray/miniforge3/envs/coffea-dev/lib/python3.8/site-packages/awkward/operations/ak_type.py", line 60, in type
    return _impl(array)
  File "/Users/lgray/miniforge3/envs/coffea-dev/lib/python3.8/site-packages/awkward/operations/ak_type.py", line 101, in _impl
    raise ak._errors.wrap_error(
TypeError: while calling

    ak.type(
        array = numpy.ndarray([ 0.27824033 -1.483569   -0.61108357 ...
    )

Error details: numpy array type is unrecognized by awkward: <class 'numpy.float64'>

np.float64 (or np.<type> in general) is a fairly common way for folks to denote typing, we should probably support it.

@lgray lgray added the bug (unverified) The problem described would be a bug, but needs to be triaged label Oct 26, 2022
@jpivarski
Copy link
Member

Here's where it can go: in awkward/operations/ak_type.py, there's a case-catcher for

elif isinstance(
array,
tuple(x.type for x in ak.types.numpytype._dtype_to_primitive_dict),
):
return ak.types.NumpyType(
ak.types.numpytype._dtype_to_primitive_dict[array.dtype]
)

The keys of that dict are actual np.dtype objects, but you want the type instances, which are obtained via some_dtype_object.type.

They can be identified as

isinstance(array, type) and issubclass(array, np.generic)

@lgray
Copy link
Contributor Author

lgray commented Oct 26, 2022

Wouldn't this be fixed instead by asking for array.dtype instead of array.dtype.type (which gives the class as opposed to the dtype). Here: https://github.com/scikit-hep/awkward/blob/main/src/awkward/operations/ak_type.py#L99

?

@agoose77
Copy link
Collaborator

agoose77 commented Oct 27, 2022

Thanks for this @lgray. We should be testing the comprehensive dtype here rather than the underlying type.

@agoose77 agoose77 added bug The problem described is something that must be fixed and removed bug (unverified) The problem described would be a bug, but needs to be triaged labels Oct 27, 2022
lgray added a commit to lgray/awkward-1.0 that referenced this issue Oct 27, 2022
jpivarski added a commit that referenced this issue Oct 27, 2022
…rimitive_dict (#1841)

* array.dtype.type will never be in ak.types.numpytype._dtype_to_primitive_dict

* add in test for #1840 fixed by #1841

* style: pre-commit fixes

* Handle np.dtype and NumPy raw types.

* fix: support datetime64

* refactor: use existing type-check code

* fix: catch missing cases from last commit

* fix: support instances of `np.generic`

* Revert the `(bool, np.bool_)` in 0fff03e

* Complete coverage of the types in NumpyType.

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Jim Pivarski <[email protected]>
Co-authored-by: Angus Hollands <[email protected]>
Co-authored-by: Jim Pivarski <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug The problem described is something that must be fixed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants