2
2
import bisect
3
3
import abc
4
4
5
+ from ._subset import _is_single_subset_noop
6
+
5
7
6
8
class AbstractGrid (abc .ABC ):
7
9
"""
@@ -39,7 +41,7 @@ def transpose(self, perm: Tuple[int, ...]) -> "AbstractGrid":
39
41
40
42
41
43
@abc .abstractmethod
42
- def subset (self , subset : Tuple [Optional [ Sequence [int ] ], ...]) -> "AbstractGrid" :
44
+ def subset (self , subset : Tuple [Sequence [int ], ...]) -> "AbstractGrid" :
43
45
pass
44
46
45
47
@@ -169,7 +171,7 @@ def transpose(self, perm: Tuple[int, ...]) -> "SimpleGrid":
169
171
)
170
172
171
173
172
- def subset (self , subset : Tuple [Optional [ Sequence [int ] ], ...]) -> "SimpleGrid" :
174
+ def subset (self , subset : Tuple [Sequence [int ], ...]) -> "SimpleGrid" :
173
175
"""
174
176
Subset a grid to reflect the same operation on the associated array.
175
177
For any given dimension, consecutive elements in the subset are only
@@ -195,7 +197,7 @@ def subset(self, subset: Tuple[Optional[Sequence[int]], ...]) -> "SimpleGrid":
195
197
new_maxgap = []
196
198
for i , bounds in enumerate (self ._boundaries ):
197
199
cursub = subset [i ]
198
- if cursub is None :
200
+ if _is_single_subset_noop ( self . _shape [ i ], cursub ) :
199
201
new_boundaries .append (bounds )
200
202
new_shape .append (self ._shape [i ])
201
203
new_maxgap .append (self ._maxgap [i ])
@@ -504,7 +506,7 @@ def transpose(self, perm: Tuple[int, ...]) -> "CompositeGrid":
504
506
)
505
507
506
508
507
- def subset (self , subset : Tuple [Optional [ Sequence [int ] ], ...]) -> "CompositeGrid" :
509
+ def subset (self , subset : Tuple [Sequence [int ], ...]) -> "CompositeGrid" :
508
510
"""
509
511
Subset a grid to reflect the same operation on the associated array.
510
512
This splits up the subset sequence for the ``along`` dimension and
@@ -515,14 +517,19 @@ def subset(self, subset: Tuple[Optional[Sequence[int]], ...]) -> "CompositeGrid"
515
517
Tuple of length equal to the number of grid dimensions. Each
516
518
entry should be a (possibly unsorted) sequence of integers,
517
519
specifying the subset to apply to each dimension of the grid.
518
- Alternatively, an entry may be None if no subsetting is to be
519
- applied to the corresponding dimension.
520
520
521
521
Returns:
522
522
A new ``CompositeGrid`` object.
523
523
"""
524
- if subset [self ._along ] is None :
525
- new_components = [grid .subset (subset ) for grid in self ._components ]
524
+ if len (subset ) != len (self ._shape ):
525
+ raise ValueError ("'shape' and 'subset' should have the same length" )
526
+
527
+ if _is_single_subset_noop (self ._shape [self ._along ], subset [self ._along ]):
528
+ new_components = []
529
+ new_subset = list (subset )
530
+ for grid in self ._components :
531
+ new_subset [self ._along ] = range (grid .shape [self ._along ])
532
+ new_components .append (grid .subset ((* new_subset ,)))
526
533
return CompositeGrid (new_components , self ._along )
527
534
528
535
component_limits = []
0 commit comments