Changeset 4e9f227 in sasview
- Timestamp:
- May 15, 2014 7:07:43 PM (11 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:
- 786685e
- Parents:
- e3efa6b3
- Files:
-
- 1 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sans/fit/BumpsFitting.py
re3efa6b3 r4e9f227 2 2 BumpsFitting module runs the bumps optimizer. 3 3 """ 4 import time5 6 4 import numpy 7 5 … … 11 9 from bumps.fitproblem import FitProblem 12 10 13 from sans.fit.AbstractFitEngine import FitEngine 14 from sans.fit.AbstractFitEngine import FResult 11 from .AbstractFitEngine import FitEngine 12 from .AbstractFitEngine import FResult 13 from .expression import compile_constraints 15 14 16 15 class BumpsMonitor(object): … … 52 51 53 52 53 # Note: currently using bumps parameters for each parameter object so that 54 # a SasFitness can be used directly in bumps with the usual semantics. 55 # The disadvantage of this technique is that we need to copy every parameter 56 # back into the model each time the function is evaluated. We could instead 57 # define reference parameters for each sans parameter, but then we would not 58 # be able to express constraints using python expressions in the usual way 59 # from bumps, and would instead need to use string expressions. 54 60 class SasFitness(object): 55 61 """ 56 62 Wrap SAS model as a bumps fitness object 57 63 """ 58 def __init__(self, name, model, data, fitted=[], **kw):59 self.name = name60 self.model = model 64 def __init__(self, model, data, fitted=[], constraints={}, **kw): 65 self.name = model.name 66 self.model = model.model 61 67 self.data = data 62 68 self._define_pars() 63 69 self._init_pars(kw) 70 self.constraints = dict(constraints) 64 71 self.set_fitted(fitted) 65 72 self._dirty = True … … 73 80 self._pars[k] = parameter.Parameter(value=value, bounds=bounds, 74 81 fixed=True, name=name) 82 #print parameter.summarize(self._pars.values()) 75 83 76 84 def _init_pars(self, kw): … … 102 110 """ 103 111 for k,p in self._pars.items(): 104 p.fixed = (k not in param_list )112 p.fixed = (k not in param_list or k in self.constraints) 105 113 self.fitted_pars = [self._pars[k] for k in param_list] 106 114 self.fitted_par_names = param_list … … 112 120 def update(self): 113 121 for k,v in self._pars.items(): 122 #print "updating",k,v,v.value 114 123 self.model.setParam(k,v.value) 115 124 self._dirty = True … … 154 163 ftol=1.49012e-8, reset_flag=False): 155 164 # Build collection of bumps fitness calculators 156 models = [ SasFitness(name="M%d"%(i+1), 157 model=M.get_model().model, 165 models = [ SasFitness(model=M.get_model(), 158 166 data=M.get_data(), 167 constraints=M.constraints, 159 168 fitted=M.pars) 160 169 for i,M in enumerate(self.fit_arrange_dict.values()) 161 170 if M.get_to_fit() == 1 ] 162 171 problem = FitProblem(models) 172 173 174 # Build constraint expressions 175 exprs = {} 176 for M in models: 177 exprs.update((".".join((M.name,k)),v) for k,v in M.constraints.items()) 178 if exprs: 179 symtab = dict((".".join((M.name,k)),p) 180 for M in models 181 for k,p in M.parameters().items()) 182 constraints = compile_constraints(symtab,exprs) 183 else: 184 constraints = lambda: 0 185 186 # Override model update so that parameter constraints are applied 187 problem._model_update = problem.model_update 188 def model_update(): 189 constraints() 190 problem._model_update() 191 problem.model_update = model_update 163 192 164 193 # Run the fit -
src/sans/perspectives/fitting/fitpage.py
rd44648e r4e9f227 661 661 wx.EXPAND | wx.ADJUST_MINSIZE, 0) 662 662 663 if self.engine_type == "park":663 if self.engine_type in ("park","bumps"): 664 664 self.text_disp_max.Show(True) 665 665 self.text_disp_min.Show(True) … … 738 738 wx.EXPAND | wx.ADJUST_MINSIZE, 0) 739 739 740 if self.engine_type == "park":740 if self.engine_type in ("park","bumps"): 741 741 ctl3.Show(True) 742 742 ctl4.Show(True) … … 1005 1005 return 1006 1006 1007 if len(self._manager.fit_thread_list) > 0 and\1008 self._manager._fit_engine != "park" and\1009 self._manager.sim_page != None and \1010 self._manager.sim_page.uid == self.uid:1007 if (len(self._manager.fit_thread_list) > 0 1008 and self._manager._fit_engine not in ("park","bumps") 1009 and self._manager.sim_page != None 1010 and self._manager.sim_page.uid == self.uid): 1011 1011 msg = "The FitEnging will be set to 'ParkMC'\n" 1012 1012 msg += " to fit with more than one data set..." … … 2108 2108 if chisqr != None and numpy.isfinite(chisqr): 2109 2109 #format chi2 2110 if self.engine_type == "park":2110 if self.engine_type in ("park","bumps"): 2111 2111 npt_fit = float(self.get_npts2fit()) 2112 2112 chi2 = format_number(chisqr, True) -
src/sans/perspectives/fitting/fitting.py
rf121904 r4e9f227 999 999 #simulatanous fit only one engine need to be created 1000 1000 ## if simultaneous fit change automatically the engine to park 1001 self._on_change_engine(engine='park') 1001 if self._fit_engine not in ("park","bumps"): 1002 self._on_change_engine(engine='park') 1002 1003 sim_fitter = Fit(self._fit_engine) 1003 1004 sim_fitter.fitter_id = self.sim_page.uid … … 1007 1008 1008 1009 self.fitproblem_count = fitproblem_count 1009 if self._fit_engine == "park":1010 if self._fit_engine in ("park","bumps"): 1010 1011 engineType = "Simultaneous Fit" 1011 1012 else: -
src/sans/perspectives/fitting/simfitpage.py
rfb7180c r4e9f227 152 152 ## making sure all parameters content a constraint 153 153 ## validity of the constraint expression is own by fit engine 154 if self.parent._manager._fit_engine != "park"and flag:154 if self.parent._manager._fit_engine not in ("park","bumps") and flag: 155 155 msg = "The FitEnging will be set to 'Park' fit engine\n" 156 156 msg += " for the simultaneous fit..." -
test/park_integration/test/utest_fit_cylinder.py
re3efa6b3 r4e9f227 10 10 from sans.dataloader.loader import Loader 11 11 12 #@unittest.skip("") 12 13 class TestSingleFit(unittest.TestCase): 13 14 """ test single fitting """ … … 33 34 fitter.set_model(self.model,1,self.pars1) 34 35 fitter.select_problem_for_fit(id=1,value=1) 35 return fitter.fit() 36 36 result1, = fitter.fit() 37 37 38 def test_scipy(self):39 """ Simple cylinder model fit (scipy) """40 41 result1, = self._fit("scipy")42 43 38 self.assert_(result1) 44 39 self.assertTrue(len(result1.pvec)>0 or len(result1.pvec)==0 ) 45 40 self.assertTrue(len(result1.stderr)> 0 or len(result1.stderr)==0) 46 41 47 42 self.assertTrue( math.fabs(result1.pvec[0]-400.0)/3.0 < result1.stderr[0] ) 48 43 self.assertTrue( math.fabs(result1.pvec[1]-20.0)/3.0 < result1.stderr[1] ) 49 44 self.assertTrue( math.fabs(result1.pvec[2]-1.0)/3.0 < result1.stderr[2] ) 50 45 self.assertTrue( result1.fitness < 1.0 ) 51 52 46 47 48 def test_scipy(self): 49 """ Simple cylinder model fit (scipy) """ 50 self._fit("scipy") 51 52 53 53 def test_park(self): 54 54 """ Simple cylinder model fit (park) """ 55 #raise NotImplementedError() 56 result1, = self._fit("park") 57 58 self.assert_(result1) 59 self.assertTrue(len(result1.pvec)>0 or len(result1.pvec)==0 ) 60 self.assertTrue(len(result1.stderr)> 0 or len(result1.stderr)==0) 61 62 self.assertTrue( math.fabs(result1.pvec[0]-400.0)/3.0 < result1.stderr[0] ) 63 self.assertTrue( math.fabs(result1.pvec[1]-20.0)/3.0 < result1.stderr[1] ) 64 self.assertTrue( math.fabs(result1.pvec[2]-1.0)/3.0 < result1.stderr[2] ) 65 self.assertTrue( result1.fitness < 1.0 ) 66 67 55 self._fit("park") 56 57 def test_bumps(self): 58 """ Simple cylinder model fit (park) """ 59 self._fit("bumps") 60 61 68 62 69 63 class TestSimultaneousFit(unittest.TestCase): … … 82 76 self.model1.set(scale= 1.0) 83 77 self.model1.set(radius=18) 84 self.model1.set(length= 396)78 self.model1.set(length=200) 85 79 self.model1.set(sldCyl=3e-006, sldSolv=0.0) 86 80 self.model1.set(background=0.0) 87 81 88 82 cyl2 = CylinderModel() 89 83 cyl2.name = "C2" … … 91 85 self.model2.set(scale= 1.0) 92 86 self.model2.set(radius=37) 93 self.model2.set(length= 'C1.length')87 self.model2.set(length=300) 94 88 self.model2.set(sldCyl=3e-006, sldSolv=0.0) 95 89 self.model2.set(background=0.0) 96 97 90 98 def test_park2(self): 91 92 def test_constrained_bumps(self): 93 """ Simultaneous cylinder model fit (park) """ 94 self._run_fit(Fit('bumps')) 95 96 #@unittest.skip("") 97 def test_constrained_park(self): 99 98 """ Simultaneous cylinder model fit (park) """ 100 99 self._run_fit(Fit('park')) … … 107 106 108 107 for n, v, dv in zip(result1.param_list, result1.pvec, result1.stderr): 109 print " M1.%s = %s +/- %s"%(n,v,dv)108 print "%s M1.%s = %s +/- %s"%(fitter._engine.__class__.__name__,n,v,dv) 110 109 if n == "length": 111 110 self.assertTrue( math.fabs(v-400.0)/3.0 < dv ) … … 115 114 self.assertTrue( math.fabs(v-1.0)/3.0 < dv ) 116 115 for n, v, dv in zip(result2.param_list, result2.pvec, result2.stderr): 117 print " M2.%s = %s +/- %s"%(n,v,dv)116 print "%s M2.%s = %s +/- %s"%(fitter._engine.__class__.__name__,n,v,dv) 118 117 if n=='radius': 119 118 self.assertTrue( math.fabs(v-40.0)/3.0 < dv ) … … 127 126 128 127 fitter.set_data(self.data2,2) 129 fitter.set_model(self.model2, 2, ['radius','scale']) 128 fitter.set_model(self.model2, 2, ['radius','scale'], 129 constraints=[("length","C1.length")]) 130 130 fitter.select_problem_for_fit(id=1,value=1) 131 131 fitter.select_problem_for_fit(id=2,value=1)
Note: See TracChangeset
for help on using the changeset viewer.