Skip to content

Commit 67edc2a

Browse files
committed
Added tests and workflow, upgraded poetry 2, removed Active storage dependency
1 parent 9d50ea8 commit 67edc2a

File tree

5 files changed

+738
-58
lines changed

5 files changed

+738
-58
lines changed

.github/workflows/ci.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Automatic Test
2+
3+
on:
4+
- push
5+
- pull_request
6+
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
strategy:
11+
fail-fast: false
12+
matrix:
13+
python-version: ["3.9", "3.10", "3.11", "3.12"]
14+
15+
steps:
16+
- name: Checkout repository
17+
uses: actions/checkout@v4
18+
19+
- name: Set up Python ${{ matrix.python-version }}
20+
uses: actions/setup-python@v5
21+
with:
22+
python-version: ${{ matrix.python-version }}
23+
- name: Install Python dependencies
24+
run: |
25+
python -m pip install --upgrade pip
26+
pip3 install poetry
27+
poetry install
28+
- name: Run tests
29+
run: poetry run pytest

arraypartition/partition.py

Lines changed: 88 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,21 @@
1010
from dask.utils import SerializableLock
1111
from dask.array.core import normalize_chunks
1212

13-
try:
14-
from XarrayActive import ActiveChunk
15-
except:
16-
class ActiveChunk:
17-
pass
13+
from typing import Union
1814

1915
class ArrayLike:
2016
"""
2117
Container class for Array-like behaviour
2218
"""
2319
description = 'Container class for Array-Like behaviour'
2420

25-
def __init__(self, shape, units=None, dtype=None, source_shape=None):
21+
def __init__(
22+
self,
23+
shape: tuple,
24+
units: Union[str,None] = None,
25+
dtype: Union[np.dtype,None] = None,
26+
source_shape: Union[tuple,None] = None
27+
):
2628

2729
# Standard parameters to store for array-like behaviour
2830
self.shape = shape
@@ -180,10 +182,11 @@ def get_kwargs(self):
180182
'named_dims': self.named_dims
181183
} | super().get_kwargs()
182184

183-
class ArrayPartition(ActiveChunk, SuperLazyArrayLike):
185+
class ArrayPartition(SuperLazyArrayLike):
184186
"""
185187
Complete Array-like object with all proper methods for data retrieval.
186-
May include methods from ``XarrayActive.ActiveChunk`` if installed."""
188+
189+
Overridden in Active-enabled packages."""
187190

188191
description = "Complete Array-like object with all proper methods for data retrieval."
189192

@@ -640,48 +643,76 @@ def get_dask_chunks(
640643

641644
return normalize_chunks(fsizes_per_dim, shape=array_space, dtype=dtype)
642645

643-
def combine_slices(shape, extent, newslice):
646+
def combine_sliced_dim(
647+
old: slice,
648+
new: slice,
649+
shape: tuple,
650+
dim: int
651+
) -> slice:
652+
"""
653+
Combine slices for a given dimension.
654+
655+
:param old: (slice) Current slice applied to the dimension.
656+
657+
:param new: (slice) New slice to be combined with the old slice.
658+
659+
:param shape: (tuple) The shape of the native array, before
660+
application of any slices.
661+
662+
:param dim: (int) Integer index of the dimension in the array.
644663
"""
645-
Combine existing ``extent`` attribute with a new set of slices.
646664

647-
:param newslice: (tuple) A set of slices to apply to the data
648-
'Super-Lazily', i.e the slices will be combined with existing information
649-
and applied later in the process.
665+
if isinstance(new, int):
666+
new = slice(new, new+1)
650667

651-
:returns: The combined set of slices.
652-
"""
668+
ostart = old.start or 0
669+
ostop = old.stop or shape[dim]
670+
ostep = old.step or 1
653671

654-
if len(newslice) != len(shape):
672+
osize = (ostop - ostart)/ostep
655673

656-
raise ValueError(
657-
"Compute chain broken - dimensions have been reduced already."
674+
nstart = new.start or 0
675+
nstop = new.stop or shape[dim]
676+
nstep = new.step or 1
677+
678+
nsize = (nstop - nstart)/nstep
679+
680+
if nsize > osize:
681+
raise IndexError(
682+
f'Attempted to slice dimension "{dim}" with new slice "({nstart},{nstop},{nstep})'
683+
f'but the dimension size is limited to {osize}.'
658684
)
685+
686+
start = ostart + ostep*nstart
687+
step = ostep * nstep
688+
stop = start + step * (nstop - nstart)
659689

660-
def combine_sliced_dim(old, new, dim):
690+
return slice(start, stop, step)
661691

662-
ostart = old.start or 0
663-
ostop = old.stop or shape[dim]
664-
ostep = old.step or 1
692+
def combine_slices(
693+
shape: tuple[int],
694+
extent: tuple[slice],
695+
newslice: tuple[slice,int]
696+
) -> tuple[slice]:
697+
"""
698+
Combine existing ``extent`` attribute with a new set of slices.
665699
666-
osize = (ostop - ostart)/ostep
700+
:param shape: (tuple) The native source shape of the array.
667701
668-
nstart = new.start or 0
669-
nstop = new.stop or shape[dim]
670-
nstep = new.step or 1
702+
:param extent: (tuple) The current set of slices recorded
703+
for this data selection but not yet applied.
671704
672-
nsize = (nstop - nstart)/nstep
705+
:param newslice: (tuple) A set of slices to apply to the data
706+
'Super-Lazily', i.e the slices will be combined with existing information
707+
and applied later in the process.
673708
674-
if nsize > osize:
675-
raise IndexError(
676-
f'Attempted to slice dimension "{dim}" with new slice "({nstart},{nstop},{nstep})'
677-
f'but the dimension size is limited to {osize}.'
678-
)
709+
:returns: The combined set of slices.
710+
"""
679711

680-
start = ostart + ostep*nstart
681-
step = ostep * nstep
682-
stop = start + step * (nstop - nstart)
683-
684-
return slice(start, stop, step)
712+
if len(newslice) != len(shape):
713+
raise ValueError(
714+
"Compute chain broken - dimensions have been reduced already."
715+
)
685716

686717
if not extent:
687718
return newslice
@@ -691,7 +722,24 @@ def combine_sliced_dim(old, new, dim):
691722
extent[dim] = combine_sliced_dim(extent[dim], newslice[dim], dim)
692723
return extent
693724

694-
def normalize_partition_chunks(chunks, shape, dtype, named_dims):
725+
def normalize_partition_chunks(
726+
chunks: dict,
727+
shape: tuple[int],
728+
dtype: np.dtype,
729+
named_dims: list
730+
):
731+
"""
732+
Prepare information for dask to normalise chunks.
733+
734+
:param chunks: (dict) Dictionary of named dimensions and their number
735+
of partitions across different sources.
736+
737+
:param shape: (tuple) The total shape of the array in all dimensions.
738+
739+
:param dtype: (np.dtype) The data type for this array.
740+
741+
:param named_dims: (list) The set of named dimensions for this array.
742+
"""
695743

696744
chunk_values = []
697745

@@ -704,6 +752,8 @@ def normalize_partition_chunks(chunks, shape, dtype, named_dims):
704752
except ValueError:
705753
chunk_values.append(chunks[nd])
706754

755+
# Construct chunk values for each named dimension
756+
707757
return normalize_chunks(
708758
chunk_values,
709759
shape,
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class TestConsistency:
2+
3+
def test_core(self):
4+
from ArrayPartition import (
5+
ArrayLike,
6+
SuperLazyArrayLike,
7+
ArrayPartition,
8+
get_chunk_space,
9+
get_chunk_shape,
10+
get_chunk_positions,
11+
get_chunk_extent,
12+
get_dask_chunks,
13+
combine_sliced_dim,
14+
combine_slices,
15+
normalize_partition_chunks
16+
)
17+
18+
assert True

0 commit comments

Comments
 (0)