Skip to content

Commit

Permalink
Demo revisions: remove inline parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
pgunn committed Mar 1, 2024
1 parent e0d2734 commit ad31003
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 149 deletions.
38 changes: 2 additions & 36 deletions demos/general/demo_OnACID.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,41 +37,7 @@ def main():
"%(relativeCreated)12d [%(filename)s:%(funcName)20s():%(lineno)s][%(process)d] %(message)s",
level=logging.WARNING)

if cfg.configfile:
opts = cnmf.params.CNMFParams(params_from_file=cfg.configfile)
else:
# set up some parameters
fr = 10 # frame rate (Hz)
decay_time = .75 # approximate length of transient event in seconds
gSig = [6, 6] # expected half size of neurons
p = 1 # order of AR indicator dynamics
min_SNR = 1 # minimum SNR for accepting candidate components
thresh_CNN_noisy = 0.65 # CNN threshold for candidate components
gnb = 2 # number of background components
init_method = 'cnmf' # initialization method

# set up CNMF initialization parameters
init_batch = 400 # number of frames for initialization
patch_size = 32 # size of patch
stride = 3 # amount of overlap between patches
K = 4 # max number of components in each patch

params_dict = {'fr': fr,
'fnames': fnames,
'decay_time': decay_time,
'gSig': gSig,
'p': p,
'min_SNR': min_SNR,
'nb': gnb,
'init_batch': init_batch,
'init_method': init_method,
'rf': patch_size//2,
'stride': stride,
'sniper_mode': True,
'thresh_CNN_noisy': thresh_CNN_noisy,
'K': K}

opts = cnmf.params.CNMFParams(params_dict=params_dict)
opts = cnmf.params.CNMFParams(params_from_file=cfg.configfile)

if cfg.input is not None:
opts.change_params({"data": {"fnames": cfg.input}})
Expand Down Expand Up @@ -105,7 +71,7 @@ def main():

def handle_args():
parser = argparse.ArgumentParser(description="Demonstrate basic Caiman Online functionality with CNMF initialization")
parser.add_argument("--configfile", help="JSON Configfile for Caiman parameters")
parser.add_argument("--configfile", default=os.path.join(caiman_datadir(), 'demos', 'general', 'params_demo_OnACID.json'), help="JSON Configfile for Caiman parameters")
parser.add_argument("--input", action="append", help="File(s) to work on, provide multiple times for more files")
parser.add_argument("--logfile", help="If specified, log to the named file")
return parser.parse_args()
Expand Down
51 changes: 2 additions & 49 deletions demos/general/demo_OnACID_mesoscope.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,54 +46,7 @@ def main():
"%(relativeCreated)12d [%(filename)s:%(funcName)20s():%(lineno)s][%(process)d] %(message)s",
level=logging.WARNING)

if cfg.configfile:
opts = cnmf.params.CNMFParams(params_from_file=cfg.configfile)
else:
# Set up parameters using builtin defaults
params_dict = {
'data': {
'fr': 15,
'decay_time': 0.5,
},
'init': {
'gSig': (3, 3), # The notebooks have (4, 4) though
'nb': 2,
'K': 2,
},
'preprocess': {
'p': 1, # Possibly redundant
},
'temporal': {
'p': 1, # Possibly redundant
},
'online': {
'min_SNR': 1,
'rval_thr': 0.9,
'ds_factor': 1,
'motion_correct': True,
'init_batch': 200,
'init_method': 'bare',
'normalize': True,
'sniper_mode': True,
'epochs': 1,
'max_shifts_online': 10,
'dist_shape_update': True,
'min_num_trial': 10,
},
'motion': {
'pw_rigid': False,

},
'quality': {
'min_SNR': 2,
'rval_thr': 0.85,
'use_cnn': True,
'min_cnn_thr': 0.99,
'cnn_lowest': 0.1
},
}
opts = cnmf.params.CNMFParams(params_dict=params_dict)

opts = cnmf.params.CNMFParams(params_from_file=cfg.configfile)
opts.change_params({'online': {'show_movie': not cfg.no_play}}) # Override from CLI

if cfg.input is not None: # CLI arg can override all other settings for fnames, although other data-centric commands still must match source data
Expand Down Expand Up @@ -165,7 +118,7 @@ def main():

def handle_args():
parser = argparse.ArgumentParser(description="Full OnACID Caiman demo")
parser.add_argument("--configfile", help="JSON Configfile for Caiman parameters")
parser.add_argument("--configfile", default=os.path.join(caiman_datadir(), 'demos', 'general', 'params_demo_OnACID_mesoscope.json'), help="JSON Configfile for Caiman parameters")
parser.add_argument("--no_play", action="store_true", help="Do not display results")
parser.add_argument("--cluster_backend", default="multiprocessing", help="Specify multiprocessing, ipyparallel, or single to pick an engine")
parser.add_argument("--cluster_nproc", type=int, default=None, help="Override automatic selection of number of workers to use")
Expand Down
73 changes: 9 additions & 64 deletions demos/general/demo_caiman_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
except:
pass

import caiman as cm
import caiman
from caiman.paths import caiman_datadir
from caiman.source_extraction.cnmf import cnmf as cnmf
from caiman.source_extraction.cnmf import params as params
Expand All @@ -47,64 +47,9 @@ def main():
"%(relativeCreated)12d [%(filename)s:%(funcName)20s():%(lineno)s][%(process)d] %(message)s",
level=logging.WARNING)

if cfg.configfile:
opts = params.CNMFParams(params_from_file=cfg.configfile)
if opts.data['fnames'] is None:
opts.change_params({"data": {"fnames": fnames}})
else:
# set up some parameters
fr = 10 # approximate frame rate of data
decay_time = 5.0 # length of transient

# See the without_patches json for an alternative
rf = 10 # half size of each patch
stride = 4 # overlap between patches
K = 4 # number of components in each patch

gSig = [6, 6] # expected half size of neurons
merge_thresh = 0.80 # merging threshold, max correlation allowed
p = 2 # order of the autoregressive system
gnb = 2 # global background order

min_SNR = 2 # peak SNR for accepted components (if above this, accept)
rval_thr = 0.85 # space correlation threshold (if above this, accept)
use_cnn = True # use the CNN classifier
min_cnn_thr = 0.99 # if cnn classifier predicts below this value, reject
cnn_lowest = 0.1 # neurons with cnn probability lower than this value are rejected

params_dict = {
'data': {
'fnames': fnames,
'fr': fr,
'decay_time': decay_time,
},
'init': {
'gSig': gSig,
'K': K,
'nb': gnb
},
'patch': {
'rf': rf,
'stride': stride,
},
'merging': {
'merge_thr': merge_thresh,
},
'preprocess': {
'p': p,
},
'temporal': {
'p': p,
},
'quality': {
'min_SNR': min_SNR,
'rval_thr': rval_thr,
'use_cnn': use_cnn,
'min_cnn_thr': min_cnn_thr,
'cnn_lowest': cnn_lowest
}
}
opts = params.CNMFParams(params_dict=params_dict)
opts = params.CNMFParams(params_from_file=cfg.configfile)
if opts.data['fnames'] is None:
opts.change_params({"data": {"fnames": fnames}})

if cfg.input is not None: # CLI arg can override all other settings for fnames, although other data-centric commands still must match source data
opts.change_params({'data': {'fnames': cfg.input}})
Expand All @@ -115,7 +60,7 @@ def main():
opts.change_params({'data': {'fnames': fnames}})

# start a cluster for parallel processing
c, dview, n_processes = cm.cluster.setup_cluster(backend=cfg.cluster_backend, n_processes=cfg.cluster_nproc)
c, dview, n_processes = caiman.cluster.setup_cluster(backend=cfg.cluster_backend, n_processes=cfg.cluster_nproc)


# Run CaImAn Batch (CNMF)
Expand All @@ -134,7 +79,7 @@ def main():
cnm.estimates.plot_contours(img=Cn)

# load memory mapped file
Yr, dims, T = cm.load_memmap(cnm.mmap_file)
Yr, dims, T = caiman.load_memmap(cnm.mmap_file)
images = np.reshape(Yr.T, [T] + list(dims), order='F')

# refit
Expand Down Expand Up @@ -162,14 +107,14 @@ def main():

# save results
cnm2.estimates.Cn = Cn
cnm2.save(cnm2.mmap_file[:-4]+'hdf5')
cnm2.save(cnm2.mmap_file[:-4] + 'hdf5') # FIXME use the path library to do this the right way

# play movie with results (original, reconstructed, amplified residual)
if not cfg.no_play:
cnm2.estimates.play_movie(images, magnification=4);

# Stop the cluster and clean up log files
cm.stop_server(dview=dview)
caiman.stop_server(dview=dview)

if not cfg.keep_logs:
log_files = glob.glob('Yr*_LOG_*')
Expand All @@ -178,7 +123,7 @@ def main():

def handle_args():
parser = argparse.ArgumentParser(description="Demonstrate basic Caiman functionality")
parser.add_argument("--configfile", help="JSON Configfile for Caiman parameters")
parser.add_argument("--configfile", default=os.path.join(caiman_datadir(), 'demos', 'general', 'params_demo_caiman_basic_with_patches.json'), help="JSON Configfile for Caiman parameters")
parser.add_argument("--keep_logs", action="store_true", help="Keep temporary logfiles")
parser.add_argument("--no_play", action="store_true", help="Do not display results")
parser.add_argument("--cluster_backend", default="multiprocessing", help="Specify multiprocessing, ipyparallel, or single to pick an engine")
Expand Down
41 changes: 41 additions & 0 deletions demos/general/params_demo_OnACID_mesoscope.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"data": {
"fr": 15,
"decay_time": 0.5
},
"init": {
"gSig": (4, 4),
"nb": 2,
"K": 2
},
"preprocess": {
"p": 1
},
"temporal": {
"p": 1
},
"online": {
"min_SNR": 1,
"rval_thr": 0.9,
"ds_factor": 1,
"motion_correct": true,
"init_batch": 200,
"init_method": "bare",
"normalize": true,
"sniper_mode": true,
"epochs": 1,
"max_shifts_online": 10,
"dist_shape_update": true,
"min_num_trial": 10
},
"motion": {
"pw_rigid": false
},
"quality": {
"min_SNR": 2,
"rval_thr": 0.85,
"use_cnn": true,
"min_cnn_thr": 0.99,
"cnn_lowest": 0.1
}
}

0 comments on commit ad31003

Please sign in to comment.