MCPcopy Index your code
hub / github.com/pydata/xarray / _broadcast_indexes_vectorized

Method _broadcast_indexes_vectorized

xarray/core/variable.py:757–813  ·  view source on GitHub ↗
(self, key)

Source from the content-addressed store, hash-verified

755 return dims, OuterIndexer(tuple(new_key)), None
756
757 def _broadcast_indexes_vectorized(self, key):
758 variables = []
759 out_dims_set = OrderedSet()
760 for dim, value in zip(self.dims, key, strict=True):
761 if isinstance(value, slice):
762 out_dims_set.add(dim)
763 else:
764 variable = (
765 value
766 if isinstance(value, Variable)
767 else as_variable(value, name=dim, auto_convert=False)
768 )
769 if variable.dims == (dim,):
770 variable = variable.to_index_variable()
771 if variable.dtype.kind == "b": # boolean indexing case
772 (variable,) = variable._nonzero()
773
774 variables.append(variable)
775 out_dims_set.update(variable.dims)
776
777 variable_dims = set()
778 for variable in variables:
779 variable_dims.update(variable.dims)
780
781 slices = []
782 for i, (dim, value) in enumerate(zip(self.dims, key, strict=True)):
783 if isinstance(value, slice):
784 if dim in variable_dims:
785 # We only convert slice objects to variables if they share
786 # a dimension with at least one other variable. Otherwise,
787 # we can equivalently leave them as slices and transpose
788 # the result. This is significantly faster/more efficient
789 # for most array backends.
790 values = np.arange(*value.indices(self.sizes[dim]))
791 variables.insert(i - len(slices), Variable((dim,), values))
792 else:
793 slices.append((i, value))
794
795 try:
796 variables = _broadcast_compat_variables(*variables)
797 except ValueError as err:
798 raise IndexError(f"Dimensions of indexers mismatch: {key}") from err
799
800 out_key = [variable.data for variable in variables]
801 out_dims = tuple(out_dims_set)
802 slice_positions = set()
803 for i, value in slices:
804 out_key.insert(i, value)
805 new_position = out_dims.index(self.dims[i])
806 slice_positions.add(new_position)
807
808 if slice_positions:
809 new_order = [i for i in range(len(out_dims)) if i not in slice_positions]
810 else:
811 new_order = None
812
813 return out_dims, VectorizedIndexer(tuple(out_key)), new_order
814

Calls 12

addMethod · 0.95
updateMethod · 0.95
OrderedSetClass · 0.90
VectorizedIndexerClass · 0.90
as_variableFunction · 0.85
_nonzeroMethod · 0.80
arangeMethod · 0.80
VariableClass · 0.70
to_index_variableMethod · 0.45
updateMethod · 0.45
indexMethod · 0.45