Skip to content

Commit

Permalink
Update roifile/roifile.py
Browse files Browse the repository at this point in the history
  • Loading branch information
cgohlke committed Aug 30, 2023
1 parent 3fa7f93 commit 1328b19
Showing 1 changed file with 29 additions and 32 deletions.
61 changes: 29 additions & 32 deletions roifile/roifile.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
:Author: `Christoph Gohlke <https://www.cgohlke.com>`_
:License: BSD 3-Clause
:Version: 2023.5.12
:Version: 2023.8.30
:DOI: `10.5281/zenodo.6941603 <https://doi.org/10.5281/zenodo.6941603>`_
Quickstart
Expand All @@ -65,14 +65,19 @@
This revision was tested with the following requirements and dependencies
(other versions may work):
- `CPython <https://www.python.org>`_ 3.9.13, 3.10.11, 3.11.3
- `Numpy <https://pypi.org/project/numpy/>`_ 1.23.5
- `Tifffile <https://pypi.org/project/tifffile/>`_ 2023.4.12 (optional)
- `Matplotlib <https://pypi.org/project/matplotlib/>`_ 3.7.1 (optional)
- `CPython <https://www.python.org>`_ 3.9.13, 3.10.11, 3.11.5, 3.12rc
- `Numpy <https://pypi.org/project/numpy/>`_ 1.25.2
- `Tifffile <https://pypi.org/project/tifffile/>`_ 2023.8.30 (optional)
- `Matplotlib <https://pypi.org/project/matplotlib/>`_ 3.7.2 (optional)
Revisions
---------
2023.8.30
- Fix linting issues.
- Add py.typed marker.
2023.5.12
- Improve object repr and type hints.
Expand Down Expand Up @@ -192,7 +197,7 @@

from __future__ import annotations

__version__ = '2023.5.12'
__version__ = '2023.8.30'

__all__ = [
'roiread',
Expand All @@ -206,19 +211,19 @@
'ROI_COLOR_NONE',
]

import dataclasses
import enum
import os
import struct
import sys
import dataclasses
from typing import TYPE_CHECKING

import numpy

from typing import TYPE_CHECKING

if TYPE_CHECKING:
from typing import Any, Literal, Union
from collections.abc import Iterable
from typing import Any, Literal, Union

from numpy.typing import ArrayLike, NDArray

ZipFileMode = Union[Literal['r'], Literal['w'], Literal['x'], Literal['a']]
Expand Down Expand Up @@ -268,6 +273,7 @@ def roiwrite(
for n, r in zip(name, roi):
with zf.open(n, 'w') as fh:
fh.write(r.tobytes())
return None


class ROI_TYPE(enum.IntEnum):
Expand Down Expand Up @@ -484,7 +490,7 @@ def fromfile(
import tifffile

with tifffile.TiffFile(filename) as tif:
if not tif.is_imagej:
if tif.imagej_metadata is None:
raise ValueError('file does not contain ImagejRoi')
rois = []
if 'Overlays' in tif.imagej_metadata:
Expand Down Expand Up @@ -584,10 +590,7 @@ def frombytes(
elif (
self.roitype == ROI_TYPE.LINE
or self.roitype == ROI_TYPE.FREEHAND
and (
self.subtype == ROI_SUBTYPE.ELLIPSE
or self.subtype == ROI_SUBTYPE.ROTATED_RECT
)
and self.subtype in {ROI_SUBTYPE.ELLIPSE, ROI_SUBTYPE.ROTATED_RECT}
):
(self.x1, self.y1, self.x2, self.y2) = struct.unpack(
self.byteorder + 'ffff', data[18:34]
Expand Down Expand Up @@ -733,10 +736,10 @@ def tobytes(self) -> bytes:
self.byteorder + 'hBxhhhhH',
self.version,
self.roitype.value,
numpy.array(self.top, 'i2'),
numpy.array(self.left, 'i2'),
numpy.array(self.bottom, 'i2'),
numpy.array(self.right, 'i2'),
numpy.array(self.top).astype('i2'),
numpy.array(self.left).astype('i2'),
numpy.array(self.bottom).astype('i2'),
numpy.array(self.right).astype('i2'),
self.n_coordinates if self.n_coordinates < 2**16 else 0,
)
)
Expand All @@ -754,10 +757,7 @@ def tobytes(self) -> bytes:
elif (
self.roitype == ROI_TYPE.LINE
or self.roitype == ROI_TYPE.FREEHAND
and (
self.subtype == ROI_SUBTYPE.ELLIPSE
or self.subtype == ROI_SUBTYPE.ROTATED_RECT
)
and self.subtype in {ROI_SUBTYPE.ELLIPSE, ROI_SUBTYPE.ROTATED_RECT}
):
result.append(
struct.pack(
Expand Down Expand Up @@ -947,7 +947,7 @@ def plot( # type: ignore
# TODO: use data units
if self.float_stroke_width > 0.0:
kwargs['linewidth'] = self.float_stroke_width
elif self.stroke_width > 0.0:
elif self.stroke_width > 0:
kwargs['linewidth'] = self.stroke_width
if roitype == ROI_TYPE.POINT:
if 'marker' not in kwargs:
Expand Down Expand Up @@ -1091,8 +1091,7 @@ def min_int_coord(value: int | None = None) -> int:
return -5000
if -32768 <= value <= 0:
return int(value)
else:
raise ValueError('min_int_coord out of range')
raise ValueError('min_int_coord out of range')

@property
def composite(self) -> bool:
Expand All @@ -1115,9 +1114,7 @@ def subpixelrect(self) -> bool:
return (
self.version >= 223
and self.subpixelresolution
and (
self.roitype == ROI_TYPE.RECT or self.roitype == ROI_TYPE.OVAL
)
and self.roitype in (ROI_TYPE.RECT, ROI_TYPE.OVAL)
)

@property
Expand Down Expand Up @@ -1226,7 +1223,7 @@ def test(verbose: bool = False) -> None:
roi.coordinates()
if verbose:
print(roi)
roi.__str__()
str(roi)

# re-write ROIs to a ZIP file
try:
Expand All @@ -1253,7 +1250,7 @@ def test(verbose: bool = False) -> None:
assert coords[-1][-1][-1] == 587.0
assert roi.multi_coordinates is not None
assert roi.multi_coordinates[0] == 0.0
roi.__str__()
str(roi)

roi = ImagejRoi.frompoints([[1, 2], [3, 4], [5, 6]])
assert roi == ImagejRoi.frombytes(roi.tobytes())
Expand Down Expand Up @@ -1299,7 +1296,7 @@ def test(verbose: bool = False) -> None:
roi.coordinates()
if verbose:
print(roi)
roi.__str__()
str(roi)

assert ImagejRoi() == ImagejRoi()

Expand Down

0 comments on commit 1328b19

Please sign in to comment.