Changeset 8809e48 in sasview for sansmodels/src/sans/models
- Timestamp:
- Oct 15, 2008 1:01:06 PM (16 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:
- 00353b4
- Parents:
- ae60f86
- Location:
- sansmodels/src/sans/models
- Files:
-
- 3 added
- 13 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
sansmodels/src/sans/models/BaseComponent.py
rae60f86 r8809e48 38 38 def clone(self): 39 39 """ Returns a new object identical to the current object """ 40 41 40 obj = copy.deepcopy(self) 41 return self._clone(obj) 42 43 def _clone(self, obj): 44 """ 45 Internal utility function to copy the internal 46 data members to a fresh copy. 47 """ 42 48 obj.params = copy.deepcopy(self.params) 43 49 obj.details = copy.deepcopy(self.details) 44 50 obj.dispersion = copy.deepcopy(self.dispersion) 45 46 51 return obj 47 52 … … 70 75 71 76 raise ValueError, "Model does not contain parameter %s" % name 72 73 74 def _setParam(self, name, value):75 """76 Set the value of a model parameter77 78 @param name: name of the parameter79 @param value: value of the parameter80 """81 # Look for dispersion parameters82 toks = name.split('.')83 if len(toks)==2 and toks[0] in self.dispersion:84 # Setting a dispersion model parameter85 if toks[1] in self.dispersion[toks[0]]:86 self.dispersion[toks[0]][toks[1]] = value87 return88 else:89 raise ValueError, "Model does not contain parameter %s.%s" % (toks[0], toks[1])90 91 if name in self.params.keys():92 self.params[name] = value93 else:94 raise ValueError, "Model does not contain parameter %s" % name95 96 def _getParam(self, name):97 """98 Set the value of a model parameter99 100 @param name: name of the parameter101 """102 # Look for dispersion parameters103 toks = name.split('.')104 if len(toks)==2 and toks[0] in self.dispersion:105 # Setting a dispersion model parameter106 if toks[1] in self.dispersion[toks[0]]:107 return self.dispersion[toks[0]][toks[1]]108 else:109 raise ValueError, "Model does not contain parameter %s.%s" % (toks[0], toks[1])110 111 if name in self.params.keys():112 return self.params[name]113 else:114 raise ValueError, "Model does not contain parameter %s" % name115 77 116 78 def getParam(self, name): -
sansmodels/src/sans/models/BaseModel.py
rae60f86 r8809e48 39 39 self.value = value 40 40 41 def __str__(self): 42 return "%s: %g" % (self.name, self.value) 41 43 42 44 class ParameterProperty(object): … … 68 70 ## Dictionary of Parameter objects 69 71 self.parameters = {} 70 71 for item in self.params:72 self.parameters[item] = Parameter(item, self.params[item])73 74 75 72 76 73 # Evaluation methods to be implemented by the models … … 106 103 if name in self.parameters: 107 104 print "found" 105 #self.parameters[name].value = value 108 106 return object.__setattr__(self, name, value) 109 107 -
sansmodels/src/sans/models/CoreShellModel.py
r0f5bc9f r8809e48 65 65 def clone(self): 66 66 """ Return a identical copy of self """ 67 obj = CoreShellModel() 68 obj.params = copy.deepcopy(self.params) 69 return obj 67 return self._clone(CoreShellModel()) 70 68 71 69 def run(self, x = 0.0): -
sansmodels/src/sans/models/CylinderModel.py
raf03ddd r8809e48 65 65 def clone(self): 66 66 """ Return a identical copy of self """ 67 obj = CylinderModel() 68 obj.params = copy.deepcopy(self.params) 69 return obj 67 return self._clone(CylinderModel()) 70 68 71 69 def run(self, x = 0.0): -
sansmodels/src/sans/models/EllipsoidModel.py
r0f5bc9f r8809e48 65 65 def clone(self): 66 66 """ Return a identical copy of self """ 67 obj = EllipsoidModel() 68 obj.params = copy.deepcopy(self.params) 69 return obj 67 return self._clone(EllipsoidModel()) 70 68 71 69 def run(self, x = 0.0): -
sansmodels/src/sans/models/EllipticalCylinderModel.py
r0f5bc9f r8809e48 69 69 def clone(self): 70 70 """ Return a identical copy of self """ 71 obj = EllipticalCylinderModel() 72 obj.params = copy.deepcopy(self.params) 73 return obj 71 return self._clone(EllipticalCylinderModel()) 74 72 75 73 def run(self, x = 0.0): -
sansmodels/src/sans/models/ModelAdaptor.py
rae60f86 r8809e48 53 53 def __init__(self): 54 54 """ Initialization""" 55 ## Dictionary of Parameter objects 56 self.parameters = {} 55 57 ## Dictionary of parameters, available for backward compatibility 56 58 self.params = ParameterDict(self.parameters) -
sansmodels/src/sans/models/NewCylinderModel.py
rae60f86 r8809e48 22 22 """ 23 23 24 from sans.models.BaseModel import BaseModel 24 from sans.models.BaseModel import BaseModel, Parameter, ParameterProperty 25 25 from sans_extension.c_models import CCylinderModel 26 26 import copy … … 41 41 42 42 """ 43 43 scale = ParameterProperty('scale') 44 radius = ParameterProperty('radius') 45 length = ParameterProperty('length') 46 contrast = ParameterProperty('contrast') 47 background = ParameterProperty('background') 48 cyl_theta = ParameterProperty('cyl_theta') 49 cyl_phi = ParameterProperty('cyl_phi') 50 44 51 def __init__(self): 45 52 """ Initialization """ … … 62 69 self.details['cyl_phi'] = ['rad', None, None] 63 70 64 71 # The C models have a self.params dictionary 72 for item in self.params: 73 self.parameters[item] = Parameter(item, self.params[item]) 74 65 75 def clone(self): 66 76 """ Return a identical copy of self """ -
sansmodels/src/sans/models/SphereModel.py
r0f5bc9f r8809e48 59 59 def clone(self): 60 60 """ Return a identical copy of self """ 61 obj = SphereModel() 62 obj.params = copy.deepcopy(self.params) 63 return obj 61 return self._clone(SphereModel()) 64 62 65 63 def run(self, x = 0.0): -
sansmodels/src/sans/models/c_models/modelTemplate.txt
raf03ddd r8809e48 48 48 def clone(self): 49 49 """ Return a identical copy of self """ 50 obj = [PYTHONCLASS]() 51 obj.params = copy.deepcopy(self.params) 52 return obj 50 return self._clone([PYTHONCLASS]()) 53 51 54 52 def run(self, x = 0.0): -
sansmodels/src/sans/models/test/utest_dispersity.py
rae60f86 r8809e48 47 47 self.assertAlmostEqual(self.model.run(0.001), 4723.32213339, 3) 48 48 self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 4743.56, 2) 49 50 def test_clone(self): 51 from sans.models.dispersion_models import GaussianDispersion 52 disp = GaussianDispersion() 53 self.model.set_dispersion('radius', disp) 54 self.model.dispersion['radius']['width'] = 5.0 55 self.model.dispersion['radius']['npts'] = 100 56 self.model.setParam('scale', 10.0) 57 58 new_model = self.model.clone() 59 self.assertAlmostEqual(new_model.run(0.001), 4723.32213339, 3) 60 self.assertAlmostEqual(new_model.runXY([0.001,0.001]), 4743.56, 2) 49 61 50 62 def test_gaussian_zero(self): -
sansmodels/src/sans/models/test/utest_newstylemodels.py
rae60f86 r8809e48 22 22 return self.params['scale']*x 23 23 24 from sans.models.BaseModel import BaseModel, ParameterProperty24 from sans.models.BaseModel import BaseModel, Parameter, ParameterProperty 25 25 class NewTestModel(BaseModel): 26 26 scale = ParameterProperty('scale') … … 31 31 self.parameters['scale'] = Parameter('scale', 4.0) 32 32 33 def run(self, x): 33 34 def runXY(self, x): 34 35 return self.scale*x 35 36 36 37 class TestBogusModel(unittest.TestCase): 38 def setUp(self): 39 self.model = NewTestModel() 40 41 def test_call(self): 42 self.assertEqual(self.model(1), 4.0) 43 self.model.scale = 1.0 44 self.assertEqual(self.model(1), 1.0) 45 self.model.setParam('scale',2.0) 46 self.assertEqual(self.model(1), 2.0) 47 self.assertEqual(self.model.getParam('scale'), 2.0) 48 37 49 class TestBaseModel(unittest.TestCase): 38 50 """ … … 66 78 67 79 68 print self.model.parameters69 80 70 81 print self.model(0.001) 82 print self.model.parameters['length'] 71 83 self.model.setParam("length", 250.0) 84 self.assertEquals(self.model.getParam("length"), 250.0) 85 86 print self.model.parameters['length'] 72 87 print self.model(0.001) 73 88 -
sansmodels/src/sans/models/test/utest_nonshape.py
ra55fac1 r8809e48 130 130 # An exceptio is raised 131 131 self.assertRaises(ZeroDivisionError, self.model.run, 0.0) 132 133 def test1D_clone(self): 134 value = self._func(50.0, 1.0, 10.0, 2.0) 135 self.model.setParam('background', 10.0) 136 clone = self.model.clone() 137 self.assertEqual(clone.run(2.0), value) 138 self.assertEqual(clone.runXY(2.0), value) 139 140 # User enter zero as a value of x 141 # An exceptio is raised 142 self.assertRaises(ZeroDivisionError, clone.run, 0.0) 132 143 133 144 def test2D(self):
Note: See TracChangeset
for help on using the changeset viewer.