| 1 |
from copy import copy, deepcopy |
| 2 |
|
| 3 |
import numpy as np |
| 4 |
from hdmf.container import Data |
| 5 |
from hdmf.data_utils import DataIO |
| 6 |
from hdmf.testing import TestCase |
| 7 |
import warnings |
| 8 |
|
| 9 |
|
| 10 |
class DataIOTests(TestCase): |
| 11 |
|
| 12 |
def test_copy(self): |
| 13 |
obj = DataIO(data=[1., 2., 3.]) |
| 14 |
obj_copy = copy(obj) |
| 15 |
self.assertNotEqual(id(obj), id(obj_copy)) |
| 16 |
self.assertEqual(id(obj.data), id(obj_copy.data)) |
| 17 |
|
| 18 |
def test_deepcopy(self): |
| 19 |
obj = DataIO(data=[1., 2., 3.]) |
| 20 |
obj_copy = deepcopy(obj) |
| 21 |
self.assertNotEqual(id(obj), id(obj_copy)) |
| 22 |
self.assertNotEqual(id(obj.data), id(obj_copy.data)) |
| 23 |
|
| 24 |
def test_dataio_slice_delegation(self): |
| 25 |
indata = np.arange(30) |
| 26 |
dset = DataIO(indata) |
| 27 |
self.assertTrue(np.all(dset[2:15] == indata[2:15])) |
| 28 |
|
| 29 |
indata = np.arange(50).reshape(5, 10) |
| 30 |
dset = DataIO(indata) |
| 31 |
self.assertTrue(np.all(dset[1:3, 5:8] == indata[1:3, 5:8])) |
| 32 |
|
| 33 |
def test_set_dataio(self): |
| 34 |
""" |
| 35 |
Test that Data.set_dataio works as intended |
| 36 |
""" |
| 37 |
dataio = DataIO() |
| 38 |
data = np.arange(30).reshape(5, 2, 3) |
| 39 |
container = Data('wrapped_data', data) |
| 40 |
msg = "Data.set_dataio() is deprecated. Please use Data.set_data_io() instead." |
| 41 |
with self.assertWarnsWith(DeprecationWarning, msg): |
| 42 |
container.set_dataio(dataio) |
| 43 |
self.assertIs(dataio.data, data) |
| 44 |
self.assertIs(dataio, container.data) |
| 45 |
|
| 46 |
def test_set_dataio_data_already_set(self): |
| 47 |
""" |
| 48 |
Test that Data.set_dataio works as intended |
| 49 |
""" |
| 50 |
dataio = DataIO(data=np.arange(30).reshape(5, 2, 3)) |
| 51 |
data = np.arange(30).reshape(5, 2, 3) |
| 52 |
container = Data('wrapped_data', data) |
| 53 |
with self.assertRaisesWith(ValueError, "cannot overwrite 'data' on DataIO"): |
| 54 |
with warnings.catch_warnings(record=True): |
| 55 |
warnings.filterwarnings( |
| 56 |
action='ignore', |
| 57 |
category=DeprecationWarning, |
| 58 |
message="Data.set_dataio() is deprecated. Please use Data.set_data_io() instead.", |
| 59 |
) |
| 60 |
container.set_dataio(dataio) |
| 61 |
|
| 62 |
def test_dataio_options(self): |
| 63 |
""" |
| 64 |
Test that either data or dtype+shape are specified exclusively |
| 65 |
""" |
| 66 |
with self.assertWarnsRegex(UserWarning, "Argument 'dtype' is ignored when 'data' is specified"): |
| 67 |
DataIO(data=np.arange(5), dtype=int) |
| 68 |
with self.assertWarnsRegex(UserWarning, "Argument 'shape' is ignored when 'data' is specified"): |
| 69 |
DataIO(data=np.arange(5), shape=(3,)) |
| 70 |
|
| 71 |
dataio = DataIO(shape=(3,), dtype=int) |
| 72 |
with self.assertRaisesRegex(ValueError, "Setting data when dtype and shape are not None is not supported"): |
| 73 |
dataio.data = np.arange(5) |