def apply(self, stim): data = self._stft(stim) events = [] time_bins = np.arange(0., stim.duration - self.frame_size, self.hop_size) if isinstance(self.freq_bins, int): bins = [] bin_size = data.shape[1] / self.freq_bins for i in range(self.freq_bins): bins.append((i * bin_size, (i + 1) * bin_size)) self.freq_bins = bins for i, tb in enumerate(time_bins): ev = Event(onset=tb, duration=self.frame_size) value_data = {} for fb in self.freq_bins: label = '%d_%d' % fb start, stop = fb val = data[i, start:stop].mean() if np.isinf(val): val = 0. value_data[label] = val ev.add_value(Value(stim, self, value_data)) events.append(ev) return events
def apply(self, stim): data = self._stft(stim) events = [] time_bins = np.arange(0., stim.duration-self.frame_size, self.hop_size) if isinstance(self.freq_bins, int): bins = [] bin_size = data.shape[1] / self.freq_bins for i in range(self.freq_bins): bins.append((i*bin_size, (i+1)*bin_size)) self.freq_bins = bins for i, tb in enumerate(time_bins): ev = Event(onset=tb, duration=self.frame_size) value_data = {} for fb in self.freq_bins: label = '%d_%d' % fb start, stop = fb val = data[i, start:stop].mean() if np.isinf(val): val = 0. value_data[label] = val ev.add_value(Value(stim, self, value_data)) events.append(ev) return events
def apply(self, stim): events = [] time_bins = np.arange(0., stim.duration, 1.) for i, tb in enumerate(time_bins): ev = Event(onset=tb, duration=1000) ev.add_value(Value(stim, self, {'second': i})) return events
def apply(self, stim, show=False): events = [] for i, f in enumerate(stim): img = f.data img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) if i == 0: last_frame = img total_flow = 0 flow = cv2.calcOpticalFlowFarneback( last_frame, img, 0.5, 3, 15, 3, 5, 1.2, 0) flow = np.sqrt((flow ** 2).sum(2)) if show: cv2.imshow('frame', flow.astype('int8')) cv2.waitKey(1) last_frame = img total_flow = flow.sum() value = Value(stim, self, {'total_flow': total_flow}) event = Event(onset=f.onset, duration=f.duration, values=[value]) events.append(event) return events
def extract(self, extractors, merge_events=True, **kwargs): period = 1. / self.fps timeline = Timeline(period=period) for ext in extractors: # For VideoExtractors, pass the entire stim if ext.target.__name__ == self.__class__.__name__: events = ext.apply(self, **kwargs) for ev in events: timeline.add_event(ev, merge=merge_events) # Otherwise, for images, loop over frames else: c = 0 for frame in self: if frame.data is not None: event = Event(onset=c * period) event.add_value(ext.apply(frame)) timeline.add_event(event, merge=merge_events) c += 1 return timeline
def test_dummy_code_timeline(self): data = [{'A': 12.0, 'B': 'abc'}, { 'A': 7, 'B': 'def'}, { 'C': 40 }] events = [Event(values=[Value(None, None, x)], duration=1) for x in data] tl = Timeline(events=events, period=1) self.assertEqual(tl.to_df().shape, (5, 4)) tl_dummy = tl.dummy_code() self.assertEqual(tl_dummy.to_df().shape, (7, 4)) tl = Timeline(events=events, period=1) tl_dummy = tl.dummy_code(string_only=False) self.assertEqual(tl_dummy.to_df().shape, (9, 4))
def apply(self, stim): amps = stim.data sampling_rate = stim.sampling_rate elements = stim.transcription.elements events = [] for i, el in enumerate(elements): onset = sampling_rate * el.onset duration = sampling_rate * el.duration r_onset = np.round(onset).astype(int) r_offset = np.round(onset + duration).astype(int) if not r_offset <= amps.shape[0]: raise Exception('Block ends after data.') mean_amplitude = np.mean(amps[r_onset:r_offset]) amplitude_data = {'mean_amplitude': mean_amplitude} ev = Event(onset=onset, duration=duration) ev.add_value(Value(stim, self, amplitude_data)) events.append(ev) return events
def apply(self, text): tokens = [token.text for token in text] scores = self.model(tokens) events = [] for i, w in enumerate(text): if type(scores[i]) == float: values = [Value(text, self, {self.name: scores[i]})] elif type(scores[i]) == dict: values = [] for k in scores[i].keys(): values.append( Value(text, self, {self.name + '_' + k: scores[i][k]})) # parse dictionary into list if getting dict back event = Event(onset=w.onset, duration=w.duration, values=values) events.append(event) return events
def extract(self, extractors, merge_events=True): timeline = Timeline() # Extractors can either take ComplexTextStim input, in which case we # pass the current instance, or TextStim input, in which case we loop # over all elements. for ext in extractors: if ext.target.__name__ == self.__class__.__name__: events = ext.apply(self) for ev in events: timeline.add_event(ev, merge=merge_events) else: for elem in self.elements: # If no onset is available, index with order onset = elem.onset or elem.order event = Event(onset=onset, values=[ext.apply(elem)]) timeline.add_event(event, merge=merge_events) return timeline