forked from diffpy/pyobjcryst
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_powderpattern.py
More file actions
330 lines (279 loc) · 12.1 KB
/
test_powderpattern.py
File metadata and controls
330 lines (279 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/env python
##############################################################################
#
# pyobjcryst Complex Modeling Initiative
# (c) 2018 Brookhaven Science Associates,
# Brookhaven National Laboratory.
# All rights reserved.
#
# File coded by: Pavol Juhas
#
# See AUTHORS.txt for a list of people who contributed.
# See LICENSE.txt for license information.
#
##############################################################################
"""Unit tests for pyobjcryst.powderpattern (with indexing &"""
import unittest
import numpy as np
import pytest
from pyobjcryst import ObjCrystException
from pyobjcryst.indexing import CrystalCentering, CrystalSystem, quick_index
from pyobjcryst.powderpattern import PowderPattern, SpaceGroupExplorer
from pyobjcryst.radiation import RadiationType, WavelengthType
from pyobjcryst.reflectionprofile import ReflectionProfileType
# ----------------------------------------------------------------------------
class TestRoutines(unittest.TestCase):
pass
# def test_CreatePowderPatternFromCIF(self): assert False
# End of class TestRoutines
# ----------------------------------------------------------------------------
class TestPowderPattern(unittest.TestCase):
@pytest.fixture(autouse=True)
def prepare_fixture(self, loadcifdata):
self.loadcifdata = loadcifdata
def setUp(self):
self.pp = PowderPattern()
return
def test___init__(self):
self.assertEqual(0, self.pp.GetNbPowderPatternComponent())
self.assertEqual(0, len(self.pp.GetPowderPatternX()))
self.assertEqual(0, len(self.pp.GetPowderPatternObs()))
self.assertEqual(0, len(self.pp.GetPowderPatternCalc()))
return
# def test_AddPowderPatternBackground(self): assert False
# def test_AddPowderPatternDiffraction(self): assert False
# def test_FitScaleFactorForIntegratedR(self): assert False
# def test_FitScaleFactorForIntegratedRw(self): assert False
# def test_FitScaleFactorForR(self): assert False
# def test_FitScaleFactorForRw(self): assert False
# def test_GetMaxSinThetaOvLambda(self): assert False
# def test_GetNbPowderPatternComponent(self): assert False
# def test_GetPowderPatternCalc(self): assert False
# def test_GetPowderPatternComponent(self): assert False
def test_GetPowderPatternObs(self):
self.assertTrue(np.array_equal([], self.pp.GetPowderPatternObs()))
return
def test_GetPowderPatternX(self):
self.assertTrue(np.array_equal([], self.pp.GetPowderPatternX()))
return
# def test_GetScaleFactor(self): assert False
# def test_ImportPowderPattern2ThetaObs(self): assert False
# def test_ImportPowderPattern2ThetaObsSigma(self): assert False
# def test_ImportPowderPatternFullprof(self): assert False
# def test_ImportPowderPatternFullprof4(self): assert False
# def test_ImportPowderPatternGSAS(self): assert False
# def test_ImportPowderPatternILL_D1A5(self): assert False
# def test_ImportPowderPatternMultiDetectorLLBG42(self): assert False
# def test_ImportPowderPatternPSI_DMC(self): assert False
# def test_ImportPowderPatternSietronicsCPI(self): assert False
# def test_ImportPowderPatternTOF_ISIS_XYSigma(self): assert False
# def test_ImportPowderPatternXdd(self): assert False
# def test_Prepare(self): assert False
# def test_SetEnergy(self): assert False
# def test_SetMaxSinThetaOvLambda(self): assert False
def test_SetPowderPatternObs(self):
pp = self.pp
obs = np.array([1.0, 3.0, 7.0])
self.assertRaises(ObjCrystException, pp.SetPowderPatternObs, obs)
pp.SetPowderPatternPar(0, 0.5, 3)
pp.SetPowderPatternObs(obs)
self.assertTrue(np.array_equal(obs, pp.GetPowderPatternObs()))
pp.SetPowderPatternObs(list(obs)[::-1])
self.assertTrue(np.array_equal(obs[::-1], pp.GetPowderPatternObs()))
return
def test_SetPowderPatternObsSigma(self):
pp = self.pp
obs = np.array([1.0, 3.0, 7.0])
sig = np.array([1.0, 2.0, 3.0])
self.assertRaises(ObjCrystException, pp.SetPowderPatternObsSigma, sig)
pp.SetPowderPatternPar(0, 0.5, 3)
pp.SetPowderPatternObs(obs)
pp.SetPowderPatternObsSigma(sig)
self.assertTrue(np.array_equal(sig, pp.GetPowderPatternObsSigma()))
return
def test_SetPowderPatternPar(self):
pp = self.pp
pp.SetPowderPatternPar(0, 0.25, 5)
tth = np.linspace(0, 1, 5)
self.assertTrue(np.array_equal(tth, pp.GetPowderPatternX()))
pp.SetPowderPatternPar(0, 0.25, 0)
self.assertEqual(0, len(pp.GetPowderPatternX()))
return
def test_SetPowderPatternX(self):
pp = self.pp
tth0 = np.array([0, 0.1, 0.3, 0.7])
tth1 = np.array([0, 0.1, 0.3, 0.7, 0.75, 0.77, 0.80])
pp.SetPowderPatternX(tth0)
self.assertTrue(np.array_equal(tth0, pp.GetPowderPatternX()))
pp.SetPowderPatternX(list(tth1))
self.assertTrue(np.array_equal(tth1, pp.GetPowderPatternX()))
pp.SetPowderPatternX(tuple(2 * tth0))
self.assertTrue(np.array_equal(2 * tth0, pp.GetPowderPatternX()))
return
def test_SetPowderPatternXempty(self):
pp = self.pp
pp.SetPowderPatternX([0, 0.1, 0.2, 0.3])
pp.SetPowderPatternX([])
self.assertEqual(0, len(pp.GetPowderPatternX()))
return
def test_SetWavelength(self):
pp = self.pp
pp.SetWavelength(1.2345)
self.assertAlmostEqual(pp.GetWavelength(), 1.2345, places=4)
def test_SetWavelengthXrayTube(self):
pp = self.pp
t = pp.GetRadiation().GetWavelengthType()
w = pp.GetWavelength()
pp.SetWavelength("Cu")
self.assertAlmostEqual(pp.GetWavelength(), 1.5418, places=4)
self.assertEqual(
pp.GetRadiation().GetWavelengthType(),
WavelengthType.WAVELENGTH_ALPHA12,
)
pp.GetRadiation().SetWavelengthType(t)
pp.SetWavelength(w)
def test_SetRadiationType(self):
pp = self.pp
t = pp.GetRadiationType()
pp.SetRadiationType(RadiationType.RAD_NEUTRON)
self.assertEqual(pp.GetRadiationType(), RadiationType.RAD_NEUTRON)
pp.SetRadiationType(t)
def test_quick_fit(self):
c = self.loadcifdata("paracetamol.cif")
p = PowderPattern()
p.SetWavelength(0.7)
x = np.linspace(0, 40, 8001)
p.SetPowderPatternX(np.deg2rad(x))
p.SetPowderPatternObs(np.ones_like(x))
pd = p.AddPowderPatternDiffraction(c)
pd.SetReflectionProfilePar(
ReflectionProfileType.PROFILE_PSEUDO_VOIGT, 1e-6
)
# p.plot(hkl=True)
calc = p.GetPowderPatternCalc()
obs = np.random.poisson(calc * 1e5 / calc.max() + 50).astype(
np.float64
)
p.SetPowderPatternObs(obs)
p.SetMaxSinThetaOvLambda(0.3)
p.quick_fit_profile(auto_background=True, verbose=False, plot=False)
def test_peaklist_index(self):
c = self.loadcifdata("paracetamol.cif")
p = PowderPattern()
p.SetWavelength(0.7)
x = np.linspace(0, 40, 16001)
p.SetPowderPatternX(np.deg2rad(x))
p.SetPowderPatternObs(np.ones_like(x))
pd = p.AddPowderPatternDiffraction(c)
pd.SetReflectionProfilePar(
ReflectionProfileType.PROFILE_PSEUDO_VOIGT, 1e-7
)
# p.plot(hkl=True)
calc = p.GetPowderPatternCalc()
obs = np.random.poisson(calc * 1e6 / calc.max() + 50).astype(
np.float64
)
p.SetPowderPatternObs(obs)
p.SetMaxSinThetaOvLambda(0.2)
p.FitScaleFactorForIntegratedRw()
pl = p.FindPeaks()
ex = quick_index(pl, verbose=False)
sols = ex.GetSolutions()
self.assertGreater(len(sols), 0)
ruc = sols[0][0]
# Check lattice type
self.assertEqual(ruc.centering, CrystalCentering.LATTICE_P)
self.assertEqual(ruc.lattice, CrystalSystem.MONOCLINIC)
# Cell volume
self.assertAlmostEqual(
ruc.DirectUnitCell()[-1], c.GetVolume(), delta=5
)
def test_spacegroup_explorer(self):
c = self.loadcifdata("paracetamol.cif")
p = PowderPattern()
p.SetWavelength(0.7)
x = np.linspace(0, 40, 8001)
p.SetPowderPatternX(np.deg2rad(x))
p.SetPowderPatternObs(np.ones_like(x))
pd = p.AddPowderPatternDiffraction(c)
pd.SetReflectionProfilePar(
ReflectionProfileType.PROFILE_PSEUDO_VOIGT, 1e-6, 0, 0, 0, 0
)
# p.plot(hkl=True)
calc = p.GetPowderPatternCalc()
obs = np.random.poisson(calc * 1e6 / calc.max() + 50).astype(
np.float64
)
p.SetPowderPatternObs(obs)
# NB: with max(stol)=0.2 this fails and best result is P1
p.SetMaxSinThetaOvLambda(0.3)
# Do the profile optimisation in P1
pd.GetCrystal().GetSpaceGroup().ChangeSpaceGroup("P1")
p.FitScaleFactorForIntegratedRw()
p.quick_fit_profile(
auto_background=True, init_profile=False, verbose=False, plot=False
)
spgex = SpaceGroupExplorer(pd)
spgex.Run("P 1 21/c 1")
spgex.RunAll(verbose=False)
spg = spgex.GetScores()[0] # noqa F841
# This fails about XX% of the time (fit not converging well enough ?)
# self.assertEqual(spg.hermann_mauguin, 'P 1 21/c 1')
# if True: #spg.hermann_mauguin != 'P 1 21/c 1':
# print()
# for s in spgex.GetScores():
# print(s)
def test_update_nbrefl(self):
c = self.loadcifdata("paracetamol.cif")
p = PowderPattern()
p.SetWavelength(1.5)
x = np.linspace(0, 40, 4000)
p.SetPowderPatternX(np.deg2rad(x))
p.SetPowderPatternObs(np.ones_like(x))
pd = p.AddPowderPatternDiffraction(c)
p.GetPowderPatternCalc()
self.assertEqual(pd.GetNbRefl(), 89)
# Change lattice parameter, the reflection list is updated
# during the next powder pattern calculation
c.a *= 1.1
p.GetPowderPatternCalc()
self.assertEqual(pd.GetNbRefl(), 92)
# Change the spacegroup, the reflection list is updated
# during the next powder pattern calculation
c.GetSpaceGroup().ChangeSpaceGroup("P1")
p.GetPowderPatternCalc()
self.assertEqual(pd.GetNbRefl(), 187)
# def test_SetScaleFactor(self): assert False
# End of class TestPowderPattern
# ----------------------------------------------------------------------------
class TestPowderPatternComponent(unittest.TestCase):
pass
# def test___init__(self): assert False
# def test_GetParentPowderPattern(self): assert False
# End of class TestPowderPatternComponent
# ----------------------------------------------------------------------------
class TestPowderPatternBackground(unittest.TestCase):
pass
# def test___init__(self): assert False
# def test_FixParametersBeyondMaxresolution(self): assert False
# def test_GetPowderPatternCalc(self): assert False
# def test_ImportUserBackground(self): assert False
# def test_OptimizeBayesianBackground(self): assert False
# def test_SetInterpPoints(self): assert False
# End of class TestPowderPatternBackground
# ----------------------------------------------------------------------------
class TestPowderPatternDiffraction(unittest.TestCase):
pass
# def test___init__(self): assert False
# def test_ExtractLeBail(self): assert False
# def test_GetExtractionMode(self): assert False
# def test_GetNbReflBelowMaxSinThetaOvLambda(self): assert False
# def test_GetPowderPatternCalc(self): assert False
# def test_GetProfile(self): assert False
# def test_SetCrystal(self): assert False
# def test_SetExtractionMode(self): assert False
# def test_SetReflectionProfilePar(self): assert False
# End of class TestPowderPatternDiffraction
# ----------------------------------------------------------------------------
if __name__ == "__main__":
unittest.main()