CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
test_constants.py91 linesDownload Raw Back to tests
1import pytest
2
3import scipy.constants as sc
4from scipy.conftest import array_api_compatible
5from scipy._lib._array_api_no_0d import xp_assert_equal, xp_assert_close
6from numpy.testing import assert_allclose
7
8
9pytestmark = [array_api_compatible, pytest.mark.usefixtures("skip_xp_backends")]
10skip_xp_backends = pytest.mark.skip_xp_backends
11
12
13class TestConvertTemperature:
14    def test_convert_temperature(self, xp):
15        xp_assert_equal(sc.convert_temperature(xp.asarray(32.), 'f', 'Celsius'),
16                        xp.asarray(0.0))
17        xp_assert_equal(sc.convert_temperature(xp.asarray([0., 0.]),
18                                               'celsius', 'Kelvin'),
19                        xp.asarray([273.15, 273.15]))
20        xp_assert_equal(sc.convert_temperature(xp.asarray([0., 0.]), 'kelvin', 'c'),
21                        xp.asarray([-273.15, -273.15]))
22        xp_assert_equal(sc.convert_temperature(xp.asarray([32., 32.]), 'f', 'k'),
23                        xp.asarray([273.15, 273.15]))
24        xp_assert_equal(sc.convert_temperature(xp.asarray([273.15, 273.15]),
25                                               'kelvin', 'F'),
26                        xp.asarray([32., 32.]))
27        xp_assert_equal(sc.convert_temperature(xp.asarray([0., 0.]), 'C', 'fahrenheit'),
28                        xp.asarray([32., 32.]))
29        xp_assert_close(sc.convert_temperature(xp.asarray([0., 0.], dtype=xp.float64),
30                                               'c', 'r'),
31                        xp.asarray([491.67, 491.67], dtype=xp.float64),
32                        rtol=0., atol=1e-13)
33        xp_assert_close(sc.convert_temperature(xp.asarray([491.67, 491.67],
34                                                        dtype=xp.float64),
35                                               'Rankine', 'C'),
36                        xp.asarray([0., 0.], dtype=xp.float64), rtol=0., atol=1e-13)
37        xp_assert_close(sc.convert_temperature(xp.asarray([491.67, 491.67],
38                                                        dtype=xp.float64),
39                                               'r', 'F'),
40                        xp.asarray([32., 32.], dtype=xp.float64), rtol=0., atol=1e-13)
41        xp_assert_close(sc.convert_temperature(xp.asarray([32., 32.], dtype=xp.float64),
42                                               'fahrenheit', 'R'),
43                        xp.asarray([491.67, 491.67], dtype=xp.float64),
44                        rtol=0., atol=1e-13)
45        xp_assert_close(sc.convert_temperature(xp.asarray([273.15, 273.15],
46                                                        dtype=xp.float64),
47                                               'K', 'R'),
48                        xp.asarray([491.67, 491.67], dtype=xp.float64),
49                        rtol=0., atol=1e-13)
50        xp_assert_close(sc.convert_temperature(xp.asarray([491.67, 0.],
51                                                          dtype=xp.float64),
52                                               'rankine', 'kelvin'),
53                        xp.asarray([273.15, 0.], dtype=xp.float64), rtol=0., atol=1e-13)
54
55    @skip_xp_backends(np_only=True, reason='Python list input uses NumPy backend')
56    def test_convert_temperature_array_like(self):
57        assert_allclose(sc.convert_temperature([491.67, 0.], 'rankine', 'kelvin'),
58                        [273.15, 0.], rtol=0., atol=1e-13)
59
60
61    @skip_xp_backends(np_only=True, reason='Python int input uses NumPy backend')
62    def test_convert_temperature_errors(self, xp):
63        with pytest.raises(NotImplementedError, match="old_scale="):
64            sc.convert_temperature(1, old_scale="cheddar", new_scale="kelvin")
65        with pytest.raises(NotImplementedError, match="new_scale="):
66            sc.convert_temperature(1, old_scale="kelvin", new_scale="brie")
67
68
69class TestLambdaToNu:
70    def test_lambda_to_nu(self, xp):
71        xp_assert_equal(sc.lambda2nu(xp.asarray([sc.speed_of_light, 1])),
72                        xp.asarray([1, sc.speed_of_light]))
73
74
75    @skip_xp_backends(np_only=True, reason='Python list input uses NumPy backend')
76    def test_lambda_to_nu_array_like(self, xp):
77        assert_allclose(sc.lambda2nu([sc.speed_of_light, 1]),
78                        [1, sc.speed_of_light])
79
80
81class TestNuToLambda:
82    def test_nu_to_lambda(self, xp):
83        xp_assert_equal(sc.nu2lambda(xp.asarray([sc.speed_of_light, 1])),
84                        xp.asarray([1, sc.speed_of_light]))
85
86    @skip_xp_backends(np_only=True, reason='Python list input uses NumPy backend')
87    def test_nu_to_lambda_array_like(self, xp):
88        assert_allclose(sc.nu2lambda([sc.speed_of_light, 1]),
89                        [1, sc.speed_of_light])
90
91 
Aluode/PerceptionLabPortable · CoolFace