Ejemplo n.º 1
0
    def __array_ufunc__(self, function, method, *inputs, **kwargs):
        # TODO: it would be more logical to have this in Quantity already,
        # instead of in UFUNC_HELPERS, where it cannot be overridden.
        # And really it should just return NotImplemented, since possibly
        # another argument might know what to do.
        if function not in self._supported_ufuncs:
            raise UnitTypeError(
                "Cannot use ufunc '{0}' with function quantities".format(
                    function.__name__))

        return super().__array_ufunc__(function, method, *inputs, **kwargs)
Ejemplo n.º 2
0
    def _set_unit(self, unit):
        if not isinstance(unit, self._unit_class):
            # Have to take care of, e.g., (10*u.mag).view(u.Magnitude)
            try:
                # "or 'nonsense'" ensures `None` breaks, just in case.
                unit = self._unit_class(function_unit=unit or 'nonsense')
            except Exception:
                raise UnitTypeError(
                    "{0} instances require {1} function units".format(
                        type(self).__name__, self._unit_class.__name__) +
                    ", so cannot set it to '{0}'.".format(unit))

        self._unit = unit
Ejemplo n.º 3
0
    def __rtruediv__(self, other):
        if self.unit.physical_unit == dimensionless_unscaled:
            return self._function_view.__rdiv__(other)

        raise UnitTypeError("Cannot divide function quantities which "
                            "are not dimensionless into anything.")
Ejemplo n.º 4
0
    def __mul__(self, other):
        if self.unit.physical_unit == dimensionless_unscaled:
            return self._function_view * other

        raise UnitTypeError("Cannot multiply function quantities which "
                            "are not dimensionless with anything.")
Ejemplo n.º 5
0
def check_param_values(model, **user_param):
    """Performs generic check that the requested model parameters have valid values and units for the requested
    SNEWPY model.
    Parameters
    ----------
    model : snewpy.model.SupernovaModel
        Model class used to perform parameter check
    user_param : varies
        User-requested model parameters to be tested for validity. MUST be provided as keyword arguments that match the
        model `param` class member
    Raises
    ------
    ValueError
        If invalid model parameters are provided based on units, allowed values, etc.
    UnitTypeError
        If invalid units are provided for a model parameter
    See Also
    --------
    snewpy.models.ccsn
    snewpy.models.presn
    """
    model_param = model.param
    # Check that the appropriate number of params are provided
    if len(user_param) != len(model_param):
        raise ValueError(
            f"Invalid model parameters, expected {len(model_param)} "
            f"but {len(user_param)} were given")

    # Check that user-requested params have valid units and values
    for (key, allowed_params), user_param in zip(model_param.items(),
                                                 user_param.values()):
        # If both have units, check that the user param value is valid. If valid, continue. Else, error
        if type(user_param) == Quantity and type(allowed_params) == Quantity:
            if get_physical_type(user_param.unit) != get_physical_type(
                    allowed_params.unit):
                raise UnitTypeError(
                    f"Incorrect units {user_param.unit} provided for parameter {key}, "
                    f"expected {allowed_params.unit}")
            elif user_param.to(
                    allowed_params.unit).value in allowed_params.value:
                continue
            else:
                raise ValueError(
                    f"Invalid value '{user_param}' provided for parameter {key}, "
                    f"allowed value(s): {allowed_params}")

        # If one only one has units, then error
        elif (type(user_param) == Quantity) ^ (type(allowed_params)
                                               == Quantity):
            # User param has units, model param is unitless
            if type(user_param) == Quantity:
                raise ValueError(
                    f"Invalid units {user_param.unit} for parameter {key} provided, expected None"
                )
            else:
                raise ValueError(
                    f"Missing units for parameter {key}, expected {allowed_params.unit}"
                )

        # Check that unitless user param value is valid. If valid, continue. Else, Error
        elif user_param in allowed_params:
            continue
        else:
            raise ValueError(
                f"Invalid value '{user_param}' provided for parameter {key}, "
                f"allowed value(s): {allowed_params}")