Skip to content

Commit 768580b

Browse files
committed
pdfcontribution.py
1 parent da8c24e commit 768580b

2 files changed

Lines changed: 39 additions & 48 deletions

File tree

src/diffpy/cmipdf/pdfcontribution.py

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
__all__ = ["PDFContribution"]
2323

24+
from diffpy.cmipdf.debyepdfgenerator import DebyePDFGenerator
25+
from diffpy.cmipdf.pdfgenerator import PDFGenerator
2426
from diffpy.srfit.fitbase import FitContribution, Profile, ProfileParser
2527

2628

@@ -106,7 +108,7 @@ def __init__(self, name):
106108

107109
# Data methods
108110

109-
def loadData(self, datafile):
111+
def load_data(self, datafile):
110112
"""Load the data from a datafile.
111113
112114
Parameters
@@ -121,7 +123,7 @@ def loadData(self, datafile):
121123
self.profile.load_parsed_data(parser)
122124
return
123125

124-
def setCalculationRange(self, xmin=None, xmax=None, dx=None):
126+
def set_calculation_range(self, xmin=None, xmax=None, dx=None):
125127
"""Set epsilon-inclusive calculation range.
126128
127129
Adhere to the observed ``xobs`` points when ``dx`` is the same
@@ -166,42 +168,40 @@ def savetxt(self, fname, **kwargs):
166168

167169
# Phase methods
168170

169-
def addStructure(self, name, structure, periodic=True):
171+
def add_structure(self, structure, name="phase", periodic=True):
170172
"""Add a phase that goes into the PDF calculation.
171173
172174
Parameters
173175
----------
174-
name
176+
structure : Structure object
177+
`diffpy.structure.Structure`, `pyobjcryst.crystal.Crystal` or
178+
`pyobjcryst.molecule.Molecule` instance.
179+
name : str, optional
175180
A name to give the generator that will manage the PDF
176181
calculation from the passed structure. The adapted
177182
structure will be accessible via the name "phase" as an
178183
attribute of the generator, e.g.
179184
contribution.name.phase, where 'contribution' is this
180185
contribution and 'name' is passed name.
181-
(default), then the name will be set as "phase".
182-
structure
183-
diffpy.structure.Structure, pyobjcryst.crystal.Crystal or
184-
pyobjcryst.molecule.Molecule instance. Default None.
185-
periodic
186-
The structure should be treated as periodic. If this is
186+
Default is `"phase"`.
187+
periodic : bool, optional
188+
The structure should be treated as periodic. If this is
187189
True (default), then a PDFGenerator will be used to
188190
calculate the PDF from the phase. Otherwise, a
189191
DebyePDFGenerator will be used. Note that some structures
190192
do not support periodicity, in which case this may be
191193
ignored.
192194
193195
194-
Returns the new phase (ParameterSet appropriate for what was passed in
195-
structure.)
196+
Returns
197+
-------
198+
The new phase (ParameterSet appropriate for what was passed in
199+
structure.)
196200
"""
197201
# Based on periodic, create the proper generator.
198202
if periodic:
199-
from diffpy.cmipdf.pdfgenerator import PDFGenerator
200-
201203
gen = PDFGenerator(name)
202204
else:
203-
from diffpy.cmipdf.debyepdfgenerator import DebyePDFGenerator
204-
205205
gen = DebyePDFGenerator(name)
206206

207207
# Set up the generator
@@ -210,49 +210,45 @@ def addStructure(self, name, structure, periodic=True):
210210

211211
return gen.phase
212212

213-
def addPhase(self, name, parset, periodic=True):
214-
"""Add a phase that goes into the PDF calculation.
213+
def add_structure_from_parset(self, parset, name, periodic=True):
214+
"""Add a phase that goes into the PDF calculation from a
215+
ParameterSet.
215216
216217
Parameters
217218
----------
218-
name
219-
A name to give the generator that will manage the PDF
219+
parset : SrealParSet object
220+
A SrRealParSet that holds the structural information.
221+
This can be used to share the phase between multiple
222+
BasePDFGenerators, and have the changes in one reflect in
223+
another.
224+
name : str
225+
The name to give the generator that will manage the PDF
220226
calculation from the passed parameter phase. The parset
221227
will be accessible via the name "phase" as an attribute
222228
of the generator, e.g., contribution.name.phase, where
223229
'contribution' is this contribution and 'name' is passed
224230
name.
225-
parset
226-
A SrRealParSet that holds the structural information.
227-
This can be used to share the phase between multiple
228-
BasePDFGenerators, and have the changes in one reflect in
229-
another.
230-
periodic
231+
periodic : bool, optional
231232
The structure should be treated as periodic. If this is
232233
True (default), then a PDFGenerator will be used to
233234
calculate the PDF from the phase. Otherwise, a
234235
DebyePDFGenerator will be used. Note that some structures
235236
do not support periodicity, in which case this may be
236237
ignored.
237238
238-
239-
Returns the new phase (ParameterSet appropriate for what was passed in
240-
structure.)
239+
Returns
240+
-------
241+
The new phase (ParameterSet appropriate for what was passed in
242+
parset.)
241243
"""
242244
# Based on periodic, create the proper generator.
243245
if periodic:
244-
from diffpy.cmipdf.pdfgenerator import PDFGenerator
245-
246246
gen = PDFGenerator(name)
247247
else:
248-
from diffpy.cmipdf.debyepdfgenerator import DebyePDFGenerator
249-
250248
gen = DebyePDFGenerator(name)
251-
252249
# Set up the generator
253250
gen.set_structure_from_parset(parset, periodic)
254251
self._setup_generator(gen)
255-
256252
return gen.phase
257253

258254
def _setup_generator(self, gen):

tests/test_pdf.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ def test_setQmax():
210210
"""Check PDFContribution.setQmax()"""
211211
pc = PDFContribution("pdf")
212212
pc.set_qmax(21)
213-
pc.addStructure("empty", Structure())
213+
pc.add_structure(Structure(), name="empty")
214214
assert 21 == pc.empty.get_qmax()
215215
pc.set_qmax(22)
216216
assert 22 == pc.get_qmax()
@@ -228,7 +228,7 @@ def test_get_qmax():
228228
assert 17 == pc1.get_qmax()
229229
# (2) contribution metadata
230230
pc2 = PDFContribution("pdf")
231-
pc2.addStructure("empty", Structure())
231+
pc2.add_structure(Structure(), name="empty")
232232
pc2.empty.set_qmax(18)
233233
assert 18 == pc2.get_qmax()
234234
# (3) profile metadata
@@ -242,17 +242,12 @@ def test_savetxt(
242242
diffpy_structure_available, diffpy_srreal_available, datafile
243243
):
244244
"check PDFContribution.savetxt()"
245-
if not diffpy_structure_available:
246-
pytest.skip("diffpy.structure package not available")
247245
from diffpy.structure import Structure
248246

249-
if not diffpy_srreal_available:
250-
pytest.skip("diffpy.srreal package not available")
251-
252247
pc = PDFContribution("pdf")
253-
pc.loadData(datafile("si-q27r60-xray.gr"))
254-
pc.setCalculationRange(0, 10)
255-
pc.addStructure("empty", Structure())
248+
pc.load_data(datafile("si-q27r60-xray.gr"))
249+
pc.set_calculation_range(0, 10)
250+
pc.add_structure(Structure(), name="empty")
256251
fp = io.BytesIO()
257252
with pytest.raises(SrFitError):
258253
pc.savetxt(fp)
@@ -269,13 +264,13 @@ def test_pickling(
269264
):
270265
"validate PDFContribution.residual() after pickling."
271266
pc = PDFContribution("pdf")
272-
pc.loadData(datafile("ni-q27r100-neutron.gr"))
267+
pc.load_data(datafile("ni-q27r100-neutron.gr"))
273268
ciffile = datafile("ni.cif")
274269
cif_path = str(ciffile)
275270
ni = loadStructure(cif_path)
276271
ni.Uisoequiv = 0.003
277-
pc.addStructure("ni", ni)
278-
pc.setCalculationRange(0, 10)
272+
pc.add_structure(ni, name="ni")
273+
pc.set_calculation_range(0, 10)
279274
pc2 = pickle.loads(pickle.dumps(pc))
280275
res0 = pc.residual()
281276
assert numpy.array_equal(res0, pc2.residual())

0 commit comments

Comments
 (0)