Skip to content

Instantly share code, notes, and snippets.

@ajtritt
Created August 16, 2017 04:09
Show Gist options
  • Save ajtritt/a705d184344d7177b9a847519ea79b8b to your computer and use it in GitHub Desktop.
Save ajtritt/a705d184344d7177b9a847519ea79b8b to your computer and use it in GitHub Desktop.
An example of how to use NWBDatasetSpec to create a table
import numpy as np
from pynwb import get_class, load_namespaces
from datetime import datetime
from pynwb import NWBFile
import pandas as pd
from form.backends.hdf5 import HDF5IO
from pynwb import get_build_manager
ns_path = "alleninstitute.namespace.yaml"
ext_source = "alleninstitute.extensions.yaml"
help_attr = NWBAttributeSpec(name='help',
dtype='str',
doc='no help for you',
value='YUP')
# Declare the FooBarTable type
# -- this is just a special dataset, where we use a compound data type
nwb_ds = NWBDatasetSpec(doc='An example type definition of a dataset that is a "table"',
dtype=[{'dtype': 'int', 'label': 'foo'},
{'dtype': 'float', 'label': 'bar'}],
attributes=[NWBAttributeSpec(name='baz', dtype='str', doc='an attribute for the sake of adding an attrubte')],
# lets not require a specific name for our new type.
# we can always specify a name when we include it elsewhere
#name='table',
neurodata_type_def='FooBarTable')
ns_builder = NWBNamespaceBuilder('Allen Institute extensions', "alleninstitute")
# Add this to the namespace on its own so it can be used elsewhere
ns_builder.add_spec(ext_source, nwb_ds)
# Now lets create another new type that uses the type we already created i.e. FooBarTable
# Note that we don't need to use TableContainer here, this is just an example of including an existing type
ext = NWBGroupSpec('An example table spec',
datasets=[NWBDatasetSpec(name='table', neurodata_type_inc='FooBarTable')], # this says this group will have datasets of type FooBarTable
neurodata_type_inc='NWBContainer',
neurodata_type_def='TableContainer',
attributes=[help_attr])
ns_builder.add_spec(ext_source, ext)
ns_builder.export(ns_path)
ns_path = "alleninstitute.namespace.yaml"
load_namespaces(ns_path)
FooBarTable = get_class('FooBarTable', 'alleninstitute')
TableContainer = get_class('TableContainer', 'alleninstitute')
# The usage of the FooBarTable constructor is not necessarily correct
# I am just demonstrating that FooBarTable is a type on its own that can be used
fs = TableContainer(
table=FooBarTable(data={'foo':[0, 1, 2], 'bar':[3, 4, 5]}),
name='my_population',
source='acquisition',
unit='second',
#help='None') # getting rid of this since we've hardcoded help
f = NWBFile(file_name='tmp.nwb',
source='me',
session_description='my first synthetic recording',
identifier='EXAMPLE_ID',
session_start_time=datetime.now(),
experimenter='Dr. Bilbo Baggins',
lab='Bag End Labatory',
institution='University of Middle Earth at the Shire',
experiment_description='empty',
session_id='LONELYMTN')
f.add_stimulus(fs)
manager = get_build_manager()
io = HDF5IO('tmp.nwb', manager, mode='w')
io.write(f)
io.close()
@bendichter
Copy link

I believe the requirements have changed a bit and the datatype specifications on line 20-21 now require 'doc' and 'name', not 'label'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment