Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
pre-commit-ci[bot] committed Jul 2, 2024
1 parent 95f847f commit 07637de
Show file tree
Hide file tree
Showing 120 changed files with 2,060 additions and 1,911 deletions.
38 changes: 22 additions & 16 deletions devutils/makesdist
Original file line number Diff line number Diff line change
@@ -1,51 +1,57 @@
#!/usr/bin/env python

'''Create source distribution tar.gz archive, where each file belongs
"""Create source distribution tar.gz archive, where each file belongs
to a root user and modification time is set to the git commit time.
'''
"""

import sys
import glob
import gzip
import os
import subprocess
import glob
import sys
import tarfile
import gzip

BASEDIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
sys.path.insert(0, BASEDIR)

from setup import versiondata, FALLBACK_VERSION
timestamp = versiondata.getint('DEFAULT', 'timestamp')
from setup import FALLBACK_VERSION, versiondata

vfb = versiondata.get('DEFAULT', 'version').split('.post')[0] + '.post0'
timestamp = versiondata.getint("DEFAULT", "timestamp")

vfb = versiondata.get("DEFAULT", "version").split(".post")[0] + ".post0"
emsg = "Invalid FALLBACK_VERSION. Expected %r got %r."
assert vfb == FALLBACK_VERSION, emsg % (vfb, FALLBACK_VERSION)


def inform(s):
sys.stdout.write(s)
sys.stdout.flush()
return


inform('Run "setup.py sdist --formats=tar" ')
cmd_sdist = [sys.executable] + 'setup.py sdist --formats=tar'.split()
ec = subprocess.call(cmd_sdist, cwd=BASEDIR, stdout=open(os.devnull, 'w'))
if ec: sys.exit(ec)
cmd_sdist = [sys.executable] + "setup.py sdist --formats=tar".split()
ec = subprocess.call(cmd_sdist, cwd=BASEDIR, stdout=open(os.devnull, "w"))
if ec:
sys.exit(ec)
inform("[done]\n")

tarname = max(glob.glob(BASEDIR + '/dist/*.tar'), key=os.path.getmtime)
tarname = max(glob.glob(BASEDIR + "/dist/*.tar"), key=os.path.getmtime)

tfin = tarfile.open(tarname)
fpout = gzip.GzipFile(tarname + '.gz', 'w', mtime=0)
tfout = tarfile.open(fileobj=fpout, mode='w')
fpout = gzip.GzipFile(tarname + ".gz", "w", mtime=0)
tfout = tarfile.open(fileobj=fpout, mode="w")


def fixtarinfo(tinfo):
tinfo.uid = tinfo.gid = 0
tinfo.uname = tinfo.gname = 'root'
tinfo.uname = tinfo.gname = "root"
tinfo.mtime = timestamp
tinfo.mode &= ~0o022
return tinfo

inform('Filter %s --> %s.gz ' % (2 * (os.path.basename(tarname),)))

inform("Filter %s --> %s.gz " % (2 * (os.path.basename(tarname),)))
for ti in tfin:
tfout.addfile(fixtarinfo(ti), tfin.extractfile(ti))

Expand Down
57 changes: 30 additions & 27 deletions doc/examples/coreshellnp.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,15 @@
"""

import numpy
from scipy.optimize import leastsq

from pyobjcryst import loadCrystal
from scipy.optimize import leastsq

from diffpy.srfit.fitbase import FitContribution, FitRecipe, FitResults, Profile
from diffpy.srfit.pdf import PDFGenerator, PDFParser
from diffpy.srfit.fitbase import Profile
from diffpy.srfit.fitbase import FitContribution, FitRecipe
from diffpy.srfit.fitbase import FitResults

####### Example Code


def makeRecipe(stru1, stru2, datname):
"""Create a fitting recipe for crystalline PDF data."""

Expand All @@ -43,7 +41,7 @@ def makeRecipe(stru1, stru2, datname):
parser = PDFParser()
parser.parseFile(datname)
profile.loadParsedData(parser)
profile.setCalculationRange(xmin=1.5, xmax = 45, dx = 0.1)
profile.setCalculationRange(xmin=1.5, xmax=45, dx=0.1)

## The ProfileGenerator
# In order to fit the core and shell phases simultaneously, we must use two
Expand All @@ -66,15 +64,16 @@ def makeRecipe(stru1, stru2, datname):
contribution = FitContribution("cdszns")
contribution.addProfileGenerator(generator_cds)
contribution.addProfileGenerator(generator_zns)
contribution.setProfile(profile, xname = "r")
contribution.setProfile(profile, xname="r")

# Set up the characteristic functions. We use a spherical CF for the core
# and a spherical shell CF for the shell. Since this is set up as two
# phases, we implicitly assume that the core-shell correlations contribute
# very little to the PDF.
from diffpy.srfit.pdf.characteristicfunctions import sphericalCF, shellCF
contribution.registerFunction(sphericalCF, name = "f_CdS")
contribution.registerFunction(shellCF, name = "f_ZnS")
from diffpy.srfit.pdf.characteristicfunctions import shellCF, sphericalCF

contribution.registerFunction(sphericalCF, name="f_CdS")
contribution.registerFunction(shellCF, name="f_ZnS")

# Write the fitting equation. We want to sum the PDFs from each phase and
# multiply it by a scaling factor.
Expand Down Expand Up @@ -106,27 +105,28 @@ def makeRecipe(stru1, stru2, datname):
# subsequent refinement.
phase_cds = generator_cds.phase
for par in phase_cds.sgpars.latpars:
recipe.addVar(par, name = par.name + "_cds", tag = "lat")
recipe.addVar(par, name=par.name + "_cds", tag="lat")
for par in phase_cds.sgpars.adppars:
recipe.addVar(par, 1, name = par.name + "_cds", tag = "adp")
recipe.addVar(phase_cds.sgpars.xyzpars.z_1, name = "z_1_cds", tag = "xyz")
recipe.addVar(par, 1, name=par.name + "_cds", tag="adp")
recipe.addVar(phase_cds.sgpars.xyzpars.z_1, name="z_1_cds", tag="xyz")
# Since we know these have stacking disorder, constrain the B33 adps for
# each atom type.
recipe.constrain("B33_1_cds", "B33_0_cds")
recipe.addVar(generator_cds.delta2, name = "delta2_cds", value = 5)
recipe.addVar(generator_cds.delta2, name="delta2_cds", value=5)

phase_zns = generator_zns.phase
for par in phase_zns.sgpars.latpars:
recipe.addVar(par, name = par.name + "_zns", tag = "lat")
recipe.addVar(par, name=par.name + "_zns", tag="lat")
for par in phase_zns.sgpars.adppars:
recipe.addVar(par, 1, name = par.name + "_zns", tag = "adp")
recipe.addVar(phase_zns.sgpars.xyzpars.z_1, name = "z_1_zns", tag = "xyz")
recipe.addVar(par, 1, name=par.name + "_zns", tag="adp")
recipe.addVar(phase_zns.sgpars.xyzpars.z_1, name="z_1_zns", tag="xyz")
recipe.constrain("B33_1_zns", "B33_0_zns")
recipe.addVar(generator_zns.delta2, name = "delta2_zns", value = 2.5)
recipe.addVar(generator_zns.delta2, name="delta2_zns", value=2.5)

# Give the recipe away so it can be used!
return recipe


def plotResults(recipe):
"""Plot the results contained within a refined FitRecipe."""

Expand All @@ -138,10 +138,11 @@ def plotResults(recipe):
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.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)
Expand All @@ -163,6 +164,7 @@ def main():
stru2 = loadCrystal(znsciffile)
recipe = makeRecipe(stru1, stru2, data)
from diffpy.srfit.fitbase.fithook import PlotFitHook

recipe.pushFitHook(PlotFitHook())
recipe.fithooks[0].verbose = 3

Expand All @@ -172,23 +174,23 @@ def main():
# Start with the lattice parameters. In makeRecipe, these were tagged with
# "lat". Here is how we use that.
recipe.free("lat")
leastsq(recipe.residual, recipe.values, maxfev = 50)
leastsq(recipe.residual, recipe.values, maxfev=50)

# Now the scale and phase fraction.
recipe.free("scale", "scale_CdS")
leastsq(recipe.residual, recipe.values, maxfev = 50)
leastsq(recipe.residual, recipe.values, maxfev=50)

# The ADPs.
recipe.free("adp")
leastsq(recipe.residual, recipe.values, maxfev = 100)
leastsq(recipe.residual, recipe.values, maxfev=100)

# The delta2 parameters.
recipe.free("delta2_cds", "delta2_zns")
leastsq(recipe.residual, recipe.values, maxfev = 50)
leastsq(recipe.residual, recipe.values, maxfev=50)

# The shape parameters.
recipe.free("radius", "thickness")
leastsq(recipe.residual, recipe.values, maxfev = 50)
leastsq(recipe.residual, recipe.values, maxfev=50)

# The positional parameters.
recipe.free("xyz")
Expand All @@ -202,6 +204,7 @@ def main():
plotResults(recipe)
return


if __name__ == "__main__":
main()

Expand Down
26 changes: 14 additions & 12 deletions doc/examples/crystalpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,15 @@
"""

import numpy
from gaussianrecipe import scipyOptimize

from diffpy.structure import Structure
from diffpy.srfit.fitbase import FitContribution, FitRecipe, FitResults, Profile
from diffpy.srfit.pdf import PDFGenerator, PDFParser
from diffpy.srfit.fitbase import Profile
from diffpy.srfit.fitbase import FitContribution, FitRecipe
from diffpy.srfit.fitbase import FitResults

from gaussianrecipe import scipyOptimize
from diffpy.structure import Structure

####### Example Code


def makeRecipe(ciffile, datname):
"""Create a fitting recipe for crystalline PDF data."""

Expand All @@ -53,7 +51,7 @@ def makeRecipe(ciffile, datname):
parser = PDFParser()
parser.parseFile(datname)
profile.loadParsedData(parser)
profile.setCalculationRange(xmax = 20)
profile.setCalculationRange(xmax=20)

## The ProfileGenerator
# The PDFGenerator is for configuring and calculating a PDF profile. Here,
Expand All @@ -72,7 +70,7 @@ def makeRecipe(ciffile, datname):
# before.
contribution = FitContribution("nickel")
contribution.addProfileGenerator(generator)
contribution.setProfile(profile, xname = "r")
contribution.setProfile(profile, xname="r")

## Make the FitRecipe and add the FitContribution.
recipe = FitRecipe()
Expand All @@ -95,6 +93,7 @@ def makeRecipe(ciffile, datname):
# documentation for more details. The 'constrainAsSpaceGroup' method may
# create new Parameters, which it returns in a SpaceGroupParameters object.
from diffpy.srfit.structure import constrainAsSpaceGroup

sgpars = constrainAsSpaceGroup(phase, "Fm-3m")

# The SpaceGroupParameters object returned by 'constrainAsSpaceGroup' holds
Expand Down Expand Up @@ -124,6 +123,7 @@ def makeRecipe(ciffile, datname):
# Give the recipe away so it can be used!
return recipe


def plotResults(recipe):
"""Plot the results contained within a refined FitRecipe."""

Expand All @@ -135,17 +135,19 @@ def plotResults(recipe):
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.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


if __name__ == "__main__":

# Make the data and the recipe
Expand Down
Loading

0 comments on commit 07637de

Please sign in to comment.