Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

[solved] Setting the number of cycles in a loop programatically

edited June 2014 in OpenSesame

So I am skipping the built in variables approach as I prefer to use my own arrays and variables for sequencing and presentation of trials.

I do this by defining a set of global variables at the beginning. There are several block sequences within which there are blockloops that contain trial sequences. Although the experiment has many different blocks, the basic structure is the same; thus I can reuse the same objects. The only thing I do is make sure that all the variables (stimulus and response arrays are setup before the block loop begins and within the block loop I have another inline script that sets the presentation stimuli and responses for each trial).

So far so good. I am puzzling how to set the number of trials (cycles) for the loop using a global variable.

In the script mode for the block loop, I see a line that says

set cycles "8"

How can I reset this to a global variable value (say something called gblocksize?) that has been set earlier by an inline script? I know that this script mode can't actually deal with python. What is the trick to getting to accept code? If I just naively say "[gblocksize]" instead of "8" that does not work as the GUI objects.

Thanks.

Comments

  • edited June 2014

    Ok I am using the approach suggested in an older forum post which is just about deleting the loop object and using an inline script to loop in the run tab. When I do this and set the global variable just before the prepare and run for trial sequence, it doesn't seem to recognize the variable inside the sketchpad. Wonder if this is related to scope.

    Here is what I am trying to do

    All variables starting with g have been defined as globals at start of experiment

    contents of RUN tab in the loop in-line script

    for i in range(gblocksize):
            gtrialnumber = i 
            gtrialstimulus = gitems[gitemsorder[gtrialnumber]]
            gtrialresponse = gitemsresponse[gitemsorder[gtrialnumber]]
            exp.items["trialsequence"].prepare()
            exp.items["trialsequence"].run()
    

    Problem is that the sketchpad inside the trialsequence does not recognize [gtrialstimulus] even though values are correctly set inside the loop (can check with print statement). Now earlier I had similar code running in a script inside the trialsequence and that worked. Why should this matter as the g variables are all previously defined as global.

    If I define the gtrialstimulus outside the loop to a fixed value, then the sketchpad seems to recognize it (say in a different inline script that is outside the trialsequence as well).

    So I guess my question is: How can I set the values within the loop so that it correctly changes the global variable gtrialstimulus and is recognized in the sketchpad (as it was doing previously when the code was inside the trialsequence itself.

  • edited 2:24PM

    I do find that the g variables being changed in an inline script inside the trialsequence object are recognized. One other oddity. Inside this loop, the count_trialsequence seems to start from 1, not 0. Is this documented behavior that we can rely on?

  • edited June 2014

    Now its started from zero but it loops the trial n+1 instead of n

    for i in range(8):
            exp.items["trialsequence"].prepare()
            exp.items["trialsequence"].run()
    

    This shows 9 trials, instead of 8. Puzzled. The count_trialsequence does start from zero.

  • edited June 2014

    I've put a print statement that counts the number of trials. It goes from 0 to 8 for some weird reason, instead of 0 to 7. Any ideas?

  • edited June 2014

    Above issue likely due to not turning off the trial sequence using "never". Problem with counter continues (it starts at 1 instead of 0).

  • edited 2:24PM

    https://www.dropbox.com/s/krjy4vs2desdm73/counttrialsequence.opensesame.tar.gz

    shows this counting from 1 instead of zero. Is this normal?

  • edited 2:24PM

    I think this file gets at what I was looking for.. A custom counter that does start from zero. One thing I learned was to use to exp.set and exp.get for simple numeric or string variables. For arrays/objects, the only option is perhaps using the global definition.

    https://www.dropbox.com/s/utk8itsbaootdv4/customcounter.opensesame.tar.gz

  • edited 2:24PM

    Wow, you've been busy! To make life easier for people reading (and possibly answer) your question, could you let us know whether you've been able to resolve this issue, and if not, where you are exactly?

  • edited June 2014

    Yes, this got resolved. See the lessons learnt post for a generic description.

Sign In or Register to comment.