Skip to content

Commit a7f0118

Browse files
adding twochan notebook and updating bug with badframes is None
1 parent 0c48d91 commit a7f0118

2 files changed

Lines changed: 176 additions & 1 deletion

File tree

notebooks/run_twochan.ipynb

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import numpy as np\n",
10+
"import sys\n",
11+
"import os\n",
12+
"from pathlib import Path\n",
13+
"import suite2p\n",
14+
"from suite2p import default_settings\n",
15+
"from suite2p.run_s2p import logger_setup\n",
16+
"\n",
17+
"db = {\"data_path\": [\"/media/carsen/disk2/test_suite2p/GT1/\"], \n",
18+
" \"nplanes\": 1, # each tiff has these many planes in sequence\n",
19+
" \"nchannels\": 2, # each tiff has these many channels per plane\n",
20+
" }\n",
21+
"db[\"save_path0\"] = db[\"data_path\"][0]\n",
22+
"\n",
23+
"# initialize logger\n",
24+
"logger_setup(db[\"save_path0\"])\n",
25+
"\n",
26+
"# initialize settings\n",
27+
"settings = default_settings()\n",
28+
"settings[\"registration\"][\"align_by_chan2\"] = True # optional - use anatomical chan for reg\n",
29+
"settings[\"run\"][\"do_registration\"] = 1\n",
30+
"settings[\"fs\"] = 10\n",
31+
"settings[\"tau\"] = 1.0 # timescale of gcamp to use for deconvolution\n",
32+
"settings[\"detection\"][\"threshold_scaling\"] = 1.0\n",
33+
"settings[\"detection\"][\"algorithm\"] = \"sparsery\"\n",
34+
"settings[\"torch_device\"] = \"cuda\" # use mps for mac, cuda for nvidia gpu, or \"cpu\"\n"
35+
]
36+
},
37+
{
38+
"cell_type": "markdown",
39+
"metadata": {},
40+
"source": [
41+
"run suite2p with detection on functional channel"
42+
]
43+
},
44+
{
45+
"cell_type": "code",
46+
"execution_count": null,
47+
"metadata": {},
48+
"outputs": [],
49+
"source": [
50+
"\n",
51+
"db_paths = suite2p.run_s2p(settings=settings, db=db)\n"
52+
]
53+
},
54+
{
55+
"cell_type": "markdown",
56+
"metadata": {},
57+
"source": [
58+
"visualize green and red channel"
59+
]
60+
},
61+
{
62+
"cell_type": "code",
63+
"execution_count": null,
64+
"metadata": {},
65+
"outputs": [],
66+
"source": [
67+
"reg_outputs = np.load(Path(db[\"save_path0\"]) / \"suite2p/plane0/reg_outputs.npy\", allow_pickle=True).item()\n",
68+
"import matplotlib.pyplot as plt \n",
69+
"from cellpose.transforms import normalize99\n",
70+
"redchan = np.clip(normalize99(reg_outputs[\"meanImg_chan2\"]), 0, 1)\n",
71+
"greenchan = np.clip(normalize99(reg_outputs[\"meanImg\"]), 0, 1)\n",
72+
"rgb = np.stack((redchan, greenchan, redchan), axis=-1)\n",
73+
"\n",
74+
"plt.figure(figsize=(14,8))\n",
75+
"plt.subplot(1,3,1)\n",
76+
"plt.imshow(greenchan)\n",
77+
"plt.title(\"green (func)\")\n",
78+
"plt.axis(\"off\")\n",
79+
"plt.subplot(1,3,2)\n",
80+
"plt.imshow(redchan)\n",
81+
"plt.title(\"red (anat)\")\n",
82+
"plt.axis(\"off\")\n",
83+
"plt.subplot(1,3,3)\n",
84+
"plt.imshow(rgb)\n",
85+
"plt.title(\"overlay\")\n",
86+
"plt.axis(\"off\")"
87+
]
88+
},
89+
{
90+
"cell_type": "markdown",
91+
"metadata": {},
92+
"source": [
93+
"## run ROI detection in second channel \n",
94+
"\n",
95+
"only use this if you have **two** functional indicators, e.g. gcamp + rgeco"
96+
]
97+
},
98+
{
99+
"cell_type": "code",
100+
"execution_count": null,
101+
"metadata": {
102+
"scrolled": true
103+
},
104+
"outputs": [],
105+
"source": [
106+
"from suite2p.pipeline_s2p import pipeline\n",
107+
"from suite2p.io import BinaryFile\n",
108+
"import torch\n",
109+
"import shutil \n",
110+
"\n",
111+
"db0 = np.load(Path(db[\"save_path0\"]) / \"suite2p/db.npy\", allow_pickle=True).item()\n",
112+
"nfolds = db0[\"nplanes\"] * db0.get(\"nrois\", 1)\n",
113+
"\n",
114+
"for i in range(nfolds):\n",
115+
" db_green = np.load(Path(db0[\"save_path0\"]) / f\"suite2p/plane{i}/db.npy\", allow_pickle=True).item()\n",
116+
" Ly, Lx, n_frames = db_green[\"Ly\"], db_green[\"Lx\"], db_green[\"nframes\"]\n",
117+
"\n",
118+
" # swap reg files\n",
119+
" reg_file = db_green[\"reg_file_chan2\"]\n",
120+
" reg_file_chan2 = db_green[\"reg_file\"]\n",
121+
"\n",
122+
" # new save path\n",
123+
" save_path = Path(db[\"save_path0\"]) / \"suite2p_red\" \n",
124+
" save_path.mkdir(exist_ok=True)\n",
125+
" (save_path / f\"plane{i}\").mkdir(exist_ok=True)\n",
126+
" \n",
127+
" # update db \n",
128+
" db_red = db_green \n",
129+
" db_red[\"reg_file\"] = reg_file\n",
130+
" db_red[\"reg_file_chan2\"] = reg_file_chan2\n",
131+
" np.save(save_path / f\"plane{i}/db.npy\", db_red)\n",
132+
" np.save(save_path / f\"plane{i}/settings.npy\", settings)\n",
133+
"\n",
134+
" # swap meanImg/meanImg_chan2 in reg_outputs for gui vis \n",
135+
" reg_outputs = np.load(Path(db[\"save_path0\"]) / f\"suite2p/plane{i}/reg_outputs.npy\", allow_pickle=True).item()\n",
136+
" reg_outputs_red = reg_outputs.copy()\n",
137+
" reg_outputs_red[\"meanImg\"] = reg_outputs[\"meanImg_chan2\"]\n",
138+
" reg_outputs_red[\"meanImg_chan2\"] = reg_outputs[\"meanImg\"]\n",
139+
" np.save(save_path / f\"plane{i}/reg_outputs.npy\", reg_outputs_red)\n",
140+
" \n",
141+
" with BinaryFile(Ly=Ly, Lx=Lx, filename=reg_file, n_frames=n_frames) as f_reg, \\\n",
142+
" BinaryFile(Ly=Ly, Lx=Lx, filename=reg_file_chan2, n_frames=n_frames) as f_reg_chan2:\n",
143+
" out = pipeline(str(save_path / f\"plane{i}\"), f_reg=f_reg, f_reg_chan2=f_reg_chan2, \n",
144+
" run_registration=False, settings=settings,\n",
145+
" device=torch.device(settings[\"torch_device\"]))\n",
146+
"\n",
147+
" # outputs from detection on chan2 reg file \n",
148+
" (reg_outputs, detect_outputs, stat, F, Fneu, F_chan2, Fneu_chan2, \n",
149+
" spks, iscell, redcell, zcorr, plane_times) = out"
150+
]
151+
}
152+
],
153+
"metadata": {
154+
"kernelspec": {
155+
"display_name": "s2p",
156+
"language": "python",
157+
"name": "python3"
158+
},
159+
"language_info": {
160+
"codemirror_mode": {
161+
"name": "ipython",
162+
"version": 3
163+
},
164+
"file_extension": ".py",
165+
"mimetype": "text/x-python",
166+
"name": "python",
167+
"nbconvert_exporter": "python",
168+
"pygments_lexer": "ipython3",
169+
"version": "3.11.13"
170+
}
171+
},
172+
"nbformat": 4,
173+
"nbformat_minor": 2
174+
}

suite2p/pipeline_s2p.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ def pipeline(save_path, f_reg, f_raw=None, f_reg_chan2=None, f_raw_chan2=None,
144144
t11 = time.time()
145145
if stat is None:
146146
bad_frames = reg_outputs["badframes"]
147-
bad_frames[badframes] = True
147+
if badframes is not None:
148+
bad_frames[badframes] = True
148149
logger.info(f"Excluding {bad_frames.sum()} bad frames from detection")
149150
if not isinstance(settings["diameter"], (list, tuple, np.ndarray)):
150151
settings["diameter"] = np.array([settings["diameter"], settings["diameter"]])

0 commit comments

Comments
 (0)