[0473b3]: / tests / test_nparr.py

Download this file

147 lines (119 with data), 4.7 kB

  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
from copy import copy
import numpy as np
import pytest
from janggu.data import Array
from janggu.data import NanToNumConverter
from janggu.data import RandomOrientation
from janggu.data import RandomShift
from janggu.data import RandomSignalScale
from janggu.data import ReduceDim
from janggu.data import SqueezeDim
def test_nparr(tmpdir):
X = Array("X", np.random.random((1000, 100)))
y = Array('y', np.random.randint(2, size=(1000,)))
np.testing.assert_equal(len(X), len(y))
np.testing.assert_equal(len(X), 1000)
np.testing.assert_equal(X.shape, (1000, 100,))
np.testing.assert_equal(y.shape, (1000, 1))
assert y.ndim == 2
assert y.shape == (1000, 1)
new_X = copy(X)
def test_reducedim():
x_orig = np.zeros((3,1,1,2))
np.testing.assert_equal(x_orig.ndim, 4)
x_reduce = ReduceDim(Array('test', x_orig, conditions=["A", "B"]))
x_reduce = ReduceDim(Array('test', x_orig, conditions=["A", "B"]), aggregator='mean')
x_reduce = ReduceDim(Array('test', x_orig, conditions=["A", "B"]), aggregator='max')
x_reduce = ReduceDim(Array('test', x_orig, conditions=["A", "B"]), aggregator=np.mean)
with pytest.raises(ValueError):
ReduceDim(Array('test', x_orig, conditions=["A", "B"]), aggregator='nonsense')
np.testing.assert_equal(len(x_reduce), 3)
np.testing.assert_equal(x_reduce.shape, (3,2))
np.testing.assert_equal(x_reduce.ndim, 2)
assert x_reduce[0].shape == (1, 2)
assert x_reduce[:3].shape == (3, 2)
assert x_reduce[[0,1]].shape == (2, 2)
assert x_reduce.ndim == 2
new_x = copy(x_reduce)
assert x_reduce[0].shape == new_x[0].shape
assert x_reduce.conditions == ["A", "B"]
def test_squeezedim():
x_orig = np.zeros((3,1,1,2))
np.testing.assert_equal(x_orig.ndim, 4)
x_sq = SqueezeDim(Array('test', x_orig, conditions=["A", "B"]))
np.testing.assert_equal(len(x_sq), 3)
np.testing.assert_equal(x_sq.shape, (3,2))
np.testing.assert_equal(x_sq.ndim, 2)
assert x_sq[0].shape == (2,)
assert x_sq[:3].shape == (3, 2)
assert x_sq[[0,1]].shape == (2, 2)
assert x_sq.ndim == 2
new_x = copy(x_sq)
assert x_sq[0].shape == new_x[0].shape
assert x_sq.conditions == ["A", "B"]
def test_nantonumconverter():
x_orig = np.zeros((3,1,1,2))
x_orig[0,0,0,0] = np.nan
arr = Array('test', x_orig, conditions=["A", "B"])
assert np.isnan(arr[0].mean())
x_tr = NanToNumConverter(Array('test', x_orig, conditions=["A", "B"]))
assert x_tr[0].shape == (1, 1, 1, 2)
assert x_tr[:3].shape == (3, 1, 1, 2)
assert x_tr[[0,1]].shape == (2, 1, 1, 2)
assert len(x_tr) == 3
assert x_tr.shape == (3, 1, 1, 2)
assert x_tr.ndim == 4
assert not np.isnan(x_tr[0].mean())
np.testing.assert_equal(x_tr[0], [[[[0,0]]]])
new_x = copy(x_tr)
assert x_tr[0].shape == new_x[0].shape
assert x_tr.conditions == ["A", "B"]
def test_randomorientation():
x_orig = np.zeros((3,1,1,2))
x_tr = RandomOrientation(Array('test', x_orig, conditions=["A", "B"]))
assert x_tr[0].shape == (1, 1, 1, 2)
assert x_tr[:3].shape == (3, 1, 1, 2)
assert x_tr[[0,1]].shape == (2, 1, 1, 2)
np.testing.assert_equal(len(x_tr), 3)
assert len(x_tr) == 3
assert x_tr.shape == (3, 1, 1, 2)
assert x_tr.ndim == 4
np.testing.assert_equal(x_tr[0], [[[[0,0]]]])
new_x = copy(x_tr)
assert x_tr[0].shape == new_x[0].shape
assert x_tr.conditions == ["A", "B"]
def test_randomsignalscale():
x_orig = np.ones((3,1,1,2))
x_tr = RandomSignalScale(Array('test', x_orig), .1)
assert x_tr[0].shape == (1, 1, 1, 2)
assert x_tr[:3].shape == (3, 1, 1, 2)
assert x_tr[[0,1]].shape == (2, 1, 1, 2)
np.testing.assert_equal(len(x_tr), 3)
assert len(x_tr) == 3
assert x_tr.shape == (3, 1, 1, 2)
assert x_tr.ndim == 4
new_x = copy(x_tr)
assert x_tr[0].shape == new_x[0].shape
assert x_tr.conditions == None
def test_randomshift():
x_orig = np.zeros((1,4,1,4))
x_orig[0, 0, 0,0] = 1
x_orig[0, 1, 0,1] = 1
x_orig[0, 2, 0,2] = 1
x_orig[0, 3, 0,3] = 1
x_tr = RandomShift(Array('test', x_orig), 1)
assert x_tr[0].shape == (1, 4, 1, 4)
np.testing.assert_equal(len(x_tr), 1)
assert x_tr.shape == (1, 4, 1, 4)
assert x_tr.ndim == 4
new_x = copy(x_tr)
assert x_tr[0].shape == new_x[0].shape
assert x_tr.conditions == None
x_tr = RandomShift(Array('test', x_orig), 1, True)
assert x_tr[0].shape == (1, 4, 1, 4)
np.testing.assert_equal(len(x_tr), 1)
assert x_tr.shape == (1, 4, 1, 4)
assert x_tr.ndim == 4
new_x = copy(x_tr)
assert x_tr[0].shape == new_x[0].shape
assert x_tr.conditions == None