Skip to content

Commit

Permalink
Remove ipython breakpoints, modernise some string interpolation, othe…
Browse files Browse the repository at this point in the history
…r janitorial work
  • Loading branch information
pgunn committed Feb 15, 2024
1 parent e1c3f92 commit acc394a
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 76 deletions.
6 changes: 3 additions & 3 deletions caiman/base/movies.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ def extract_shifts(self, max_shift_w: int = 5, max_shift_h: int = 5, template=No

template = template[ms_h:h_i - ms_h, ms_w:w_i - ms_w].astype(np.float32)

#% run algorithm, press q to stop it
# run algorithm, press q to stop it
shifts = [] # store the amount of shift in each frame
xcorrs = []

Expand Down Expand Up @@ -604,7 +604,7 @@ def computeDFF(self, secsWindow: int = 5, quantilMin: int = 8, method: str = 'on
**self.__dict__)
numFramesNew, linePerFrame, pixPerLine = np.shape(mov_out)

#% compute baseline quickly
# compute baseline quickly
logging.debug("binning data ...")
sys.stdout.flush()

Expand All @@ -627,7 +627,7 @@ def computeDFF(self, secsWindow: int = 5, quantilMin: int = 8, method: str = 'on
cval=0.0,
prefilter=False)

#% compute DF/F
# compute DF/F
if not in_place:
if method == 'delta_f_over_sqrt_f':
mov_out = (mov_out - movBL) / np.sqrt(movBL)
Expand Down
13 changes: 6 additions & 7 deletions caiman/base/rois.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import cv2
import json
Expand Down Expand Up @@ -242,7 +241,7 @@ def nf_match_neurons_in_binary_masks(masks_gt,
cm_cnmf = [scipy.ndimage.center_of_mass(mm) for mm in masks_comp]

if D is None:
#% find distances and matches
# find distances and matches
# find the distance between each masks
D = distance_masks([A_ben, A_cnmf], [cm_ben, cm_cnmf], min_dist, enclosed_thr=enclosed_thr)

Expand All @@ -252,7 +251,7 @@ def nf_match_neurons_in_binary_masks(masks_gt,
matches = matches[0]
costs = costs[0]

#%% compute precision and recall
# compute precision and recall
TP = np.sum(np.array(costs) < thresh_cost) * 1.
FN = np.shape(masks_gt)[0] - TP
FP = np.shape(masks_comp)[0] - TP
Expand All @@ -264,7 +263,7 @@ def nf_match_neurons_in_binary_masks(masks_gt,
performance['accuracy'] = (TP + TN) / (TP + FP + FN + TN)
performance['f1_score'] = 2 * TP / (2 * TP + FP + FN)
logging.debug(performance)
#%%

idx_tp = np.where(np.array(costs) < thresh_cost)[0]
idx_tp_ben = matches[0][idx_tp] # ground truth
idx_tp_cnmf = matches[1][idx_tp] # algorithm - comp
Expand Down Expand Up @@ -471,7 +470,7 @@ def register_ROIs(A1,
matches = matches[0]
costs = costs[0]

#%% store indices
# store indices

idx_tp = np.where(np.array(costs) < thresh_cost)[0]
if len(idx_tp) > 0:
Expand All @@ -488,7 +487,7 @@ def register_ROIs(A1,
non_matched1 = list(range(D[0].shape[0]))
non_matched2 = list(range(D[0].shape[1]))

#%% compute precision and recall
# compute precision and recall

FN = D[0].shape[0] - TP
FP = D[0].shape[1] - TP
Expand Down Expand Up @@ -1001,7 +1000,7 @@ def getfloat():
fill_color = get32()
subtype = get16()
if subtype != 0:
raise ValueError('roireader: ROI subtype %s not supported (!= 0)' % subtype)
raise ValueError(f'roireader: ROI subtype {subtype} not supported (!= 0)')
options = get16()
arrow_style = get8()
arrow_head_size = get8()
Expand Down
1 change: 0 additions & 1 deletion caiman/base/traces.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import cv2
import logging
Expand Down
2 changes: 1 addition & 1 deletion caiman/components_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ def evaluate_components(Y: np.ndarray,
padafter = int(np.ceil(elm_missing / 2.))
tr_tmp = np.pad(traces.T, ((padbefore, padafter), (0, 0)), mode='reflect')
numFramesNew, num_traces = np.shape(tr_tmp)
#% compute baseline quickly
# compute baseline quickly
logging.debug("binning data ...")
tr_BL = np.reshape(tr_tmp, (downsampfact, numFramesNew // downsampfact, num_traces), order='F')
tr_BL = np.percentile(tr_BL, 8, axis=0)
Expand Down
3 changes: 1 addition & 2 deletions caiman/mmapping.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import ipyparallel as parallel
from itertools import chain
import logging
import numpy as np
import os
import pathlib
import pickle
import sys
import tifffile
from typing import Any, Optional, Union
import pathlib

import caiman
import caiman.paths
Expand Down
1 change: 0 additions & 1 deletion caiman/motion_correction.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Expand Down
7 changes: 0 additions & 7 deletions caiman/source_extraction/cnmf/cnmf_optional_outputs.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Constrained Nonnegative Matrix Factorization
A similar CNMF class that will output data that are not usually present in the regular CNMF process
Created on Fri Aug 26 15:44:32 2016
@author: agiovann
"""

import numpy as np
Expand Down
12 changes: 2 additions & 10 deletions caiman/source_extraction/cnmf/deconvolution.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Extract neural activity from a fluorescence trace using a constrained deconvolution approach
Created on Tue Sep 1 16:11:25 2015
@author: Eftychios A. Pnevmatikakis, based on an implementation by T. Machado, Andrea Giovannucci & Ben Deverett
"""
Extract neural activity from a fluorescence trace using a constrained deconvolution approach
"""

import numpy as np
Expand All @@ -15,8 +12,6 @@
from math import log, sqrt, exp

import sys
#%%


def constrained_foopsi(fluor, bl=None, c1=None, g=None, sn=None, p=None, method_deconvolution='oasis', bas_nonneg=True,
noise_range=[.25, .5], noise_method='logmexp', lags=5, fudge_factor=1.,
Expand Down Expand Up @@ -104,9 +99,6 @@ def constrained_foopsi(fluor, bl=None, c1=None, g=None, sn=None, p=None, metho
References:
* Pnevmatikakis et al. 2016. Neuron, in press, http://dx.doi.org/10.1016/j.neuron.2015.11.037
* Machado et al. 2015. Cell 162(2):338-350
\image: docs/img/deconvolution.png
\image: docs/img/evaluationcomponent.png
"""

if p is None:
Expand Down
37 changes: 9 additions & 28 deletions caiman/source_extraction/cnmf/online_cnmf.py
Original file line number Diff line number Diff line change
Expand Up @@ -1375,8 +1375,6 @@ def create_frame(self, frame_cor, show_residuals=True, resize_fact=3, transpose=
self.dims = self.dims[::-1]
return vid_frame


#%%
def bare_initialization(Y, init_batch=1000, k=1, method_init='greedy_roi', gnb=1,
gSig=[5, 5], motion_flag=False, p=1,
return_object=True, **kwargs):
Expand Down Expand Up @@ -1456,8 +1454,6 @@ def bare_initialization(Y, init_batch=1000, k=1, method_init='greedy_roi', gnb=1
except:
return Ain, np.array(b_in), Cin, f_in, YrA


#%%
def seeded_initialization(Y, Ain, dims=None, init_batch=1000, order_init=None, gnb=1, p=1,
return_object=True, **kwargs):

Expand Down Expand Up @@ -1513,7 +1509,7 @@ def seeded_initialization(Y, Ain, dims=None, init_batch=1000, order_init=None, g

model_comp = NMF(n_components=1, init='nndsvdar', max_iter=50)
for count, idx_in in enumerate(order_init):
if count%10 == 0:
if count % 10 == 0:
print(count)
idx_domain = np.where(Ain[:,idx_in])[0]
Ain[idx_domain,idx_in] = model_comp.fit_transform(\
Expand Down Expand Up @@ -1771,7 +1767,7 @@ def refit(o, c):
return C, noisyC, OASISinstances


#%% Estimate shapes on small initial batch
# Estimate shapes on small initial batch
def init_shapes_and_sufficient_stats(Y, A, C, b, f, W=None, b0=None, ssub_B=1, bSiz=3,
downscale_matrix=None, upscale_matrix=None):
# smooth the components
Expand Down Expand Up @@ -1943,8 +1939,6 @@ def get_last_frames(self, num_frames):
else:
return np.concatenate([self[(self.cur - num_frames):], self[:self.cur]], axis=0)


#%%
def csc_append(a, b):
""" Takes in 2 csc_matrices and appends the second one to the right of the first one.
Much faster than scipy.sparse.hstack but assumes the type to be csc and overwrites
Expand Down Expand Up @@ -2000,7 +1994,6 @@ def rank1nmf(Ypx, ain, iters=10):
cin = np.maximum(cin_res, 0)
return ain, cin, cin_res

#%%
@profile
def get_candidate_components(sv, dims, Yres_buf, min_num_trial=3, gSig=(5, 5),
gHalf=(5, 5), sniper_mode=True, rval_thr=0.85,
Expand Down Expand Up @@ -2127,8 +2120,6 @@ def get_candidate_components(sv, dims, Yres_buf, min_num_trial=3, gSig=(5, 5),

return Ain, Cin, Cin_res, idx, ijsig_all, cnn_pos, local_maxima


#%%
@profile
def update_num_components(t, sv, Ab, Cf, Yres_buf, Y_buf, rho_buf,
dims, gSig, gSiz, ind_A, CY, CC, groups, oases, gnb=1,
Expand Down Expand Up @@ -2371,7 +2362,7 @@ def update_num_components(t, sv, Ab, Cf, Yres_buf, Y_buf, rho_buf,
return Ab, Cf, Yres_buf, rho_buf, CC, CY, ind_A, sv, groups, ind_new, ind_new_all, sv, cnn_pos


#%% remove components online
# remove components online
def remove_components_online(ind_rem, gnb, Ab, use_dense, Ab_dense, AtA, CY,
CC, M, N, noisyC, OASISinstances, C_on, exp_comps):

Expand Down Expand Up @@ -2438,10 +2429,7 @@ def initialize_movie_online(Y, K, gSig, rf, stride, base_name,
images = np.reshape(Yr.T, [T] + list(dims), order='F')
Y = np.reshape(Yr, dims + (T,), order='F')
Cn2 = caiman.local_correlations(Y)
# pl.imshow(Cn2)
#%
#% RUN ALGORITHM ON PATCHES
# pl.close('all')
# RUN ALGORITHM ON PATCHES
cnm_init = caiman.source_extraction.cnmf.CNMF(n_processes, method_init='greedy_roi', k=K, gSig=gSig, merge_thresh=merge_thresh,
p=0, dview=dview, Ain=None, rf=rf, stride=stride, method_deconvolution='oasis', skip_refinement=False,
normalize_init=False, options_local_NMF=None,
Expand All @@ -2456,9 +2444,7 @@ def initialize_movie_online(Y, K, gSig, rf, stride, base_name,
b_tot = cnm_init.b
f_tot = cnm_init.f

print(('Number of components:' + str(A_tot.shape[-1])))

#%
print(f"Number of components: {A_tot.shape[-1]}")

traces = C_tot + YrA_tot
fitness_raw, fitness_delta, erfc_raw, erfc_delta, r_values, significant_samples = caiman.components_evaluation.evaluate_components(
Expand All @@ -2478,7 +2464,7 @@ def initialize_movie_online(Y, K, gSig, rf, stride, base_name,

A_tot = A_tot.tocsc()[:, idx_components]
C_tot = C_tot[idx_components]
#%

cnm_refine = caiman.source_extraction.cnmf.CNMF(n_processes, method_init='greedy_roi', k=A_tot.shape, gSig=gSig, merge_thresh=merge_thresh, rf=None, stride=None,
p=p, dview=dview, Ain=A_tot, Cin=C_tot, f_in=f_tot, method_deconvolution='oasis', skip_refinement=True,
normalize_init=False, options_local_NMF=None,
Expand All @@ -2487,9 +2473,9 @@ def initialize_movie_online(Y, K, gSig, rf, stride, base_name,
batch_update_suff_stat=True, max_comp_update_shape=5)

cnm_refine = cnm_refine.fit(images)
#%

A, C, b, f, YrA = cnm_refine.A, cnm_refine.C, cnm_refine.b, cnm_refine.f, cnm_refine.YrA
#%

final_frate = 10
Npeaks = 10
traces = C + YrA
Expand All @@ -2510,7 +2496,7 @@ def initialize_movie_online(Y, K, gSig, rf, stride, base_name,
print(' ***** ')
print((len(traces)))
print((len(idx_components)))
#%

cnm_refine.sn = sn # FIXME: There is no sn in scope here
cnm_refine.idx_components = idx_components
cnm_refine.idx_components_bad = idx_components_bad
Expand All @@ -2519,11 +2505,6 @@ def initialize_movie_online(Y, K, gSig, rf, stride, base_name,
cnm_refine.fitness_delta = fitness_delta
cnm_refine.Cn2 = Cn2

#%

# cnm_init.dview = None
# save_object(cnm_init,fls[0][:-4]+ '_DS_' + str(ds)+ '_init.pkl')

return cnm_refine, Cn2, fname_new

def load_OnlineCNMF(filename, dview = None):
Expand Down
9 changes: 2 additions & 7 deletions caiman/source_extraction/cnmf/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ def fast_prct_filt(input_data, level=8, frames_window=1000):
padafter = int(np.ceil(elm_missing / 2.))
tr_tmp = np.pad(data.T, ((padbefore, padafter), (0, 0)), mode='reflect')
numFramesNew, num_traces = np.shape(tr_tmp)
#% compute baseline quickly
# compute baseline quickly

tr_BL = np.reshape(tr_tmp, (downsampfact, int(numFramesNew / downsampfact),
num_traces), order='F')
Expand All @@ -596,7 +596,7 @@ def fast_prct_filt(input_data, level=8, frames_window=1000):
data -= tr_BL[padbefore:-padafter].T

return data.squeeze()
#%%

def detrend_df_f_auto(A, b, C, f, dims=None, YrA=None, use_annulus = True,
dist1 = 7, dist2 = 5, frames_window=1000,
use_fast = False):
Expand Down Expand Up @@ -699,9 +699,6 @@ def detrend_df_f_auto(A, b, C, f, dims=None, YrA=None, use_annulus = True,

return F_df

#%%


def manually_refine_components(Y, xxx_todo_changeme, A, C, Cn, thr=0.9, display_numbers=True,
max_number=None, cmap=None, **kwargs):
"""Plots contour of spatial components
Expand Down Expand Up @@ -1014,8 +1011,6 @@ def update_order_greedy(A, flag_AA=True):
parllcomp.append([i])
len_parrllcomp = [len(ls) for ls in parllcomp]
return parllcomp, len_parrllcomp
#%%


def compute_residuals(Yr_mmap_file, A_, b_, C_, f_, dview=None, block_size=1000, num_blocks_per_run=5):
'''compute residuals from memory mapped file and output of CNMF
Expand Down
2 changes: 1 addition & 1 deletion caiman/utils/labelling.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import logging
from scipy.ndimage import filters as ft

import caiman

def pre_preprocess_movie_labeling(dview, file_names, median_filter_size=(2, 1, 1),
Expand Down
Loading

0 comments on commit acc394a

Please sign in to comment.