source: sasview/guitools/transform.py @ 19403da

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 19403da was ad8bcd6, checked in by Mathieu Doucet <doucetm@…>, 17 years ago

Improved transformation so that it always plots something reasonable in log scale. Minor tweak to dragging.

  • Property mode set to 100644
File size: 6.8 KB
Line 
1import math 
2         
3def toX(x,y=None):
4    """
5    This function is used to load value on Plottable.View
6    @param x: Float value
7    @return x,
8    """
9    return x
10
11def toX_pos(x,y=None):
12    """
13    This function is used to load value on Plottable.View
14    @param x: Float value
15    @return x,
16    """
17    if not x>0:
18        raise ValueError, "Transformation only accepts positive values."
19    else:
20        return x
21
22def toX2(x,y=None):
23    """
24        This function is used to load value on Plottable.View
25        Calculate x^(2)
26        @param x: float value
27    """
28    return x*x
29
30def fromX2(x,y=None):
31     """
32         This function is used to load value on Plottable.View
33        Calculate square root of x
34        @param x: float value
35     """
36     if not x >=0 :
37         raise ValueError, "square root of a negative value "
38     else:
39         return math.sqrt(x)
40     
41def toLogX(x,y=None):
42    """
43        This function is used to load value on Plottable.View
44        calculate log x
45        @param x: float value
46    """
47    if not x > 0:
48        raise ValueError, "Log(x)of a negative value "
49    else:
50        return math.log(x)
51   
52def toOneOverX(x,y=None):
53    if x !=0:
54        return 1/x
55    else:
56        raise ValueError,"cannot divide by zero"
57   
58def toOneOverSqrtX(y , x=None):
59    if y > 0:
60        return 1/math.sqrt(y)
61    else:
62        raise ValueError,"transform.toOneOverSqrtX: cannot be computed"
63   
64   
65def toLogYX2(y,x):
66    if y*(x**2) >0:
67        return math.log(y*(x**2))
68    else:
69         raise ValueError,"transform.toLogYX2: cannot be computed"
70     
71
72def toLogYX4(y, x):
73    if math.pow(x,4)*y > 0:
74        return math.log(math.pow(x,4)*y)
75    else:
76         raise ValueError,"transform.toLogYX4: input error"
77
78def toLogXY(y,x):
79    """
80        This function is used to load value on Plottable.View
81        calculate log x
82        @param x: float value
83    """
84    if not x*y > 0:
85        raise ValueError, "Log(X*Y)of a negative value "
86    else:
87        return math.log(x*y)
88
89
90
91def errToX(x,y=None,dx=None,dy=None):
92    """
93        calculate error of x**2
94        @param x: float value
95        @param dx: float value
96    """
97    if dx==None:
98        dx=0
99    return dx
100
101def errToX_pos(x,y=None,dx=None,dy=None):
102    """
103        calculate error of x**2
104        @param x: float value
105        @param dx: float value
106    """
107    if dx==None:
108        dx=0
109       
110    # Check that the point on the graph is positive
111    # within errors
112    if not x-dx > 0:
113        raise ValueError, "Transformation does not accept point that are consistent with zero."
114   
115    return dx
116
117
118def errToX2(x,y=None,dx=None,dy=None):
119    """
120        calculate error of x**2
121        @param x: float value
122        @param dx: float value
123    """
124    if  dx != None:
125        err = 2*x*dx
126        return math.fabs(err)
127    else:
128        return 0.0
129   
130def errFromX2(x,y=None,dx=None,dy=None):
131    """
132        calculate error of sqrt(x)
133        @param x: float value
134        @param dx: float value
135    """
136    if (x > 0):
137        if(dx != None):
138            err = dx/(2*math.sqrt(x))
139        else:
140            err = 0
141        return math.fabs(err)
142    else:
143        raise ValueError, "transform.errFromX2: can't compute error of negative x"
144   
145def errToLog10X(x,y=None,dx=None,dy=None):
146    """
147        calculate error of Log(x)
148        @param x: float value
149        @param dx: float value
150    """
151    if dx==None:
152        dx = 0
153       
154    # Check that the point on the graph is positive
155    # within errors
156    if not x-dx > 0:
157        raise ValueError, "Transformation does not accept point that are consistent with zero."
158       
159    if x!=0:
160        dx = dx/(x*math.log(10))
161    else:
162        raise ValueError, "errToLogX: divide by zero"
163   
164    return dx
165   
166def errToLogX(x,y=None,dx=None,dy=None):
167    """
168        calculate error of Log(x)
169        @param x: float value
170        @param dx: float value
171    """
172    if dx==None:
173        dx = 0
174           
175    # Check that the point on the graph is positive
176    # within errors
177    if not x-dx > 0:
178        raise ValueError, "Transformation does not accept point that are consistent with zero."
179
180    if x!=0:
181        dx = dx/x
182    else:
183        raise ValueError, "errToLogX: divide by zero"
184   
185    return dx
186
187
188
189def errToYX2(x, y, dx=None, dy=None):
190    if dx==None:
191        dx=0
192    if dy==None:
193        dy=0
194    err =math.sqrt((2*x*y*dx)**2 +((x**2)*dy)**2)
195    return err
196   
197def errToLogXY(x,y,dx=None, dy=None):
198    """
199        calculate error of Log(xy)
200    """
201    # Check that the point on the graph is positive
202    # within errors
203    if (not x-dx > 0) or (not y-dy > 0):
204        raise ValueError, "Transformation does not accept point that are consistent with zero."
205   
206    if (x!=0) and (y!=0):
207        if dx == None:
208            dx = 0
209        if dy == None:
210            dy = 0
211        err = (dx/x)**2 + (dy/y)**2
212    else:
213        raise ValueError, "cannot compute this error"
214   
215    return math.sqrt(math.fabs(err))
216   
217def errToLogYX2(x,y,dx=None, dy=None):
218    """
219        calculate error of Log(yx**2)
220    """
221    # Check that the point on the graph is positive
222    # within errors
223    if (not x-dx > 0) or (not y-dy > 0):
224        raise ValueError, "Transformation does not accept point that are consistent with zero."
225   
226    if (x > 0) and (y > 0):
227        if (dx == None):
228            dx = 0
229        if (dy == None):
230            dy = 0
231        err = (2.0*dx/x)**2 + (dy/y)**2
232    else:
233         raise ValueError, "cannot compute this error"
234     
235    return math.sqrt(math.fabs(err)) 
236       
237def errOneOverX(x,y=None,dx=None, dy=None):
238    """
239         calculate error on 1/x
240    """
241    if (x != 0):
242        if dx ==None:
243            dx= 0
244        err = dx/x**2
245    else:
246        raise ValueError,"Cannot compute this error"
247   
248    return math.fabs(err)
249
250def errOneOverSqrtX(x,y=None, dx=None,dy=None):
251    """
252        Calculate error on 1/sqrt(x)
253    """
254    if (x >0):
255        if dx==None:
256            dx =0
257        err= -1/2*math.pow(x, -3.0/2.0)* dx
258    else:
259        raise ValueError, "Cannot compute this error"
260   
261    return math.fabs(err)
262
263def errToLogYX4(x,y=None,dx=None,dy=None):
264    """
265        error for ln(y*x^(4))
266        @param x: float value
267    """
268    # Check that the point on the graph is positive
269    # within errors
270    if (not x-dx > 0) or (not y-dy > 0):
271        raise ValueError, "Transformation does not accept point that are consistent with zero."
272   
273    if dx==None:
274        dx=0
275    if dy==None:
276        dy=0
277    err =math.sqrt((4.0*dx/x)**2 +(dy/y)**2)
278    return err
279
280           
Note: See TracBrowser for help on using the repository browser.