Suggestion regarding an inline script
Hi !
I am building a task to assess working memory function. I have quick query. I want to display random number of letters for which I would like to create a variable that would contain random number of letters each time the variable is called. This variable is called 'letters'. I am thinking to define the variable on an inline code script which will be attached to the very beginning of the experiment. I am totally new to python.
The intention to define the variable in an inline script is to reduce the constrain on the experiment design since incorporating 26 levels (i.e., the number of English alphabets) under a single variable would cause the experiment to be too long with too many trials. However, using an inline code would deliver this by generating a unique alphabet every time the variable 'letters' is called in the trail.
Description of the task:
** A black “+” symbol is displayed at the center of the screen for 500 ms as a reminder to the participants to pay attention. Subsequently, a blank screen is displayed for approximately 600 ms before a letter loop appears. In a clock-like loop, 4–10 upper case letters are displayed in a random order for 1,500 ms each. After the letter loop disappears, the screen remains blank for 1,300–1,800 ms before a lowercase letter appears at the center of the screen for a further 1,500 ms. Participants must determine whether this letter was among the letters in the clock-like loop (irrespective of case). Subsequently, another blank screen is displayed for 1,000 ms, after which the task ends**
I have created the following code for this purpose.
import random
import string
var = random.choice(string_letters)
Questions 1: Is this the correct way to do it?
Question 2: I am not sure whether I should write the code in the pre-run phase or in the run phase?
Design for my experiment is:
ISI (with 6 levels) X stimulus_set ( 7 levels)
I would be grateful if someone could help in this please
Thanks
Vatsal
Comments
Hi Vatsal,
Here a little snippet of code that does what you want to
So from here one, you have the letter identities stored in the stimuli list, in which the first item is the target and the others are the distractors (all capitalized).
Good luck from here on.
Eduard
Hi Eduard!
Thanks for responding and the code.
I intend to use inline script to create a list of upper case letters and then randomly select 4 to 10 sets of letters and then present them. Following presentation I will present a 'lowercase letter' that can be same or different than from the set of letter presented previously, this would be our 'target' letter. I had posted a similar query in a post of mine to which you've responded. After following the tutorial of opensesame advance with inline code and few tutorials on python I have come up with the following code:
Your previous code would've selected the last string as a target from the variable list [ as suggested by letters.pop()] dont you think it would cause the task to be anticipatory? Here although I've managed to create a target but I am not sure how should I go about in creating a non target which is pretty tricky. The non target should not contain any of the letters present in the stimulus list. So, if the Target variable is set to Off and the Stimulus set is set to a value of 4 I need to create a non-target that should contain a letter randomly selected from the variable 'letters' but excluding the ones that are present in the stimulus list 'stim_list1'.
I might be wrong though and I am still a newbee in python
Thanks
Vatsal
Hi vatsal,
No, because I randomize the letter list, before I pick the last item. Therefore, the target will be random.
Using
random.sample
is a good option, but given your constraints, I'd probably use a different routine.Does that help?
Eduard
Hey Eduard!
Thanks for replying
This code looks good. Although, I am new to python I just need to know whether I've got it correctly
so:
However, I am making a small change in the above code in order make the target and non target in lowercase
The code runs perfectly when I execute it. However, the next big challenge was displaying the items from the stim_list1. The tutorial on opensesame with inline code mentioned that list items cannot be used to display the items on a sketchpad since variables cannot be a list.
Though, I played a little gamble and searched for a similar problem I found this discussion where someone is displaying letter strings that have been stored in a variable defined previously in an inline script. So, I ran the experiment and I got an error saying "variable stim_list1 does not exist". I had got this error previously also so I followed this solution from a discussion i.e., adding an inline script at the very beginning of the experiment and coding it with the following.
var.stim_list1 = 0
After running the experiment I faced another error which is something related to error executing inline script. I have attached the screenshot (error1.jpg).
Looks like I have to display items of the stim_list1 by creating a canvas object as described in the tutorial. Isn't it?
Thanks
Vatsal
I have attached the experiment file
Thanks
Vatsal
Hi vatsal,
Attached the updated experiment, something was weird with the run-if statements. The load 5 blocks intervened even I set them to never be executed. Don;t know why this was, but for sakes of testing the load 4 condition, I deleted all the blocks with a load higher than 4.
The condition 4 I tested and it works now. Basically the issue was, that if you use python code in a
sketchpad
, (by using this syntax[=<pythoncode>]
, you have to refer to the variables directly by name. using[]
doesn't replacevar.
anymore. I relabelledvar.stim_list
to stim_list1 and that did the trick.Eduard
Hey Eduard!
Thanks for the experiment. It is indeed running but there are another issue. The experiment should run in the following sequence:
Fixation > Blank screen > Presentation of letters that range from 4 to 10 depending on the variable "Stimulus_set" > A blank screen duration of which is varied between 1300 to 1800 millisecond i.e., according to variable "ISI" > Target/Non-Target presentation the presentation of which is varied according to the variable "Target" > Another Blank screen.
Although, the experiment should ideally run according to the sequence mentioned above but it doesn't. Whats happening is that it simply displays the string of letters once and then there are only target screen being displayed. The target/non target screen should be presented only once and after which it should wait until the next string of letters (4-10) are flashed. I guess this is happening because there are no block loads more than 4 and those extra target-non target screen are of the other block loads right?
Lastly, I am really indebted to your time and effort and if I am able to pull this off I am confident to build more complex task in opensesame. Nevertheless, I couldn't have done this without your help!
Once again Thanks a ton!!
Vatsal
Hey
A little heads up. I checked why the experiment repeatedly showed the target non target slid. Indeed it is the other block loads that is causing the issue. However, I discovered that the experiment is displaying only a particular set of letters which should not be the case it should display different set of letters every time the stim_list1 variable is called for. I made a slight change in the inline script:
import random
import string
letters = list(string.ascii_uppercase)
random.shuffle(letters)
stim_list1 == []
random_letters = random.sample(letters,15)
random.shuffle(random_letters)
for i in range(var.Stimulus_set):
stim_list1.append(random_letters.pop())
if var.Target == "On":
tar1 = random.sample(stim_list1,1)
var.tar_lower = map(str.lower, tar1)[0]
var.nontar_lower = None
else:
nontar = letters.pop()
var.nontar_lower = map(str.lower, nontar)[0]
var.tar_lower = None
print stim_list1
Yet again the issue remains the same
Thanks
Vatsal