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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# Copyright (c) 2024 Radio Astronomy Software Group
# Licensed under the 2-clause BSD License
"""Tests for helper utility functions."""

import numpy as np
import pytest

from pyuvdata import utils
from pyuvdata.parameter import UVParameter
from pyuvdata.testing import check_warnings


@pytest.mark.parametrize(
    "filename1,filename2,answer",
    [
        (["foo.uvh5"], ["bar.uvh5"], ["foo.uvh5", "bar.uvh5"]),
        (["foo.uvh5", "bar.uvh5"], ["foo.uvh5"], ["foo.uvh5", "bar.uvh5"]),
        (["foo.uvh5"], None, ["foo.uvh5"]),
        (None, ["bar.uvh5"], ["bar.uvh5"]),
        (None, None, None),
    ],
)
def test_combine_filenames(filename1, filename2, answer):
    combined_filenames = utils.tools._combine_filenames(filename1, filename2)
    if answer is None:
        assert combined_filenames is answer
    else:
        # use sets to test equality so that order doesn't matter
        assert set(combined_filenames) == set(answer)

    return


def test_slicify():
    assert utils.tools.slicify(None) is None
    assert utils.tools.slicify(slice(None)) == slice(None)
    assert utils.tools.slicify([]) is None
    assert utils.tools.slicify([1, 2, 3]) == slice(1, 4, 1)
    assert utils.tools.slicify([1]) == slice(1, 2, 1)
    assert utils.tools.slicify([0, 2, 4]) == slice(0, 6, 2)
    assert utils.tools.slicify([0, 1, 2, 7]) == [0, 1, 2, 7]


@pytest.mark.parametrize(
    "obj1,obj2,union_result,interset_result,diff_result",
    [
        [[1, 2, 3], [3, 4, 5], [1, 2, 3, 4, 5], [3], [1, 2]],  # Partial overlap
        [[1, 2], [1, 2], [1, 2], [1, 2], []],  # Full overlap
        [[1, 3, 5], [2, 4, 6], [1, 2, 3, 4, 5, 6], [], [1, 3, 5]],  # No overlap
        [[1, 2], None, [1, 2], [1, 2], [1, 2]],  # Nones
    ],
)
def test_sorted_unique_ops(obj1, obj2, union_result, interset_result, diff_result):
    assert utils.tools._sorted_unique_union(obj1, obj2) == union_result
    assert utils.tools._sorted_unique_intersection(obj1, obj2) == interset_result
    assert utils.tools._sorted_unique_difference(obj1, obj2) == diff_result


@pytest.mark.parametrize("strict", [True, False, None])
def test_strict_raise(strict):
    if strict:
        with pytest.raises(ValueError, match="This is a test"):
            utils.tools._strict_raise("This is a test", strict=strict)
    else:
        with check_warnings(None if strict is None else UserWarning, "This is a test"):
            utils.tools._strict_raise("This is a test", strict=strict)


@pytest.mark.parametrize(
    "inds,nrecs,exp_output,nwarn",
    [
        [[0, 1, 2], 3, [0, 1, 2], 0],
        [[0, 1, 2], 2, [0, 1], 1],
        [[-1, 0, 1, 2], 3, [0, 1, 2], 1],
        [[-1, 0, 1, 2, 3], 3, [0, 1, 2], 2],
        [[1], 3, [1], 0],
    ],
)
def test_eval_inds(inds, nrecs, exp_output, nwarn):
    with check_warnings(
        UserWarning if nwarn > 0 else None, ["inds contains indices that are"] * nwarn
    ):
        output = utils.tools._eval_inds(inds=inds, nrecs=nrecs, strict=False)
    assert all(exp_output == output)


@pytest.mark.parametrize("is_param", [True, False])
@pytest.mark.parametrize(
    "inp_arr,tols,exp_outcome",
    [
        [np.array([0, 0, 0, 0]), (0, 0), True],
        [[0, 0, 0, 0], None, True],
        [[0, 0, 0, 1], (0, 0), False],
        [[0, 0, 0, 1], None, False],
        [[0, 0, 0, 1], (1, 0), True],
    ],
)
def test_array_constant(inp_arr, is_param, tols, exp_outcome):
    if is_param:
        kwargs = {"value": inp_arr}
        if tols is not None:
            kwargs["tols"] = tols
        inp_arr = UVParameter("test", **kwargs)
    assert exp_outcome == utils.tools._test_array_constant(inp_arr, tols=tols)


@pytest.mark.parametrize("tols", [(0, 0, 0), [0, 0]])
def test_array_constant_errors(tols):
    inp_arr = UVParameter("test", value=np.array([0, 0, 0, 0]))
    with pytest.raises(
        ValueError,
        match="Something went wrong in utils.tools._test_array_constant. Please "
        "file an issue in our GitHub issue log so that we can help: "
        "https://github.com/RadioAstronomySoftwareGroup/pyuvdata/issues. "
        "Developer info: tols must be a length-2 tuple.",
    ):
        utils.tools._test_array_constant(inp_arr, tols=tols)


@pytest.mark.parametrize("is_param", [True, False])
@pytest.mark.parametrize(
    "inp_arr,inp2_arr,tols,exp_outcome",
    [
        [np.array([0, 0, 0, 0]), [0, 0, 0, 0], (0, 0), True],
        [[1, 2, 3, 4], np.array([1, 1, 1, 1]), None, True],
        [[0, 0, 0, 1], [0, 0, 0, 0], (0, 0), False],
        [[0, 0, 0, 1], [0, 0, 0, 0], None, False],
        [[1, 2, 3, 4], [0, 0, 0, 0], (0, 1), True],
    ],
)
def test_array_consistent(inp_arr, inp2_arr, is_param, tols, exp_outcome):
    if is_param:
        kwargs = {"value": inp_arr}
        if tols is not None:
            kwargs["tols"] = tols
        inp_arr = UVParameter("test", **kwargs)
        inp2_arr = UVParameter("test2", value=inp2_arr)
    assert exp_outcome == utils.tools._test_array_consistent(
        inp_arr, inp2_arr, tols=tols
    )


@pytest.mark.parametrize(
    ("inp_arr", "inp2_arr", "tols", "msg"),
    [
        (
            np.array([0, 0, 0, 0]),
            np.array([0, 0, 0]),
            (0, 0),
            "Something went wrong in utils.tools._test_array_consistent. Please "
            "file an issue in our GitHub issue log so that we can help: "
            "https://github.com/RadioAstronomySoftwareGroup/pyuvdata/issues. "
            "Developer info: array and deltas must have same shape.",
        ),
        (
            np.array([0, 0, 0, 0]),
            np.array([0, 0, 0, 0]),
            (0, 0, 0),
            "Something went wrong in utils.tools._test_array_consistent. Please "
            "file an issue in our GitHub issue log so that we can help: "
            "https://github.com/RadioAstronomySoftwareGroup/pyuvdata/issues. "
            "Developer info: tols must be a length-2 tuple.",
        ),
        (
            np.array([0, 0, 0, 0]),
            np.array([0, 0, 0, 0]),
            [0, 0],
            "Something went wrong in utils.tools._test_array_consistent. Please "
            "file an issue in our GitHub issue log so that we can help: "
            "https://github.com/RadioAstronomySoftwareGroup/pyuvdata/issues. "
            "Developer info: tols must be a length-2 tuple.",
        ),
    ],
)
def test_array_consistent_errors(inp_arr, inp2_arr, tols, msg):
    with pytest.raises(ValueError, match=msg):
        utils.tools._test_array_consistent(inp_arr, inp2_arr, tols=tols)


@pytest.mark.parametrize(
    "kwargs,exp_output",
    [
        [
            {"indices": [1], "max_nslice": 1, "return_index_on_fail": True},
            [slice(1, 2, 1)],
        ],
        [
            {"indices": [0, 1], "max_nslice": 1, "return_index_on_fail": True},
            [slice(0, 2, 1)],
        ],
        [
            {"indices": [1, 0], "max_nslice": 1, "return_index_on_fail": True},
            [slice(1, None, -1)],
        ],
        [
            {"indices": [3, 2, 1, 0], "max_nslice": 1, "return_index_on_fail": True},
            [slice(3, None, -1)],
        ],
        [
            {"indices": [2, 3, 1, 0], "max_nslice": 1, "return_index_on_fail": True},
            [[2, 3, 1, 0]],
        ],
        [
            {
                "indices": np.array([True, False, True, False]),
                "max_nslice": 1,
                "return_index_on_fail": True,
            },
            [slice(0, 4, 2)],
        ],
        [
            {
                "indices": np.array([True, False, True, True]),
                "max_nslice": 1,
                "return_index_on_fail": True,
            },
            [[True, False, True, True]],
        ],
        [
            {"indices": [0, 2, 4, 5], "max_nslice": 2},
            [slice(0, 6, 2), slice(5, 6, None)],
        ],
        [
            {"indices": [4, 2, 0, 5], "max_nslice": 2},
            [slice(4, None, -2), slice(5, 6, None)],
        ],
        [
            {"indices": [4, 2, 0, 1, 3, 5], "max_nslice": 2},
            [slice(4, None, -2), slice(1, 7, 2)],
        ],
    ],
)
def test_convert_to_slices(kwargs, exp_output):
    slice_list, check = utils.tools._convert_to_slices(**kwargs)

    if (len(slice_list) == 1) and isinstance(slice_list[0], np.ndarray):
        slice_list[0] = slice_list[0].tolist()

    assert isinstance(slice_list[0], slice) == check
    assert slice_list == exp_output