File size: 1,101 Bytes
16760fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b1390b1
 
 
 
 
 
16760fa
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import numpy as np
import pytest

from toaster.core import Grouping, Selection


def test_constructors_and_indices():
    sel = Selection.from_indices(np.array([1, 3]), n=5)
    assert sel.indices.tolist() == [1, 3]
    assert sel.count == 2
    assert Selection.empty(5).is_empty()
    assert Selection.from_pick(2, 5).indices.tolist() == [2]


def test_boolean_algebra():
    a = Selection.from_indices([0, 1, 2], 5)
    b = Selection.from_indices([1, 2, 3], 5)
    assert (a | b).indices.tolist() == [0, 1, 2, 3]
    assert (a & b).indices.tolist() == [1, 2]
    assert (a - b).indices.tolist() == [0]
    assert (a ^ b).indices.tolist() == [0, 3]


def test_all():
    sel = Selection.all(5)
    assert sel.indices.tolist() == [0, 1, 2, 3, 4]
    assert sel.count == 5


def test_from_group():
    grouping = Grouping(np.array([0, 0, 1, -1, 1], np.int32))
    sel = Selection.from_group(grouping, 1)
    assert sel.indices.tolist() == [2, 4]


def test_size_mismatch_raises():
    with pytest.raises(ValueError):
        Selection.empty(4) | Selection.empty(5)