Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/api/metrics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ reference includes the ECE definition and formula used by these helpers.
metrics/pyhealth.metrics.generative
metrics/pyhealth.metrics.survival
metrics/pyhealth.metrics.survival_curve
metrics/pyhealth.metrics.anomaly_detection
21 changes: 21 additions & 0 deletions docs/api/metrics/pyhealth.metrics.anomaly_detection.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
pyhealth.metrics.anomaly_detection
===================================

Point-wise evaluation of reconstruction-based time-series anomaly detection,
following MedTsLLM (`Chan et al., MLHC 2024 <https://arxiv.org/abs/2408.07773>`_):
per-step scores from reconstruction errors, a quantile threshold, optional
point adjustment, and the resulting classification metrics. The threshold-free
AUROC of the raw scores is reported as well.

The threshold flags the top ``contamination`` fraction of scores. The authors'
"auto" contamination is the number of anomalous test steps divided by the
number of test plus training steps. It uses the test labels, so it is
optimistic; a contamination from the training labels is the stricter choice.

.. currentmodule:: pyhealth.metrics.anomaly_detection

.. autofunction:: reconstruction_anomaly_scores

.. autofunction:: point_adjust

.. autofunction:: anomaly_detection_metrics
1 change: 1 addition & 0 deletions docs/api/models.rst
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ API Reference
models/pyhealth.models.DescEmb
models/pyhealth.models.bulk_rna_bert
models/pyhealth.models.DearLLM
models/pyhealth.models.MedTsLLMAnomaly
models/pyhealth.models.CHET
models/pyhealth.models.GAME
models/pyhealth.models.MedTok
Expand Down
35 changes: 35 additions & 0 deletions docs/api/models/pyhealth.models.MedTsLLMAnomaly.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
pyhealth.models.MedTsLLMAnomaly
===================================

MedTsLLM (`Chan et al., MLHC 2024 <https://arxiv.org/abs/2408.07773>`_) for
reconstruction-based anomaly detection in multichannel signals such as the
two-lead MIT-BIH ECG. Windows are normalized per instance (RevIN), cut into
overlapping patches, and each patch is mapped into a frozen LLM's input
embedding space by cross-attention onto text prototypes built from the LLM's
word embeddings. A text prompt (dataset and task description, optionally a
patient description) is prepended, and a linear head reconstructs the window
from the LLM's outputs at the patch positions. Only these adapter layers are
trained.

This is a port of the authors' implementation restricted to the paper's
anomaly detection configuration; with the same weights and inputs it computes
the same outputs. The model reports ``mode == "regression"`` (reconstruction
against the standardized input), so it trains with the standard ``Trainer``.
Score the reconstructions with :mod:`pyhealth.metrics.anomaly_detection`.
See ``examples/mitbih_anomaly_detection_medtsllm.py`` for the full pipeline
with :class:`~pyhealth.datasets.MITBIHDataset` and
:class:`~pyhealth.tasks.MITBIHAnomalyDetection`.

.. autoclass:: pyhealth.models.MedTsLLMAnomaly
:members:
:undoc-members:
:show-inheritance:

.. autoclass:: pyhealth.models.medtsllm_anomaly.RevIN
:members:

.. autoclass:: pyhealth.models.medtsllm_anomaly.PatchEmbedding
:members:

.. autoclass:: pyhealth.models.medtsllm_anomaly.ReprogrammingLayer
:members:
Loading
Loading