forked from diffpy/diffpy.morph
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmorphfuncy.py
More file actions
68 lines (55 loc) · 2.52 KB
/
morphfuncy.py
File metadata and controls
68 lines (55 loc) · 2.52 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from diffpy.morph.morphs.morph import LABEL_GR, LABEL_RA, Morph
class MorphFuncy(Morph):
"""Apply the user-supplied Python function to the y-coordinates of the
morph data"""
# Define input output types
summary = "Apply a Python function to the y-axis data"
xinlabel = LABEL_RA
yinlabel = LABEL_GR
xoutlabel = LABEL_RA
youtlabel = LABEL_GR
def morph(self, x_morph, y_morph, x_target, y_target):
"""General morph function that applies a user-supplied function to the
y-coordinates of morph data to make it align with a target.
Configuration Variables
-----------------------
function: callable
The user-supplied function that applies a transformation to the
y-coordinates of the data.
parameters: dict
A dictionary of parameters to pass to the function.
These parameters are unpacked using **kwargs.
Returns
-------
A tuple (x_morph_out, y_morph_out, x_target_out, y_target_out)
where the target values remain the same and the morph data is
transformed according to the user-specified function and parameters
The morphed data is returned on the same grid as the unmorphed data
Example
-------
Import the funcy morph function:
>>> from diffpy.morph.morphs.morphfuncy import MorphFuncy
Define or import the user-supplied transformation function:
>>> def sine_function(x, y, amplitude, frequency):
>>> return amplitude * np.sin(frequency * x) * y
Provide initial guess for parameters:
>>> parameters = {'amplitude': 2, 'frequency': 2}
Run the funcy morph given input morph array (x_morph, y_morph)
and target array (x_target, y_target):
>>> morph = MorphFuncy()
>>> morph.function = sine_function
>>> morph.parameters = parameters
>>> x_morph_out, y_morph_out, x_target_out, y_target_out = morph.morph(
... x_morph, y_morph, x_target, y_target)
To access parameters from the morph instance:
>>> x_morph_in = morph.x_morph_in
>>> y_morph_in = morph.y_morph_in
>>> x_target_in = morph.x_target_in
>>> y_target_in = morph.y_target_in
>>> parameters_out = morph.parameters
"""
Morph.morph(self, x_morph, y_morph, x_target, y_target)
self.y_morph_out = self.function(
self.x_morph_in, self.y_morph_in, **self.parameters
)
return self.xyallout