source: sasview/src/sas/qtgui/Plotting/DataTransform.py @ e20870bc

ESS_GUIESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since e20870bc was d744767, checked in by krzywon, 6 years ago

Merge branch 'ESS_GUI' into ESS_GUI_Pr

  • Property mode set to 100755
File size: 7.9 KB
RevLine 
[749b715]1import math
2
3
4def toX(x, y=None):
5    """
6    This function is used to load value on Plottable.View
7
8    :param x: Float value
9
10    :return: x
11
12    """
13    return x
14
15
16def toX_pos(x, y=None):
17    """
18    This function is used to load value on Plottable.View
19
20    :param x: Float value
21
22    :return: x
23
24    """
25    if not x > 0:
[b3e8629]26        raise ValueError("Transformation only accepts positive values.")
[749b715]27    else:
28        return x
29
30
31def toX2(x, y=None):
32    """
33    This function is used to load value on Plottable.View
34
35    Calculate x^(2)
36
37    :param x: float value
38
39    """
40    return x * x
41
42
43def fromX2(x, y=None):
44    """
45    This function is used to load value on Plottable.View
46    Calculate square root of x
47
48    :param x: float value
49
50    """
51    if not x >= 0:
[b3e8629]52        raise ValueError("square root of a negative value ")
[749b715]53    else:
54        return math.sqrt(x)
55
56
57def toX4(x, y=None):
58    """
59    This function is used to load value on Plottable.View
60
61    Calculate x^(4)
62
63    :param x: float value
64
65    """
66    return x * x * x * x
67
68
69def fromX4(x, y=None):
70    """
71    This function is used to load value on Plottable.View
72    Calculate square root of x
73
74    :param x: float value
75
76    """
77    if not x >= 0:
[b3e8629]78        raise ValueError("double square root of a negative value ")
[749b715]79    else:
80        return math.sqrt(math.sqrt(x))
81
82
83def toLogX(x, y=None):
84    """
85    This function is used to load value on Plottable.View
86    calculate log x
87
88    :param x: float value
89
90    """
91    if not x > 0:
[b3e8629]92        raise ValueError("Log(x)of a negative value ")
[749b715]93    else:
94        return math.log(x)
95
96def toOneOverX(x, y=None):
97    """
98    """
99    if x != 0:
100        return 1 / x
101    else:
[b3e8629]102        raise ValueError("cannot divide by zero")
[749b715]103
104
105def toOneOverSqrtX(y, x=None):
106    """
107    """
108    if y > 0:
109        return 1 / math.sqrt(y)
110    else:
[b3e8629]111        raise ValueError("transform.toOneOverSqrtX: cannot be computed")
[749b715]112
113
114def toLogYX2(y, x):
115    """
116    """
117    if (y * (x ** 2)) > 0:
118        return math.log(y * (x ** 2))
119    else:
[b3e8629]120        raise ValueError("transform.toLogYX2: cannot be computed")
[749b715]121
122
123def toLogYX4(y, x):
124    """
125    """
126    if (math.pow(x, 4) * y) > 0:
127        return math.log(math.pow(x, 4) * y)
128    else:
[b3e8629]129        raise ValueError("transform.toLogYX4: input error")
[749b715]130
131
132def toYX4(y, x):
133    """
134    """
135    return math.pow(x, 4) * y
136
137def toYX2(y, x):
138    """
139    """
140    return math.pow(x, 2) * y
141
142def toLogXY(y, x):
143    """
144    This function is used to load value on Plottable.View
145    calculate log x
146
147    :param x: float value
148
149    """
150    if not (x * y) > 0:
[b3e8629]151        raise ValueError("Log(X*Y)of a negative value ")
[749b715]152    else:
153        return math.log(x * y)
154
155
156def errToX(x, y=None, dx=None, dy=None):
157    """
158    calculate error of x**2
159
160    :param x: float value
161    :param dx: float value
162
163    """
[cee5c78]164    if dx is None:
[749b715]165        dx = 0
166    return dx
167
168
169def errToX_pos(x, y=None, dx=None, dy=None):
170    """
171    calculate error of x**2
172
173    :param x: float value
174    :param dx: float value
175
176    """
[cee5c78]177    if dx is None:
[749b715]178        dx = 0
179    return dx
180
181
182def errToX2(x, y=None, dx=None, dy=None):
183    """
184    calculate error of x**2
185
186    :param x: float value
187    :param dx: float value
188
189    """
[cee5c78]190    if  dx is not None:
[749b715]191        err = 2 * x * dx
192        return math.fabs(err)
193    else:
194        return 0.0
195
196
197def errFromX2(x, y=None, dx=None, dy=None):
198    """
199    calculate error of sqrt(x)
200
201    :param x: float value
202    :param dx: float value
203
204    """
205    if x > 0:
[cee5c78]206        if dx is not None:
[749b715]207            err = dx / (2 * math.sqrt(x))
208        else:
209            err = 0
210        return math.fabs(err)
211    else:
212        msg = "transform.errFromX2: can't compute error of negative x"
[b3e8629]213        raise ValueError(msg)
[749b715]214
215
216def errToX4(x, y=None, dx=None, dy=None):
217    """
218    calculate error of x**4
219
220    :param x: float value
221    :param dx: float value
222
223    """
[cee5c78]224    if dx is not None:
[749b715]225        err = 4 * math.pow(x, 3) * dx
226        return math.fabs(err)
227    else:
228        return 0.0
229
230
231def errFromX4(x, y=None, dx=None, dy=None):
232    """
233    calculate error of x^1/4
234
235    :param x: float value
236    :param dx: float value
237
238    """
239    if x > 0:
[cee5c78]240        if dx is not None:
[749b715]241            err = dx / (4 * math.pow(x, 3 / 4))
242        else:
243            err = 0
244        return math.fabs(err)
245    else:
246        msg = "transform.errFromX4: can't compute error of negative x"
[b3e8629]247        raise ValueError(msg)
[749b715]248
249
250def errToLog10X(x, y=None, dx=None, dy=None):
251    """
252    calculate error of Log(x)
253
254    :param x: float value
255    :param dx: float value
256
257    """
[cee5c78]258    if dx is None:
[749b715]259        dx = 0
260
261    # Check that the point on the graph is positive
262    # within errors
263    if not (x - dx) > 0:
264        msg = "Transformation does not accept"
265        msg += " point that are consistent with zero."
[b3e8629]266        raise ValueError(msg)
[749b715]267    if x != 0:
268        dx = dx / (x * math.log(10))
269    else:
[b3e8629]270        raise ValueError("errToLogX: divide by zero")
[749b715]271    return dx
272
273
274def errToLogX(x, y=None, dx=None, dy=None):
275    """
276    calculate error of Log(x)
277
278    :param x: float value
279    :param dx: float value
280
281    """
[cee5c78]282    if dx is None:
[749b715]283        dx = 0
284
285    # Check that the x point on the graph is zero
286    if x != 0:
287        dx = dx / x
288    else:
[b3e8629]289        raise ValueError("errToLogX: divide by zero")
[749b715]290    return dx
291
292
293def errToYX2(y, x, dy=None, dx=None):
294    """
295    """
[cee5c78]296    if dx is None:
[749b715]297        dx = 0
[cee5c78]298    if dy is None:
[749b715]299        dy = 0
300    err = math.sqrt((2 * x * y * dx) ** 2 + ((x ** 2) * dy) ** 2)
301    return err
302
303
304def errToLogXY(x, y, dx=None, dy=None):
305    """
306    calculate error of Log(xy)
307
308    """
309    # Check that the point on the graph is positive
310    # within errors
311    if not (x - dx) > 0 or not (y - dy) > 0:
312        msg = "Transformation does not accept point "
313        msg += " that are consistent with zero."
[b3e8629]314        raise ValueError(msg)
[749b715]315    if x != 0 and y != 0:
[cee5c78]316        if dx is None:
[749b715]317            dx = 0
[cee5c78]318        if dy is None:
[749b715]319            dy = 0
320        err = (dx / x) ** 2 + (dy / y) ** 2
321    else:
[b3e8629]322        raise ValueError("cannot compute this error")
[749b715]323
324    return math.sqrt(math.fabs(err))
325
326
327def errToLogYX2(y, x, dy=None, dx=None):
328    """
329    calculate error of Log(yx**2)
330
331    """
332    # Check that the point on the graph is positive
333    # within errors
334    if not (x - dx) > 0 or not (y - dy) > 0:
335        msg = "Transformation does not accept point"
336        msg += " that are consistent with zero."
[b3e8629]337        raise ValueError(msg)
[749b715]338    if x > 0 and y > 0:
[cee5c78]339        if dx is None:
[749b715]340            dx = 0
[cee5c78]341        if dy is None:
[749b715]342            dy = 0
343        err = (2.0 * dx / x) ** 2 + (dy / y) ** 2
344    else:
[b3e8629]345        raise ValueError("cannot compute this error")
[749b715]346    return math.sqrt(math.fabs(err))
347
348
349def errOneOverX(x, y=None, dx=None, dy=None):
350    """
351    calculate error on 1/x
352
353    """
354    if x != 0:
[cee5c78]355        if dx is None:
[749b715]356            dx = 0
357        err = dx / x ** 2
358    else:
[b3e8629]359        raise ValueError("Cannot compute this error")
[749b715]360    return math.fabs(err)
361
362
363def errOneOverSqrtX(x, y=None, dx=None, dy=None):
364    """
365    Calculate error on 1/sqrt(x)
366
367    """
368    if x > 0:
[cee5c78]369        if dx is None:
[749b715]370            dx = 0
371        err = -1 / 2 * math.pow(x, -3.0 / 2.0) * dx
372    else:
[b3e8629]373        raise ValueError("Cannot compute this error")
[749b715]374    return math.fabs(err)
375
376
377def errToLogYX4(y, x, dy=None, dx=None):
378    """
379    error for ln(y*x^(4))
380
381    :param x: float value
382
383    """
384    # Check that the point on the graph is positive
385    # within errors
386    if (not (x - dx) > 0) or (not (y - dy) > 0):
387        msg = "Transformation does not accept point "
388        msg += " that are consistent with zero."
[b3e8629]389        raise ValueError(msg)
[cee5c78]390    if dx is None:
[749b715]391        dx = 0
[cee5c78]392    if dy is None:
[749b715]393        dy = 0
394    err = math.sqrt((4.0 * dx / x) ** 2 + (dy / y) ** 2)
395    return err
396
397
398def errToYX4(y, x, dy=None, dx=None):
399    """
400    error for (y*x^(4))
401
402    :param x: float value
403
404    """
405    # Check that the point on the graph is positive
406    # within errors
407
[cee5c78]408    if dx is None:
[749b715]409        dx = 0
[cee5c78]410    if dy is None:
[749b715]411        dy = 0
412    err = math.sqrt((dy * pow(x, 4)) ** 2 + (4 * y * dx * math.pow(x, 3)) ** 2)
413    return err
414
Note: See TracBrowser for help on using the repository browser.