| 1 |
|
| 2 |
) |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
from nixio.exceptions.exceptions import InvalidSlice |
| 10 |
import os |
| 11 |
import time |
| 12 |
import unittest |
| 13 |
from collections import OrderedDict |
| 14 |
import numpy as np |
| 15 |
import nixio as nix |
| 16 |
from nixio.exceptions import DuplicateName, UnsupportedLinkType |
| 17 |
from .tmp import TempDir |
| 18 |
|
| 19 |
|
| 20 |
class TestMultiTags(unittest.TestCase): |
| 21 |
|
| 22 |
def setUp(self): |
| 23 |
interval = 1.0 |
| 24 |
ticks = [1.2, 2.3, 3.4, 4.5, 6.7] |
| 25 |
unit = "ms" |
| 26 |
|
| 27 |
self.tmpdir = TempDir("mtagtest") |
| 28 |
self.testfilename = os.path.join(self.tmpdir.path, "mtagtest.nix") |
| 29 |
self.file = nix.File.open(self.testfilename, nix.FileMode.Overwrite) |
| 30 |
self.block = self.file.create_block("test block", "recordingsession") |
| 31 |
|
| 32 |
self.my_array = self.block.create_data_array("my array", "test", nix.DataType.Int16, (0, 0)) |
| 33 |
self.my_tag = self.block.create_multi_tag("my tag", "tag", self.my_array) |
| 34 |
|
| 35 |
self.your_array = self.block.create_data_array("your array", "test", nix.DataType.Int16, (0, 0)) |
| 36 |
self.your_tag = self.block.create_multi_tag("your tag", "tag", self.your_array) |
| 37 |
|
| 38 |
self.data_array = self.block.create_data_array("featureTest", "test", nix.DataType.Double, (2, 10, 5)) |
| 39 |
|
| 40 |
data = np.zeros((2, 10, 5)) |
| 41 |
value = 0. |
| 42 |
for i in range(2): |
| 43 |
value = 0 |
| 44 |
for j in range(10): |
| 45 |
for k in range(5): |
| 46 |
value += 1 |
| 47 |
data[i, j, k] = value |
| 48 |
|
| 49 |
self.data_array[:, :, :] = data |
| 50 |
|
| 51 |
set_dim = self.data_array.append_set_dimension() |
| 52 |
set_dim.labels = ["label_a", "label_b"] |
| 53 |
sampled_dim = self.data_array.append_sampled_dimension(interval) |
| 54 |
sampled_dim.unit = unit |
| 55 |
range_dim = self.data_array.append_range_dimension(ticks) |
| 56 |
range_dim.unit = unit |
| 57 |
|
| 58 |
event_positions = np.zeros((2, 3)) |
| 59 |
event_positions[0, 0] = 0.0 |
| 60 |
event_positions[0, 1] = 3.0 |
| 61 |
event_positions[0, 2] = 3.4 |
| 62 |
|
| 63 |
event_positions[1, 0] = 0.0 |
| 64 |
event_positions[1, 1] = 8.0 |
| 65 |
event_positions[1, 2] = 2.3 |
| 66 |
|
| 67 |
event_extents = np.zeros((2, 3)) |
| 68 |
event_extents[0, 0] = 1.0 |
| 69 |
event_extents[0, 1] = 6.0 |
| 70 |
event_extents[0, 2] = 2.3 |
| 71 |
|
| 72 |
event_extents[1, 0] = 1.0 |
| 73 |
event_extents[1, 1] = 3.0 |
| 74 |
event_extents[1, 2] = 2.0 |
| 75 |
|
| 76 |
event_labels = ["event 1", "event 2"] |
| 77 |
dim_labels = ["dim 0", "dim 1", "dim 2"] |
| 78 |
|
| 79 |
self.event_array = self.block.create_data_array("positions", "test", |
| 80 |
data=event_positions) |
| 81 |
|
| 82 |
self.extent_array = self.block.create_data_array("extents", "test", |
| 83 |
data=event_extents) |
| 84 |
extent_set_dim = self.extent_array.append_set_dimension() |
| 85 |
extent_set_dim.labels = event_labels |
| 86 |
extent_set_dim = self.extent_array.append_set_dimension() |
| 87 |
extent_set_dim.labels = dim_labels |
| 88 |
|
| 89 |
self.feature_tag = self.block.create_multi_tag("feature_tag", "events", |
| 90 |
self.event_array) |
| 91 |
self.feature_tag.extents = self.extent_array |
| 92 |
self.feature_tag.references.append(self.data_array) |
| 93 |
|
| 94 |
def tearDown(self): |
| 95 |
del self.file.blocks[self.block.id] |
| 96 |
self.file.close() |
| 97 |
self.tmpdir.cleanup() |
| 98 |
|
| 99 |
def test_multi_tag_new_constructor(self): |
| 100 |
pos = np.random.random_sample((2, 3)) |
| 101 |
ext = np.random.random_sample((2, 3)) |
| 102 |
mt = self.block.create_multi_tag("conv_test", "test", pos, ext) |
| 103 |
np.testing.assert_almost_equal(pos, mt.positions[:]) |
| 104 |
np.testing.assert_almost_equal(ext, mt.extents[:]) |
| 105 |
|
| 106 |
assert mt.positions.name == "conv_test-positions" |
| 107 |
assert mt.positions.type == "test-positions" |
| 108 |
assert mt.extents.name == "conv_test-extents" |
| 109 |
assert mt.extents.type == "test-extents" |
| 110 |
|
| 111 |
pos = None |
| 112 |
ext = np.random.random_sample((2, 3)) |
| 113 |
self.assertRaises(ValueError, self.block.create_multi_tag, |
| 114 |
"err_test", "test", pos, ext) |
| 115 |
self.block.create_data_array("dup_test-" |
| 116 |
"positions", "test", data=[0]) |
| 117 |
pos = np.random.random_sample((2, 3)) |
| 118 |
ext = np.random.random_sample((2, 3)) |
| 119 |
self.assertRaises(DuplicateName, self.block.create_multi_tag, |
| 120 |
"dup_test", "test", pos, ext) |
| 121 |
del self.block.data_arrays["dup_test-positions"] |
| 122 |
self.block.create_data_array("dup_test2-" |
| 123 |
"extents", "test", data=[0]) |
| 124 |
pos = np.random.random_sample((2, 3)) |
| 125 |
ext = np.random.random_sample((2, 3)) |
| 126 |
self.assertRaises(DuplicateName, self.block.create_multi_tag, |
| 127 |
"dup_test2", "test", pos, ext) |
| 128 |
pos = np.random.random_sample((2, 3)) |
| 129 |
ext = [None, None] |
| 130 |
self.assertRaises(TypeError, self.block.create_multi_tag, |
| 131 |
"dup_test3", "test", pos, ext) |
| 132 |
|
| 133 |
def test_multi_tag_flex(self): |
| 134 |
pos1d = self.block.create_data_array("pos1", "pos", data=[[0], [1]]) |
| 135 |
pos1d1d = self.block.create_data_array("pos1d1d", "pos", data=[0, 1]) |
| 136 |
pos2d = self.block.create_data_array("pos2", "pos", data=[[0, 0], [1, 1]]) |
| 137 |
pos3d = self.block.create_data_array("pos3", "pos", data=[[0, 1, 2], [1, 2, 3]]) |
| 138 |
ext1d = self.block.create_data_array('ext1', 'ext', data=[[1], [1]]) |
| 139 |
ext1d1d = self.block.create_data_array('ext1d1d', 'ext', data=[1, 1]) |
| 140 |
ext2d = self.block.create_data_array('ext2', 'ext', data=[[1, 2], [0, 2]]) |
| 141 |
ext3d = self.block.create_data_array('ext3', 'ext', data=[[1, 1, 1], [1, 1, 1]]) |
| 142 |
mt1d = self.block.create_multi_tag("mt1d", "mt", pos1d) |
| 143 |
mt1d.extents = ext1d |
| 144 |
mt1d1d = self.block.create_multi_tag("mt1d1d", "mt", pos1d1d) |
| 145 |
mt1d1d.extents = ext1d1d |
| 146 |
mt2d = self.block.create_multi_tag("mt2d", "mt", pos2d) |
| 147 |
mt2d.extents = ext2d |
| 148 |
mt3d = self.block.create_multi_tag("mt3d", "mt", pos3d) |
| 149 |
mt3d.extents = ext3d |
| 150 |
|
| 151 |
da1d = self.block.create_data_array('ref1d', 'ref', data=np.arange(10)) |
| 152 |
da1d.append_sampled_dimension(1., label="time", unit="s") |
| 153 |
da2d = self.block.create_data_array('ref2d', 'ref', data=np.arange(100).reshape((10, 10))) |
| 154 |
da2d.append_sampled_dimension(1., label="time", unit="s") |
| 155 |
da2d.append_set_dimension() |
| 156 |
da3d = self.block.create_data_array('ref3d', 'ref', data=np.arange(1000).reshape((10, 10, 10))) |
| 157 |
da3d.append_sampled_dimension(1., label="time", unit="s") |
| 158 |
da3d.append_set_dimension() |
| 159 |
da3d.append_set_dimension() |
| 160 |
mt1d.references.extend([da1d, da2d, da3d]) |
| 161 |
mt1d1d.references.extend([da1d, da2d, da3d]) |
| 162 |
mt2d.references.extend([da1d, da2d, da3d]) |
| 163 |
mt3d.references.extend([da1d, da2d, da3d]) |
| 164 |
np.testing.assert_almost_equal(mt1d.tagged_data(0, 0)[:], da1d[0:1]) |
| 165 |
np.testing.assert_almost_equal(mt1d.tagged_data(0, 1)[:], da2d[0:1, :]) |
| 166 |
np.testing.assert_almost_equal(mt1d.tagged_data(0, 2)[:], da3d[0:1, :, :]) |
| 167 |
np.testing.assert_almost_equal(mt1d1d.tagged_data(0, 0)[:], da1d[0:1]) |
| 168 |
np.testing.assert_almost_equal(mt1d1d.tagged_data(0, 1)[:], da2d[0:1, :]) |
| 169 |
np.testing.assert_almost_equal(mt1d1d.tagged_data(0, 2)[:], da3d[0:1, :, :]) |
| 170 |
np.testing.assert_almost_equal(mt2d.tagged_data(0, 0)[:], da1d[0:1]) |
| 171 |
np.testing.assert_almost_equal(mt2d.tagged_data(0, 1)[:], da2d[0:1, 0:2]) |
| 172 |
np.testing.assert_almost_equal(mt2d.tagged_data(0, 2)[:], da3d[0:1, 0:2, :]) |
| 173 |
np.testing.assert_almost_equal(mt3d.tagged_data(1, 0)[:], da1d[1:2]) |
| 174 |
np.testing.assert_almost_equal(mt3d.tagged_data(1, 1)[:], da2d[1:2, 2:3]) |
| 175 |
np.testing.assert_almost_equal(mt3d.tagged_data(1, 2)[:], da3d[1:2, 2:3, 3:4]) |
| 176 |
|
| 177 |
def test_multi_tag_eq(self): |
| 178 |
assert self.my_tag == self.my_tag |
| 179 |
assert not self.my_tag == self.your_tag |
| 180 |
assert self.my_tag is not None |
| 181 |
|
| 182 |
def test_multi_tag_id(self): |
| 183 |
assert self.my_tag.id is not None |
| 184 |
|
| 185 |
def test_multi_tag_name(self): |
| 186 |
assert self.my_tag.name is not None |
| 187 |
|
| 188 |
def test_multi_tag_type(self): |
| 189 |
def set_none(): |
| 190 |
self.my_tag.type = None |
| 191 |
|
| 192 |
assert self.my_tag.type is not None |
| 193 |
self.assertRaises(Exception, set_none) |
| 194 |
|
| 195 |
self.my_tag.type = "foo type" |
| 196 |
assert self.my_tag.type == "foo type" |
| 197 |
|
| 198 |
def test_multi_tag_definition(self): |
| 199 |
assert self.my_tag.definition is None |
| 200 |
|
| 201 |
self.my_tag.definition = "definition" |
| 202 |
assert self.my_tag.definition == "definition" |
| 203 |
|
| 204 |
self.my_tag.definition = None |
| 205 |
assert self.my_tag.definition is None |
| 206 |
|
| 207 |
def test_multi_tag_timestamps(self): |
| 208 |
created_at = self.my_tag.created_at |
| 209 |
assert created_at > 0 |
| 210 |
|
| 211 |
updated_at = self.my_tag.updated_at |
| 212 |
assert updated_at > 0 |
| 213 |
|
| 214 |
self.my_tag.force_created_at(1403530068) |
| 215 |
assert self.my_tag.created_at == 1403530068 |
| 216 |
|
| 217 |
def test_multi_tag_units(self): |
| 218 |
assert self.my_tag.units == () |
| 219 |
|
| 220 |
self.my_tag.units = ["mV", "ms"] |
| 221 |
assert self.my_tag.units == ("mV", "ms") |
| 222 |
|
| 223 |
self.my_tag.units = [] |
| 224 |
assert self.my_tag.units == () |
| 225 |
|
| 226 |
def test_multi_tag_positions(self): |
| 227 |
def set_none(): |
| 228 |
self.my_tag.positions = None |
| 229 |
|
| 230 |
assert self.my_tag.positions is not None |
| 231 |
old_positions = self.my_tag.positions |
| 232 |
|
| 233 |
new_positions = self.block.create_data_array("pos", "position", |
| 234 |
nix.DataType.Int16, |
| 235 |
(0, 0)) |
| 236 |
self.my_tag.positions = new_positions |
| 237 |
assert self.my_tag.positions == new_positions |
| 238 |
|
| 239 |
self.assertRaises(TypeError, set_none) |
| 240 |
|
| 241 |
self.my_tag.positions = old_positions |
| 242 |
assert self.my_tag.positions == old_positions |
| 243 |
|
| 244 |
def test_multi_tag_extents(self): |
| 245 |
assert self.my_tag.extents is None |
| 246 |
|
| 247 |
new_extents = self.block.create_data_array("ext", "extent", |
| 248 |
nix.DataType.Int16, (0, 0)) |
| 249 |
self.my_tag.extents = new_extents |
| 250 |
assert self.my_tag.extents == new_extents |
| 251 |
|
| 252 |
self.my_tag.extents = None |
| 253 |
assert self.my_tag.extents is None |
| 254 |
|
| 255 |
def test_multi_tag_references(self): |
| 256 |
assert len(self.my_tag.references) == 0 |
| 257 |
|
| 258 |
self.assertRaises(TypeError, self.my_tag.references.append, 100) |
| 259 |
|
| 260 |
reference1 = self.block.create_data_array("reference1", "stimuli", |
| 261 |
nix.DataType.Int16, (0,)) |
| 262 |
reference2 = self.block.create_data_array("reference2", "stimuli", |
| 263 |
nix.DataType.Int16, (0,)) |
| 264 |
|
| 265 |
self.my_tag.references.append(reference1) |
| 266 |
self.my_tag.references.append(reference2) |
| 267 |
|
| 268 |
assert len(self.my_tag.references) == 2 |
| 269 |
assert reference1 in self.my_tag.references |
| 270 |
assert reference2 in self.my_tag.references |
| 271 |
|
| 272 |
|
| 273 |
assert reference1 == self.my_tag.references[reference1.name] |
| 274 |
assert reference1 == self.my_tag.references[reference1.id] |
| 275 |
assert reference2 == self.my_tag.references[reference2.name] |
| 276 |
assert reference2 == self.my_tag.references[reference2.id] |
| 277 |
|
| 278 |
assert reference1.name in self.my_tag.references |
| 279 |
assert reference2.name in self.my_tag.references |
| 280 |
assert reference1.id in self.my_tag.references |
| 281 |
assert reference2.id in self.my_tag.references |
| 282 |
|
| 283 |
del self.my_tag.references[reference2] |
| 284 |
assert self.my_tag.references[0] == reference1 |
| 285 |
|
| 286 |
del self.my_tag.references[reference1] |
| 287 |
assert len(self.my_tag.references) == 0 |
| 288 |
|
| 289 |
def test_multi_tag_features(self): |
| 290 |
assert len(self.my_tag.features) == 0 |
| 291 |
|
| 292 |
data_array = self.block.create_data_array("feature", "stimuli", |
| 293 |
nix.DataType.Int16, (0,)) |
| 294 |
feature = self.my_tag.create_feature(data_array, |
| 295 |
nix.LinkType.Untagged) |
| 296 |
assert len(self.my_tag.features) == 1 |
| 297 |
|
| 298 |
assert feature in self.my_tag.features |
| 299 |
assert feature.id in self.my_tag.features |
| 300 |
assert "notexist" not in self.my_tag.features |
| 301 |
|
| 302 |
assert feature.id == self.my_tag.features[0].id |
| 303 |
assert feature.id == self.my_tag.features[-1].id |
| 304 |
|
| 305 |
|
| 306 |
assert feature.id == self.my_tag.features[feature.id].id |
| 307 |
assert feature.id == self.my_tag.features[data_array.id].id |
| 308 |
assert feature.id == self.my_tag.features[data_array.name].id |
| 309 |
assert data_array == self.my_tag.features[data_array.id].data |
| 310 |
assert data_array == self.my_tag.features[data_array.name].data |
| 311 |
|
| 312 |
assert data_array.id in self.my_tag.features |
| 313 |
assert data_array.name in self.my_tag.features |
| 314 |
|
| 315 |
data_frame = self.block.create_data_frame( |
| 316 |
"dataframe feature", "test", |
| 317 |
col_dict=OrderedDict([("number", nix.DataType.Float)]), |
| 318 |
data=[(10.,)] |
| 319 |
) |
| 320 |
df_feature = self.my_tag.create_feature(data_frame, nix.LinkType.Untagged) |
| 321 |
|
| 322 |
assert len(self.my_tag.features) == 2 |
| 323 |
|
| 324 |
assert df_feature in self.my_tag.features |
| 325 |
assert df_feature.id in self.my_tag.features |
| 326 |
|
| 327 |
assert df_feature.id == self.my_tag.features[1].id |
| 328 |
assert df_feature.id == self.my_tag.features[-1].id |
| 329 |
|
| 330 |
|
| 331 |
assert df_feature.id == self.my_tag.features[df_feature.id].id |
| 332 |
assert df_feature.id == self.my_tag.features[data_frame.id].id |
| 333 |
assert df_feature.id == self.my_tag.features[data_frame.name].id |
| 334 |
assert data_frame == self.my_tag.features[data_frame.id].data |
| 335 |
assert data_frame == self.my_tag.features[data_frame.name].data |
| 336 |
|
| 337 |
assert data_frame.id in self.my_tag.features |
| 338 |
assert data_frame.name in self.my_tag.features |
| 339 |
|
| 340 |
assert isinstance(self.my_tag.features[0].data, nix.DataArray) |
| 341 |
assert isinstance(self.my_tag.features[1].data, nix.DataFrame) |
| 342 |
|
| 343 |
del self.my_tag.features[0] |
| 344 |
assert len(self.my_tag.features) == 1 |
| 345 |
del self.my_tag.features[0] |
| 346 |
assert len(self.my_tag.features) == 0 |
| 347 |
|
| 348 |
def test_multi_tag_tagged_data(self): |
| 349 |
sample_iv = 0.001 |
| 350 |
x_data = np.arange(0, 10, sample_iv) |
| 351 |
y_data = np.sin(2 * np.pi * x_data) |
| 352 |
|
| 353 |
block = self.block |
| 354 |
da = block.create_data_array("sin", "data", data=y_data) |
| 355 |
da.unit = 'dB' |
| 356 |
dim = da.append_sampled_dimension(sample_iv) |
| 357 |
dim.unit = 's' |
| 358 |
|
| 359 |
pos = block.create_data_array('pos1', 'positions', data=np.array([0.]).reshape(1, 1)) |
| 360 |
pos.append_set_dimension() |
| 361 |
pos.append_set_dimension() |
| 362 |
pos.unit = 'ms' |
| 363 |
ext = block.create_data_array('ext1', 'extents', data=np.array([2000.]).reshape(1, 1)) |
| 364 |
ext.append_set_dimension() |
| 365 |
ext.append_set_dimension() |
| 366 |
ext.unit = 'ms' |
| 367 |
|
| 368 |
mtag = block.create_multi_tag("sin1", "tag", pos) |
| 369 |
mtag.extents = ext |
| 370 |
mtag.units = ['ms'] |
| 371 |
mtag.references.append(da) |
| 372 |
|
| 373 |
assert mtag.tagged_data(0, 0).shape == (2000,) |
| 374 |
assert np.array_equal(y_data[:2000], mtag.tagged_data(0, 0)[:]) |
| 375 |
assert mtag.tagged_data(0, 0, stop_rule=nix.SliceMode.Inclusive).shape == (2001,) |
| 376 |
assert np.array_equal(y_data[:2001], mtag.tagged_data(0, 0, stop_rule=nix.SliceMode.Inclusive)[:]) |
| 377 |
|
| 378 |
|
| 379 |
data = mtag.tagged_data(0, da.name) |
| 380 |
assert data.shape == (2000,) |
| 381 |
assert np.array_equal(y_data[:2000], data[:]) |
| 382 |
|
| 383 |
|
| 384 |
data = mtag.tagged_data(0, da.id) |
| 385 |
assert data.shape == (2000,) |
| 386 |
assert np.array_equal(y_data[:2000], data[:]) |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
sample_iv = 1.0 |
| 393 |
ticks = [1.2, 2.3, 3.4, 4.5, 6.7] |
| 394 |
unit = "ms" |
| 395 |
pos = self.block.create_data_array("pos", "test", data=[[1, 1, 1], [1, 1, 1.2], [1, 1, 1.2]]) |
| 396 |
pos.append_set_dimension() |
| 397 |
pos.append_set_dimension() |
| 398 |
ext = self.block.create_data_array("ext", "test", data=[[1, 5, 2], [1, 5, 2], [0, 4, 1]]) |
| 399 |
ext.append_set_dimension() |
| 400 |
ext.append_set_dimension() |
| 401 |
units = ["none", "ms", "ms"] |
| 402 |
data = np.random.random_sample((3, 10, 5)) |
| 403 |
da = self.block.create_data_array("dimtest", "test", data=data) |
| 404 |
setdim = da.append_set_dimension() |
| 405 |
setdim.labels = ["Label A", "Label B", "Label D"] |
| 406 |
samdim = da.append_sampled_dimension(sample_iv) |
| 407 |
samdim.unit = unit |
| 408 |
randim = da.append_range_dimension(ticks) |
| 409 |
randim.unit = unit |
| 410 |
|
| 411 |
postag = self.block.create_multi_tag("postag", "event", pos) |
| 412 |
postag.references.append(da) |
| 413 |
postag.units = units |
| 414 |
|
| 415 |
segtag = self.block.create_multi_tag("region", "segment", pos) |
| 416 |
segtag.references.append(da) |
| 417 |
segtag.extents = ext |
| 418 |
segtag.units = units |
| 419 |
|
| 420 |
posdata = postag.tagged_data(0, 0) |
| 421 |
assert not posdata.valid |
| 422 |
assert "InvalidSlice error" in posdata.debug_message |
| 423 |
assert posdata.data_extent is None |
| 424 |
assert posdata.shape is None |
| 425 |
with self.assertRaises(InvalidSlice): |
| 426 |
posdata._write_data(np.random.randn(1)) |
| 427 |
assert sum(posdata[:].shape) == 0 |
| 428 |
|
| 429 |
posdata = postag.tagged_data(1, 0) |
| 430 |
assert posdata.valid |
| 431 |
assert posdata.debug_message == "" |
| 432 |
assert len(posdata.shape) == 3 |
| 433 |
assert posdata.shape == (1, 1, 1) |
| 434 |
assert np.isclose(posdata[0, 0, 0], data[1, 1, 0]) |
| 435 |
|
| 436 |
posdata = postag.tagged_data(2, 0) |
| 437 |
assert len(posdata.shape) == 3 |
| 438 |
assert posdata.shape == (1, 1, 1) |
| 439 |
assert np.isclose(posdata[0, 0, 0], data[1, 1, 0]) |
| 440 |
|
| 441 |
segdata = segtag.tagged_data(1, 0) |
| 442 |
assert len(segdata.shape) == 3 |
| 443 |
assert segdata.shape == (1, 5, 2) |
| 444 |
|
| 445 |
segdata = segtag.tagged_data(2, 0) |
| 446 |
assert len(segdata.shape) == 3 |
| 447 |
assert segdata.shape == (1, 4, 1) |
| 448 |
|
| 449 |
|
| 450 |
for ridx, _ in enumerate(mtag.references): |
| 451 |
for pidx, _ in enumerate(mtag.positions): |
| 452 |
mtag.tagged_data(pidx, ridx) |
| 453 |
|
| 454 |
wrong_pos = self.block.create_data_array("incorpos", "test", data=[[1, 1, 1], [100, 1, 1]]) |
| 455 |
wrong_pos.append_set_dimension() |
| 456 |
wrong_pos.append_set_dimension() |
| 457 |
postag.positions = wrong_pos |
| 458 |
self.assertRaises(IndexError, postag.tagged_data, 1, 1) |
| 459 |
wrong_ext = self.block.create_data_array("incorext", "test", data=[[1, 500, 2], [0, 4, 1]]) |
| 460 |
wrong_ext.append_set_dimension() |
| 461 |
wrong_ext.append_set_dimension() |
| 462 |
segtag.extents = wrong_ext |
| 463 |
self.assertRaises(IndexError, segtag.tagged_data, 0, 1) |
| 464 |
|
| 465 |
def test_multi_tag_data_coefficients(self): |
| 466 |
sample_iv = 0.001 |
| 467 |
x_data = np.arange(0, 10, sample_iv) |
| 468 |
y_data = np.sin(2 * np.pi * x_data) |
| 469 |
|
| 470 |
block = self.block |
| 471 |
da = block.create_data_array("sin", "data", data=y_data) |
| 472 |
da.unit = 'V' |
| 473 |
da.polynom_coefficients = (10, 0.3) |
| 474 |
dim = da.append_sampled_dimension(sample_iv) |
| 475 |
dim.unit = 's' |
| 476 |
|
| 477 |
pos = block.create_data_array('pos1', 'positions', data=np.array([0.]).reshape(1, 1)) |
| 478 |
pos.append_set_dimension() |
| 479 |
pos.append_set_dimension() |
| 480 |
pos.unit = 'ms' |
| 481 |
ext = block.create_data_array('ext1', 'extents', data=np.array([2000.]).reshape(1, 1)) |
| 482 |
ext.append_set_dimension() |
| 483 |
ext.append_set_dimension() |
| 484 |
ext.unit = 'ms' |
| 485 |
|
| 486 |
mtag = block.create_multi_tag("sin1", "tag", pos) |
| 487 |
mtag.extents = ext |
| 488 |
mtag.units = ['ms'] |
| 489 |
mtag.references.append(da) |
| 490 |
|
| 491 |
assert np.array_equal(da[:2000], mtag.tagged_data(0, 0)[:]) |
| 492 |
|
| 493 |
da.expansion_origin = 0.89 |
| 494 |
assert np.array_equal(da[:2000], mtag.tagged_data(0, 0)[:]) |
| 495 |
|
| 496 |
def test_multi_tag_tagged_data_1d(self): |
| 497 |
|
| 498 |
|
| 499 |
oneddata = self.block.create_data_array("1dda", "data", |
| 500 |
data=list(range(100))) |
| 501 |
oneddata.append_sampled_dimension(0.1) |
| 502 |
onedpos = self.block.create_data_array("1dpos", "positions", |
| 503 |
data=[1, 9, 9.5]) |
| 504 |
onedmtag = self.block.create_multi_tag("2dmt", "mtag", |
| 505 |
positions=onedpos) |
| 506 |
onedmtag.references.append(oneddata) |
| 507 |
for pidx, _ in enumerate(onedmtag.positions): |
| 508 |
onedmtag.tagged_data(pidx, 0) |
| 509 |
|
| 510 |
def test_multi_tag_feature_data(self): |
| 511 |
index_data = self.block.create_data_array("indexed feature data", "test", |
| 512 |
dtype=nix.DataType.Double, shape=(10, 10)) |
| 513 |
dim1 = index_data.append_sampled_dimension(1.0) |
| 514 |
dim1.unit = "ms" |
| 515 |
dim2 = index_data.append_sampled_dimension(1.0) |
| 516 |
dim2.unit = "ms" |
| 517 |
|
| 518 |
data1 = np.zeros((10, 10)) |
| 519 |
value = 0.0 |
| 520 |
total = 0.0 |
| 521 |
for i in range(10): |
| 522 |
value = 100 * i |
| 523 |
for j in range(10): |
| 524 |
value += 1 |
| 525 |
data1[i, j] = value |
| 526 |
total += data1[i, j] |
| 527 |
|
| 528 |
index_data[:, :] = data1 |
| 529 |
|
| 530 |
tagged_data = self.block.create_data_array("tagged feature data", "test", |
| 531 |
dtype=nix.DataType.Double, shape=(10, 20, 10)) |
| 532 |
dim1 = tagged_data.append_sampled_dimension(1.0) |
| 533 |
dim1.unit = "ms" |
| 534 |
dim2 = tagged_data.append_sampled_dimension(1.0) |
| 535 |
dim2.unit = "ms" |
| 536 |
dim3 = tagged_data.append_sampled_dimension(1.0) |
| 537 |
dim3.unit = "ms" |
| 538 |
|
| 539 |
data2 = np.zeros((10, 20, 10)) |
| 540 |
for i in range(10): |
| 541 |
value = 100 * i |
| 542 |
for j in range(20): |
| 543 |
for k in range(10): |
| 544 |
value += 1 |
| 545 |
data2[i, j, k] = value |
| 546 |
|
| 547 |
tagged_data[:, :, :] = data2 |
| 548 |
|
| 549 |
self.feature_tag.create_feature(index_data, nix.LinkType.Indexed) |
| 550 |
self.feature_tag.create_feature(tagged_data, nix.LinkType.Tagged) |
| 551 |
self.feature_tag.create_feature(index_data, nix.LinkType.Untagged) |
| 552 |
|
| 553 |
|
| 554 |
assert len(self.feature_tag.features) == 3 |
| 555 |
|
| 556 |
|
| 557 |
feat_data = self.feature_tag.feature_data(0, 0) |
| 558 |
assert len(feat_data.shape) == 2 |
| 559 |
assert feat_data.size == 10 |
| 560 |
assert np.sum(feat_data) == 55 |
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
data_view = self.feature_tag.feature_data(9, 0) |
| 565 |
assert np.sum(data_view[:, :]) == 9055 |
| 566 |
|
| 567 |
|
| 568 |
data_view = self.feature_tag.feature_data(0, 2) |
| 569 |
assert data_view.size == 100 |
| 570 |
|
| 571 |
data_view = self.feature_tag.feature_data(0, 2) |
| 572 |
assert data_view.size == 100 |
| 573 |
assert np.sum(data_view) == total |
| 574 |
|
| 575 |
|
| 576 |
data_view = self.feature_tag.feature_data(0, 1) |
| 577 |
assert len(data_view.shape) == 3 |
| 578 |
|
| 579 |
data_view = self.feature_tag.feature_data(1, 1) |
| 580 |
assert len(data_view.shape) == 3 |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
feat_data = self.feature_tag.feature_data(0, index_data.name) |
| 585 |
assert len(feat_data.shape) == 2 |
| 586 |
assert feat_data.size == 10 |
| 587 |
assert np.sum(feat_data) == 55 |
| 588 |
|
| 589 |
|
| 590 |
data_view = self.feature_tag.feature_data(9, index_data.name) |
| 591 |
assert np.sum(data_view[:, :]) == 9055 |
| 592 |
|
| 593 |
|
| 594 |
data_view = self.feature_tag.feature_data(0, tagged_data.name) |
| 595 |
assert len(data_view.shape) == 3 |
| 596 |
|
| 597 |
data_view = self.feature_tag.feature_data(1, tagged_data.name) |
| 598 |
assert len(data_view.shape) == 3 |
| 599 |
|
| 600 |
def out_of_bounds(): |
| 601 |
self.feature_tag.feature_data(2, 1) |
| 602 |
|
| 603 |
self.assertRaises(IndexError, out_of_bounds) |
| 604 |
|
| 605 |
def test_timestamp_autoupdate(self): |
| 606 |
pos = self.block.create_data_array("positions.time", "test.time", |
| 607 |
nix.DataType.Int16, (0, 0)) |
| 608 |
mtag = self.block.create_multi_tag("mtag.time", "test.time", pos) |
| 609 |
|
| 610 |
mtagtime = mtag.updated_at |
| 611 |
time.sleep(1) |
| 612 |
mtag.positions = self.block.create_data_array("pos2.time", |
| 613 |
"test.time", |
| 614 |
nix.DataType.Int8, (0,)) |
| 615 |
self.assertNotEqual(mtag.updated_at, mtagtime) |
| 616 |
|
| 617 |
mtagtime = mtag.updated_at |
| 618 |
time.sleep(1) |
| 619 |
mtag.extents = self.block.create_data_array("extents.time", |
| 620 |
"test.time", |
| 621 |
nix.DataType.Int8, (0,)) |
| 622 |
self.assertNotEqual(mtag.updated_at, mtagtime) |
| 623 |
|
| 624 |
def test_timestamp_noautoupdate(self): |
| 625 |
self.file.auto_update_timestamps = False |
| 626 |
pos = self.block.create_data_array("positions.time", "test.time", |
| 627 |
nix.DataType.Int16, (0, 0)) |
| 628 |
mtag = self.block.create_multi_tag("mtag.time", "test.time", pos) |
| 629 |
|
| 630 |
mtagtime = mtag.updated_at |
| 631 |
time.sleep(1) |
| 632 |
mtag.positions = self.block.create_data_array("pos2.time", |
| 633 |
"test.time", |
| 634 |
nix.DataType.Int8, (0,)) |
| 635 |
self.assertEqual(mtag.updated_at, mtagtime) |
| 636 |
|
| 637 |
mtagtime = mtag.updated_at |
| 638 |
time.sleep(1) |
| 639 |
mtag.extents = self.block.create_data_array("extents.time", |
| 640 |
"test.time", |
| 641 |
nix.DataType.Int8, (0,)) |
| 642 |
self.assertEqual(mtag.updated_at, mtagtime) |
| 643 |
|
| 644 |
def test_multi_tag_feature_dataframe(self): |
| 645 |
numberdata = np.random.random(20) |
| 646 |
number_feat = self.block.create_data_frame( |
| 647 |
"number feature", "test", |
| 648 |
col_dict=OrderedDict([("number", nix.DataType.Float)]), |
| 649 |
data=[(n,) for n in numberdata] |
| 650 |
) |
| 651 |
column_descriptions = OrderedDict([("name", nix.DataType.String), |
| 652 |
("duration", nix.DataType.Double)]) |
| 653 |
values = [("One", 0.1), ("Two", 0.2), ("Three", 0.3), ("Four", 0.4), |
| 654 |
("Five", 0.5), ("Six", 0.6), ("Seven", 0.7), ("Eight", 0.8), |
| 655 |
("Nine", 0.9), ("Ten", 1.0)] |
| 656 |
ramp_feat = self.block.create_data_frame("ramp feature", "test", |
| 657 |
col_dict=column_descriptions, |
| 658 |
data=values) |
| 659 |
ramp_feat.label = "voltage" |
| 660 |
ramp_feat.units = (None, "s") |
| 661 |
|
| 662 |
pos_tag = self.block.create_multi_tag("feature test", "test", [4, 7, 8]) |
| 663 |
|
| 664 |
with self.assertRaises(UnsupportedLinkType): |
| 665 |
pos_tag.create_feature(number_feat, nix.LinkType.Tagged) |
| 666 |
pos_tag.create_feature(number_feat, nix.LinkType.Untagged) |
| 667 |
pos_tag.create_feature(number_feat, nix.LinkType.Indexed) |
| 668 |
with self.assertRaises(UnsupportedLinkType): |
| 669 |
pos_tag.create_feature(ramp_feat, nix.LinkType.Tagged) |
| 670 |
pos_tag.create_feature(ramp_feat, nix.LinkType.Untagged) |
| 671 |
pos_tag.create_feature(ramp_feat, nix.LinkType.Indexed) |
| 672 |
assert len(pos_tag.features) == 4 |
| 673 |
|
| 674 |
for idx, _ in enumerate(pos_tag.positions): |
| 675 |
data1 = pos_tag.feature_data(idx, 0) |
| 676 |
data2 = pos_tag.feature_data(idx, 1) |
| 677 |
data3 = pos_tag.feature_data(idx, 2) |
| 678 |
data4 = pos_tag.feature_data(idx, 3) |
| 679 |
|
| 680 |
|
| 681 |
assert np.all(data1[:] == number_feat[:]) |
| 682 |
assert np.all(data2[:] == number_feat[idx]) |
| 683 |
assert np.all(data3[:] == ramp_feat[:]) |
| 684 |
assert np.all(data4[:] == ramp_feat[idx]) |
| 685 |
|
| 686 |
|
| 687 |
extents = self.block.create_data_array("feature test.extents", "test", |
| 688 |
data=[2, 2, 5]) |
| 689 |
pos_tag.extents = extents |
| 690 |
for idx, _ in enumerate(pos_tag.positions): |
| 691 |
data1 = pos_tag.feature_data(idx, 0) |
| 692 |
data2 = pos_tag.feature_data(idx, 1) |
| 693 |
data3 = pos_tag.feature_data(idx, 2) |
| 694 |
data4 = pos_tag.feature_data(idx, 3) |
| 695 |
|
| 696 |
|
| 697 |
assert np.all(data1[:] == number_feat[:]) |
| 698 |
assert np.all(data2[:] == number_feat[idx]) |
| 699 |
assert np.all(data3[:] == ramp_feat[:]) |
| 700 |
assert np.all(data4[:] == ramp_feat[idx]) |
| 701 |
|
| 702 |
def test_multi_tag_tagged_data_slice_mode(self): |
| 703 |
data = np.random.random_sample((3, 100, 10)) |
| 704 |
da = self.block.create_data_array("signals", "test.signals", data=data) |
| 705 |
da.unit = "mV" |
| 706 |
da.append_set_dimension(labels=["A", "B", "C"]) |
| 707 |
sample_iv = 0.001 |
| 708 |
timedim = da.append_sampled_dimension(sampling_interval=sample_iv) |
| 709 |
timedim.unit = "s" |
| 710 |
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]) |
| 711 |
posdim.unit = "mm" |
| 712 |
|
| 713 |
|
| 714 |
exact_tag = self.block.create_multi_tag("tickpoint", "test.tag", |
| 715 |
positions=[(0, 0.03, 0.0011), (1, 0.05, 0.0015)], |
| 716 |
extents=[(1, 0.02, 0.0005), (1, 0.04, 0.0003)]) |
| 717 |
exact_tag.units = ["none", "s", "m"] |
| 718 |
|
| 719 |
exact_tag.references.append(da) |
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
slice_default = exact_tag.tagged_data(0, 0) |
| 731 |
assert slice_default.shape == (1, 20, 5) |
| 732 |
np.testing.assert_array_equal(slice_default, da[0:1, 30:50, 1:6]) |
| 733 |
|
| 734 |
slice_inclusive = exact_tag.tagged_data(0, 0, stop_rule=nix.SliceMode.Inclusive) |
| 735 |
assert slice_inclusive.shape == (2, 21, 6) |
| 736 |
np.testing.assert_array_equal(slice_inclusive, da[0:2, 30:51, 1:7]) |
| 737 |
|
| 738 |
slice_exclusive = exact_tag.tagged_data(0, 0, stop_rule=nix.SliceMode.Exclusive) |
| 739 |
assert slice_exclusive.shape == (1, 20, 5) |
| 740 |
np.testing.assert_array_equal(slice_exclusive, da[0:1, 30:50, 1:6]) |
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
slice_default = exact_tag.tagged_data(1, 0) |
| 752 |
assert slice_default.shape == (1, 40, 3) |
| 753 |
np.testing.assert_array_equal(slice_default, da[1:2, 50:90, 5:8]) |
| 754 |
|
| 755 |
slice_inclusive = exact_tag.tagged_data(1, 0, stop_rule=nix.SliceMode.Inclusive) |
| 756 |
assert slice_inclusive.shape == (2, 41, 4) |
| 757 |
np.testing.assert_array_equal(slice_inclusive, da[1:3, 50:91, 5:9]) |
| 758 |
|
| 759 |
slice_exclusive = exact_tag.tagged_data(1, 0, stop_rule=nix.SliceMode.Exclusive) |
| 760 |
assert slice_exclusive.shape == (1, 40, 3) |
| 761 |
np.testing.assert_array_equal(slice_exclusive, da[1:2, 50:90, 5:8]) |
| 762 |
|
| 763 |
|
| 764 |
midpoint_tag = self.block.create_multi_tag("midpoint", "test.tag", |
| 765 |
positions=([0, 0.03, 0.0011], [1, 0.05, 0.0015]), |
| 766 |
extents=([1, 0.0301, 0.00051], [1, 0.0401, 0.00031])) |
| 767 |
midpoint_tag.units = ["none", "s", "m"] |
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
midpoint_tag.references.append(da) |
| 779 |
|
| 780 |
|
| 781 |
slice_default = midpoint_tag.tagged_data(0, 0) |
| 782 |
assert slice_default.shape == (1, 31, 6) |
| 783 |
np.testing.assert_array_equal(slice_default, da[0:1, 30:61, 1:7]) |
| 784 |
|
| 785 |
slice_inclusive = midpoint_tag.tagged_data(0, 0, stop_rule=nix.SliceMode.Inclusive) |
| 786 |
assert slice_inclusive.shape == (2, 31, 6) |
| 787 |
np.testing.assert_array_equal(slice_inclusive, da[0:2, 30:61, 1:7]) |
| 788 |
|
| 789 |
slice_exclusive = midpoint_tag.tagged_data(0, 0, stop_rule=nix.SliceMode.Exclusive) |
| 790 |
assert slice_exclusive.shape == (1, 31, 6) |
| 791 |
np.testing.assert_array_equal(slice_exclusive, da[0:1, 30:61, 1:7]) |
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
midpoint_tag.references.append(da) |
| 803 |
|
| 804 |
|
| 805 |
slice_default = midpoint_tag.tagged_data(1, 0) |
| 806 |
assert slice_default.shape == (1, 41, 4) |
| 807 |
np.testing.assert_array_equal(slice_default, da[1:2, 50:91, 5:9]) |
| 808 |
|
| 809 |
slice_inclusive = midpoint_tag.tagged_data(1, 0, stop_rule=nix.SliceMode.Inclusive) |
| 810 |
assert slice_inclusive.shape == (2, 41, 4) |
| 811 |
np.testing.assert_array_equal(slice_inclusive, da[1:3, 50:91, 5:9]) |
| 812 |
|
| 813 |
slice_exclusive = midpoint_tag.tagged_data(1, 0, stop_rule=nix.SliceMode.Exclusive) |
| 814 |
assert slice_exclusive.shape == (1, 41, 4) |
| 815 |
np.testing.assert_array_equal(slice_exclusive, da[1:2, 50:91, 5:9]) |
| 816 |
|
| 817 |
def test_tagged_set_dim(self): |
| 818 |
""" |
| 819 |
Simple test where the slice can be calculated directly from the position and extent and compared to the original |
| 820 |
data. |
| 821 |
Set dimension slicing. |
| 822 |
""" |
| 823 |
nsignals = 10 |
| 824 |
data = np.random.random_sample((nsignals, 100)) |
| 825 |
da = self.block.create_data_array("data", "data", data=data) |
| 826 |
da.append_set_dimension() |
| 827 |
da.append_sampled_dimension(sampling_interval=1).unit = "s" |
| 828 |
|
| 829 |
posarray = self.block.create_data_array("mtag.positions", "test.positions", dtype=float, shape=(1,)) |
| 830 |
extarray = self.block.create_data_array("mtag.extents", "test.extents", dtype=float, shape=(1,)) |
| 831 |
mtag = self.block.create_multi_tag("mtag", "simple", positions=posarray) |
| 832 |
mtag.extents = extarray |
| 833 |
|
| 834 |
mtag.references.append(da) |
| 835 |
|
| 836 |
for pos in range(nsignals): |
| 837 |
for ext in range(2, nsignals-pos): |
| 838 |
mtag.positions[:] = [pos] |
| 839 |
mtag.extents[:] = [ext] |
| 840 |
np.testing.assert_array_almost_equal(mtag.tagged_data(0, 0), da[pos:pos+ext]) |
| 841 |
np.testing.assert_array_almost_equal(mtag.tagged_data(0, 0, nix.SliceMode.Exclusive), da[pos:pos+ext]) |
| 842 |
np.testing.assert_array_almost_equal(mtag.tagged_data(0, 0, nix.SliceMode.Inclusive), da[pos:pos+ext+1]) |
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
mtag.positions[:] = [pos+0.1] |
| 848 |
mtag.extents[:] = [ext+0.1] |
| 849 |
start = pos+1 |
| 850 |
stop = pos+ext+1 |
| 851 |
np.testing.assert_array_almost_equal(mtag.tagged_data(0, 0), da[start:stop]) |
| 852 |
np.testing.assert_array_almost_equal(mtag.tagged_data(0, 0, nix.SliceMode.Exclusive), da[start:stop]) |
| 853 |
np.testing.assert_array_almost_equal(mtag.tagged_data(0, 0, nix.SliceMode.Inclusive), da[start:stop]) |
| 854 |
|
| 855 |
if pos+ext+2 < len(da): |
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
mtag.positions[:] = [pos+0.9] |
| 860 |
mtag.extents[:] = [ext+0.9] |
| 861 |
start = pos+1 |
| 862 |
stop = pos+ext+2 |
| 863 |
np.testing.assert_array_almost_equal(mtag.tagged_data(0, 0), da[start:stop]) |
| 864 |
np.testing.assert_array_almost_equal(mtag.tagged_data(0, 0, nix.SliceMode.Exclusive), |
| 865 |
da[start:stop]) |
| 866 |
np.testing.assert_array_almost_equal(mtag.tagged_data(0, 0, nix.SliceMode.Inclusive), |
| 867 |
da[start:stop]) |
| 868 |
|
| 869 |
def test_tagged_range_dim(self): |
| 870 |
""" |
| 871 |
Simple test where the slice can be calculated directly from the position and extent and compared to the original |
| 872 |
data. |
| 873 |
Range dimension slicing. |
| 874 |
""" |
| 875 |
nticks = 10 |
| 876 |
data = np.random.random_sample((nticks, 100)) |
| 877 |
da = self.block.create_data_array("data", "data", data=data) |
| 878 |
da.append_range_dimension(ticks=range(nticks)) |
| 879 |
da.append_sampled_dimension(sampling_interval=1).unit = "s" |
| 880 |
|
| 881 |
posarray = self.block.create_data_array("mtag.positions", "test.positions", dtype=float, shape=(1,)) |
| 882 |
extarray = self.block.create_data_array("mtag.extents", "test.extents", dtype=float, shape=(1,)) |
| 883 |
mtag = self.block.create_multi_tag("mtag", "simple", positions=posarray) |
| 884 |
mtag.extents = extarray |
| 885 |
|
| 886 |
mtag.references.append(da) |
| 887 |
|
| 888 |
for pos in range(nticks): |
| 889 |
for ext in range(2, nticks-pos): |
| 890 |
mtag.positions[:] = [pos] |
| 891 |
mtag.extents[:] = [ext] |
| 892 |
np.testing.assert_array_almost_equal(mtag.tagged_data(0, 0), da[pos:pos+ext]) |
| 893 |
np.testing.assert_array_almost_equal(mtag.tagged_data(0, 0, nix.SliceMode.Exclusive), da[pos:pos+ext]) |
| 894 |
np.testing.assert_array_almost_equal(mtag.tagged_data(0, 0, nix.SliceMode.Inclusive), da[pos:pos+ext+1]) |
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
mtag.positions[:] = [pos+0.1] |
| 900 |
mtag.extents[:] = [ext+0.1] |
| 901 |
start = pos+1 |
| 902 |
stop = pos+ext+1 |
| 903 |
np.testing.assert_array_almost_equal(mtag.tagged_data(0, 0), da[start:stop]) |
| 904 |
np.testing.assert_array_almost_equal(mtag.tagged_data(0, 0, nix.SliceMode.Exclusive), da[start:stop]) |
| 905 |
np.testing.assert_array_almost_equal(mtag.tagged_data(0, 0, nix.SliceMode.Inclusive), da[start:stop]) |
| 906 |
|
| 907 |
if pos+ext+2 < len(da): |
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
mtag.positions[:] = [pos+0.9] |
| 912 |
mtag.extents[:] = [ext+0.9] |
| 913 |
start = pos+1 |
| 914 |
stop = pos+ext+2 |
| 915 |
np.testing.assert_array_almost_equal(mtag.tagged_data(0, 0), da[start:stop]) |
| 916 |
np.testing.assert_array_almost_equal(mtag.tagged_data(0, 0, nix.SliceMode.Exclusive), |
| 917 |
da[start:stop]) |
| 918 |
np.testing.assert_array_almost_equal(mtag.tagged_data(0, 0, nix.SliceMode.Inclusive), |
| 919 |
da[start:stop]) |
| 920 |
|
| 921 |
def test_tagged_sampled_dim(self): |
| 922 |
""" |
| 923 |
Simple test where the slice can be calculated directly from the position and extent and compared to the original |
| 924 |
data. |
| 925 |
Sampled dimension slicing. |
| 926 |
""" |
| 927 |
nticks = 10 |
| 928 |
data = np.random.random_sample((nticks, 100)) |
| 929 |
da = self.block.create_data_array("data", "data", data=data) |
| 930 |
da.append_sampled_dimension(sampling_interval=1).unit = "V" |
| 931 |
da.append_sampled_dimension(sampling_interval=1).unit = "s" |
| 932 |
|
| 933 |
posarray = self.block.create_data_array("mtag.positions", "test.positions", dtype=float, shape=(1,)) |
| 934 |
extarray = self.block.create_data_array("mtag.extents", "test.extents", dtype=float, shape=(1,)) |
| 935 |
mtag = self.block.create_multi_tag("mtag", "simple", positions=posarray) |
| 936 |
mtag.extents = extarray |
| 937 |
mtag.units = ["V", "s"] |
| 938 |
|
| 939 |
mtag.references.append(da) |
| 940 |
|
| 941 |
for pos in range(nticks): |
| 942 |
for ext in range(2, nticks-pos): |
| 943 |
mtag.positions[:] = [pos] |
| 944 |
mtag.extents[:] = [ext] |
| 945 |
np.testing.assert_array_almost_equal(mtag.tagged_data(0, 0), da[pos:pos+ext]) |
| 946 |
np.testing.assert_array_almost_equal(mtag.tagged_data(0, 0, nix.SliceMode.Exclusive), da[pos:pos+ext]) |
| 947 |
np.testing.assert_array_almost_equal(mtag.tagged_data(0, 0, nix.SliceMode.Inclusive), |
| 948 |
da[pos:pos+ext+1]) |
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
mtag.positions[:] = [pos+0.1] |
| 954 |
mtag.extents[:] = [ext+0.1] |
| 955 |
start = pos+1 |
| 956 |
stop = pos+ext+1 |
| 957 |
np.testing.assert_array_almost_equal(mtag.tagged_data(0, 0), da[start:stop]) |
| 958 |
np.testing.assert_array_almost_equal(mtag.tagged_data(0, 0, nix.SliceMode.Exclusive), da[start:stop]) |
| 959 |
np.testing.assert_array_almost_equal(mtag.tagged_data(0, 0, nix.SliceMode.Inclusive), da[start:stop]) |
| 960 |
|
| 961 |
if pos+ext+2 < len(da): |
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
mtag.positions[:] = [pos+0.9] |
| 966 |
mtag.extents[:] = [ext+0.9] |
| 967 |
start = pos+1 |
| 968 |
stop = pos+ext+2 |
| 969 |
np.testing.assert_array_almost_equal(mtag.tagged_data(0, 0), da[start:stop]) |
| 970 |
np.testing.assert_array_almost_equal(mtag.tagged_data(0, 0, nix.SliceMode.Exclusive), |
| 971 |
da[start:stop]) |
| 972 |
np.testing.assert_array_almost_equal(mtag.tagged_data(0, 0, nix.SliceMode.Inclusive), |
| 973 |
da[start:stop]) |