Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mannypaeza committed Jan 7, 2025
1 parent 9532291 commit bac09fb
Show file tree
Hide file tree
Showing 9 changed files with 420 additions and 5,180 deletions.
3 changes: 1 addition & 2 deletions caiman/train/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env python
import pkg_resources

from caiman.train.train_cnn_model_keras import cnn_model_keras, save_model_keras, load_model_keras
from caiman.train.train_cnn_model_pytorch import cnn_model_pytorch, train_test_split, train, validate, get_batch_accuracy, save_model_pytorch, load_model_pytorch
from caiman.train.train_cnn_model_helper import cnn_model_pytorch, train_test_split, train, validate, get_batch_accuracy, save_model_pytorch, load_model_pytorch, cnn_model_keras, save_model_keras, load_model_keras

__version__ = pkg_resources.get_distribution('caiman').version
132 changes: 97 additions & 35 deletions caiman/train/ground_truth_cnmf_seeded.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Parameters for the Movie and Name of the Movie "
"### Parameters for the Movie"
]
},
{
Expand All @@ -292,7 +292,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Analysis "
"## Run Analysis"
]
},
{
Expand All @@ -308,16 +308,30 @@
"d1, d2 = dims\n",
"images = np.reshape(Yr.T, [T] + list(dims), order='F')\n",
"Y = np.reshape(Yr, dims + (T,), order='F')\n",
"m_images = cm.movie(images)\n",
"\n",
"# correlation image\n",
"m_images = cm.movie(images)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Correlation Image"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if m_images.shape[0] < 10000:\n",
" Cn = m_images.local_correlations(\n",
" swap_dim=params_movie['swap_dim'], frames_per_chunk=1500)\n",
" Cn[np.isnan(Cn)] = 0\n",
"else:\n",
" #Saved as a tif file\n",
" Cn = np.array(cm.load(('/'.join(params_movie['gtname'][0].split('/')[:-2] + [\n",
" 'projections', 'correlation_image_better.tif'])))).squeeze()\n",
" 'projections', 'correlation_image_better.tif'])))).squeeze() \n",
"\n",
"plt.imshow(Cn, cmap='gray', vmax=.95)"
]
Expand All @@ -338,8 +352,15 @@
"\n",
"print(radius)\n",
"print(roi_cons.shape)\n",
"pl.imshow(roi_cons.sum(0))\n",
"\n",
"plt.imshow(roi_cons.sum(0))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if params_movie['kernel'] is not None: # kernel usually two\n",
" kernel = np.ones(\n",
" (radius // params_movie['kernel'], radius // params_movie['kernel']), np.uint8)\n",
Expand All @@ -349,24 +370,42 @@
"\n",
"A_in = np.reshape(roi_cons.transpose(\n",
" [2, 1, 0]), (-1, roi_cons.shape[0]), order='C')\n",
"pl.figure()\n",
"plt.figure()\n",
"crd = plot_contours(A_in, Cn, thr=.99999)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Parameter Setting"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# %% some parameter settings\n",
"# order of the autoregressive fit to calcium imaging in general one (slow gcamps) or two (fast gcamps fast scanning)\n",
"p = params_movie['p']\n",
"# merging threshold, max correlation allowed\n",
"merge_thresh = params_movie['merge_thresh']\n",
"\n",
"# %% Extract spatial and temporal components on patches\n",
"# TODO: todocument\n",
"merge_thresh = params_movie['merge_thresh']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Extract spatial and temporal components on patches"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if images.shape[0] > 10000:\n",
" check_nan = False\n",
"else:\n",
Expand All @@ -383,13 +422,17 @@
"f = cnm.f\n",
"snt = cnm.sn\n",
"print(('Number of components:' + str(A.shape[-1])))\n",
"# %%\n",
"pl.figure()\n",
"# TODO: show screenshot 12`\n",
"# TODO : change the way it is used\n",
"plt.figure()\n",
"crd = plot_contours(A, Cn, thr=params_display['thr_plot'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Threshold Components "
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -400,10 +443,9 @@
"view_patches_bar(Yr, scipy.sparse.coo_matrix(A.tocsc()[:, :]), C[:, :], b, f, dims[0], dims[1],\n",
" YrA=YrA[:, :], img=Cn)\n",
"\n",
"#%%\n",
"c, dview, n_processes = cm.cluster.setup_cluster(\n",
" backend='local', n_processes=None, single_thread=False)\n",
"#%% thredshold components\n",
"\n",
"min_size_neuro = 3 * 2 * np.pi\n",
"max_size_neuro = (2 * radius)**2 * np.pi\n",
"A_thr = cm.source_extraction.cnmf.spatial.threshold_components(A.tocsc()[:, :].toarray(), dims, medw=None, thr_method='max', maxthr=0.2, nrgthr=0.99, extract_cc=True,\n",
Expand All @@ -415,37 +457,57 @@
" & (size_neurons < max_size_neuro))[0]\n",
"A_thr = A_thr[:, idx_size_neuro]\n",
"print(A_thr.shape)\n",
"#%%\n",
"\n",
"crd = plot_contours(scipy.sparse.coo_matrix(\n",
" A_thr * 1.), Cn, thr=.99, vmax=0.35)\n",
"#%%\n",
"\n",
"roi_cons = np.load(params_movie['gtname'][0])\n",
"print(roi_cons.shape)\n",
"pl.imshow(roi_cons.sum(0))\n",
"#%% compare CNMF sedded with ground truth\n",
"pl.figure(figsize=(30, 20))\n",
"plt.imshow(roi_cons.sum(0))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Compare CNMF Seeded with ground truth"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.figure(figsize=(30, 20))\n",
"tp_gt, tp_comp, fn_gt, fp_comp, performance_cons_off = cm.base.rois.nf_match_neurons_in_binary_masks(roi_cons, A_thr[:, :].reshape([dims[0], dims[1], -1], order='F').transpose([2, 0, 1]) * 1., thresh_cost=.7, min_dist=10,\n",
" print_assignment=False, plot_results=False, Cn=Cn, labels=['GT', 'Offline'])\n",
"pl.rcParams['pdf.fonttype'] = 42\n",
"plt.rcParams['pdf.fonttype'] = 42\n",
"font = {'family': 'Myriad Pro',\n",
" 'weight': 'regular',\n",
" 'size': 20}\n",
"pl.rc('font', **font)\n",
"\n",
"#%%\n",
"np.savez(os.path.join(os.path.split(fname_new)[0], os.path.split(fname_new)[1][:-4] + 'match_masks.npz'), Cn=Cn,\n",
" tp_gt=tp_gt, tp_comp=tp_comp, fn_gt=fn_gt, fp_comp=fp_comp, performance_cons_off=performance_cons_off, idx_size_neuro_gt=idx_size_neuro, A_thr=A_thr,\n",
" A_gt=A, C_gt=C, b_gt=b, f_gt=f, YrA_gt=YrA, d1=d1, d2=d2, idx_components_gt=idx_size_neuro[\n",
" tp_comp],\n",
" idx_components_bad_gt=idx_size_neuro[fp_comp], fname_new=fname_new)\n"
"plt.rc('font', **font)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Generating match_masks.npz"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"np.savez(os.path.join(os.path.split(fname_new)[0], os.path.split(fname_new)[1][:-4] + 'match_masks.npz'), Cn=Cn,\n",
" tp_gt=tp_gt, tp_comp=tp_comp, fn_gt=fn_gt, fp_comp=fp_comp, performance_cons_off=performance_cons_off, idx_size_neuro_gt=idx_size_neuro, A_thr=A_thr,\n",
" A_gt=A, C_gt=C, b_gt=b, f_gt=f, YrA_gt=YrA, d1=d1, d2=d2, idx_components_gt=idx_size_neuro[\n",
" tp_comp],\n",
" idx_components_bad_gt=idx_size_neuro[fp_comp], fname_new=fname_new)"
]
}
],
"metadata": {
Expand Down
Loading

0 comments on commit bac09fb

Please sign in to comment.