Skip to content

Tidy up code for blog post - #1

Merged
VolkerH merged 20 commits into
mainfrom
blog_post
Nov 29, 2021
Merged

VolkerH merged 20 commits into
mainfrom
blog_post

Conversation

@VolkerH

@VolkerH VolkerH commented Nov 1, 2021

Copy link
Copy Markdown
Owner

Tidying up code for dask blog post

  • removed plenty of unused code
  • (hopefully) more consistent and descriptive variable naming
  • intersection calculation between chunks and tiles with shapely srtree structure
  • add fuse_info property to shapely objects so I don't need to pass lots of lists to the function calculating intersections
  • isort and black formatting

@GenevieveBuckley GenevieveBuckley left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! All the explanatory detail at each step is really nice, and all the shapely diagrams are very helpful for understanding what's happening at each step.

I have some comments for you on the DaskFusion_Example.ipynb notebook. I've also looked at the diffs for the non-notebook code, and that all looks reasonable, no comments there.

  1. chardet should added to requirements.txt (see #2)
  2. It's difficult to notice the (0,0) point coordinate in the shapely drawing (code cell 10). You might want to add a sentence describing where to look for it, eg:

    Let's create shapely objects from the numpy arrays that hold the corner points. If we also include the a shapely Point at (0,0) we can visualize where the tiles are with respect to the origin of the joint coordinate system. You can see this point at the bottom left corner of the next diagram."

and/or

"As we can see above, the image tiles are a fair way away from the origin in the bottom left corner."

  1. I get a shapely deprecation warning (code cell 21). I think you need to set custom attributes on the geometry object, so if it's not easy to update the code, then we should add an upper bound to the shapely version specified in requirements.txt. We don't want the notebook to have an error in the future.
    /tmp/ipykernel_77756/2267014653.py:4: ShapelyDeprecationWarning: Setting custom attributes on geometry objects is deprecated, and will raise an AttributeError in Shapely 2.0 tile_shifted_shapely.fuse_info = {'file':file, 'transform':transform}
  2. Newer versions of napari should not need %gui qt in the notebook. We should be able to remove this cell, and the sleep cell after it.

@VolkerH

VolkerH commented Nov 2, 2021

Copy link
Copy Markdown
Owner Author

Thanks, very useful comments. What a shame that shapely will deprecate setting custom attributes. I just thought that would be a neat thing to do (and my version doesn't throw a deprecation warning). I think I might just pin the requirements to a version that doesn't raise this warning for the purposes of this demo.

One question I forgot to ask:

Some of the core action happens in the fuse function which is imported into the notebook.
I was wondering whether this function should be moved into a notebook cell for the purposes of explaining what happens during map_blocks.

Also, one of the peculiar things about functions that get passed to map_blocks is that block_info in the function signature defaults to None. I opened a question on stackoverflow a few days ago: https://stackoverflow.com/questions/69750241/check-whether-block-info-is-not-none-for-functions-passed-to-map-blocks
Somehow I would think not passing a default argument to block_info would be cleaner as we are relying on map_blocks setting this argument.

@GenevieveBuckley

Copy link
Copy Markdown
Collaborator

Some of the core action happens in the fuse function which is imported into the notebook.
I was wondering whether this function should be moved into a notebook cell for the purposes of explaining what happens during map_blocks.

How about including a markdown cell with a copy of the function text, plus the explanation detail you want to draw attention to? That way you can keep fuse defined in its current location (good if people want to use your code like a library later) but also discuss the important parts about how it works in the notebook.

@GenevieveBuckley

Copy link
Copy Markdown
Collaborator

Re: your stackoverflow question
I don't have a stackoverflow account, so I'll respond here (you're welcome to copy-paste this over to stackoverflow)

Is it recommended to include a check to see whether block_info is not None or is it considered ok just to rely on the contract with map_blocks that it will pass in the argument?

I don't think you have to include this check. I'm struggling to see when it would prevent an error in practice.

There's two steps with Dask: building the task graph, then computing the result.
block_info is only None at one point during building the task graph, which is not when the function is actually being run. That's why I think it doesn't really matter if you include a check. When the function actually runs at compute time, block_info is always a dictionary at that point.

import dask.array as da

def func(array_chunk, block_info=None):
    print(f"Is block_info None?: {block_info is None}")
    print(f"block_info: {block_info} \n")
    return array_chunk

arr = da.random.random((100,100), chunks=(50,50))
# Construct Dask task graph
out = arr.map_blocks(func, dtype=arr.dtype)
Is block_info None?: True
block_info: None 

Is block_info None?: False
block_info: [] 
# Compute Dask task graph
# we're using the single-threaded scheduler for easier reasoning about what happens in which order
out.compute(scheduler='single-threaded')
Is block_info None?: False
block_info: {0: {'shape': (100, 100), 'num-chunks': (2, 2), 'array-location': [(0, 50), (0, 50)], 'chunk-location': (0, 0)}, None: {'shape': (100, 100), 'num-chunks': (2, 2), 'array-location': [(0, 50), (0, 50)], 'chunk-location': (0, 0), 'chunk-shape': (50, 50), 'dtype': dtype('float64')}} 

Is block_info None?: False
block_info: {0: {'shape': (100, 100), 'num-chunks': (2, 2), 'array-location': [(0, 50), (50, 100)], 'chunk-location': (0, 1)}, None: {'shape': (100, 100), 'num-chunks': (2, 2), 'array-location': [(0, 50), (50, 100)], 'chunk-location': (0, 1), 'chunk-shape': (50, 50), 'dtype': dtype('float64')}} 

Is block_info None?: False
block_info: {0: {'shape': (100, 100), 'num-chunks': (2, 2), 'array-location': [(50, 100), (0, 50)], 'chunk-location': (1, 0)}, None: {'shape': (100, 100), 'num-chunks': (2, 2), 'array-location': [(50, 100), (0, 50)], 'chunk-location': (1, 0), 'chunk-shape': (50, 50), 'dtype': dtype('float64')}} 

Is block_info None?: False
block_info: {0: {'shape': (100, 100), 'num-chunks': (2, 2), 'array-location': [(50, 100), (50, 100)], 'chunk-location': (1, 1)}, None: {'shape': (100, 100), 'num-chunks': (2, 2), 'array-location': [(50, 100), (50, 100)], 'chunk-location': (1, 1), 'chunk-shape': (50, 50), 'dtype': dtype('float64')}} 

array([[0.9836907 , 0.11517323, 0.5308961 , ...

Alternatively, would it be better to just leave out the default argument None?

I don't quite understand what you are asking in this part of your question. Leave it out from where?

@GenevieveBuckley

Copy link
Copy Markdown
Collaborator

tbh one thing I don't fully understand is why the block_info dictionary at the time of compute has keys for 0 and None. As far as I can tell, the content is duplicated. It might not matter all that much, part of the reason I still don't understand it is that I stopped looking at it once I had something that solved my problem. Also, I think the block_info part of the code was written fairly early on and may not have been changed or refined much after that, since it's a relatively underused part of the code base.

@GenevieveBuckley

Copy link
Copy Markdown
Collaborator

FYI, Ian is starting a new dask discourse forum here: https://dask.discourse.group/
I think the eventual idea is that it will be a bit more active than stackoverflow (which doesn't really have maintainers who check that much anymore). We'll see how that goes, but just wanted to mention it.

@m-albert

m-albert commented Nov 3, 2021 •

Copy link
Copy Markdown
Collaborator

tbh one thing I don't fully understand is why the block_info dictionary at the time of compute has keys for 0 and None. As far as I can tell, the content is duplicated. It might not matter all that much, part of the reason I still don't understand it is that I stopped looking at it once I had something that solved my problem. Also, I think the block_info part of the code was written fairly early on and may not have been changed or refined much after that, since it's a relatively underused part of the code base.

Was wondering about that too at first and then figured out that block_info[0] contains info about the input chunks and block_info[None] about the output chunks. This can be super useful when the chunk shape is changed by map_blocks.

@GenevieveBuckley

Copy link
Copy Markdown
Collaborator

tbh one thing I don't fully understand is why the block_info dictionary at the time of compute has keys for 0 and None. As far as I can tell, the content is duplicated. It might not matter all that much, part of the reason I still don't understand it is that I stopped looking at it once I had something that solved my problem. Also, I think the block_info part of the code was written fairly early on and may not have been changed or refined much after that, since it's a relatively underused part of the code base.

Was wondering about that too at first and then figured out that block_info[0] contains info about the input chunks and block_info[None] about the output chunks. This can be super useful when the chunk shape is changed by map_blocks.

Ooooh, ok!
That's very useful to know. I'm gonna open up an issue and try to get that clarification added to the Dask docs. No wonder I never figured it out, all of my test examples generally use the same input & output chunksize.

@VolkerH

VolkerH commented Nov 29, 2021

Copy link
Copy Markdown
Owner Author

I implemented your suggestions, @GenevieveBuckley

  • added chardet
  • pinned shapely version to avoid DeprecationWarnings
  • point out where to look for the origin
  • removed %qui qt, sleep(1)

I also expanded the explanations for fuse_func and map_blocks.

Will merge this now.

@VolkerH
VolkerH merged commit 840c756 into main Nov 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants