19.14. Utils

popxl.utils.table_to_string(rows, delimiter=' | ', header=True)

Create a string that resembles a table from inputs rows.

Each item in rows represents a row which will be delimited with delimiter. Each row should exactly have the same length.

Example:

rows = [
    ["num", "foo", "name"],
    [3, "aaab", "args"],
    [4, "barrrr", "kwargs"],
    [3, "me", "inspect"],
    [-1, "who", "popxl"],
]
print(table_to_string(rows))

Output:

num | foo    | name
-----------------------
3   | aaab   | args
4   | barrrr | kwargs
3   | me     | inspect
-1  | who    | popxl
Parameters
  • rows (Sequence[Sequence]) – A row by column nested sequence. Each item needs to be a string or stringable

  • delimiter (str) – String used to delimit columns

  • header (bool) – If true, the first row is underlined

Returns

A string representation of the table

Return type

str

popxl.utils.to_numpy(x, dtype=None, downcast=True, copy=True, log2_scale=None, nan_on_overflow=None)

Convert a HostScalarTensor to a numpy array and copies the data if enabled.

If x is an np.memmap and copy is False, the following conditions must be satisfied (to ensure no copy is possible):

  • x does not require downcasting

  • dtype is None or equal to x.dtype

  • x is in C-array form.

The returned array in this case will be exactly x. It will still be an np.memmap.

Parameters
  • x (HostScalarTensor) – The data used to initialise the tensor. This can be an np.ndarray, torch.tensor or a value NumPy can use to construct an np.ndarray. If dtype is of float8 type this must be a np.float16, np.float32 or float64 type, torch equivalent, or native type equivalent. Other values are not supported.

  • dtype (Optional[dtypes.dtype]) – The data type of the tensor to be created. If not specified NumPy will infer the data type and downcast to 32 bits if necessary. For float8 dtypes automatic inference of dtype is not currently possible, please explicitly specify the dtype.

  • downcast (bool) – If True and no dtype is provided, 64-bit float/ints will be downcast to 32-bit variants. Defaults to True.

  • copy (bool) – If true the objects data is guaranteed to be copied.

  • log2_scale (int) – If dtype is either popxl.float8_143 or popxl.float8_152 then multiply the incoming data by pow2(log2_scale) before casting.

  • nan_on_overflow (bool) – If dtype is either popxl.float8_143 or popxl.float8_152 and this flag is set then replace values that cannot be represented by the requested dtype with np.nan values.

Raises
  • RuntimeError – If parameters are not supported.

  • TypeError – If dtype is of float8 type and x is not of type np.float16,

  • np.float32 or float64, torch equivalent, or native type equivalent.

  • ValueError – If the data parameter is a np.memmap and it is either not

  • a C-array, has a dtype that requires downcasting, or the dtype parameter

  • is not None and conflicts with data.dtype.

Returns

A NumPy array.

Return type

np.ndarray