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

Fix removeBL for 3D movies #1363

Merged
merged 5 commits into from
Jul 24, 2024
Merged
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ __pycache__/

# C extensions
*.so
*.c
caiman/source_extraction/cnmf/oasis.cpp

#movie tif
*.tif
Expand Down
12 changes: 6 additions & 6 deletions caiman/base/movies.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,17 +550,17 @@ def removeBL(self, windowSize:int=100, quantilMin:int=8, in_place:bool=False, re
self -= caiman.movie(cv2.resize(res,pixs.shape[::-1]),fr=self.fr).to3DFromPixelxTime(self.shape)
return self

def to2DPixelxTime(self, order='F'):
def to2DPixelxTime(self, order='F') -> 'movie':
"""
Transform 3D movie into 2D
Transform 3D or 4D movie into 2D (pixels x time)
"""
return self.transpose([2,1,0]).reshape((-1,self.shape[0]),order=order)
return self.T.reshape((-1,self.shape[0]),order=order)

def to3DFromPixelxTime(self, shape, order='F'):
def to3DFromPixelxTime(self, shape, order='F') -> 'movie':
"""
Transform 2D movie into 3D
Transform 2D movie (pixels x time) into 3D or 4D
"""
return to_3D(self,shape[::-1],order=order).transpose([2,1,0])
return to_3D(self,shape[::-1],order=order).T

def computeDFF(self, secsWindow: int = 5, quantilMin: int = 8, method: str = 'only_baseline', in_place: bool = False,
order: str = 'F') -> tuple[Any, Any]:
Expand Down
25 changes: 22 additions & 3 deletions caiman/tests/test_movies.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,37 @@
import numpy as np
import numpy.testing as npt
import os
from caiman.base.movies import load, load_iter
from caiman.base import movies
from caiman.paths import caiman_datadir


def test_load_iter():
fname = os.path.join(caiman_datadir(), 'example_movies', 'demoMovie.tif')
for subindices in (None, slice(100, None, 2)):
m = load_iter(fname, subindices=subindices)
m = movies.load_iter(fname, subindices=subindices)
S = 0
while True:
try:
S += np.sum(next(m))
except StopIteration:
break
npt.assert_allclose(S, load(fname, subindices=subindices).sum(), rtol=1e-6)
npt.assert_allclose(S, movies.load(fname, subindices=subindices).sum(), rtol=1e-6)


def test_to2D_to3D():
def reference_to2D(mov, order: str):
return mov.T.reshape((-1, mov.shape[0]), order=order)

# set up small test movies
rng = np.random.default_rng()
T, y, x, z = 10, 6, 4, 2
movie_3D = movies.movie(rng.random((T, y, x)))
movie_4D = movies.movie(rng.random((T, y, x, z)))

for movie in (movie_3D, movie_4D):
for order in ('F', 'C'):
shape = movie.shape
movie_flat = movie.to2DPixelxTime(order=order)
npt.assert_array_equal(movie_flat, reference_to2D(movie, order))
movie_from_flat = movie_flat.to3DFromPixelxTime(shape, order)
npt.assert_array_equal(movie_from_flat, movie)