Exemplo n.º 1
0
    def test_write_clobber(self):
        io = HDF5IO(self.path, self.manager)
        io.write(self.container)
        io.close()
        f = File(self.path)  # noqa: F841

        if six.PY2:
            assert_file_exists = IOError
        elif six.PY3:
            assert_file_exists = OSError

        with self.assertRaises(assert_file_exists):
            io = HDF5IO(self.path, self.manager, mode='w-')
            io.write(self.container)
            io.close()
Exemplo n.º 2
0
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)
Exemplo n.º 3
0
 def testInFromMatNWB(self):
     filename = 'MatNWB.' + self.__class__.__name__ + '.testOutToPyNWB.nwb'
     with HDF5IO(filename, manager=get_manager(), mode='r') as io:
         matfile = io.read()
         matcontainer = self.getContainer(matfile)
         pycontainer = self.getContainer(self.file)
         self.assertContainerEqual(matcontainer, pycontainer)
def main():
    import os.path

    # example: start
    from datetime import datetime

    from pynwb import NWBFile, TimeSeries, get_manager
    from pynwb.form.backends.hdf5 import HDF5IO

    start_time = datetime(1970, 1, 1, 12, 0, 0)
    create_date = datetime(2017, 4, 15, 12, 0, 0)

    nwbfile = NWBFile('the PyNWB tutorial',
                      'a test NWB File',
                      'TEST123',
                      start_time,
                      file_create_date=create_date)

    ts = TimeSeries('test_timeseries',
                    'example_source',
                    list(range(100, 200, 10)),
                    'SIunit',
                    timestamps=list(range(10)),
                    resolution=0.1)

    nwbfile.add_acquisition(ts)

    io = HDF5IO("example.h5", manager=get_manager(), mode='w')
    io.write(nwbfile)
    io.close()
    # example: end

    os.remove("example.h5")
Exemplo n.º 5
0
 def test_read_builder(self):
     self.maxDiff = None
     io = HDF5IO(self.path, self.manager)
     io.write_builder(self.builder)
     builder = io.read_builder()
     self.assertBuilderEqual(builder, self.builder)
     io.close()
Exemplo n.º 6
0
 def test_write_attribute_reference_builder(self):
     writer = HDF5IO(self.path, self.manager)
     self.builder.set_attribute('ref_attribute', self.ts_builder)
     writer.write_builder(self.builder)
     writer.close()
     f = self.check_fields()
     self.assertIsInstance(f.attrs['ref_attribute'], Reference)
     self.assertEqual(f['acquisition/timeseries/test_timeseries'], f[f.attrs['ref_attribute']])
Exemplo n.º 7
0
 def test_overwrite_written(self):
     self.maxDiff = None
     io = HDF5IO(self.path, self.manager)
     io.write_builder(self.builder)
     builder = io.read_builder()
     with self.assertRaisesRegex(ValueError,
                                 "cannot change written to not written"):
         builder.written = False
     io.close()
Exemplo n.º 8
0
 def test_read(self):
     hdf5io = HDF5IO(self.path, self.manager)
     hdf5io.write(self.container)
     hdf5io.close()
     container = hdf5io.read()
     self.assertIsInstance(container, NWBFile)
     raw_ts = container.acquisition
     self.assertEqual(len(raw_ts), 1)
     self.assertIsInstance(raw_ts[0], TimeSeries)
     hdf5io.close()
Exemplo n.º 9
0
    def roundtripContainer(self):
        description = 'a file to test writing and reading a %s' % self.container_type
        identifier = 'TEST_%s' % self.container_type
        nwbfile = NWBFile(description,
                          identifier,
                          self.start_time,
                          file_create_date=self.create_date)
        self.addContainer(nwbfile)

        self.writer = HDF5IO(self.filename, manager=get_manager(), mode='w')
        self.writer.write(nwbfile)
        self.writer.close()
        self.reader = HDF5IO(self.filename, manager=get_manager(), mode='r')
        read_nwbfile = self.reader.read()

        try:
            tmp = self.getContainer(read_nwbfile)
            return tmp
        except Exception as e:
            self.reader.close()
            self.reader = None
            raise e
Exemplo n.º 10
0
 def test_roundtrip(self):
     description = 'a file to test writing and reading a %s' % self.container_type
     source = 'test_roundtrip for %s' % self.container_type
     identifier = 'TEST_%s' % self.container_type
     nwbfile = NWBFile(source, description, identifier, self.start_time, file_create_date=self.create_date)
     self.addContainer(nwbfile)
     io = HDF5IO(self.filename, self.manager)
     io.write(nwbfile)
     try:
         read_nwbfile = io.read()
         read_container = self.getContainer(read_nwbfile)
         self.assertContainerEqual(self.container, read_container)
     finally:
         io.close()
Exemplo n.º 11
0
 def test_nwbio(self):
     io = HDF5IO(self.path, self.manager)
     io.write(self.container)
     io.close()
     f = File(self.path)
     self.assertIn('acquisition', f)
     self.assertIn('analysis', f)
     self.assertIn('general', f)
     self.assertIn('processing', f)
     self.assertIn('file_create_date', f)
     self.assertIn('identifier', f)
     self.assertIn('session_description', f)
     self.assertIn('session_start_time', f)
     acq = f.get('acquisition')
     self.assertIn('test_timeseries', acq)
Exemplo n.º 12
0
 def roundtripContainer(self):
     description = 'a file to test writing and reading a %s' % self.container_type
     source = 'test_roundtrip for %s' % self.container_type
     identifier = 'TEST_%s' % self.container_type
     nwbfile = NWBFile(source, description, identifier, self.start_time, file_create_date=self.create_date)
     self.addContainer(nwbfile)
     self.io = HDF5IO(self.filename, self.manager)
     self.io.write(nwbfile)
     read_nwbfile = self.io.read()
     try:
         tmp = self.getContainer(read_nwbfile)
         return tmp
     except Exception as e:
         self.io.close()
         self.io = None
         raise e
Exemplo n.º 13
0
 def test_write(self):
     hdf5io = HDF5IO(self.path, self.manager)
     hdf5io.write(self.container)
     hdf5io.close()
Exemplo n.º 14
0
def readfile(filename):
    io = HDF5IO(filename, manager=pynwb.get_manager(), mode='r')
    nwbfile = io.read()
    io.close()
    return nwbfile
Exemplo n.º 15
0
 def test_write_context_manager(self):
     with HDF5IO(self.path, self.manager) as writer:
         writer.write_builder(self.builder)
     self.check_fields()
Exemplo n.º 16
0
 def test_write_builder(self):
     writer = HDF5IO(self.path, self.manager)
     writer.write_builder(self.builder)
     writer.close()
     self.check_fields()
Exemplo n.º 17
0
 def test_write(self):
     hdf5io = HDF5IO(self.path, manager=self.manager, mode='a')
     hdf5io.write(self.container)
     hdf5io.close()
Exemplo n.º 18
0
 def testOutToMatNWB(self):
     filename = 'PyNWB.' + self.__class__.__name__ + '.testOutToMatNWB.nwb'
     with HDF5IO(filename, manager=get_manager(), mode='w') as io:
         io.write(self.file)
     self.assertTrue(os.path.isfile(filename))
Exemplo n.º 19
0
 def testOutToMatNWB(self):
     filename = 'PyNWB.' + self.__class__.__name__ + '.testOutToMatNWB.nwb'
     io = HDF5IO(filename, manager=get_manager())
     io.write(self.file)
     io.close()
     self.assertTrue(os.path.isfile(filename))
def main():

    import os.path

    # prerequisites: start
    import numpy as np

    rate = 10.0
    np.random.seed(1234)
    data_len = 1000
    ephys_data = np.random.rand(data_len)
    ephys_timestamps = np.arange(data_len) / rate
    spatial_timestamps = ephys_timestamps[::10]
    spatial_data = np.cumsum(np.random.normal(size=(2, len(spatial_timestamps))), axis=-1).T
    # prerequisites: end

    # create-nwbfile: start
    from datetime import datetime
    from pynwb import NWBFile

    f = NWBFile('the PyNWB tutorial', 'my first synthetic recording', 'EXAMPLE_ID', datetime.now(),
                experimenter='Dr. Bilbo Baggins',
                lab='Bag End Laboratory',
                institution='University of Middle Earth at the Shire',
                experiment_description='I went on an adventure with thirteen dwarves to reclaim vast treasures.',
                session_id='LONELYMTN')
    # create-nwbfile: end

    # save-nwbfile: start
    from pynwb import get_manager
    from pynwb.form.backends.hdf5 import HDF5IO

    filename = "example.h5"
    io = HDF5IO(filename, manager=get_manager(), mode='w')
    io.write(f)
    io.close()
    # save-nwbfile: end

    os.remove(filename)

    # create-device: start
    device = f.create_device(name='trodes_rig123', source="a source")
    # create-device: end

    # create-electrode-groups: start
    electrode_name = 'tetrode1'
    source = "an hypothetical source"
    description = "an example tetrode"
    location = "somewhere in the hippocampus"

    electrode_group = f.create_electrode_group(electrode_name,
                                               source=source,
                                               description=description,
                                               location=location,
                                               device=device)

    # create-electrode-groups: end

    # create-electrode-table-region: start
    for idx in [1, 2, 3, 4]:
        f.add_electrode(idx,
                        x=1.0, y=2.0, z=3.0,
                        imp=float(-idx),
                        location='CA1', filtering='none',
                        description='channel %s' % idx, group=electrode_group)

    electrode_table_region = f.create_electrode_table_region([0, 2], 'the first and third electrodes')
    # create-electrode-table-region: end

    # create-timeseries: start
    from pynwb.ecephys import ElectricalSeries
    from pynwb.behavior import SpatialSeries

    ephys_ts = ElectricalSeries('test_ephys_data',
                                'an hypothetical source',
                                ephys_data,
                                electrode_table_region,
                                timestamps=ephys_timestamps,
                                # Alternatively, could specify starting_time and rate as follows
                                # starting_time=ephys_timestamps[0],
                                # rate=rate,
                                resolution=0.001,
                                comments="This data was randomly generated with numpy, using 1234 as the seed",
                                description="Random numbers generated with numpy.random.rand")
    f.add_acquisition(ephys_ts)

    spatial_ts = SpatialSeries('test_spatial_timeseries',
                               'a stumbling rat',
                               spatial_data,
                               'origin on x,y-plane',
                               timestamps=spatial_timestamps,
                               resolution=0.1,
                               comments="This data was generated with numpy, using 1234 as the seed",
                               description="This 2D Brownian process generated with "
                                           "np.cumsum(np.random.normal(size=(2, len(spatial_timestamps))), axis=-1).T")
    f.add_acquisition(spatial_ts)
    # create-timeseries: end

    # create-data-interface: start
    from pynwb.ecephys import LFP
    from pynwb.behavior import Position

    lfp = f.add_acquisition(LFP('a hypothetical source'))
    ephys_ts = lfp.create_electrical_series('test_ephys_data',
                                            'an hypothetical source',
                                            ephys_data,
                                            electrode_table_region,
                                            timestamps=ephys_timestamps,
                                            resolution=0.001,
                                            comments="This data was randomly generated with numpy, using 1234 as the seed",  # noqa: E501
                                            description="Random numbers generated with numpy.random.rand")

    pos = f.add_acquisition(Position('a hypothetical source'))
    spatial_ts = pos.create_spatial_series('test_spatial_timeseries',
                                           'a stumbling rat',
                                           spatial_data,
                                           'origin on x,y-plane',
                                           timestamps=spatial_timestamps,
                                           resolution=0.1,
                                           comments="This data was generated with numpy, using 1234 as the seed",
                                           description="This 2D Brownian process generated with "
                                                       "np.cumsum(np.random.normal(size=(2, len(spatial_timestamps))), axis=-1).T")  # noqa: E501
    # create-data-interface: end

    # create-epochs: start
    epoch_tags = ('example_epoch',)

    f.create_epoch(name='epoch1', start_time=0.0, stop_time=1.0, tags=epoch_tags,
                   description="the first test epoch", timeseries=[ephys_ts, spatial_ts])

    f.create_epoch(name='epoch2', start_time=0.0, stop_time=1.0, tags=epoch_tags,
                   description="the second test epoch", timeseries=[ephys_ts, spatial_ts])
    # create-epochs: end

    # create-compressed-timeseries: start
    from pynwb.ecephys import ElectricalSeries
    from pynwb.behavior import SpatialSeries
    from pynwb.form.backends.hdf5 import H5DataIO

    ephys_ts = ElectricalSeries('test_compressed_ephys_data',
                                'an hypothetical source',
                                H5DataIO(ephys_data, compress=True),
                                electrode_table_region,
                                timestamps=H5DataIO(ephys_timestamps, compress=True),
                                resolution=0.001,
                                comments="This data was randomly generated with numpy, using 1234 as the seed",
                                description="Random numbers generated with numpy.random.rand")
    f.add_acquisition(ephys_ts)

    spatial_ts = SpatialSeries('test_compressed_spatial_timeseries',
                               'a stumbling rat',
                               H5DataIO(spatial_data, compress=True),
                               'origin on x,y-plane',
                               timestamps=H5DataIO(spatial_timestamps, compress=True),
                               resolution=0.1,
                               comments="This data was generated with numpy, using 1234 as the seed",
                               description="This 2D Brownian process generated with "
                                           "np.cumsum(np.random.normal(size=(2, len(spatial_timestamps))), axis=-1).T")
    f.add_acquisition(spatial_ts)
Exemplo n.º 21
0
def writefile(nwbfile, filename):
    io = HDF5IO(filename, manager=pynwb.get_manager(), mode='w')
    io.write(nwbfile)
    io.close()