|
1
|
|
|
# coding=utf-8 |
|
2
|
|
|
|
|
3
|
|
|
""" |
|
4
|
|
|
Tests for deepreg/dataset/util.py in |
|
5
|
|
|
pytest style |
|
6
|
|
|
""" |
|
7
|
|
|
|
|
8
|
|
|
import h5py |
|
9
|
|
|
import numpy as np |
|
10
|
|
|
import pytest |
|
11
|
|
|
from testfixtures import TempDirectory |
|
12
|
|
|
|
|
13
|
|
|
import deepreg.dataset.util as util |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
View Code Duplication |
def test_sorted_h5_keys(): |
|
|
|
|
|
|
17
|
|
|
""" |
|
18
|
|
|
Test to check a key is returned with one entry |
|
19
|
|
|
""" |
|
20
|
|
|
with TempDirectory() as tempdir: |
|
21
|
|
|
# Creating some dummy data |
|
22
|
|
|
d1 = np.random.random(size=(1000, 20)) |
|
23
|
|
|
hf = h5py.File(tempdir.path + "data.h5", "w") |
|
24
|
|
|
hf.create_dataset("dataset_1", data=d1) |
|
25
|
|
|
hf.close() |
|
26
|
|
|
# Checking func returns the same thing |
|
27
|
|
|
expected = ["dataset_1"] |
|
28
|
|
|
actual = util.get_h5_sorted_keys(tempdir.path + "data.h5") |
|
29
|
|
|
assert expected == actual |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
View Code Duplication |
def test_sorted_h5_keys_many(): |
|
|
|
|
|
|
33
|
|
|
""" |
|
34
|
|
|
Test to check a key is returned with many entries |
|
35
|
|
|
""" |
|
36
|
|
|
with TempDirectory() as tempdir: |
|
37
|
|
|
# Creating some dummy data |
|
38
|
|
|
d1 = np.random.random(size=(10, 20)) |
|
39
|
|
|
hf = h5py.File(tempdir.path + "data.h5", "w") |
|
40
|
|
|
# Adding entries in different order |
|
41
|
|
|
hf.create_dataset("dataset_1", data=d1) |
|
42
|
|
|
hf.create_dataset("dataset_3", data=d1) |
|
43
|
|
|
hf.create_dataset("dataset_2", data=d1) |
|
44
|
|
|
hf.close() |
|
45
|
|
|
# Checking func returns the same thing |
|
46
|
|
|
expected = ["dataset_1", "dataset_2", "dataset_3"] |
|
47
|
|
|
actual = util.get_h5_sorted_keys(tempdir.path + "data.h5") |
|
48
|
|
|
assert expected == actual |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
def test_get_sorted_file_paths_in_dir_with_suffix(): |
|
52
|
|
|
""" |
|
53
|
|
|
Checking sorted file names returned |
|
54
|
|
|
""" |
|
55
|
|
|
|
|
56
|
|
|
# one dir, single suffix |
|
57
|
|
|
with TempDirectory() as tempdir: |
|
58
|
|
|
tempdir.write((tempdir.path + "/a.txt"), (bytes(1))) |
|
59
|
|
|
tempdir.write((tempdir.path + "/b.txt"), (bytes(1))) |
|
60
|
|
|
tempdir.write((tempdir.path + "/c.txt"), (bytes(1))) |
|
61
|
|
|
expected = [("a", "txt"), ("b", "txt"), ("c", "txt")] |
|
62
|
|
|
actual = util.get_sorted_file_paths_in_dir_with_suffix(tempdir.path, "txt") |
|
63
|
|
|
assert expected == actual |
|
64
|
|
|
|
|
65
|
|
|
# one dir, multiple suffixes |
|
66
|
|
|
with TempDirectory() as tempdir: |
|
67
|
|
|
tempdir.write((tempdir.path + "/a.txt"), (bytes(1))) |
|
68
|
|
|
tempdir.write((tempdir.path + "/b.txt"), (bytes(1))) |
|
69
|
|
|
tempdir.write((tempdir.path + "/c.md"), (bytes(1))) |
|
70
|
|
|
expected = [("a", "txt"), ("b", "txt"), ("c", "md")] |
|
71
|
|
|
actual = util.get_sorted_file_paths_in_dir_with_suffix( |
|
72
|
|
|
tempdir.path, ["txt", "md"] |
|
73
|
|
|
) |
|
74
|
|
|
assert expected == actual |
|
75
|
|
|
|
|
76
|
|
|
# multiple sub-dirs, single suffix |
|
77
|
|
|
with TempDirectory() as tempdir: |
|
78
|
|
|
tempdir.write((tempdir.path + "/1/a.txt"), (bytes(1))) |
|
79
|
|
|
tempdir.write((tempdir.path + "/1/b.txt"), (bytes(1))) |
|
80
|
|
|
tempdir.write((tempdir.path + "/1/c.txt"), (bytes(1))) |
|
81
|
|
|
tempdir.write((tempdir.path + "/2/a.txt"), (bytes(1))) |
|
82
|
|
|
tempdir.write((tempdir.path + "/2/b.txt"), (bytes(1))) |
|
83
|
|
|
tempdir.write((tempdir.path + "/2/c.txt"), (bytes(1))) |
|
84
|
|
|
expected = [ |
|
85
|
|
|
("1/a", "txt"), |
|
86
|
|
|
("1/b", "txt"), |
|
87
|
|
|
("1/c", "txt"), |
|
88
|
|
|
("2/a", "txt"), |
|
89
|
|
|
("2/b", "txt"), |
|
90
|
|
|
("2/c", "txt"), |
|
91
|
|
|
] |
|
92
|
|
|
actual = util.get_sorted_file_paths_in_dir_with_suffix(tempdir.path, "txt") |
|
93
|
|
|
assert expected == actual |
|
94
|
|
|
|
|
95
|
|
|
# multiple sub-dirs, multiple suffixes |
|
96
|
|
|
with TempDirectory() as tempdir: |
|
97
|
|
|
tempdir.write((tempdir.path + "/1/a.txt"), (bytes(1))) |
|
98
|
|
|
tempdir.write((tempdir.path + "/1/b.txt"), (bytes(1))) |
|
99
|
|
|
tempdir.write((tempdir.path + "/1/c.txt"), (bytes(1))) |
|
100
|
|
|
tempdir.write((tempdir.path + "/2/a.txt"), (bytes(1))) |
|
101
|
|
|
tempdir.write((tempdir.path + "/2/b.txt"), (bytes(1))) |
|
102
|
|
|
tempdir.write((tempdir.path + "/2/c.md"), (bytes(1))) |
|
103
|
|
|
expected = [ |
|
104
|
|
|
("1/a", "txt"), |
|
105
|
|
|
("1/b", "txt"), |
|
106
|
|
|
("1/c", "txt"), |
|
107
|
|
|
("2/a", "txt"), |
|
108
|
|
|
("2/b", "txt"), |
|
109
|
|
|
("2/c", "md"), |
|
110
|
|
|
] |
|
111
|
|
|
actual = util.get_sorted_file_paths_in_dir_with_suffix( |
|
112
|
|
|
tempdir.path, ["txt", "md"] |
|
113
|
|
|
) |
|
114
|
|
|
assert expected == actual |
|
115
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
def test_check_difference_between_two_lists(): |
|
118
|
|
|
""" |
|
119
|
|
|
Check check_difference_between_two_lists by verifying ValueError |
|
120
|
|
|
""" |
|
121
|
|
|
# same lists, no error |
|
122
|
|
|
list1 = list2 = [0, 1, 2] |
|
123
|
|
|
util.check_difference_between_two_lists(list1, list2, name="same case") |
|
124
|
|
|
|
|
125
|
|
|
# diff lists with same unique numbers |
|
126
|
|
|
list_1 = [0, 1, 2] |
|
127
|
|
|
list_2 = [1, 2, 0] |
|
128
|
|
|
with pytest.raises(ValueError) as err_info: |
|
129
|
|
|
util.check_difference_between_two_lists(list_1, list_2, name="diff case") |
|
130
|
|
|
assert "diff case are not identical" in str(err_info.value) |
|
131
|
|
|
|
|
132
|
|
|
# totally diff lists |
|
133
|
|
|
list_1 = [0, 1, 2] |
|
134
|
|
|
list_2 = [3, 4, 5] |
|
135
|
|
|
with pytest.raises(ValueError) as err_info: |
|
136
|
|
|
util.check_difference_between_two_lists(list_1, list_2, name="diff case") |
|
137
|
|
|
assert "diff case are not identical" in str(err_info.value) |
|
138
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
def test_label_indices_sample(): |
|
141
|
|
|
""" |
|
142
|
|
|
Assert random number for passed arg returned |
|
143
|
|
|
""" |
|
144
|
|
|
expected = {0, 1, 2, 3} |
|
145
|
|
|
actual = util.get_label_indices(4, "sample") |
|
146
|
|
|
assert expected.intersection(set(actual)) |
|
147
|
|
|
|
|
148
|
|
|
|
|
149
|
|
|
def test_label_indices_all(): |
|
150
|
|
|
""" |
|
151
|
|
|
Assert list with all labels returned if all passed |
|
152
|
|
|
""" |
|
153
|
|
|
expected = [0, 1, 2] |
|
154
|
|
|
actual = util.get_label_indices(3, "all") |
|
155
|
|
|
assert expected == actual |
|
156
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
def test_label_indices_unknown(): |
|
159
|
|
|
""" |
|
160
|
|
|
Assert ValueError raised if unknown string passed to sample |
|
161
|
|
|
label |
|
162
|
|
|
""" |
|
163
|
|
|
with pytest.raises(ValueError): |
|
164
|
|
|
util.get_label_indices(3, "random_str") |
|
165
|
|
|
|