Skip to content

Commit a5bc1bf

Browse files
briannlongzhaowantingmao01adityaradk
authored
[Dataset + Taks + Model] PMData stress detection; CLIPStress raw-text input and zero-shot (#1675)
* [Dataset + Task] PMData dataset and daily stress detection Add PMDataDataset for the PMData sports-logging dataset (Fitbit, PMSYS, Google Forms, participant overview and food-image paths as 11 tables) and PMDataStressDetection, the binary daily stress task of Yang et al., "Contrastive Pretraining for Stress Detection with Multimodal Wearable Sensor Data and Surveys" (CHIL 2025). Includes synthetic-data tests, API docs, and an MLP example with task ablations (feature groups, label threshold, day vs participant split). Co-authored-by: Wanting Mao <wmao8@illinois.edu> Co-authored-by: Aditya Radhakrishnan <aditya63@illinois.edu> * [Model] CLSP contrastive language-sequence pretraining for PMData stress Add CLSP, the model of Yang et al., "Contrastive Pretraining for Stress Detection with Multimodal Wearable Sensor Data and Surveys" (CHIL 2025): a Transformer + tabular residual MLP encoder pretrained with a CLIP loss against frozen BERT embeddings of each day's text description, then linear probing or fine-tuning. Stages switch with set_stage() so every stage runs through Trainer; zero_shot() scores samples against class texts. PMDataStressDetection gains include_text / text_statistics, which add the paper's text description of each day (channel statistics and feature lines, never the label). Includes model tests with a fake text encoder, task text tests, API docs, and a full-pipeline example with ablations (soft vs one-to-one targets, text without statistics, zero-shot, 10% labels, day vs participant split). Co-authored-by: Wanting Mao <wmao8@illinois.edu> Co-authored-by: Aditya Radhakrishnan <aditya63@illinois.edu> * [Example] CLSP hyperparameter sweep, type hints - examples/pmdata_stress_detection_clsp.py: one-at-a-time sweep of the contrastive temperature (0.03, 0.2) and hidden/projection size (32, 128), with real-data results and findings in the docstring. - Annotate **kwargs and PMDataDataset.default_task's return type. Co-authored-by: Wanting Mao <wmao8@illinois.edu> Co-authored-by: Aditya Radhakrishnan <aditya63@illinois.edu> * [Example] Comment each ablation in the PMData examples Co-authored-by: Wanting Mao <wmao8@illinois.edu> Co-authored-by: Aditya Radhakrishnan <aditya63@illinois.edu> * Fix PR rule check: modern type hints, ClassVar schemas, narrow except - Use built-in generics and X | None (ruff UP006/UP007/UP035/UP045). - Annotate the task's class-level schemas with ClassVar (RUF012). - Catch only the errors reading a photo's EXIF time can raise (BLE001). - from torch import nn (PLR0402); same style applied to tests and examples. Co-authored-by: Wanting Mao <wmao8@illinois.edu> Co-authored-by: Aditya Radhakrishnan <aditya63@illinois.edu> * Use the existing CLIPStress instead of a second model; add raw text and zero-shot PyHealth already has CLIPStress (#1516) for the same paper, so CLSP is removed and the PMData pipeline runs on CLIPStress, extended with: - text_encoder: CLIPStress can take a task's raw text field directly, embedded by a frozen encoder cached per text (FrozenTextEncoder in clip_stress_utils) and kept out of the state dict. Cached text_embedding tensors work exactly as before. - zero_shot(): label-free prediction by similarity to class descriptions, scored as in pretraining. Tests in tests/core/test_clip_stress_text.py; the existing CLIPStress tests pass unchanged. The full pipeline example is now examples/pmdata_stress_detection_clip_stress.py. Co-authored-by: Wanting Mao <wmao8@illinois.edu> Co-authored-by: Aditya Radhakrishnan <aditya63@illinois.edu> * Real-data CLIPStress results and findings in the PMData pipeline example Co-authored-by: Wanting Mao <wmao8@illinois.edu> Co-authored-by: Aditya Radhakrishnan <aditya63@illinois.edu> * Type hints for the CLIPStress extension test helpers Co-authored-by: Wanting Mao <wmao8@illinois.edu> Co-authored-by: Aditya Radhakrishnan <aditya63@illinois.edu> --------- Co-authored-by: Wanting Mao <wmao8@illinois.edu> Co-authored-by: Aditya Radhakrishnan <aditya63@illinois.edu>
1 parent 35170a1 commit a5bc1bf

16 files changed

Lines changed: 3609 additions & 3 deletions

‎docs/api/datasets.rst‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,11 @@ PyHealth does not ship any data: you download each dataset from its source and p
410410
- `Kaggle <https://www.kaggle.com/datasets/tawsifurrahman/covid19-radiography-database>`__
411411
- No
412412
- —
413+
* - :doc:`PMData <datasets/pyhealth.datasets.PMDataDataset>`
414+
- Open (CC BY-NC 4.0)
415+
- `Simula <https://datasets.simula.no/pmdata/>`__
416+
- No
417+
- —
413418
* - :doc:`ISIC 2019 <datasets/pyhealth.datasets.ISIC2019Dataset>`
414419
- Open (CC BY-NC 4.0)
415420
- `ISIC <https://challenge.isic-archive.com/data/#2019>`__
@@ -673,6 +678,7 @@ Available Datasets
673678
datasets/pyhealth.datasets.OMOPDataset
674679
datasets/pyhealth.datasets.SyntheaCSVDataset
675680
datasets/pyhealth.datasets.DREAMTDataset
681+
datasets/pyhealth.datasets.PMDataDataset
676682
datasets/pyhealth.datasets.SHHSDataset
677683
datasets/pyhealth.datasets.SleepEDFDataset
678684
datasets/pyhealth.datasets.NSRRSleepDataset
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
pyhealth.datasets.PMDataDataset
2+
===============================
3+
4+
Overview
5+
--------
6+
7+
PMData is a sports logging dataset of 16 recreational athletes followed for
8+
about five months (November 2019 to March 2020). It combines a Fitbit Versa 2
9+
smartwatch (minute-level steps, calories and distance, heart rate every few
10+
seconds, sleep, exercise), the PMSYS app (a daily wellness report with a 1-5
11+
stress score, training sessions and injuries), daily Google Forms reports
12+
(meals, weight, fluids, alcohol), a participant overview spreadsheet and, for
13+
three participants, photos of their meals.
14+
15+
The data is open (CC BY-NC 4.0). Download the archive from the source below,
16+
unzip it and pass the folder that holds ``p01`` ... ``p16`` and
17+
``participant-overview.xlsx`` as ``root``. On first use the raw files are
18+
converted into ``pmdata-<table>.csv`` files in ``root`` (heart rate becomes
19+
hourly means, other per-minute signals hourly sums); food photos are indexed
20+
by path and EXIF capture time, not loaded.
21+
22+
The dataset is used for daily stress detection by
23+
:class:`~pyhealth.tasks.PMDataStressDetection`, following Yang et al.,
24+
"Contrastive Pretraining for Stress Detection with Multimodal Wearable Sensor
25+
Data and Surveys" (CHIL 2025).
26+
27+
Source: `PMData (Simula) <https://datasets.simula.no/pmdata/>`_; paper:
28+
Thambawita et al., `PMData: a sports logging dataset
29+
<https://doi.org/10.1145/3339825.3394926>`_ (MMSys 2020).
30+
31+
API Reference
32+
-------------
33+
34+
.. autoclass:: pyhealth.datasets.PMDataDataset
35+
:members:
36+
:undoc-members:
37+
:show-inheritance:

‎docs/api/models/pyhealth.models.CLIPStress.rst‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,36 @@ ordinal categorical fields retain their codes. Text must be regenerated from
8484
the normalized sensor windows separately for each fold. A single globally
8585
cached text archive cannot represent this protocol.
8686

87+
Raw text and zero-shot prediction
88+
---------------------------------
89+
90+
Instead of cached ``text_embedding`` vectors, ``text_key`` may name a ``raw``
91+
string field, such as the ``text`` description produced by
92+
``PMDataStressDetection(include_text=True)``. Pass ``text_encoder`` (a Hugging
93+
Face model name, or a callable ``(texts, device) -> tensor`` with an
94+
``embedding_dim`` attribute): ``FrozenTextEncoder`` embeds each distinct text
95+
once with gradients off, averaging token states over the attention mask, and
96+
caches the result. The encoder is held outside the module tree, so it is not
97+
trained and not saved in the state dict; classification stages never call it.
98+
99+
``zero_shot`` classifies samples without labels or a classifier: each sample's
100+
sensor/table projection is compared with the text projection of one
101+
description per class (default ``"label: relaxed"`` and ``"label: stressed"``),
102+
scored as in pretraining and divided by the temperature, then softmaxed.
103+
Models trained on cached vectors pass ``class_embeddings`` instead::
104+
105+
task = PMDataStressDetection(include_text=True)
106+
train, val, test = dataset.set_task(task)
107+
model = CLIPStress(train, signal_key="wearable", tabular_key="survey",
108+
text_key="text", text_encoder="bert-base-uncased",
109+
objective="paper_cosine")
110+
# ... pretrain with Trainer, then:
111+
model.eval()
112+
probs = model.zero_shot(**batch) # (batch, 2): P(relaxed), P(stressed)
113+
114+
``examples/pmdata_stress_detection_clip_stress.py`` runs the complete PMData
115+
pipeline with these options.
116+
87117
Runnable comparisons
88118
--------------------
89119

‎docs/api/tasks.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ Available Tasks
266266
Patient Linkage (MIMIC-III, legacy) <tasks/pyhealth.tasks.patient_linkage_mimic3_fn>
267267
Patient Linkage (MIMIC-III) <tasks/pyhealth.tasks.patient_linkage_mimic3>
268268
Readmission Prediction <tasks/pyhealth.tasks.readmission_prediction>
269+
Stress Detection (PMData) <tasks/pyhealth.tasks.PMDataStressDetection>
269270
Sleep Staging <tasks/pyhealth.tasks.sleep_staging>
270271
Sleep Staging (SleepEDF) <tasks/pyhealth.tasks.SleepStagingSleepEDF>
271272
Sleep-EDF Forecasting <tasks/pyhealth.tasks.SleepEDFForecasting>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
pyhealth.tasks.PMDataStressDetection
2+
====================================
3+
4+
Binary daily stress detection on :class:`~pyhealth.datasets.PMDataDataset`,
5+
following the data preparation of Yang et al., `Contrastive Pretraining for
6+
Stress Detection with Multimodal Wearable Sensor Data and Surveys
7+
<https://proceedings.mlr.press/v287/yang25a.html>`_ (CHIL 2025). Each
8+
participant-day with a wellness report becomes one sample with the day's 24
9+
hourly Fitbit values, a vector of daily tabular features, and a label that is
10+
1 when the self-reported 1-5 stress score is above 3.
11+
12+
.. autoclass:: pyhealth.tasks.PMDataStressDetection
13+
:members:
14+
:undoc-members:
15+
:show-inheritance:

0 commit comments

Comments
 (0)