def test_catch_dup_name(self): ns_builder1 = NWBNamespaceBuilder('Extension for us in my Lab', "pynwb_test_extension1") ext1 = NWBGroupSpec('A custom ElectricalSeries for my lab', attributes=[ NWBAttributeSpec(name='trode_id', doc='the tetrode id', dtype='int') ], neurodata_type_inc='ElectricalSeries', neurodata_type_def='TetrodeSeries') ns_builder1.add_spec(self.ext_source1, ext1) ns_builder1.export(self.ns_path1, outdir=self.tempdir) ns_builder2 = NWBNamespaceBuilder('Extension for us in my Lab', "pynwb_test_extension1") ext2 = NWBGroupSpec('A custom ElectricalSeries for my lab', attributes=[ NWBAttributeSpec(name='trode_id', doc='the tetrode id', dtype='int') ], neurodata_type_inc='ElectricalSeries', neurodata_type_def='TetrodeSeries') ns_builder2.add_spec(self.ext_source2, ext2) ns_builder2.export(self.ns_path2, outdir=self.tempdir) load_namespaces(os.path.join(self.tempdir, self.ns_path1)) with self.assertRaises(KeyError): load_namespaces(os.path.join(self.tempdir, self.ns_path2))
def build_toy_example(): load_namespaces(ns_path) CompartmentSeries = get_class('CompartmentSeries', project_name) membrane_potential = CompartmentSeries(source='source', name='membrane_potential', gid=[1, 2, 3], index_pointer=[1, 2, 3], cell_var='Membrane potential (mV)', data=[[1., 2., 4.], [1., 2., 4.]], timestamps=np.arange(2), element_id=[0, 0, 0], element_pos=[1., 1., 1.], unit='µV') nwbfile = NWBFile(source='source', session_description='session_description', identifier='identifier', session_start_time=datetime.now(), file_create_date=datetime.now(), institution='institution', lab='lab') module = nwbfile.create_processing_module(name='membrane_potential', source='source', description='description') module.add_container(membrane_potential) with NWBHDF5IO('mem_potential_toy.nwb', mode='w') as io: io.write(nwbfile)
def test_lab_meta(self): ns_builder = NWBNamespaceBuilder('Extension for use in my Lab', self.prefix, version='0.1.0') test_meta_ext = NWBGroupSpec( neurodata_type_def='MyTestMetaData', neurodata_type_inc='LabMetaData', doc='my test meta data', attributes=[ NWBAttributeSpec(name='test_attr', dtype='float', doc='test_dtype')]) ns_builder.add_spec(self.ext_source, test_meta_ext) ns_builder.export(self.ns_path, outdir=self.tempdir) ns_abs_path = os.path.join(self.tempdir, self.ns_path) load_namespaces(ns_abs_path) @register_class('MyTestMetaData', self.prefix) class MyTestMetaData(LabMetaData): __nwbfields__ = ('test_attr',) @docval({'name': 'name', 'type': str, 'doc': 'name'}, {'name': 'test_attr', 'type': float, 'doc': 'test attribute'}) def __init__(self, **kwargs): test_attr = popargs('test_attr', kwargs) super(MyTestMetaData, self).__init__(**kwargs) self.test_attr = test_attr nwbfile = NWBFile("a file with header data", "NB123A", datetime(2017, 5, 1, 12, 0, 0, tzinfo=tzlocal())) nwbfile.add_lab_meta_data(MyTestMetaData(name='test_name', test_attr=5.))
def build_real_example(): load_namespaces(ns_path) VarTable = get_class('VarTable', project_name) input_data = h5py.File('sim_data/cell_vars.h5', 'r') vmtable = VarTable(source='source', name='vm_table', data=np.array(input_data['/v/data']), gid=np.array(input_data['mapping/gids']), index_pointer=np.array( input_data['mapping/index_pointer']), cell_var='Membrane potential (mV)', element_id=np.array(input_data['mapping/element_id']), element_pos=np.array(input_data['mapping/element_pos'])) nwbfile = NWBFile(source='source', session_description='session_description', identifier='identifier', session_start_time=datetime.now(), file_create_date=datetime.now(), institution='institution', lab='lab') module = nwbfile.create_processing_module(name='name', source='source', description='description') module.add_container(vmtable) io = NWBHDF5IO('mem_potential_toy.nwb', mode='w') io.write(nwbfile) io.close()
def test_lab_meta_auto(self): ns_builder = NWBNamespaceBuilder('Extension for use in my Lab', self.prefix, version='0.1.0') test_meta_ext = NWBGroupSpec(neurodata_type_def='MyTestMetaData', neurodata_type_inc='LabMetaData', doc='my test meta data', attributes=[ NWBAttributeSpec(name='test_attr', dtype='float', doc='test_dtype') ]) ns_builder.add_spec(self.ext_source, test_meta_ext) ns_builder.export(self.ns_path, outdir=self.tempdir) ns_abs_path = os.path.join(self.tempdir, self.ns_path) load_namespaces(ns_abs_path) MyTestMetaData = get_class('MyTestMetaData', self.prefix) nwbfile = NWBFile("a file with header data", "NB123A", datetime(2017, 5, 1, 12, 0, 0, tzinfo=tzlocal())) nwbfile.add_lab_meta_data( MyTestMetaData(name='test_name', test_attr=5.))
def build_settings(dict): """ WIP builds metadata based on passed in dict Does not support dict(s) deeper than 1 ATM""" # Settings: neurodata_type = 'MetaData' prefix = 'NHP' outdir = './' extension_doc = 'lab metadata extension' metadata_ext_group_spec = NWBGroupSpec(neurodata_type_def=neurodata_type, neurodata_type_inc='LabMetaData', doc=extension_doc, attributes=[ NWBAttributeSpec( name='experiment_id', dtype='int', doc='HW'), NWBAttributeSpec(name='test', dtype='text', doc='HW') ]) #Export spec: ext_source = '%s_extension.yaml' % prefix ns_path = '%s_namespace.yaml' % prefix ns_builder = NWBNamespaceBuilder(extension_doc, prefix, version=str(1)) ns_builder.add_spec(ext_source, metadata_ext_group_spec) ns_builder.export(ns_path, outdir=outdir) #Read spec and load namespace: ns_abs_path = os.path.join(outdir, ns_path) load_namespaces(ns_abs_path) class MetaData(LabMetaData): __nwbfields__ = ('experiment_id', 'test') @docval({ 'name': 'name', 'type': str, 'doc': 'name' }, { 'name': 'experiment_id', 'type': int, 'doc': 'HW' }, { 'name': 'test', 'type': str, 'doc': 'HW' }) def __init__(self, **kwargs): name, ophys_experiment_id, test = popargs('name', 'experiment_id', 'test', kwargs) super(OphysBehaviorMetaData, self).__init__(name=name) self.experiment_id = experiment_id self.test = test register_class('MetaData', prefix, MetaData) return MetaData
def load_pynwb_extension(schema, prefix: str): neurodata_type = schema.neurodata_type outdir = os.path.abspath(os.path.dirname(__file__)) ns_path = f'{prefix}.namespace.yaml' # Read spec and load namespace: ns_abs_path = os.path.join(outdir, ns_path) pynwb.load_namespaces(ns_abs_path) return pynwb.get_class(neurodata_type, prefix)
def load_pynwb_extension(schema, prefix: str): docval_list, attributes, nwbfields_list = extract_from_schema(schema) neurodata_type = schema.neurodata_type outdir = os.path.abspath(os.path.dirname(__file__)) ns_path = f'{prefix}.namespace.yaml' # Read spec and load namespace: ns_abs_path = os.path.join(outdir, ns_path) pynwb.load_namespaces(ns_abs_path) return pynwb.get_class(neurodata_type, prefix)
def test_load_namespace_with_reftype_attribute(self): ns_builder = NWBNamespaceBuilder('Extension for use in my Lab', self.prefix) test_ds_ext = NWBDatasetSpec( doc='test dataset to add an attr', name='test_data', shape=(None, ), attributes=[ NWBAttributeSpec(name='target_ds', doc='the target the dataset applies to', dtype=RefSpec('TimeSeries', 'object')) ], neurodata_type_def='my_new_type') ns_builder.add_spec(self.ext_source, test_ds_ext) ns_builder.export(self.ns_path, outdir=self.tempdir) load_namespaces(os.path.join(self.tempdir, self.ns_path))
def main(): ep = """ use --nspath to validate against an extension. If --ns is not specified, validate against all namespaces in namespace file. """ parser = ArgumentParser(description="Validate an NWB file", epilog=ep) parser.add_argument("path", type=str, help="the path to the NWB file") parser.add_argument('-p', '--nspath', type=str, help="the path to the namespace file") parser.add_argument("-n", "--ns", type=str, help="the namespace to validate against") args = parser.parse_args() if not os.path.exists(args.path): print('%s not found' % args.path, file=sys.stderr) sys.exit(1) io = HDF5IO(args.path, get_manager(), mode='r') if args.nspath is not None: namespaces = load_namespaces(args.nspath) if args.ns is not None: print('Validating against %s from %s.' % (args.ns, args.ns_path)) else: print('Validating using namespaces in %s.' % args.nspath) for ns in namespaces: print('Validating against %s' % ns) errors = validate(io, ns) _print_errors(errors) else: errors = validate(io) print('Validating against core namespace') _print_errors(errors)
def init_extensions(): for namespace_file in glob.glob(EXTENSION_PATH + '/**/*.namespace.*ml'): extension_path = os.path.dirname(namespace_file) extension_name = os.path.basename(extension_path) logging.info('Initializing extension ' + extension_name) load_namespaces(namespace_file) init_file = os.path.join(extension_path, 'init.py') if os.path.exists(init_file): try: with open(init_file) as f: for line in f: eval(line) except Exception as e: logging.error("Error evaluating extension init file " + init_file, exc_info=True)
def load_icephys_meta_namespace(): """ Internal helper function for loading the icephys_meta extension namespace for PyNWB Uses the load_namespaces function from PyNWB and as such modifies the state of PyNWB """ # use function level imports here to avoid pulling these functions into the module namespace import os from pynwb import load_namespaces # Set the path where the spec will be installed by default ndx_icephys_meta_specpath = os.path.join(os.path.dirname(__file__), 'spec', 'ndx-icephys-meta.namespace.yaml') # If the extensions has not been installed but we running directly from the git repo if not os.path.exists(ndx_icephys_meta_specpath): ndx_icephys_meta_specpath = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../spec/', 'ndx-icephys-meta.namespace.yaml')) # load namespace load_namespaces(ndx_icephys_meta_specpath)
def load_LabMetaData_extension(schema, prefix): docval_list, attributes, nwbfields_list = extract_from_schema(schema) neurodata_type = schema.neurodata_type outdir = os.path.abspath(os.path.dirname(__file__)) ns_path = '%s_namespace.yaml' % prefix # Read spec and load namespace: ns_abs_path = os.path.join(outdir, ns_path) load_namespaces(ns_abs_path) @register_class(neurodata_type, prefix) class ExtensionClass(LabMetaData): __nwbfields__ = tuple(nwbfields_list) @docval(*docval_list) def __init__(self, **kwargs): name = kwargs.pop('name') super(ExtensionClass, self).__init__(name=name) for attr, val in kwargs.items(): setattr(self, attr, val) return ExtensionClass
def test_load_namespace_with_reftype_attribute_check_autoclass_const(self): ns_builder = NWBNamespaceBuilder('Extension for use in my Lab', self.prefix) test_ds_ext = NWBDatasetSpec( doc='test dataset to add an attr', name='test_data', shape=(None, ), attributes=[ NWBAttributeSpec(name='target_ds', doc='the target the dataset applies to', dtype=RefSpec('TimeSeries', 'object')) ], neurodata_type_def='my_new_type') ns_builder.add_spec(self.ext_source, test_ds_ext) ns_builder.export(self.ns_path, outdir=self.tempdir) load_namespaces(os.path.join(self.tempdir, self.ns_path)) my_new_type = get_class('my_new_type', self.prefix) docval = None for tmp in get_docval(my_new_type.__init__): if tmp['name'] == 'target_ds': docval = tmp break self.assertIsNotNone(docval) self.assertEqual(docval['type'], TimeSeries)
def main(): ep = """ use --nspath to validate against an extension. If --ns is not specified, validate against all namespaces in namespace file. """ parser = ArgumentParser(description="Validate an NWB file", epilog=ep) parser.add_argument("paths", type=str, nargs='+', help="NWB file paths") parser.add_argument('-p', '--nspath', type=str, help="the path to the namespace file") parser.add_argument("-n", "--ns", type=str, help="the namespace to validate against") args = parser.parse_args() ret = 0 for path in args.paths: if not os.path.exists(path): print('%s not found' % path, file=sys.stderr) ret = 1 continue with NWBHDF5IO(path, get_manager(), mode='r') as io: if args.nspath is not None: namespaces = load_namespaces(args.nspath) if args.ns is not None: print('Validating %s against %s from %s.' % (path, args.ns, args.ns_path)) ret = ret or _validate_helper(io=io, namespace=args.ns) else: print('Validating %s using namespaces in %s.' % (path, args.nspath)) for ns in namespaces: print('Validating against %s' % ns) ret = ret or _validate_helper(io=io, namespace=ns) else: print('Validating %s against core namespace' % path) ret = ret or _validate_helper(io=io) sys.exit(ret)
import os from pynwb import load_namespaces, get_class # Set path of the namespace.yaml file to the expected install location ndx_pose_specpath = os.path.join(os.path.dirname(__file__), 'spec', 'ndx-pose.namespace.yaml') # If the extension has not been installed yet but we are running directly from # the git repo if not os.path.exists(ndx_pose_specpath): ndx_pose_specpath = os.path.abspath( os.path.join(os.path.dirname(__file__), '..', '..', '..', 'spec', 'ndx-pose.namespace.yaml')) # Load the namespace load_namespaces(ndx_pose_specpath) from . import io as __io from .pose import PoseEstimationSeries, PoseEstimation, PoseGroupingSeries, AnimalIdentitySeries
import pandas as pd import pynwb import SimpleITK as sitk import os import collections from allensdk.brain_observatory.running_speed import RunningSpeed from allensdk.brain_observatory.behavior.image_api import ImageApi from pynwb import load_namespaces namespace_path = os.path.join(os.path.dirname(__file__), 'AIBS_ophys_behavior_namespace.yaml') load_namespaces(namespace_path) class NwbApi: __slots__ = ('path', '_nwbfile') @property def nwbfile(self): if hasattr(self, '_nwbfile'): return self._nwbfile io = pynwb.NWBHDF5IO(self.path, 'r') return io.read() def __init__(self, path, **kwargs): ''' Reads data for a single Brain Observatory session from an NWB 2.0 file '''
# self.vertices = getargs('vertices', kwargs) #ns_builder.add_spec(ext_source, blender_surface) blender_plane_segmentation = NWBGroupSpec( 'A plane to store data from blender', neurodata_type_inc='PlaneSegmentation', neurodata_type_def='BlenderPlaneSegmentation', groups=[blender_surface]) ns_builder.add_spec(ext_source, blender_plane_segmentation) #Writes YAML files ns_builder.export(ns_path) load_namespaces('blenderbits.namespace.yaml') BlenderSurface = get_class('BlenderSurface', 'TanLab') BlenderPlaneSegmentation = get_class('BlenderPlaneSegmentation', 'TanLab') #Read in OBJ os.chdir( 'C:/Users/Mrika/OneDrive/TanLab/NWBHackathonFiles/HackthonFiles/ObjectModels' ) soma_triangles = o3d.io.read_triangle_mesh("soma.obj") soma_triangles = np.asarray(soma_triangles.triangles) #Testing stuff soma_surface = BlenderSurface(vertices=[[0.0, 1.0, 1.0], [1.0, 1.0, 2.0], [2.0, 2.0, 1.0], [2.0, 1.0, 1.0], [1.0, 2.0, 1.0]], faces=soma_triangles,
# .. _using_extension: # # Using extensions # ----------------------------------------------------- # # After an extension has been created, it can be used by downstream codes for reading and writing data. # There are two main mechanisms for reading and writing extension data with PyNWB. # The first involves defining new :py:class:`~pynwb.core.NWBContainer` classes that are then mapped # to the neurodata types in the extension. from pynwb import register_class, load_namespaces from pynwb.ecephys import ElectricalSeries from pynwb.form.utils import docval, call_docval_func, getargs, get_docval ns_path = "mylab.namespace.yaml" load_namespaces(ns_path) @register_class('TetrodeSeries', 'mylab') class TetrodeSeries(ElectricalSeries): __nwbfields__ = ('trode_id', ) @docval(*get_docval(ElectricalSeries.__init__) + ({ 'name': 'trode_id', 'type': int, 'doc': 'the tetrode id' }, )) def __init__(self, **kwargs): call_docval_func(super(TetrodeSeries, self).__init__, kwargs) self.trode_id = getargs('trode_id', kwargs)
# .. _using_extension: # # Using extensions # ----------------------------------------------------- # # After an extension has been created, it can be used by downstream codes for reading and writing data. # There are two main mechanisms for reading and writing extension data with PyNWB. # The first involves defining new :py:class:`~pynwb.core.NWBContainer` classes that are then mapped # to the neurodata types in the extension. from pynwb import register_class, load_namespaces from pynwb.ecephys import ElectricalSeries from hdmf.utils import docval, call_docval_func, getargs, get_docval ns_path = "mylab.namespace.yaml" load_namespaces(ns_path) @register_class('TetrodeSeries', 'mylab') class TetrodeSeries(ElectricalSeries): __nwbfields__ = ('trode_id', ) @docval(*get_docval(ElectricalSeries.__init__) + ({ 'name': 'trode_id', 'type': int, 'doc': 'the tetrode id' }, )) def __init__(self, **kwargs): call_docval_func(super(TetrodeSeries, self).__init__, kwargs) self.trode_id = getargs('trode_id', kwargs)
import os from pynwb import load_namespaces from ..auto_class import get_class, get_multi_container import numpy as np filepath = os.path.realpath(__file__) basedir = os.path.split(filepath)[0] name = 'ecog' load_namespaces(os.path.join(basedir, name + '.namespace.yaml')) def surface_init_add(faces, vertices, **kwargs): if np.max(faces) >= len(vertices): raise ValueError( 'index of faces exceeds number vertices for {}. ' 'Faces should be 0-indexed, not 1-indexed'.format(name)) if np.min(faces < 0): raise ValueError( 'faces hold indices of vertices and should be non-negative') Surface = get_class(name, 'Surface', surface_init_add) CorticalSurfaces = get_multi_container(name, 'CorticalSurfaces', Surface)
# If the extension has not been installed yet but we are running directly from # the git repo if not os.path.exists(ndx_photometry_specpath): ndx_photometry_specpath = os.path.abspath( os.path.join( os.path.dirname(__file__), "..", "..", "..", "spec", "ndx-photometry.namespace.yaml", )) # Load the namespace load_namespaces(ndx_photometry_specpath) # TODO: import your classes here or define your class using get_class to make from .fibers_table import FibersTable # them accessible at the package level (CommandedVoltageSeries, ExcitationSourcesTable, PhotodetectorsTable, DeconvolvedRoiResponseSeries, MultiCommandedVoltage, FiberPhotometry, FluorophoresTable) = [ get_class(x, "ndx-photometry") for x in ("CommandedVoltageSeries", "ExcitationSourcesTable", "PhotodetectorsTable", "DeconvolvedRoiResponseSeries", "MultiCommandedVoltage", 'FiberPhotometry', "FluorophoresTable") ]
import os from pynwb import load_namespaces, get_class # Set path of the namespace.yaml file to the expected install location ndx_hierarchical_behavioral_data_specpath = os.path.join( os.path.dirname(__file__), 'spec', 'ndx-hierarchical-behavioral-data.namespace.yaml' ) # If the extension has not been installed yet but we are running directly from # the git repo if not os.path.exists(ndx_hierarchical_behavioral_data_specpath): ndx_hierarchical_behavioral_data_specpath = os.path.abspath(os.path.join( os.path.dirname(__file__), '..', '..', '..', 'spec', 'ndx-hierarchical-behavioral-data.namespace.yaml' )) # Load the namespace load_namespaces(ndx_hierarchical_behavioral_data_specpath) from .hierarchical_behavioral_data import *
import os from pynwb import load_namespaces, get_class import ndx_events # Set path of the namespace.yaml file to the expected install location ndx_ellipse_eye_tracking_specpath = os.path.join( os.path.dirname(__file__), 'spec', 'ndx-ellipse-eye-tracking.namespace.yaml' ) # If the extension has not been installed yet but we are running directly from # the git repo if not os.path.exists(ndx_ellipse_eye_tracking_specpath): ndx_ellipse_eye_tracking_specpath = os.path.abspath(os.path.join( os.path.dirname(__file__), '..', '..', '..', 'spec', 'ndx-ellipse-eye-tracking.namespace.yaml' )) # Load the namespace load_namespaces(ndx_events.ndx_events_specpath) load_namespaces(ndx_ellipse_eye_tracking_specpath) # TODO: import your classes here or define your class using get_class to make # them accessible at the package level EllipseSeries = get_class('EllipseSeries', 'ndx-ellipse-eye-tracking') EllipseEyeTracking = get_class('EllipseEyeTracking', 'ndx-ellipse-eye-tracking')
import os from pynwb import load_namespaces from ..auto_class import get_class filepath = os.path.realpath(__file__) basedir = os.path.split(filepath)[0] load_namespaces(os.path.join(basedir, 'time_frequency.namespace.yaml')) HilbertSeries = get_class('time_frequency', 'HilbertSeries')
def test_load_namespace(self): self.test_export() load_namespaces(os.path.join(self.tempdir, self.ns_path))
cat_cell_info = NWBGroupSpec(neurodata_type_def='CatCellInfo', doc='Categorical Cell Info', datasets=[gid_spec, cat_data_spec], neurodata_type_inc='NWBDataInterface') # export ns_builder = NWBNamespaceBuilder(name + ' extensions', name) for spec in [cat_cell_info]: ns_builder.add_spec(ext_source, spec) ns_builder.export(ns_path) ''' EXAMPLE USAGE: from pynwb import get_class, load_namespaces, NWBHDF5IO ns_path = name + '.namespace.yaml' load_namespaces(ns_path) CatCellInfo = get_class('CatCellInfo', name) import numpy as np from pynwb import NWBFile from datetime import datetime
from pathlib import Path import pynwb file_dir = Path(__file__).parent namespace_path = (file_dir / "ndx-aibs-ecephys.namespace.yaml").resolve() pynwb.load_namespaces(str(namespace_path)) EcephysProbe = pynwb.get_class('EcephysProbe', 'ndx-aibs-ecephys') EcephysElectrodeGroup = pynwb.get_class('EcephysElectrodeGroup', 'ndx-aibs-ecephys') EcephysSpecimen = pynwb.get_class('EcephysSpecimen', 'ndx-aibs-ecephys') EcephysEyeTrackingRigMetadata = pynwb.get_class( 'EcephysEyeTrackingRigMetadata', 'ndx-aibs-ecephys') EcephysCSD = pynwb.get_class('EcephysCSD', 'ndx-aibs-ecephys')
# -*- coding: utf-8 -*- """ Initialize the StimSeries and StimTable classes. """ # Standard libraries import os # Third party libraries from pynwb import load_namespaces # Set path of the namespace.yaml file to the expected install location ndx_electrical_stim_specpath = os.path.join( os.path.dirname(__file__), 'spec', 'ndx-electrical-stim.namespace.yaml') # If the extension has not been installed yet but we are running directly from # the git repo if not os.path.exists(ndx_electrical_stim_specpath): ndx_electrical_stim_specpath = os.path.abspath( os.path.join(os.path.dirname(__file__), '..', '..', '..', 'spec', 'ndx-electrical-stim.namespace.yaml')) # Load the namespace load_namespaces(ndx_electrical_stim_specpath) from .ndx_electrical_stim import StimSeries, StimTable
import os from pynwb import load_namespaces # Set path of the namespace.yaml file to the expected install location ndx_franklab_novela_specpath = os.path.join( os.path.dirname(__file__), 'spec', 'ndx-franklab-novela.namespace.yaml') # If the extension has not been installed yet but we are running directly from # the git repo if not os.path.exists(ndx_franklab_novela_specpath): ndx_franklab_novela_specpath = os.path.abspath( os.path.join(os.path.dirname(__file__), '..', '..', '..', 'spec', 'ndx-franklab-novela.namespace.yaml')) # Load the namespace load_namespaces(ndx_franklab_novela_specpath)