Skip to content
Merged
36 changes: 10 additions & 26 deletions docs/examples/coreshellnp.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
different phases, each with an appropriate characteristic function.
"""

import numpy
from pathlib import Path

from pyobjcryst import loadCrystal
from scipy.optimize import leastsq

Expand Down Expand Up @@ -134,35 +135,18 @@ def makeRecipe(stru1, stru2, datname):
return recipe


def plotResults(recipe):
"""Plot the results contained within a refined FitRecipe."""
# All this should be pretty familiar by now.
r = recipe.cdszns.profile.x
g = recipe.cdszns.profile.y
gcalc = recipe.cdszns.profile.ycalc
diffzero = -0.8 * max(g) * numpy.ones_like(g)
diff = g - gcalc + diffzero

import pylab

pylab.plot(r, g, "bo", label="G(r) Data")
pylab.plot(r, gcalc, "r-", label="G(r) Fit")
pylab.plot(r, diff, "g-", label="G(r) diff")
pylab.plot(r, diffzero, "k-")
pylab.xlabel(r"$r (\AA)$")
pylab.ylabel(r"$G (\AA^{-2})$")
pylab.legend(loc=1)

pylab.show()
return
plot_styles = {
"xlabel": r"$r (\AA)$",
"ylabel": r"$G (\AA^{-2})$",
}


def main():
"""Set up and refine the recipe."""
# Make the data and the recipe
cdsciffile = "data/CdS.cif"
znsciffile = "data/ZnS.cif"
data = "data/CdS_ZnS_nano.gr"
cdsciffile = Path(__file__).parent / "data/CdS.cif"
znsciffile = Path(__file__).parent / "data/ZnS.cif"
data = Path(__file__).parent / "data/CdS_ZnS_nano.gr"

# Make the recipe
stru1 = loadCrystal(cdsciffile)
Expand Down Expand Up @@ -206,7 +190,7 @@ def main():
res.print_results()

# Plot!
plotResults(recipe)
recipe.plot_recipe(**plot_styles)
return


Expand Down
36 changes: 10 additions & 26 deletions docs/examples/crystalpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
demonstrates only the basic configuration.
"""

import numpy
from pathlib import Path

from gaussianrecipe import scipyOptimize

from diffpy.srfit.fitbase import (
Expand Down Expand Up @@ -67,7 +68,7 @@ def makeRecipe(ciffile, datname):
# Qmax value, as well as initial values for the non-structural Parameters.
generator = PDFGenerator("G")
stru = Structure()
stru.read(ciffile)
stru.read(str(ciffile))
generator.setStructure(stru)

# The FitContribution
Expand Down Expand Up @@ -129,34 +130,17 @@ def makeRecipe(ciffile, datname):
return recipe


def plotResults(recipe):
"""Plot the results contained within a refined FitRecipe."""
# All this should be pretty familiar by now.
r = recipe.nickel.profile.x
g = recipe.nickel.profile.y
gcalc = recipe.nickel.profile.ycalc
diffzero = -0.8 * max(g) * numpy.ones_like(g)
diff = g - gcalc + diffzero

import pylab

pylab.plot(r, g, "bo", label="G(r) Data")
pylab.plot(r, gcalc, "r-", label="G(r) Fit")
pylab.plot(r, diff, "g-", label="G(r) diff")
pylab.plot(r, diffzero, "k-")
pylab.xlabel(r"$r (\AA)$")
pylab.ylabel(r"$G (\AA^{-2})$")
pylab.legend(loc=1)

pylab.show()
return
plot_styles = {
"xlabel": r"$r (\AA)$",
"ylabel": r"$G (\AA^{-2})$",
}


if __name__ == "__main__":

# Make the data and the recipe
ciffile = "data/ni.cif"
data = "data/ni-q27r100-neutron.gr"
ciffile = Path(__file__).parent / "data/ni.cif"
data = Path(__file__).parent / "data/ni-q27r100-neutron.gr"

# Make the recipe
recipe = makeRecipe(ciffile, data)
Expand All @@ -170,6 +154,6 @@ def plotResults(recipe):
res.print_results()

# Plot!
plotResults(recipe)
recipe.plot_recipe(**plot_styles)

# End of file
91 changes: 16 additions & 75 deletions docs/examples/crystalpdfall.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
structure to all the available data.
"""

import numpy
from pathlib import Path

from gaussianrecipe import scipyOptimize
from pyobjcryst import loadCrystal

Expand Down Expand Up @@ -143,82 +144,20 @@ def makeRecipe(
return recipe


def plotResults(recipe):
"""Plot the results contained within a refined FitRecipe."""
# All this should be pretty familiar by now.
xnickel = recipe.xnickel
xr_ni = xnickel.profile.x
xg_ni = xnickel.profile.y
xgcalc_ni = xnickel.profile.ycalc
xdiffzero_ni = -0.8 * max(xg_ni) * numpy.ones_like(xg_ni)
xdiff_ni = xg_ni - xgcalc_ni + xdiffzero_ni

xsilicon = recipe.xsilicon
xr_si = xsilicon.profile.x
xg_si = xsilicon.profile.y
xgcalc_si = xsilicon.profile.ycalc
xdiffzero_si = -0.8 * max(xg_si) * numpy.ones_like(xg_si)
xdiff_si = xg_si - xgcalc_si + xdiffzero_si

nnickel = recipe.nnickel
nr_ni = nnickel.profile.x
ng_ni = nnickel.profile.y
ngcalc_ni = nnickel.profile.ycalc
ndiffzero_ni = -0.8 * max(ng_ni) * numpy.ones_like(ng_ni)
ndiff_ni = ng_ni - ngcalc_ni + ndiffzero_ni

xsini = recipe.xsini
xr_sini = xsini.profile.x
xg_sini = xsini.profile.y
xgcalc_sini = xsini.profile.ycalc
xdiffzero_sini = -0.8 * max(xg_sini) * numpy.ones_like(xg_sini)
xdiff_sini = xg_sini - xgcalc_sini + xdiffzero_sini

import pylab

pylab.subplot(2, 2, 1)
pylab.plot(xr_ni, xg_ni, "bo", label="G(r) x-ray nickel Data")
pylab.plot(xr_ni, xgcalc_ni, "r-", label="G(r) x-ray nickel Fit")
pylab.plot(xr_ni, xdiff_ni, "g-", label="G(r) x-ray nickel diff")
pylab.plot(xr_ni, xdiffzero_ni, "k-")
pylab.xlabel(r"$r (\AA)$")
pylab.ylabel(r"$G (\AA^{-2})$")
pylab.legend(loc=1)

pylab.subplot(2, 2, 2)
pylab.plot(xr_si, xg_si, "bo", label="G(r) x-ray silicon Data")
pylab.plot(xr_si, xgcalc_si, "r-", label="G(r) x-ray silicon Fit")
pylab.plot(xr_si, xdiff_si, "g-", label="G(r) x-ray silicon diff")
pylab.plot(xr_si, xdiffzero_si, "k-")
pylab.legend(loc=1)

pylab.subplot(2, 2, 3)
pylab.plot(nr_ni, ng_ni, "bo", label="G(r) neutron nickel Data")
pylab.plot(nr_ni, ngcalc_ni, "r-", label="G(r) neutron nickel Fit")
pylab.plot(nr_ni, ndiff_ni, "g-", label="G(r) neutron nickel diff")
pylab.plot(nr_ni, ndiffzero_ni, "k-")
pylab.legend(loc=1)

pylab.subplot(2, 2, 4)
pylab.plot(xr_sini, xg_sini, "bo", label="G(r) x-ray sini Data")
pylab.plot(xr_sini, xgcalc_sini, "r-", label="G(r) x-ray sini Fit")
pylab.plot(xr_sini, xdiff_sini, "g-", label="G(r) x-ray sini diff")
pylab.plot(xr_sini, xdiffzero_sini, "k-")
pylab.legend(loc=1)

pylab.show()
return

plot_styles = {
"xlabel": r"$r (\AA)$",
"ylabel": r"$G (\AA^{-2})$",
}

if __name__ == "__main__":

# Make the data and the recipe
ciffile_ni = "data/ni.cif"
ciffile_si = "data/si.cif"
xdata_ni = "data/ni-q27r60-xray.gr"
ndata_ni = "data/ni-q27r100-neutron.gr"
xdata_si = "data/si-q27r60-xray.gr"
xdata_sini = "data/si90ni10-q27r60-xray.gr"
ciffile_ni = Path(__file__).parent / "data/ni.cif"
ciffile_si = Path(__file__).parent / "data/si.cif"
xdata_ni = Path(__file__).parent / "data/ni-q27r60-xray.gr"
ndata_ni = Path(__file__).parent / "data/ni-q27r100-neutron.gr"
xdata_si = Path(__file__).parent / "data/si-q27r60-xray.gr"
xdata_sini = Path(__file__).parent / "data/si90ni10-q27r60-xray.gr"

# Make the recipe
recipe = makeRecipe(
Expand All @@ -232,7 +171,9 @@ def plotResults(recipe):
res = FitResults(recipe)
res.print_results()

# Plot!
plotResults(recipe)
# Plot! The recipe has four contributions ("xnickel", "xsilicon",
# "nnickel", "xsini"), so plot_recipe produces one figure per
# contribution.
recipe.plot_recipe(**plot_styles)

# End of file
18 changes: 11 additions & 7 deletions docs/examples/crystalpdfobjcryst.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
provided by the ObjCrystCrystalParSet structure adapter.
"""

from crystalpdf import plotResults
from pathlib import Path

from gaussianrecipe import scipyOptimize
from pyobjcryst import loadCrystal

Expand Down Expand Up @@ -90,9 +91,6 @@ def makeRecipe(ciffile, datname):
# things by iterating through all the sgpars.
for par in phase.sgpars:
recipe.add_variable(par)
# set the initial thermal factor to a non-zero value
assert hasattr(recipe, "B11_0")
recipe.B11_0 = 0.1

# We now select non-structural parameters to refine.
# This controls the scaling of the PDF.
Expand All @@ -106,11 +104,17 @@ def makeRecipe(ciffile, datname):
return recipe


plot_styles = {
"xlabel": r"$r (\AA)$",
"ylabel": r"$G (\AA^{-2})$",
}


if __name__ == "__main__":

# Make the data and the recipe
ciffile = "data/si.cif"
data = "data/si-q27r60-xray.gr"
ciffile = Path(__file__).parent / "data/si.cif"
data = Path(__file__).parent / "data/si-q27r60-xray.gr"

# Make the recipe
recipe = makeRecipe(ciffile, data)
Expand All @@ -123,6 +127,6 @@ def makeRecipe(ciffile, datname):
res.print_results()

# Plot!
plotResults(recipe)
recipe.plot_recipe(**plot_styles)

# End of file
54 changes: 13 additions & 41 deletions docs/examples/crystalpdftwodata.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
underlying ObjCrystCrystalParSet.
"""

import numpy
from pathlib import Path

from gaussianrecipe import scipyOptimize
from pyobjcryst import loadCrystal

Expand Down Expand Up @@ -134,49 +135,18 @@ def makeRecipe(ciffile, xdatname, ndatname):
return recipe


def plotResults(recipe):
"""Plot the results contained within a refined FitRecipe."""
# All this should be pretty familiar by now.
xr = recipe.xnickel.profile.x
xg = recipe.xnickel.profile.y
xgcalc = recipe.xnickel.profile.ycalc
xdiffzero = -0.8 * max(xg) * numpy.ones_like(xg)
xdiff = xg - xgcalc + xdiffzero

nr = recipe.nnickel.profile.x
ng = recipe.nnickel.profile.y
ngcalc = recipe.nnickel.profile.ycalc
ndiffzero = -0.8 * max(ng) * numpy.ones_like(ng)
ndiff = ng - ngcalc + ndiffzero

import pylab

pylab.subplot(2, 1, 1)
pylab.plot(xr, xg, "bo", label="G(r) x-ray Data")
pylab.plot(xr, xgcalc, "r-", label="G(r) x-ray Fit")
pylab.plot(xr, xdiff, "g-", label="G(r) x-ray diff")
pylab.plot(xr, xdiffzero, "k-")
pylab.legend(loc=1)

pylab.subplot(2, 1, 2)
pylab.plot(nr, ng, "bo", label="G(r) neutron Data")
pylab.plot(nr, ngcalc, "r-", label="G(r) neutron Fit")
pylab.plot(nr, ndiff, "g-", label="G(r) neutron diff")
pylab.plot(nr, ndiffzero, "k-")
pylab.xlabel(r"$r (\AA)$")
pylab.ylabel(r"$G (\AA^{-2})$")
pylab.legend(loc=1)

pylab.show()
return
plot_styles = {
"xlabel": r"$r (\AA)$",
"ylabel": r"$G (\AA^{-2})$",
}


if __name__ == "__main__":

# Make the data and the recipe
ciffile = "data/ni.cif"
xdata = "data/ni-q27r60nodg-xray.gr"
ndata = "data/ni-q27r100-neutron.gr"
ciffile = Path(__file__).parent / "data/ni.cif"
xdata = Path(__file__).parent / "data/ni-q27r60nodg-xray.gr"
ndata = Path(__file__).parent / "data/ni-q27r100-neutron.gr"

# Make the recipe
recipe = makeRecipe(ciffile, xdata, ndata)
Expand All @@ -188,7 +158,9 @@ def plotResults(recipe):
res = FitResults(recipe)
res.print_results()

# Plot!
plotResults(recipe)
# Plot! The recipe has two contributions ("xnickel" for x-ray,
# "nnickel" for neutron), so plot_recipe produces one figure per
# contribution.
recipe.plot_recipe(**plot_styles)

# End of file
Loading
Loading