Skip to content
Snippets Groups Projects
Commit 8d7c0f4f authored by Peter Lünenschloß's avatar Peter Lünenschloß
Browse files

target broadcasting and numpy array support for generics

parent 11513306
No related branches found
No related tags found
1 merge request!824target broadcasting and numpy array support for generics
...@@ -8,7 +8,7 @@ SPDX-License-Identifier: GPL-3.0-or-later ...@@ -8,7 +8,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
## Unreleased ## Unreleased
[List of commits](https://git.ufz.de/rdm-software/saqc/-/compare/v2.5.0...develop) [List of commits](https://git.ufz.de/rdm-software/saqc/-/compare/v2.5.0...develop)
### Added ### Added
- `flagGeneric`: target broadcasting - `flagGeneric`, `processGeneric`: target broadcasting and numpy array support
- `SaQC`: automatic translation of incoming flags - `SaQC`: automatic translation of incoming flags
- Option to change the flagging scheme after initialization - Option to change the flagging scheme after initialization
- `flagByClick`: manually assign flags using a graphical user interface - `flagByClick`: manually assign flags using a graphical user interface
......
...@@ -77,10 +77,27 @@ def _execGeneric( ...@@ -77,10 +77,27 @@ def _execGeneric(
return func(*cols) return func(*cols)
def _inferBroadcast(obj, trg_shape) -> pd.DataFrame:
# simple single value broadcasting
if pd.api.types.is_scalar(obj):
return np.full(trg_shape, obj)
return obj
def _inferDF(obj, cols, index):
# infer dataframe if result is numpy array of fitting shape
if isinstance(obj, np.ndarray):
lc = len(cols)
li = len(index)
if (obj.shape == (li, lc)) or (obj.shape == (li,)):
return pd.DataFrame(obj, columns=cols, index=index)
return obj
def _castResult(obj) -> DictOfSeries: def _castResult(obj) -> DictOfSeries:
# Note: the actual keys aka. column names # Note: the actual keys aka. column names
# we use here to create a DictOfSeries # we use here to create a DictOfSeries
# are never used, and only exists temporary. # are never used and only exist temporarily.
if isinstance(obj, pd.Series): if isinstance(obj, pd.Series):
return DictOfSeries({"0": obj}) return DictOfSeries({"0": obj})
...@@ -151,7 +168,10 @@ class GenericMixin: ...@@ -151,7 +168,10 @@ class GenericMixin:
targets = fields if target is None else toSequence(target) targets = fields if target is None else toSequence(target)
dchunk, fchunk = self._data[fields].copy(), self._flags[fields].copy() dchunk, fchunk = self._data[fields].copy(), self._flags[fields].copy()
trg_idx = dchunk[dchunk.columns[0]].index
result = _execGeneric(fchunk, dchunk, func, dfilter=dfilter) result = _execGeneric(fchunk, dchunk, func, dfilter=dfilter)
result = _inferBroadcast(result, (len(trg_idx), len(targets)))
result = _inferDF(result, cols=targets, index=trg_idx)
result = _castResult(result) result = _castResult(result)
# update data & flags # update data & flags
...@@ -231,7 +251,10 @@ class GenericMixin: ...@@ -231,7 +251,10 @@ class GenericMixin:
dfilter = kwargs.get("dfilter", BAD) dfilter = kwargs.get("dfilter", BAD)
dchunk, fchunk = self._data[fields].copy(), self._flags[fields].copy() dchunk, fchunk = self._data[fields].copy(), self._flags[fields].copy()
trg_idx = dchunk[dchunk.columns[0]].index
result = _execGeneric(fchunk, dchunk, func, dfilter=dfilter) result = _execGeneric(fchunk, dchunk, func, dfilter=dfilter)
result = _inferBroadcast(result, (len(trg_idx), len(targets)))
result = _inferDF(result, cols=targets, index=trg_idx)
result = _castResult(result) result = _castResult(result)
if len(result.columns) > 1 and len(targets) != len(result.columns): if len(result.columns) > 1 and len(targets) != len(result.columns):
......
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