 Hi, my name is Sebastian Amato, and today I'm going to show you how you can use Python inline script in your Open Sesame experiment So basically what we are going to do is we're going to convert this very simple lexical decision task that we have built in the Open Sesame user interface into a Python script Right just so you just so that you get an idea of how the user interface maps on to a Python script This I'm going to assume that you know in principle how Python works, right? So that you're familiar with the Python language and also that you're familiar with Open Sesame If you're not familiar with those things there are plenty of other tutorials available But the goal here in this screencast is just to show how you can use Python in the context of Open Sesame Now a little bit about the experiment that we're going to create. We're going to create a very simple lexical decision task In which each trial starts with a fixation dot. Let's click on it for 500 milliseconds. Just a small dot in the center of the screen Followed by a target word And the target word is just a defined by the variable stimulus Which will is defined here in the block loop. You will see that later This is followed immediately by a keyboard response Which has a timeout of three seconds and response only to left and right key presses This is followed by a sound glass dot ogg a little sound of falling glass But if you click here you see in the run if statement That the sound is only played when the variable correct is zero or in other words when the participant makes a mistake When you press the wrong key or no key And the trial ends with a logger that writes all the variables to file, right? Now so this is the trial sequence One level up. We have the block loop which defines ten trials So the trial sequence is called ten times Each time with a different value for stimulus Category and correct response. So you see here we have five real English words There we have five strings that are not English words This is indicated here, right? So these these are all in the category word all in the category non-word and you see that participant correct Response for words is right and the correct response for non-words is left okay so what we're going to do is we're going to replace these five items here in the trial sequence by a by a Inline script So we're just going to take this inline script and we drop it onto our block loop But and then we say set this item to run for block loop Which means that we will replace trial sequence, right? It's quite unusual thing to do really. Yes, okay So now and we can call this trial script Okay, so now We have a completely empty Python script and you see that in in open sesame An inline script item is basically just a text editor that has two tabs One tab called prepare and one tab called run now you can find more about the Difference between prepare and the run phase in experiments online and the documentation but the basic idea is that during the in the prepare tab you add all the preparatory stuff like for example creating a canvas a canvas is like a Stimulus display So you create that you prepare that during your prepare phase because it can take a little bit of time And it can throw off the timing of your experiment and then in the run phase You only do the the minimum number of things that you need to do during your trial sequence, right? So during the prepare phase, for example, you would create a canvas with a fixation dot with the target You would create a keyboard object. It would load the sample, etc. And then during the run phase You would only show the calm buses collect the keyboard response Play the sound if necessary, but you wouldn't do any of the preparatory stuff that you need to do before Okay, I hope that's a bit clear now, okay So let's start with the fixation dot, right? So we need a a fixation dot and In by an inline script in open sesame visual stimulus displays are called canvas objects So we can do we can create a canvas object like this fix dot underscore canvas is canvas, right? This will create a new canvas object Empty canvas object which we call fix dot canvas now if you want to get help for your for the Python object You can get that in you can go of course go to the website in your browser But you can also in open sesame go to help online help by the inline code And then click on canvas functions This is the documentation for the canvas object You see all the functions that are available to you and what we are going to use is basically the canvas fix dot object Right, so it just creates and here you already see an example that does exactly what we want to do as well We do you just create in a new empty canvas and you draw a fixed on it. Okay, so let's do that fix dot canvas Dot fix dot right so create a new canvas draw fixation dot on it Let's add some comments create a create fixation Dot canvas. All right Now the second thing that we need to do is Create a canvas for the for the for the stimulus right for the word or the non-word Through the target. Let's call it the target create target canvas Okay, target canvas is again, of course an empty canvas And this time we are not going to draw fixation dot on it But we're going to draw a text on it Now say that for example, we want to draw the text dummy on on there You would do it like this right so the text function is really simple You just say dot text dummy and if you don't specify a position It will be shown in the center of the screen just like the fixation dot by the way by default It will be shown in the center of the screen But of course what we want to do is show The variable stimulus right we have a variable stimulus which has a certain string in it and you can do that like this var dot stimulus So let's take a moment to understand what happened. What's happening here. So open sesame has a var object and this var object has And all the experimental variables that you have in your experiment are properties of this var object so If we define as we have in a block loop we define stimulus category and correct response That means that this var object has the properties stimulus category and correct response, right? So var dot stimulus basically does exactly what we want to do It's kind of comparable to using the square bracket notation in the open sesame user interface Okay, so we've created target canvas now we're going to create a keyboard response Let's just call that KKB for keyboard Okay keyboard now So we create a new keyboard response object and we've called it KB Create the keyboard response Now the keyboard response can take if you if you create a new keyboard response. It can take a few Keyword arguments and again, you can find all those keyword arguments You can find the description of all those keyword arguments in the documentation basically the two important keyword arguments are timeout is 3000 right there is a timeout of 3000 milliseconds or three seconds and key list Which is a last list of key names that are accepted in our case left and right So that's it basically we've created our keyboard response Then we need to create our sample right we were going to play back a sound sample create error sound We go we were going to play back in an error sound if the participant makes mistakes So we need to prepare that as well. So the error sound is Sample sampler, sorry and the sampler Always takes a file man file name, right? what we can and The file that we want to play back is is this one glass dot ogg Now we cannot just say glad pass the glass dot ogg to sampler Because the sampler expects that you pass the full file name like including the the directory and place on the on your On your in your file system and not just the name and we can do that like this. So we can say path is pool Glass dot ogg now and then we say sampler Okay, so let's take a moment to understand what's happening here So open sesame has a pool object and this pool object works like a dictionary And you can get Items from this pool dictionary And basically what what how the way it works is that if you if you get glass dot ogg from this dictionary It will return the full path to glass dot ogg on your disk So probably glass dot ogg will be in located in some kind of temporary folder That open sesame uses and this will be and this exact temporary folder will be saved in path and therefore we can Pass a path to sampler, right? I hope the logic is kind of clear Okay, and then no, that's it. Those are the the the The the objects that we need we need a fixation dot canvas target canvas keyboard and an error sound Right. So now we've done all the preparatory stuff And we're going to move on to the run phase in which we're going to actually handle the trial progression, right? So here we've prepared all the things as far as we could and now we're going to move on to the prepare phase to really Do the trial sequence? Run, okay, so we have our fixed fix fixation dot canvas fix dot canvas and we're going to show that That's the first thing that's gonna happen during the trial right? We show the fixation then we wait for 495 milliseconds will which will be rounded up to five five hundred clock dot sleep 495 if you want to if if you Want to know more about how you why? 495 will be rounded up to 500 you can find more information about that in the timing documentation on the on the website Now just to take a moment here Clock is an object that open sesame creates automatically It's your clock and it has a function called dot sleep which sleeps for a specified time It also has a function called time which will return the current time, right? So it's basically very simple clock object Now after showing the fixation dot we're going to show the target canvas target canvas dot show Okay, that's fine and the target canvas is The moment at which the target canvas is shown is important because we need that In order to calculate the response time right because the response time is relative to the moment that the target canvas is shown so To to do that we're going to make use of the fact that show always returns a timestamp So if we say t zero is target canvas dot show T zero will have the value will have the timestamp of the moment that the target canvas was shown Right this we could also get the timestamp of the moment that the fixation dot was shown But we don't really care about it. So we just gonna leave it. We leave it like this, okay? Okay, yes, and then we're going to get a keyboard response goes like this Okay var dot response T1 is KB dot Get key Okay, so what's happening here? So we have our keyboard object that we created before and we call the function get key This function returns two values, which in Python is called a tuple and These two values are the response So the key that was pressed and the time at which the key where it was pressed So the response and the timestamp of the key We're going to assign the response to var dot response So that it becomes an experimental variable, right? If we would do like this it would work as well But the what response wouldn't be an experimental variable and it would for example not be automatically locked And you wouldn't be able to use it elsewhere in your experiment So if you have variables in your Python inline script that you want to that you would consider a Experimental variable right something that you want to use outside the scope of this this this little script you can make it You can assign it As a property of the var object and it will become an experimental variable Okay, now we need to have the response time, right and The response time is of course just a var dot response time is T1 minus T0 right so it's a difference between the moment at the time the key was pressed and the moment that the target compass was shown Then we want to Create an experimental variable for the correct status right whether the the response was correct or not We can do that like this if var dot response Is var dot correct response Remember we defined the variable correct response in the block loop so it exists Oh, Sesame knows about it and we've created var dot response ourselves here Okay, if var dot response is var dot correct response then var dot correct is one else For adult correct is zero, right? So if the response matches the correct response the variable correct will be one otherwise the variable correct will be zero We need to do one other thing you remember if the participant made a mistake Then we were gonna play the sound the error sound so If there is a mistake we can also say error sound hope error Sound dot play okay, so we play the error sound Okay, that is basically our entire trial sequence What we shouldn't forget at the end is to log all the variables for that open Sesame has a again a special object called log And you can do various things with it You can write all kinds of information to the log file, but the easiest is to just say write vars And it will write all experimental variables to the log file. Let me just check be sure that I say this correctly By the inline code you see so even I have to refer occasionally to the documentation to see how things work Write vars right there you go. So you can just write all your variables like this Okay, so let's save And let's go through it one more time Comment it a little bit. Okay show fixation dot and wait right for a little moment Show target canvas and get the timestamp right the moment at which the target canvas was shown Get the response and of course the timestamp of the response Do some bookkeeping bookkeeping in the sense that we determine the response time We determine the correct status, etc. We play a sound if If it's incorrect and then write Data to log file, okay So this this little script has exactly the same behavior as the the experiment that we had to begin with which didn't use any script Right, so basically we're make we were making it unnecessarily hard on ourselves, but it's a good Example, so I'm just playing it to give you see let you Let you see that it actually works Test guy Keem is incorrect read guy Tesser incorrect left Right if I make a mistake. Oh less Okay, so there we go