|
5 | 5 | # Copyright (c) 2015 Pierre Raybaut, for the Python translation/optimization |
6 | 6 | # (see LICENSE file for more details) |
7 | 7 |
|
| 8 | +""" |
| 9 | +QwtTransform |
| 10 | +------------ |
| 11 | +
|
| 12 | +.. autoclass:: QwtTransform |
| 13 | + :members: |
| 14 | + |
| 15 | +QwtNullTransform |
| 16 | +---------------- |
| 17 | +
|
| 18 | +.. autoclass:: QwtNullTransform |
| 19 | + :members: |
| 20 | +
|
| 21 | +QwtLogTransform |
| 22 | +--------------- |
| 23 | +
|
| 24 | +.. autoclass:: QwtLogTransform |
| 25 | + :members: |
| 26 | + |
| 27 | +QwtPowerTransform |
| 28 | +----------------- |
| 29 | +
|
| 30 | +.. autoclass:: QwtPowerTransform |
| 31 | + :members: |
| 32 | +""" |
| 33 | + |
8 | 34 | import numpy as np |
9 | 35 |
|
10 | 36 |
|
11 | 37 | class QwtTransform(object): |
| 38 | + """ |
| 39 | + A transformation between coordinate systems |
| 40 | +
|
| 41 | + QwtTransform manipulates values, when being mapped between |
| 42 | + the scale and the paint device coordinate system. |
| 43 | +
|
| 44 | + A transformation consists of 2 methods: |
| 45 | +
|
| 46 | + - transform |
| 47 | + - invTransform |
| 48 | +
|
| 49 | + where one is is the inverse function of the other. |
| 50 | +
|
| 51 | + When p1, p2 are the boundaries of the paint device coordinates |
| 52 | + and s1, s2 the boundaries of the scale, QwtScaleMap uses the |
| 53 | + following calculations:: |
| 54 | + |
| 55 | + p = p1 + (p2 - p1) * ( T(s) - T(s1) / (T(s2) - T(s1)) ) |
| 56 | + s = invT( T(s1) + ( T(s2) - T(s1) ) * (p - p1) / (p2 - p1) ) |
| 57 | + """ |
12 | 58 | def __init__(self): |
13 | 59 | pass |
14 | 60 |
|
15 | 61 | def bounded(self, value): |
| 62 | + """ |
| 63 | + Modify value to be a valid value for the transformation. |
| 64 | + The default implementation does nothing. |
| 65 | + """ |
16 | 66 | return value |
17 | 67 |
|
18 | 68 | def transform(self, value): |
| 69 | + """ |
| 70 | + Transformation function |
| 71 | +
|
| 72 | + :param float value: Value |
| 73 | + :return: Modified value |
| 74 | + |
| 75 | + .. seealso:: |
| 76 | + |
| 77 | + :py:meth:`invTransform()` |
| 78 | + """ |
19 | 79 | raise NotImplementedError |
20 | 80 |
|
21 | 81 | def invTransform(self, value): |
| 82 | + """ |
| 83 | + Inverse transformation function |
| 84 | +
|
| 85 | + :param float value: Value |
| 86 | + :return: Modified value |
| 87 | + |
| 88 | + .. seealso:: |
| 89 | + |
| 90 | + :py:meth:`transform()` |
| 91 | + """ |
22 | 92 | raise NotImplementedError |
23 | 93 |
|
24 | 94 | def copy(self): |
| 95 | + """ |
| 96 | + :return: Clone of the transformation |
| 97 | + |
| 98 | + The default implementation does nothing. |
| 99 | + """ |
25 | 100 | raise NotImplementedError |
26 | 101 |
|
27 | 102 |
|
28 | 103 | class QwtNullTransform(QwtTransform): |
29 | 104 | def transform(self, value): |
| 105 | + """ |
| 106 | + Transformation function |
| 107 | +
|
| 108 | + :param float value: Value |
| 109 | + :return: Modified value |
| 110 | + |
| 111 | + .. seealso:: |
| 112 | + |
| 113 | + :py:meth:`invTransform()` |
| 114 | + """ |
30 | 115 | return value |
31 | 116 |
|
32 | 117 | def invTransform(self, value): |
| 118 | + """ |
| 119 | + Inverse transformation function |
| 120 | +
|
| 121 | + :param float value: Value |
| 122 | + :return: Modified value |
| 123 | + |
| 124 | + .. seealso:: |
| 125 | + |
| 126 | + :py:meth:`transform()` |
| 127 | + """ |
33 | 128 | return value |
34 | 129 |
|
35 | 130 | def copy(self): |
| 131 | + """ |
| 132 | + :return: Clone of the transformation |
| 133 | + """ |
36 | 134 | return QwtNullTransform() |
37 | 135 |
|
38 | 136 |
|
39 | 137 | class QwtLogTransform(QwtTransform): |
| 138 | + """ |
| 139 | + Logarithmic transformation |
| 140 | +
|
| 141 | + `QwtLogTransform` modifies the values using `numpy.log()` and |
| 142 | + `numpy.exp()`. |
| 143 | +
|
| 144 | + .. note:: |
| 145 | + |
| 146 | + In the calculations of `QwtScaleMap` the base of the log function |
| 147 | + has no effect on the mapping. So `QwtLogTransform` can be used |
| 148 | + for logarithmic scale in base 2 or base 10 or any other base. |
| 149 | + |
| 150 | + Extremum values: |
| 151 | + |
| 152 | + * `QwtLogTransform.LogMin`: Smallest allowed value for logarithmic |
| 153 | + scales: 1.0e-150 |
| 154 | + * `QwtLogTransform.LogMax`: Largest allowed value for logarithmic |
| 155 | + scales: 1.0e150 |
| 156 | + """ |
| 157 | + |
40 | 158 | LogMin = 1.0e-150 |
41 | 159 | LogMax = 1.0e150 |
| 160 | + |
42 | 161 | def bounded(self, value): |
| 162 | + """ |
| 163 | + Modify value to be a valid value for the transformation. |
| 164 | + |
| 165 | + :param float value: Value to be bounded |
| 166 | + :return: Value unmodified |
| 167 | + """ |
43 | 168 | return np.clip(value, self.LogMin, self.LogMax) |
44 | 169 |
|
45 | 170 | def transform(self, value): |
| 171 | + """ |
| 172 | + Transformation function |
| 173 | +
|
| 174 | + :param float value: Value |
| 175 | + :return: Modified value |
| 176 | + |
| 177 | + .. seealso:: |
| 178 | + |
| 179 | + :py:meth:`invTransform()` |
| 180 | + """ |
46 | 181 | return np.log(value) |
47 | 182 |
|
48 | 183 | def invTransform(self, value): |
| 184 | + """ |
| 185 | + Inverse transformation function |
| 186 | +
|
| 187 | + :param float value: Value |
| 188 | + :return: Modified value |
| 189 | + |
| 190 | + .. seealso:: |
| 191 | + |
| 192 | + :py:meth:`transform()` |
| 193 | + """ |
49 | 194 | return np.exp(value) |
50 | 195 |
|
51 | 196 | def copy(self): |
| 197 | + """ |
| 198 | + :return: Clone of the transformation |
| 199 | + """ |
52 | 200 | return QwtLogTransform() |
53 | 201 |
|
54 | 202 |
|
55 | 203 | class QwtPowerTransform(QwtTransform): |
| 204 | + """ |
| 205 | + A transformation using `numpy.pow()` |
| 206 | +
|
| 207 | + `QwtPowerTransform` preserves the sign of a value. |
| 208 | + F.e. a transformation with a factor of 2 |
| 209 | + transforms a value of -3 to -9 and v.v. Thus `QwtPowerTransform` |
| 210 | + can be used for scales including negative values. |
| 211 | + """ |
| 212 | + |
56 | 213 | def __init__(self, exponent): |
57 | 214 | self.__exponent = exponent |
58 | 215 | super(QwtPowerTransform, self).__init__() |
59 | 216 |
|
60 | 217 | def transform(self, value): |
| 218 | + """ |
| 219 | + Transformation function |
| 220 | +
|
| 221 | + :param float value: Value |
| 222 | + :return: Modified value |
| 223 | + |
| 224 | + .. seealso:: |
| 225 | + |
| 226 | + :py:meth:`invTransform()` |
| 227 | + """ |
61 | 228 | if value < 0.: |
62 | 229 | return -np.pow(-value, 1./self.__exponent) |
63 | 230 | else: |
64 | 231 | return np.pow(value, 1./self.__exponent) |
65 | 232 |
|
66 | 233 | def invTransform(self, value): |
| 234 | + """ |
| 235 | + Inverse transformation function |
| 236 | +
|
| 237 | + :param float value: Value |
| 238 | + :return: Modified value |
| 239 | + |
| 240 | + .. seealso:: |
| 241 | + |
| 242 | + :py:meth:`transform()` |
| 243 | + """ |
67 | 244 | if value < 0.: |
68 | 245 | return -np.pow(-value, self.__exponent) |
69 | 246 | else: |
70 | 247 | return np.pow(value, self.__exponent) |
71 | 248 |
|
72 | 249 | def copy(self): |
73 | | - return QwtPowerTransform() |
| 250 | + """ |
| 251 | + :return: Clone of the transformation |
| 252 | + """ |
| 253 | + return QwtPowerTransform(self.__exponent) |
0 commit comments