Changeset 7b15990 in sasview for src/sas/sascalc/data_util


Ignore:
Timestamp:
Sep 18, 2017 2:45:52 PM (7 years ago)
Author:
Paul Kienzle <pkienzle@…>
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.
Message:

Merge branch 'master' into costrafo411

Location:
src/sas/sascalc/data_util
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • src/sas/sascalc/data_util/calcthread.py

    r7432acb ra1b8fee  
    44#  \brief Abstract class for defining calculation threads. 
    55# 
     6from __future__ import print_function 
    67 
    78import thread 
     
    295296    """ 
    296297    def __init__(self, n=20000): 
    297         print thread.get_ident() 
     298        print(thread.get_ident()) 
    298299        self.starttime = clock() 
    299300        self.done = False 
     
    307308        self.work2.queue(n) 
    308309        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("") 
    311312        self.work.ready(.5) 
    312313        while not self.done: 
    313314            sleep(1) 
    314             print "Main thread %d at %.2f" % (thread.get_ident(), 
    315                                               clock() - self.starttime) 
     315            print("Main thread %d at %.2f" % (thread.get_ident(), 
     316                                              clock() - self.starttime)) 
    316317 
    317318    def update(self, i=0): 
    318         print "Update i=%d from thread %d at %.2f" % (i, thread.get_ident(), 
    319                                                       clock() - self.starttime) 
     319        print("Update i=%d from thread %d at %.2f" % (i, thread.get_ident(), 
     320                                                      clock() - self.starttime)) 
    320321        self.work.ready(2.5) 
    321322 
    322323    def complete(self, total=0.0): 
    323         print "Complete total=%g from thread %d at %.2f" % (total, 
     324        print("Complete total=%g from thread %d at %.2f" % (total, 
    324325                                                    thread.get_ident(), 
    325                                                     clock() - self.starttime) 
     326                                                    clock() - self.starttime)) 
    326327        self.done = True 
  • src/sas/sascalc/data_util/formatnum.py

    r9a5097c ra1b8fee  
    3737formatter.compact flag. 
    3838""" 
    39 from __future__ import division 
     39from __future__ import division, print_function 
    4040 
    4141import math 
  • src/sas/sascalc/data_util/nxsunit.py

    rb699768 r8e9536f  
    136136    sld = { '10^-6 Angstrom^-2': 1e-6, 'Angstrom^-2': 1 } 
    137137    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, 
    139139          'nm^-1': 0.1, '1/nm': 0.1, 'n_m^-1': 0.1 } 
    140140 
     
    195195    _check(1,Converter('n_m^-1')(10,'invA')) # 10 nm^-1 = 1 inv Angstroms 
    196196    _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 
    197198    _check(0.003,Converter('microseconds')(3,units='ms')) # 3 us -> 0.003 ms 
    198199    _check(45,Converter('nanokelvin')(45))  # 45 nK -> 45 nK 
  • src/sas/sascalc/data_util/qsmearing.py

    r9f59333 r7b15990  
    6565            raise ValueError('one or more of your dx values are negative, please check the data file!') 
    6666 
    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) 
    7069        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 
    7275        Rmax = 10000000 
     76        # Then return the actual transform, as if it were a smearing function 
    7377        # data must have the isoriented flag here! 
    74         # Then return the actual transform, as if it were a smearing function 
    7578        if getattr(data, 'isoriented', False): 
    7679            costransform = OrientedSesansTransform(data.x, SElength, zaccept, Rmax) 
    7780            return PySmear(costransform, model, offset=0) 
    7881        else: 
    79             hankel = SesansTransform(data.x, SElength, zaccept, Rmax) 
     82            hankel = SesansTransform(data.x, SElength, 
     83                                     data.source.wavelength, zaccept, Rmax) 
    8084            return PySmear(hankel, model, offset=0) 
    8185 
  • src/sas/sascalc/data_util/registry.py

    rb699768 r5a8cdbb  
    1 # This program is public domain 
    21""" 
    32File extension registry. 
     
    65and registers the built-in file extensions. 
    76""" 
     7from __future__ import print_function 
    88 
    9 import os.path 
     9from sas.sascalc.dataloader.loader_exceptions import NoKnownLoaderException 
     10 
    1011 
    1112class ExtensionRegistry(object): 
     
    2122        # Add an association by setting an element 
    2223        registry['.zip'] = unzip 
    23          
     24 
    2425        # Multiple extensions for one loader 
    2526        registry['.tgz'] = untar 
    2627        registry['.tar.gz'] = untar 
    2728 
    28         # Generic extensions to use after trying more specific extensions;  
     29        # Generic extensions to use after trying more specific extensions; 
    2930        # these will be checked after the more specific extensions fail. 
    3031        registry['.gz'] = gunzip 
     
    3738        # Show registered extensions 
    3839        print registry.extensions() 
    39          
     40 
    4041        # Can also register a format name for explicit control from caller 
    4142        registry['cx3'] = cx3 
     
    6162    def __init__(self, **kw): 
    6263        self.loaders = {} 
     64 
    6365    def __setitem__(self, ext, loader): 
    6466        if ext not in self.loaders: 
    6567            self.loaders[ext] = [] 
    6668        self.loaders[ext].insert(0,loader) 
     69 
    6770    def __getitem__(self, ext): 
    6871        return self.loaders[ext] 
     72 
    6973    def __contains__(self, ext): 
    7074        return ext in self.loaders 
     75 
    7176    def formats(self): 
    7277        """ 
     
    7681        names.sort() 
    7782        return names 
     83 
    7884    def extensions(self): 
    7985        """ 
     
    8389        exts.sort() 
    8490        return exts 
     91 
    8592    def lookup(self, path): 
    8693        """ 
    8794        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        """ 
    91100        # Find matching extensions 
    92101        extlist = [ext for ext in self.extensions() if path.endswith(ext)] 
     
    105114        # Raise an error if there are no matching extensions 
    106115        if len(loaders) == 0: 
    107             raise ValueError, "Unknown file type for "+path 
    108         # All done 
     116            raise ValueError("Unknown file type for "+path) 
    109117        return loaders 
     118 
    110119    def load(self, path, format=None): 
    111120        """ 
    112121        Call the loader for the file type of path. 
    113122 
    114         Raises ValueError if no loader is available. 
    115         Raises KeyError if 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. 
    117126        """ 
     127        loaders = [] 
    118128        if format is None: 
    119             loaders = self.lookup(path) 
     129            try: 
     130                loaders = self.lookup(path) 
     131            except ValueError as e: 
     132                pass 
    120133        else: 
    121             loaders = self.loaders[format] 
     134            try: 
     135                loaders = self.loaders[format] 
     136            except KeyError as e: 
     137                pass 
     138        last_exc = None 
    122139        for fn in loaders: 
    123140            try: 
    124141                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 
    127145        # 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.