-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simpler Dask array construction, w/ optional batching #462
Draft
svank
wants to merge
6
commits into
DKISTDC:main
Choose a base branch
from
svank:making-the-array
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
83a4751
Build dask array more directly
svank ccfc600
Try handling non-default chunksizes
svank 15d08ae
Move final shape responsibility into dask_utils
svank d376bea
Docstring
svank 4a6551d
Removing 'batching' idea, which isn't giving me any speedups
svank 4a3fd5f
Make sure loaded files have a leading dimension
svank File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,57 @@ | ||
from functools import partial | ||
|
||
import dask.array as da | ||
import dask | ||
import numpy as np | ||
|
||
__all__ = ["stack_loader_array"] | ||
|
||
|
||
def stack_loader_array(loader_array, chunksize): | ||
def stack_loader_array(loader_array, output_shape, chunksize=None): | ||
""" | ||
Stack a loader array along each of its dimensions. | ||
Converts an array of loaders to a dask array that loads a chunk from each loader | ||
|
||
This results in a dask array with the correct chunks and dimensions. | ||
|
||
Parameters | ||
---------- | ||
loader_array : `dkist.io.reference_collections.BaseFITSArrayContainer` | ||
loader_array : `dkist.io.loaders.BaseFITSLoader` | ||
An array of loader objects | ||
output_shape : tuple[int] | ||
The intended shape of the final array | ||
chunksize : tuple[int] | ||
Can be used to set a chunk size. If not provided, each batch is one chunk | ||
|
||
Returns | ||
------- | ||
array : `dask.array.Array` | ||
""" | ||
# If the chunksize isn't specified then use the whole array shape | ||
chunksize = chunksize or loader_array.flat[0].shape | ||
|
||
if loader_array.size == 1: | ||
return tuple(loader_to_dask(loader_array, chunksize))[0] | ||
if len(loader_array.shape) == 1: | ||
return da.stack(loader_to_dask(loader_array, chunksize)) | ||
stacks = [] | ||
for i in range(loader_array.shape[0]): | ||
stacks.append(stack_loader_array(loader_array[i], chunksize)) | ||
return da.stack(stacks) | ||
|
||
|
||
def _partial_to_array(loader, *, meta, chunks): | ||
# Set the name of the array to the filename, that should be unique within the array | ||
return da.from_array(loader, meta=meta, chunks=chunks, name=loader.fileuri) | ||
|
||
|
||
def loader_to_dask(loader_array, chunksize): | ||
""" | ||
Map a call to `dask.array.from_array` onto all the elements in ``loader_array``. | ||
|
||
This is done so that an explicit ``meta=`` argument can be provided to | ||
prevent loading data from disk. | ||
""" | ||
if loader_array.size != 1 and len(loader_array.shape) != 1: | ||
raise ValueError("Can only be used on one dimensional arrays") | ||
|
||
loader_array = np.atleast_1d(loader_array) | ||
|
||
# The meta argument to from array is used to determine properties of the | ||
# array, such as dtype. We explicitly specify it here to prevent dask | ||
# trying to auto calculate it by reading from the actual array on disk. | ||
meta = np.zeros((0,), dtype=loader_array[0].dtype) | ||
|
||
to_array = partial(_partial_to_array, meta=meta, chunks=chunksize) | ||
|
||
return map(to_array, loader_array) | ||
file_shape = loader_array.flat[0].shape | ||
|
||
tasks = {} | ||
for i, loader in enumerate(loader_array.flat): | ||
# The key identifies this chunk's position in the (partially-flattened) final data cube | ||
key = ("load_files", i) | ||
key += (0,) * len(file_shape) | ||
# Each task will be to call _call_loader, with the loader as an argument | ||
tasks[key] = (_call_loader, loader) | ||
|
||
dsk = dask.highlevelgraph.HighLevelGraph.from_collections("load_files", tasks, dependencies=()) | ||
# Specifies that each chunk occupies a space of 1 pixel in the first dimension, and all the pixels in the others | ||
chunks = ((1,) * loader_array.size,) + tuple((s,) for s in file_shape) | ||
array = dask.array.Array(dsk, | ||
name="load_files", | ||
chunks=chunks, | ||
dtype=loader_array.flat[0].dtype) | ||
# Now impose the higher dimensions on the data cube | ||
array = array.reshape(output_shape) | ||
if chunksize is not None: | ||
# If requested, re-chunk the array. Not sure this is optimal | ||
new_chunks = (1,) * (array.ndim - len(chunksize)) + chunksize | ||
array = array.rechunk(new_chunks) | ||
return array | ||
|
||
|
||
def _call_loader(loader): | ||
data = loader.data | ||
# The data needs an extra dimension for the leading index of the intermediate data cube, which has a leading | ||
# index for file number | ||
data = np.expand_dims(data, 0) | ||
return data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is only true for some arrays? i.e. VISP and not VBI?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think at this stage, the array is just
(n_chunks, *chunk_size)
, and a few lines down the actual data cube shape is imposed. I think that approach seemed easier that figuring out how to assemble the loaders into the actual data cube shape from the beginning.