| 1 |
# ptufile/numcodecs.py |
| 2 |
|
| 3 |
# Copyright (c) 2024-2026, Christoph Gohlke |
| 4 |
# All rights reserved. |
| 5 |
# |
| 6 |
# Redistribution and use in source and binary forms, with or without |
| 7 |
# modification, are permitted provided that the following conditions are met: |
| 8 |
# |
| 9 |
# 1. Redistributions of source code must retain the above copyright notice, |
| 10 |
# this list of conditions and the following disclaimer. |
| 11 |
# |
| 12 |
# 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 |
# this list of conditions and the following disclaimer in the documentation |
| 14 |
# and/or other materials provided with the distribution. |
| 15 |
# |
| 16 |
# 3. Neither the name of the copyright holder nor the names of its |
| 17 |
# contributors may be used to endorse or promote products derived from |
| 18 |
# this software without specific prior written permission. |
| 19 |
# |
| 20 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 21 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 24 |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 25 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 26 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 27 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 28 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 29 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 30 |
# POSSIBILITY OF SUCH DAMAGE. |
| 31 |
|
| 32 |
"""PTU codec for the Numcodecs package.""" |
| 33 |
|
| 34 |
from __future__ import annotations |
| 35 |
|
| 36 |
__all__ = ['Ptu', 'register_codec'] |
| 37 |
|
| 38 |
from io import BytesIO |
| 39 |
from typing import TYPE_CHECKING |
| 40 |
|
| 41 |
from numcodecs import registry |
| 42 |
from numcodecs.abc import Codec |
| 43 |
|
| 44 |
from .ptufile import PtuFile |
| 45 |
|
| 46 |
if TYPE_CHECKING: |
| 47 |
from collections.abc import Sequence |
| 48 |
from types import EllipsisType |
| 49 |
from typing import Any |
| 50 |
|
| 51 |
from numpy.typing import ArrayLike, DTypeLike, NDArray |
| 52 |
|
| 53 |
|
| 54 |
class Ptu(Codec): # type: ignore[misc] |
| 55 |
"""Ptu codec for Numcodecs.""" |
| 56 |
|
| 57 |
codec_id = 'ptufile' |
| 58 |
|
| 59 |
def __init__( |
| 60 |
self, |
| 61 |
*, |
| 62 |
selection: Sequence[int | slice | EllipsisType | None] | None = None, |
| 63 |
dtype: DTypeLike | None = None, |
| 64 |
channel: int | None = None, |
| 65 |
frame: int | None = None, |
| 66 |
dtime: int | None = None, |
| 67 |
pixel_time: float | None = None, |
| 68 |
trimdims: str | None = None, |
| 69 |
keepdims: bool = True, |
| 70 |
) -> None: |
| 71 |
if selection is not None: |
| 72 |
# TODO: serialize slices, EllipsisType |
| 73 |
msg = f'{selection=}' |
| 74 |
raise NotImplementedError(msg) |
| 75 |
self.selection = selection |
| 76 |
self.dtype = dtype |
| 77 |
self.channel = channel |
| 78 |
self.frame = frame |
| 79 |
self.dtime = dtime |
| 80 |
self.pixel_time = pixel_time |
| 81 |
self.trimdims = trimdims |
| 82 |
self.keepdims = bool(keepdims) |
| 83 |
|
| 84 |
def encode(self, buf: ArrayLike) -> None: |
| 85 |
"""Return Ptu file as bytes.""" |
| 86 |
raise NotImplementedError |
| 87 |
|
| 88 |
def decode(self, buf: bytes, out: Any | None = None) -> NDArray[Any]: |
| 89 |
"""Return decoded image as NumPy array.""" |
| 90 |
with BytesIO(buf) as fh, PtuFile(fh, trimdims=self.trimdims) as ptu: |
| 91 |
return ptu.decode_image( |
| 92 |
self.selection, |
| 93 |
dtype=self.dtype, |
| 94 |
channel=self.channel, |
| 95 |
frame=self.frame, |
| 96 |
dtime=self.dtime, |
| 97 |
pixel_time=self.pixel_time, |
| 98 |
keepdims=self.keepdims, |
| 99 |
) |
| 100 |
|
| 101 |
|
| 102 |
def register_codec(cls: Codec = Ptu, codec_id: str | None = None) -> None: |
| 103 |
"""Register :py:class:`Ptu` codec with Numcodecs.""" |
| 104 |
registry.register_codec(cls, codec_id=codec_id) |