DataDIVER is a collection of Python utilities designed to support the discovery, fitting, simulation, and diagnostic evaluation of interpretable scientific behavioral models:
- Code Complexity Analysis (
code_metrics.py): Metrics and scores to evaluate code complexity (e.g. for searching or evolving code models). - JAX-based Model Fitting (
jax_model_fitting.py): Utilities for fitting and evaluating choice prediction models using JAX, optimized for speed with JIT compilation. - Dataset Containers & Utilities (
types.py,dataset_utils.py): Hierarchical session/subject containers (NeuroSession,NeuroDataset,ArgSpec), NaN-safe JSON serialization (load_dataset_json,save_dataset_json), and multi-subject fitting (fit_dataset_subjects). - Closed-Loop Synthetic Simulation (
synthetic_data.py): Closed-loop task environments (PayoutMatrixObservationEnvironment,TwoStepEnvironment,environment_from_dataset) and synthetic rollout generation (simulate_dataset,generate_synthetic_dataset). - Behavioral Diagnostics (
diagnostics.py): Session trajectory plots (plot_session), bout-length distributions (plot_bout_lengths), and lagged trial-history regressions (plot_trial_history_regression). - Interactive Parameter Explorer (
param_explorer/): Notebook widget for exploring models loaded from standalone.pyfiles on real and handcrafted probe sessions, with live parameter sliders and model-file hot-reloading.
This module provides tools for evaluating code complexity using standard AST analysis and Halstead metrics.
-
get_complexity_metrics(code: str)Computes raw complexity metrics for the given Python code string. Returned metrics include:num_lines: Total line count.num_comments: Number of comment lines (starting with#).total_imports: Number of imported symbols.num_variables: Number of variables.halstead_vocab,halstead_size,halstead_volume,halstead_difficulty,halstead_effort: Standard Halstead complexity metrics.
-
get_complexity_scores(code: str)Computes complexity scores normalized to the range[0, 1]. Scores are transformed such that higher values always represent lower complexity, making them suitable for optimization objectives. -
normalize_metrics(raw_metrics: dict[str, Any])Normalizes raw metric values to[0, 1]based on empirical min/max ranges. -
verify_num_params(agent_fun: str, n_params: int)Verifies that the number of parameters accessed by the agent function source code is at mostn_params. Raises aValueErrorif the function accesses more parameters than are available.
This module provides JIT-compiled optimization loops to fit agent parameters to behavioral choice datasets.
-
ChoicePredictionDataset(dataclass) Represents the dataset for model fitting.inputs: A dictionary mapping input names to JAX arrays (e.g. past choices, rewards).next_choice: An array of target choices to predict.next_ignore: A binary mask indicating trials to ignore during loss computation.
-
ChoicePredictionAgentFunction(Protocol) A protocol defining the interface for target agent models:def __call__( self, *, params: chex.Array, agent_state: Any, **inputs: jax.typing.ArrayLike, ) -> tuple[chex.Array, Any]: ...
Returns logits for the next choice and the updated internal agent state.
-
make_choice_prediction_dataset(...)Helper function to format a raw data dictionary into aChoicePredictionDataset. Shifts inputs by one trial so they represent past trial history. -
split_choice_prediction_dataset(...)Splits a dataset into two halves (even-indexed and odd-indexed sessions) for cross-validation. -
fit_choice_prediction_model(...)Fits agent parameters to a dataset. Uses a JIT-compiled optimization loop and executes multiple optimization restarts to avoid local minima. -
evaluate_choice_prediction_model(...)Evaluates the choice prediction performance of an agent with given parameters on a dataset. Returns the normalized likelihood and predictions. -
choice_prediction_twofold_crossvalidation(...)Performs even-odd two-fold cross-validation. Fits the model on one half of the dataset and evaluates it on the other half, returning the cross-validated normalized likelihood.
load_dataset_json(path)/save_dataset_json(dataset, path)Loads or saves aNeuroDatasetwith per-subject session indexing and model input signature metadata.fit_dataset_subjects(dataset, agent_fun, n_params=10, ...)Fitsagent_funacross subjects indataset.simulate_dataset(dataset, agent_fun, agent_params, ...)Simulates closed-loop synthetic behavioral data matchingdataset's task structure and session payout schedules.plot_session(dataset, ...)/plot_bout_lengths(dataset, ...)/plot_trial_history_regression(dataset, ...)Plots single-session trajectories, bout-length CDFs, and task-appropriate lagged trial-history regressions.
Launch the interactive widget in Jupyter or Colab from a standardized dataset
JSON, a standalone .py file defining your JAX model(s), and an optional
precomputed fits JSON:
import datadiver
from datadiver.library import dataset_utils
# 1. Load a standardized dataset JSON and fit a JAX model per subject
dataset = dataset_utils.load_dataset_json("path/to/dataset.json")
fit_results = dataset_utils.fit_dataset_subjects(dataset, my_agent_fn, n_params=4)
# 2. Launch the interactive Parameter Explorer on a standalone .py model file
session = datadiver.build_param_explorer_from_files(
dataset_json_path="path/to/dataset.json",
model_py_path="path/to/models.py",
fits_json_path="path/to/fits.json", # optional
)
session.show_explorer()Edit models.py and click Reload (or call session.reload_model_file())
to inspect updated models without rebuilding the session.
Copyright 2026 DeepMind Technologies Limited
All software is licensed under the Apache License, Version 2.0 (Apache 2.0); you may not use this file except in compliance with the Apache 2.0 license. You may obtain a copy of the Apache 2.0 license at: https://www.apache.org/licenses/LICENSE-2.0
All other materials are licensed under the Creative Commons Attribution 4.0 International License (CC-BY). You may obtain a copy of the CC-BY license at: https://creativecommons.org/licenses/by/4.0/legalcode
Unless required by applicable law or agreed to in writing, all software and materials distributed here under the Apache 2.0 or CC-BY licenses are distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the licenses for the specific language governing permissions and limitations under those licenses.
This is not an officially supported Google product.