pyhealth.processors.TimeseriesLabelProcessor#

Base processor for labels given per time step, returning (value, mask) on the time grid of the input time series.

class pyhealth.processors.TimeseriesLabelProcessor(sampling_rate=datetime.timedelta(seconds=3600))[source]#

Bases: FeatureProcessor

Base processor for labels given per time step.

The label is a time series (timestamps, values), placed on the same kind of uniform grid as TimeseriesProcessor: a label at time t goes to step floor((t - t0) / sampling_rate), where t0 is the first label timestamp. Steps that no label falls into, or whose label is NaN, are masked out and never imputed. Two labels in the same step raise a ValueError rather than one silently replacing the other.

The processor does not see the input time series, so the caller must give the labels the same first timestamp and sampling_rate as the input for each label to line up with one input step. A label series longer than its input is rejected by the model’s loss.

Output:
  • (value, mask): two float tensors of shape (S,), where mask is 1 for steps with a label. Padding added when batching has mask 0.

The registered names "timeseries_binary" and "timeseries_regression" are model modes of their own, so modules that assume one prediction per sample (calibration, prediction sets, interpretability) do not treat these labels as "binary" or "regression".

Parameters:

sampling_rate (timedelta) – Interval of the sampled time steps.

Examples

>>> from datetime import datetime, timedelta
>>> from pyhealth.processors import TimeseriesBinaryLabelProcessor
>>> t0 = datetime(2020, 1, 1)
>>> times = [t0, t0 + timedelta(hours=2)]
>>> TimeseriesBinaryLabelProcessor().process((times, [0, 1]))
(tensor([0., 0., 1.]), tensor([1., 0., 1.]))
process(value)[source]#

Process an individual field value.

Parameters:

value (tuple[list[datetime], ndarray]) – Raw field value.

Return type:

tuple[Tensor, Tensor]

Returns:

Processed value.

size()[source]#
Return type:

int

is_token()[source]#

Returns whether the output (in particular, the value tensor) of the processor represents discrete token indices (True) or continuous values (False). This is used to determine whether to apply token-based transformations (e.g. nn.Embedding) or value-based augmentations (e.g. nn.Linear).

Return type:

bool

Returns:

True if the output of the processor represents discrete token indices, False otherwise.

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 (Tensor.dim()) for each output tensor, in the same order as the output tuple.

Return type:

tuple[int, ...]

Returns:

Tuple of integers corresponding to the number of dimensions of each output tensor.

spatial()[source]#

Whether each dimension (axis) of the value tensor is spatial (i.e. corresponds to a spatial axis like time, height, width, etc.) or not. This is used to determine how to apply augmentations and other transformations that should only be applied to spatial dimensions.

E.g. for CNN or RNN features, this would help determine which dimensions to apply spatial augmentations to, and which dimensions to treat as channels or features.

Return type:

tuple[bool, ...]

Returns:

Tuple of booleans corresponding to whether each axis of the value tensor is spatial or not.

fit(samples, field)#

Fit the processor to the samples.

Parameters:

samples (Iterable[Dict[str, Any]]) – List of sample dictionaries.

Return type:

None

learns_statistics: bool = False#
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

stores_tensor: bool = False#