Skip to content

Fix conversion to float32 dtype - #4193

Merged
hmaarrfk merged 14 commits into
scikit-image:masterfrom
rfezzani:Fix_convert_upcasting
Oct 2, 2019
Merged

hmaarrfk merged 14 commits into
scikit-image:masterfrom
rfezzani:Fix_convert_upcasting

Conversation

@rfezzani

Copy link
Copy Markdown
Member

Description

Closes #4189.

This PR consists in:

  • refactoring skimage.util.convert: defining annex functions (_scale, _dtype_bits, _dtype_itemsize) outside the body of the convert function to make it more readable,
  • refactoring of _dtype_bits: removing the definition of compare function,
  • using np.obj2sctype(dtype) instead of type_out to fix the conversion to float32 whatever is numpy version,
  • adding tests for conversion from any combination of floating data type definition (float32, np.float, float, etc...)
  • adding tests for conversion from a subclass of the desired output datatype (check for no regression).

Checklist

For reviewers

  • Check that the PR title is short, concise, and will make sense 1 year
    later.
  • Check that new functions are imported in corresponding __init__.py.
  • Check that new features, API changes, and deprecations are mentioned in
    doc/release/release_dev.rst.
  • Consider backporting the PR with @meeseeksdev backport to v0.14.x

@sciunto sciunto added this to the 0.17 milestone Sep 27, 2019
@sciunto sciunto added 🔧 type: Maintenance Refactoring and maintenance of internals 🧐 Needs review labels Sep 27, 2019

@hmaarrfk hmaarrfk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Presumably theses tests pass when we increase the version to 1.14 for bumpy? If so. Lets go with that.

The added tests are nice, the change to the code is mildly interesting.

@sciunto

sciunto commented Sep 29, 2019

Copy link
Copy Markdown
Member

I'm +1 with @hmaarrfk : bump numpy + keep the tests.

@rfezzani

Copy link
Copy Markdown
Member Author

I removed inner functions because they are not required by design and they make convert function body length from 262 lines to 173. It made my debugging much easier!
I would also refer to @jni comment last item concerning functions length...
But well, if you all agree and request reverting this refactoring, I will do it ;-)

@sciunto

sciunto commented Sep 30, 2019

Copy link
Copy Markdown
Member

It's not really the fact you separated the function but rather the shortcuts such as compare_bits_to = bits.__le__ if kind == 'u' else bits.__lt__ that make me less happy with the modifications.

@rfezzani

Copy link
Copy Markdown
Member Author

I also wanted to remove the compare inner function... I confess that I don't like them if they are not required 😄...
I understand that it is may be less readable, but you may agree that this line is not much tricky then the rest of the function 😄

Comment thread skimage/util/dtype.py Outdated

"""

compare_bits_to = bits.__le__ if kind == 'u' else bits.__lt__

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I agree with @sciunto, undo this particular optimisation, as it is much less readable than the inner compare function. Something preferable would be to just be explicit in the loop:

s = next(i for i in (itemsize,) + (2, 4, 8)
         if bits < i*8 or bits == i*8 and kind == 'u')

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I like this ;-) well done @jni

@jni jni left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@rfezzani I agree with the bulk of your refactor but also with @sciunto's comment. I've suggested a form that I think is clearer than both the original and your refactor. =) Other than that, add the NumPy bump and we'll be golden! =)

@pep8speaks

pep8speaks commented Sep 30, 2019 •

Copy link
Copy Markdown

Hello @rfezzani! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2019-09-30 14:18:53 UTC

@sciunto sciunto left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Awesome guys. I approve (although we need to wait for Travis)

@rfezzani

Copy link
Copy Markdown
Member Author

Thx @sciunto, I am still stuck with Travis failures :-( Even using # doctest: +NORMALIZE_WHITESPACE doesn't solve the problem 😞

@sciunto

sciunto commented Sep 30, 2019

Copy link
Copy Markdown
Member

Try numpy 0.14.1. Looks like there is a fix backported in this version that might be related to this.

@sciunto

sciunto commented Sep 30, 2019

Copy link
Copy Markdown
Member

It works.

Comment thread skimage/measure/_moments.py Outdated
>>> coords2 = np.concatenate((coords, [[17, 17]]), axis=0)
>>> np.round(moments_coords_central(coords2), 2)
>>> np.round(moments_coords_central(coords2),
... decimals=2) # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think ELLIPSIS is need. The output does not contain ...

@jni

jni commented Oct 2, 2019

Copy link
Copy Markdown
Member

@hmaarrfk this is ready for you to pull the trigger! =) Thanks all!

@hmaarrfk

hmaarrfk commented Oct 2, 2019

Copy link
Copy Markdown
Member

didn't know that my change request was blocking. I rarely mean to have blocking requests..

@hmaarrfk
hmaarrfk merged commit 065cd12 into scikit-image:master Oct 2, 2019
@jni

jni commented Oct 2, 2019

Copy link
Copy Markdown
Member

@hmaarrfk typically when I have requests that I don't want to block, I use "comment" rather than "request changes". Otherwise the big red x remains. I can "dismiss" it, but that feels, well, dismissive. =D

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

Labels

🧐 Needs review 🔧 type: Maintenance Refactoring and maintenance of internals

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

5 participants