forked from diffpy/diffpy.srfit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
208 lines (160 loc) · 4.95 KB
/
conftest.py
File metadata and controls
208 lines (160 loc) · 4.95 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
import importlib.resources
import logging
import sys
from functools import lru_cache
import pytest
import six
from numpy import linspace, pi, sin
import diffpy.srfit.equation.literals as literals
from diffpy.srfit.fitbase import FitContribution, FitRecipe, Profile
logger = logging.getLogger(__name__)
@lru_cache()
def has_sas():
try:
import sas
import sasmodels
del sas
del sasmodels
return True
except ImportError:
return False
# diffpy.structure
@lru_cache()
def has_diffpy_structure():
try:
import diffpy.structure as m
del m
return True
except ImportError:
return False
logger.warning(
"Cannot import diffpy.structure, Structure tests skipped."
)
@lru_cache()
def has_pyobjcryst():
try:
import pyobjcryst as m
del m
return True
except ImportError:
return False
logger.warning("Cannot import pyobjcryst, pyobjcryst tests skipped.")
# diffpy.srreal
@lru_cache()
def has_diffpy_srreal():
try:
import diffpy.srreal.pdfcalculator as m
del m
return True
except ImportError:
return False
logger.warning("Cannot import diffpy.srreal, PDF tests skipped.")
@pytest.fixture(scope="session")
def sas_available():
return has_sas()
@pytest.fixture(scope="session")
def diffpy_srreal_available():
return has_diffpy_srreal()
@pytest.fixture(scope="session")
def pyobjcryst_available():
return has_pyobjcryst()
@pytest.fixture(scope="session")
def datafile():
"""Fixture to load a test data file from the testdata package directory."""
def _datafile(filename):
return importlib.resources.files("tests.testdata").joinpath(filename)
return _datafile
@pytest.fixture(scope="session")
def make_args():
def _makeArgs(num):
args = []
for i in range(num):
j = i + 1
args.append(literals.Argument(name="v%i" % j, value=j))
return args
return _makeArgs
@pytest.fixture(scope="session")
def noObserversInGlobalBuilders():
def _no_observers_in_global_builders():
"""True if no observer function leaks to global builder objects.
Ensure objects are not immortal due to a reference from static
value.
"""
from diffpy.srfit.equation.builder import _builders
rv = True
for n, b in _builders.items():
if b.literal and b.literal._observers:
rv = False
break
return rv
return _no_observers_in_global_builders()
@pytest.fixture(scope="session")
def capturestdout():
def _capturestdout(f, *args, **kwargs):
"""Capture the standard output from a call of function f."""
savestdout = sys.stdout
fp = six.StringIO()
try:
sys.stdout = fp
f(*args, **kwargs)
finally:
sys.stdout = savestdout
return fp.getvalue()
return _capturestdout
@pytest.fixture(scope="session")
def build_recipe_one_contribution():
"helper to build a simple recipe"
profile = Profile()
x = linspace(0, pi, 10)
y = sin(x)
profile.setObservedProfile(x, y)
contribution = FitContribution("c1")
contribution.set_profile(profile)
contribution.setEquation("A*sin(k*x + c)")
recipe = FitRecipe()
recipe.addContribution(contribution)
recipe.addVar(contribution.A, 1)
recipe.addVar(contribution.k, 1)
recipe.addVar(contribution.c, 1)
return recipe
@pytest.fixture(scope="session")
def build_recipe_two_contributions():
"helper to build a recipe with two contributions"
profile1 = Profile()
x = linspace(0, pi, 10)
y1 = sin(x)
profile1.setObservedProfile(x, y1)
contribution1 = FitContribution("c1")
contribution1.set_profile(profile1)
contribution1.setEquation("A*sin(k*x + c)")
profile2 = Profile()
y2 = 0.5 * sin(2 * x)
profile2.setObservedProfile(x, y2)
contribution2 = FitContribution("c2")
contribution2.set_profile(profile2)
contribution2.setEquation("B*sin(m*x + d)")
recipe = FitRecipe()
recipe.addContribution(contribution1)
recipe.addContribution(contribution2)
recipe.addVar(contribution1.A, 1)
recipe.addVar(contribution1.k, 1)
recipe.addVar(contribution1.c, 1)
recipe.addVar(contribution2.B, 0.5)
recipe.addVar(contribution2.m, 2)
recipe.addVar(contribution2.d, 0)
return recipe
@pytest.fixture
def temp_data_files(tmp_path):
"""
Temporary directory containing:
- data_with_meta.gr
- data_without_meta.dat
Each file contains a single line of data.
"""
file_with_meta = tmp_path / "gr_file.gr"
file_with_meta.write_text("1.0 2.0\n" "1.1 2.1\n" "1.2 2.2\n")
dat_file = tmp_path / "dat_file.dat"
dat_file.write_text("1.0 2.0\n" "1.1 2.1\n" "1.2 2.2\n")
cgr_file = tmp_path / "cgr_file.cgr"
cgr_file.write_text("1.0 2.0\n" "1.1 2.1\n" "1.2 2.2\n")
yield tmp_path