Changeset 4026380 in sasview for DataLoader
- Timestamp:
- Nov 14, 2009 10:33:21 AM (15 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, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- fd6b789
- Parents:
- c13b8cc
- Location:
- DataLoader
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
DataLoader/data_info.py
rfe78c7b r4026380 564 564 return _str 565 565 566 def is_slit_smeared(self): 567 """ 568 Check whether the data has slit smearing information 569 570 @return: True is slit smearing info is present, False otherwise 571 """ 572 def _check(v): 573 if (v.__class__==list or v.__class__==numpy.ndarray) \ 574 and len(v)>0 and min(v)>0: 575 return True 576 577 return False 578 579 return _check(self.dxl) or _check(self.dxw) 580 566 581 def clone_without_data(self, length=0, clone=None): 567 582 """ -
DataLoader/invariant.py
r01fb70b r4026380 14 14 @note: The data boundaries are assumed as infinite range. 15 15 """ 16 def __init__(self, data ,contrast=None, pConst=None):16 def __init__(self, data): 17 17 """ 18 18 Initialize variables … … 21 21 @param pConst: Porod Constant of type float 22 22 """ 23 self.q_star = self.getQstar(data= data) 24 self.volume = self._getVolFrac(contrast= contrast) 25 self.surface= self._getSurface(pConst= pConst) 23 ## Invariant 24 self.q_star = self._get_q_star(data= data) 26 25 27 def __call__(self, contrast, pConst): 28 """ 29 @precondition: self.q_star has already been computed 30 """ 31 self.volume = self._getVolFrac(contrast= contrast) 32 self.surface = self._getSurface(pConst= pConst) 33 34 return self 35 36 def getQstar(self, data): 26 def _get_q_star(self, data): 37 27 """ 38 28 @param data: data of type Data1D … … 42 32 #Process only data that inherited from DataLoader.Data_info.Data1D 43 33 raise ValueError,"Data must be of type DataLoader.Data1D" 44 if data.dxl == None: 45 return self._getQStarUnsmear(data= data) 34 35 # Check whether we have slit smearing information 36 if data.is_slit_smeared(): 37 return self._get_qstar_unsmeared(data= data) 46 38 else: 47 return self._get QStarSmear(data= data)39 return self._get_qstar_smeared(data= data) 48 40 49 41 50 42 51 def _get QStarUnsmear(self, data):43 def _get_qstar_unsmeared(self, data): 52 44 """ 53 45 @param data: data of type Data1D … … 86 78 87 79 88 def _get QStarSmear(self, data):80 def _get_qstar_smeared(self, data): 89 81 """ 90 82 @param data: data of type Data1D 91 Compute invariant with s mearing info83 Compute invariant with slit smearing info 92 84 q_star= x0*dxl *y0 *dx0 + x1*dxl *y1 *dx1 + ..+ xn*dxl *yn *dxn 93 85 where n= infinity … … 125 117 return sum 126 118 127 def _getVolFrac(self,contrast):119 def get_volume_fraction(self, contrast): 128 120 """ 129 121 Compute volume fraction is given by: … … 137 129 q_star unit 1/A^(3)*1/cm 138 130 139 the result returned will be 0<= volume <= 1 or None131 the result returned will be 0<= volume <= 1 140 132 141 133 @param contrast: contrast value provides by the user of type float 142 @return None : if the invariant Calculator does not a computed 143 q_star already stored 134 @return: volume fraction 144 135 @note: volume fraction must have no unit 145 136 """ 146 if contrast ==None:147 #No contrast value is provided for calculation then no calculation148 return149 137 if contrast < 0: 150 138 raise ValueError, "contrast must be greater than zero" 151 139 152 140 if self.q_star ==None: 153 r eturn141 raise RuntimeError, "Q_star is not defined" 154 142 155 143 if self.q_star < 0: 156 raise ValueError, "invariant must be greater than zero "144 raise ValueError, "invariant must be greater than zero. Q_star=%g" % self.q_star 157 145 158 146 #compute intermediate constant … … 161 149 discrim= 1 - 4*k 162 150 if discrim < 0: 163 r eturn151 raise RuntimeError, "could not compute the volume fraction: negative discriminant" 164 152 elif discrim ==0: 165 153 volume = 1/2 … … 169 157 volume1 = 0.5 *(1 - math.sqrt(discrim)) 170 158 volume2 = 0.5 *(1 + math.sqrt(discrim)) 171 print "volume1",volume1172 print "volume2",volume2173 159 174 160 if 0<= volume1 and volume1 <= 1: … … 176 162 elif 0<= volume2 and volume2<= 1: 177 163 return volume2 178 r eturn164 raise RuntimeError, "could not compute the volume fraction: inconsistent results" 179 165 180 def _getSurface(self, pConst, volume=None):166 def get_surface(self, contrast, porod_const): 181 167 """ 182 168 Compute the surface given by: 183 169 surface = (2*pi *volume(1- volume)*pConst)/ q_star 184 @param volume : volume previously calculated 185 @return None: if volume used for computation equal None 170 171 @param contrast: contrast value provides by the user of type float 172 @param porod_const: Porod constant 173 @return: specific surface 186 174 """ 187 #check if the user provides a value for volume 188 if volume != None: 189 self.volume = float(volume) 175 # Compute the volume 176 volume = self.get_volume_fraction(contrast) 190 177 191 #return None if volume or q_star is not computed 192 if self.q_star ==None or self.volume == None: 193 return 178 # Check whether we have Q star 179 if self.q_star ==None: 180 raise RuntimeError, "Q_star is not defined" 181 194 182 if self.q_star ==0: 195 raise ZeroDivisionError, "invariant must be greater than zero"183 raise ValueError, "invariant must be greater than zero. Q_star=%g" % self.q_star 196 184 197 return 2*math.pi* self.volume*(1- self.volume)*float(pConst)/self.q_star185 return 2*math.pi*volume*(1-volume)*float(porod_const)/self.q_star 198 186 199 187 -
DataLoader/test/utest_invariant.py
rac13512 r4026380 15 15 16 16 def setUp(self): 17 #Data iwth no slit smear information 18 data0= Loader().load("PolySpheres.txt") 19 self.I0 = InvariantCalculator( data= data0,contrast=2.6e-6, pConst=20) 17 #Data with no slit smear information 18 data0 = Loader().load("PolySpheres.txt") 19 self.I0 = InvariantCalculator( data=data0) 20 20 21 # data with smear info 21 22 list = Loader().load("latex_smeared.xml") 22 23 data1= list[0] 23 self.I1= InvariantCalculator( data= data1,contrast=5.3e-6, pConst=20) 24 self.I1= InvariantCalculator( data= data1) 25 24 26 data2= list[1] 25 self.I2= InvariantCalculator( data= data2 ,contrast=5.3e-6, pConst=20)27 self.I2= InvariantCalculator( data= data2) 26 28 27 def test Invariant(self):29 def test_invariant(self): 28 30 """ test the invariant value for data with no slit smear""" 29 31 self.assertAlmostEquals(self.I0.q_star, 7.48959e-5) 32 self.assertTrue(self.I1.q_star>0) 33 self.assertTrue(self.I2.q_star>0) 30 34 31 32 def test_Computation(self): 35 def test_computation(self): 33 36 """ 34 37 Test identity smearing 35 38 """ 36 # compute invariant with smear information 37 print "invariant initialized only with data:", self.I1 38 print "invariant q_star",self.I1.q_star 39 print "invariant volume",self.I1.volume 40 print "Invariant surface",self.I1.surface 41 print 42 print "invariant initialized __call__:", self.I1 43 print "invariant q_star",self.I1.q_star 44 print "invariant volume",self.I1.volume 45 print "Invariant surface",self.I1.surface 46 print 47 # compute invariant with smear information 48 print "invariant initialize with data, contrast,pConst:", self.I2 49 print "invariant q_star",self.I2.q_star 50 print "invariant volume",self.I2.volume 51 print "Invariant surface",self.I2.surface 52 print 39 vol = self.I0.get_volume_fraction(contrast=2.6e-6) 40 surface = self.I0.get_surface(contrast=2.6e-6, porod_const=20) 41 42 # TODO: Need to test output values 43 #self.assertAlmostEquals(vol, 0) 44 #self.assertAlmostEquals(surface, 0) 45 vol = self.I1.get_volume_fraction(contrast=5.3e-6) 46 surface = self.I1.get_surface(contrast=5.3e-6, porod_const=20) 47 48 # TODO: Need to test output values 49 #self.assertAlmostEquals(vol, 0) 50 #self.assertAlmostEquals(surface, 0) 51 52 vol = self.I2.get_volume_fraction(contrast=5.3e-6) 53 surface = self.I2.get_surface(contrast=5.3e-6, porod_const=20) 54 55 # TODO: Need to test output values 56 self.assertAlmostEquals(vol, 0) 57 self.assertAlmostEquals(surface, 0) 58 53 59 54 60
Note: See TracChangeset
for help on using the changeset viewer.