_tensor_utils Module#

_tensor_utils Module#

waloviz._tensor_utils.broadcast_to_channels(tensor: Union[Tensor, Tuple[Tensor, Tensor]], channels: int) Union[Tensor, Tuple][source]#
Given a hierarchical tensor object recursively broadcast all leaf tensors to have exactly 2 dimensions and the same amount of channels as the wav .

Parameters#

tensortorch.Tensor | (torch.Tensor, torch.Tensor)

A hierarchical tensor object with varying amounts of channels

Returns#

objtorch.Tensor

A hierarchical tensor object with the same amount of channels

Raises#

ValueError
When a leaf tensor has 0 non squeezable dimensions
OR
When the initial amount of channels of a leaf tensor was larger than the target amount

waloviz._tensor_utils.handle_dict_axes(over_curve_names: Optional[List[str]], over_curve_axes: Dict[str, str]) List[Optional[str]][source]#
Handles the case where over_curve_axes is a dict and converts it to a list.

Parameters#

over_curve_namesList[str] | None

.

over_curve_axesDict[str, str]

.

Returns#

over_curve_axesList[str | None]

.

Raises#

ValueError
When over_curve_axes was a dict but over_curve_names was not provided

waloviz._tensor_utils.handle_dict_colors(over_curve_names: Optional[List[str]], over_curve_colors: Dict[str, str]) List[Optional[str]][source]#
Handles the case where over_curve_colors is a dict and converts it to a list.

Parameters#

over_curve_namesList[str] | None

.

over_curve_colorsDict[str, str]

.

Returns#

over_curve_colorsList[str | None]

.

Raises#

ValueError
When over_curve_colors was a dict but over_curve_names was not provided

waloviz._tensor_utils.handle_dict_over_curve(over_curve: Dict[str, Any], over_curve_names: Optional[List[str]]) Tuple[List[Any], List[str]][source]#
Handles the case where over_curve is a dict and converts it to a list.

Parameters#

over_curveDict[str, Any]

.

over_curve_namesList[str] | None

.

Returns#

over_curveList[Any]

.

over_curve_namesList[str]

.

Raises#

ValueError
When over_curve_names was provided but over_curve was a dict

waloviz._tensor_utils.handle_list_over_curve(over_curve: List[Any], over_curve_names: Optional[List[str]], over_curve_colors: Optional[Union[List[Optional[str]], Dict[str, str]]], over_curve_axes: Optional[Union[List[Optional[str]], List[str], Dict[str, str]]]) Optional[List[str]][source]#
Handles the case where over_curve is a list, makes a bunch of validations and generates over_curve_names if None were provided.

Parameters#

over_curveDict[str, Any]

.

over_curve_namesList[str] | None

.

over_curve_colorsList[str] | None

.

over_curve_axesList[str] | None

.

Returns#

over_curve_namesList[str]

.

Raises#

ValueError
When over_curve_names was provided but of a different size from over_curve
OR
When over_curve_colors was provided but of a different size from over_curve
OR
When over_curve_axes was provided but of a different size from over_curve

waloviz._tensor_utils.preprocess_over_curve(wav: Tensor, sr: int, channels: int, over_curve: Optional[Union[List[Any], Dict[str, Any], ndarray, Tensor, Tuple[Any, Any], Any]], over_curve_names: Optional[Union[str, List[str]]] = None, over_curve_colors: Optional[Union[str, List[Optional[str]], Dict[str, str]]] = None, over_curve_axes: Optional[Union[str, List[Optional[str]], List[str], Dict[str, str]]] = None) Tuple[Optional[List[Tensor]], Optional[List[str]], Optional[List[Optional[str]]], Optional[List[str]]][source]#
Converts user defined overlaid curves related options into a standard format, in terms of object structure and types.

Parameters#

wavtorch.Tensor

Loaded audio tensor

srint

Resolved sample-rate

channelsint

The amount of channels in wav

over_curvetensorlike | List[tensorlike] | Dict[str, tensorlike] | callable

User provided

over_curve_namesList[str]

User provided

over_curve_colorsList[str]

User provided

over_curve_axesList[str]

User provided

Returns#

over_curveList[torch.Tensor]

Standardized

over_curve_namesList[str]

Standardized

over_curve_colorsList[str]

Standardized

over_curve_axesList[str]

User provided

Raises#

ValueError
When over_curve_names was provided but of a different size from over_curve
OR
When over_curve_colors was provided but of a different size from over_curve
OR
When over_curve_axes was provided but of a different size from over_curve
OR
When over_curve_colors was a dict but over_curve_names was not provided
OR
When over_curve_axes was a dict but over_curve_names was not provided
OR
When over_curve_names was provided but over_curve was a dict
OR
When over_curve contained an (X,Y) tuple of size not equal to 2
OR
When over_curve contained an (X,Y) tuple where X.shape != Y.shape

waloviz._tensor_utils.single_value_to_list(over_curve: Optional[Union[List[Any], Dict[str, Any], ndarray, Tensor, Tuple[Any, Any], Any]], over_curve_names: Optional[Union[str, List[str]]] = None, over_curve_colors: Optional[Union[str, List[Optional[str]], Dict[str, str]]] = None, over_curve_axes: Optional[Union[str, List[Optional[str]], List[str], Dict[str, str]]] = None) Tuple[Optional[Union[List[Any], Dict[str, Any], ndarray, Tensor, Tuple[Any, Any], Any]], Optional[List[str]], Optional[Union[List[Optional[str]], Dict[str, str]]], Optional[Union[List[Optional[str]], List[str], Dict[str, str]]]][source]#
Makes sure that if single values were provided they are wrapped in single element lists.

Parameters#

over_curvetensorlike | List[tensorlike] | Dict[str, tensorlike] | callable

User provided

over_curve_namesList[str]

User provided

over_curve_colorsList[str]

User provided

over_curve_axesList[str]

User provided

Returns#

over_curvetensorlike | List[tensorlike] | Dict[str, tensorlike] | callable

.

over_curve_namesList[str]

.

over_curve_colorsList[str]

.

over_curve_axesList[str]

.


waloviz._tensor_utils.skip_to_size(tensor: Union[Tensor, Tuple], max_size: int) Union[Tensor, Tuple][source]#
Given a hierarchical tensor object skip equally spaced tensor values along the time dimension ( dim=-1 ) to become lower than the max_size value.
This helps with the responsiveness of the player and avoids errors at the
cost of losing information.
This is used for both the overlaid curves and the spectrogram itself.

Parameters#

tensortorch.Tensor | (torch.Tensor, torch.Tensor)

A hierarchical tensor object with an unknown time size

max_sizeint

The maximum allowed time dimension size

Returns#

objtorch.Tensor

A hierarchical tensor object with a time size lower than max_size


waloviz._tensor_utils.to_tensor(obj: Any) Union[Tensor, Tuple[Tensor, Tensor], Tuple][source]#
Given a hierarchical object recursively converts all leaf nodes into PyTorch tensors.

Parameters#

objAny

A hierarchical tuple object

Returns#

objtorch.Tensor

A hierarchical tuple tensor object


waloviz._tensor_utils.validate_XY_over_curve(over_curve: Union[List[Any], Dict[str, Any], ndarray, Tensor, Tuple[Any, Any], Any]) None[source]#
If the over_curve has X and Y tensors, make sure their of the same shape.

Parameters#

over_curveList[torch.Tensor | (torch.Tensor, torch.Tensor)]

Either a list of tensors or (X,Y) tuples of tensors

Raises#

ValueError
When over_curve contained an (X,Y) tuple of size not equal to 2
OR
When over_curve contained an (X,Y) tuple where X.shape != Y.shape