Skip to content
Snippets Groups Projects
Commit 9ce9f067 authored by David Schäfer's avatar David Schäfer
Browse files

implement an abstract translatro interface

parent 714aac54
No related branches found
No related tags found
2 merge requests!591Test b,!579Translation cleanups
......@@ -8,6 +8,7 @@
from saqc.core.translation.basescheme import (
FloatScheme,
SimpleScheme,
MappingScheme,
TranslationScheme,
)
from saqc.core.translation.dmpscheme import DmpScheme
......
......@@ -7,6 +7,7 @@
# -*- coding: utf-8 -*-
from __future__ import annotations
from abc import abstractmethod
from typing import Any, Dict
......@@ -16,13 +17,27 @@ import pandas as pd
from dios import DictOfSeries
from saqc.constants import BAD, FILTER_ALL, GOOD, UNFLAGGED
from saqc.core.flags import Flags
from saqc.lib.types import ExternalFlag
from saqc.lib.types import ExternalFlag, PandasLike
ForwardMap = Dict[ExternalFlag, float]
BackwardMap = Dict[float, ExternalFlag]
class TranslationScheme:
@abstractmethod
def __call__(self, ExternalFlag) -> float:
pass
@abstractmethod
def forward(self, PandasLike) -> Flags:
pass
@abstractmethod
def backward(self, Flags) -> DictOfSeries:
pass
class MappingScheme(TranslationScheme):
"""
This class provides the basic translation mechanism and should serve as
a base class for every other translation scheme.
......@@ -81,7 +96,7 @@ class TranslationScheme:
@staticmethod
def _translate(
flags: Flags | pd.DataFrame | pd.Series,
flags: Flags | pd.DataFrame | pd.Series | DictOfSeries,
trans_map: ForwardMap | BackwardMap,
) -> DictOfSeries:
"""
......@@ -130,7 +145,7 @@ class TranslationScheme:
return float(flag)
return self._forward[flag]
def forward(self, flags: pd.DataFrame) -> Flags:
def forward(self, flags: pd.DataFrame | DictOfSeries | pd.Series) -> Flags:
"""
Translate from 'external flags' to 'internal flags'
......@@ -177,7 +192,7 @@ class TranslationScheme:
return out
class FloatScheme(TranslationScheme):
class FloatScheme(MappingScheme):
"""
Acts as the default Translator, provides a changeable subset of the
......@@ -193,7 +208,7 @@ class FloatScheme(TranslationScheme):
super().__init__(self._MAP, self._MAP)
class SimpleScheme(TranslationScheme):
class SimpleScheme(MappingScheme):
"""
Acts as the default Translator, provides a changeable subset of the
......
......@@ -17,7 +17,7 @@ import pandas as pd
from saqc.constants import BAD, DOUBTFUL, GOOD, UNFLAGGED
from saqc.core.flags import Flags
from saqc.core.history import History
from saqc.core.translation.basescheme import BackwardMap, ForwardMap, TranslationScheme
from saqc.core.translation.basescheme import BackwardMap, ForwardMap, MappingScheme
_QUALITY_CAUSES = [
"",
......@@ -40,7 +40,7 @@ _QUALITY_LABELS = [
]
class DmpScheme(TranslationScheme):
class DmpScheme(MappingScheme):
"""
Implements the translation from and to the flagging scheme implemented in
......
......@@ -12,10 +12,10 @@ import pandas as pd
from saqc.constants import BAD, DOUBTFUL, GOOD, UNFLAGGED
from saqc.core.flags import Flags, History
from saqc.core.translation.basescheme import BackwardMap, ForwardMap, TranslationScheme
from saqc.core.translation.basescheme import BackwardMap, ForwardMap, MappingScheme
class PositionalScheme(TranslationScheme):
class PositionalScheme(MappingScheme):
"""
Implements the translation from and to the flagging scheme implemented by CHS
......
......@@ -16,7 +16,7 @@ import pytest
from saqc.constants import BAD, DOUBTFUL, FILTER_NONE, UNFLAGGED
from saqc.core.core import SaQC
from saqc.core.flags import Flags
from saqc.core.translation import DmpScheme, PositionalScheme, TranslationScheme
from saqc.core.translation import DmpScheme, PositionalScheme, MappingScheme
from tests.common import initData
......@@ -27,7 +27,7 @@ def _genTranslators():
dtype(-1): BAD,
**{dtype(f * 10): float(f) for f in range(10)},
}
scheme = TranslationScheme(flags, {v: k for k, v in flags.items()})
scheme = MappingScheme(flags, {v: k for k, v in flags.items()})
yield flags, scheme
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment