| 1 |
|
| 2 |
) |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
import os |
| 10 |
import unittest |
| 11 |
import nixio as nix |
| 12 |
from .tmp import TempDir |
| 13 |
|
| 14 |
|
| 15 |
class TestBlock(unittest.TestCase): |
| 16 |
|
| 17 |
def setUp(self): |
| 18 |
self.tmpdir = TempDir("blocktest") |
| 19 |
self.testfilename = os.path.join(self.tmpdir.path, "blocktest.nix") |
| 20 |
self.file = nix.File.open(self.testfilename, nix.FileMode.Overwrite) |
| 21 |
self.block = self.file.create_block("test block", "recordingsession") |
| 22 |
self.other = self.file.create_block("other block", "recordingsession") |
| 23 |
|
| 24 |
def tearDown(self): |
| 25 |
del self.file.blocks[self.block.id] |
| 26 |
del self.file.blocks[self.other.id] |
| 27 |
self.file.close() |
| 28 |
self.tmpdir.cleanup() |
| 29 |
|
| 30 |
def test_block_eq(self): |
| 31 |
assert self.block == self.block |
| 32 |
assert not self.block == self.other |
| 33 |
assert self.block is not None |
| 34 |
|
| 35 |
def test_block_id(self): |
| 36 |
assert self.block.id is not None |
| 37 |
|
| 38 |
def test_block_name(self): |
| 39 |
assert self.block.name is not None |
| 40 |
|
| 41 |
def test_block_type(self): |
| 42 |
def set_none(): |
| 43 |
self.block.type = None |
| 44 |
|
| 45 |
assert self.block.type is not None |
| 46 |
self.assertRaises(Exception, set_none) |
| 47 |
|
| 48 |
self.block.type = "foo type" |
| 49 |
assert self.block.type == "foo type" |
| 50 |
|
| 51 |
def test_block_definition(self): |
| 52 |
assert self.block.definition is None |
| 53 |
|
| 54 |
self.block.definition = "definition" |
| 55 |
assert self.block.definition == "definition" |
| 56 |
|
| 57 |
self.block.definition = None |
| 58 |
assert self.block.definition is None |
| 59 |
|
| 60 |
def test_block_timestamps(self): |
| 61 |
created_at = self.block.created_at |
| 62 |
assert created_at > 0 |
| 63 |
|
| 64 |
updated_at = self.block.updated_at |
| 65 |
assert updated_at > 0 |
| 66 |
|
| 67 |
self.block.force_created_at(1403530068) |
| 68 |
assert self.block.created_at == 1403530068 |
| 69 |
|
| 70 |
def test_block_data_arrays(self): |
| 71 |
assert len(self.block.data_arrays) == 0 |
| 72 |
|
| 73 |
data_array = self.block.create_data_array("test data_array", |
| 74 |
"recordingsession", |
| 75 |
nix.DataType.Int32, (0, )) |
| 76 |
|
| 77 |
assert len(self.block.data_arrays) == 1 |
| 78 |
|
| 79 |
|
| 80 |
assert data_array.id in self.block.data_arrays |
| 81 |
assert "notexist" not in self.block.data_arrays |
| 82 |
|
| 83 |
assert data_array.id == self.block.data_arrays[0].id |
| 84 |
assert data_array.id == self.block.data_arrays[-1].id |
| 85 |
|
| 86 |
del self.block.data_arrays[0] |
| 87 |
|
| 88 |
assert len(self.block.data_arrays) == 0 |
| 89 |
|
| 90 |
data_array = self.block.create_data_array("test data_array", |
| 91 |
"recordingsession", |
| 92 |
nix.DataType.Int32, (0, ), label="voltage", unit="V") |
| 93 |
|
| 94 |
assert len(self.block.data_arrays) == 1 |
| 95 |
assert data_array.unit == "V" |
| 96 |
assert data_array.label == "voltage" |
| 97 |
|
| 98 |
del self.block.data_arrays[0] |
| 99 |
|
| 100 |
assert len(self.block.data_arrays) == 0 |
| 101 |
|
| 102 |
|
| 103 |
def test_block_multi_tags(self): |
| 104 |
assert len(self.block.multi_tags) == 0 |
| 105 |
|
| 106 |
data_array = self.block.create_data_array("test array", |
| 107 |
"recording", |
| 108 |
nix.DataType.Int32, (0, )) |
| 109 |
multi_tag = self.block.create_multi_tag("test multi_tag", |
| 110 |
"recordingsession", data_array) |
| 111 |
|
| 112 |
assert len(self.block.multi_tags) == 1 |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
assert multi_tag.id in self.block.multi_tags |
| 117 |
assert "notexist" not in self.block.multi_tags |
| 118 |
|
| 119 |
assert multi_tag.id == self.block.multi_tags[0].id |
| 120 |
assert multi_tag.id == self.block.multi_tags[-1].id |
| 121 |
|
| 122 |
del self.block.multi_tags[0] |
| 123 |
|
| 124 |
assert len(self.block.multi_tags) == 0 |
| 125 |
|
| 126 |
def test_block_tags(self): |
| 127 |
assert len(self.block.tags) == 0 |
| 128 |
|
| 129 |
tag = self.block.create_tag("test tag", "recordingsession", [0]) |
| 130 |
|
| 131 |
assert len(self.block.tags) == 1 |
| 132 |
|
| 133 |
assert tag in self.block.tags |
| 134 |
assert tag.id in self.block.tags |
| 135 |
assert "notexist" not in self.block.tags |
| 136 |
|
| 137 |
assert tag.id == self.block.tags[0].id |
| 138 |
assert tag.id == self.block.tags[-1].id |
| 139 |
|
| 140 |
del self.block.tags[0] |
| 141 |
|
| 142 |
assert len(self.block.tags) == 0 |
| 143 |
|
| 144 |
def test_block_sources(self): |
| 145 |
assert len(self.block.sources) == 0 |
| 146 |
|
| 147 |
source = self.block.create_source("test source", "electrode") |
| 148 |
|
| 149 |
assert len(self.block.sources) == 1 |
| 150 |
|
| 151 |
assert source in self.block.sources |
| 152 |
assert source.id in self.block.sources |
| 153 |
assert "notexist" not in self.block.sources |
| 154 |
|
| 155 |
assert source.id == self.block.sources[0].id |
| 156 |
assert source.id == self.block.sources[-1].id |
| 157 |
|
| 158 |
del self.block.sources[0] |
| 159 |
|
| 160 |
assert len(self.block.sources) == 0 |
| 161 |
|
| 162 |
def test_block_find_sources(self): |
| 163 |
for i in range(2): |
| 164 |
self.block.create_source("level1-p0-s" + str(i), "dummy") |
| 165 |
for i in range(2): |
| 166 |
self.block.sources[0].create_source("level2-p1-s" + str(i), |
| 167 |
"dummy") |
| 168 |
for i in range(2): |
| 169 |
self.block.sources[1].create_source("level2-p2-s" + str(i), |
| 170 |
"dummy") |
| 171 |
for i in range(2): |
| 172 |
self.block.sources[0].sources[0].create_source( |
| 173 |
"level3-p1-s" + str(i), "dummy" |
| 174 |
) |
| 175 |
|
| 176 |
assert len(self.block.find_sources()) == 8 |
| 177 |
assert len(self.block.find_sources(limit=1)) == 2 |
| 178 |
assert len(self.block.find_sources(filtr=lambda x: "level2-p1-s" in x.name)) == 2 |
| 179 |
assert len(self.block.find_sources(filtr=lambda x: "level2-p1-s" in x.name, limit=1)) == 0 |
| 180 |
|
| 181 |
def test_block_groups(self): |
| 182 |
assert len(self.block.groups) == 0 |
| 183 |
|
| 184 |
group = self.block.create_group("test group", "RecordingChannelGroup") |
| 185 |
|
| 186 |
assert len(self.block.groups) == 1 |
| 187 |
|
| 188 |
assert group in self.block.groups |
| 189 |
assert group.id in self.block.groups |
| 190 |
assert "notexist" not in self.block.groups |
| 191 |
|
| 192 |
assert group.id == self.block.groups[0].id |
| 193 |
assert group.id == self.block.groups[-1].id |
| 194 |
|
| 195 |
del self.block.groups[0] |
| 196 |
|
| 197 |
assert len(self.block.groups) == 0 |
| 198 |
|
| 199 |
def test_copy_on_block(self): |
| 200 |
tar_filename = os.path.join(self.tmpdir.path, "copytarget.nix") |
| 201 |
tar_file = nix.File.open(tar_filename, nix.FileMode.Overwrite) |
| 202 |
blk2 = tar_file.create_block("blk2", "blk") |
| 203 |
data = [(1, 2, 3), (4, 5, 6)] |
| 204 |
da = self.block.create_data_array("da1", 'grp da1', data=data) |
| 205 |
mt = self.block.create_multi_tag("mt1", "some mt", da) |
| 206 |
namelist = ['name', 'id', 'time'] |
| 207 |
dtlist = [nix.DataType.Int64, str, |
| 208 |
nix.DataType.Float] |
| 209 |
arr = [(1, "cat", 20.18), (2, 'dog', 20.15), (2, 'dog', 20.15)] |
| 210 |
df = self.block.create_data_frame('test1', 'for_test', |
| 211 |
col_names=namelist, |
| 212 |
col_dtypes=dtlist, data=arr) |
| 213 |
tag = self.block.create_tag("a tag", "some tag", position=(4, 5, 6)) |
| 214 |
blk2.create_multi_tag(copy_from=mt) |
| 215 |
blk2.create_data_array(copy_from=da) |
| 216 |
blk2.create_data_frame(copy_from=df) |
| 217 |
blk2.create_tag(copy_from=tag) |
| 218 |
assert mt == blk2.multi_tags[0] |
| 219 |
assert da == blk2.data_arrays[0] |
| 220 |
assert df == blk2.data_frames[0] |
| 221 |
assert tag == blk2.tags[0] |
| 222 |
tar_file.close() |
| 223 |
self.block.create_data_array("da2", 'grp da2', data=[(1, 2, 3)]) |
| 224 |
da2 = self.block.data_arrays[1] |
| 225 |
mt2 = self.block.multi_tags[0] |
| 226 |
self.assertRaises(NameError, self.block.create_data_array, |
| 227 |
copy_from=da2) |
| 228 |
self.block.create_data_array(name="new da name", copy_from=da2) |
| 229 |
self.block.create_multi_tag(name="new mt name", copy_from=mt2) |
| 230 |
assert self.block.multi_tags[0] == mt2 |
| 231 |
assert self.block.data_arrays[1] == da2 |