Skip to content

Implement shape argument in *_like functions #4875

Closed

Description

@pentschev

The shape argument in empty_like (and counterparts: full_like, ones_like and zeros_like) is important to guarantee functions that depend on creation of such arrays (without _like) to work correctly with the introduction of NEP-18 and _meta in Dask arrays. The argument was introduced by numpy/numpy#13046.

An example where we need such functionality is the moment function, creating both ones and zeros, also with non-default chunks:

return ones(reduced.shape, chunks=reduced.chunks, dtype='f8')

Activity

  1. pentschev commented on Feb 19, 2020

    @pentschev
    MemberAuthor
  2. andersy005 commented on Mar 5, 2020

    @andersy005
    Member

    I took a stab at addressing this issue, but quickly realized that the shape and chunks of a dask array are intertwined.

    • How should the outgoing chunks be handled in cases where the shape is specified and the chunks argument is not?
    • Should it be the user's responsibility to specify the outgoing chunks when the shape argument is specified?
    In [8]: import numpy as np                                                                                         
    
    In [9]: import dask.array as da                                                                                    
    
    In [10]: a = np.arange(6).reshape(3, 2)                                                                            
    
    In [11]: np.ones_like(a, shape=(5, 2))                                                                             
    Out[11]: 
    array([[1, 1],
           [1, 1],
           [1, 1],
           [1, 1],
           [1, 1]])
    
    In [12]: darr = da.from_array(a)                                                                                   
    
    In [13]: darr                                                                                                      
    Out[13]: dask.array<array, shape=(3, 2), dtype=int64, chunksize=(3, 2), chunktype=numpy.ndarray>
    
    In [14]: da.ones_like(darr, shape=(5,2))                                                                           
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-14-4f971820f7f5> in <module>
    ----> 1 da.ones_like(darr, shape=(5,2))
    
    ~/devel/pangeo/dask/dask/array/creation.py in ones_like(a, dtype, chunks, shape)
        106         (shape or a.shape),
        107         dtype=(dtype or a.dtype),
    --> 108         chunks=(chunks if chunks is not None else a.chunks),
        109     )
        110 
    
    ~/devel/pangeo/dask/dask/array/wrap.py in wrap_func_shape_as_first_arg(func, *args, **kwargs)
         60         )
         61 
    ---> 62     parsed = _parse_wrap_args(func, args, kwargs, shape)
         63     shape = parsed["shape"]
         64     dtype = parsed["dtype"]
    
    ~/devel/pangeo/dask/dask/array/wrap.py in _parse_wrap_args(func, args, kwargs, shape)
         29     dtype = np.dtype(dtype)
         30 
    ---> 31     chunks = normalize_chunks(chunks, shape, dtype=dtype)
         32     name = kwargs.pop("name", None)
         33 
    
    ~/devel/pangeo/dask/dask/array/core.py in normalize_chunks(chunks, shape, limit, dtype, previous_chunks)
       2469             raise ValueError(
       2470                 "Chunks do not add up to shape. "
    -> 2471                 "Got chunks=%s, shape=%s" % (chunks, shape)
       2472             )
       2473 
    
    ValueError: Chunks do not add up to shape. Got chunks=((3,), (2,)), shape=(5, 2)
    In [27]: da.ones_like(darr, chunks=(5, 1), shape=(5,2))                                                            
    Out[27]: dask.array<ones, shape=(5, 2), dtype=int64, chunksize=(5, 1), chunktype=numpy.ndarray>
  3. mrocklin commented on Mar 8, 2020

    @mrocklin
    Member

    I don't have answers to your questions. Solving this problem probably also involves coming up with those answers. However, you might want to look at dask.array.core.normalize_chunks, which may have some useful functionality here.

  4. pentschev commented on Feb 15, 2021

    @pentschev
    MemberAuthor

    This has been solved quite a while ago in #6064, thanks @andersy005 !

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