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:
FeatureProcessorBase processor for labels given per time step.
The label is a time series
(timestamps, values), placed on the same kind of uniform grid asTimeseriesProcessor: a label at timetgoes to stepfloor((t - t0) / sampling_rate), wheret0is 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 aValueErrorrather 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_rateas 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,), wheremaskis 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.]))
- 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:
- 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)
- dim()[source]#
Number of dimensions (Tensor.dim()) for each output tensor, in the same order as the output tuple.
- 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.
- fit(samples, field)#
Fit the processor to the samples.
- load(path)#
Optional: Load processor state from disk.
- save(path)#
Optional: Save processor state to disk.