Common functions
The following functions are available in inline_script items:
- Canvas(auto_prepare=True, **style_args)
- Experiment(osexp_path=None, log_path='defaultlog.csv', fullscreen=False, subject_nr=0, **kwargs)
- Form(*args, **kwargs)
- Keyboard(**resp_args)
- Mouse(**resp_args)
- Sampler(src, **playback_args)
- Synth(osc='sine', freq=440, length=100, attack=0, decay=5, **playback_args)
- copy_sketchpad(name)
- pause()
- register_cleanup_function(fnc)
- reset_feedback()
- set_subject_nr(nr)
- sometimes(p=0.5)
- xy_circle(n, rho, phi0=0, pole=(0, 0))
- xy_distance(x1, y1, x2, y2)
- xy_from_polar(rho, phi, pole=(0, 0))
- xy_grid(n, spacing, pole=(0, 0))
- xy_random(n, width, height, min_dist=0, pole=(0, 0))
- xy_to_polar(x, y, pole=(0, 0))
Canvas(auto_prepare=True, **style_args)
A factory function that creates a new Canvas
object. For a
description of possible keywords, see:
Returns
- A
Canvas
object.
Example
my_canvas = Canvas(color=u'red', penwidth=2)
my_canvas.line(-10, -10, 10, 10)
my_canvas.line(-10, 10, 10, -10)
my_canvas.show()
Experiment(osexp_path=None, log_path='defaultlog.csv', fullscreen=False, subject_nr=0, **kwargs)
A factory function that creates a new Experiment
object. This is only
useful when implementing an experiment entirely through a Python script,
rather than through the user interface.
Parameters
- osexp_path: If a path to an
.osexp
file is specified, this file is opened and can be run directly by callingExperiment.run()
. If no experiment file is specified, an empty experiment is initialized. - log_path:
- fullscreen:
- subject_nr:
- kwargs: Optional keyword arguments that are interpreted as experimental
variables. The main use of this is to specify the backend through
the
canvas_backend
keyword.
Returns
- An (exp, win, clock, log) tuple corresponding to the Experiment, window handle (backend-specific), Clock, and Log objects.
Example
To implement an experiment fully programmatically:
from libopensesame.python_workspace_api import (
Experiment, Canvas, Text, Keyboard)
exp, win, clock, log = Experiment(canvas_backend='legacy')
c = Canvas()
c += Text('Press any key')
c.show()
kb = Keyboard()
kb.get_key()
exp.end()
To load an experiment file and run it:
from libopensesame.python_workspace_api import Experiment
exp, win, clock, log = Experiment(osexp_path='my_experiment.osexp',
subject_nr=2)
exp.run()
Form(*args, **kwargs)
A factory function that creates a new Form
object. For a
description
of possible keywords, see:
Returns
- A
Form
object.
Example
form = Form()
label = Label(text='label')
button = Button(text='Ok')
form.set_widget(label, (0,0))
form.set_widget(button, (0,1))
form._exec()
Keyboard(**resp_args)
A factory function that creates a new Keyboard
object. For a
description of possible keywords, see:
Returns
- A
Keyboard
object.
Example
my_keyboard = Keyboard(keylist=[u'a', u'b'], timeout=5000)
key, time = my_keyboard.get_key()
Mouse(**resp_args)
A factory function that creates a new Mouse
object. For a
description
of possible keywords, see:
Returns
- A
mouse
object.
Example
my_mouse = Mouse(keylist=[1,3], timeout=5000)
button, time = my_mouse.get_button()
Sampler(src, **playback_args)
A factory function that creates a new Sampler
object. For a
description of possible keywords, see:
Returns
- A sampler object.
Example
src = pool['bark.ogg']
my_sampler = Sampler(src, volume=.5, pan='left')
my_sampler.play()
Synth(osc='sine', freq=440, length=100, attack=0, decay=5, **playback_args)
A factory function that synthesizes a sound and returns it as a
Sampler
object.
Parameters
- osc: Oscillator, can be "sine", "saw", "square" or "white_noise".
- freq: Frequency, either an integer value (value in hertz) or a string ("A1", "eb2", etc.).
- length: The length of the sound in milliseconds.
- attack: The attack (fade-in) time in milliseconds.
- decay: The decay (fade-out) time in milliseconds.
- **playback_args: Optional playback keywords, such as volume and pan, as described under /python/sampler/.
Returns
- A sampler object.
Example
my_sampler = Synth(freq='b2', length=500)
copy_sketchpad(name)
Returns a copy of a sketchpad
's canvas.
Parameters
- name: The name of the
sketchpad
.
Returns
- A copy of the
sketchpad
's canvas.
Example
my_canvas = copy_sketchpad('my_sketchpad')
my_canvas.show()
pause()
Pauses the experiment.
register_cleanup_function(fnc)
Registers a clean-up function, which is executed when the experiment ends. Clean-up functions are executed at the very end, after the display, sound device, and log file have been closed. Clean-up functions are also executed when the experiment crashes.
Example
def my_cleanup_function():
print(u'The experiment is finished!')
register_cleanup_function(my_cleanup_function)
reset_feedback()
Resets all feedback variables to their initial state.
Example
reset_feedback()
set_subject_nr(nr)
Sets the subject number and parity (even/ odd). This function is called automatically when an experiment is started, so you only need to call it yourself if you overwrite the subject number that was specified when the experiment was launched.
Parameters
- nr: The subject nr.
Example
set_subject_nr(1)
print('Subject nr = %d' % var.subject_nr)
print('Subject parity = %s' % var.subject_parity)
sometimes(p=0.5)
Returns True with a certain probability. (For more advanced
randomization, use the Python random
module.)
Parameters
- p: The probability of returning True.
Returns
- True or False
Example
if sometimes():
print('Sometimes you win')
else:
print('Sometimes you loose')
xy_circle(n, rho, phi0=0, pole=(0, 0))
Generates a list of points (x,y coordinates) in a circle. This can be used to draw stimuli in a circular arrangement.
Parameters
- n: The number of x,y coordinates to generate.
- rho: The radial coordinate, also distance or eccentricity, of the first point.
- phi0: The angular coordinate for the first coordinate. This is a counterclockwise rotation in degrees (i.e. not radians), where 0 is straight right.
- pole: The reference point.
Returns
- A list of (x,y) coordinate tuples.
Example
# Draw 8 rectangles around a central fixation dot
c = Canvas()
c.fixdot()
for x, y in xy_circle(8, 100):
c.rect(x-10, y-10, 20, 20)
c.show()
xy_distance(x1, y1, x2, y2)
Gives the distance between two points.
Parameters
- x1: The x coordinate of the first point.
- y1: The y coordinate of the first point.
- x2: The x coordinate of the second point.
- y2: The y coordinate of the second point.
Returns
- The distance between the two points.
xy_from_polar(rho, phi, pole=(0, 0))
Converts polar coordinates (distance, angle) to Cartesian coordinates (x, y).
Parameters
- rho: The radial coordinate, also distance or eccentricity.
- phi: The angular coordinate. This reflects a clockwise rotation in degrees (i.e. not radians), where 0 is straight right.
- pole: The reference point.
Returns
- An (x, y) coordinate tuple.
Example
# Draw a cross
x1, y1 = xy_from_polar(100, 45)
x2, y2 = xy_from_polar(100, -45)
c = Canvas()
c.line(x1, y1, -x1, -y1)
c.line(x2, y2, -x2, -y2)
c.show()
xy_grid(n, spacing, pole=(0, 0))
Generates a list of points (x,y coordinates) in a grid. This can be used to draw stimuli in a grid arrangement.
Parameters
- n: An
int
that indicates the number of columns and rows, so thatn=2
indicates a 2x2 grid, or a (n_col, n_row)tuple
, so thatn=(2,3)
indicates a 2x3 grid. - spacing: A numeric value that indicates the spacing between cells, or a (col_spacing, row_spacing) tuple.
- pole: The reference point.
Returns
- A list of (x,y) coordinate tuples.
Example
# Draw a 4x4 grid of rectangles
c = Canvas()
c.fixdot()
for x, y in xy_grid(4, 100):
c.rect(x-10, y-10, 20, 20)
c.show()
xy_random(n, width, height, min_dist=0, pole=(0, 0))
Generates a list of random points (x,y coordinates) with a minimum spacing between each pair of points. This function will raise an Exception when the coordinate list cannot be generated, typically because there are too many points, the min_dist is set too high, or the width or height are set too low.
Parameters
- n: The number of points to generate.
- width: The width of the field with random points.
- height: The height of the field with random points.
- min_dist: The minimum distance between each point.
- pole: The reference point.
Returns
- A list of (x,y) coordinate tuples.
Example
# Draw a 50 rectangles in a random grid
c = Canvas()
c.fixdot()
for x, y in xy_random(50, 500, 500, min_dist=40):
c.rect(x-10, y-10, 20, 20)
c.show()
xy_to_polar(x, y, pole=(0, 0))
Converts Cartesian coordinates (x, y) to polar coordinates (distance, angle).
Parameters
- x: The X coordinate.
- y: The Y coordinate.
- pole: The reference point.
Returns
- An (rho, phi) coordinate tuple. Here,
rho
is the radial coordinate, also distance or eccentricity.phi
is the angular coordinate in degrees (i.e. not radians), and reflects a counterclockwise rotation, where 0 is straight right.
Example
rho, phi = xy_to_polar(100, 100)