def load(self, z, subpath): """load data from a zipped project file z -- zipped project file subpath -- path to its own storage within project file returns a tree of internal hierachy """ # subpath = projName/myName/ from diffpy.pdfgui.utils import unquote_plain subs = subpath.split('/') rootDict = z.fileTree[subs[0]][subs[1]] if rootDict.has_key('structure'): for strucName in rootDict['structure'].keys(): struc = FitStructure(unquote_plain(strucName)) struc.load(z, subpath + 'structure/' + strucName + '/') self.add(struc) if rootDict.has_key('dataset'): for datasetName in rootDict['dataset'].keys(): dataset = FitDataSet(unquote_plain(datasetName)) dataset.load(z, subpath + 'dataset/' + datasetName + '/') self.add(dataset) if rootDict.has_key('calculation'): for calcName in rootDict['calculation'].keys(): calc = Calculation(unquote_plain(calcName)) calc.load(z, subpath + 'calculation/' + calcName + '/') self.add(calc) self.__forward_spdiameter() return self.organization()
def loadDataset(self, targetID, filename, name=None, position=None): """load Dataset from a file to a Fitting targetID -- reference to Fitting name -- name of the new Dataset, default is file basename position -- where the dataset is to be inserted, default is last return: Dataset reference """ self.__validateType(targetID) if name is None: name = os.path.basename(filename) #insert to target dataset = FitDataSet(name) dataset.readObs(filename) targetID.add(dataset, position) return dataset
def test__resampledPDFDataSet(self): """check FitDataSet._resampledPDFDataSet() """ fNi_data = datafile("Ni_2-8.chi.gr") fds = FitDataSet('Ni') fds.read(fNi_data) npts = len(fds.rcalc) rds = fds._resampledPDFDataSet() self.assertEqual(npts, len(rds.robs)) self.assertEqual(npts, len(rds.drobs)) self.assertEqual(npts, len(rds.Gobs)) self.assertEqual(npts, len(rds.dGobs)) # reduce fitrmax to one half fds.fitrmax = fds.rmax / 2.0 npts1 = len(fds.rcalc) self.failUnless(npts1 < npts) rds1 = fds._resampledPDFDataSet() self.assertEqual(npts1, len(rds1.robs)) self.assertEqual(npts1, len(rds1.drobs)) self.assertEqual(npts1, len(rds1.Gobs)) self.assertEqual(npts1, len(rds1.dGobs)) return
def read_data_string(self, s, stype, qmax, qdamp, name=""): """Read pdf data from a string into memory. s -- string containing the contents of the data file stype -- 'X' (xray) or 'N' (neutron) qmax -- maximum q value qdamp -- instrumental q-resolution factor name -- tag with which to label data """ curfit = self._fits[-1] fd = FitDataSet(name) fd.readStr(s) fd.stype = stype fd.qmax = qmax fd.qdamp = qdamp curfit.add(fd) self._curdataset = len(curfit.datasets) - 1 return
def read_data(self, filename, stype, qmax, qdamp): """Read pdf data from file into memory. filename -- name of file from which to read data stype -- 'X' (xray) or 'N' (neutron) qmax -- maximum q value qdamp -- instrumental q-resolution factor Raises: IOError when the file cannot be read from disk """ curfit = self._fits[-1] name, ext = os.path.splitext(os.path.basename(filename)) fd = FitDataSet(name) fd.read(filename) fd.stype = stype fd.qmax = qmax fd.qdamp = qdamp curfit.add(fd) self._curdataset = len(curfit.datasets) - 1 return
def makeDopingSeries(control, fit, base, dopant, paths, doping): """Make a temperature series. control -- pdguicontrol instance fit -- The template fit base -- Name of the base element dopant -- Name of the dopant element paths -- list of path names of new datasets doping -- list of doping values corresponding to the datasets returns a list of the new fit organization objects """ from diffpy.pdffit2 import is_element # Make sure that base and dopant are elements base = base.title() dopant = dopant.title() if not is_element(base): raise ControlValueError("'%s' is not an element!"%base) if not is_element(dopant): raise ControlValueError("'%s' is not an element!"%dopant) # Make sure that base and dopant are in the structure file(s) hasBase = False hasDopant = False for S in fit.strucs: for atom in S: if atom.element == base: hasBase = True if atom.element == dopant: hasDopant = True if hasBase and hasDopant: break if not hasBase: message = "The template structure does not contain the base atom." raise ControlValueError(message) if not hasDopant: message = "The template structure does not contain the dopant atom." raise ControlValueError(message) # Make sure we're only replacing a single dataset if len(fit.datasets) != 1: message = "Can't apply macro to fits with multiple datasets." raise ControlValueError(message) fits = [] # holds all of the other information about the dataset fitbasename = fit.name fitnewname = fit.name fitlastname = fit.name dataset = fit.datasets[0] for i in range(len(paths)): filename = paths[i] fitlastname = fitnewname fitcopy = control.copy(fit) # Get rid of the old dataset temp = fitcopy.datasets[0] fitcopy.remove(temp) # Configure the new dataset dsname = os.path.basename(filename) newdataset = FitDataSet(dsname) newdataset.readObs(filename) newdataset.qdamp = dataset.qdamp newdataset.qbroad = dataset.qbroad newdataset.dscale = dataset.dscale newdataset.fitrmin = dataset.fitrmin newdataset.fitrmax = dataset.fitrmax rstep = dataset.fitrstep st = dataset.getFitSamplingType() newdataset.setFitSamplingType(st, rstep) temperature = dataset.metadata.get("temperature") if temperature is None: temperature = 300.0 newdataset.metadata["temperature"] = temperature newdataset.constraints = copy.deepcopy(dataset.constraints) # Set the chosen temperature newdataset.metadata["doping"] = doping[i] # Add the dataset to the fitcopy fitcopy.add(newdataset, None) # Update the doping information in the structures for S in fitcopy.strucs: for A in S: if A.element == dopant: A.occupancy = doping[i] if A.element == base: A.occupancy = 1-doping[i] # Set the parameters to the previous fit's name, if one exists. if fitlastname: parval = "=%s" % fitlastname for par in fitcopy.parameters.values(): par.setInitial(parval) # Now paste the copy into the control. fitnewname = "%s-%1.4f" % (fitbasename, doping[i]) o = control.paste(fitcopy, new_name = fitnewname) fits.append(o) return [f.organization() for f in fits]
def makeTemperatureSeries(control, fit, paths, temperatures): """Make a temperature series. control -- pdguicontrol instance fit -- The template fit paths -- list of path names of new datasets temperatures -- list of temperatures corresponding to the datasets returns a list of the new fit organization objects """ if len(fit.datasets) != 1: message = "Can't apply macro to fits with multiple datasets." raise ControlValueError(message) fits = [] # holds all of the other information about the dataset fitbasename = fit.name fitnewname = fit.name fitlastname = fit.name dataset = fit.datasets[0] for i in range(len(paths)): filename = paths[i] fitlastname = fitnewname fitcopy = control.copy(fit) # Get rid of the old dataset temp = fitcopy.datasets[0] fitcopy.remove(temp) # Configure the new dataset dsname = os.path.basename(filename) newdataset = FitDataSet(dsname) newdataset.readObs(filename) newdataset.qdamp = dataset.qdamp newdataset.qbroad = dataset.qbroad newdataset.dscale = dataset.dscale newdataset.fitrmin = dataset.fitrmin newdataset.fitrmax = dataset.fitrmax rstep = dataset.fitrstep st = dataset.getFitSamplingType() newdataset.setFitSamplingType(st, rstep) doping = dataset.metadata.get("doping") if doping is None: doping = 0.0 newdataset.metadata["doping"] = doping newdataset.constraints = copy.deepcopy(dataset.constraints) # Set the chosen temperature newdataset.metadata["temperature"] = temperatures[i] # Add the dataset to the fitcopy fitcopy.add(newdataset, None) # Set the parameters to the previous fit's name, if one exists. if fitlastname: parval = "=%s" % fitlastname for par in fitcopy.parameters.values(): par.setInitial(parval) # Now paste the copy into the control. fitnewname = "%s-T%i=%g" % (fitbasename, i + 1, temperatures[i]) o = control.paste(fitcopy, new_name = fitnewname) fits.append(o) return [f.organization() for f in fits]
def makeDopingSeries(control, fit, base, dopant, paths, doping): """Make a temperature series. control -- pdguicontrol instance fit -- The template fit base -- Name of the base element dopant -- Name of the dopant element paths -- list of path names of new datasets doping -- list of doping values corresponding to the datasets returns a list of the new fit organization objects """ from diffpy.pdffit2 import is_element # Make sure that base and dopant are elements base = base.title() dopant = dopant.title() if not is_element(base): raise ControlValueError("'%s' is not an element!" % base) if not is_element(dopant): raise ControlValueError("'%s' is not an element!" % dopant) # Make sure that base and dopant are in the structure file(s) hasBase = False hasDopant = False for S in fit.strucs: for atom in S: if atom.element == base: hasBase = True if atom.element == dopant: hasDopant = True if hasBase and hasDopant: break if not hasBase: message = "The template structure does not contain the base atom." raise ControlValueError(message) if not hasDopant: message = "The template structure does not contain the dopant atom." raise ControlValueError(message) # Make sure we're only replacing a single dataset if len(fit.datasets) != 1: message = "Can't apply macro to fits with multiple datasets." raise ControlValueError(message) fits = [] # holds all of the other information about the dataset fitbasename = fit.name fitnewname = fit.name fitlastname = fit.name dataset = fit.datasets[0] for i in range(len(paths)): filename = paths[i] fitlastname = fitnewname fitcopy = control.copy(fit) # Get rid of the old dataset temp = fitcopy.datasets[0] fitcopy.remove(temp) # Configure the new dataset dsname = os.path.basename(filename) newdataset = FitDataSet(dsname) newdataset.readObs(filename) newdataset.qdamp = dataset.qdamp newdataset.qbroad = dataset.qbroad newdataset.dscale = dataset.dscale newdataset.fitrmin = dataset.fitrmin newdataset.fitrmax = dataset.fitrmax rstep = dataset.fitrstep st = dataset.getFitSamplingType() newdataset.setFitSamplingType(st, rstep) temperature = dataset.metadata.get("temperature") if temperature is None: temperature = 300.0 newdataset.metadata["temperature"] = temperature newdataset.constraints = copy.deepcopy(dataset.constraints) # Set the chosen temperature newdataset.metadata["doping"] = doping[i] # Add the dataset to the fitcopy fitcopy.add(newdataset, None) # Update the doping information in the structures for S in fitcopy.strucs: for A in S: if A.element == dopant: A.occupancy = doping[i] if A.element == base: A.occupancy = 1 - doping[i] # Set the parameters to the previous fit's name, if one exists. if fitlastname: parval = "=%s" % fitlastname for par in fitcopy.parameters.values(): par.setInitial(parval) # Now paste the copy into the control. fitnewname = "%s-%1.4f" % (fitbasename, doping[i]) o = control.paste(fitcopy, new_name=fitnewname) fits.append(o) return [f.organization() for f in fits]
def makeTemperatureSeries(control, fit, paths, temperatures): """Make a temperature series. control -- pdguicontrol instance fit -- The template fit paths -- list of path names of new datasets temperatures -- list of temperatures corresponding to the datasets returns a list of the new fit organization objects """ if len(fit.datasets) != 1: message = "Can't apply macro to fits with multiple datasets." raise ControlValueError(message) fits = [] # holds all of the other information about the dataset fitbasename = fit.name fitnewname = fit.name fitlastname = fit.name dataset = fit.datasets[0] for i in range(len(paths)): filename = paths[i] fitlastname = fitnewname fitcopy = control.copy(fit) # Get rid of the old dataset temp = fitcopy.datasets[0] fitcopy.remove(temp) # Configure the new dataset dsname = os.path.basename(filename) newdataset = FitDataSet(dsname) newdataset.readObs(filename) newdataset.qdamp = dataset.qdamp newdataset.qbroad = dataset.qbroad newdataset.dscale = dataset.dscale newdataset.fitrmin = dataset.fitrmin newdataset.fitrmax = dataset.fitrmax rstep = dataset.fitrstep st = dataset.getFitSamplingType() newdataset.setFitSamplingType(st, rstep) doping = dataset.metadata.get("doping") if doping is None: doping = 0.0 newdataset.metadata["doping"] = doping newdataset.constraints = copy.deepcopy(dataset.constraints) # Set the chosen temperature newdataset.metadata["temperature"] = temperatures[i] # Add the dataset to the fitcopy fitcopy.add(newdataset, None) # Set the parameters to the previous fit's name, if one exists. if fitlastname: parval = "=%s" % fitlastname for par in fitcopy.parameters.values(): par.setInitial(parval) # Now paste the copy into the control. fitnewname = "%s-T%i=%g" % (fitbasename, i + 1, temperatures[i]) o = control.paste(fitcopy, new_name=fitnewname) fits.append(o) return [f.organization() for f in fits]