Skip to content
eulerlabPublic

About

2P imaging data joint tables and schemas

Resources

Stars

2 stars

Watchers

1 watching

Forks

Repository files navigation

Unit tests Coverage MySQL integration Python DataJoint License: MIT uv Lint: flake8

djimaging

2P imaging data joint tables and schemas

Getting started

Create a MySQL account

Ask IT to create a MySQL user account for you. They will send you a username and activation password.

❗ You probably don't want your own local MySQL server, so don't try to set one up but get access to the shared one.

Create a remote docker container

Create a docker container and install this package and its requirements in it.

Download the package:

git clone https://github.com/eulerlab/djimaging.git

Install the package as an editable package using uv:

cd djimaging
uv pip install -e .

If you want to use autorois, receptive fields or the CellViz viewer (see CellViz), install with the optional extras:

uv pip install -e ".[autorois]"
uv pip install -e ".[rf]"
uv pip install -e ".[cellviz]"
uv pip install -e ".[autorois,rf,cellviz]"

AutoROIs models

By default, draw_roi_mask / load_default_autorois_models offer the latest U-Net (plus the correlation-based model), which only need the autorois extra. Other models have to be requested explicitly:

autorois_models = load_default_autorois_models(models=['UNet', 'Cellpose3_cyto3', 'MEDIAR'])  # also: 'Cellpose3_cyto2', 'UNet_v1', 'CorrRoiMask'

The finetuned Cellpose3 (Cellpose3_cyto2, Cellpose3_cyto3) and MEDIAR models are not part of the package dependencies, because their requirements conflict with the up-to-date core dependencies: mediar (fork sarmueller/MEDIAR) pins monai==1.4.0, which needs numpy<2, and cellpose 3.x needs numpy<2.1 (older numpy/numba for everyone). Adding them breaks uv sync. If you need them, install them yourself, preferably in a separate environment, e.g. uv pip install "cellpose>=3.1.1,<4" or uv pip install git+https://github.com/sarmueller/MEDIAR.git (possibly with monai>=1.5 and --no-deps to keep your numpy). The MEDIAR wrapper is untested with monai>=1.5.

Cascade spike estimation

The spike_estimation tables (CascadeTraces, CascadeSpikes) use Cascade, which is not a package dependency: it is not on PyPI, and its setup.py pins Python 3.7, tensorflow==2.3 and keras==2.3.1, which conflicts with the core dependencies (Python >=3.11, numpy>=2). Its pretrained models also live in a folder of the Cascade repository. So use a git checkout of Cascade:

  1. Clone it: git clone https://github.com/HelmchenLabSoftware/Cascade.git. Use commit bcc0014 (2026-10-27) or later: older versions shift all spike probabilities by one frame (issue #90). CascadeSpikes.populate warns when the checkout is older, and stores its git commit in CascadeSpikes.cascade_commit.
  2. Install its runtime dependencies (tensorflow, h5py, ruamel.yaml) in the djimaging environment, without Cascade's own pins. Recent TensorFlow versions with Keras 3 may need tf-keras to load the older .h5 models.
  3. Point CascadeParams to the checkout, e.g. CascadeParams().add_default(model_name=..., cascade_folder='/path/to/Cascade') (models are looked up in <cascade_folder>/Pretrained_models by default). CascadeSpikes.populate adds the folder to sys.path and imports cascade2p from there.

How ROI masks get into the database, the ROI canvas, corrections, shrink to fit and motion mode: see ROI_MASKS.md.

⚠️ Motion detection and motion QC are experimental (motion / motion_refine blocks: MotionDetection, MotionRoiMask, MotionTrust, motion-corrected Traces; also HighResAlignment). They were validated on a limited set of recordings (xy RGC fields of four experiments and one z-stack), and thresholds and the z estimates are not calibrated beyond that. Check the plots for your data before relying on them. Method, validation and known limitations: docs/motion_qc.md.

❗ To test if the package was successfully installed, e.g. open a jupyter notebook in your container and call import djimaging.

Create a config file

Create a datajoint config *.json file, e.g. based on the template djimaging/djconfig/djconf_template.json. Fill out the missing values; if you don't know how, ask someone in your group.

❗ Never upload this personal config file to GitHub.

DataJoint 2 requires MySQL 8 with utf8mb4/utf8mb4_bin and named stores. The template uses a reference-only DataJoint store rooted at /gpfs01/euler/data for externally managed acquisition, stimulus, and model files. DataJoint does not write <filepath> values, although filesystem tools such as ROI-mask editors may still require write access to adjacent folders. Traces, snippets, response vectors, field/presentation channel averages, ROI masks, and small analysis arrays/objects use <blob> and are stored directly in the database. Large stimulus and population matrices, full spatiotemporal receptive fields, high-resolution channel averages, correlation maps, and model objects use <npy@processed> or <blob@processed> in the separate writable processed store. Paths inserted into <filepath@reference> fields must be relative to the common reference root. JSON does not support comments, so these store roles are documented here rather than inside the config template.

Create a project

Inside the root folder djimaging (not in djimaging/djimaging) run the make project script by calling

python3 make_project.py your_project_name

This creates a standalone project next to the djimaging repo, in <parent folder of djimaging>/djimaging-projects/your_project_name/ (choose another folder with --target-dir PATH; it must be outside the djimaging repo). The project is an installable package (dji_your_project_name) that is ready to be a git repository:

djimaging-projects/your_project_name/
  README.md               the exact make_project command, djimaging commit, blocks and notebooks
  pyproject.toml          depends on djimaging (+ the extras the blocks need), [tool.uv.sources] points to your
                          local djimaging repo (editable); pytest fails on DataJoint v1 fetch()/ERD warnings
  .gitignore              Python, environments, notebook checkpoints, DataJoint config files, large data files
  .gitattributes, .flake8
  tests/test_schema.py    the schema can be imported and defines its tables (no database needed)
  dji_your_project_name/
    settings.py           CONFIG_FILE and SCHEMA_NAME, used by all notebooks
    schemas/your_project_name_schema.py   the schema, built from the table blocks you select
    tables/my_table.py    copy of _new_table_example.py, a starting point for your own table templates
    notebooks/
      01_setup.ipynb      data import (UserInfo, Channel, RawDataParams, Experiment, setup tables, Field, Stimulus,
                          Presentation, experiment history), creates the schema and shows its diagram
      02_roi_masks.ipynb  ROI masks (AutoROIs, corrections, ROI canvas) and Roi
      03_traces.ipynb     motion correction (motion block), traces, preprocessing, snippets, averages
      04_<block>.ipynb    one numbered notebook per further block, in the order of the table below
      drop_schema.ipynb   drops the schema (not numbered, so it is never run by accident)

Then install the project, e.g. with uv sync --extra dev in the project folder (or pip install -e "<djimaging>[autorois]" and pip install -e ".[dev]"), check settings.py and run the notebooks in their order. Every notebook connects on its own (config, schema, activate_schema), so a notebook can be rerun without the ones before; only 01_setup creates the schema. settings.SCHEMA_NAME defaults to ageuler_<login name>_<project>; use a fixed name for a schema that several users populate together.

❗ Do not commit personal config files (the generated .gitignore excludes the usual names).

Older projects in the git-ignored djimaging/djimaging/user/<project>/ folder keep working (imported as djimaging.user.<project>), but make_project.py no longer creates them.

Choosing table blocks

Instead of copying a fixed schema, make_project.py assembles the schema of your project (and the notebook) from table blocks. A block is a small group of related tables, so you only get the tables (and the notebook sections) you need.

Block Tables Requires
core UserInfo, Experiment, Region, Field, RetinaOrientation, Stimulus, Presentation, RoiMask, Roi, Traces, Snippets, Averages –
history ExpHistory, ExpHistoryField, ExpHistoryStimLog, QdsPyLogFile, QdsPyLog, PresentationLog (see below) core
basic ChirpQI, ChirpFeatures, OsDsIndexes core
location OpticDisk, RelativeFieldLocation, RetinalFieldLocation core
highres HighResAlignment (shift and match of the HighRes scan vs. the ROI mask; populates HighRes) core
classifier Baden16TracesV2, ClassifierV2, CelltypeAssignmentV2 basic
rf_sta FastSta, SplitRF, FitGauss2DRF, FitDoG2DRF core
rf_glm RfGlm, SplitRfGlm, FitGauss2DRfGlm, FitDoG2DRfGlm core
stimulator Stimulator, ExperimentStimulator, ImagingSetup, ExperimentImagingSetup (before Field) core
mouse_cam MouseCamGeometry stimulator
mb_direction MovingBarVersion, MbDirectionMap, MbRetinalPrefDir basic, stimulator
rf_offset RelativeRoiLocationWrtField, RfOffset, RfRoiOffset location, rf_sta, stimulator
cellviz no tables: notebook with the CellViz viewer (see CellViz) core

The classifier block reads the moving bar features of the RGC classifier (time component and DS p-value) from OsDsIndexes (block basic), so both use the same moving bar analysis (Baden et al. 2016: SVD tuning curve, DS/OS index as resultant vector length, permutation test). They match the training data of the classifier only with the per-trial snippet baseline of Baden et al. 2016 (snippet_base_dt in Stimulus: 5 x 0.128 s for the moving bar, 8 x 0.128 s for the chirp; the defaults of add_movingbar and add_chirp), OsDsIndexes._n_shuffles >= 1000 and moving bar snippets at 7.8125 Hz; Baden16TracesV2 checks this. Without the per-trial baseline, slow drift shared by the consecutive opposite directions of the moving bar inflates orientation selectivity.

The stimulator blocks hold all setup-dependent parameters, per setup and period and assigned to each experiment: stimulus geometry (how the QDSpy screen is projected onto the retina, used for moving bar directions on the retina, RF positions relative to ROIs and mouse cam orientation) and stimulator delay (Stimulator), and scan width and light artifact rows (ImagingSetup). make_project.py inserts the stimulator tables before Field and binds them in Field, Presentation and HighRes, which then take delay, pixel size and artifact rows from them (without the block, the setupid constants of setup_utils are used as before). Every assignment has a confidence (high, medium, low) and a reason; conventions are documented in djimaging/utils/stimulator.py. With the history block, moving bar and mouse cam versions are read from the QDSpy logs (PresentationLog).

The project name becomes part of the package name dji_<project> and the module <project>_schema, so it may only contain letters, digits and underscores and must not start with a digit; anything else is rejected.

core is always included, and any block a selected block requires is added automatically.

python3 make_project.py your_project_name                         # interactive: asks y/N for each block
python3 make_project.py your_project_name --blocks basic,rf_sta   # explicit list (core is added for you)
python3 make_project.py your_project_name --all                   # every block (except motion_refine, see below)
python3 make_project.py your_project_name --blocks basic --refined-roi-mask   # also the optional motion_refine block

motion_refine (RefinedRoiMask, refines the field ROI mask borders in motion mode) is not asked interactively and not part of --all, because it made no measurable difference to the traces. Force it with --refined-roi-mask (or by listing it in --blocks); it requires, and thereby adds, the motion block.

Without --blocks/--all, the script asks interactively; when there is no terminal (e.g. in a script or CI) it falls back to core only. Unknown block names raise an error that lists the available blocks.

Existing files are never overwritten: if a file of the project already exists, the script prints a message and skips it. To regenerate files with a different selection, move or delete the old ones first. The project README records the command with every option given explicitly (also the answers given interactively), so running it again creates the same project. The notebooks are written as Jupyter notebooks (.ipynb) by default. With --notebook-format py they are written as percent-format .py files instead (easier to diff and review); they can be opened as notebooks or converted later with jupytext --to ipynb.

Experiment history and listed stimuli (history block)

ExpHistory registers every ScanM recording of every experiment, independent of stimuli and fields: z-stacks, high resolution, optic disk and blood vessel recordings, test recordings, recordings that were stopped early and recordings of stimuli that are not in the Stimulus table. It scans the pre_data_dir and raw_data_dir of UserInfo and the folders in ExpHistory.extra_data_dirs, links .smp/.smh and .h5 files of the same recording (by the original file name stored in the .h5, the file name, or the time stamp; see link_method), and stores the scan parameters needed for the light exposure history (start, duration, zoom, pixel size, pixel dwell time, stage position, setup, number of triggers). Laser power and stimulus intensity are not stored digitally. The pixel size is computed from the dated scan width of ExperimentImagingSetup if ExpHistory.experiment_imaging_setup_table is bound (as for Field, Presentation and HighRes), otherwise from the setupid constant; the scan width used is stored in scan_width_um. ExpHistoryField links recordings to fields, ExpHistoryStimLog assigns the QDSpy stimuli (log entries) to the recordings during which they were shown (the clock offset between the QDSpy and ScanM computers is estimated).

A recording is listed if the stimulus part of its file name is one of the _-separated aliases of a stimulus in Stimulus (djimaging.utils.stimulus_listing). Only listed recordings become Presentations, and everything with stimulus-specific data (Traces, Snippets, Averages, analyses) depends on Presentation. So unlisted (e.g. unpublished) stimuli are visible in the history with their names, but never get traces. ExpHistory().listed(Stimulus) shows the status of every recording.

The light exposure history of an experiment (djimaging.utils.exposure_history, fetched with exposure_history_dj.fetch_exposure_history) lists every laser scan and stimulus in time order with position, duration, setup, stimulator and processing status, and writes it as CSV, JSON and HTML (export_experiment). Unknown values are written as unknown and gaps are listed explicitly; unlisted stimuli appear by name only, never with results. The HTML report also has a field map: the images of all recorded fields (also structure-only fields, read from their files) at their stage positions, with a border colour for response vs. structure-only fields and the retinal position if known (exposure_history_dj.fetch_field_map). export_exposure_history(..., next_to_ini=True) also writes the report to <experiment folder>/exposure_history/ next to the .ini file; exists='error' | 'skip' | 'overwrite' (default 'skip') decides about existing reports, and only files recognised as earlier reports are ever overwritten. The history notebook part ends with a cell that exports it for every experiment.

Imaging channels (functional and background)

The imaging channels are defined in a Channel table (ch_id, ch_name, is_functional, roi_default), which every generated schema contains. The standard recording has one functional channel (Ch0) and one background channel (Ch1, e.g. SR-101 as marker for blood vessels):

python3 make_project.py your_project_name --blocks basic                                   # 1 functional, 1 background
python3 make_project.py your_project_name --n-functional 2 --n-background 0                # two-color imaging
python3 make_project.py your_project_name --legacy-userinfo-stacks                         # old design, see below

Without --n-functional and --n-background, the script asks interactively (default 1 and 1; the defaults are also used when there is no terminal). The channels are named wDataCh0, wDataCh1, ... (functional channels first); the generated 01_setup notebook fills Channel in an extra cell, where you can edit the names and the functional list.

  • Functional channels (is_functional): traces are only extracted for these. Background channels have no traces, but they are loaded in the ROI canvas and the field plots.
  • With one functional channel, keys, tables and plots are the same as in older schemas. With several functional channels, the generated schema sets Traces._use_channels = True, and ch_id becomes part of the primary key of Traces and of all tables that depend on it (PreprocessTraces, Snippets, Averages, response features, ...), so these are computed separately per channel. This changes primary keys, so the number of functional channels cannot be changed in a populated schema; create a new schema and repopulate.
  • Traces must be computed from the stack (compute_from_stack=True in RawDataParams) with several functional channels.
  • UserInfo has _use_channels = True and channel_table = Channel: the channels replace the columns data_stack_name and alt_stack_name, which are then not part of UserInfo. Tables that need a main and an alternative stack (field plots, Roi, Brightness, ...) use the roi_default channel and the first other channel (UserInfo().get_stack_names(key)).

ROI canvas: draw_roi_mask loads all channels of Channel. Every channel is available as background (stack: <name>[mean] / [std]). One channel is the target for ROI detection (cc tool, diagnostics, auto shift and AutoROIs); it is preselected by roi_default in Channel (Channel().add_channels(..., roi_channel='wDataCh1'), default: the first functional channel), can be overridden with draw_roi_mask(roi_channel=...) and changed in the canvas ("Target"). AutoROIs only use the target channel (plus one other channel for the two-channel U-Net v0.1.0); multi-color AutoROIs are not supported. The ROI mask is shared by all channels.

Old schemas keep working, because the templates default to the old behavior: UserInfoTemplate._use_channels and TracesTemplate._use_channels are False, so the stacks are defined by data_stack_name (functional channel) and alt_stack_name in UserInfo. To generate a new schema like this, use --legacy-userinfo-stacks. To use the Channel table in a hand-written schema, define class Channel(core.ChannelTemplate) and set _use_channels = True and channel_table = Channel on UserInfo (and on Traces for several functional channels). The Channel table and UserInfo must be created together, so this requires a new schema.

Notes:

  • Tables that aggregate over ROIs and are not listed above should be checked before use with several functional channels.

How blocks work

Every block has two matching parts:

  • Schema part: djimaging/schemas/blocks/<name>.py subclasses the table templates under @schema and binds their abstract table attributes. A block imports the block it builds on (e.g. from djimaging.schemas.blocks.core import *), so it is also a valid, importable schema on its own.
  • Notebook part: djimaging/notebooks/blocks/<name>.py is a percent-format (# %%) notebook section that starts with a top-level heading (# # Title), which becomes the title of its notebook. The notebook key of the block in BLOCKS says where it goes: 'own' (default) a numbered notebook of its own, 'setup', 'rois' or 'traces' appended to 01_setup, 02_roi_masks or 03_traces. The core part is split at its # # Core: ROIs and # # Core: Traces and responses headings into these three notebooks; stimulator (before Field), motion (before Traces) and motion_refine (before MotionRoiMask) are inserted at fixed places. Every notebook starts with preamble.py (connect, import and activate the schema), 01_setup also gets overview.py (diagram), and cleanup.py becomes drop_schema. Placeholders such as SCHEMA_MODULE, PACKAGE and BLOCKS are filled in. Use plot_if_data (from djimaging.utils.dj_utils, imported by the preamble) for plots, so that Run all works on empty tables. A notebook must only use names it defines itself or gets from the preamble.

To generate your files, make_project.py

  1. resolves the selection (adds required blocks, removes duplicates, orders them as in BLOCKS),
  2. concatenates the schema parts into one file: the inter-block imports are dropped (the block code is already included above), all other imports are de-duplicated, merged per module and moved to the top,
  3. builds the numbered notebooks from the notebook parts and converts them to .ipynb via jupytext,
  4. writes the project files (README, pyproject, settings, ...); extras of the selected blocks become extras of the djimaging dependency.

The generated schema starts with a header (blocks, channels, the make_project command) and ends with a commented-out example showing how to add your own table (see tables/my_table.py). The ready-made schemas in djimaging/schemas/ (core_schema, full_rgc_schema, rf_glm_schema, tutorial_schema) are still available to copy; core_schema re-exports the core block.

Adding a new block

  1. Add the schema part djimaging/schemas/blocks/<name>.py. Start with an import of the blocks it depends on, e.g. from djimaging.schemas.blocks.core import *, then define the tables. Imports must be top-level, since they are parsed to merge the blocks.
  2. Add the notebook part djimaging/notebooks/blocks/<name>.py with the sections that populate and inspect your tables, starting with a top-level heading (# # Title).
  3. Register the block in BLOCKS in make_project.py with a description and its requires, and if needed notebook (default 'own') and extras (extras of djimaging it needs, e.g. ['rf']). The order of that dict is the order in the generated files (and of the numbered notebooks), so list a block after the blocks it requires.

CellViz: inspect recordings

djimaging.cellviz is a read-only notebook viewer for one field of your populated schema: choose a presentation and channel, play back the functional movie with the ROIs of that presentation's mask (or the HighRes image with the ROIs registered onto it), show the ROI outlines, the ROI mask or the ROI activity as overlay, select cells by clicking or from a list, and follow their traces (or an ROI x time heatmap of all eligible ROIs) in sync with the movie and the stimulus triggers, next to their trial mean ± SD (repeats cut like Snippets; for the moving bar per direction). A quality filter uses ChirpQI.qidx (chirp) and OsDsIndexes.d_qi (moving bar), and the current view can be exported as MP4, by default into <processed store>/cellviz/. Nothing is written to the database and no table is added.

uv pip install -e ".[cellviz]"   # ipywidgets, imageio-ffmpeg (MP4 export), ipympl (clickable figure)

In a project created by make_project.py, add the cellviz block (it adds no tables, only the last numbered notebook, and the cellviz extra to the project's dependencies). Once RoiMask and Traces (and optionally PreprocessTraces, the QI tables and HighRes) are populated, run that notebook. Elsewhere, connect and activate your schema as in your other notebooks, then:

from djimaging.cellviz import CellViz

fields = CellViz.list_fields(schema)  # fields with a ROI mask and traces
cv = CellViz.from_schema(schema, field_key=dict(date='2026-09-16', field='GCL0'))  # a (partial) key of one field
cv

Instead of schema, you can also pass your schema module or globals(). The field key may be partial as long as it matches exactly one field; field_key=None takes the first field.

The notebook of the cellviz block (djimaging/notebooks/blocks/cellviz.py) walks through choosing a field, the overlays and the MP4 export.

CellViz also works without database, on the ScanM files and saved ROI masks of one experiment folder: CellViz(LocalFieldSource(experiment_dir, field='GCL0', roi_mask_dir='AutoROIs')) (both from djimaging.cellviz). Traces, triggers and QIs are then recomputed with djimaging's functions and default parameters and labelled as such.

Clean up

When you no longer need your test schema, make sure you drop it by calling schema.drop() (e.g. in the drop_schema notebook of your project) and confirm by entering yes.

⚠️ Make sure you only drop your own schema! schema.drop() will show you the name of the schema. If you are not sure about the schema's origin, don't drop it!

Local testing

Install the test environment with DataJoint 2.3.x:

uv pip install pytest "setuptools<81" -e .

Run unit tests:

pytest -q tests --ignore=tests/integration

Run integration tests against a temporary MySQL 8.0.43 server:

docker run --rm --name djimaging-mysql-test \
  -e MYSQL_ROOT_PASSWORD=datajoint -e MYSQL_ROOT_HOST=% \
  -p 3307:3306 -d mysql:8.0.43 \
  --character-set-server=utf8mb4 --collation-server=utf8mb4_bin
until docker exec djimaging-mysql-test mysqladmin ping -h 127.0.0.1 -pdatajoint; do sleep 1; done

DJ_TEST_MYSQL=1 DJ_HOST=127.0.0.1 DJ_PORT=3307 \
DJ_USER=root DJ_PASS=datajoint pytest -q tests/integration

docker stop djimaging-mysql-test

Skipped tests

Some tests skip themselves when their prerequisites are missing. To run them:

  • Real test data (test_autoshift.py, test_orientation.py, test_scanm_utils.py): set DJIMAGING_TEST_DATA to the folder with the data (default <repo>/test_data; tests with a missing file are skipped), e.g. DJIMAGING_TEST_DATA=/path/to/DataJointTestData pytest -q tests --ignore=tests/integration.
  • MySQL integration tests (tests/integration): set DJ_TEST_MYSQL=1 and point to a MySQL server, either with DJ_TEST_CONFIG=<dj config file> or DJ_HOST/DJ_PORT/DJ_USER/DJ_PASS (defaults 127.0.0.1:3306, root/datajoint). On a shared server set a unique DJ_TEST_SCHEMA=<prefix>: the run aborts if one of the schemas <prefix>_tutorial/_motion/_motion_refine/_stimulator/_history exists, and only schemas created by the run are dropped. Your account needs CREATE/DROP rights for that prefix.
  • Optional packages: torch, pytorch_lightning and torchvision (autorois models, autorois extra), ipycanvas, cv2, rfest (rf extra), jupytext, pynwb. Install the missing ones to run the matching tests.

About

2P imaging data joint tables and schemas

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages