Skip to content

Instantly share code, notes, and snippets.

@EricThomson
Created August 24, 2023 20:58
Show Gist options
  • Save EricThomson/a1421997f4056456c60e2f9034a8034d to your computer and use it in GitHub Desktop.
Save EricThomson/a1421997f4056456c60e2f9034a8034d to your computer and use it in GitHub Desktop.
Testing load and load_iter
#%%
try:
if __IPYTHON__:
# this is used for debugging purposes only. allows to reload classes when changed
ipython().magic('load_ext autoreload')
ipython().magic('autoreload 2')
except NameError:
pass
import bokeh.plotting as bpl
import holoviews as hv
import logging
import matplotlib.pyplot as plt
import numpy as np
import caiman as cm
from caiman.source_extraction import cnmf as cnmf
from caiman.motion_correction import MotionCorrect
from caiman.utils.utils import download_demo
from caiman.utils.visualization import nb_inspect_correlation_pnr
logging.basicConfig(format=
"%(relativeCreated)12d [%(filename)s:%(funcName)10s():%(lineno)s] [%(process)d] %(message)s",
# filename="/tmp/caiman.log",
level=logging.WARNING)
bpl.output_notebook()
hv.notebook_extension('bokeh')
#%% movie_ind 0 for the avi, 1 for the tif (in case avi loading troubles)
movie_ind = 0
fnames = ['msCam13_2x.avi', 'data_endoscope.tif'] # filename to be processed
fnames = [download_demo(fnames[movie_ind])]
print(fnames)
#####################
# Case 1: no subindices
# Test loading for load() and load_iter() for full movie make sure they match
# during pims fallback. Note: on my machine it doesn't work in opencv.
##
#%% Test load ()
movie_orig = cm.load(fnames[0])
dims = list(movie_orig.shape)
print(f"original movie dims: {dims}")
#%% view original movie
temporal_downsampling = 0.2
movie_orig.resize(1, 1, temporal_downsampling).play(gain = 0.6,
q_max=99.9,
q_min=1,
fr=20,
magnification=1,
plot_text=True,
do_loop=True)
#%% alternately, use load_iter()
num_frames = movie_orig.shape[0]
movie_orig_iter = cm.base.movies.load_iter(fnames[0])
iterated_arr = np.array([next(movie_orig_iter) for frame in range(num_frames)], dtype='float32')
movie_orig_iter = cm.movie(iterated_arr)
print(f"Iterated movie shape: {movie_orig_iter.shape}")
# test
assert(movie_orig.shape == movie_orig_iter.shape), "Shapes are different"
print("Shapes match")
#%% view iter-loaded and original together
cm.concatenate([movie_orig.resize(1, 1, temporal_downsampling),
movie_orig_iter.resize(1, 1, temporal_downsampling)],
axis=2).play(fr=20,
gain=0.6,
magnification=1,
plot_text=True,
do_loop=True,
q_max=99.9,
q_min=1) # press q to exit
# %% test (this isn't pefect it returns an array )
movie_orig == movie_orig_iter
##############################
# Case 2: subindices
# Test loading for load() and load_iter() with subindices make sure they match
# during pims fallback. Note: on my machine it doesn't work in opencv.
##
#%% define subinds to be used
# note subinds can also be a range or np.array
num_subinds = 150
start_frame = 850
subinds = slice(start_frame, start_frame + num_subinds)
#%% Test load () with subinds
movie_sub = cm.load(fnames[0], subindices=subinds)
print(f"movie_sub shape {movie_sub.shape}")
# test
assert(movie_sub.shape[0] == num_subinds), "Incorrect length of iter-movie"
print("Correct length!")
# %% view
movie_sub.resize(1, 1, temporal_downsampling).play(gain = 0.6,
q_max=99.9,
q_min=1,
fr=20,
magnification=1,
plot_text=True,
do_loop=True)
# %% Now test load_iter() with subinds
movie_sub_iter = cm.base.movies.load_iter(fnames[0], subindices=subinds)
iterated_arr_sub = np.array([next(movie_sub_iter) for frame in range(num_subinds)], dtype='float32')
movie_sub_iter = cm.movie(iterated_arr_sub)
print(f"Iterated movie shape: {movie_sub_iter.shape}")
# test
assert(movie_sub.shape == movie_sub_iter.shape), "Loat and load_iter don't match when using sub"
print("Shapes match for load_iter and load when using sub")
# %% view side-by-side
temporal_downsampling = 0.5
cm.concatenate([movie_sub.resize(1, 1, temporal_downsampling),
movie_sub_iter.resize(1, 1, temporal_downsampling)],
axis=2).play(fr=20,
gain=0.6,
magnification=1,
plot_text=True,
do_loop=True,
q_max=99.9,
q_min=1) # press q to exit
# %% compoare directly (again, not the perfect test)
movie_sub == movie_sub_iter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment