Skip to content

BUG: dtype(<generic>).type has different output than deprecated obj2sctype? #25580

Open

Description

@lagru

Describe the issue:

I'm currently working on adapting skimage for NumPy 2.0 during which I was told to replace our usages of the former np.core.numerictypes.obj2sctype(x) with the equivalent np.dtype(x).type. However, it seems that if x is an generic dtype (I think that's what they are called) the result differs. The example below shows the difference on the current nightly wheel of NumPy.

Reproduce the code example:

import numpy as np
from numpy._core.numerictypes import obj2sctype
assert obj2sctype(np.floating), np.dtype(np.floating).type

Error message:

(numpy.floating, numpy.float64)

Python and NumPy Versions:

2.0.0.dev0+git20240109.6e3b923
3.12.1 | packaged by conda-forge | (main, Dec 23 2023, 08:03:24) [GCC 12.3.0]

Runtime Environment:

Details

[{'numpy_version': '2.0.0.dev0+git20240109.6e3b923',
  'python': '3.12.1 | packaged by conda-forge | (main, Dec 23 2023, 08:03:24) '
            '[GCC 12.3.0]',
  'uname': uname_result(system='Linux', node='hue', release='6.6.10-arch1-1', version='#1 SMP PREEMPT_DYNAMIC Fri, 05 Jan 2024 16:20:41 +0000', machine='x86_64')},
 {'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
                      'found': ['SSSE3',
                                'SSE41',
                                'POPCNT',
                                'SSE42',
                                'AVX',
                                'F16C',
                                'FMA3',
                                'AVX2'],
                      'not_found': ['AVX512F',
                                    'AVX512CD',
                                    'AVX512_KNL',
                                    'AVX512_KNM',
                                    'AVX512_SKX',
                                    'AVX512_CLX',
                                    'AVX512_CNL',
                                    'AVX512_ICL']}},
 {'architecture': 'Haswell',
  'filepath': '/home/lg/.local/lib/micromamba/envs/skimagedev312/lib/python3.12/site-packages/numpy.libs/libopenblas64_p-r0-0cf96a72.3.23.dev.so',
  'internal_api': 'openblas',
  'num_threads': 8,
  'prefix': 'libopenblas',
  'threading_layer': 'pthreads',
  'user_api': 'blas',
  'version': '0.3.23.dev'}]
None

Context for the issue:

I guess this is kind of an obscure edge case we are hitting in skimage, and I think I can figure out a way around it. But I still wanted to raise the issue in case this was something that was missed. Feel free to close this, if this isn't something to worry about. Though, I'd be curious if there is an alternative in NumPy 2.0 with which the original output can be revoverd.

Slightly related #17325.

Activity

  1. added a commit that references this issue on Jan 12, 2024
  2. seberg commented on Jan 13, 2024

    @seberg
    Member

    Could you link to the context/code? It doesn't work for the abstract scalar types indeed, but then the abstract scalar types rarely do what you want them to do, except for that odd issubdtype I guess...

  3. lagru commented on Jan 13, 2024

    @lagru
    ContributorAuthor

    Sure, have a look at skimage/util/dtype.py#L277. If I replace it with just dtype(...).type a test checking for np.float32 passthrough would fail because dtype(np.floating).type is turned into np.float64.

    Though, it seems that we can get by with just passing dtype to np.issubdtype(...) directly (see this diff). The tests still pass and as far as I can see,np.issubdtype applies dtype(arg2).type automatically if an argument isn't a subclass of generic...

    if not issubclass_(arg2, generic):
    arg2 = dtype(arg2).type

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions