speriment
index
/Users/presley/Speriment/speriment/speriment.py

This is the API you can use to write a Python script for use with
Speriment.

 
Modules
       
copy
csv
json
jsonschema

 
Classes
       
json.encoder.JSONEncoder(__builtin__.object)
ExperimentEncoder
Component
Block
Experiment
Option
Page
ExperimentMaker
IDGenerator
RunIf

 
class Block(Component)
     Methods defined here:
__init__(self, pages=None, groups=None, blocks=None, id_str=None, exchangeable=[], latin_square=None, pseudorandom=None, **kwargs)
Exactly one of pages, groups, and blocks must be provided.
 
pages: [Page], the pages contained by this Block.
 
groups: [[Page]], the groups contained by this Block. One page from each
inner list of Pages will be displayed per participant.
 
blocks: [Block], the blocks contained by this Block.
 
id_str: String, optional, identifier unique among the Blocks in this
experiment.
 
exchangeable: [Block], only valid if contents is [Block]. A subset of
contents. The Blocks in this list are allowed to switch places with each
other. Blocks which are not exchangeable run only in the order in which
they were given. Exchangeable blocks can be used for counterbalancing
designs. For example, if there are three blocks, A, B, and C, and A and
C are exchangeable, they can run in the order A, B, C or C, B, A.
 
latin_square: boolean, only valid if contents is [[Page]], that is,
groups of Pages. If True, Pages are chosen from groups according to a
Latin Square. This process requires that all groups in the Block have
equal length. Additionally, the number of groups in the Block should be
a multiple of the number of Pages in a group, and the Pages in each
group should be in the same order in terms of their experimental
condition. The condition attribute of Pages is not used to check the
order.
 
pseudorandom: boolean, only valid if contents is [[Pages]] or [Pages], 
all Pages have a condition attribute specified, and there will be an equal
number of each Pages of each condition displayed (therefore it is not
valid if contents is [[Pages]] and latin_square is False, because there's
no guarantee about how many Pages of each condition will display). If True,
no two Pages with the same condition will display in a row.
 
**kwargs: optional keyword arguments, which can include:
 
criterion: integer or float between 0 and 1. If integer, it must be
less than or equal to the number of Pages contained by this Block
(currently it can only be used on Blocks where contents is [Page] or
[[Page]], but eventually it will be usable with [Block], and this
restriction will apply to the Pages that are ultimately contained by the
Block) that have a specification for the correct attribute, or have
Options that do. The integer then represents the number of Pages that
can be correct and were correct in a row at the end of the Block. The
idea is that participants may make mistakes at the beginning, but by the
end of the Block should give correct answers for this long of a streak
in order to show master. If it's a float, it represents a percentage of
Pages that can be correct that were correct out of the entire block, not
in a streak. Whether it's an integer or a float, this attribute signals
that a participant should see the (reshuffled) contents of this Block as
many times as it takes to reach criterion before moving on to later
Blocks, and can be used to train participants before later testing them
on novel information.
 
run_if: RunIf, gives a condition that must be met in order for this Block
to display its contents, no matter the type of contents. See the RunIf
documentation for more information.
validate(self)
validate_contents(self)
validate_latin_square(self)
validate_pseudorandom(self)

Methods inherited from Component:
new(self)
Use this method to return a new experimental component with the same
data as one you've already constructed if you're using an IDGenerator to
handle IDs. The two components will have different IDs, so
they can coexist in an experiment and won't be confused with each other,
for instance if one is referred to in a RunIf.
set_id(self, id_str=None)
set_optional_args(self, **kwargs)

Data and other attributes inherited from Component:
id_generator = None

 
class Component
    This is the superclass of OptionPageBlock, and Experiment. You should
not instantiate this class.
 
  Methods defined here:
new(self)
Use this method to return a new experimental component with the same
data as one you've already constructed if you're using an IDGenerator to
handle IDs. The two components will have different IDs, so
they can coexist in an experiment and won't be confused with each other,
for instance if one is referred to in a RunIf.
set_id(self, id_str=None)
set_optional_args(self, **kwargs)
validate(self)
To be defined for each subtype.

Data and other attributes defined here:
id_generator = None

 
class Experiment(Component)
    An Experiment holds all the information describing one experiment. If you
don't make your own IDs (IDs should be unique among pages, among options,
and among blocks within one experiment), then use one IDGenerator per
experiment.
 
  Methods defined here:
__init__(self, blocks, exchangeable=[])
blocks: [Block], the contents of the experiment.
 
exchangeable: [Block], a subset of the Blocks. These Blocks will be
considered exchangeable. See Block documentation for more information.
install(self, experiment_name)
Validates the structure of the experiment, writes it as a JSON object
in a JavaScript file, and gives PsiTurk access to Speriment and the JSON
object.
to_JSON(self)
to_file(self, filename, varname)
Validates the structure of the experiment and writes it as a JSON
object in a JavaScript file.
validate(self)
validate_json(self, json_object)
validate_option_tags(self)
validate_page_tags(self)

Data and other attributes defined here:
contents = '{\n "$schema": "http://json-schema.org/draft-0..."}\n }\n },\n "required": ["blocks"]\n}\n'
f = <closed file 'sperimentschema.json', mode 'r'>
schema = {u'$schema': u'http://json-schema.org/draft-04/schema#', u'definitions': {u'block': {u'description': u'A grouping of blocks, pages, or groups of pages....e same container can swap places with each other.', u'oneOf': [{u'properties': {u'criterion': {...}, u'id': {...}, u'pages': {...}, u'pseudorandom': {...}, u'runIf': {...}}, u'required': [u'id', u'pages']}, {u'properties': {u'criterion': {...}, u'groups': {...}, u'id': {...}, u'latinSquare': {...}, u'pseudorandom': {...}, u'runIf': {...}}, u'required': [u'id', u'groups']}, {u'properties': {u'blocks': {...}, u'criterion': {...}, u'exchangeable': {...}, u'id': {...}, u'runIf': {...}}, u'required': [u'id', u'blocks']}], u'type': u'object'}, u'correct': {u'description': u'Either whether this option is correct, or a regular expression that any correct response will match.', u'type': [u'string', u'boolean']}, u'criterion': {u'description': u'', u'type': u'number'}, u'exchangeable': {u'description': u'IDs of blocks that can swap positions with each ...ble, the possible orders are 1, 2, 3 and 3, 2, 1.', u'items': {u'description': u'Block ID', u'type': u'string'}, u'type': u'array'}, u'feedback': {u'description': u'The feedback to display after this page/if this option is chosen.', u'type': u'string'}, u'id': {u'description': u'Unique identifier for a component of the experiment.', u'type': u'string'}, u'option': {u'description': u'Option for user response.', u'properties': {u'correct': {u'$ref': u'#/definitions/correct'}, u'feedback': {u'$ref': u'#/definitions/feedback'}, u'id': {u'$ref': u'#/definitions/id'}, u'tags': {u'description': u'Array of descriptors. This array should be the s...the entire experiment, or absent for all options.', u'items': {u'$ref': u'#/definitions/tag'}, u'type': u'array'}, u'text': {u'$ref': u'#/definitions/text'}}, u'required': [u'id', u'text'], u'type': u'object'}, u'page': {u'description': u'A unit of the experiment to be showed at once, s...r a page or its options will be shown separately.', u'properties': {u'condition': {u'description': u'The experimental condition that this item belong... items of the same condition from being adjacent.', u'type': u'string'}, u'correct': {u'$ref': u'#/definitions/correct'}, u'exclusive': {u'description': u'Can only one of these options be chosen? Defaults to true.', u'type': u'boolean'}, u'feedback': {u'$ref': u'#/definitions/feedback'}, u'freetext': {u'description': u'Does this question have a text box option, as opposed to discrete choices? Defaults to false.', u'type': u'boolean'}, u'id': {u'$ref': u'#/definitions/id'}, u'ordered': {u'description': u'Are the options for this question ordered? Used ...mize the order of the options. Defaults to false.', u'type': u'boolean'}, u'tags': {u'description': u'Array of descriptors. This array should be the s...n the entire experiment, or absent for all pages.', u'items': {u'$ref': u'#/definitions/tag'}, u'type': u'array'}, u'text': {u'$ref': u'#/definitions/text'}}, u'required': [u'id', u'text'], u'type': u'object'}, u'pageID': {u'description': u'', u'type': u'string'}, u'pseudorandom': {u'description': u'', u'type': u'string'}, ...}, u'properties': {u'blocks': {u'description': u"Experiments are made up of blocks. Blocks are ma...ch you specify them, unless they're exchangeable.", u'items': {u'$ref': u'#/definitions/block'}, u'type': u'array'}, u'breakoff': {u'description': u'Does the experiment allow people to quit early and submit incomplete data?', u'type': u'boolean'}, u'exchangeable': {u'$ref': u'#/definitions/exchangeable'}}, u'required': [u'blocks'], u'title': u'Speriment Schema'}

Methods inherited from Component:
new(self)
Use this method to return a new experimental component with the same
data as one you've already constructed if you're using an IDGenerator to
handle IDs. The two components will have different IDs, so
they can coexist in an experiment and won't be confused with each other,
for instance if one is referred to in a RunIf.
set_id(self, id_str=None)
set_optional_args(self, **kwargs)

Data and other attributes inherited from Component:
id_generator = None

 
class ExperimentEncoder(json.encoder.JSONEncoder)
    This class enables nested Python objects to be correctly serialized to JSON.
It also requires that non-schema validation pass before the JSON is
generated.
 
 
Method resolution order:
ExperimentEncoder
json.encoder.JSONEncoder
__builtin__.object

Methods defined here:
default(self, obj)
rename_key(self, dictionary, key, new_key)

Methods inherited from json.encoder.JSONEncoder:
__init__(self, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, encoding='utf-8', default=None)
Constructor for JSONEncoder, with sensible defaults.
 
If skipkeys is false, then it is a TypeError to attempt
encoding of keys that are not str, int, long, float or None.  If
skipkeys is True, such items are simply skipped.
 
If *ensure_ascii* is true (the default), all non-ASCII
characters in the output are escaped with \uXXXX sequences,
and the results are str instances consisting of ASCII
characters only.  If ensure_ascii is False, a result may be a
unicode instance.  This usually happens if the input contains
unicode strings or the *encoding* parameter is used.
 
If check_circular is true, then lists, dicts, and custom encoded
objects will be checked for circular references during encoding to
prevent an infinite recursion (which would cause an OverflowError).
Otherwise, no such check takes place.
 
If allow_nan is true, then NaN, Infinity, and -Infinity will be
encoded as such.  This behavior is not JSON specification compliant,
but is consistent with most JavaScript based encoders and decoders.
Otherwise, it will be a ValueError to encode such floats.
 
If sort_keys is true, then the output of dictionaries will be
sorted by key; this is useful for regression tests to ensure
that JSON serializations can be compared on a day-to-day basis.
 
If indent is a non-negative integer, then JSON array
elements and object members will be pretty-printed with that
indent level.  An indent level of 0 will only insert newlines.
None is the most compact representation.  Since the default
item separator is ', ',  the output might include trailing
whitespace when indent is specified.  You can use
separators=(',', ': ') to avoid this.
 
If specified, separators should be a (item_separator, key_separator)
tuple.  The default is (', ', ': ').  To get the most compact JSON
representation you should specify (',', ':') to eliminate whitespace.
 
If specified, default is a function that gets called for objects
that can't otherwise be serialized.  It should return a JSON encodable
version of the object or raise a ``TypeError``.
 
If encoding is not None, then all input strings will be
transformed into unicode using that encoding prior to JSON-encoding.
The default is UTF-8.
encode(self, o)
Return a JSON string representation of a Python data structure.
 
>>> JSONEncoder().encode({"foo": ["bar", "baz"]})
'{"foo": ["bar", "baz"]}'
iterencode(self, o, _one_shot=False)
Encode the given object and yield each string
representation as available.
 
For example::
 
    for chunk in JSONEncoder().iterencode(bigobject):
        mysocket.write(chunk)

Data descriptors inherited from json.encoder.JSONEncoder:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

Data and other attributes inherited from json.encoder.JSONEncoder:
item_separator = ', '
key_separator = ': '

 
class ExperimentMaker
    This class makes the "with" statement for automatic ID generation
possible. It sets an IDGenerator as a class variable in Component, making it
available to Options, Pages, and Blocks. At the end of the with block,
it is removed and any errors encountered in the block are raised.
 
  Methods defined here:
__enter__(self)
__exit__(self, etype, evalue, etrace)
__init__(self, id_generator)

 
class IDGenerator
    Creates an object to generate unique IDs for experimental components. You
should create exactly one per experiment so that all IDs in that experiment
will be distinct.
 
Usage:
with make_experiment(IDGenerator()):
    <experiment code>
 
  Methods defined here:
__init__(self, seed=0)
seed: optional, an integer to start making IDs at.
next_id(self)
Takes no arguments and returns a string, which is a new unique ID.

 
class Option(Component)
     Methods defined here:
__init__(self, text=None, id_str=None, **kwargs)
text: If the Option is not a text box, this is the label for the Option
that will be displayed on the page. If the Option is a text box, this is
not currently used.
 
id_str: String, optional, an identifier unique among all options in this
experiment. (Currently uniqueness in its page is sufficient but this may
change in the future.)
 
**kwargs: optional keyword arguments, which can include:
 
feedback: string, the feedback to be displayed if this Option is chosen.
 
correct: If the Option is not a text box, a boolean representing whether
this Option is correct. If the Option is a text box, a string
representing a regular expression that any correct answers will match.
For example, correct = 'hi.*' would mean that 'hi' and 'high' are both
correct inputs.
 
tags: [string], any metadata you want to associate with this Option. It
will not be used in the experiment, but will be passed through to the
data file. All options in the entire experiment must have the same
number of tags so that they will stay aligned in the output file.
 
Note that the type of an Option (radio button, check box, dropdown, or
text box) is determined based on its data and the attributes of its
containing Page. It is not set directly in the Option.

Methods inherited from Component:
new(self)
Use this method to return a new experimental component with the same
data as one you've already constructed if you're using an IDGenerator to
handle IDs. The two components will have different IDs, so
they can coexist in an experiment and won't be confused with each other,
for instance if one is referred to in a RunIf.
set_id(self, id_str=None)
set_optional_args(self, **kwargs)
validate(self)
To be defined for each subtype.

Data and other attributes inherited from Component:
id_generator = None

 
class Page(Component)
     Methods defined here:
__init__(self, text, options=None, id_str=None, **kwargs)
text: The text to be displayed on the page.
 
options: [Option], optional, the answer choices to be displayed on the
page.
 
id_str: String, optional, an identifier for this page unique among all
pages in the experiment.
 
**kwargs: optional keyword arguments, which can include:
 
feedback: string, the feedback to be displayed after an answer is chosen.
 
correct: If freetext is False, a string representing the id_str of the
correct Option. If freetext is True, a string representing a regular
expression that a correct answer will match.
 
tags: [string], any metadata you want to associate with this Page. It
will not be used in the experiment, but will be passed through to the
data file. All pages in the entire experiment must have the same number
of tags so that they will stay aligned in the output file.
 
condition: string, the condition this Page belongs to in the
experimental manipulation. Used if this Page is in a block where
pseudorandom is True, to keep Pages with the same condition from
appearing in a row.
 
resources: [string], filenames of any images, audio, or video that
should display on the page.
 
ordered: boolean, whether the Options for this Page need to be kept in
the order in which they were given. If True, the Options may be reversed
but will not be shuffled.
 
exclusive: boolean, whether the participant can choose only one Option
as their response.
 
freetext: boolean, whether the Option is a text box rather than multiple
choice.
validate(self)
validate_freetext(self)
validate_resources(self)

Methods inherited from Component:
new(self)
Use this method to return a new experimental component with the same
data as one you've already constructed if you're using an IDGenerator to
handle IDs. The two components will have different IDs, so
they can coexist in an experiment and won't be confused with each other,
for instance if one is referred to in a RunIf.
set_id(self, id_str=None)
set_optional_args(self, **kwargs)

Data and other attributes inherited from Component:
id_generator = None

 
class RunIf
     Methods defined here:
__init__(self, page, option=None, regex=None)
page: Page, the Page to look at to see which answer was given.
 
option: Option, optional. If given, the block containing this RunIf will
only run if this Option was chosen the last time page was displayed.
 
regex: string, optional. If given, the block containing this RunIf will
only run if a response matching a regular expression made from the
string regex was given the last time page was displayed.
 
Exactly one of option and regex must be given.
 
The reason RunIfs depend on "the last time" their page was displayed is
that Pages can display multiple times if they are in Blocks with a
criterion.

 
Functions
       
get_dicts(csvfile)
csvfile: string, a filename of a csv file. The file should have a header
row with column names.
 
sep: string, the delimiter used to separate values in the csv file. Defaults
to comma.
 
Returns: lists (one for each row in the file) of dictionaries mapping
strings (column names) to strings (cell values).
get_rows(csvfile)
csvfile: string, a filename of a csv file.
 
sep: string, the delimited used to separate values in the csv file. Defaults
to comma.
 
Returns: lists (one for each row in the file) of lists (one for each cell in
the row) of strings.
make_exp(filename)
Add script tags to PsiTurk's exp.html file so it can use speriment.js and
the JSON object.
make_experiment(id_generator)
id_generator: IDGenerator, an object that will make unique IDs for
everything in an Experiment.
 
side effect: puts id_generator in scope for the duration of the with block.
Options, Pages, and Blocks will automatically use it to create unique IDs.
 
Usage:
with make_experiment(IDGenerator()):
    <experiment code>
 
<any code here will not use that IDGenerator anymore>
make_task(varname)
Replace PsiTurk's example task.js with the standard Speriment task.js,
with the JSON object variable name inserted.

 
Data
        __warningregistry__ = {("Not importing directory './json': missing __init__.py", <type 'exceptions.ImportWarning'>, 3): True, ("Not importing directory '/Users/presley/Speriment/json': missing __init__.py", <type 'exceptions.ImportWarning'>, 3): True}