-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathtransform.py
More file actions
258 lines (186 loc) · 5.94 KB
/
transform.py
File metadata and controls
258 lines (186 loc) · 5.94 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
# -*- coding: utf-8 -*-
#
# Licensed under the terms of the Qwt License
# Copyright (c) 2002 Uwe Rathmann, for the original C++ code
# Copyright (c) 2015 Pierre Raybaut, for the Python translation/optimization
# (see LICENSE file for more details)
"""
Coordinate tranformations
-------------------------
QwtTransform
~~~~~~~~~~~~
.. autoclass:: QwtTransform
:members:
QwtNullTransform
~~~~~~~~~~~~~~~~
.. autoclass:: QwtNullTransform
:members:
QwtLogTransform
~~~~~~~~~~~~~~~
.. autoclass:: QwtLogTransform
:members:
QwtPowerTransform
~~~~~~~~~~~~~~~~~
.. autoclass:: QwtPowerTransform
:members:
"""
import numpy as np
class QwtTransform(object):
"""
A transformation between coordinate systems
QwtTransform manipulates values, when being mapped between
the scale and the paint device coordinate system.
A transformation consists of 2 methods:
- transform
- invTransform
where one is is the inverse function of the other.
When p1, p2 are the boundaries of the paint device coordinates
and s1, s2 the boundaries of the scale, QwtScaleMap uses the
following calculations::
p = p1 + (p2 - p1) * ( T(s) - T(s1) / (T(s2) - T(s1)) )
s = invT( T(s1) + ( T(s2) - T(s1) ) * (p - p1) / (p2 - p1) )
"""
def __init__(self):
pass
def bounded(self, value):
"""
Modify value to be a valid value for the transformation.
The default implementation does nothing.
"""
return value
def transform(self, value):
"""
Transformation function
:param float value: Value
:return: Modified value
.. seealso::
:py:meth:`invTransform()`
"""
raise NotImplementedError
def invTransform(self, value):
"""
Inverse transformation function
:param float value: Value
:return: Modified value
.. seealso::
:py:meth:`transform()`
"""
raise NotImplementedError
def copy(self):
"""
:return: Clone of the transformation
The default implementation does nothing.
"""
raise NotImplementedError
class QwtNullTransform(QwtTransform):
def transform(self, value):
"""
Transformation function
:param float value: Value
:return: Modified value
.. seealso::
:py:meth:`invTransform()`
"""
return value
def invTransform(self, value):
"""
Inverse transformation function
:param float value: Value
:return: Modified value
.. seealso::
:py:meth:`transform()`
"""
return value
def copy(self):
"""
:return: Clone of the transformation
"""
return QwtNullTransform()
class QwtLogTransform(QwtTransform):
"""
Logarithmic transformation
`QwtLogTransform` modifies the values using `numpy.log()` and
`numpy.exp()`.
.. note::
In the calculations of `QwtScaleMap` the base of the log function
has no effect on the mapping. So `QwtLogTransform` can be used
for logarithmic scale in base 2 or base 10 or any other base.
Extremum values:
* `QwtLogTransform.LogMin`: Smallest allowed value for logarithmic
scales: 1.0e-150
* `QwtLogTransform.LogMax`: Largest allowed value for logarithmic
scales: 1.0e150
"""
LogMin = 1.0e-150
LogMax = 1.0e150
def bounded(self, value):
"""
Modify value to be a valid value for the transformation.
:param float value: Value to be bounded
:return: Value modified
"""
bval = np.clip(np.asarray(value, dtype=np.float64), self.LogMin, self.LogMax)
return bval.item() if bval.ndim == 0 else bval
def transform(self, value):
"""
Transformation function
:param float value: Value
:return: Modified value
.. seealso::
:py:meth:`invTransform()`
"""
return np.log(self.bounded(value))
def invTransform(self, value):
"""
Inverse transformation function
:param float value: Value
:return: Modified value
.. seealso::
:py:meth:`transform()`
"""
return np.exp(value)
def copy(self):
"""
:return: Clone of the transformation
"""
return QwtLogTransform()
class QwtPowerTransform(QwtTransform):
"""
A transformation using `numpy.pow()`
`QwtPowerTransform` preserves the sign of a value.
F.e. a transformation with a factor of 2
transforms a value of -3 to -9 and v.v. Thus `QwtPowerTransform`
can be used for scales including negative values.
"""
def __init__(self, exponent):
self.__exponent = exponent
super(QwtPowerTransform, self).__init__()
def transform(self, value):
"""
Transformation function
:param float value: Value
:return: Modified value
.. seealso::
:py:meth:`invTransform()`
"""
if value < 0.0:
return -np.pow(-value, 1.0 / self.__exponent)
else:
return np.pow(value, 1.0 / self.__exponent)
def invTransform(self, value):
"""
Inverse transformation function
:param float value: Value
:return: Modified value
.. seealso::
:py:meth:`transform()`
"""
if value < 0.0:
return -np.pow(-value, self.__exponent)
else:
return np.pow(value, self.__exponent)
def copy(self):
"""
:return: Clone of the transformation
"""
return QwtPowerTransform(self.__exponent)