histogram_base.py 文件源码

python
阅读 46 收藏 0 点赞 0 评论 0

项目:physt 作者: janpipek 项目源码 文件源码
def set_dtype(self, value, check=True):
        """Change data type of the bin contents.

        Allowed conversions:
        - from integral to float types
        - between the same category of type (float/integer)
        - from float types to integer if weights are trivial

        Parameters
        ----------
        value: np.dtype or something convertible to it.
        check: bool
            If True (default), all values are checked against the limits
        """

        # TODO: Refactor out?
        # TODO? Deal with unsigned types
        value = np.dtype(value)

        if value == self.dtype:
            return    # No change

        if value.kind in "iu":
            type_info = np.iinfo(value)
        elif value.kind == "f":
            type_info = np.finfo(value)
        else:
            raise RuntimeError("Unsupported dtype. Only integer/floating-point types are supported.")

        if np.can_cast(self.dtype, value):
            pass    # Ok
        elif check:
            if np.issubdtype(value, np.integer):
                if self.dtype.kind == "f":
                    for array in (self._frequencies, self._errors2):
                        if np.any(array % 1.0):
                            raise RuntimeError("Data contain non-integer values.")
            for array in (self._frequencies, self._errors2):
                if np.any((array > type_info.max) | (array < type_info.min)):
                    raise RuntimeError("Data contain values outside the specified range.")

        self._dtype = value
        self._frequencies = self._frequencies.astype(value)
        self._errors2 = self._errors2.astype(value)
        self._missed = self._missed.astype(value)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号