def _display(self, window, stim):

        # display frames
        time_stamp = []
        start_time = time.clock()
        singleRunFrames = self.sequence.shape[0]

        if self.is_sync_pulse:
            syncPulseTask = iodaq.DigitalOutput(self.sync_pulse_NI_dev,
                                                self.sync_pulse_NI_port,
                                                self.sync_pulse_NI_line)
            syncPulseTask.StartTask()
            _ = syncPulseTask.write(np.array([0]).astype(np.uint8))

        i = 0

        while self.keep_display and i < (singleRunFrames * self.display_iter):

            frame_num = i % singleRunFrames

            # obsolete
            # if self.display_order == 1:
            #     # Then display sequence in order
            #      frame_num = i % singleRunFrames
            #
            # if self.display_order == -1:
            #     # then display sequence backwards
            #      frame_num = singleRunFrames - (i % singleRunFrames) -1

            stim.setImage(self.sequence[frame_num][::-1])
            stim.draw()
            time_stamp.append(time.clock() - start_time)

            # set syncPuls signal
            if self.is_sync_pulse:
                _ = syncPulseTask.write(np.array([1]).astype(np.uint8))

            # show visual stim
            window.flip()
            self.displayed_frames.append(
                self.seq_log['stimulation']['frames'][frame_num])

            # set syncPuls signal
            if self.is_sync_pulse:
                _ = syncPulseTask.write(np.array([0]).astype(np.uint8))

            self._update_display_status()
            i = i + 1

        stop_time = time.clock()
        window.close()

        if self.is_sync_pulse:
            syncPulseTask.StopTask()

        self.time_stamp = np.array(time_stamp)
        self.display_length = stop_time - start_time

        if self.keep_display == True:
            print '\nDisplay successfully completed.'
Exemplo n.º 2
0
    def _display(self, window, stim):
        """
        display stimulus
        """
        frame_ts_start = []
        frame_ts_end = []
        start_time = time.clock()

        if self.is_by_index:
            index_to_display = self.seq_log['stimulation']['index_to_display']
            iter_frame_num = len(index_to_display)
        else:
            iter_frame_num = self.sequence.shape[0]
            index_to_display = range(iter_frame_num)

        # print('frame per iter: {}'.format(iter_frame_num))

        if self.is_sync_pulse:
            syncPulseTask = iodaq.DigitalOutput(self.sync_pulse_NI_dev,
                                                self.sync_pulse_NI_port,
                                                self.sync_pulse_NI_line)
            syncPulseTask.StartTask()
            _ = syncPulseTask.write(np.array([0]).astype(np.uint8))

        i = 0
        self.displayed_frames = []

        while self.keep_display and i < (iter_frame_num * self.display_iter):

            frame_num = i % iter_frame_num
            frame_index = int(index_to_display[frame_num])

            #print('i:{}; index_display_ind:{}; frame_ind{}.'.format(i, frame_num, frame_index))

            if self.color_weights == (1., 1., 1.):
                stim.setImage(self.sequence[frame_index][::-1])
            else:
                curr_frame = self.sequence[frame_index]
                curr_frame = ((curr_frame + 1.) * 255 / 2.)
                curr_frame_r = PIL.Image.fromarray((curr_frame * self.color_weights[0]).astype(np.uint8))
                curr_frame_g = PIL.Image.fromarray((curr_frame * self.color_weights[1]).astype(np.uint8))
                curr_frame_b = PIL.Image.fromarray((curr_frame * self.color_weights[2]).astype(np.uint8))
                curr_frame = PIL.Image.merge('RGB', (curr_frame_r, curr_frame_g, curr_frame_b))
                # plt.imshow(curr_frame)
                # plt.show()
                stim.setImage(curr_frame)

            stim.draw()

            # set sync pulse start signal
            if self.is_sync_pulse:
                _ = syncPulseTask.write(np.array([1]).astype(np.uint8))

            # save frame start timestamp
            frame_ts_start.append(time.clock() - start_time)

            # show visual stim
            window.flip()

            # save displayed frame information
            if self.is_by_index:
                self.displayed_frames.append(self.seq_log['stimulation']['frames_unique'][frame_index])
            else:
                self.displayed_frames.append(self.seq_log['stimulation']['frames'][frame_index])

            # save frame end timestamp
            frame_ts_end.append(time.clock() - start_time)

            # set sync pulse end signal
            if self.is_sync_pulse:
                _ = syncPulseTask.write(np.array([0]).astype(np.uint8))

            self._update_display_status()
            i += 1

        stop_time = time.clock()
        window.close()

        if self.is_sync_pulse:
            syncPulseTask.StopTask()

        self.frame_ts_start = np.array(frame_ts_start)
        self.frame_ts_end = np.array(frame_ts_end)
        self.display_length = stop_time - start_time

        if self.keep_display == True:
            print('\nDisplay successfully completed.')
    def _display_by_index(self, window, stim):
        """ display by index routine for simpler stim routines """

        # display frames by index
        time_stamps = []
        start_time = time.clock()
        index_to_display = self.seq_log['stimulation']['index_to_display']
        num_iters = len(index_to_display)

        # print 'frame per iter:', num_iters

        if self.is_sync_pulse:
            syncPulseTask = iodaq.DigitalOutput(self.sync_pulse_NI_dev,
                                                self.sync_pulse_NI_port,
                                                self.sync_pulse_NI_line)
            syncPulseTask.StartTask()
            _ = syncPulseTask.write(np.array([0]).astype(np.uint8))

        i = 0
        self.displayed_frames = []

        while self.keep_display and i < (num_iters * self.display_iter):

            frame_num = i % num_iters

            # obsolete
            # if self.display_order == 1:
            #     # Then display sequence in order
            #      frame_num = i % num_iters
            #
            # if self.display_order == -1:
            #     # Then display sequence backwards
            #      frame_num = num_iters - (i % num_iters) -1

            frame_index = index_to_display[frame_num]

            # print 'i:', i, '; index_display_ind:', frame_num, '; frame_ind:', frame_index

            stim.setImage(self.sequence[frame_index][::-1])
            stim.draw()
            time_stamps.append(time.clock() - start_time)

            # set syncPuls signal
            if self.is_sync_pulse:
                _ = syncPulseTask.write(np.array([1]).astype(np.uint8))

            # show visual stim
            window.flip()
            self.displayed_frames.append(
                self.seq_log['stimulation']['frames_unique'][frame_index])

            # set syncPuls signal
            if self.is_sync_pulse:
                _ = syncPulseTask.write(np.array([0]).astype(np.uint8))

            self._update_display_status()
            i += 1

        stop_time = time.clock()
        window.close()

        if self.is_sync_pulse:
            syncPulseTask.StopTask()

        self.time_stamp = np.array(time_stamps)
        self.display_length = stop_time - start_time

        if self.keep_display == True:
            print '\nDisplay successfully completed.'