-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deployed ca5ad19 with MkDocs version: 1.6.1
- Loading branch information
Unknown
committed
Nov 29, 2024
0 parents
commit 0cae5fc
Showing
129 changed files
with
145,995 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
462 changes: 462 additions & 0 deletions
462
Examples/Psychophysics/psychophysics/psychophysics.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,289 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "3juoofXHHcUh" | ||
}, | ||
"source": [ | ||
"# Generating Trial Sequences for a Closed-Loop Psychophysics Study\n", | ||
"\n", | ||
"In this example, we use SweetPea to generate an experimental sequence for a same-different Psychophysics experiment.\n", | ||
"\n", | ||
"You can find more in-depth tutorials on automated experimental design at the [SweetPea Website](https://sites.google.com/view/sweetpea-ai)." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "idp-M8hoHcUi" | ||
}, | ||
"source": [ | ||
"## Installation\n", | ||
"\n", | ||
"First, we will install SweetPea." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"id": "DBwMKbBvHcUi" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"%%capture\n", | ||
"!pip install sweetpea" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "Eq8G1LC3HcUj" | ||
}, | ||
"source": [ | ||
"## Regular Factors\n", | ||
"\n", | ||
"Next, we define the experimental factors of our experiment. In this experiment, select whether the number of dots in the two sets is the same or not.\n", | ||
"\n", | ||
"The experiment has two independent variables: The number of dots in the first set and the number of dots in the second set. Here, we want to counterbalance the number of dots for stimulus 1 and stimulus, respectively." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"id": "m87zIbuLHcUj" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"from sweetpea import Factor\n", | ||
"\n", | ||
"# the first experimental factor indicates the number of dots in the\n", | ||
"# left stimulus. It has two levels, i.e., two possible values for the\n", | ||
"# number of dots, either 40 and or 70 dots.\n", | ||
"num_dots_left = Factor('dots_left', [40, 70])\n", | ||
"\n", | ||
"# the second experimental factor indicates the number of dots in the\n", | ||
"# right stimulus. It also has two levels: 40 and 70 dots.\n", | ||
"num_dots_right = Factor('dots_right', [70, 40])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "lwtv_rJvHcUj" | ||
}, | ||
"source": [ | ||
"The code defines the two variables in terms of two experimental factors, respectively: the number of dots in the left stimulus and the number of dots in the right stimulus. Here, we assume that the number of dots can either be 40 or 70." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "CxOt9_B1HcUj" | ||
}, | ||
"source": [ | ||
"## Synthesize Trial Sequences\n", | ||
"\n", | ||
"Next, we generate an experiment trial sequence. In this sequence, we want to counterbalance the levels of both factors. We also want to specify a constraint to include at least 20 trials." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"id": "wJxPbAPTHcUj", | ||
"outputId": "f2c69dbf-dba1-4de5-e237-28e17385a97b" | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Sampling 1 trial sequences using CMSGen.\n", | ||
"Encoding experiment constraints...\n", | ||
"Running CMSGen...\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"from sweetpea import MinimumTrials, CrossBlock, synthesize_trials, CMSGen, experiments_to_dicts\n", | ||
"\n", | ||
"# the experimental design includes all relevant factors\n", | ||
"# (whether counterbalanced or not)\n", | ||
"design = [num_dots_left, num_dots_right]\n", | ||
"\n", | ||
"# the crossing specifies which factors are fully counterbalanced\n", | ||
"crossing = [num_dots_left, num_dots_right]\n", | ||
"\n", | ||
"# we also add a constraint to include at least 20 trials\n", | ||
"constraints = [MinimumTrials(20)]\n", | ||
"\n", | ||
"# next, we define an experimental block based on the design, crossing,\n", | ||
"# and constraints\n", | ||
"block = CrossBlock(design, crossing, constraints)\n", | ||
"\n", | ||
"# we then use a SAT-Solver to find one suitable experimental sequence\n", | ||
"experiment = synthesize_trials(block, 1, CMSGen)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "12-m8X4fHcUk" | ||
}, | ||
"source": [ | ||
"## Printing and Extracting Experimental Sequence" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "XhPZkwLtHcUk" | ||
}, | ||
"source": [ | ||
"We can print the resulting experiment:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"id": "qMsNBDw1HcUk", | ||
"outputId": "45b7e0c1-1c05-4988-aa79-87d9fa7ce585" | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"\n", | ||
"1 trial sequences found.\n", | ||
"\n", | ||
"Experiment 0:\n", | ||
"Number of Dots for Left Stimulus 70 | Number of Dots for Right Stimulus 1 40\n", | ||
"Number of Dots for Left Stimulus 40 | Number of Dots for Right Stimulus 1 70\n", | ||
"Number of Dots for Left Stimulus 40 | Number of Dots for Right Stimulus 1 40\n", | ||
"Number of Dots for Left Stimulus 40 | Number of Dots for Right Stimulus 1 40\n", | ||
"Number of Dots for Left Stimulus 70 | Number of Dots for Right Stimulus 1 70\n", | ||
"Number of Dots for Left Stimulus 70 | Number of Dots for Right Stimulus 1 70\n", | ||
"Number of Dots for Left Stimulus 40 | Number of Dots for Right Stimulus 1 40\n", | ||
"Number of Dots for Left Stimulus 70 | Number of Dots for Right Stimulus 1 70\n", | ||
"Number of Dots for Left Stimulus 40 | Number of Dots for Right Stimulus 1 40\n", | ||
"Number of Dots for Left Stimulus 70 | Number of Dots for Right Stimulus 1 70\n", | ||
"Number of Dots for Left Stimulus 70 | Number of Dots for Right Stimulus 1 40\n", | ||
"Number of Dots for Left Stimulus 70 | Number of Dots for Right Stimulus 1 40\n", | ||
"Number of Dots for Left Stimulus 40 | Number of Dots for Right Stimulus 1 40\n", | ||
"Number of Dots for Left Stimulus 70 | Number of Dots for Right Stimulus 1 40\n", | ||
"Number of Dots for Left Stimulus 40 | Number of Dots for Right Stimulus 1 70\n", | ||
"Number of Dots for Left Stimulus 70 | Number of Dots for Right Stimulus 1 40\n", | ||
"Number of Dots for Left Stimulus 40 | Number of Dots for Right Stimulus 1 70\n", | ||
"Number of Dots for Left Stimulus 40 | Number of Dots for Right Stimulus 1 70\n", | ||
"Number of Dots for Left Stimulus 40 | Number of Dots for Right Stimulus 1 70\n", | ||
"Number of Dots for Left Stimulus 70 | Number of Dots for Right Stimulus 1 70\n", | ||
"\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"from sweetpea import print_experiments\n", | ||
"\n", | ||
"print_experiments(block, experiment)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "2NkHXLoOHcUk" | ||
}, | ||
"source": [ | ||
"And we can store the resulting experimental sequence in a dictionary:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"id": "1u5Vif3jHcUk", | ||
"outputId": "79dd8001-a232-453c-eeaf-20f6f0dc18b4" | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"[[{'Number of Dots for Left Stimulus': 70, 'Number of Dots for Right Stimulus 1': 40}, {'Number of Dots for Left Stimulus': 40, 'Number of Dots for Right Stimulus 1': 70}, {'Number of Dots for Left Stimulus': 40, 'Number of Dots for Right Stimulus 1': 40}, {'Number of Dots for Left Stimulus': 40, 'Number of Dots for Right Stimulus 1': 40}, {'Number of Dots for Left Stimulus': 70, 'Number of Dots for Right Stimulus 1': 70}, {'Number of Dots for Left Stimulus': 70, 'Number of Dots for Right Stimulus 1': 70}, {'Number of Dots for Left Stimulus': 40, 'Number of Dots for Right Stimulus 1': 40}, {'Number of Dots for Left Stimulus': 70, 'Number of Dots for Right Stimulus 1': 70}, {'Number of Dots for Left Stimulus': 40, 'Number of Dots for Right Stimulus 1': 40}, {'Number of Dots for Left Stimulus': 70, 'Number of Dots for Right Stimulus 1': 70}, {'Number of Dots for Left Stimulus': 70, 'Number of Dots for Right Stimulus 1': 40}, {'Number of Dots for Left Stimulus': 70, 'Number of Dots for Right Stimulus 1': 40}, {'Number of Dots for Left Stimulus': 40, 'Number of Dots for Right Stimulus 1': 40}, {'Number of Dots for Left Stimulus': 70, 'Number of Dots for Right Stimulus 1': 40}, {'Number of Dots for Left Stimulus': 40, 'Number of Dots for Right Stimulus 1': 70}, {'Number of Dots for Left Stimulus': 70, 'Number of Dots for Right Stimulus 1': 40}, {'Number of Dots for Left Stimulus': 40, 'Number of Dots for Right Stimulus 1': 70}, {'Number of Dots for Left Stimulus': 40, 'Number of Dots for Right Stimulus 1': 70}, {'Number of Dots for Left Stimulus': 40, 'Number of Dots for Right Stimulus 1': 70}, {'Number of Dots for Left Stimulus': 70, 'Number of Dots for Right Stimulus 1': 70}]]\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# we can export the experimental sequence as a dictionary\n", | ||
"sequence = experiments_to_dicts(block, experiment)\n", | ||
"print(sequence)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "6ZzJayP3HcUk" | ||
}, | ||
"source": [ | ||
"## Writing a Function to Automate the Generation of Trial Sequences" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "kUHzWgMHHcUk" | ||
}, | ||
"source": [ | ||
"Next, we wrap the code above into a function that will generate an experimental sequence for an arbitrary set of two stimulus intensities, e.g., number of dots. We will accept the number of trials as an argument." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"id": "PUFCqHRrHcUk" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"from sweetpea import Factor, MinimumTrials, CrossBlock, synthesize_trials, CMSGen, experiments_to_dicts\n", | ||
"\n", | ||
"def trial_sequence(num_dots_1, num_dots_2, min_trials):\n", | ||
"\n", | ||
" # define regular factors\n", | ||
" num_dots_left = Factor('dots_left', [num_dots_1, num_dots_2])\n", | ||
" num_dots_right = Factor('dots_right', [num_dots_1, num_dots_2])\n", | ||
"\n", | ||
" # define experimental block\n", | ||
" design = [num_dots_left, num_dots_right]\n", | ||
" crossing = [num_dots_left, num_dots_right]\n", | ||
" constraints = [MinimumTrials(min_trials)]\n", | ||
"\n", | ||
" block = CrossBlock(design, crossing, constraints)\n", | ||
"\n", | ||
" # synthesize trial sequence\n", | ||
" experiment = synthesize_trials(block, 1, CMSGen)\n", | ||
"\n", | ||
" # export as dictionary\n", | ||
" return experiments_to_dicts(block, experiment)[0]" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"colab": { | ||
"provenance": [] | ||
}, | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"name": "python" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |
Oops, something went wrong.