pyhealth.processors.TimeseriesProcessor#
Processor for timeseries data.
- class pyhealth.processors.TimeseriesProcessor(sampling_rate=datetime.timedelta(seconds=3600), impute_strategy='forward_fill', normalization=None, missing_indicators=False)[source]#
Bases:
FeatureProcessorFeature processor for irregular time series with missing values.
- Input:
timestamps: List[datetime]
values: np.ndarray of shape (T, F)
- Processing:
Uniform sampling at fixed intervals.
Optional normalization, with statistics learned in
fit()from the observed (non-missing) sampled values.Optional missing-value indicators, one per feature, 1 where the value was missing before imputation.
Imputation for missing values.
With
normalization="zscore"andimpute_strategy="forward_fill", values missing before the first observation are filled with 0, which is the training mean after normalization.- Output:
torch.Tensor of shape (S, F), or (S, 2F) with missing-value indicators appended, where S is the number of sampled time steps.
- Parameters:
sampling_rate (
timedelta) – Interval of the sampled time steps.impute_strategy (
str) –"forward_fill"(values before the first observation are filled with 0) or"zero".normalization (
Optional[Literal[‘zscore’, ‘minmax’]]) –None,"zscore"(zero mean, unit variance) or"minmax"(scaled to [0, 1] on the fitted range).missing_indicators (
bool) – Whether to append the missing-value indicators.
Examples
>>> from datetime import datetime, timedelta >>> from pyhealth.processors import TimeseriesProcessor >>> t0 = datetime(2020, 1, 1) >>> value = ([t0, t0 + timedelta(hours=2)], [[1.0], [3.0]]) >>> p = TimeseriesProcessor(sampling_rate=timedelta(hours=1)) >>> p.fit([{"x": value}], "x") >>> p.process(value).shape torch.Size([3, 1])
- fit(samples, field)[source]#
Fit the processor.
Determines n_features from the first valid sample and, with
normalization, learns the per-feature statistics from all samples.
- 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)
- 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.
- load(path)#
Optional: Load processor state from disk.