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

Unify metric API #294

Merged
merged 62 commits into from
Nov 15, 2023
Merged

Unify metric API #294

merged 62 commits into from
Nov 15, 2023

Conversation

aaarrti
Copy link
Collaborator

@aaarrti aaarrti commented Aug 12, 2023

Description

  • Unify metric API, by removing the distinction between BatchedMetric and Metric, this way we will have centralized uniform pre- and post-processing as well as clearly defined API.
    Some metrics can easily be batched, but this will be done in separate PR.

Implemented changes

  • Moved batched pre-processing logic to Metric, remove evaluate_instance anstract method.
  • BatchedMetric is not just an alias to Metric
  • Make instance-wise metric conform to the API defined by Metric

Changelist

  • The only place with any real changes are base.py (mostly copied over from base_batched.py)
  • All other changes in metrics are just wrapping evaluate_instance in for-loop.

Minimum acceptance criteria

  • No breaking changes.

@codecov-commenter
Copy link

codecov-commenter commented Aug 12, 2023

Codecov Report

Attention: 67 lines in your changes are missing coverage. Please review.

❗ No coverage uploaded for pull request base (main@1de69e6). Click here to learn what that means.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #294   +/-   ##
=======================================
  Coverage        ?   91.03%           
=======================================
  Files           ?       62           
  Lines           ?     3402           
  Branches        ?        0           
=======================================
  Hits            ?     3097           
  Misses          ?      305           
  Partials        ?        0           
Files Coverage Δ
quantus/__init__.py 100.00% <100.00%> (ø)
quantus/helpers/model/models.py 84.29% <ø> (ø)
quantus/helpers/model/pytorch_model.py 87.41% <100.00%> (ø)
quantus/helpers/model/tf_model.py 91.60% <100.00%> (ø)
quantus/helpers/utils.py 92.28% <100.00%> (ø)
quantus/metrics/__init__.py 100.00% <100.00%> (ø)
quantus/helpers/model/model_interface.py 77.50% <87.50%> (ø)
quantus/metrics/axiomatic/completeness.py 93.02% <94.11%> (ø)
quantus/metrics/axiomatic/input_invariance.py 95.34% <93.75%> (ø)
quantus/metrics/axiomatic/non_sensitivity.py 96.00% <93.75%> (ø)
... and 34 more

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@aaarrti aaarrti changed the title WIP Unify metric API Aug 13, 2023
@aaarrti aaarrti marked this pull request as ready for review August 13, 2023 14:40
Copy link
Member

@annahedstroem annahedstroem left a comment

Choose a reason for hiding this comment

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

Review part 1/N.

Copy link
Collaborator

@dkrako dkrako left a comment

Choose a reason for hiding this comment

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

Great, this takes a good direction!

I'll try to block sime time to review in detail next week

@annahedstroem annahedstroem self-requested a review October 4, 2023 15:21
Copy link
Member

@annahedstroem annahedstroem left a comment

Choose a reason for hiding this comment

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

Really great work Artem, like the simple and straightfoward solution about looping over all evaluate_instance in the evaluate_batch. Solves the problem neatly. Do however put an issue for vectorisation as you've mentioned in several todos.

Almost ready for merge :muscle!

quantus/metrics/base.py Show resolved Hide resolved
quantus/metrics/base_batched.py Outdated Show resolved Hide resolved
quantus/metrics/base_perturbed.py Outdated Show resolved Hide resolved
quantus/metrics/base_perturbed.py Outdated Show resolved Hide resolved
quantus/metrics/base.py Outdated Show resolved Hide resolved
quantus/metrics/robustness/avg_sensitivity.py Show resolved Hide resolved
quantus/metrics/robustness/consistency.py Outdated Show resolved Hide resolved
quantus/metrics/robustness/continuity.py Outdated Show resolved Hide resolved
@annahedstroem
Copy link
Member

Is this ready to merge @aaarrti

@dkrako would you have the chance to take a quick look?

quantus/metrics/robustness/continuity.py Show resolved Hide resolved
----------
args:
Unused.
a_batch:
Copy link
Member

Choose a reason for hiding this comment

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

@aaarrti missing docs, please please double-check now everywhere that it is consistent.

Copy link
Member

Choose a reason for hiding this comment

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

# Quantus project URL: <https://github.com/understandable-machine-intelligence-lab/Quantus>.


if sys.version_info >= (3, 8):
Copy link
Member

Choose a reason for hiding this comment

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

what is this? this is new?

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@final shows users, that this class must not be inherited from.
E.g.,

from typing import final


@final
class A:
    pass


class B(A):
    pass
mypy example.py 

mypy.ini: [mypy]: Unrecognized option: show_none_errors = False
example.py:9: error: Cannot inherit from final class "A"  [misc]
Found 1 error in 1 file (checked 1 source file)

The if sys.version_info >= (3, 8) can be removed, after we drop python3.7 support (which I hope will be soon, because it is not maintained since half a year https://devguide.python.org/versions/)

Copy link
Collaborator

@dkrako dkrako left a comment

Choose a reason for hiding this comment

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

I just saw that there are mutable default parameters in a lot of metric constructors.

using mutable defaults for parameters is dangerous and can lead to very hard to find bugs:
https://florimond.dev/en/posts/2018/08/python-mutable-defaults-are-the-source-of-all-evil/

quantus/metrics/axiomatic/completeness.py Outdated Show resolved Hide resolved
@aaarrti
Copy link
Collaborator Author

aaarrti commented Nov 2, 2023

I just saw that there are mutable default parameters in a lot of metric constructors.

using mutable defaults for parameters is dangerous and can lead to very hard to find bugs: https://florimond.dev/en/posts/2018/08/python-mutable-defaults-are-the-source-of-all-evil/

@dkrako @annahedstroem Hi, I've realised every metric has had mutable default all this time, e.g., https://github.com/understandable-machine-intelligence-lab/Quantus/blob/main/quantus/metrics/axiomatic/completeness.py#L71. I will replace those with None also and add linter check for mutable defaults.

@aaarrti
Copy link
Collaborator Author

aaarrti commented Nov 3, 2023

Really great work Artem, like the simple and straightfoward solution about looping over all evaluate_instance in the evaluate_batch. Solves the problem neatly. Do however put an issue for vectorisation as you've mentioned in several todos.

Almost ready for merge :muscle!

done #299

@annahedstroem annahedstroem merged commit 1ed8b32 into main Nov 15, 2023
7 checks passed
@aaarrti aaarrti deleted the unfiy-metric-api branch December 23, 2023 19:14
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.

None yet

5 participants