-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransformFunction.py
42 lines (34 loc) · 1.32 KB
/
transformFunction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import numpy as np
class transformFunction:
"""
Class containing a function and its parameters.
Made to transform a 3D Vignette in real time with a slider.
function must return an array only
"""
def __init__(self, function, parameters={"nom":{'value':None, 'minValue':None, 'maxValue':None}}):
self.function = function
self.parameters = parameters
self.initParameters = {k:v for k,v in parameters.items()}
def changeValue(self, name, newValue):
self.parameters[name]['value'] = newValue
def transform(self, x):
kwargs = {k:v['value'] for k,v in self.parameters.items()}
return self.function(x, **kwargs)
def reset(self):
self.parameters = {k:v for k,v in self.initParameters.items()}
# Identity transform
transformIdentity = transformFunction(lambda x:x, parameters={})
## An example
def isolateExtrema(x, amp=1, spread=1):
"""
Function designed to isolate extrema values of an array
"""
x = np.array(x) - np.mean(x)
y1 = np.sinc((x - np.max(x)) / (np.std(x) ** spread))
y2 = np.sinc((x - np.min(x)) / (np.std(x) ** spread))
invR = np.sign(x) / np.sqrt(x**2 + (y1+y2)**2)
return amp * invR
# Enter all modifiable parameters
parameters = {"amp":{"value":1, "minValue":0, "maxValue":5},
"spread":{"value":1, "minValue":1, "maxValue":2}}
transformIsolate = transformFunction(isolateExtrema, parameters)