Skip to content

Instantly share code, notes, and snippets.

@ajtritt
Created August 16, 2017 04:13
Show Gist options
  • Save ajtritt/fec1af907b51304df4769db359167450 to your computer and use it in GitHub Desktop.
Save ajtritt/fec1af907b51304df4769db359167450 to your computer and use it in GitHub Desktop.
An example of how to create and use a table by itself
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"
# 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)
ns_builder.export(ns_path)
ns_path = "alleninstitute.namespace.yaml"
load_namespaces(ns_path)
FooBarTable = get_class('FooBarTable', '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
stim_table = FooBarTable(data={'foo':[0, 1, 2], 'bar':[3, 4, 5]}))
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(stim_table)
manager = get_build_manager()
io = HDF5IO('tmp.nwb', manager, mode='w')
io.write(f)
io.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment