def test(self): '''Testing code goes here''' from diffpy.pdfgui.control.parameter import Parameter self.window.parameters = {3:Parameter(3), 17:Parameter(17), 11:Parameter(11)} self.window.parameters[3].setInitial(1) self.window.parameters[17].setInitial(0.55) self.window.parameters[11].setInitial(5.532) self.window.refresh()
def setUp(self): self.app = wx.App() self.frame = wx.Frame(None) self.panel = ParametersPanel(self.frame, -1) self.panel.parameters.update([ (1, Parameter(1, 0.1)), (5, Parameter(5, 0.5)), ]) self.panel.refresh() self.panel.mainFrame = self._mockUpMainFrame() self.frame.window = self.panel return
def test___init__(self): "check Parameter.__init__" p = Parameter(3, 2.2) self.assertEqual(3, p.idx) self.assertIsNone(p.refined) self.assertEqual(2.2, p.initialValue()) p100 = Parameter(100, self.fitting) self.assertAlmostEqual(0.7957747, p100.initialValue(), 6) p102 = Parameter(102, "=fit-d300:102") self.assertAlmostEqual(1.1811493, p102.initialValue(), 6) self.assertRaises(ControlTypeError, Parameter, 1, None) return
def test_refine(self): """check PdfFitSandbox.refine() """ fNi_stru = datafile('Ni.stru') fNi_data = datafile("Ni_2-8.chi.gr") sandbox = self.box.sandbox() sandbox.update({"fNi_stru": fNi_stru, "fNi_data": fNi_data}) exec "read_data(fNi_data, X, 40.1, 0.05)" in sandbox exec "read_struct(fNi_stru)" in sandbox exec "refine()" in sandbox self.assertEqual(2, len(self.box._fits)) self.assertEqual(1, len(self.box._fits[0].strucs)) self.assertEqual(1, len(self.box._fits[0].datasets)) self.assertEqual(1, len(self.box._fits[1].strucs)) self.assertEqual(1, len(self.box._fits[1].datasets)) # check fit linking: self.box._fits[-1].parameters[1] = Parameter(1, 0.05) exec "refine(0.01)" in sandbox self.assertEqual(3, len(self.box._fits)) names = [f.name for f in self.box._fits] self.assertEqual('0', names[0]) self.assertEqual('1', names[1]) self.assertEqual('2', names[2]) self.assertEqual(0, self.box._curdataset) self.assertEqual(0, self.box._curphase) s = self.box._fits[-1].parameters[1].initialStr() self.assertEqual("=1:1", s) return
def setpar(self, idx, val): """Set value of constrained parameter. idx -- parameter index val -- Either a numerical value or a reference to variable Raises: KeyError when parameter is yet to be constrained """ # people do not use parenthesis, e.g., "setpar(3, qdamp)" # in such case val is a reference to PdfFit method curfit = self._fits[-1] if callable(val): val = val() # here val can be either number or variable string if type(val) in (types.IntType, types.FloatType): value = float(val) # it is string of either dataset variable elif val in PdfFitSandbox._dataset_vars: curdataset = curfit.datasets[self._curdataset] value = curdataset.getvar(val) # or phase variable otherwise else: curphase = curfit.strucs[self._curphase] value = curphase.getvar(val) # here we can set the parameter curfit.parameters[idx] = Parameter(idx, initial=value) return
def test_initialValue(self): "check Parameter.initialValue" p1 = Parameter(1, 0.25) self.assertEqual(0.25, p1.initialValue()) self.assertEqual("0.25", p1.initialStr()) px = Parameter(7, "=undefined") self.assertRaises(ControlKeyError, px.initialValue) self.assertEqual("=undefined:7", px.initialStr()) return
def findParameters(self): """Obtain dictionary of parameters used by self.constraints. The keys of returned dictionary are integer parameter indices, and their values Parameter instances, with guessed initial values. returns dictionary of indices and Parameter instances """ foundpars = {} for var, con in self.constraints.iteritems(): con.guess(self.getvar(var)) for pidx, pguess in con.parguess.iteritems(): # skip if already found if pidx in foundpars: continue # insert to foundpars otherwise if pguess is not None: foundpars[pidx] = Parameter(pidx, initial=pguess) else: foundpars[pidx] = Parameter(pidx, initial=0.0) return foundpars
def test_fixpar(self): """check PdfFitSandbox.fixpar() """ sandbox = self.box.sandbox() curfit = self.box._fits[-1] for i in range(1, 4): curfit.parameters[i] = Parameter(i, 1.0 * i) exec 'fixpar(1)' in sandbox self.assertEqual(True, curfit.parameters[1].fixed) self.assertEqual(False, curfit.parameters[2].fixed) self.assertEqual(False, curfit.parameters[3].fixed) exec 'fixpar(ALL)' in sandbox self.assertEqual(True, curfit.parameters[1].fixed) self.assertEqual(True, curfit.parameters[2].fixed) self.assertEqual(True, curfit.parameters[3].fixed) self.assertRaises(ControlKeyError, self.box.fixpar, 77) return