You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
da.store and da.to_hdf5 cannot write same array to different dataset of a hdf5 file. #2837
import numpy as np
import dask.array as da
import h5py
import tempfile
# -- Write -- #
def h5py_write(fname, a):
with h5py.File(fname) as h5f:
for key in 'ab':
h5f.create_dataset(key, a.shape, a.dtype)[:] = a
def dask_store(fname, a):
with h5py.File(fname) as h5f:
dsets = [
h5f.create_dataset(key, a.shape, np.float64) for key in 'ab'
]
da.store([a, a], dsets)
def dask_to_hdf5(fname, a):
da.to_hdf5(fname, {'/a': a, '/b': a})
# -- Print -- #
def print_file(fname):
np.set_printoptions(suppress=True)
with h5py.File(fname, 'r') as h5f:
for key in 'ab':
print("key %s" % key)
print(h5f[key].value)
# -- Main -- #
def main():
a = np.round(np.random.normal(0, 100, (3, 5)), 2)
a = da.from_array(a, 1000)
d = [['h5py', h5py_write], ['da.store', dask_store],
['da.to_hdf5', dask_to_hdf5]]
for key, f in d:
with tempfile.NamedTemporaryFile() as tempf:
fname = tempf.name
f(fname, a)
print("---- %s ----" % key)
print_file(fname)
main()
changed the title [-]da.store and da.to_hdf5 write nonzero array as zeros[/-][+]da.store and da.to_hdf5 cannot write same array to different dataset of a hdf5 file.[/+]on Oct 28, 2017
I kind of suspect when dask has two arrays A and B with exactly same shape and values, and both A and B are written to the same hdf5 file simultaneously, one of the array will be written as zero. But I am not sure at this point. If that is the case, I can work around this.
Nope. I have just tested that. If I make a copy of the array, da.store and da.to_hdf5 write with no problem.
I guess these two functions write incorrectly only if the same dask array is written to two different datasets in the file at the same time.
Test code:
import numpy as np
import dask.array as da
import h5py
import tempfile
# -- Write -- #
def h5py_write(fname, a, b):
with h5py.File(fname) as h5f:
for key, arr in zip('ab', [a, b]):
h5f.create_dataset(key, arr.shape, arr.dtype)[:] = arr
def dask_store(fname, a, b):
with h5py.File(fname) as h5f:
dsets = []
for key, arr in zip('ab', [a, b]):
dsets.append(
h5f.create_dataset(key, arr.shape, arr.dtype)
)
da.store([a, b], dsets)
def dask_to_hdf5(fname, a, b):
da.to_hdf5(fname, {'/a': a, '/b': b})
# -- Print -- #
def print_file(fname):
np.set_printoptions(suppress=True)
with h5py.File(fname, 'r') as h5f:
for key in 'ab':
print("key %s" % key)
print(h5f[key].value)
# -- Main -- #
def main():
a = np.round(np.random.normal(0, 100, (3, 5)), 2)
a = da.from_array(a, 1000)
b = da.from_array(a.copy(), 1000)
d = [['h5py', h5py_write], ['da.store', dask_store],
['da.to_hdf5', dask_to_hdf5]]
for key, f in d:
with tempfile.NamedTemporaryFile() as tempf:
fname = tempf.name
f(fname, a, b)
print("---- %s ----" % key)
print_file(fname)
main()
I could be missing the point of this issue, but in searching for an answer for a similar question, this is where I ended up.
I am not sure if this is the intended behaviour of dask.store, but it seems that source arrays that have identical results are not written more than once to multiple targets. Here is a simple way to reproduce this behaviour:
import h5py
import numpy as np
import dask.array as da
# Data for testing
a = da.random.random(6, chunks=2)
b = da.random.random(6, chunks=2)
c = a * b # Identical result
d = a * b # Identical result
data = {'/c': c, '/d': d}
# Create new HDF5 file
h5_file = 'test.h5'
da.to_hdf5(h5_file, data)
# Check written output
with h5py.File(h5_file) as f:
print(np.isclose(f['c'], c))
print(np.isclose(f['d'], d))
"""
Out:
[ True True True True True True]
[False False False False False False]
"""
Activity
[-]da.store and h5py write differently[/-][+]da.store, da.to_hdf5 and h5py write differently[/+][-]da.store, da.to_hdf5 and h5py write differently[/-][+]da.store, da.to_hdf5, and h5py write differently[/+][-]da.store, da.to_hdf5, and h5py write differently[/-][+]da.store and da.to_hdf5 write nonzero array as zeros[/+][-]da.store and da.to_hdf5 write nonzero array as zeros[/-][+]da.store and da.to_hdf5 cannot write same array to different dataset of a hdf5 file.[/+]rzu512 commentedon Oct 28, 2017
I kind of suspect when dask has two arrays A and B with exactly same shape and values, and both A and B are written to the same hdf5 file simultaneously, one of the array will be written as zero. But I am not sure at this point. If that is the case, I can work around this.
mrocklin commentedon Oct 28, 2017
rzu512 commentedon Oct 29, 2017
Nope. I have just tested that. If I make a copy of the array, da.store and da.to_hdf5 write with no problem.
I guess these two functions write incorrectly only if the same dask array is written to two different datasets in the file at the same time.
Test code:
Result:
jakirkham commentedon Apr 10, 2018
Is this still an issue?
bluegeo commentedon May 8, 2018
I could be missing the point of this issue, but in searching for an answer for a similar question, this is where I ended up.
I am not sure if this is the intended behaviour of
dask.store, but it seems that source arrays that have identical results are not written more than once to multiple targets. Here is a simple way to reproduce this behaviour:jakirkham commentedon May 9, 2018
Suspect we need to generate a UUID for each storage operation. That should fix the issue. Shouldn’t be too difficult if someone wants to try a PR. 😉
jakirkham commentedon Jul 5, 2018
Solved by PR ( #3540 ).