Debian Codegraph @

nixio/test/test_data_view.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # Copyright © 2019, German Neuroinformatics Node (G-Node)
3 #
4 # All rights reserved.
5 #
6 # Redistribution and use in section and binary forms, with or without
7 # modification, are permitted under the terms of the BSD License. See
8 # LICENSE file in the root of the Project.
9 import os
10 import unittest
11 import numpy as np
12 import nixio as nix
13 from .tmp import TempDir
14
15
16 class TestDataView(unittest.TestCase):
17
18 def setUp(self):
19 self.tmpdir = TempDir("dvtest")
20 self.testfilename = os.path.join(self.tmpdir.path, "dvtest.nix")
21 self.file = nix.File.open(self.testfilename, nix.FileMode.Overwrite)
22 self.data = np.random.random_sample((40, 80))
23 block = self.file.create_block("testblock", "nix.test.block")
24 block.create_data_array("data", "nix.test.data", data=self.data)
25
26 def tearDown(self):
27 del self.file.blocks[0]
28 self.file.close()
29 self.tmpdir.cleanup()
30
31 def test_data_view_read(self):
32 da = self.file.blocks[0].data_arrays[0]
33 dv = da.get_slice((10, 3), extents=(1, 3))
34 np.testing.assert_almost_equal(dv[:], da[10:11, 3:6])
35
36 def test_data_view_read_2d_str_array(self):
37 numbers = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]
38 data = np.empty((10, 4), dtype=str)
39 for i in range(10):
40 for j in range(4):
41 data[i, j] = " ".join([numbers[i], numbers[j]])
42 da = self.file.blocks[0].create_data_array("str_2d_array", "test", dtype=nix.DataType.String, data=data)
43 da.append_set_dimension()
44 da.append_set_dimension()
45
46 dv = da.get_slice((0, 0), extents=(10, 4))
47 npeq = np.testing.assert_equal
48 npeq(dv.shape, da.shape)
49 npeq(dv[:], data)
50 for d in dv[:]:
51 assert("numpy.str_" not in str(type(d)))
52
53 def test_data_view_fancy_slicing(self):
54 da = self.file.blocks[0].data_arrays[0]
55 dv = da.get_slice((5, 8), extents=(10, 20))
56
57 npeq = np.testing.assert_almost_equal
58
59 # when stop > length, stops at len-1
60 npeq(dv[:100, 1], da[5:15, 9])
61 npeq(dv[0, 5:2000], da[5, 13:28])
62 npeq(dv[9:100, 18:1000], da[14:15, 26:28])
63
64 # negative slicing
65 npeq(dv[-5:, 1], da[10:15, 9])
66 npeq(dv[-5:-2, -1], da[10:13, 27])
67 npeq(dv[:, -10:13], da[5:15, 18:21])
68
69 def test_data_view_write_direct(self):
70 da = self.file.blocks[0].data_arrays[0]
71 np.testing.assert_almost_equal(da[:], self.data)
72
73 dv = da.get_slice((10, 3), extents=(1, 3))
74 dv.write_direct([1, 2, 3])
75
76 newdata = self.data.copy()
77 newdata[10:11, 3:6] = [1, 2, 3]
78
79 np.testing.assert_almost_equal(da[:], newdata)
80
81 def test_data_view_write_index(self):
82 """
83 Write through DataView to the underlying DataArray using [slice]
84 notation.
85
86 Update copy of underlying data with expected values and compare
87 directly to DataArray for assertions.
88 """
89 da = self.file.blocks[0].data_arrays[0]
90 dv = da.get_slice((10, 20), extents=(20, 15))
91 newdata = self.data.copy()
92
93 npeq = np.testing.assert_almost_equal
94
95 # straightforward slicing
96 rand_data = np.random.random_sample((3, 4))
97 dv[0:3, 1:5] = rand_data
98 newdata[10:13, 21:25] = rand_data
99 npeq(da[:], newdata)
100
101 # slicing with steps
102 rand_data = np.random.random_sample((5, 3))
103 dv[0:20:4, 0:15:5] = rand_data
104 newdata[10:30:4, 20:35:5] = rand_data
105 npeq(da[:], newdata)
106
107 # steps only
108 rand_data = np.random.random_sample((5, 3))
109 dv[::4, ::5] = rand_data
110 newdata[10:30:4, 20:35:5] = rand_data
111 npeq(da[:], newdata)
112
113 # plain index
114 dv[1, 1] = 20
115 newdata[11, 21] = 20
116 npeq(da[:], newdata)
117
118 # negative index
119 dv[-1, -1] = 80
120 newdata[29, 34] = 80
121 npeq(da[:], newdata)
122
123 # negative slice start
124 rand_data = np.random.random_sample(3)
125 dv[-3::, 10] = rand_data
126 newdata[27:30, 30] = rand_data
127 npeq(da[:], newdata)
128
129 # negative slice stop
130 rand_data = np.random.random_sample(15)
131 dv[:-5, 2] = rand_data
132 newdata[10:25, 22] = rand_data
133 npeq(da[:], newdata)
134
135 # negative slice start and stop
136 rand_data = np.random.random_sample(10)
137 dv[10, -15:-5] = rand_data
138 newdata[20, 20:30] = rand_data
139 npeq(da[:], newdata)
140
141 def test_data_view_oob(self):
142 da = self.file.blocks[0].data_arrays[0]
143
144 dv = da.get_slice((41, 81), extents=(1, 1))
145 assert not dv.valid
146 assert "OutOfBounds error" in dv.debug_message
147
148 dv = da.get_slice((0, 0), extents=(100, 5))
149 assert not dv.valid
150 assert "OutOfBounds error" in dv.debug_message
151
152 with self.assertRaises(nix.exceptions.IncompatibleDimensions):
153 da.get_slice((0, 0, 0), extents=(5, 5, 5))
154
155 dv = da.get_slice((5, 8), extents=(10, 20))
156 assert dv.valid
157
158 with self.assertRaises(nix.exceptions.OutOfBounds):
159 _ = dv[12, :]
160
161 with self.assertRaises(nix.exceptions.OutOfBounds):
162 _ = dv[12, :]
163
164 with self.assertRaises(nix.exceptions.OutOfBounds):
165 _ = dv[10]
166
167 with self.assertRaises(nix.exceptions.OutOfBounds):
168 _ = dv[0, 20]
169
170 with self.assertRaises(nix.exceptions.OutOfBounds):
171 _ = dv[:, 25]
172
173 with self.assertRaises(nix.exceptions.OutOfBounds):
174 _ = dv[-11]
175
176 with self.assertRaises(nix.exceptions.OutOfBounds):
177 _ = dv[0, -21]
178
179 def test_data_view_ellipsis(self):
180 block = self.file.blocks[0]
181 data = np.random.random_sample((10, 11, 12, 13, 14))
182 da = block.create_data_array("data2", "nix.test.data", data=data)
183
184 npeq = np.testing.assert_almost_equal
185
186 dv = da.get_slice((5, 6, 7, 8, 9), (5, 5, 5, 5, 5))
187 npeq(dv[1, ..., 2], da[6, 6:11, 7:12, 8:13, 11])
188 npeq(dv[..., 0, 0], da[5:10, 6:11, 7:12, 8, 9])
189 npeq(dv[1:3, 0, ...], da[6:8, 6, 7:12, 8:13, 9:14])
190 npeq(dv[1:3, :, ...], da[6:8, 6:11, 7:12, 8:13, 9:14])