|
| 1 | +# SPDX-License-Identifier: BSD-3-Clause |
| 2 | +# Copyright (c) 2023 Scipp contributors (https://github.com/scipp) |
| 3 | +# @author Andrew R. McCluskey (arm61) |
| 4 | +import pytest |
| 5 | +import scipp as sc |
| 6 | + |
| 7 | +from essreflectometry.amor import tools |
| 8 | + |
| 9 | + |
| 10 | +def test_linlogspace_linear(): |
| 11 | + q_lin = tools.linlogspace( |
| 12 | + dim='qz', edges=[0.008, 0.08], scale='linear', num=50, unit='1/angstrom' |
| 13 | + ) |
| 14 | + expected = sc.linspace(dim='qz', start=0.008, stop=0.08, num=50, unit='1/angstrom') |
| 15 | + assert sc.allclose(q_lin, expected) |
| 16 | + |
| 17 | + |
| 18 | +def test_linlogspace_linear_list_input(): |
| 19 | + q_lin = tools.linlogspace( |
| 20 | + dim='qz', edges=[0.008, 0.08], unit='1/angstrom', scale=['linear'], num=[50] |
| 21 | + ) |
| 22 | + expected = sc.linspace(dim='qz', start=0.008, stop=0.08, num=50, unit='1/angstrom') |
| 23 | + assert sc.allclose(q_lin, expected) |
| 24 | + |
| 25 | + |
| 26 | +def test_linlogspace_log(): |
| 27 | + q_log = tools.linlogspace( |
| 28 | + dim='qz', edges=[0.008, 0.08], unit='1/angstrom', scale='log', num=50 |
| 29 | + ) |
| 30 | + expected = sc.geomspace(dim='qz', start=0.008, stop=0.08, num=50, unit='1/angstrom') |
| 31 | + assert sc.allclose(q_log, expected) |
| 32 | + |
| 33 | + |
| 34 | +def test_linlogspace_linear_log(): |
| 35 | + q_linlog = tools.linlogspace( |
| 36 | + dim='qz', |
| 37 | + edges=[0.008, 0.03, 0.08], |
| 38 | + unit='1/angstrom', |
| 39 | + scale=['linear', 'log'], |
| 40 | + num=[16, 20], |
| 41 | + ) |
| 42 | + exp_lin = sc.linspace(dim='qz', start=0.008, stop=0.03, num=16, unit='1/angstrom') |
| 43 | + exp_log = sc.geomspace(dim='qz', start=0.03, stop=0.08, num=21, unit='1/angstrom') |
| 44 | + expected = sc.concat([exp_lin, exp_log['qz', 1:]], 'qz') |
| 45 | + assert sc.allclose(q_linlog, expected) |
| 46 | + |
| 47 | + |
| 48 | +def test_linlogspace_log_linear(): |
| 49 | + q_loglin = tools.linlogspace( |
| 50 | + dim='qz', |
| 51 | + edges=[0.008, 0.03, 0.08], |
| 52 | + unit='1/angstrom', |
| 53 | + scale=['log', 'linear'], |
| 54 | + num=[16, 20], |
| 55 | + ) |
| 56 | + exp_log = sc.geomspace(dim='qz', start=0.008, stop=0.03, num=16, unit='1/angstrom') |
| 57 | + exp_lin = sc.linspace(dim='qz', start=0.03, stop=0.08, num=21, unit='1/angstrom') |
| 58 | + expected = sc.concat([exp_log, exp_lin['qz', 1:]], 'qz') |
| 59 | + assert sc.allclose(q_loglin, expected) |
| 60 | + |
| 61 | + |
| 62 | +def test_linlogspace_linear_log_linear(): |
| 63 | + q_linloglin = tools.linlogspace( |
| 64 | + dim='qz', |
| 65 | + edges=[0.008, 0.03, 0.08, 0.12], |
| 66 | + unit='1/angstrom', |
| 67 | + scale=['linear', 'log', 'linear'], |
| 68 | + num=[16, 20, 10], |
| 69 | + ) |
| 70 | + exp_lin = sc.linspace(dim='qz', start=0.008, stop=0.03, num=16, unit='1/angstrom') |
| 71 | + exp_log = sc.geomspace(dim='qz', start=0.03, stop=0.08, num=21, unit='1/angstrom') |
| 72 | + exp_lin2 = sc.linspace(dim='qz', start=0.08, stop=0.12, num=11, unit='1/angstrom') |
| 73 | + expected = sc.concat([exp_lin, exp_log['qz', 1:], exp_lin2['qz', 1:]], 'qz') |
| 74 | + assert sc.allclose(q_linloglin, expected) |
| 75 | + |
| 76 | + |
| 77 | +def test_linlogspace_bad_input(): |
| 78 | + with pytest.raises(ValueError): |
| 79 | + _ = tools.linlogspace( |
| 80 | + dim='qz', |
| 81 | + edges=[0.008, 0.03, 0.08, 0.12], |
| 82 | + unit='1/angstrom', |
| 83 | + scale=['linear', 'log'], |
| 84 | + num=[16, 20], |
| 85 | + ) |
0 commit comments