torchwrench.extras.numpy package

class torchwrench.extras.numpy.InvalidTorchDType(*args, **kwargs)[source]

Bases: object

Default return type for torch_dtype when an invalid data is passed as argument of scan_torch_dtype function. (like str for example)

class torchwrench.extras.numpy.ShapeDTypeInfo(shape: tuple[int, ...], torch_dtype: torch.dtype | +T_Invalid | +T_EmptyTorch, numpy_dtype: numpy.dtype | +T_EmptyNp, valid_shape: bool)[source]

Bases: Generic[T_Invalid, T_EmptyTorch, T_EmptyNp]

property fill_value : bool | int | float | complex | None | str | bytes
property get_ndim : int
property kind : str
numpy_dtype : dtype | T_EmptyNp
shape : Tuple[int, ...]
torch_dtype : dtype | T_Invalid | T_EmptyTorch
valid_shape : bool
torchwrench.extras.numpy.get_default_numpy_dtype() dtype[source]
torchwrench.extras.numpy.is_numpy_bool_array(x: Any) TypeGuard[bool | ndarray][source]
torchwrench.extras.numpy.is_numpy_integral_array(x: Any) TypeGuard[ndarray | generic][source]
torchwrench.extras.numpy.is_numpy_number_like(x: Any) TypeGuard[ndarray | number][source]

Returns True if x is an instance of a numpy number type, a np.bool_ or a zero-dimensional numpy array. If numpy is not installed, this function always returns False.

torchwrench.extras.numpy.is_numpy_scalar_like(x: Any) TypeGuard[ndarray | generic][source]

Returns True if x is an instance of a numpy number type or a zero-dimensional numpy array. If numpy is not installed, this function always returns False.

torchwrench.extras.numpy.is_numpy_str_array(x: Any) TypeGuard[str_ | ndarray][source]
torchwrench.extras.numpy.logical_and_lst(*args, start=None)

Reduce elements using “and” operator (&).

torchwrench.extras.numpy.logical_or_lst(*args, start=None)

Reduce elements using “or” operator (|).

torchwrench.extras.numpy.merge_numpy_dtypes(dtypes: Iterable[dtype | T_EmptyNp], *, empty: T_EmptyNp = dtype('V')) dtype | T_EmptyNp[source]
torchwrench.extras.numpy.merge_torch_dtypes(dtypes: Iterable[dtype | T_Invalid | T_EmptyNp], *, invalid: T_Invalid = InvalidTorchDType(), empty: T_EmptyNp = None) dtype | T_Invalid | T_EmptyNp[source]
torchwrench.extras.numpy.ndarray_to_tensor(x: ndarray | number, *, device: device | None | 'default' | 'cuda_if_available' | str | int = None, dtype: dtype | None | 'default' | str | DTypeEnum = None) Tensor[source]

Convert numpy array to PyTorch tensor.

torchwrench.extras.numpy.numpy_all_eq(x: generic | ndarray, dim: None = None) bool[source]
torchwrench.extras.numpy.numpy_all_eq(x: generic | ndarray, dim: int) ndarray
torchwrench.extras.numpy.numpy_all_ne(x: generic | ndarray) bool[source]
torchwrench.extras.numpy.numpy_complex_dtype_to_float_dtype(dtype: dtype) dtype[source]

Returns the associated float dtype from complex dtype. If input dtype is not complex, it just returns the same dtype.

torchwrench.extras.numpy.numpy_dtype_to_fill_value(dtype: Any) bool | int | float | complex | None | str | bytes[source]
torchwrench.extras.numpy.numpy_dtype_to_torch_dtype(dtype: dtype, *, invalid: T_Invalid = InvalidTorchDType()) dtype | T_Invalid[source]
torchwrench.extras.numpy.numpy_is_complex(x: ndarray | generic) bool[source]
torchwrench.extras.numpy.numpy_is_complex_dtype(dtype: dtype) bool[source]
torchwrench.extras.numpy.numpy_is_floating_point(x: ndarray | generic) bool[source]
torchwrench.extras.numpy.numpy_item(x: ndarray | generic | bool | int | float | complex | None | str | bytes) generic[source]
torchwrench.extras.numpy.numpy_to_tensor(x: ndarray | number, *, device: device | None | 'default' | 'cuda_if_available' | str | int = None, dtype: dtype | None | 'default' | str | DTypeEnum = None) Tensor[source]

Convert numpy array to PyTorch tensor.

torchwrench.extras.numpy.numpy_topk(x: ndarray, k: int, dim: int = -1, largest: bool = True, sorted: bool = True) tuple[ndarray, ndarray][source]
torchwrench.extras.numpy.numpy_view_as_complex(x: ndarray) ndarray[source]

Convert complex array to float array.

Args:

x: The input float array of any shape (…, 2)

Returns:

x_real: The same data in a complex array of shape (…,)

torchwrench.extras.numpy.numpy_view_as_real(x: ndarray) ndarray[source]

Convert complex array to float array.

Args:

x: The input complex array of any shape (…,)

Returns:

x_real: The same data in a float array of shape (…, 2)

torchwrench.extras.numpy.scan_numpy_dtype(x: Any, *, empty: T_EmptyNp = dtype('V')) dtype | T_EmptyNp[source]
torchwrench.extras.numpy.scan_shape_dtypes(x: Any, *, accept_heterogeneous_shape: bool = False, empty_torch: T_EmptyTorch = None, empty_np: T_EmptyNp = dtype('V')) ShapeDTypeInfo[InvalidTorchDType, T_EmptyTorch, T_EmptyNp][source]

Returns the shape and the hdf_dtype for an input.

torchwrench.extras.numpy.scan_torch_dtype(x: Any, *, invalid: T_Invalid = InvalidTorchDType(), empty: T_EmptyTorch = None) dtype | T_Invalid | T_EmptyTorch[source]

Returns torch dtype of an arbitrary object. Works recursively on tuples and lists. An instance of InvalidTorchDType can be returned if a str is passed.

torchwrench.extras.numpy.tensor_to_ndarray(x: Tensor, *, dtype: str | dtype | None = None, force: bool = False) ndarray[source]

Convert PyTorch tensor to numpy array.

torchwrench.extras.numpy.tensor_to_numpy(x: Tensor, *, dtype: str | dtype | None = None, force: bool = False) ndarray[source]

Convert PyTorch tensor to numpy array.

torchwrench.extras.numpy.to_ndarray(x: Tensor | ndarray | Iterable | bool | int | float | complex | None | str | bytes, *, dtype: str | dtype | None = None, force: bool = False) ndarray[source]

Convert input to numpy array. Works with any arbitrary object.

torchwrench.extras.numpy.to_numpy(x: Tensor | ndarray | Iterable | bool | int | float | complex | None | str | bytes, *, dtype: str | dtype | None = None, force: bool = False) ndarray[source]

Convert input to numpy array. Works with any arbitrary object.

torchwrench.extras.numpy.torch_dtype_to_numpy_dtype(dtype: dtype) dtype[source]

Submodules