-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathdiversity.py
More file actions
351 lines (312 loc) · 11.9 KB
/
diversity.py
File metadata and controls
351 lines (312 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Operations to measure diversity."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from meterstick import operations
from meterstick import sql
import numpy as np
import pandas as pd
class DiversityBase(operations.Distribution):
"""Base class that captures shared logic of diversity Operations."""
def __init__(
self,
over,
child,
name_tmpl,
where,
additional_fingerprint_attrs=None,
**kwargs,
):
super(DiversityBase, self).__init__(
over,
child,
name_tmpl,
where=where,
additional_fingerprint_attrs=additional_fingerprint_attrs,
**kwargs
)
self.extra_index = []
def get_distribution_sql_and_columns(
self, table, split_by, global_filter, indexes, local_filter, with_data
):
dist_sql, with_data = super(DiversityBase, self).get_sql_and_with_clause(
table, split_by, global_filter, indexes, local_filter, with_data
)
# Remove 'Distribution of ' in column names.
for c in dist_sql.columns:
alias = c.alias_raw
if alias.startswith('Distribution of '):
c.set_alias(alias[len('Distribution of ') :])
child_table = sql.Datasource(dist_sql, 'Distribution')
child_table_alias = with_data.merge(child_table)
return child_table_alias, dist_sql.columns, with_data
def to_dataframe(self, res):
if isinstance(res, pd.Series):
return res.to_frame().T
return super(DiversityBase, self).to_dataframe(res)
class HHI(DiversityBase):
"""Herfindahl–Hirschman index of metric distribution."""
def __init__(self, over, child=None, where=None, **kwargs):
super(HHI, self).__init__(over, child, 'HHI of {}', where, **kwargs)
def compute_on_children(self, child, split_by):
dist = super(HHI, self).compute_on_children(child, split_by)
res = self.group(dist**2, split_by).sum()
return self.to_dataframe(res)
def get_sql_and_with_clause(
self, table, split_by, global_filter, indexes, local_filter, with_data
):
# First get the queries for Distribution(over, child).
child_table_alias, dist_columns, with_data = (
self.get_distribution_sql_and_columns(
table, split_by, global_filter, indexes, local_filter, with_data
)
)
columns = sql.Columns()
all_split_by = (
sql.Columns(split_by.aliases).add(self.extra_split_by).aliases
)
# For every value column, compute SUM(POWER(val, 2)) which is the HHI.
for c in dist_columns:
if c.alias in all_split_by:
continue
col = sql.Column(
c.alias,
'SUM(POWER({}, 2))',
)
col.set_alias(self.name_tmpl.format(c.alias_raw))
columns.add(col)
return (
sql.Sql(columns, child_table_alias, groupby=indexes.aliases),
with_data,
)
class Entropy(DiversityBase):
"""Entropy of metric distribution."""
def __init__(self, over, child=None, where=None, **kwargs):
super(Entropy, self).__init__(over, child, 'Entropy of {}', where, **kwargs)
def compute_on_children(self, child, split_by):
dist = super(Entropy, self).compute_on_children(child, split_by)
res = self.group(-dist * np.log(dist), split_by).sum()
return self.to_dataframe(res)
def get_sql_and_with_clause(
self, table, split_by, global_filter, indexes, local_filter, with_data
):
# First get the queries for Distribution(over, child).
child_table_alias, dist_columns, with_data = (
self.get_distribution_sql_and_columns(
table, split_by, global_filter, indexes, local_filter, with_data
)
)
all_split_by = (
sql.Columns(split_by.aliases).add(self.extra_split_by).aliases
)
columns = sql.Columns()
# For every value column, compute -SUM(val * LOG(val)) which is the entropy.
for c in dist_columns:
if c.alias in all_split_by:
continue
col = sql.Column(
(c.alias, c.alias),
'-SUM({} * LOG({}))',
)
col.set_alias(self.name_tmpl.format(c.alias_raw))
columns.add(col)
return (
sql.Sql(columns, child_table_alias, groupby=indexes.aliases),
with_data,
)
class TopK(DiversityBase):
"""The total share of the largest k contributors."""
def __init__(
self,
over,
k,
child=None,
where=None,
additional_fingerprint_attrs=None,
**kwargs,
):
if not isinstance(k, int):
raise ValueError('k must be an integer!')
super(TopK, self).__init__(
over,
child,
"Top-%s's share of {}" % k,
where,
['k'] + (additional_fingerprint_attrs or []),
**kwargs,
)
self.k = k
def compute_on_children(self, child, split_by):
dist = super(TopK, self).compute_on_children(child, split_by)
top_k = []
grouped = self.group(dist, split_by)
# groupby().nlargest() only works on Series but not DataFrame, so we need to
# iterate the columns.
for col in dist:
top_k.append(self.group(grouped[col].nlargest(self.k), split_by).sum())
if split_by:
return pd.concat(top_k, axis=1)
return pd.DataFrame([top_k], columns=dist.columns)
def get_sql_and_with_clause(
self, table, split_by, global_filter, indexes, local_filter, with_data
):
"""Gets the SQL query and WITH clause.
The query is constructed in 4 steps.
1. Get the query for the Distribution of the child Metric.
2. Keep all indexing/groupby columns unchanged.
3. For all value columns, collect the top-k values into an array by
ARRAY_AGG(val_col IGNORE NULLS ORDER BY val_col DESC LIMIT k) AS val_arr.
Note that the ordering between number and NULLs varies by dialect so we
use IGNORE NULLS.
4. For all value columns, do
'SELECT SUM(x) FROM UNNEST(val_arr) AS x WITH OFFSET AS i WHERE i < k'
to get the sum of the top-k values. Note that the
'WITH OFFSET AS i WHERE i < k' is redundant here but many external
dialects don't support 'LIMIT k' in #3 so we need to do it in #4.
Args:
table: The table we want to query from.
split_by: The columns that we use to split the data.
global_filter: The sql.Filters that can be applied to the whole Metric
tree.
indexes: The columns that we shouldn't apply any arithmetic operation.
local_filter: The sql.Filters that have been accumulated so far.
with_data: A global variable that contains all the WITH clauses we need.
Returns:
The SQL instance for metric, without the WITH clause component.
The global with_data which holds all datasources we need in the WITH
clause.
"""
child_table_alias, dist_columns, with_data = (
self.get_distribution_sql_and_columns(
table, split_by, global_filter, indexes, local_filter, with_data
)
)
all_split_by = (
sql.Columns(split_by.aliases).add(self.extra_split_by).aliases
)
top_k_array_columns = sql.Columns()
top_k_sum_columns = sql.Columns(indexes.aliases)
for c in dist_columns:
if c.alias in all_split_by:
continue
top_k_array_col = sql.Column(
c.alias,
sql.ARRAY_AGG_FN(c.alias, ascending=False, dropna=True, limit=self.k),
)
top_k_array_col.set_alias(c.alias_raw)
top_k_array_columns.add(top_k_array_col)
top_k_sum_col = sql.Column(
'(SELECT SUM(x) FROM'
f' {sql.UNNEST_ARRAY_FN(top_k_array_col.alias, "x", "i", self.k)})',
)
top_k_sum_col.set_alias(self.name_tmpl.format(c.alias_raw))
top_k_sum_columns.add(top_k_sum_col)
top_k_sql = sql.Sql(
top_k_array_columns, child_table_alias, groupby=indexes.aliases
)
top_k_table = sql.Datasource(top_k_sql, 'TopKArrays')
top_k_table_alias = with_data.merge(top_k_table)
return sql.Sql(top_k_sum_columns, top_k_table_alias), with_data
class Nxx(DiversityBase):
"""The minimum number of contributors to achieve certain share."""
def __init__(
self,
over,
share,
child=None,
where=None,
additional_fingerprint_attrs=None,
**kwargs,
):
if not 0 < share <= 1:
raise ValueError('Share must be in (0, 1]!')
super(Nxx, self).__init__(
over,
child,
'N(%s) of {}'
% (int(100 * share) if (100 * share).is_integer() else 100 * share),
where,
['share'] + (additional_fingerprint_attrs or []),
**kwargs,
)
self.share = share
def compute_on_children(self, child, split_by):
dist = super(Nxx, self).compute_on_children(child, split_by)
return pd.concat(
[self.nxx_for_one_col(dist[[c]], split_by) for c in dist], axis=1
)
def nxx_for_one_col(self, col, split_by):
sorted_col = col.sort_values(split_by + list(col.columns), ascending=False)
cumsum = self.group(sorted_col, split_by).cumsum()
res = self.group(cumsum < self.share, split_by).sum() + 1
return self.to_dataframe(res)
def get_sql_and_with_clause(
self, table, split_by, global_filter, indexes, local_filter, with_data
):
"""Gets the SQL query and WITH clause.
The query is constructed in 4 steps.
1. Get the query for the Distribution of the child Metric.
2. Keep all indexing/groupby columns unchanged.
3. For all value columns, order the values in descending order and compute
the cumulative sum by SELECT
SUM(val_col) OVER
(ORDER BY val_col DESC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
4. Get the minimum number of players to achieve the share by SELECT
COUNTIF(cumulative_sum < share) + 1.
Args:
table: The table we want to query from.
split_by: The columns that we use to split the data.
global_filter: The sql.Filters that can be applied to the whole Metric
tree.
indexes: The columns that we shouldn't apply any arithmetic operation.
local_filter: The sql.Filters that have been accumulated so far.
with_data: A global variable that contains all the WITH clauses we need.
Returns:
The SQL instance for metric, without the WITH clause component.
The global with_data which holds all datasources we need in the WITH
clause.
"""
child_table_alias, dist_columns, with_data = (
self.get_distribution_sql_and_columns(
table, split_by, global_filter, indexes, local_filter, with_data
)
)
all_split_by = (
sql.Columns(split_by.aliases).add(self.extra_split_by).aliases
)
cumsum_cols = sql.Columns(indexes.aliases)
nxx_cols = sql.Columns()
for c in dist_columns:
if c.alias in all_split_by:
continue
cumsum_col = sql.Column(
c.alias,
'SUM({})',
partition=split_by.aliases,
order=f'{c.alias} DESC',
window_frame='ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW',
)
cumsum_col.set_alias('Cumulative %s' % c.alias_raw)
cumsum_cols.add(cumsum_col)
nxx_col = sql.Column(
cumsum_col.alias, sql.COUNTIF_FN('{} < %s' % self.share) + ' + 1'
)
nxx_col.set_alias(self.name_tmpl.format(c.alias_raw))
nxx_cols.add(nxx_col)
cumsum_sql = sql.Sql(cumsum_cols, child_table_alias)
cumsum_table = sql.Datasource(cumsum_sql, 'CumulativeDistribution')
cumsum_alias = with_data.merge(cumsum_table)
return sql.Sql(nxx_cols, cumsum_alias, groupby=indexes.aliases), with_data