pyhealth.processors.TensorProcessor#

Processor for tensor data.

class pyhealth.processors.TensorProcessor(dtype=torch.float32, spatial_dims=None)[source]#

Bases: FeatureProcessor

Feature processor for converting numerical lists to tensors.

Input:
  • List of numbers (int/float) or nested lists of numbers

Processing:
  • Convert input directly to torch.Tensor using torch.tensor()

Output:
  • torch.Tensor with appropriate shape and dtype

fit(samples, field)[source]#

Infer n_dim from the first valid sample.

Parameters:
  • samples (Iterable[Dict[str, Any]]) – Iterable of sample dictionaries.

  • field (str) – The field name to extract from samples.

Return type:

None

process(value)[source]#

Process a numerical value or list into a torch.Tensor.

Parameters:

value (Any) – Input value (list of numbers or nested lists)

Returns:

Processed tensor

Return type:

torch.Tensor

size()[source]#

Get the feature size of the processor.

Returns:

Size is not predetermined for tensor processor

Return type:

None

is_token()[source]#

Whether the output tensor represents discrete token indices, inferred from dtype.

Return type:

bool

Returns:

True if dtype is integer (discrete tokens), False if floating point (continuous).

schema()[source]#

Returns the schema of the processed feature. For a processor that emits a single tensor, this should just return [“value”]. For a processor that emits a tuple of tensors, this should return a tuple of the same length as the tuple, with the semantic name of each tensor, such as [“time”, “value”], [“value”, “mask”], etc.

Typical semantic names include:
  • “value”: the main processed tensor output of the processor

  • “time”: the time tensor output of the processor (mostly for StageNet)

  • “mask”: the mask tensor output of the processor (if applicable)

Return type:

tuple[str, ...]

Returns:

Tuple of semantic names corresponding to the output of the processor.

dim()[source]#

Number of dimensions for the output tensor.

Return type:

tuple[int, ...]

Returns:

(n_dim,)

Raises:

NotImplementedError – If n_dim was not provided and fit() was not called.

spatial()[source]#

Whether each dimension of the output tensor is spatial.

If spatial_dims was provided at init, returns that. Otherwise defaults to all False based on n_dim.

Return type:

tuple[bool, ...]

load(path)#

Optional: Load processor state from disk.

Parameters:

path (str) – File path to load processor state from.

Return type:

None

save(path)#

Optional: Save processor state to disk.

Parameters:

path (str) – File path to save processor state.

Return type:

None