예제 #1
0
def machine_epsilon(fp_format: np.dtype) -> np.number:
    """
    Calculate the machine precision for the given floating point type.

    Arguments:
    fp_format: floating point format, e.g. float32 or float64

    Return:
    eps : calculated machine precision

    Raised Exceptions:
    -

    Side Effects:
    Prints out iteration values.

    Forbidden: numpy.finfo
    """

    # TODO: create epsilon element with correct initial value and data format fp_format
    eps = fp_format.type(0.0)

    # Create necessary variables for iteration
    one = fp_format.type(1.0)
    two = fp_format.type(2.0)
    i = 0

    print('  i  |       2^(-i)        |  1 + 2^(-i)  ')
    print('  ----------------------------------------')

    # TODO: determine machine precision without the use of numpy.finfo()

    print('{0:4.0f} |  {1:16.8e}   | equal 1'.format(i, eps))
    return eps
예제 #2
0
def encode_fill_value(v: Any, dtype: np.dtype) -> Any:
    # early out
    if v is None:
        return v
    if dtype.kind == 'f':
        if np.isnan(v):
            return 'NaN'
        elif np.isposinf(v):
            return 'Infinity'
        elif np.isneginf(v):
            return '-Infinity'
        else:
            return float(v)
    elif dtype.kind in 'ui':
        return int(v)
    elif dtype.kind == 'b':
        return bool(v)
    elif dtype.kind in 'c':
        v = (encode_fill_value(v.real, dtype.type().real.dtype),
             encode_fill_value(v.imag, dtype.type().imag.dtype))
        return v
    elif dtype.kind in 'SV':
        v = str(base64.standard_b64encode(v), 'ascii')
        return v
    elif dtype.kind == 'U':
        return v
    elif dtype.kind in 'mM':
        return int(v.view('i8'))
    else:
        return v
예제 #3
0
    def from_bytes(cls, data: bytes, axiskeys: str, dtype: np.dtype, compression: N5Compressor, location: Point5D):
        data = np.frombuffer(data, dtype=np.uint8)

        header_types = [
            ("mode", ">u2"),  # mode (uint16 big endian, default = 0x0000, varlength = 0x0001)
            ("num_dims", ">u2"),  # number of dimensions (uint16 big endian)
        ]
        preamble = np.frombuffer(data, dtype=header_types, count=1)
        header_types.append(
              # dimension 1[,...,n] (uint32 big endian)
            ("dimensions", str(preamble["num_dims"].item()) + ">u4") # type: ignore
        )

        if preamble["mode"].item() == cls.Modes.VARLENGTH.value:
            # mode == varlength ? number of elements (uint32 big endian)
            header_types.append(("num_elements", ">u4")) # type: ignore
            raise RuntimeError("Don't know how to handle varlen N5 blocks")

        header_dtype = np.dtype(header_types)
        header_data = np.frombuffer(data, dtype=header_dtype, count=1)
        array_shape = header_data["dimensions"].squeeze()

        compressed_buffer = np.frombuffer(data, offset=header_dtype.itemsize, dtype=np.uint8)
        decompressed_buffer = compression.decompress(compressed_buffer.tobytes())
        raw_array = np.frombuffer(decompressed_buffer, dtype=dtype.newbyteorder(">")).reshape(array_shape, order="F") # type: ignore

        return cls(raw_array, axiskeys=axiskeys[::-1], location=location)
예제 #4
0
def to_bytes(obj: Any, dtype: np.dtype) -> bytes:
    """
    Converts an object to bytes.

    :param obj: The object to convert to bytes.
    :param dtype: The numpy data type to interpret the object as when converting to bytes.
    :return: A bytes object, representing the object obj as type dtype.
    """
    return dtype.type(obj).tobytes()
예제 #5
0
    def __readInt(self, size: int, dtype: np.dtype,
                  byteorder: Optional[str]) -> int:
        # call expects needed bytes to be available in base stream
        buffer = self.read(size)

        if len(buffer) != size:
            raise RuntimeError(f"Failed to read {size}-bytes from stream")

        if not (byteorder is None
                and self.defaultIsNative) and byteorder != sys.byteorder:
            dtype = dtype.newbyteorder()

        return np.frombuffer(buffer, dtype)[0]
예제 #6
0
 def decode(
     self,
     *,
     roi: Interval5D,
     dtype: np.dtype, #type: ignore
     raw_chunk: bytes
 ) -> Array5D:
     # "The (...) data (...) chunk is stored directly in little-endian binary format in [x, y, z, channel] Fortran order"
     raw_tile = np.frombuffer(
         raw_chunk,
         dtype=dtype.newbyteorder("<") # type: ignore
     ).reshape(roi.shape.to_tuple("xyzc"), order="F")
     tile_5d = Array5D(raw_tile, axiskeys="xyzc", location=roi.start)
     return tile_5d
    def __readInt(self, size: int, dtype: np.dtype,
                  byteorder: Optional[str]) -> int:
        if not (byteorder is None
                and self.defaultIsNative) and byteorder != sys.byteorder:
            dtype = dtype.newbyteorder()

        if self.receivePosition <= self.receiveLength - size:
            value = np.frombuffer(
                self.receiveBuffer[self.receivePosition:self.receivePosition +
                                   size], dtype)[0]
            self.receivePosition += size
            return value

        self.ReadAll(self.buffer, 0, size)
        return np.frombuffer(self.buffer[0:size], dtype)[0]
예제 #8
0
def _check_lossless_cast(value, dtype: np.dtype) -> bool:
    """
    Check if we can cast the given value to the given dtype _losslesly_.

    Parameters
    ----------
    value : object
    dtype : np.dtype

    Returns
    -------
    bool
    """
    casted = dtype.type(value)
    if casted == value:
        return True
    return False
예제 #9
0
 def decode_fill_value(cls, v: Any, dtype: np.dtype, object_codec: Any = None) -> Any:
     # early out
     if v is None:
         return v
     if dtype.kind == 'V' and dtype.hasobject:
         if object_codec is None:
             raise ValueError('missing object_codec for object array')
         v = base64.standard_b64decode(v)
         v = object_codec.decode(v)
         v = np.array(v, dtype=dtype)[()]
         return v
     if dtype.kind == "f":
         if v == "NaN":
             return np.nan
         elif v == "Infinity":
             return np.PINF
         elif v == "-Infinity":
             return np.NINF
         else:
             return np.array(v, dtype=dtype)[()]
     elif dtype.kind in "c":
         v = (
             cls.decode_fill_value(v[0], dtype.type().real.dtype),
             cls.decode_fill_value(v[1], dtype.type().imag.dtype),
         )
         v = v[0] + 1j * v[1]
         return np.array(v, dtype=dtype)[()]
     elif dtype.kind == "S":
         # noinspection PyBroadException
         try:
             v = base64.standard_b64decode(v)
         except Exception:
             # be lenient, allow for other values that may have been used before base64
             # encoding and may work as fill values, e.g., the number 0
             pass
         v = np.array(v, dtype=dtype)[()]
         return v
     elif dtype.kind == "V":
         v = base64.standard_b64decode(v)
         v = np.array(v, dtype=dtype.str).view(dtype)[()]
         return v
     elif dtype.kind == "U":
         # leave as-is
         return v
     else:
         return np.array(v, dtype=dtype)[()]
예제 #10
0
파일: utils.py 프로젝트: hekaisheng/mars
def on_serialize_numpy_type(value: np.dtype):
    if value is pd.NaT:
        value = None
    return value.item() if isinstance(value, np.generic) else value