Skip to content

black #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions diffpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@


from pkgutil import extend_path

__path__ = extend_path(__path__, __name__)


Expand Down
4 changes: 2 additions & 2 deletions diffpy/pdfmorph/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
__version__ = '0.0.1'

# top-level import
from diffpy.pdfmorph.pdfmorph_api import (pdfmorph, morph_default_config,
plot_morph)
from diffpy.pdfmorph.pdfmorph_api import pdfmorph, morph_default_config, plot_morph

# End of file
1 change: 1 addition & 0 deletions diffpy/pdfmorph/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ def setVerbosity(vb):
plog.info("log level set to %r", level)
return


# End of file
17 changes: 14 additions & 3 deletions diffpy/pdfmorph/morphs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,19 @@
from diffpy.pdfmorph.morphs.morphstretch import MorphStretch

# List of morphs
morphs = [ MorphRGrid, MorphScale, MorphStretch, MorphXtalPDFtoRDF, MorphSmear,
MorphXtalRDFtoPDF, MorphSphere, MorphSpheroid, MorphISphere,
MorphISpheroid, MorphResolutionDamping, MorphShift]
morphs = [
MorphRGrid,
MorphScale,
MorphStretch,
MorphXtalPDFtoRDF,
MorphSmear,
MorphXtalRDFtoPDF,
MorphSphere,
MorphSpheroid,
MorphISphere,
MorphISpheroid,
MorphResolutionDamping,
MorphShift,
]

# End of file
61 changes: 32 additions & 29 deletions diffpy/pdfmorph/morphs/morph.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
"""


LABEL_RA = 'r (A)' # r-grid
LABEL_GR = 'G (1/A^2)' # PDF G(r)
LABEL_RR = 'R (1/A)' # RDF R(r)
LABEL_RA = 'r (A)' # r-grid
LABEL_GR = 'G (1/A^2)' # PDF G(r)
LABEL_RR = 'R (1/A)' # RDF R(r)


class Morph(object):
'''Base class for implementing a morph on an objective given a reference.
Expand Down Expand Up @@ -75,17 +76,26 @@ class Morph(object):

# Properties

xyobjin = property(lambda self: (self.xobjin, self.yobjin),
doc='Return a tuple of objective input arrays')
xyobjout = property(lambda self: (self.xobjout, self.yobjout),
doc='Return a tuple of objective output arrays')
xyrefin = property(lambda self: (self.xrefin, self.yrefin),
doc='Return a tuple of reference input arrays')
xyrefout = property(lambda self: (self.xrefout, self.yrefout),
doc='Return a tuple of reference output arrays')
xyallout = property(lambda self:
(self.xobjout, self.yobjout, self.xrefout, self.yrefout),
doc='Return a tuple of all output arrays')
xyobjin = property(
lambda self: (self.xobjin, self.yobjin),
doc='Return a tuple of objective input arrays',
)
xyobjout = property(
lambda self: (self.xobjout, self.yobjout),
doc='Return a tuple of objective output arrays',
)
xyrefin = property(
lambda self: (self.xrefin, self.yrefin),
doc='Return a tuple of reference input arrays',
)
xyrefout = property(
lambda self: (self.xrefout, self.yrefout),
doc='Return a tuple of reference output arrays',
)
xyallout = property(
lambda self: (self.xobjout, self.yobjout, self.xrefout, self.yrefout),
doc='Return a tuple of all output arrays',
)

def __init__(self, config=None):
'''Create a default Morph instance.
Expand All @@ -107,7 +117,6 @@ def __init__(self, config=None):
self.applyConfig(config)
return


def morph(self, xobj, yobj, xref, yref):
'''Morph arrays objective or reference.

Expand All @@ -130,13 +139,10 @@ def morph(self, xobj, yobj, xref, yref):
self.checkConfig()
return self.xyallout


def __call__(self, xobj, yobj, xref, yref):
'''Alias for morph.
'''
'''Alias for morph.'''
return self.morph(xobj, yobj, xref, yref)


def applyConfig(self, config):
'''Process any configuration data from a dictionary.

Expand All @@ -147,15 +153,13 @@ def applyConfig(self, config):
self.config = config
return


def checkConfig(self):
'''Verify data in self.config. No action by default.

To be overridden in a derived class.
'''
return


def plotInputs(self, xylabels=True):
'''Plot input arrays using matplotlib.pyplot

Expand All @@ -164,14 +168,14 @@ def plotInputs(self, xylabels=True):
Return a list of matplotlib line objects.
'''
from matplotlib.pyplot import plot, xlabel, ylabel
rv = plot(self.xrefin, self.yrefin, label = "reference")
rv = plot(self.xobjin, self.yobjin, label = "objective")

rv = plot(self.xrefin, self.yrefin, label="reference")
rv = plot(self.xobjin, self.yobjin, label="objective")
if xylabels:
xlabel(self.xinlabel)
ylabel(self.yinlabel)
return rv


def plotOutputs(self, xylabels=True, **plotargs):
'''Plot output arrays using matplotlib.pyplot

Expand All @@ -182,16 +186,16 @@ def plotOutputs(self, xylabels=True, **plotargs):
Return a list of matplotlib line objects.
'''
from matplotlib.pyplot import plot, xlabel, ylabel

pargs = dict(plotargs)
pargs.pop("label", None)
rv = plot(self.xrefout, self.yrefout, label = "reference", **pargs)
rv = plot(self.xobjout, self.yobjout, label = "objective", **pargs)
rv = plot(self.xrefout, self.yrefout, label="reference", **pargs)
rv = plot(self.xobjout, self.yobjout, label="objective", **pargs)
if xylabels:
xlabel(self.xoutlabel)
ylabel(self.youtlabel)
return rv


def __getattr__(self, name):
'''Obtain the value from self.config, when normal lookup fails.

Expand All @@ -206,7 +210,6 @@ def __getattr__(self, name):
emsg = 'Object has no attribute %r' % name
raise AttributeError(emsg)


def __setattr__(self, name, val):
'''Set configuration variables to config.

Expand All @@ -220,5 +223,5 @@ def __setattr__(self, name, val):
object.__setattr__(self, name, val)
return

# End class Morph

# End class Morph
51 changes: 18 additions & 33 deletions diffpy/pdfmorph/morphs/morphchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,49 +52,39 @@ class MorphChain(list):

'''

xobjin = property(
lambda self: None if len(self) == 0 else self[0].xobjin)
yobjin = property(
lambda self: None if len(self) == 0 else self[0].yobjin)
xrefin = property(
lambda self: None if len(self) == 0 else self[0].xrefin)
yrefin = property(
lambda self: None if len(self) == 0 else self[0].yrefin)
xobjout = property(
lambda self: None if len(self) == 0 else self[-1].xobjout)
yobjout = property(
lambda self: None if len(self) == 0 else self[-1].yobjout)
xrefout = property(
lambda self: None if len(self) == 0 else self[-1].xrefout)
yrefout = property(
lambda self: None if len(self) == 0 else self[-1].yrefout)
xyobjin = property(
lambda self: (None, None) if len(self) == 0 else self[0].xyobjin)
xobjin = property(lambda self: None if len(self) == 0 else self[0].xobjin)
yobjin = property(lambda self: None if len(self) == 0 else self[0].yobjin)
xrefin = property(lambda self: None if len(self) == 0 else self[0].xrefin)
yrefin = property(lambda self: None if len(self) == 0 else self[0].yrefin)
xobjout = property(lambda self: None if len(self) == 0 else self[-1].xobjout)
yobjout = property(lambda self: None if len(self) == 0 else self[-1].yobjout)
xrefout = property(lambda self: None if len(self) == 0 else self[-1].xrefout)
yrefout = property(lambda self: None if len(self) == 0 else self[-1].yrefout)
xyobjin = property(lambda self: (None, None) if len(self) == 0 else self[0].xyobjin)
xyobjout = property(
lambda self: (None, None) if len(self) == 0 else self[-1].xyobjout)
xyrefin = property(
lambda self: (None, None) if len(self) == 0 else self[0].xyrefin)
lambda self: (None, None) if len(self) == 0 else self[-1].xyobjout
)
xyrefin = property(lambda self: (None, None) if len(self) == 0 else self[0].xyrefin)
xyrefout = property(
lambda self: (None, None) if len(self) == 0 else self[-1].xyrefout)
lambda self: (None, None) if len(self) == 0 else self[-1].xyrefout
)
xyallout = property(
lambda self: (None, None, None, None) if len(self) == 0 \
else self[-1].xyallout)
lambda self: (None, None, None, None) if len(self) == 0 else self[-1].xyallout
)
parnames = property(lambda self: set(p for m in self for p in m.parnames))


def __init__(self, config, *args):
"""Initialize the configuration.

config -- Configuration dictionary.

Additional arguments are morphs that will extend the queue of morphs.

"""
self.config = config
self.extend(args)
return


def morph(self, xobj, yobj, xref, yref):
'''Apply the chain of morphs to the input data.

Expand All @@ -112,13 +102,10 @@ def morph(self, xobj, yobj, xref, yref):
xyall = morph(*xyall)
return xyall


def __call__(self, xobj, yobj, xref, yref):
'''Alias for morph.
'''
'''Alias for morph.'''
return self.morph(xobj, yobj, xref, yref)


def __getattr__(self, name):
'''Obtain the value from self.config, when normal lookup fails.

Expand All @@ -133,7 +120,6 @@ def __getattr__(self, name):
emsg = 'Object has no attribute %r' % name
raise AttributeError(emsg)


def __setattr__(self, name, val):
'''Set configuration variables to config.

Expand All @@ -149,4 +135,3 @@ def __setattr__(self, name, val):


# End class MorphChain

4 changes: 4 additions & 0 deletions diffpy/pdfmorph/morphs/morphishape.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class MorphISpheroid -- apply inverse spheroidal shape function
from diffpy.pdfmorph.morphs.morph import *
from diffpy.pdfmorph.morphs.morphshape import _sphericalCF, _spheroidalCF


class MorphISphere(Morph):
'''Apply inverse spherical characteristic function to the objective

Expand All @@ -47,8 +48,10 @@ def morph(self, xobj, yobj, xref, yref):
self.yobjout[f == 0] = 0
return self.xyallout


# End of class MorphISphere


class MorphISpheroid(Morph):
'''Apply inverse spherical characteristic function to the objective

Expand All @@ -75,4 +78,5 @@ def morph(self, xobj, yobj, xref, yref):
self.yobjout[f == 0] == 0
return self.xyallout


# End of class MorphSpheroid
2 changes: 2 additions & 0 deletions diffpy/pdfmorph/morphs/morphpdftordf.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

from diffpy.pdfmorph.morphs.morph import *


class MorphXtalPDFtoRDF(Morph):
'''Morph crystal PDFs to RDFs.

Expand Down Expand Up @@ -53,4 +54,5 @@ def morph(self, xobj, yobj, xref, yref):
self.yrefout = self.xrefin * (self.yrefin - refbaseline)
return self.xyallout


# End of class MorphXtalPDFtoRDF
5 changes: 3 additions & 2 deletions diffpy/pdfmorph/morphs/morphrdftopdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ def morph(self, xobj, yobj, xref, yref):
Morph.morph(self, xobj, yobj, xref, yref)
objbaseline = self.baselineslope * self.xobjin
refbaseline = self.baselineslope * self.xrefin
self.yrefout = self.yrefin / self.xrefin + refbaseline
self.yrefout = self.yrefin / self.xrefin + refbaseline
if self.xrefin[0] == 0:
self.yrefout[0] = 0
self.yobjout = self.yobjin / self.xobjin + objbaseline
self.yobjout = self.yobjin / self.xobjin + objbaseline
if self.xobjin[0] == 0:
self.yobjout[0] = 0
return self.xyallout


# End of class MorphScale
5 changes: 3 additions & 2 deletions diffpy/pdfmorph/morphs/morphresolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

from diffpy.pdfmorph.morphs.morph import *


class MorphResolutionDamping(Morph):
'''Apply resolution damping and broadening to the objective.

Expand All @@ -44,9 +45,9 @@ class MorphResolutionDamping(Morph):
def morph(self, xobj, yobj, xref, yref):
"""Apply a resolution damping."""
Morph.morph(self, xobj, yobj, xref, yref)
b = numpy.exp(-0.5 * (self.xobjin*self.qdamp)**2)
b = numpy.exp(-0.5 * (self.xobjin * self.qdamp) ** 2)
self.yobjout *= b
return self.xyallout

# End of class MorphResolutionDamping

# End of class MorphResolutionDamping
4 changes: 3 additions & 1 deletion diffpy/pdfmorph/morphs/morphrgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
# roundoff tolerance for selecting bounds on arrays.
epsilon = 1e-8


class MorphRGrid(Morph):
'''Resample to specified r-grid.

Expand All @@ -40,7 +41,7 @@ class MorphRGrid(Morph):
If any of these is not defined or outside the bounds of the input arrays,
then it will be taken to be the most inclusive value from the input arrays.
These modified values will be stored as the above attributes.

'''

# Define input output types
Expand Down Expand Up @@ -72,4 +73,5 @@ def morph(self, xobj, yobj, xref, yref):
self.yrefout = numpy.interp(self.xrefout, self.xrefin, self.yrefin)
return self.xyallout


# End of class MorphRGrid
2 changes: 2 additions & 0 deletions diffpy/pdfmorph/morphs/morphscale.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

from diffpy.pdfmorph.morphs.morph import *


class MorphScale(Morph):
'''Scale the objective.

Expand All @@ -45,4 +46,5 @@ def morph(self, xobj, yobj, xref, yref):
self.yobjout *= self.scale
return self.xyallout


# End of class MorphScale
Loading