Changeset 7b15990 in sasview for src/sas/sascalc/data_util
- Timestamp:
- Sep 18, 2017 2:45:52 PM (7 years ago)
- Branches:
- costrafo411
- Parents:
- 9f59333 (diff), cfd27dd (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent. - Location:
- src/sas/sascalc/data_util
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sascalc/data_util/calcthread.py
r7432acb ra1b8fee 4 4 # \brief Abstract class for defining calculation threads. 5 5 # 6 from __future__ import print_function 6 7 7 8 import thread … … 295 296 """ 296 297 def __init__(self, n=20000): 297 print thread.get_ident()298 print(thread.get_ident()) 298 299 self.starttime = clock() 299 300 self.done = False … … 307 308 self.work2.queue(n) 308 309 self.work3.queue(n) 309 print "Expect updates from Main every second and from thread every 2.5 seconds"310 print ""310 print("Expect updates from Main every second and from thread every 2.5 seconds") 311 print("") 311 312 self.work.ready(.5) 312 313 while not self.done: 313 314 sleep(1) 314 print 315 clock() - self.starttime) 315 print("Main thread %d at %.2f" % (thread.get_ident(), 316 clock() - self.starttime)) 316 317 317 318 def update(self, i=0): 318 print 319 clock() - self.starttime) 319 print("Update i=%d from thread %d at %.2f" % (i, thread.get_ident(), 320 clock() - self.starttime)) 320 321 self.work.ready(2.5) 321 322 322 323 def complete(self, total=0.0): 323 print 324 print("Complete total=%g from thread %d at %.2f" % (total, 324 325 thread.get_ident(), 325 clock() - self.starttime) 326 clock() - self.starttime)) 326 327 self.done = True -
src/sas/sascalc/data_util/formatnum.py
r9a5097c ra1b8fee 37 37 formatter.compact flag. 38 38 """ 39 from __future__ import division 39 from __future__ import division, print_function 40 40 41 41 import math -
src/sas/sascalc/data_util/nxsunit.py
rb699768 r8e9536f 136 136 sld = { '10^-6 Angstrom^-2': 1e-6, 'Angstrom^-2': 1 } 137 137 Q = { 'invA': 1, 'invAng': 1, 'invAngstroms': 1, '1/A': 1, 138 '10^-3 Angstrom^-1': 1e-3, '1/cm': 1e-8, 138 '10^-3 Angstrom^-1': 1e-3, '1/cm': 1e-8, '1/m': 1e-10, 139 139 'nm^-1': 0.1, '1/nm': 0.1, 'n_m^-1': 0.1 } 140 140 … … 195 195 _check(1,Converter('n_m^-1')(10,'invA')) # 10 nm^-1 = 1 inv Angstroms 196 196 _check(2,Converter('mm')(2000,'m')) # 2000 mm -> 2 m 197 _check(2.011e10,Converter('1/A')(2.011,"1/m")) # 2.011 1/A -> 2.011 * 10^10 1/m 197 198 _check(0.003,Converter('microseconds')(3,units='ms')) # 3 us -> 0.003 ms 198 199 _check(45,Converter('nanokelvin')(45)) # 45 nK -> 45 nK -
src/sas/sascalc/data_util/qsmearing.py
r9f59333 r7b15990 65 65 raise ValueError('one or more of your dx values are negative, please check the data file!') 66 66 67 if _found_sesans == True: 68 #Pre-compute the Hankel matrix (H) 69 qmax, qunits = data.sample.zacceptance 67 if _found_sesans: 68 # Pre-compute the Hankel matrix (H) 70 69 SElength = Converter(data._xunit)(data.x, "A") 71 zaccept = Converter(qunits)(qmax, "1/A"), 70 71 theta_max = Converter("radians")(data.sample.zacceptance)[0] 72 q_max = 2 * np.pi / np.max(data.source.wavelength) * np.sin(theta_max) 73 zaccept = Converter("1/A")(q_max, "1/" + data.source.wavelength_unit), 74 72 75 Rmax = 10000000 76 # Then return the actual transform, as if it were a smearing function 73 77 # data must have the isoriented flag here! 74 # Then return the actual transform, as if it were a smearing function75 78 if getattr(data, 'isoriented', False): 76 79 costransform = OrientedSesansTransform(data.x, SElength, zaccept, Rmax) 77 80 return PySmear(costransform, model, offset=0) 78 81 else: 79 hankel = SesansTransform(data.x, SElength, zaccept, Rmax) 82 hankel = SesansTransform(data.x, SElength, 83 data.source.wavelength, zaccept, Rmax) 80 84 return PySmear(hankel, model, offset=0) 81 85 -
src/sas/sascalc/data_util/registry.py
rb699768 r5a8cdbb 1 # This program is public domain2 1 """ 3 2 File extension registry. … … 6 5 and registers the built-in file extensions. 7 6 """ 7 from __future__ import print_function 8 8 9 import os.path 9 from sas.sascalc.dataloader.loader_exceptions import NoKnownLoaderException 10 10 11 11 12 class ExtensionRegistry(object): … … 21 22 # Add an association by setting an element 22 23 registry['.zip'] = unzip 23 24 24 25 # Multiple extensions for one loader 25 26 registry['.tgz'] = untar 26 27 registry['.tar.gz'] = untar 27 28 28 # Generic extensions to use after trying more specific extensions; 29 # Generic extensions to use after trying more specific extensions; 29 30 # these will be checked after the more specific extensions fail. 30 31 registry['.gz'] = gunzip … … 37 38 # Show registered extensions 38 39 print registry.extensions() 39 40 40 41 # Can also register a format name for explicit control from caller 41 42 registry['cx3'] = cx3 … … 61 62 def __init__(self, **kw): 62 63 self.loaders = {} 64 63 65 def __setitem__(self, ext, loader): 64 66 if ext not in self.loaders: 65 67 self.loaders[ext] = [] 66 68 self.loaders[ext].insert(0,loader) 69 67 70 def __getitem__(self, ext): 68 71 return self.loaders[ext] 72 69 73 def __contains__(self, ext): 70 74 return ext in self.loaders 75 71 76 def formats(self): 72 77 """ … … 76 81 names.sort() 77 82 return names 83 78 84 def extensions(self): 79 85 """ … … 83 89 exts.sort() 84 90 return exts 91 85 92 def lookup(self, path): 86 93 """ 87 94 Return the loader associated with the file type of path. 88 89 Raises ValueError if file type is not known. 90 """ 95 96 :param path: Data file path 97 :raises ValueError: When no loaders are found for the file. 98 :return: List of available readers for the file extension 99 """ 91 100 # Find matching extensions 92 101 extlist = [ext for ext in self.extensions() if path.endswith(ext)] … … 105 114 # Raise an error if there are no matching extensions 106 115 if len(loaders) == 0: 107 raise ValueError, "Unknown file type for "+path 108 # All done 116 raise ValueError("Unknown file type for "+path) 109 117 return loaders 118 110 119 def load(self, path, format=None): 111 120 """ 112 121 Call the loader for the file type of path. 113 122 114 Raises ValueErrorif no loader is available.115 Raises KeyErrorif format is not available.116 May raise a loader-defined exception if loader fails. 123 :raise ValueError: if no loader is available. 124 :raise KeyError: if format is not available. 125 May raise a loader-defined exception if loader fails. 117 126 """ 127 loaders = [] 118 128 if format is None: 119 loaders = self.lookup(path) 129 try: 130 loaders = self.lookup(path) 131 except ValueError as e: 132 pass 120 133 else: 121 loaders = self.loaders[format] 134 try: 135 loaders = self.loaders[format] 136 except KeyError as e: 137 pass 138 last_exc = None 122 139 for fn in loaders: 123 140 try: 124 141 return fn(path) 125 except: 126 pass # give other loaders a chance to succeed 142 except Exception as e: 143 last_exc = e 144 pass # give other loaders a chance to succeed 127 145 # If we get here it is because all loaders failed 128 raise # reraises last exception 129 130 def test(): 131 reg = ExtensionRegistry() 132 class CxError(Exception): pass 133 def cx(file): return 'cx' 134 def new_cx(file): return 'new_cx' 135 def fail_cx(file): raise CxError 136 def cat(file): return 'cat' 137 def gunzip(file): return 'gunzip' 138 reg['.cx'] = cx 139 reg['.cx1'] = cx 140 reg['.cx'] = new_cx 141 reg['.gz'] = gunzip 142 reg['.cx.gz'] = new_cx 143 reg['.cx1.gz'] = fail_cx 144 reg['.cx1'] = fail_cx 145 reg['.cx2'] = fail_cx 146 reg['new_cx'] = new_cx 147 148 # Two loaders associated with .cx 149 assert reg.lookup('hello.cx') == [new_cx,cx] 150 # Make sure the last loader applies first 151 assert reg.load('hello.cx') == 'new_cx' 152 # Make sure the next loader applies if the first fails 153 assert reg.load('hello.cx1') == 'cx' 154 # Make sure the format override works 155 assert reg.load('hello.cx1',format='.cx.gz') == 'new_cx' 156 # Make sure the format override works 157 assert reg.load('hello.cx1',format='new_cx') == 'new_cx' 158 # Make sure the case of all loaders failing is correct 159 try: reg.load('hello.cx2') 160 except CxError: pass # correct failure 161 else: raise AssertError,"Incorrect error on load failure" 162 # Make sure the case of no loaders fails correctly 163 try: reg.load('hello.missing') 164 except ValueError,msg: 165 assert str(msg)=="Unknown file type for hello.missing",'Message: <%s>'%(msg) 166 else: raise AssertError,"No error raised for missing extension" 167 assert reg.formats() == ['new_cx'] 168 assert reg.extensions() == ['.cx','.cx.gz','.cx1','.cx1.gz','.cx2','.gz'] 169 # make sure that it supports multiple '.' in filename 170 assert reg.load('hello.extra.cx1') == 'cx' 171 assert reg.load('hello.gz') == 'gunzip' 172 assert reg.load('hello.cx1.gz') == 'gunzip' # Since .cx1.gz fails 173 174 if __name__ == "__main__": test() 146 if last_exc is not None and len(loaders) != 0: 147 # If file has associated loader(s) and they;ve failed 148 raise last_exc 149 raise NoKnownLoaderException(e.message) # raise generic exception
Note: See TracChangeset
for help on using the changeset viewer.