Skip to content

Refactor radon transform module - #4136

Merged
soupault merged 15 commits into
scikit-image:masterfrom
rfezzani:Refactor_radon_transform
Sep 12, 2019
Merged

soupault merged 15 commits into
scikit-image:masterfrom
rfezzani:Refactor_radon_transform

Conversation

@rfezzani

@rfezzani rfezzani commented Sep 4, 2019

Copy link
Copy Markdown
Member

Description

The major changes introduced by this PR are:

  • use warp instead of _warp_fast to ease the maintenance when an enhancement/fix is introduced to _warp_cy (_warp_fast is now called only by the warp function),
  • remove unnecessary inner functions (build_rotation in radon and angle_distance in order_angles_golden_ratio),
  • define _get_fourier_filter function to improve iradon readability,
  • replace double dot product by a direct array definition for the creation of the rotation matrix in radon,
  • simplify some if statements,
  • use scipy.ndimage.measurements.find_objects to find reconstruction circle area instead of a hand made loop,
  • use functools.partial to make np.interp call consistent with scipy.interpolate.interp1d call in iradon

This results in some lines drop and no API change is introduced.

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 the 🔧 type: Maintenance Refactoring and maintenance of internals label Sep 5, 2019
@sciunto sciunto added this to the 0.17 milestone Sep 5, 2019
@sciunto
sciunto requested a review from emmanuelle September 5, 2019 05:37

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

Great work! Thanks for this contribution. I made minor comments only, the result is much easier to read 👍

Can you check that this does not introduce any performance regression please? (likely not)

Comment thread skimage/transform/radon_transform.py Resolved
Comment thread skimage/transform/radon_transform.py Resolved
Comment thread skimage/transform/radon_transform.py Resolved
Comment thread skimage/transform/radon_transform.py Resolved
Comment thread skimage/transform/radon_transform.py Resolved
Comment thread skimage/transform/radon_transform.py Resolved
Comment thread skimage/transform/radon_transform.py Resolved
Comment thread skimage/transform/radon_transform.py Resolved
@rfezzani

rfezzani commented Sep 5, 2019

Copy link
Copy Markdown
Member Author

Thank you @sciunto for your kind comments and for your review. I already made some tests and I observed no performance drop:

Setup

In [1]: from skimage.transform.radon_transform import order_angles_golden_ratio, radon, iradon          

In [2]: from skimage.data import shepp_logan_phantom                                                    

In [3]: image = shepp_logan_phantom()                                                                   

In [4]: theta = np.linspace(0., 180., max(image.shape), endpoint=False)                                 

In [5]: sinogram = radon(image, theta=theta, circle=True)  

Actual version

In [6]: %timeit -n10 -r10 iradon(sinogram, theta=theta, circle=True)                                    
572 ms ± 9.51 ms per loop (mean ± std. dev. of 10 runs, 10 loops each)

In [7]: %timeit -n10 -r10 radon(image, theta=theta, circle=True)                                        
1.21 s ± 4.71 ms per loop (mean ± std. dev. of 10 runs, 10 loops each)

In [8]: %timeit -n10 -r10 [x for x in order_angles_golden_ratio(theta)]                                 
6.96 ms ± 312 µs per loop (mean ± std. dev. of 10 runs, 10 loops each)

Refactored version

In [6]: %timeit -n10 -r10 iradon(sinogram, theta=theta, circle=True)                                    
572 ms ± 769 µs per loop (mean ± std. dev. of 10 runs, 10 loops each)

In [7]: %timeit -n10 -r10 radon(image, theta=theta, circle=True)                                        
1.2 s ± 3.18 ms per loop (mean ± std. dev. of 10 runs, 10 loops each)

In [8]: %timeit -n10 -r10 [x for x in order_angles_golden_ratio(theta)]                                 
6.66 ms ± 285 µs per loop (mean ± std. dev. of 10 runs, 10 loops each)

Comment thread skimage/transform/radon_transform.py Resolved
@sciunto

sciunto commented Sep 5, 2019

Copy link
Copy Markdown
Member

Perfect. we just need a second approval by another core dev.

@rfezzani

rfezzani commented Sep 5, 2019 •

Copy link
Copy Markdown
Member Author

Thank you @sciunto, my last commit is just about replacing np.pi * np.linspace(0, 1, size, endpoint=False) by np.linspace(0, np.pi, size, endpoint=False).

@rfezzani

rfezzani commented Sep 5, 2019 •

Copy link
Copy Markdown
Member Author

Given the golden ratio γ. It's inverse g = 1/ γ verify g² = (1 - g) (See the appendix in [1]).
The actual implementation make use of (1-g) with g computed as (np.sqrt(5) - 1) / 2.
I used scipy.constants.golden_ratio to simplify the expression and replaced (1-g) = 1 - (np.sqrt(5) - 1) / 2 by g² = 1/golden_ratio**2

@hmaarrfk

hmaarrfk commented Sep 5, 2019

Copy link
Copy Markdown
Member

is this going to conflict with: #4131

which one do you want us to look at first?

@rfezzani

rfezzani commented Sep 5, 2019 •

Copy link
Copy Markdown
Member Author

There will be merge conflicts, but fixing is not difficult. I tested both directions (#4131 in #4136 and #4136 in #4131), both are easy. So I let you decide about the priorities ;-)

@rfezzani

rfezzani commented Sep 5, 2019

Copy link
Copy Markdown
Member Author

This PR makes #4128 pass the CI tests, while #4131 not.
So considering this PR first allows both #4131 and #4128 to be eligible for merge.

@emmanuelle emmanuelle self-assigned this Sep 9, 2019
Comment thread skimage/transform/radon_transform.py Outdated
else:
slices.append(slice(None))
slices = tuple(slices)
slices = find_objects((~outside_reconstruction_circle).astype(int))[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.

calling find_objects is more elegant, but it takes a few ms for a 2000x2000 image. This is not a big deal since the whole function takes several seconds to complete, but if someday we accelerate massively this function (the loop on angles could easily be parallelized) it could be significant. Even then it would only represent around 10% of the execution time so I'm ok with this change, I just wanted to raise this point in case somebody wants to 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.

Seems silly to analyze the data instead of the metadata when looking for a slice....

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 with you, the use of find_object requires also negating outside_reconstruction_circle and casing it to int, which is a lot of processing to find the desired slice...
My last commit removes the call to find_object.

@emmanuelle

Copy link
Copy Markdown
Member

I just left one comment, but it looks good to me so I'll merge tomorrow if there's nothing to add. Note that it would be great to parallelize the loop on angles (if it is the bottleneck which I did not check, but it's likely).

@rfezzani

Copy link
Copy Markdown
Member Author

Thank you @emmanuelle for your feedback. I also think that the radon function could be optimized by parallelizing the loop on angles but this will be at the cost of a larger memory use... I will try this in an other PR once this one is merged ;-). Thank you again.

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

One minor suggestion, otherwise 👍 . Thanks for the PR, it is a good work!

if not output_size:

filter_types = ('ramp', 'shepp-logan', 'cosine', 'hamming', 'hann', None)
if filter not in filter_types:

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.

Oh, wow, this is a terrible name for an argument. I think we should fix this and the similar cases all across the library. In a different PR, of course.

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.

oh yes thank you for catching this @soupault

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 fully agree with you, but this is the original argument name!
I also opened an equivalent issue in scipy where input is used as argument name in many functions. This issue was closed and will not be fixed for retro-compatibility reasons :-(
I proposed this gist as a solution for this problem. I don't know if skimage accepts such a solution...

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 can't blame Ralf, that is understandable from the core developer perspective 😄. We have had a brief discussion with the similar outcome recently - #2838 (review), but I still believe that we should change that. The proper approach is to deprecate all the occurencies of such cases in the public API (practically, similar to your gist). I have raised a ticket for this - #4152, feel free to jump into the discussion.

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.

Of course, this is understandable, but a little bit sad 😄 I would be happy to help on this ;-)

Comment thread skimage/transform/radon_transform.py Outdated
[-np.sin(T), np.cos(T), 0],
for i, T in enumerate(np.deg2rad(theta)):
cos_T, sin_T = np.cos(T), np.sin(T)
R = np.array([[cos_T, sin_T, -center * (cos_T + sin_T - 1)],

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.

Please, use lowercase for the variable names.

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. Thank you @soupault for your review.

@soupault

Copy link
Copy Markdown
Member

@rfezzani Looking good, thanks for the contribution!

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.

5 participants