File: test_core.py

package info (click to toggle)
pynwb 2.8.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 44,312 kB
  • sloc: python: 17,501; makefile: 597; sh: 11
file content (152 lines) | stat: -rw-r--r-- 4,627 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from datetime import datetime
from dateutil.tz import tzlocal

import numpy as np
from hdmf.utils import docval

from pynwb import NWBFile, TimeSeries, available_namespaces
from pynwb.core import NWBContainer, NWBData
from pynwb.testing import TestCase


class MyTestClass(NWBContainer):

    __nwbfields__ = ("prop1", "prop2")

    @docval({"name": "name", "type": str, "doc": "The name of this container"})
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.prop1 = "test1"


class TestNWBContainer(TestCase):
    def test_constructor(self):
        """Test constructor
        """
        obj = MyTestClass("obj1")
        self.assertEqual(obj.name, "obj1")
        obj.prop2 = "test2"

    def test_nwbfields(self):
        """Test that getters and setters work for nwbfields
        """
        obj = MyTestClass("obj1")
        obj.prop2 = "test2"
        self.assertEqual(obj.prop1, "test1")
        self.assertEqual(obj.prop2, "test2")

    def test_get_data_type(self):
        obj = NWBContainer("obj1")
        dt = obj.data_type
        self.assertEqual(dt, 'NWBContainer')


class MyNWBData(NWBData):

    __nwbfields__ = ("data", )

    @docval(
        {"name": "name", "type": str, "doc": "The name of this container"},
        {"name": "data", "type": ("array_data", "data"), "doc": "any data"},
    )
    def __init__(self, **kwargs):
        super().__init__(**kwargs)


class TestNWBData(TestCase):
    def test_constructor(self):
        """Test constructor
        """
        obj = MyNWBData("obj1", data=[[1, 2, 3], [1, 2, 3]])
        self.assertEqual(obj.name, "obj1")

    def test_append_list(self):

        obj = MyNWBData("obj1", data=[[1, 2, 3], [1, 2, 3]])
        obj.append([4, 5, 6])
        np.testing.assert_array_equal(obj.data, [[1, 2, 3], [1, 2, 3], [4, 5, 6]])

    def test_append_ndarray_2d(self):
        obj = MyNWBData("obj1", data=np.array([[1, 2, 3], [1, 2, 3]]))
        obj.append([4, 5, 6])
        np.testing.assert_array_equal(obj.data, [[1, 2, 3], [1, 2, 3], [4, 5, 6]])

        def test_append_ndarray_1d(self):
            obj = MyNWBData("obj1", data=np.array([1, 2, 3]))
            obj.append([4])
            np.testing.assert_array_equal(obj.data, [1, 2, 3, 4])

    def test_extend_list(self):
        obj = MyNWBData("obj1", data=[[1, 2, 3], [1, 2, 3]])
        obj.extend([[4, 5, 6]])
        np.testing.assert_array_equal(obj.data, [[1, 2, 3], [1, 2, 3], [4, 5, 6]])

    def test_extend_ndarray_1d(self):
        obj = MyNWBData("obj1", data=np.array([1, 2, 3]))
        obj.extend([4, 5, 6])
        np.testing.assert_array_equal(obj.data, [1, 2, 3, 4, 5, 6])

    def test_extend_ndarray_2d(self):
        obj = MyNWBData("obj1", data=np.array([[1, 2, 3], [1, 2, 3]]))
        obj.extend([[4, 5, 6]])
        np.testing.assert_array_equal(obj.data, [[1, 2, 3], [1, 2, 3], [4, 5, 6]])


class TestPrint(TestCase):
    def test_print_file(self):
        nwbfile = NWBFile(
            session_description="session_description",
            identifier="identifier",
            session_start_time=datetime.now(tzlocal()),
        )
        ts1 = TimeSeries(
            name="name1",
            data=[1000, 2000, 3000],
            unit="unit",
            timestamps=[1.0, 2.0, 3.0],
        )
        ts2 = TimeSeries(
            name="name2",
            data=[1000, 2000, 3000],
            unit="unit",
            timestamps=[1.0, 2.0, 3.0],
        )
        expected = """name1 pynwb.base.TimeSeries at 0x%d
Fields:
  comments: no comments
  conversion: 1.0
  data: [1000 2000 3000]
  description: no description
  interval: 1
  offset: 0.0
  resolution: -1.0
  timestamps: [1. 2. 3.]
  timestamps_unit: seconds
  unit: unit
"""
        expected %= id(ts1)
        self.assertEqual(str(ts1), expected)
        nwbfile.add_acquisition(ts1)
        nwbfile.add_acquisition(ts2)
        nwbfile.add_epoch(start_time=1.0, stop_time=10.0, tags=["tag1", "tag2"])
        expected_re = r"""root pynwb\.file\.NWBFile at 0x\d+
Fields:
  acquisition: {
    name1 <class 'pynwb\.base\.TimeSeries'>,
    name2 <class 'pynwb\.base\.TimeSeries'>
  }
  epochs: epochs <class 'pynwb.epoch.TimeIntervals'>
  file_create_date: \[datetime.datetime\(.*\)\]
  identifier: identifier
  session_description: session_description
  session_start_time: .*
  timestamps_reference_time: .*
"""
        self.assertRegex(str(nwbfile), expected_re)


class TestAvailableNamespaces(TestCase):
    def test_available_namespaces(self):
        self.assertEqual(
            available_namespaces(), ("hdmf-common", "hdmf-experimental", "core")
        )