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_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_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 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_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)
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 numpy as np from pynwb import get_class, load_namespaces from datetime import datetime from pynwb import NWBFile from form.backends.hdf5 import HDF5IO from pynwb import get_build_manager ns_path = "alleninstitute.namespace.yaml" load_namespaces(ns_path) PopulationSpikeTrain = get_class('PopulationSpikeTrain', 'alleninstitute') fs = PopulationSpikeTrain(data=(10*np.random.random(5)).astype(np.int), name='my_population', source='acquisition', unit='second', timestamps=np.random.rand(5)) f = NWBFile(source='noone', session_description='my first synthetic recording', file_name='tmp.nwb', identifier='hi', experimenter='Dr. Bilbo Baggins', lab='Bag End Labatory', institution='University of Middle Earth at the Shire', experiment_description='empty', session_id='LONELYMTN', session_start_time=datetime.now()) f.add_raw_timeseries(fs)
def test_get_class(self): self.test_load_namespace() TetrodeSeries = get_class('TetrodeSeries', 'pynwb_test_extension') # noqa: F841
from pynwb import get_class from .maze_extension import MazeExtension LabMetaDataExtension = get_class('LabMetaDataExtension', 'ndx-tank-metadata') RigExtension = get_class('RigExtension', 'ndx-tank-metadata')
import os from pynwb import load_namespaces, get_class # Set path of the namespace.yaml file to the expected install location ndx_bipolar_scheme_specpath = os.path.join( os.path.dirname(__file__), 'spec', 'ndx-bipolar-scheme.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_bipolar_scheme_specpath): ndx_bipolar_scheme_specpath = os.path.abspath( os.path.join(os.path.dirname(__file__), '..', '..', '..', 'spec', 'ndx-bipolar-scheme.namespace.yaml')) # Load the namespace load_namespaces(ndx_bipolar_scheme_specpath) EcephysExt = get_class('EcephysExt', 'ndx-bipolar-scheme')
# and :py:func:`~pynwb.form.utils.get_docval` # # When extending :py:class:`~pynwb.core.NWBContainer` or :py:class:`~pynwb.core.NWBContainer` # subclasses, you should defining the class field ``__nwbfields__``. This will # tell PyNWB the properties of the :py:class:`~pynwb.core.NWBContainer` extension. # # If you do not want to write additional code to read your extensions, PyNWB is able to dynamically # create an :py:class:`~pynwb.core.NWBContainer` subclass for use within the PyNWB API. # Dynamically created classes can be inspected using the built-in :py:func:`.help` or the :py:mod:`inspect` module. from pynwb import get_class, load_namespaces ns_path = "mylab.namespace.yaml" load_namespaces(ns_path) AutoTetrodeSeries = get_class('TetrodeSeries', 'mylab') #################### # .. note:: # # When defining your own :py:class:`~pynwb.core.NWBContainer`, the subclass name does not need to be the same # as the extension type name. However, it is encouraged to keep class and extension names the same for the # purposes of readibility. #################### # .. _caching_extension: # # Caching extensions to file # ----------------------------------------------------- # # Extensions can be cached to file so that your NWB file will carry the extensions needed to read the file with it.
def test_get_class(self): self.test_load_namespace() TetrodeSeries = get_class('TetrodeSeries', self.prefix) # noqa: F841
where these are (this is) the compartment(s) compartment_labels: str | Iterable(str) (optional) or where these are (this is) the label(s) Returns ------- np.array(dtype=int) """ if compartment_numbers is not None and compartment_labels is not None: raise ValueError('you cannot specify both compartments and compartment_labels') if cell == 0: start_ind = 0 else: start_ind = self.compartments['number_index'].data[cell-1] cell_compartments = self.compartments['number'][cell] if compartment_numbers is not None: return self._compartment_finder(cell_compartments, compartment_numbers, int, start_ind) elif compartment_labels is not None: return self._compartment_finder(cell_compartments, compartment_labels, str, start_ind) else: return np.arange(start_ind, start_ind + len(cell_compartments), dtype=int) CompartmentSeries = get_class('CompartmentSeries', name) CompartmentSeries._compartment_finder = _compartment_finder CompartmentSeries.find_compartments = find_compartments SimulationMetaData = get_class('SimulationMetaData', name)
#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, name='soma')
import os from pynwb import load_namespaces, get_class ibl_metadata_specpath = os.path.join( os.path.dirname(__file__), 'spec', 'ndx-ibl-metadata.namespace.yaml' ) if not os.path.exists(ibl_metadata_specpath): ibl_metadata_specpath = os.path.abspath(os.path.join( os.path.dirname(__file__), '..', '..', '..', 'spec', 'ndx-ibl-metadata.namespace.yaml' )) # Load the namespace load_namespaces(ibl_metadata_specpath) IblSessionData = get_class('IblSessionData', 'ndx-ibl-metadata') IblSubject = get_class('IblSubject', 'ndx-ibl-metadata') IblProbes = get_class('IblProbes', 'ndx-ibl-metadata')
import os from pynwb import load_namespaces, get_class from os import path from pynwb.file import MultiContainerInterface from pynwb import register_class from pynwb.file import LabMetaData name = 'ndx-task' here = path.abspath(path.dirname(__file__)) ns_path = os.path.join(here, 'spec', name + '.namespace.yaml') load_namespaces(ns_path) Task = get_class('Task', name) @register_class('Tasks', name) class Tasks(MultiContainerInterface, LabMetaData): """ Purpose: Topological graph representing connected components of a behavioral Environment. Arguments: name (str): name of this Environment tasks (list): list of Environment objects """ __nwbfields__ = ('name', 'tasks')
from pynwb import load_namespaces, get_class from os import path from pynwb.file import MultiContainerInterface from pynwb import register_class from pynwb.file import LabMetaData name = 'ndx-maze' here = path.abspath(path.dirname(__file__)) ns_path = os.path.join(here, 'spec', name + '.namespace.yaml') load_namespaces(ns_path) Node = get_class('Node', name) Edge = get_class('Edge', name) PointNode = get_class('PointNode', name) SegmentNode = get_class('SegmentNode', name) PolygonNode = get_class('PolygonNode', name) @register_class('Environment', name) class Environment(MultiContainerInterface): """ Purpose: Topological graph representing connected components of a beahvioral Environment. Arguments: name (str): name of this Environment nodes (list): list of Node objects contained in this Environment
import os from pynwb import load_namespaces, get_class from os import path name = 'ndx-nonrigid-motion-correction' here = path.abspath(path.dirname(__file__)) ns_path = os.path.join(here, 'spec', name + '.namespace.yaml') load_namespaces(ns_path) NonrigidMotionCorrection = get_class('NonrigidMotionCorrection', name) def local_interpolation(transform, image, npixels=1): """ Parameters ---------- transform image npixels: int Returns ------- """ pass def nonrigid_motion_correction_apply(self, ts):
import os from pynwb import load_namespaces, get_class from os import path name = 'ndx-fret' here = path.abspath(path.dirname(__file__)) ns_path = os.path.join(here, 'spec', name + '.namespace.yaml') load_namespaces(ns_path) FRETSeries = get_class('FRETSeries', name) FRET = get_class('FRET', name)
# 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 numpy as np import pandas as pd from bs4 import BeautifulSoup from pynwb import load_namespaces, get_class from pynwb.misc import UnitTimes from general import gzip import pdb name = 'general' ns_path = name + '.namespace.yaml' load_namespaces(ns_path) PopulationSpikeTimes = get_class('PopulationSpikeTimes', name) def load_xml(filepath): with open(filepath, 'r') as xml_file: contents = xml_file.read() soup = BeautifulSoup(contents, 'xml') return soup def get_channel_groups(xml_filepath): """Get the groups of channels that are recorded on each shank from the xml file Parameters ----------
# and :py:func:`~hdmf.utils.get_docval` # # When extending :py:class:`~pynwb.core.NWBContainer` or :py:class:`~pynwb.core.NWBContainer` # subclasses, you should define the class field ``__nwbfields__``. This will # tell PyNWB the properties of the :py:class:`~pynwb.core.NWBContainer` extension. # # If you do not want to write additional code to read your extensions, PyNWB is able to dynamically # create an :py:class:`~pynwb.core.NWBContainer` subclass for use within the PyNWB API. # Dynamically created classes can be inspected using the built-in :py:mod:`inspect` module. from pynwb import get_class, load_namespaces ns_path = "mylab.namespace.yaml" load_namespaces(ns_path) AutoTetrodeSeries = get_class('TetrodeSeries', 'mylab') #################### # .. note:: # # When defining your own :py:class:`~pynwb.core.NWBContainer`, the subclass name does not need to be the same # as the extension type name. However, it is encouraged to keep class and extension names the same for the # purposes of readability. #################### # .. _caching_extension: # # Caching extensions to file # ----------------------------------------------------- # # By default, extensions are cached to file so that your NWB file will carry the extensions needed to read the file
import os from pynwb import load_namespaces, get_class from os import path name = 'ndx-spectrum' here = path.abspath(path.dirname(__file__)) ns_path = os.path.join(here, 'spec', name + '.namespace.yaml') load_namespaces(ns_path) Spectrum = get_class('Spectrum', name)
import os import pandas as pd from pynwb import load_namespaces, get_class from pynwb.misc import AnnotationSeries name = 'ndx-miniscope' here = os.path.abspath(os.path.dirname(__file__)) ns_path = os.path.join(here, 'spec', name + '.namespace.yaml') load_namespaces(ns_path) Miniscope = get_class('Miniscope', name) def load_miniscope_timestamps(fpath, cam_num=1): """Reads timestamp.dat and outputs a list of times in seconds Parameters ---------- fpath: str data directory or path to timestamp.dat cam_num: int number of feed Returns ------- numpy.ndarray list if times in seconds """
import os from pynwb import load_namespaces, get_class # Set path of the namespace.yaml file to the expected install location ndx_stimulus_template_specpath = os.path.join( os.path.dirname(__file__), 'ndx-aibs-stimulus-template.namespace.yaml' ) # Load the namespace load_namespaces(ndx_stimulus_template_specpath) StimulusTemplateExtension = get_class('StimulusTemplate', 'ndx-aibs-stimulus-template')
import os from pynwb import load_namespaces, get_class from pynwb import register_class, docval from hdmf.utils import call_docval_func from pynwb.file import MultiContainerInterface from hdmf.common.table import DynamicTable, ElementIdentifiers name = 'ndx-tempo' here = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..')) ns_path = os.path.join(here, 'spec', name + '.namespace.yaml') load_namespaces(ns_path) Measurement = get_class('Measurement', name) LaserLine = get_class('LaserLine', name) PhotoDetector = get_class('PhotoDetector', name) @register_class('LockInAmplifier', name) class LockInAmplifier(DynamicTable): __columns__ = ( {'name': 'channel_name', 'description': 'name of the channel of lock_in_amp'}, {'name': 'offset', 'description': 'offset for channel of lock_in_amp'}, {'name': 'gain', 'description': 'gain for channel of lock_in_amp'} ) @docval({'name': 'name', 'type': str, 'doc': 'Name of this Compartments object', 'default': 'compartments'},
import os from pynwb import load_namespaces, get_class # Set path of the namespace.yaml file to the expected install location ndx_ophys_events_specpath = os.path.join( os.path.dirname(__file__), 'ndx-aibs-ophys-event-detection.namespace.yaml' ) # Load the namespace load_namespaces(ndx_ophys_events_specpath) OphysEventDetection = get_class('OphysEventDetection', 'ndx-aibs-ophys-event-detection')
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')
import os from pynwb import load_namespaces, get_class name = 'ndx-bipolar-scheme' # Set path of the namespace.yaml file to the expected install location ndx_bipolar_scheme_specpath = os.path.join(os.path.dirname(__file__), 'spec', name + '.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_bipolar_scheme_specpath): ndx_bipolar_scheme_specpath = os.path.abspath( os.path.join(os.path.dirname(__file__), '..', '..', '..', 'spec', name + '.namespace.yaml')) # Load the namespace load_namespaces(ndx_bipolar_scheme_specpath) BipolarSchemeTable = get_class('BipolarSchemeTable', 'ndx-bipolar-scheme') NdxBipolarScheme = get_class('NdxBipolarScheme', 'ndx-bipolar-scheme')