Skip to content

Commit

Permalink
Merge pull request #1363 from proektlab/summary-images-3d
Browse files Browse the repository at this point in the history
Fix removeBL for 3D movies
  • Loading branch information
pgunn authored Jul 24, 2024
2 parents bf7e8de + 7e372b3 commit 0dac913
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
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)

0 comments on commit 0dac913

Please sign in to comment.