Beispiel #1
0
 def __init__(self, eeg, config):
     self.sendPulse = False
     self.EEGTrack = eeg
     self.config = config
     self.lastPulse = timing.now()
     self.pulseCount = 0
     addPollCallback(self.EEGpulseCallback)
Beispiel #2
0
    def configure(self, experiment, version, session_num, subject):
        """Set various experiment options so they can be transmitted to the host
        PC and add poll callbacks.

        TODO: move this functionality into ``__init__``.

        :param str experiment:
        :param version:
        :param session_num:
        :param str subject:

        """
        self.experiment = experiment
        self.version = version
        self.session_num = session_num
        self.subject = subject
        self._configured = True

        addPollCallback(self.socket.update)
        addPollCallback(self.check_connection)
        if self.voice_server is not None:
            addPollCallback(self.check_voice_server)
Beispiel #3
0
 def start_heartbeat(self):
     """Begin sending heartbeat messages to the host PC."""
     self.logger.info("Starting heartbeat...")
     addPollCallback(self.socket.send_heartbeat)
Beispiel #4
0
def run(exp,config):
    """
    Runs through the experiment trial-by-trial.
    """

    #get the state
    state = exp.restoreState()

    target = Text('*')

    # set up session...
    print state.trial
    print config.NUM_TRIALS
    print state.session
    if state.trial >= config.NUM_TRIALS:
        # if all the trials have been run, make a new session
        prepare(exp,config,state.session + 1)
        state = exp.restoreState()

    # set the session number (so PyEPL knows what directory to put the data in)
    exp.setSession(str(state.session) + "_RUN")#state.trial)
    
    # initialize the tracks
    t = Tracks(config)

    # get session specific configuration
    trialconfig = config.sequence(state.trial)

    # check if mic is recording
    t.vid.clear('black')
    soundgood = micTest(2000, 1.0)
    if not soundgood:
        print "mic not working"
        return

    stimSess = waitForYKey("Is this a stim session?\nPress 'y' for yes, any other key for no.")
    if stimSess:
        msg = 'This is a stim session.\n  Pulses will NOT be sent throughout.'
    else:
        msg = 'This is not a stim session.\n  Pulses will be sent throughout.'
    flashStimulus(Text(msg), duration=config.CONFIRMATION_DURATION)
    if not stimSess:
        stimTrial = False
        doSync = False
        removePollCallback(t.pulseControl.EEGpulseCallback)
        t.eeg.startLogging()
        addPollCallback(t.eeg.pulseCallback)

    # present the instructions
    t.vid.clear('black')
    instruct(trialconfig.INTRO_FILE,clk=t.clk)

    while (state.trial < trialconfig.NUM_TRIALS):
        print stimSess
        if stimSess:
            stimTrial = waitForYKey("Is this a stim trial?\nPress 'y' for yes, any other key for no.")
        if stimTrial:
            msg = "Okay this IS a stim trial"
        else:
            msg = "Okay this is NOT a stim trial"
        flashStimulus(Text(msg), duration=config.CONFIRMATION_DURATION)
        t.log.logMessage("TRIAL_%d STIM_%s"%(state.trial, str(stimTrial)),t.clk)
        
        if stimSess:
            doSync = waitForYKey("Would you like to sync?\nPress 'y' for yes, any other key for no.")
        if doSync:
            waitForAnyKey(t.clk, Text("Please plug into EEG RIG.\n\nThen press any key to continue"))
#            sync(t.log, t.pulseControl, t.clk, config, state.trialData[state.trial * trialconfig.NUM_PAIRS])
            sync(t.log, t.eeg, t.clk, config)
        if stimTrial:
            elec = textInput("Electrodes: ", t.vid, t.key, t.clk)
            t.log.logMessage("TRIAL_%d ELECTRODES_%s" % (state.trial, elec), t.clk)
            cur = textInput("Current: ", t.vid, t.key, t.clk)
            t.log.logMessage("TRIAL_%d CURRENT_%s" % (state.trial, cur), t.clk)

            waitForAnyKey(t.clk, Text("Please plug into STIMULATOR.\n\nThen press any key."))
            stimOnOff(t.log, t.pulseControl, t.clk, config, state.trialData[state.trial * trialconfig.NUM_PAIRS], elec, cur)


        waitForAnyKey(t.clk, Text("Press any key to start trial %d." % (state.trial + 1)))
        flashStimulus(Text(""), duration=config.AFTER_STIM_QUESTION)

        ####### STUDY ######
	t.vid.clear("black")

	# Log the start of the study period
	t.log.logMessage("STUDY_START\tTRIAL_%d"%(state.trial),t.clk)
    
	for pair in range(state.trial*trialconfig.NUM_PAIRS,
			  (state.trial+1)*trialconfig.NUM_PAIRS):
	    # Present the orienting stimulus
	    t.clk.delay(trialconfig.DELAY_ORIENT,jitter=trialconfig.JITTER)
	    state.trialData[pair].tOrientStudy = t.clk.get()
	    stamp = flashStimulus(Text(trialconfig.ORIENTING_STUDY),
				  duration=trialconfig.DURATION_ORIENT,
				  clk=t.clk)
	    # Log the presentation of the orienting stimulus
	    t.log.logMessage("STUDY_ORIENT\tTRIAL_%d"%(state.trial),stamp)

            # present the pair of words
            t.clk.delay(trialconfig.DELAY_WORD,jitter=trialconfig.JITTER)
            now = t.clk.get()
            state.trialData[pair].tWord[0] = now
            state.trialData[pair].tWord[1] = now
            word1 = state.trialData[pair].word[0]
            word2 = state.trialData[pair].word[1]
            text =  word1 + "\n\n" + word2
#             print "trial " + str(state.trial) + " study: " + state.trialData[pair].word[0] + " " + state.trialData[pair].word[1]

            #STIM NOW for config.STUDY_STIM_DURATION
            if stimTrial:
                if (state.trialData[state.trial].stimOdds and pair % 2 == 1) or ((not state.trialData[state.trial].stimOdds) and pair % 2 == 0):
                    duration = trialconfig.STUDY_STIM_DURATION
                    t.log.logMessage("BEGIN_STUDY_STIM DURATION_%d\tTRIAL_%d" % (duration, state.trial))
                    stim(duration, t.pulseControl, t.clk, trialconfig)
                    didStim = True
                else:
                    didStim = False
            else:
                didStim = False

            state.trialData[pair].didStim = didStim

            if stimTrial:
                state.trialData[pair].elec = elec
                state.trialData[pair].cur = cur
            else:
                state.trialData[pair].elec = 999
                state.trialData[pair].cur = 999
                

            stamp = flashStimulus(Text(text),
                                  duration=trialconfig.DURATION_WORD,
                                  clk=t.clk)
            state.trialData[pair].studyStamp = stamp
            # Log the presentation of each word in the pair
            t.log.logMessage("STUDY_PAIR_%d\tTRIAL_%d\tWORD1_%s\tWORD2_%s"%(pair, state.trial, word1, word2), stamp)

        ######  TEST   ######
	# Cued Recall

	# Log the start of the trial period
	t.log.logMessage("TEST_START\tTRIAL_%d"%(state.trial),t.clk)
    
	# Loop through all the items in the list
	for test in range(0,trialconfig.NUM_PAIRS):

	    # first find the index of the word to be tested
	    index = -1
            all_words = []
 	    for i in range (state.trial*trialconfig.NUM_PAIRS,
			    (state.trial+1)*trialconfig.NUM_PAIRS):
                all_words.extend(state.trialData[i].word)
		if(state.trialData[i].cueOrder == test):
		    index = i
		    pair = index % trialconfig.NUM_PAIRS

    	    direction = state.trialData[index].cueDir

 	    # create the probe
	    probe = "%s"%(state.trialData[index].word[direction])
#             print "trial " + str(state.trial) + " cue: " + probe

	    # create the filename for output
	    fname = "%d_%d"%(state.trial,pair)

            lstname = "data/" + str(exp.options["subject"]) + "/session_" + str(state.session) + "_RUN/" + str(state.trial) + '_' + str(pair) + '.lst'
            lst = open(lstname, 'w')
            if direction == 1:
                opposite_direction = 0
            else:
                opposite_direction = 1
            expecting = state.trialData[index].word[opposite_direction]
            lst.writelines("\n".join(all_words))
            lst.close()

    
	    # present the orienting stimulus
            if test == 0:
                t.clk.delay(trialconfig.DELAY_ORIENT)
            
	    stamp = flashStimulus(Text(trialconfig.ORIENTING_TEST),
				  duration=trialconfig.DURATION_ORIENT,
				  clk=t.clk)
	    t.vid.updateScreen(t.clk)
	    # Log the presentation of the orienting stimulus
	    t.log.logMessage("TEST_ORIENT\tTRIAL_%d"%(state.trial),stamp)

	    # present the word
	    t.clk.delay(trialconfig.DELAY_CUE)
	    state.trialData[index].tCue = t.clk.get()
	    probeHandle = t.vid.showCentered(Text(probe))
	    stamp = t.vid.updateScreen(t.clk)
            state.trialData[index].cueStamp = stamp
	    # Log the presentation of the probe
	    t.log.logMessage("TEST_PROBE_%d\tTRIAL%d\tPROBE_%s\tEXPECTING_%s\tDIRECTION_%d"%(index,state.trial, probe, expecting, direction),stamp)

            if stimTrial:
                if (state.trialData[state.trial].stimOdds and index % 2 ==1) or ((not state.trialData[state.trial].stimOdds) and index % 2 == 0):
                    duration = trialconfig.CUE_STIM_DURATION
                    t.log.logMessage("BEGIN_CUE_STIM DURATION_%d\tTRIAL_%d" % (duration, state.trial))
                    stim(duration, t.pulseControl, t.clk, trialconfig)


	    # Record
	    (rec,timestamp) = t.aud.startRecording(fname,t=t.clk)
	    t.log.logMessage("REC_START"%(),timestamp)

	    # if we need to erase the cue, do so after a delay
	    if(trialconfig.DURATION_CUE > 0):
		t.clk.delay(trialconfig.DURATION_CUE)
		t.vid.unshow(probeHandle)
		t.vid.updateScreen(t.clk)

   	    # record voice after recording for a specified time
	    if(trialconfig.RECORD_LEN > 0):
		t.clk.delay(trialconfig.RECORD_LEN)
	    else:
		waitForAnyKey(clk=t.clk)

   	    # clear the probe (if we haven't done so already)
	    if(trialconfig.DURATION_CUE <= 0):
		t.vid.unshow(probeHandle)
		t.vid.updateScreen(t.clk)

	    (rec,timestamp) = t.aud.stopRecording(t.clk)
	    t.log.logMessage("REC_END"%(),timestamp)

	    # present the orienting stimulus
            if test != 0:
                t.clk.delay(trialconfig.DELAY_ORIENT)

	# Save the State
	exp.saveState(state,trial=state.trial+1)
        #get the state
	state = exp.restoreState()

    waitForAnyKey(t.clk,Text("You have finished.\n"+
			     "Please inform the experimenter"))