def Xml2Fit_v1(self, fitElement, calibration=None): """ Creates a fit object from information found in a xml file """ # <fit> success = True peakModel = fitElement.get("peakModel") bgElement = fitElement.find("background") # Simple fix for older xml file versions, where the only background # model was a polynomial, and therefore it did not have to be stored try: backgroundModel = bgElement.get("backgroundModel") except AttributeError: backgroundModel = "polynomial" if backgroundModel is None: backgroundModel = "polynomial" fitter = Fitter(peakModel, backgroundModel) fit = Fit(fitter, cal=calibration) try: fit.chi = float(fitElement.get("chi")) except ValueError: fit.chi = None # <bgMarker> for bgElement in fitElement.findall("bgMarker"): # Read begin marker beginElement = bgElement.find("begin") begin = self._getPosFromElement(beginElement, fit) fit.ChangeMarker("bg", begin, "set") # Read end marker endElement = bgElement.find("end") end = self._getPosFromElement(endElement, fit) fit.ChangeMarker("bg", end, "set") # <regionMarker> for regionElement in fitElement.findall("regionMarker"): # Read begin marker beginElement = regionElement.find("begin") begin = self._getPosFromElement(beginElement, fit) fit.ChangeMarker("region", begin, "set") # Read end marker endElement = regionElement.find("end") end = self._getPosFromElement(endElement, fit) fit.ChangeMarker("region", end, "set") # <peakMarker> for peakElement in fitElement.findall("peakMarker"): # Read position marker posElement = peakElement.find("position") pos = self._getPosFromElement(posElement, fit) fit.ChangeMarker("peak", pos, "set") # <background> bgElement = fitElement.find("background") if bgElement: try: fit.bgChi = float(bgElement.get("chisquare")) except ValueError: pass params = list() # Distinguish between old notation of background parameters (coeff, ncoeff), # which interprets the parameters as coefficients of a polynomial, and the # new notation (params, npar), which interprets them as arbitrary parameters. paramCounterName = 'npar' paramElements = bgElement.findall('param') if not paramElements: paramElements = bgElement.findall('coeff') paramCounterName = 'deg' for paramElement in paramElements: npar = int(paramElement.get(paramCounterName)) # <value> valueElement = paramElement.find("value") value = float(valueElement.text) # <error> errorElement = paramElement.find("error") error = float(errorElement.text) param = ufloat(value, error) params.append([npar, param]) params.sort() fit.bgParams = [p[1] for p in params] # <peak> statusdict = dict() for peakElement in fitElement.findall("peak"): # <uncal> uncalElement = peakElement.find("uncal") parameter = dict() for paramElement in uncalElement: # parameter value/error name = paramElement.tag parameter[name] = self._readParamElement(paramElement) # parameter status status = paramElement.get("status", "free") try: statusdict[name].append(status) except KeyError: statusdict[name] = [status] # <extras> extraElement = peakElement.find("extras") extras = dict() if extraElement is not None: for paramElement in extraElement: name = paramElement.tag if len(paramElement) == 1: extras[name] = paramElement.text else: # <value> valueElement = paramElement.find("value") value = float(valueElement.text) # <error> errorElement = paramElement.find("error") error = float(errorElement.text) extras[name] = ufloat(value, error) # create peak try: peak = fit.fitter.peakModel.Peak(cal=calibration, **parameter) except TypeError: hdtv.ui.error("Error reading peak with parameters: %s" % str(parameter)) success = False continue peak.extras = extras fit.peaks.append(peak) # set parameter status of fitter for name in list(statusdict.keys()): # check if status is the same for all peaks check = set(statusdict[name]) if len(check) == 1: status = statusdict[name][0] else: status = statusdict[name] fitter.SetParameter(name, status) integrals = dict() for integral in fitElement.findall('integral'): integral_type = integral.get("integraltype") integrals[integral_type] = dict() for calElement in integral: cal_type = calElement.tag integrals[integral_type][cal_type] = dict() for paramElement in calElement: # <value> valueElement = paramElement.find("value") value = float(valueElement.text) # <error> errorElement = paramElement.find("error") error = float(errorElement.text) coeff = ufloat(value, error) integrals[integral_type][cal_type][ paramElement.tag] = coeff if not integrals: integrals = None for integral_type in ['sub', 'bg']: if integrals and not integral_type in integrals: integrals[integral_type] = None fit.integral = integrals return (fit, success)
def RestoreFromXml_v0(self, root, do_fit=False): """ Reads fits from xml file (version = 0.*) Note: For performance reasons this does not reconstruct the fit results. It only sets the markers and restores the status of the fitter. The user must repeat the fit, if he/she wants to see the results again. (This should be improved in later versions.) """ count = 0 spectra = self.spectra # <spectrum> for specElement in root.iter(): name = specElement.get("name") # find this spectrum from Element in the real world spec = None for sid in spectra.ids: if spectra.dict[sid].name == name: spec = spectra.dict[sid] # maybe the spectrum that is referred to in XML is currently not # loaded if spec is None: continue # <fit> for fitElement in specElement: count = count + 1 peakModel = fitElement.get("peakModel") # Simple fix for older xml file versions, where the only background # model was a polynomial, and therefore it did not have to be stored fitter = Fitter(peakModel, "polynomial") # <result> params = dict() for resultElement in fitElement.findall("result"): for paramElement in resultElement: parname = paramElement.tag status = paramElement.get("status") try: params[parname].append(status) except KeyError: params[parname] = [status] for parname in list(params.keys()): status = params[parname] fitter.SetParameter(parname, status) fit = Fit(fitter, cal=spec.cal) # <background> for bgElement in fitElement.findall("background"): # Read begin/p1 marker beginElement = bgElement.find("begin") if beginElement is None: # Maybe old Element (ver 0.1) beginElement = bgElement.find("p1") begin = float(beginElement.find("uncal").text) fit.ChangeMarker("bg", fit.cal.Ch2E(begin), "set") # Read end/p2 marker endElement = bgElement.find("end") if endElement is None: # Maybe old Element (ver 0.1) endElement = bgElement.find("p2") end = float(endElement.find("uncal").text) fit.ChangeMarker("bg", fit.cal.Ch2E(end), "set") # <region> for regionElement in fitElement.findall("region"): # Read begin/p1 marker beginElement = regionElement.find("begin") if beginElement is None: # Maybe old Element (ver 0.1) beginElement = regionElement.find("p1") begin = float(beginElement.find("uncal").text) fit.ChangeMarker("region", fit.cal.Ch2E(begin), "set") # Read end/p2 marker endElement = regionElement.find("end") if endElement is None: # Maybe old Element (ver 0.1) endElement = regionElement.find("p2") end = float(endElement.find("uncal").text) fit.ChangeMarker("region", fit.cal.Ch2E(end), "set") # <peak> for peakElement in fitElement.findall("peak"): # Read position/p1 marker posElement = peakElement.find("position") if posElement is None: posElement = peakElement.find("p1") pos = float(posElement.find("uncal").text) fit.ChangeMarker("peak", fit.cal.Ch2E(pos), "set") if do_fit: fit.FitPeakFunc(spec) spec.Insert(fit) return count, [fit]
def Xml2Fit_v1(self, fitElement, calibration=None): """ Creates a fit object from information found in a xml file """ # <fit> success = True peakModel = fitElement.get("peakModel") bgdeg = int(fitElement.get("bgDegree")) fitter = Fitter(peakModel, bgdeg) fit = Fit(fitter, cal=calibration) try: fit.chi = float(fitElement.get("chi")) except ValueError: fit.chi = None # <bgMarker> for bgElement in fitElement.findall("bgMarker"): # Read begin marker beginElement = bgElement.find("begin") begin = self._getPosFromElement(beginElement, fit) fit.ChangeMarker("bg", begin, "set") # Read end marker endElement = bgElement.find("end") end = self._getPosFromElement(endElement, fit) fit.ChangeMarker("bg", end, "set") # <regionMarker> for regionElement in fitElement.findall("regionMarker"): # Read begin marker beginElement = regionElement.find("begin") begin = self._getPosFromElement(beginElement, fit) fit.ChangeMarker("region", begin, "set") # Read end marker endElement = regionElement.find("end") end = self._getPosFromElement(endElement, fit) fit.ChangeMarker("region", end, "set") # <peakMarker> for peakElement in fitElement.findall("peakMarker"): # Read position marker posElement = peakElement.find("position") pos = self._getPosFromElement(posElement, fit) fit.ChangeMarker("peak", pos, "set") # <background> bgElement = fitElement.find("background") if bgElement: try: fit.bgChi = float(bgElement.get("chisquare")) except ValueError: pass coeffs = list() for coeffElement in bgElement.findall("coeff"): deg = int(coeffElement.get("deg")) # <value> valueElement = coeffElement.find("value") value = float(valueElement.text) # <error> errorElement = coeffElement.find("error") error = float(errorElement.text) coeff = ufloat(value, error) coeffs.append([deg, coeff]) coeffs.sort() fit.bgCoeffs = [c[1] for c in coeffs] # <peak> statusdict = dict() for peakElement in fitElement.findall("peak"): # <uncal> uncalElement = peakElement.find("uncal") parameter = dict() for paramElement in uncalElement: # parameter value/error name = paramElement.tag parameter[name] = self._readParamElement(paramElement) # parameter status status = paramElement.get("status", "free") try: statusdict[name].append(status) except KeyError: statusdict[name] = [status] # <extras> extraElement = peakElement.find("extras") extras = dict() if extraElement is not None: for paramElement in extraElement: name = paramElement.tag if len(paramElement) == 1: extras[name] = paramElement.text else: # <value> valueElement = paramElement.find("value") value = float(valueElement.text) # <error> errorElement = paramElement.find("error") error = float(errorElement.text) extras[name] = ufloat(value, error) # create peak try: peak = fit.fitter.peakModel.Peak(cal=calibration, **parameter) except TypeError: hdtv.ui.error( "Error reading peak with parameters: %s" % str(parameter)) success = False continue peak.extras = extras fit.peaks.append(peak) # set parameter status of fitter for name in list(statusdict.keys()): # check if status is the same for all peaks check = set(statusdict[name]) if len(check) == 1: status = statusdict[name][0] else: status = statusdict[name] fitter.SetParameter(name, status) integrals = dict() for integral in fitElement.findall('integral'): integral_type = integral.get("integraltype") integrals[integral_type] = dict() for calElement in integral: cal_type = calElement.tag integrals[integral_type][cal_type] = dict() for paramElement in calElement: # <value> valueElement = paramElement.find("value") value = float(valueElement.text) # <error> errorElement = paramElement.find("error") error = float(errorElement.text) coeff = ufloat(value, error) integrals[integral_type][cal_type][paramElement.tag] = coeff if not integrals: integrals = None for integral_type in ['sub', 'bg']: if integrals and not integral_type in integrals: integrals[integral_type] = None fit.integral = integrals return (fit, success)