Skip to content

da.store and da.to_hdf5 cannot write same array to different dataset of a hdf5 file. #2837

Closed

Description

@rzu512

Test code:

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()

Output:

---- h5py ----
key a
[[-190.69   81.22    7.9   241.42   93.71]
 [  16.67  -39.19 -151.7    53.67   81.64]
 [ -52.67  178.54  184.64  122.76  -69.45]]
key b
[[-190.69   81.22    7.9   241.42   93.71]
 [  16.67  -39.19 -151.7    53.67   81.64]
 [ -52.67  178.54  184.64  122.76  -69.45]]
---- da.store ----
key a
[[ 0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.]]
key b
[[-190.69   81.22    7.9   241.42   93.71]
 [  16.67  -39.19 -151.7    53.67   81.64]
 [ -52.67  178.54  184.64  122.76  -69.45]]
---- da.to_hdf5 ----
key a
[[ 0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.]]
key b
[[-190.69   81.22    7.9   241.42   93.71]
 [  16.67  -39.19 -151.7    53.67   81.64]
 [ -52.67  178.54  184.64  122.76  -69.45]]

Versions:

Python 3.5.3
Dask 0.15.3
h5py 2.7.0
libhdf5-dev 1.10.0-patch1+docs-3
Ubuntu amd64

Activity

  1. changed the title [-]da.store and h5py write differently[/-] [+]da.store, da.to_hdf5 and h5py write differently[/+] on Oct 28, 2017
  2. changed the title [-]da.store, da.to_hdf5 and h5py write differently[/-] [+]da.store, da.to_hdf5, and h5py write differently[/+] on Oct 28, 2017
  3. changed the title [-]da.store, da.to_hdf5, and h5py write differently[/-] [+]da.store and da.to_hdf5 write nonzero array as zeros[/+] on Oct 28, 2017
  4. 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
  5. rzu512 commented on Oct 28, 2017

    @rzu512
    Author

    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.

  6. mrocklin commented on Oct 28, 2017

    @mrocklin
    Member
  7. rzu512 commented on Oct 29, 2017

    @rzu512
    Author

    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()
    
    

    Result:

    ---- h5py ----
    key a
    [[ -60.42  214.05 -138.86  146.14  -56.01]
     [ -45.04  -57.44   76.25  194.38   79.14]
     [   1.4  -107.62  226.07  -57.12    4.89]]
    key b
    [[ -60.42  214.05 -138.86  146.14  -56.01]
     [ -45.04  -57.44   76.25  194.38   79.14]
     [   1.4  -107.62  226.07  -57.12    4.89]]
    ---- da.store ----
    key a
    [[ -60.42  214.05 -138.86  146.14  -56.01]
     [ -45.04  -57.44   76.25  194.38   79.14]
     [   1.4  -107.62  226.07  -57.12    4.89]]
    key b
    [[ -60.42  214.05 -138.86  146.14  -56.01]
     [ -45.04  -57.44   76.25  194.38   79.14]
     [   1.4  -107.62  226.07  -57.12    4.89]]
    ---- da.to_hdf5 ----
    key a
    [[ -60.42  214.05 -138.86  146.14  -56.01]
     [ -45.04  -57.44   76.25  194.38   79.14]
     [   1.4  -107.62  226.07  -57.12    4.89]]
    key b
    [[ -60.42  214.05 -138.86  146.14  -56.01]
     [ -45.04  -57.44   76.25  194.38   79.14]
     [   1.4  -107.62  226.07  -57.12    4.89]]
    
    
  8. jakirkham commented on Apr 10, 2018

    @jakirkham
    Member

    Is this still an issue?

  9. bluegeo commented on May 8, 2018

    @bluegeo

    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]
    """
    
  10. jakirkham commented on May 9, 2018

    @jakirkham
    Member

    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. 😉

  11. jakirkham commented on Jul 5, 2018

    @jakirkham
    Member

    Solved by PR ( #3540 ).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions