From c41dde68b38dd4d5679a9b5605c99eb085e25cab Mon Sep 17 00:00:00 2001 From: David Schaefer <david.schaefer@ufz.de> Date: Mon, 21 Nov 2022 14:23:30 +0100 Subject: [PATCH] the FloatTranslator now actually accept floating point values --- saqc/core/core.py | 2 +- saqc/core/flags.py | 4 +-- saqc/core/translation/__init__.py | 2 +- saqc/core/translation/basescheme.py | 50 ++++++++++++++++++++--------- saqc/core/translation/dmpscheme.py | 2 +- 5 files changed, 39 insertions(+), 21 deletions(-) diff --git a/saqc/core/core.py b/saqc/core/core.py index abb47c7a5..b52c5e01a 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 4f6424a26..5529d6215 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 c2db9856f..c40914611 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 78347b020..4e25c1f0e 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 f56762398..8376f3b84 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(), -- GitLab