Skip to content

Warn if positional arguments are used in MetricFrame - #822

Merged
romanlutz merged 20 commits into
fairlearn:mainfrom
MiroDudik:deprecate-positional-metric-frame
Jun 9, 2021
Merged

romanlutz merged 20 commits into
fairlearn:mainfrom
MiroDudik:deprecate-positional-metric-frame

Conversation

@MiroDudik

@MiroDudik MiroDudik commented May 25, 2021 •

Copy link
Copy Markdown
Member

The initial step in #756. Specifically, Step 1 from #756 (comment), but with the benefit of the constructor signature that looks like Step 2.

It's currently using _deprecate_positional_args utility from sklearn (see here).

This is what the warning looks like:

>>> from fairlearn.metrics import MetricFrame
>>> import sklearn.metrics as skm
>>> y_true=[0,1,0,0,1]
>>> y_pred=[0,1,1,0,1]
>>> sf=['a','a','b','b','b']
>>> mf = MetricFrame(skm.accuracy_score, y_true, y_pred, sensitive_features=sf)
/mnt/c/Users/mdudik/projects/fairlearn/.venv/lib/python3.6/site-packages/sklearn/utils/validation.py:72: FutureWarning: Pass metrics=<function accuracy_score at 0x7fe2a3e45598>, y_true=[0, 1, 0, 0, 1], y_pred=[0, 1, 1, 0, 1] as keyword args. From version 0.8.0 passing these as positional arguments will result in an error
  "will result in an error", FutureWarning)
>>> mf.difference()
0.33333333333333337
>>> mf = MetricFrame(metrics=skm.accuracy_score, y_true=y_true, y_pred=y_pred, sensitive_features=sf)
>>> mf.difference()
0.33333333333333337

I don't love the look of the warning message, but I like the convenience of just using the decorator to add the warning to the code.

Signed-off-by: MiroDudik [email protected]

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

No point recreating a warning that already exists. If it's not out by v0.8.0, we should remember to revise the message, though ;-)

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

Please add a note to CHANGES.md for this.

@MiroDudik

Copy link
Copy Markdown
Member Author

@hildeweerts : thanks for fixing the example! After this PR is in, I was planning to create a separate issue to fix all the examples / notebooks / tests. Let me know if any other updates are required at this point besides updating CHANGES.md as roman requested.

@hildeweerts

Copy link
Copy Markdown
Contributor

Would it make sense to add a more meaningful error message for users who use metric instead of metrics, or is that not necessary in your opinion?

@romanlutz

Copy link
Copy Markdown
Member

Theoretically one could keep it as a kwarg for a release or two and map it back to the new metrics, while printing a warning. I'm not sure that's needed, though. I'd be curious to hear what @riedgar-ms amd @adrinjalali think, too.

@hildeweerts

Copy link
Copy Markdown
Contributor

I was thinking about catching the TypeError: MetricFrame got an unexpected keyword argument metric (or whatever the basic error looks like) to say something more meaningful like "did you mean metrics?" or "metric was changed to metrics in version blabla.

I agree it's probably not necessary to actually map it back, but I'm curious about other people's thoughts!

@MiroDudik

Copy link
Copy Markdown
Member Author

@hildeweerts , @romanlutz : I don't want to mess with the function signature, but I could see whether I can create a custom decorator that would basically have an additional keyword argument "metric" and throw a less obscure warning if that argument is used.

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

LGTM assuming you add a note to CHANGES.md as you mentioned.

@MiroDudik

Copy link
Copy Markdown
Member Author

I've added a custom decorator. The behavior is now this:

>>> from fairlearn.metrics import MetricFrame
>>> import sklearn.metrics as skm
>>> y_true=[0,1,0,0,1]
>>> y_pred=[0,1,1,0,1]
>>> sf=['a','a','b','b','b']

>>> mf = MetricFrame(skm.accuracy_score, y_true, y_pred, sensitive_features=sf)
/mnt/c/Users/mdudik/projects/fairlearn/fairlearn/metrics/_metric_frame.py:67: FutureWarning: Pass metrics=<function accuracy_score at 0x7f2ce8c87ea0>, y_true=[0, 1, 0, 0, 1], y_pred=[0, 1, 1, 0, 1] as keyword args. From version 0.8.0 passing these as positional arguments will result in an error
  FutureWarning)
>>> mf.difference()
0.33333333333333337

>>> mf = MetricFrame(skm.accuracy_score, y_true=y_true, y_pred=y_pred, sensitive_features=sf)
/mnt/c/Users/mdudik/projects/fairlearn/fairlearn/metrics/_metric_frame.py:67: FutureWarning: Pass metrics=<function accuracy_score at 0x7f2ce8c87ea0> as keyword args. From version 0.8.0 passing these as positional arguments will result in an error
  FutureWarning)
>>> mf.difference()
0.33333333333333337

>>> mf = MetricFrame(metric=skm.accuracy_score, y_true=y_true, y_pred=y_pred, sensitive_features=sf)
/mnt/c/Users/mdudik/projects/fairlearn/fairlearn/metrics/_metric_frame.py:77: FutureWarning: The positional argument 'metric' has been replaced by a keyword argument 'metrics'. From version 0.8.0 passing it as a positional argument or as a keyword argument 'metric' will result in an error
  FutureWarning)
>>> mf.difference()
0.33333333333333337

>>> mf = MetricFrame(metrics=skm.accuracy_score, y_true=y_true, y_pred=y_pred, sensitive_features=sf)
>>> mf.difference()
0.33333333333333337

@hildeweerts , @romanlutz , @riedgar-ms : please review that you're still ok with this!

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

Although I'm happy with the changes so far, I've just remembered that the docs and examples need updating too (not to mention the tests and _create_group_metric_set()). We shouldn't have warnings being emitted from those, so please don't merge until those have been changed as well.

@MiroDudik

Copy link
Copy Markdown
Member Author

@riedgar-ms -- i am aware and agree, see my comment #822 (comment). let me fix the examples and docs and leave tests and notebooks for sprints.

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

let me fix the examples and docs and leave tests and notebooks for sprints.

I'm not quite sure that's how we should be treating these. I know you wrote this in the context of creating good first issues (in fact I asked everyone to think about this a bit yesterday!). However, we set criteria for PRs such as having documentation and tests. If we start now offloading supposedly simple test adjustments to the future then how can we ask other contributors to write tests?

We already have a task to fix warnings in tests. Unsurprisingly nobody has picked that up because it's just not terribly interesting. Besides, sometimes these can take a while to investigate. I'd vote for not knowingly adding more, but I'm sure other @fairlearn/fairlearn-maintainers have opinions, too.

@romanlutz
romanlutz self-requested a review May 28, 2021 19:52
@MiroDudik

MiroDudik commented May 28, 2021 •

Copy link
Copy Markdown
Member Author

Well... I did one pass with a couple of RegEx queries. .ipynb notebooks are incomplete, but I'm hoping it covered the rest. And as a bonus, I think I fixed a FutureWarning in exponentiated gradient tests.

Re. .ipynb we can either create an issue and/or tackle them when they are converted into .py?

@romanlutz @riedgar-ms

MiroDudik added 3 commits May 28, 2021 20:27
Signed-off-by: MiroDudik <[email protected]>
Signed-off-by: MiroDudik <[email protected]>
Signed-off-by: MiroDudik <[email protected]>

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

Thanks for the test update. I don't care as much about the ipynb notebooks, more about examples, so this is fine by me.

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

Thank you for eliminating the warnings from our own code.

Comment thread CHANGES.md Resolved
Comment thread docs/user_guide/mitigation.rst Resolved

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

Thanks @MiroDudik

Comment thread CHANGES.md Resolved
Comment thread docs/user_guide/mitigation.rst Resolved
Comment thread fairlearn/metrics/_metric_frame.py Outdated

# If 1-3 positional arguments are provided (apart fom self), issue warning.
if len(args) > 0:
args_msg = ", ".join([f"{name}={value}" for name, value in positional_dict.items()])

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.

passing the value here makes the warning message very confusing. I would rather keep the warnings as they were in sklearn.

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.

@adrinjalali : this is actually almost exactly what sklearn does. i don't like it that much either :-)... how about this:

>>>
>>> mf = MetricFrame(skm.accuracy_score, y_true, y_pred, sensitive_features=sf)
FutureWarning: You have provided metrics, y_true, y_pred as positional arguments. Please pass them as keyword arguments. From version 0.8.0 passing these as positional arguments will result in an error.

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 like your proposed alternative.

Comment on lines -117 to +177
metric: Union[Callable, Dict[str, Callable]],
*,
metrics: Union[Callable, Dict[str, Callable]],

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 we can't do, we need to deprecate metric.

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.

Not sure I quite understand. The code in this PR issues a deprecation warning if you pass metric (it's handled by the decorator). You get the following message:

>>> mf = MetricFrame(metric=skm.accuracy_score, y_true=y_true, y_pred=y_pred, sensitive_features=sf)
FutureWarning: The positional argument 'metric' has been replaced by a keyword argument 'metrics'. From version 0.8.0 passing it as a positional argument or as a keyword argument 'metric' will result in an error

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 see. I would rather not have this warning in the decorator. The decorator is nice to handle the positional argument deprecation, but we can easily deprecate an old parameter in the function itself, which would make the code more readable.

@MiroDudik MiroDudik Jun 2, 2021 •

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 see. If I understand correctly, you'd like to have both metrics and metric as keyword arguments, but:

  • in the numpy doc section use a deprecation block
  • in the constructor code, issue a warning

One annoying issue is that the intermediate metric argument is optional, so it will need to appear after sensitive_features, so the signature looks like this:

    def __init__(self,
                 *,
                 metrics: Union[Callable, Dict[str, Callable]],
                 y_true,
                 y_pred,
                 sensitive_features,
                 control_features: Optional = None,
                 sample_params: Optional[Union[Dict[str, Any], Dict[str, Dict[str, Any]]]] = None,
                 metric = None):

But maybe that's ok.

If both metrics and metric are passed, I'm going to raise TypeError.

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.

Yes, exactly

@MiroDudik MiroDudik Jun 2, 2021 •

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, there's an issue--if we want to have compatibility with the old API, we need to make metrics / y_true / y_pred formally optional, otherwise we'll run into this:

>>> mf = MetricFrame(metric=skm.accuracy_score, y_true=y_true, y_pred=y_pred, sensitive_features=sf)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/mnt/c/Users/mdudik/projects/fairlearn/fairlearn/metrics/_metric_frame.py", line 71, in compatible_metric_frame_init
    **kwargs)
TypeError: __init__() missing 1 required keyword-only argument: 'metrics'

So my suggestion is to have the formal signature with metrics=None, y_true=None, y_pred=None, but raise if any of them is None. An alternative would be to keep the correct signature, but go back to the decorator approach.

@MiroDudik MiroDudik Jun 2, 2021 •

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.

@adrinjalali : when I tried to do this without a decorator, I found myself re-implementing standard Python error warnings around missing keyword-only arguments, so I've decided to go with the decorator approach, but provide an explanatory comment (for the code contributors), and add the metric deprecation block in the docs (for the package users).

@adrinjalali

Copy link
Copy Markdown
Member

If both metric and metrics are passed, I'm happy to raise.

@MiroDudik

Copy link
Copy Markdown
Member Author

@adrinjalali -- i'm done with my updates. i ended up keeping metric keyword handling in the decorator, but added a comment and a deprecation block to address confusion.

@adrinjalali adrinjalali 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 guess since this is gonna be all deleted soon, I'm happy with the implementation. However, since we're doing releases rather quickly, I'd let it be there for at least one extra release.

Could you please write tests for the changes? To make sure the right warnings are raised when planned and no warning is raised when correctly used?

We need codecov, gonna open a separate issue for that.

@MiroDudik

Copy link
Copy Markdown
Member Author

@adrinjalali -- I've added tests and also changed the deprecation warning, so it says that the old API will raise errors starting in 0.9.0. (The next version we're about to release is 0.7.0.) Is that what you had in mind?

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

LGTM. I'd go for deprecation end around v.10 instead of .9 though. We do tend to release rather frequently.

Signed-off-by: MiroDudik <[email protected]>
@MiroDudik

MiroDudik commented Jun 8, 2021 •

Copy link
Copy Markdown
Member Author

i've bumped the future version from 0.9.0 to 0.10.0. once the checks pass, please merge! @fairlearn/fairlearn-maintainers

MiroDudik added 2 commits June 8, 2021 17:02
Signed-off-by: MiroDudik <[email protected]>
Signed-off-by: MiroDudik <[email protected]>
@romanlutz
romanlutz merged commit 1247b08 into fairlearn:main Jun 9, 2021
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.

5 participants