def trials(self): from psychopy.data import TrialHandler # eats some time # blank comes first # flicker comes later conditions = [ Condition(ori, sf, tf, cnt) for ori, sf, tf, cnt in product( self.component.orientations, self.component.sfrequencies, self.component.tfrequencies, self.component.contrasts, ) ] if self.component.blank: logging.msg('has blank condition...') conditions.append(BlankCondition()) if self.component.flicker: logging.msg('has flicker condition...') conditions.append(FlickerCondition()) ts = [ Trial(self, cond, self.component.on_duration, self.interval) for cond in conditions ] return TrialHandler( ts, nReps=self.component.repetition, method=('random' if self.component.randomize else 'sequential'))
def wait(self, stimulus): wtime = self.component.wait_time logging.msg('Clock synchronized. Session starts after %s sec(s)...' % wtime) logging.flush() for countdown in reversed(range(wtime)): stimulus.flip_text( 'Clock synchronized.\nSession starting in %s...' % (countdown+1)) core.wait(1)
def __iter__(self): for trial in self.trials: index = self.trials.thisN logging.msg('Entering trial #%s...' % index) # self.update_trial(trial) # self.trials.addData('on_time', self.clock.getTime()) yield trial.start() # self.trials.addData('off_time', self.clock.getTime()) self.flip_blank()
def __iter__(self): for trial in self.trials: index = self.trials.thisN logging.msg('Entering trial #%s...' % index) self.update_trial(trial) self.trials.addData('on_time', self.clock.getTime()) yield trial.start() self.trials.addData('off_time', self.clock.getTime()) self.flip_blank() # core.wait(self.component.off_duration) self.instance.opacity = 1.0
def __iter__(self): trials = self.trials clock = self.clock.instance clock.reset() for trial in trials: self.update_trial(trial) logging.msg('Entering trial #%s...' % trials.thisN) trials.addData('on_time', clock.getTime()) yield trial.start() trials.addData('off_time', clock.getTime()) self.flip_blank() core.wait(self.component.off_duration) self.instance.opacity = 1.0 if self.should_stop: logging.msg('UserAbortException raised!') raise UserAbortException()
def update_phase(self, trial): if self.should_stop: logging.msg('UserAbortException raised!') raise UserAbortException() now = trial.tick() cont = np.sin(now * self.contrast_factor) opa = np.cos(now * self.opacity_factor) self.instance.contrast = signal.square(cont) opacity = (opa + 1) / 2 max_contrast = self.component.max_contrast opacity = min(opacity, max_contrast) self.instance.opacity = opacity self.instance.draw() self.window.flip() self.clock.flipped()
def synchronize(self, stimulus): logging.msg('Await a signal from Labjack in {} sec...'.format( self.component.timeout)) logging.flush() for i in range(self.component.timeout, 0, -1): timer = CountdownTimer(1) msg = 'Await a signal from LabJack in {} sec...'.format(i) stimulus.flip_text(msg) while timer.getTime() > 0: if event.getKeys('escape'): raise UserAbortException() if self.u3.get_counter(): logging.msg('counter increased') logging.flush() # self.instance.reset_timer() return else: # timeout... raise TimeoutException('Could not catch any signal from LabJack.')
def __exit__(self, type, value, tb): if type is None: self.handler.current.service_done(self) elif type is UserAbortException: logging.msg('User aborted the session.') elif type is TimeoutException: logging.error('Timeout. Session aborted.') elif type is ComponentNotFoundError: logging.error('ComponentNotFoundError: ' + str(value)) elif type is ServiceRuntimeException: trace = '\n'.join(traceback.format_tb(tb)) logging.critical('An error occurred. ({})\n\n{}'.format( value, trace)) else: trace = '\n'.join(traceback.format_tb(tb)) logging.critical('Unknown error occurred. ({}:{})\n\n{}'.format( type, value, trace)) logging.msg('Exiting visual stimulus...') logging.flush() return True
def synchronize(self, stimulus): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind(('0.0.0.0', self.component.port)) s.listen(1) s.settimeout(self.component.timeout) await_msg = 'Await signal for %s sec(s)...' % self.component.timeout logging.msg(await_msg) logging.flush() stimulus.flip_text(await_msg) try: conn, addr = s.accept() # block! try: data = conn.recv(0) # just for faster response except Exception as e: print 'ERROR FROM RECV', str(e) self.wait(stimulus) # now = time.time() # conn.send(str(now)) conn.close() except socket.timeout: raise TimeoutException finally: s.close()
def __enter__(self): clock = logging.clock.MonotonicClock() logging.setDefaultClock(clock) logging.msg('Entering visual stimulus...') logging.flush() return {} # prepared for result
def __enter__(self): win = self.window.instance proj = self.projection.instance try: eyepoint_x = proj.eyepoint[0] except: eyepoint_x = 0.5 self.textstim = TextStim(win, text='') size_overriden = self.component.snp_viewport_override view_size = size_overriden if any(size_overriden) else win.size win_x, win_y = win.size view_x, view_y = view_size afr = 30 # int(win.getActualFrameRate() * 0.5) logging.msg('Fixed frame rate: ' + str(afr)) self.flip_text('Generating stimulus...it may take a few minutes.') try: logging.msg('init movie...') print 'init movie...' width, height = win.monitor.getSizePix() mgen = SweepingNoiseGenerator( max_spat_freq=self.component.snp_max_spat_freq, max_temp_freq=self.component.snp_max_temp_freq, contrast=self.component.snp_contrast, rotation=self.component.snp_rotation, bandfilter=self.component.snp_filter, duration=self.component.snp_duration, bandwidth=self.component.snp_bandwidth, viewwidth=self.component.snp_view_width, imsize=self.component.snp_img_size, binary_threshold=self.component.snp_cont_thresh, framerate=afr, contr_period=self.component.snp_contr_period, screenWidthPix=win_x, screenHeightPix=win_y, viewport_x=view_x, viewport_y=view_y, screenWidthCm=self.window.monitor.component.width, screenHeightCm=self.window.monitor.component.height, screenDistanceCm=self.window.monitor.component.dist, eyepoint_x=eyepoint_x) except Exception as e: print 'got exception', type(e) print e else: print 'generating...' mgen.generate() print 'rotating...' mgen.rotate() print 'viewmasking...' mgen.viewmask() # print 'croping...' # mgen.crop() print 'done!' self.movie = mgen.moviedata print 'shape is', self.movie.shape logging.msg('masking window...') print 'masking window...' # Setting viewport width # screenWidthCm = self.window.monitor.component.width # screenDistanceCm = self.window.monitor.component.dist self.flip_text('Generating stimulus...done!') logging.msg('creating image stim...') print 'creating image stim...' # imagebuffer to play each frame self.instance = ImageStim( win=win, # size = [1440, 900], # prevent stretch # size = [900, 900], # for mbpr with 10cm size=view_size, units='pix', interpolate=True) logging.msg('ImageStim size: ' + str(self.instance.size)) try: logging.msg('getting ISI...') self.interval = self.window.get_isi() except Exception as e: raise ServiceRuntimeException( 'Could not acquire window object. Please try again') return self