Data¶
Package¶
data
¶
psyphy.data¶
submodule for handling psychophysical experiment data.
Includes: - dataset: ResponseData, TrialBatch, loaders - transforms: color/model space conversions - io: save/load datasets
Classes:
| Name | Description |
|---|---|
ResponseData |
Python-friendly incremental trial log. |
TrialBatch |
Container for a proposed batch of trials. |
TrialData |
Batched trial data for compute. |
ResponseData
¶
Python-friendly incremental trial log.
This container is convenient for adaptive trial placement and I/O (e.g., CSV), but it is not a compute-efficient representation for JAX.
Use :class:TrialData for model fitting and likelihood evaluation.
Methods:
| Name | Description |
|---|---|
add_batch |
Append responses for a batch of trials. |
add_trial |
append a single trial. |
copy |
Create a deep copy of this dataset. |
from_arrays |
Construct ResponseData from arrays. |
from_trial_data |
Build a ResponseData log from a :class: |
merge |
Merge another dataset into this one (in-place). |
tail |
Return last n trials as a new ResponseData. |
to_numpy |
Return stimuli and responses as NumPy arrays. |
to_trial_data |
Convert this log into the canonical JAX batch (:class: |
Attributes:
| Name | Type | Description |
|---|---|---|
contexts |
list[array]
|
|
responses |
list[array]
|
|
stim_shape |
tuple | None
|
|
stimuli |
list[array]
|
|
trials |
list[tuple[Any, ...]]
|
Return list of (stim1, stim2, ... , response) tuples. |
Source code in src/psyphy/data/dataset.py
trials
¶
add_batch
¶
add_batch(responses: list[Any], trial_batch: TrialBatch, contexts: list[Any] | None = None) -> None
Append responses for a batch of trials.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
responses
|
List[Any]
|
Responses corresponding to each stimulus group in the trial batch. |
required |
trial_batch
|
TrialBatch
|
The batch of proposed trials. |
required |
Source code in src/psyphy/data/dataset.py
add_trial
¶
append a single trial.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
tuple(Any, ...)
|
Group of presented stimuli each represented in any format (numpy array, list, etc.) Input must contain appropriate number of stimuli of appropriate dimension. |
required |
resp
|
Any
|
Subject response |
required |
Source code in src/psyphy/data/dataset.py
copy
¶
copy() -> ResponseData
Create a deep copy of this dataset.
Returns:
| Type | Description |
|---|---|
ResponseData
|
New dataset with copied data |
Source code in src/psyphy/data/dataset.py
from_arrays
¶
from_arrays(X: ndarray | ndarray, y: ndarray | ndarray, c: ndarray | ndarray | None = None) -> ResponseData
Construct ResponseData from arrays.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
(array, shape(n_trials, n_stimuli, input_dim) or (n_trials, input_dim))
|
Stimuli. If 3D, second axis is input stumili. For OddityTask, this is (ref, comparison) |
required |
y
|
(array, shape(n_trials, response_dim))
|
Responses |
required |
c
|
optional array, shape (n_trials, context_dim)
|
Context |
None
|
Returns:
| Type | Description |
|---|---|
ResponseData
|
Data container |
OddityTask Example
From paired stimuli¶
X = jnp.array([[[0, 0], [1, 0]], [[1, 1], [2, 1]]])
X is formed from refs = [[0, 0], [1, 1]], comparisons = [[1, 0], [2, 1]]¶
y = jnp.array([1, 0]) data = ResponseData.from_arrays(X, y)
Source code in src/psyphy/data/dataset.py
from_trial_data
¶
from_trial_data(data: TrialData) -> ResponseData
Build a ResponseData log from a :class:TrialData batch.
Source code in src/psyphy/data/dataset.py
merge
¶
merge(other: ResponseData) -> None
Merge another dataset into this one (in-place).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
ResponseData
|
Dataset to merge |
required |
Source code in src/psyphy/data/dataset.py
tail
¶
tail(n: int) -> ResponseData
Return last n trials as a new ResponseData.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of trials to keep |
required |
Returns:
| Type | Description |
|---|---|
ResponseData
|
New dataset with last n trials |
Source code in src/psyphy/data/dataset.py
to_numpy
¶
to_numpy() -> tuple[ndarray, ndarray]
Return stimuli and responses as NumPy arrays. Will NOT include contexts by default. Output always fixed length of 2.
Source code in src/psyphy/data/dataset.py
to_trial_data
¶
to_trial_data() -> TrialData
Convert this log into the canonical JAX batch (:class:TrialData).
Source code in src/psyphy/data/dataset.py
TrialBatch
¶
Container for a proposed batch of trials. Does NOT include context or responses.
Attributes:
| Name | Type | Description |
|---|---|---|
stimuli |
List[Tuple[Any, ...]]
|
Each trial is a tuple of all presented stimuli (stim1, stim2, ...). For OddityTask this is (reference, comparison) |
Methods:
| Name | Description |
|---|---|
from_stimuli |
Construct a TrialBatch from a list of stimuli (stim1, stim2, ...) groups. |
Source code in src/psyphy/data/dataset.py
from_stimuli
¶
from_stimuli(groups: list[tuple[Any, ...]]) -> TrialBatch
Construct a TrialBatch from a list of stimuli (stim1, stim2, ...) groups.
TrialData
¶
TrialData(stimuli: ndarray, responses: ndarray, context: ndarray | None = None, stimulus_names: tuple[str, ...] = ())
Batched trial data for compute.
This is the canonical, compute-efficient representation of observed trials.
Shapes
stimuli : (N, K, d) responses : (N, R) context : optional (N, C)
Dimension key
N : number of trials (batch dimension) K : number of stimuli per trial (e.g. K=2 for a two-alternative task; K=2 for the oddity task, where the reference is presented twice but only the unique mean is stored — the duplication is encoded in the task likelihood, not here) d : dimensionality of each stimulus coordinate R : number of response channels (R=1 for binary; R=2 for e.g. (choice, RT)) C : number of context channels (observer-state covariates that condition the likelihood but are not part of the stimulus space, e.g. fatigue level)
Notes
- You can also think of this as a more generic ML-style dataset
Xwith shape (N, K, d) plusywith shape (N, R). - This is intended to be JAX-friendly (PyTree of arrays) so likelihood and inference code can be JIT-compiled without touching Python containers.
- Context is optional. No current inbuilt uses.
Methods:
| Name | Description |
|---|---|
stimulus |
Return stimuli[:, k, :] for the slot named |
Attributes:
| Name | Type | Description |
|---|---|---|
context |
ndarray | None
|
|
num_trials |
int
|
Number of trials (N). |
responses |
ndarray
|
|
stimuli |
ndarray
|
|
stimulus_names |
tuple[str, ...]
|
|
stimulus
¶
stimulus(name: str) -> ndarray
Return stimuli[:, k, :] for the slot named name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Must match one of the entries in |
required |
Returns:
| Type | Description |
|---|---|
(ndarray, shape(N, d))
|
Stimulus coordinates for all trials at the named slot. |
Source code in src/psyphy/data/dataset.py
Data Containers for Response Data and Proposed Next Trials¶
dataset
¶
dataset.py
Core data containers for psyphy.
defines: - ResponseData: container for psychophysical trial data - TrialBatch: container for a proposed batch of trials
Notes
- Data is stored in standard NumPy (mutable!) arrays or Python lists.
- Use numpy for I/O and analysis.
- Convert to jax.numpy (jnp) (immutable!) arrays only when passing into WPPM or inference engines that require JAX/Optax.
Classes:
| Name | Description |
|---|---|
ResponseData |
Python-friendly incremental trial log. |
TrialBatch |
Container for a proposed batch of trials. |
TrialData |
Batched trial data for compute. |
ResponseData
¶
Python-friendly incremental trial log.
This container is convenient for adaptive trial placement and I/O (e.g., CSV), but it is not a compute-efficient representation for JAX.
Use :class:TrialData for model fitting and likelihood evaluation.
Methods:
| Name | Description |
|---|---|
add_batch |
Append responses for a batch of trials. |
add_trial |
append a single trial. |
copy |
Create a deep copy of this dataset. |
from_arrays |
Construct ResponseData from arrays. |
from_trial_data |
Build a ResponseData log from a :class: |
merge |
Merge another dataset into this one (in-place). |
tail |
Return last n trials as a new ResponseData. |
to_numpy |
Return stimuli and responses as NumPy arrays. |
to_trial_data |
Convert this log into the canonical JAX batch (:class: |
Attributes:
| Name | Type | Description |
|---|---|---|
contexts |
list[array]
|
|
responses |
list[array]
|
|
stim_shape |
tuple | None
|
|
stimuli |
list[array]
|
|
trials |
list[tuple[Any, ...]]
|
Return list of (stim1, stim2, ... , response) tuples. |
Source code in src/psyphy/data/dataset.py
trials
¶
add_batch
¶
add_batch(responses: list[Any], trial_batch: TrialBatch, contexts: list[Any] | None = None) -> None
Append responses for a batch of trials.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
responses
|
List[Any]
|
Responses corresponding to each stimulus group in the trial batch. |
required |
trial_batch
|
TrialBatch
|
The batch of proposed trials. |
required |
Source code in src/psyphy/data/dataset.py
add_trial
¶
append a single trial.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
tuple(Any, ...)
|
Group of presented stimuli each represented in any format (numpy array, list, etc.) Input must contain appropriate number of stimuli of appropriate dimension. |
required |
resp
|
Any
|
Subject response |
required |
Source code in src/psyphy/data/dataset.py
copy
¶
copy() -> ResponseData
Create a deep copy of this dataset.
Returns:
| Type | Description |
|---|---|
ResponseData
|
New dataset with copied data |
Source code in src/psyphy/data/dataset.py
from_arrays
¶
from_arrays(X: ndarray | ndarray, y: ndarray | ndarray, c: ndarray | ndarray | None = None) -> ResponseData
Construct ResponseData from arrays.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
(array, shape(n_trials, n_stimuli, input_dim) or (n_trials, input_dim))
|
Stimuli. If 3D, second axis is input stumili. For OddityTask, this is (ref, comparison) |
required |
y
|
(array, shape(n_trials, response_dim))
|
Responses |
required |
c
|
optional array, shape (n_trials, context_dim)
|
Context |
None
|
Returns:
| Type | Description |
|---|---|
ResponseData
|
Data container |
OddityTask Example
From paired stimuli¶
X = jnp.array([[[0, 0], [1, 0]], [[1, 1], [2, 1]]])
X is formed from refs = [[0, 0], [1, 1]], comparisons = [[1, 0], [2, 1]]¶
y = jnp.array([1, 0]) data = ResponseData.from_arrays(X, y)
Source code in src/psyphy/data/dataset.py
from_trial_data
¶
from_trial_data(data: TrialData) -> ResponseData
Build a ResponseData log from a :class:TrialData batch.
Source code in src/psyphy/data/dataset.py
merge
¶
merge(other: ResponseData) -> None
Merge another dataset into this one (in-place).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
ResponseData
|
Dataset to merge |
required |
Source code in src/psyphy/data/dataset.py
tail
¶
tail(n: int) -> ResponseData
Return last n trials as a new ResponseData.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of trials to keep |
required |
Returns:
| Type | Description |
|---|---|
ResponseData
|
New dataset with last n trials |
Source code in src/psyphy/data/dataset.py
to_numpy
¶
to_numpy() -> tuple[ndarray, ndarray]
Return stimuli and responses as NumPy arrays. Will NOT include contexts by default. Output always fixed length of 2.
Source code in src/psyphy/data/dataset.py
to_trial_data
¶
to_trial_data() -> TrialData
Convert this log into the canonical JAX batch (:class:TrialData).
Source code in src/psyphy/data/dataset.py
TrialBatch
¶
Container for a proposed batch of trials. Does NOT include context or responses.
Attributes:
| Name | Type | Description |
|---|---|---|
stimuli |
List[Tuple[Any, ...]]
|
Each trial is a tuple of all presented stimuli (stim1, stim2, ...). For OddityTask this is (reference, comparison) |
Methods:
| Name | Description |
|---|---|
from_stimuli |
Construct a TrialBatch from a list of stimuli (stim1, stim2, ...) groups. |
Source code in src/psyphy/data/dataset.py
from_stimuli
¶
from_stimuli(groups: list[tuple[Any, ...]]) -> TrialBatch
Construct a TrialBatch from a list of stimuli (stim1, stim2, ...) groups.
TrialData
¶
TrialData(stimuli: ndarray, responses: ndarray, context: ndarray | None = None, stimulus_names: tuple[str, ...] = ())
Batched trial data for compute.
This is the canonical, compute-efficient representation of observed trials.
Shapes
stimuli : (N, K, d) responses : (N, R) context : optional (N, C)
Dimension key
N : number of trials (batch dimension) K : number of stimuli per trial (e.g. K=2 for a two-alternative task; K=2 for the oddity task, where the reference is presented twice but only the unique mean is stored — the duplication is encoded in the task likelihood, not here) d : dimensionality of each stimulus coordinate R : number of response channels (R=1 for binary; R=2 for e.g. (choice, RT)) C : number of context channels (observer-state covariates that condition the likelihood but are not part of the stimulus space, e.g. fatigue level)
Notes
- You can also think of this as a more generic ML-style dataset
Xwith shape (N, K, d) plusywith shape (N, R). - This is intended to be JAX-friendly (PyTree of arrays) so likelihood and inference code can be JIT-compiled without touching Python containers.
- Context is optional. No current inbuilt uses.
Methods:
| Name | Description |
|---|---|
stimulus |
Return stimuli[:, k, :] for the slot named |
Attributes:
| Name | Type | Description |
|---|---|---|
context |
ndarray | None
|
|
num_trials |
int
|
Number of trials (N). |
responses |
ndarray
|
|
stimuli |
ndarray
|
|
stimulus_names |
tuple[str, ...]
|
|
stimulus
¶
stimulus(name: str) -> ndarray
Return stimuli[:, k, :] for the slot named name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Must match one of the entries in |
required |
Returns:
| Type | Description |
|---|---|
(ndarray, shape(N, d))
|
Stimulus coordinates for all trials at the named slot. |
Source code in src/psyphy/data/dataset.py
I/O¶
io
¶
io.py
I/O utilities for saving and loading psyphy data.
Supports: - CSV for human-readable trial logs
Notes
- Data is stored in NumPy arrays (via ResponseData.to_numpy()).
- Convert to jax.numpy when passing into models.
Functions:
| Name | Description |
|---|---|
load_posterior |
Load a Posterior object from pickle. |
load_responses_csv |
Load ResponseData from a CSV file. |
save_posterior |
Save a Posterior object to disk using pickle. |
save_responses_csv |
Save ResponseData to a CSV file. This is completely task agnostic. In the |
Attributes:
| Name | Type | Description |
|---|---|---|
PathLike |
|
load_posterior
¶
load_responses_csv
¶
Load ResponseData from a CSV file. Currently catering to OddityTask data format ONLY.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str or Path
|
Must be of expected format for OddityTask. |
required |
Returns:
| Type | Description |
|---|---|
ResponseData
|
|
Source code in src/psyphy/data/io.py
save_posterior
¶
save_responses_csv
¶
save_responses_csv(data: TrialData | ResponseData, path: PathLike) -> None
Save ResponseData to a CSV file. This is completely task agnostic. In the current implementation, it will simply create as many stimulus columns as inputs in the data and will label them "stimulus 1", "stimulus 2", etc. The response column will be labeled "response"
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
ResponseData
|
|
required |
path
|
str or Path
|
|
required |