Repository navigation
Conversation
if preserve_range is True and the image is already float, it is no more converted to double
Adding float32 support to _warp_fast caused test_iradon_bias_circular_phantom to fail. In this test image.dtype is bool.
hmaarrfk
left a comment
There was a problem hiding this comment.
Good cythonizing. I know this isn't easy but it looks on the right track. The test looks thorough too.
Maybe this is what I needed in my previous attempt to speed up wrap for different data tyoes.
| cdef floating[:, ::1] img = np.ascontiguousarray(image) | ||
| cdef floating[:, ::1] M = np.ascontiguousarray(H) | ||
|
|
||
| if floating is float: |
There was a problem hiding this comment.
Is float ambiguous here? I'm concerned there is a difference between python floats and c floats.
There was a problem hiding this comment.
In Cython context, I think that float belongs to cython.float. So I suppose that it is not ambiguous...
There was a problem hiding this comment.
I took it from cython doc in Type Checking Specializations section ;-).
There was a problem hiding this comment.
The fact that you used the word "think" and "not ambiguous" in the same sentence to me makes it ambiguous.
The challenge comes when having code like this:
float clearly means different things when used in python+numpy, or in cython.
I would just replace float with np.float32_t, making it explicit that you mean float32.
There was a problem hiding this comment.
OK, I was thinking that you where talking about computer ambiguity, not understanding ambiguity. Sorry for my poor English speaking/understanding.
On python side, the ambiguity is clearly there, but in cython context I don't think so, the whole test block is explicit:
if floating is float:
dtype = np.float32
else:
dtype = np.doubleWe can make it even more explicit using:
if floating is float:
dtype = np.float32
elif floating is double:
dtype = np.double
else:
raise TypeError("Only float32 and float64 arrays are supported.")There was a problem hiding this comment.
The last part of my answer is wrong, since floating can only be float or double. Sorry
|
|
||
| def convert_to_float(image, preserve_range): | ||
| """Convert input image to double image with the appropriate range. | ||
| """Convert input image to float image with the appropriate range. |
There was a problem hiding this comment.
This needs to say something like:
Floating point types will maintain their precision, while integer types will be converted to double.
There was a problem hiding this comment.
This also true for al the functions defined in _warp_cy.pyx and for radon. It is in my TODO list ;-)
There was a problem hiding this comment.
Sorry, I am not awake yet :-)
I will also fix the docstrings for radon and the function in _warp_cy.
There was a problem hiding this comment.
According to my comment, since not all the floating point precision are supported, I added a Notes section in the docstring specifying this: "Input images with float32 data type are not upcasted."
| """ | ||
| if preserve_range: | ||
| image = image.astype(np.double) | ||
| if image.dtype.char not in 'df': |
There was a problem hiding this comment.
| if image.dtype.char not in 'df': | |
| if not np.issubdtype(image.dtype, np.floating): |
>>> import numpy as np
>>> np.issubdtype(np.float32, np.floating)
True
>>> np.issubdtype(np.float64, np.floating)
True
>>> np.issubdtype(np.float16, np.floating)
True
>>> np.issubdtype(np.uint8, np.floating)
FalseThere was a problem hiding this comment.
That's definitely more readable ;-). I excluded float16 and float128 because I though that these types are not supported by skimage.
There was a problem hiding this comment.
This will cause a bug since float16 is not supported by cython.floating (only float and double) :-(
>>> a = np.ones((5, 5), dtype=np.float16)
>>> resize(a, (10, 10))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "skimage/transform/_warps_cy.pyx", line 70, in skimage.transform._warps_cy._warp_fast
def _warp_fast(floating[:, :] image, floating[:, :] H, output_shape=None,
ValueError: Does not understand character buffer dtype format string ('e')- We can use
but I am not sure that it is more readable...
if image.dtype not in [float, np.float, np.double, np.float32, np.float64]:
- The other option is to keep the original test (
if image.dtype.char not in 'df') and to add a comment in the code.
What do you think?
There was a problem hiding this comment.
Just ignore float16, the whole library is broken with it.
There was a problem hiding this comment.
All right, I will have to ignore np.float128 also:
>>> a = ones((5, 5), dtype=np.float128)
>>> resize(a, (10, 10))
ValueError: Buffer dtype mismatch, expected 'double' but got 'long double'This is because cython.floating only supports single and double precision floating points.
Do you prefer if not (np.issubdtype(image.dtype, np.floating) or image.dtype in [np.float16, np.float128]) instead of if image.dtype.char not in 'df'?
There was a problem hiding this comment.
I once wrote a fused numerics file. You can add things there.
That said, neither of those two datatypes are efficient in hardware, nor are they well tested.
If you want to, you can add to the fused numerics file maybe. Not exactly sure what will happen to the rest of the library.
There was a problem hiding this comment.
Honestly, you could just add a comment. That said, you could also add a Todo, and raise an issue that float16 and float128 have yet undefined behaviours in the img_as routines.
There was a problem hiding this comment.
I already tried some explorations with skimage._shared.fused_numerics.np_floats, but numpy doesn't declare float16_t and float128_t for cimport :-(
There was a problem hiding this comment.
yeah, float16 and float128 are pretty badly supported in general. Adding their definitions to the cimport could be added as an issue upstream.
| x = np.zeros((5, 5)) | ||
| x_f32 = x.astype(np.float32) | ||
| x_u8 = x.astype(np.uint8) | ||
| x_b = x.astype(bool) |
There was a problem hiding this comment.
I'm not sure we care about supporting boolean data types. @jni?
There was a problem hiding this comment.
Eh. If it works, why not keep it working?
| warped = _warp_fast(image, matrix, | ||
| output_shape=output_shape, | ||
| order=order, mode=mode, cval=cval) | ||
| warped = _warp_fast[ctype](image, matrix, |
There was a problem hiding this comment.
this won't select things automatially? Maybe that was my mistake....
There was a problem hiding this comment.
Nop, it wasn't working without this specialization...
hmaarrfk
left a comment
There was a problem hiding this comment.
Just a few small comments. Looks good otherwise!
| if matrix is not None: | ||
| matrix = matrix.astype(np.double) | ||
| matrix = matrix.astype(image.dtype) | ||
| ctype = 'float' if image.dtype.char == 'f' else 'double' |
There was a problem hiding this comment.
I would really like it if you found a way to call these float32 and float64. Mostly float is ambiguous, double not so much.
There was a problem hiding this comment.
I was happy to make it work without the explicit import here to cython: I did not knew that passing strings for specialization works :-D
Well, I will give it a try.
There was a problem hiding this comment.
Infortunatly, this doesn't work :-( This is because _warp_cy.__signatures__.keys() only contains "float" and "double".
I added a comment trying to alleviate the ambiguity:
# Manage _warp_fast specialization according to input data
# type floating point precision (single or double)
ctype = 'float' if image.dtype.char == 'f' else 'double'There was a problem hiding this comment.
It needs a sentence like:
Contrary to Python, Cython
floatfollows the meaning offloatin C, and is equivalent tofloat32.
|
Thank you @hmaarrfk for your review, I tried in my last push to address all your concerns. |
|
I think what would make me happy is that if you included this sentence
Whenver you used |
|
Using ctype = 'float' if image.dtype.char == 'f' else 'double'becomes ctype = 'float32_t' if image.dtype == np.float32 else 'float64_t'and if floating is float:
dtype = np.float32
else:
dtype = np.doublebecomes if np_floats is cnp.float32_t:
dtype = np.float32
else:
dtype = np.float64I think that there is no need then for more comments in the code. Thx @hmaarrfk ;-) |
|
The cython stuff seems fine for me. I think the radon fix should be more fleshed out, and ideally left to a different PR if it can be. |
|
You know, come to think of it, I think I had opened an issue about this in
the past. Will have to find it. Please leave this out for now. if you can.
…On Fri, Aug 30, 2019 at 9:44 AM Riadh Fezzani ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In skimage/transform/radon_transform.py
<#4128 (comment)>
:
> @@ -62,6 +63,8 @@ def radon(image, theta=None, circle=True):
if theta is None:
theta = np.arange(180)
+ image = convert_to_float(image, preserve_range=True)
I though about leaving preserve_range to its default value, but this
would potentially break users code, but well, I can open an issue about the
fact that the arrays are implicitly casted to double by _warp_cy in radon
;-)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#4128?email_source=notifications&email_token=AAAV7GHP7JQHNMN7OGPC26DQHEP2ZA5CNFSM4ISG5NN2YY3PNVWWK3TUL52HS4DFWFIHK3DMKJSXC5LFON2FEZLWNFSXPKTDN5WW2ZLOORPWSZGOCDHZSEY#discussion_r319518318>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAAV7GHDA3AWE7MNP5467FLQHEP2ZANCNFSM4ISG5NNQ>
.
|
|
Isn't it #3799? |
|
Thats the one! Although that was a bug, I think that behaviour definitely needs to be deprecated too. If you try to add the fix for that in here, then the review time will be multiplicative, instead of additive, and will delay this getting merged. |
|
Fixing the |
So what do you recommend? Should we fix #4130 directly in this PR and close both related issues? |
|
Just fix the issue of rescale.
…On Fri, Aug 30, 2019, 10:46 AM Riadh Fezzani ***@***.***> wrote:
Thats the one!
Although that was a bug, I think that behaviour definitely needs to be
deprecated too.
If you try to add the fix for that in here, then the review time will be
multiplicative, instead of additive, and will delay this getting merged.
So what do you recommend? Should we fix #4130
<#4130> directly in
this PR and close both related issues?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#4128?email_source=notifications&email_token=AAAV7GG6MMJ5RNIYVYHSGCTQHEXF5A5CNFSM4ISG5NN2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD5R3YKI#issuecomment-526629929>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAAV7GEZFC4ZDXAJNYQYU6TQHEXF5ANCNFSM4ISG5NNQ>
.
|
|
OK, but this will cause |
|
So you need the preserve range to be set to true? If so, that is fine, add a note #3799 |
Not necessarly, only the failing test has to be fixed ;-) |
…into Fix_resize_output_detype_inconsistency
…into Fix_resize_output_detype_inconsistency
|
As expected, the CI tests pass again after merging #4136. 🎉 |
Co-Authored-By: Juan Nunez-Iglesias <[email protected]>
Co-Authored-By: Juan Nunez-Iglesias <[email protected]>
|
Thank you for the review @jni ;-) |
|
@stefanv any chance you can review this? I think it's a pretty major improvement, which would be nice to get into 0.16. |
|
This looks good to me, merging. If there are any other minor updates, @hmaarrfk, feel free to add a refinement PR. |

Description
This PR fixes #4108 and #4190.
Checklist
./doc/examples(new features only)./benchmarks, if your changes aren't covered by anexisting benchmark
For reviewers
later.
__init__.py.doc/release/release_dev.rst.@meeseeksdev backport to v0.14.x