test_preprocess.py 7.14 KB
Newer Older
1
# @author lucasmiranda42
2
3
4
5
6
7
8
9
# encoding: utf-8
# module deepof

"""

Testing module for deepof.preprocess

"""
10
11
12
13

from hypothesis import given
from hypothesis import settings
from hypothesis import strategies as st
lucas_miranda's avatar
lucas_miranda committed
14
from collections import defaultdict
15
16
17
from deepof.utils import *
import deepof.preprocess
import pytest
lucas_miranda's avatar
lucas_miranda committed
18
19


lucas_miranda's avatar
lucas_miranda committed
20
21
22
23
24
25
@settings(deadline=None)
@given(
    table_type=st.integers(min_value=0, max_value=2),
    arena_type=st.integers(min_value=0, max_value=1),
)
def test_project_init(table_type, arena_type):
lucas_miranda's avatar
lucas_miranda committed
26
27

    table_type = [".h5", ".csv", ".foo"][table_type]
lucas_miranda's avatar
lucas_miranda committed
28
29
30
31
32
33
34
    arena_type = ["circular", "foo"][arena_type]

    if arena_type == "foo":
        with pytest.raises(NotImplementedError):
            prun = deepof.preprocess.project(
                path=os.path.join(".", "tests", "test_examples"),
                arena=arena_type,
lucas_miranda's avatar
lucas_miranda committed
35
                arena_dims=tuple([380]),
lucas_miranda's avatar
lucas_miranda committed
36
37
38
39
40
41
42
43
                angles=False,
                video_format=".mp4",
                table_format=table_type,
            )
    else:
        prun = deepof.preprocess.project(
            path=os.path.join(".", "tests", "test_examples"),
            arena=arena_type,
lucas_miranda's avatar
lucas_miranda committed
44
            arena_dims=tuple([380]),
lucas_miranda's avatar
lucas_miranda committed
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
            angles=False,
            video_format=".mp4",
            table_format=table_type,
        )

    if table_type != ".foo" and arena_type != "foo":

        assert type(prun) == deepof.preprocess.project
        assert type(prun.load_tables(verbose=True)) == tuple
        assert type(prun.get_scale) == np.ndarray
        print(prun)

    elif table_type == ".foo" and arena_type != "foo":
        with pytest.raises(NotImplementedError):
            prun.load_tables(verbose=True)


@settings(deadline=None)
@given(
    nodes=st.integers(min_value=0, max_value=1),
    ego=st.integers(min_value=0, max_value=2),
)
def test_get_distances(nodes, ego):

    nodes = ["All", ["Center", "Nose", "Tail_base"]][nodes]
    ego = [False, "Center", "Nose"][ego]
lucas_miranda's avatar
lucas_miranda committed
71
72
73
74

    prun = deepof.preprocess.project(
        path=os.path.join(".", "tests", "test_examples"),
        arena="circular",
lucas_miranda's avatar
lucas_miranda committed
75
        arena_dims=tuple([380]),
lucas_miranda's avatar
lucas_miranda committed
76
77
        angles=False,
        video_format=".mp4",
lucas_miranda's avatar
lucas_miranda committed
78
79
80
        table_format=".h5",
        distances=nodes,
        ego=ego,
lucas_miranda's avatar
lucas_miranda committed
81
82
    )

lucas_miranda's avatar
lucas_miranda committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    prun = prun.get_distances(prun.load_tables()[0], verbose=True)

    assert type(prun) == dict


@settings(deadline=None)
@given(
    nodes=st.integers(min_value=0, max_value=1),
    ego=st.integers(min_value=0, max_value=2),
)
def test_get_angles(nodes, ego):

    nodes = ["All", ["Center", "Nose", "Tail_base"]][nodes]
    ego = [False, "Center", "Nose"][ego]

    prun = deepof.preprocess.project(
        path=os.path.join(".", "tests", "test_examples"),
        arena="circular",
lucas_miranda's avatar
lucas_miranda committed
101
        arena_dims=tuple([380]),
lucas_miranda's avatar
lucas_miranda committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
        video_format=".mp4",
        table_format=".h5",
        distances=nodes,
        ego=ego,
    )

    prun = prun.get_angles(prun.load_tables()[0], verbose=True)

    assert type(prun) == dict


@settings(deadline=None)
@given(
    nodes=st.integers(min_value=0, max_value=1),
    ego=st.integers(min_value=0, max_value=2),
)
def test_run(nodes, ego):

    nodes = ["All", ["Center", "Nose", "Tail_base"]][nodes]
    ego = [False, "Center", "Nose"][ego]

    prun = deepof.preprocess.project(
        path=os.path.join(".", "tests", "test_examples"),
        arena="circular",
lucas_miranda's avatar
lucas_miranda committed
126
        arena_dims=tuple([380]),
lucas_miranda's avatar
lucas_miranda committed
127
128
129
130
131
132
133
        video_format=".mp4",
        table_format=".h5",
        distances=nodes,
        ego=ego,
    ).run(verbose=True)

    assert type(prun) == deepof.preprocess.coordinates
lucas_miranda's avatar
lucas_miranda committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149


@settings(deadline=None)
@given(
    nodes=st.integers(min_value=0, max_value=1),
    ego=st.integers(min_value=0, max_value=2),
    sampler=st.data(),
)
def test_get_table_dicts(nodes, ego, sampler):

    nodes = ["All", ["Center", "Nose", "Tail_base"]][nodes]
    ego = [False, "Center", "Nose"][ego]

    prun = deepof.preprocess.project(
        path=os.path.join(".", "tests", "test_examples"),
        arena="circular",
lucas_miranda's avatar
lucas_miranda committed
150
        arena_dims=tuple([380]),
lucas_miranda's avatar
lucas_miranda committed
151
152
153
154
        video_format=".mp4",
        table_format=".h5",
        distances=nodes,
        ego=ego,
lucas_miranda's avatar
lucas_miranda committed
155
156
157
    ).run(verbose=False)

    algn = sampler.draw(st.one_of(st.just(False), st.just("Nose")))
lucas_miranda's avatar
lucas_miranda committed
158
159
    polar = sampler.draw(st.booleans())
    speed = sampler.draw(st.integers(min_value=0, max_value=5))
lucas_miranda's avatar
lucas_miranda committed
160
161
162

    coords = prun.get_coords(
        center=sampler.draw(st.one_of(st.just("arena"), st.just("Center"))),
lucas_miranda's avatar
lucas_miranda committed
163
        polar=polar,
lucas_miranda's avatar
lucas_miranda committed
164
        length=sampler.draw(st.one_of(st.just(False), st.just("00:10:00"))),
lucas_miranda's avatar
lucas_miranda committed
165
        align=algn,
lucas_miranda's avatar
lucas_miranda committed
166
167
168
169
170
    )
    speeds = prun.get_coords(
        center=sampler.draw(st.one_of(st.just("arena"), st.just("Center"))),
        polar=sampler.draw(st.booleans()),
        length=sampler.draw(st.one_of(st.just(False), st.just("00:10:00"))),
lucas_miranda's avatar
lucas_miranda committed
171
        speed=speed,
lucas_miranda's avatar
lucas_miranda committed
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
    )
    distances = prun.get_distances(
        length=sampler.draw(st.one_of(st.just(False), st.just("00:10:00"))),
        speed=sampler.draw(st.integers(min_value=0, max_value=5)),
    )
    angles = prun.get_angles(
        degrees=sampler.draw(st.booleans()),
        length=sampler.draw(st.one_of(st.just(False), st.just("00:10:00"))),
        speed=sampler.draw(st.integers(min_value=0, max_value=5)),
    )

    # deepof.coordinates testing

    assert type(coords) == deepof.preprocess.table_dict
    assert type(speeds) == deepof.preprocess.table_dict
    assert type(distances) == deepof.preprocess.table_dict
    assert type(angles) == deepof.preprocess.table_dict
    assert type(prun.get_videos()) == list
    assert prun.get_exp_conditions is None
    assert type(prun.get_quality()) == defaultdict
    assert type(prun.get_arenas) == tuple

    # deepof.table_dict testing

    table = sampler.draw(
lucas_miranda's avatar
lucas_miranda committed
197
198
199
200
        st.one_of(
            st.just(coords), st.just(speeds), st.just(distances), st.just(angles)
        ),
        st.just(deepof.preprocess.merge_tables(coords, speeds, distances, angles)),
lucas_miranda's avatar
lucas_miranda committed
201
202
    )

lucas_miranda's avatar
lucas_miranda committed
203
204
205
206
207
208
209
    assert table.filter(["test"]) == table
    tset = table.get_training_set(
        test_videos=sampler.draw(st.integers(min_value=0, max_value=len(table) - 1))
    )
    assert len(tset) == 2
    assert type(tset[0]) == np.ndarray

lucas_miranda's avatar
lucas_miranda committed
210
211
212
213
214
215
216
    if table._type == "coords" and algn == "Nose" and polar is False and speed == 0:
        align = sampler.draw(
            st.one_of(st.just(False), st.just("all"), st.just("center"))
        )
    else:
        align = False

lucas_miranda's avatar
lucas_miranda committed
217
    prep = table.preprocess(
lucas_miranda's avatar
lucas_miranda committed
218
219
220
221
222
223
224
225
226
        window_size=11,
        window_step=1,
        scale=sampler.draw(st.one_of(st.just("standard"), st.just("minmax"))),
        test_videos=sampler.draw(st.integers(min_value=0, max_value=len(table) - 1)),
        verbose=True,
        conv_filter=sampler.draw(st.one_of(st.just(None), st.just("gaussian"))),
        sigma=sampler.draw(st.floats(min_value=0.5, max_value=5.0)),
        shift=sampler.draw(st.floats(min_value=-1.0, max_value=1.0)),
        shuffle=sampler.draw(st.booleans()),
lucas_miranda's avatar
lucas_miranda committed
227
        align=align,
lucas_miranda's avatar
lucas_miranda committed
228
    )
lucas_miranda's avatar
lucas_miranda committed
229

230
    assert type(prep) == np.ndarray or type(prep) == tuple
lucas_miranda's avatar
lucas_miranda committed
231
232
233
234
235
236
237
238
239

    if type(prep) == tuple:
        assert type(prep[0]) == np.ndarray

    # deepof dimensionality reduction testing

    assert type(table.random_projection(n_components=2, sample=50)) == tuple
    assert type(table.pca(n_components=2, sample=50)) == tuple
    assert type(table.tsne(n_components=2, sample=50)) == tuple