adding randomization and turk module docs

This commit is contained in:
Josh de Leeuw 2014-10-16 12:42:15 -04:00
parent 3c6d7fdd66
commit 48ac399886
5 changed files with 299 additions and 94 deletions

View File

@ -1,51 +0,0 @@
# The jsPsych core library
Every jsPsych experiment utilizes the core library (contained in the `jspsych.js` file). The core library is the glue that holds all of the various plugins together. There are also several modules that are contained in the core library for tasks that are common to many different experiments. These modules are available whenever the `jspsych.js` file is loaded.
## Core library API
### Core
* jsPsych.init
* jsPsych.progress
* jsPsych.startTime
* jsPsych.totalTime
* jsPsych.preloadImages
* jsPsych.getDisplayElement
* jsPsych.finishTrial
* jsPsych.currentTrial
* jsPsych.initSettings
* jsPsych.currentChunkID
### Data module
* jsPsych.data.getData
* jsPsych.data.write
* jsPsych.data.dataAsCSV
* jsPsych.data.localSave
* jsPsych.data.getTrialsOfType
* jsPsych.data.getTrialsFromChunk
* jsPsych.data.getLastTrialData
* jsPsych.data.getLastChunkData
* jsPsych.data.displayData
### Turk module
* jsPsych.turk.turkInfo
* jsPsych.turk.submitToTurk
### Randomization module
* jsPsych.randomization.repeat
* jsPsych.randomization.factorial
### PluginAPI module
* jsPsych.pluginAPI.getKeyboardResponse
* jsPsych.pluginAPI.cancelKeyboardResponse
* jsPsych.pluginAPI.cancelAllKeyboardResponses
* jsPsych.pluginAPI.normalizeTrialVariables
* jsPsych.pluginAPI.enforceArray

View File

@ -1,51 +1,205 @@
# The jsPsych core library
# jsPsych.randomization
Every jsPsych experiment utilizes the core library (contained in the `jspsych.js` file). The core library is the glue that holds all of the various plugins together. There are also several modules that are contained in the core library for tasks that are common to many different experiments. These modules are available whenever the `jspsych.js` file is loaded.
The jsPsych.randomization module contains methods that are useful for generating random lists of trial variables.
## Core library API
---
### Core
## jsPsych.randomization.factorial
* jsPsych.init
* jsPsych.progress
* jsPsych.startTime
* jsPsych.totalTime
* jsPsych.preloadImages
* jsPsych.getDisplayElement
* jsPsych.finishTrial
* jsPsych.currentTrial
* jsPsych.initSettings
* jsPsych.currentChunkID
```
jsPsych.randomization.factorial(factors, repetitions, unpack)
```
### Data module
### Parameters
* jsPsych.data.getData
* jsPsych.data.write
* jsPsych.data.dataAsCSV
* jsPsych.data.localSave
* jsPsych.data.getTrialsOfType
* jsPsych.data.getTrialsFromChunk
* jsPsych.data.getLastTrialData
* jsPsych.data.getLastChunkData
* jsPsych.data.displayData
Parameter | Type | Description
----------|------|------------
factors | object | The `factors` object should contain a property for each different factor. Each property-factor should have a value of an array, with each element of the array corresponding to a level of the factor.
repetitions | integer | The number of times to repeat each unique combination of the factors in the output sample.
unpack | boolean | If `true` then the output will be an object with a property for each factor in the original `factors` object. The value of each property-factor will be an array containing the levels of the factor in a random order. The order will be consistent across each property-factor (e.g. the first element of each property-factor will specify one unique combination of the factors). If `false`, then the return value will be an array of objects where each property-factor contains only a single value.
### Turk module
### Return value
* jsPsych.turk.turkInfo
* jsPsych.turk.submitToTurk
The return value depends on the `unpack` parameter. See description of the parameter above, and examples below.
### Randomization module
### Description
* jsPsych.randomization.repeat
* jsPsych.randomization.factorial
This method takes a list of factors and their levels, and creates a full factorial design by creating each unique combination of the factors. The returned set of combinations is in a random order.
### Examples
#### Create full factorial design
```javascript
var factors = {
stimulus: ['a.jpg', 'b.jpg'],
ms_delay: [100, 200]
}
var full_design = jsPsych.randomization.factorial(factors, 1);
/*
output:
full_design = [
{stimulus: 'a.jpg', ms_delay: 200},
{stimulus: 'b.jpg', ms_delay: 200},
{stimulus: 'b.jpg', ms_delay: 100},
{stimulus: 'a.jpg', ms_delay: 100},
]
*/
```
#### Create full factorial design with repeats
```javascript
var factors = {
stimulus: ['a.jpg', 'b.jpg'],
ms_delay: [100, 200]
}
var full_design = jsPsych.randomization.factorial(factors, 2);
/*
output:
full_design = [
{stimulus: 'b.jpg', ms_delay: 200},
{stimulus: 'b.jpg', ms_delay: 100},
{stimulus: 'b.jpg', ms_delay: 100},
{stimulus: 'a.jpg', ms_delay: 100},
{stimulus: 'a.jpg', ms_delay: 200},
{stimulus: 'b.jpg', ms_delay: 200},
{stimulus: 'a.jpg', ms_delay: 100},
{stimulus: 'a.jpg', ms_delay: 200},
]
*/
```
#### Create full factorial design, unpacked
```javascript
var factors = {
stimulus: ['a.jpg', 'b.jpg'],
ms_delay: [100, 200]
}
var full_design = jsPsych.randomization.factorial(factors, 1, true);
/*
output:
full_design = {
stimulus: ['a.jpg','b.jpg','b.jpg','a.jpg'],
ms_delay: [200, 100, 200, 100]
]
*/
```
---
## jsPsych.randomization.repeat
```
jsPsych.randomization.repeat(array, repetitions, unpack)
```
### Parameters
Parameter | Type | Description
----------|------|------------
array | array | The array of values to randomize & repeat.
repetitions | integer or array | The number of times to repeat each element of the `array` in the final sample. If this parameter is defined as an integer, then each element of `array` is repeated the same number of times. This parameter can also be an array of the same length as `array`, in which case each element of `array` will be repeated the number of times defined in the corresponding position of the `repetitions` array.
unpack | boolean | If each element of `array` is an object with an equivalent set of properties, then setting `unpack` to `true` will make the return value an object with a property for each of the unique properties among the elements of the `array`. Each property in the output object will be an array containing the values for that property in the randomized order. The order will be consistent across properties. If this is `false` then the output is just an array containing a randomized order of the original `array` elements.
### Return value
The return value depends on the `unpack` parameter. See description of the parameter above, and examples below.
### Description
This method takes an array of values and generates a new random order of the array, with the option of repeating each element of the array a specified number of times.
If the array elements are objects with the same set of properties, then this method can optionally return a single object where each property is a randomized order of the properties defined in the original set of objects. This is useful for randomizing sets of parameters that are used to define a jsPsych block.
### Examples
#### Shuffle an array, no repeats
```javascript
var myArray = [1,2,3,4,5];
var shuffledArray = jsPsych.randomization.repeat(myArray, 1);
// output: shuffledArray = [3,2,4,1,5]
```
#### Shuffle an array with repeats
```javascript
var myArray = [1,2,3,4,5];
var shuffledArray = jsPsych.randomization.repeat(myArray, 2);
// output: shuffledArray = [1,3,4,2,2,4,5,1,5,3]
```
#### Shuffle an array of objects
```javascript
var trial1 = {
stimulus: 'img/faceA.jpg',
correct_key: 80,
person_name: 'Joe'
}
var trial2 = {
stimulus: 'img/faceB.jpg',
correct_key: 80,
person_name: 'Fred'
}
var trial3 = {
stimulus: 'img/faceC.jpg',
correct_key: 81,
person_name: 'Mary'
}
var myArray = [ trial1, trial2, trial3 ];
var shuffledArray = jsPsych.randomization.repeat(myArray, 2);
// output: shuffledArray = [ trial1, trial3, trial3, trial2, trial1, trial2 ]
```
#### Shuffle an array of objects, with unpack
```javascript
var trial1 = {
stimulus: 'img/faceA.jpg',
correct_key: 80,
person_name: 'Joe'
}
var trial2 = {
stimulus: 'img/faceB.jpg',
correct_key: 80,
person_name: 'Fred'
}
var trial3 = {
stimulus: 'img/faceC.jpg',
correct_key: 81,
person_name: 'Mary'
}
var myArray = [ trial1, trial2, trial3 ];
var shuffledArray = jsPsych.randomization.repeat(myArray, 2, true);
/*
output: shuffledArray = {
stimulus: ['img/faceB.jpg','img/faceA.jpg','img/faceC.jpg','img/faceA.jpg','img/faceC.jpg','img/faceB.jpg'],
correct_key: [80, 80, 81, 80, 81, 80],
person_name: ['Fred','Joe', 'Mary', 'Joe', 'Mary', 'Fred']
}
*/
```
### PluginAPI module
* jsPsych.pluginAPI.getKeyboardResponse
* jsPsych.pluginAPI.cancelKeyboardResponse
* jsPsych.pluginAPI.cancelAllKeyboardResponses
* jsPsych.pluginAPI.normalizeTrialVariables
* jsPsych.pluginAPI.enforceArray

View File

@ -0,0 +1,102 @@
# jsPsych.turk
The jsPsych.turk module contains functions for interacting with Mechanical Turk.
---
## jsPsych.turk.submitToTurk
```
jsPsych.turk.submitToTurk(data)
```
### Parameters
Parameter | Type | Description
----------|------|------------
data | object | The `data` parameter is an object of `key: value` pairs. Any pairs in the `data` parameter will be saved by Mechanical Turk, and can be downloaded in a CSV file through the Mechanical Turk interface.
### Return value
Returns nothing.
### Description
This method will submit a HIT to Mechanical Turk, causing the HIT to finish.
This method will only work when called from within the mechanical turk website. If you are using an external HIT to send workers to your own server, this method will not work on an externally hosted page. It will work if your external content is loaded in the iframe on the Mechanical Turk website.
### Example
```html
<p>Enter the code you were given:</p>
<input type="text" id="code"></input>
<button onclick="sendData();">Submit HIT</button>
<script>
// this content must be loaded in the iframe on the mechanical turk website.
// usually, this means that the content is part of your 'recruitment ad', the
// page the workers can see when they are deciding whether or not to accept a HIT.
// one option is to include a simple form on this page that workers submit, with a
// special code that they get at the end of the experiment.
function sendData() {
jsPsych.turk.submitToTurk({
code: document.getElementById('code').value
});
}
</script>
```
---
## jsPsych.turk.turkInfo
```
jsPsych.turk.turkInfo()
```
### Parameters
None.
### Return value
Returns an object with six properties:
* `.assignmentId` contains the assignment ID string of the HIT.
* `.hitId` contains the HIT ID.
* `.workerId` contains the worker ID of the worker completing the HIT.
* `.turkSubmitTo` contains the URL for submitting the HIT. This parameter is used in the `jsPsych.turk.submitToTurk` method, and is probably not useful outside of that context.
* `.previewMode` is a boolean value indicating whether or not the worker has accepted the HIT yet. If the page is viewed inside Mechancial Turk and the worker has not clicked 'Accept HIT' then this will be true. If the page is viewed outside Mechanical Turk or the worker has acccepted the HIT, then it will be false.
* `.outsideTurk` is a boolean value indicating if the page is being viewed within Mechanical Turk, or if it is being viewed from another source (e.g. someone directly going to the page URL instead of going through mturk).
### Description
This method returns basic information about the current Mechanical Turk session, including the worker ID, assignment ID, and HIT ID.
### Example
```javascript
var turkInfo = jsPsych.turk.turkInfo();
alert('Worker ID is: ' + turkInfo.workerId);
alert('Assignment ID is: ' + turkInfo.assignmentId);
alert('HIT ID is: ' + turkInfo.hitId);
// true if the page is viewed within Mechanical Turk,
// but worker has not accepted the HIT yet.
// false if the page is viewed outside Mechanical Turk,
// OR the worker has accepted the HIT.
alert('Preview mode? ' + turkInfo.previewMode);
// true if the page is viewed outside mechanical turk,
// false otherwise.
alert('Outside turk? ' + turkInfo.outsideTurk);
```

View File

@ -29,15 +29,15 @@ Every jsPsych experiment utilizes the core library (contained in the `jspsych.js
* jsPsych.data.getLastChunkData
* jsPsych.data.displayData
### Turk module
### [Turk module](jspsych-turk.md)
* jsPsych.turk.turkInfo
* jsPsych.turk.submitToTurk
* [jsPsych.turk.submitToTurk](jspsych-turk.md#jspsychturksubmittoturk)
* [jsPsych.turk.turkInfo](jspsych-turk.md#jspsychturkturkinfo)
### Randomization module
### [Randomization module](jspsych-randomization.md)
* jsPsych.randomization.repeat
* jsPsych.randomization.factorial
* [jsPsych.randomization.factorial](jspsych-randomization.md#jspsychrandomizationfactorial)
* [jsPsych.randomization.repeat](jspsych-randomization.md#jspsychrandomizationrepeat)
### PluginAPI module

View File

@ -16,7 +16,7 @@ pages:
- ['core_library/jspsych-core.md', 'Core Library API', 'jsPsych core']
- ['core_library/jspsych-data.md', 'Core Library API', 'jsPsych.data']
- ['core_library/jspsych-randomization.md', 'Core Library API', 'jsPsych.randomization']
- ['core_library/jspsych-mturk.md', 'Core Library API', 'jsPsych.mturk']
- ['core_library/jspsych-turk.md', 'Core Library API', 'jsPsych.turk']
- ['core_library/jspsych-pluginAPI.md', 'Core Library API', 'jsPsych.pluginAPI']
- ['plugins/overview.md', 'Plugins', 'Overview']
- ['plugins/creating-a-plugin.md', 'Plugins', 'Creating a New Plugin']