| 1 |
|
| 2 |
) |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
import os |
| 10 |
import time |
| 11 |
import unittest |
| 12 |
from collections import OrderedDict |
| 13 |
import numpy as np |
| 14 |
import nixio as nix |
| 15 |
from nixio.exceptions import UnsupportedLinkType |
| 16 |
from .tmp import TempDir |
| 17 |
|
| 18 |
|
| 19 |
class TestTags(unittest.TestCase): |
| 20 |
|
| 21 |
def setUp(self): |
| 22 |
self.tmpdir = TempDir("tagtest") |
| 23 |
self.testfilename = os.path.join(self.tmpdir.path, "tagtest.nix") |
| 24 |
self.file = nix.File.open(self.testfilename, nix.FileMode.Overwrite) |
| 25 |
self.block = self.file.create_block("test block", "recordingsession") |
| 26 |
|
| 27 |
self.my_array = self.block.create_data_array("my array", "test", |
| 28 |
nix.DataType.Int16, (1,)) |
| 29 |
self.my_tag = self.block.create_tag( |
| 30 |
"my tag", "tag", [0] |
| 31 |
) |
| 32 |
self.my_tag.references.append(self.my_array) |
| 33 |
|
| 34 |
self.your_array = self.block.create_data_array( |
| 35 |
"your array", "test", nix.DataType.Int16, (1,) |
| 36 |
) |
| 37 |
self.your_tag = self.block.create_tag( |
| 38 |
"your tag", "tag", [0] |
| 39 |
) |
| 40 |
self.your_tag.references.append(self.your_array) |
| 41 |
|
| 42 |
def tearDown(self): |
| 43 |
del self.file.blocks[self.block.id] |
| 44 |
self.file.close() |
| 45 |
self.tmpdir.cleanup() |
| 46 |
|
| 47 |
def test_flex(self): |
| 48 |
tag1d = self.block.create_tag("1dim tag", "tag", [1]) |
| 49 |
tag2d = self.block.create_tag("2dim tag", "tag", [1, 1]) |
| 50 |
tag3d = self.block.create_tag("3dim tag", "tag", [1, 1, 1]) |
| 51 |
da1d = self.block.create_data_array("1d array", "da", |
| 52 |
data=np.arange(5)) |
| 53 |
da1d.append_sampled_dimension(1., label="time", unit="s") |
| 54 |
da2d = self.block.create_data_array("2d array", "da", |
| 55 |
data=np.arange(25).reshape((5, 5))) |
| 56 |
da2d.append_sampled_dimension(1., label="time", unit="s") |
| 57 |
da2d.append_set_dimension() |
| 58 |
da3d = self.block.create_data_array("3d array", "da", |
| 59 |
data=np.arange(125) |
| 60 |
.reshape((5, 5, 5))) |
| 61 |
da3d.append_sampled_dimension(1., label="time", unit="s") |
| 62 |
da3d.append_set_dimension() |
| 63 |
da3d.append_set_dimension() |
| 64 |
tag1d.extent = [1] |
| 65 |
tag1d.references.extend([da1d, da2d, da3d]) |
| 66 |
assert list(tag1d.references) == [da1d, da2d, da3d] |
| 67 |
ref1 = tag1d.tagged_data(1) |
| 68 |
for (ref_val, da_val) in zip(ref1[:].flatten(), da2d[1:3, :].flatten()): |
| 69 |
assert ref_val == da_val |
| 70 |
tag2d.extent = [1, 2] |
| 71 |
tag2d.references.extend([da1d, da2d, da3d]) |
| 72 |
tag2d.tagged_data(0) |
| 73 |
tag3d.extent = [1, 2, 3] |
| 74 |
tag3d.references.extend([da1d, da2d, da3d]) |
| 75 |
np.testing.assert_array_equal(tag3d.tagged_data(0), da1d[1:2]) |
| 76 |
np.testing.assert_array_equal(tag3d.tagged_data(1), da2d[1:2, 1:3]) |
| 77 |
np.testing.assert_array_equal(tag3d.tagged_data(2), da3d[1:2, 1:3, 1:4]) |
| 78 |
|
| 79 |
def test_tag_eq(self): |
| 80 |
assert self.my_tag == self.my_tag |
| 81 |
assert not self.my_tag == self.your_tag |
| 82 |
assert self.my_tag is not None |
| 83 |
|
| 84 |
def test_tag_id(self): |
| 85 |
assert self.my_tag.id is not None |
| 86 |
|
| 87 |
def test_tag_name(self): |
| 88 |
assert self.my_tag.name is not None |
| 89 |
|
| 90 |
def test_tag_type(self): |
| 91 |
def set_none(): |
| 92 |
self.my_tag.type = None |
| 93 |
|
| 94 |
assert self.my_tag.type is not None |
| 95 |
self.assertRaises(Exception, set_none) |
| 96 |
|
| 97 |
self.my_tag.type = "foo type" |
| 98 |
assert self.my_tag.type == "foo type" |
| 99 |
|
| 100 |
def test_tag_definition(self): |
| 101 |
assert self.my_tag.definition is None |
| 102 |
|
| 103 |
self.my_tag.definition = "definition" |
| 104 |
assert self.my_tag.definition == "definition" |
| 105 |
|
| 106 |
self.my_tag.definition = None |
| 107 |
assert self.my_tag.definition is None |
| 108 |
|
| 109 |
def test_tag_timestamps(self): |
| 110 |
created_at = self.my_tag.created_at |
| 111 |
assert created_at > 0 |
| 112 |
|
| 113 |
updated_at = self.my_tag.updated_at |
| 114 |
assert updated_at > 0 |
| 115 |
|
| 116 |
self.my_tag.force_created_at(1403530068) |
| 117 |
assert self.my_tag.created_at == 1403530068 |
| 118 |
|
| 119 |
def test_tag_units(self): |
| 120 |
assert self.my_tag.units == () |
| 121 |
|
| 122 |
self.my_tag.units = ["mV", "ms"] |
| 123 |
assert self.my_tag.units == ("mV", "ms") |
| 124 |
|
| 125 |
self.my_tag.units = [] |
| 126 |
assert self.my_tag.units == () |
| 127 |
|
| 128 |
def test_tag_position(self): |
| 129 |
assert self.my_tag.position == (0,) |
| 130 |
|
| 131 |
self.my_tag.position = (1.0, 2.0, 3.0) |
| 132 |
assert self.my_tag.position == (1.0, 2.0, 3.0) |
| 133 |
|
| 134 |
def test_tag_extent(self): |
| 135 |
assert self.my_tag.extent == () |
| 136 |
|
| 137 |
self.my_tag.extent = (1.0, 2.0, 3.0) |
| 138 |
assert self.my_tag.extent == (1.0, 2.0, 3.0) |
| 139 |
|
| 140 |
self.my_tag.extent = [] |
| 141 |
assert self.my_tag.extent == () |
| 142 |
|
| 143 |
def test_tag_references(self): |
| 144 |
assert len(self.my_tag.references) == 1 |
| 145 |
|
| 146 |
self.assertRaises(TypeError, self.my_tag.references.append, 100) |
| 147 |
|
| 148 |
reference1 = self.block.create_data_array("reference1", "stimuli", |
| 149 |
nix.DataType.Int16, (1,)) |
| 150 |
reference2 = self.block.create_data_array("reference2", "stimuli", |
| 151 |
nix.DataType.Int16, (1,)) |
| 152 |
|
| 153 |
self.my_tag.references.append(reference1) |
| 154 |
self.my_tag.references.append(reference2) |
| 155 |
|
| 156 |
assert reference1.name in self.my_tag.references |
| 157 |
|
| 158 |
assert len(self.my_tag.references) == 3 |
| 159 |
assert reference1 in self.my_tag.references |
| 160 |
assert reference2 in self.my_tag.references |
| 161 |
|
| 162 |
|
| 163 |
assert reference1 == self.my_tag.references[reference1.name] |
| 164 |
assert reference1 == self.my_tag.references[reference1.id] |
| 165 |
assert reference2 == self.my_tag.references[reference2.name] |
| 166 |
assert reference2 == self.my_tag.references[reference2.id] |
| 167 |
|
| 168 |
assert reference1.name in self.my_tag.references |
| 169 |
assert reference2.name in self.my_tag.references |
| 170 |
assert reference1.id in self.my_tag.references |
| 171 |
assert reference2.id in self.my_tag.references |
| 172 |
|
| 173 |
del self.my_tag.references[reference2] |
| 174 |
assert self.my_array in self.my_tag.references |
| 175 |
assert reference1 in self.my_tag.references |
| 176 |
|
| 177 |
del self.my_tag.references[reference1] |
| 178 |
assert len(self.my_tag.references) == 1 |
| 179 |
|
| 180 |
def test_tag_features(self): |
| 181 |
assert len(self.my_tag.features) == 0 |
| 182 |
|
| 183 |
data_array = self.block.create_data_array("feature", "stimuli", |
| 184 |
nix.DataType.Int16, (1,)) |
| 185 |
da_feature = self.my_tag.create_feature(data_array, nix.LinkType.Untagged) |
| 186 |
|
| 187 |
assert len(self.my_tag.features) == 1 |
| 188 |
|
| 189 |
assert da_feature in self.my_tag.features |
| 190 |
assert da_feature.id in self.my_tag.features |
| 191 |
assert "notexist" not in self.my_tag.features |
| 192 |
|
| 193 |
assert da_feature.id == self.my_tag.features[0].id |
| 194 |
assert da_feature.id == self.my_tag.features[-1].id |
| 195 |
|
| 196 |
|
| 197 |
assert da_feature.id == self.my_tag.features[da_feature.id].id |
| 198 |
assert da_feature.id == self.my_tag.features[data_array.id].id |
| 199 |
assert da_feature.id == self.my_tag.features[data_array.name].id |
| 200 |
assert data_array == self.my_tag.features[data_array.id].data |
| 201 |
assert data_array == self.my_tag.features[data_array.name].data |
| 202 |
|
| 203 |
assert data_array.id in self.my_tag.features |
| 204 |
assert data_array.name in self.my_tag.features |
| 205 |
|
| 206 |
data_frame = self.block.create_data_frame( |
| 207 |
"dataframe feature", "test", |
| 208 |
col_dict=OrderedDict([("number", nix.DataType.Float)]), |
| 209 |
data=[(10.,)] |
| 210 |
) |
| 211 |
df_feature = self.my_tag.create_feature(data_frame, nix.LinkType.Untagged) |
| 212 |
|
| 213 |
assert len(self.my_tag.features) == 2 |
| 214 |
|
| 215 |
assert df_feature in self.my_tag.features |
| 216 |
assert df_feature.id in self.my_tag.features |
| 217 |
|
| 218 |
assert df_feature.id == self.my_tag.features[1].id |
| 219 |
assert df_feature.id == self.my_tag.features[-1].id |
| 220 |
|
| 221 |
|
| 222 |
assert df_feature.id == self.my_tag.features[df_feature.id].id |
| 223 |
assert df_feature.id == self.my_tag.features[data_frame.id].id |
| 224 |
assert df_feature.id == self.my_tag.features[data_frame.name].id |
| 225 |
assert data_frame == self.my_tag.features[data_frame.id].data |
| 226 |
assert data_frame == self.my_tag.features[data_frame.name].data |
| 227 |
|
| 228 |
assert data_frame.id in self.my_tag.features |
| 229 |
assert data_frame.name in self.my_tag.features |
| 230 |
|
| 231 |
assert isinstance(self.my_tag.features[0].data, nix.DataArray) |
| 232 |
assert isinstance(self.my_tag.features[1].data, nix.DataFrame) |
| 233 |
|
| 234 |
del self.my_tag.features[0] |
| 235 |
assert len(self.my_tag.features) == 1 |
| 236 |
del self.my_tag.features[0] |
| 237 |
assert len(self.my_tag.features) == 0 |
| 238 |
|
| 239 |
def test_tag_tagged_data(self): |
| 240 |
sample_iv = 1.0 |
| 241 |
ticks = [1.2, 2.3, 3.4, 4.5, 6.7] |
| 242 |
unit = "ms" |
| 243 |
pos = [0.0, 2.0, 3.4] |
| 244 |
ext = [0.1, 6.0, 2.3] |
| 245 |
units = ["none", "ms", "ms"] |
| 246 |
data = np.random.random_sample((2, 10, 5)) |
| 247 |
da = self.block.create_data_array("dimtest", "test", |
| 248 |
data=data) |
| 249 |
setdim = da.append_set_dimension() |
| 250 |
setdim.labels = ["Label A", "Label B"] |
| 251 |
samdim = da.append_sampled_dimension(sample_iv) |
| 252 |
samdim.unit = unit |
| 253 |
randim = da.append_range_dimension(ticks) |
| 254 |
randim.unit = unit |
| 255 |
|
| 256 |
postag = self.block.create_tag("postag", "event", pos) |
| 257 |
postag.references.append(da) |
| 258 |
postag.units = units |
| 259 |
|
| 260 |
segtag = self.block.create_tag("region", "segment", pos) |
| 261 |
segtag.references.append(da) |
| 262 |
segtag.extent = ext |
| 263 |
segtag.units = units |
| 264 |
|
| 265 |
posdata = postag.tagged_data(0) |
| 266 |
assert len(posdata.shape) == 3 |
| 267 |
assert posdata.shape == (1, 1, 1) |
| 268 |
|
| 269 |
segdata = segtag.tagged_data(0) |
| 270 |
assert len(segdata.shape) == 3 |
| 271 |
assert segdata.shape == (1, 6, 2) |
| 272 |
|
| 273 |
|
| 274 |
posdata = postag.tagged_data(da.name) |
| 275 |
assert len(posdata.shape) == 3 |
| 276 |
assert posdata.shape == (1, 1, 1) |
| 277 |
segdata = segtag.tagged_data(da.name) |
| 278 |
assert len(segdata.shape) == 3 |
| 279 |
assert segdata.shape == (1, 6, 2) |
| 280 |
|
| 281 |
posdata = postag.tagged_data(da.id) |
| 282 |
assert len(posdata.shape) == 3 |
| 283 |
assert posdata.shape == (1, 1, 1) |
| 284 |
segdata = segtag.tagged_data(da.id) |
| 285 |
assert len(segdata.shape) == 3 |
| 286 |
assert segdata.shape == (1, 6, 2) |
| 287 |
|
| 288 |
def test_tag_tagged_data_slice_mode(self): |
| 289 |
data = np.random.random_sample((3, 100, 10)) |
| 290 |
da = self.block.create_data_array("signals", "test.signals", data=data) |
| 291 |
da.unit = "mV" |
| 292 |
da.append_set_dimension(labels=["A", "B", "C"]) |
| 293 |
sample_iv = 0.001 |
| 294 |
timedim = da.append_sampled_dimension(sampling_interval=sample_iv) |
| 295 |
timedim.unit = "s" |
| 296 |
posdim = da.append_range_dimension([1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9]) |
| 297 |
posdim.unit = "mm" |
| 298 |
|
| 299 |
|
| 300 |
exact_tag = self.block.create_tag("tickpoint", "test.tag", position=[0, 0.03, 0.0011]) |
| 301 |
exact_tag.extent = [0.2, 0.02, 0.0005] |
| 302 |
exact_tag.units = ["none", "s", "m"] |
| 303 |
|
| 304 |
exact_tag.references.append(da) |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
slice_default = exact_tag.tagged_data(0) |
| 315 |
assert slice_default.shape == (1, 20, 5) |
| 316 |
np.testing.assert_array_equal(slice_default, da[0:1, 30:50, 1:6]) |
| 317 |
|
| 318 |
slice_inclusive = exact_tag.tagged_data(0, stop_rule=nix.SliceMode.Inclusive) |
| 319 |
assert slice_inclusive.shape == (1, 21, 6) |
| 320 |
np.testing.assert_array_equal(slice_inclusive, da[0:1, 30:51, 1:7]) |
| 321 |
|
| 322 |
slice_exclusive = exact_tag.tagged_data(0, stop_rule=nix.SliceMode.Exclusive) |
| 323 |
assert slice_exclusive.shape == (1, 20, 5) |
| 324 |
np.testing.assert_array_equal(slice_exclusive, da[0:1, 30:50, 1:6]) |
| 325 |
|
| 326 |
|
| 327 |
midpoint_tag = self.block.create_tag("midpoint", "test.tag", position=[0, 0.03, 0.0011]) |
| 328 |
midpoint_tag.extent = [0.1, 0.0301, 0.00051] |
| 329 |
midpoint_tag.units = ["none", "s", "m"] |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
midpoint_tag.references.append(da) |
| 340 |
|
| 341 |
|
| 342 |
slice_default = midpoint_tag.tagged_data(0) |
| 343 |
assert slice_default.shape == (1, 31, 6) |
| 344 |
np.testing.assert_array_equal(slice_default, da[0:1, 30:61, 1:7]) |
| 345 |
|
| 346 |
slice_inclusive = midpoint_tag.tagged_data(0, stop_rule=nix.SliceMode.Inclusive) |
| 347 |
assert slice_inclusive.shape == (1, 31, 6) |
| 348 |
np.testing.assert_array_equal(slice_inclusive, da[0:1, 30:61, 1:7]) |
| 349 |
|
| 350 |
slice_exclusive = midpoint_tag.tagged_data(0, stop_rule=nix.SliceMode.Exclusive) |
| 351 |
assert slice_exclusive.shape == (1, 31, 6) |
| 352 |
np.testing.assert_array_equal(slice_exclusive, da[0:1, 30:61, 1:7]) |
| 353 |
|
| 354 |
def test_tag_feature_data(self): |
| 355 |
number_data = np.random.random(20) |
| 356 |
number_feat = self.block.create_data_array("number feature", "test", |
| 357 |
data=number_data) |
| 358 |
dim = number_feat.append_sampled_dimension(1.0) |
| 359 |
dim.unit = "ms" |
| 360 |
dim.offset = 1.0 |
| 361 |
|
| 362 |
ramp_data = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0] |
| 363 |
ramp_feat = self.block.create_data_array("ramp feature", "test", |
| 364 |
data=np.asarray(ramp_data)) |
| 365 |
ramp_feat.label = "voltage" |
| 366 |
ramp_feat.unit = "mV" |
| 367 |
dim = ramp_feat.append_sampled_dimension(1.0) |
| 368 |
dim.unit = "ms" |
| 369 |
dim.offset = 2.0 |
| 370 |
|
| 371 |
pos_tag = self.block.create_tag("feature test", "test", [5.0]) |
| 372 |
pos_tag.units = ["ms"] |
| 373 |
|
| 374 |
pos_tag.create_feature(number_feat, nix.LinkType.Tagged) |
| 375 |
pos_tag.create_feature(number_feat, nix.LinkType.Untagged) |
| 376 |
pos_tag.create_feature(number_feat, nix.LinkType.Indexed) |
| 377 |
pos_tag.create_feature(ramp_feat, nix.LinkType.Tagged) |
| 378 |
pos_tag.create_feature(ramp_feat, nix.LinkType.Untagged) |
| 379 |
pos_tag.create_feature(ramp_feat, nix.LinkType.Indexed) |
| 380 |
assert len(pos_tag.features) == 6 |
| 381 |
|
| 382 |
data1 = pos_tag.feature_data(0) |
| 383 |
data2 = pos_tag.feature_data(1) |
| 384 |
data3 = pos_tag.feature_data(2) |
| 385 |
data4 = pos_tag.feature_data(3) |
| 386 |
data5 = pos_tag.feature_data(4) |
| 387 |
data6 = pos_tag.feature_data(5) |
| 388 |
|
| 389 |
assert data1.size == 1 |
| 390 |
assert data2.size == len(number_data) |
| 391 |
assert data3.size == len(number_data) |
| 392 |
assert data4.size == 1 |
| 393 |
assert data5.size == len(ramp_data) |
| 394 |
assert data6.size == len(ramp_data) |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
assert np.all(data1[:] == number_data[4:5]) |
| 399 |
assert np.all(data2[:] == number_data[:]) |
| 400 |
assert np.all(data3[:] == number_data[:]) |
| 401 |
assert np.all(data4[:] == ramp_data[3:4]) |
| 402 |
assert np.all(data5[:] == ramp_data[:]) |
| 403 |
assert np.all(data6[:] == ramp_data[:]) |
| 404 |
|
| 405 |
|
| 406 |
pos_tag.extent = [2.0] |
| 407 |
data1 = pos_tag.feature_data(0) |
| 408 |
data2 = pos_tag.feature_data(1) |
| 409 |
data3 = pos_tag.feature_data(2) |
| 410 |
data4 = pos_tag.feature_data(3) |
| 411 |
data5 = pos_tag.feature_data(4) |
| 412 |
data6 = pos_tag.feature_data(5) |
| 413 |
|
| 414 |
assert np.all(data1[:] == number_data[4:6]) |
| 415 |
assert np.all(data2[:] == number_data[:]) |
| 416 |
assert np.all(data3[:] == number_data[:]) |
| 417 |
assert np.all(data4[:] == ramp_data[3:5]) |
| 418 |
assert np.all(data5[:] == ramp_data[:]) |
| 419 |
assert np.all(data6[:] == ramp_data[:]) |
| 420 |
|
| 421 |
def test_tag_feature_dataframe(self): |
| 422 |
numberdata = np.random.random(20) |
| 423 |
number_feat = self.block.create_data_frame( |
| 424 |
"number feature", "test", |
| 425 |
col_dict=OrderedDict([("number", nix.DataType.Float)]), |
| 426 |
data=[(n,) for n in numberdata] |
| 427 |
) |
| 428 |
column_descriptions = OrderedDict([("name", nix.DataType.String), ("duration", nix.DataType.Double)]) |
| 429 |
values = [("One", 0.1), ("Two", 0.2), ("Three", 0.3), ("Four", 0.4), ("Five", 0.5), |
| 430 |
("Six", 0.6), ("Seven", 0.7), ("Eight", 0.8), ("Nine", 0.9), ("Ten", 1.0)] |
| 431 |
ramp_feat = self.block.create_data_frame("ramp feature", "test", col_dict=column_descriptions, data=values) |
| 432 |
ramp_feat.label = "voltage" |
| 433 |
ramp_feat.units = (None, "s") |
| 434 |
|
| 435 |
pos_tag = self.block.create_tag("feature test", "test", [5.0]) |
| 436 |
|
| 437 |
with self.assertRaises(UnsupportedLinkType): |
| 438 |
pos_tag.create_feature(number_feat, nix.LinkType.Tagged) |
| 439 |
pos_tag.create_feature(number_feat, nix.LinkType.Untagged) |
| 440 |
pos_tag.create_feature(number_feat, nix.LinkType.Indexed) |
| 441 |
with self.assertRaises(UnsupportedLinkType): |
| 442 |
pos_tag.create_feature(ramp_feat, nix.LinkType.Tagged) |
| 443 |
pos_tag.create_feature(ramp_feat, nix.LinkType.Untagged) |
| 444 |
pos_tag.create_feature(ramp_feat, nix.LinkType.Indexed) |
| 445 |
assert len(pos_tag.features) == 4 |
| 446 |
|
| 447 |
data1 = pos_tag.feature_data(0) |
| 448 |
data2 = pos_tag.feature_data(1) |
| 449 |
data3 = pos_tag.feature_data(2) |
| 450 |
data4 = pos_tag.feature_data(3) |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
assert np.all(data1[:] == number_feat[:]) |
| 455 |
assert np.all(data2[:] == number_feat[:]) |
| 456 |
assert np.all(data3[:] == ramp_feat[:]) |
| 457 |
assert np.all(data4[:] == ramp_feat[:]) |
| 458 |
|
| 459 |
|
| 460 |
pos_tag.extent = [2.0] |
| 461 |
data1 = pos_tag.feature_data(0) |
| 462 |
data2 = pos_tag.feature_data(1) |
| 463 |
data3 = pos_tag.feature_data(2) |
| 464 |
data4 = pos_tag.feature_data(3) |
| 465 |
|
| 466 |
assert np.all(data1[:] == number_feat[:]) |
| 467 |
assert np.all(data2[:] == number_feat[:]) |
| 468 |
assert np.all(data3[:] == ramp_feat[:]) |
| 469 |
assert np.all(data4[:] == ramp_feat[:]) |
| 470 |
|
| 471 |
def test_timestamp_autoupdate(self): |
| 472 |
tag = self.block.create_tag("tag.time", "test.time", [-1]) |
| 473 |
|
| 474 |
tagtime = tag.updated_at |
| 475 |
time.sleep(1) |
| 476 |
tag.position = [-100] |
| 477 |
self.assertNotEqual(tag.updated_at, tagtime) |
| 478 |
|
| 479 |
tagtime = tag.updated_at |
| 480 |
time.sleep(1) |
| 481 |
tag.extent = [30] |
| 482 |
self.assertNotEqual(tag.updated_at, tagtime) |
| 483 |
|
| 484 |
tagtime = tag.updated_at |
| 485 |
time.sleep(1) |
| 486 |
tag.units = "Mm" |
| 487 |
self.assertNotEqual(tag.updated_at, tagtime) |
| 488 |
|
| 489 |
def test_timestamp_noautoupdate(self): |
| 490 |
self.file.auto_update_timestamps = False |
| 491 |
tag = self.block.create_tag("tag.time", "test.time", [-1]) |
| 492 |
|
| 493 |
tagtime = tag.updated_at |
| 494 |
time.sleep(1) |
| 495 |
tag.position = [-100] |
| 496 |
self.assertEqual(tag.updated_at, tagtime) |
| 497 |
|
| 498 |
tagtime = tag.updated_at |
| 499 |
time.sleep(1) |
| 500 |
tag.extent = [30] |
| 501 |
self.assertEqual(tag.updated_at, tagtime) |
| 502 |
|
| 503 |
tagtime = tag.updated_at |
| 504 |
time.sleep(1) |
| 505 |
tag.units = "Mm" |
| 506 |
self.assertEqual(tag.updated_at, tagtime) |
| 507 |
|
| 508 |
def test_tagged_set_dim(self): |
| 509 |
""" |
| 510 |
Simple test where the slice can be calculated directly from the position and extent and compared to the original |
| 511 |
data. |
| 512 |
Set dimension slicing. |
| 513 |
""" |
| 514 |
nsignals = 10 |
| 515 |
data = np.random.random_sample((nsignals, 100)) |
| 516 |
da = self.block.create_data_array("data", "data", data=data) |
| 517 |
da.append_set_dimension() |
| 518 |
da.append_sampled_dimension(sampling_interval=1).unit = "s" |
| 519 |
|
| 520 |
tag = self.block.create_tag("tag", "simple", position=[]) |
| 521 |
|
| 522 |
tag.references.append(da) |
| 523 |
|
| 524 |
for pos in range(nsignals): |
| 525 |
for ext in range(2, nsignals-pos): |
| 526 |
tag.position = [pos] |
| 527 |
tag.extent = [ext] |
| 528 |
np.testing.assert_array_almost_equal(tag.tagged_data(0), da[pos:pos+ext]) |
| 529 |
np.testing.assert_array_almost_equal(tag.tagged_data(0, nix.SliceMode.Exclusive), da[pos:pos+ext]) |
| 530 |
np.testing.assert_array_almost_equal(tag.tagged_data(0, nix.SliceMode.Inclusive), da[pos:pos+ext+1]) |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
tag.position = [pos+0.1] |
| 536 |
tag.extent = [ext+0.1] |
| 537 |
start = pos+1 |
| 538 |
stop = pos+ext+1 |
| 539 |
np.testing.assert_array_almost_equal(tag.tagged_data(0), da[start:stop]) |
| 540 |
np.testing.assert_array_almost_equal(tag.tagged_data(0, nix.SliceMode.Exclusive), da[start:stop]) |
| 541 |
np.testing.assert_array_almost_equal(tag.tagged_data(0, nix.SliceMode.Inclusive), da[start:stop]) |
| 542 |
|
| 543 |
if pos+ext+2 < len(da): |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
tag.position = [pos+0.9] |
| 548 |
tag.extent = [ext+0.9] |
| 549 |
start = pos+1 |
| 550 |
stop = pos+ext+2 |
| 551 |
np.testing.assert_array_almost_equal(tag.tagged_data(0), da[start:stop]) |
| 552 |
np.testing.assert_array_almost_equal(tag.tagged_data(0, nix.SliceMode.Exclusive), da[start:stop]) |
| 553 |
np.testing.assert_array_almost_equal(tag.tagged_data(0, nix.SliceMode.Inclusive), da[start:stop]) |
| 554 |
|
| 555 |
def test_tagged_range_dim(self): |
| 556 |
""" |
| 557 |
Simple test where the slice can be calculated directly from the position and extent and compared to the original |
| 558 |
data. |
| 559 |
Range dimension slicing. |
| 560 |
""" |
| 561 |
nticks = 10 |
| 562 |
data = np.random.random_sample((nticks, 100)) |
| 563 |
da = self.block.create_data_array("data", "data", data=data) |
| 564 |
da.append_range_dimension(ticks=range(nticks)) |
| 565 |
da.append_sampled_dimension(sampling_interval=1).unit = "s" |
| 566 |
|
| 567 |
tag = self.block.create_tag("tag", "simple", position=[]) |
| 568 |
|
| 569 |
tag.references.append(da) |
| 570 |
|
| 571 |
for pos in range(nticks): |
| 572 |
for ext in range(2, nticks-pos): |
| 573 |
tag.position = [pos] |
| 574 |
tag.extent = [ext] |
| 575 |
np.testing.assert_array_almost_equal(tag.tagged_data(0), da[pos:pos+ext]) |
| 576 |
np.testing.assert_array_almost_equal(tag.tagged_data(0, nix.SliceMode.Exclusive), da[pos:pos+ext]) |
| 577 |
np.testing.assert_array_almost_equal(tag.tagged_data(0, nix.SliceMode.Inclusive), da[pos:pos+ext+1]) |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
tag.position = [pos + 0.1] |
| 583 |
tag.extent = [ext + 0.1] |
| 584 |
start = pos + 1 |
| 585 |
stop = pos + ext + 1 |
| 586 |
np.testing.assert_array_almost_equal(tag.tagged_data(0), da[start:stop]) |
| 587 |
np.testing.assert_array_almost_equal(tag.tagged_data(0, nix.SliceMode.Exclusive), da[start:stop]) |
| 588 |
np.testing.assert_array_almost_equal(tag.tagged_data(0, nix.SliceMode.Inclusive), da[start:stop]) |
| 589 |
|
| 590 |
if pos + ext + 2 < len(da): |
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
tag.position = [pos + 0.9] |
| 595 |
tag.extent = [ext + 0.9] |
| 596 |
start = pos + 1 |
| 597 |
stop = pos + ext + 2 |
| 598 |
np.testing.assert_array_almost_equal(tag.tagged_data(0), da[start:stop]) |
| 599 |
np.testing.assert_array_almost_equal(tag.tagged_data(0, nix.SliceMode.Exclusive), da[start:stop]) |
| 600 |
np.testing.assert_array_almost_equal(tag.tagged_data(0, nix.SliceMode.Inclusive), da[start:stop]) |
| 601 |
|
| 602 |
time_vector = np.arange(0.0, 10., 0.001) |
| 603 |
indices = np.random.rand(len(time_vector)) |
| 604 |
event_data = time_vector[(indices < 0.1)] |
| 605 |
event_data = event_data[(event_data < 4) | (event_data > 7)] |
| 606 |
|
| 607 |
event_da = self.block.create_data_array("event_data", "nix.events", data=event_data, unit="s") |
| 608 |
event_da.append_range_dimension_using_self() |
| 609 |
|
| 610 |
tt = self.block.create_tag("no_event_segment", "nix.segment", 4.5) |
| 611 |
tt.extent = 1.0 |
| 612 |
tt.references.append(event_da) |
| 613 |
sl = tt.tagged_data(0) |
| 614 |
self.assertFalse(sl.valid) |
| 615 |
|
| 616 |
tt2 = self.block.create_tag("beyond data", "nix.segment", 12.0) |
| 617 |
tt2.extent = 3.0 |
| 618 |
tt2.references.append(event_da) |
| 619 |
sl = tt2.tagged_data(0) |
| 620 |
self.assertFalse(sl.valid) |
| 621 |
|
| 622 |
tt3 = self.block.create_tag("reachingbeyonddata", "nix.segment", 8.5) |
| 623 |
tt3.extent = [3.0] |
| 624 |
tt3.references.append(event_da) |
| 625 |
sl = tt3.tagged_data(0) |
| 626 |
self.assertTrue(sl.valid) |
| 627 |
|
| 628 |
def test_tagged_sampled_dim(self): |
| 629 |
""" |
| 630 |
Simple test where the slice can be calculated directly from the position and extent and compared to the original |
| 631 |
data. |
| 632 |
Sampled dimension slicing. |
| 633 |
""" |
| 634 |
nticks = 10 |
| 635 |
data = np.random.random_sample((nticks, 100)) |
| 636 |
da = self.block.create_data_array("data", "data", data=data) |
| 637 |
da.append_sampled_dimension(sampling_interval=1).unit = "V" |
| 638 |
da.append_sampled_dimension(sampling_interval=1).unit = "s" |
| 639 |
|
| 640 |
tag = self.block.create_tag("tag", "simple", position=[]) |
| 641 |
tag.units = ["V", "s"] |
| 642 |
|
| 643 |
tag.references.append(da) |
| 644 |
|
| 645 |
for pos in range(nticks): |
| 646 |
for ext in range(2, nticks-pos): |
| 647 |
tag.position = [pos] |
| 648 |
tag.extent = [ext] |
| 649 |
np.testing.assert_array_almost_equal(tag.tagged_data(0), da[pos:pos+ext]) |
| 650 |
np.testing.assert_array_almost_equal(tag.tagged_data(0, nix.SliceMode.Exclusive), da[pos:pos+ext]) |
| 651 |
np.testing.assert_array_almost_equal(tag.tagged_data(0, nix.SliceMode.Inclusive), da[pos:pos+ext+1]) |
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
tag.position = [pos+0.1] |
| 657 |
tag.extent = [ext+0.1] |
| 658 |
start = pos+1 |
| 659 |
stop = pos+ext+1 |
| 660 |
np.testing.assert_array_almost_equal(tag.tagged_data(0), da[start:stop]) |
| 661 |
np.testing.assert_array_almost_equal(tag.tagged_data(0, nix.SliceMode.Exclusive), da[start:stop]) |
| 662 |
np.testing.assert_array_almost_equal(tag.tagged_data(0, nix.SliceMode.Inclusive), da[start:stop]) |
| 663 |
|
| 664 |
if pos+ext+2 < len(da): |
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
tag.position = [pos+0.9] |
| 669 |
tag.extent = [ext+0.9] |
| 670 |
start = pos+1 |
| 671 |
stop = pos+ext+2 |
| 672 |
np.testing.assert_array_almost_equal(tag.tagged_data(0), da[start:stop]) |
| 673 |
np.testing.assert_array_almost_equal(tag.tagged_data(0, nix.SliceMode.Exclusive), da[start:stop]) |
| 674 |
np.testing.assert_array_almost_equal(tag.tagged_data(0, nix.SliceMode.Inclusive), da[start:stop]) |