Esempio n. 1
0
    def test__construct_subsegment_by_unit(self):
        nb_seg = 3
        nb_unit = 7
        unit_with_sig = [0, 2, 5]
        signal_types = ['Vm', 'Conductances']
        sig_len = 100

        #recordingchannelgroups
        rcgs = [ RecordingChannelGroup(name = 'Vm', channel_indexes = unit_with_sig),
                        RecordingChannelGroup(name = 'Conductance', channel_indexes = unit_with_sig), ]

        # Unit
        all_unit = [ ]
        for u in range(nb_unit):
            un = Unit(name = 'Unit #%d' % u, channel_indexes = [u])
            all_unit.append(un)

        bl = Block()
        for s in range(nb_seg):
            seg = Segment(name = 'Simulation %s' % s)
            for j in range(nb_unit):
                st = SpikeTrain([1, 2, 3], units = 'ms', t_start = 0., t_stop = 10)
                st.unit = all_unit[j]

            for t in signal_types:
                anasigarr = AnalogSignalArray( np.zeros((sig_len, len(unit_with_sig)) ), units = 'nA',
                                sampling_rate = 1000.*pq.Hz, channel_indexes = unit_with_sig )
                seg.analogsignalarrays.append(anasigarr)

        # what you want
        subseg = seg.construct_subsegment_by_unit(all_unit[:4])
Esempio n. 2
0
    def test_time_slice_None(self):
        time_slices = [(None, 5.0 * pq.s), (5.0 * pq.s, None), (None, None)]

        anasig = AnalogSignal(np.arange(50.0) * pq.mV,
                              sampling_rate=1.0 * pq.Hz)
        seg = Segment()
        seg.analogsignals = [anasig]

        block = Block()
        block.segments = [seg]
        block.create_many_to_one_relationship()

        # test without resetting the time
        for t_start, t_stop in time_slices:
            sliced = seg.time_slice(t_start, t_stop)

            assert_neo_object_is_compliant(sliced)
            self.assertEqual(len(sliced.analogsignals), 1)

            exp_t_start, exp_t_stop = t_start, t_stop
            if exp_t_start is None:
                exp_t_start = seg.t_start
            if exp_t_stop is None:
                exp_t_stop = seg.t_stop

            self.assertEqual(exp_t_start, sliced.t_start)
            self.assertEqual(exp_t_stop, sliced.t_stop)
Esempio n. 3
0
    def setup_segments(self):
        params = {'testarg2': 'yes', 'testarg3': True}
        self.segment1 = Segment(name='test', description='tester 1',
                                file_origin='test.file',
                                testarg1=1, **params)
        self.segment2 = Segment(name='test', description='tester 2',
                                file_origin='test.file',
                                testarg1=1, **params)
        self.segment1.annotate(testarg1=1.1, testarg0=[1, 2, 3])
        self.segment2.annotate(testarg11=1.1, testarg10=[1, 2, 3])

        self.segment1.analogsignals = self.sig1
        self.segment2.analogsignals = self.sig2

        self.segment1.analogsignalarrays = self.sigarr1
        self.segment2.analogsignalarrays = self.sigarr2

        self.segment1.epochs = self.epoch1
        self.segment2.epochs = self.epoch2

        self.segment1.epocharrays = self.epocharr1
        self.segment2.epocharrays = self.epocharr2

        self.segment1.events = self.event1
        self.segment2.events = self.event2

        self.segment1.eventarrays = self.eventarr1
        self.segment2.eventarrays = self.eventarr2

        self.segment1.irregularlysampledsignals = self.irsig1
        self.segment2.irregularlysampledsignals = self.irsig2

        self.segment1.spikes = self.spike1
        self.segment2.spikes = self.spike2

        self.segment1.spiketrains = self.train1
        self.segment2.spiketrains = self.train2

        create_many_to_one_relationship(self.segment1)
        create_many_to_one_relationship(self.segment2)
Esempio n. 4
0
    def test__construct_subsegment_by_unit(self):
        nb_seg = 3
        nb_unit = 7
        unit_with_sig = np.array([0, 2, 5])
        signal_types = ['Vm', 'Conductances']
        sig_len = 100

        # channelindexes
        chxs = [ChannelIndex(name='Vm',
                             index=unit_with_sig),
                ChannelIndex(name='Conductance',
                             index=unit_with_sig)]

        # Unit
        all_unit = []
        for u in range(nb_unit):
            un = Unit(name='Unit #%d' % u, channel_indexes=np.array([u]))
            assert_neo_object_is_compliant(un)
            all_unit.append(un)

        blk = Block()
        blk.channel_indexes = chxs
        for s in range(nb_seg):
            seg = Segment(name='Simulation %s' % s)
            for j in range(nb_unit):
                st = SpikeTrain([1, 2], units='ms',
                                t_start=0., t_stop=10)
                st.unit = all_unit[j]

            for t in signal_types:
                anasigarr = AnalogSignal(np.zeros((sig_len,
                                                   len(unit_with_sig))),
                                         units='nA',
                                         sampling_rate=1000. * pq.Hz,
                                         channel_indexes=unit_with_sig)
                seg.analogsignals.append(anasigarr)

        blk.create_many_to_one_relationship()
        for unit in all_unit:
            assert_neo_object_is_compliant(unit)
        for chx in chxs:
            assert_neo_object_is_compliant(chx)
        assert_neo_object_is_compliant(blk)

        # what you want
        newseg = seg.construct_subsegment_by_unit(all_unit[:4])
        assert_neo_object_is_compliant(newseg)
Esempio n. 5
0
 def setUp(self):
     test_data = np.random.rand(100, 8) * pq.mV
     channel_names = np.array(["a", "b", "c", "d", "e", "f", "g", "h"])
     self.test_signal = AnalogSignal(test_data,
                                     sampling_period=0.1 * pq.ms,
                                     name="test signal",
                                     description="this is a test signal",
                                     array_annotations={"channel_names": channel_names},
                                     attUQoLtUaE=42)
     self.test_view = ChannelView(self.test_signal, [1, 2, 5, 7],
                           name="view of test signal",
                           description="this is a view of a test signal",
                           array_annotations={"something": np.array(["A", "B", "C", "D"])},
                           sLaTfat="fish")
     self.test_spiketrains = [SpikeTrain(np.arange(100.0), units="ms", t_stop=200),
                              SpikeTrain(np.arange(0.5, 100.5), units="ms", t_stop=200)]
     self.test_segment = Segment()
     self.test_segment.analogsignals.append(self.test_signal)
     self.test_segment.spiketrains.extend(self.test_spiketrains)
Esempio n. 6
0
 def test_init(self):
     seg = Segment(name='a segment', index=3)
     assert_neo_object_is_compliant(seg)
     self.assertEqual(seg.name, 'a segment')
     self.assertEqual(seg.file_origin, None)
     self.assertEqual(seg.index, 3)
Esempio n. 7
0
    def setUp(self):
        unitname11 = 'unit 1 1'
        unitname12 = 'unit 1 2'
        unitname21 = 'unit 2 1'
        unitname22 = 'unit 2 2'

        channame11 = 'chan 1 1'
        channame12 = 'chan 1 2'
        channame21 = 'chan 2 1'
        channame22 = 'chan 2 2'

        segname11 = 'seg 1 1'
        segname12 = 'seg 1 2'
        segname21 = 'seg 2 1'
        segname22 = 'seg 2 2'

        self.rcgname1 = 'rcg 1'
        self.rcgname2 = 'rcg 2'
        self.rcgnames = [self.rcgname1, self.rcgname2]

        self.unitnames1 = [unitname11, unitname12]
        self.unitnames2 = [unitname21, unitname22, unitname11]
        self.unitnames = [unitname11, unitname12, unitname21, unitname22]

        self.channames1 = [channame11, channame12]
        self.channames2 = [channame21, channame22, channame11]
        self.channames = [channame11, channame12, channame21, channame22]

        self.segnames1 = [segname11, segname12]
        self.segnames2 = [segname21, segname22, segname11]
        self.segnames = [segname11, segname12, segname21, segname22]

        unit11 = Unit(name=unitname11)
        unit12 = Unit(name=unitname12)
        unit21 = Unit(name=unitname21)
        unit22 = Unit(name=unitname22)
        unit23 = unit11

        chan11 = RecordingChannel(name=channame11)
        chan12 = RecordingChannel(name=channame12)
        chan21 = RecordingChannel(name=channame21)
        chan22 = RecordingChannel(name=channame22)
        chan23 = chan11

        seg11 = Segment(name=segname11)
        seg12 = Segment(name=segname12)
        seg21 = Segment(name=segname21)
        seg22 = Segment(name=segname22)
        seg23 = seg11

        self.units1 = [unit11, unit12]
        self.units2 = [unit21, unit22, unit23]
        self.units = [unit11, unit12, unit21, unit22]

        self.chan1 = [chan11, chan12]
        self.chan2 = [chan21, chan22, chan23]
        self.chan = [chan11, chan12, chan21, chan22]

        self.seg1 = [seg11, seg12]
        self.seg2 = [seg21, seg22, seg23]
        self.seg = [seg11, seg12, seg21, seg22]

        self.rcg1 = RecordingChannelGroup(name=self.rcgname1)
        self.rcg2 = RecordingChannelGroup(name=self.rcgname2)
        self.rcg = [self.rcg1, self.rcg2]

        self.rcg1.units = self.units1
        self.rcg2.units = self.units2
        self.rcg1.recordingchannels = self.chan1
        self.rcg2.recordingchannels = self.chan2
Esempio n. 8
0
    def test__time_slice(self):
        time_slice = [.5, 5.6] * pq.s

        epoch2 = Epoch([0.6, 9.5, 16.8, 34.1] * pq.s,
                       durations=[4.5, 4.8, 5.0, 5.0] * pq.s,
                       t_start=.1 * pq.s)
        epoch2.annotate(epoch_type='b')
        epoch2.array_annotate(trial_id=[1, 2, 3, 4])

        event = Event(times=[0.5, 10.0, 25.2] * pq.s, t_start=.1 * pq.s)
        event.annotate(event_type='trial start')
        event.array_annotate(trial_id=[1, 2, 3])

        anasig = AnalogSignal(np.arange(50.0) * pq.mV,
                              t_start=.1 * pq.s,
                              sampling_rate=1.0 * pq.Hz)
        irrsig = IrregularlySampledSignal(signal=np.arange(50.0) * pq.mV,
                                          times=anasig.times,
                                          t_start=.1 * pq.s)
        st = SpikeTrain(
            np.arange(0.5, 50, 7) * pq.s,
            t_start=.1 * pq.s,
            t_stop=50.0 * pq.s,
            waveforms=np.array(
                [[[0., 1.], [0.1, 1.1]], [[2., 3.], [2.1, 3.1]],
                 [[4., 5.], [4.1, 5.1]], [[6., 7.], [6.1, 7.1]],
                 [[8., 9.], [8.1, 9.1]], [[12., 13.], [12.1, 13.1]],
                 [[14., 15.], [14.1, 15.1]], [[16., 17.], [16.1, 17.1]]]) *
            pq.mV,
            array_annotations={'spikenum': np.arange(1, 9)})

        seg = Segment()
        seg.epochs = [epoch2]
        seg.events = [event]
        seg.analogsignals = [anasig]
        seg.irregularlysampledsignals = [irrsig]
        seg.spiketrains = [st]

        block = Block()
        block.segments = [seg]
        block.create_many_to_one_relationship()

        # test without resetting the time
        sliced = seg.time_slice(time_slice[0], time_slice[1])

        assert_neo_object_is_compliant(sliced)

        self.assertEqual(len(sliced.events), 1)
        self.assertEqual(len(sliced.spiketrains), 1)
        self.assertEqual(len(sliced.analogsignals), 1)
        self.assertEqual(len(sliced.irregularlysampledsignals), 1)
        self.assertEqual(len(sliced.epochs), 1)

        assert_same_attributes(
            sliced.spiketrains[0],
            st.time_slice(t_start=time_slice[0], t_stop=time_slice[1]))
        assert_same_attributes(
            sliced.analogsignals[0],
            anasig.time_slice(t_start=time_slice[0], t_stop=time_slice[1]))
        assert_same_attributes(
            sliced.irregularlysampledsignals[0],
            irrsig.time_slice(t_start=time_slice[0], t_stop=time_slice[1]))
        assert_same_attributes(
            sliced.events[0],
            event.time_slice(t_start=time_slice[0], t_stop=time_slice[1]))
        assert_same_attributes(
            sliced.epochs[0],
            epoch2.time_slice(t_start=time_slice[0], t_stop=time_slice[1]))

        seg = Segment()
        seg.epochs = [epoch2]
        seg.events = [event]
        seg.analogsignals = [anasig]
        seg.irregularlysampledsignals = [irrsig]
        seg.spiketrains = [st]

        block = Block()
        block.segments = [seg]
        block.create_many_to_one_relationship()

        # test with resetting the time
        sliced = seg.time_slice(time_slice[0], time_slice[1], reset_time=True)

        assert_neo_object_is_compliant(sliced)

        self.assertEqual(len(sliced.events), 1)
        self.assertEqual(len(sliced.spiketrains), 1)
        self.assertEqual(len(sliced.analogsignals), 1)
        self.assertEqual(len(sliced.irregularlysampledsignals), 1)
        self.assertEqual(len(sliced.epochs), 1)

        assert_same_attributes(
            sliced.spiketrains[0],
            st.time_shift(-time_slice[0]).time_slice(t_start=0 * pq.s,
                                                     t_stop=time_slice[1] -
                                                     time_slice[0]))

        anasig_target = anasig.copy()
        anasig_target = anasig_target.time_shift(-time_slice[0]).time_slice(
            t_start=0 * pq.s, t_stop=time_slice[1] - time_slice[0])
        assert_same_attributes(sliced.analogsignals[0], anasig_target)
        irrsig_target = irrsig.copy()
        irrsig_target = irrsig_target.time_shift(-time_slice[0]).time_slice(
            t_start=0 * pq.s, t_stop=time_slice[1] - time_slice[0])
        assert_same_attributes(sliced.irregularlysampledsignals[0],
                               irrsig_target)
        assert_same_attributes(
            sliced.events[0],
            event.time_shift(-time_slice[0]).time_slice(t_start=0 * pq.s,
                                                        t_stop=time_slice[1] -
                                                        time_slice[0]))
        assert_same_attributes(
            sliced.epochs[0],
            epoch2.time_shift(-time_slice[0]).time_slice(t_start=0 * pq.s,
                                                         t_stop=time_slice[1] -
                                                         time_slice[0]))

        seg = Segment()

        reader = ExampleRawIO(filename='my_filename.fake')
        reader.parse_header()

        proxy_anasig = AnalogSignalProxy(rawio=reader,
                                         stream_index=0,
                                         inner_stream_channels=None,
                                         block_index=0,
                                         seg_index=0)
        seg.analogsignals.append(proxy_anasig)

        proxy_st = SpikeTrainProxy(rawio=reader,
                                   spike_channel_index=0,
                                   block_index=0,
                                   seg_index=0)
        seg.spiketrains.append(proxy_st)

        proxy_event = EventProxy(rawio=reader,
                                 event_channel_index=0,
                                 block_index=0,
                                 seg_index=0)
        seg.events.append(proxy_event)

        proxy_epoch = EpochProxy(rawio=reader,
                                 event_channel_index=1,
                                 block_index=0,
                                 seg_index=0)
        proxy_epoch.annotate(pick='me')
        seg.epochs.append(proxy_epoch)

        loaded_epoch = proxy_epoch.load()
        loaded_event = proxy_event.load()
        loaded_st = proxy_st.load()
        loaded_anasig = proxy_anasig.load()

        block = Block()
        block.segments = [seg]
        block.create_many_to_one_relationship()

        # test with proxy objects
        sliced = seg.time_slice(time_slice[0], time_slice[1])

        assert_neo_object_is_compliant(sliced)

        sliced_event = loaded_event.time_slice(t_start=time_slice[0],
                                               t_stop=time_slice[1])
        has_event = len(sliced_event) > 0

        sliced_anasig = loaded_anasig.time_slice(t_start=time_slice[0],
                                                 t_stop=time_slice[1])

        sliced_st = loaded_st.time_slice(t_start=time_slice[0],
                                         t_stop=time_slice[1])

        self.assertEqual(len(sliced.events), int(has_event))
        self.assertEqual(len(sliced.spiketrains), 1)
        self.assertEqual(len(sliced.analogsignals), 1)

        self.assertTrue(isinstance(sliced.spiketrains[0], SpikeTrain))
        assert_same_attributes(sliced.spiketrains[0], sliced_st)

        self.assertTrue(isinstance(sliced.analogsignals[0], AnalogSignal))
        assert_same_attributes(sliced.analogsignals[0], sliced_anasig)

        if has_event:
            self.assertTrue(isinstance(sliced.events[0], Event))
            assert_same_attributes(sliced.events[0], sliced_event)
Esempio n. 9
0
    def test_times(self):
        for seg in [self.seg1, self.seg2]:
            # calculate target values for t_start and t_stop
            t_starts, t_stops = [], []
            for children in [
                    seg.analogsignals, seg.epochs, seg.events,
                    seg.irregularlysampledsignals, seg.spiketrains
            ]:
                for child in children:
                    if hasattr(child, 't_start'):
                        t_starts.append(child.t_start)
                    if hasattr(child, 't_stop'):
                        t_stops.append(child.t_stop)
                    if hasattr(child, 'time'):
                        t_starts.append(child.time)
                        t_stops.append(child.time)
                    if hasattr(child, 'times'):
                        t_starts.append(child.times[0])
                        t_stops.append(child.times[-1])
            targ_t_start = min(t_starts)
            targ_t_stop = max(t_stops)

            self.assertEqual(seg.t_start, targ_t_start)
            self.assertEqual(seg.t_stop, targ_t_stop)

        # Testing times with ProxyObjects
        seg = Segment()
        reader = ExampleRawIO(filename='my_filename.fake')
        reader.parse_header()

        proxy_anasig = AnalogSignalProxy(rawio=reader,
                                         stream_index=0,
                                         inner_stream_channels=None,
                                         block_index=0,
                                         seg_index=0)
        seg.analogsignals.append(proxy_anasig)

        proxy_st = SpikeTrainProxy(rawio=reader,
                                   spike_channel_index=0,
                                   block_index=0,
                                   seg_index=0)
        seg.spiketrains.append(proxy_st)

        proxy_event = EventProxy(rawio=reader,
                                 event_channel_index=0,
                                 block_index=0,
                                 seg_index=0)
        seg.events.append(proxy_event)

        proxy_epoch = EpochProxy(rawio=reader,
                                 event_channel_index=1,
                                 block_index=0,
                                 seg_index=0)
        seg.epochs.append(proxy_epoch)

        t_starts, t_stops = [], []
        for children in [
                seg.analogsignals, seg.epochs, seg.events,
                seg.irregularlysampledsignals, seg.spiketrains
        ]:
            for child in children:
                if hasattr(child, 't_start'):
                    t_starts.append(child.t_start)
                if hasattr(child, 't_stop'):
                    t_stops.append(child.t_stop)
                if hasattr(child, 'time'):
                    t_starts.append(child.time)
                    t_stops.append(child.time)
                if hasattr(child, 'times'):
                    t_starts.append(child.times[0])
                    t_stops.append(child.times[-1])
        targ_t_start = min(t_starts)
        targ_t_stop = max(t_stops)

        self.assertEqual(seg.t_start, targ_t_start)
        self.assertEqual(seg.t_stop, targ_t_stop)
Esempio n. 10
0
 def test_init(self):
     seg = Segment(name='a segment', index=3)
     self.assertEqual(seg.name, 'a segment')
     self.assertEqual(seg.file_origin, None)
     self.assertEqual(seg.index, 3)
Esempio n. 11
0
def merge_datastores(
    datastores,
    root_directory,
    merge_recordings=True,
    merge_analysis=True,
    merge_stimuli=True,
    replace=False,
):
    """
    This function takes a tuple of datastore in input and merge them into one single datastore which will be saved in root_directory.
    The type of data that should be merged can be controlled through the merge_recordings, merge_analysis and merge_stimuli booleans
    It returns this datastore as a Datastore object.
    """
    merged_datastore = PickledDataStore(
        load=False,
        parameters=ParameterSet({
            "root_directory": root_directory,
            "store_stimuli": merge_stimuli
        }),
        replace=replace,
    )
    j = 0

    # Here we check if sheets and neurons are the same in all datastores
    assert compare_sheets_datastores(
        datastores), "All datastores should contain the same sheets"
    assert compare_neurons_ids_datastores(
        datastores), "Neurons in the datastores should have the same ids"
    assert compare_neurons_position_datastores(
        datastores), "Neurons in the datastores should have the same position"
    assert compare_neurons_annotations_datastores(
        datastores
    ), "Neurons in the datastores should have the same annotations"

    if not os.path.isdir(root_directory):
        os.makedirs(root_directory)

    # Change the block annotations so that it gets the merged version of the experiment parameters
    merged_datastore.block.annotations = datastores[0].block.annotations
    merged_datastore.block.annotations[
        "experiment_parameters"] = merge_experiment_parameters_datastores(
            datastores)

    j = 0
    for datastore in datastores:

        # Merge the recording of all the datastores if this flag is set to true
        if merge_recordings:
            segments = datastore.get_segments()
            segments += datastore.get_segments(null=True)
            for seg in segments:
                for s in merged_datastore.get_segments():
                    if seg.annotations == s.annotations and seg.null == s.null:
                        print(
                            "Warning: A segment with the same parametrization was already added in the datastore.: %s"
                            % (seg.annotations))
                        raise ValueError(
                            "A segment with the same parametrization was already added in the datastore already added in the datastore. Currently uniqueness is required. User should check what caused this and modify his simulations  to avoid this!: %s \n %s"
                            % (str(seg.annotations), str(s.annotations)))

                # Load the full segment and adds it to the merged datastore
                if not seg.full:
                    seg.load_full()
                merged_datastore.block.segments.append(
                    PickledDataStoreNeoWrapper(seg,
                                               "Segment" + str(j),
                                               root_directory,
                                               null=seg.null))
                merged_datastore.stimulus_dict[
                    seg.annotations["stimulus"]] = True

                # Create a new pickle file for this mozaik segment and store a corresponding neo segment there
                f = open(root_directory + "/" + "Segment" + str(j) + ".pickle",
                         "wb")
                s = Segment(description=seg.description,
                            file_origin=seg.file_origin,
                            file_datetime=seg.file_datetime,
                            rec_datetime=seg.rec_datetime,
                            index=seg.index,
                            **seg.annotations)
                s.spiketrains = seg.spiketrains
                s.analogsignals = seg.analogsignals
                pickle.dump(s, f)

                # Release each segment once it has been added to the merged datastore to save memory
                seg.release()
                j = j + 1

        # Merge the analysis of all the datastores if this flag is set to true
        if merge_analysis:
            adss = datastore.get_analysis_result()
            for ads in adss:
                merged_datastore.add_analysis_result(ads)

        # Merge the stimuli all the datastores if this flag is set to true
        if merge_stimuli:
            for key, value in datastore.sensory_stimulus.items():
                merged_datastore.sensory_stimulus[key] = value

    return merged_datastore