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:7: UserWarning: You've explicitly selected to perform computations on the cpu, performance will be significantly slower
warn(
[25-11-16 02:56:10]: Starting compression
[25-11-16 02:56:10]: sampled from the following regions: [0]
[25-11-16 02:56:10]: We are initializing on a total of 2000 frames
[25-11-16 02:56:10]: Loading data to estimate complete spatial basis
[25-11-16 02:56:10]: skipping the pruning step for frame cutoff
[25-11-16 02:56:10]: Finding spatiotemporal roughness thresholds
0%| | 0/250 [00:00<?, ?it/s]
2%|▏ | 5/250 [00:00<00:05, 42.41it/s]
4%|▍ | 10/250 [00:00<00:05, 43.21it/s]
6%|▌ | 15/250 [00:00<00:05, 43.97it/s]
8%|▊ | 20/250 [00:00<00:05, 44.39it/s]
10%|█ | 25/250 [00:00<00:05, 42.23it/s]
12%|█▏ | 30/250 [00:00<00:05, 43.29it/s]
14%|█▍ | 35/250 [00:00<00:04, 43.98it/s]
16%|█▌ | 40/250 [00:00<00:04, 44.52it/s]
18%|█▊ | 45/250 [00:01<00:04, 44.94it/s]
20%|██ | 50/250 [00:01<00:04, 45.19it/s]
22%|██▏ | 55/250 [00:01<00:04, 45.33it/s]
24%|██▍ | 60/250 [00:01<00:04, 45.35it/s]
26%|██▌ | 65/250 [00:01<00:04, 45.25it/s]
28%|██▊ | 70/250 [00:01<00:03, 45.51it/s]
30%|███ | 75/250 [00:01<00:03, 45.58it/s]
32%|███▏ | 80/250 [00:01<00:03, 45.50it/s]
34%|███▍ | 85/250 [00:01<00:03, 45.36it/s]
36%|███▌ | 90/250 [00:02<00:03, 45.51it/s]
38%|███▊ | 95/250 [00:02<00:03, 45.60it/s]
40%|████ | 100/250 [00:02<00:03, 45.61it/s]
42%|████▏ | 105/250 [00:02<00:03, 45.69it/s]
44%|████▍ | 110/250 [00:02<00:03, 45.83it/s]
46%|████▌ | 115/250 [00:02<00:02, 45.80it/s]
48%|████▊ | 120/250 [00:02<00:02, 45.73it/s]
50%|█████ | 125/250 [00:02<00:02, 45.76it/s]
52%|█████▏ | 130/250 [00:02<00:02, 45.86it/s]
54%|█████▍ | 135/250 [00:02<00:02, 45.81it/s]
56%|█████▌ | 140/250 [00:03<00:02, 45.89it/s]
58%|█████▊ | 145/250 [00:03<00:02, 45.90it/s]
60%|██████ | 150/250 [00:03<00:02, 45.88it/s]
62%|██████▏ | 155/250 [00:03<00:02, 45.75it/s]
64%|██████▍ | 160/250 [00:03<00:01, 45.77it/s]
66%|██████▌ | 165/250 [00:03<00:01, 45.86it/s]
68%|██████▊ | 170/250 [00:03<00:01, 45.96it/s]
70%|███████ | 175/250 [00:03<00:01, 45.90it/s]
72%|███████▏ | 180/250 [00:03<00:01, 45.89it/s]
74%|███████▍ | 185/250 [00:04<00:01, 45.78it/s]
76%|███████▌ | 190/250 [00:04<00:01, 46.01it/s]
78%|███████▊ | 195/250 [00:04<00:01, 46.18it/s]
80%|████████ | 200/250 [00:04<00:01, 46.22it/s]
82%|████████▏ | 205/250 [00:04<00:00, 46.24it/s]
84%|████████▍ | 210/250 [00:04<00:00, 46.36it/s]
86%|████████▌ | 215/250 [00:04<00:00, 44.89it/s]
88%|████████▊ | 220/250 [00:04<00:00, 45.33it/s]
90%|█████████ | 225/250 [00:04<00:00, 45.67it/s]
92%|█████████▏| 230/250 [00:05<00:00, 45.79it/s]
94%|█████████▍| 235/250 [00:05<00:00, 45.65it/s]
96%|█████████▌| 240/250 [00:05<00:00, 45.74it/s]
98%|█████████▊| 245/250 [00:05<00:00, 45.99it/s]
100%|██████████| 250/250 [00:05<00:00, 46.09it/s]
100%|██████████| 250/250 [00:05<00:00, 45.47it/s]
[25-11-16 02:56:16]: Running Blockwise Decompositions
[25-11-16 02:56:16]: Constructed U matrix. Rank of U is 190
[25-11-16 02:56:16]: 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 51.838 seconds)