Ignore:
Timestamp:
Sep 22, 2017 2:01:32 PM (7 years ago)
Author:
Paul Kienzle <pkienzle@…>
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
Message:

convert sascalc to python 2/3 syntax

File:
1 edited

Legend:

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

    r9a5097c r574adc7  
    22Uncertainty propagation class for arithmetic, log and exp. 
    33 
    4 Based on scalars or numpy vectors, this class allows you to store and  
     4Based on scalars or numpy vectors, this class allows you to store and 
    55manipulate values+uncertainties, with propagation of gaussian error for 
    66addition, subtraction, multiplication, division, power, exp and log. 
    77 
    88Storage properties are determined by the numbers used to set the value 
    9 and uncertainty.  Be sure to use floating point uncertainty vectors  
     9and uncertainty.  Be sure to use floating point uncertainty vectors 
    1010for inplace operations since numpy does not do automatic type conversion. 
    1111Normal operations can use mixed integer and floating point.  In place 
     
    1818 
    1919import numpy as np 
    20 import err1d 
    21 from formatnum import format_uncertainty 
     20 
     21from .import err1d 
     22from .formatnum import format_uncertainty 
    2223 
    2324__all__ = ['Uncertainty'] 
     
    2829    # Make standard deviation available 
    2930    def _getdx(self): return np.sqrt(self.variance) 
    30     def _setdx(self,dx):  
     31    def _setdx(self,dx): 
    3132        # Direct operation 
    3233        #    variance = dx**2 
     
    3839    # Constructor 
    3940    def __init__(self, x, variance=None): 
    40         self.x, self.variance = x, variance     
    41   
     41        self.x, self.variance = x, variance 
     42 
    4243    # Numpy array slicing operations 
    43     def __len__(self):  
     44    def __len__(self): 
    4445        return len(self.x) 
    45     def __getitem__(self,key):  
     46    def __getitem__(self,key): 
    4647        return Uncertainty(self.x[key],self.variance[key]) 
    4748    def __setitem__(self,key,value): 
     
    137138    def __idiv__(self, other): return self.__itruediv__(other) 
    138139 
    139          
     140 
    140141    # Unary ops 
    141142    def __neg__(self): 
     
    151152            return format_uncertainty(self.x,np.sqrt(self.variance)) 
    152153        else: 
    153             return [format_uncertainty(v,dv)  
     154            return [format_uncertainty(v,dv) 
    154155                    for v,dv in zip(self.x,np.sqrt(self.variance))] 
    155156    def __repr__(self): 
     
    219220    z = a/4 
    220221    assert z.x == 5./4 and z.variance == 3./4**2 
    221      
     222 
    222223    # Reverse scalar operations 
    223224    z = 4+a 
     
    229230    z = 4/a 
    230231    assert z.x == 4./5 and abs(z.variance - 3./5**4 * 4**2) < 1e-15 
    231      
     232 
    232233    # Power operations 
    233234    z = a**2 
     
    250251    assert z.x == 5./4 and abs(z.variance - (3./5**2 + 2./4**2)*(5./4)**2) < 1e-15 
    251252 
    252     # ===== Inplace operations =====     
     253    # ===== Inplace operations ===== 
    253254    # Scalar operations 
    254255    y = a+0; y += 4 
     
    308309    assert (z.x == 5./4).all() 
    309310    assert (abs(z.variance - (3./5**2 + 2./4**2)*(5./4)**2) < 1e-15).all() 
    310      
     311 
    311312    # printing; note that sqrt(3) ~ 1.7 
    312313    assert str(Uncertainty(5,3)) == "5.0(17)" 
Note: See TracChangeset for help on using the changeset viewer.