Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
192 changes: 104 additions & 88 deletions paper/detect_benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def make_neuropil(root, n_planes=4, iplane=1, device=torch.device('cuda')):


def hybrid_gt(root, n_planes=4, iplane=1, ds=2, n_ell=2000, neu_coeff=5,
poisson_coeff=50, test=False, device=torch.device('cuda')):
poisson_coeff=50, test=False, device=torch.device('cuda'), save_output=False):

if n_ell > 0:
stat_ell, F_ell = make_dendrites(root, n_planes=n_planes, iplane=iplane, ds=ds, n_ell=n_ell)
Expand Down Expand Up @@ -455,9 +455,9 @@ def create_gt(root=Path('/media/carsen/ssd2/suite2p_paper/VG1/3/')):
from suite2p import detection, extraction
from suite2p.run_s2p import logger_setup

def hybrid_gt_run(root, n_planes=4, iplane=1, ds=2,
n_ell=2000, neu_coeff=0.4, poisson_coeff=20,
threshold_scaling=0.7):
def hybrid_gt_run(root, n_planes=4, iplane=1, ds=2, alg='sparsery',
n_ell=2000, neu_coeff=0.4, poisson_coeff=20, nt=0,
threshold_scaling=0.7, save_output=False):

torch.manual_seed(0)
torch.cuda.manual_seed_all(0)
Expand Down Expand Up @@ -489,12 +489,17 @@ def hybrid_gt_run(root, n_planes=4, iplane=1, ds=2,
settings['detection']['sparsery_settings']['max_ROIs'] = 10000
settings['detection']['sparsery_settings']['highpass_neuropil'] = 25
settings['tau'] = 0.25
settings['diameter'] = np.array([8, 8])
settings['detection']['algorithm'] = alg

# data_bin = root / 'sims' / f'data_neu_{neu_coeff:.2f}_ell_{n_ell}_poisson_{poisson_coeff}.bin'


with BinaryFile(Ly=Ly, Lx=Lx,
filename=reg_file) as f_reg:
if nt > 0:
f_reg = f_reg[:nt]
print(f_reg.shape)
detect_outputs, stat, _ = detection.detection_wrapper(f_reg, diameter=settings['diameter'],
tau=settings['tau'], fs=settings['fs'],
settings=settings['detection'],
Expand All @@ -506,18 +511,25 @@ def hybrid_gt_run(root, n_planes=4, iplane=1, ds=2,
dF = F.copy() - 0.7 * Fneu

th = threshold_scaling
np.save(root / 'sims' / f'stat_s2p_{th:.1f}_neu_{neu_coeff:.2f}_ell_{n_ell}_poisson_{poisson_coeff}_plane{iplane}.npy', stat)
np.save(root / 'sims' / f'F_s2p_{th:.1f}_neu_{neu_coeff:.2f}_ell_{n_ell}_poisson_{poisson_coeff}_plane{iplane}.npy', F)
np.save(root / 'sims' / f'Fneu_s2p_{th:.1f}_neu_{neu_coeff:.2f}_ell_{n_ell}_poisson_{poisson_coeff}_plane{iplane}.npy', Fneu)

algstr = '' if alg == 'sparsery' else 'sourcery_'
if save_output:
np.save(root / 'sims' / f'stat_s2p_{algstr}{th:.1f}_neu_{neu_coeff:.2f}_ell_{n_ell}_poisson_{poisson_coeff}_plane{iplane}.npy', stat)
np.save(root / 'sims' / f'F_s2p_{algstr}{th:.1f}_neu_{neu_coeff:.2f}_ell_{n_ell}_poisson_{poisson_coeff}_plane{iplane}.npy', F)
np.save(root / 'sims' / f'Fneu_s2p_{algstr}{th:.1f}_neu_{neu_coeff:.2f}_ell_{n_ell}_poisson_{poisson_coeff}_plane{iplane}.npy', Fneu)

stat_gt = np.load(root / 'benchmarks' / f'stat_gt_plane{iplane}.npy', allow_pickle=True)
F_gt = np.load(root / 'benchmarks' / f'F_gt_plane{iplane}.npy', allow_pickle=True)
Fneu_gt = np.load(root / 'benchmarks' / f'Fneu_gt_plane{iplane}.npy', allow_pickle=True)
dF_gt = F_gt.copy() - 0.7 * Fneu_gt

if nt > 0:
dF_gt = dF_gt[:, :nt]

### SNR
tp, fp, fn, f1 = detect_f1_score(dF, dF_gt, stat, stat_gt, Ly=Ly, Lx=Lx, snr_threshold=0.25)

np.save(root / 'sims' / f'results_s2p_{th:.1f}_neu_{neu_coeff:.2f}_ell_{n_ell}_poisson_{poisson_coeff}_plane{iplane}.npy',
ntstr = '' if nt==0 else f'_nt_{nt}'
np.save(root / 'sims' / f'results_s2p_{algstr}{th:.1f}_neu_{neu_coeff:.2f}_ell_{n_ell}_poisson_{poisson_coeff}_plane{iplane}{ntstr}.npy',
np.array([tp, fp, fn, f1]))


Expand All @@ -528,12 +540,19 @@ def hybrid_gt_run(root, n_planes=4, iplane=1, ds=2,
# argparse
arg_parser = argparse.ArgumentParser(description='Run hybrid ground-truth generation and Suite2p detection/extraction.')
arg_parser.add_argument('--root', type=str, default='')
arg_parser.add_argument('--alg', type=str, default='sparsery')
arg_parser.add_argument('--save_output', action='store_true',
help='Save outputs of run: stat, F and Fneu.')
arg_parser.add_argument('--sweep', action='store_true',
help='Run a sweep of hybrid ground-truth generation with different parameters.')
arg_parser.add_argument('--all_planes', action='store_true',
help='Run all planes.')
arg_parser.add_argument('--param_sweep', action='store_true',
help='Run a parameter sweep over threshold scaling for the middle sim.')
arg_parser.add_argument('--threshold_scaling', type=float, default=0.7,
help='threshold_scaling param for ROI detection in suite2p.')
arg_parser.add_argument('--nt', type=int, default=0,
help='Number of timepoints to use for detection.')
arg_parser.add_argument('--n_ell', type=int, default=2000,
help='Number of dendritic ellipses to generate.')
arg_parser.add_argument('--neu_coeff', type=float, default=0.4,
Expand All @@ -549,89 +568,86 @@ def hybrid_gt_run(root, n_planes=4, iplane=1, ds=2,
if len(args.root) > 0 and not args.sweep and not args.param_sweep:
root = Path(args.root)
(root / 'sims').mkdir(parents=True, exist_ok=True)
hybrid_gt_run(root, n_planes=4, iplane=args.iplane, ds=2,
hybrid_gt_run(root, n_planes=4, iplane=args.iplane, ds=2, alg=args.alg, nt=args.nt,
n_ell=args.n_ell, neu_coeff=args.neu_coeff, poisson_coeff=args.poisson_coeff,
threshold_scaling=args.threshold_scaling)
elif args.param_sweep:
root = Path(args.root)
(root / 'sims').mkdir(parents=True, exist_ok=True)
(root / 'logs').mkdir(parents=True, exist_ok=True)
neu_coeff = 0.4
poisson_coeff = 20
n_ell = 2000
for th in np.arange(0.6, 1.5, 0.1):
bsub = f'bsub -n 8 -gpu "num=1" -q gpu_a100 ' \
f'-J {root}/logs/hybrid_gt_th_{th:.1f} ' \
f'-o {root}/logs/hybrid_gt_th_{th:.1f}.out ' \
f'"~/miniforge3/envs/s2p/bin/python {__file__} --root {root} --n_ell {n_ell} ' \
f'--neu_coeff {neu_coeff:.2f} --poisson_coeff {poisson_coeff} ' \
f'--iplane {args.iplane} --threshold_scaling {th:.1f} ' \
f' > {root}/logs/hybrid_gt_th_{th:.1f}.log"'
print(bsub)
os.system(bsub)

elif args.sweep:
root = Path(args.root)
(root / 'sims').mkdir(parents=True, exist_ok=True)
(root / 'logs').mkdir(parents=True, exist_ok=True)
root = args.root
th = args.threshold_scaling
iplane = args.iplane

# n_ell = 0
# neu_coeff = 0
# poisson_coeff = 0
# bsub = f'bsub -n 8 -gpu "num=1" -q gpu_l4 ' \
# f'-J {root}/logs/hybrid_gt_{n_ell}_{neu_coeff:.2f}_{poisson_coeff} ' \
# f'-o {root}/logs/hybrid_gt_{n_ell}_{neu_coeff:.2f}_{poisson_coeff}.out ' \
# f'"~/miniforge3/envs/s2p/bin/python {__file__} --root {root} --n_ell {n_ell} ' \
# f'--neu_coeff {neu_coeff:.2f} --poisson_coeff {poisson_coeff} --iplane {iplane} ' \
# f' > {root}/logs/hybrid_gt_{n_ell}_{neu_coeff:.2f}_{poisson_coeff}.log"'
# print(bsub)
# os.system(bsub)


for n_ell in np.arange(0, 4001, 500):
threshold_scaling=args.threshold_scaling, save_output=args.save_output)
else:
if args.param_sweep:
root = Path(args.root)
(root / 'sims').mkdir(parents=True, exist_ok=True)
(root / 'logs').mkdir(parents=True, exist_ok=True)
neu_coeff = 0.4
poisson_coeff = 20
bsub = f'bsub -n 8 -gpu "num=1" -q gpu_a100 ' \
f'-J {root}/logs/hybrid_gt_{n_ell}_{neu_coeff:.2f}_{poisson_coeff} ' \
f'-o {root}/logs/hybrid_gt_{n_ell}_{neu_coeff:.2f}_{poisson_coeff}.out ' \
f'"~/miniforge3/envs/s2p/bin/python {__file__} --root {root} --n_ell {n_ell} ' \
f'--neu_coeff {neu_coeff:.2f} --poisson_coeff {poisson_coeff} ' \
f'--iplane {args.iplane} --threshold_scaling {th:.1f} ' \
f' > {root}/logs/hybrid_gt_{n_ell}_{neu_coeff:.2f}_{poisson_coeff}.log"'
print(bsub)
os.system(bsub)

for neu_coeff in np.arange(0, 0.81, 0.1):
n_ell = 2000
poisson_coeff = 20
if neu_coeff == 0.4:
continue # in loop above
bsub = f'bsub -n 8 -gpu "num=1" -q gpu_a100 ' \
f'-J {root}/logs/hybrid_gt_{n_ell}_{neu_coeff:.2f}_{poisson_coeff} ' \
f'-o {root}/logs/hybrid_gt_{n_ell}_{neu_coeff:.2f}_{poisson_coeff}.out ' \
f'"~/miniforge3/envs/s2p/bin/python {__file__} --root {root} --n_ell {n_ell} ' \
f'--neu_coeff {neu_coeff:.2f} --poisson_coeff {poisson_coeff} --iplane {iplane} ' \
f' > {root}/logs/hybrid_gt_{n_ell}_{neu_coeff:.2f}_{poisson_coeff}.log"'
print(bsub)
os.system(bsub)


for poisson_coeff in [0, 5, 10, 20, 50, 100, 200]:
n_ell = 2000
neu_coeff = 0.4
if poisson_coeff == 20:
continue # in loop above
bsub = f'bsub -n 8 -gpu "num=1" -q gpu_a100 ' \
f'-J {root}/logs/hybrid_gt_{n_ell}_{neu_coeff:.2f}_{poisson_coeff} ' \
f'-o {root}/logs/hybrid_gt_{n_ell}_{neu_coeff:.2f}_{poisson_coeff}.out ' \
f'"~/miniforge3/envs/s2p/bin/python {__file__} --root {root} --n_ell {n_ell} ' \
f'--neu_coeff {neu_coeff:.2f} --poisson_coeff {poisson_coeff} --iplane {iplane} ' \
f' > {root}/logs/hybrid_gt_{n_ell}_{neu_coeff:.2f}_{poisson_coeff}.log"'
print(bsub)
os.system(bsub)
alg = args.alg
iplanes = [args.iplane] if not args.all_planes else np.arange(4)
for iplane in iplanes:
for th in np.arange(0.6, 1.5, 0.1):
bsub = f'bsub -n 8 -gpu "num=1" -q gpu_a100 ' \
f'-J {root}/logs/hybrid_gt_th_{th:.1f} ' \
f'-o {root}/logs/hybrid_gt_th_{th:.1f}.out ' \
f'"~/miniforge3/envs/s2p/bin/python {__file__} --root {root} --n_ell {n_ell} ' \
f'--neu_coeff {neu_coeff:.2f} --poisson_coeff {poisson_coeff} ' \
f'--iplane {iplane} --threshold_scaling {th:.1f} --alg {alg} ' \
f' > {root}/logs/hybrid_gt_th_{th:.1f}.log"'
print(bsub)
os.system(bsub)

elif args.sweep:
root = Path(args.root)
(root / 'sims').mkdir(parents=True, exist_ok=True)
(root / 'logs').mkdir(parents=True, exist_ok=True)
root = args.root
th = args.threshold_scaling
alg = args.alg
nt = args.nt
iplanes = [args.iplane] if not args.all_planes else np.arange(4)
for iplane in iplanes:

for n_ell in np.arange(0, 4001, 500):
neu_coeff = 0.4
poisson_coeff = 20
save_str = '--save_output' if n_ell == 2000 else ''
bsub = f'bsub -n 8 -gpu "num=1" -q gpu_a100 ' \
f'-J {root}/logs/hybrid_gt_{n_ell}_{neu_coeff:.2f}_{poisson_coeff} ' \
f'-o {root}/logs/hybrid_gt_{n_ell}_{neu_coeff:.2f}_{poisson_coeff}_{alg}.out ' \
f'"~/miniforge3/envs/s2p/bin/python {__file__} --root {root} --n_ell {n_ell} ' \
f'--neu_coeff {neu_coeff:.2f} --poisson_coeff {poisson_coeff} --alg {alg} --nt {nt} ' \
f'--iplane {iplane} --threshold_scaling {th:.1f} {save_str}' \
f' > {root}/logs/hybrid_gt_{n_ell}_{neu_coeff:.2f}_{poisson_coeff}_{alg}.log"'
print(bsub)
os.system(bsub)

for neu_coeff in np.arange(0, 0.91, 0.1):
n_ell = 2000
poisson_coeff = 20
if neu_coeff == 0.4:
continue # in loop above
bsub = f'bsub -n 8 -gpu "num=1" -q gpu_a100 ' \
f'-J {root}/logs/hybrid_gt_{n_ell}_{neu_coeff:.2f}_{poisson_coeff} ' \
f'-o {root}/logs/hybrid_gt_{n_ell}_{neu_coeff:.2f}_{poisson_coeff}_{alg}.out ' \
f'"~/miniforge3/envs/s2p/bin/python {__file__} --root {root} --n_ell {n_ell} ' \
f'--neu_coeff {neu_coeff:.2f} --threshold_scaling {th:.1f} ' \
f'--poisson_coeff {poisson_coeff} --alg {alg} --nt {nt} --iplane {iplane} ' \
f' > {root}/logs/hybrid_gt_{n_ell}_{neu_coeff:.2f}_{poisson_coeff}_{alg}.log"'
print(bsub)
os.system(bsub)


for poisson_coeff in [0, 5, 10, 20, 50, 100, 200, 400]:
n_ell = 2000
neu_coeff = 0.4
if poisson_coeff == 20:
continue # in loop above
bsub = f'bsub -n 8 -gpu "num=1" -q gpu_a100 ' \
f'-J {root}/logs/hybrid_gt_{n_ell}_{neu_coeff:.2f}_{poisson_coeff} ' \
f'-o {root}/logs/hybrid_gt_{n_ell}_{neu_coeff:.2f}_{poisson_coeff}_{alg}.out ' \
f'"~/miniforge3/envs/s2p/bin/python {__file__} --root {root} --n_ell {n_ell} ' \
f'--neu_coeff {neu_coeff:.2f} --threshold_scaling {th:.1f} ' \
f'--poisson_coeff {poisson_coeff} --alg {alg} --nt {nt} --iplane {iplane} ' \
f' > {root}/logs/hybrid_gt_{n_ell}_{neu_coeff:.2f}_{poisson_coeff}_{alg}.log"'
print(bsub)
os.system(bsub)



Expand Down
39 changes: 15 additions & 24 deletions paper/detect_caiman.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
def hybrid_detect(root, iplane=1, neu_coeff=0.4, n_ell=2000, poisson_coeff=20,
num_processors_to_use=45, K=10,
p=1, gnb=2, gSig0=5, rf=15,
tiff_file=None, filename=None, delete=True):
tiff_file=None, filename=None, delete=True, save_output=False):

if isinstance(K, int):
K = [K]
Expand Down Expand Up @@ -205,12 +205,13 @@ def hybrid_detect(root, iplane=1, neu_coeff=0.4, n_ell=2000, poisson_coeff=20,

stat_caiman = np.array(stat_caiman)

np.save(root / 'sims' / f"F_{run_name0}.npy", cnmf_refit.estimates.F_dff)
np.save(root / 'sims' / f"stat_{run_name0}.npy", stat_caiman)
np.save(root / 'sims' / f"C_{run_name0}.npy", cnmf_refit.estimates.C)
np.save(root / 'sims' / f"YrA_{run_name0}.npy", cnmf_refit.estimates.YrA)
np.save(root / 'sims' / f"r_{run_name0}.npy", cnmf_refit.estimates.r_values)
np.save(root / 'sims' / f"S_{run_name0}.npy", cnmf_refit.estimates.S)
if save_output:
np.save(root / 'sims' / f"F_{run_name0}.npy", cnmf_refit.estimates.F_dff)
np.save(root / 'sims' / f"stat_{run_name0}.npy", stat_caiman)
np.save(root / 'sims' / f"C_{run_name0}.npy", cnmf_refit.estimates.C)
np.save(root / 'sims' / f"YrA_{run_name0}.npy", cnmf_refit.estimates.YrA)
np.save(root / 'sims' / f"r_{run_name0}.npy", cnmf_refit.estimates.r_values)
np.save(root / 'sims' / f"S_{run_name0}.npy", cnmf_refit.estimates.S)

dF = cnmf_refit.estimates.F_dff.copy()

Expand Down Expand Up @@ -250,6 +251,8 @@ def hybrid_detect(root, iplane=1, neu_coeff=0.4, n_ell=2000, poisson_coeff=20,
# argparse
arg_parser = argparse.ArgumentParser(description='Run hybrid ground-truth generation and Suite2p detection/extraction.')
arg_parser.add_argument('--root', type=str, default='')
arg_parser.add_argument('--save_output', action='store_true',
help='Save outputs of run.')
arg_parser.add_argument('--param_sweep', action='store_true',
help='sweep number of components.')
arg_parser.add_argument('--param_sweep_grid', action='store_true',
Expand All @@ -262,7 +265,7 @@ def hybrid_detect(root, iplane=1, neu_coeff=0.4, n_ell=2000, poisson_coeff=20,
help='Number of dendritic ellipses to generate.')
arg_parser.add_argument('--Ksweep', action='store_true',
help='Run a sweep over K param in caiman.')
arg_parser.add_argument('--K', type=int, default=8,
arg_parser.add_argument('--K', type=int, default=9,
help='Number of components for caiman.')
arg_parser.add_argument('--p', type=int, default=1,
help='Autoregressive order for caiman.')
Expand Down Expand Up @@ -298,7 +301,8 @@ def hybrid_detect(root, iplane=1, neu_coeff=0.4, n_ell=2000, poisson_coeff=20,
hybrid_detect(root, iplane=args.iplane, K=K, p=args.p, gnb=args.gnb, gSig0=args.gSig0, rf=args.rf,
n_ell=args.n_ell, neu_coeff=args.neu_coeff, poisson_coeff=args.poisson_coeff,
num_processors_to_use=args.n_processors,
tiff_file=args.tiff_file, filename=args.filename, delete=(not args.no_delete))
tiff_file=args.tiff_file, filename=args.filename, delete=(not args.no_delete),
save_output=args.save_output)
elif args.param_sweep:
root = Path(args.root)
(root / 'sims').mkdir(parents=True, exist_ok=True)
Expand Down Expand Up @@ -360,28 +364,15 @@ def hybrid_detect(root, iplane=1, neu_coeff=0.4, n_ell=2000, poisson_coeff=20,

iplane = args.iplane

# # run original data
# n_ell = 0
# neu_coeff = 0
# poisson_coeff = 0
# bsub = f'bsub -n 16 ' \
# f'-J {root}/logs/caiman_{n_ell}_{neu_coeff:.2f}_{poisson_coeff} ' \
# f'-o {root}/logs/caiman_{n_ell}_{neu_coeff:.2f}_{poisson_coeff}.out ' \
# f'"source ~/add_mini.sh; source activate cm; ~/miniforge3/envs/cm/bin/python {__file__} --root {root} --n_ell {n_ell} ' \
# f'--neu_coeff {neu_coeff:.2f} --poisson_coeff {poisson_coeff} ' \
# f'--iplane {iplane} ' \
# f' > {root}/logs/caiman_{n_ell}_{neu_coeff:.2f}_{poisson_coeff}.log"'
# print(bsub)
# os.system(bsub)

for n_ell in np.arange(0, 4001, 500):
neu_coeff = 0.4
poisson_coeff = 20
save_str = '--save_output' if n_ell == 2000 else ''
bsub = f'bsub -n 16 ' \
f'-J {root}/logs/caiman_{n_ell}_{neu_coeff:.2f}_{poisson_coeff}_plane{iplane} ' \
f'-o {root}/logs/caiman_{n_ell}_{neu_coeff:.2f}_{poisson_coeff}_plane{iplane}.out ' \
f'"source ~/add_mini.sh; source activate cm; ~/miniforge3/envs/cm/bin/python {__file__} --root {root} --n_ell {n_ell} ' \
f'--neu_coeff {neu_coeff:.2f} --poisson_coeff {poisson_coeff} --K 9 ' \
f'--neu_coeff {neu_coeff:.2f} --poisson_coeff {poisson_coeff} --K 9 {save_str}' \
f'--iplane {iplane} ' \
f' > {root}/logs/caiman_{n_ell}_{neu_coeff:.2f}_{poisson_coeff}_plane{iplane}.log"'
print(bsub)
Expand Down
Loading
Loading