Note

This page was generated from a Jupyter notebook. Not all interactive visualization will work on this web page. Consider downloading the notebooks for full Python-backed interactivity.

[ ]:
# holoviews initialization

2. Allign Videos

2.1. open datasets

All the metadata defined in id_dims will be printed out for each dataset. It is important to make sure all the metadata are correct, otherwise you may get unexpected results. If metadata was not saved correctly, consider putting the datasets into correct hierarchical directory structures and use the post_process argument of open_minian_mf to correct for metadata. See the main pipeline.ipynb and API reference for more detail.

[5]:
minian_ds = open_minian_mf(
    dpath, id_dims, pattern=f_pattern)
opening dataset under /home/runner/work/minian/minian/demo_data/session2
opening minian.nc
['session: session2']
opening dataset under /home/runner/work/minian/minian/demo_data/session1
opening minian.nc
['session: session1']

2.2. estimate shifts

Here we estimate a translational shift along the session dimension using the max projection for each dataset. We combine the shifts, original templates temps, and shifted templates temps_sh into a single dataset shiftds to use later.

[6]:
%%time
temps = minian_ds['max_proj'].rename('temps')
shifts = estimate_motion(temps, dim='session').compute().rename('shifts')
temps_sh = apply_transform(temps, shifts).compute().rename('temps_shifted')
shiftds = xr.merge([temps, shifts, temps_sh])
CPU times: user 241 ms, sys: 172 ms, total: 413 ms
Wall time: 430 ms

2.3. visualize alignment

We visualize alignment of sessions by plotting the templates before and after the shift for each session.

[7]:
hv.output(size=int(output_size * 0.6))
opts_im = {
    'aspect': shiftds.sizes['width'] / shiftds.sizes['height'],
    'frame_width': 500, 'cmap': 'viridis'}
hv_temps = (hv.Dataset(temps).to(hv.Image, kdims=['width', 'height'])
            .opts(**opts_im).layout('session').cols(1))
hv_temps_sh = (hv.Dataset(temps_sh).to(hv.Image, kdims=['width', 'height'])
            .opts(**opts_im).layout('session').cols(1))
display(hv_temps + hv_temps_sh)

2.4. visualize overlap of field of view across all sessions

Since only pixels that are common across all sessions are considered, it is important to sanity-check that this overlap window capture most of our cells.

[8]:
hv.output(size=int(output_size * 0.6))
opts_im = {
    'aspect': shiftds.sizes['width'] / shiftds.sizes['height'],
    'frame_width': 500, 'cmap': 'viridis'}
window = shiftds['temps_shifted'].isnull().sum('session')
window, _ = xr.broadcast(window, shiftds['temps_shifted'])
hv_wnd = hv.Dataset(window).to(hv.Image, ['width', 'height'])
hv_temps = hv.Dataset(temps_sh).to(hv.Image, ['width', 'height'])
hv_wnd.opts(**opts_im).relabel("Window") + hv_temps.opts(**opts_im).relabel("Shifted Templates")
[8]:

2.5. apply shifts and set window

If the shifts and overlaps all look good, we commit by applying them to the spatial footprints of each session.

[9]:
A_shifted = apply_transform(minian_ds['A'].chunk(dict(height=-1, width=-1)), shiftds['shifts'])
[10]:
def set_window(wnd):
    return wnd == wnd.min()
window = xr.apply_ufunc(
    set_window,
    window,
    input_core_dims=[['height', 'width']],
    output_core_dims=[['height', 'width']],
    vectorize=True)