Repository navigation
Warn if positional arguments are used in MetricFrame - #822
Conversation
Signed-off-by: MiroDudik <[email protected]>
riedgar-ms
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
Please add a note to CHANGES.md for this.
Signed-off-by: MiroDudik <[email protected]>
|
@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. |
|
Would it make sense to add a more meaningful error message for users who use |
|
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. |
|
I was thinking about catching the I agree it's probably not necessary to actually map it back, but I'm curious about other people's thoughts! |
|
@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
left a comment
There was a problem hiding this comment.
LGTM assuming you add a note to CHANGES.md as you mentioned.
Signed-off-by: MiroDudik <[email protected]>
|
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! |
There was a problem hiding this comment.
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.
|
@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
left a comment
There was a problem hiding this comment.
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.
…in simple_learners Signed-off-by: MiroDudik <[email protected]>
|
Well... I did one pass with a couple of RegEx queries. Re. |
Signed-off-by: MiroDudik <[email protected]>
Signed-off-by: MiroDudik <[email protected]>
Signed-off-by: MiroDudik <[email protected]>
romanlutz
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
Thank you for eliminating the warnings from our own code.
|
|
||
| # 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()]) |
There was a problem hiding this comment.
passing the value here makes the warning message very confusing. I would rather keep the warnings as they were in sklearn.
There was a problem hiding this comment.
@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.There was a problem hiding this comment.
I like your proposed alternative.
| metric: Union[Callable, Dict[str, Callable]], | ||
| *, | ||
| metrics: Union[Callable, Dict[str, Callable]], |
There was a problem hiding this comment.
this we can't do, we need to deprecate metric.
There was a problem hiding this comment.
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 errorThere was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
@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).
|
If both |
Signed-off-by: MiroDudik <[email protected]>
Signed-off-by: MiroDudik <[email protected]>
|
@adrinjalali -- i'm done with my updates. i ended up keeping |
adrinjalali
left a comment
There was a problem hiding this comment.
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.
Signed-off-by: MiroDudik <[email protected]>
Signed-off-by: MiroDudik <[email protected]>
Signed-off-by: MiroDudik <[email protected]>
Signed-off-by: MiroDudik <[email protected]>
|
@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
left a comment
There was a problem hiding this comment.
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]>
|
i've bumped the future version from 0.9.0 to 0.10.0. once the checks pass, please merge! @fairlearn/fairlearn-maintainers |
Signed-off-by: MiroDudik <[email protected]>
Signed-off-by: MiroDudik <[email protected]>
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_argsutility from sklearn (see here).This is what the warning looks like:
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]