Coverage for openhcs/processing/backends/lib_registry/pyclesperanto_registry.py: 51.5%
54 statements
« prev ^ index » next coverage.py v7.10.7, created at 2025-10-01 18:33 +0000
« prev ^ index » next coverage.py v7.10.7, created at 2025-10-01 18:33 +0000
1"""
2Clean Pyclesperanto Registry Implementation
4Implements clean abstraction with internal library-specific logic.
5All pyclesperanto-specific details (dtype conversions, Z-parameters, etc.)
6are handled internally without leaking into the ABC.
7"""
8from __future__ import annotations
10import inspect
11import numpy as np
12from functools import wraps
13from typing import Tuple, Callable, List, Any, Dict
14from openhcs.constants import MemoryType
15from openhcs.core.utils import optional_import
16from .unified_registry import LibraryRegistryBase, RuntimeTestingRegistryBase, ProcessingContract, FunctionMetadata
18cle = optional_import("pyclesperanto")
21class PyclesperantoRegistry(RuntimeTestingRegistryBase):
22 """Clean pyclesperanto registry with internal library-specific logic."""
24 # Library-specific exclusions extending common ones
25 EXCLUSIONS = LibraryRegistryBase.COMMON_EXCLUSIONS | {
26 'push_zyx', 'pull_zyx', 'create_zyx', 'set_wait_for_kernel_finish',
27 'get_device', 'select_device', 'list_available_devices'
28 }
30 # Modules to scan for functions
31 MODULES_TO_SCAN = [""] # Pyclesperanto functions are in main namespace
33 # Memory type for this registry
34 MEMORY_TYPE = MemoryType.PYCLESPERANTO.value
36 # Float dtype for this registry
37 FLOAT_DTYPE = np.float32
39 def __init__(self):
40 super().__init__("pyclesperanto")
41 # Internal constants for dtype handling
42 self._BINARY_FUNCTIONS = {'binary_infsup', 'binary_supinf'}
43 self._UINT8_FUNCTIONS = {'mode', 'mode_box', 'mode_sphere'}
44 self._IMAGE_PARAM_NAMES = {"src", "source", "image", "input", "src1", "input_image", "input_image0"}
46 # ===== ESSENTIAL ABC METHODS =====
47 def get_library_version(self) -> str:
48 return cle.__version__
50 def is_library_available(self) -> bool:
51 return cle is not None
53 def get_library_object(self):
54 return cle
56 def get_module_patterns(self) -> List[str]:
57 """Get module patterns for pyclesperanto (includes 'cle' alternative)."""
58 return ['pyclesperanto', 'cle']
60 # ===== HOOK IMPLEMENTATIONS =====
61 def _create_array(self, shape: Tuple[int, ...], dtype):
62 return np.random.rand(*shape).astype(dtype)
64 def _check_first_parameter(self, first_param, func_name: str) -> bool:
65 return (first_param.name.lower() in self._IMAGE_PARAM_NAMES and
66 first_param.kind in (inspect.Parameter.POSITIONAL_ONLY,
67 inspect.Parameter.POSITIONAL_OR_KEYWORD))
69 def _preprocess_input(self, image, func_name: str):
70 return self._convert_input_dtype(image, func_name)
72 def _postprocess_output(self, result, original_image, func_name: str):
73 return self._convert_output_dtype(result, original_image.dtype, func_name)
75 # ===== LIBRARY-SPECIFIC HELPER METHODS =====
76 def _convert_input_dtype(self, image, func_name):
77 """Internal dtype conversion logic."""
78 if func_name in self._BINARY_FUNCTIONS:
79 return ((image > 0.5) * 255).astype(np.uint8)
80 elif func_name in self._UINT8_FUNCTIONS:
81 return (np.clip(image, 0, 1) * 255).astype(np.uint8)
82 return image
84 def _convert_output_dtype(self, result, original_dtype, func_name):
85 """Internal output dtype conversion."""
86 if func_name in self._BINARY_FUNCTIONS or func_name in self._UINT8_FUNCTIONS:
87 if result.dtype != original_dtype:
88 if result.dtype == np.uint8 and original_dtype == np.float32:
89 return result.astype(np.float32) / 255.0
90 elif result.dtype == np.bool_ and original_dtype == np.float32:
91 return result.astype(np.float32)
92 return result
94 # ===== LIBRARY-SPECIFIC IMPLEMENTATIONS =====
95 def _stack_2d_results(self, func, test_3d):
96 """Stack 2D results using CLE."""
97 results = [func(test_3d[z]) for z in range(test_3d.shape[0])]
98 return cle.concatenate_along_z(*results)
100 def _arrays_close(self, arr1, arr2):
101 """Compare arrays using CLE."""
102 return np.allclose(arr1.get(), arr2.get(), rtol=1e-5, atol=1e-8)