How to use the pynbody.units.UnitsException function in pynbody

To help you get started, we’ve selected a few pynbody 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 pynbody / pynbody / pynbody / units.py View on Github external
for cosmological quantities like 'a' and 'h', but can also
        be used for any IrreducibleUnit.

        >>> Unit("1 Mpc a").ratio("kpc", a=0.25)
        250.0
        >>> Unit("1 Mpc").ratio("Msol")
        UnitsException: not convertible
        >>> Unit("1 Mpc").ratio("Msol", kg=25.0, m=50.0)
        3.1028701506345152e-08
        """

        if isinstance(other, str):
            other = Unit(other)

        if hasattr(other, "_no_unit"):
            raise UnitsException("Unknown units")

        try:
            return (self / other).dimensionless_constant(**substitutions)
        except UnitsException:
            raise UnitsException("Not convertible")
github pynbody / pynbody / pynbody / units.py View on Github external
>>> Unit("1 Mpc").ratio("Msol")
        UnitsException: not convertible
        >>> Unit("1 Mpc").ratio("Msol", kg=25.0, m=50.0)
        3.1028701506345152e-08
        """

        if isinstance(other, str):
            other = Unit(other)

        if hasattr(other, "_no_unit"):
            raise UnitsException("Unknown units")

        try:
            return (self / other).dimensionless_constant(**substitutions)
        except UnitsException:
            raise UnitsException("Not convertible")
github pynbody / pynbody / pynbody / units.py View on Github external
M_T_M_inv = util.rational_matrix_inv(M_T_M)
        except np.linalg.linalg.LinAlgError:
            raise UnitsException("Basis units are not linearly independent")

        my_powers = [me_irrep._power_of(base) for base in bases]

        candidate = np.dot(M_T_M_inv, np.dot(matrix.transpose(), my_powers))

        # Because our method involves a loss of information (multiplying
        # by M^T), we could get a spurious solution. Check this is not the
        # case...

        if any(np.dot(matrix, candidate) != my_powers):
            # Spurious solution, meaning the base vectors did not span the
            # units required in the first place.
            raise UnitsException(
                "Basis units do not span dimensions of specified unit")

        return candidate
github pynbody / pynbody / pynbody / snapshot / tipsy.py View on Github external
# something has gone wrong with the cosmological side of
            # things
            warnings.warn(
                "Paramfile suggests time is cosmological, but header values are not sensible in this context.", RuntimeWarning)
            sim.properties['time'] = sim.properties['a']

        #sim.properties['a'] = t

    else:
        # Assume a non-cosmological run
        sim.properties['time'] = sim.properties['a']

    time_unit = None
    try:
        time_unit = sim.infer_original_units('yr')
    except units.UnitsException:
        pass

    if time_unit is not None:
        sim.properties['time'] *= time_unit
github pynbody / pynbody / pynbody / units.py View on Github external
# If the solution to that does not solve v = M.d, there is no
        # admissable solution to v=M.d, i.e. the supplied base vectors do not
        # span
        # the requires space.
        #
        # If (M^T M) is singular, the vectors are not linearly independent, so
        # any
        # solution would not be unique.
        M_T_M = np.dot(matrix.transpose(), matrix)

        from . import util

        try:
            M_T_M_inv = util.rational_matrix_inv(M_T_M)
        except np.linalg.linalg.LinAlgError:
            raise UnitsException("Basis units are not linearly independent")

        my_powers = [me_irrep._power_of(base) for base in bases]

        candidate = np.dot(M_T_M_inv, np.dot(matrix.transpose(), my_powers))

        # Because our method involves a loss of information (multiplying
        # by M^T), we could get a spurious solution. Check this is not the
        # case...

        if any(np.dot(matrix, candidate) != my_powers):
            # Spurious solution, meaning the base vectors did not span the
            # units required in the first place.
            raise UnitsException(
                "Basis units do not span dimensions of specified unit")

        return candidate
github pynbody / pynbody / pynbody / array.py View on Github external
@_u(np.add)
@_u(np.subtract)
def _consistent_units(a, b):
    a_units, b_units = _get_units_or_none(a, b)
    if a_units is not None and b_units is not None:
        if a_units == b_units:
            return a_units
        else:
            raise units.UnitsException("Incompatible units")

    elif a_units is not None:
        return a_units
    else:
        return b_units
github pynbody / pynbody / pynbody / plot / sph.py View on Github external
def _units_imply_projection(sim, qty, units):
    try:
        sim[qty].units.ratio(units, **sim[qty].conversion_context())
        # if this fails, perhaps we're requesting a projected image?
        return False
    except _units.UnitsException:
        # if the following fails, there's no interpretation this routine
        # can cope with. The error will be allowed to propagate.
        sim[qty].units.ratio(
            units / (sim['x'].units), **sim[qty].conversion_context())
        return True
github pynbody / pynbody / pynbody / array.py View on Github external
@_u(np.power)
def _pow_units(a, b):
    a_units = _get_units_or_none(a)
    if a_units is not None:
        if not isinstance(b, int) and not isinstance(b, units.Fraction):
            raise units.UnitsException("Can't track units")
        return a_units ** b
    else:
        return None