Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _getitem(
self, inp: Union[str, ndarray, int, slice], sinks: bool = False,
) -> Union[ndarray, SubSnap]:
"""Return an array, or family, or subset."""
if isinstance(inp, str):
return self._getitem_from_str(inp, sinks)
if sinks:
raise ValueError('Cannot return sinks as SubSnap')
if isinstance(inp, ndarray):
if np.issubdtype(np.bool, inp.dtype):
return SubSnap(self, np.flatnonzero(inp))
if np.issubdtype(np.int, inp.dtype):
return SubSnap(self, inp)
if isinstance(inp, int):
return SubSnap(self, np.array([inp]))
if isinstance(inp, slice):
i1, i2, step = inp.start, inp.stop, inp.step
if i1 is None:
i1 = 0
if i2 is None:
i2 = len(self)
if step is not None:
return SubSnap(self, np.arange(i1, i2, step))
return SubSnap(self, np.arange(i1, i2))
if self._extra_quantities:
raise ValueError('Cannot determine item to return.')
raise ValueError(
'Cannot determine item to return. Extra quantities are available via\n'
'snap.extra_quantities().'
def _get_family_subsnap(self, name: str):
"""Get a family by name."""
if name in self._families:
if self._families[name] is None:
ind = self.particle_indices(name)
if len(ind) == 0:
raise ValueError(f'No {name} particles available')
self._families[name] = ind
ind = self._families[name]
if isinstance(ind, list):
return [SubSnap(self, _ind) for _ind in ind]
return SubSnap(self, ind)
raise ValueError('Family not available')
def _get_family_subsnap(self, name: str):
"""Get a family by name."""
if name in self._families:
if self._families[name] is None:
ind = self.particle_indices(name)
if len(ind) == 0:
raise ValueError(f'No {name} particles available')
self._families[name] = ind
ind = self._families[name]
if isinstance(ind, list):
return [SubSnap(self, _ind) for _ind in ind]
return SubSnap(self, ind)
raise ValueError('Family not available')
if isinstance(inp, ndarray):
if np.issubdtype(np.bool, inp.dtype):
return SubSnap(self, np.flatnonzero(inp))
if np.issubdtype(np.int, inp.dtype):
return SubSnap(self, inp)
if isinstance(inp, int):
return SubSnap(self, np.array([inp]))
if isinstance(inp, slice):
i1, i2, step = inp.start, inp.stop, inp.step
if i1 is None:
i1 = 0
if i2 is None:
i2 = len(self)
if step is not None:
return SubSnap(self, np.arange(i1, i2, step))
return SubSnap(self, np.arange(i1, i2))
if self._extra_quantities:
raise ValueError('Cannot determine item to return.')
raise ValueError(
'Cannot determine item to return. Extra quantities are available via\n'
'snap.extra_quantities().'
def _getitem(
self, inp: Union[str, ndarray, int, slice], sinks: bool = False,
) -> Union[ndarray, SubSnap]:
"""Return an array, or family, or subset."""
if isinstance(inp, str):
return self._getitem_from_str(inp, sinks)
if sinks:
raise ValueError('Cannot return sinks as SubSnap')
if isinstance(inp, ndarray):
if np.issubdtype(np.bool, inp.dtype):
return SubSnap(self, np.flatnonzero(inp))
if np.issubdtype(np.int, inp.dtype):
return SubSnap(self, inp)
if isinstance(inp, int):
return SubSnap(self, np.array([inp]))
if isinstance(inp, slice):
i1, i2, step = inp.start, inp.stop, inp.step
if i1 is None:
i1 = 0
if i2 is None:
i2 = len(self)
if step is not None:
return SubSnap(self, np.arange(i1, i2, step))
return SubSnap(self, np.arange(i1, i2))
if self._extra_quantities:
raise ValueError('Cannot determine item to return.')
raise ValueError(
'Cannot determine item to return. Extra quantities are available via\n'
'snap.extra_quantities().'