Skip to content

Deprecate undefined data type management in radon function - #4131

Merged
jni merged 13 commits into
scikit-image:masterfrom
rfezzani:Fix_radon_data_type_management
Sep 17, 2019
Merged

jni merged 13 commits into
scikit-image:masterfrom
rfezzani:Fix_radon_data_type_management

Conversation

@rfezzani

Copy link
Copy Markdown
Member

Description

Fixes #4130 and #3799.

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

@rfezzani rfezzani changed the title Fix data type management to radon function Fix data type management in radon function Aug 30, 2019
if theta is None:
theta = np.arange(180)

image = convert_to_float(image, preserve_range)

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 think I would feel better with a deprecation cycle. This kind of unit change is REALLY annoying pin down, and a warning can go a long way.

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.

This deprecation warning should only be raised when a non float array is provided, right? But this is strange since the doc says that only float array is supported.

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.

The challenges encountered here are why this deserves its own PR. I would identify the cases where the behavior is changed, and warn only on those.

Finally, update the documentation specifying the support for uint8 if you want. If you don't, keep it as is.

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.

Yes, I agree, the addition of a new parameter can't be harmless. I will write a test to assert the multiple data types support.
The use of convert_to_float allows to use any data type, so I think that it is enough to simply remove the restriction to float in the doc.

@pep8speaks

pep8speaks commented Sep 1, 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-12 11:31:22 UTC

@hmaarrfk hmaarrfk added this to the 0.16 milestone Sep 1, 2019
@rfezzani

rfezzani commented Sep 1, 2019

Copy link
Copy Markdown
Member Author

When float32 support will be added to _warp_fast, we will have to ensure that build_rotation function defined in radon return a matrix with the same dtype as padded_image, otherwise an error will be raised.
Should we create a specific issue to Add float32 support to _warp_fast and make the changes there? Any function calling _warp_fast is potentially impacted.

Comment thread skimage/transform/radon_transform.py Outdated


def radon(image, theta=None, circle=True):
def radon(image, theta=None, circle=True, preserve_range=False):

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.

To keep the old behavior going, this should be set to None with a warning for users that it will change in a future version.

@jni, this function's documentation only included support for floating point types, but actually accepted integer types. Should we follow the general deprecation procedure?

@rfezzani rfezzani Sep 2, 2019 •

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.

Actually, setting preserve_range to True keeps the actual behavior unchanged since the actual version of convert_to_float directly convert any array to double when preserve_range is True.
When preserve_range is False, img_as_float is called, and in this case, non float arrays are rescaled betweeen 0 and 1. The backward compatibility may be broken here.
Should we raise a deprecation warning when preserve_range is False and dtype is not floating?

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.

Right, so I was suggesting we go through a deprecation cycle.

  1. Set preserve_range=None by default.
  2. If not set, and we have integer types, raise a warning, and set it to True
  3. In 0.18, make it False by default.

Comment thread skimage/transform/radon_transform.py Outdated
theta = np.arange(180)

if preserve_range is None and np.issubdtype(image.dtype, np.integer):
warn('Image dtype is not float. To keep old behavior, '

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.

This needs to be fleshed out a little more.

Imge dtype is not float. By default radon will assume you want to preserve the range of your image (preserve_range=True). In scikit-image 0.18 this behavior will change to preserve_range=False. To avoid this warning, explictiely specify the preserve_range parameter.

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.

Done

Comment thread skimage/transform/radon_transform.py Outdated


def radon(image, theta=None, circle=True):
def radon(image, theta=None, circle=True, preserve_range=None):

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.

Suggested change
def radon(image, theta=None, circle=True, preserve_range=None):
def radon(image, theta=None, circle=True, *, preserve_range=None):

These kind of "behavior" parameters should definitely be made keyword only (something that wasn't possible in python 2.7)

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.

Done

@rfezzani

rfezzani commented Sep 2, 2019 •

Copy link
Copy Markdown
Member Author

Should we create a specific issue to Add float32 support to _warp_fast and make the changes there? Any function calling _warp_fast is potentially impacted.

_warp_fast is only called by resize and radon. What about making a two step resolution of #4128:

  1. Adding float32 support to _warp_fast,
  2. Fix convert_to_float to not upcast float32 arrays.

This way #4128 issue and its equivalent for radon will be solved. What do you think @hmaarrfk ?

Comment thread skimage/transform/radon_transform.py Outdated
'(preserve_range=True). In scikit-image 0.18 this behavior will'
'change to preserve_range=False. To avoid this warning,'
'explictiely specify the preserve_range parameter.',
DeprecationWarning)

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 like DeprecationWarning, it tends to get hidden if it isn't called directly by __main__, and users won't get the warning. Let it be a standard warning, or FutureWarning.

Suggested change
DeprecationWarning)
stacklevel=2)

For us, Deperecations will actually break things for users in 2 versions (1 year).

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.

Done

Comment thread skimage/transform/radon_transform.py Outdated
if preserve_range is None and np.issubdtype(image.dtype, np.integer):
warn('Image dtype is not float. By default radon will assume'
'you want to preserve the range of your image'
'(preserve_range=True). In scikit-image 0.18 this behavior will'

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.

you need spaces just before each closing quote.

@hmaarrfk hmaarrfk changed the title Fix data type management in radon function Deprecate undefined data type management in radon function Sep 2, 2019
Comment thread skimage/transform/radon_transform.py Outdated
T = np.deg2rad(theta)
R = np.array([[np.cos(T), np.sin(T), 0],
[-np.sin(T), np.cos(T), 0],
c, s = np.cos(T), np.sin(T)

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.

Suggested change
c, s = np.cos(T), np.sin(T)
cos_T, sin_T = np.cos(T), np.sin(T)

Comment thread skimage/transform/radon_transform.py Outdated
R = np.array([[np.cos(T), np.sin(T), 0],
[-np.sin(T), np.cos(T), 0],
c, s = np.cos(T), np.sin(T)
R = np.array([[c, s, 0],

@hmaarrfk hmaarrfk Sep 2, 2019 •

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.

Suggested change
R = np.array([[c, s, 0],
R = np.array([[ cos_T, sin_T, 0],

Comment thread skimage/transform/radon_transform.py Outdated
[-np.sin(T), np.cos(T), 0],
c, s = np.cos(T), np.sin(T)
R = np.array([[c, s, 0],
[-s, c, 0],

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.

Suggested change
[-s, c, 0],
[-sin_T, cos_T, 0],



@testing.parametrize("shape, circle",
@testing.parametrize("shape, circle, dtype, preserve_range",

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.

this is extremely confusing. You can just have multiple parametrize decorators which becomes much more readable. One decorator per parameter. itertools product is pretty obscur for science programming.

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 agree, just wanted to keep the same test structure as before. Can I also rename the parameter lists 😄 ?

def test_radon_center_rectangular(shape):
check_radon_center(shape, False)
# @testing.parametrize("shape", rectangular_shapes)
@testing.parametrize("shape, dtype, preserve_range",

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.

same thing for this use of itertools product.


rectangular_shapes = [(32, 16), (33, 17)]
dtype_for_test_radon_center = [np.float64, np.float32, np.uint8, bool]
preserve_range_for_test_radon_center = [True, False]

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.

once you stop using itertools product, these definitions are likely useless too.

@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.

A few nitpicks regarding the design of the tests. I know you were trying to follow the old style, but it is clear that that style isn't manageable. Whatever happens, I think it is ready for somebody else to take a look at.

@hmaarrfk hmaarrfk mentioned this pull request Sep 5, 2019
9 tasks
@rfezzani

Copy link
Copy Markdown
Member Author

I resolved the conflicts with #4136, everything is fine now ;-)

@jni
jni merged commit de42b4c into scikit-image:master Sep 17, 2019
@jni

jni commented Sep 17, 2019

Copy link
Copy Markdown
Member

Great stuff! Thanks @rfezzani (again!) for your work and thanks @hmaarrfk for the detailed reviews, which made my job very easy. #pushthegreenbutton =P

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

radon function accepts any data type and implicitly converts arrays to float

5 participants