forked from GogollaLab/MouseFacialExpressionAnalysis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitial_correlations.py
More file actions
44 lines (33 loc) · 1.46 KB
/
Copy pathinitial_correlations.py
File metadata and controls
44 lines (33 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Open zarr, perform correlation across entire dataset frame by frame
import zarr
import numpy as np
import time
import dask.array as da
import dask.config
# Use Blosc as compressor for zarrs written to disk
from numcodecs.blosc import Blosc
from tqdm import tqdm
# data = zarr.open("/snlkt/lvhome/jdelahanty/facial_expression_demo_data/CSE020/data.zarr")
# data = data.descriptors
def chunk_corrcoef(first, second):
assert first.shape == second.shape
n_zarrs = first.shape[0]
first_out = np.corrcoef(first[0], second[0])
out = np.empty((n_zarrs,) + first_out.shape)
out[0] = first_out
for i in range(1, n_zarrs):
out[i] = np.corrcoef(first[i], second[i])
return out
input_file = "/snlkt/lvhome/jdelahanty/facial_expression_demo_data/CSE020/data.zarr/descriptors"
output_file = "/snlkt/lvhome/jdelahanty/facial_expression_demo_data/corrs/data.zarr"
z = da.from_zarr(input_file) # 2d array
# To prepare for ``map_blocks()``, ensure the same chunking for both input arrays
# (Not necessary if using ``blockwise()`` instead.)
new_chunks = {0: z.chunks[0][0], 1: -1}
firsts = z[0:-1, :].rechunk(new_chunks)
seconds = z[1:, :].rechunk(new_chunks)
delayed_results = da.map_blocks(
chunk_corrcoef, firsts, seconds, new_axis=2, chunks=(firsts.chunks[0], 2, 2)
)
with dask.config.set(scheduler='threading'): # compare to ``scheduler='processes'``?
delayed_results.to_zarr(output_file, overwrite=True)