2222"""
2323
2424import numpy
25- from scipy .optimize import leastsq
26-
2725from pyobjcryst import loadCrystal
26+ from scipy .optimize import leastsq
2827
28+ from diffpy .srfit .fitbase import FitContribution , FitRecipe , FitResults , Profile
2929from diffpy .srfit .pdf import PDFGenerator , PDFParser
30- from diffpy .srfit .fitbase import Profile
31- from diffpy .srfit .fitbase import FitContribution , FitRecipe
32- from diffpy .srfit .fitbase import FitResults
3330
3431####### Example Code
3532
33+
3634def makeRecipe (stru1 , stru2 , datname ):
3735 """Create a fitting recipe for crystalline PDF data."""
3836
@@ -43,7 +41,7 @@ def makeRecipe(stru1, stru2, datname):
4341 parser = PDFParser ()
4442 parser .parseFile (datname )
4543 profile .loadParsedData (parser )
46- profile .setCalculationRange (xmin = 1.5 , xmax = 45 , dx = 0.1 )
44+ profile .setCalculationRange (xmin = 1.5 , xmax = 45 , dx = 0.1 )
4745
4846 ## The ProfileGenerator
4947 # In order to fit the core and shell phases simultaneously, we must use two
@@ -66,15 +64,16 @@ def makeRecipe(stru1, stru2, datname):
6664 contribution = FitContribution ("cdszns" )
6765 contribution .addProfileGenerator (generator_cds )
6866 contribution .addProfileGenerator (generator_zns )
69- contribution .setProfile (profile , xname = "r" )
67+ contribution .setProfile (profile , xname = "r" )
7068
7169 # Set up the characteristic functions. We use a spherical CF for the core
7270 # and a spherical shell CF for the shell. Since this is set up as two
7371 # phases, we implicitly assume that the core-shell correlations contribute
7472 # very little to the PDF.
75- from diffpy .srfit .pdf .characteristicfunctions import sphericalCF , shellCF
76- contribution .registerFunction (sphericalCF , name = "f_CdS" )
77- contribution .registerFunction (shellCF , name = "f_ZnS" )
73+ from diffpy .srfit .pdf .characteristicfunctions import shellCF , sphericalCF
74+
75+ contribution .registerFunction (sphericalCF , name = "f_CdS" )
76+ contribution .registerFunction (shellCF , name = "f_ZnS" )
7877
7978 # Write the fitting equation. We want to sum the PDFs from each phase and
8079 # multiply it by a scaling factor.
@@ -106,27 +105,28 @@ def makeRecipe(stru1, stru2, datname):
106105 # subsequent refinement.
107106 phase_cds = generator_cds .phase
108107 for par in phase_cds .sgpars .latpars :
109- recipe .addVar (par , name = par .name + "_cds" , tag = "lat" )
108+ recipe .addVar (par , name = par .name + "_cds" , tag = "lat" )
110109 for par in phase_cds .sgpars .adppars :
111- recipe .addVar (par , 1 , name = par .name + "_cds" , tag = "adp" )
112- recipe .addVar (phase_cds .sgpars .xyzpars .z_1 , name = "z_1_cds" , tag = "xyz" )
110+ recipe .addVar (par , 1 , name = par .name + "_cds" , tag = "adp" )
111+ recipe .addVar (phase_cds .sgpars .xyzpars .z_1 , name = "z_1_cds" , tag = "xyz" )
113112 # Since we know these have stacking disorder, constrain the B33 adps for
114113 # each atom type.
115114 recipe .constrain ("B33_1_cds" , "B33_0_cds" )
116- recipe .addVar (generator_cds .delta2 , name = "delta2_cds" , value = 5 )
115+ recipe .addVar (generator_cds .delta2 , name = "delta2_cds" , value = 5 )
117116
118117 phase_zns = generator_zns .phase
119118 for par in phase_zns .sgpars .latpars :
120- recipe .addVar (par , name = par .name + "_zns" , tag = "lat" )
119+ recipe .addVar (par , name = par .name + "_zns" , tag = "lat" )
121120 for par in phase_zns .sgpars .adppars :
122- recipe .addVar (par , 1 , name = par .name + "_zns" , tag = "adp" )
123- recipe .addVar (phase_zns .sgpars .xyzpars .z_1 , name = "z_1_zns" , tag = "xyz" )
121+ recipe .addVar (par , 1 , name = par .name + "_zns" , tag = "adp" )
122+ recipe .addVar (phase_zns .sgpars .xyzpars .z_1 , name = "z_1_zns" , tag = "xyz" )
124123 recipe .constrain ("B33_1_zns" , "B33_0_zns" )
125- recipe .addVar (generator_zns .delta2 , name = "delta2_zns" , value = 2.5 )
124+ recipe .addVar (generator_zns .delta2 , name = "delta2_zns" , value = 2.5 )
126125
127126 # Give the recipe away so it can be used!
128127 return recipe
129128
129+
130130def plotResults (recipe ):
131131 """Plot the results contained within a refined FitRecipe."""
132132
@@ -138,10 +138,11 @@ def plotResults(recipe):
138138 diff = g - gcalc + diffzero
139139
140140 import pylab
141- pylab .plot (r ,g ,'bo' ,label = "G(r) Data" )
142- pylab .plot (r , gcalc ,'r-' ,label = "G(r) Fit" )
143- pylab .plot (r ,diff ,'g-' ,label = "G(r) diff" )
144- pylab .plot (r ,diffzero ,'k-' )
141+
142+ pylab .plot (r , g , "bo" , label = "G(r) Data" )
143+ pylab .plot (r , gcalc , "r-" , label = "G(r) Fit" )
144+ pylab .plot (r , diff , "g-" , label = "G(r) diff" )
145+ pylab .plot (r , diffzero , "k-" )
145146 pylab .xlabel (r"$r (\AA)$" )
146147 pylab .ylabel (r"$G (\AA^{-2})$" )
147148 pylab .legend (loc = 1 )
@@ -163,6 +164,7 @@ def main():
163164 stru2 = loadCrystal (znsciffile )
164165 recipe = makeRecipe (stru1 , stru2 , data )
165166 from diffpy .srfit .fitbase .fithook import PlotFitHook
167+
166168 recipe .pushFitHook (PlotFitHook ())
167169 recipe .fithooks [0 ].verbose = 3
168170
@@ -172,23 +174,23 @@ def main():
172174 # Start with the lattice parameters. In makeRecipe, these were tagged with
173175 # "lat". Here is how we use that.
174176 recipe .free ("lat" )
175- leastsq (recipe .residual , recipe .values , maxfev = 50 )
177+ leastsq (recipe .residual , recipe .values , maxfev = 50 )
176178
177179 # Now the scale and phase fraction.
178180 recipe .free ("scale" , "scale_CdS" )
179- leastsq (recipe .residual , recipe .values , maxfev = 50 )
181+ leastsq (recipe .residual , recipe .values , maxfev = 50 )
180182
181183 # The ADPs.
182184 recipe .free ("adp" )
183- leastsq (recipe .residual , recipe .values , maxfev = 100 )
185+ leastsq (recipe .residual , recipe .values , maxfev = 100 )
184186
185187 # The delta2 parameters.
186188 recipe .free ("delta2_cds" , "delta2_zns" )
187- leastsq (recipe .residual , recipe .values , maxfev = 50 )
189+ leastsq (recipe .residual , recipe .values , maxfev = 50 )
188190
189191 # The shape parameters.
190192 recipe .free ("radius" , "thickness" )
191- leastsq (recipe .residual , recipe .values , maxfev = 50 )
193+ leastsq (recipe .residual , recipe .values , maxfev = 50 )
192194
193195 # The positional parameters.
194196 recipe .free ("xyz" )
@@ -202,6 +204,7 @@ def main():
202204 plotResults (recipe )
203205 return
204206
207+
205208if __name__ == "__main__" :
206209 main ()
207210
0 commit comments