示例#1
0
    def _extend(self, other_field, memo) -> None:
        """Add observations from another field"""
        if other_field.data.ndim != self.data.ndim:
            raise ValueError(
                f"Field '{self.name}' cannot be extended. Dimensions must be equal. ({other_field.data.ndim} != {self.data.ndim})"
            )

        try:
            factors = [
                Unit(from_unit, to_unit)
                for from_unit, to_unit in zip(other_field._unit, self._unit)
            ]
        except exceptions.UnitError:
            raise exceptions.UnitError(
                f"Cannot extend field '{self.name}'. {other_field._unit} cannot be converted to {self._unit}"
            )
        except TypeError:
            if self._unit == other_field._unit == None:
                factors = 1
            else:
                raise exceptions.UnitError(
                    f"Cannot extend field '{self.name}'. {other_field._unit} cannot be converted to {self._unit}"
                )
        old_id = id(self.data)
        self.data = np.insert(self.data,
                              self.num_obs,
                              other_field.data * factors,
                              axis=0)
        memo[old_id] = self.data
示例#2
0
 def set_unit(self, subfield, new_unit):
     """Update unit(s) of field"""
     if not subfield:
         self._unit = self._validate_unit(self.data, new_unit)
         self.data.set_unit(self._unit)
     else:
         raise exceptions.UnitError(
             f"Can not change unit of the subfields of a sigma field. Subfields are the same unit as the sigma field"
         )
示例#3
0
文件: constant.py 项目: uasau/midgard
 def unit(self, constant: str) -> str:
     """Unit of constant"""
     try:
         return self._constants[constant].__unit__.str
     except exceptions.MissingSectionError:
         raise exceptions.UnknownConstantError(
             f"Constant {constant!r} is not defined in {', '.join(self._constants.sources)}"
         ) from None
     except exceptions.MissingEntryError:
         raise exceptions.UnitError(
             f"Constant {constant!r} has not defined a unit in {', '.join(self._constants.sources)}"
         ) from None
示例#4
0
    def _get_unit(module_name: str, func_name: str) -> str:
        """Get registered unit of function/method/property

        Outside code should use the `unit_factory` to get registered units.

        Args:
            module_name:   Name of module containing function/method/property.
            func_name:     Name of function/method/property with registered unit.

        Returns:
            Name of unit.
        """
        units = _UNITS.get(module_name, dict())
        try:
            return units[func_name]
        except KeyError:
            raise exceptions.UnitError(
                f"No unit is registered for {func_name!r} in {module_name!r}"
            ) from None
示例#5
0
    def __call__(cls,
                 from_unit: str,
                 to_unit: Optional[str] = None) -> Any:  # type: ignore
        """Calculate the conversion scale between from_unit and to_unit

        If `to_unit` is not given, then `from_unit` is returned as a `pint` Quantity.

        Args:
            from_unit:  The unit to convert from.
            to_unit:    The unit to convert to.

        Returns:
            Scale to multiply by to convert from from_unit to to_unit, or from_unit as a Quantity.
        """
        try:
            if to_unit is None:
                return cls._ureg(from_unit)
            else:
                return cls._ureg(from_unit).to(to_unit).magnitude
        except pint.errors.DimensionalityError as err:
            raise exceptions.UnitError(err)
示例#6
0
 def set_unit(self, subfield, new_unit):
     """Update unit(s) of field"""
     raise exceptions.UnitError(
         f"Can not change the unit of a posvel delta field")
示例#7
0
 def unit(self, _):
     """Unit of fields"""
     raise exceptions.UnitError("Time fields do not have units")
示例#8
0
 def set_unit(self, subfield, new_unit):
     """Update unit of field"""
     if not subfield:
         raise exceptions.UnitError(f"Collections do not have units")
     else:
         super().set_unit(subfield, new_unit)
示例#9
0
 def unit(self, subfield):
     """Unit(s) of field"""
     if not subfield:
         raise exceptions.UnitError(f"Collections do not have units")
     else:
         return super().unit(subfield)