From ef9236af37c44cfe0f7e7dd74bb7a909e6aeaf42 Mon Sep 17 00:00:00 2001
From: Peter Luenenschloss <peter.luenenschloss@ufz.de>
Date: Wed, 29 Apr 2020 14:41:12 +0200
Subject: [PATCH] added parameter passing syntax to trafo architecture

---
 saqc/lib/tools.py        | 16 +++++++++++-----
 saqc/lib/ts_operators.py |  4 ++--
 2 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/saqc/lib/tools.py b/saqc/lib/tools.py
index 030fb5ae1..28b7136b7 100644
--- a/saqc/lib/tools.py
+++ b/saqc/lib/tools.py
@@ -11,6 +11,7 @@ import sklearn
 from functools import reduce, partial
 import pandas as pd
 import dios
+import inspect
 
 # from saqc.flagger import BaseFlagger
 from saqc.lib.types import T, PandasLike
@@ -50,15 +51,18 @@ OP_MODULES = {'pd': pd,
               }
 
 
-def evalFuncString(func_string):
-    if not isinstance(func_string, str):
-        return func_string
+def evalFuncString(full_func_string):
+    if not isinstance(full_func_string, str):
+        return full_func_string
+    full_func_string = full_func_string.split("$")
+    func_string = full_func_string[0]
+    paras = full_func_string[1:]
     module_dot = func_string.find(".")
     first, *rest = func_string.split(".")
     if rest:
         module = func_string[:module_dot]
         try:
-            return reduce(lambda m, f: getattr(m, f), rest, OP_MODULES[first])
+            func = reduce(lambda m, f: getattr(m, f), rest, OP_MODULES[first])
         except KeyError:
             availability_list = [f"'{k}' (= {s.__name__})" for k, s in OP_MODULES.items()]
             availability_list = " \n".join(availability_list)
@@ -69,7 +73,7 @@ def evalFuncString(func_string):
 
     else:
         if func_string in SAQC_OPERATORS:
-            return SAQC_OPERATORS[func_string]
+            func = SAQC_OPERATORS[func_string]
         else:
             availability_list = [f"'{k}' (= {s.__name__})" for k, s in SAQC_OPERATORS.items()]
             availability_list = " \n".join(availability_list)
@@ -78,6 +82,8 @@ def evalFuncString(func_string):
                 f"dispatcher. \n Please select from: \n{availability_list}"
             )
 
+    para_names = inspect.getfullargspec(func).args[1:1 + len(paras)]
+    return partial(func, **dict(zip(para_names, paras)))
 
 def composeFunction(functions):
     if callable(functions):
diff --git a/saqc/lib/ts_operators.py b/saqc/lib/ts_operators.py
index 5e7ed0b1c..ebfa042e0 100644
--- a/saqc/lib/ts_operators.py
+++ b/saqc/lib/ts_operators.py
@@ -75,7 +75,7 @@ def _kNN(in_arr, n_neighbors, algorithm="ball_tree"):
     return nbrs.kneighbors()
 
 
-def kNNMaxGap(in_arr, n_neighbors, algorithm='ball_tree'):
+def kNNMaxGap(in_arr, n_neighbors=10, algorithm='ball_tree'):
     in_arr = np.asarray(in_arr)
     dist, *_ = _kNN(in_arr, n_neighbors, algorithm=algorithm)
     sample_size = dist.shape[0]
@@ -84,7 +84,7 @@ def kNNMaxGap(in_arr, n_neighbors, algorithm='ball_tree'):
     return dist[range(0, sample_size), max_gap_ind]
 
 
-def kNNSum(in_arr, n_neighbors, algorithm="ball_tree"):
+def kNNSum(in_arr, n_neighbors=10, algorithm="ball_tree"):
     in_arr = np.asarray(in_arr)
     dist, *_ = _kNN(in_arr, n_neighbors, algorithm=algorithm)
     return dist.sum(axis=1)
-- 
GitLab