Example notebook neuralib.imaging
Example of suite2p registration
Example of calculating dF/F
Example of converting dF/F to spike (using either
OASISdeconvolution orCascademodel evaluation)
[ ]:
import logging
import absl
import matplotlib.pyplot as plt
import numpy as np
from neuralib.spikes import oasis_dcnv, cascade_predict
from neuralib.suite2p import *
from neuralib.io.dataset import load_example_suite2p_result
[ ]:
%load_ext autoreload
%autoreload
Load Suite2p results
[ ]:
# s2p = Suite2PResult.load(..., cell_prob=0.0, channel=0) # replace ... to suite2p base directory (*/suite2p/plane*)
s2p = load_example_suite2p_result(quiet=False, cached=True, rename_folder='s2p')
See the Suite2p registered ROIs
[6]:
# see ROIs
_, ax = plt.subplots(1, 2)
pix = get_soma_pixel(s2p, color_diff=True)
pix[pix == 0] = np.nan
ax[0].imshow(s2p.image_mean, cmap='grey')
ax[1].imshow(pix, cmap='nipy_spectral')
plt.show()
Calculate dF/F
[7]:
dff = get_neuron_signal(s2p, n=np.arange(10), signal_type='df_f', normalize=True, dff=True, correct_neuropil=True)[0]
# pick the first 100 sec
points = int(100 * s2p.fs)
dff = dff[:, :points]
n_neurons = dff.shape[0]
y = 0
for i in range(n_neurons):
plt.plot(dff[i] + y)
y += 1
plt.xlabel('n_points')
plt.ylabel('# neuron ID')
plt.show()
Calculate deconvolution spikes using OASIS method
[8]:
spks = oasis_dcnv(dff, tau=s2p.indicator_tau, fs=s2p.fs)
spks = spks / np.max(spks, axis=1, keepdims=True)
n_neurons = spks.shape[0]
y = 0
for i in range(n_neurons):
plt.plot(spks[i] + y)
y += 1
plt.xlabel('n_points')
plt.ylabel('# neuron ID')
plt.show()
Predict spikes from dF/F using pre-trained model from Cascade
[9]:
# verbose disable
absl.logging.set_verbosity('error')
logging.getLogger('tensorflow').setLevel(logging.ERROR)
spks = cascade_predict(dff, model_type='Global_EXC_30Hz_smoothing100ms', verbose=False)
spks = spks / np.max(spks, axis=1, keepdims=True)
n_neurons = spks.shape[0]
y = 0
for i in range(n_neurons):
plt.plot(spks[i] + y)
y += 1
plt.xlabel('n_points')
plt.ylabel('# neuron ID')
plt.show()
[INFO][25-04-10 17:05:15] - The selected model was trained on 18 datasets, with 5 ensembles for each noise level, at a sampling rate of 30 Hz, with a resampled ground truth that was smoothed with a Gaussian kernelof a standard deviation of 200 ms.
[INFO][25-04-10 17:05:15] - Loaded model was trained at frame rate 30 Hz
[INFO][25-04-10 17:05:15] - Given argument traces contains 10 neurons and 2998 frames.
[INFO][25-04-10 17:05:15] - Noise levels (mean, std; in standard units): 0.46, 0.14
Predictions for noise level 1
... ensemble 0
4/4 ━━━━━━━━━━━━━━━━━━━━ 0s 28ms/step
/opt/homebrew/Caskroom/miniconda/base/envs/py310/lib/python3.10/site-packages/keras/src/optimizers/base_optimizer.py:33: UserWarning: Argument `decay` is no longer supported and will be ignored.
warnings.warn(
4/4 ━━━━━━━━━━━━━━━━━━━━ 0s 28ms/step
... ensemble 1
4/4 ━━━━━━━━━━━━━━━━━━━━ 0s 31ms/step
... ensemble 2
4/4 ━━━━━━━━━━━━━━━━━━━━ 0s 29ms/step
... ensemble 3
4/4 ━━━━━━━━━━━━━━━━━━━━ 0s 26ms/step
... ensemble 4
4/4 ━━━━━━━━━━━━━━━━━━━━ 0s 29ms/step
Predictions for noise level 2
No neurons for this noise level: 2
Predictions for noise level 3
No neurons for this noise level: 3
Predictions for noise level 4
No neurons for this noise level: 4
Predictions for noise level 5
No neurons for this noise level: 5
Predictions for noise level 6
No neurons for this noise level: 6
Predictions for noise level 7
No neurons for this noise level: 7
Predictions for noise level 8
No neurons for this noise level: 8