def test__children(self):
        params = {'test2': 'y1', 'test3': True}
        evt = Event([1.1, 1.5, 1.7] * pq.ms,
                    labels=np.array(
                        ['test event 1', 'test event 2', 'test event 3'],
                        dtype='S'),
                    name='test',
                    description='tester',
                    file_origin='test.file',
                    test1=1,
                    **params)
        evt.annotate(test1=1.1, test0=[1, 2])
        assert_neo_object_is_compliant(evt)

        segment = Segment(name='seg1')
        segment.events = [evt]
        segment.create_many_to_one_relationship()

        self.assertEqual(evt._single_parent_objects, ('Segment', ))
        self.assertEqual(evt._multi_parent_objects, ())

        self.assertEqual(evt._single_parent_containers, ('segment', ))
        self.assertEqual(evt._multi_parent_containers, ())

        self.assertEqual(evt._parent_objects, ('Segment', ))
        self.assertEqual(evt._parent_containers, ('segment', ))

        self.assertEqual(len(evt.parents), 1)
        self.assertEqual(evt.parents[0].name, 'seg1')

        assert_neo_object_is_compliant(evt)
Example #2
0
    def read_segment(self, lazy=False, cascade=True):
        seg = Segment(name=self.filename[-4])
        seg.file_origin = os.path.basename(self.filename)

        with open(self.filename, 'r') as nf:
            for i, l in enumerate(nf):
                if len(l) <= 2:
                    continue
                spikes = map(float, l[:-1].split(", "))
                seg.spiketrains.append(
                    SpikeTrain(spikes, name=str(i),
                               units=pq.s, t_stop=np.amax(spikes)))
        return seg
Example #3
0
    def test_write_read_single_spike(self):
        block1 = Block()
        seg = Segment('segment1')
        spiketrain1 = SpikeTrain([1] * pq.s,
                                 t_stop=10 * pq.s,
                                 sampling_rate=1 * pq.Hz)
        spiketrain1.annotate(yep='yop')
        block1.segments.append(seg)
        seg.spiketrains.append(spiketrain1)

        # write block
        filename = self.get_local_path('matlabiotestfile.mat')
        io1 = self.ioclass(filename)
        io1.write_block(block1)

        # read block
        io2 = self.ioclass(filename)
        block2 = io2.read_block()

        self.assertEqual(block1.segments[0].spiketrains[0],
                         block2.segments[0].spiketrains[0])

        # test annotations
        spiketrain2 = block2.segments[0].spiketrains[0]
        assert 'yep' in spiketrain2.annotations
        assert spiketrain2.annotations['yep'] == 'yop'
Example #4
0
    def saveCCData(self):

        currentAmpsSet = np.sort(
            list(set([float(x.magnitude)
                      for x in self.currentAmps]))).tolist()

        self.CCData = Block('Current Clamp Data')
        self.CCData.segments = [
            Segment(name='Current Of ' + unicode(iAmp) + 'nA')
            for iAmp in currentAmpsSet
        ]
        for iAmp, vTrace in zip(self.currentAmps, self.voltageTraces):
            presSegInd = currentAmpsSet.index(iAmp)
            self.CCData.segments[presSegInd].analogsignals.append(vTrace)
            self.CCData.segments[presSegInd].events.append(
                Event(time=vTrace.t_start, label=unicode(iAmp)))
            self.CCData.segments[presSegInd].epochs.append(
                Epoch(time=vTrace.t_start - 50 * qu.ms,
                      duration=50 * qu.ms,
                      label=unicode(
                          self.restingMembranePotentials[presSegInd])))

        writer = NeoHdf5IO(
            os.path.join(
                os.path.split(self.ephysFile)[0], self.expName + '_CC.hdf5'))
        writer.write_block(self.CCData)
        writer.close()
 def test_event_to_epoch(self):
     seg = Segment(name="test")
     # event = Event(times=np.array([5.0, 12.0, 23.0, 45.0]), units="ms", labels=np.array(["A", "B", "C", "D"]))
     event = Event(times=np.array([5.0, 12.0, 23.0, 45.0]),
                   units="ms",
                   labels=np.array(["A", "B", "C", "D"]))
     print("event : " + str(event))
     event.segment = seg
     epoch = event.to_epoch()
     print("epoch.magnitude : " + str(epoch.durations.magnitude))
     print("np.array([7.0, 11.0, 22.0]) : " +
           str(np.array([7.0, 11.0, 22.0])))
     assert_array_equal(epoch.durations.magnitude,
                        np.array([7.0, 11.0, 22.0]))
     self.assertEqual(str(type(epoch).__name__), 'Epoch')
     pass
    def test_write_read_single_spike(self):
        block1 = Block()
        seg = Segment('segment1')
        spiketrain = SpikeTrain([1] * pq.s, t_stop=10 * pq.s, sampling_rate=1 * pq.Hz)
        block1.segments.append(seg)
        seg.spiketrains.append(spiketrain)

        # write block
        filename = BaseTestIO.get_filename_path(self, 'matlabiotestfile.mat')
        io1 = self.ioclass(filename)
        io1.write_block(block1)

        # read block
        io2 = self.ioclass(filename)
        block2 = io2.read_block()

        self.assertEqual(block1.segments[0].spiketrains[0],
                         block2.segments[0].spiketrains[0])
Example #7
0
    nchannels = 3

    sampling_rate = pq.Quantity(1, "Hz")

    indexes = np.arange(nchannels)
    for cidx, signal in enumerate([data_a, data_b, data_c]):
        indexes = np.arange(signal.shape[1]) + ch_count
        ch_count += signal.shape[1]
        chx = ChannelIndex(name="channel-{}".format(cidx),
                           index=indexes,
                           channel_names=["S" + str(cidx) + chr(ord("a") + i) for i in indexes],
                           channel_ids=cidx * 100 + indexes + 1)
        block.channel_indexes.append(chx)

    for idx in range(nsegments):
        seg = Segment("seg-ex{}".format(idx),
                      description="Segment number {}".format(idx))
        block.segments.append(seg)

        # signal +idx is for testing if segment is correct
        for didx, data in enumerate((data_a, data_b, data_c)):
            if didx == 1:
                asig = AnalogSignal(name="Seg {} :: Data {}".format(idx, didx),
                                    signal=data, units="V",
                                    sampling_rate=sampling_rate)
            else:
                asig = AnalogSignal(name="Seg {} :: Data {}".format(idx, didx),
                                signal=data, units="mV",
                                sampling_rate=sampling_rate)
            seg.analogsignals.append(asig)
            block.channel_indexes[didx].analogsignals.append(asig)
        asig_count += len((data_a, data_b, data_c))
Example #8
0
    def uploadToGNode(self):

        blk = Block()
        blk.name = self.blockNameProc
        blk.file_origin = self.originalFile
        blk.file_datetime = asctime()
        blk.description = 'Regions of Interest of electrophysiological recordings of a vibration sensitive neuron'
        blk = self.GNodeSession.set(blk)

        expSec = self.mainSec.sections[self.expName + '_Experiment']
        freqProp = expSec.properties['FrequenciesUsed']
        writtenFreq = getValuesOfProperty(freqProp)
        durProp = expSec.properties['PulseInputDurations']
        writtenDur = getValuesOfProperty(durProp)
        intervalProp = expSec.properties['PulseInputIntervals']
        writtenIntervals = getValuesOfProperty(intervalProp)

        blk.section = expSec
        blk = self.GNodeSession.set(blk)

        count = 0
        for (freq, amp, resp, stim, dur, inter) in \
            zip(self.stimFreqs, self.stimAmps, self.responseVTraces, self.stimTraces, self.stimDur, self.stimInterval):

            count += 1

            print 'Uploading Segment' + str(count)
            seg = self.GNodeSession.set(
                Segment(name=blk.name + '_seg' + str(count), index=count))

            seg.block = blk
            seg = self.GNodeSession.set(seg)

            resp.name = 'Membrane Potential'
            resp.description = 'Response to the associated vibration stimulus applied to the antenna.'
            stim.name = 'Vibration Stimulus'
            stim.description = 'Vibration Stimulus applied to the antenna'

            resp = self.GNodeSession.set(resp)
            stim = self.GNodeSession.set(stim)

            resp.segment = seg
            stim.segment = seg

            resp = self.GNodeSession.set(resp)
            stim = self.GNodeSession.set(stim)

            metadata = []

            metadata.append(freqProp.values[find_nearest_Id(writtenFreq,
                                                            freq)])
            if min(abs(writtenDur - dur)).magnitude < 5:
                metadata.append(durProp.values[find_nearest_Id(
                    writtenDur, dur)])
                metadata.append(intervalProp.values[find_nearest_Id(
                    writtenIntervals, inter)])

            seg.metadata = metadata
            seg = self.GNodeSession.set(seg)

            print 'Uploading Segment' + str(count) + ' Done'
            import ipdb
            ipdb.set_trace()
Example #9
0
    def read_segment(self, lazy=False, cascade=True):
        seg = Segment(name=self.filename[-4])
        seg.file_origin = os.path.basename(self.filename)

        # Just one pass through the file! Do it live!
        with open(self.filename, 'r') as nf:
            for i, l in enumerate(nf):
                if i == 0:
                    # First line, header. Set up basic data structures.
                    cols = l[:-1].split(",")
                    data = [[] for _ in xrange(len(cols))]
                    lastdata = [None for _ in xrange(len(cols))]
                    types = []
                    for col in cols:
                        if col == "time":
                            types.append("Time")
                        elif col.endswith("_spikes"):
                            types.append("SpikeTrains")
                        elif col.endswith("_event"):
                            types.append("EventArray")
                        elif col.endswith("_states"):
                            types.append("EpochArray")
                        else:
                            types.append("AnalogSignal")
                    continue

                ll = l[:-1].split(",")

                if i == 1:
                    # Second line. Get number of dimensions for everything.
                    for j, t in enumerate(types):
                        if t == "SpikeTrains" or t == "AnalogSignal":
                            dims = len(ll[j].split(';'))
                            data[j] = [[] for _ in xrange(dims)]
                        elif t == "EventArray":
                            data[j] = [[] for _ in xrange(3)]
                    # No continue, keep going

                time = float(ll[0])
                for j, lll in enumerate(ll):
                    if types[j] == "Time":
                        data[j].append(float(lll))
                    elif types[j] == "AnalogSignal":
                        for k, v in enumerate(lll.split(";")):
                            data[j][k].append(float(v))
                    elif types[j] == "EventArray":
                        curdata = float(lll)
                        if curdata == 1.0 and curdata != lastdata[j]:
                            data[j][0].append(time)
                        elif curdata == 0.0 and curdata != lastdata[j]:
                            data[j][1].append(time)
                        elif curdata == -1.0 and curdata != lastdata[j]:
                            data[j][2].append(time)
                        lastdata[j] = curdata
                    elif types[j] == "SpikeTrains":
                        for k in [k for k, s in
                                  enumerate(lll.split(";")) if s == '1']:
                            data[j][k].append(time)

        t_start = data[0][0]
        t_stop = data[0][-1]
        period = pq.s * (t_stop - t_start) / float(len(data[0]))
        # File closed now, process each column and add to segment
        # for col, typ, dat in izip(cols, types, data):
        for col, typ, dat in zip(cols, types, data):
            if typ == "SpikeTrains":
                for i, times in enumerate(dat):
                    seg.spiketrains.append(
                        SpikeTrain(times, name=col + '_' + str(i),
                                   units=pq.s, t_stop=t_stop,
                                   sampling_rate=1.0 / period))
            elif typ == "AnalogSignal":

                for i, v in enumerate(dat):
                    seg.analogsignals.append(
                        AnalogSignal(v, units=pq.mV, sampling_period=period,
                                     name=col + '_' + str(i)))
            elif typ == "EventArray":
                name = col[:-5] + 'on'
                labels = np.array([name] * len(dat[0]), dtype='S')
                seg.eventarrays.append(
                    EventArray(times=dat[0], labels=labels, channel_name=name))
                name = col[:-5] + 'zero'
                labels = np.array([name] * len(dat[0]), dtype='S')
                seg.eventarrays.append(
                    EventArray(times=dat[1], labels=labels, channel_name=name))
                name = col[:-5] + 'off'
                labels = np.array([name] * len(dat[0]), dtype='S')
                seg.eventarrays.append(
                    EventArray(times=dat[2], labels=labels, channel_name=name))

        return seg
Example #10
0
    def uploadToGNode(self):

        self.csvData = extractCSVMetaData(self.csvFile, self.expName)

        self.dataBlockToUpload = Block(name=self.blockName, file_origin=self.expName)

        raw_seg = Segment(name='rawData', index=0)

        self.vibrationSignal.name = 'Vibration Stimulus'
        self.vibrationSignal.description = 'Vibration Stimulus applied to the honey bee antenna'
        self.voltageSignal.name = 'Membrane Potential'
        self.voltageSignal.description = 'Vibration Sensitive inter-neuron membrane potential'
        self.vibrationSignal.segment = raw_seg
        self.voltageSignal.segment = raw_seg

        raw_seg.analogsignals.append(self.vibrationSignal)
        raw_seg.analogsignals.append(self.voltageSignal)

        if len(self.dataBlock.segments[0].analogsignals) > 2:

            self.currentSignal.name = 'Current Signal'
            self.currentSignal.description = 'Indicates whether a current is being injected or not. The magnitudes ' \
                                             'are given in an event array'

            self.currentSignal.segment = raw_seg
            raw_seg.analogsignals.append(self.currentSignal)

            if len(self.dataBlock.segments[0].eventarrays) == 2:
                raw_seg.eventarrays.append(self.dataBlock.segments[0].eventarrays[1])
                self.dataBlock.segments[0].eventarrays[1].segment = raw_seg

        raw_seg.block = self.dataBlockToUpload
        self.dataBlockToUpload.segments.append(raw_seg)

        self.doc = odml.Document(author="Ajayrama K.", version="1.0")

        self.mainSec = odml.Section(name=self.expName, type='experiment')
        self.doc.append(self.mainSec)

        expSummary = odml.Section(name='VibrationStimulus', type='experiment/electrophysiology')

        quantity_parser = lambda lst: [odml.Value(data=float(x), unit=x.dimensionality.string) for x in lst]

        frequencies = quantity_parser(self.csvData['freqs'])
        if frequencies:
            expSummary.append(odml.Property(name='FrequenciesUsed', value=frequencies))

        durations = quantity_parser(self.csvData['pulse'][0])
        if durations:
            expSummary.append(odml.Property(name='PulseInputDurations', value=durations))

        intervals = quantity_parser(self.csvData['pulse'][1])
        if intervals:
            expSummary.append(odml.Property(name='PulseInputIntervals', value=intervals))

        expSummary.append(odml.Property(name='SpontaneousActivityPresence', value=self.csvData['spont']))

        if not self.csvData['resp'] == '':
            expSummary.append(odml.Property(name='NatureOfResponse', value=self.csvData['resp']))

        self.mainSec.append(expSummary)

        print asctime() + ' : Uploading metadata'
        doc = self.session.set_all(self.doc)
        print asctime() + ' : Uploading metadata Done'

        print asctime() + ' : Refreshing metadata'
        mainSec = self.session.get(doc.sections[0].location, refresh=True, recursive=True)
        print asctime() + ' : Refreshing metadata Done'

        self.dataBlockToUpload.section = mainSec

        print asctime() + ' : Uploading Data'
        blkLoc = self.session.set_all(self.dataBlockToUpload)
        print asctime() + ' : Uploading Data Done'
Example #11
0
"""
Example for usecases.rst
"""

from itertools import cycle
import numpy as np
from quantities import ms, mV, kHz
import matplotlib.pyplot as plt
from neo import Block, Segment, ChannelView, Group, SpikeTrain, AnalogSignal

store_signals = False

block = Block(name="probe data", tetrode_ids=["Tetrode #1", "Tetrode #2"])
block.segments = [
    Segment(name="trial #1", index=0),
    Segment(name="trial #2", index=1),
    Segment(name="trial #3", index=2)
]

n_units = {"Tetrode #1": 2, "Tetrode #2": 5}

# Create a group for each neuron, annotate each group with the tetrode from which it was recorded
groups = []
counter = 0
for tetrode_id, n in n_units.items():
    groups.extend([
        Group(name=f"neuron #{counter + i + 1}", tetrode_id=tetrode_id)
        for i in range(n)
    ])
    counter += n
block.groups.extend(groups)
Example #12
0
from neo import (Block, Segment, AnalogSignal, IrregularlySampledSignal, Event,
                 Epoch, SpikeTrain, ChannelIndex, Unit)
from neo.io.nixio import NixIO

import numpy as np
import quantities as pq

for b in range(3):
    # Create a Block called example
    block = Block("example" + str(b),
                  description="The root block for this example")

    # Create a Segment called seg-ex1 and attach it to the Block
    seg_a = Segment("seg-ex1", description="Segment one")
    block.segments.append(seg_a)

    # A second segment with an added comment
    # The comment is an "annotation"; any keyword argument can be used
    seg_b = Segment("seg-ex2",
                    description="Segment two",
                    comment="Second recording set")
    block.segments.append(seg_b)

    # Generate 3 fake data signals using numpy's random function
    # The shapes of the arrays are arbitrary
    data_a = np.random.random((300, 10))
    data_b = np.random.random((1200, 3))
    data_c = np.random.random((8000, 5))

    # random sampling times for data_b
    data_b_t = np.cumsum(np.random.random(1200))
"""
Example for usecases.rst
"""

import numpy as np
from neo import Segment, AnalogSignal, SpikeTrain, Group, ChannelView
from quantities import Hz

# generate some fake data
seg = Segment()
seg.analogsignals.append(
    AnalogSignal([[0.1, 0.1, 0.1, 0.1],
                  [-2.0, -2.0, -2.0, -2.0],
                  [0.1, 0.1, 0.1, 0.1],
                  [-0.1, -0.1, -0.1, -0.1],
                  [-0.1, -0.1, -0.1, -0.1],
                  [-3.0, -3.0, -3.0, -3.0],
                  [0.1, 0.1, 0.1, 0.1],
                  [0.1, 0.1, 0.1, 0.1]],
                 sampling_rate=1000 * Hz, units='V'))

# extract spike trains from all channels
st_list = []
for signal in seg.analogsignals:
    # use a simple threshhold detector
    spike_mask = np.where(np.min(signal.magnitude, axis=1) < -1.0)[0]

    # create a spike train
    spike_times = signal.times[spike_mask]
    st = SpikeTrain(spike_times, t_start=signal.t_start, t_stop=signal.t_stop)