コード例 #1
0
ファイル: test_axes.py プロジェクト: tsung1029/nata
def test_Axis_repr():
    axis = Axis(1, name="some_name", label="some_label", unit="some_unit")

    assert (repr(axis) == "Axis(" + "name='some_name', " +
            "label='some_label', " + "unit='some_unit', " + "axis_dim=0, " +
            "len=1" + ")")

    axis = Axis(
        [0, 1],
        name="some_name",
        label="some_label",
        unit="some_unit",
    )
    assert (repr(axis) == "Axis(" + "name='some_name', " +
            "label='some_label', " + "unit='some_unit', " + "axis_dim=0, " +
            "len=2" + ")")

    axis = Axis(
        [[0, 1]],
        name="some_name",
        label="some_label",
        unit="some_unit",
    )
    assert (repr(axis) == "Axis(" + "name='some_name', " +
            "label='some_label', " + "unit='some_unit', " + "axis_dim=1, " +
            "len=1" + ")")
    axis = Axis(
        [[0, 1], [0, 1]],
        name="some_name",
        label="some_label",
        unit="some_unit",
    )
    assert (repr(axis) == "Axis(" + "name='some_name', " +
            "label='some_label', " + "unit='some_unit', " + "axis_dim=1, " +
            "len=2" + ")")
コード例 #2
0
ファイル: test_axes.py プロジェクト: tsung1029/nata
def test_Axis_name_setter():
    axis = Axis(0, name="something")
    assert axis.name == "something"

    axis.name = "something_else"
    assert axis.name == "something_else"

    with pytest.raises(ValueError, match="Invalid name provided!"):
        axis.name = ""
コード例 #3
0
ファイル: test_axes.py プロジェクト: tsung1029/nata
def test_Axis_data_change_not_broadcastable(base, new):
    try:
        np.broadcast_to(new, base.shape)
        assume(False)  # not valid hypothesis test
    except ValueError:
        pass

    axis = Axis(base)

    with pytest.raises(ValueError):
        axis.data = new
コード例 #4
0
ファイル: test_axes.py プロジェクト: tsung1029/nata
def test_Axis_dimensionality():
    axis = Axis(np.empty(10))
    assert axis.shape == (10, )
    assert axis.ndim == 1
    assert len(axis) == 10

    axis = Axis(np.empty((1, 10)))
    assert axis.shape == (10, )
    assert axis.ndim == 1
    assert len(axis) == 1

    axis = Axis(np.empty((2, 10)))
    assert axis.shape == (2, 10)
    assert axis.ndim == 2
    assert len(axis) == 2
コード例 #5
0
ファイル: test_axes.py プロジェクト: tsung1029/nata
def test_Axis_data_change_broadcastable(base, new):
    try:
        expected = np.broadcast_to(new, base.shape)
    except ValueError:
        assume(False)

    # compensating the dimension consumption
    if base.shape and len(base) == 1:
        axis = Axis(base[np.newaxis])
    else:
        axis = Axis(base)
    np.testing.assert_array_equal(axis, base)

    axis.data = new
    np.testing.assert_array_equal(axis, expected)
コード例 #6
0
ファイル: test_axes.py プロジェクト: tsung1029/nata
def test_Axis_default_init(arr):
    axis = Axis(arr)

    assert axis.name == "unnamed"
    assert axis.label == ""
    assert axis.unit == ""
    assert axis.shape == (arr.shape[1:]
                          if arr.ndim != 0 and len(arr) == 1 else arr.shape)
    if arr.ndim != 0 and len(arr) == 1:
        np.testing.assert_array_equal(axis, arr[0, ...])
    else:
        np.testing.assert_array_equal(axis, arr)
コード例 #7
0
ファイル: test_axes.py プロジェクト: tsung1029/nata
def test_Axis_getitem(initarr, indexing, expected_array):
    """Test getitem [..., ...] for Axis

    - Numbers are chosen randomly and tests are only for behavior.
    - Only the array interface is checked.
    - Properties `ndim`/`shape`are part of array assert statement
    """
    name = "some_name"
    label = "some_label"
    unit = "some_unit"

    axis = Axis(initarr, name=name, label=label, unit=unit)
    subaxis = axis[indexing]

    assert subaxis is not axis
    assert subaxis.name == name
    assert subaxis.label == label
    assert subaxis.unit == unit

    np.testing.assert_array_equal(subaxis, expected_array)
コード例 #8
0
ファイル: test_axes.py プロジェクト: tsung1029/nata
def test_Axis_iterator_multiple_items_0d():
    name = "some_name"
    label = "some_label"
    unit = "some_unit"

    arrays = [np.array(1), np.array(2), np.array(3)]
    axis = Axis(arrays[0], name=name, label=label, unit=unit)
    axis.append(Axis(arrays[1], name=name, label=label, unit=unit))
    axis.append(Axis(arrays[2], name=name, label=label, unit=unit))

    for ax, arr in zip(axis, arrays):
        assert ax is not axis
        assert ax.name == name
        assert ax.label == label
        assert ax.unit == unit
        np.testing.assert_array_equal(ax, arr)
コード例 #9
0
ファイル: test_axes.py プロジェクト: tsung1029/nata
def test_Axis_iterator_single_item():
    axis = Axis(1)

    for ax in axis:
        assert ax is not axis
        np.testing.assert_array_equal(ax, 1)
コード例 #10
0
ファイル: test_axes.py プロジェクト: tsung1029/nata
def test_Axis_len():
    axis = Axis(1)
    assert len(axis) == 1

    axis.append(Axis(2))
    assert len(axis) == 2

    axis.append(Axis([3, 4]))
    assert len(axis) == 4

    axis = Axis([1])
    assert len(axis) == 1

    axis = Axis([1, 2])
    assert len(axis) == 2

    axis = Axis([[1, 2]])
    assert len(axis) == 1
コード例 #11
0
ファイル: test_axes.py プロジェクト: tsung1029/nata
def test_Axis_append_different_axis():
    with pytest.raises(ValueError, match="Mismatch in attributes"):
        Axis(np.array(1),
             name="some_name").append(Axis(np.array(1), name="different_name"))
コード例 #12
0
ファイル: test_axes.py プロジェクト: tsung1029/nata
def test_Axis_append_wrong_type():
    with pytest.raises(TypeError, match="Can not append"):
        Axis(np.array(1)).append(1)
コード例 #13
0
ファイル: test_axes.py プロジェクト: tsung1029/nata
def test_Axis_label_init():
    axis = Axis(np.empty(10), label="test")
    assert axis.label == "test"
コード例 #14
0
ファイル: test_axes.py プロジェクト: tsung1029/nata
def test_Axis_array_interface(arr):
    axis = Axis(arr)
    np.testing.assert_array_equal(np.array(axis, dtype=float),
                                  arr.astype(float))
    np.testing.assert_array_equal(axis, arr)
コード例 #15
0
ファイル: test_axes.py プロジェクト: tsung1029/nata
def test_Axis_ndim(arr, expected_ndim):
    axis = Axis(arr)
    assert axis.ndim == expected_ndim
コード例 #16
0
ファイル: test_axes.py プロジェクト: tsung1029/nata
def test_Axis_dtype(arr):
    axis = Axis(arr)
    assert axis.dtype == arr.dtype
コード例 #17
0
ファイル: test_axes.py プロジェクト: tsung1029/nata
def test_Axis_name_init():
    axis = Axis(np.empty(10), name="test")
    assert axis.name == "test"
コード例 #18
0
ファイル: test_axes.py プロジェクト: tsung1029/nata
def test_Axis_unit_setter():
    axis = Axis(0, unit="something")
    assert axis.unit == "something"

    axis.unit = "something_else"
    assert axis.unit == "something_else"
コード例 #19
0
ファイル: test_axes.py プロジェクト: tsung1029/nata
def test_Axis_unit_init():
    axis = Axis(np.empty(10), unit="test")
    assert axis.unit == "test"
コード例 #20
0
ファイル: test_axes.py プロジェクト: tsung1029/nata
def test_Axis_label_setter():
    axis = Axis(0, label="something")
    assert axis.label == "something"

    axis.label = "something_else"
    assert axis.label == "something_else"
コード例 #21
0
ファイル: test_axes.py プロジェクト: tsung1029/nata
def test_Axis_getitem_raise_when_not_basic_indexing(arr, indexing):
    axis = Axis(arr)

    with pytest.raises(IndexError, match="Only basic indexing is supported!"):
        axis[indexing]
コード例 #22
0
ファイル: test_axes.py プロジェクト: tsung1029/nata
def test_Axis_equivalent():
    axis = Axis(1)

    assert axis.equivalent(Axis(1)) is True
    assert axis.equivalent(object()) is False
    assert axis.equivalent(Axis(np.array(1), name="other")) is False
    assert axis.equivalent(Axis(np.array(1), label="other")) is False
    assert axis.equivalent(Axis(np.array(1), unit="other")) is False
    assert axis.equivalent(Axis(np.array([1]))) is True
    assert axis.equivalent(Axis(np.array([[1]]))) is False
コード例 #23
0
ファイル: test_axes.py プロジェクト: tsung1029/nata
def test_Axis_name_parsing():
    assert Axis(0, name="").name == "unnamed"
コード例 #24
0
ファイル: test_axes.py プロジェクト: tsung1029/nata
def test_Axis_shape(arr, expected_shape):
    axis = Axis(arr)
    assert axis.shape == expected_shape
コード例 #25
0
ファイル: test_axes.py プロジェクト: tsung1029/nata
def test_Axis_append():
    axis = Axis(1)
    assert axis.shape == ()

    axis.append(Axis(2))
    assert axis.shape == (2, )
    np.testing.assert_array_equal(axis, [1, 2])

    axis.append(Axis([3, 4]))
    assert axis.shape == (4, )
    np.testing.assert_array_equal(axis, [1, 2, 3, 4])

    axis = Axis(1)
    axis.append(Axis([2, 3]))
    assert axis.shape == (3, )
    np.testing.assert_array_equal(axis, [1, 2, 3])

    axis = Axis([[1, 2]])
    assert axis.shape == (2, )
    axis.append(Axis([[3, 4]]))
    assert axis.shape == (2, 2)
    axis.append(Axis([[5, 6]]))
    assert axis.shape == (3, 2)
コード例 #26
0
ファイル: test_axes.py プロジェクト: tsung1029/nata
def test_Axis_axis_dim(arr, expected_axis_dim):
    axis = Axis(arr)
    assert axis.axis_dim == expected_axis_dim
コード例 #27
0
ファイル: test_GridDataset.py プロジェクト: fyli16/nata
    ids=["backend", "name", "label", "unit"],
)
def test_GridDataset_attr_from_array_init(TestGridBackend, attr, value):
    """Parameterize check for different props from random array"""
    ds = GridDataset(np.random.random_sample((4, 5, 6)))
    assert getattr(ds, attr) == value


_GridDataset_getitem_tests = {}
_GridDataset_getitem_tests["1d, single time step, [int]"] = (
    # indexing
    np.s_[2],
    # data
    np.arange(10).reshape((1, 10)),
    # iteration
    Axis(0),
    # time
    Axis(0),
    # grid_axes
    [GridAxis(np.arange(10).reshape((1, 10)))],
    # expected data
    np.array(2),
    # expected iteration
    Axis(0),
    # expected time
    Axis(0),
    # expected grid_axes
    [],
)
_GridDataset_getitem_tests["1d, single time step, [:]"] = (
    # indexing