abracudabra#

Convert dataframes, arrays, and tensors to CPU/CUDA.

Submodules#

Classes#

Device

A device with a name and index.

Functions#

to_array(→ abracudabra.annotations.Array)

Convert an array, series, or dataframe to a numpy array.

to_dataframe(→ abracudabra.annotations.DataFrame)

Convert to a pandas/cudf dataframe.

to_series(→ abracudabra.annotations.Series)

Convert an array or tensor to a pandas/cudf series.

to_tensor(→ torch.Tensor)

Convert an array, series, or dataframe to a torch tensor.

to_device(...)

Move an array, series, or tensor to a device.

get_np_or_cp(→ types.ModuleType)

Get the numpy or cupy library based on the device type.

get_pd_or_cudf(→ types.ModuleType)

Get the pandas or cudf library based on the device type.

get_device(…)

Get the device of a numpy/cupy array or series.

Package Contents#

abracudabra.to_array(sequence: abracudabra.annotations.Array | abracudabra.annotations.Series | abracudabra.annotations.DataFrame | torch.Tensor, /, device: str | abracudabra.device.base.Device | None = None, *, strict: bool = False) abracudabra.annotations.Array[source]#

Convert an array, series, or dataframe to a numpy array.

Parameters:
  • sequence – The sequence to convert.

  • device – The device to convert the sequence to. If None, the sequence stays on the same device.

  • strict – Whether to raise an error if the sequence is not a valid type. A numpy/cupy array, pandas/cudf series or dataframe, or torch tensor are valid types. If False, the sequence is converted to a numpy/cupy array if possible, but it might raise an error if the conversion is not possible.

Returns:

A numpy/cupy array.

abracudabra.to_dataframe(data: collections.abc.Mapping[str, abracudabra.annotations.Array | torch.Tensor] | torch.Tensor | abracudabra.annotations.Array, /, index: abracudabra.annotations.Array | torch.Tensor | None = None, device: str | abracudabra.device.base.Device | None = None, *, strict: bool = False, **kwargs: Any) abracudabra.annotations.DataFrame[source]#

Convert to a pandas/cudf dataframe.

Parameters:
  • data – The data to convert. If a mapping, the keys will be used as column names.

  • index – The optional index for the dataframe.

  • device – The device to use for the dataframe.

  • strict – Whether to raise an error for unknown data types.

  • kwargs – Additional keyword arguments for the dataframe.

Returns:

The converted dataframe.

abracudabra.to_series(sequence: object, /, index: abracudabra.annotations.Array | torch.Tensor | None = None, device: str | abracudabra.device.base.Device | None = None, *, strict: bool = False, **kwargs: Any) abracudabra.annotations.Series[source]#

Convert an array or tensor to a pandas/cudf series.

Parameters:
  • sequence – The array or tensor to convert.

  • index – The optional index for the series.

  • device – The device to use for the series.

  • strict – Whether to raise an error for unknown data types.

  • kwargs – Additional keyword arguments for the series.

Returns:

The converted series.

abracudabra.to_tensor(sequence: abracudabra.annotations.Array | abracudabra.annotations.Series | torch.Tensor, /, device: abracudabra.device.base.Device | str | None = None, *, strict: bool = False) torch.Tensor[source]#

Convert an array, series, or dataframe to a torch tensor.

Parameters:
  • sequence – The sequence to convert.

  • device – The device to convert the sequence to. If None, the sequence stays on the same device.

  • strict – Whether to raise an error if the sequence is not a valid type. A numpy/cupy array, pandas/cudf series or dataframe, or torch tensor are valid types. If False, the sequence is converted to a torch tensor if possible, but it might raise an error if the conversion is not possible.

Returns:

A torch tensor.

class abracudabra.Device[source]#

Bases: NamedTuple

A device with a name and index.

type: DeviceType#

The device type, e.g., "cpu" or "cuda".

idx: int | None = None#

The device index, e.g., 0 or None.

__str__() str[source]#

Return the device name.

static _validate_device(device: object, /) DeviceType[source]#

Validate a device name.

static _validate_idx(idx: object | None, /) int | None[source]#

Validate a device index.

classmethod validate(device: object, idx: object | None = None) Device[source]#

Return a device, validating the device name.

classmethod from_str(device: str, /) Device[source]#

Return a device from a string.

classmethod parse(device: str | Device, /) Device[source]#

Return a device from a string or device.

to_torch() torch.device[source]#

Return a torch device.

abracudabra.to_device(sequence: abracudabra.annotations.Array | abracudabra.annotations.Series | torch.Tensor, /, device: abracudabra.device.base.Device | str) abracudabra.annotations.Array | abracudabra.annotations.Series | torch.Tensor[source]#

Move an array, series, or tensor to a device.

Call the appropriate function to move the element to the device:

  • array_to_device() for numpy/cupy arrays.

  • series_to_device() for pandas/cudf series.

  • tensor_to_device() for torch tensors.

abracudabra.get_np_or_cp(device_type: abracudabra.device.base.DeviceType | None = None) types.ModuleType[source]#

Get the numpy or cupy library based on the device type.

abracudabra.get_pd_or_cudf(device_type: abracudabra.device.base.DeviceType | None = None) types.ModuleType[source]#

Get the pandas or cudf library based on the device type.

abracudabra.get_device(element: abracudabra.annotations.Array | torch.Tensor, /, *, raise_if_unknown: Literal[True] = ...) abracudabra.device.base.Device[source]#
abracudabra.get_device(element: abracudabra.annotations.Array | torch.Tensor, /, *, raise_if_unknown: bool = ...) abracudabra.device.base.Device | None

Get the device of a numpy/cupy array or series.

Parameters:
  • element – The element to check.

  • raise_if_unknown – Whether to raise an error if the element is not a known array or tensor.

Returns:

The device of the element.