Skip to content

map_blocks can't detect block_id keyword in func(*args, block_id=None) #3081

Closed

Description

@rzu512

Test:

import dask.array as da

def f0(xs, ys, block_id=None):
    print(block_id)
    return xs

def f1(*xs, block_id=None):  # <-- can't do this in python 2.
    print(block_id)
    return xs[0]

a = da.ones((2, 2), chunks=1)
b = 1
r0 = da.map_blocks(f0, a, b, dtype=a.dtype, chunks=a.chunks)
r1 = da.map_blocks(f1, a, b, dtype=a.dtype, chunks=a.chunks)
print("--- result 0 ---")
r0.compute()
print("--- result 1 ---")
r1.compute()

Result:

--- result 0 ---
(0, 0)
(1, 0)
(1, 1)
(0, 1)
--- result 1 ---
None
None
None
None

Software Versions:

Python 3.6.3
Dask 0.16.1

Activity

  1. changed the title [-]da.map_blocks does not pass block_id to func(*args, block_id=None)[/-] [+]getargspec can't detect block_id keyword in func(*args, block_id=None)[/+] on Jan 19, 2018
  2. changed the title [-]getargspec can't detect block_id keyword in func(*args, block_id=None)[/-] [+]map_blocks can't detect block_id keyword in func(*args, block_id=None)[/+] on Jan 19, 2018
  3. rzu512 commented on Jan 19, 2018

    @rzu512
    Author

    Sorry... this seems to be a problem just in map_blocks.
    Fix:

    import dask.array as da
    from dask.compatibility import getargspec
    def func_has_arg(func, argname, kwargs, kwargs_insert='__dummy__'):
        try:
            spec = getargspec(func) 
            print("-- spec --")
            print(spec)
            get_a = getattr(spec, 'kwonly_args', ())
            get_b = getattr(spec, 'kwonlyargs', ())
            print("get_a", get_a)
            print("get_b", get_b)
    
            has = (argname in spec.args or
                   argname in get_a or
                   argname in get_b)  # <-- new
        except Exception:
            has = False
    
        if has:
            kwargs[argname] = kwargs_insert
        return has
    
    def f0(xs, ys, block_id=None):
        print(block_id)
        return xs
    
    def f1(*xs, block_id=None):
        print(block_id)
        return xs[0]
    
    kwargs0 = {}
    print(func_has_arg(f0, 'block_id', kwargs0))
    print(kwargs0)
    kwargs1 = {}
    print(func_has_arg(f1, 'block_id', kwargs1))
    print(kwargs1)

    Result:

    -- spec --
    FullArgSpec(args=['xs', 'ys', 'block_id'], varargs=None, varkw=None, defaults=(None,), kwonlyargs=[], kwonlydefaults=None, annotations={})
    get_a ()
    get_b []
    True
    {'block_id': '__dummy__'}
    -- spec --
    FullArgSpec(args=[], varargs='xs', varkw=None, defaults=None, kwonlyargs=['block_id'], kwonlydefaults={'block_id': None}, annotations={})
    get_a ()
    get_b ['block_id']
    True
    {'block_id': '__dummy__'}
    
  4. rzu512 commented on Jan 19, 2018

    @rzu512
    Author

    Since map_blocks(...) gets the block_id from the key to the Dask graph, finding the block_id doesn't take much time.

    is it a good idea to make da.map_blocks(...) to pass block_id to func(*args, **kwargs)?

    Alternatively can add a send_block_id keyword to map_blocks that overrides the semi-automatic detection of block_id.

  5. mrocklin commented on Jan 19, 2018

    @mrocklin
    Member
  6. rzu512 commented on Jan 19, 2018

    @rzu512
    Author

    For python 2 / 3 compatibility, I usually write:

    def func(*args, **kwargs):
        block_id = kwargs.pop('block_id', None)
        ...

    instead of

    def func(*args, block_id=None):
       ...

    I learned that from the current implementation of map_blocks(...).

    But in the first case, block_id is not a keyword argument to func. I don't think inspect.getfullargspec can detect my intention here.

    That makes me think about either adding a send_block_id keyword to da.map_blocks(...) or send block_id for all func(..., **kwargs).

    Another alternative is to not use *args.

  7. hmaarrfk commented on Sep 17, 2018

    @hmaarrfk
    Contributor

    This PR might be of help.

    #3966

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