File: test_feature.py

package info (click to toggle)
python-nixio 1.5.4%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,888 kB
  • sloc: python: 12,527; cpp: 832; makefile: 25
file content (132 lines) | stat: -rw-r--r-- 5,075 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# -*- coding: utf-8 -*-
# Copyright © 2014, German Neuroinformatics Node (G-Node)
#
# All rights reserved.
#
# Redistribution and use in section and binary forms, with or without
# modification, are permitted under the terms of the BSD License. See
# LICENSE file in the root of the Project.
import os
import time
import unittest
from collections import OrderedDict
import nixio as nix
from .tmp import TempDir


class TestFeatures(unittest.TestCase):

    def setUp(self):
        self.tmpdir = TempDir("featuretest")
        self.testfilename = os.path.join(self.tmpdir.path, "featuretest.nix")
        self.file = nix.File.open(self.testfilename, nix.FileMode.Overwrite)
        self.block = self.file.create_block("test block", "recordingsession")

        self.group = self.block.create_group("test group", "feature test")

        self.signal = self.block.create_data_array("output", "analogsignal",
                                                   nix.DataType.Float, (0, ))
        self.stimuli_tag = self.block.create_tag(
            "stimuli used", "tag", [0]
        )
        self.stimuli_tag.references.append(self.signal)

        self.movie1 = self.block.create_data_array("stimulus movie 1", "movie",
                                                   nix.DataType.Float, (0, ))
        self.feature_1 = self.stimuli_tag.create_feature(
            self.movie1, nix.LinkType.Tagged
        )
        self.movie2 = self.block.create_data_array("stimulus movie 2", "movie",
                                                   nix.DataType.Float, (0, ))
        self.feature_2 = self.stimuli_tag.create_feature(
            self.movie2, nix.LinkType.Tagged
        )

    def tearDown(self):
        del self.file.blocks[self.block.id]
        self.file.close()
        self.tmpdir.cleanup()

    def test_feature_eq(self):
        assert self.feature_1 == self.feature_1
        assert not self.feature_1 == self.feature_2
        assert self.feature_1 is not None

    def test_feature_id(self):
        assert self.feature_1.id is not None

    def test_feature_link_type(self):
        def set_none():
            self.feature_1.link_type = None

        assert self.feature_1.link_type is not None
        self.assertRaises(Exception, set_none)

        self.feature_1.link_type = nix.LinkType.Untagged
        assert self.feature_1.link_type == nix.LinkType.Untagged

    def test_feature_data(self):
        def set_none():
            self.feature_1.data = None

        assert self.feature_1.data is not None
        self.assertRaises(Exception, set_none)

        new_data_ref = self.block.create_data_array("test", "current",
                                                    nix.DataType.Float, (0, ))
        self.feature_1.data = new_data_ref
        assert self.feature_1.data == new_data_ref

    def test_feature_dataframe(self):
        coltypes = OrderedDict(
            idx=int,
            name=str,
            value=float,
        )
        new_data_frame = self.block.create_data_frame("table", "test.feature",
                                                      col_dict=coltypes)
        df_feature = self.stimuli_tag.create_feature(new_data_frame, nix.LinkType.Indexed)
        assert df_feature.data == new_data_frame

    def test_feature_on_group(self):
        grouptag = self.block.create_tag("I am tag", "grouptest", [0])
        self.group.tags.append(grouptag)

        grouptag = self.group.tags[-1]
        grouptag.create_feature(self.movie1, nix.LinkType.Tagged)

    def test_create_diff_link_type_style(self):
        self.stimuli_tag.create_feature(self.movie1, nix.LinkType.Tagged)

    def test_timestamp_autoupdate(self):
        array = self.block.create_data_array("array.time", "signal",
                                             nix.DataType.Double, (100, ))
        feature = self.stimuli_tag.create_feature(array, nix.LinkType.Tagged)

        ftime = feature.updated_at
        time.sleep(1)
        feature.data = self.block.create_data_array("alt.array", "signal",
                                                    nix.DataType.Int8, (1,))
        self.assertNotEqual(ftime, feature.updated_at)

        ftime = feature.updated_at
        time.sleep(1)
        feature.link_type = nix.LinkType.Untagged
        self.assertNotEqual(ftime, feature.updated_at)

    def test_timestamp_noautoupdate(self):
        self.file.auto_update_timestamps = False
        array = self.block.create_data_array("array.time", "signal",
                                             nix.DataType.Double, (100, ))
        feature = self.stimuli_tag.create_feature(array, nix.LinkType.Tagged)

        ftime = feature.updated_at
        time.sleep(1)
        feature.data = self.block.create_data_array("alt.array", "signal",
                                                    nix.DataType.Int8, (1,))
        self.assertEqual(ftime, feature.updated_at)

        ftime = feature.updated_at
        time.sleep(1)
        feature.link_type = nix.LinkType.Untagged
        self.assertEqual(ftime, feature.updated_at)