Skip to content

Writing Tutorials #81

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

Closed
wants to merge 12 commits into from
Next Next commit
Initial reformatting
  • Loading branch information
Andrew Yang committed Jun 22, 2023
commit 4a5acea8f30e4bb86d30d0cd055255b4392b3efe
4 changes: 2 additions & 2 deletions diffpy/pdfmorph/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
def set_verbosity(vb):
'''Set verbosity of the pdfmorph logger.

vb -- integer or one of ('debug', 'info', 'warning', 'error') strings
:param vb: integer or one of ('debug', 'info', 'warning', 'error') strings.

No return value.
:return: No return value.
'''
try:
if type(vb) is str:
Expand Down
68 changes: 28 additions & 40 deletions diffpy/pdfmorph/pdfplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,13 @@
def plotPDFs(pairlist, labels=None, offset='auto', rmin=None, rmax=None):
"""Plots several PDFs on top of one another.

pairlist -- iterable of (r, gr) pairs to plot
labels -- iterable of names for the pairs. If this is not the same
length as the pairlist, a legend will not be shown (default
[]).
offset -- offset to place between plots. PDFs will be sequentially
shifted in the y-direction by the offset. If offset is
'auto' (default), the optimal offset will be determined
automatically.
rmin -- The minimum r-value to plot. If this is None (default), the
lower bound of the PDF is not altered.
rmax -- The maximum r-value to plot. If this is None (default), the
upper bound of the PDF is not altered.

:param pairlist: iterable of (r, gr) pairs to plot
:param labels: iterable of names for the pairs. If this is not the same length as the pairlist, a legend will not
be shown (default []).
:param offset: offset to place between plots. PDFs will be sequentially shifted in the y-direction by the offset.
If offset is 'auto' (default), the optimal offset will be determined automatically.
:param rmin: The minimum r-value to plot. If this is None (default), the lower bound of the PDF is not altered.
:param rmax: The maximum r-value to plot. If this is None (default), the upper bound of the PDF is not altered.
"""
if labels is None:
labels = []
Expand Down Expand Up @@ -76,26 +70,22 @@ def comparePDFs(
legend=True,
l_width=1.5,
):
"""Plot two PDFs on top of each other and difference curve.

pairlist -- iterable of (r, gr) pairs to plot
labels -- iterable of names for the pairs. If this is not the same
length as the pairlist, a legend will not be shown (default
[]).
rmin -- The minimum r-value to plot. If this is None (default), the
lower bound of the PDF is not altered.
rmax -- The maximum r-value to plot. If this is None (default), the
upper bound of the PDF is not altered.
show -- Show the plot (True)
maglim -- Point after which to magnify the signal by mag. If None
(default), no magnification will take place.
mag -- Magnification factor (default 5)
rw -- Rw value to display on the plot, if any.
legend -- Display the legend (default True).

The second PDF will be shown as blue circles below and the first as a red
line. The difference curve will be in green and offset for clarity.

"""Plots two PDFs on top of each other and difference curve.

:param pairlist: iterable of (r, gr) pairs to plot.
:param labels: iterable of names for the pairs. If this is not the same length as the pairlist, a legend will not
be shown (default []).
:param rmin: The minimum r-value to plot. If this is None (default), the lower bound of the PDF is not altered.
:param rmax: The maximum r-value to plot. If this is None (default), the upper bound of the PDF is not altered.
:param show: Show the plot (default True).
:param maglim: Point after which to magnify the signal by mag. If None (default), no magnification will take
place.
:param mag: Magnification factor (default 5)
:param rw: Rw value to display on the plot, if any.
:param legend: Display the legend (default True).

The second PDF will be shown as blue circles below and the first as a red line. The difference curve will be in
green and offset for clarity.
"""
if labels is None:
labels = [2]
Expand Down Expand Up @@ -210,14 +200,12 @@ def comparePDFs(
def truncatePDFs(r, gr, rmin=None, rmax=None):
"""Truncate a PDF to specified bounds.

r -- r-values of the PDF
gr -- PDF values.
rmin -- The minimum r-value. If this is None (default), the lower
bound of the PDF is not altered.
rmax -- The maximum r-value. If this is None (default), the upper
bound of the PDF is not altered.
:param r: r-values of the PDF
:param gr: PDF values.
:param rmin: The minimum r-value. If this is None (default), the lower bound of the PDF is not altered.
:param rmax: The maximum r-value. If this is None (default), the upper bound of the PDF is not altered.

Returns the truncated r, gr
:return: Returns the truncated r, gr.
"""

if rmin is not None:
Expand Down
33 changes: 13 additions & 20 deletions diffpy/pdfmorph/refine.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
#
##############################################################################

"""refine -- Refine a morph or morph chain
"""
"""Refine a morph or morph chain."""

from scipy.optimize import leastsq
from scipy.stats import pearsonr
Expand All @@ -35,17 +34,16 @@ class Refiner(object):
x_morph, y_morph -- Morphed arrays.
x_target, y_target -- Target arrays.
pars -- List of names of parameters to be refined.
residual -- The residual function to optimize. Default _residual. Can
be assigned to other functions.
residual -- The residual function to optimize. Default _residual. Can be assigned to other functions.

"""

def __init__(self, chain, x_morph, y_morph, x_target, y_target):
"""Initialize the arrays.

chain -- The Morph or MorphChain to refine
x_morph, y_morph -- Morphed arrays.
x_target, y_target -- Target arrays.
:param chain: The Morph or MorphChain to refine.
:param x_morph, y_morph: Morphed arrays.
:param x_target, y_target: Target arrays.
"""
self.chain = chain
self.x_morph = x_morph
Expand Down Expand Up @@ -74,9 +72,8 @@ def _residual(self, pvals):
def _pearson(self, pvals):
"""Pearson correlation function.

This gives e**-p (vector), where p is the pearson correlation function.
We seek to minimize this, which occurrs when the correlation is the
largest.
This gives e**-p (vector), where p is the pearson correlation function. We seek to minimize this, which
occurs when the correlation is the largest.
"""
self._update_chain(pvals)
_x_morph, _y_morph, _x_target, _y_target = self.chain(
Expand All @@ -95,19 +92,16 @@ def _add_pearson(self, pvals):
def refine(self, *args, **kw):
"""Refine the chain.

Additional arguments are used to specify which parameters are to be
refined. If no arguments are passed, then all parameters will be
refined. Keywords pass initial values to the parameters, whether or
not they are refined.
Additional arguments are used to specify which parameters are to be refined. If no arguments are passed, then
all parameters will be refined. Keywords pass initial values to the parameters, whether or not they are
refined.

This uses the leastsq algorithm from scipy.optimize.

This returns the final scalar residual value. The parameters from the
fit can be retrieved from the config dictionary of the morph or morph
chain.

Raises ValueError if a minimum cannot be found.
:return: This returns the final scalar residual value. The parameters from the fit can be retrieved from the
config dictionary of the morph or morph chain.

:raise ValueError: if a minimum cannot be found.
"""

self.pars = args or self.chain.config.keys()
Expand All @@ -133,5 +127,4 @@ def refine(self, *args, **kw):

return dot(fvec, fvec)


# End class Refiner
24 changes: 9 additions & 15 deletions diffpy/pdfmorph/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
##############################################################################


"""Tools used in morphs and morph chains.
"""
"""Tools used in morphs and morph chains."""


import numpy
Expand All @@ -31,18 +30,14 @@ def estimateScale(y_morph_in, y_target_in):
def estimateBaselineSlope(r, gr, rmin=None, rmax=None):
"""Estimate the slope of the linear baseline of a PDF.

This fits a the equation slope*r through the bottom of the PDF.
This fits an equation of the form slope*r through the bottom of the PDF.

r -- The r-grid used for the PDF.
gr -- The PDF over the r-grid.
rmin -- The minimum r-value to consider. If this is None (default)
is None, then the minimum of r is used.
rmax -- The maximum r-value to consider. If this is None (default)
is None, then the maximum of r is used.

Returns the slope of baseline. If the PDF is scaled properly, this is equal
to -4*pi*rho0.
:param r: The r-grid used for the PDF.
:param gr: The PDF over the r-grid.
:param rmin: The minimum r-value to consider. If this is None (default) is None, then the minimum of r is used.
:param rmax: The maximum r-value to consider. If this is None (default) is None, then the maximum of r is used.

:return: Returns the slope of baseline. If the PDF is scaled properly, this is equal to -4*pi*rho0.
"""
from scipy.optimize import leastsq
from numpy import dot
Expand Down Expand Up @@ -99,10 +94,9 @@ def get_pearson(chain):
def readPDF(fname):
"""Reads an .gr file, loads r and G(r) vectors.

fname -- name of the file we want to read.

Returns r and gr arrays.
:param fname: name of the file we want to read.

:return: Returns r and gr arrays.
"""
from diffpy.utils.parsers import loadData

Expand Down
3 changes: 1 addition & 2 deletions diffpy/pdfmorph/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
#
##############################################################################

"""Definition of __version__ and __date__ for diffpy.pdfmorph.
"""
"""Definition of __version__ and __date__ for diffpy.pdfmorph."""


# obtain version information
Expand Down