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
|
from copy import copy, deepcopy
import numpy as np
from hdmf.container import Data
from hdmf.data_utils import DataIO
from hdmf.testing import TestCase
import warnings
class DataIOTests(TestCase):
def test_copy(self):
obj = DataIO(data=[1., 2., 3.])
obj_copy = copy(obj)
self.assertNotEqual(id(obj), id(obj_copy))
self.assertEqual(id(obj.data), id(obj_copy.data))
def test_deepcopy(self):
obj = DataIO(data=[1., 2., 3.])
obj_copy = deepcopy(obj)
self.assertNotEqual(id(obj), id(obj_copy))
self.assertNotEqual(id(obj.data), id(obj_copy.data))
def test_dataio_slice_delegation(self):
indata = np.arange(30)
dset = DataIO(indata)
self.assertTrue(np.all(dset[2:15] == indata[2:15]))
indata = np.arange(50).reshape(5, 10)
dset = DataIO(indata)
self.assertTrue(np.all(dset[1:3, 5:8] == indata[1:3, 5:8]))
def test_set_dataio(self):
"""
Test that Data.set_dataio works as intended
"""
dataio = DataIO()
data = np.arange(30).reshape(5, 2, 3)
container = Data('wrapped_data', data)
msg = "Data.set_dataio() is deprecated. Please use Data.set_data_io() instead."
with self.assertWarnsWith(DeprecationWarning, msg):
container.set_dataio(dataio)
self.assertIs(dataio.data, data)
self.assertIs(dataio, container.data)
def test_set_dataio_data_already_set(self):
"""
Test that Data.set_dataio works as intended
"""
dataio = DataIO(data=np.arange(30).reshape(5, 2, 3))
data = np.arange(30).reshape(5, 2, 3)
container = Data('wrapped_data', data)
with self.assertRaisesWith(ValueError, "cannot overwrite 'data' on DataIO"):
with warnings.catch_warnings(record=True):
warnings.filterwarnings(
action='ignore',
category=DeprecationWarning,
message="Data.set_dataio() is deprecated. Please use Data.set_data_io() instead.",
)
container.set_dataio(dataio)
def test_dataio_options(self):
"""
Test that either data or dtype+shape are specified exclusively
"""
with self.assertWarnsRegex(UserWarning, "Argument 'dtype' is ignored when 'data' is specified"):
DataIO(data=np.arange(5), dtype=int)
with self.assertWarnsRegex(UserWarning, "Argument 'shape' is ignored when 'data' is specified"):
DataIO(data=np.arange(5), shape=(3,))
dataio = DataIO(shape=(3,), dtype=int)
with self.assertRaisesRegex(ValueError, "Setting data when dtype and shape are not None is not supported"):
dataio.data = np.arange(5)
|