spine.data.batch.TensorBatch
- class spine.data.batch.TensorBatch(data: Any | SparseTensorLike, counts: Sequence[int] | Any | None = None, batch_size: int | None = None, is_sparse: bool = False, has_batch_col: bool = False, coord_cols: Sequence[int] | ndarray | None = None)[source]
Batched tensor with the necessary methods to slice it.
- Attributes:
Methods
apply_mask(mask)Apply a global mask to the underlying tensor, update batching.
from_list(data_list)Builds a batch from a list of tensors.
get_counts(batch_ids, batch_size)Finds the number of elements in each entry, provided a batch ID list.
get_edges(counts)Finds the edges between successive entries in the batch.
merge(tensor_batch)Merge this tensor batch with another.
split()Breaks up the tensor batch into its constituents.
to_cm(meta)Converts the pixel coordinates of the tensor to cm.
to_numpy()Cast underlying tensor to a np.ndarray and return a new instance.
to_px(meta)Converts the coordinates of the tensor to pixel indexes.
to_tensor([dtype, device])Cast underlying tensor to a torch.tensor and return a new instance.
- __init__(data: Any | SparseTensorLike, counts: Sequence[int] | Any | None = None, batch_size: int | None = None, is_sparse: bool = False, has_batch_col: bool = False, coord_cols: Sequence[int] | ndarray | None = None) None[source]
Initialize the attributes of the class.
- Parameters:
data (Union[np.ndarray, torch.Tensor, ME.SparseTensor]) – (N, C) Batched tensors
counts (Union[List[int], np.ndarray, torch.Tensor]) –
Number of data rows in each entry
batch_size (int, optional) – Number of entries that make up the batched data
is_sparse (bool, default False) – If initializing from an ME sparse data, flip to True
has_batch_col (bool, default False) – Wheather the tensor has a column specifying the batch ID
coord_cols (Union[List[int], np.ndarray], optional) – List of columns specifying coordinates
Methods
__init__(data[, counts, batch_size, ...])Initialize the attributes of the class.
apply_mask(mask)Apply a global mask to the underlying tensor, update batching.
from_list(data_list)Builds a batch from a list of tensors.
get_counts(batch_ids, batch_size)Finds the number of elements in each entry, provided a batch ID list.
get_edges(counts)Finds the edges between successive entries in the batch.
merge(tensor_batch)Merge this tensor batch with another.
split()Breaks up the tensor batch into its constituents.
to_cm(meta)Converts the pixel coordinates of the tensor to cm.
to_numpy()Cast underlying tensor to a np.ndarray and return a new instance.
to_px(meta)Converts the coordinates of the tensor to pixel indexes.
to_tensor([dtype, device])Cast underlying tensor to a torch.tensor and return a new instance.
Attributes
Returns the batch ID of each of the elements in the tensor.
shapeShape of the underlying data.
splitsBoundaries needed to split the data into its constituents.
Alias for the underlying data stored.
- data: Any | SparseTensorLike
- counts: Any
- edges: Any
- batch_size: int
- has_batch_col: bool
- coord_cols: Sequence[int] | ndarray | None
- property tensor: Any | SparseTensorLike
Alias for the underlying data stored.
- Returns:
Underlying tensor of data
- Return type:
Union[np.ndarray, torch.Tensor, ME.SparseTensor]
- property batch_ids: Any
Returns the batch ID of each of the elements in the tensor.
- Returns:
Batch ID of each element in the tensor
- Return type:
Union[np.ndarray, torch.Tensor]
- split() list[Any | SparseTensorLike][source]
Breaks up the tensor batch into its constituents.
- Returns:
List of one tensor per entry in the batch
- Return type:
List[Union[np.ndarray, torch.Tensor]]
- apply_mask(mask: Any) None[source]
Apply a global mask to the underlying tensor, update batching.
- Parameters:
mask (Union[np.ndarray, torch.Tensor]) –
Boolean mask to apply to the underlying tensor
- merge(tensor_batch: TensorBatch) TensorBatch[source]
Merge this tensor batch with another.
- Parameters:
tensor_batch (TensorBatch) – Other tensor batch object to merge with
- Returns:
Merged tensor batch
- Return type:
- to_numpy() TensorBatch[source]
Cast underlying tensor to a np.ndarray and return a new instance.
- Returns:
New TensorBatch object with an underlying np.ndarray tensor.
- Return type:
- to_tensor(dtype: Any = None, device: Any = None) TensorBatch[source]
Cast underlying tensor to a torch.tensor and return a new instance.
- Parameters:
dtype (torch.dtype, optional) – Data type of the tensor to create
device (torch.device, optional) – Device on which to put the tensor
- Returns:
New TensorBatch object with an underlying np.ndarray tensor.
- Return type:
- to_cm(meta: Any) None[source]
Converts the pixel coordinates of the tensor to cm.
- Parameters:
meta (Meta) – Metadata information about the rasterized image
- to_px(meta: Any) None[source]
Converts the coordinates of the tensor to pixel indexes.
- Parameters:
meta (Meta) – Metadata information about the rasterized image
- classmethod from_list(data_list: Sequence[Any]) TensorBatch[source]
Builds a batch from a list of tensors.
- Parameters:
data_list (List[Union[np.ndarray, torch.Tensor]]) – List of tensors, exactly one per batch