PMD Compression#

Simple PMD compression, the hello world of masknmf

pmd compression
/home/runner/work/masknmf-toolbox/masknmf-toolbox/masknmf/utils/_cuda.py:10: UserWarning: You've explicitly selected to perform computations on the cpu, performance will be significantly slower
  warn(
[26-03-21 06:34:57]: Starting compression
[26-03-21 06:34:57]: sampled from the following regions: [0]
[26-03-21 06:34:57]: We are initializing on a total of 2000 frames
[26-03-21 06:34:57]: Loading data to estimate complete spatial basis
[26-03-21 06:34:57]: skipping the pruning step for frame cutoff
[26-03-21 06:34:57]: Finding spatiotemporal roughness thresholds

  0%|          | 0/250 [00:00<?, ?it/s]
  2%|▏         | 4/250 [00:00<00:06, 37.15it/s]
  4%|▎         | 9/250 [00:00<00:05, 42.80it/s]
  6%|▌         | 14/250 [00:00<00:05, 44.93it/s]
  8%|▊         | 19/250 [00:00<00:05, 44.68it/s]
 10%|▉         | 24/250 [00:00<00:05, 44.32it/s]
 12%|█▏        | 29/250 [00:00<00:04, 44.25it/s]
 14%|█▎        | 34/250 [00:00<00:04, 45.13it/s]
 16%|█▌        | 39/250 [00:00<00:04, 45.98it/s]
 18%|█▊        | 44/250 [00:00<00:04, 46.71it/s]
 20%|█▉        | 49/250 [00:01<00:04, 47.22it/s]
 22%|██▏       | 54/250 [00:01<00:04, 47.44it/s]
 24%|██▎       | 59/250 [00:01<00:04, 47.67it/s]
 26%|██▌       | 64/250 [00:01<00:03, 47.81it/s]
 28%|██▊       | 69/250 [00:01<00:03, 47.77it/s]
 30%|██▉       | 74/250 [00:01<00:03, 47.99it/s]
 32%|███▏      | 79/250 [00:01<00:03, 48.04it/s]
 34%|███▎      | 84/250 [00:01<00:03, 48.02it/s]
 36%|███▌      | 89/250 [00:01<00:03, 48.24it/s]
 38%|███▊      | 94/250 [00:02<00:03, 48.13it/s]
 40%|███▉      | 99/250 [00:02<00:03, 48.15it/s]
 42%|████▏     | 104/250 [00:02<00:03, 48.12it/s]
 44%|████▎     | 109/250 [00:02<00:02, 48.11it/s]
 46%|████▌     | 114/250 [00:02<00:02, 48.02it/s]
 48%|████▊     | 119/250 [00:02<00:02, 48.03it/s]
 50%|████▉     | 124/250 [00:02<00:02, 48.14it/s]
 52%|█████▏    | 129/250 [00:02<00:02, 48.10it/s]
 54%|█████▎    | 134/250 [00:02<00:02, 48.32it/s]
 56%|█████▌    | 139/250 [00:02<00:02, 48.31it/s]
 58%|█████▊    | 144/250 [00:03<00:02, 48.22it/s]
 60%|█████▉    | 149/250 [00:03<00:02, 48.49it/s]
 62%|██████▏   | 154/250 [00:03<00:01, 48.52it/s]
 64%|██████▎   | 159/250 [00:03<00:01, 48.33it/s]
 66%|██████▌   | 164/250 [00:03<00:01, 48.31it/s]
 68%|██████▊   | 169/250 [00:03<00:01, 48.19it/s]
 70%|██████▉   | 174/250 [00:03<00:01, 47.85it/s]
 72%|███████▏  | 179/250 [00:03<00:01, 47.63it/s]
 74%|███████▎  | 184/250 [00:03<00:01, 47.59it/s]
 76%|███████▌  | 189/250 [00:03<00:01, 47.45it/s]
 78%|███████▊  | 194/250 [00:04<00:01, 47.54it/s]
 80%|███████▉  | 199/250 [00:04<00:01, 47.55it/s]
 82%|████████▏ | 204/250 [00:04<00:00, 47.19it/s]
 84%|████████▎ | 209/250 [00:04<00:00, 47.43it/s]
 86%|████████▌ | 214/250 [00:04<00:00, 47.80it/s]
 88%|████████▊ | 219/250 [00:04<00:00, 47.87it/s]
 90%|████████▉ | 224/250 [00:04<00:00, 48.25it/s]
 92%|█████████▏| 229/250 [00:04<00:00, 48.38it/s]
 94%|█████████▎| 234/250 [00:04<00:00, 48.26it/s]
 96%|█████████▌| 239/250 [00:05<00:00, 48.40it/s]
 98%|█████████▊| 244/250 [00:05<00:00, 48.36it/s]
100%|█████████▉| 249/250 [00:05<00:00, 48.06it/s]
100%|██████████| 250/250 [00:05<00:00, 47.52it/s]
[26-03-21 06:35:02]: Running Blockwise Decompositions

  0%|          | 0/3 [00:00<?, ?it/s]
 67%|██████▋   | 2/3 [00:00<00:00, 12.73it/s]
100%|██████████| 3/3 [00:00<00:00, 12.82it/s]
[26-03-21 06:35:03]: Constructed U matrix. Rank of U is 182
[26-03-21 06:35:03]: PMD Objected constructed
/opt/hostedtoolcache/Python/3.12.13/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 49.262 seconds)

Gallery generated by Sphinx-Gallery