/ proof-of-concept / benchcompress
Sign in
proof-of-concept / benchcompress
106 lines · 3.0 KBBlameHistoryRaw
1import numpy as np
2import os
5SOURCE_FILE = "lz4/__init__.py"
8def _load_long_description():
9 current_dir = os.path.dirname(os.path.abspath(__file__))
10 md_path = os.path.join(current_dir, "lz4.md")
11 with open(md_path, "r", encoding="utf-8") as f:
12 return f.read()
15LONG_DESCRIPTION = _load_long_description()
18def lz4_encode(x: np.ndarray, level: int) -> bytes:
19 import lz4.frame
21 buf = x.tobytes()
22 compressed = lz4.frame.compress(buf, compression_level=level)
23 return compressed
26def lz4_decode(x: bytes, dtype: str, shape: tuple) -> np.ndarray:
27 import lz4.frame
29 buf = lz4.frame.decompress(x)
30 y = np.frombuffer(buf, dtype=dtype)
31 return y.reshape(shape)
34def lz4_delta_encode(x: np.ndarray, level: int) -> bytes:
35 import lz4.frame
37 assert x.ndim == 1
38 y = np.diff(x)
39 y = np.insert(y, 0, x[0])
40 buf = y.tobytes()
41 compressed = lz4.frame.compress(buf, compression_level=level)
42 return compressed
45def lz4_delta_decode(x: bytes, dtype: str, shape: tuple) -> np.ndarray:
46 import lz4.frame
48 assert len(shape) == 1
50 buf = lz4.frame.decompress(x)
51 y = np.frombuffer(buf, dtype=dtype)
52 return np.cumsum(y)
55algorithms = [
56 {
57 "name": "lz4-0",
58 "version": "1",
59 "encode": lambda x: lz4_encode(x, level=0),
60 "decode": lambda x, dtype, shape: lz4_decode(x, dtype, shape),
61 "description": "LZ4 compression at level 0 (fastest).",
62 "tags": ["lz4"],
63 "source_file": SOURCE_FILE,
64 "long_description": LONG_DESCRIPTION,
65 },
66 {
67 "name": "lz4-3",
68 "version": "1",
69 "encode": lambda x: lz4_encode(x, level=3),
70 "decode": lambda x, dtype, shape: lz4_decode(x, dtype, shape),
71 "description": "LZ4 compression at level 3 (minimum high compression).",
72 "tags": ["lz4"],
73 "source_file": SOURCE_FILE,
74 "long_description": LONG_DESCRIPTION,
75 },
76 {
77 "name": "lz4-10",
78 "version": "1",
79 "encode": lambda x: lz4_encode(x, level=10),
80 "decode": lambda x, dtype, shape: lz4_decode(x, dtype, shape),
81 "description": "LZ4 compression at level 10.",
82 "tags": ["lz4"],
83 "source_file": SOURCE_FILE,
84 "long_description": LONG_DESCRIPTION,
85 },
86 {
87 "name": "lz4-16",
88 "version": "1",
89 "encode": lambda x: lz4_encode(x, level=16),
90 "decode": lambda x, dtype, shape: lz4_decode(x, dtype, shape),
91 "description": "LZ4 compression at level 16 (highest compression).",
92 "tags": ["lz4"],
93 "source_file": SOURCE_FILE,
94 "long_description": LONG_DESCRIPTION,
95 },
96 {
97 "name": "lz4-16-delta",
98 "version": "1",
99 "encode": lambda x: lz4_delta_encode(x, level=16),
100 "decode": lambda x, dtype, shape: lz4_delta_decode(x, dtype, shape),
101 "description": "LZ4 compression at level 16 with delta encoding.",
102 "tags": ["lz4", "delta_encoding", "1d"],
103 "source_file": SOURCE_FILE,
104 "long_description": LONG_DESCRIPTION,
105 },
↑↓ move↵openescclose