Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to: Python and OpenSesame FOR PROS. OpenSesame In OpenSesame you can add Python in-line codes which enables complex experiment. We will go.

Similar presentations


Presentation on theme: "Introduction to: Python and OpenSesame FOR PROS. OpenSesame In OpenSesame you can add Python in-line codes which enables complex experiment. We will go."— Presentation transcript:

1 Introduction to: Python and OpenSesame FOR PROS

2 OpenSesame In OpenSesame you can add Python in-line codes which enables complex experiment. We will go over a few examples of tasks that require coding. In this session, you should work with me on the examples (work on the _blank files) Intro to: Python and OpenSesame2

3 OpenSesame Opensesame has a debugger window, which is actually a Python Shell! Open the debugger and Python around! Hint: We can use print statements to the debugger to see if the code we wrote is good Intro to: Python and OpenSesame3

4 OpenSesame + Python Interacting with OpenSesame with the Python in-lines is through objects. [Recall: Turtle!] For that we need to: 1.Import the modules relevant for the objects we need 2.Create instances 3.Use their methods Intro to: Python and OpenSesame4 Import turtle yoni = turtle.Turtle() yoni.forward(100)

5 In-Line Object To be able to write Python code, you need to add a Python in-line object to the sequence. You can add multiple Python in-lines to a task, but they act as one large script (so a variable defined in one is available in the other). Intro to: Python and OpenSesame5

6 In-Line Object You can append the in-line object in different places in the experiment. For example, in the start of the experiment to initialize some variables. Or during a trial loop to alter variables after a trial. Intro to: Python and OpenSesame6

7 OpenSesame + Python http://osdoc.cogsci.nl/python/ This link has all the objects that OpenSesame has and how to interact with them. Intro to: Python and OpenSesame7

8 In-line Variables First thing we need to learn is how to interact with OpenSesame Variables. That is to get the value of a variable And set the value of a variable Intro to: Python and OpenSesame8

9 Example #0 – working with inlines Open a new experiment in OpenSesame and drag an inline object to the experiment sequence. We can get access OpenSesame experiment variables with the self.get(“var”) method. the input is a string with the name of the opensesame variable Intro to: Python and OpenSesame9 The self object is actually the in-line script object. The get method makes experiment variables available to it.

10 Example #0 – working with inlines To know which variables are available in OpenSesame, we can check the Variable inspector. Do it yourself: Set a python variable num to be the subject number (an experiment variable). Print it to the debugger Intro to: Python and OpenSesame10 self.get(‘variable’)

11 Example #0 – working with inlines To use variables we defined in the in-line script outside the in-line script, we need to set them as OpenSesame experiment variables. We can use the exp.set(“var”,value) method. The input is a string with the name of the variable and a value Intro to: Python and OpenSesame11 Exp is the experiment object and the set method sets the variable to some value. You can create a new variable / update an existing variable

12 Example #0 – working with inlines Do it yourself: Set an experiment variable msg to be the message: Welcome subject number : (use the subject number) Intro to: Python and OpenSesame12 exp.set(‘var’,value)

13 Example #0 – working with inlines Using variables that were set in inline code: 1.Create a sketchpad object after the python inline object. 2.Print the message in the experiment variable msg with the sketchpad Intro to: Python and OpenSesame13

14 Prepare / Run Opensesame has two phases: Prepare – runs first, before running a sequence Run – runs only when called Intro to: Python and OpenSesame14 This is true for every object, as well as the sketchpad object. The prepare phase of the sketchpad object couldn’t recognize the msg variable because it wasn’t there yet.. We should create our variable in the prepare phase of the inline script

15 Example #1: Updating during Exp For our first [real] example we will create the stop-signal task. In this task participants are presented with two types of stimuli and they need to make a speeded discrimination. On 25% of the trials, a beep appears after the presentation of a stimuli and the participant needs to withhold their response. Intro to: Python and OpenSesame15

16 Example #1: Updating during Exp If the participant succeeded in avoiding a response, the SSD [Stop-Signal Delay] is increased, so that the time between the appearance of the stimuli and the stop-signal is longer (harder to stop) If the participant responded, the SSD is decreased, so that the time between the appearance of the stimuli and the stop-signal is shorter (easier to stop) Intro to: Python and OpenSesame16

17 Example #1: Updating during Exp We need a way to control the change in the SSD during the task. For that we need some in-line scripting. Open the stop-signal task Intro to: Python and OpenSesame17

18 Example #1: Updating during Exp 1.First, we need to define our SSD variable as an experiment variable [set it to 200ms]. 2.Make sure that the experiment uses our SSD variable. 3.We need to update SSD to: – +20 if the subject didn’t respond – -20 if the subject did respond Intro to: Python and OpenSesame18

19 1. Define SSD Create a new inline object in the head of the experiment. Set a new experiment variable called SSD and set it to 200. Intro to: Python and OpenSesame19

20 2. Setup the experiment (key idea) Intro to: Python and OpenSesame20 Target appears Stop Signal SSD Keyboard Response Keyboard Response Duration

21 2. Setup the experiment We will use SSD as the duration for the key1 keyboard response object (the response after presentation of the stimuli and before the beep. We want to make sure that we get 2000ms of response time available, so we should set the timing of key2 to be 2000-SSD Intro to: Python and OpenSesame21

22 3. Update SSD – Key Ideas We want to update SSD according to the response after the beep (yes / no). But what happens if the subject pressed before the beep? Do we update? What if the participant doesn’t press anything? Can SSD get to below zero? Intro to: Python and OpenSesame22

23 Update SSD Add a new in-line script object after the key2 response. Create the conditions needed to update SSD according to the task rules. Intro to: Python and OpenSesame23 Note: Keyboard response objects create an experiment variable that stores the response. It stores “None” if no key was pressed…

24 Test Drive Run the experiment Intro to: Python and OpenSesame24

25 Example #2: Drawing Objects We will learn how to draw objects on the screen with this next example. In the visual search task a few objects appear on the screen and the participant needs to say if a target object is present. Intro to: Python and OpenSesame25

26 Intro to: Python and OpenSesame26

27 Example #2: Key Idea Intro to: Python and OpenSesame27 (x_cent,y_cent)

28 Example #2: Drawing Objects We need a way to draw shapes randomly on the screen. For that we need some in-line scripting. Open the visual_search task Intro to: Python and OpenSesame28

29 Canvas object In order to present things on the screen, we need access to the canvas object. We can import it: from openexp.canvas import canvas Intro to: Python and OpenSesame29 Note: Opensesame doesn’t follow the Python convention of writing objects with a first capital letter.

30 Using Canvas 1.Create an instance of canvas (my_canvas) 2.Draw things on it (with methods) 3.Show it on the screen (the show() method) Intro to: Python and OpenSesame30

31 Inline script - Overview 1.Import the libraries we need 2.Get and set the relevant variables and instances 3.Plot stimuli on the screen 4.Show screen Intro to: Python and OpenSesame31

32 1. importing We need two modules – 1.Canvas (from openexp.canvas import canvas) 2.random Intro to: Python and OpenSesame32

33 2. Get/Set relevant variables We need: 1.Access to the num_items and target experiment variables. – Get from the experiment loop 2.A canvas instance (my_canvas) – my_canvas = canvas(exp) 3.Set variables with the center coordinates – Canvas methods xcenter() and ycenter() Intro to: Python and OpenSesame33

34 3. Plot Stimuli We need to: 1.Randomly place (num_items – 1) distractors 2.Randomly place a target stimulus or another distractor (depending on the target [yes/no] value) Intro to: Python and OpenSesame34 We will use circles as the stimuli: Distractors are red Target is green Syntax: my_canvas.circle(x, y, radius, fill=[True/False], color=‘’”)

35 Test Drive Run the experiment Intro to: Python and OpenSesame35

36 Visual Search Versions The task has two versions: Easy(pop-out) – the distractors are in the same shape and color, the target has a different color Hard(Feature conjunction) – the distractors have varying shapes and colors, the target stimulus is a unique combination of shape and color. Intro to: Python and OpenSesame36 Home exercise – try to create the hard version

37 Example #3: Control We will learn how to use input devices to control changes in the experiment. We will create a basic sensitivity learning task. a participant sees a reference stimulus and needs to set the size of another target stimulus to be as close as possible to the reference. Intro to: Python and OpenSesame37

38 Example #3: Key Idea Intro to: Python and OpenSesame38 Reference Stimulus Target Stimulus “pressed” You are off by: 10%

39 Inline script - Overview 1.Import the libraries we need 2.Get and set the relevant variables and instances 3.Present the starting position 4.Wait for mouse response 5.Increase target size while button is pressed 6.Calculate size difference 7.present feedback Intro to: Python and OpenSesame39

40 1. Importing We will use modules for: 1.A canvas object to draw stuff on the screen – from openexp.canvas import canvas 2.A mouse object to control the size – from openexp.mouse import mouse Intro to: Python and OpenSesame40

41 2. Get/Set relevant variables We need: 1.A canvas instance – my_canvas = canvas(exp) 2.A mouse instance (with timeout of 2 seconds) – my_mouse = mouse(exp,timeout = 2000) 3.x,y coordinates of the center of the screen – Canvas methods xcenter() and ycenter() 4.The size of the reference stimulus – Get from the experiment loop 5.A baseline size 6.A scalar value Intro to: Python and OpenSesame41

42 3. Presenting the starting position We need to: 1.Draw the reference stimulus (with given size) – Canvas method circle() 2.Draw the target stimulus (at baseline size) – Canvas methods circle() 3. Show the canvas – Canvas method show() Intro to: Python and OpenSesame42

43 3. Wait for mouse response We need to: 1.Wait for a mouse click response for a maximum duration of 2 seconds. Intro to: Python and OpenSesame43 button, position, timestamp = my_mouse.get_click() Returns three variables, the buttons pressed, the position on the screen and the time the click happened

44 5. Increase target size While the mouse button is pressed: 1.clear the canvas (helps reduce memory consumption) – Canvas method clear() 2.Increase target size by scaler 3.Re-draw the reference and target (new size) stimuli 4.Show canvas 5.Re-check if button is still pressed Intro to: Python and OpenSesame44 buttons = my_mouse.get_pressed() Returns a tuple with the state [True/False] of all three mouse buttons in the form of (0, 0, 0)

45 6. Calculate size difference After the button is no longer pressed: 1.Calculate the ratio between the target size and the reference size. – If the target is size 150 and the reference is 100, you should get a size_diff of +50% 2.Create a string to present as feedback: – you are off by : x % 3.Save the size difference as an experiment variable Intro to: Python and OpenSesame45

46 7. Present Feedback 1.Draw the final size of the two stimuli 2.Present the text string – Canvas method text() 3.Show canvas 4.Pause for 1 second – Inline method sleep(ms) Intro to: Python and OpenSesame46

47 Test Drive Run the experiment Intro to: Python and OpenSesame47

48 Course Summary For the last month we studied a bit of the Python programming language and the OpenSesame experiment building platform. Hopefully you have seen how with few lines of code you can create complex and interesting experiments. Intro to: Python and OpenSesame48

49 Course Summary What to do next? Analyzing Results with Excel Intro to: Python and OpenSesame49

50 Course Summary As always, I encourage you to take what you’ve learned here to the next level: Study more of Python Build crazy and complex experiments with code Intro to: Python and OpenSesame50

51


Download ppt "Introduction to: Python and OpenSesame FOR PROS. OpenSesame In OpenSesame you can add Python in-line codes which enables complex experiment. We will go."

Similar presentations


Ads by Google