Changeset 25427d9 in sasview for src/sas/sascalc
- Timestamp:
- Mar 21, 2016 8:32:15 AM (8 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:
- cf92b1f, 5757bbe
- Parents:
- 1c0e3b0 (diff), bb0c836 (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. - Location:
- src/sas/sascalc
- Files:
-
- 1 added
- 3 edited
- 11 moved
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sascalc/calculator/BaseComponent.py
r79492222 r9e531f2 1 1 #!/usr/bin/env python 2 2 3 """ 3 """ 4 4 Provide base functionality for all model components 5 5 """ 6 6 7 # imports 7 # imports 8 8 import copy 9 9 import numpy 10 10 #TO DO: that about a way to make the parameter 11 #is self return if it is fittable or not 11 #is self return if it is fittable or not 12 12 13 13 class BaseComponent: 14 """ 14 """ 15 15 Basic model component 16 16 17 17 Since version 0.5.0, basic operations are no longer supported. 18 18 """ … … 20 20 def __init__(self): 21 21 """ Initialization""" 22 22 23 23 ## Name of the model 24 24 self.name = "BaseComponent" 25 25 26 26 ## Parameters to be accessed by client 27 27 self.params = {} … … 49 49 self.output_name = "Intensity" 50 50 self.output_unit = "cm^{-1}" 51 51 52 52 def __str__(self): 53 """ 53 """ 54 54 :return: string representatio 55 56 55 """ 57 56 return self.name 58 57 59 58 def is_fittable(self, par_name): 60 59 """ 61 60 Check if a given parameter is fittable or not 62 63 :param par_name: the parameter name to check 64 61 62 :param par_name: the parameter name to check 63 65 64 """ 66 65 return par_name.lower() in self.fixed 67 66 #For the future 68 67 #return self.params[str(par_name)].is_fittable() 69 70 def run(self, x): 68 69 def run(self, x): 71 70 """ 72 71 run 1d 73 72 """ 74 73 return NotImplemented 75 76 def runXY(self, x): 74 75 def runXY(self, x): 77 76 """ 78 77 run 2d 79 78 """ 80 return NotImplemented 81 82 def calculate_ER(self): 79 return NotImplemented 80 81 def calculate_ER(self): 83 82 """ 84 83 Calculate effective radius 85 84 """ 86 return NotImplemented 87 88 def calculate_VR(self): 85 return NotImplemented 86 87 def calculate_VR(self): 89 88 """ 90 89 Calculate volume fraction ratio 91 90 """ 92 return NotImplemented 93 91 return NotImplemented 92 94 93 def evalDistribution(self, qdist): 95 94 """ 96 95 Evaluate a distribution of q-values. 97 96 98 97 * For 1D, a numpy array is expected as input: :: 99 98 100 99 evalDistribution(q) 101 100 102 101 where q is a numpy array. 103 104 102 103 105 104 * For 2D, a list of numpy arrays are expected: [qx_prime,qy_prime], 106 105 where 1D arrays, :: 107 106 108 107 qx_prime = [ qx[0], qx[1], qx[2], ....] 109 110 108 and :: 111 112 qy_prime = [ qy[0], qy[1], qy[2], ....] 113 109 qy_prime = [ qy[0], qy[1], qy[2], ....] 110 114 111 Then get :: 115 116 112 q = numpy.sqrt(qx_prime^2+qy_prime^2) 117 113 118 114 that is a qr in 1D array; :: 119 120 q = [q[0], q[1], q[2], ....] 121 115 q = [q[0], q[1], q[2], ....] 116 122 117 ..note:: 123 Due to 2D speed issue, no anisotropic scattering 118 Due to 2D speed issue, no anisotropic scattering 124 119 is supported for python models, thus C-models should have 125 120 their own evalDistribution methods. 126 121 127 122 The method is then called the following way: :: 128 123 129 124 evalDistribution(q) 130 131 125 where q is a numpy array. 132 126 133 127 :param qdist: ndarray of scalar q-values or list [qx,qy] where qx,qy are 1D ndarrays 134 128 """ … … 140 134 msg = "evalDistribution expects a list of 2 ndarrays" 141 135 raise RuntimeError, msg 142 136 143 137 # Extract qx and qy for code clarity 144 138 qx = qdist[0] 145 139 qy = qdist[1] 146 140 147 141 # calculate q_r component for 2D isotropic 148 142 q = numpy.sqrt(qx**2+qy**2) … … 153 147 154 148 return iq_array 155 149 156 150 elif qdist.__class__.__name__ == 'ndarray': 157 151 # We have a simple 1D distribution of q-values … … 159 153 iq_array = v_model(qdist) 160 154 return iq_array 161 155 162 156 else: 163 157 mesg = "evalDistribution is expecting an ndarray of scalar q-values" 164 158 mesg += " or a list [qx,qy] where qx,qy are 2D ndarrays." 165 159 raise RuntimeError, mesg 166 167 168 160 161 162 169 163 def clone(self): 170 164 """ Returns a new object identical to the current object """ 171 165 obj = copy.deepcopy(self) 172 166 return self._clone(obj) 173 167 174 168 def _clone(self, obj): 175 169 """ … … 182 176 obj._persistency_dict = copy.deepcopy( self._persistency_dict) 183 177 return obj 184 178 185 179 def set_dispersion(self, parameter, dispersion): 186 180 """ 187 181 model dispersions 188 """ 182 """ 189 183 ##Not Implemented 190 184 return None 191 185 192 186 def getProfile(self): 193 187 """ 194 Get SLD profile 195 188 Get SLD profile 189 196 190 : return: (z, beta) where z is a list of depth of the transition points 197 beta is a list of the corresponding SLD values 191 beta is a list of the corresponding SLD values 198 192 """ 199 193 #Not Implemented 200 194 return None, None 201 195 202 196 def setParam(self, name, value): 203 """ 197 """ 204 198 Set the value of a model parameter 205 199 206 200 :param name: name of the parameter 207 201 :param value: value of the parameter 208 202 209 203 """ 210 204 # Look for dispersion parameters … … 223 217 self.params[item] = value 224 218 return 225 219 226 220 raise ValueError, "Model does not contain parameter %s" % name 227 221 228 222 def getParam(self, name): 229 """ 223 """ 230 224 Set the value of a model parameter 231 232 225 :param name: name of the parameter 233 226 234 227 """ 235 228 # Look for dispersion parameters … … 246 239 if item.lower()==name.lower(): 247 240 return self.params[item] 248 241 249 242 raise ValueError, "Model does not contain parameter %s" % name 250 243 251 244 def getParamList(self): 252 """ 245 """ 253 246 Return a list of all available parameters for the model 254 """ 247 """ 255 248 list = self.params.keys() 256 249 # WARNING: Extending the list with the dispersion parameters 257 250 list.extend(self.getDispParamList()) 258 251 return list 259 252 260 253 def getDispParamList(self): 261 """ 254 """ 262 255 Return a list of all available parameters for the model 263 """ 256 """ 264 257 list = [] 265 258 266 259 for item in self.dispersion.keys(): 267 260 for p in self.dispersion[item].keys(): 268 261 if p not in ['type']: 269 262 list.append('%s.%s' % (item.lower(), p.lower())) 270 263 271 264 return list 272 265 273 266 # Old-style methods that are no longer used 274 def setParamWithToken(self, name, value, token, member): 267 def setParamWithToken(self, name, value, token, member): 275 268 """ 276 269 set Param With Token 277 270 """ 278 271 return NotImplemented 279 def getParamWithToken(self, name, token, member): 272 def getParamWithToken(self, name, token, member): 280 273 """ 281 274 get Param With Token 282 275 """ 283 276 return NotImplemented 284 285 def getParamListWithToken(self, token, member): 277 278 def getParamListWithToken(self, token, member): 286 279 """ 287 280 get Param List With Token 288 281 """ 289 282 return NotImplemented 290 def __add__(self, other): 283 def __add__(self, other): 291 284 """ 292 285 add 293 286 """ 294 287 raise ValueError, "Model operation are no longer supported" 295 def __sub__(self, other): 288 def __sub__(self, other): 296 289 """ 297 290 sub 298 291 """ 299 292 raise ValueError, "Model operation are no longer supported" 300 def __mul__(self, other): 293 def __mul__(self, other): 301 294 """ 302 295 mul 303 296 """ 304 297 raise ValueError, "Model operation are no longer supported" 305 def __div__(self, other): 298 def __div__(self, other): 306 299 """ 307 300 div 308 301 """ 309 302 raise ValueError, "Model operation are no longer supported" 310 -
src/sas/sascalc/calculator/c_extensions/libfunc.c
r79492222 r9e531f2 3 3 #include <math.h> 4 4 5 #include "lib multifunc/libfunc.h"5 #include "libfunc.h" 6 6 7 7 #include <stdio.h> -
src/sas/sascalc/calculator/c_extensions/librefl.c
ra6d2e3b r9e531f2 4 4 5 5 #include <math.h> 6 #include "lib multifunc/librefl.h"6 #include "librefl.h" 7 7 #include <stdio.h> 8 8 #include <stdlib.h> 9 9 #if defined(_MSC_VER) 10 #include " ../libigor/winFuncs.h"10 #include "winFuncs.h" 11 11 #endif 12 12 -
src/sas/sascalc/calculator/c_extensions/sld2i.cpp
r79492222 r9e531f2 7 7 using namespace std; 8 8 extern "C" { 9 #include "lib multifunc/libfunc.h"10 #include "lib multifunc/librefl.h"9 #include "libfunc.h" 10 #include "librefl.h" 11 11 } 12 12 /** -
src/sas/sascalc/calculator/sas_gen.py
r06aaa75d r9e531f2 3 3 SAS generic computation and sld file readers 4 4 """ 5 from sas. models.BaseComponent import BaseComponent6 import sas. models.sas_extension.sld2i as mod5 from sas.sascalc.calculator.BaseComponent import BaseComponent 6 import sas.sascalc.calculator.core.sld2i as mod 7 7 from periodictable import formula 8 8 from periodictable import nsf -
src/sas/sascalc/data_util/qsmearing.py
- Property mode changed from 100755 to 100644
r8cb0692 rf8aa738 19 19 def smear_selection(data, model = None): 20 20 """ 21 Creates the right type of smearer according 21 Creates the right type of smearer according 22 22 to the data. 23 24 23 The canSAS format has a rule that either 25 24 slit smearing data OR resolution smearing data 26 is available. 27 25 is available. 26 28 27 For the present purpose, we choose the one that 29 28 has none-zero data. If both slit and resolution 30 smearing arrays are filled with good data 29 smearing arrays are filled with good data 31 30 (which should not happen), then we choose the 32 resolution smearing data. 33 31 resolution smearing data. 32 34 33 :param data: Data1D object 35 34 :param model: sas.model instance … … 43 42 return None 44 43 return Pinhole2D(data) 45 44 46 45 if not hasattr(data, "dx") and not hasattr(data, "dxl")\ 47 46 and not hasattr(data, "dxw"): 48 47 return None 49 48 50 49 # Look for resolution smearing data 51 50 _found_resolution = False 52 51 if data.dx is not None and len(data.dx) == len(data.x): 53 52 54 53 # Check that we have non-zero data 55 54 if data.dx[0] > 0.0: … … 65 64 if data.dxl is not None and len(data.dxl) == len(data.x) \ 66 65 and data.dxw is not None and len(data.dxw) == len(data.x): 67 66 68 67 # Check that we have non-zero data 69 68 if data.dxl[0] > 0.0 or data.dxw[0] > 0.0: 70 69 _found_slit = True 71 70 72 71 # Sanity check: all data should be the same as a function of Q 73 72 for item in data.dxl: … … 75 74 _found_resolution = False 76 75 break 77 76 78 77 for item in data.dxw: 79 78 if data.dxw[0] != item: … … 98 97 """ 99 98 Apply the resolution function to the data. 100 101 99 Note that this is called with iq_in matching data.x, but with 102 100 iq_in[first_bin:last_bin] set to theory values for these bins, 103 101 and the remainder left undefined. The first_bin, last_bin values 104 102 should be those returned from get_bin_range. 105 106 103 The returned value is of the same length as iq_in, with the range 107 104 first_bin:last_bin set to the resolution smeared values. … … 123 120 """ 124 121 For a given q_min, q_max, find the corresponding indices in the data. 125 126 122 Returns first, last. 127 128 123 Note that these are indexes into q from the data, not the q_calc 129 124 needed by the resolution function. Note also that these are the -
src/sas/sascalc/fit/pluginmodel.py
r79492222 r9e531f2 1 2 from sas.models.BaseComponent import BaseComponent 1 from sas.sascalc.calculator.BaseComponent import BaseComponent 3 2 import math 4 3 5 4 class Model1DPlugin(BaseComponent): 6 5 ## Name of the model 7 6 8 7 def __init__(self , name="Plugin Model" ): 9 8 """ Initialization """ … … 13 12 self.params = {} 14 13 self.description = 'plugin model' 15 14 16 15 def function(self, x): 17 16 """ … … 19 18 """ 20 19 return x 21 20 22 21 def run(self, x = 0.0): 23 """ 22 """ 24 23 Evaluate the model 25 24 26 25 :param x: input x, or [x, phi] [radian] 27 26 28 27 :return: function value 29 28 30 29 """ 31 30 if x.__class__.__name__ == 'list': … … 37 36 else: 38 37 return self.function(x) 39 40 38 39 41 40 def runXY(self, x = 0.0): 42 """ 41 """ 43 42 Evaluate the model 44 45 :param x: input x, or [x, y] 46 43 44 :param x: input x, or [x, y] 45 47 46 :return: function value 48 47 49 48 """ 50 49 if x.__class__.__name__ == 'list': … … 54 53 else: 55 54 return self.function(x) 56 55 57 56 def set_details(self): 58 57 """ … … 61 60 if not self.params: 62 61 return {} 63 62 64 63 for key in self.params.keys(): 65 64 self.details[key] = ['', None, None] -
src/sas/sascalc/dataloader/readers/sesans_reader.py
r3689302 r1c0e3b0 142 142 default_p_unit = " " 143 143 lam_unit = lam_header[1].replace("[","").replace("]","") 144 for i,x_val in output.x:145 output.x[i], output.x_unit = self._unit_conversion(x_val, lam_unit, default_z_unit)146 for i,y_val in output.y:147 output.y[i], output.y_unit = self._unit_conversion(y_val, " ", default_p_unit)148 144 varheader=[zvals[0],dzvals[0],lamvals[0],dlamvals[0],Pvals[0],dPvals[0]] 149 145 valrange=range(1, len(zvals)) … … 163 159 input_f.close() 164 160 165 #Data166 output. x = x #[x != 0]167 output. y = y #[x != 0]161 output.x, output.x_unit = self._unit_conversion(x, lam_unit, default_z_unit) 162 output.y = y 163 output.dx, output.dx_unit = self._unit_conversion(dx, lam_unit, default_z_unit) 168 164 output.dy = dy 169 output.dx = dx 170 output.lam = lam 171 output.dlam = dlam 165 output.lam, output.lam_unit = self._unit_conversion(lam, lam_unit, default_z_unit) 166 output.dlam, output.dlam_unit = self._unit_conversion(dlam, lam_unit, default_z_unit) 172 167 173 168 output.xaxis("\rm{z}", output.x_unit) … … 182 177 if zaccept_unit.strip() == '\AA^-1': 183 178 zaccept_unit = "1/A" 184 output.sample.zacceptance=float(paramvals[7]) 185 output.sample.zacceptance=self._unit_conversion(output.sample.zacceptance, 186 zaccept_unit, "1/" + default_z_unit) 179 output.sample.zacceptance=(float(paramvals[7]),zaccept_unit) 187 180 output.vars=varheader 188 181
Note: See TracChangeset
for help on using the changeset viewer.