forked from diffpy/diffpy.morph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmorph.py
More file actions
318 lines (276 loc) · 8.87 KB
/
morph.py
File metadata and controls
318 lines (276 loc) · 8.87 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/bin/env python
##############################################################################
#
# diffpy.morph by DANSE Diffraction group
# Simon J. L. Billinge
# (c) 2010 Trustees of the Columbia University
# in the City of New York. All rights reserved.
#
# File coded by: Pavol Juhas, Chris Farrow
#
# See AUTHORS.txt for a list of people who contributed.
# See LICENSE.txt for license information.
#
##############################################################################
"""Morph -- base class for defining a morph."""
import numpy
LABEL_RA = "r (A)" # r-grid
LABEL_GR = "G (1/A^2)" # PDF G(r)
LABEL_RR = "R (1/A)" # RDF R(r)
class Morph(object):
"""Base class for implementing a morph given a target.
Adapted from diffpy.pdfgetx to include two sets of arrays that get passed
through.
Attributes are taken from config when not found locally. The morph may
modify the config dictionary. This is the means by which to communicate
automatically modified attributes.
Class Attributes
----------------
summary
Short description of a morph.
xinlabel
Descriptive label for the x input array.
yinlabel
Descriptive label for the y input array.
xoutlabel
Descriptive label for the x output array.
youtlabel
Descriptive label for the y output array.
parnames: list
Names of configuration variables.
Instance Attributes
-------------------
config: dict
All configuration variables.
x_morph_in
Last morph input x data.
y_morph_in
Last morph input y data.
x_morph_out
Last morph result x data.
y_morph_out
Last morph result y data.
x_target_in
Last target input x data.
y_target_in
Last target input y data.
x_target_out
Last target result x data.
y_target_out
Last target result y data.
Properties
----------
xy_morph_in
Tuple of (x_morph_in, y_morph_in).
xy_morph_out
Tuple of (x_morph_out, y_morph_out).
xy_target_in
Tuple of (x_target_in, y_target_in).
xy_target_out
Tuple of (x_target_out, y_target_out).
xyallout
Tuple of (x_morph_out, y_morph_out, x_target_out, y_target_out).
"""
# Class variables
# default array types are empty
summary = "identity transformation"
xinlabel = "x"
yinlabel = "y"
xoutlabel = "x"
youtlabel = "y"
parnames = []
# Properties
xy_morph_in = property(
lambda self: (self.x_morph_in, self.y_morph_in),
doc="Return a tuple of morph input arrays",
)
xy_morph_out = property(
lambda self: (self.x_morph_out, self.y_morph_out),
doc="Return a tuple of morph output arrays",
)
xy_target_in = property(
lambda self: (self.x_target_in, self.y_target_in),
doc="Return a tuple of target input arrays",
)
xy_target_out = property(
lambda self: (self.x_target_out, self.y_target_out),
doc="Return a tuple of target output arrays",
)
xyallout = property(
lambda self: (
self.x_morph_out,
self.y_morph_out,
self.x_target_out,
self.y_target_out,
),
doc="Return a tuple of all output arrays",
)
def __init__(self, config=None):
"""Create a default Morph instance.
config: dict
All configuration variables.
"""
# declare empty attributes
if config is None:
config = {}
self.x_morph_in = None
self.y_morph_in = None
self.x_morph_out = None
self.y_morph_out = None
self.x_target_in = None
self.y_target_in = None
self.x_target_out = None
self.y_target_out = None
# process arguments
self.applyConfig(config)
return
def morph(self, x_morph, y_morph, x_target, y_target):
"""Morph arrays morphed or target.
Identity operation.
This method should be overloaded in a derived class.
Parameters
----------
x_morph, y_morph
Morphed arrays.
x_target, y_target
Target arrays.
Returns
-------
tuple
A tuple of numpy arrays
(x_morph_out, y_morph_out, x_target_out, y_target_out)
"""
self.x_morph_in = x_morph
self.y_morph_in = y_morph
self.x_target_in = x_target
self.y_target_in = y_target
self.x_morph_out = x_morph.copy()
self.y_morph_out = y_morph.copy()
self.x_target_out = x_target.copy()
self.y_target_out = y_target.copy()
self.checkConfig()
return self.xyallout
def __call__(self, x_morph, y_morph, x_target, y_target):
"""Alias for morph."""
return self.morph(x_morph, y_morph, x_target, y_target)
def applyConfig(self, config):
"""Process any configuration data from a dictionary.
Parameters
----------
config: dict
Configuration dictionary.
Returns
-------
No return value.
"""
self.config = config
return
def checkConfig(self):
"""Verify data in self.config. No action by default.
To be overridden in a derived class.
"""
return
def plotInputs(self, xylabels=True):
"""Plot input arrays using matplotlib.pyplot.
Parameters
----------
xylabels
Flag for updating x and y axes labels.
Returns
-------
list:
A list of matplotlib line objects.
"""
from matplotlib.pyplot import plot, xlabel, ylabel
rv = plot(self.x_target_in, self.y_target_in, label="target")
rv = plot(self.x_morph_in, self.y_morph_in, label="morph")
if xylabels:
xlabel(self.xinlabel)
ylabel(self.yinlabel)
return rv
def plotOutputs(self, xylabels=True, **plotargs):
"""Plot output arrays using matplotlib.pyplot.
Parameters
----------
xylabels: bool
Flag for updating x and y axes labels.
plotargs
Arguments passed to the pylab plot function.
Note that "label" will be ignored.
Returns
-------
list
A list of matplotlib line objects.
"""
from matplotlib.pyplot import plot, xlabel, ylabel
pargs = dict(plotargs)
pargs.pop("label", None)
rv = plot(
self.x_target_out, self.y_target_out, label="target", **pargs
)
rv = plot(self.x_morph_out, self.y_morph_out, label="morph", **pargs)
if xylabels:
xlabel(self.xoutlabel)
ylabel(self.youtlabel)
return rv
def set_extrapolation_info(self, x_true, x_extrapolate):
"""Set extrapolation information of the concerned morphing
process.
Parameters
----------
x_true : array
original x values
x_extrapolate : array
x values after a morphing process
"""
cutoff_low = min(x_true)
extrap_low_x = numpy.where(x_extrapolate < cutoff_low)[0]
is_extrap_low = False if len(extrap_low_x) == 0 else True
cutoff_high = max(x_true)
extrap_high_x = numpy.where(x_extrapolate > cutoff_high)[0]
is_extrap_high = False if len(extrap_high_x) == 0 else True
extrap_index_low = extrap_low_x[-1] if is_extrap_low else 0
extrap_index_high = extrap_high_x[0] if is_extrap_high else -1
extrapolation_info = {
"is_extrap_low": is_extrap_low,
"cutoff_low": cutoff_low,
"extrap_index_low": extrap_index_low,
"is_extrap_high": is_extrap_high,
"cutoff_high": cutoff_high,
"extrap_index_high": extrap_index_high,
}
self.extrapolation_info = extrapolation_info
def __getattr__(self, name):
"""Obtain the value from self.config, when normal lookup fails.
Parameters
----------
name
Name of the attribute to be recovered.
Returns
-------
self.config.get(name)
Raises
------
AttributeError
Name is not available from self.config.
"""
if name in self.config:
return self.config[name]
else:
emsg = "Object has no attribute %r" % name
raise AttributeError(emsg)
def __setattr__(self, name, val):
"""Set configuration variables to config.
Parameters
----------
name
Name of the attribute.
val
Value of the attribute.
"""
if name in self.parnames:
self.config[name] = val
else:
object.__setattr__(self, name, val)
return
# End class Morph