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 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