Skip to content

Commit 49544af

Browse files
committed
In SFAverage allow string type as sftb argument.
If string use it to create ScatteringFactorTable of the specified type. Otherwise assume it is already a ScatteringFactorTable instance.
1 parent 52eb53d commit 49544af

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

Diff for: diffpy/srreal/sfaverage.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,10 @@ def fromStructure(cls, stru, sftb, q=0):
8686
The structure object that stores the atom species and their
8787
occupancies. Can be any type with a registered conversion
8888
to the StructureAdapter class.
89-
sftb : ScatteringFactorTable
89+
sftb : ScatteringFactorTable or str
9090
The ScatteringFactorTable object for looking up the values.
91+
When string use `ScatteringFactorTable.createByType` to create
92+
a new lookup table of the specified type.
9193
q : float or NumPy array (optional)
9294
The Q value in inverse Angstroms for which to lookup
9395
the scattering factor values.
@@ -127,8 +129,10 @@ def fromComposition(cls, composition, sftb, q=0):
127129
composition : dictionary or a list of (symbol, amount) pairs.
128130
The chemical composition for evaluating the average. Atom
129131
symbols may repeat when it is a list of (symbol, amount) pairs.
130-
sftb : ScatteringFactorTable
132+
sftb : ScatteringFactorTable or str
131133
The ScatteringFactorTable object for looking up the values.
134+
When string use `ScatteringFactorTable.createByType` to create
135+
a new lookup table of the specified type.
132136
q : float or NumPy array (optional)
133137
The Q value in inverse Angstroms for which to lookup
134138
the scattering factor values.
@@ -138,6 +142,7 @@ def fromComposition(cls, composition, sftb, q=0):
138142
SFAverage
139143
The calculated scattering factor averages.
140144
"""
145+
from diffpy.srreal.scatteringfactortable import ScatteringFactorTable
141146
sfa = cls()
142147
sfa.composition = {}
143148
if isinstance(composition, dict):
@@ -149,8 +154,11 @@ def fromComposition(cls, composition, sftb, q=0):
149154
sfa.composition[smbl] += cnt
150155
sfa.f1sum = 0.0 * q
151156
sfa.f2sum = 0.0 * q
157+
# resolve the lookup table object `tb`
158+
tb = (sftb if not isinstance(sftb, basestring)
159+
else ScatteringFactorTable.createByType(sftb))
152160
for smbl, cnt in sfa.composition.items():
153-
sfq = sftb.lookup(smbl, q)
161+
sfq = tb.lookup(smbl, q)
154162
sfa.f1sum += cnt * sfq
155163
sfa.f2sum += cnt * sfq**2
156164
sfa.count += cnt

Diff for: diffpy/srreal/tests/testsfaverage.py

+7
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,15 @@ def test_fromStructure_CdSe(self):
4646
self.assertEqual(sfavg.f2sum, sfavg2.f2sum[0])
4747
sfavg3 = SFAverage.fromStructure(cdse, self.sftn, qa)
4848
self.assertEqual(sfavg3.f1sum[0], sfavg3.f1sum[-1])
49+
sfavg4 = SFAverage.fromStructure(cdse, 'N', qa)
50+
self.assertTrue(numpy.array_equal(sfavg3.f1sum, sfavg4.f1sum))
51+
self.assertTrue(numpy.array_equal(sfavg3.f2sum, sfavg4.f2sum))
52+
sfavg5 = SFAverage.fromStructure(cdse, 'EN', qa)
53+
self.assertFalse(numpy.array_equal(sfavg3.f1sum, sfavg5.f1sum))
4954
self.assertRaises(TypeError, SFAverage.fromStructure,
5055
'notastructure', self.sftx)
56+
self.assertRaises(ValueError, SFAverage.fromStructure,
57+
cdse, 'invalid')
5158
return
5259

5360

0 commit comments

Comments
 (0)