Repository navigation
Towards Unified Sequence Representation I: Add SequenceProcessorV2 for packed hierarchical records #1343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
+2,793
−13
Open
Towards Unified Sequence Representation I: Add SequenceProcessorV2 for packed hierarchical records #1343
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
188bf9b
Add CrossMap.has_many_targets to tell x-to-one mappings from x-to-many
Logiquo 1417039
Add SequenceProcessorV2 for hierarchical packed records
Logiquo 000f427
Add imputation to SequenceProcessorV2 and split out resampling
Logiquo 2b0512b
Add normalization to SequenceProcessorV2
Logiquo 7cbfa7b
Add missing indicators to SequenceProcessorV2
Logiquo f893816
Store SequenceProcessorV2 fields as tensors and batch them with colla…
Logiquo 90fdd9a
Fix time-based imputation on a level without records
Logiquo 51f5c7e
Add docstring examples and modern annotations for the PR rules
Logiquo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
docs/api/processors/pyhealth.processors.SequenceProcessorV2.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| pyhealth.processors.SequenceProcessorV2 | ||
| ======================================= | ||
|
|
||
| Processor for hierarchical packed records: codes, values and timestamps at any | ||
| depth of nesting, stored without padding or truncation. | ||
|
|
||
| The task returns each field as a ``Records``, a plain dict of columns. | ||
| ``children`` holds the records one level down, and the innermost records | ||
| (level 0) have no ``children``. The processor emits a tuple of tensors in | ||
| ``schema()`` order: by level, then by role (``tokens``, ``values``, ``mask``, | ||
| ``time``, ``offsets``). ``offsets`` at level ``l`` partitions the level-``l`` records into | ||
| level-``l + 1`` records, in CSR layout. | ||
|
|
||
| Each level's configuration declares its roles: ``tokens`` gives the number of | ||
| token namespaces ``K`` and ``values`` gives the value shape ``S``. ``fit()`` | ||
| only builds the vocabularies, and every record is checked against the | ||
| configuration. | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| proc = SequenceProcessorV2(levels=[{"ordering": "set", "tokens": 1}, {"ordering": "sequence"}]) | ||
| samples = [{"visits": {"children": [{"tokens": ["A", "B"]}, {"tokens": ["C"]}]}}] | ||
| proc.fit(samples, "visits") | ||
| proc.schema() # ((0, 'tokens'), (0, 'offsets')) | ||
| proc.process({"children": [{"tokens": ["A", "B"]}, {"tokens": []}, {"tokens": ["C"]}]}) | ||
| # (tensor([2, 3, 4]), tensor([0, 2, 2, 3])) | ||
|
|
||
| In a task schema the configuration is plain data: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| input_schema = { | ||
| "visits": ("sequence_v2", {"levels": [{"ordering": "set", "tokens": 1}, {"ordering": "sequence"}]}), | ||
| "vitals": ("sequence_v2", {"levels": [{"ordering": "series", "values": [2], "resample": {"step": 1.0, "method": "last"}}]}), | ||
| } | ||
|
|
||
| Batches are packed with ``collate()``, which follows ``batch_schema()``: the | ||
| fields of ``schema()`` plus the batch offsets ``[B + 1]``. After resampling, | ||
| ``impute`` fills NaN values (see ``Impute``) and ``normalize`` scales them (see | ||
| ``Normalize``). With ``missing_indicator`` (which needs ``impute``), a level | ||
| also emits ``mask`` right after ``values``: a bool tensor of the same shape, | ||
| True where a value was missing before imputation. Values still containing NaN | ||
| or inf raise. | ||
|
|
||
| .. autoclass:: pyhealth.processors.SequenceProcessorV2 | ||
| :members: | ||
| :undoc-members: | ||
| :show-inheritance: | ||
|
|
||
| .. autoclass:: pyhealth.processors.Records | ||
| :members: | ||
|
|
||
| .. autoclass:: pyhealth.processors.LevelConfig | ||
| :members: | ||
|
|
||
| .. autoclass:: pyhealth.processors.Resample | ||
| :members: | ||
|
|
||
| .. autoclass:: pyhealth.processors.Impute | ||
| :members: | ||
|
|
||
| .. autoclass:: pyhealth.processors.Normalize | ||
| :members: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[low] Tuple-of-tensors storage does not enforce the same-length rule the docs state
The base-class and encode docs say a tuple stored with stores_tensor must have the same length in every sample, but encode only checks that every item is a tensor. A processor that varies the length will fail inside litdata with an unclear error rather than the ValueError the function raises for other type mismatches. Add a length check against the first sample, or soften the doc wording.