How to use the plonk._units.Quantity function in plonk

To help you get started, we’ve selected a few plonk examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dmentipl / plonk / plonk / visualize / interpolation.py View on Github external
"""Get array in code units.

    Parameters
    ----------
    snap
        The Snap or SubSnap.
    name
        The array name.

    Returns
    -------
    ndarray
        The array on the particles in code units.
    """
    arr = snap[name]
    if isinstance(arr, Quantity):
        return (arr / snap.get_array_unit(name)).magnitude
    return arr
github dmentipl / plonk / plonk / analysis / profile.py View on Github external
def _set_range_physical_units(
        self, radius_min: Optional[Any], radius_max: Optional[Any]
    ) -> Tuple[float, float]:
        if radius_min is None:
            rmin = self._x.min()
        else:
            rmin = Quantity(radius_min)
            if not rmin.dimensionality == Quantity('cm').dimensionality:
                raise ValueError(
                    'Snap has physical units: must use dimensional radius_min'
                )
            rmin = rmin.to_base_units()
        if radius_max is None:
            rmax = np.percentile(self._x.magnitude, 99, axis=0) * self._x.units
        else:
            rmax = Quantity(radius_max)
            if not rmax.dimensionality == Quantity('cm').dimensionality:
                raise ValueError(
                    'Snap has physical units: must use dimensional radius_max'
                )
            rmax = rmax.to_base_units()

        return rmin, rmax
github dmentipl / plonk / plonk / utils / math.py View on Github external
"""Cross product.

    Parameters
    ----------
    x, y
        The two arrays (N, 3) to take the cross product of. Can be
        ndarray or pint Quantity.
    **kwargs
        Keyword arguments to pass to np.cross.

    Returns
    -------
    ndarray
        The cross product of x and y.
    """
    if isinstance(x, Quantity):
        return np.cross(x.magnitude, y.magnitude, **kwargs) * x.units * y.units
    return np.cross(x, y, **kwargs)
github dmentipl / plonk / plonk / analysis / profile.py View on Github external
def _set_range_physical_units(
        self, radius_min: Optional[Any], radius_max: Optional[Any]
    ) -> Tuple[float, float]:
        if radius_min is None:
            rmin = self._x.min()
        else:
            rmin = Quantity(radius_min)
            if not rmin.dimensionality == Quantity('cm').dimensionality:
                raise ValueError(
                    'Snap has physical units: must use dimensional radius_min'
                )
            rmin = rmin.to_base_units()
        if radius_max is None:
            rmax = np.percentile(self._x.magnitude, 99, axis=0) * self._x.units
        else:
            rmax = Quantity(radius_max)
            if not rmax.dimensionality == Quantity('cm').dimensionality:
                raise ValueError(
                    'Snap has physical units: must use dimensional radius_max'
                )
            rmax = rmax.to_base_units()

        return rmin, rmax
github dmentipl / plonk / plonk / analysis / profile.py View on Github external
self, radius_min: Optional[Any], radius_max: Optional[Any]
    ) -> Tuple[float, float]:
        if radius_min is None:
            rmin = self._x.min()
        else:
            rmin = Quantity(radius_min)
            if not rmin.dimensionality == Quantity('cm').dimensionality:
                raise ValueError(
                    'Snap has physical units: must use dimensional radius_min'
                )
            rmin = rmin.to_base_units()
        if radius_max is None:
            rmax = np.percentile(self._x.magnitude, 99, axis=0) * self._x.units
        else:
            rmax = Quantity(radius_max)
            if not rmax.dimensionality == Quantity('cm').dimensionality:
                raise ValueError(
                    'Snap has physical units: must use dimensional radius_max'
                )
            rmax = rmax.to_base_units()

        return rmin, rmax
github dmentipl / plonk / plonk / visualize / visualization.py View on Github external
if ax is None:
        fig, ax = plt.subplots()
    else:
        fig = ax.figure

    if extent == (-1, -1, -1, -1):
        extent = get_extent_from_percentile(snap=snap, x=x, y=y)
    if isinstance(extent[0], Quantity):
        extent = (
            (extent[0] / snap.units['length']).to_base_units().magnitude,
            (extent[1] / snap.units['length']).to_base_units().magnitude,
            (extent[2] / snap.units['length']).to_base_units().magnitude,
            (extent[3] / snap.units['length']).to_base_units().magnitude,
        )
    if isinstance(z_slice, Quantity):
        z_slice = (z_slice / snap.units['length']).to_base_units().magnitude

    interpolation_kwargs = ('number_of_pixels', 'density_weighted')
    __kwargs = {key: val for key, val in _kwargs.items() if key in interpolation_kwargs}
    for key in __kwargs:
        _kwargs.pop(key)
    interpolated_data = interpolate(
        snap=snap,
        quantity=quantity,
        x=x,
        y=y,
        interp=interp,
        z_slice=z_slice,
        extent=extent,
        **__kwargs,
    )
github dmentipl / plonk / plonk / analysis / total.py View on Github external
Parameters
    ----------
    snap
        The Snap object.
    ignore_accreted : optional
        Ignore accreted particles. Default is True.

    Returns
    -------
    float
        The mean inclination.
    """
    angmom = angular_momentum(snap=snap, ignore_accreted=ignore_accreted)
    if isinstance(angmom, Quantity):
        pi_2 = np.pi / 2 * Quantity('radian')
    else:
        pi_2 = np.pi / 2
    return np.arctan2(angmom[1], angmom[0]) + pi_2
github dmentipl / plonk / plonk / utils / math.py View on Github external
Parameters
    ----------
    x
        The array (N,) to take the norm of. Can be ndarray or pint
        Quantity.
    weights
        The weights for averaging.
    **kwargs
        Keyword arguments to pass to np.average.

    Returns
    -------
    ndarray
        The average of x.
    """
    if isinstance(x, Quantity):
        return np.average(x.magnitude, weights=weights.magnitude, **kwargs) * x.units
    return np.average(x, weights=weights, **kwargs)
github dmentipl / plonk / plonk / analysis / profile.py View on Github external
def _set_range_physical_units(
        self, radius_min: Optional[Any], radius_max: Optional[Any]
    ) -> Tuple[float, float]:
        if radius_min is None:
            rmin = self._x.min()
        else:
            rmin = Quantity(radius_min)
            if not rmin.dimensionality == Quantity('cm').dimensionality:
                raise ValueError(
                    'Snap has physical units: must use dimensional radius_min'
                )
            rmin = rmin.to_base_units()
        if radius_max is None:
            rmax = np.percentile(self._x.magnitude, 99, axis=0) * self._x.units
        else:
            rmax = Quantity(radius_max)
            if not rmax.dimensionality == Quantity('cm').dimensionality:
                raise ValueError(
                    'Snap has physical units: must use dimensional radius_max'
                )
            rmax = rmax.to_base_units()

        return rmin, rmax