abracudabra =========== .. py:module:: abracudabra .. autoapi-nested-parse:: Convert dataframes, arrays, and tensors to CPU/CUDA. Submodules ---------- .. toctree:: :maxdepth: 1 /autoapi/abracudabra/_import/index /autoapi/abracudabra/_validate/index /autoapi/abracudabra/annotations/index /autoapi/abracudabra/conversion/index /autoapi/abracudabra/device/index /autoapi/abracudabra/testing/index Classes ------- .. autoapisummary:: abracudabra.Device Functions --------- .. autoapisummary:: abracudabra.to_array abracudabra.to_dataframe abracudabra.to_series abracudabra.to_tensor abracudabra.to_device abracudabra.get_np_or_cp abracudabra.get_pd_or_cudf abracudabra.get_device Package Contents ---------------- .. py:function:: 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 Convert an array, series, or dataframe to a numpy array. :param sequence: The sequence to convert. :param device: The device to convert the sequence to. If None, the sequence stays on the same device. :param 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. .. py:function:: 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 Convert to a pandas/cudf dataframe. :param data: The data to convert. If a mapping, the keys will be used as column names. :param index: The optional index for the dataframe. :param device: The device to use for the dataframe. :param strict: Whether to raise an error for unknown data types. :param kwargs: Additional keyword arguments for the dataframe. :returns: The converted dataframe. .. py:function:: 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 Convert an array or tensor to a pandas/cudf series. :param sequence: The array or tensor to convert. :param index: The optional index for the series. :param device: The device to use for the series. :param strict: Whether to raise an error for unknown data types. :param kwargs: Additional keyword arguments for the series. :returns: The converted series. .. py:function:: to_tensor(sequence: abracudabra.annotations.Array | abracudabra.annotations.Series | torch.Tensor, /, device: abracudabra.device.base.Device | str | None = None, *, strict: bool = False) -> torch.Tensor Convert an array, series, or dataframe to a torch tensor. :param sequence: The sequence to convert. :param device: The device to convert the sequence to. If None, the sequence stays on the same device. :param 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. .. py:class:: Device Bases: :py:obj:`NamedTuple` A device with a name and index. .. py:attribute:: type :type: DeviceType The device type, e.g., ``"cpu"`` or ``"cuda"``. .. py:attribute:: idx :type: int | None :value: None The device index, e.g., ``0`` or ``None``. .. py:method:: __str__() -> str Return the device name. .. py:method:: _validate_device(device: object, /) -> DeviceType :staticmethod: Validate a device name. .. py:method:: _validate_idx(idx: object | None, /) -> int | None :staticmethod: Validate a device index. .. py:method:: validate(device: object, idx: object | None = None) -> Device :classmethod: Return a device, validating the device name. .. py:method:: from_str(device: str, /) -> Device :classmethod: Return a device from a string. .. py:method:: parse(device: str | Device, /) -> Device :classmethod: Return a device from a string or device. .. py:method:: to_torch() -> torch.device Return a torch device. .. py:function:: 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 Move an array, series, or tensor to a device. Call the appropriate function to move the element to the device: * :py:func:`array_to_device` for numpy/cupy arrays. * :py:func:`series_to_device` for pandas/cudf series. * :py:func:`tensor_to_device` for torch tensors. .. py:function:: get_np_or_cp(device_type: abracudabra.device.base.DeviceType | None = None) -> types.ModuleType Get the numpy or cupy library based on the device type. .. py:function:: get_pd_or_cudf(device_type: abracudabra.device.base.DeviceType | None = None) -> types.ModuleType Get the pandas or cudf library based on the device type. .. py:function:: get_device(element: abracudabra.annotations.Array | torch.Tensor, /, *, raise_if_unknown: Literal[True] = ...) -> abracudabra.device.base.Device 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. :param element: The element to check. :param raise_if_unknown: Whether to raise an error if the element is not a known array or tensor. :returns: The device of the element.