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

Upgraded pcodec to 0.2 and used new ModeSpec configuration #544

Merged
merged 5 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 2 additions & 3 deletions fixture/pcodec/codec.00/config.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"delta_encoding_order": null,
"equal_pages_up_to": 262144,
"float_mult_spec": "enabled",
"id": "pcodec",
"int_mult_spec": "enabled",
"level": 8
"level": 8,
"mode_spec": "auto"
}
5 changes: 2 additions & 3 deletions fixture/pcodec/codec.01/config.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"delta_encoding_order": null,
"equal_pages_up_to": 262144,
"float_mult_spec": "enabled",
"id": "pcodec",
"int_mult_spec": "enabled",
"level": 1
"level": 1,
"mode_spec": "auto"
}
5 changes: 2 additions & 3 deletions fixture/pcodec/codec.02/config.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"delta_encoding_order": null,
"equal_pages_up_to": 262144,
"float_mult_spec": "enabled",
"id": "pcodec",
"int_mult_spec": "enabled",
"level": 5
"level": 5,
"mode_spec": "auto"
}
5 changes: 2 additions & 3 deletions fixture/pcodec/codec.03/config.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"delta_encoding_order": null,
"equal_pages_up_to": 262144,
"float_mult_spec": "enabled",
"id": "pcodec",
"int_mult_spec": "enabled",
"level": 9
"level": 9,
"mode_spec": "auto"
}
5 changes: 2 additions & 3 deletions fixture/pcodec/codec.04/config.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"delta_encoding_order": null,
"equal_pages_up_to": 262144,
"float_mult_spec": "disabled",
"id": "pcodec",
"int_mult_spec": "disabled",
"level": 8
"level": 8,
"mode_spec": "classic"
}
5 changes: 2 additions & 3 deletions fixture/pcodec/codec.05/config.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"delta_encoding_order": null,
"equal_pages_up_to": 300,
"float_mult_spec": "enabled",
"id": "pcodec",
"int_mult_spec": "enabled",
"level": 8
"level": 8,
"mode_spec": "auto"
}
36 changes: 19 additions & 17 deletions numcodecs/pcodec.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from numcodecs.compat import ensure_contiguous_ndarray

try:
from pcodec import standalone, ChunkConfig, PagingSpec
from pcodec import standalone, ChunkConfig, PagingSpec, ModeSpec
except ImportError: # pragma: no cover
standalone = None

Expand All @@ -20,8 +20,8 @@

See `PCodec Repo <https://github.com/mwlon/pcodec>`_ for more information.

PCodec supports only the following numerical dtypes: uint32, unit64, int32,
int64, float32, and float64.
PCodec supports only the following numerical dtypes: uint16, uint32, uint64,
int16, int32, int64, float16, float32, and float64.

Parameters
----------
Expand All @@ -31,14 +31,11 @@
delta_encoding_order : init or None
Either a delta encoding level from 0-7 or None. If set to None, pcodec
will try to infer the optimal delta encoding order.
int_mult_spec : {'enabled', 'disabled'}
If enabled, pcodec will consider using int mult mode, which can
substantially improve compression ratio but decrease speed in some cases
for integer types.
float_mult_spec : {'enabled', 'disabled'}
If enabled, pcodec will consider using float mult mode, which can
substantially improve compression ratio but decrease speed in some cases
for float types.
mode_spec : {'auto', 'classic'}
Configures whether Pcodec should try to infer the best "mode" or
structure of the data (e.g. approximate multiples of 0.1) to improve
compression ratio, or skip this step and just use the numbers as-is
(Classic mode).
equal_pages_up_to : int
Divide the chunk into equal pages of up to this many numbers.
"""
Expand All @@ -49,9 +46,9 @@
self,
level: int = 8,
delta_encoding_order: Optional[int] = None,
int_mult_spec: Literal["enabled", "disabled"] = "enabled",
float_mult_spec: Literal["enabled", "disabled"] = "enabled",
equal_pages_up_to: int = 262144,
# TODO one day, add support for the Try* mode specs
mode_spec: Literal['auto', 'classic'] = 'auto',
):
if standalone is None: # pragma: no cover
raise ImportError("pcodec must be installed to use the PCodec codec.")
Expand All @@ -60,20 +57,25 @@
# match other codecs
self.level = level
self.delta_encoding_order = delta_encoding_order
self.int_mult_spec = int_mult_spec
self.float_mult_spec = float_mult_spec
self.equal_pages_up_to = equal_pages_up_to
self.mode_spec = mode_spec

def encode(self, buf):
buf = ensure_contiguous_ndarray(buf)

match self.mode_spec:
case 'auto':
mode_spec = ModeSpec.auto()
case 'classic':
mode_spec = ModeSpec.classic()
case _:
raise ValueError(f"unknown value for mode_spec: {self.mode_spec}")

Check warning on line 72 in numcodecs/pcodec.py

View check run for this annotation

Codecov / codecov/patch

numcodecs/pcodec.py#L71-L72

Added lines #L71 - L72 were not covered by tests
paging_spec = PagingSpec.equal_pages_up_to(self.equal_pages_up_to)

config = ChunkConfig(
compression_level=self.level,
delta_encoding_order=self.delta_encoding_order,
int_mult_spec=self.int_mult_spec,
float_mult_spec=self.float_mult_spec,
mode_spec=mode_spec,
paging_spec=paging_spec,
)
return standalone.simple_compress(buf, config)
Expand Down
5 changes: 2 additions & 3 deletions numcodecs/tests/test_pcodec.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
PCodec(level=1),
PCodec(level=5),
PCodec(level=9),
PCodec(float_mult_spec="disabled", int_mult_spec="disabled"),
PCodec(mode_spec='classic'),
PCodec(equal_pages_up_to=300),
]

Expand Down Expand Up @@ -59,8 +59,7 @@ def test_config():

def test_repr():
check_repr(
"PCodec(delta_encoding_order=None, equal_pages_up_to=262144, float_mult_spec='enabled', "
"int_mult_spec='enabled', level=3)"
"PCodec(delta_encoding_order=None, equal_pages_up_to=262144, level=3, mode_spec='auto')"
)


Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ zfpy = [
"numpy<2.0.0",
]
pcodec = [
"pcodec>=0.1.0",
"pcodec>=0.2.0",
]

[tool.setuptools]
Expand Down