Skip to content

Commit 5bda830

Browse files
committed
Wrap delta filter
x-ref TileDB-Inc/TileDB#3969
1 parent 449828a commit 5bda830

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

tiledb/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
Bzip2Filter,
5454
ChecksumMD5Filter,
5555
ChecksumSHA256Filter,
56+
DeltaFilter,
5657
DictionaryFilter,
5758
DoubleDeltaFilter,
5859
Filter,

tiledb/cc/enum.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ void init_enums(py::module &m) {
8787
.value("LZ4", TILEDB_FILTER_LZ4)
8888
.value("RLE", TILEDB_FILTER_RLE)
8989
.value("BZIP2", TILEDB_FILTER_BZIP2)
90+
.value("DELTA", TILEDB_FILTER_DELTA)
9091
.value("DOUBLE_DELTA", TILEDB_FILTER_DOUBLE_DELTA)
9192
.value("BIT_WIDTH_REDUCTION", TILEDB_FILTER_BIT_WIDTH_REDUCTION)
9293
.value("BITSHUFFLE", TILEDB_FILTER_BITSHUFFLE)

tiledb/filter.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,35 @@ def _attrs_(self):
249249
return {}
250250

251251

252+
class DeltaFilter(CompressionFilter):
253+
"""
254+
Filter that compresses using run-length encoding (RLE).
255+
256+
:param level: -1 (default) sets the compressor level to the default level as specified in TileDB core. Otherwise, sets the compressor level to the given value.
257+
:type level: int
258+
259+
**Example:**
260+
261+
>>> import tiledb, numpy as np, tempfile
262+
>>> with tempfile.TemporaryDirectory() as tmp:
263+
... dom = tiledb.Domain(tiledb.Dim(domain=(0, 9), tile=2, dtype=np.uint64))
264+
... a1 = tiledb.Attr(name="a1", dtype=np.int64,
265+
... filters=tiledb.FilterList([tiledb.RleFilter()]))
266+
... schema = tiledb.ArraySchema(domain=dom, attrs=(a1,))
267+
... tiledb.DenseArray.create(tmp + "/array", schema)
268+
269+
"""
270+
271+
def __init__(self, level: int = -1, ctx: Optional[Ctx] = None):
272+
if not isinstance(level, int):
273+
raise ValueError("`level` argument must be a int")
274+
275+
super().__init__(lt.FilterType.DELTA, level, ctx)
276+
277+
def _attrs_(self):
278+
return {}
279+
280+
252281
class DoubleDeltaFilter(CompressionFilter):
253282
"""
254283
Filter that performs double-delta encoding.
@@ -737,6 +766,7 @@ class FilterList(CtxMixin, lt.FilterList):
737766
lt.FilterType.LZ4: LZ4Filter,
738767
lt.FilterType.BZIP2: Bzip2Filter,
739768
lt.FilterType.RLE: RleFilter,
769+
lt.FilterType.DELTA: DeltaFilter,
740770
lt.FilterType.DOUBLE_DELTA: DoubleDeltaFilter,
741771
lt.FilterType.BIT_WIDTH_REDUCTION: BitWidthReductionFilter,
742772
lt.FilterType.BITSHUFFLE: BitShuffleFilter,

tiledb/tests/test_filters.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
tiledb.LZ4Filter,
1616
tiledb.RleFilter,
1717
tiledb.Bzip2Filter,
18+
tiledb.DeltaFilter,
1819
tiledb.DoubleDeltaFilter,
1920
tiledb.DictionaryFilter,
2021
tiledb.BitWidthReductionFilter,
@@ -157,3 +158,23 @@ def test_float_scaling_filter(self, factor, offset, bytewidth):
157158

158159
# TODO compute the correct tolerance here
159160
assert_allclose(data, A[:][""], rtol=1, atol=1)
161+
162+
def test_delta_filter(self):
163+
path = self.path("test_delta_filter")
164+
165+
dom = tiledb.Domain(tiledb.Dim(name="row", domain=(0, 9), dtype=np.uint64))
166+
167+
filter = tiledb.DeltaFilter()
168+
169+
attr = tiledb.Attr(dtype=np.int64, filters=tiledb.FilterList([filter]))
170+
schema = tiledb.ArraySchema(domain=dom, attrs=[attr], sparse=False)
171+
tiledb.Array.create(path, schema)
172+
173+
data = np.random.randint(0, 10_000_000, size=10)
174+
175+
with tiledb.open(path, "w") as A:
176+
A[:] = data
177+
178+
with tiledb.open(path) as A:
179+
res = A[:]
180+
assert_array_equal(res, data)

0 commit comments

Comments
 (0)