@@ -523,11 +523,16 @@ class IndexingAdapter:
523
523
indexing semantics.
524
524
"""
525
525
526
+ # FIXME: @abstractmethod here
527
+ def get_duck_array ():
528
+ raise NotImplementedError
529
+
526
530
async def async_get_duck_array ():
527
- """These classes are applied to in-memory arrays, so async get isn't needed."""
528
- raise RuntimeError (
529
- f"{ type (self ).__name__ } async_get_duck_array was called. This should not happen."
530
- )
531
+ """These classes are applied to in-memory arrays, so specific async support isn't needed."""
532
+ return self .get_duck_array ()
533
+ # raise RuntimeError(
534
+ # f"{type(self).__name__} async_get_duck_array was called. This should not happen."
535
+ # )
531
536
532
537
533
538
class ExplicitlyIndexedNDArrayMixin (NDArrayMixin , ExplicitlyIndexed ):
@@ -664,37 +669,23 @@ def shape(self) -> _Shape:
664
669
return self ._shape
665
670
666
671
def get_duck_array (self ):
667
- if isinstance (self .array , ExplicitlyIndexedNDArrayMixin ):
668
- array = apply_indexer (self .array , self .key )
669
- else :
670
- # If the array is not an ExplicitlyIndexedNDArrayMixin,
671
- # it may wrap a BackendArray so use its __getitem__
672
- array = self .array [self .key ]
672
+ from xarray .backends .common import BackendArray
673
673
674
- # self.array[self.key] is now a numpy array when
675
- # self.array is a BackendArray subclass
676
- # and self.key is BasicIndexer((slice(None, None, None),))
677
- # so we need the explicit check for ExplicitlyIndexed
678
- if isinstance (array , ExplicitlyIndexed ):
674
+ if isinstance (self .array , BackendArray ):
675
+ array = self .array [self .key ]
676
+ else :
677
+ array = apply_indexer (self .array , self .key )
679
678
array = array .get_duck_array ()
680
679
return _wrap_numpy_scalars (array )
681
680
682
681
async def async_get_duck_array (self ):
683
- if isinstance (self .array , ExplicitlyIndexedNDArrayMixin ):
684
- array = apply_indexer (self .array , self .key )
685
- else :
686
- # If the array is not an ExplicitlyIndexedNDArrayMixin,
687
- # it may wrap a BackendArray so use its (async) getitem
688
- array = await self .array .async_getitem (self .key )
682
+ from xarray .backends .common import BackendArray
689
683
690
- # self.array[self.key] is now a numpy array when
691
- # self.array is a BackendArray subclass
692
- # and self.key is BasicIndexer((slice(None, None, None),))
693
- # so we need the explicit check for ExplicitlyIndexed
694
- if isinstance (array , ExplicitlyIndexed ):
695
- # At this point, we have issued completed the possible async load from disk
696
- # and array is in-memory. So use the sync get
697
- array = array .get_duck_array ()
684
+ if isinstance (self .array , BackendArray ):
685
+ array = await self .array .async_getitem (self .key )
686
+ else :
687
+ array = apply_indexer (self .array , self .key )
688
+ array = await array .async_get_duck_array ()
698
689
return _wrap_numpy_scalars (array )
699
690
700
691
def transpose (self , order ):
@@ -758,36 +749,24 @@ def shape(self) -> _Shape:
758
749
return np .broadcast (* self .key .tuple ).shape
759
750
760
751
def get_duck_array (self ):
761
- if isinstance (self .array , ExplicitlyIndexedNDArrayMixin ):
762
- array = apply_indexer (self .array , self .key )
763
- else :
764
- # If the array is not an ExplicitlyIndexedNDArrayMixin,
765
- # it may wrap a BackendArray so use its __getitem__
752
+ from xarray .backends .common import BackendArray
753
+
754
+ if isinstance (self .array , BackendArray ):
766
755
array = self .array [self .key ]
767
- # self.array[self.key] is now a numpy array when
768
- # self.array is a BackendArray subclass
769
- # and self.key is BasicIndexer((slice(None, None, None),))
770
- # so we need the explicit check for ExplicitlyIndexed
771
- if isinstance (array , ExplicitlyIndexed ):
756
+ else :
757
+ array = apply_indexer (self .array , self .key )
772
758
array = array .get_duck_array ()
773
759
return _wrap_numpy_scalars (array )
774
760
775
761
async def async_get_duck_array (self ):
776
762
print ("inside LazilyVectorizedIndexedArray.async_get_duck_array" )
777
- if isinstance (self .array , ExplicitlyIndexedNDArrayMixin ):
778
- array = apply_indexer (self .array , self .key )
779
- else :
780
- # If the array is not an ExplicitlyIndexedNDArrayMixin,
781
- # it may wrap a BackendArray so use its __getitem__
763
+ from xarray .backends .common import BackendArray
764
+
765
+ if isinstance (self .array , BackendArray ):
782
766
array = await self .array .async_getitem (self .key )
783
- # self.array[self.key] is now a numpy array when
784
- # self.array is a BackendArray subclass
785
- # and self.key is BasicIndexer((slice(None, None, None),))
786
- # so we need the explicit check for ExplicitlyIndexed
787
- if isinstance (array , ExplicitlyIndexed ):
788
- # At this point, we have issued completed the possible async load from disk
789
- # and array is in-memory. So use the sync get
790
- array = array .get_duck_array ()
767
+ else :
768
+ array = apply_indexer (self .array , self .key )
769
+ array = await array .async_get_duck_array ()
791
770
return _wrap_numpy_scalars (array )
792
771
793
772
def _updated_key (self , new_key : ExplicitIndexer ):
0 commit comments