| 1 |
from copy import copy, deepcopy |
| 2 |
import os |
| 3 |
import urllib.request |
| 4 |
import h5py |
| 5 |
import warnings |
| 6 |
|
| 7 |
from hdmf.backends.hdf5.h5tools import HDF5IO |
| 8 |
from hdmf.build import TypeMap, BuildManager |
| 9 |
from hdmf.common import get_hdf5io, get_type_map |
| 10 |
from hdmf.spec import GroupSpec, DatasetSpec, SpecNamespace, NamespaceBuilder, NamespaceCatalog |
| 11 |
from hdmf.testing import TestCase |
| 12 |
from hdmf.utils import docval, get_docval |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
class TestRos3(TestCase): |
| 17 |
"""Test reading an HDMF file using HDF5 ROS3 streaming. |
| 18 |
|
| 19 |
TODO: test streaming via fsspec/h5py |
| 20 |
""" |
| 21 |
|
| 22 |
def setUp(self): |
| 23 |
# Skip ROS3 tests if internet is not available or the ROS3 driver is not installed |
| 24 |
try: |
| 25 |
# this is a 174 KB file |
| 26 |
urllib.request.urlopen("https://dandiarchive.s3.amazonaws.com/ros3test.nwb", timeout=1) |
| 27 |
except urllib.request.URLError: |
| 28 |
self.skipTest("Internet access to DANDI failed. Skipping all Ros3 streaming tests.") |
| 29 |
if "ros3" not in h5py.registered_drivers(): |
| 30 |
self.skipTest("ROS3 driver not installed. Skipping all Ros3 streaming tests.") |
| 31 |
|
| 32 |
# set up build manager with a simplified version of the NWB schema so that we can test |
| 33 |
# ROS3 streaming from S3 |
| 34 |
namespace_name = "core" |
| 35 |
self.ns_filename = namespace_name + ".namespace.yaml" |
| 36 |
self.ext_filename = namespace_name + ".extensions.yaml" |
| 37 |
self.output_dir = "." |
| 38 |
nwb_container_spec = NWBGroupSpec( |
| 39 |
neurodata_type_def="NWBContainer", |
| 40 |
neurodata_type_inc="Container", |
| 41 |
doc=("An abstract data type for a generic container storing collections of data and metadata. " |
| 42 |
"Base type for all data and metadata containers."), |
| 43 |
) |
| 44 |
subject_spec = NWBGroupSpec( |
| 45 |
neurodata_type_def="Subject", |
| 46 |
neurodata_type_inc="NWBContainer", |
| 47 |
doc="Information about the animal or person from which the data was measured.", |
| 48 |
) |
| 49 |
nwbfile_spec = NWBGroupSpec( |
| 50 |
neurodata_type_def="NWBFile", |
| 51 |
neurodata_type_inc="NWBContainer", |
| 52 |
doc="An NWB file storing cellular-based neurophysiology data from a single experimental session.", |
| 53 |
groups=[ |
| 54 |
NWBGroupSpec( |
| 55 |
name="subject", |
| 56 |
neurodata_type_inc="Subject", |
| 57 |
doc="Information about the animal or person from which the data was measured.", |
| 58 |
quantity="?", |
| 59 |
), |
| 60 |
], |
| 61 |
) |
| 62 |
|
| 63 |
ns_builder = NamespaceBuilder( |
| 64 |
name=namespace_name, |
| 65 |
doc="a test namespace", |
| 66 |
version="0.1.0", |
| 67 |
) |
| 68 |
ns_builder.include_namespace("hdmf-common") |
| 69 |
ns_builder.add_spec(self.ext_filename, nwb_container_spec) |
| 70 |
ns_builder.add_spec(self.ext_filename, subject_spec) |
| 71 |
ns_builder.add_spec(self.ext_filename, nwbfile_spec) |
| 72 |
|
| 73 |
ns_builder.export(self.ns_filename, outdir=self.output_dir) |
| 74 |
ns_path = os.path.join(self.output_dir, self.ns_filename) |
| 75 |
|
| 76 |
ns_catalog = NamespaceCatalog(NWBGroupSpec, NWBDatasetSpec, NWBNamespace) |
| 77 |
type_map = TypeMap(ns_catalog) |
| 78 |
type_map.merge(get_type_map(), ns_catalog=True) |
| 79 |
type_map.load_namespaces(ns_path) |
| 80 |
|
| 81 |
self.manager = BuildManager(type_map) |
| 82 |
|
| 83 |
warnings.filterwarnings(action="ignore", message="Ignoring cached namespace .*") |
| 84 |
|
| 85 |
def tearDown(self): |
| 86 |
if os.path.exists(self.ns_filename): |
| 87 |
os.remove(self.ns_filename) |
| 88 |
if os.path.exists(self.ext_filename): |
| 89 |
os.remove(self.ext_filename) |
| 90 |
|
| 91 |
def test_basic_read(self): |
| 92 |
s3_path = "https://dandiarchive.s3.amazonaws.com/blobs/11e/c89/11ec8933-1456-4942-922b-94e5878bb991" |
| 93 |
|
| 94 |
with get_hdf5io(s3_path, "r", manager=self.manager, driver="ros3") as io: |
| 95 |
io.read() |
| 96 |
|
| 97 |
def test_basic_read_with_aws_region(self): |
| 98 |
s3_path = "https://dandiarchive.s3.amazonaws.com/blobs/11e/c89/11ec8933-1456-4942-922b-94e5878bb991" |
| 99 |
|
| 100 |
with get_hdf5io(s3_path, "r", manager=self.manager, driver="ros3", aws_region="us-east-2") as io: |
| 101 |
io.read() |
| 102 |
|
| 103 |
def test_basic_read_s3_with_aws_region(self): |
| 104 |
# NOTE: if an s3 path is used with ros3 driver, aws_region must be specified |
| 105 |
s3_path = "s3://dandiarchive/blobs/11e/c89/11ec8933-1456-4942-922b-94e5878bb991" |
| 106 |
|
| 107 |
with get_hdf5io(s3_path, "r", manager=self.manager, driver="ros3", aws_region="us-east-2") as io: |
| 108 |
io.read() |
| 109 |
assert io.aws_region == "us-east-2" |
| 110 |
|
| 111 |
def test_get_namespaces(self): |
| 112 |
s3_path = "https://dandiarchive.s3.amazonaws.com/blobs/11e/c89/11ec8933-1456-4942-922b-94e5878bb991" |
| 113 |
|
| 114 |
namespaces = HDF5IO.get_namespaces(s3_path, driver="ros3") |
| 115 |
self.assertEqual(namespaces, {'core': '2.3.0', 'hdmf-common': '1.5.0', 'hdmf-experimental': '0.1.0'}) |
| 116 |
|
| 117 |
def test_get_namespaces_with_aws_region(self): |
| 118 |
s3_path = "https://dandiarchive.s3.amazonaws.com/blobs/11e/c89/11ec8933-1456-4942-922b-94e5878bb991" |
| 119 |
|
| 120 |
namespaces = HDF5IO.get_namespaces(s3_path, driver="ros3", aws_region="us-east-2") |
| 121 |
self.assertEqual(namespaces, {'core': '2.3.0', 'hdmf-common': '1.5.0', 'hdmf-experimental': '0.1.0'}) |
| 122 |
|
| 123 |
def test_get_namespaces_s3_with_aws_region(self): |
| 124 |
s3_path = "s3://dandiarchive/blobs/11e/c89/11ec8933-1456-4942-922b-94e5878bb991" |
| 125 |
|
| 126 |
namespaces = HDF5IO.get_namespaces(s3_path, driver="ros3", aws_region="us-east-2") |
| 127 |
self.assertEqual(namespaces, {'core': '2.3.0', 'hdmf-common': '1.5.0', 'hdmf-experimental': '0.1.0'}) |
| 128 |
|
| 129 |
def test_load_namespaces(self): |
| 130 |
s3_path = "https://dandiarchive.s3.amazonaws.com/blobs/11e/c89/11ec8933-1456-4942-922b-94e5878bb991" |
| 131 |
|
| 132 |
HDF5IO.load_namespaces(self.manager.namespace_catalog, path=s3_path, driver="ros3") |
| 133 |
assert set(self.manager.namespace_catalog.namespaces) == set(["core", "hdmf-common", "hdmf-experimental"]) |
| 134 |
|
| 135 |
def test_load_namespaces_with_aws_region(self): |
| 136 |
s3_path = "https://dandiarchive.s3.amazonaws.com/blobs/11e/c89/11ec8933-1456-4942-922b-94e5878bb991" |
| 137 |
|
| 138 |
HDF5IO.load_namespaces(self.manager.namespace_catalog, path=s3_path, driver="ros3", aws_region="us-east-2") |
| 139 |
assert set(self.manager.namespace_catalog.namespaces) == set(["core", "hdmf-common", "hdmf-experimental"]) |
| 140 |
|
| 141 |
def test_load_namespaces_s3_with_aws_region(self): |
| 142 |
s3_path = "s3://dandiarchive/blobs/11e/c89/11ec8933-1456-4942-922b-94e5878bb991" |
| 143 |
|
| 144 |
HDF5IO.load_namespaces(self.manager.namespace_catalog, path=s3_path, driver="ros3", aws_region="us-east-2") |
| 145 |
assert set(self.manager.namespace_catalog.namespaces) == set(["core", "hdmf-common", "hdmf-experimental"]) |
| 146 |
|
| 147 |
|
| 148 |
# Util functions and classes to enable loading of the NWB namespace -- see pynwb/src/pynwb/spec.py |
| 149 |
|
| 150 |
|
| 151 |
def __swap_inc_def(cls): |
| 152 |
args = get_docval(cls.__init__) |
| 153 |
clsname = "NWB%s" % cls.__name__ |
| 154 |
ret = list() |
| 155 |
# do not set default neurodata_type_inc for base hdmf-common types that should not have data_type_inc |
| 156 |
for arg in args: |
| 157 |
if arg["name"] == "data_type_def": |
| 158 |
ret.append({"name": "neurodata_type_def", "type": str, |
| 159 |
"doc": "the NWB data type this spec defines", "default": None}) |
| 160 |
elif arg["name"] == "data_type_inc": |
| 161 |
ret.append({"name": "neurodata_type_inc", "type": (clsname, str), |
| 162 |
"doc": "the NWB data type this spec includes", "default": None}) |
| 163 |
else: |
| 164 |
ret.append(copy(arg)) |
| 165 |
return ret |
| 166 |
|
| 167 |
|
| 168 |
class BaseStorageOverride: |
| 169 |
""" This class is used for the purpose of overriding |
| 170 |
BaseStorageSpec classmethods, without creating diamond |
| 171 |
inheritance hierarchies. |
| 172 |
""" |
| 173 |
|
| 174 |
__type_key = "neurodata_type" |
| 175 |
__inc_key = "neurodata_type_inc" |
| 176 |
__def_key = "neurodata_type_def" |
| 177 |
|
| 178 |
@classmethod |
| 179 |
def type_key(cls): |
| 180 |
""" Get the key used to store data type on an instance""" |
| 181 |
return cls.__type_key |
| 182 |
|
| 183 |
@classmethod |
| 184 |
def inc_key(cls): |
| 185 |
""" Get the key used to define a data_type include.""" |
| 186 |
return cls.__inc_key |
| 187 |
|
| 188 |
@classmethod |
| 189 |
def def_key(cls): |
| 190 |
""" Get the key used to define a data_type definition.""" |
| 191 |
return cls.__def_key |
| 192 |
|
| 193 |
@property |
| 194 |
def neurodata_type_inc(self): |
| 195 |
return self.data_type_inc |
| 196 |
|
| 197 |
@property |
| 198 |
def neurodata_type_def(self): |
| 199 |
return self.data_type_def |
| 200 |
|
| 201 |
@classmethod |
| 202 |
def build_const_args(cls, spec_dict): |
| 203 |
"""Extend base functionality to remap data_type_def and data_type_inc keys""" |
| 204 |
spec_dict = copy(spec_dict) |
| 205 |
proxy = super() |
| 206 |
if proxy.inc_key() in spec_dict: |
| 207 |
spec_dict[cls.inc_key()] = spec_dict.pop(proxy.inc_key()) |
| 208 |
if proxy.def_key() in spec_dict: |
| 209 |
spec_dict[cls.def_key()] = spec_dict.pop(proxy.def_key()) |
| 210 |
ret = proxy.build_const_args(spec_dict) |
| 211 |
return ret |
| 212 |
|
| 213 |
@classmethod |
| 214 |
def _translate_kwargs(cls, kwargs): |
| 215 |
"""Swap neurodata_type_def and neurodata_type_inc for data_type_def and data_type_inc, respectively""" |
| 216 |
proxy = super() |
| 217 |
kwargs[proxy.def_key()] = kwargs.pop(cls.def_key()) |
| 218 |
kwargs[proxy.inc_key()] = kwargs.pop(cls.inc_key()) |
| 219 |
return kwargs |
| 220 |
|
| 221 |
|
| 222 |
_dataset_docval = __swap_inc_def(DatasetSpec) |
| 223 |
|
| 224 |
|
| 225 |
class NWBDatasetSpec(BaseStorageOverride, DatasetSpec): |
| 226 |
""" The Spec class to use for NWB dataset specifications. |
| 227 |
|
| 228 |
Classes will automatically include NWBData if None is specified. |
| 229 |
""" |
| 230 |
|
| 231 |
@docval(*deepcopy(_dataset_docval)) |
| 232 |
def __init__(self, **kwargs): |
| 233 |
kwargs = self._translate_kwargs(kwargs) |
| 234 |
# set data_type_inc to NWBData only if it is not specified and the type is not an HDMF base type |
| 235 |
if kwargs["data_type_inc"] is None and kwargs["data_type_def"] not in (None, "Data"): |
| 236 |
kwargs["data_type_inc"] = "NWBData" |
| 237 |
super().__init__(**kwargs) |
| 238 |
|
| 239 |
|
| 240 |
_group_docval = __swap_inc_def(GroupSpec) |
| 241 |
|
| 242 |
|
| 243 |
class NWBGroupSpec(BaseStorageOverride, GroupSpec): |
| 244 |
""" The Spec class to use for NWB group specifications. |
| 245 |
|
| 246 |
Classes will automatically include NWBContainer if None is specified. |
| 247 |
""" |
| 248 |
|
| 249 |
@docval(*deepcopy(_group_docval)) |
| 250 |
def __init__(self, **kwargs): |
| 251 |
kwargs = self._translate_kwargs(kwargs) |
| 252 |
# set data_type_inc to NWBData only if it is not specified and the type is not an HDMF base type |
| 253 |
# NOTE: CSRMatrix in hdmf-common-schema does not have a data_type_inc but should not inherit from |
| 254 |
# NWBContainer. This will be fixed in hdmf-common-schema 1.2.1. |
| 255 |
if kwargs["data_type_inc"] is None and kwargs["data_type_def"] not in (None, "Container", "CSRMatrix"): |
| 256 |
kwargs["data_type_inc"] = "NWBContainer" |
| 257 |
super().__init__(**kwargs) |
| 258 |
|
| 259 |
@classmethod |
| 260 |
def dataset_spec_cls(cls): |
| 261 |
return NWBDatasetSpec |
| 262 |
|
| 263 |
@docval({"name": "neurodata_type", "type": str, "doc": "the neurodata_type to retrieve"}) |
| 264 |
def get_neurodata_type(self, **kwargs): |
| 265 |
""" Get a specification by "neurodata_type" """ |
| 266 |
return super().get_data_type(kwargs["neurodata_type"]) |
| 267 |
|
| 268 |
|
| 269 |
class NWBNamespace(SpecNamespace): |
| 270 |
""" |
| 271 |
A Namespace class for NWB |
| 272 |
""" |
| 273 |
|
| 274 |
__types_key = "neurodata_types" |
| 275 |
|
| 276 |
@classmethod |
| 277 |
def types_key(cls): |
| 278 |
return cls.__types_key |