frombuffer#

ivy.frombuffer(buffer, dtype=None, count=-1, offset=0)[source]#

Interpret a buffer as a 1-dimensional array.

Note

Note that either of the following must be true: 1. count is a positive non-zero number, and the total number of bytes in the buffer is equal or greater than offset plus count times the size (in bytes) of dtype. 2. count is negative, and the length (number of bytes) of the buffer subtracted by the offset is a multiple of the size (in bytes) of dtype.

Parameters:
  • buffer (bytes) – An object that exposes the buffer interface.

  • dtype (Optional[Union[Dtype, NativeDtype]], default: None) – Data-type of the returned array; default: float.

  • count (Optional[int], default: -1) – Number of items to read. -1 means all data in the buffer.

  • offset (Optional[int], default: 0) – Start reading the buffer from this offset (in bytes); default: 0.

Return type:

Array

Returns:

out – 1-dimensional array.

Examples

With bytes inputs:

>>> x = b'\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x00@'
>>> y = ivy.frombuffer(x, dtype=ivy.float64)
>>> print(y)
ivy.array([1., 2.])
>>> x = b'\x01\x02\x03\x04'
>>> y = ivy.frombuffer(x, dtype='int8', count=-2, offset=1)
>>> print(y)
ivy.array([2, 3, 4])
>>> x = b'\x00<\x00@\x00B\x00D\x00E'
>>> y = ivy.frombuffer(x, dtype='float16', count=4, offset=2)
>>> print(y)
ivy.array([2., 3., 4., 5.])
Container.frombuffer(self, dtype=<class 'float'>, count=-1, offset=0, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False)[source]#

ivy.Container instance method variant of ivy.frombuffer. This method simply wraps the function, and so the docstring for ivy.frombuffer also applies to this method with minimal changes.

Parameters:
  • self (Container) – An object that exposes the buffer interface.

  • dtype (Optional[Union[Dtype, NativeDtype]], default: <class 'float'>) – Data-type of the returned array; default: float.

  • count (Optional[Union[int, Container]], default: -1) – Number of items to read. -1 means all data in the buffer.

  • offset (Optional[Union[int, Container]], default: 0) – Start reading the buffer from this offset (in bytes); default: 0.

  • key_chains (Optional[Union[List[str], Dict[str, str], Container]], default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (Union[bool, Container], default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (Union[bool, Container], default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

  • map_sequences (Union[bool, Container], default: False) – Whether to also map method to sequences (lists, tuples). Default is False.

Return type:

Container

Returns:

out – 1-dimensional array.

Examples

With ivy.Container inputs:

>>> x = ivy.Container(
...     a = b'\x00\x00\x00\x00\x00\x00\xf0?',
...     b = b'\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x00@'
... )
>>> y = x.frombuffer(dtype=ivy.float64)
>>> print(y)
{
    a: ivy.array([1.]),
    b: ivy.array([1., 2.])
}
>>> x = ivy.Container(
...     a = b'\x01\x02\x03\x04',
...     b = b'\x05\x04\x03\x03\x02'
... )
>>> y = x.frombuffer(dtype=ivy.int8, count=3, offset=1)
>>> print(y)
{
    a: ivy.array([2, 3, 4]),
    b: ivy.array([4, 3, 3])
}