Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sign error fix in measure.regionprops for orientations of 45 degrees #6836

Conversation

jakeMartin1234
Copy link
Contributor

@jakeMartin1234 jakeMartin1234 commented Mar 15, 2023

Description

Fixed a sign error in the orientation property in measure.regionprops. The preset values for the orientations PI/4 and -PI/4 needed to be switched. This probably dates from switching regionprops over from xy coordinates to rc coordinates.

Closes #6815

Checklist

  • Docstrings for all functions
  • Gallery example in ./doc/examples (new features only)
  • Benchmark in ./benchmarks, if your changes aren't covered by an
    existing benchmark
  • Unit tests
  • Clean style in the spirit of PEP8
  • Descriptive commit messages (see below)

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.
  • There is a bot to help automate backporting a PR to an older branch. For
    example, to backport to v0.19.x after merging, add the following in a PR
    comment: @meeseeksdev backport to v0.19.x
  • To run benchmarks on a PR, add the run-benchmark label. To rerun, the label
    can be removed and then added again. The benchmark output can be checked in
    the "Actions" tab.

@jakeMartin1234 jakeMartin1234 changed the title Sign error in measure.regionprops for orientations of 45 degrees Sign error fix in measure.regionprops for orientations of 45 degrees Mar 15, 2023
@decorouz
Copy link
Contributor

Nice work man. I was about to create a PR for same.

@jni
Copy link
Member

jni commented Mar 16, 2023

thanks @jakeMartin1234! It looks like a test needs updating:

_______________________________ test_orientation _______________________________

    def test_orientation():
        orient = regionprops(SAMPLE)[0].orientation
        # determined with MATLAB
        target_orient = -1.4663278802756865
        assert_almost_equal(orient, target_orient)

        orient = regionprops(SAMPLE, spacing=(2, 2))[0].orientation
        assert_almost_equal(orient, target_orient)

        # test diagonal regions
        diag = np.eye(10, dtype=int)
        orient_diag = regionprops(diag)[0].orientation
>       assert_almost_equal(orient_diag, -math.pi / 4)

/opt/hostedtoolcache/Python/3.11.2/x64/lib/python3.11/site-packages/skimage/measure/tests/test_regionprops.py:648:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

args = (0.7853981633974483, -0.7853981633974483), kwds = {}

    @wraps(func)
    def inner(*args, **kwds):
        with self._recreate_cm():
>           return func(*args, **kwds)
E           AssertionError:
E           Arrays are not almost equal to 7 decimals
E            ACTUAL: 0.7853981633974483
E            DESIRED: -0.7853981633974483

I'd also like it if you could add an extra test: copy image over from #6815, then do something like:

def test_orientation_continuity():
    props = regionprops(image)
    orientations = [prop.orientation for prop in props]
    np.testing.assert_allclose(orientations, orientations[1], rtol=0, atol=0.08)

@jakeMartin1234
Copy link
Contributor Author

@jni I just updated the test. Are you saying that I should copy over the image and store it in the repository then load it into the python file or copy over the arrays that that represent the images in #6815? If I need to store the image in the repository, where should I store it? Would be great if you could let me know so that I can add the test to my branch.

@jni
Copy link
Member

jni commented Mar 16, 2023

You can just copy the code from the issue into the test function:

def test_orientation_continuity():
    # nearly diagonal array
    arr1 = np.array([[0, 0, 1, 1],[0, 0, 1, 0],[0, 1, 0, 0],[1, 0, 0, 0]])
    # diagonal array 
    arr2 = np.array([[0, 0, 0, 2],[0, 0, 2, 0],[0, 2, 0, 0],[2, 0, 0, 0]])
    # nearly diagonal array 
    arr3 = np.array([[0, 0, 0, 3],[0, 0, 3, 3],[0, 3, 0, 0],[3, 0, 0, 0]])
    image = np.hstack((arr1,arr2,arr3))
    props = regionprops(image)
    orientations = [prop.orientation for prop in props]
    np.testing.assert_allclose(orientations, orientations[1], rtol=0, atol=0.08)

@jni
Copy link
Member

jni commented Mar 16, 2023

Remember to push any additional commits to your branch so we can see them here! 😅

@jakeMartin1234
Copy link
Contributor Author

Hi @jni, I just pushed the new changes to the test file. I added a few more checks in the test_orientation_continuity(). Let me know if you think anything else needs changing.

Copy link
Member

@jni jni left a comment

Choose a reason for hiding this comment

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

Awesome! 😍

@jarrodmillman jarrodmillman added this to the 0.21 milestone Mar 17, 2023
@jarrodmillman jarrodmillman merged commit 6db5c5c into scikit-image:main Mar 17, 2023
decorouz pushed a commit to decorouz/scikit-image that referenced this pull request Mar 26, 2023
…cikit-image#6836)

* fixed sign error for PI/4 and -PI/4

* fixed orientation test and added continuity test
@lagru lagru added the 🩹 type: Bug fix Fixes unexpected or incorrect behavior label May 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🩹 type: Bug fix Fixes unexpected or incorrect behavior
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sign error in measure.regionprops for orientations of 45 degrees
5 participants