Skip to content

Commit 872b465

Browse files
committed
Adding inverse shape functions, cleaning up code
1 parent 461b1d0 commit 872b465

File tree

12 files changed

+170
-22
lines changed

12 files changed

+170
-22
lines changed

diffpy/pdfmorph/log.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
##############################################################################
2+
#
3+
# diffpy.pdfmorph by DANSE Diffraction group
4+
# Simon J. L. Billinge
5+
# (c) 2010 Trustees of the Columbia University
6+
# in the City of New York. All rights reserved.
7+
#
8+
# File coded by: Pavol Juhas, Chris Farrow
9+
#
10+
# See AUTHORS.txt for a list of people who contributed.
11+
# See LICENSE.txt for license information.
12+
#
13+
##############################################################################
14+
15+
"""Configuration of loggers used in this package.
16+
17+
Logger instances:
18+
19+
plog -- logger instance for normal operation
20+
"""
21+
22+
# module version
23+
__id__ = "$Id$"
24+
25+
import logging
26+
27+
# logging configuration
28+
plog = logging.getLogger('diffpy.pdfmorph')
29+
30+
31+
def setVerbosity(vb):
32+
'''Set verbosity of the pdfmorph logger.
33+
34+
vb -- integer or one of ('debug', 'info', 'warning', 'error') strings
35+
36+
No return value.
37+
'''
38+
try:
39+
if type(vb) is str:
40+
level = int(getattr(logging, vb.upper(), vb))
41+
else:
42+
level = int(vb)
43+
except (TypeError, AttributeError):
44+
emsg = "invalid value of verbose %r" % vb
45+
raise ValueError(emsg)
46+
plog.setLevel(level)
47+
plog.info("log level set to %r", level)
48+
return
49+
50+
# End of file

diffpy/pdfmorph/morphs/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@
2626
from diffpy.pdfmorph.morphs.morphrgrid import MorphRGrid
2727
from diffpy.pdfmorph.morphs.morphscale import MorphScale
2828
from diffpy.pdfmorph.morphs.morphshape import MorphSphere, MorphSpheroid
29+
from diffpy.pdfmorph.morphs.morphishape import MorphISphere, MorphISpheroid
2930
from diffpy.pdfmorph.morphs.morphshift import MorphShift
3031
from diffpy.pdfmorph.morphs.morphsmear import MorphSmear
3132
from diffpy.pdfmorph.morphs.morphstretch import MorphStretch
3233

3334
# List of morphs
3435
morphs = [ MorphRGrid, MorphScale, MorphStretch, MorphXtalPDFtoRDF, MorphSmear,
35-
MorphXtalRDFtoPDF, MorphSphere, MorphSpheroid, MorphResolutionDamping,
36-
MorphShift]
36+
MorphXtalRDFtoPDF, MorphSphere, MorphSpheroid, MorphISphere,
37+
MorphISpheroid, MorphResolutionDamping, MorphShift]
3738

3839
# obtain version information
3940
from diffpy.pdfmorph.version import __version__

diffpy/pdfmorph/morphs/morph.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def __getattr__(self, name):
204204
else:
205205
emsg = 'Object has no attribute %r' % name
206206
raise AttributeError(emsg)
207-
return rv
207+
return
208208

209209

210210
def __setattr__(self, name, val):

diffpy/pdfmorph/morphs/morphchain.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def __getattr__(self, name):
133133
else:
134134
emsg = 'Object has no attribute %r' % name
135135
raise AttributeError(emsg)
136-
return rv
136+
return
137137

138138

139139
def __setattr__(self, name, val):
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
##############################################################################
2+
#
3+
# diffpy.pdfmorph by DANSE Diffraction group
4+
# Simon J. L. Billinge
5+
# (c) 2010 Trustees of the Columbia University
6+
# in the City of New York. All rights reserved.
7+
#
8+
# File coded by: Chris Farrow
9+
#
10+
# See AUTHORS.txt for a list of people who contributed.
11+
# See LICENSE.txt for license information.
12+
#
13+
##############################################################################
14+
15+
16+
"""class MorphISphere -- apply inverse spherical shape function
17+
class MorphISpheroid -- apply inverse spheroidal shape function
18+
"""
19+
20+
# module version
21+
__id__ = "$Id$"
22+
23+
from diffpy.pdfmorph.morphs.morph import *
24+
from diffpy.pdfmorph.morphs.morphshape import _sphericalCF, _spheroidalCF
25+
26+
class MorphISphere(Morph):
27+
'''Apply inverse spherical characteristic function to the objective
28+
29+
Configuration variables:
30+
31+
iradius -- The radius of the sphere
32+
33+
'''
34+
35+
# Define input output types
36+
summary = 'Apply inverse spherical characteristic function to objective'
37+
xinlabel = LABEL_RA
38+
yinlabel = LABEL_GR
39+
xoutlabel = LABEL_RA
40+
youtlabel = LABEL_GR
41+
parnames = ["iradius"]
42+
43+
def morph(self, xobj, yobj, xref, yref):
44+
"""Apply a scale factor."""
45+
Morph.morph(self, xobj, yobj, xref, yref)
46+
f = _sphericalCF(xobj, 2 * self.iradius)
47+
self.yobjout /= f
48+
self.yobjout[f == 0] = 0
49+
return self.xyallout
50+
51+
# End of class MorphISphere
52+
53+
class MorphISpheroid(Morph):
54+
'''Apply inverse spherical characteristic function to the objective
55+
56+
Configuration variables:
57+
58+
iradius -- The equatorial radius of the spheroid
59+
ipradius -- The polar radius of the spheroid
60+
61+
'''
62+
63+
# Define input output types
64+
summary = 'Apply inverse spheroidal characteristic function to objective'
65+
xinlabel = LABEL_RA
66+
yinlabel = LABEL_GR
67+
xoutlabel = LABEL_RA
68+
youtlabel = LABEL_GR
69+
parnames = ["iradius", "ipradius"]
70+
71+
def morph(self, xobj, yobj, xref, yref):
72+
"""Apply a scale factor."""
73+
Morph.morph(self, xobj, yobj, xref, yref)
74+
f = _spheroidalCF(xobj, self.iradius, self.ipradius)
75+
self.yobjout /= f
76+
self.yobjout[f == 0] == 0
77+
return self.xyallout
78+
79+
# End of class MorphSpheroid

diffpy/pdfmorph/morphs/morphpdftordf.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@
1919
# module version
2020
__id__ = "$Id$"
2121

22-
2322
from diffpy.pdfmorph.morphs.morph import *
2423

25-
2624
class MorphXtalPDFtoRDF(Morph):
2725
'''Morph crystal PDFs to RDFs.
2826

diffpy/pdfmorph/morphs/morphrdftopdf.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
__id__ = "$Id$"
2121

2222

23-
import numpy
2423
from diffpy.pdfmorph.morphs.morph import *
2524

2625

diffpy/pdfmorph/morphs/morphshape.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class MorphSpheroid -- apply a spheroidal shape function to the objective
2121
__id__ = "$Id$"
2222

2323
import numpy
24-
from numpy import pi, sqrt, log, exp, log2, ceil, sign
24+
from numpy import sqrt
2525
from numpy import arctan as atan
2626
from numpy import arctanh as atanh
2727

@@ -78,7 +78,7 @@ def morph(self, xobj, yobj, xref, yref):
7878
self.yobjout *= f
7979
return self.xyallout
8080

81-
# End of class MorphScale
81+
# End of class MorphSpheroid
8282

8383
def _sphericalCF(r, psize):
8484
"""Spherical nanoparticle characteristic function.

diffpy/pdfmorph/pdfmorphapp.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ def createOptionParser():
8282
group.add_option('--pradius', type="float", metavar="PRADIUS",
8383
help="""Apply characteristic function of spheroid with equatorial
8484
radius RADIUS and polar radius PRADIUS. If only PRADIUS is specified, instead apply characteristic function of sphere with radius PRADIUS.""")
85-
85+
group.add_option('--iradius', type="float", metavar="IRADIUS",
86+
help="""Apply inverse characteristic function of sphere with radius IRADIUS. If IPRADIUS is also specified, instead apply inverse characteristic function of spheroid with equatorial radius IRADIUS and polar radius IPRADIUS.""")
87+
group.add_option('--ipradius', type="float", metavar="IPRADIUS",
88+
help="""Apply inverse characteristic function of spheroid with equatorial radius IRADIUS and polar radius IPRADIUS. If only IPRADIUS is specified, instead apply inverse characteristic function of sphere with radius IPRADIUS.""")
8689

8790
# Plot Options
8891
group = optparse.OptionGroup(parser, "Plot Options",
@@ -122,9 +125,9 @@ def main():
122125

123126
# Get configuration values
124127
config = {}
125-
config.setdefault("rmin", None)
126-
config.setdefault("rmax", None)
127-
config.setdefault("rstep", None)
128+
config["rmin"] = opts.rmin
129+
config["rmax"] = opts.rmax
130+
config["rstep"] = None
128131
if opts.rmin is not None and opts.rmax is not None and \
129132
opts.rmax <= opts.rmin:
130133
e = "rmin must be less than rmax"
@@ -168,9 +171,23 @@ def main():
168171
elif nrad == 2:
169172
config["radius"] = radii[0]
170173
refpars.append("radius")
171-
config["pradius"] = radii[0]
174+
config["pradius"] = radii[1]
172175
refpars.append("pradius")
173176
chain.append( morphs.MorphSpheroid() )
177+
iradii = [opts.iradius, opts.ipradius]
178+
inrad = 2 - iradii.count(None)
179+
if inrad == 1:
180+
iradii.remove(None)
181+
config["iradius"] = iradii[0]
182+
chain.append( morphs.MorphISphere() )
183+
refpars.append("iradius")
184+
elif inrad == 2:
185+
config["iradius"] = iradii[0]
186+
refpars.append("iradius")
187+
config["ipradius"] = iradii[1]
188+
refpars.append("ipradius")
189+
chain.append( morphs.MorphISpheroid() )
190+
174191
## Resolution
175192
if opts.qdamp is not None:
176193
chain.append( morphs.MorphResolutionDamping() )
@@ -243,7 +260,7 @@ def main():
243260
maglim = opts.maglim
244261
mag = opts.mag
245262
pdfplot.comparePDFs(pairlist, labels, rmin = pmin, rmax = pmax, maglim
246-
= maglim, mag = mag)
263+
= maglim, mag = mag, rw = rw)
247264

248265
return
249266

diffpy/pdfmorph/pdfplot.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def plotPDFs(pairlist, labels = [], offset = 'auto', rmin = None, rmax = None):
5959
return
6060

6161
def comparePDFs(pairlist, labels = [], rmin = None, rmax = None, show = True,
62-
maglim = None, mag = 5):
62+
maglim = None, mag = 5, rw = None, legend = True):
6363
"""Plot two PDFs on top of each other and difference curve.
6464
6565
pairlist -- iterable of (r, gr) pairs to plot
@@ -74,6 +74,8 @@ def comparePDFs(pairlist, labels = [], rmin = None, rmax = None, show = True,
7474
maglim -- Point after which to magnify the signal by mag. If None
7575
(default), no magnification will take place.
7676
mag -- Magnification factor (default 5)
77+
rw -- Rw value to display on the plot, if any.
78+
legend -- Display the legend (default True).
7779
7880
The second PDF will be shown as blue circles below and the first as a red
7981
line. The difference curve will be in green and offset for clarity.
@@ -101,8 +103,9 @@ def comparePDFs(pairlist, labels = [], rmin = None, rmax = None, show = True,
101103
diff = grdat - gtemp
102104

103105
# Put rw in the label
104-
rw = (sum(diff**2) / sum(grdat**2))**0.5
105-
labeldiff = "difference (Rw = %.2f)"%rw if len(labels) < 3 else labels[2]
106+
labeldiff = "difference" if len(labels) < 3 else labels[2]
107+
if rw is not None:
108+
labeldiff += " (Rw = %.3f)"%rw
106109

107110
# Magnify if necessary
108111
if maglim is not None:
@@ -176,8 +179,9 @@ def comparePDFs(pairlist, labels = [], rmin = None, rmax = None, show = True,
176179
pylab.xlabel("r $(\AA)$")
177180
pylab.ylabel("G $(\AA^{-1})$")
178181
#pylab.legend(loc = 0)
179-
pylab.legend(bbox_to_anchor=(0.005, 1.02, 0.99, .10), loc=3,
180-
ncol=3, mode="expand", borderaxespad=0)
182+
if legend:
183+
pylab.legend(bbox_to_anchor=(0.005, 1.02, 0.99, .10), loc=3,
184+
ncol=3, mode="expand", borderaxespad=0)
181185
if show: pylab.show()
182186

183187
return

0 commit comments

Comments
 (0)