Skip to content

Commit 89c4076

Browse files
committed
fixed further index problem with 5D data array
1 parent db89971 commit 89c4076

File tree

1 file changed

+36
-12
lines changed

1 file changed

+36
-12
lines changed

suite2p/io/h5.py

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,16 @@ def h5py_to_binary(ops):
5252

5353
for ih5, h5 in enumerate(h5list):
5454
with h5py.File(h5, "r") as f:
55-
# if h5py data is 5D or 4D instead of 3D, assume that
56-
# data = (nchan x) (nframes x) nplanes x pixels x pixels
5755
# 5D/4D data is flattened to process the same way as interleaved data
5856
for key in keys:
5957
hdims = f[key].ndim
6058
# keep track of the plane identity of the first frame (channel identity is assumed always 0)
6159
ncp = nplanes * nchannels
6260
nbatch = ncp * math.ceil(ops1[0]["batch_size"] / ncp)
63-
nframes_all = f[key].shape[
64-
0] if hdims == 3 else f[key].shape[0] * f[key].shape[1]
61+
nframes_all = np.prod(f[key].shape[:-2]) # product except for the last two dim
6562
nbatch = min(nbatch, nframes_all)
6663
nfunc = ops["functional_chan"] - 1 if nchannels > 1 else 0
67-
# loop over all tiffs
64+
# loop over all frames
6865
ik = 0
6966
while 1:
7067
if hdims == 3:
@@ -78,31 +75,58 @@ def h5py_to_binary(ops):
7875
1)
7976
if irange.size == 0:
8077
break
81-
im = f[key][irange, ...]
82-
if im.ndim == 5 and im.shape[0] == nchannels:
78+
if hdims == 4:
79+
# for 4D, expect
80+
# data = nframes x nplanes x pixels x pixels or
81+
# data = nframes x nchans x pixels x pixels
82+
# Data sanity check
83+
if not (
84+
((f[key].shape[1] == nchannels) and (nplanes == 1)) or
85+
((f[key].shape[1] == nplanes) and (nchannels == 1))):
86+
raise ValueError("Channel/Plane dimension mismatch.\n"
87+
"h5 file should follow the following dimension\n"
88+
"nframes x nplanes x pixels x pixels or"
89+
"nframes x nchans x pixels x pixels")
90+
im = f[key][irange, ...]
91+
elif hdims == 5:
92+
# for 5D, expect
93+
# data = nchans x nframes x nplanes x pixels x pixels
94+
# Data sanity check
95+
if not (
96+
(f[key].shape[0] == nchannels) and
97+
(f[key].shape[2] == nplanes)):
98+
raise ValueError("Channel/Plane dimension mismatch.\n"
99+
"h5 file should follow the following dimension\n"
100+
"nchans x nframes x nplanes x pixels x pixels")
101+
im = f[key][:, irange, :, :, :]
83102
im = im.transpose((1, 0, 2, 3, 4))
84103
# flatten to frames x pixels x pixels
104+
# for 5D data with 2 channels, 2 planes, this results
105+
# [fr0 ch0 pl0], [fr0 ch0 pl1], [fr0 ch1 pl0],
106+
# [fr0 ch1 pl1] [fr1 ch0 pl0], ...
85107
im = np.reshape(im, (-1, im.shape[-2], im.shape[-1]))
86108
nframes = im.shape[0]
87-
if type(im[0, 0, 0]) == np.uint16:
109+
if im.dtype == np.uint16:
88110
im = im / 2
89111
for j in range(0, nplanes):
90112
if iall == 0:
91113
ops1[j]["meanImg"] = np.zeros((im.shape[1], im.shape[2]),
92114
np.float32)
93115
if nchannels > 1:
116+
# TODO: either show error when nchannel > 2 or
117+
# implement generalized func. for nchannel > 2
94118
ops1[j]["meanImg_chan2"] = np.zeros(
95119
(im.shape[1], im.shape[2]), np.float32)
96120
ops1[j]["nframes"] = 0
97-
i0 = nchannels * ((j) % nplanes)
121+
i0 = j
98122
im2write = im[np.arange(int(i0) +
99-
nfunc, nframes, ncp), :, :].astype(
123+
nfunc*nplanes, nframes, ncp), :, :].astype(
100124
np.int16)
101125
reg_file[j].write(bytearray(im2write))
102126
ops1[j]["meanImg"] += im2write.astype(np.float32).sum(axis=0)
103127
if nchannels > 1:
104-
im2write = im[np.arange(int(i0) + 1 -
105-
nfunc, nframes, ncp), :, :].astype(
128+
im2write = im[np.arange(int(i0) + nplanes -
129+
nfunc*nplanes, nframes, ncp), :, :].astype(
106130
np.int16)
107131
reg_file_chan2[j].write(bytearray(im2write))
108132
ops1[j]["meanImg_chan2"] += im2write.astype(

0 commit comments

Comments
 (0)