Changeset 574adc7 in sasview for src/sas/sascalc/data_util
- Timestamp:
- Sep 22, 2017 2:01:32 PM (7 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 34d7b35
- Parents:
- 9706d88
- Location:
- src/sas/sascalc/data_util
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sascalc/data_util/calcthread.py
ra1b8fee r574adc7 6 6 from __future__ import print_function 7 7 8 import thread9 8 import traceback 10 9 import sys 11 10 import logging 11 try: 12 import _thread as thread 13 except ImportError: # CRUFT: python 2 support 14 import thread 12 15 13 16 if sys.platform.count("darwin") > 0: 14 17 import time 15 18 stime = time.time() 16 19 17 20 def clock(): 18 21 return time.time() - stime 19 22 20 23 def sleep(t): 21 24 return time.sleep(t) … … 35 38 CalcThread.__init__, passing it the keyword arguments for 36 39 yieldtime, worktime, update and complete. 37 40 38 41 When defining the compute() method you need to include code which 39 42 allows the GUI to run. They are as follows: :: … … 211 214 self._lock.release() 212 215 self._time_for_update += 1e6 # No more updates 213 216 214 217 self.updatefn(**kwargs) 215 218 sleep(self.yieldtime) -
src/sas/sascalc/data_util/nxsunit.py
r8e9536f r574adc7 13 13 in the NeXus definition files. 14 14 15 Unlike other units packages, this package does not carry the units along with 15 Unlike other units packages, this package does not carry the units along with 16 16 the value but merely provides a conversion function for transforming values. 17 17 … … 68 68 Ack! Allows, e.g., Coulomb and coulomb even though Coulomb is not 69 69 a unit because some NeXus files store it that way! 70 70 71 71 Returns a dictionary of names and scales. 72 72 """ … … 78 78 n=1e-9,p=1e-12,f=1e-15) 79 79 map = {abbr:1} 80 map.update([(P+abbr,scale) for (P,scale) in short_prefix.ite ritems()])80 map.update([(P+abbr,scale) for (P,scale) in short_prefix.items()]) 81 81 for name in [unit,unit.capitalize()]: 82 82 map.update({name:1,name+'s':1}) 83 map.update([(P+name,scale) for (P,scale) in prefix.ite ritems()])84 map.update([(P+'*'+name,scale) for (P,scale) in prefix.ite ritems()])85 map.update([(P+name+'s',scale) for (P,scale) in prefix.ite ritems()])83 map.update([(P+name,scale) for (P,scale) in prefix.items()]) 84 map.update([(P+'*'+name,scale) for (P,scale) in prefix.items()]) 85 map.update([(P+name+'s',scale) for (P,scale) in prefix.items()]) 86 86 return map 87 87 … … 91 91 """ 92 92 map = {} 93 map.update([(name,scale) for name,scale in kw.ite ritems()])94 map.update([(name+'s',scale) for name,scale in kw.ite ritems()])93 map.update([(name,scale) for name,scale in kw.items()]) 94 map.update([(name+'s',scale) for name,scale in kw.items()]) 95 95 return map 96 96 … … 101 101 * WARNING * this will incorrect transform 10^3 to 103. 102 102 """ 103 s.update((k.replace('^',''),v) 104 for k, v in s.items()103 s.update((k.replace('^',''),v) 104 for k, v in list(s.items()) 105 105 if '^' in k) 106 106 … … 130 130 temperature.update(_build_metric_units('Celcius', 'C')) 131 131 temperature.update(_build_metric_units('celcius', 'C')) 132 132 133 133 charge = _build_metric_units('coulomb','C') 134 134 charge.update({'microAmp*hour':0.0036}) 135 135 136 136 sld = { '10^-6 Angstrom^-2': 1e-6, 'Angstrom^-2': 1 } 137 Q = { 'invA': 1, 'invAng': 1, 'invAngstroms': 1, '1/A': 1, 137 Q = { 'invA': 1, 'invAng': 1, 'invAngstroms': 1, '1/A': 1, 138 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 } … … 189 189 190 190 def _check(expect,get): 191 if expect != get: raise ValueError, "Expected %s but got %s"%(expect,get) 191 if expect != get: 192 raise ValueError("Expected %s but got %s"%(expect, get)) 192 193 #print expect,"==",get 193 194 … … 202 203 _check(123,Converter('a.u.')(123,units='s')) # arbitrary units always returns the same value 203 204 _check(123,Converter('a.u.')(123,units='')) # arbitrary units always returns the same value 204 try: Converter('help') 205 except KeyError: pass 206 else: raise Exception("unknown unit did not raise an error") 205 try: 206 Converter('help') 207 except KeyError: 208 pass 209 else: 210 raise Exception("unknown unit did not raise an error") 207 211 208 212 # TODO: more tests -
src/sas/sascalc/data_util/odict.py
rb699768 r574adc7 39 39 """ 40 40 A class of dictionary that keeps the insertion order of keys. 41 41 42 42 All appropriate methods return keys, items, or values in an ordered way. 43 43 44 44 All normal dictionary methods are available. Update and comparison is 45 45 restricted to other OrderedDict objects. 46 46 47 47 Various sequence methods are available, including the ability to explicitly 48 48 mutate the key ordering. 49 49 50 50 __contains__ tests: 51 51 52 52 >>> d = OrderedDict(((1, 3),)) 53 53 >>> 1 in d … … 55 55 >>> 4 in d 56 56 0 57 57 58 58 __getitem__ tests: 59 59 60 60 >>> OrderedDict(((1, 3), (3, 2), (2, 1)))[2] 61 61 1 … … 63 63 Traceback (most recent call last): 64 64 KeyError: 4 65 65 66 66 __len__ tests: 67 67 68 68 >>> len(OrderedDict()) 69 69 0 70 70 >>> len(OrderedDict(((1, 3), (3, 2), (2, 1)))) 71 71 3 72 72 73 73 get tests: 74 74 75 75 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1))) 76 76 >>> d.get(1) … … 82 82 >>> d 83 83 OrderedDict([(1, 3), (3, 2), (2, 1)]) 84 84 85 85 has_key tests: 86 86 87 87 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1))) 88 88 >>> d.has_key(1) … … 96 96 Create a new ordered dictionary. Cannot init from a normal dict, 97 97 nor from kwargs, since items order is undefined in those cases. 98 98 99 99 If the ``strict`` keyword argument is ``True`` (``False`` is the 100 100 default) then when doing slice assignment - the ``OrderedDict`` you are 101 101 assigning from *must not* contain any keys in the remaining dict. 102 102 103 103 >>> OrderedDict() 104 104 OrderedDict([]) … … 283 283 """ 284 284 Used for __repr__ and __str__ 285 285 286 286 >>> r1 = repr(OrderedDict((('a', 'b'), ('c', 'd'), ('e', 'f')))) 287 287 >>> r1 … … 321 321 >>> d 322 322 OrderedDict([(0, 1), (1, 2), (5, 6), (7, 8), (3, 4)]) 323 323 324 324 >>> a = OrderedDict(((0, 1), (1, 2), (2, 3)), strict=True) 325 325 >>> a[3] = 4 … … 345 345 >>> a 346 346 OrderedDict([(3, 4), (2, 3), (1, 2), (0, 1)]) 347 347 348 348 >>> d = OrderedDict([(0, 1), (1, 2), (2, 3), (3, 4)]) 349 349 >>> d[:1] = 3 350 350 Traceback (most recent call last): 351 351 TypeError: slice assignment requires an OrderedDict 352 352 353 353 >>> d = OrderedDict([(0, 1), (1, 2), (2, 3), (3, 4)]) 354 354 >>> d[:1] = OrderedDict([(9, 8)]) … … 444 444 """ 445 445 Implemented so that access to ``sequence`` raises a warning. 446 446 447 447 >>> d = OrderedDict() 448 448 >>> d.sequence … … 463 463 """ 464 464 To allow deepcopy to work with OrderedDict. 465 465 466 466 >>> from copy import deepcopy 467 467 >>> a = OrderedDict([(1, 1), (2, 2), (3, 3)]) … … 490 490 def items(self): 491 491 """ 492 ``items`` returns a list of tuples representing all the 492 ``items`` returns a list of tuples representing all the 493 493 ``(key, value)`` pairs in the dictionary. 494 494 495 495 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1))) 496 496 >>> d.items() … … 505 505 """ 506 506 Return a list of keys in the ``OrderedDict``. 507 507 508 508 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1))) 509 509 >>> d.keys() … … 515 515 """ 516 516 Return a list of all the values in the OrderedDict. 517 517 518 518 Optionally you can pass in a list of values, which will replace the 519 519 current list. The value list must be the same len as the OrderedDict. 520 520 521 521 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1))) 522 522 >>> d.values() … … 596 596 """ 597 597 No dict.pop in Python 2.2, gotta reimplement it 598 598 599 599 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1))) 600 600 >>> d.pop(3) … … 612 612 """ 613 613 if len(args) > 1: 614 raise TypeError ,('pop expected at most 2 arguments, got %s' %614 raise TypeError('pop expected at most 2 arguments, got %s' % 615 615 (len(args) + 1)) 616 616 if key in self: … … 628 628 Delete and return an item specified by index, not a random one as in 629 629 dict. The index is -1 by default (the last item). 630 630 631 631 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1))) 632 632 >>> d.popitem() … … 674 674 """ 675 675 Update from another OrderedDict or sequence of (key, value) pairs 676 676 677 677 >>> d = OrderedDict(((1, 0), (0, 1))) 678 678 >>> d.update(OrderedDict(((1, 3), (3, 2), (2, 1)))) … … 706 706 """ 707 707 Rename the key for a given value, without modifying sequence order. 708 708 709 709 For the case where new_key already exists this raise an exception, 710 710 since if new_key exists, it is ambiguous as to what happens to the 711 711 associated values, and the position of new_key in the sequence. 712 712 713 713 >>> od = OrderedDict() 714 714 >>> od['a'] = 1 … … 732 732 raise ValueError("New key already exists: %r" % new_key) 733 733 # rename sequence entry 734 value = self[old_key] 734 value = self[old_key] 735 735 old_idx = self._sequence.index(old_key) 736 736 self._sequence[old_idx] = new_key … … 742 742 """ 743 743 This method allows you to set the items in the dict. 744 744 745 745 It takes a list of tuples - of the same sort returned by the ``items`` 746 746 method. 747 747 748 748 >>> d = OrderedDict() 749 749 >>> d.setitems(((3, 1), (2, 3), (1, 2))) … … 760 760 replace the current set. This must contain the same set of keys, but 761 761 need not be in the same order. 762 762 763 763 If you pass in new keys that don't match, a ``KeyError`` will be 764 764 raised. 765 765 766 766 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1))) 767 767 >>> d.keys() … … 791 791 You can pass in a list of values, which will replace the 792 792 current list. The value list must be the same len as the OrderedDict. 793 793 794 794 (Or a ``ValueError`` is raised.) 795 795 796 796 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1))) 797 797 >>> d.setvalues((1, 2, 3)) … … 813 813 """ 814 814 Return the position of the specified key in the OrderedDict. 815 815 816 816 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1))) 817 817 >>> d.index(3) … … 826 826 """ 827 827 Takes ``index``, ``key``, and ``value`` as arguments. 828 828 829 829 Sets ``key`` to ``value``, so that ``key`` is at position ``index`` in 830 830 the OrderedDict. 831 831 832 832 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1))) 833 833 >>> d.insert(0, 4, 0) … … 850 850 """ 851 851 Reverse the order of the OrderedDict. 852 852 853 853 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1))) 854 854 >>> d.reverse() … … 861 861 """ 862 862 Sort the key order in the OrderedDict. 863 863 864 864 This method takes the same arguments as the ``list.sort`` method on 865 865 your version of Python. 866 866 867 867 >>> d = OrderedDict(((4, 1), (2, 2), (3, 3), (1, 4))) 868 868 >>> d.sort() … … 876 876 """ 877 877 Custom object for accessing the keys of an OrderedDict. 878 878 879 879 Can be called like the normal ``OrderedDict.keys`` method, but also 880 880 supports indexing and sequence methods. … … 897 897 You cannot assign to keys, but you can do slice assignment to re-order 898 898 them. 899 899 900 900 You can only do slice assignment if the new set of keys is a reordering 901 901 of the original set. … … 967 967 """ 968 968 Custom object for accessing the items of an OrderedDict. 969 969 970 970 Can be called like the normal ``OrderedDict.items`` method, but also 971 971 supports indexing and sequence methods. … … 1077 1077 """ 1078 1078 Custom object for accessing the values of an OrderedDict. 1079 1079 1080 1080 Can be called like the normal ``OrderedDict.values`` method, but also 1081 1081 supports indexing and sequence methods. … … 1099 1099 """ 1100 1100 Set the value at position i to value. 1101 1101 1102 1102 You can only do slice assignment to values if you supply a sequence of 1103 1103 equal length to the slice you are replacing. … … 1168 1168 Experimental version of OrderedDict that has a custom object for ``keys``, 1169 1169 ``values``, and ``items``. 1170 1170 1171 1171 These are callable sequence objects that work as methods, or can be 1172 1172 manipulated directly as sequences. 1173 1173 1174 1174 Test for ``keys``, ``items`` and ``values``. 1175 1175 1176 1176 >>> d = SequenceOrderedDict(((1, 2), (2, 3), (3, 4))) 1177 1177 >>> d … … 1293 1293 >>> d 1294 1294 SequenceOrderedDict([(1, 1), (2, 2), (3, 3)]) 1295 1295 1296 1296 >>> d = SequenceOrderedDict(((1, 2), (2, 3), (3, 4))) 1297 1297 >>> d -
src/sas/sascalc/data_util/uncertainty.py
r9a5097c r574adc7 2 2 Uncertainty propagation class for arithmetic, log and exp. 3 3 4 Based on scalars or numpy vectors, this class allows you to store and 4 Based on scalars or numpy vectors, this class allows you to store and 5 5 manipulate values+uncertainties, with propagation of gaussian error for 6 6 addition, subtraction, multiplication, division, power, exp and log. 7 7 8 8 Storage properties are determined by the numbers used to set the value 9 and uncertainty. Be sure to use floating point uncertainty vectors 9 and uncertainty. Be sure to use floating point uncertainty vectors 10 10 for inplace operations since numpy does not do automatic type conversion. 11 11 Normal operations can use mixed integer and floating point. In place … … 18 18 19 19 import numpy as np 20 import err1d 21 from formatnum import format_uncertainty 20 21 from .import err1d 22 from .formatnum import format_uncertainty 22 23 23 24 __all__ = ['Uncertainty'] … … 28 29 # Make standard deviation available 29 30 def _getdx(self): return np.sqrt(self.variance) 30 def _setdx(self,dx): 31 def _setdx(self,dx): 31 32 # Direct operation 32 33 # variance = dx**2 … … 38 39 # Constructor 39 40 def __init__(self, x, variance=None): 40 self.x, self.variance = x, variance 41 41 self.x, self.variance = x, variance 42 42 43 # Numpy array slicing operations 43 def __len__(self): 44 def __len__(self): 44 45 return len(self.x) 45 def __getitem__(self,key): 46 def __getitem__(self,key): 46 47 return Uncertainty(self.x[key],self.variance[key]) 47 48 def __setitem__(self,key,value): … … 137 138 def __idiv__(self, other): return self.__itruediv__(other) 138 139 139 140 140 141 # Unary ops 141 142 def __neg__(self): … … 151 152 return format_uncertainty(self.x,np.sqrt(self.variance)) 152 153 else: 153 return [format_uncertainty(v,dv) 154 return [format_uncertainty(v,dv) 154 155 for v,dv in zip(self.x,np.sqrt(self.variance))] 155 156 def __repr__(self): … … 219 220 z = a/4 220 221 assert z.x == 5./4 and z.variance == 3./4**2 221 222 222 223 # Reverse scalar operations 223 224 z = 4+a … … 229 230 z = 4/a 230 231 assert z.x == 4./5 and abs(z.variance - 3./5**4 * 4**2) < 1e-15 231 232 232 233 # Power operations 233 234 z = a**2 … … 250 251 assert z.x == 5./4 and abs(z.variance - (3./5**2 + 2./4**2)*(5./4)**2) < 1e-15 251 252 252 # ===== Inplace operations ===== 253 # ===== Inplace operations ===== 253 254 # Scalar operations 254 255 y = a+0; y += 4 … … 308 309 assert (z.x == 5./4).all() 309 310 assert (abs(z.variance - (3./5**2 + 2./4**2)*(5./4)**2) < 1e-15).all() 310 311 311 312 # printing; note that sqrt(3) ~ 1.7 312 313 assert str(Uncertainty(5,3)) == "5.0(17)"
Note: See TracChangeset
for help on using the changeset viewer.