Note
Go to the end to download the full example code.
PMD Compression#
Simple PMD compression, the hello world of masknmf

/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-01-08 22:29:57]: Starting compression
[26-01-08 22:29:57]: sampled from the following regions: [0]
[26-01-08 22:29:57]: We are initializing on a total of 2000 frames
[26-01-08 22:29:57]: Loading data to estimate complete spatial basis
[26-01-08 22:29:57]: skipping the pruning step for frame cutoff
[26-01-08 22:29:57]: Finding spatiotemporal roughness thresholds
0%| | 0/250 [00:00<?, ?it/s]
2%|▏ | 5/250 [00:00<00:05, 46.65it/s]
4%|▍ | 10/250 [00:00<00:04, 48.26it/s]
6%|▌ | 15/250 [00:00<00:04, 48.64it/s]
8%|▊ | 21/250 [00:00<00:04, 49.13it/s]
11%|█ | 27/250 [00:00<00:04, 49.51it/s]
13%|█▎ | 33/250 [00:00<00:04, 49.95it/s]
16%|█▌ | 39/250 [00:00<00:04, 49.95it/s]
18%|█▊ | 45/250 [00:00<00:04, 49.97it/s]
20%|██ | 50/250 [00:01<00:04, 49.95it/s]
22%|██▏ | 55/250 [00:01<00:03, 49.94it/s]
24%|██▍ | 60/250 [00:01<00:03, 49.70it/s]
26%|██▋ | 66/250 [00:01<00:03, 49.80it/s]
29%|██▉ | 72/250 [00:01<00:03, 49.79it/s]
31%|███ | 78/250 [00:01<00:03, 50.05it/s]
34%|███▎ | 84/250 [00:01<00:03, 50.01it/s]
36%|███▌ | 90/250 [00:01<00:03, 49.95it/s]
38%|███▊ | 95/250 [00:01<00:03, 49.89it/s]
40%|████ | 101/250 [00:02<00:02, 50.02it/s]
43%|████▎ | 107/250 [00:02<00:02, 50.05it/s]
45%|████▌ | 113/250 [00:02<00:02, 49.94it/s]
47%|████▋ | 118/250 [00:02<00:02, 49.88it/s]
49%|████▉ | 123/250 [00:02<00:02, 49.71it/s]
51%|█████ | 128/250 [00:02<00:02, 49.67it/s]
53%|█████▎ | 133/250 [00:02<00:02, 49.57it/s]
55%|█████▌ | 138/250 [00:02<00:02, 49.56it/s]
57%|█████▋ | 143/250 [00:02<00:02, 49.67it/s]
59%|█████▉ | 148/250 [00:02<00:02, 49.67it/s]
61%|██████ | 153/250 [00:03<00:01, 49.73it/s]
63%|██████▎ | 158/250 [00:03<00:01, 49.81it/s]
66%|██████▌ | 164/250 [00:03<00:01, 49.90it/s]
68%|██████▊ | 169/250 [00:03<00:01, 49.84it/s]
70%|██████▉ | 174/250 [00:03<00:01, 48.72it/s]
72%|███████▏ | 179/250 [00:03<00:01, 46.77it/s]
74%|███████▎ | 184/250 [00:03<00:01, 47.17it/s]
76%|███████▌ | 189/250 [00:03<00:01, 47.00it/s]
78%|███████▊ | 194/250 [00:03<00:01, 47.31it/s]
80%|███████▉ | 199/250 [00:04<00:01, 47.18it/s]
82%|████████▏ | 204/250 [00:04<00:00, 47.51it/s]
84%|████████▎ | 209/250 [00:04<00:00, 46.71it/s]
86%|████████▌ | 214/250 [00:04<00:00, 47.14it/s]
88%|████████▊ | 219/250 [00:04<00:00, 47.36it/s]
90%|████████▉ | 224/250 [00:04<00:00, 47.94it/s]
92%|█████████▏| 229/250 [00:04<00:00, 48.15it/s]
94%|█████████▎| 234/250 [00:04<00:00, 48.09it/s]
96%|█████████▌| 239/250 [00:04<00:00, 48.52it/s]
98%|█████████▊| 244/250 [00:04<00:00, 48.19it/s]
100%|█████████▉| 249/250 [00:05<00:00, 47.44it/s]
100%|██████████| 250/250 [00:05<00:00, 48.91it/s]
[26-01-08 22:30:02]: Running Blockwise Decompositions
[26-01-08 22:30:02]: Constructed U matrix. Rank of U is 215
[26-01-08 22:30:02]: 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 52.076 seconds)