summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Friesel <daniel.friesel@uos.de>2020-07-06 15:28:07 +0200
committerDaniel Friesel <daniel.friesel@uos.de>2020-07-06 15:28:07 +0200
commit2a4ee78fd4c8b57f759135e068d85cf730b2e268 (patch)
tree1495e9a11d56d0b4b3b2f478fec83ee229c00241
parentf8d1ec53748231a97c4591da31310f73711ec5a8 (diff)
move gplearn_to_function to functions module
-rwxr-xr-xbin/analyze-archive.py2
-rwxr-xr-xbin/analyze-timing.py2
-rwxr-xr-xbin/test_corrcoef.py2
-rw-r--r--lib/dfatool.py47
-rw-r--r--lib/functions.py41
5 files changed, 44 insertions, 50 deletions
diff --git a/bin/analyze-archive.py b/bin/analyze-archive.py
index a18c1e1..7cac27a 100755
--- a/bin/analyze-archive.py
+++ b/bin/analyze-archive.py
@@ -114,7 +114,7 @@ import re
import sys
from dfatool import plotter
from dfatool.dfatool import RawData, pta_trace_to_aggregate
-from dfatool.dfatool import gplearn_to_function
+from dfatool.functions import gplearn_to_function
from dfatool.model import PTAModel
from dfatool.validation import CrossValidator
from dfatool.utils import filter_aggregate_by_param
diff --git a/bin/analyze-timing.py b/bin/analyze-timing.py
index e27acbf..3503279 100755
--- a/bin/analyze-timing.py
+++ b/bin/analyze-timing.py
@@ -80,7 +80,7 @@ import re
import sys
from dfatool import plotter
from dfatool.dfatool import TimingData, pta_trace_to_aggregate
-from dfatool.dfatool import gplearn_to_function
+from dfatool.functions import gplearn_to_function
from dfatool.model import AnalyticModel
from dfatool.validation import CrossValidator
from dfatool.utils import filter_aggregate_by_param
diff --git a/bin/test_corrcoef.py b/bin/test_corrcoef.py
index 75b5d0d..fef0b24 100755
--- a/bin/test_corrcoef.py
+++ b/bin/test_corrcoef.py
@@ -5,7 +5,7 @@ import re
import sys
from dfatool import plotter
from dfatool.dfatool import RawData, pta_trace_to_aggregate
-from dfatool.dfatool import gplearn_to_function
+from dfatool.functions import gplearn_to_function
from dfatool.model import PTAModel
opt = dict()
diff --git a/lib/dfatool.py b/lib/dfatool.py
index 07aa7b3..47ce24e 100644
--- a/lib/dfatool.py
+++ b/lib/dfatool.py
@@ -27,53 +27,6 @@ except ImportError:
arg_support_enabled = True
-def gplearn_to_function(function_str: str):
- """
- Convert gplearn-style function string to Python function.
-
- Takes a function string like "mul(add(X0, X1), X2)" and returns
- a Python function implementing the specified behaviour,
- e.g. "lambda x, y, z: (x + y) * z".
-
- Supported functions:
- add -- x + y
- sub -- x - y
- mul -- x * y
- div -- x / y if |y| > 0.001, otherwise 1
- sqrt -- sqrt(|x|)
- log -- log(|x|) if |x| > 0.001, otherwise 0
- inv -- 1 / x if |x| > 0.001, otherwise 0
- """
- eval_globals = {
- "add": lambda x, y: x + y,
- "sub": lambda x, y: x - y,
- "mul": lambda x, y: x * y,
- "div": lambda x, y: np.divide(x, y) if np.abs(y) > 0.001 else 1.0,
- "sqrt": lambda x: np.sqrt(np.abs(x)),
- "log": lambda x: np.log(np.abs(x)) if np.abs(x) > 0.001 else 0.0,
- "inv": lambda x: 1.0 / x if np.abs(x) > 0.001 else 0.0,
- }
-
- last_arg_index = 0
- for i in range(0, 100):
- if function_str.find("X{:d}".format(i)) >= 0:
- last_arg_index = i
-
- arg_list = []
- for i in range(0, last_arg_index + 1):
- arg_list.append("X{:d}".format(i))
-
- eval_str = "lambda {}, *whatever: {}".format(",".join(arg_list), function_str)
- logger.debug(eval_str)
- return eval(eval_str, eval_globals)
-
-
-def append_if_set(aggregate: dict, data: dict, key: str):
- """Append data[key] to aggregate if key in data."""
- if key in data:
- aggregate.append(data[key])
-
-
def mean_or_none(arr):
"""
Compute mean of NumPy array `arr`, return -1 if empty.
diff --git a/lib/functions.py b/lib/functions.py
index 99ba17d..94b1aaf 100644
--- a/lib/functions.py
+++ b/lib/functions.py
@@ -25,6 +25,47 @@ def powerset(iterable):
return chain.from_iterable(combinations(s, r) for r in range(len(s) + 1))
+def gplearn_to_function(function_str: str):
+ """
+ Convert gplearn-style function string to Python function.
+
+ Takes a function string like "mul(add(X0, X1), X2)" and returns
+ a Python function implementing the specified behaviour,
+ e.g. "lambda x, y, z: (x + y) * z".
+
+ Supported functions:
+ add -- x + y
+ sub -- x - y
+ mul -- x * y
+ div -- x / y if |y| > 0.001, otherwise 1
+ sqrt -- sqrt(|x|)
+ log -- log(|x|) if |x| > 0.001, otherwise 0
+ inv -- 1 / x if |x| > 0.001, otherwise 0
+ """
+ eval_globals = {
+ "add": lambda x, y: x + y,
+ "sub": lambda x, y: x - y,
+ "mul": lambda x, y: x * y,
+ "div": lambda x, y: np.divide(x, y) if np.abs(y) > 0.001 else 1.0,
+ "sqrt": lambda x: np.sqrt(np.abs(x)),
+ "log": lambda x: np.log(np.abs(x)) if np.abs(x) > 0.001 else 0.0,
+ "inv": lambda x: 1.0 / x if np.abs(x) > 0.001 else 0.0,
+ }
+
+ last_arg_index = 0
+ for i in range(0, 100):
+ if function_str.find("X{:d}".format(i)) >= 0:
+ last_arg_index = i
+
+ arg_list = []
+ for i in range(0, last_arg_index + 1):
+ arg_list.append("X{:d}".format(i))
+
+ eval_str = "lambda {}, *whatever: {}".format(",".join(arg_list), function_str)
+ logger.debug(eval_str)
+ return eval(eval_str, eval_globals)
+
+
class ParamFunction:
"""
A one-dimensional model function, ready for least squares optimization and similar.