Esempio n. 1
0
#%%
fname = [u'demo_behavior.h5']
if fname[0] in ['demo_behavior.h5']:
    # TODO: todocument
    fname = [download_demo(fname[0])]
# TODO: todocument
m = cm.load(fname[0], is_behavior=True)

#%% load, rotate and eliminate useless pixels
m = m.transpose([0, 2, 1])
m = m[:, 150:, :]
#%% visualize movie
m.play()
#%% select interesting portion of the FOV (draw a polygon on the figure that pops up, when done press enter)
mask = np.array(behavior.select_roi(np.median(m[::100], 0), 1)[0], np.float32)
#%%
n_components = 4  # number of movement looked for
resize_fact = 0.5  # for computational efficiency movies are downsampled
# number of standard deviations above mean for the magnitude that are considered enough to measure the angle in polar coordinates
num_std_mag_for_angle = .6
only_magnitude = False  # if onlu interested in factorizing over the magnitude
method_factorization = 'dict_learn'  # could also use nmf
# number of iterations for the dictionary learning algorithm (Marial et al, 2010)
max_iter_DL = -30

spatial_filter_, time_trace_, of_or = cm.behavior.behavior.extract_motor_components_OF(m, n_components, mask=mask,
                                                                                       resize_fact=resize_fact, only_magnitude=only_magnitude, verbose=True, method_factorization='dict_learn', max_iter_DL=max_iter_DL)

#%%
mags, dircts, dircts_thresh, spatial_masks_thrs = cm.behavior.behavior.extract_magnitude_and_angle_from_OF(
        np.sqrt(
            np.diff(np.array(pts[0]).T, axis=0)**2 +
            np.diff(np.array(pts[8]).T, axis=0)**2), 3, 1)[2:]

    mat_files = [e1]
    m = cm.load_movie_chain(mat_files[:1], fr=100)[2:]
    pl.figure()
    pl.imshow(m[10], cmap='gray')
    pl.plot(pts[0][10] / 3, pts[8][10] / 3, 'r*')

    if mask_all:
        mask = np.ones(m.shape[1:])
        np.save(e[:-4] + '_mask_all_lat.npy', mask)

    else:
        mask = behavior.select_roi(np.median(m[::100], 0), 1)[0]
        np.save(e[:-4] + '_mask_lat_hl.npy', mask)
#%%
r_values = []
only_magnitude = False
n_components = 6
resize_fact = .5
num_std_mag_for_angle = .6
if whole_field:
    max_iter_DL = -30
else:
    max_iter_DL = -30

for e, e1 in zip(gt_names, expt_names):

    mat_files = [e1]
Esempio n. 3
0
def main():
    pass  # For compatibility between running under Spyder and the CLI

    #%%
    pl.ion()

    fname = [u'demo_behavior.h5']
    if fname[0] in ['demo_behavior.h5']:
        # TODO: todocument
        fname = [download_demo(fname[0])]
    # TODO: todocument
    m = cm.load(fname[0], is_behavior=True)

    #%% load, rotate and eliminate useless pixels
    m = m.transpose([0, 2, 1])
    m = m[:, 150:, :]
    #%% visualize movie
    m.play()
    #%% select interesting portion of the FOV (draw a polygon on the figure that pops up, when done press enter)
    print(
        "Please draw a polygon delimiting the ROI on the image that will be displayed after the image; press enter when done"
    )
    mask = np.array(
        behavior.select_roi(np.median(m[::100], 0), 1)[0], np.float32)
    #%%
    n_components = 4  # number of movement looked for
    resize_fact = 0.5  # for computational efficiency movies are downsampled
    # number of standard deviations above mean for the magnitude that are considered enough to measure the angle in polar coordinates
    num_std_mag_for_angle = .6
    only_magnitude = False  # if onlu interested in factorizing over the magnitude
    method_factorization = 'dict_learn'  # could also use nmf
    # number of iterations for the dictionary learning algorithm (Marial et al, 2010)
    max_iter_DL = -30

    spatial_filter_, time_trace_, of_or = cm.behavior.behavior.extract_motor_components_OF(
        m,
        n_components,
        mask=mask,
        resize_fact=resize_fact,
        only_magnitude=only_magnitude,
        verbose=True,
        method_factorization='dict_learn',
        max_iter_DL=max_iter_DL)

    #%%
    mags, dircts, dircts_thresh, spatial_masks_thrs = cm.behavior.behavior.extract_magnitude_and_angle_from_OF(
        spatial_filter_,
        time_trace_,
        of_or,
        num_std_mag_for_angle=num_std_mag_for_angle,
        sav_filter_size=3,
        only_magnitude=only_magnitude)
    #%%
    idd = 0
    axlin = pl.subplot(n_components, 2, 2)
    for mag, dirct, spatial_filter in zip(mags, dircts_thresh,
                                          spatial_filter_):
        pl.subplot(n_components, 2, 1 + idd * 2)
        min_x, min_y = np.min(np.where(mask), 1)

        spfl = spatial_filter
        spfl = cm.movie(spfl[None, :, :]).resize(1 / resize_fact,
                                                 1 / resize_fact, 1).squeeze()
        max_x, max_y = np.add((min_x, min_y), np.shape(spfl))

        mask[min_x:max_x, min_y:max_y] = spfl
        mask[mask < np.nanpercentile(spfl, 70)] = np.nan
        pl.imshow(m[0], cmap='gray')
        pl.imshow(mask, alpha=.5)
        pl.axis('off')

        axelin = pl.subplot(n_components, 2, 2 + idd * 2, sharex=axlin)
        pl.plot(mag / 10, 'k')
        dirct[mag < 0.5 * np.std(mag)] = np.nan
        pl.plot(dirct, 'r-', linewidth=2)

        idd += 1
Esempio n. 4
0
#%%
fname = [u'demo_behavior.h5']
if fname[0] in ['demo_behavior.h5']:
    # TODO: todocument
    fname = [download_demo(fname[0])]
# TODO: todocument
m = cm.load(fname[0], is_behavior=True)

#%% load, rotate and eliminate useless pixels
m = m.transpose([0, 2, 1])
m = m[:, 150:, :]
#%% visualize movie
m.play()
#%% select interesting portion of the FOV (draw a polygon on the figure that pops up, when done press enter)
mask = np.array(behavior.select_roi(np.median(m[::100], 0), 1)[0], np.float32)
#%%
n_components = 4  # number of movement looked for
resize_fact = 0.5  # for computational efficiency movies are downsampled
# number of standard deviations above mean for the magnitude that are considered enough to measure the angle in polar coordinates
num_std_mag_for_angle = .6
only_magnitude = False  # if onlu interested in factorizing over the magnitude
method_factorization = 'dict_learn'  # could also use nmf
# number of iterations for the dictionary learning algorithm (Marial et al, 2010)
max_iter_DL = -30

spatial_filter_, time_trace_, of_or = cm.behavior.behavior.extract_motor_components_OF(
    m,
    n_components,
    mask=mask,
    resize_fact=resize_fact,
#%
import caiman as cm
from caiman.source_extraction.cnmf import cnmf as cnmf
from caiman.components_evaluation import evaluate_components
from caiman.utils.visualization import plot_contours, view_patches_bar
from caiman.base.rois import extract_binary_masks_blob
from caiman.behavior import behavior
#%%
from scipy.sparse import coo_matrix

n_components = 3
m = cm.load('./test.tif')
m.play(gain=3.)
#%% extract optical flow and ry NMF on it
# select the portion of FOV interesting (could be all of it as well)
mask = behavior.select_roi(np.median(m[::100], 0), 1)[0]
resize_fact = .5
num_std_mag_for_angle = .6
whole_field = True
only_magnitude = False
spatial_filter_, time_trace_, of_or = cm.behavior.behavior.extract_motor_components_OF(
    m,
    n_components,
    mask=mask,
    resize_fact=resize_fact,
    only_magnitude=only_magnitude,
    max_iter=1000,
    verbose=True,
    method_factorization='nmf')
mags, dircts, dircts_thresh, spatial_masks_thrs = cm.behavior.behavior.extract_magnitude_and_angle_from_OF(
    spatial_filter_,
Esempio n. 6
0
print(expt_names)
print(expt_fold)
#%%
for e in gt_names:
    print(e)
    pts = scipy.io.loadmat(e)['points'][0][0]
    whisk_sess = scipy.signal.savgol_filter(
        np.abs(np.diff(np.array(pts[8]).T, axis=0)), 3, 1)[2:]
    num_tr = str(re.findall('\d+', e[-8:-4])[0])
    mat_files = [os.path.join(os.path.split(e)[0], 'trial' + num_tr + '.mat')]
    m = cm.load_movie_chain(mat_files[:1], fr=100)[2:]
    pl.figure()
    pl.imshow(m[0], cmap='gray')
    pl.plot(pts[0][10] / 3, pts[8][10] / 3, 'r*')

    mask = behavior.select_roi(np.median(m[::100], 0), 1)[0]

    if mask_all:

        np.save(e[:-4] + '_mask_all.npy', mask)

    else:

        np.save(e[:-4] + '_mask_whisk.npy', mask)


#%%
r_values = []
only_magnitude = False
n_components = 3
resize_fact = .5
Esempio n. 7
0
#%
import caiman as cm
from caiman.source_extraction.cnmf import cnmf as cnmf
from caiman.components_evaluation import evaluate_components
from caiman.utils.visualization import plot_contours, view_patches_bar
from caiman.base.rois import extract_binary_masks_blob
from caiman.behavior import behavior
#%%
from scipy.sparse import coo_matrix

n_components = 3
m = cm.load('./test.tif')
m.play(gain=3.)
#%% extract optical flow and ry NMF on it
mask = behavior.select_roi(
    np.median(m[::100], 0),
    1)[0]  # select the portion of FOV interesting (could be all of it as well)
resize_fact = .5
num_std_mag_for_angle = .6
whole_field = True
only_magnitude = False
spatial_filter_, time_trace_, of_or = cm.behavior.behavior.extract_motor_components_OF(
    m,
    n_components,
    mask=mask,
    resize_fact=resize_fact,
    only_magnitude=only_magnitude,
    max_iter=1000,
    verbose=True,
    method_factorization='nmf')
mags, dircts, dircts_thresh, spatial_masks_thrs = cm.behavior.behavior.extract_magnitude_and_angle_from_OF(
Esempio n. 8
0
#%
import caiman as cm
from caiman.source_extraction.cnmf import cnmf as cnmf
from caiman.components_evaluation import evaluate_components
from caiman.utils.visualization import plot_contours, view_patches_bar
from caiman.base.rois import extract_binary_masks_blob
from caiman.behavior import behavior
#%%
from scipy.sparse import coo_matrix

n_components = 3
m = cm.load('./test.tif')
m.play(gain=3.)
#%% extract optical flow and ry NMF on it
# select the portion of FOV interesting (could be all of it as well)
mask = behavior.select_roi(np.median(m[::100], 0), 1)[0]
resize_fact = .5
num_std_mag_for_angle = .6
whole_field = True
only_magnitude = False
spatial_filter_, time_trace_, of_or = cm.behavior.behavior.extract_motor_components_OF(
    m, n_components, mask=mask,  resize_fact=resize_fact, only_magnitude=only_magnitude, max_iter=1000, verbose=True, method_factorization='nmf')
mags, dircts, dircts_thresh, spatial_masks_thrs = cm.behavior.behavior.extract_magnitude_and_angle_from_OF(
    spatial_filter_, time_trace_, of_or, num_std_mag_for_angle=num_std_mag_for_angle, sav_filter_size=3, only_magnitude=only_magnitude)
#%% if you want to visualize optical flow
ms = [mask * fr for fr in m]
ms = np.dstack(ms)
ms = cm.movie(ms.transpose([2, 0, 1]))
_ = cm.behavior.behavior.compute_optical_flow(
    ms, do_show=True, polar_coord=True)