| 1 |
import unittest |
| 2 |
|
| 3 |
import nixio as nix |
| 4 |
from .tmp import TempDir |
| 5 |
import os |
| 6 |
import time |
| 7 |
import numpy as np |
| 8 |
try: |
| 9 |
from collections.abc import OrderedDict |
| 10 |
except ImportError: |
| 11 |
from collections import OrderedDict |
| 12 |
|
| 13 |
|
| 14 |
class TestDataFrame(unittest.TestCase): |
| 15 |
|
| 16 |
def setUp(self): |
| 17 |
self.tmpdir = TempDir("dataframetest") |
| 18 |
self.testfilename = os.path.join(self.tmpdir.path, "dataframetest.nix") |
| 19 |
self.file = nix.File.open(self.testfilename, nix.FileMode.Overwrite) |
| 20 |
self.block = self.file.create_block("test block", "recordingsession") |
| 21 |
self.df1_dtype = OrderedDict([('name', np.int64), ('id', str), ('time', float), |
| 22 |
('sig1', np.float64), ('sig2', np.int32)]) |
| 23 |
self.df1_data = [(1, "alpha", 20.18, 5.0, 100), |
| 24 |
(2, "beta", 20.09, 5.5, 101), |
| 25 |
(2, "gamma", 20.05, 5.1, 100), |
| 26 |
(1, "delta", 20.15, 5.3, 150), |
| 27 |
(2, "epsilon", 20.23, 5.7, 200), |
| 28 |
(2, "fi", 20.07, 5.2, 300), |
| 29 |
(1, "zeta", 20.12, 5.1, 39), |
| 30 |
(1, "eta", 20.27, 5.1, 600), |
| 31 |
(2, "theta", 20.15, 5.6, 400), |
| 32 |
(2, "iota", 20.08, 5.1, 200)] |
| 33 |
other_arr = np.arange(11101, 11200).reshape((33, 3)) |
| 34 |
other_di = OrderedDict({'name': np.int64, 'id': int, 'time': float}) |
| 35 |
self.df1 = self.block.create_data_frame("test df", "signal1", |
| 36 |
data=self.df1_data, col_dict=self.df1_dtype) |
| 37 |
self.df2 = self.block.create_data_frame("other df", "signal2", |
| 38 |
data=self.df1_data, col_dict=self.df1_dtype) |
| 39 |
self.df3 = self.block.create_data_frame("reference df", "signal3", |
| 40 |
data=other_arr, |
| 41 |
col_dict=other_di) |
| 42 |
self.dtype = self.df1._h5group.group["data"].dtype |
| 43 |
|
| 44 |
def tearDown(self): |
| 45 |
self.file.close() |
| 46 |
self.tmpdir.cleanup() |
| 47 |
|
| 48 |
def test_data_frame_eq(self): |
| 49 |
assert self.df1 == self.df1 |
| 50 |
assert not self.df1 == self.df2 |
| 51 |
assert self.df2 == self.df2 |
| 52 |
assert self.df1 is not None |
| 53 |
assert self.df2 is not None |
| 54 |
|
| 55 |
def test_create_with_list(self): |
| 56 |
arr = [(1, 'a', 20.18, 5.1, 100), (2, 'b', 20.09, 5.5, 101), |
| 57 |
(2, 'c', 20.05, 5.1, 100)] |
| 58 |
namelist = np.array(['name', 'id', 'time', 'sig1', 'sig2']) |
| 59 |
dtlist = np.array([np.int64, str, float, np.float64, np.int32]) |
| 60 |
df_li = self.block.create_data_frame("test_list", "make_of_list", |
| 61 |
data=arr, col_names=namelist, |
| 62 |
col_dtypes=dtlist) |
| 63 |
assert df_li.column_names == self.df1.column_names |
| 64 |
assert df_li.dtype == self.df1.dtype |
| 65 |
for i in df_li[:]: |
| 66 |
self.assertIsInstance(i['id'], str) |
| 67 |
self.assertIsInstance(i['sig2'], np.int32) |
| 68 |
|
| 69 |
def test_column_name_collision(self): |
| 70 |
arr = [(1, 'a', 20.18, 5.1, 100), (2, 'b', 20.09, 5.5, 101), |
| 71 |
(2, 'c', 20.05, 5.1, 100)] |
| 72 |
dtlist = np.array([np.int64, str, float, np.float64, np.int32]) |
| 73 |
namelist = np.array(['name', 'name', 'name', 'name', 'name']) |
| 74 |
self.assertRaises(nix.exceptions.DuplicateColumnName, |
| 75 |
self.block.create_data_frame, |
| 76 |
'testerror', 'for_test', |
| 77 |
col_names=namelist, |
| 78 |
col_dtypes=dtlist, data=arr) |
| 79 |
|
| 80 |
def test_data_frame_type(self): |
| 81 |
assert self.df1.type == "signal1" |
| 82 |
self.df1.type = "test change" |
| 83 |
assert self.df1.type == "test change" |
| 84 |
|
| 85 |
def test_write_row(self): |
| 86 |
|
| 87 |
row = ["1", 'abc', 3, 4.4556356242341, 5.1111111] |
| 88 |
assert list(self.df1[9]) == [2, 'iota', 20.08, 5.1, 200] |
| 89 |
self.df1.write_rows([row], [9]) |
| 90 |
assert list(self.df1[9]) == [1, 'abc', 3., 4.4556356242341, 5] |
| 91 |
self.assertIsInstance(self.df1[9]['name'], np.integer) |
| 92 |
self.assertIsInstance(self.df1[9]['sig2'], np.int32) |
| 93 |
assert self.df1[9]['sig2'] == int(5) |
| 94 |
|
| 95 |
multi_rows = [[1775, '12355', 1777, 1778, 1779], |
| 96 |
[1785, '12355', 1787, 1788, 1789]] |
| 97 |
self.df1.write_rows(multi_rows, [1, 2]) |
| 98 |
assert list(self.df1[1]) == [1775, '12355', 1777, 1778, 1779] |
| 99 |
assert list(self.df1[2]) == [1785, '12355', 1787, 1788, 1789] |
| 100 |
|
| 101 |
def test_write_column(self): |
| 102 |
|
| 103 |
column1 = np.arange(10000, 10010) |
| 104 |
self.df1.write_column(column1, name='sig1') |
| 105 |
assert list(self.df1[:]['sig1']) == list(column1) |
| 106 |
|
| 107 |
column2 = np.arange(20000, 20010) |
| 108 |
self.df1.write_column(column2, index=4) |
| 109 |
|
| 110 |
assert list(self.df1[:]['sig2']) == list(column2) |
| 111 |
|
| 112 |
def test_read_row(self): |
| 113 |
df1_array = np.array(self.df1_data, dtype=list(self.df1_dtype.items())) |
| 114 |
|
| 115 |
assert self.df1.read_rows(0) == df1_array[0] |
| 116 |
|
| 117 |
multi_rows = self.df1.read_rows(np.arange(4, 9)) |
| 118 |
np.testing.assert_array_equal(multi_rows, df1_array[4:9]) |
| 119 |
multi_rows = self.df1.read_rows([3, 6]) |
| 120 |
np.testing.assert_array_equal(multi_rows, [df1_array[3], df1_array[6]]) |
| 121 |
|
| 122 |
def test_read_column(self): |
| 123 |
|
| 124 |
single_idx_col = self.df1.read_columns(index=[1]) |
| 125 |
data = np.array([row[1] for row in self.df1_data], dtype=nix.DataType.String) |
| 126 |
np.testing.assert_array_equal(single_idx_col, data) |
| 127 |
|
| 128 |
|
| 129 |
multi_col = self.df1.read_columns(name=['sig1', 'sig2']) |
| 130 |
data = [(row[3], row[4]) for row in self.df1_data] |
| 131 |
assert len(multi_col) == 10 |
| 132 |
for data_row, df_row in zip(data, multi_col): |
| 133 |
assert data_row == tuple(df_row) |
| 134 |
|
| 135 |
|
| 136 |
slice_cols = self.df1.read_columns(name=['sig1', 'sig2'], slc=slice(0, 6)) |
| 137 |
data = [(row[3], row[4]) for row in self.df1_data[:6]] |
| 138 |
assert len(slice_cols) == 6 |
| 139 |
for data_row, df_row in zip(data, slice_cols): |
| 140 |
assert data_row == tuple(df_row) |
| 141 |
|
| 142 |
|
| 143 |
single_idx_col = self.df1.read_columns(name=["sig2"]) |
| 144 |
data = np.array([100, 101, 100, 150, 200, 300, 39, 600, 400, 200], dtype=nix.DataType.Int32) |
| 145 |
np.testing.assert_array_equal(single_idx_col, data) |
| 146 |
|
| 147 |
|
| 148 |
slice_str_cols = self.df1.read_columns(name=['id', 'sig2'], slc=slice(3, 10)) |
| 149 |
data = [(row[1], row[4]) for row in self.df1_data[3:10]] |
| 150 |
assert len(slice_str_cols) == 7 |
| 151 |
for data_row, df_row in zip(data, slice_str_cols): |
| 152 |
assert data_row == tuple(df_row) |
| 153 |
|
| 154 |
def test_index_column_by_name(self): |
| 155 |
for colidx, colname in enumerate(self.df1_dtype.keys()): |
| 156 |
expdata = [row[colidx] for row in self.df1_data] |
| 157 |
assert all(self.df1[colname] == expdata) |
| 158 |
|
| 159 |
def test_read_cell(self): |
| 160 |
|
| 161 |
scell = self.df1.read_cell(position=[5, 3]) |
| 162 |
assert scell == 5.2 |
| 163 |
|
| 164 |
crcell = self.df1.read_cell(col_name=['id'], row_idx=9) |
| 165 |
assert crcell == 'iota' |
| 166 |
|
| 167 |
self.assertRaises(ValueError, self.df1.read_cell, row_idx=10) |
| 168 |
self.assertRaises(ValueError, self.df1.read_cell, col_name='sig1') |
| 169 |
|
| 170 |
def test_write_cell(self): |
| 171 |
|
| 172 |
self.df1.write_cell(105, position=[8, 3]) |
| 173 |
assert self.df1[8]['sig1'] == 105 |
| 174 |
|
| 175 |
self.df1.write_cell('test', col_name='id', row_idx=3) |
| 176 |
assert self.df1[3]['id'] == 'test' |
| 177 |
|
| 178 |
self.assertRaises(ValueError, self.df1.write_cell, 11, col_name='sig1') |
| 179 |
|
| 180 |
def test_append_column(self): |
| 181 |
col_data = np.arange(start=16000, stop=16010, step=1) |
| 182 |
self.df1.append_column(col_data, name='trial_col', datatype=int) |
| 183 |
assert self.df1.column_names == ('name', 'id', 'time', |
| 184 |
'sig1', 'sig2', 'trial_col') |
| 185 |
assert len(self.df1.dtype) == 6 |
| 186 |
k = np.array(self.df1[0:10]["trial_col"], dtype=np.int64) |
| 187 |
np.testing.assert_almost_equal(k, col_data) |
| 188 |
|
| 189 |
sh_col = np.arange(start=16000, stop=16003, step=1) |
| 190 |
with self.assertRaises(ValueError): |
| 191 |
self.df1.append_column(sh_col, name='sh_col') |
| 192 |
|
| 193 |
long = np.arange(start=16000, stop=16500, step=1) |
| 194 |
with self.assertRaises(ValueError): |
| 195 |
self.df1.append_column(long, name='long') |
| 196 |
|
| 197 |
def test_append_rows(self): |
| 198 |
|
| 199 |
srow = (1, "test", 3, 4, 5) |
| 200 |
self.df1.append_rows([srow]) |
| 201 |
assert self.df1[10] == np.array(srow, dtype=list(self.df1_dtype.items())) |
| 202 |
|
| 203 |
mrows = [(1, "2", 3, 4, 5), (6, "testing", 8, 9, 10)] |
| 204 |
self.df1.append_rows(mrows) |
| 205 |
assert all(self.df1[-2:] == np.array(mrows, dtype=list(self.df1_dtype.items()))) |
| 206 |
|
| 207 |
errrow = [5, 6, 7, 8] |
| 208 |
self.assertRaises(ValueError, self.df1.append_rows, [errrow]) |
| 209 |
|
| 210 |
def test_unit(self): |
| 211 |
assert self.df1.units is None |
| 212 |
self.df1.units = ["s", 'A', 'ms', 'Hz', 'mA'] |
| 213 |
np.testing.assert_array_equal(self.df1.units, |
| 214 |
np.array(["s", 'A', 'ms', 'Hz', 'mA'])) |
| 215 |
assert self.df2.units is None |
| 216 |
|
| 217 |
def test_df_shape(self): |
| 218 |
assert tuple(self.df1.df_shape) == (10, 5) |
| 219 |
|
| 220 |
arr = np.arange(1000).reshape(10, 10, 10) |
| 221 |
with self.assertRaises(ValueError): |
| 222 |
self.block.create_data_frame('err', 'err', |
| 223 |
{'name': np.int64}, |
| 224 |
data=arr) |
| 225 |
|
| 226 |
def test_data_type(self): |
| 227 |
assert self.df1.dtype[4] == np.int32 |
| 228 |
assert self.df1.dtype[0] != self.df1.dtype[4] |
| 229 |
assert self.df1.dtype[2] == self.df1.dtype[3] |
| 230 |
|
| 231 |
def test_create_without_dtypes(self): |
| 232 |
data = np.array([("a", 1, 2.2), ("b", 2, 3.3), ("c", 3, 4.4)], |
| 233 |
dtype=[('name', 'U10'), ("id", 'i4'), ('val', 'f4')]) |
| 234 |
df = self.block.create_data_frame("without_name", "test", data=data) |
| 235 |
assert sorted(list(df.column_names)) == sorted(["name", "id", "val"]) |
| 236 |
assert sorted(list(df["name"])) == ["a", "b", "c"] |
| 237 |
|
| 238 |
def test_timestamp_autoupdate(self): |
| 239 |
self.file.auto_update_timestamps = True |
| 240 |
df = self.block.create_data_frame("df.time", "test.time", |
| 241 |
col_dict=OrderedDict({"idx": int})) |
| 242 |
dftime = df.updated_at |
| 243 |
time.sleep(1) |
| 244 |
df.units = ("ly",) |
| 245 |
self.assertNotEqual(dftime, df.updated_at) |
| 246 |
|
| 247 |
def test_timestamp_noautoupdate(self): |
| 248 |
self.file.auto_update_timestamps = False |
| 249 |
df = self.block.create_data_frame("df.time", "test.time", |
| 250 |
col_dict=OrderedDict({"idx": int})) |
| 251 |
dftime = df.updated_at |
| 252 |
time.sleep(1) |
| 253 |
df.units = ("ly",) |
| 254 |
self.assertEqual(dftime, df.updated_at) |