1010from dask .utils import SerializableLock
1111from 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
1915class 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 ,
0 commit comments