Repository navigation
Improve MetricFrame performance - #985
riedgar-ms merged 72 commits into
Conversation
Signed-off-by: Richard Edgar <[email protected]>
Signed-off-by: Richard Edgar <[email protected]>
Signed-off-by: Richard Edgar <[email protected]>
Signed-off-by: Richard Edgar <[email protected]>
Signed-off-by: Richard Edgar <[email protected]>
Signed-off-by: Richard Edgar <[email protected]>
Signed-off-by: Richard Edgar <[email protected]>
Signed-off-by: Richard Edgar <[email protected]>
Signed-off-by: Richard Edgar <[email protected]>
Signed-off-by: Richard Edgar <[email protected]>
Signed-off-by: Richard Edgar <[email protected]>
Signed-off-by: Richard Edgar <[email protected]>
romanlutz
left a comment
There was a problem hiding this comment.
I like the direction this is going! Holding off on reviewing tests since it's still WIP, but I thought I'd take a brief look at least.
| args: List[str] = None, | ||
| kwargs: Dict[str, str] = None): | ||
| self._func = func | ||
| self._args = ['y_true', 'y_pred'] |
There was a problem hiding this comment.
Do we expect the args to contain these two? If so, should we check that the provided args contain them?
There was a problem hiding this comment.
Evidently not obvious.... these are the positional args, so the names in this array represent the columns of the DataFrame which are going to be extracted. The names used by the function itself don't matter.
| import pandas as pd | ||
|
|
||
|
|
||
| class AnnotatedMetricFunction: |
There was a problem hiding this comment.
Since you're (potentially) adding args and kwargs I feel like "augmented" would be better than "annotated". Perhaps it's just not a very important decision, but if anyone has thoughts please respond 🙂
Signed-off-by: Richard Edgar <[email protected]>
Signed-off-by: Richard Edgar <[email protected]>
Signed-off-by: Richard Edgar <[email protected]>
|
We need to discuss the two tests I've disabled. The essential problem is that both of them turn one or more of the inputs into a 2D array. When the code attempts to put this into a |
|
I've been doing a few more experiments, and I'm not sure that there's a way forward which doesn't involve breaking something. The fundamental problem appears to be that we're changing the internal representation of the data from an The trap is this: in some of our test cases, the data really are 2d. In others, the test passed in an array with dummy dimensions of length 1, which were removed when I'm scratching my head a bit here - there are metrics such as |
|
OK, some more experimentation just completed.... import functools
import numpy as np
import pandas as pd
import sklearn.metrics as skm
from fairlearn.metrics._annotated_metric_function import AnnotatedMetricFunction
def test_experimenting():
n_elements = 100
metric_fn = functools.partial(skm.r2_score, multioutput='raw_values')
y_t_2 = np.random.rand(n_elements, 2)
y_p_2 = np.random.rand(n_elements, 2)
direct_result = metric_fn(y_t_2, y_p_2)
all_data = np.concatenate([y_t_2, y_p_2], axis=1)
data = pd.DataFrame(data=all_data, columns=['y_t_o1', 'y_t_o2', 'y_p_p1', 'y_p_o2'])
amf = AnnotatedMetricFunction(func=metric_fn,
args=[['y_t_o1', 'y_t_o2'], ['y_p_p1', 'y_p_o2']])
result_from_annotated = amf.invoke(data)
assert np.array_equal(result_from_annotated, direct_result)I think that the least disruptive thing to do would be to add a requirement that 'all arguments to a Since it is a breaking change, we perhaps want a new class, though.... |
|
@adrinjalali could you expand on your concerns about using |
|
I don't think we have to use For instance, to get def group_min(groups, data):
# sort with major key groups, minor key data
order = np.lexsort((data, groups))
groups = groups[order] # this is only needed if groups is unsorted
data = data[order]
# construct an index which marks borders between groups
index = np.empty(len(groups), 'bool')
index[0] = True
index[1:] = groups[1:] != groups[:-1]
return data[index]You could do a similar thing for other aggregations. I personally don't have the data in a pandas DataFrame to feed to |
|
@adrinjalali , this current change would not require the input be in |
|
Just noting that @adrinjalali also commented on the first pass at this: |
…ame-performance-improvements
Signed-off-by: Richard Edgar <[email protected]>
Signed-off-by: Richard Edgar <[email protected]>
|
Running on Python 3.7. First installing and with the new improved innards: |
Signed-off-by: Richard Edgar <[email protected]>
Signed-off-by: Richard Edgar <[email protected]>
Signed-off-by: Richard Edgar <[email protected]>
|
@adrinjalali , I've gone back and improved some of the documentation. However, the changes are smaller now that the user-facing API is being preserved. |
…ame-performance-improvements
Signed-off-by: Richard Edgar <[email protected]>
| y_p = _convert_to_ndarray_and_squeeze(y_pred) | ||
|
|
||
| func_dict = self._process_functions(metrics, sample_params) | ||
| all_data['y_true'] = list(y_t) |
There was a problem hiding this comment.
why do we convert things to an ndarray and then a list here?
There was a problem hiding this comment.
Because if you try assigning a 2D array to a single pandas column, it complains. However, it doesn't object to a list-of-lists.
| if hasattr(func, "__name__"): | ||
| self.name = func.__name__ | ||
| else: | ||
| logger.warning("Supplied 'func' had no __name__ attribute") |
There was a problem hiding this comment.
this is not tested (hard to find what's not tested, this is why I love codecov)
There was a problem hiding this comment.
Do you mean that I don't check for the log message? This branch of the if statement is covered by test_constructor_no_name()
There was a problem hiding this comment.
Ping @adrinjalali I'm not sure which part you mean isn't tested.
There was a problem hiding this comment.
Yes, the test doesn't check that this warning is raised (I guess that means the test actually raises a warning, which it ideally shouldn't).
Signed-off-by: Richard Edgar <[email protected]>
Signed-off-by: Richard Edgar <[email protected]>
Signed-off-by: Richard Edgar <[email protected]>
Signed-off-by: Richard Edgar <[email protected]>
|
@adrinjalali please could you take another look? |
…ame-performance-improvements
…ps://github.com/riedgar-ms/fairlearn into riedgar-ms/metricframe-performance-improvements
|
Ping.... @adrinjalali , have I adequately addressed your concerns? I would really like to get this checked in. |
adrinjalali
left a comment
There was a problem hiding this comment.
Otherwise this looks good to me. I think it's a positive change. I'm still not super confident with the code since I find it hard to follow in some parts, but this is making things much cleaner than before.
| if hasattr(func, "__name__"): | ||
| self.name = func.__name__ | ||
| else: | ||
| logger.warning("Supplied 'func' had no __name__ attribute") |
There was a problem hiding this comment.
Yes, the test doesn't check that this warning is raised (I guess that means the test actually raises a warning, which it ideally shouldn't).
Signed-off-by: Richard Edgar <[email protected]>
Signed-off-by: Richard Edgar <[email protected]>
|
@adrinjalali I've adjusted the test so that the warning is checked (although I had to go through |
adrinjalali
left a comment
There was a problem hiding this comment.
I'm happy to have this merged as is, and do improvements in future PRs.
Rework the innards of
MetricFrameto useDataFrame.groupby()to improve performance. The public API does not change, although there is a slight change to the return - the resultantDataFrames will havedtype=float64etc. when appropriate, rather than just beingobject.The
FunctionContainerclass has been replaced byAnnotatedMetricFunction. In addition, theGroupFeatureclass has been simplified, removing its dependence onLabelEncoder. These changes have obsoleted quite a few tests.