Fit your own recordings¶
Three things can be done from a file, either from the command line or from Python:
- fit one recording with the single-trace model, which gives the factor by which the injection changed the transient rate in that session;
- fit a cohort jointly with the hierarchical model, which gives the difference between conditions with between-animal variability in its interval;
- calibrate the kernel to your sensor, before either fit.
Each recording is a Doric .doric file, or a .csv, .txt, .tsv, .npy or .npz
file. A Doric file is corrected to dF/F with its isosbestic channel, and can take its
injection time from its events. Any other file holds either the raw sensor channel and
its isosbestic control in two columns, which are corrected the same way, or a trace
already corrected to dF/F; you give the sampling rate and the injection time in seconds.
The correction scales the control onto the sensor's baseline over the stretch before the injection (at most the first 10 minutes) and divides it out. It needs a recording longer than 10 minutes, and trims one second from each end.
Handling the animal at the injection moves the fibre and leaves artefacts at the moment
the rate is meant to change, so a handling window after each injection is left out of
the likelihood: from the injection to inj_end, and at least --mask-min-s seconds. The
paper used 120 s.
Try it on toy data first¶
These two scripts simulate recordings, write them to photosvi_toy/ as your own files
would be, and fit them with the functions below. Each runs in under a minute.
"""Fit one simulated recording with `fit_session`, the way you would fit your own.
Writes the toy recording to `photosvi_toy/session.csv` first, so the same file can then
be fitted from the command line. Short, and fewer steps than a real fit.
"""
import os
from pathlib import Path
import numpy as np
import yaml
from photosvi.api import FitSettings, config_path, fit_session, load_session
from photosvi.experiments.cohort import simulate_cohort, spec_from_config
out = Path(os.environ.get("PHOTOSVI_TOY", "photosvi_toy"))
out.mkdir(exist_ok=True)
# --- 1. a toy recording from the paper's simulator: 6 minutes at 48 Hz, the injection at
# 2 minutes doubling the transient rate (0.7 on the log scale). The transients are
# made larger and more frequent than in the recordings so that 6 minutes suffice.
cfg = yaml.safe_load(config_path("final_cohort_48.yaml").read_text())
cfg["cohort"].update(n_animals=1, T=48 * 360, inj_t=48 * 120, inj_jitter=0, mu_saline=0.7,
beta0_mean=-2.2, mu_a=-2.7, sigma_b0=0.0, sigma_b0_session=0.0,
sigma_animal=0.0, sigma_cell=0.0)
toy = simulate_cohort(spec_from_config(cfg, delta=0.0), seed=1)
np.savetxt(out / "session.csv", toy["y"][0].numpy(), fmt="%.6f")
# --- 2. read it as you would read your own trace: a dF/F file, its rate, the injection.
session = load_session(out / "session.csv", fs=48, inj_s=120, animal="toy", condition="drug")
# --- 3. fit. FitSettings() alone is the paper's model; the step counts are cut for the toy.
result = fit_session(session, FitSettings(steps=800, max_steps=1600))
rr = result.rate_ratio
print(f"rate ratio {rr['estimate']:.2f} [{rr['lo']:.2f}, {rr['hi']:.2f}] "
f"(simulated {np.exp(0.7):.2f})")
print(f"{len(result.events()['bins'])} transients above the detection threshold")
result.save(out / "run_session")
"""Fit a simulated cohort with `fit_cohort`, the way you would fit your own.
Writes one file per recording and the table of sessions to `photosvi_toy/` first, so the
same cohort can then be fitted from the command line. Two animals, each recorded under a
vehicle and under a drug; short recordings and few steps, so the numbers are not the paper's.
"""
import csv
import os
from pathlib import Path
import numpy as np
import yaml
from photosvi.api import CohortSettings, Study, config_path, fit_cohort, read_manifest
from photosvi.experiments.cohort import simulate_cohort, spec_from_config
out = Path(os.environ.get("PHOTOSVI_TOY", "photosvi_toy"))
out.mkdir(exist_ok=True)
# --- 1. a toy cohort from the paper's simulator: 2 animals x {vehicle, drug}, 6 minutes
# each at 48 Hz with the injection at 2 minutes, the drug raising the transient rate
# by 0.7 on the log scale over the vehicle. Larger, more frequent transients than in
# the recordings, so that a few short recordings suffice.
cfg = yaml.safe_load(config_path("final_cohort_48.yaml").read_text())
cfg["cohort"].update(n_animals=2, T=48 * 360, inj_t=48 * 120, inj_jitter=0,
beta0_mean=-2.2, mu_a=-2.7)
toy = simulate_cohort(spec_from_config(cfg, delta=0.7), seed=0)
names = ["vehicle", "drug"]
rows = []
for i in range(toy["y"].shape[0]):
animal, cond = f"m{int(toy['animal'][i]) + 1}", names[int(toy["drug"][i])]
path = f"{animal}_{cond}.csv"
np.savetxt(out / path, toy["y"][i].numpy(), fmt="%.6f")
rows.append(dict(path=path, animal=animal, condition=cond, fs=48, inj_start=120,
inj_end=120))
with (out / "sessions.csv").open("w", newline="") as fh:
w = csv.DictWriter(fh, fieldnames=list(rows[0]))
w.writeheader()
w.writerows(rows)
# --- 2. read the table: one row per recording, with its animal and its condition.
sessions, _ = read_manifest(out / "sessions.csv")
# --- 3. fit jointly. CohortSettings() alone is the paper's fit; steps cut for the toy.
result = fit_cohort(Study(sessions), CohortSettings(min_steps=200, max_steps=300,
probe_every=100, pre_s=None),
reference="vehicle")
# With two animals and a few hundred steps the interval is wide and the estimate is still
# shrunk toward zero; a real cohort is fitted for tens of thousands of steps.
c = result.contrast("drug")
print(f"drug vs vehicle: rate ratio {c['ratio']:.2f} [{c['ratio_lo']:.2f}, "
f"{c['ratio_hi']:.2f}] (simulated {np.exp(0.7):.2f})")
print("design:", {k: result.design[k] for k in ("n_animals", "n_sessions", "within_animal")})
result.save(out / "run_cohort")
The same files can then be fitted from the command line, with as few steps:
photosvi fit photosvi_toy/session.csv --fs 48 --inj-start 120 --steps 800 --out run/
photosvi fit-cohort photosvi_toy/sessions.csv --reference vehicle \
--min-steps 200 --max-steps 300 --out run_cohort/
Calibrating the kernel¶
The kernel is the shape of one transient. The shipped one is dLight1.3b in the nucleus accumbens; with another sensor or region, calibrate it from your recordings first. The procedure is described in Choosing your settings.
photosvi kernel calibrate rec1.doric rec2.doric rec3.doric --mask-min-s 120 \
--out kernel.yaml --plot kernel.pdf
photosvi kernel calibrate rec1.csv rec2.csv --fs 48 --sensor GCaMP6f --out kernel.yaml
photosvi kernel calibrate raw1.csv raw2.csv --fs 48 --column signal \
--isosbestic-column iso --inj-start 600 --mask-min-s 120 --out kernel.yaml
photosvi kernel calibrate --sessions sessions.csv --mask-min-s 120 --out kernel.yaml
The command prints every candidate kernel with its error on transients it was not fitted
to, writes the suggested one to kernel.yaml and draws all of them over the average
transient in kernel.pdf. --choose writes another candidate instead. --sessions
takes a table of sessions (see below), which gives each file its own injection times;
use it for Doric files that do not record the injection as an event, and for files with an
isosbestic column injected at different times.
Pass the file to either fit with --kernel kernel.yaml. From Python:
from photosvi.api import FitSettings, calibrate_kernel
cal = calibrate_kernel(sessions, sensor="GCaMP6f")
cal.plot("kernel.pdf")
settings = FitSettings(**cal.settings()) # the suggested kernel
One recording¶
# a Doric file, injection read from its events
photosvi fit session.doric --mask-min-s 120 --out run/
# a Doric file, injection times given
photosvi fit session.doric --inj-start 1502.6 --inj-end 1520.1 --out run/
# the raw sensor and isosbestic channels in a text file, sampled at 48 Hz
photosvi fit raw.csv --fs 48 --column signal --isosbestic-column iso \
--inj-start 600 --inj-end 640 --mask-min-s 120 --out run/
# a dF/F trace, with a calibrated kernel
photosvi fit trace.csv --fs 48 --inj-start 600 --inj-end 640 --mask-min-s 120 \
--kernel kernel.yaml --out run/
--column says which column holds the trace, by header name or index, if the file has
several; with --isosbestic-column it is the raw sensor channel. The command prints
the rate ratio with its 90% interval and writes four files to --out:
| file | contents |
|---|---|
result.json |
the rate ratio, the posterior of the rate coefficients and every setting used |
events.csv |
one row per detected transient: bin, time in seconds, posterior probability |
curves.npz |
the trace, the per-bin event probability and the fitted tonic carrier |
injection.json |
the injection and masked window used, and where the times came from |
photosvi fit --help lists the other options (device, threads, number of steps).
From Python:
from photosvi.api import FitSettings, Session, fit_session, load_doric_session, load_session
# a Doric file; extra keyword arguments are stored as the session's metadata
session, info = load_doric_session("session.doric", mask_min_s=120,
animal="m1", condition="drug")
# or the raw sensor and isosbestic channels of a text file, corrected the same way
session = load_session("raw.csv", fs=48, inj_s=600, column="signal",
isosbestic_column="iso", animal="m1", condition="drug")
# or two arrays already in memory
session = Session.from_channels(signal, iso, fs=48, inj_s=600, inj_end_s=640,
mask_min_s=120, animal="m1", condition="drug")
# or a trace already corrected to dF/F
session = load_session("trace.csv", fs=48, inj_s=600, animal="m1", condition="drug")
result = fit_session(session, FitSettings(), on_progress=print)
result.rate_ratio # {'estimate', 'lo', 'hi', 'level'}: e^beta_inj, 90% interval
result.events() # {'bins', 'times_s', 'prob'} of the detected transients
result.rate_per_min() # detections per minute before and after the injection
result.save("run/") # the same files as the command line
FitSettings() with no arguments is the paper's model; every field left at None takes
the shipped value. A forty-minute recording takes about ten minutes on four CPU threads.
result.rate_ratio is the estimate to report. It is a parameter of the model, so it
does not depend on the detection threshold. events() and rate_per_min() are readouts
of the per-bin posterior and do: result.events(threshold=0.7) re-thresholds without
refitting.
A cohort¶
A cohort is described by a table of sessions, one row per recording:
path,animal,condition,inj_start,inj_end
m1_vehicle.doric,m1,vehicle,,
m1_drug.doric,m1,drug,,
m2_vehicle.doric,m2,vehicle,1502.6,1520.1
m2_drug.csv,m2,drug,600,640
path is read relative to the table. animal and condition give the design: the model
reads from them which animals were recorded under which conditions, and so which
variance components it can estimate. inj_start and inj_end are in seconds; an empty
cell on a Doric file means the file's injection event. A trace file also needs fs, in
the table or with --fs, and column and isosbestic_column say which columns hold
the sensor channel and its control (leave isosbestic_column empty for a dF/F trace).
Other columns are kept as metadata.
photosvi fit-cohort sessions.csv --reference vehicle --mask-min-s 120 --out run/
The command prints, for each condition, the factor by which its transient rate after the
injection differs from that under --reference, with its 90% interval, and writes:
| file | contents |
|---|---|
result.json |
the contrasts, the tonic response of each condition, the variance components, the design and every setting used |
curves.npz |
the fitted traces, the per-bin event probability, each session's tonic curve and each condition's pooled one |
sessions.json |
the injection and masked window of each session, and where the times came from |
All sessions must have the same sampling rate. Each is cut to the same window: 10 minutes
before its injection (--pre-s) and as much after it as every session has. The window
should not reach back to an earlier injection. The paper's cohort, 13 forty-minute
recordings, takes about half an hour on a GPU.
From Python:
from photosvi.api import CohortSettings, Study, fit_cohort, read_manifest
sessions, _ = read_manifest("sessions.csv", mask_min_s=120)
# or build them one by one with load_doric_session / load_session, giving each
# animal= and condition= as metadata
result = fit_cohort(Study(sessions), CohortSettings(), reference="vehicle")
result.contrast("drug") # drug - vehicle: estimate, lo, hi and as a ratio
result.contrast("drug_b", "drug") # any two conditions
result.tonic_ratio("drug") # ratio of the tonic peak heights, 90% interval
result.tonic # each condition's pooled tonic curve parameters
result.variance_components # what a power calculation for the next cohort needs
result.design # within- or between-animal, levels estimated
result.save("run/")
CohortSettings() with no arguments is the paper's real-cohort fit. On the paper's 13
recordings it reproduces the reported fit exactly: a rate ratio of 1.57 [1.13, 2.17] for
RO64-6198 against vehicle, and a tonic height ratio of 0.41 [0.40, 0.43].
To fit each recording on its own instead, fit_study returns one single-trace result per
session; that is not the hierarchical model and its rate ratios are per session.