diff --git a/saqc/core/core.py b/saqc/core/core.py index abb47c7a511ef1aefac7a732e77ff83bd28cedf1..b52c5e01a697ca62d70484d90cdf28af7a0e3864 100644 --- a/saqc/core/core.py +++ b/saqc/core/core.py @@ -110,7 +110,7 @@ class SaQC(FunctionsMixin): @property def flags(self) -> MutableMapping: - flags = self._scheme.backward(self._flags, attrs=self._attrs, raw=True) + flags = self._scheme.backward(self._flags, attrs=self._attrs) flags.attrs = self._attrs.copy() return flags diff --git a/saqc/core/flags.py b/saqc/core/flags.py index 4f6424a261bc5202fb5ee57d29a771f91da604dd..5529d6215b572e2ef3290158771164cbad2b224b 100644 --- a/saqc/core/flags.py +++ b/saqc/core/flags.py @@ -191,9 +191,7 @@ class Flags: 2 -inf 25.0 25.0 0.0 99.0 """ - def __init__( - self, raw_data: DictLike | Flags | None = None, copy: bool = False - ): + def __init__(self, raw_data: DictLike | Flags | None = None, copy: bool = False): self._data: dict[str, History] diff --git a/saqc/core/translation/__init__.py b/saqc/core/translation/__init__.py index c2db9856fcc5b3cefa80ff358de92f02ef37473b..c40914611bbe1fad26132d3520f2e39e4a803dc6 100644 --- a/saqc/core/translation/__init__.py +++ b/saqc/core/translation/__init__.py @@ -7,8 +7,8 @@ # -*- coding: utf-8 -*- from saqc.core.translation.basescheme import ( FloatScheme, - SimpleScheme, MappingScheme, + SimpleScheme, TranslationScheme, ) from saqc.core.translation.dmpscheme import DmpScheme diff --git a/saqc/core/translation/basescheme.py b/saqc/core/translation/basescheme.py index 78347b020886feec2dd27b089121d5df073ef14b..4e25c1f0e6ddb6822bf0c48773b190705622a281 100644 --- a/saqc/core/translation/basescheme.py +++ b/saqc/core/translation/basescheme.py @@ -7,8 +7,8 @@ # -*- coding: utf-8 -*- from __future__ import annotations -from abc import abstractmethod +from abc import abstractmethod, abstractproperty from typing import Any, Dict import numpy as np @@ -24,16 +24,24 @@ BackwardMap = Dict[float, ExternalFlag] class TranslationScheme: + @abstractproperty + def DFILTER_DEFAULT(self): + pass + @abstractmethod def __call__(self, ExternalFlag) -> float: pass @abstractmethod - def forward(self, pd.DataFrame | DictOfSeries) -> Flags: + def forward(self, user_flags: pd.DataFrame | DictOfSeries) -> Flags: pass @abstractmethod - def backward(self, Flags) -> DictOfSeries: + def backward( + self, + internal_flags: Flags, + attrs: dict | None = None, + ) -> DictOfSeries: pass @@ -163,10 +171,8 @@ class MappingScheme(TranslationScheme): def backward( self, flags: Flags, - raw: bool = False, attrs: dict | None = None, - **kwargs, - ) -> pd.DataFrame | DictOfSeries: + ) -> DictOfSeries: """ Translate from 'internal flags' to 'external flags' @@ -187,25 +193,39 @@ class MappingScheme(TranslationScheme): """ out = self._translate(flags, self._backward) out.attrs = attrs or {} - if not raw: - out = out.to_df() return out -class FloatScheme(MappingScheme): +class FloatScheme(TranslationScheme): """ Acts as the default Translator, provides a changeable subset of the internal float flags """ - _MAP = { - -np.inf: -np.inf, - **{k: k for k in np.arange(0, 256, dtype=float)}, - } + DFILTER_DEFAULT: float = FILTER_ALL - def __init__(self): - super().__init__(self._MAP, self._MAP) + def _check(self, val): + return ~((val < 0) | (val > 255) | (val != UNFLAGGED)) + + def __call__(self, flag: float | int) -> float: + if self._check(flag): + return float(flag) + raise ValueError( + f"invalid flag, expected a numerical value in the interval [0, 255], got: {flag}" + ) + + def forward(self, flags: pd.DataFrame | DictOfSeries) -> Flags: + if self._check(flags): + return Flags(flags) + raise ValueError( + f"invalid flag, expected a collection of numerical value in the interval [0, 255], got: {flags}" + ) + + def backward(self, flags: Flags, attrs: dict | None = None) -> DictOfSeries: + out = flags.toDios() + out.attrs = attrs or {} + return out class SimpleScheme(MappingScheme): diff --git a/saqc/core/translation/dmpscheme.py b/saqc/core/translation/dmpscheme.py index f567623988a4cd2a87c1954118a3e42659ce69ff..8376f3b840675d081c5fadabe1a979a1f27ee007 100644 --- a/saqc/core/translation/dmpscheme.py +++ b/saqc/core/translation/dmpscheme.py @@ -131,7 +131,7 @@ class DmpScheme(MappingScheme): ------- translated flags """ - tflags = super().backward(flags, raw=True, attrs=attrs) + tflags = super().backward(flags, attrs=attrs) out = pd.DataFrame( index=reduce(lambda x, y: x.union(y), tflags.indexes).sort_values(),