Package reflectometry :: Package model1d :: Package model :: Module auxs

Source Code for Module reflectometry.model1d.model.auxs

  1  """ 
  2  Supplementary or auxiliary Functions 
  3  """ 
  4   
  5  import wx 
  6  import numpy 
  7  from numpy import isscalar 
  8   
9 -def vector(n):
10 """ 11 Return a vector of the given length. 12 """ 13 return numpy.empty(n,'d')
14 15
16 -def dense(v):
17 """ 18 Force v to be a dense array. 19 20 The return value may be different from the input value. 21 If used as a return vector, be sure to call it as v=dense(v); 22 f(v) rather than f(dense(v)) because the latter form will send a 23 temporary vector to f() which is immediately garbage collected. 24 If the vector is a pure input vector the two forms are equivalent. 25 """ 26 return numpy.ascontiguousarray(v,'d')
27 28
29 -def ravel(biglist):
30 """ 31 Ravels a list of lists into a single list. 32 33 Similar to numpy.ravel() except you don't need each list to have 34 the same number of elements 35 """ 36 37 newlist = [dense(x) for x in biglist] 38 return numpy.concatenate(newlist)
39 40
41 -def takelist(biglist, indices):
42 """ 43 mimics numeric.take 44 45 except lists can be comprised of any element(even other lists) 46 """ 47 reslist = [] 48 for x in range(len(biglist)): 49 if(indices.count(x) > 0): 50 reslist += [biglist[x]] 51 return reslist
52 53
54 -def combine(biglist):
55 """ 56 combines a list of lists into one list and removes duplicates 57 """ 58 startList = ravel(biglist) 59 theDict = {} 60 for x in startList: 61 theDict[x] = x 62 return theDict.keys()
63 64
65 -def resize_array(array, newlen):
66 """ 67 resise an array to new length 68 """ 69 array = dense(array) 70 if len(array) == 0: 71 return vector(newlen) 72 dn = newlen - len(array) 73 if dn > 0: 74 return numpy.concatenate((array,[array[-1]]*dn)) 75 if dn < 0: 76 return array[:dn] 77 else: 78 return array
79 80
81 -def pmp(x,p):
82 """pmp(x,p) returns the pair x plus/minus p% 83 84 This is useful for constructing fitting variables centered 85 around a particular value. Note that the book values can 86 vary due to e.g., density changes in the material from thermal 87 expansion. 88 """ 89 if p == 0: 90 return x 91 elif x > 0: 92 return [x*(1-p/100.), x*(1+p/100.)] 93 else: 94 return [x*(1+p/100.), x*(1-p/100.)]
95 96
97 -def pm(x,v):
98 """pm(x,v) returns the pair x plus/minus v 99 100 This is useful for constructing fitting variables centered 101 around a particular value. 102 """ 103 if v == 0: 104 return x 105 else: 106 return [x-v, x+v]
107 108
109 -def isInt(v):
110 """ 111 Check variable v is a int or not? 112 """ 113 return type(v) is int
114
115 -def isLong(v):
116 """ 117 Check variable v is a Long or not? 118 """ 119 return type(v) is long
120
121 -def isFloat(v):
122 """ 123 Check variable v is a float or not? 124 """ 125 return type(v) is float
126
127 -def isBool(v):
128 """ 129 Check variable v is a boolean or not? 130 """ 131 return type(v) is bool
132
133 -def isString(v):
134 """ 135 Check variable v is a string or not? 136 """ 137 return type(v) is str
138 139
140 -def isType(v):
141 """ 142 Check variable v is a type or not? 143 """ 144 return type(v) is type
145
146 -def isObject(v):
147 """ 148 Check variable v is a object or not? 149 """ 150 return type(v) is object
151 152
153 -def isvector(v):
154 """ 155 Check variable v is a vector or not? 156 """ 157 return hasattr(v,'__len__')
158 159
160 -def isfactory(v):
161 """ 162 Check variable v is a factory or not? 163 """ 164 return hasattr(v,'build')
165 166
167 -def isscientific(v):
168 """ 169 Return true if string v contains value(uncertainty). 170 171 This implementation does not check that value and uncertainty 172 are indeed numbers. 173 """ 174 p = str( v ) 175 lastp = p.strip()[-1] 176 if lastp==")": 177 return True 178 else: 179 return False
180 181
182 -def _cells(v):
183 """ 184 Convert a vector to a list of strings 185 """ 186 return [str(x) for x in v]
187 188
189 -def _joincolumns(v):
190 """ 191 Join a list of cells to a single string separated by tabs. 192 """ 193 return ''.join([x+'\t' for x in v[:-1]])+str(v[-1])
194 195
196 -def _joinrows(v):
197 """ 198 Join a list of row strings to a string separated by lf. 199 """ 200 return ''.join([x+'\n' for x in v])
201 202
203 -def clipboard(vectors,labels=None):
204 """ 205 clipboard((v1,v2,...),labels=('label1','label2',...)) 206 207 Put vectors v1,v2,... onto the clipboard with columns separated 208 by tabs and rows separated by linefeed. If labels is specified, 209 a label will be added to the head of each column. 210 """ 211 data = zip(*vectors) 212 lines = _joinrows([_joincolumns(_cells(row)) for row in data]) 213 if labels: 214 lines = _joincolumns(labels)+'\n'+lines 215 clipdata = wx.TextDataObject(lines) 216 wx.TheClipboard.Open() 217 wx.TheClipboard.SetData(clipdata) 218 wx.TheClipboard.Close()
219 220
221 -def getBmpImage4Fig(fig):
222 return fig.bitmap
223 224
225 -def CopyImage(canvas, 226 plotType=0 227 ):
228 """ 229 0: matplotlib plot 230 1: wx.lib.plot 231 2: other 232 """ 233 bmp = wx.BitmapDataObject() 234 if plotType == 0: 235 bitmap = getBmpImage4Fig(canvas) 236 237 elif plotType == 1: 238 bitmap = canvas.getBitBmp() 239 else: 240 pass 241 242 bmp.SetBitmap( bitmap ) 243 244 wx.TheClipboard.Open() 245 wx.TheClipboard.SetData(bmp) 246 wx.TheClipboard.Close()
247 248
249 -def CopyImageOLD(canvas):
250 """ 251 From PBSrefl 252 """ 253 graphdc = wx.ClientDC(canvas) 254 w,h = graphdc.GetSize() 255 256 bmp = wx.EmptyBitmap(w,h) 257 memdc = wx.MemoryDC() 258 memdc.SelectObject(bmp) 259 memdc.Blit(0,0, w, h, memdc, 0, 0, wx.COPY, True) 260 memdc.SelectObject(wx.NullBitmap) 261 262 clipdata = wx.PyBitmapDataObject( bmp ) 263 wx.TheClipboard.Open() 264 wx.TheClipboard.SetData(clipdata) 265 wx.TheClipboard.Close()
266 267 268
269 -def AFloat(x):
270 """ 271 Change a variable x into a scalar float varible 272 """ 273 if isString(x): return x 274 elif isfactory(x): return eval( str(x.run()) ) 275 elif isvector(x): return (x[0]+x[1])/2.0 276 else: return x
277 278
279 -def tFloat(x):
280 """ 281 Change a variable x into a scalar float varible 282 """ 283 if isString(x): return x 284 elif isfactory(x): return x.build() 285 elif isvector(x): 286 if len(x)==3: 287 return (x[0]+x[2])/2.0 288 else: 289 return (x[0]+x[1])/2.0 290 else: 291 return x*1.0
292 293
294 -def readdata(filename):
295 """ 296 Read data from file 297 298 readdata(filename) 299 Read a multicolumn data file ignoring lines starting with '#' 300 """ 301 f = open(filename,'r') 302 lines = [L for L in f.readlines() if not L.startswith('#')] 303 f.close() 304 return "".join(lines)
305 306
307 -def savedata(data, filename):
308 """ 309 Save data into file. Data is assumed to be five column 310 reflectivity data Q,dQ,R,dR,wavelength. 311 """ 312 [Q,dQ,R,dR,L] = data 313 fd=open(filename, mode='w') 314 for x in range(len(Q)) : 315 line = "%.15g %.15g %.15g %.15g %.15g\n"%(Q[x],dQ[x],R[x],dR[x],L[x]) 316 fd.write( aline ) 317 fd.close()
318