Ejemplo n.º 1
0
def echoClient(timeout=5000):
        		
    ## init connection to the buffer
    ftc,hdr=bufhelp.connect();

    # Now do the echo client
    nEvents=hdr.nEvents;
    endExpt=None;
    while endExpt is None:
        bufhelp.sendEvent('echo',1)
        
        # wait for ackknowledgement from Server
        ack_received = False
        while ack_received is False:
            (curSamp,curEvents)=ftc.wait(-1,nEvents,timeout) # Block until there are new events to process
            if curEvents>nEvents :
                evts=ftc.getEvents([nEvents,curEvents-1]) 
                nEvents=curEvents # update record of which events we've seen
                for evt in evts:
                    if evt.type == "exit": endExpt=1
                    if evt.type == "ack": ack_received = True
                    print(evt)
            else:
                print("Waiting for acknowledgement...")
    
    ftc.disconnect() # disconnect from buffer when done
Ejemplo n.º 2
0
def echoClient(timeout=5000):

    ## init connection to the buffer
    ftc, hdr = bufhelp.connect()

    # Now do the echo client
    nEvents = hdr.nEvents
    endExpt = None
    while endExpt is None:
        bufhelp.sendEvent('echo', 1)

        # wait for ackknowledgement from Server
        ack_received = False
        while ack_received is False:
            (curSamp, curEvents) = ftc.wait(
                -1, nEvents,
                timeout)  # Block until there are new events to process
            if curEvents > nEvents:
                evts = ftc.getEvents([nEvents, curEvents - 1])
                nEvents = curEvents  # update record of which events we've seen
                for evt in evts:
                    if evt.type == "exit": endExpt = 1
                    if evt.type == "ack": ack_received = True
                    print(evt)
            else:
                print("Waiting for acknowledgement...")

    ftc.disconnect()  # disconnect from buffer when done
Ejemplo n.º 3
0
    def single_character_flashing(self):
        "Runs the single charachter flashing sequence"
        options = list(range(self.num_of_options))

        if DEBUG:
            fake_target = choice(options)
            print('Fake target', fake_target)

        total_sequence = np.array([], dtype=np.int16)
        for i in range(NUM_ROUNDS):
            shuffle(options)
            total_sequence = np.append(total_sequence, options)
            for o in options:
                bufhelp.sendEvent('p300.flash', o)

                if DEBUG:
                    if o == fake_target:
                        self.inject_erp()

                self.si.flash_option(o, color='red', duration=FLASH_DURATION)

        # Wait some more for the last characters to be recorded
        self.si.wait(CLEAR_DURATION)
        if DEBUG:
            print('Fake target', fake_target)
            self.all_targets.append(fake_target)

        bufhelp.sendEvent('p300.singleseq', 'end')
        return total_sequence
Ejemplo n.º 4
0
def run_calibration(nSequences, nBlock, trialDuration, intertrialDuration, baselineDuration):
    # Display fixation circle and two arows (left and right)
    initial_display()
    # pygame.time.delay(intertrialDuration)

    # make the target sequence
    nSymbols = 4
    targetSequence = list(range(nSymbols)) * int(nSequences / nSymbols + 1)  # sequence in sequential order
    shuffle(targetSequence)  # N.B. shuffle works in-place!

    # 0 - left, 1 - right, 2 - both, 3 - none (no movement) 
    for target in targetSequence:
        sleep(intertrialDuration)

        # show the baseline
        pygame.draw.circle(display_surface, red, [X // 2, Y // 2], 40)
        pygame.display.update()
        bufhelp.sendEvent('stimulus.baseline', 'start')
        sleep(baselineDuration)
        bufhelp.sendEvent('stimulus.baseline', 'end')

       # initial_display()


        # show the target

        if target == 0:
            pygame.draw.circle(display_surface, yellow, [X // 2, Y // 2], 40) # fixation yellow
            pygame.draw.circle(display_surface, yellow, (535, 400), 100) # mark target
            display_surface.blit(left_arr, (471, 336))
            pygame.display.update()
        elif target == 1:
            pygame.draw.circle(display_surface, yellow, [X // 2, Y // 2], 40)  # fixation yellow
            pygame.draw.circle(display_surface, yellow, (1065, 400), 100) # mark target
            display_surface.blit(right_arr, (1001, 336))
            pygame.display.update()
        elif target == 2:
            pygame.draw.circle(display_surface, yellow, [X // 2, Y // 2], 40)  # fixation yellow
            pygame.draw.circle(display_surface, yellow, (535, 400), 100)  # mark target
            pygame.draw.circle(display_surface, yellow, (1065, 400), 100)  # mark target
            display_surface.blit(left_arr, (471, 336))
            display_surface.blit(right_arr, (1001, 336))
            pygame.display.update()
        else:
            pygame.draw.circle(display_surface, yellow, [X // 2, Y // 2], 40)  # fixation yellow
            pygame.display.update()

        bufhelp.sendEvent('stimulus.trial', 'start')
        bufhelp.sendEvent('stimulus.target', target)
        injectERP(amp=1)

        sleep(trialDuration)

        # reset the display
        initial_display()
        bufhelp.sendEvent('stimulus.trial', 'end');
Ejemplo n.º 5
0
    def send_prior_loop(events, timeout=2):

        ## init connection to the buffer
        ftc, hdr = bufhelp.connect()

        ev_list = []

        # Now do the echo client
        nEvents = hdr.nEvents
        endExpt = None
        bufhelp.sendEvent('prior.start', '1')

        priors = calc_priors(ev_list)
        bufhelp.sendEvent('priors', priors.to_csv())
        priors.to_csv('priors.csv')
        # bufhelp.sendEvent('p300preds', priors.to_csv())
        i = 0
        while endExpt is None:
            (curSamp, curEvents) = ftc.wait(
                -1, nEvents,
                timeout)  # Block until there are new events to process
            if curEvents > nEvents:
                evts = ftc.getEvents([nEvents, curEvents - 1])
                nEvents = curEvents  # update record of which events we've seen
                for evt in evts:
                    if evt.type == "exit": endExpt = 1
                    if evt.type == "p300preds":
                        # read classifier predictions
                        preds = pd.read_csv(StringIO(evt.value),
                                            header=None).set_index(0)[1]
                        preds = preds.drop('pause')
                        preds.to_csv('p300pred_wouter' + str(i) + '.csv')
                        i += 1
                        print(priors)
                        print('preds', preds)
                        p = preds * priors
                        print(p)
                        p /= sum(p)
                        print(p)
                        best = p.sort_values().index[-1]
                        best_event, best_value = best.split('.')
                        if best_event == 'sos':
                            bufhelp.sendEvent('sos', 'start')
                        # send actual event to virtual environment
                        bufhelp.sendEvent(best_event, best_value)
                        # send event to MatLab code
                        bufhelp.sendEvent('finalprediction',
                                          best_event + '.' + best_value)
                        ev_list.append(best)
                        priors = calc_priors(ev_list)
                        priors.to_csv('priors.csv')

                    # print(evt)

        ftc.disconnect()  # disconnect from buffer when done
Ejemplo n.º 6
0
def echoClient(timeout=5000):

    ## init connection to the buffer
    ftc, hdr = bufhelp.connect()

    # send event to buffer
    bufhelp.sendEvent('echo', 1)

    # wait for ackknowledgement from Server

    ftc.disconnect()  # disconnect from buffer when done
Ejemplo n.º 7
0
def echoClient(timeout=5000):
        		
    ## init connection to the buffer
    ftc,hdr=bufhelp.connect();

    # send event to buffer
    bufhelp.sendEvent('echo',1)
        
    # wait for ackknowledgement from Server
       
    ftc.disconnect() # disconnect from buffer when done
Ejemplo n.º 8
0
def echoClient(timeout=5000):
        		
    ## init connection to the buffer
    ftc,hdr=bufhelp.connect();

    # send event to buffer
    while True:
        events = bufhelp.buffer_newevents(state=True)

        for event in events:
            if event.type == 'quit':
                ftc.disconnect()
                return
            elif event.type != 'echo':
                bufhelp.sendEvent('echo', event.value)
Ejemplo n.º 9
0
def charStim(t):
    for i in range(5):
        h.set(text='')
        drawnow()

        np.random.shuffle(letters)
        sleep(1)
        print(letters)
        bufhelp.sendEvent('stimulus.repetition', i)
        for c in letters:
            bufhelp.sendEvent('stimulus.char',
                              c if t == None else c + '_' + str(c == t))
            if c == t:
                injectERP(1)
            h.set(text=c)
            drawnow()  # update the display
            sleep(.1)
Ejemplo n.º 10
0
def calibrate(text, n_targets=10, repetitions=5):
    """
    Calibration phase

    Args:
    - window: Render the stimuli to this window
    - n_targets: Number of iterations with a random target letter to do
    - repetitions: Number of times each letter occurs per iteration
    """
    letters = list(string.ascii_uppercase)
    target_letters = np.random.choice(letters, size=n_targets)
    bufhelp.sendEvent('experiment.start', 1)
    for target in target_letters:
        text.set(text=target, color='green')
        drawnow()
        plt.pause(2)
        text.set(text='')
        drawnow()
        plt.pause(1)
        for letter in np.random.permutation(letters * repetitions):
            text.set(text=letter, color='white')
            drawnow()
            stimulus_name = int(letter == target)
            bufhelp.sendEvent('stimulus.type', stimulus_name)
            plt.pause(0.1)
    bufhelp.sendEvent('experiment.end', 1)
Ejemplo n.º 11
0
def test(text, n_targets=10, repetitions=5):
    """
    Online feedback phase

    Args:
    - text: handle to Text object
    - n_targets: Number of iterations to do
    - repetitions: Number of times each letter occurs per iteration
    """
    letters = list(string.ascii_uppercase)
    letter2idx = {l: i for i, l in enumerate(letters)}

    # Make sure all classification events are retrieved
    bufhelp.MAXEVENTHISTORY = 800
    bufhelp.sendEvent('experiment.start', 1)
    for target in range(n_targets):
        _, state = bufhelp.buffer_newevents('classifier.prediction',
                                            timeout_ms=0,
                                            state=None)
        flashes = np.zeros((len(letters), len(letters) * repetitions))
        text.set(text='Think of your target letter\nand get ready',
                 color='green',
                 fontsize=14)
        drawnow()
        plt.pause(2)
        text.set(text='')
        drawnow()
        plt.pause(1)
        for i, letter in enumerate(np.random.permutation(letters *
                                                         repetitions)):
            flashes[letter2idx[letter], i] = 1
            text.set(text=letter, color='white', fontsize=120)
            drawnow()
            bufhelp.sendEvent('stimulus.type', 'test')
            plt.pause(0.1)
        text.set(text='')
        # Wait for classifier
        plt.pause(3)

        predictions, _ = bufhelp.buffer_newevents('classifier.prediction',
                                                  state=state,
                                                  timeout_ms=5000)
        # Make sure the predictions are in the right order
        predictions = sorted(predictions, key=lambda p: p.sample)
        if len(predictions) != flashes.shape[1]:
            print('Too few predictions found..., removing last trials')
            flashes = flashes[:, :len(predictions)]

        predictions = np.array([p.value[0] for p in predictions])
        # Compute inner product of each letter
        similarities = flashes @ predictions
        # Pick the letter with the highest similarity
        letter = np.argmax(similarities)
        text.set(text=letters[letter], fontsize=120, color='blue')
        plt.pause(2)
    bufhelp.sendEvent('experiment.end', 1)
Ejemplo n.º 12
0
def calibration():
    targets = np.random.choice(letters, 10, replace=False)
    bufhelp.sendEvent('stimulus.targets', targets)
    for t in targets:
        bufhelp.sendEvent('stimulus.target', t)
        h.set(text=t, color='g')
        drawnow()
        sleep(2)
        h.set(text='', color='k')
        drawnow()
        sleep(1)
        charStim(t)
        h.set(text='', color='k')
        drawnow()
        sleep(1)
    bufhelp.sendEvent('stimulus.training', 'end')
Ejemplo n.º 13
0
def single_character_flashing(target, bufhelp, num_of_options):
    "Runs the single charachter flashing sequence"
    options = list(range(num_of_options))

    total_sequence = np.array([], dtype=np.int16)
    for i in range(NUM_ROUNDS):
        shuffle(options)
        total_sequence = np.append(total_sequence, options)
        for o in options:
            bufhelp.sendEvent('stimulus.letter', o)
            bufhelp.sendEvent('stimulus.cue',
                              'target' if o == target else 'nontarget')
            if (o == target and DEBUG):
                inject_erp()
            si.flash_option(o, color='red', duration=FLASH_DURATION)
            si.wait(INTERSTIMULUS_DURATION)

    # Wait some more for the last characters to be recorded
    #si.wait(4)
    bufhelp.sendEvent('stimulus.singleseq', 'end')
    return total_sequence
Ejemplo n.º 14
0
interCharDuration = 1

ftc, hdr = bufhelp.connect()

# set the display and the string for stimulus
fig = plt.figure()
fig.suptitle('RunSentences-Stimulus', fontsize=24, fontweight='bold')
ax = fig.add_subplot(111)  # default full-screen ax
ax.set_xlim((-1, 1))
ax.set_ylim((-1, 1))
ax.set_axis_off()
h = ax.text(0, 0, '', style='italic')

## init connection to the buffer

bufhelp.sendEvent('stimulus.letters', 'start')
## STARTING PROGRAM LOOP
drawnow()  # update the display

# inject a signal into the simulated data-stream for debugging your signal-processing scripts

# sleep(interCharDuration)

##------------------------------------------------------
##                    ADD YOUR CODE BELOW HERE
##------------------------------------------------------


def charStim(t):
    for i in range(5):
        h.set(text='')
Ejemplo n.º 15
0
circ = patches.Circle((0, 0), stimRadius / 4, facecolor=bgColor)
hhi = ax.add_patch(circ)
hdls.insert(nSymbs, hhi)
[_.set(visible=False) for _ in hdls]  # make all invisible

## init connection to the buffer
ftc, hdr = bufhelp.connect()

#wait for key-press to continue
[_.set(facecolor=bgColor) for _ in hdls]
txthdl.set(text='Press key to start')
drawnow()
waitforkey(fig)
txthdl.set(visible=False)

bufhelp.sendEvent('stimulus.training', 'start')
## STARTING stimulus loop
for si, tgt in enumerate(tgtSeq):

    sleep(intertrialDuration)

    # show the baseline
    hdls[-1].set(visible=True, facecolor=fixColor)
    drawnow()
    bufhelp.sendEvent('stimulus.baseline', 'start')
    sleep(baselineDuration)
    bufhelp.sendEvent('stimulus.baseline', 'end')

    #show the target
    print("%d) tgt=%d :" % (si, tgt))
    [_.set(facecolor=bgColor, visible=True) for _ in hdls]
Ejemplo n.º 16
0
mywin = visual.Window(size=(screenWidth, screenHeight), fullscr=False, screen=1, allowGUI=False, allowStencil=False,
    monitor='testMonitor', units="pix",color=[1,1,1], colorSpace='rgb',blendMode='avg', useFBO=True)

#define variables
sentence = ['Hello World!', 'How are you today?', 'Good, thank you!']
interCharDuration=1
interSentenceDuration=5

#create some stimuli
welcome_text = visual.TextStim(mywin, text='Welcome! \n\nPress a key to start...',color=(-1,-1,-1),wrapWidth = 800) 
current_text = visual.TextStim(mywin, text='',color=(-1,-1,-1),wrapWidth = 800) 
goodbye_text = visual.TextStim(mywin, text='Bye! \n\nPress a key to finish...',color=(-1,-1,-1),wrapWidth = 800) 

# ************** Start run sentences **************
showText(welcome_text)
bufhelp.sendEvent('experiment','start')
waitForKeypress()

bufhelp.sendEvent('stimulus.seq','start')
for s in range(len(sentence)):
    current_sentence = sentence[s]
    bufhelp.sendEvent('stimulus.sentence','start')
    for i in range(len(current_sentence)):
        for c in range(i+1):
            current_text.text = current_text.text + current_sentence[c]
        showText(current_text)
        bufhelp.sendEvent('stimulus.letter',current_sentence[i])
        current_text.text = '';
        core.wait(interCharDuration) 
    bufhelp.sendEvent('stimulus.sentence','end')
    mywin.flip()
Ejemplo n.º 17
0
        rowh=[]
        # TODO: ensure row is enumeratable...
        for j,symb in enumerate(row):
            y=axh[0]+j*h+h/2
            print('%s @(%f,%f)'%(symb,x,y))
            txthdl =ax.text(x, y, symb, color=txtColor,visible=True)
            rowh.append(txthdl)
        if len(rowh)==1: rowh=rowh[0] # BODGE: ensure hdls has same structure as symbols
        hdls.append(rowh)
    print('hds(%d)=[%s]'%(len(hdls),str(hdls)))
    drawnow()
    return hdls
            
##--------------------- Start of the actual experiment loop ----------------------------------

bufhelp.sendEvent('stimulus.feedback','start');
state = None

## STARTING stimulus loop

#catch the prediction
# N.B. use state to track which events processed so far
events,state = bufhelp.buffer_newevents('classifier.prediction',5000,state)
if events == []:
    print("Error! no predictions, continuing")
else:
    # get all true stimulus into 1 numpy array
    stimSeq = np.array(stimSeq) # [ nEpochs x nSymbs ]
    # get all predictions into 1 numpy array
    pred = np.array([e.value for e in events]) # [ nEpochs ]
    ss   = stimSeq[:len(pred),:] # [ nSymbs x nEpochs ]
Ejemplo n.º 18
0
interCharDuration=1;
    
##--------------------- Start of the actual experiment loop ----------------------------------
# set the display and the string for stimulus
fig = plt.figure()
fig.suptitle('RunSentences-Stimulus', fontsize=14, fontweight='bold')
ax = fig.add_subplot(111) # default full-screen ax
ax.set_xlim((-1,1))
ax.set_ylim((-1,1))
ax.set_axis_off()
txthdl = ax.text(0, 0, 'This is some text', style='italic')

## init connection to the buffer
ftc,hdr=bufhelp.connect();

bufhelp.sendEvent('stimulus.sentences','start');
## STARTING PROGRAM LOOP
for si,sentence in enumerate(sentences):
    
    # reset the display
    txthdl.set(text='')
    drawnow()
    bufhelp.sendEvent('stimulus.sentence',sentence)

    for ci in range(len(sentence)):
        char = sentence[ci]
        print("%d) char=%s sentence=%s"%(ci,char,sentence[0:ci+1]))
        txthdl.set(text=sentence[0:ci+1])
        drawnow()
        bufhelp.sendEvent('stimulus.character',char)
        injectERP(1)  # inject erp into data = should see in sig-viewer / event-locked average
for ti in range(0,nSymbs):
    t_theta=2*math.pi*ti/(nSymbs+1)
    t_center[ti]=(int(math.cos(t_theta)*winRect.width*.3)+winRect.centerx,
                  -int(math.sin(t_theta)*winRect.height*.3)+winRect.centery)
t_center[nSymbs]=winRect.center # fixation point

##--------------------- Start of the actual experiment loop ----------------------------------
drawString(["Motor Imagery Experiment" "Feedback phase" "" "Perform the Green cued task" "after trial feedback given in blue" "" "Key to continue"])
pygame.display.update() # drawnow equivalent...
waitForKey()

# make the target sequence
tgtSeq = range(0,nSymbs)*nSeq
random.shuffle(tgtSeq)

bufhelp.sendEvent('stimulus.testing','start')
events,state = buffer_newevents(timeout_ms=0) # initialize event queue
## STARTING STIMULUS LOOP
for si in range(0,nSeq):
    
    # reset the display
    drawString('')
    pygame.display.update() # drawnow equivalent...
    time.sleep(intertrialDuration)

    # reset with red fixation to alert to trial start
    drawTrial([t_center[-1]])
    pygame.display.update()
    bufhelp.sendEvent('stimulus.baseline','start')
    time.sleep(baselineDuration)
    bufhelp.sendEvent('stimulus.baseline','end')
for ti in range(0,nSymbs):
    t_theta=2*math.pi*ti/(nSymbs+1)
    t_center[ti]=(int(math.cos(t_theta)*winRect.width*.3)+winRect.centerx,
                  -int(math.sin(t_theta)*winRect.height*.3)+winRect.centery)
t_center[nSymbs]=winRect.center # fixation point

##--------------------- Start of the actual experiment loop ----------------------------------
drawString(["Motor Imagery Experiment" "Feedback phase" "" "Perform the Green cued task" "after trial feedback given in blue" "" "Key to continue"])
pygame.display.update() # drawnow equivalent...
waitForKey()

# make the target sequence
tgtSeq = range(0,nSymbs)*nSeq
random.shuffle(tgtSeq)

bufhelp.sendEvent('stimulus.testing','start')
events,state = buffer_newevents(timeout_ms=0) # initialize event queue
## STARTING STIMULUS LOOP
for si in range(0,nSeq):
    
    # reset the display
    drawString('')
    pygame.display.update() # drawnow equivalent...
    time.sleep(intertrialDuration)

    # reset with red fixation to alert to trial start
    drawTrial([t_center[-1]])
    pygame.display.update()
    bufhelp.sendEvent('stimulus.baseline','start')
    time.sleep(baselineDuration)
    bufhelp.sendEvent('stimulus.baseline','end')
Ejemplo n.º 21
0
interSentenceDuration = 3
interCharDuration = 1

##------------------------------------------------------
##                    ADD YOUR CODE BELOW HERE
##------------------------------------------------------

# set the display and the string for stimulus
fig = plt.figure()
fig.suptitle('RunSentences-Stimulus', fontsize=24, fontweight='bold')
ax = fig.add_subplot(111)  # default full-screen ax
ax.set_xlim((-1, 1))
ax.set_ylim((-1, 1))
ax.set_axis_off()
h = ax.text(0, 0, 'This is some text', style='italic')

## init connection to the buffer
ftc, hdr = bufhelp.connect()

bufhelp.sendEvent('stimulus.sentences', 'start')
## STARTING PROGRAM LOOP
h.set_text('some_text')
drawnow()  # update the display

# inject a signal into the simulated data-stream for debugging your signal-processing scripts
injectERP(1)

sleep(interCharDuration)

waitforkey(fig)
Ejemplo n.º 22
0
        nSamples = bufhelp.globalstate[0]
        for point in gather:
            evt, end_sample = point
            if end_sample < nSamples:
                # Enough time has passed to collect the data
                data = ftc.getData((evt.sample, end_sample))
                # Preprocess
                data = data[:, :
                            10]  # select channels which were actually connected
                data = preproc.detrend([data])
                data[0] = data[0][:, good_channels]  # filter out bad-channels
                data = preproc.spatialfilter(data, type='car')
                data = preproc.spectralfilter(data, (5, 6, 31, 32),
                                              hdr.fSample)

                freqs = np.linspace(0, hdr.fSample / 2, len(data[0]))
                data = [d[(5 <= freqs) & (freqs <= 32)] for d in data]
                data[0] = util.subsample_frequencies(data[0],
                                                     width=13).reshape(-1)

                # This event is processed, so it can be removed
                gather.remove(point)

                # Classify
                data[0] = data[0]
                pred = clf.predict_proba(data)[0][1]
                print(pred)

                # Send back event
                bufhelp.sendEvent('classifier.prediction', pred)
Ejemplo n.º 23
0
## init connection to the buffer
ftc,hdr=bufhelp.connect();

#wait for key-press to continue
[_.set(color=bgColor) for _ in hdls]
txthdl.set(text='Press key to start')
drawnow()
waitforkey(fig)
txthdl.set(visible=False)

plt.ion()

[ _.set(visible=True) for _ in hdls]
drawnow()

bufhelp.sendEvent('stimulus.feedback','start');
# initialize the state for tracking and catching classifier prediction events
#_,state = bufhelp.buffer_newevents('classifier.prediction',0,False,True)
_ = bufhelp.buffer_newevents('classifier.prediction',0)
## STARTING stimulus loop
for ti in range(nSeq):
    
    sleep(interSeqDuration)
      
    # reset the display
    [ _.set(color=bgColor) for _ in hdls]
    drawnow()
    sleep(postCueDuration)
    
    # start the scanning loop
    stimSeq=[] # [ nSymbs x nEpochs ] info on flash/non-flash state of each output
Ejemplo n.º 24
0
score = n_shots = n_deaths = n_hits = 0
rect = screen.get_rect()
ship = ShipSprite()
ship_group = pygame.sprite.RenderPlain(ship)
controller = ProbablisticConroller(alpha=0.5*PREDICTION_TIME) if SIMULATION else PlayerController()
enemy_group = pygame.sprite.RenderPlain()
bullet_group = pygame.sprite.RenderPlain()
last_enemy_spawned = -ENEMY_SPAWN_TIME
ship_start_pos = 0
last_pred_time = 0
left = True
pygame.init()
font = pygame.font.Font(pygame.font.get_default_font(), 16)
lasttime = last_bullet_spawned = game_start_time = time.time()
if not SIMULATION: bufhelp.sendEvent('experiment.im', 'start')
while True:
    clock.tick(60)  # 60 FPS
    curtime = time.time()
    deltatime = curtime - lasttime
    lasttime = curtime

    # Record which keys are pressed and check if the game should exit
    for event in pygame.event.get():
        if not hasattr(event, 'key'): continue
        down = event.type == KEYDOWN
        keys[event.key] = down
        if event.type == QUIT or keys[K_ESCAPE]:
            sys.exit()
    screen.fill(BACKGROUND_COLOR)
Ejemplo n.º 25
0
# setup the targets
hdls=initGrid(symbols)
[ _.set(visible=False) for _ in hdls] # make all invisible

## init connection to the buffer
ftc,hdr=bufhelp.connect();

#wait for key-press to continue
[_.set(facecolor=bgColor) for _ in hdls]
txthdl.set(text='Press key to start')
drawnow()
waitforkey(fig)
txthdl.set(visible=False)

bufhelp.sendEvent('stimulus.training','start');
## STARTING stimulus loop
for si,tgt in enumerate(tgtSeq):
    
    sleep(interSeqDuration)
      
    # start the scanning loop
    for rep in range(nRep): 
        for si in range(nSymbs):
            # flash
            hdls[si].set(color=flashColor)
            drawnow()
            bufhelp.sendEvent('stimulus.cue',si)
            bufhelp.sendEvent('stimulus.tgtFlash',si==tgt)
            sleep(stimDuration)                
            # reset
Ejemplo n.º 26
0
hdls=initGrid(symbols)
[ _.set(visible=False) for _ in hdls] # make all invisible

## init connection to the buffer
ftc,hdr=bufhelp.connect();

#wait for key-press to continue
[_.set(color=bgColor) for _ in hdls]
txthdl.set(text='Press key to start')
drawnow()
waitforkey(fig)
txthdl.set(visible=False)

plt.ion()

bufhelp.sendEvent('stimulus.training','start');

## START EXPERIMENTAL LOOP #
      
#show the target/cue
print("%d) tgt=%d :"%(ti,tgt))
[_.set(color=bgColor,visible=True) for _ in hdls]
hdls[tgt].set(color=tgtColor)
drawnow()
bufhelp.sendEvent('stimulus.target',tgt)
sleep(cueDuration)
  
# reset the display
[ _.set(color=bgColor) for _ in hdls]
drawnow()
Ejemplo n.º 27
0
    # get data in correct format
    data = np.transpose(data)

    # 1: detrend
    data = preproc.detrend(data)
    # 2: bad-channel removal (as identifed in classifier training)
    data = data[goodch, :, :]
    # 3: apply spatial filter (as in classifier training)
    data = preproc.spatialfilter(data, type=spatialfilter)
    # 4: map to frequencies (TODO: check fs matches!!)
    data, freqs = preproc.powerspectrum(data, dim=1, fSample=fs)
    # 5: select frequency bins we want
    data, freqIdx = preproc.selectbands(data,
                                        dim=1,
                                        band=freqbands,
                                        bins=freqs)
    print(data.shape)
    # 6: apply the classifier, get raw predictions
    X2d = np.reshape(
        data,
        (-1,
         data.shape[2])).T  # sklearn needs data to be [nTrials x nFeatures]
    fraw = classifier.predict(X2d)
    # 7: map from fraw to event values
    # predictions = [ ivaluedict[round(i)] for i in fraw ]
    predictions = fraw
    # send the prediction events
    for letter, pred in zip(event_letters, predictions):
        bufhelp.sendEvent("classifier.prediction", letter + '_' + str(pred))
        print(pred)
Ejemplo n.º 28
0
stimRadius=.5;
theta=np.linspace(0,np.pi,nSymbs)
stimPos=np.stack((np.cos(theta),np.sin(theta))) #[2 x nSymbs]
for hi,pos in enumerate(stimPos):
    rect=patches.Rectangle((pos[0]-stimRadius/2,pos[1]-stimRadius/2),stimRadius/2,stimRadius/2,facecolor=bgColor)
    hhi=ax.add_patch(rect)
    hdls.insert(hi,hhi)
# add symbol for the center of the screen
spos = np.array((0,0)).reshape((-1,1))
stimPos=np.hstack((stimPos,spos)) #[2 x nSymbs+1]
rect = patches.Rectangle((0-stimRadius/4,0-stimRadius/4),stimRadius/2,stimRadius/2,facecolor=bgColor)
hhi  =ax.add_patch(rect)
hdls.insert(nSymbs,hhi)
[ _.set(visible=False) for _ in hdls] # make all invisible


## init connection to the buffer
ftc,hdr=bufhelp.connect();

#---------------------------------------------------------------------------------------
#                             YOUR CODE BELOW HERE
#---------------------------------------------------------------------------------------

#wait for key-press to continue
[_.set(facecolor=bgColor) for _ in hdls] # set color for all boxes
txthdl.set(text='Press key to start',visible=True) # set the text info and make visible
drawnow()
waitforkey(fig) # wait for a keypress

bufhelp.sendEvent('stimulus.training','start') # send an event
Ejemplo n.º 29
0
    while len(allKeys)==0:
        allKeys = event.getKeys()
    if 'escape' in allKeys[0]:
        mywin.close() # quit
        core.quit()

# Setup the stimulus window
screenWidth = 1300
screenHeight = 700 
mywin = visual.Window(size=(screenWidth, screenHeight), fullscr=False, screen=1, allowGUI=False, allowStencil=False,
    monitor='testMonitor', units="pix",color=[1,1,1], colorSpace='rgb',blendMode='avg', useFBO=True)

#create some stimuli
current_text = visual.TextStim(mywin, text='blah',color=(-1,-1,-1),wrapWidth = 800) 

# ************** Start run sentences **************

# show some text
showText(current_text)

# label data
bufhelp.sendEvent('experiment','start')

# pause (in secs)
core.wait(1) 





Ejemplo n.º 30
0
        for j, symb in enumerate(row):
            y = axh[0] + j * h + h / 2
            print('%s @(%f,%f)' % (symb, x, y))
            txthdl = ax.text(x, y, symb, color=txtColor, visible=True)
            rowh.append(txthdl)
        if len(rowh) == 1:
            rowh = rowh[0]  # BODGE: ensure hdls has same structure as symbols
        hdls.append(rowh)
    print('hds(%d)=[%s]' % (len(hdls), str(hdls)))
    drawnow()
    return hdls


##--------------------- Start of the actual experiment loop ----------------------------------

bufhelp.sendEvent('stimulus.feedback', 'start')
state = None

## STARTING stimulus loop

#catch the prediction
# N.B. use state to track which events processed so far
events, state = bufhelp.buffer_newevents('classifier.prediction', 5000, state)
if events == []:
    print("Error! no predictions, continuing")
else:
    # get all true stimulus into 1 numpy array
    stimSeq = np.array(stimSeq)  # [ nEpochs x nSymbs ]
    # get all predictions into 1 numpy array
    pred = np.array([e.value for e in events])  # [ nEpochs ]
    ss = stimSeq[:len(pred), :]  # [ nSymbs x nEpochs ]
Ejemplo n.º 31
0
interCharDuration = 1

##--------------------- Start of the actual experiment loop ----------------------------------
# set the display and the string for stimulus
fig = plt.figure()
fig.suptitle('RunSentences-Stimulus', fontsize=14, fontweight='bold')
ax = fig.add_subplot(111)  # default full-screen ax
ax.set_xlim((-1, 1))
ax.set_ylim((-1, 1))
ax.set_axis_off()
txthdl = ax.text(0, 0, 'This is some text', style='italic')

## init connection to the buffer
ftc, hdr = bufhelp.connect()

bufhelp.sendEvent('stimulus.sentences', 'start')
## STARTING PROGRAM LOOP
for si, sentence in enumerate(sentences):

    # reset the display
    txthdl.set(text='')
    drawnow()
    bufhelp.sendEvent('stimulus.sentence', sentence)

    for ci in range(len(sentence)):
        char = sentence[ci]
        print("%d) char=%s sentence=%s" % (ci, char, sentence[0:ci + 1]))
        txthdl.set(text=sentence[0:ci + 1])
        drawnow()
        bufhelp.sendEvent('stimulus.character', char)
        sleep(interCharDuration)
Ejemplo n.º 32
0
hdls = initGrid(symbols)
[_.set(visible=False) for _ in hdls]  # make all invisible

## init connection to the buffer
ftc, hdr = bufhelp.connect()

#wait for key-press to continue
[_.set(color=bgColor) for _ in hdls]
txthdl.set(text='Press key to start')
drawnow()
waitforkey(fig)
txthdl.set(visible=False)

plt.ion()

bufhelp.sendEvent('stimulus.training', 'start')
## STARTING stimulus loop
for si, tgt in enumerate(tgtSeq):

    sleep(interSeqDuration)

    #show the target/cue
    print("%d) tgt=%d :" % (si, tgt))
    [_.set(color=bgColor, visible=True) for _ in hdls]
    hdls[tgt].set(color=tgtColor)
    drawnow()
    bufhelp.sendEvent('stimulus.target', tgt)
    bufhelp.sendEvent('stimulus.trial', 'start')
    sleep(cueDuration)

    # reset the display
Ejemplo n.º 33
0
screenWidth = 1300
screenHeight = 700
mywin = visual.Window(size=(screenWidth, screenHeight),
                      fullscr=False,
                      screen=1,
                      allowGUI=False,
                      allowStencil=False,
                      monitor='testMonitor',
                      units="pix",
                      color=[1, 1, 1],
                      colorSpace='rgb',
                      blendMode='avg',
                      useFBO=True)

#create some stimuli
current_text = visual.TextStim(mywin,
                               text='blah',
                               color=(-1, -1, -1),
                               wrapWidth=800)

# ************** Start run sentences **************

# show some text
showText(current_text)

# label data
bufhelp.sendEvent('experiment', 'start')

# pause (in secs)
core.wait(1)
Ejemplo n.º 34
0
        # combine predictions from classifiers

        if prediction == [0, 0]:
                prediction_int = 3
        if prediction == [1, 0]:
                prediction_int = 0
        if prediction == [0, 1]:
                prediction_int = 1
        if prediction == [1, 1]:
                prediction_int = 2

        print(prediction , prediction_int, pred)

        threshold.append([events_im[0].value, prediction0 , prediction_int, pred, data_rec])

        bufhelp.sendEvent('classifier.prediction', str(prediction_int))

        # stop processing if needed
        if "stimulus.last_target" in event_types :
            break

    else:
        # wait for data after a trigger event
        #  exitevent=None means return as soon as data is ready
        #  N.B. be sure to propogate state between calls
        data_im, events_im, stopevents, pending = bufhelp.gatherdata(["stimulus.target", "stimulus.last_target"],im_length,[], milliseconds=True)
        print('asd')
        # get all event type labels
        event_types = [e.type for e in events_im]
        print(event_types)
        # get all event values
Ejemplo n.º 35
0
    # get all event type labels
    event_types = [e.type[0] for e in events] 
    
    # stop processing if needed
    if "stimulus.feedback" in event_types:
        break

    print("Applying classifier to %d events"%(len(events)))
    
    # get data in correct format
    data = np.transpose(data) # make it [d x tau]
    
    # 1: detrend
    data = preproc.detrend(data)
    # 2: bad-channel removal (as identifed in classifier training)
    data = data[goodch,:,:]
    # 3: apply spatial filter (as in classifier training)
    data = preproc.spatialfilter(data,type=spatialfilter)
    # 4 & 5: spectral filter (TODO: check fs matches!!)
    data = preproc.fftfilter(data, 1, freqbands, fs)
    # 6 : bad-trial removal
    # 7: apply the classifier, get raw predictions
    X2d  = np.reshape(data,(-1,data.shape[2])).T # sklearn needs data to be [nTrials x nFeatures]
    fraw = classifier.predict(X2d)
    # 8: map from fraw to event values (note probably not necessary here!)    
    #predictions = [ ivaluedict[round(i)] for i in fraw ]
    # send the prediction events
    for i,f in enumerate(fraw):
        print("%d) {%s}=%f(raw)\n"%(i,str(events[i]),f))
        bufhelp.sendEvent("classifier.prediction",f)
Ejemplo n.º 36
0
    # stop processing if needed
    if "stimulus.feedback" in event_types:
        break

    print("Applying classifier to %d events" % (len(events)))

    # get data in correct format
    data = np.transpose(data)  # make it [d x tau]

    # 1: detrend
    data = preproc.detrend(data)
    # 2: bad-channel removal (as identifed in classifier training)
    data = data[goodch, :, :]
    # 3: apply spatial filter (as in classifier training)
    data = preproc.spatialfilter(data, type=spatialfilter)
    # 4 & 5: spectral filter (TODO: check fs matches!!)
    data = preproc.fftfilter(data, 1, freqbands, fs)
    # 6 : bad-trial removal
    # 7: apply the classifier, get raw predictions
    X2d = np.reshape(
        data,
        (-1,
         data.shape[2])).T  # sklearn needs data to be [nTrials x nFeatures]
    fraw = classifier.predict(X2d)
    # 8: map from fraw to event values (note probably not necessary here!)
    #predictions = [ ivaluedict[round(i)] for i in fraw ]
    # send the prediction events
    for i, f in enumerate(fraw):
        print("%d) {%s}=%f(raw)\n" % (i, str(events[i]), f))
        bufhelp.sendEvent("classifier.prediction", f)
Ejemplo n.º 37
0

## init connection to the buffer
ftc,hdr=bufhelp.connect();

#wait for key-press to continue
[_.set(facecolor=bgColor) for _ in hdls]
txthdl.set(text='Press key to start')
drawnow()
waitforkey(fig)

# set stimuli to visible
txthdl.set(visible=False)
[_.set(facecolor=bgColor,visible=True) for _ in hdls]

bufhelp.sendEvent('stimulus.training','start')
state=None
txthdl.set(visible=False)
## STARTING stimulus loop
for si,tgt in enumerate(tgtSeq):
    
    sleep(intertrialDuration)

    # show the baseline
    hdls[-1].set(facecolor=fixColor) # fixation cross red
    drawnow()
    bufhelp.sendEvent('stimulus.baseline','start')
    sleep(baselineDuration)
    bufhelp.sendEvent('stimulus.baseline','end')
      
    #show the target
Ejemplo n.º 38
0
        elif e.value == "training":
            print("Training classifier")

            data = preproc.detrend(data)
            data, badch = preproc.badchannelremoval(data)
            data = preproc.spatialfilter(data)
            data = preproc.spectralfilter(data, (1, 10, 15, 25),
                                          bufhelp.fSample)
            data, events, badtrials = preproc.badtrailremoval(data, events)
            mapping = {
                ('stimulus.hybrid', 'left'): -1,
                ('stimulus.hybrid', 'right'): 1
            }
            classifier = linear.fit(data, events, mapping)
            bufhelp.update()
            bufhelp.sendEvent("sigproc.training", "done")

        elif e.value == "contfeedback":
            print("Feedback phase")
            while True:
                test_data, test_events, stopevents = bufhelp.gatherdata(
                    "stimulus.hybrid",
                    trlen_ms,
                    "stimulus.hybrid",
                    milliseconds=True)

                #                if isinstance(stopevents, list):
                #                    if any(["stimulus.feedback" in x.type for x in stopevents]):
                #                        break
                #                else:
                #                    if "stimulus.sequence" in stopevents.type:
Ejemplo n.º 39
0
        data = np.reshape(data, (1,-1))             # Data now, is an bunch of array containing the power spectrum of every channel in the selected frequencies (#ch, #freq), in this line all those arrays become one of size (1, #ch * #freq)
        
        prediction_right = classifier_right.predict(data)    # The freq informations is now passed to the classifier, each one yielding a float value
        prediction_left  = classifier_left.predict(data)
        
        pred = [prediction_left, prediction_right]                        # The predictions are put into an array, for debugging reasons, this isn't what will be passed
        prediction = [np.round(prediction_left).astype(int), np.round(prediction_right).astype(int)]    # In this array the predictions are rounded, as the expected values are 0,1 for each classifier but the outputs are float, they need to be rounded
        

        # In the following lunes, the conversion is made from a the array to a single int value, easier to pass as an event value
        # 0 - left [1, 0]   1- right [0, 1]   2 - both [1, 1]    3 - none [0, 0]

        if prediction == [0, 0]:       # None
                prediction_int = 3
        if prediction == [1, 0]:       # Left Hand
                prediction_int = 0
        if prediction == [0, 1]:       # Right Hand
                prediction_int = 1
        if prediction == [1, 1]:       # Both Hands
                prediction_int = 2

        if verbose: print(pred, prediction)        # Information in terminal, verbose option, for debugging purposes


        bufhelp.sendEvent('classifier.prediction', str(prediction_int))   # After the classifier has done its job, send an event with ethe expected result as value


        # This iteration has ended, now the classifier goes back to the biggining of the loop and waits for more events

Ejemplo n.º 40
0
hdls=initGrid(symbols)
[ _.set(visible=False) for _ in hdls] # make all invisible

## init connection to the buffer
ftc,hdr=bufhelp.connect();

#wait for key-press to continue
[_.set(color=bgColor) for _ in hdls]
txthdl.set(text='Press key to start')
drawnow()
waitforkey(fig)
txthdl.set(visible=False)

plt.ion()

bufhelp.sendEvent('stimulus.training','start');
## STARTING stimulus loop
for ti,tgt in enumerate(tgtSeq):
    
    sleep(interSeqDuration)
      
    #show the target/cue
    print("%d) tgt=%d :"%(ti,tgt))
    [_.set(color=bgColor,visible=True) for _ in hdls]
    hdls[tgt].set(color=tgtColor)
    drawnow()
    bufhelp.sendEvent('stimulus.target',tgt)
    bufhelp.sendEvent('stimulus.trial','start')
    sleep(cueDuration)
      
    # reset the display
Ejemplo n.º 41
0
rect = patches.Rectangle((0-stimRadius/4,0-stimRadius/4),stimRadius/2,stimRadius/2,facecolor=bgColor)
hhi  =ax.add_patch(rect)
hdls.insert(nSymbs,hhi)
[ _.set(visible=False) for _ in hdls] # make all invisible


## init connection to the buffer
ftc,hdr=bufhelp.connect();

#wait for key-press to continue
[_.set(facecolor=bgColor) for _ in hdls]
txthdl.set(text='Press key to start')
drawnow()
waitforkey(fig)

bufhelp.sendEvent('stimulus.training','start')
state=None
## STARTING stimulus loop
for si,tgt in enumerate(tgtSeq):
    
    # YOUR CODE HERE #

    #catch the prediction
    # N.B. use state to track which events processed so far
    events,state = bufhelp.buffer_newevents('classifier.prediction',1500,state)
        
    # reset the display
    hdls[-1].set_xy(stimPos[-1,:])
    [ _.set(visible=False) for _ in hdls]
    txthdl.set(visible=False)
    drawnow()
Ejemplo n.º 42
0
        # 7: apply the classifier, get raw predictions
        X2d = np.reshape(
            data,
            (data.shape[0], data.shape[1] *
             data.shape[2]))  # sklearn needs data to be [nTrials x nFeatures]
        fraw = classifier.predict(
            scaler.transform(X2d))  # normalize the features
        # 8: map from fraw to event values
        # send the prediction events
        for i, f in enumerate(fraw):
            if f < treshold:
                new_p = 0
            else:
                new_p = 1
            print("%d) %f(raw)\n" % (i, new_p))
            bufhelp.sendEvent("errp.prediction", new_p)

    else:
        # DEBUG
        if verbose: print('Collecting data...')

        data, events, stopevents, pending = bufhelp.gatherdata(
            "errp.trigger", recording_lenght, [], milliseconds=True)

        # get all event type labels
        event_types = [e.type for e in events]

        # Send artificial prediction
        bufhelp.sendEvent("errp.prediction", debug_predictions[0])
        print(debug_predictions[0])
        debug_predictions = debug_predictions[1:]