def test__get_events(self): starts_1 = Event(times=[0.5, 10.0, 25.2] * pq.s) starts_1.annotate(event_type='trial start', pick='me') starts_1.array_annotate(trial_id=[1, 2, 3]) stops_1 = Event(times=[5.5, 14.9, 30.1] * pq.s) stops_1.annotate(event_type='trial stop') stops_1.array_annotate(trial_id=[1, 2, 3]) proxy_event = EventProxy(rawio=self.reader, event_channel_index=0, block_index=0, seg_index=0) proxy_event.annotate(event_type='trial start') seg = Segment() seg.events = [starts_1, stops_1, proxy_event] # test getting multiple events including a proxy extracted_starts = get_events(seg, event_type='trial start') self.assertEqual(len(extracted_starts), 2) # make sure the event is loaded and a neo.Event object is returned self.assertTrue(isinstance(extracted_starts[0], Event)) self.assertTrue(isinstance(extracted_starts[1], Event))
def test__get_events(self): starts_1 = Event(times=[0.5, 10.0, 25.2] * pq.s) starts_1.annotate(event_type='trial start', pick='me') starts_1.array_annotate(trial_id=[1, 2, 3]) stops_1 = Event(times=[5.5, 14.9, 30.1] * pq.s) stops_1.annotate(event_type='trial stop') stops_1.array_annotate(trial_id=[1, 2, 3]) starts_2 = Event(times=[33.2, 41.7, 52.4] * pq.s) starts_2.annotate(event_type='trial start') starts_2.array_annotate(trial_id=[4, 5, 6]) stops_2 = Event(times=[37.6, 46.1, 57.0] * pq.s) stops_2.annotate(event_type='trial stop') stops_2.array_annotate(trial_id=[4, 5, 6]) seg = Segment() seg2 = Segment() seg.events = [starts_1, stops_1] seg2.events = [starts_2, stops_2] block = Block() block.segments = [seg, seg2] # test getting one whole event via annotation extracted_starts1 = get_events(seg, event_type='trial start') extracted_starts1b = get_events(block, pick='me') self.assertEqual(len(extracted_starts1), 1) self.assertEqual(len(extracted_starts1b), 1) extracted_starts1 = extracted_starts1[0] extracted_starts1b = extracted_starts1b[0] assert_same_attributes(extracted_starts1, starts_1) assert_same_attributes(extracted_starts1b, starts_1) # test getting an empty list by searching for a non-existent property empty1 = get_events(seg, foo='bar') self.assertEqual(len(empty1), 0) # test getting an empty list by searching for a non-existent property value empty2 = get_events(seg, event_type='undefined') self.assertEqual(len(empty2), 0) # test getting only one event time of one event trial_2 = get_events(block, trial_id=2, event_type='trial start') self.assertEqual(len(trial_2), 1) trial_2 = trial_2[0] self.assertEqual(starts_1.name, trial_2.name) self.assertEqual(starts_1.description, trial_2.description) self.assertEqual(starts_1.file_origin, trial_2.file_origin) self.assertEqual(starts_1.annotations['event_type'], trial_2.annotations['event_type']) assert_arrays_equal(trial_2.array_annotations['trial_id'], np.array([2])) self.assertIsInstance(trial_2.array_annotations, ArrayDict) # test getting only one event time of more than one event trial_2b = get_events(block, trial_id=2) self.assertEqual(len(trial_2b), 2) start_idx = np.where( np.array([ev.annotations['event_type'] for ev in trial_2b]) == 'trial start')[0][0] trial_2b_start = trial_2b[start_idx] trial_2b_stop = trial_2b[start_idx - 1] assert_same_attributes(trial_2b_start, trial_2) self.assertEqual(stops_1.name, trial_2b_stop.name) self.assertEqual(stops_1.description, trial_2b_stop.description) self.assertEqual(stops_1.file_origin, trial_2b_stop.file_origin) self.assertEqual(stops_1.annotations['event_type'], trial_2b_stop.annotations['event_type']) assert_arrays_equal(trial_2b_stop.array_annotations['trial_id'], np.array([2])) self.assertIsInstance(trial_2b_stop.array_annotations, ArrayDict) # test getting more than one event time of one event trials_1_2 = get_events(block, trial_id=[1, 2], event_type='trial start') self.assertEqual(len(trials_1_2), 1) trials_1_2 = trials_1_2[0] self.assertEqual(starts_1.name, trials_1_2.name) self.assertEqual(starts_1.description, trials_1_2.description) self.assertEqual(starts_1.file_origin, trials_1_2.file_origin) self.assertEqual(starts_1.annotations['event_type'], trials_1_2.annotations['event_type']) assert_arrays_equal(trials_1_2.array_annotations['trial_id'], np.array([1, 2])) self.assertIsInstance(trials_1_2.array_annotations, ArrayDict) # test getting more than one event time of more than one event trials_1_2b = get_events(block, trial_id=[1, 2]) self.assertEqual(len(trials_1_2b), 2) start_idx = np.where( np.array([ev.annotations['event_type'] for ev in trials_1_2b]) == 'trial start')[0][0] trials_1_2b_start = trials_1_2b[start_idx] trials_1_2b_stop = trials_1_2b[start_idx - 1] assert_same_attributes(trials_1_2b_start, trials_1_2) self.assertEqual(stops_1.name, trials_1_2b_stop.name) self.assertEqual(stops_1.description, trials_1_2b_stop.description) self.assertEqual(stops_1.file_origin, trials_1_2b_stop.file_origin) self.assertEqual(stops_1.annotations['event_type'], trials_1_2b_stop.annotations['event_type']) assert_arrays_equal(trials_1_2b_stop.array_annotations['trial_id'], np.array([1, 2])) self.assertIsInstance(trials_1_2b_stop.array_annotations, ArrayDict)
pow_wavelet = np.abs(sig_wavelet) ** 2 pow_normed = pow_wavelet / pow_wavelet.mean() transformed_anasig = anasig.duplicate_with_new_data(pow_normed) transformed_anasig.name = "normed_wavelet_transformed_lfp" del transformed_anasig.annotations['nix_name'] for key, val in anasig.array_annotations.items(): if len(val): transformed_anasig.annotations[key] = val[channel_idx] anasig.segment.analogsignals.append(transformed_anasig) seg.create_relationship() trigger_event = 'target_02_on' trigger_events = utils.get_events(container=seg, name='DecodedEvents', labels=trigger_event, in_successful_trial=True, # trial_protocol=trial_protocol )[0] epoch = utils.add_epoch( seg, event1=trigger_events, event2=None, pre=pre, post=post, attach_result=False, name='analysis_epochs') target_02_on_segs = utils.cut_segment_by_epoch( seg=seg, epoch=epoch, reset_time=True)