Debian Codegraph @

nixio/test/test_feature.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # Copyright © 2014, 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 time
11 import unittest
12 from collections import OrderedDict
13 import nixio as nix
14 from .tmp import TempDir
15
16
17 class TestFeatures(unittest.TestCase):
18
19 def setUp(self):
20 self.tmpdir = TempDir("featuretest")
21 self.testfilename = os.path.join(self.tmpdir.path, "featuretest.nix")
22 self.file = nix.File.open(self.testfilename, nix.FileMode.Overwrite)
23 self.block = self.file.create_block("test block", "recordingsession")
24
25 self.group = self.block.create_group("test group", "feature test")
26
27 self.signal = self.block.create_data_array("output", "analogsignal",
28 nix.DataType.Float, (0, ))
29 self.stimuli_tag = self.block.create_tag(
30 "stimuli used", "tag", [0]
31 )
32 self.stimuli_tag.references.append(self.signal)
33
34 self.movie1 = self.block.create_data_array("stimulus movie 1", "movie",
35 nix.DataType.Float, (0, ))
36 self.feature_1 = self.stimuli_tag.create_feature(
37 self.movie1, nix.LinkType.Tagged
38 )
39 self.movie2 = self.block.create_data_array("stimulus movie 2", "movie",
40 nix.DataType.Float, (0, ))
41 self.feature_2 = self.stimuli_tag.create_feature(
42 self.movie2, nix.LinkType.Tagged
43 )
44
45 def tearDown(self):
46 del self.file.blocks[self.block.id]
47 self.file.close()
48 self.tmpdir.cleanup()
49
50 def test_feature_eq(self):
51 assert self.feature_1 == self.feature_1
52 assert not self.feature_1 == self.feature_2
53 assert self.feature_1 is not None
54
55 def test_feature_id(self):
56 assert self.feature_1.id is not None
57
58 def test_feature_link_type(self):
59 def set_none():
60 self.feature_1.link_type = None
61
62 assert self.feature_1.link_type is not None
63 self.assertRaises(Exception, set_none)
64
65 self.feature_1.link_type = nix.LinkType.Untagged
66 assert self.feature_1.link_type == nix.LinkType.Untagged
67
68 def test_feature_data(self):
69 def set_none():
70 self.feature_1.data = None
71
72 assert self.feature_1.data is not None
73 self.assertRaises(Exception, set_none)
74
75 new_data_ref = self.block.create_data_array("test", "current",
76 nix.DataType.Float, (0, ))
77 self.feature_1.data = new_data_ref
78 assert self.feature_1.data == new_data_ref
79
80 def test_feature_dataframe(self):
81 coltypes = OrderedDict(
82 idx=int,
83 name=str,
84 value=float,
85 )
86 new_data_frame = self.block.create_data_frame("table", "test.feature",
87 col_dict=coltypes)
88 df_feature = self.stimuli_tag.create_feature(new_data_frame, nix.LinkType.Indexed)
89 assert df_feature.data == new_data_frame
90
91 def test_feature_on_group(self):
92 grouptag = self.block.create_tag("I am tag", "grouptest", [0])
93 self.group.tags.append(grouptag)
94
95 grouptag = self.group.tags[-1]
96 grouptag.create_feature(self.movie1, nix.LinkType.Tagged)
97
98 def test_create_diff_link_type_style(self):
99 self.stimuli_tag.create_feature(self.movie1, nix.LinkType.Tagged)
100
101 def test_timestamp_autoupdate(self):
102 array = self.block.create_data_array("array.time", "signal",
103 nix.DataType.Double, (100, ))
104 feature = self.stimuli_tag.create_feature(array, nix.LinkType.Tagged)
105
106 ftime = feature.updated_at
107 time.sleep(1)
108 feature.data = self.block.create_data_array("alt.array", "signal",
109 nix.DataType.Int8, (1,))
110 self.assertNotEqual(ftime, feature.updated_at)
111
112 ftime = feature.updated_at
113 time.sleep(1)
114 feature.link_type = nix.LinkType.Untagged
115 self.assertNotEqual(ftime, feature.updated_at)
116
117 def test_timestamp_noautoupdate(self):
118 self.file.auto_update_timestamps = False
119 array = self.block.create_data_array("array.time", "signal",
120 nix.DataType.Double, (100, ))
121 feature = self.stimuli_tag.create_feature(array, nix.LinkType.Tagged)
122
123 ftime = feature.updated_at
124 time.sleep(1)
125 feature.data = self.block.create_data_array("alt.array", "signal",
126 nix.DataType.Int8, (1,))
127 self.assertEqual(ftime, feature.updated_at)
128
129 ftime = feature.updated_at
130 time.sleep(1)
131 feature.link_type = nix.LinkType.Untagged
132 self.assertEqual(ftime, feature.updated_at)