Skip to content

skimage.util.convert not converting to 'float32' with numpy v1.13.3 #4189

Closed

Description

@rfezzani

Description

During my explorations for PR #4161 CI failure, I found that skimage.util.convert doesn't convert double precision images to single precision with numpy v1.13.3 when called with dtype='float32'. Even more strange, it works when dtype=np.float32

Way to reproduce

>>> import numpy as np
>>> from skimage.util.dtype import convert
>>> a = np.random.rand(5, 5)
>>> convert(a, 'float32').dtype
dtype('float64')
>>> convert(a, np.float32).dtype
dtype('float32')
>>> np.__version__
'1.13.3'

This doesn't happen with more recent numpy version:

>>> a = np.random.rand(5, 5)
>>> convert(a, 'float32').dtype
dtype('float32')
>>> convert(a, np.float32).dtype
dtype('float32')
>>> np.__version__
'1.16.4'

Version information

3.6.7 | packaged by conda-forge | (default, Jul  2 2019, 02:18:42) 
[GCC 7.3.0]
Linux-4.15.0-64-generic-x86_64-with-debian-buster-sid
scikit-image version: 0.16.dev0
numpy version: 1.13.3

Activity

  1. jni commented on Sep 26, 2019

    @jni
    Member

    @rfezzani Crazy outcome of your investigation! Can this issue be fixed by internally changing any 'float32's to np.float32?

  2. jni commented on Sep 26, 2019

    @jni
    Member

    "internally" meaning inside the "convert" function

  3. rfezzani commented on Sep 26, 2019

    @rfezzani
    MemberAuthor

    I am checking this ;)

  4. rfezzani commented on Sep 26, 2019

    @rfezzani
    MemberAuthor

    OK, the bug comes from np.issubdtype that was fixed in numpy v1.14:

    Using numpy v1.13, I obtain:

    >>> a = np.random.rand(5)
    >>> a.dtype
    dtype('float64')
    >>> np.issubdtype(a.dtype, 'float32')
    True
    >>> np.issubdtype(a.dtype, np.dtype('float32'))
    True
    >>> np.issubdtype(a.dtype, np.float32)
    False
    >>> np.issubdtype(a.dtype, np.dtype(np.float32))
    True

    There is no obvious fix for convert, but I will investigate more ;-)

  5. hmaarrfk commented on Sep 26, 2019

    @hmaarrfk
    Member

    Numpy 1.13.3 is basically dead. Numpy doesn't recommend supporting older than 1.14. So let's just bump out version number

  6. rfezzani commented on Sep 26, 2019

    @rfezzani
    MemberAuthor

    What about using dtypeobj_in.kind == dtypeobj_out.kind and np.can_cast(dtypeobj_in, dtypeobj_out) instead of np.issubdtype(dtypeobj_in, type_out) in the convert function?
    I can come with a PR for this if you agree with this solution...

  7. jni commented on Sep 27, 2019

    @jni
    Member

    @stefanv thoughts about bumping up NumPy dep?

    @hmaarrfk I thought about that, but checked and NumPy 1.13 is only 2yo, which isn't that long. Having said that, if someone tried to support skimage from 2y ago simultaneously with today's skimage, they would not have fun, so perhaps we should move a bit faster.

  8. stefanv commented on Sep 27, 2019

    @stefanv
    Member
  9. stefanv commented on Sep 27, 2019

    @stefanv
    Member
    On Jan 16, 2019 drop support for Numpy 1.12 (initially released on Jan 15, 2017)
    On Mar 14, 2019 drop support for Python 3.5 (initially released on Sep 13, 2015)
    On Jun 08, 2019 drop support for Numpy 1.13 (initially released on Jun 07, 2017)
    On Jan 07, 2020 drop support for Numpy 1.14 (initially released on Jan 06, 2018)
    On Jun 23, 2020 drop support for Python 3.6 (initially released on Dec 23, 2016)
    On Jul 23, 2020 drop support for Numpy 1.15 (initially released on Jul 23, 2018)
    On Jan 13, 2021 drop support for Numpy 1.16 (initially released on Jan 13, 2019)
    On Jul 26, 2021 drop support for Numpy 1.17 (initially released on Jul 26, 2019)
    On Dec 26, 2021 drop support for Python 3.7 (initially released on Jun 27, 2018)
    
  10. stefanv commented on Sep 27, 2019

    @stefanv
    Member

    By the way, there is code at the bottom of that NEP to calculate this table, so we can automate a check so that we automatically upgrade our NumPy and Python deps.

  11. rfezzani commented on Sep 27, 2019

    @rfezzani
    MemberAuthor

    This bug also affects img_as_float32:

    >>> img_as_float32(rand(5)).dtype
    dtype('float64')
  12. rfezzani commented on Sep 27, 2019

    @rfezzani
    MemberAuthor

    If you don't want to wait for numpy 1.13 drop, the PR #4193 fixes this issue ;-)

  13. hmaarrfk commented on Sep 27, 2019

    @hmaarrfk
    Member

    I thought we were saying the numpy drop is today. And we would support a PR that Drops it. Most of time, unless there is a real big, we don't drop it, but maybe we should just drop regardless. That NEP is new.

  14. rfezzani commented on Sep 27, 2019

    @rfezzani
    MemberAuthor

    Oh, I misread @stefanv comment, I though that the NEP was advising to drop numpy 1.13 on On Jun 08, 2020 😄 .
    By the way, the PR includes also small refactoring of skimage.utils.dtype and some tests that may have avoided this issue.

  15. stefanv commented on Sep 27, 2019

    @stefanv
    Member

    The NEP is likely to be accepted; there have been no objections.

    Go ahead and drop 1.13 support.

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions