Changeset 574adc7 in sasview for src/sas/sascalc/data_util/uncertainty.py
- Timestamp:
- Sep 22, 2017 4: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
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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.