|
a |
|
b/tests/test_nparr.py |
|
|
1 |
from copy import copy |
|
|
2 |
|
|
|
3 |
import numpy as np |
|
|
4 |
import pytest |
|
|
5 |
|
|
|
6 |
from janggu.data import Array |
|
|
7 |
from janggu.data import NanToNumConverter |
|
|
8 |
from janggu.data import RandomOrientation |
|
|
9 |
from janggu.data import RandomShift |
|
|
10 |
from janggu.data import RandomSignalScale |
|
|
11 |
from janggu.data import ReduceDim |
|
|
12 |
from janggu.data import SqueezeDim |
|
|
13 |
|
|
|
14 |
|
|
|
15 |
def test_nparr(tmpdir): |
|
|
16 |
X = Array("X", np.random.random((1000, 100))) |
|
|
17 |
y = Array('y', np.random.randint(2, size=(1000,))) |
|
|
18 |
|
|
|
19 |
np.testing.assert_equal(len(X), len(y)) |
|
|
20 |
np.testing.assert_equal(len(X), 1000) |
|
|
21 |
np.testing.assert_equal(X.shape, (1000, 100,)) |
|
|
22 |
np.testing.assert_equal(y.shape, (1000, 1)) |
|
|
23 |
assert y.ndim == 2 |
|
|
24 |
assert y.shape == (1000, 1) |
|
|
25 |
new_X = copy(X) |
|
|
26 |
|
|
|
27 |
|
|
|
28 |
def test_reducedim(): |
|
|
29 |
x_orig = np.zeros((3,1,1,2)) |
|
|
30 |
|
|
|
31 |
np.testing.assert_equal(x_orig.ndim, 4) |
|
|
32 |
x_reduce = ReduceDim(Array('test', x_orig, conditions=["A", "B"])) |
|
|
33 |
x_reduce = ReduceDim(Array('test', x_orig, conditions=["A", "B"]), aggregator='mean') |
|
|
34 |
x_reduce = ReduceDim(Array('test', x_orig, conditions=["A", "B"]), aggregator='max') |
|
|
35 |
x_reduce = ReduceDim(Array('test', x_orig, conditions=["A", "B"]), aggregator=np.mean) |
|
|
36 |
with pytest.raises(ValueError): |
|
|
37 |
ReduceDim(Array('test', x_orig, conditions=["A", "B"]), aggregator='nonsense') |
|
|
38 |
|
|
|
39 |
np.testing.assert_equal(len(x_reduce), 3) |
|
|
40 |
np.testing.assert_equal(x_reduce.shape, (3,2)) |
|
|
41 |
np.testing.assert_equal(x_reduce.ndim, 2) |
|
|
42 |
assert x_reduce[0].shape == (1, 2) |
|
|
43 |
assert x_reduce[:3].shape == (3, 2) |
|
|
44 |
assert x_reduce[[0,1]].shape == (2, 2) |
|
|
45 |
assert x_reduce.ndim == 2 |
|
|
46 |
new_x = copy(x_reduce) |
|
|
47 |
assert x_reduce[0].shape == new_x[0].shape |
|
|
48 |
assert x_reduce.conditions == ["A", "B"] |
|
|
49 |
|
|
|
50 |
def test_squeezedim(): |
|
|
51 |
x_orig = np.zeros((3,1,1,2)) |
|
|
52 |
|
|
|
53 |
np.testing.assert_equal(x_orig.ndim, 4) |
|
|
54 |
x_sq = SqueezeDim(Array('test', x_orig, conditions=["A", "B"])) |
|
|
55 |
|
|
|
56 |
np.testing.assert_equal(len(x_sq), 3) |
|
|
57 |
|
|
|
58 |
np.testing.assert_equal(x_sq.shape, (3,2)) |
|
|
59 |
|
|
|
60 |
np.testing.assert_equal(x_sq.ndim, 2) |
|
|
61 |
assert x_sq[0].shape == (2,) |
|
|
62 |
assert x_sq[:3].shape == (3, 2) |
|
|
63 |
assert x_sq[[0,1]].shape == (2, 2) |
|
|
64 |
assert x_sq.ndim == 2 |
|
|
65 |
new_x = copy(x_sq) |
|
|
66 |
assert x_sq[0].shape == new_x[0].shape |
|
|
67 |
assert x_sq.conditions == ["A", "B"] |
|
|
68 |
|
|
|
69 |
|
|
|
70 |
def test_nantonumconverter(): |
|
|
71 |
x_orig = np.zeros((3,1,1,2)) |
|
|
72 |
x_orig[0,0,0,0] = np.nan |
|
|
73 |
arr = Array('test', x_orig, conditions=["A", "B"]) |
|
|
74 |
assert np.isnan(arr[0].mean()) |
|
|
75 |
|
|
|
76 |
x_tr = NanToNumConverter(Array('test', x_orig, conditions=["A", "B"])) |
|
|
77 |
assert x_tr[0].shape == (1, 1, 1, 2) |
|
|
78 |
assert x_tr[:3].shape == (3, 1, 1, 2) |
|
|
79 |
assert x_tr[[0,1]].shape == (2, 1, 1, 2) |
|
|
80 |
assert len(x_tr) == 3 |
|
|
81 |
assert x_tr.shape == (3, 1, 1, 2) |
|
|
82 |
assert x_tr.ndim == 4 |
|
|
83 |
assert not np.isnan(x_tr[0].mean()) |
|
|
84 |
np.testing.assert_equal(x_tr[0], [[[[0,0]]]]) |
|
|
85 |
new_x = copy(x_tr) |
|
|
86 |
assert x_tr[0].shape == new_x[0].shape |
|
|
87 |
assert x_tr.conditions == ["A", "B"] |
|
|
88 |
|
|
|
89 |
def test_randomorientation(): |
|
|
90 |
x_orig = np.zeros((3,1,1,2)) |
|
|
91 |
|
|
|
92 |
x_tr = RandomOrientation(Array('test', x_orig, conditions=["A", "B"])) |
|
|
93 |
assert x_tr[0].shape == (1, 1, 1, 2) |
|
|
94 |
assert x_tr[:3].shape == (3, 1, 1, 2) |
|
|
95 |
assert x_tr[[0,1]].shape == (2, 1, 1, 2) |
|
|
96 |
np.testing.assert_equal(len(x_tr), 3) |
|
|
97 |
assert len(x_tr) == 3 |
|
|
98 |
assert x_tr.shape == (3, 1, 1, 2) |
|
|
99 |
assert x_tr.ndim == 4 |
|
|
100 |
np.testing.assert_equal(x_tr[0], [[[[0,0]]]]) |
|
|
101 |
new_x = copy(x_tr) |
|
|
102 |
assert x_tr[0].shape == new_x[0].shape |
|
|
103 |
assert x_tr.conditions == ["A", "B"] |
|
|
104 |
|
|
|
105 |
|
|
|
106 |
def test_randomsignalscale(): |
|
|
107 |
x_orig = np.ones((3,1,1,2)) |
|
|
108 |
|
|
|
109 |
x_tr = RandomSignalScale(Array('test', x_orig), .1) |
|
|
110 |
assert x_tr[0].shape == (1, 1, 1, 2) |
|
|
111 |
assert x_tr[:3].shape == (3, 1, 1, 2) |
|
|
112 |
assert x_tr[[0,1]].shape == (2, 1, 1, 2) |
|
|
113 |
np.testing.assert_equal(len(x_tr), 3) |
|
|
114 |
assert len(x_tr) == 3 |
|
|
115 |
assert x_tr.shape == (3, 1, 1, 2) |
|
|
116 |
assert x_tr.ndim == 4 |
|
|
117 |
new_x = copy(x_tr) |
|
|
118 |
assert x_tr[0].shape == new_x[0].shape |
|
|
119 |
assert x_tr.conditions == None |
|
|
120 |
|
|
|
121 |
|
|
|
122 |
def test_randomshift(): |
|
|
123 |
x_orig = np.zeros((1,4,1,4)) |
|
|
124 |
x_orig[0, 0, 0,0] = 1 |
|
|
125 |
x_orig[0, 1, 0,1] = 1 |
|
|
126 |
x_orig[0, 2, 0,2] = 1 |
|
|
127 |
x_orig[0, 3, 0,3] = 1 |
|
|
128 |
|
|
|
129 |
x_tr = RandomShift(Array('test', x_orig), 1) |
|
|
130 |
assert x_tr[0].shape == (1, 4, 1, 4) |
|
|
131 |
np.testing.assert_equal(len(x_tr), 1) |
|
|
132 |
assert x_tr.shape == (1, 4, 1, 4) |
|
|
133 |
assert x_tr.ndim == 4 |
|
|
134 |
new_x = copy(x_tr) |
|
|
135 |
assert x_tr[0].shape == new_x[0].shape |
|
|
136 |
assert x_tr.conditions == None |
|
|
137 |
|
|
|
138 |
x_tr = RandomShift(Array('test', x_orig), 1, True) |
|
|
139 |
assert x_tr[0].shape == (1, 4, 1, 4) |
|
|
140 |
np.testing.assert_equal(len(x_tr), 1) |
|
|
141 |
assert x_tr.shape == (1, 4, 1, 4) |
|
|
142 |
assert x_tr.ndim == 4 |
|
|
143 |
new_x = copy(x_tr) |
|
|
144 |
assert x_tr[0].shape == new_x[0].shape |
|
|
145 |
assert x_tr.conditions == None |
|
|
146 |
|