1import numpy as np
2import os
5SOURCE_FILE = "lzma/__init__.py"
8def _load_long_description():
9 current_dir = os.path.dirname(os.path.abspath(__file__))
10 md_path = os.path.join(current_dir, "lzma.md")
11 with open(md_path, "r", encoding="utf-8") as f:
12 return f.read()
15LONG_DESCRIPTION = _load_long_description()
18def lzma_delta_encode(x: np.ndarray, preset: int) -> bytes:
19 import lzma
21 assert x.ndim == 1
22 y = np.diff(x)
23 y = np.insert(y, 0, x[0])
24 buf = y.tobytes()
25 compressed = lzma.compress(buf, preset=preset)
26 return compressed
29def lzma_delta_decode(x: bytes, dtype: str, shape: tuple) -> np.ndarray:
30 import lzma
32 assert len(shape) == 1
34 buf = lzma.decompress(x)
35 y = np.frombuffer(buf, dtype=dtype)
36 return np.cumsum(y)
39def lzma_encode(x: np.ndarray, preset: int) -> bytes:
40 import lzma
42 buf = x.tobytes()
43 compressed = lzma.compress(buf, preset=preset)
44 return compressed
47def lzma_decode(x: bytes, dtype: str, shape: tuple) -> np.ndarray:
48 import lzma
50 buf = lzma.decompress(x)
51 y = np.frombuffer(buf, dtype=dtype)
52 return y.reshape(shape)
55algorithms = [
56 {
57 "name": "lzma-9",
58 "version": "1",
59 "encode": lambda x: lzma_encode(x, preset=9),
60 "decode": lambda x, dtype, shape: lzma_decode(x, dtype, shape),
61 "description": "LZMA compression at maximum preset 9 for highest compression ratio.",
62 "tags": ["lzma"],
63 "source_file": SOURCE_FILE,
64 "long_description": LONG_DESCRIPTION,
65 },
66 {
67 "name": "lzma-9-delta",
68 "version": "1",
69 "encode": lambda x: lzma_delta_encode(x, preset=9),
70 "decode": lambda x, dtype, shape: lzma_delta_decode(x, dtype, shape),
71 "description": "LZMA compression at preset 9 with delta encoding for improved compression of sequential data.",
72 "tags": ["lzma", "delta_encoding", "1d"],
73 "source_file": SOURCE_FILE,
74 "long_description": LONG_DESCRIPTION,
75 },
76]