PMD Compression#

Simple PMD compression, the hello world of masknmf

pmd compression
[25-10-29 20:03:05]: Starting compression
[25-10-29 20:03:05]: sampled from the following regions: [0]
[25-10-29 20:03:05]: We are initializing on a total of 2000 frames
[25-10-29 20:03:05]: Loading data to estimate complete spatial basis
[25-10-29 20:03:05]: skipping the pruning step for frame cutoff
[25-10-29 20:03:05]: Finding spatiotemporal roughness thresholds

  0%|          | 0/250 [00:00<?, ?it/s]
  2%|▏         | 5/250 [00:00<00:05, 48.09it/s]
  4%|▍         | 10/250 [00:00<00:04, 49.19it/s]
  6%|▌         | 15/250 [00:00<00:04, 49.48it/s]
  8%|▊         | 21/250 [00:00<00:04, 49.85it/s]
 10%|█         | 26/250 [00:00<00:04, 49.86it/s]
 13%|█▎        | 32/250 [00:00<00:04, 49.90it/s]
 15%|█▍        | 37/250 [00:00<00:04, 49.89it/s]
 17%|█▋        | 43/250 [00:00<00:04, 50.16it/s]
 20%|█▉        | 49/250 [00:00<00:03, 50.55it/s]
 22%|██▏       | 55/250 [00:01<00:03, 50.76it/s]
 24%|██▍       | 61/250 [00:01<00:03, 50.88it/s]
 27%|██▋       | 67/250 [00:01<00:03, 50.87it/s]
 29%|██▉       | 73/250 [00:01<00:03, 50.84it/s]
 32%|███▏      | 79/250 [00:01<00:03, 50.62it/s]
 34%|███▍      | 85/250 [00:01<00:03, 50.56it/s]
 36%|███▋      | 91/250 [00:01<00:03, 50.35it/s]
 39%|███▉      | 97/250 [00:01<00:03, 50.37it/s]
 41%|████      | 103/250 [00:02<00:02, 50.33it/s]
 44%|████▎     | 109/250 [00:02<00:02, 50.43it/s]
 46%|████▌     | 115/250 [00:02<00:02, 50.26it/s]
 48%|████▊     | 121/250 [00:02<00:02, 50.11it/s]
 51%|█████     | 127/250 [00:02<00:02, 50.00it/s]
 53%|█████▎    | 133/250 [00:02<00:02, 49.99it/s]
 55%|█████▌    | 138/250 [00:02<00:02, 49.96it/s]
 57%|█████▋    | 143/250 [00:02<00:02, 49.92it/s]
 59%|█████▉    | 148/250 [00:02<00:02, 49.89it/s]
 62%|██████▏   | 154/250 [00:03<00:01, 50.01it/s]
 64%|██████▍   | 160/250 [00:03<00:01, 50.06it/s]
 66%|██████▋   | 166/250 [00:03<00:01, 50.01it/s]
 69%|██████▉   | 172/250 [00:03<00:01, 49.95it/s]
 71%|███████   | 177/250 [00:03<00:01, 49.91it/s]
 73%|███████▎  | 182/250 [00:03<00:01, 49.93it/s]
 75%|███████▍  | 187/250 [00:03<00:01, 49.94it/s]
 77%|███████▋  | 192/250 [00:03<00:01, 49.88it/s]
 79%|███████▉  | 197/250 [00:03<00:01, 49.76it/s]
 81%|████████  | 202/250 [00:04<00:00, 49.78it/s]
 83%|████████▎ | 208/250 [00:04<00:00, 49.88it/s]
 85%|████████▌ | 213/250 [00:04<00:00, 49.72it/s]
 88%|████████▊ | 219/250 [00:04<00:00, 49.78it/s]
 90%|████████▉ | 224/250 [00:04<00:00, 49.82it/s]
 92%|█████████▏| 230/250 [00:04<00:00, 49.79it/s]
 94%|█████████▍| 235/250 [00:04<00:00, 49.76it/s]
 96%|█████████▋| 241/250 [00:04<00:00, 49.83it/s]
 98%|█████████▊| 246/250 [00:04<00:00, 49.83it/s]
100%|██████████| 250/250 [00:04<00:00, 50.06it/s]
[25-10-29 20:03:10]: Running Blockwise Decompositions
[25-10-29 20:03:10]: Constructed U matrix. Rank of U is 194
[25-10-29 20:03:10]: PMD Objected constructed
/opt/hostedtoolcache/Python/3.12.11/x64/lib/python3.12/site-packages/fastplotlib/graphics/features/_base.py:18: UserWarning: casting float64 array to float32
  warn(f"casting {array.dtype} array to float32")

# test_example = true

import masknmf
import torch
import fastplotlib as fpl
from urllib.request import urlretrieve
import tifffile


urlretrieve(
    "https://github.com/flatironinstitute/CaImAn/raw/refs/heads/main/example_movies/demoMovie.tif",
    "./demo.tif"
)

# always lazy load raw data by memmaping or other methods
data = tifffile.imread("./demo.tif")

block_sizes = [32, 32]
max_components = 20

# it's recommended to use masknmf on a machine with a GPU
# device = "cuda" if torch.cuda.is_available() else "cpu"
device = "cpu"
# number of frames used to estimate the spatial basis in PMD
num_frames_for_spatial_fit = data.shape[0]

# perform PMD
pmd_result = masknmf.compression.pmd_decomposition(
    data,
    block_sizes,
    num_frames_for_spatial_fit,
    max_components=max_components,
    device=device,
    frame_batch_size=1024
)

# get the residual
pmd_residual = masknmf.PMDResidualArray(data, pmd_result)

# view the movies, note that all these array are LAZY evaluated, allowing you to view extremely large datasets!
iw = fpl.ImageWidget(
    data=[data, pmd_result, pmd_residual],
    names=["raw", "pmd", "residual"],
    figure_kwargs={"size": (1000, 340), "shape": (1, 3)},
    cmap="gnuplot2",
)

iw.show()

# use the time slider or set the frame index programmatically
iw.current_index = {"t": 1610}

# manually set vmin-vmax to emphasize noise in raw video
# you can also adjust the vmin-vmax using the histogram tool
# reset the vmin-vmax by clicking the buttons under "ImageWidget Controls"
for image in iw.managed_graphics:
    image.vmax = 3_200

# remove toolbar to reduce clutter
for subplot in iw.figure:
    subplot.toolbar = False


# ignore the remaining lines these are just for docs generation
figure = iw.figure
if __name__ == "__main__":
    print(__doc__)
    fpl.loop.run()

Total running time of the script: (0 minutes 41.683 seconds)

Gallery generated by Sphinx-Gallery