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

Remove weight parameter from TDIGEST.ADD and fix tests #2408

Merged
merged 1 commit into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions redis/commands/bf/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,19 +370,13 @@ def reset(self, key):
""" # noqa
return self.execute_command(TDIGEST_RESET, key)

def add(self, key, values, weights):
def add(self, key, values):
"""
Add one or more samples (value with weight) to a sketch `key`.
Both `values` and `weights` are lists.
For more information see `TDIGEST.ADD <https://redis.io/commands/tdigest.add>`_.

Example:
Adds one or more observations to a t-digest sketch `key`.

>>> tdigestadd('A', [1500.0], [1.0])
For more information see `TDIGEST.ADD <https://redis.io/commands/tdigest.add>`_.
""" # noqa
params = [key]
self.append_values_and_weights(params, values, weights)
return self.execute_command(TDIGEST_ADD, *params)
return self.execute_command(TDIGEST_ADD, key, *values)

def merge(self, destination_key, num_keys, *keys, compression=None, override=False):
"""
Expand Down
2 changes: 0 additions & 2 deletions redis/commands/bf/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ class TDigestInfo(object):
merged_weight = None
unmerged_weight = None
total_compressions = None
sum_weights = None
memory_usage = None

def __init__(self, args):
Expand All @@ -85,5 +84,4 @@ def __init__(self, args):
self.merged_weight = response["Merged weight"]
self.unmerged_weight = response["Unmerged weight"]
self.total_compressions = response["Total compressions"]
self.sum_weights = response["Sum weights"]
self.memory_usage = response["Memory usage"]
36 changes: 18 additions & 18 deletions tests/test_asyncio/test_bloom.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ async def test_tdigest_reset(modclient: redis.Redis):
# reset on empty histogram
assert await modclient.tdigest().reset("tDigest")
# insert data-points into sketch
assert await modclient.tdigest().add("tDigest", list(range(10)), [1] * 10)
assert await modclient.tdigest().add("tDigest", list(range(10)))

assert await modclient.tdigest().reset("tDigest")
# assert we have 0 unmerged nodes
Expand All @@ -337,19 +337,19 @@ async def test_tdigest_merge(modclient: redis.Redis):
assert await modclient.tdigest().create("to-tDigest", 10)
assert await modclient.tdigest().create("from-tDigest", 10)
# insert data-points into sketch
assert await modclient.tdigest().add("from-tDigest", [1.0] * 10, [1] * 10)
assert await modclient.tdigest().add("to-tDigest", [2.0] * 10, [10] * 10)
assert await modclient.tdigest().add("from-tDigest", [1.0] * 10)
assert await modclient.tdigest().add("to-tDigest", [2.0] * 10)
# merge from-tdigest into to-tdigest
assert await modclient.tdigest().merge("to-tDigest", 1, "from-tDigest")
# we should now have 110 weight on to-histogram
info = await modclient.tdigest().info("to-tDigest")
total_weight_to = float(info.merged_weight) + float(info.unmerged_weight)
assert 110 == total_weight_to
assert 20.0 == total_weight_to
# test override
assert await modclient.tdigest().create("from-override", 10)
assert await modclient.tdigest().create("from-override-2", 10)
assert await modclient.tdigest().add("from-override", [3.0] * 10, [10] * 10)
assert await modclient.tdigest().add("from-override-2", [4.0] * 10, [10] * 10)
assert await modclient.tdigest().add("from-override", [3.0] * 10)
assert await modclient.tdigest().add("from-override-2", [4.0] * 10)
assert await modclient.tdigest().merge(
"to-tDigest", 2, "from-override", "from-override-2", override=True
)
Expand All @@ -362,7 +362,7 @@ async def test_tdigest_merge(modclient: redis.Redis):
async def test_tdigest_min_and_max(modclient: redis.Redis):
assert await modclient.tdigest().create("tDigest", 100)
# insert data-points into sketch
assert await modclient.tdigest().add("tDigest", [1, 2, 3], [1] * 3)
assert await modclient.tdigest().add("tDigest", [1, 2, 3])
# min/max
assert 3 == await modclient.tdigest().max("tDigest")
assert 1 == await modclient.tdigest().min("tDigest")
Expand All @@ -375,7 +375,7 @@ async def test_tdigest_quantile(modclient: redis.Redis):
assert await modclient.tdigest().create("tDigest", 500)
# insert data-points into sketch
assert await modclient.tdigest().add(
"tDigest", list([x * 0.01 for x in range(1, 10000)]), [1] * 10000
"tDigest", list([x * 0.01 for x in range(1, 10000)])
)
# assert min min/max have same result as quantile 0 and 1
assert (
Expand All @@ -392,7 +392,7 @@ async def test_tdigest_quantile(modclient: redis.Redis):

# test multiple quantiles
assert await modclient.tdigest().create("t-digest", 100)
assert await modclient.tdigest().add("t-digest", [1, 2, 3, 4, 5], [1] * 5)
assert await modclient.tdigest().add("t-digest", [1, 2, 3, 4, 5])
res = await modclient.tdigest().quantile("t-digest", 0.5, 0.8)
assert [3.0, 5.0] == res

Expand All @@ -402,7 +402,7 @@ async def test_tdigest_quantile(modclient: redis.Redis):
async def test_tdigest_cdf(modclient: redis.Redis):
assert await modclient.tdigest().create("tDigest", 100)
# insert data-points into sketch
assert await modclient.tdigest().add("tDigest", list(range(1, 10)), [1] * 10)
assert await modclient.tdigest().add("tDigest", list(range(1, 10)))
assert 0.1 == round((await modclient.tdigest().cdf("tDigest", 1.0))[0], 1)
assert 0.9 == round((await modclient.tdigest().cdf("tDigest", 9.0))[0], 1)
res = await modclient.tdigest().cdf("tDigest", 1.0, 9.0)
Expand All @@ -415,7 +415,7 @@ async def test_tdigest_cdf(modclient: redis.Redis):
async def test_tdigest_trimmed_mean(modclient: redis.Redis):
assert await modclient.tdigest().create("tDigest", 100)
# insert data-points into sketch
assert await modclient.tdigest().add("tDigest", list(range(1, 10)), [1] * 10)
assert await modclient.tdigest().add("tDigest", list(range(1, 10)))
assert 5 == await modclient.tdigest().trimmed_mean("tDigest", 0.1, 0.9)
assert 4.5 == await modclient.tdigest().trimmed_mean("tDigest", 0.4, 0.5)

Expand All @@ -424,7 +424,7 @@ async def test_tdigest_trimmed_mean(modclient: redis.Redis):
@pytest.mark.experimental
async def test_tdigest_rank(modclient: redis.Redis):
assert await modclient.tdigest().create("t-digest", 500)
assert await modclient.tdigest().add("t-digest", list(range(0, 20)), [1] * 20)
assert await modclient.tdigest().add("t-digest", list(range(0, 20)))
assert -1 == (await modclient.tdigest().rank("t-digest", -1))[0]
assert 1 == (await modclient.tdigest().rank("t-digest", 0))[0]
assert 11 == (await modclient.tdigest().rank("t-digest", 10))[0]
Expand All @@ -435,17 +435,17 @@ async def test_tdigest_rank(modclient: redis.Redis):
@pytest.mark.experimental
async def test_tdigest_revrank(modclient: redis.Redis):
assert await modclient.tdigest().create("t-digest", 500)
assert await modclient.tdigest().add("t-digest", list(range(0, 20)), [1] * 20)
assert await modclient.tdigest().add("t-digest", list(range(0, 20)))
assert -1 == (await modclient.tdigest().revrank("t-digest", 20))[0]
assert 20 == (await modclient.tdigest().revrank("t-digest", 0))[0]
assert [-1, 20, 10] == await modclient.tdigest().revrank("t-digest", 21, 0, 10)
assert 19 == (await modclient.tdigest().revrank("t-digest", 0))[0]
assert [-1, 19, 9] == await modclient.tdigest().revrank("t-digest", 21, 0, 10)


@pytest.mark.redismod
@pytest.mark.experimental
async def test_tdigest_byrank(modclient: redis.Redis):
assert await modclient.tdigest().create("t-digest", 500)
assert await modclient.tdigest().add("t-digest", list(range(1, 11)), [1] * 20)
assert await modclient.tdigest().add("t-digest", list(range(1, 11)))
assert 1 == (await modclient.tdigest().byrank("t-digest", 0))[0]
assert 10 == (await modclient.tdigest().byrank("t-digest", 9))[0]
assert (await modclient.tdigest().byrank("t-digest", 100))[0] == inf
Expand All @@ -457,9 +457,9 @@ async def test_tdigest_byrank(modclient: redis.Redis):
@pytest.mark.experimental
async def test_tdigest_byrevrank(modclient: redis.Redis):
assert await modclient.tdigest().create("t-digest", 500)
assert await modclient.tdigest().add("t-digest", list(range(1, 11)), [1] * 20)
assert await modclient.tdigest().add("t-digest", list(range(1, 11)))
assert 10 == (await modclient.tdigest().byrevrank("t-digest", 0))[0]
assert 2 == (await modclient.tdigest().byrevrank("t-digest", 9))[0]
assert 1 == (await modclient.tdigest().byrevrank("t-digest", 9))[0]
assert (await modclient.tdigest().byrevrank("t-digest", 100))[0] == -inf
with pytest.raises(redis.ResponseError):
(await modclient.tdigest().byrevrank("t-digest", -1))[0]
Expand Down
38 changes: 18 additions & 20 deletions tests/test_bloom.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ def test_tdigest_reset(client):
# reset on empty histogram
assert client.tdigest().reset("tDigest")
# insert data-points into sketch
assert client.tdigest().add("tDigest", list(range(10)), [1] * 10)
assert client.tdigest().add("tDigest", list(range(10)))

assert client.tdigest().reset("tDigest")
# assert we have 0 unmerged nodes
Expand All @@ -352,19 +352,19 @@ def test_tdigest_merge(client):
assert client.tdigest().create("to-tDigest", 10)
assert client.tdigest().create("from-tDigest", 10)
# insert data-points into sketch
assert client.tdigest().add("from-tDigest", [1.0] * 10, [1] * 10)
assert client.tdigest().add("to-tDigest", [2.0] * 10, [10] * 10)
assert client.tdigest().add("from-tDigest", [1.0] * 10)
assert client.tdigest().add("to-tDigest", [2.0] * 10)
# merge from-tdigest into to-tdigest
assert client.tdigest().merge("to-tDigest", 1, "from-tDigest")
# we should now have 110 weight on to-histogram
info = client.tdigest().info("to-tDigest")
total_weight_to = float(info.merged_weight) + float(info.unmerged_weight)
assert 110 == total_weight_to
assert 20 == total_weight_to
# test override
assert client.tdigest().create("from-override", 10)
assert client.tdigest().create("from-override-2", 10)
assert client.tdigest().add("from-override", [3.0] * 10, [10] * 10)
assert client.tdigest().add("from-override-2", [4.0] * 10, [10] * 10)
assert client.tdigest().add("from-override", [3.0] * 10)
assert client.tdigest().add("from-override-2", [4.0] * 10)
assert client.tdigest().merge(
"to-tDigest", 2, "from-override", "from-override-2", override=True
)
Expand All @@ -377,7 +377,7 @@ def test_tdigest_merge(client):
def test_tdigest_min_and_max(client):
assert client.tdigest().create("tDigest", 100)
# insert data-points into sketch
assert client.tdigest().add("tDigest", [1, 2, 3], [1] * 3)
assert client.tdigest().add("tDigest", [1, 2, 3])
# min/max
assert 3 == client.tdigest().max("tDigest")
assert 1 == client.tdigest().min("tDigest")
Expand All @@ -389,9 +389,7 @@ def test_tdigest_min_and_max(client):
def test_tdigest_quantile(client):
assert client.tdigest().create("tDigest", 500)
# insert data-points into sketch
assert client.tdigest().add(
"tDigest", list([x * 0.01 for x in range(1, 10000)]), [1] * 10000
)
assert client.tdigest().add("tDigest", list([x * 0.01 for x in range(1, 10000)]))
# assert min min/max have same result as quantile 0 and 1
res = client.tdigest().quantile("tDigest", 1.0)
assert client.tdigest().max("tDigest") == res[0]
Expand All @@ -403,7 +401,7 @@ def test_tdigest_quantile(client):

# test multiple quantiles
assert client.tdigest().create("t-digest", 100)
assert client.tdigest().add("t-digest", [1, 2, 3, 4, 5], [1] * 5)
assert client.tdigest().add("t-digest", [1, 2, 3, 4, 5])
assert [3.0, 5.0] == client.tdigest().quantile("t-digest", 0.5, 0.8)


Expand All @@ -412,7 +410,7 @@ def test_tdigest_quantile(client):
def test_tdigest_cdf(client):
assert client.tdigest().create("tDigest", 100)
# insert data-points into sketch
assert client.tdigest().add("tDigest", list(range(1, 10)), [1] * 10)
assert client.tdigest().add("tDigest", list(range(1, 10)))
assert 0.1 == round(client.tdigest().cdf("tDigest", 1.0)[0], 1)
assert 0.9 == round(client.tdigest().cdf("tDigest", 9.0)[0], 1)
res = client.tdigest().cdf("tDigest", 1.0, 9.0)
Expand All @@ -425,7 +423,7 @@ def test_tdigest_cdf(client):
def test_tdigest_trimmed_mean(client):
assert client.tdigest().create("tDigest", 100)
# insert data-points into sketch
assert client.tdigest().add("tDigest", list(range(1, 10)), [1] * 10)
assert client.tdigest().add("tDigest", list(range(1, 10)))
assert 5 == client.tdigest().trimmed_mean("tDigest", 0.1, 0.9)
assert 4.5 == client.tdigest().trimmed_mean("tDigest", 0.4, 0.5)

Expand All @@ -434,7 +432,7 @@ def test_tdigest_trimmed_mean(client):
@pytest.mark.experimental
def test_tdigest_rank(client):
assert client.tdigest().create("t-digest", 500)
assert client.tdigest().add("t-digest", list(range(0, 20)), [1] * 20)
assert client.tdigest().add("t-digest", list(range(0, 20)))
assert -1 == client.tdigest().rank("t-digest", -1)[0]
assert 1 == client.tdigest().rank("t-digest", 0)[0]
assert 11 == client.tdigest().rank("t-digest", 10)[0]
Expand All @@ -445,17 +443,17 @@ def test_tdigest_rank(client):
@pytest.mark.experimental
def test_tdigest_revrank(client):
assert client.tdigest().create("t-digest", 500)
assert client.tdigest().add("t-digest", list(range(0, 20)), [1] * 20)
assert client.tdigest().add("t-digest", list(range(0, 20)))
assert -1 == client.tdigest().revrank("t-digest", 20)[0]
assert 20 == client.tdigest().revrank("t-digest", 0)[0]
assert [-1, 20, 10] == client.tdigest().revrank("t-digest", 21, 0, 10)
assert 19 == client.tdigest().revrank("t-digest", 0)[0]
assert [-1, 19, 9] == client.tdigest().revrank("t-digest", 21, 0, 10)


@pytest.mark.redismod
@pytest.mark.experimental
def test_tdigest_byrank(client):
assert client.tdigest().create("t-digest", 500)
assert client.tdigest().add("t-digest", list(range(1, 11)), [1] * 20)
assert client.tdigest().add("t-digest", list(range(1, 11)))
assert 1 == client.tdigest().byrank("t-digest", 0)[0]
assert 10 == client.tdigest().byrank("t-digest", 9)[0]
assert client.tdigest().byrank("t-digest", 100)[0] == inf
Expand All @@ -467,9 +465,9 @@ def test_tdigest_byrank(client):
@pytest.mark.experimental
def test_tdigest_byrevrank(client):
assert client.tdigest().create("t-digest", 500)
assert client.tdigest().add("t-digest", list(range(1, 11)), [1] * 20)
assert client.tdigest().add("t-digest", list(range(1, 11)))
assert 10 == client.tdigest().byrevrank("t-digest", 0)[0]
assert 2 == client.tdigest().byrevrank("t-digest", 9)[0]
assert 1 == client.tdigest().byrevrank("t-digest", 9)[0]
assert client.tdigest().byrevrank("t-digest", 100)[0] == -inf
with pytest.raises(redis.ResponseError):
client.tdigest().byrevrank("t-digest", -1)[0]
Expand Down