Howdy, Stranger!

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

Supported by

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


    import random import string letters = string.letters[:26] # only capitals random.shuffle(letters) # randomize letters no_letters = 5 # how many letters per search display target = letters.pop() # define a target stimuli.append(target) # add it to the list of stimuli while len(stimuli) !=no_letters: stimuli.append(letters.pop())

    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

    Buy Me A Coffee

  • edited June 2017

    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:

    import random
    import string
    
    
    # Creating a list that contains
    # list of upper case alphabets
    
    letters = list(string.ascii_uppercase) 
    
    # Creating a stimulus list (stim_list1) that contain 4 letters randomly selected from the 
    # variable "letters". This stimulus list is created when the variable stimulus set variable
    # from the loop has a value of 4 and the target variable from the loop has a 
    # value set to "On". The target is then selected from this stimulus list called
    # here stim_list1 and converted into a lower case letter string and stored in
    # a variable called tar1_l
    
    if var.Stimulus_set == 4: and var.Target == "On": 
        stim_list1 = random.sample(letters, 4)
         tar1 = random.sample(stim_list1, 1)
         tar1_l = map(str.lower, tar1) 
    

    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 :wink:

    Thanks
    Vatsal

  • Hi vatsal,

    Your previous code would've selected the last string as a target from the variable list

    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.

    letters = list(string.ascii_uppercase) 
    # shuffle your letters
    random.shuffle(letters)
    stim_list1 = []
    for i in range(var.Stimulus_set):
        stim_list1.append(letters.pop())
    if var.Target == "On":
        tar1 = random.sample(stim_list1,1)
    else:
        nontar = letters.pop()
    

    Does that help?
    Eduard

    Buy Me A Coffee

  • edited June 2017

    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:

    # first you shuffle the letters
    random.shuffle(letters)
    #then you create an empty stim_list1
    stim_list1 [ ]
    
    #Then you set up a for loop that would extract n items items from shuffled letters and add them to empty list stim_list1
    
    for i in range(var.Stimulus_set):
        stim_list1.append(letters.pop())
    
    # Then if the var.Target is set to "On" then a target variable will be randomly selected and stored in the variable tar1 
     tar1 = random.sample(stim_list1,1)
    
    # Or else if the var.Target is set to "Off" a non target will be created and stored it in a variable called nontar
    
    nontar = letters.pop()
    

    However, I am making a small change in the above code in order make the target and non target in lowercase

    letters = list(string.ascii_uppercase) 
    
    random.shuffle(letters)
    stim_list1 = []
    for i in range(var.Stimulus_set):
        stim_list1.append(letters.pop())
    if var.Target == "On":
        tar1 = random.sample(stim_list1,1)
        tar_lower = map(str.lower, tar1)
    else:
        nontar = letters.pop()
        nontar_lower =map(str.lower, nontar)
    

    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 :wink: 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? :confused:

    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 replace var. anymore. I relabelled var.stim_list to stim_list1 and that did the trick.

    Eduard

    Buy Me A Coffee

  • 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 :confused:

    Thanks

    Vatsal

Sign In or Register to comment.

agen judi bola , sportbook, casino, togel, number game, singapore, tangkas, basket, slot, poker, dominoqq, agen bola. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 50.000 ,- bonus cashback hingga 10% , diskon togel hingga 66% bisa bermain di android dan IOS kapanpun dan dimana pun. poker , bandarq , aduq, domino qq , dominobet. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 10.000 ,- bonus turnover 0.5% dan bonus referral 20%. Bonus - bonus yang dihadirkan bisa terbilang cukup tinggi dan memuaskan, anda hanya perlu memasang pada situs yang memberikan bursa pasaran terbaik yaitu http://45.77.173.118/ Bola168. Situs penyedia segala jenis permainan poker online kini semakin banyak ditemukan di Internet, salah satunya TahunQQ merupakan situs Agen Judi Domino66 Dan BandarQ Terpercaya yang mampu memberikan banyak provit bagi bettornya. Permainan Yang Di Sediakan Dewi365 Juga sangat banyak Dan menarik dan Peluang untuk memenangkan Taruhan Judi online ini juga sangat mudah . Mainkan Segera Taruhan Sportbook anda bersama Agen Judi Bola Bersama Dewi365 Kemenangan Anda Berapa pun akan Terbayarkan. Tersedia 9 macam permainan seru yang bisa kamu mainkan hanya di dalam 1 ID saja. Permainan seru yang tersedia seperti Poker, Domino QQ Dan juga BandarQ Online. Semuanya tersedia lengkap hanya di ABGQQ. Situs ABGQQ sangat mudah dimenangkan, kamu juga akan mendapatkan mega bonus dan setiap pemain berhak mendapatkan cashback mingguan. ABGQQ juga telah diakui sebagai Bandar Domino Online yang menjamin sistem FAIR PLAY disetiap permainan yang bisa dimainkan dengan deposit minimal hanya Rp.25.000. DEWI365 adalah Bandar Judi Bola Terpercaya & resmi dan terpercaya di indonesia. Situs judi bola ini menyediakan fasilitas bagi anda untuk dapat bermain memainkan permainan judi bola. Didalam situs ini memiliki berbagai permainan taruhan bola terlengkap seperti Sbobet, yang membuat DEWI365 menjadi situs judi bola terbaik dan terpercaya di Indonesia. Tentunya sebagai situs yang bertugas sebagai Bandar Poker Online pastinya akan berusaha untuk menjaga semua informasi dan keamanan yang terdapat di POKERQQ13. Kotakqq adalah situs Judi Poker Online Terpercayayang menyediakan 9 jenis permainan sakong online, dominoqq, domino99, bandarq, bandar ceme, aduq, poker online, bandar poker, balak66, perang baccarat, dan capsa susun. Dengan minimal deposit withdraw 15.000 Anda sudah bisa memainkan semua permaina pkv games di situs kami. Jackpot besar,Win rate tinggi, Fair play, PKV Games