Source code for isimple.core.interface

import abc
from typing import Type, Tuple, Optional, Dict

import numpy as np

from isimple import get_logger
from isimple.core import Described
from isimple.core.config import BaseConfig, Configurable, Factory
from isimple.maths.colors import Color
from isimple.maths.coordinates import ShapeCoo, Roi

from pydantic import validator

log = get_logger(__name__)


[docs]class InterfaceFactory(Factory): _type = Configurable _mapping: Dict[str, Type[Described]] = {}
[docs] def get(self) -> Type[Configurable]: interface = super().get() assert issubclass(interface, Configurable) return interface
[docs] def config_schema(self) -> dict: return self.get().config_schema()
[docs]class HandlerConfig(BaseConfig, abc.ABC): type: InterfaceFactory data: BaseConfig @validator('type', pre=True) def _validate_type(cls, value, values: dict): if isinstance(value, cls.__fields__['type'].type_): return value else: return cls.__fields__['type'].type_(value) @validator('data', pre=True) def _validate_data(cls, value, values: dict): if isinstance(value, values['type'].get().config_class()): return value elif isinstance(value, dict): return values['type'].get().config_class()(**value) else: raise NotImplementedError
[docs]class TransformConfig(BaseConfig): """Undefined transform""" pass
[docs]class TransformInterface(Configurable, abc.ABC):
[docs] @abc.abstractmethod def validate(self, matrix: Optional[np.ndarray]) -> bool: raise NotImplementedError
[docs] @abc.abstractmethod def from_coordinates(self, roi: Roi, from_shape: tuple) -> np.ndarray: raise NotImplementedError
[docs] @abc.abstractmethod def to_coordinates(self, to_shape: tuple) -> np.ndarray: raise NotImplementedError
[docs] @abc.abstractmethod def estimate(self, roi: Roi, from_shape: tuple, to_shape: tuple) -> np.ndarray: raise NotImplementedError
[docs] def invert(self, matrix: Optional[np.ndarray]) -> Optional[np.ndarray]: if matrix is not None: return np.linalg.inv(matrix) else: return None
[docs] @abc.abstractmethod def transform(self, transform: np.ndarray, img: np.ndarray, shape: Tuple[int, int]) -> np.ndarray: raise NotImplementedError
[docs] @abc.abstractmethod def coordinate(self, transform: np.ndarray, coordinate: ShapeCoo, shape: Tuple[int, int]) -> ShapeCoo: raise NotImplementedError
[docs]class FilterConfig(BaseConfig): """Undefined filter""" @property def ready(self): """Return true if filter can be applied ~ this configuration. Override for specific filter implementations """ raise NotImplementedError
[docs]class FilterInterface(Configurable, abc.ABC): """Handles pixel filtering operations """
[docs] @abc.abstractmethod def set_filter(self, filter, color: Color): raise NotImplementedError
[docs] @abc.abstractmethod def mean_color(self, filter) -> Color: raise NotImplementedError
[docs] @abc.abstractmethod def filter(self, filter, image: np.ndarray, mask: np.ndarray = None) -> np.ndarray: raise NotImplementedError
[docs]class FilterType(InterfaceFactory): _type = FilterInterface _mapping: Dict[str, Type[Described]] = {}
[docs] def get(self) -> Type[FilterInterface]: interface = super().get() assert issubclass(interface, FilterInterface) return interface
[docs]class TransformType(InterfaceFactory): _type = TransformInterface _mapping: Dict[str, Type[Described]] = {}
[docs] def get(self) -> Type[TransformInterface]: interface = super().get() assert issubclass(interface, TransformInterface) return interface