Welcome!

Sign in with your CogSci, Facebook, Google, or Twitter account.

Or register to create a new account.

We'll use your information only for signing in to this forum.

Supported by

Reset variable for adaptive procedure

Good Evening,

I'm still quite new to Python (and programming in general) and trying to code a working-memory task (word span sequences) with an adaptive number of items to be remembered, depending on the performance in the trial before; however it is only adapted every two trials. In total there are 10 trials. I have a set of 9 words, from which randomly a number "n" is drawn.
So let's say I start with 3 words in a sequence and get two trials right, the number of words for the next two trials should increase to 4. If I get these next two wrong, the number should reduce to 3 again. If I get one correct and one wrong, the number of words per sequence should remain the same.
The words are to be presented one by one acoustically (with a ISI of 1500ms), and at the end the whole sequence is presented to the person administering the test. That person scores with "1" or "0" whether the response was correct or not (i.e. whether the sequence was reproduced correctly)

So I created a loop that contains 10 trials in sequential order and a "reset" variable that is alternatingly "0" and "1". The loop calls a sequence that contains two inline scripts. The first is for resetting my "adapt"-variable to 0, the second contains the actual trial.

So far so good, but the adaptive procedure "every other trial" does not seem be stable, Python produces quite frequent crashes, mostly after 5-7 trials. Most of the time the window just freezes and has to be shut down manually.

I assume that my coding is erroneous or contains pitfalls that I naively don't see ;)
I begin with evaluating whether the "adapt"-variable is +2 or -2 (from the previous two trials) and accordingly adjusting "n" (passed to function random.sample)

if adapt == -2:
    var.n = var.n - 1
elif adapt == 2: 
    var.n = var.n + 1

After the presentation of the sequence, I collect the keyboard response (0 or 1), adjusting "adapt" accordingly

if var.correct == 1:
    adapt = adapt + 1
else:
    adapt = adapt - 1

After every other trial, the "adapt" variable should be reset to zero, so that it only reflects two trials. I had to insert an extra inline script because within the actual trial-script it would not work at all. This way works sometimes, however not very stable. It seems to work when I omit the "adapt" procedure every two items and change the number of words per sequence after every trial:

if var.correct == 0:
    var.n = var.n - 1
elif var.correct == 1: 
    var.n = var.n + 1

I also get a

UserWarning: var name is stored as attribute of item experiment % (var, self.item.name))

which I found in another post (http://forum.cogsci.nl/index.php?p=/discussion/2616/random-crashes-on-droid-back-end-possibly-connected-to-var-store-warning), but appearantly it is not related to the issue? (although however, the phenomenon seems quite similar). In the other post it was suggested that memory may be an issue, however I'm running OS on a regular windows Laptop and am not using large files.

So do you have any ideas how I could solve this; maybe a completely different and more reasonable approach?
It will be very much appreciated :)

Cheers,
Gunnar

Comments

  • sebastiaansebastiaan Posts: 2,856

    Hi Gunnar,

    Although you've provided a quite extensive description of the problem (which is always good and appreciated), it's still difficult to see what might be wrong without some more context. It would help if you post the exact error message that you get when the experiment crashes. Attaching the experiment to this discussion would also help.

    Cheers!
    Sebastiaan

    There's much bigger issues in the world, I know. But I first have to take care of the world I know.
    cogsci.nl/smathot

  • gbrunsgbruns Posts: 4

    Hi Sebastiaan,

    thanks for your answer. Please find attached the experiment.
    After a few items the experiment usually freezes after the keyboard response, so I have to shut down the experimental window "by force". After that the error message simply says that Python crashed. Besides there is a warning about non-integer numbers (which however also shows up when the experiment mysterically finishes without problem ;) ). Sometimes it suddenly shuts down after the last item was presented but also with the only error message "Python crashed".

    C:\Program Files (x86)\OpenSesame\lib\site-packages\numpy\core\numeric.py:190: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
    a = empty(shape, dtype, order)
    C:\Program Files (x86)\OpenSesame\lib\site-packages\libopensesame\var_store.py:236: UserWarning: var name is stored as attribute of item experiment
    % (var, self.item.name))

    Python seems to have crashed. This should not happen. If Python crashes often, please report it on the OpenSesame forum.

    I've been using Expyriment backend, but also Legacy didn't make any difference.
    Thanks again! :)
    Gunnar

  • sebastiaansebastiaan Posts: 2,856

    Hi Gunnar,

    The freezing (which probably results in the Python interpreter crashing) is due to a simple logical bug in your code, leading to an infinite loop. Can you spot the issue here?

    var.sequence = random.sample(echt_kurz, var.n)
    if var.count_sequence > 0:
        while var.sequence[0] == firstitem:
            random.sample(echt_kurz, var.n)
    

    (Other than that, the error message that you see indicates that the Python interpreter has crashed, which should never happen, not even if you make a mistake in your code. I do suspect it's indirectly triggered by the infinite loop though.)

    Cheers!
    Sebastiaan

    There's much bigger issues in the world, I know. But I first have to take care of the world I know.
    cogsci.nl/smathot

  • gbrunsgbruns Posts: 4

    Hi Sebastiaan,

    ah yes of course, I was too focused on the other part :smiley: thanks!
    but it should work if I just assign the new random sample to the sequence variable in the while-loop, shouldn't it? If not, is there a way to avoid that two sequences in a row begin with the same word?

    while var.sequence[0] == firstitem:
            var.sequence = random.sample(echt_kurz, var.n)
    

    And I also have a follow-up question ;)
    As I will be presenting the stimuli acoustically, I need to prepare the samplers for each word of the sequence, so that I can simply loop through the sampler.play() for each word in the run phase. To my understanding it should be enough to prepare the sampler for each word once for the whole experiment (so I don't have to do it in every prepare-phase), is that correct? Something like

    wort_sound_list = []
    for i, word in enumerate(echt_kurz):
        word_sampler = exp.pool[word + '.ogg']
        word_sound = sampler(word_sampler)
        word_sound_list.append(word_sound)
    

    So then I could call the word_sound.play() in the run-phase for each element of the sequence. I would like to do something like the following, using [word] as a variable to call only the word_sounds that are element of the current sequence

    for word in var.sequence:
        word_sound.play()
        clock.sleep(var.interval - 5)
    

    This however will not work, as I cannot get the "word_sound.play()" to be dynamic (it just takes the last value of "word_sound" from the loop before; if I use a different index (e.g. "i") there is a warning that i_canvas does not exist). This is obvious, but is there a way to make the names of sounds (or canvases respectively) dynamic?
    Or do I have to prepare all the samplers for each word of the sequence anew in the prepare-phase for every trial? I hope it becomes clear what I'm trying to achieve?

    Thank you a lot
    Cheers,
    Gunnar

  • sebastiaansebastiaan Posts: 2,856

    Hi Gunnar,

    What you're looking for is a dict. Right now you're creating a list of sampler objects, which makes it tricky to associate each sampler with a word (which effectively what you want to do). If you use a dict, you can use a word as the key, and the sampler object as the value. Then it's easy to retrieve the correct sampler object during each trial. Does that make sense? (I'll leave it to you as an exercise to implement this.)

    To my understanding it should be enough to prepare the sampler for each word once for the whole experiment (so I don't have to do it in every prepare-phase), is that correct?

    Totally.

    Cheers,
    Sebastiaan

    There's much bigger issues in the world, I know. But I first have to take care of the world I know.
    cogsci.nl/smathot

  • gbrunsgbruns Posts: 4

    Hi Sebastiaan,
    alright, thank you for the hint, and your 'didactic' answer ;) I'll give it a shot...
    Cheers,
    Gunnar

Sign In or Register to comment.