/usr/share/pyshared/PyMca/SimpleFitModule.py is in pymca 4.5.0-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 | #/*##########################################################################
# Copyright (C) 2004-2010 European Synchrotron Radiation Facility
#
# This file is part of the PyMCA X-ray Fluorescence Toolkit developed at
# the ESRF by the Beamline Instrumentation Software Support (BLISS) group.
#
# This toolkit is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# PyMCA is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# PyMCA; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307, USA.
#
# PyMCA follows the dual licensing model of Trolltech's Qt and Riverbank's PyQt
# and cannot be used as a free plugin for a non-free program.
#
# Please contact the ESRF industrial unit (industry@esrf.fr) if this license
# is a problem for you.
#############################################################################*/
import numpy
import sys
import os
import types
import Gefit
import SpecfitFuns
import copy
DEBUG = 0
class SimpleFit(object):
def __init__(self):
#no data available by default
self._x0 = None
self._y0 = None
#get default configuration
self.getDefaultConfiguration()
#the list and dictionary of defined functions
self._functionList = []
self._functionDict = {}
#the current fit function
self._stripFunction = None
def getDefaultConfiguration(self):
self._fitConfiguration = {}
self._fitConfiguration['fit'] = {}
self._fitConfiguration['fit']['fit_function'] = "None"
self._fitConfiguration['fit']['function_flag'] = 1
self._fitConfiguration['fit']['background_function'] = "None"
self._fitConfiguration['fit']['background_flag'] = 1
self._fitConfiguration['fit']['strip_function'] = "Strip"
self._fitConfiguration['fit']['stripalgorithm'] = 0
self._fitConfiguration['fit']['strip_flag'] = 1
self._fitConfiguration['fit']['fit_algorithm'] = "Levenberg-Marquardt"
self._fitConfiguration['fit']['weight'] = "NO Weight"
self._fitConfiguration['fit']['maximum_fit_iterations'] = 10
self._fitConfiguration['fit']['background_estimation_policy'] = "Estimate always"
self._fitConfiguration['fit']['function_estimation_policy'] = "Estimate always"
self._fitConfiguration['fit']['minimum_delta_chi'] = 0.0010
self._fitConfiguration['fit']['use_limits'] = 0
self._fitConfiguration['fit']['xmin'] = 0.
self._fitConfiguration['fit']['xmax'] = 1000.
self._fitConfiguration['fit']['functions'] = []
#strip/snip background configuration related
self._fitConfiguration['fit']['stripanchorsflag'] = 0
self._fitConfiguration['fit']['stripanchorslist'] = []
self._fitConfiguration['fit']['stripfilterwidth'] = 1
self._fitConfiguration['fit']['snipwidth'] = 10
self._fitConfiguration['fit']['stripwidth'] = 4
self._fitConfiguration['fit']['stripiterations'] = 5000
self._fitConfiguration['fit']['stripconstant'] = 1.0
self._fitConfiguration['functions'] = {}
def configure(self, ddict=None):
if ddict is None:
return self.getConfiguration()
else:
return self.setConfiguration(ddict)
def setConfiguration(self, ddict, try_import=False):
oldConfig = self.getConfiguration()
if ddict is None:
return oldConfig
if 'fit' in ddict:
givenKeys = ddict['fit'].keys()
for key in self._fitConfiguration['fit'].keys():
if key in givenKeys:
self._fitConfiguration['fit'][key] = ddict['fit'][key]
for key in ddict.keys():
if key in ['fit', 'functions']:
continue
self._fitConfiguration[key] = ddict[key]
if 'functions' in ddict:
functionNames = ddict['functions'].keys()
for fName in functionNames:
if fName not in self._fitConfiguration['functions'].keys():
if try_import:
ffile = ddict['functions'][fName].get('file', None)
if ffile is not None:
self.importFunctions(ffile)
else:
print("WARNING:Function %s not among defined functions" % fName)
continue
self._fitConfiguration['functions'][fName]['configuration']=\
ddict['functions'][fName]['configuration']
configureMethod = self._fitConfiguration['functions'][fName]\
['configure']
if configureMethod is not None:
configureMethod(ddict['functions'][fName]['configuration'])
#if data are present, update strip background
if (self._x0 is None) or (self._y0 is None):
return
if (oldConfig['fit']['xmin'] != self._fitConfiguration['fit']['xmin']) or\
(oldConfig['fit']['xmax'] != self._fitConfiguration['fit']['xmax']):
if DEBUG:
print("SETTING DATA AGAIN")
self.setData(self._x0, self._y0,
xmin=self._fitConfiguration['fit']['xmin'],
xmax=self._fitConfiguration['fit']['xmax'])
return
for key in ['strip_flag', 'stripanchorsflag', 'stripalgorithm',
'stripwidth', 'stripiterations', 'stripconstant']:
if oldConfig['fit'][key] != self._fitConfiguration['fit'][key]:
if DEBUG:
print("RECALCULATING STRIP")
self._getStripBackground()
break
if key == 'stripanchorsflag':
if len(oldConfig['fit']['stripanchorslist']) !=\
len(self._fitConfiguration['fit']['stripanchorslist']):
if DEBUG:
print("ANCHORS CHANGE, RECALCULATING STRIP")
self._getStripBackground()
break
def getConfiguration(self):
ddict = {}
for key in self._fitConfiguration.keys():
if key == 'functions':
continue
ddict[key] = copy.deepcopy(self._fitConfiguration[key])
ddict['functions'] = {}
for key in self._fitConfiguration['functions'].keys():
ddict['functions'][key] = {}
ddict['functions'][key]['configuration'] = copy.deepcopy(\
self._fitConfiguration['functions'][key]['configuration'])
configureMethod = self._fitConfiguration['functions']\
[key]['configure']
if configureMethod is not None:
currentFunctionConfig = configureMethod()
for newKey in currentFunctionConfig.keys():
if newKey not in ['estimation']:
ddict['functions'][key]['configuration'][newKey] = currentFunctionConfig[newKey]
parameters = self._fitConfiguration['functions'][key]['parameters']
ddict['functions'][key]['parameters'] = parameters
widget = self._fitConfiguration['functions'][key]['widget']
ddict['functions'][key]['widget'] = widget
fname = self._fitConfiguration['functions'][key]['file']
ddict['functions'][key]['file'] = fname
return ddict
def setData(self, x, y, sigma=None, xmin=None, xmax=None, **kw):
idx = numpy.argsort(x)
if sigma is not None:
self._sigma = sigma[idx]
else:
self._sigma = None
self._y0 = y[idx]
self._x0 = x[idx]
if sigma is not None:
self._sigma0 = sigma[idx]
xmin, xmax = self._getLimits(self._x0, xmin, xmax)
idx = (self._x0 >= xmin) & (self._x0 <= xmax)
self._x = self._x0[idx]
self._y = self._y0[idx]
self._fitConfiguration['fit']['xmin'] = xmin * 1.0
self._fitConfiguration['fit']['xmax'] = xmax * 1.0
if sigma is not None:
self._sigma = self._sigma0[idx]
if DEBUG:
print("TODO: Make sure we have something to fit")
#get strip/SNIP background
self._z = self._getStripBackground()
def importFunctions(self, modname):
#modname can be a module or a file
if type(modname) == types.ModuleType:
newfun = modname
elif os.path.exists(modname):
sys.path.append(os.path.dirname(modname))
f=os.path.basename(os.path.splitext(modname)[0])
newfun=__import__(f)
else:
raise ValueError("Cannot interprete/find %s" % modname)
theory = newfun.THEORY
function=newfun.FUNCTION
parameters = newfun.PARAMETERS
try:
estimate=newfun.ESTIMATE
except:
estimate=None
try:
derivative=newfun.DERIVATIVE
except:
derivative=None
try:
configure=newfun.CONFIGURE
except:
configure=None
try:
widget=newfun.WIDGET
except:
widget=None
basename = os.path.basename(newfun.__file__)
for i in range(len(theory)):
ddict = {}
functionName = theory[i]
ddict['function'] = function[i]
ddict['parameters'] = parameters[i]
ddict['default_parameters'] = None
ddict['estimate'] = None
ddict['derivative'] = None
ddict['configure'] = None
ddict['widget'] = None
ddict['file'] = newfun.__file__
ddict['configuration'] = {}
if estimate is not None:
ddict['estimate'] = estimate[i]
if derivative is not None:
ddict['derivative'] = derivative[i]
if configure is not None:
ddict['configure'] = configure[i]
if ddict['configure'] is not None:
ddict['configuration'] = configure[i]()
if ddict['estimate'] is None:
ddict['configuration']['estimation'] = None
if widget is not None:
ddict['widget'] = widget[i]
self._fitConfiguration['functions'][functionName] = ddict
self._fitConfiguration['fit']['functions'].append(functionName)
def setFitFunction(self, name):
if name in [None, "None", "NONE"]:
self._fitConfiguration['fit']['fit_function'] = "None"
return
self._fitFunctionConfigured = False
if name not in self._fitConfiguration['fit']['functions']:
txt = "Function %s not among defined functions" % name
raise KeyError(txt)
self._fitConfiguration['fit']['fit_function'] = name
def getFitFunction(self):
return "%s" % self._fitConfiguration['fit']['fit_function']
def setBackgroundFunction(self, name):
if name in [None, "None", "NONE"]:
self._fitConfiguration['fit']['background_function'] = "None"
return
self._backgroundFunctionConfigured = False
if name not in self._fitConfiguration['fit']['functions']:
txt = "Function %s not among defined functions" % name
raise KeyError(txt)
self._fitConfiguration['fit']['background_function'] = name
def getBackgroundFunction(self):
return "%s" % self._fitConfiguration['fit']['background_function']
def _getLimits(self, x, xmin, xmax):
if self._fitConfiguration['fit']['use_limits']:
xmin = self._fitConfiguration['fit']['xmin']
xmax = self._fitConfiguration['fit']['xmax']
return xmin, xmax
if xmin is None:
xmin = x[0]
if xmax is None:
xmax = x[-1]
return xmin, xmax
def _getStripBackground(self, x=None, y=None):
#this makes the assumption x are equally spaced
#and I should build a spline if that is not the case
#but I do not want to put a dependency on SciPy
if y is not None:
ywork = y
else:
ywork = self._y
if x is not None:
xwork = x
else:
xwork = self._x
n=len(xwork)
#loop for anchors
anchorslist = []
if self._fitConfiguration['fit']['stripanchorsflag']:
if self._fitConfiguration['fit']['stripanchorslist'] is not None:
oldShape = xwork.shape
ravelled = xwork
ravelled.shape = -1
for channel in self._fitConfiguration['fit']['stripanchorslist']:
if channel <= ravelled[0]:continue
index = numpy.nonzero(ravelled >= channel)
if len(index):
index = min(index)
if index > 0:
anchorslist.append(index)
ravelled.shape = oldShape
#work with smoothed data
ysmooth = self._getSmooth(xwork, ywork)
#SNIP algorithm
if self._fitConfiguration['fit']['stripalgorithm'] in ["SNIP", 1]:
if DEBUG:
print("CALCULATING SNIP")
if len(anchorslist) == 0:
anchorslist = [0, len(ysmooth)-1]
anchorslist.sort()
result = 0.0 * ysmooth
lastAnchor = 0
width = self._fitConfiguration['fit']['snipwidth']
for anchor in anchorslist:
if (anchor > lastAnchor) and (anchor < len(ysmooth)):
result[lastAnchor:anchor] =\
SpecfitFuns.snip1d(ysmooth[lastAnchor:anchor], width, 0)
lastAnchor = anchor
if lastAnchor < len(ysmooth):
result[lastAnchor:] =\
SpecfitFuns.snip1d(ysmooth[lastAnchor:], width, 0)
return result
#strip background
niter = self._fitConfiguration['fit']['stripiterations']
if niter > 0:
if DEBUG:
print("CALCULATING STRIP")
print("iterations = ", niter)
print("constant = ",
self._fitConfiguration['fit']['stripconstant'])
print("width = ",
self._fitConfiguration['fit']['stripwidth'])
print("anchors = ", anchorslist)
result=SpecfitFuns.subac(ysmooth,
self._fitConfiguration['fit']['stripconstant'],
niter,
self._fitConfiguration['fit']['stripwidth'],
anchorslist)
if niter > 1000:
#make sure to get something smooth
result = SpecfitFuns.subac(result,
self._fitConfiguration['fit']['stripconstant'],
500,1,
anchorslist)
else:
#make sure to get something smooth but with less than
#500 iterations
result = SpecfitFuns.subac(result,
self._fitConfiguration['fit']['stripconstant'],
int(self._fitConfiguration['fit']['stripwidth']*2),
1,
anchorslist)
else:
if DEBUG:
print("NO STRIP, NO SNIP")
result = numpy.zeros(ysmooth.shape, numpy.float) + min(ysmooth)
return result
def _getSmooth(self, x, y):
f=[0.25,0.5,0.25]
try:
if hasattr(y, "shape"):
if len(y.shape) > 1:
result=SpecfitFuns.SavitskyGolay(numpy.ravel(y).astype(numpy.float),
self._fitConfiguration['fit']['stripfilterwidth'])
else:
result=SpecfitFuns.SavitskyGolay(numpy.array(y).astype(numpy.float),
self._fitConfiguration['fit']['stripfilterwidth'])
else:
result=SpecfitFuns.SavitskyGolay(Numeric.array(y).astype(Numeric.Float),
self._fitConfiguration['fit']['stripfilterwidth'])
except:
err = sys.exc_info()[1]
raise ValueError("Unsuccessful Savitsky-Golay smoothing: %s" % err)
result=numpy.array(y).astype(numpy.float)
if len(result) > 1:
result[1:-1]=numpy.convolve(result,f,mode=0)
result[0]=0.5*(result[0]+result[1])
result[-1]=0.5*(result[-1]+result[-2])
return result
def fit(self):
if self._y0 is None:
self._setStatus("No data to be fitted")
return
self.estimate()
self.startFit()
return self.getResult()
def estimate(self):
self._fitResult = None
if self._y0 is None:
self._setStatus("No data to be fitted")
return
self._setStatus("Estimate started")
backgroundDict = {'parameters':[]}
fitFunctionDict = {'parameters':[]}
backgroundParameters, backgroundConstraints = [], [[],[],[]]
backgroundFunction = self.getBackgroundFunction()
if self._fitConfiguration['fit']['background_flag']:
if backgroundFunction not in [None, "None", "NONE"]:
backgroundParameters, backgroundConstraints =\
self.estimateBackground()
backgroundDict = self._fitConfiguration['functions']\
[backgroundFunction]
self._setStatus("Background estimation finished")
functionParameters, functionConstraints = [], [[],[],[]]
fitFunction = self._fitConfiguration['fit']['fit_function']
if self._fitConfiguration['fit']['function_flag']:
if fitFunction not in [None, "None", "NONE"]:
functionParameters, functionConstraints=\
self.estimateFunction()
fitFunctionDict = self._fitConfiguration['functions']\
[fitFunction]
if DEBUG:
print("ESTIMATION parameters = ",functionParameters)
print("ESTIMATION constraints = ",functionConstraints)
self._setStatus("Fit function estimation finished")
#estimations are made
#Check if there can be conflicts between parameter names
#because they can have same names in the background and
#in the fit function
conflict = False
for parname in backgroundDict['parameters']:
if parname in fitFunctionDict['parameters']:
conflict = True
break
#build the parameter names
self.final_theory=[]
nBasePar = len(backgroundDict['parameters'])
nActualPar = len(backgroundParameters)
self.__nBackgroundParameters = nActualPar
if nActualPar:
for i in range(nActualPar):
parname = backgroundDict['parameters'][i%nBasePar]
if conflict:
parname = "Bkg_"+parname
if nBasePar < nActualPar:
parname = parname + ("_%d" % (1+int(i/nBasePar)))
self.final_theory.append(parname)
nBasePar = len(fitFunctionDict['parameters'])
nActualPar = len(functionParameters)
if nActualPar:
for i in range(nActualPar):
parname = fitFunctionDict['parameters'][i%nBasePar]
if nBasePar < nActualPar:
parname = parname + ("_%d" % (1+int(i/nBasePar)))
self.final_theory.append(parname)
CONS=['FREE',
'POSITIVE',
'QUOTED',
'FIXED',
'FACTOR',
'DELTA',
'SUM',
'IGNORE']
self.paramlist=[]
param = self.final_theory
j=0
i=0
k=0
xmin=self._x.min()
xmax=self._x.max()
#print "xmin = ",xmin,"xmax = ",xmax
for pname in self.final_theory:
if i < len(backgroundParameters):
self.paramlist.append({'name':pname,
'estimation':backgroundParameters[i],
'group':0,
'code':CONS[int(backgroundConstraints[0][i])],
'cons1':backgroundConstraints[1][i],
'cons2':backgroundConstraints[2][i],
'fitresult':0.0,
'sigma':0.0,
'xmin':xmin,
'xmax':xmax})
i=i+1
else:
if (j % len(fitFunctionDict['parameters'])) == 0:
k=k+1
if (CONS[int(functionConstraints[0][j])] == "FACTOR") or \
(CONS[int(functionConstraints[0][j])] == "DELTA"):
functionConstraints[1][j] = functionConstraints[1][j] +\
len(backgroundParameters)
self.paramlist.append({'name':pname,
'estimation':functionParameters[j],
'group':k,
'code':CONS[int(functionConstraints[0][j])],
'cons1':functionConstraints[1][j],
'cons2':functionConstraints[2][j],
'fitresult':0.0,
'sigma':0.0,
'xmin':xmin,
'xmax':xmax})
j=j+1
self._setStatus("Estimate finished")
return self.paramlist
def _setStatus(self, status):
self.__status = status
def getStatus(self):
return self.__status
def estimateBackground(self):
fname = self.getBackgroundFunction()
if fname is None:
return [],[[],[],[]]
ddict = self._fitConfiguration['functions'][fname]
estimateFunction = ddict['estimate']
if estimateFunction is None:
parameters = []
constraints = [[],[],[]]
if ddict['configuration']['estimation'] is not None:
estimation = ddict['configuration']['estimation']
defaultPar = estimation['parameters']
for parameter in defaultPar:
parameters.append(estimation[parameter]['estimation'])
constraints[0].append(estimation[parameter]['code'])
constraints[1].append(estimation[parameter]['cons1'])
constraints[2].append(estimation[parameter]['cons2'])
else:
defaultPar = ddict['parameters']
for parameter in defaultPar:
parameters.append(0.0)
constraints[0].append(0)
constraints[1].append(0)
constraints[2].append(0)
return parameters, constraints
parameters, constraints = estimateFunction(self._x, self._y, self._z)
return parameters, constraints
def estimateFunction(self):
self._z = self._getStripBackground()
fname = self.getFitFunction()
if fname is None:
return [],[[],[],[]]
ddict = self._fitConfiguration['functions'][fname]
estimateFunction = ddict['estimate']
if estimateFunction is None:
parameters = []
constraints = [[],[],[]]
if ddict['configuration']['estimation'] is not None:
estimation = ddict['configuration']['estimation']
defaultPar = estimation['parameters']
for parameter in defaultPar:
parameters.append(estimation[parameter]['estimation'])
constraints[0].append(estimation[parameter]['code'])
constraints[1].append(estimation[parameter]['cons1'])
constraints[2].append(estimation[parameter]['cons2'])
else:
defaultPar = ddict['parameters']
for parameter in defaultPar:
parameters.append(0.0)
constraints[0].append(0)
constraints[1].append(0)
constraints[2].append(0)
return parameters, constraints
parameters, constraints = estimateFunction(self._x * 1,
self._y * 1,
self._z * 1)
return parameters, constraints
def startFit(self):
if self._y0 is None:
self._setStatus("No data to be fitted")
return
self._setStatus("Fit started")
param_list = self.final_theory
length = len(param_list)
param_val = []
param_constrains = [[],[],[]]
flagconstrained=0
for param in self.paramlist:
#print param['name'],param['group'],param['estimation']
param_val.append(param['estimation'])
if (param['code'] != 'FREE') & (param['code'] != 0) & \
(param['code'] != 0.0) :
flagconstrained=1
param_constrains [0].append(param['code'])
param_constrains [1].append(param['cons1'])
param_constrains [2].append(param['cons2'])
#weight handling
if self._fitConfiguration['fit']['weight'] in ["NO Weight", 0]:
weightflag = 0
else:
weightflag = 1
if DEBUG:
print("STILL TO HANDLE DERIVATIVES")
model_deriv = self.modelFunctionDerivative
if self._fitConfiguration['fit']['strip_flag']:
y = self._y - self._z
else:
y = self._y
self._fitResult = None
if not flagconstrained:
param_constrains = []
if DEBUG:
result = Gefit.LeastSquaresFit(self.modelFunction,param_val,
xdata=self._x,
ydata=y,
sigmadata=self._sigma,
constrains=param_constrains,
weightflag=weightflag,
model_deriv=model_deriv,
fulloutput=True)
else:
try:
result = Gefit.LeastSquaresFit(self.modelFunction,param_val,
xdata=self._x,
ydata=y,
sigmadata=self._sigma,
constrains=param_constrains,
weightflag=weightflag,
model_deriv=model_deriv,
fulloutput=True)
except:
text = sys.exc_info()[1]
if type(text) is not type(" "):
text = text.args
if len(text):
text = text[0]
else:
text = ''
self._setStatus('Fit error : %s' %text)
raise
self._fitResult = {}
self._fitResult['fit_function'] = self.getFitFunction()
self._fitResult['background_function'] = self.getBackgroundFunction()
self._fitResult['fittedvalues'] = result[0]
self._fitResult['chisq'] = result[1]
self._fitResult['sigma_values'] = result[2]
self._fitResult['niter'] = result[3]
self._fitResult['lastdeltachi'] = result[4]
self._fitResult['n_background_parameters'] = self.__nBackgroundParameters
if DEBUG:
print("Found parameters = ", self._fitResult['fittedvalues'])
i=0
self._fitResult['parameters'] = []
for param in self.paramlist:
if param['code'] != 'IGNORE':
self._fitResult['parameters'].append(param['name'])
param['fitresult'] = result[0][i]
param['sigma']= result[2][i]
i = i + 1
self._setStatus("Fit finished")
return result
def modelFunction(self, pars, t):
result = 0.0 * t
nb = self.__nBackgroundParameters
if nb:
result += self._fitConfiguration['functions'][self.getBackgroundFunction()]\
['function'](pars[0:nb], t)
if len(self.paramlist) > nb:
result += self._fitConfiguration['functions'][self.getFitFunction()]\
['function'](pars[nb:], t)
return result
def numericDerivative(self, f, parameters, index, x):
"""
numericDerivative(self, f, parameters, index, x)
calculates the numeric derivate of f(parameters, x) respect
to the parameter indexed by the given index
"""
#numeric derivative
x=numpy.array(x)
delta = (parameters[index] + numpy.equal(parameters[index],0.0)) * 0.00001
#make a copy of the parameters
newpar = parameters * 1
newpar[index] = parameters[index] + delta
f1 = f(newpar, x)
newpar[index] = parameters[index] - delta
f2 = f(newpar, x)
return (f1-f2) / (2.0 * delta)
def modelFunctionDerivative(self, pars, index, x):
return self.numericDerivative(self.modelFunction, pars, index, x)
def getResult(self, configuration=False):
#print " get results to be implemented"
ddict = {}
ddict['result'] = self._fitResult
if configuration:
ddict['configuration'] = self.getConfiguration()
return ddict
def _evaluateBackground(self, x=None):
if x is None:
x = self._x
pars = self._fitResult['fittedvalues']
nb = self.__nBackgroundParameters
if nb:
y = self._fitConfiguration['functions'][self.getBackgroundFunction()]\
['function'](pars[:nb], x)
else:
y = numpy.zeros(x.shape, numpy.float)
if self._fitConfiguration['fit']['strip_flag']:
#If the x is not self._x, how to add the strip?
try:
y += self._z
except:
print("Cannot add strip background")
return y
def _evaluateFunction(self, x=None):
if x is None:
x = self._x
pars = self._fitResult['fittedvalues']
nb = self.__nBackgroundParameters
if len(self.paramlist) > nb:
return self._fitConfiguration['functions'][self.getFitFunction()]\
['function'](pars[nb:], x)
else:
return numpy.zeros(x.shape, numpy.float)
def evaluateDefinedFunction(self, x=None):
if x is None:
x = self._x
y = self._evaluateBackground(x)
y += self._evaluateFunction(x)
return y
def test():
import SpecfitFunctions
a=SpecfitFunctions.SpecfitFunctions()
x = numpy.arange(1000).astype(numpy.float)
p1 = numpy.array([1500,100.,50.0])
p2 = numpy.array([1500,700.,50.0])
y = a.gauss(p1, x)+1
y = y + a.gauss(p2,x)
fit = SimpleFit()
fit.importFunctions(SpecfitFunctions)
fit.setFitFunction('Gaussians')
#fit.setBackgroundFunction('Gaussians')
#fit.setBackgroundFunction('Constant')
fit.setData(x, y)
fit.fit()
print("Expected parameters 1500,100.,50.0, 1500,700.,50.0")
import PyMcaQt as qt
import Parameters
a = qt.QApplication(sys.argv)
qt.QObject.connect(a,qt.SIGNAL("lastWindowClosed()"),a,qt.SLOT("quit()"))
w =Parameters.Parameters()
w.fillfromfit(fit.paramlist)
w.show()
a.exec_()
if __name__=="__main__":
DEBUG = 1
test()
|