How to get a cedrus response to work with pyxid?

Dear discourse community,

I’m writing an n-back paradigm and I would like to program a cedrus response box for participants to press if the word is semantically related and another button if the word is semantically unrelated. I am having trouble working with pyxid and getting the response box to be recognised in my script (although it seems to work in the shell). Further, I am a little unsure on how to write code for the response box to await for key presses. Please see below for full code:

from numpy.random import choice
from psychopy import core, visual, gui, data, event, parallel
from collections import defaultdict
from psychopy.tools.filetools import fromFile, toFile
import numpy as np
import win32api
import pyxid

#get a list of all attached xid devices
devices = pyxid.get_xid_devices()

#Particiapnts information GUI
info = {'Participant':'','ExpVersion': 1.1,
        'Group': ['Pilot']}
OKGO = 0 
while OKGO == 0:
    dictDlg = gui.DlgFromDict(dictionary=info,
            title='Prospective memory - on going', fixed=['ExpVersion'])
    if len(info['Participant']) == 0:
        print "did not enter participant number"
        win32api.MessageBox(0,'You did not enter the participant number','Error')
    else:
        OKGO = 1
if dictDlg.OK:
    print(info)
else:
    print('User Cancelled')


#window
myWin = visual.Window([800,600],allowGUI=True, monitor='laptop', units='deg')

# fixation cross
myFix = visual.ShapeStim(myWin, 
    vertices=((0, -0.5), (0, 0.5), (0,0), (-0.5,0), (0.5, 0)),
    lineWidth=5,
    closeShape=False,
    lineColor='white'
)

number_of_elements = 10

#words
words = open('D:/PhD/Paradigms/Prospective memory/test_word_list.txt').read().split()

def default_probs():
    f = np.ones(len(categories))
    return f/f.sum()

 def update_probs(category):
     f = np.ones(len(categories)) * number_of_elements
     f[category] += nrep
     return f/f.sum()

 def get_nrep(categories, prob=0.70):
    n_reps = (len(categories) - 1.0)/(1-prob) - (len(categories) - 1.0)
    return int(round(n_reps))

#textStim
 word_presentation = visual.TextStim(myWin, pos=[0,0],text=u'')

categories = defaultdict(list)
for i, word in enumerate(words):
    categories[int(i/number_of_elements)].append(word)

category_labels = categories.keys()
nrep = get_nrep(categories, prob=0.70)
prob = default_probs()

max_iterations = 25
prob = default_probs()
hold_category = -1
category_repeated = -1
for iteration in xrange(max_iterations):
    myFix.draw()
    myWin.flip()
    print('Iterations is %d' % iteration)
    print('Prob is %s' % str(prob))
    category = choice(category_labels, p=prob)
    print 'category is:'
    print category

    if category == hold_category:
        category_repeated = 99
    hold_category = category
    words_in_category = categories[category]

    sampled_word = choice(words_in_category)

    word_presentation.text = sampled_word
    word_presentation.draw()
    myWin.flip()
    core.wait(2)
   
    if category_repeated == 99:
        print "Category has repeated!"

    dev = devices[0]
    if dev.is_response_device():
        dev.reset_base_timer()
        dev.reset_rt_timer()
        while True:
            dev.poll_for_response
            if dev.response_queue_size() > 0:
                response = dev.get_next_response()
                print "it worked"
    prob = update_probs(category)
    category_repeated = -1

    print('Category is "%s". Word is "%s"' % (category, sampled_word))
    allKeys=event.getKeys()
    for thisKey in allKeys:
        if thisKey in ['q', 'escape']:
            core.quit() #abort experiment
            print 'Escape key has been pressed' 

Thank you in advance for any help/advice

Mark

Just an update for this problem. It turns out that this is a problem with the response box and the drivers for windows 8 and above.

One of our own users @cbrnr recently raised an issue on the pyxid repository that sounds like the issue you’re having?
https://github.com/cedrus-opensource/pyxid/issues/12

He suggests that some recent changes they made have broken the pyxid lib. I’m currently rebuilding PsychoPy Standalone to use the version of pyxid that he found to work

1 Like