Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev no relative imports 2 #1277

Merged
merged 27 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
b1f0ff3
Redoing the diff. Also removing an unused spatial function that calle…
pgunn Feb 14, 2024
bd4fe8c
More reimplementing into new diff
pgunn Feb 14, 2024
d550961
oasis.cpp got mistakenly added again. Removing it again
pgunn Feb 14, 2024
170dc21
merging.py
pgunn Feb 14, 2024
f1a4673
initialization.py convert from old diff; note local_nmf method goes a…
pgunn Feb 14, 2024
f84062c
estimates: going less ambitious with this one, for now
pgunn Feb 14, 2024
811593f
cnmf/cnmf port over the diffs
pgunn Feb 14, 2024
9a9134d
motion_correction: merge intents of diffs
pgunn Feb 14, 2024
bc2b2f6
components_evaluation
pgunn Feb 14, 2024
ccd9dd5
caiman/cluster.py
pgunn Feb 14, 2024
ccb6a70
base/traces: going less ambitious on this one for now
pgunn Feb 14, 2024
5cef076
base/rois.py: less ambitious for now
pgunn Feb 14, 2024
542ffeb
caiman.base.movies: imports
pgunn Feb 15, 2024
e1c3f92
"import caiman as cm" -> "import caiman"
pgunn Feb 15, 2024
acc394a
Remove ipython breakpoints, modernise some string interpolation, othe…
pgunn Feb 15, 2024
f2eed29
Fix a brain-o in an imports fix
pgunn Feb 15, 2024
a2baf62
import cleanup: fix "profile" decorator I accidentally removed, also …
pgunn Feb 15, 2024
ed63aab
import cleanup: fix another typo
pgunn Feb 15, 2024
c7925a9
import cleanup: more bug squashing
pgunn Feb 15, 2024
f7a559f
imports cleanup: another bug
pgunn Feb 15, 2024
887a054
import cleanup: more fixes until it passes testing locally
pgunn Feb 15, 2024
afa972b
import cleanup: for traces we'll go with absolute import to make it c…
pgunn Feb 15, 2024
b2a2ce0
import cleanup: cluster.py - removed some unneded imports, a bit more…
pgunn Feb 15, 2024
961676b
import cleanup: order of imports
pgunn Feb 15, 2024
7741c9f
import cleanup: caiman_datadir() is never going to be confusing
pgunn Feb 15, 2024
60c73cb
import cleanup: remove another relative import
pgunn Feb 15, 2024
b98db65
Remove cnmf_optional_outputs.py, code that was broken 8 years ago wit…
pgunn Feb 15, 2024
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
8 changes: 4 additions & 4 deletions bin/caiman_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from pyqtgraph.parametertree import Parameter, ParameterTree
from scipy.sparse import csc_matrix

import caiman as cm
import caiman
from caiman.source_extraction.cnmf.cnmf import load_CNMF
from caiman.source_extraction.cnmf.params import CNMFParams

Expand Down Expand Up @@ -67,10 +67,10 @@ def make_color_img(img, gain=255, min_max=None, out_type=np.uint8):
directory=d, filter=f + ';;*.mmap')[0]

if fpath[-3:] == 'nwb':
mov = cm.load(cnm_obj.mmap_file,
mov = caiman.load(cnm_obj.mmap_file,
var_name_hdf5='acquisition/TwoPhotonSeries')
else:
mov = cm.load(cnm_obj.mmap_file)
mov = caiman.load(cnm_obj.mmap_file)

estimates = cnm_obj.estimates
params_obj = cnm_obj.params
Expand Down Expand Up @@ -106,7 +106,7 @@ def selectionchange(self,i):
cb.show()

if not hasattr(estimates, 'Cn'):
estimates.Cn = cm.local_correlations(mov, swap_dim=False)
estimates.Cn = caiman.local_correlations(mov, swap_dim=False)
#Cn = estimates.Cn

# We rotate our components 90 degrees right because of incompatibility of pyqtgraph and pyplot
Expand Down
11 changes: 5 additions & 6 deletions caiman/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#!/usr/bin/env python

import pkg_resources
from .base.movies import movie, load, load_movie_chain, _load_behavior, play_movie
from .base.timeseries import concatenate
from .cluster import start_server, stop_server
from .mmapping import load_memmap, save_memmap, save_memmap_each, save_memmap_join
from .summary_images import local_correlations
#from .source_extraction import cnmf
from caiman.base.movies import movie, load, load_movie_chain, _load_behavior, play_movie
from caiman.base.timeseries import concatenate
from caiman.cluster import start_server, stop_server
from caiman.mmapping import load_memmap, save_memmap, save_memmap_each, save_memmap_join
from caiman.summary_images import local_correlations

__version__ = pkg_resources.get_distribution('caiman').version
54 changes: 23 additions & 31 deletions caiman/base/movies.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,18 @@
import zarr
from zipfile import ZipFile

import caiman as cm

from . import timeseries
import caiman.base.timeseries
import caiman.base.traces
import caiman.mmapping
import caiman.summary_images
import caiman.utils.visualization

try:
cv2.setNumThreads(0)
except:
pass

from . import timeseries as ts
from .traces import trace

from ..mmapping import load_memmap
from ..utils import visualization
from .. import summary_images as si
from ..motion_correction import apply_shift_online, motion_correct_online


class movie(ts.timeseries):
class movie(caiman.base.timeseries.timeseries):
"""
Class representing a movie. This class subclasses timeseries,
that in turn subclasses ndarray
Expand Down Expand Up @@ -350,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 @@ -549,11 +542,11 @@ def removeBL(self, windowSize:int=100, quantilMin:int=8, in_place:bool=False, re
myperc = partial(np.percentile, q=quantilMin, axis=-1)
res = np.array(list(map(myperc,iter_win))).T
if returnBL:
return cm.movie(cv2.resize(res,pixs.shape[::-1]),fr=self.fr).to3DFromPixelxTime(self.shape)
return caiman.movie(cv2.resize(res,pixs.shape[::-1]),fr=self.fr).to3DFromPixelxTime(self.shape)
if (not in_place):
return (pixs-cv2.resize(res,pixs.shape[::-1])).to3DFromPixelxTime(self.shape)
else:
self -= cm.movie(cv2.resize(res,pixs.shape[::-1]),fr=self.fr).to3DFromPixelxTime(self.shape)
self -= caiman.movie(cv2.resize(res,pixs.shape[::-1]),fr=self.fr).to3DFromPixelxTime(self.shape)
return self

def to2DPixelxTime(self, order='F'):
Expand Down Expand Up @@ -611,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 @@ -634,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 Expand Up @@ -884,7 +877,7 @@ def local_correlations(self,
T = self.shape[0]
Cn = np.zeros(self.shape[1:])
if T <= 3000:
Cn = si.local_correlations(np.array(self),
Cn = caiman.summary_images.local_correlations(np.array(self),
eight_neighbours=eight_neighbours,
swap_dim=swap_dim,
order_mean=order_mean)
Expand All @@ -894,7 +887,7 @@ def local_correlations(self,
for jj, mv in enumerate(range(n_chunks - 1)):
logging.debug('number of chunks:' + str(jj) + ' frames: ' +
str([mv * frames_per_chunk, (mv + 1) * frames_per_chunk]))
rho = si.local_correlations(np.array(self[mv * frames_per_chunk:(mv + 1) * frames_per_chunk]),
rho = caiman.summary_images.local_correlations(np.array(self[mv * frames_per_chunk:(mv + 1) * frames_per_chunk]),
eight_neighbours=eight_neighbours,
swap_dim=swap_dim,
order_mean=order_mean)
Expand All @@ -905,7 +898,7 @@ def local_correlations(self,

logging.debug('number of chunks:' + str(n_chunks - 1) + ' frames: ' +
str([(n_chunks - 1) * frames_per_chunk, T]))
rho = si.local_correlations(np.array(self[(n_chunks - 1) * frames_per_chunk:]),
rho = caiman.summary_images.local_correlations(np.array(self[(n_chunks - 1) * frames_per_chunk:]),
eight_neighbours=eight_neighbours,
swap_dim=swap_dim,
order_mean=order_mean)
Expand Down Expand Up @@ -956,7 +949,7 @@ def partition_FOV_KMeans(self,
fovs = cv2.resize(np.uint8(fovs), (w1, h1), 1. / fx, 1. / fy, interpolation=cv2.INTER_NEAREST)
return np.uint8(fovs), mcoef, distanceMatrix

def extract_traces_from_masks(self, masks: np.ndarray) -> trace:
def extract_traces_from_masks(self, masks: np.ndarray) -> caiman.base.traces.trace:
"""
Args:
masks: array, 3D with each 2D slice bein a mask (integer or fractional)
Expand All @@ -975,7 +968,7 @@ def extract_traces_from_masks(self, masks: np.ndarray) -> trace:

pixelsA = np.sum(A, axis=1)
A = A / pixelsA[:, None] # obtain average over ROI
traces = trace(np.dot(A, np.transpose(Y)).T, **self.__dict__)
traces = caiman.base.traces.trace(np.dot(A, np.transpose(Y)).T, **self.__dict__)
return traces

def resize(self, fx=1, fy=1, fz=1, interpolation=cv2.INTER_AREA):
Expand Down Expand Up @@ -1013,7 +1006,7 @@ def resize(self, fx=1, fy=1, fz=1, interpolation=cv2.INTER_AREA):
if len(new_m) == 0:
new_m = m_tmp
else:
new_m = timeseries.concatenate([new_m, m_tmp], axis=0)
new_m = caiman.base.timeseries.concatenate([new_m, m_tmp], axis=0)

return new_m
else:
Expand Down Expand Up @@ -1566,7 +1559,7 @@ def load(file_name: Union[str, list[str]],

elif extension == '.mmap':
filename = os.path.split(file_name)[-1]
Yr, dims, T = load_memmap(
Yr, dims, T = caiman.mmapping.load_memmap(
os.path.join( # type: ignore # same dims typing issue as above
os.path.split(file_name)[0], filename))
images = np.reshape(Yr.T, [T] + list(dims), order='F')
Expand Down Expand Up @@ -1666,10 +1659,10 @@ def load_movie_chain(file_list: list[str],
m = m[:, top:h - bottom, left:w - right, z_top:d - z_bottom]

mov.append(m)
return ts.concatenate(mov, axis=0)
return caiman.base.timeseries.concatenate(mov, axis=0)

####
# This is only used for demo_behavior, and used to be part of cm.load(), activated with the
# This is only used for demo_behavior, and used to be part of caiman.load(), activated with the
# 'is_behavior' boolean flag.

def _load_behavior(file_name:str) -> Any:
Expand Down Expand Up @@ -1976,7 +1969,7 @@ def from_zip_file_to_movie(zipfile_name: str, start_end:Optional[tuple] = None)

counter += 1

return cm.movie(mov[:counter])
return caiman.movie(mov[:counter])


def from_zipfiles_to_movie_lists(zipfile_name: str, max_frames_per_movie: int = 3000,
Expand Down Expand Up @@ -2314,8 +2307,7 @@ def get_file_size(file_name, var_name_hdf5='mov') -> tuple[tuple, Union[int, tup
else:
raise Exception('File not found!')
elif isinstance(file_name, tuple):
from ...base.movies import load
dims = load(file_name[0], var_name_hdf5=var_name_hdf5).shape
dims = caiman.base.movies.load(file_name[0], var_name_hdf5=var_name_hdf5).shape
T = len(file_name)

elif isinstance(file_name, list):
Expand Down Expand Up @@ -2494,7 +2486,7 @@ def animate(i):
anim = matplotlib.animation.FuncAnimation(fig, animate, frames=frames, interval=1, blit=True)

# call our new function to display the animation
return visualization.display_animation(anim, fps=fr)
return caiman.utils.visualization.display_animation(anim, fps=fr)

elif backend == 'embed_opencv':
stopButton = widgets.ToggleButton(
Expand Down
15 changes: 7 additions & 8 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 All @@ -21,7 +20,7 @@
from typing import Any, Optional
import zipfile

from ..motion_correction import tile_and_correct
from caiman.motion_correction import tile_and_correct

try:
cv2.setNumThreads(0)
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
18 changes: 6 additions & 12 deletions caiman/base/traces.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
author: agiovann
"""
#%%

import cv2
import logging
import numpy as np
import pylab as pl
pl.ion()
from . import timeseries as ts

import caiman.base.timeseries

try:
cv2.setNumThreads(0)
except:
pass

#%%
################
# This holds the trace class, which is a specialised Caiman timeseries class.


class trace(ts.timeseries):
class trace(caiman.base.timeseries.timeseries):
"""
Class representing a trace.

Expand Down Expand Up @@ -130,7 +128,3 @@ def plot(self, stacked=True, subtract_minimum=False, cmap=pl.cm.jet, **kwargs):
def extract_epochs(self, trigs=None, tb=1, ta=1):
raise Exception('Not Implemented. Look at movie resize')


if __name__ == "__main__":
tracedata = trace(3 + np.random.random((2000, 4)), fr=30, start_time=0)
tracedata_dff = tracedata.computeDFF()
15 changes: 7 additions & 8 deletions caiman/behavior/behavior.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Functions related to optical flow
Expand All @@ -14,7 +13,7 @@
from sklearn.decomposition import NMF
import time

import caiman as cm
import caiman

try:
cv2.setNumThreads(0)
Expand Down Expand Up @@ -75,14 +74,14 @@ def extract_motor_components_OF(m,
mask = coo_matrix(np.array(mask).squeeze())
ms = [get_nonzero_subarray(mask.multiply(fr), mask) for fr in m]
ms = np.dstack(ms)
ms = cm.movie(ms.transpose([2, 0, 1]))
ms = caiman.movie(ms.transpose([2, 0, 1]))

else:
ms = m
of_or = compute_optical_flow(ms, do_show=False, polar_coord=False)
of_or = np.concatenate([
cm.movie(of_or[0]).resize(resize_fact, resize_fact, 1)[np.newaxis, :, :, :],
cm.movie(of_or[1]).resize(resize_fact, resize_fact, 1)[np.newaxis, :, :, :]
caiman.movie(of_or[0]).resize(resize_fact, resize_fact, 1)[np.newaxis, :, :, :],
caiman.movie(of_or[1]).resize(resize_fact, resize_fact, 1)[np.newaxis, :, :, :]
],
axis=0)

Expand Down Expand Up @@ -130,8 +129,8 @@ def extract_magnitude_and_angle_from_OF(spatial_filter_,
x, y = scipy.signal.medfilt(time_trace, kernel_size=[1, 1]).T
x = scipy.signal.savgol_filter(x.squeeze(), sav_filter_size, 1)
y = scipy.signal.savgol_filter(y.squeeze(), sav_filter_size, 1)
mag, dirct = to_polar(x - cm.components_evaluation.mode_robust(x),
y - cm.components_evaluation.mode_robust(y))
mag, dirct = to_polar(x - caiman.components_evaluation.mode_robust(x),
y - caiman.components_evaluation.mode_robust(y))
dirct = scipy.signal.medfilt(dirct.squeeze(), kernel_size=1).T

# normalize to pixel units
Expand Down Expand Up @@ -271,8 +270,8 @@ def compute_optical_flow(m,

return mov_tot

# NMF

#%% NMF
def extract_components(mov_tot,
n_components: int = 6,
normalize_std: bool = True,
Expand Down
Loading
Loading