forked from diffpy/diffpy.srfit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sas.py
More file actions
173 lines (151 loc) · 4.37 KB
/
test_sas.py
File metadata and controls
173 lines (151 loc) · 4.37 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
#!/usr/bin/env python
##############################################################################
#
# diffpy.srfit by DANSE Diffraction group
# Simon J. L. Billinge
# (c) 2010 The Trustees of Columbia University
# in the City of New York. All rights reserved.
#
# File coded by: Pavol Juhas
#
# See AUTHORS.txt for a list of people who contributed.
# See LICENSE_DANSE.txt for license information.
#
##############################################################################
"""Tests for sas package."""
import numpy
import pytest
from diffpy.srfit.sas import SASGenerator, SASParser, SASProfile
# ----------------------------------------------------------------------------
# FIXME: adjust sensitivity of the pytest.approx statements when ready to test
# with sasview installed.
def testParser(sas_available, datafile):
if not sas_available:
pytest.skip("sas package not available")
data = datafile("sas_ascii_test_1.txt")
parser = SASParser()
parser.parseFile(data)
x, y, dx, dy = parser.getData()
testx = numpy.array(
[
0.002618,
0.007854,
0.01309,
0.01832,
0.02356,
0.02879,
0.03402,
0.03925,
0.04448,
0.0497,
]
)
diff = testx - x
res = numpy.dot(diff, diff)
assert 0 == pytest.approx(res)
testy = numpy.array(
[
0.02198,
0.02201,
0.02695,
0.02645,
0.03024,
0.3927,
7.305,
17.43,
13.43,
8.346,
]
)
diff = testy - y
res = numpy.dot(diff, diff)
assert 0 == pytest.approx(res)
testdy = numpy.array(
[
0.002704,
0.001643,
0.002452,
0.001769,
0.001531,
0.1697,
1.006,
0.5351,
0.3677,
0.191,
]
)
diff = testdy - dy
res = numpy.dot(diff, diff)
assert 0 == pytest.approx(res)
testdx = numpy.array(
[
0.0004091,
0.005587,
0.005598,
0.005624,
0.005707,
0.005975,
0.006264,
0.006344,
0.006424,
0.006516,
]
)
diff = testdx - dx
res = numpy.dot(diff, diff)
assert 0 == pytest.approx(res)
return
# End of class TestSASParser
def test_generator(sas_available):
if not sas_available:
pytest.skip("sas package not available")
from sasmodels.sasview_model import find_model, load_standard_models
load_standard_models()
SphereModel = find_model("sphere")
model = SphereModel()
gen = SASGenerator("sphere", model)
for pname in model.params:
defval = model.getParam(pname)
par = gen.get(pname)
assert defval == par.getValue()
# Test setting values
par.setValue(1.0)
assert 1.0 == par.getValue()
assert 1.0 == model.getParam(pname)
par.setValue(defval)
assert defval == par.getValue()
assert defval == model.getParam(pname)
r = numpy.arange(1, 10, 0.1, dtype=float)
y = gen(r)
refy = model.evalDistribution(r)
diff = y - refy
res = numpy.dot(diff, diff)
assert 0 == pytest.approx(res)
return
def testGenerator2(sas_available, datafile):
if not sas_available:
pytest.skip("sas package not available")
from sasmodels.sasview_model import find_model, load_standard_models
load_standard_models()
EllipsoidModel = find_model("ellipsoid")
model = EllipsoidModel()
gen = SASGenerator("ellipsoid", model)
# Load the data using SAS tools
import sasdata.dataloader.loader as sas_dataloader
Loader = sas_dataloader.Loader
loader = Loader()
data = datafile("sas_ellipsoid_testdata.txt")
datainfo = loader.load(str(data))
profile = SASProfile(datainfo)
gen.setProfile(profile)
gen.scale.value = 1.0
gen.radius_polar.value = 20
gen.radius_equatorial.value = 400
gen.background.value = 0.01
y = gen(profile.xobs)
diff = profile.yobs - y
res = numpy.dot(diff, diff)
# FIXME: go back to default tolerance when we figure out why
# the models are not identical
assert 0 == pytest.approx(res, abs=1e-3)
return