File: transformations.py

package info (click to toggle)
python-sigima 1.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 25,608 kB
  • sloc: python: 35,251; makefile: 3
file content (394 lines) | stat: -rw-r--r-- 13,186 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# Copyright (c) DataLab Platform Developers, BSD 3-Clause license, see LICENSE file.

"""
Geometry transformations module
===============================

This module provides a unified interface for applying geometric transformations
to both geometry results (:class:`sigima.objects.GeometryResult`) and ROI objects
using the shape coordinate system (:mod:`sigima.objects.shape`).
"""

from __future__ import annotations

from typing import TYPE_CHECKING, Any

import numpy as np

from sigima.objects.scalar import GeometryResult, KindShape
from sigima.objects.shape import (
    CircleCoordinates,
    EllipseCoordinates,
    PointCoordinates,
    PolygonCoordinates,
    RectangleCoordinates,
    SegmentCoordinates,
)

if TYPE_CHECKING:
    from sigima.objects import CircularROI, ImageObj, PolygonalROI, RectangularROI


__all__ = [
    "GeometryTransformer",
    "transformer",
]


class GeometryTransformer:
    """
    Singleton class for applying transformations to geometry objects.

    Provides a unified interface for transforming both GeometryResult and ROI
    objects using the shape coordinate system.
    """

    _instance: GeometryTransformer | None = None

    def __new__(cls) -> GeometryTransformer:
        """Ensure singleton pattern."""
        if cls._instance is None:
            cls._instance = super().__new__(cls)
            cls._instance._initialized = False
        return cls._instance

    def __init__(self) -> None:
        """Initialize the transformer (only once due to singleton)."""
        if self._initialized:  # pylint: disable=access-member-before-definition
            return

        # Mapping from GeometryResult kinds to shape coordinate classes
        self._geometry_shape_map: dict[
            KindShape,
            type[
                RectangleCoordinates
                | CircleCoordinates
                | PolygonCoordinates
                | SegmentCoordinates
            ],
        ] = {
            KindShape.POINT: PointCoordinates,
            KindShape.RECTANGLE: RectangleCoordinates,
            KindShape.CIRCLE: CircleCoordinates,
            KindShape.ELLIPSE: EllipseCoordinates,
            KindShape.POLYGON: PolygonCoordinates,
            KindShape.SEGMENT: SegmentCoordinates,
            KindShape.MARKER: PointCoordinates,
        }

        # Mapping from ROI types to shape coordinate classes (lazy loaded)
        self._roi_shape_map: dict[type, type] = {}

        self._initialized = True

    def _get_roi_shape_map(
        self,
    ) -> dict[
        type[CircularROI | RectangularROI | PolygonalROI],
        type[RectangleCoordinates | CircleCoordinates | PolygonCoordinates],
    ]:
        """Lazy load ROI shape mapping to avoid circular imports."""
        if not self._roi_shape_map:
            # pylint: disable=import-outside-toplevel
            from sigima.objects.image import CircularROI, PolygonalROI, RectangularROI

            self._roi_shape_map = {
                RectangularROI: RectangleCoordinates,
                CircularROI: CircleCoordinates,
                PolygonalROI: PolygonCoordinates,
            }
        return self._roi_shape_map

    def transform_geometry(
        self, geometry: GeometryResult, operation: str, **kwargs: Any
    ) -> GeometryResult:
        """
        Transform a GeometryResult and return a new one.

        Args:
            geometry: The GeometryResult to transform.
            operation: Operation name ('rotate', 'translate', 'fliph', 'flipv',
                      'transpose', 'scale').
            **kwargs: Operation-specific parameters.

        Returns:
            New GeometryResult with transformed coordinates.

        Raises:
            ValueError: If operation is unknown or geometry kind is unsupported.
        """
        coord_class = self._geometry_shape_map.get(geometry.kind)
        if coord_class is None:
            raise ValueError(f"Unsupported geometry kind: {geometry.kind}")

        # Transform each row of coordinates
        transformed_coords = []
        for row in geometry.coords:
            # Create coordinate object for this row
            shape_coords = coord_class(row.copy())

            # Apply transformation
            self._apply_operation(shape_coords, operation, **kwargs)

            transformed_coords.append(shape_coords.data)

        # Create new GeometryResult with transformed coordinates
        return GeometryResult(
            title=geometry.title,
            kind=geometry.kind,
            coords=np.array(transformed_coords),
            roi_indices=(
                geometry.roi_indices.copy()
                if geometry.roi_indices is not None
                else None
            ),
            attrs=geometry.attrs.copy(),
            func_name=geometry.func_name,
        )

    def transform_single_roi(
        self,
        single_roi: RectangularROI | CircularROI | PolygonalROI,
        operation: str,
        **kwargs: Any,
    ) -> None:
        """
        Transform ROI coordinates inplace.

        Args:
            single_roi: ROI object with .coords attribute.
            operation: Operation name.
            **kwargs: Operation-specific parameters.

        Raises:
            ValueError: If ROI type is unsupported or operation is unknown.
        """
        roi_shape_map = self._get_roi_shape_map()
        coord_class = roi_shape_map.get(type(single_roi))
        if coord_class is None:
            raise ValueError(f"Unsupported ROI type: {type(single_roi)}")

        # Create shape coordinates and transform
        shape_coords = coord_class(single_roi.coords.copy())
        self._apply_operation(shape_coords, operation, **kwargs)

        # Update ROI coordinates inplace
        single_roi.coords[:] = shape_coords.data

    def transform_roi(self, image: ImageObj, operation: str, **kwargs: Any) -> None:
        """
        Transform all ROI coordinates in an ImageObj inplace.

        Args:
            image: Image object whose ROI coordinates will be transformed.
            operation: Operation name.
            **kwargs: Operation-specific parameters.
        """
        if image.roi is None or image.roi.is_empty():
            return

        # Import here to avoid circular imports
        # pylint: disable=import-outside-toplevel
        from sigima.objects.image import ImageROI

        # Determine ROI type and set up appropriate classes
        new_roi = ImageROI()

        # Transform each single ROI
        for single_roi in image.roi.single_rois:
            coords = single_roi.coords.copy()
            roi_class = single_roi.__class__

            # Create shape coordinates and transform
            roi_shape_map = self._get_roi_shape_map()
            coord_class = roi_shape_map.get(roi_class)
            if coord_class is None:
                raise ValueError(f"Unsupported ROI type: {roi_class}")

            shape_coords = coord_class(coords)
            self._apply_operation(shape_coords, operation, **kwargs)

            new_coords = shape_coords.data
            new_single_roi = roi_class(new_coords, single_roi.indices, single_roi.title)
            new_roi.add_roi(new_single_roi)

        image.roi = new_roi

    def _apply_operation(
        self,
        shape_coords: (
            PointCoordinates
            | RectangleCoordinates
            | CircleCoordinates
            | EllipseCoordinates
            | PolygonCoordinates
            | SegmentCoordinates
        ),
        operation: str,
        **kwargs: Any,
    ) -> None:
        """
        Apply the specified operation to shape coordinates.

        Args:
            shape_coords: Shape coordinate object to transform.
            operation: Operation name.
            **kwargs: Operation-specific parameters.

        Raises:
            ValueError: If operation is unknown.
        """
        if operation == "rotate":
            angle = kwargs.get("angle", 0)
            center = kwargs.get("center", (0, 0))
            shape_coords.rotate(angle, center)
        elif operation == "translate":
            dx = kwargs.get("dx", 0)
            dy = kwargs.get("dy", 0)
            shape_coords.translate(dx, dy)
        elif operation == "fliph":
            cx = kwargs.get("cx", 0.0)
            shape_coords.fliph(cx)
        elif operation == "flipv":
            cy = kwargs.get("cy", 0.0)
            shape_coords.flipv(cy)
        elif operation == "transpose":
            shape_coords.transpose()
        elif operation == "scale":
            sx = kwargs.get("sx", 1.0)
            sy = kwargs.get("sy", 1.0)
            center = kwargs.get("center", (0, 0))
            shape_coords.scale(sx, sy, center)
        else:
            raise ValueError(f"Unknown operation: {operation}")

    # Convenience methods for common operations
    def rotate(
        self,
        obj: GeometryResult | RectangularROI | CircularROI | PolygonalROI,
        angle: float,
        center: tuple[float, float],
    ) -> GeometryResult | None:
        """
        Rotate geometry or ROI by given angle around center.

        Args:
            obj: GeometryResult or single ROI object.
            angle: Rotation angle in radians.
            center: Center of rotation (x, y).

        Returns:
            New GeometryResult if input was GeometryResult, None if ROI (inplace).
        """
        if isinstance(obj, GeometryResult):
            return self.transform_geometry(obj, "rotate", angle=angle, center=center)
        self.transform_single_roi(obj, "rotate", angle=angle, center=center)
        return None

    def translate(
        self,
        obj: GeometryResult | RectangularROI | CircularROI | PolygonalROI,
        dx: float,
        dy: float,
    ) -> GeometryResult | None:
        """
        Translate geometry or ROI by given offset.

        Args:
            obj: GeometryResult or single ROI object.
            dx: Translation in x direction.
            dy: Translation in y direction.

        Returns:
            New GeometryResult if input was GeometryResult, None if ROI (inplace).
        """
        if isinstance(obj, GeometryResult):
            return self.transform_geometry(obj, "translate", dx=dx, dy=dy)
        self.transform_single_roi(obj, "translate", dx=dx, dy=dy)
        return None

    def fliph(
        self,
        obj: GeometryResult | RectangularROI | CircularROI | PolygonalROI,
        cx: float,
    ) -> GeometryResult | None:
        """
        Flip geometry or ROI horizontally around given x-coordinate.

        Args:
            obj: GeometryResult or single ROI object.
            cx: X-coordinate of flip axis.

        Returns:
            New GeometryResult if input was GeometryResult, None if ROI (inplace).
        """
        if isinstance(obj, GeometryResult):
            return self.transform_geometry(obj, "fliph", cx=cx)
        self.transform_single_roi(obj, "fliph", cx=cx)
        return None

    def flipv(
        self,
        obj: GeometryResult | RectangularROI | CircularROI | PolygonalROI,
        cy: float,
    ) -> GeometryResult | None:
        """
        Flip geometry or ROI vertically around given y-coordinate.

        Args:
            obj: GeometryResult or single ROI object.
            cy: Y-coordinate of flip axis.

        Returns:
            New GeometryResult if input was GeometryResult, None if ROI (inplace).
        """
        if isinstance(obj, GeometryResult):
            return self.transform_geometry(obj, "flipv", cy=cy)
        self.transform_single_roi(obj, "flipv", cy=cy)
        return None

    def transpose(
        self, obj: GeometryResult | RectangularROI | CircularROI | PolygonalROI
    ) -> GeometryResult | None:
        """
        Transpose geometry or ROI (swap x and y coordinates).

        Args:
            obj: GeometryResult or single ROI object.

        Returns:
            New GeometryResult if input was GeometryResult, None if ROI (inplace).
        """
        if isinstance(obj, GeometryResult):
            return self.transform_geometry(obj, "transpose")
        self.transform_single_roi(obj, "transpose")
        return None

    def scale(
        self,
        obj: GeometryResult | RectangularROI | CircularROI | PolygonalROI,
        sx: float,
        sy: float,
        center: tuple[float, float],
    ) -> GeometryResult | None:
        """
        Scale geometry or ROI by given factors around center.

        Args:
            obj: GeometryResult or single ROI object.
            sx: Scale factor in x direction.
            sy: Scale factor in y direction.
            center: Center of scaling (x, y).

        Returns:
            New GeometryResult if input was GeometryResult, None if ROI (inplace).
        """
        if isinstance(obj, GeometryResult):
            return self.transform_geometry(obj, "scale", sx=sx, sy=sy, center=center)
        self.transform_single_roi(obj, "scale", sx=sx, sy=sy, center=center)
        return None


#: Global singleton instance of GeometryTransformer for applying geometric
#: transformations to geometry results and ROI objects.
transformer = GeometryTransformer()