示例#1
0
def test_sigmerger_check():
    # 0-d signals
    assert SigMerger.check([Signal(0), Signal(0)])
    assert not SigMerger.check([Signal(0), Signal(1)])

    # compatible along first axis
    assert SigMerger.check(
        [Signal(np.empty((1, 2))), Signal(np.empty((2, 2)))])

    # compatible along second axis
    assert SigMerger.check(
        [Signal(np.empty((2, 1))), Signal(np.empty((2, 2)))], axis=1)
    assert not SigMerger.check(
        [Signal(np.empty((2, 1))), Signal(np.empty((2, 2)))], axis=0)

    # shape mismatch
    assert not SigMerger.check(
        [Signal(np.empty((2,))), Signal(np.empty((2, 2)))])

    # mixed dtype
    assert not SigMerger.check(
        [Signal(np.empty(2, dtype=int)), Signal(np.empty(2, dtype=float))])

    s1 = Signal(np.empty(5))
    s2 = Signal(np.empty(5))

    # mixed signal and view
    assert not SigMerger.check([s1, s1[:3]])

    # mixed bases
    assert not SigMerger.check([s1[:2], s2[2:]])

    # compatible views
    assert SigMerger.check([s1[:2], s1[2:]])
示例#2
0
def test_sigmerger_merge(allclose):
    s1 = Signal(np.array([[0, 1], [2, 3]]))
    s2 = Signal(np.array([[4, 5]]))

    sig, replacements = SigMerger.merge([s1, s2])
    assert allclose(sig.initial_value, np.array([[0, 1], [2, 3], [4, 5]]))
    assert allclose(replacements[s1].initial_value, s1.initial_value)
    assert allclose(replacements[s2].initial_value, s2.initial_value)

    with pytest.raises(ValueError, match="Cannot merge mixed views and non-views"):
        SigMerger.merge([s1[0], s2])
示例#3
0
def test_sigmerger_merge_views(allclose):
    s = Signal(np.array([[0, 1], [2, 3], [4, 5]]))
    v1, v2 = s[:2], s[2:]
    merged, _ = SigMerger.merge_views([v1, v2])

    assert allclose(merged.initial_value, s.initial_value)
    assert v1.base is s
    assert v2.base is s
示例#4
0
def test_sigmerger_merge(allclose):
    s1 = Signal(np.array([[0, 1], [2, 3]]))
    s2 = Signal(np.array([[4, 5]]))

    sig, replacements = SigMerger.merge([s1, s2])
    assert allclose(sig.initial_value, np.array([[0, 1], [2, 3], [4, 5]]))
    assert allclose(replacements[s1].initial_value, s1.initial_value)
    assert allclose(replacements[s2].initial_value, s2.initial_value)
示例#5
0
def test_sigmerger_merge_views():
    s = Signal(np.array([[0, 1], [2, 3], [4, 5]]))
    v1, v2 = s[:2], s[2:]
    merged, _ = SigMerger.merge_views([v1, v2])

    assert np.allclose(merged.initial_value, s.initial_value)
    assert v1.base is s
    assert v2.base is s
示例#6
0
def test_sigmerger_merge():
    s1 = Signal(np.array([[0, 1], [2, 3]]))
    s2 = Signal(np.array([[4, 5]]))

    sig, replacements = SigMerger.merge([s1, s2])
    assert np.allclose(sig.initial_value, np.array([[0, 1], [2, 3], [4, 5]]))
    assert np.allclose(replacements[s1].initial_value, s1.initial_value)
    assert np.allclose(replacements[s2].initial_value, s2.initial_value)
示例#7
0
def test_sigmerger_check_views():
    s1 = Signal(np.empty((5, 5)))
    s2 = Signal(np.empty((5, 5)))

    # compatible along first axis
    SigMerger.check_views([s1[:1], s1[1:]])

    # compatible along second axis
    SigMerger.check_views([s1[:1, :1], s1[:1, 1:]], axis=1)
    with pytest.raises(ValueError):
        SigMerger.check_views([s1[:1, :1], s1[:1, 1:]], axis=0)

    # shape mismatch
    with pytest.raises(ValueError):
        SigMerger.check_views([s1[:1], s1[1:, 0]])

    # different bases
    with pytest.raises(ValueError):
        SigMerger.check_views([s1[:2], s2[2:]])
示例#8
0
def test_sigmerger_check_views():
    s1 = Signal(np.empty((5, 5)))
    s2 = Signal(np.empty((5, 5)))

    # compatible along first axis
    SigMerger.check_views([s1[:1], s1[1:]])

    # compatible along second axis
    SigMerger.check_views([s1[:1, :1], s1[:1, 1:]], axis=1)
    with pytest.raises(ValueError):
        SigMerger.check_views([s1[:1, :1], s1[:1, 1:]], axis=0)

    # shape mismatch
    with pytest.raises(ValueError):
        SigMerger.check_views([s1[:1], s1[1:, 0]])

    # different bases
    with pytest.raises(ValueError):
        SigMerger.check_views([s1[:2], s2[2:]])
示例#9
0
def test_sigmerger_check():
    # 0-d signals
    assert SigMerger.check([Signal(0), Signal(0)])
    assert not SigMerger.check([Signal(0), Signal(1)])

    # compatible along first axis
    assert SigMerger.check(
        [Signal(np.empty((1, 2))),
         Signal(np.empty((2, 2)))])

    # compatible along second axis
    assert SigMerger.check(
        [Signal(np.empty(
            (2, 1))), Signal(np.empty((2, 2)))], axis=1)
    assert not SigMerger.check(
        [Signal(np.empty(
            (2, 1))), Signal(np.empty((2, 2)))], axis=0)

    # shape mismatch
    assert not SigMerger.check(
        [Signal(np.empty(
            (2, ))), Signal(np.empty((2, 2)))])

    # mixed dtype
    assert not SigMerger.check(
        [Signal(np.empty(2, dtype=int)),
         Signal(np.empty(2, dtype=float))])

    s1 = Signal(np.empty(5))
    s2 = Signal(np.empty(5))

    # mixed signal and view
    assert not SigMerger.check([s1, s1[:3]])

    # mixed bases
    assert not SigMerger.check([s1[:2], s2[2:]])

    # compatible views
    assert SigMerger.check([s1[:2], s1[2:]])
示例#10
0
def test_sigmerger_check_signals():
    # 0-d signals
    SigMerger.check_signals([Signal(0), Signal(0)])
    with pytest.raises(ValueError):
        SigMerger.check_signals([Signal(0), Signal(1)])

    # compatible along first axis
    SigMerger.check_signals(
        [Signal(np.empty((1, 2))),
         Signal(np.empty((2, 2)))])

    # compatible along second axis
    SigMerger.check_signals(
        [Signal(np.empty(
            (2, 1))), Signal(np.empty((2, 2)))], axis=1)
    with pytest.raises(ValueError):
        SigMerger.check_signals(
            [Signal(np.empty(
                (2, 1))), Signal(np.empty((2, 2)))], axis=0)

    # shape mismatch
    with pytest.raises(ValueError):
        SigMerger.check_signals(
            [Signal(np.empty((2, ))),
             Signal(np.empty((2, 2)))])

    # mixed dtype
    with pytest.raises(ValueError):
        SigMerger.check_signals(
            [Signal(np.empty(2, dtype=int)),
             Signal(np.empty(2, dtype=float))])

    # compatible views
    s = Signal(np.empty(5))
    with pytest.raises(ValueError):
        SigMerger.check_signals([s[:2], s[2:]])
示例#11
0
def test_sigmerger_check():
    # 0-d signals
    assert SigMerger.check([Signal(0), Signal(0)])
    assert not SigMerger.check([Signal(0), Signal(1)])

    # compatible along first axis
    assert SigMerger.check(
        [Signal(np.empty((1, 2))),
         Signal(np.empty((2, 2)))])

    # compatible along second axis
    assert SigMerger.check(
        [Signal(np.empty(
            (2, 1))), Signal(np.empty((2, 2)))], axis=1)
    assert not SigMerger.check(
        [Signal(np.empty(
            (2, 1))), Signal(np.empty((2, 2)))], axis=0)

    # shape mismatch
    assert not SigMerger.check(
        [Signal(np.empty(
            (2, ))), Signal(np.empty((2, 2)))])

    # mixed dtype
    assert not SigMerger.check(
        [Signal(np.empty(2, dtype=int)),
         Signal(np.empty(2, dtype=float))])

    s1 = Signal(np.empty(5))
    s2 = Signal(np.empty(5))

    # mixed signal and view
    assert not SigMerger.check([s1, s1[:3]])

    # mixed bases
    assert not SigMerger.check([s1[:2], s2[2:]])

    # compatible views
    assert SigMerger.check([s1[:2], s1[2:]])

    # sparse signals not mergeable
    assert not SigMerger.check([
        Signal(SparseMatrix([[0, 0]], 1.0, (1, 1))),
        Signal(SparseMatrix([[0, 0]], 1.0, (1, 1))),
    ])

    # same signal cannot appear twice
    sig = Signal(0)
    assert not SigMerger.check([sig, sig])
示例#12
0
def test_sigmerger_check_views():
    s1 = Signal(shape=(5, 5))
    s2 = Signal(shape=(5, 5))

    # compatible along first axis
    SigMerger.check_views([s1[:1], s1[1:]])

    # compatible along second axis
    SigMerger.check_views([s1[:1, :1], s1[:1, 1:]], axis=1)

    # non-views
    with pytest.raises(ValueError, match="Cannot merge non-views"):
        SigMerger.check_views([s1, s2])

    # different bases
    with pytest.raises(ValueError, match="must share the same base"):
        SigMerger.check_views([s1[:2], s2[2:]])

    # different ndims
    with pytest.raises(ValueError, match="must have the same number of dimensions"):
        SigMerger.check_views([s1[:1], s1[1:, 0]])

    # different strides
    with pytest.raises(ValueError, match="must have equal strides"):
        SigMerger.check_views([s1[::2], s1[::3]])

    # shape mismatch
    with pytest.raises(ValueError, match="must have same shape except on"):
        SigMerger.check_views([s1[:1, :1], s1[:1, 1:]], axis=0)

    # non-sequential
    with pytest.raises(ValueError, match="Views are not sequential"):
        SigMerger.check_views([s1[1:], s1[1:-1]])
示例#13
0
def test_sigmerger_check_signals():
    # 0-d signals
    SigMerger.check_signals([Signal(0), Signal(0)])
    with pytest.raises(ValueError):
        SigMerger.check_signals([Signal(0), Signal(1)])

    # compatible along first axis
    SigMerger.check_signals(
        [Signal(np.empty((1, 2))), Signal(np.empty((2, 2)))])

    # compatible along second axis
    SigMerger.check_signals(
        [Signal(np.empty((2, 1))), Signal(np.empty((2, 2)))], axis=1)
    with pytest.raises(ValueError):
        SigMerger.check_signals(
            [Signal(np.empty((2, 1))), Signal(np.empty((2, 2)))], axis=0)

    # shape mismatch
    with pytest.raises(ValueError):
        SigMerger.check_signals(
            [Signal(np.empty((2,))), Signal(np.empty((2, 2)))])

    # mixed dtype
    with pytest.raises(ValueError):
        SigMerger.check_signals(
            [Signal(np.empty(2, dtype=int)), Signal(np.empty(2, dtype=float))])

    # compatible views
    s = Signal(np.empty(5))
    with pytest.raises(ValueError):
        SigMerger.check_signals([s[:2], s[2:]])