-
Notifications
You must be signed in to change notification settings - Fork 54
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
Refactor index types #405
Draft
rabernat
wants to merge
14
commits into
pangeo-forge:beam-refactor
Choose a base branch
from
rabernat:refactor-index-types
base: beam-refactor
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
Refactor index types #405
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
631e464
first pass working rechunking
rabernat a95cc36
new test wip
rabernat d459db3
improve rechuking test
rabernat 4ca3076
multidim rechunking test
rabernat afa6e97
change find_concat_dim behavior
rabernat e82561d
make split_fragment preserve index elements not involved in the rechu…
rabernat 69d3186
first working combine_fragments
rabernat f9a8e14
parametrized combine test
rabernat 957e3bd
add a bunch of comments to rechunking.py
rabernat 0ee5069
starting to refactor index types
rabernat f035f60
fix combiners
rabernat ccbb967
fix writers
rabernat 40efd9a
Merge branch 'beam-refactor' into refactor-index-types
rabernat a376536
temporarily remove rechunking stuff to make review easier
rabernat 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
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
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
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from dataclasses import dataclass | ||
from enum import Enum | ||
from typing import Dict, Optional | ||
|
||
|
||
class CombineOp(Enum): | ||
"""Used to uniquely identify different combine operations across Pangeo Forge Recipes.""" | ||
|
||
MERGE = 1 | ||
CONCAT = 2 | ||
SUBSET = 3 | ||
|
||
|
||
@dataclass(frozen=True, order=True) | ||
class Dimension: | ||
""" | ||
:param name: The name of the dimension we are combining over. | ||
:param operation: What type of combination this is (merge or concat) | ||
""" | ||
|
||
name: str | ||
operation: CombineOp | ||
|
||
|
||
@dataclass(frozen=True, order=True) | ||
class Position: | ||
""" | ||
:param indexed: If True, this position represents an offset within a dataset | ||
If False, it is a position within a sequence. | ||
""" | ||
|
||
value: int | ||
# TODO: consider using a ClassVar here | ||
indexed: bool = False | ||
|
||
|
||
@dataclass(frozen=True, order=True) | ||
class IndexedPosition(Position): | ||
indexed: bool = True | ||
|
||
|
||
class Index(Dict[Dimension, Position]): | ||
"""An Index is a special sort of dictionary which describes a position within | ||
a multidimensional set. | ||
|
||
- The key is a :class:`Dimension` which tells us which dimension we are addressing. | ||
- The value is a :class:`Position` which tells us where we are within that dimension. | ||
|
||
This object is hashable and deterministically serializable. | ||
""" | ||
|
||
def __hash__(self): | ||
return hash(tuple(self.__getstate__())) | ||
|
||
def __getstate__(self): | ||
return sorted(self.items()) | ||
|
||
def __setstate__(self, state): | ||
self.__init__({k: v for k, v in state}) | ||
|
||
def find_concat_dim(self, dim_name: str) -> Optional[Dimension]: | ||
possible_concat_dims = [ | ||
d for d in self if (d.name == dim_name and d.operation == CombineOp.CONCAT) | ||
] | ||
if len(possible_concat_dims) > 1: | ||
raise ValueError( | ||
f"Found {len(possible_concat_dims)} concat dims named {dim_name} " | ||
f"in the index {self}." | ||
) | ||
elif len(possible_concat_dims) == 0: | ||
return None | ||
else: | ||
return possible_concat_dims[0] |
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 |
---|---|---|
|
@@ -10,12 +10,14 @@ | |
def _region_for(var: xr.Variable, index: Index) -> Tuple[slice, ...]: | ||
region_slice = [] | ||
for dim, dimsize in var.sizes.items(): | ||
concat_dim_val = index.find_concat_dim(dim) | ||
if concat_dim_val: | ||
concat_dimension = index.find_concat_dim(dim) | ||
if concat_dimension: | ||
# we are concatenating over this dimension | ||
assert concat_dim_val.start is not None | ||
assert concat_dim_val.stop == concat_dim_val.start + dimsize | ||
region_slice.append(slice(concat_dim_val.start, concat_dim_val.stop)) | ||
position = index[concat_dimension] | ||
assert position.indexed | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be useful to have an assert message, maybe with some message to help end-users debug. |
||
start = position.value | ||
stop = start + dimsize | ||
region_slice.append(slice(start, stop)) | ||
else: | ||
# we are writing the entire dimension | ||
region_slice.append(slice(None)) | ||
|
@@ -36,15 +38,15 @@ def _store_data(vname: str, var: xr.Variable, index: Index, zgroup: zarr.Group) | |
|
||
def _is_first_item(index): | ||
for _, v in index.items(): | ||
if v.position > 0: | ||
if v.value > 0: | ||
return False | ||
return True | ||
|
||
|
||
def _is_first_in_merge_dim(index): | ||
for k, v in index.items(): | ||
if k.operation == CombineOp.MERGE: | ||
if v.position > 0: | ||
if v.value > 0: | ||
return False | ||
return True | ||
|
||
|
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.
My personal preference is
{dimension!r}
.