File: test_util.py

package info (click to toggle)
python-fitsio 1.3.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,020 kB
  • sloc: python: 7,963; ansic: 3,962; makefile: 10
file content (78 lines) | stat: -rw-r--r-- 2,542 bytes parent folder | download
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
import math
import numpy as np

import pytest

from ..util import (
    _nonfinite_as_cfitsio_floating_null_value,
    cfitsio_version,
    _FLOATING_NULL_VALUE,
)

CFITSIO_VERSION = cfitsio_version(asfloat=True)
DTYPES = ['u1', 'i1', 'u2', 'i2', '<u4', 'i4', 'i8', '>f4', 'f8']
if CFITSIO_VERSION > 3.44:
    DTYPES += ["u8"]


@pytest.mark.parametrize("dtype", DTYPES)
@pytest.mark.parametrize("with_nan", [True, False])
@pytest.mark.parametrize("hdu_is_compressed", [True, False])
def test_nonfinite_as_cfitsio_floating_null_value(
    dtype, with_nan, hdu_is_compressed
):
    data = np.arange(5 * 20, dtype=dtype).reshape(5, 20)
    if "f" in dtype and with_nan:
        data[3, 13] = np.nan
        data[1, 7] = np.inf
        data[1, 9] = -np.inf

    with _nonfinite_as_cfitsio_floating_null_value(
        data, hdu_is_compressed
    ) as (
        nan_data,
        any_nan,
    ):
        if with_nan and "f" in dtype and hdu_is_compressed:
            assert any_nan
            assert not np.any(np.isnan(nan_data))
            msk = ~np.isfinite(data)
            np.testing.assert_array_equal(data[msk], _FLOATING_NULL_VALUE)
            np.testing.assert_array_equal(data[~msk], nan_data[~msk])
        else:
            assert not any_nan
            np.testing.assert_array_equal(nan_data, data)

    if with_nan and "f" in dtype:
        np.testing.assert_array_equal(data[3, 13], np.nan)
        np.testing.assert_array_equal(data[1, 7], np.inf)
        np.testing.assert_array_equal(data[1, 9], -np.inf)


def test_cfitsio_floating_null_value_equal_inf():
    assert np.float64(np.inf) == _FLOATING_NULL_VALUE
    assert np.float32(np.inf) == _FLOATING_NULL_VALUE
    assert np.inf == _FLOATING_NULL_VALUE
    assert math.inf == _FLOATING_NULL_VALUE


def test_nonfinite_as_cfitsio_floating_null_value_with_exception():
    data = np.arange(5 * 20, dtype=np.float32).reshape(5, 20)
    data[3, 13] = np.nan
    data[1, 7] = np.inf
    data[1, 9] = -np.inf

    with pytest.raises(RuntimeError):
        with _nonfinite_as_cfitsio_floating_null_value(data, True) as (
            nan_data,
            any_nan,
        ):
            assert any_nan
            assert not np.any(np.isnan(nan_data))
            msk = ~np.isfinite(data)
            np.testing.assert_array_equal(data[msk], _FLOATING_NULL_VALUE)
            np.testing.assert_array_equal(data[~msk], nan_data[~msk])
            raise RuntimeError("Exception raised while data is modified!")

    assert data[1, 7] == np.inf
    assert data[1, 9] == -np.inf