pyhealth.processors.RawProcessor#

Processor for raw data: values are passed through unchanged and returned as the same Python objects, including lists whose length or shape varies between samples. See “How Processed Samples Are Stored” on the Processors page.

class pyhealth.processors.RawProcessor[source]#

Bases: FeatureProcessor

Processor that returns values unchanged (pass-through).

Use it for fields a model or task reads as Python objects rather than tensors: strings, numbers, lists whose length varies between samples (notes per admission, measurement times, codes per visit), nested lists, tuples, dicts, None, bytes or NumPy arrays.

In the disk cache (set_task, create_sample_dataset(in_memory=False)) raw values are stored as one pickled item per sample, so they may vary in length, shape and type between samples, and dataset[i][field] returns exactly the object that went in (lists stay lists, tuples stay tuples). Pickled values are as trusted as the rest of the cache directory, which already holds pickled processors in schema.pkl: only load caches you created or trust.

In PyHealth 2.0.2 and earlier, raw values whose length or shape varied between samples failed to write to the disk cache, or were misread.

Examples

>>> from pyhealth.datasets import create_sample_dataset
>>> samples = [
...     {"patient_id": "p1", "notes": ["admit", "discharge"], "label": 0},
...     {"patient_id": "p2", "notes": ["admit"], "label": 1},
... ]
>>> dataset = create_sample_dataset(  
...     samples, {"notes": "raw"}, {"label": "binary"}, in_memory=False
... )
>>> dataset[0]["notes"]  
['admit', 'discharge']
process(value)[source]#

Process an individual field value.

Parameters:

value (Any) – Raw field value.

Return type:

Any

Returns:

Processed value.

size()[source]#
dim()#

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.

fit(samples, field)#

Fit the processor to the samples.

Parameters:

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

Return type:

None

is_token()#

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.

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

schema()#

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.

spatial()#

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.

stores_tensor: bool = False#