OpenSesame videos
Python videos
Supported by Supported by

Common functions

The following functions are available in inline_script items:

function Canvas(auto_prepare=True, **style_args)

A factory function that creates a new Canvas object. For a description of possible keywords, see:

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()

Keywords:

  • auto_prepare -- No description
    • Default: True

Keyword dict:

  • **style_args: No description.

Returns:

A Canvas object.

  • Type: canvas

function Form(*args, **kwargs)

A factory function that creates a new Form object. For a description of possible keywords, see:

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()

Argument list:

  • *args: No description.

Keyword dict:

  • **kwargs: No description.

Returns:

A Form object.

  • Type: canvas

function Keyboard(**resp_args)

A factory function that creates a new Keyboard object. For a description of possible keywords, see:

Example:

my_keyboard = Keyboard(keylist=[u'a', u'b'], timeout=5000)
key, time = my_keyboard.get_key()

Keyword dict:

  • **resp_args: No description.

Returns:

A Keyboard object.

  • Type: keyboard

function Mouse(**resp_args)

A factory function that creates a new Mouse object. For a description of possible keywords, see:

Example:

my_mouse = Mouse(keylist=[1,3], timeout=5000)
button, time = my_mouse.get_button()

Keyword dict:

  • **resp_args: No description.

Returns:

A mouse object.

  • Type: mouse

function Sampler(src, **playback_args)

A factory function that creates a new Sampler object. For a description of possible keywords, see:

Example:

src = pool['bark.ogg']
my_sampler = Sampler(src, volume=.5, pan='left')
my_sampler.play()

Arguments:

  • src -- No description

Keyword dict:

  • **playback_args: No description.

Returns:

A sampler object.

  • Type: sampler

function Synth(osc=u'sine', freq=440, length=100, attack=0, decay=5)

A factory function that synthesizes a sound and returns it as a Sampler object.

Example:

my_sampler = Synth(freq=u'b2', length=500)

Keywords:

  • osc -- Oscillator, can be "sine", "saw", "square" or "white_noise".
    • Type: str, unicode
    • Default: 'sine'
  • freq -- Frequency, either an integer value (value in hertz) or a string ("A1", "eb2", etc.).
    • Type: str, unicode, int, float
    • Default: 440
  • length -- The length of the sound in milliseconds.
    • Type: int, float
    • Default: 100
  • attack -- The attack (fade-in) time in milliseconds.
    • Type: int, float
    • Default: 0
  • decay -- The decay (fade-out) time in milliseconds.
    • Type: int, float
    • Default: 5

Returns:

A sampler object.

  • Type: sampler

function copy_sketchpad(name)

Returns a copy of a sketchpad's canvas.

Example:

my_canvas = copy_sketchpad('my_sketchpad')
my_canvas.show()

Arguments:

  • name -- The name of the sketchpad.
    • Type: str, unicode

Returns:

A copy of the sketchpad's canvas.

  • Type: canvas

function pause()

Pauses the experiment.

function 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)

Arguments:

  • fnc -- No description

function reset_feedback()

Resets all feedback variables to their initial state.

Example:

reset_feedback()

function 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.

Example:

set_subject_nr(1)
print('Subject nr = %d' % var.subject_nr)
print('Subject parity = %s' % var.subject_parity)

Arguments:

  • nr -- The subject nr.
    • Type: int

function sometimes(p=0.5)

Returns True with a certain probability. (For more advanced randomization, use the Python random module.)

Example:

if sometimes():
        print('Sometimes you win')
else:
        print('Sometimes you loose')

Keywords:

  • p -- The probability of returning True.
    • Type: float
    • Default: 0.5

Returns:

True or False

  • Type: bool

function 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.

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()

Arguments:

  • n -- The number of x,y coordinates to generate.
    • Type: int
  • rho -- The radial coordinate, also distance or eccentricity, of the first point.
    • Type: float

Keywords:

  • phi0 -- The angular coordinate for the first coordinate. This is a counterclockwise rotation in degrees (i.e. not radians), where 0 is straight right.
    • Type: float
    • Default: 0
  • pole -- The refence point.
    • Type: tuple
    • Default: (0, 0)

Returns:

A list of (x,y) coordinate tuples.

  • Type: list

function xy_distance(x1, y1, x2, y2)

Gives the distance between two points.

Arguments:

  • x1 -- The x coordinate of the first point.
    • Type: float
  • y1 -- The y coordinate of the first point.
    • Type: float
  • x2 -- The x coordinate of the second point.
    • Type: float
  • y2 -- The y coordinate of the second point.
    • Type: float

Returns:

The distance between the two points.

  • Type: float

function xy_from_polar(rho, phi, pole=(0, 0))

Converts polar coordinates (distance, angle) to Cartesian coordinates (x, y).

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()

Arguments:

  • rho -- The radial coordinate, also distance or eccentricity.
    • Type: float
  • phi -- The angular coordinate. This reflects a clockwise rotation in degrees (i.e. not radians), where 0 is straight right.
    • Type: float

Keywords:

  • pole -- The refence point.
    • Type: tuple
    • Default: (0, 0)

Returns:

An (x, y) coordinate tuple.

  • Type: tuple

function 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.

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()

Arguments:

  • n -- An int that indicates the number of columns and rows, so that n=2 indicates a 2x2 grid, or a (n_col, n_row) tuple, so that n=(2,3) indicates a 2x3 grid.
    • Type: int, tuple
  • spacing -- A numeric value that indicates the spacing between cells, or a (col_spacing, row_spacing) tuple.
    • Type: float

Keywords:

  • pole -- The refence point.
    • Type: tuple
    • Default: (0, 0)

Returns:

A list of (x,y) coordinate tuples.

  • Type: list

function 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 osexception 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.

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()

Arguments:

  • n -- The number of points to generate.
    • Type: int
  • width -- The width of the field with random points.
    • Type: float
  • height -- The height of the field with random points.
    • Type: float

Keywords:

  • min_dist -- The minimum distance between each point.
    • Type: float
    • Default: 0
  • pole -- The refence point.
    • Type: tuple
    • Default: (0, 0)

Returns:

A list of (x,y) coordinate tuples.

  • Type: list

function xy_to_polar(x, y, pole=(0, 0))

Converts Cartesian coordinates (x, y) to polar coordinates (distance, angle).

Example:

rho, phi = xy_to_polar(100, 100)

Arguments:

  • x -- The X coordinate.
    • Type: float
  • y -- The Y coordinate.
    • Type: float

Keywords:

  • pole -- The refence point.
    • Type: tuple
    • Default: (0, 0)

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.

  • Type: tuple
Supported by Supported by