PMD Compression#

Simple PMD compression, the hello world of masknmf

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

  0%|          | 0/250 [00:00<?, ?it/s]
  2%|▏         | 5/250 [00:00<00:05, 47.78it/s]
  4%|▍         | 11/250 [00:00<00:04, 49.54it/s]
  7%|▋         | 17/250 [00:00<00:04, 50.07it/s]
  9%|▉         | 23/250 [00:00<00:04, 50.27it/s]
 12%|█▏        | 29/250 [00:00<00:04, 50.29it/s]
 14%|█▍        | 35/250 [00:00<00:04, 49.20it/s]
 16%|█▋        | 41/250 [00:00<00:04, 49.53it/s]
 19%|█▉        | 47/250 [00:00<00:04, 49.90it/s]
 21%|██        | 53/250 [00:01<00:03, 50.12it/s]
 24%|██▎       | 59/250 [00:01<00:03, 50.32it/s]
 26%|██▌       | 65/250 [00:01<00:03, 50.19it/s]
 28%|██▊       | 71/250 [00:01<00:03, 50.19it/s]
 31%|███       | 77/250 [00:01<00:03, 50.20it/s]
 33%|███▎      | 83/250 [00:01<00:03, 50.20it/s]
 36%|███▌      | 89/250 [00:01<00:03, 50.33it/s]
 38%|███▊      | 95/250 [00:01<00:03, 50.32it/s]
 40%|████      | 101/250 [00:02<00:02, 50.45it/s]
 43%|████▎     | 107/250 [00:02<00:02, 50.59it/s]
 45%|████▌     | 113/250 [00:02<00:02, 50.67it/s]
 48%|████▊     | 119/250 [00:02<00:02, 50.58it/s]
 50%|█████     | 125/250 [00:02<00:02, 50.50it/s]
 52%|█████▏    | 131/250 [00:02<00:02, 50.52it/s]
 55%|█████▍    | 137/250 [00:02<00:02, 50.58it/s]
 57%|█████▋    | 143/250 [00:02<00:02, 50.72it/s]
 60%|█████▉    | 149/250 [00:02<00:01, 50.84it/s]
 62%|██████▏   | 155/250 [00:03<00:01, 50.82it/s]
 64%|██████▍   | 161/250 [00:03<00:01, 50.79it/s]
 67%|██████▋   | 167/250 [00:03<00:01, 50.87it/s]
 69%|██████▉   | 173/250 [00:03<00:01, 50.83it/s]
 72%|███████▏  | 179/250 [00:03<00:01, 50.73it/s]
 74%|███████▍  | 185/250 [00:03<00:01, 50.55it/s]
 76%|███████▋  | 191/250 [00:03<00:01, 50.54it/s]
 79%|███████▉  | 197/250 [00:03<00:01, 50.56it/s]
 81%|████████  | 203/250 [00:04<00:00, 50.64it/s]
 84%|████████▎ | 209/250 [00:04<00:00, 50.68it/s]
 86%|████████▌ | 215/250 [00:04<00:00, 50.81it/s]
 88%|████████▊ | 221/250 [00:04<00:00, 50.78it/s]
 91%|█████████ | 227/250 [00:04<00:00, 50.72it/s]
 93%|█████████▎| 233/250 [00:04<00:00, 50.57it/s]
 96%|█████████▌| 239/250 [00:04<00:00, 50.54it/s]
 98%|█████████▊| 245/250 [00:04<00:00, 50.63it/s]
100%|██████████| 250/250 [00:04<00:00, 50.46it/s]
[25-11-01 00:13:45]: Running Blockwise Decompositions
[25-11-01 00:13:45]: Constructed U matrix. Rank of U is 196
[25-11-01 00:13:45]: PMD Objected constructed
/opt/hostedtoolcache/Python/3.12.12/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.872 seconds)

Gallery generated by Sphinx-Gallery