Skip to content

evaluation or metrics module - #4025

Merged
sciunto merged 25 commits into
scikit-image:masterfrom
rbavery:evaluation
Jul 21, 2019
Merged

sciunto merged 25 commits into
scikit-image:masterfrom
rbavery:evaluation

Conversation

@rbavery

@rbavery rbavery commented Jul 14, 2019 •

Copy link
Copy Markdown
Contributor

Description

This pull request supersedes #3354 and addresses #2345. It refactors @seanbudd's original PR into a new skimage.evaluate module, moving simple metrics (mse, nrmse, psnr) and segmentation metrics (adapted rand error, variation of information) into evaluate.

Citations for segmentation metrics:

Arganda-Carreras I, Turaga SC, Berger DR, et al. (2015)
           Crowdsourcing the creation of image segmentation algorithms
           for connectomics. Front. Neuroanat. 9:142.
           :DOI:`10.3389/fnana.2015.00142`

Marina Meilă (2007), Comparing clusterings—an information based
           distance,Journal of Multivariate Analysis, Volume 98, Issue 5, Pages
           873-895, ISSN 0047-259X, https://doi.org/10.1016/j.jmva.2006.11.013.

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

@pep8speaks

pep8speaks commented Jul 14, 2019 •

Copy link
Copy Markdown

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

Line 23:31: E127 continuation line over-indented for visual indent

Line 4:40: E127 continuation line over-indented for visual indent
Line 5:40: E127 continuation line over-indented for visual indent

Line 210:21: E126 continuation line over-indented for hanging indent

Line 8:80: E501 line too long (89 > 79 characters)

Comment last updated at 2019-07-20 21:09:31 UTC

Comment thread requirements/docs.txt Resolved
@rbavery

rbavery commented Jul 14, 2019

Copy link
Copy Markdown
Contributor Author

I'm not sure what the python 3.7 build is failing due to this error:

Error: Download failed on Cask 'basictex' with message: Download failed: http://mirror.ctan.org/systems/mac/mactex/mactex-basictex-20190410.pkg
+shell_session_update

I'll look into this later today.

The Python 3.5 build fails are because of a typo, I need to set the minimum version to 3.0.1 not 3.1.0

@hmaarrfk

Copy link
Copy Markdown
Member

Is the build fixed now? Sometimes mirrors fail. There was a retry flag on most pip stuff. Not sure about other dependencies. THe general rule is that at least 1 build will fail everytime everythign is correct.

@JDWarner JDWarner left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you for the contribution! This is a good cleanup of the namespace in measure, and I like the API for the new variation_of_information. I really only have a few minor comments/tweaks, some which relate to the original code and not your changes, with one additional question:

Is there a reason compare_ssim didn't make the move also? It is very much in this class of functions, and seems like skimage.evaluate would be the proper home for it as well.

Comment thread doc/source/api_changes.rst Resolved
Comment thread skimage/evaluate/simple_metrics.py Resolved
Comment thread skimage/evaluate/simple_metrics.py Outdated
]


def _assert_compatible(im1, im2):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this the ideal place for this function? Would it make more sense for this to live in _shared?

Some duplicated effort where we ask for masks in other functions could be simplified with this. I wouldn't mind removing the leading underscore in that case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed, moved to _shared and renamed assert_equal_shape

Comment thread skimage/evaluate/simple_metrics.py Resolved
@rbavery

rbavery commented Jul 16, 2019 •

Copy link
Copy Markdown
Contributor Author

Is the build fixed now? Sometimes mirrors fail. There was a retry flag on most pip stuff. Not sure about other dependencies. THe general rule is that at least 1 build will fail everytime everythign is correct.

Yep @hmaarrfk , looks like all builds pass. I think the python 3.7 build that failed last time was a fluke, it's working now.

@rbavery

rbavery commented Jul 18, 2019

Copy link
Copy Markdown
Contributor Author

Thank you for the contribution! This is a good cleanup of the namespace in measure, and I like the API for the new variation_of_information. I really only have a few minor comments/tweaks, some which relate to the original code and not your changes, with one additional question:

Is there a reason compare_ssim didn't make the move also? It is very much in this class of functions, and seems like skimage.evaluate would be the proper home for it as well.

Hey @JDWarner thanks for the review! I missed compare_ssim on my first look through of measure, I've moved it to skimage.evaluate.structural_similarity and updated api_changes and TODO accordingly.

@JDWarner JDWarner left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I approve - pending @jni/@stefanv final approval for this refactor and the new submodule.

@stefanv

stefanv commented Jul 18, 2019

Copy link
Copy Markdown
Member

I don't object to the new submodule, although I wonder if evaluate evokes a strong connection with the proposed content. Evaluate exactly what? Why not compare? It holds for segmentation and images equally well.

Sorry to nitpick on such a bike-sheddable item, but since we don't often introduce top-level models I think it is worth getting the name right in this case.

@rbavery

rbavery commented Jul 18, 2019

Copy link
Copy Markdown
Contributor Author

@stefanv I hear you, I was thinking of the term evaluation metrics when I chose that name, but some other names I considered for the submodule were metrics and validate. compare sounded a bit too general to me, if I'm looking for metrics that measure error, I'd more expect to find them in a module called validate or metrics than compare. Even though not all the functions here are measuring error I think most folks will use these to compare an image to a true reference image, so choosing a name that reflects this type of comparison would help folks find the functions they need I think.

But I'm not totally opposed to compare, curious to hear what you think about the other options and if compare is still preferred.

@stefanv

stefanv commented Jul 18, 2019

Copy link
Copy Markdown
Member

Metrics would feel more familiar to me. What do others think?

@JDWarner

Copy link
Copy Markdown
Contributor

Metrics feels familiar and the colloquial definition fits. However, while I don't want to get too far into the weeds with this, note that metric has a specific mathematical definition and not all of these functions are metrics.

For example, two routines I'll be contributing as soon as this refactor happens are the Dice coefficient and Jaccard index for comparison of binary images/segmented objects against a ground truth. The Dice is not a metric, while the Jaccard is (see the Jaccard Wikipedia article for further details).

@rbavery

rbavery commented Jul 18, 2019

Copy link
Copy Markdown
Contributor Author

I'm leaning towards metrics. Maybe we can note in the module description that it also includes semimetrics like the Dice coefficient. I checked out the wiki article and it sounds like semimetric is a valid term for measures that don't satisfy the triangle equality. And there are other sets of measures that satisfy some of the rules for being a metric, like quasimetrics, and all of them seem to have the suffix metric. https://en.wikipedia.org/wiki/Metric_(mathematics)#Generalized_metrics

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

I haven't made a careful review, but I noticed two things while scrolling the code.

Comment thread skimage/measure/simple_metrics.py Resolved
@sciunto sciunto added this to the 0.17 milestone Jul 19, 2019
@sciunto sciunto added 🔧 type: Maintenance Refactoring and maintenance of internals type: new feature labels Jul 19, 2019

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

I made additional comments after a more careful reading of the code. My main concern is about the new assert_shape_equal function. Also, the rendering of the gallery example is not good on my laptop. Do you have the same?

The rest is more on details.


def assert_shape_equal(im1, im2):
"""Raise an error if the shape and dtype do not match."""
if not im1.shape == im2.shape:

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.

Why not just assert im1.shape == im2.shape? Do we really need this function? The gain looks low to me.

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.

See also my comments below.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We had assert im1.shape == im2.shape previously, @JDWarner thought it might be useful to have a function in _shared
See #4025 (comment)
If we made this function use an assert statement would that be an improvement? I'm impartial either way.

In [7]: def assert_shape_equal(a, b): 
   ...:     message = "%r does not have equal shape with %r" % (a.shape, b.shape) 
   ...:     assert a.shape == b.shape, message 

In [8]: assert_shape_equal(x,y)                                                                                                                                                   
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-8-8237c8a1005b> in <module>
----> 1 assert_shape_equal(x,y)

<ipython-input-7-2933226b4e44> in assert_shape_equal(a, b)
      1 def assert_shape_equal(a, b):
      2     message = "%r does not have equal shape with %r" % (a.shape, b.shape)
----> 3     assert a.shape == b.shape, message
      4 
      5 

AssertionError: (3, 3) does not have equal shape with (4, 4)

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.

We should not use assert if the check must be done in any situation (including with optimize flags). I would say no.

My opinion on this is to put this function in _shared/utils.py, below assert_nD, and rename these functions to do not use the word "assert". Like check_nD, check_shape_equality.

This can be addressed in another PR. These functions are not in the public API, it won't be painful to change this later.

for connectomics. Front. Neuroanat. 9:142.
:DOI:`10.3389/fnana.2015.00142`
"""
assert_shape_equal(im_true, im_test)

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 comment follows another inline comment I made where this function is defined.

I don't like calling this function assert_something, because it does not call assert. And this is important. Assert statements are removed if python is called with an optimization flag.

So, assert_shape_equal is not written in the right file. I'm not sure where it can fit the best and I'm not certain that we want this sort of functions in scikit-image. @scikit-image/core Thoughts?

Comment thread skimage/metrics/simple_metrics.py Resolved
Comment thread skimage/metrics/simple_metrics.py Resolved
(im_true.ravel(), im_test.ravel())),
dtype=float).tocsr()
table2 = contingency_table(im_true, im_test, normalize=True)
(table1 != table2).nnz == 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
(table1 != table2).nnz == 0
assert (table1 != table2).nnz == 0

?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This tests if the contingency_table function is working, forgot the assert. I changed the test to be more legible

def test_contingency_table():
    im_true = np.array([1, 2, 3, 4])
    im_test = np.array([1, 1, 8, 8])

    table1 = np.array([[0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  ],
       [0.  , 0.25, 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  ],
       [0.  , 0.25, 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  ],
       [0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.25],
       [0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.25]])

    sparse_table2 = contingency_table(im_true, im_test, normalize=True)
    table2 = sparse_table2.toarray()
    assert_array_equal(table1, table2)

ax[5].set_title('Morphological GAC')
ax[5].set_axis_off()

plt.show()

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 how the plot looks like for me.

sphx_glr_plot_metrics_001

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ah yikes, I changed the figure size and it looks good on my end.

@jni

jni commented Jul 20, 2019

Copy link
Copy Markdown
Member

Hi everyone,

apologies that I don't have time for a thorough review right now, but I just wanted to chime in on the module name. imho "metrics" is too precise. Even semi-metrics don't cut it — in some cases, we are measuring similarity, not distance.

imho, we should go with something along the lines of "evaluation", "evaluate", "performance", ... For me, a non-mathematician, "metrics" just sounds too vague. I wouldn't know to look there.

@jni

jni commented Jul 20, 2019

Copy link
Copy Markdown
Member

I'll also note, I don't necessarily want this debate to hold the merge up — I think this is going to be a prolonged debate and we should not clutter the discussion here.

@sciunto

sciunto commented Jul 21, 2019

Copy link
Copy Markdown
Member

The example looks good for me. I validate this PR as well.

@jni. Please, open an issue for the "evaluation vs metrics" debate.

I'll open another one about the assert thing.

Many thanks @rbavery for your patience!

@sciunto
sciunto merged commit 88e8eb9 into scikit-image:master Jul 21, 2019
@rbavery

rbavery commented Jul 21, 2019

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback all!

@rbavery
rbavery deleted the evaluation branch July 21, 2019 19:16
jni pushed a commit that referenced this pull request Jul 22, 2019
* unify the choice of a lower case for euclidean

* Fix typo in comment

* Fix typo in docstring
@seanbudd seanbudd mentioned this pull request Jul 24, 2019
2 of 10 tasks
@sciunto sciunto changed the title skimage.evaluation module evaluation or metrics module Jul 24, 2019
jni pushed a commit that referenced this pull request Jul 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🙏 Feature request 🔧 type: Maintenance Refactoring and maintenance of internals

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants