|
4 | 4 | import pytest
|
5 | 5 | from freezegun import freeze_time
|
6 | 6 |
|
7 |
| -from diffpy.utils.scattering_objects.diffraction_objects import DiffractionObject |
| 7 | +from diffpy.utils.scattering_objects.diffraction_objects import DiffractionObject, wavelength_warning_emsg |
8 | 8 |
|
9 | 9 | params = [
|
10 | 10 | ( # Default
|
@@ -231,6 +231,155 @@ def test_diffraction_objects_equality(inputs1, inputs2, expected):
|
231 | 231 | assert (diffraction_object1 == diffraction_object2) == expected
|
232 | 232 |
|
233 | 233 |
|
| 234 | +def _test_valid_diffraction_objects(actual_diffraction_object, function, expected_array): |
| 235 | + if actual_diffraction_object.wavelength is None: |
| 236 | + with pytest.warns(UserWarning) as warn_record: |
| 237 | + getattr(actual_diffraction_object, function)() |
| 238 | + assert str(warn_record[0].message) == wavelength_warning_emsg |
| 239 | + actual_array = getattr(actual_diffraction_object, function)() |
| 240 | + return np.allclose(actual_array, expected_array) |
| 241 | + |
| 242 | + |
| 243 | +params_q_to_tth = [ |
| 244 | + # UC1: User specified empty q values (without wavelength) |
| 245 | + ([None, [], []], [[]]), |
| 246 | + # UC2: User specified empty q values (with wavelength) |
| 247 | + ([4 * np.pi, [], []], [[]]), |
| 248 | + # UC3: User specified valid q values (without wavelength) |
| 249 | + ([None, [0, 0.2, 0.4, 0.6, 0.8, 1], [1, 2, 3, 4, 5, 6]], [[]]), |
| 250 | + # UC4: User specified valid q values (with wavelength) |
| 251 | + # expected tth values are 2*arcsin(q) in degrees |
| 252 | + ( |
| 253 | + [4 * np.pi, [0, 0.2, 0.4, 0.6, 0.8, 1], [1, 2, 3, 4, 5, 6]], |
| 254 | + [[0, 23.07392, 47.15636, 73.73980, 106.26020, 180]], |
| 255 | + ), |
| 256 | +] |
| 257 | + |
| 258 | + |
| 259 | +@pytest.mark.parametrize("inputs, expected", params_q_to_tth) |
| 260 | +def test_q_to_tth(inputs, expected): |
| 261 | + actual = DiffractionObject(wavelength=inputs[0]) |
| 262 | + actual.on_q = [inputs[1], inputs[2]] |
| 263 | + expected_tth = expected[0] |
| 264 | + assert _test_valid_diffraction_objects(actual, "q_to_tth", expected_tth) |
| 265 | + |
| 266 | + |
| 267 | +params_q_to_tth_bad = [ |
| 268 | + # UC1: user specified invalid q values that result in tth > 180 degrees |
| 269 | + ( |
| 270 | + [4 * np.pi, [0.2, 0.4, 0.6, 0.8, 1, 1.2], [1, 2, 3, 4, 5, 6]], |
| 271 | + [ |
| 272 | + ValueError, |
| 273 | + "The supplied q-array and wavelength will result in an impossible two-theta. " |
| 274 | + "Please check these values and re-instantiate the DiffractionObject with correct values.", |
| 275 | + ], |
| 276 | + ), |
| 277 | + # UC2: user specified a wrong wavelength that result in tth > 180 degrees |
| 278 | + ( |
| 279 | + [100, [0, 0.2, 0.4, 0.6, 0.8, 1], [1, 2, 3, 4, 5, 6]], |
| 280 | + [ |
| 281 | + ValueError, |
| 282 | + "The supplied q-array and wavelength will result in an impossible two-theta. " |
| 283 | + "Please check these values and re-instantiate the DiffractionObject with correct values.", |
| 284 | + ], |
| 285 | + ), |
| 286 | + # UC3: user specified a q array that does not match the length of intensity array (without wavelength) |
| 287 | + ( |
| 288 | + [None, [0, 0.2, 0.4, 0.6, 0.8, 1], [1, 2, 3, 4, 5]], |
| 289 | + [RuntimeError, "Please ensure q array and intensity array are of the same length."], |
| 290 | + ), |
| 291 | + # UC4: user specified a q array that does not match the length of intensity array (with wavelength) |
| 292 | + ( |
| 293 | + [4 * np.pi, [0, 0.2, 0.4, 0.6, 0.8, 1], [1, 2, 3, 4, 5]], |
| 294 | + [RuntimeError, "Please ensure q array and intensity array are of the same length."], |
| 295 | + ), |
| 296 | + # UC5: user specified a non-numeric value in q array (without wavelength) |
| 297 | + ( |
| 298 | + [None, [0, 0.2, 0.4, 0.6, 0.8, "invalid"], [1, 2, 3, 4, 5, 6]], |
| 299 | + [TypeError, "Invalid value found in q array. Please ensure all values are numeric."], |
| 300 | + ), |
| 301 | + # UC5: user specified a non-numeric value in q array (with wavelength) |
| 302 | + ( |
| 303 | + [4 * np.pi, [0, 0.2, 0.4, 0.6, 0.8, "invalid"], [1, 2, 3, 4, 5, 6]], |
| 304 | + [TypeError, "Invalid value found in q array. Please ensure all values are numeric."], |
| 305 | + ), |
| 306 | +] |
| 307 | + |
| 308 | + |
| 309 | +@pytest.mark.parametrize("inputs, expected", params_q_to_tth_bad) |
| 310 | +def test_q_to_tth_bad(inputs, expected): |
| 311 | + actual = DiffractionObject(wavelength=inputs[0]) |
| 312 | + actual.on_q = [inputs[1], inputs[2]] |
| 313 | + with pytest.raises(expected[0], match=expected[1]): |
| 314 | + actual.q_to_tth() |
| 315 | + |
| 316 | + |
| 317 | +params_tth_to_q = [ |
| 318 | + # UC1: User specified empty tth values (without wavelength) |
| 319 | + ([None, [], []], [[]]), |
| 320 | + # UC2: User specified empty tth values (with wavelength) |
| 321 | + ([4 * np.pi, [], []], [[]]), |
| 322 | + # UC3: User specified valid tth values between 0-180 degrees (without wavelength) |
| 323 | + ( |
| 324 | + [None, [0, 30, 60, 90, 120, 180], [1, 2, 3, 4, 5, 6]], |
| 325 | + [[]], |
| 326 | + ), |
| 327 | + # UC4: User specified valid tth values between 0-180 degrees (with wavelength) |
| 328 | + # expected q vales are sin15, sin30, sin45, sin60, sin90 |
| 329 | + ([4 * np.pi, [0, 30, 60, 90, 120, 180], [1, 2, 3, 4, 5, 6]], [[0, 0.258819, 0.5, 0.707107, 0.866025, 1]]), |
| 330 | +] |
| 331 | + |
| 332 | + |
| 333 | +@pytest.mark.parametrize("inputs, expected", params_tth_to_q) |
| 334 | +def test_tth_to_q(inputs, expected): |
| 335 | + actual = DiffractionObject(wavelength=inputs[0]) |
| 336 | + actual.on_tth = [inputs[1], inputs[2]] |
| 337 | + expected_q = expected[0] |
| 338 | + assert _test_valid_diffraction_objects(actual, "tth_to_q", expected_q) |
| 339 | + |
| 340 | + |
| 341 | +params_tth_to_q_bad = [ |
| 342 | + # UC1: user specified an invalid tth value of > 180 degrees (without wavelength) |
| 343 | + ( |
| 344 | + [None, [0, 30, 60, 90, 120, 181], [1, 2, 3, 4, 5, 6]], |
| 345 | + [ValueError, "Two theta exceeds 180 degrees. Please check the input values for errors."], |
| 346 | + ), |
| 347 | + # UC2: user specified an invalid tth value of > 180 degrees (with wavelength) |
| 348 | + ( |
| 349 | + [4 * np.pi, [0, 30, 60, 90, 120, 181], [1, 2, 3, 4, 5, 6]], |
| 350 | + [ValueError, "Two theta exceeds 180 degrees. Please check the input values for errors."], |
| 351 | + ), |
| 352 | + # UC3: user specified a two theta array that does not match the length of intensity array (without wavelength) |
| 353 | + ( |
| 354 | + [None, [0, 30, 60, 90, 120], [1, 2, 3, 4, 5, 6]], |
| 355 | + [RuntimeError, "Please ensure two theta array and intensity array are of the same length."], |
| 356 | + ), |
| 357 | + # UC4: user specified a two theta array that does not match the length of intensity array (with wavelength) |
| 358 | + ( |
| 359 | + [4 * np.pi, [0, 30, 60, 90, 120], [1, 2, 3, 4, 5, 6]], |
| 360 | + [RuntimeError, "Please ensure two theta array and intensity array are of the same length."], |
| 361 | + ), |
| 362 | + # UC5: user specified a non-numeric value in two theta array (without wavelength) |
| 363 | + ( |
| 364 | + [None, [0, 30, 60, 90, 120, "invalid"], [1, 2, 3, 4, 5, 6]], |
| 365 | + [TypeError, "Invalid value found in two theta array. Please ensure all values are numeric."], |
| 366 | + ), |
| 367 | + # UC6: user specified a non-numeric value in two theta array (with wavelength) |
| 368 | + ( |
| 369 | + [4 * np.pi, [0, 30, 60, 90, 120, "invalid"], [1, 2, 3, 4, 5, 6]], |
| 370 | + [TypeError, "Invalid value found in two theta array. Please ensure all values are numeric."], |
| 371 | + ), |
| 372 | +] |
| 373 | + |
| 374 | + |
| 375 | +@pytest.mark.parametrize("inputs, expected", params_tth_to_q_bad) |
| 376 | +def test_tth_to_q_bad(inputs, expected): |
| 377 | + actual = DiffractionObject(wavelength=inputs[0]) |
| 378 | + actual.on_tth = [inputs[1], inputs[2]] |
| 379 | + with pytest.raises(expected[0], match=expected[1]): |
| 380 | + actual.tth_to_q() |
| 381 | + |
| 382 | + |
234 | 383 | def test_dump(tmp_path, mocker):
|
235 | 384 | x, y = np.linspace(0, 5, 6), np.linspace(0, 5, 6)
|
236 | 385 | directory = Path(tmp_path)
|
|
0 commit comments