2023-08-18 08:11:14 +00:00
|
|
|
"""
|
|
|
|
Loading/saving NWB Schema yaml files
|
|
|
|
"""
|
|
|
|
from pathlib import Path
|
2023-08-19 05:09:28 +00:00
|
|
|
from typing import TypedDict, List, Dict
|
2023-08-18 08:11:14 +00:00
|
|
|
from pprint import pprint
|
2023-08-19 05:09:28 +00:00
|
|
|
import warnings
|
2023-08-18 08:11:14 +00:00
|
|
|
|
|
|
|
from linkml_runtime.loaders import yaml_loader
|
|
|
|
import yaml
|
|
|
|
|
2023-08-19 05:09:28 +00:00
|
|
|
from nwb_schema_language import Namespaces, Namespace, Group, Dataset
|
|
|
|
from nwb_linkml.namespaces import GitRepo, NamespaceRepo, NWB_CORE_REPO, HDMF_COMMON_REPO
|
|
|
|
from nwb_linkml.maps import preload
|
|
|
|
from nwb_linkml.map import PHASES, Map
|
|
|
|
|
|
|
|
class NamespaceBundle(TypedDict):
|
|
|
|
"""
|
|
|
|
A complete namespaces file and all indicated schema files
|
|
|
|
"""
|
|
|
|
namespace: Namespaces
|
|
|
|
schema: Dict[str, List[Dataset | Group]]
|
|
|
|
|
|
|
|
def load_yaml(path:Path) -> dict:
|
|
|
|
with open(path, 'r') as file:
|
|
|
|
ns_dict = yaml.safe_load(file)
|
|
|
|
|
|
|
|
# apply maps
|
|
|
|
maps = [m for m in Map.instances if m.phase == PHASES.postload]
|
|
|
|
print('got maps')
|
|
|
|
print(maps)
|
|
|
|
for amap in maps:
|
|
|
|
ns_dict = amap.apply(ns_dict)
|
|
|
|
return ns_dict
|
2023-08-18 08:11:14 +00:00
|
|
|
|
|
|
|
def load_namespaces(path:Path|NamespaceRepo) -> Namespaces:
|
|
|
|
if isinstance(path, NamespaceRepo):
|
|
|
|
path = path.provide_from_git()
|
|
|
|
|
2023-08-19 05:09:28 +00:00
|
|
|
ns_dict = load_yaml(path)
|
|
|
|
|
|
|
|
|
|
|
|
namespaces = yaml_loader.load(ns_dict, target_class=Namespaces)
|
2023-08-18 08:11:14 +00:00
|
|
|
return namespaces
|
|
|
|
|
2023-08-19 05:09:28 +00:00
|
|
|
|
2023-08-18 08:11:14 +00:00
|
|
|
|
|
|
|
def load_schema_file(path:Path) -> List[Dataset | Group]:
|
2023-08-19 05:09:28 +00:00
|
|
|
source = load_yaml(path)
|
2023-08-18 08:11:14 +00:00
|
|
|
|
|
|
|
schema = []
|
|
|
|
|
|
|
|
for dataset in source.get('datasets', []):
|
|
|
|
try:
|
|
|
|
schema.append(Dataset(**dataset))
|
|
|
|
except Exception as e:
|
|
|
|
pprint(dataset)
|
|
|
|
raise e
|
|
|
|
|
2023-08-19 05:09:28 +00:00
|
|
|
for group in source.get('groups', []):
|
|
|
|
try:
|
|
|
|
schema.append(Group(**group))
|
|
|
|
except Exception as e:
|
|
|
|
pprint(group)
|
|
|
|
raise e
|
|
|
|
|
2023-08-18 08:11:14 +00:00
|
|
|
#schema.extend([Dataset(**dataset) for dataset in source.get('datasets', [])])
|
|
|
|
#schema.extend([Group(**group) for group in source.get('groups', [])])
|
|
|
|
return schema
|
|
|
|
|
2023-08-19 05:09:28 +00:00
|
|
|
def load_namespace_schema(namespace: Namespace | Namespaces, path:Path=Path('.')) -> Dict[str, List[Dataset | Group]]:
|
|
|
|
"""
|
|
|
|
Load all schema referenced by a namespace file
|
|
|
|
|
|
|
|
Args:
|
|
|
|
namespace (:class:`.Namespace`):
|
|
|
|
path (:class:`pathlib.Path`): Location of the namespace file - all relative paths are interpreted relative to this
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
List[Union[Dataset|Group]]
|
|
|
|
"""
|
|
|
|
if isinstance(namespace, Namespace):
|
|
|
|
ns_iter = [namespace]
|
|
|
|
elif isinstance(namespace, Namespaces):
|
|
|
|
ns_iter = namespace.namespaces
|
|
|
|
else:
|
|
|
|
raise TypeError("Need to pass a namespace or namespaces :)")
|
|
|
|
|
|
|
|
path = Path(path).resolve()
|
|
|
|
if path.is_file():
|
|
|
|
# given the namespace file itself, so find paths relative to its directory
|
|
|
|
path = path.parent
|
|
|
|
|
|
|
|
sch = {}
|
|
|
|
for ns in ns_iter:
|
|
|
|
for schema in ns.schema_:
|
|
|
|
if schema.source is None:
|
|
|
|
warnings.warn(f"No source specified for {schema}")
|
|
|
|
continue
|
|
|
|
yml_file = (path / schema.source).resolve()
|
|
|
|
sch[schema.source] = load_schema_file(yml_file)
|
|
|
|
|
|
|
|
return sch
|
|
|
|
|
|
|
|
def load_nwb_core() -> Dict[str, NamespaceBundle]:
|
|
|
|
# First get hdmf-common:
|
|
|
|
hdmf_ns_file = HDMF_COMMON_REPO.provide_from_git()
|
|
|
|
hdmf_ns = load_namespaces(hdmf_ns_file)
|
|
|
|
hdmf_schema = load_namespace_schema(hdmf_ns, hdmf_ns_file)
|
|
|
|
|
2023-08-18 23:33:06 +00:00
|
|
|
namespace_file = NWB_CORE_REPO.provide_from_git()
|
|
|
|
ns = load_namespaces(namespace_file)
|
2023-08-19 05:09:28 +00:00
|
|
|
schema = load_namespace_schema(ns, namespace_file)
|
|
|
|
|
|
|
|
return {
|
|
|
|
'hdmf-common': NamespaceBundle(
|
|
|
|
namespace=hdmf_ns,
|
|
|
|
schema=hdmf_schema
|
|
|
|
),
|
|
|
|
'nwb-core': NamespaceBundle(
|
|
|
|
namespace=ns,
|
|
|
|
schema=schema
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-18 23:33:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-18 08:11:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|