@@ -34,29 +34,70 @@ class Hypnogram:
3434 """
3535 Standard class for representing and analyzing a sleep hypnogram.
3636
37- Since v0.7, YASA represents hypnograms as a dedicated object rather than a plain array of
38- integers. The main benefits are:
39-
40- * **Human-readable stages.** Sleep stages are stored as strings (``"WAKE"``, ``"N2"``,
41- ``"REM"``, ...) instead of integers, reducing the risk of misinterpretation.
42- * **Self-describing.** The object carries its own metadata: epoch duration, optional start
43- datetime with timezone, and scorer name.
44- * **Accurate alignment.** When a start datetime is provided, :py:meth:`upsample_to_data`
45- aligns the hypnogram to the EEG recording using absolute timestamps rather than sample
46- count, correctly handling recordings that start before or after the hypnogram.
47- * **Analysis built in.** Common operations — sleep statistics, stage transitions, period
48- detection, scorer agreement — are available as methods on the object itself.
49- * **Multi-modality.** Supports 2-stage actigraphy (Wake/Sleep), 4-stage wearable
50- (Wake/Light/Deep/REM), and 5-stage PSG (Wake/N1/N2/N3/REM) hypnograms.
37+ A ``Hypnogram`` is a sequence of sleep stage labels sampled at a fixed epoch duration (default
38+ 30 seconds). Three assumptions underpin every method in the class:
39+
40+ 1. **Uniform epoch duration.** Every epoch has the same length, set once via ``freq``.
41+ Variable-length epochs are not supported.
42+ 2. **Contiguous recording.** Epochs are assumed to be consecutive with no temporal gaps.
43+ 3. **Closed stage vocabulary.** Valid stage labels are fixed by ``n_stages`` at construction
44+ and cannot be customised. Supported sets are: 2-stage (Wake/Sleep), 3-stage
45+ (Wake/NREM/REM), 4-stage (Wake/Light/Deep/REM), and 5-stage (Wake/N1/N2/N3/REM).
46+ Artefact (ART) and Unscored (UNS) are always part of the vocabulary regardless of
47+ ``n_stages``.
48+
49+ Stages are stored as strings (``"WAKE"``, ``"N2"``, ``"REM"``, ...) rather than integers,
50+ reducing the risk of misinterpretation. The object also carries its own metadata: epoch
51+ duration, an optional start datetime with timezone, and an optional scorer name.
5152
5253 To create a ``Hypnogram`` from a legacy integer array, use :py:meth:`from_integers`.
5354
54- To save a ``Hypnogram`` to disk, use :py:meth:`to_json`. The ``Hypnogram`` object and all
55- its metadata can then be reloaded with :py:meth:`from_json`. To work with an in-memory
56- dictionary instead, use :py:meth:`to_dict` and :py:meth:`from_dict`.
55+ To save a ``Hypnogram`` to disk and reload it with all metadata intact, use
56+ :py:meth:`to_json` and :py:meth:`from_json`.
5757
5858 .. versionadded:: 0.7.0
5959
60+ .. rubric:: Main methods
61+
62+ .. list-table::
63+ :widths: 30 70
64+ :header-rows: 1
65+
66+ * - Method
67+ - Description
68+ * - :py:meth:`as_int`
69+ - Return hypnogram values as a :py:class:`~pandas.Series` of integers.
70+ * - :py:meth:`as_events`
71+ - Return a BIDS-compatible events :py:class:`~pandas.DataFrame` (onset, duration, stage).
72+ * - :py:meth:`get_mask`
73+ - Return a boolean array marking epochs that match one or more stage labels.
74+ * - :py:meth:`to_dict` / :py:meth:`to_json`
75+ - Serialize the hypnogram and all metadata to a dictionary or JSON file.
76+ * - :py:meth:`crop`
77+ - Slice the hypnogram by epoch index or absolute timestamp.
78+ * - :py:meth:`pad`
79+ - Extend the hypnogram before and/or after with a chosen fill stage.
80+ * - :py:meth:`upsample`
81+ - Resample the hypnogram to a finer epoch resolution.
82+ * - :py:meth:`consolidate_stages`
83+ - Merge stages to a coarser hypnogram (e.g. 5-stage to 2-stage).
84+ * - :py:meth:`upsample_to_data`
85+ - Align and upsample the hypnogram to match an EEG recording sample-by-sample.
86+ * - :py:meth:`sleep_statistics`
87+ - Compute standard AASM sleep statistics (TIB, TST, SE, WASO, stage durations, ...).
88+ * - :py:meth:`transition_matrix`
89+ - Compute the stage-transition count matrix and probability matrix.
90+ * - :py:meth:`find_periods`
91+ - Detect consecutive runs of a single stage exceeding a minimum duration.
92+ * - :py:meth:`evaluate`
93+ - Compare two hypnograms epoch-by-epoch (kappa, F1, MCC, ...).
94+ * - :py:meth:`plot_hypnogram`
95+ - Plot the hypnogram as a standard hypnogram figure.
96+ * - :py:meth:`simulate_similar`
97+ - Simulate a new hypnogram with the same transition probabilities as this one.
98+
99+ The full list of methods and attributes is available at the bottom of this page.
100+
60101 Parameters
61102 ----------
62103 values : array_like
@@ -103,36 +144,6 @@ class Hypnogram:
103144 Each row must sum to 1. This is automatically included if the hypnogram is created with
104145 :py:class:`yasa.SleepStaging`.
105146
106- Attributes
107- ----------
108- hypno : :py:class:`pandas.Series`
109- The hypnogram values as a categorical :py:class:`~pandas.Series`.
110- n_epochs : int
111- Number of epochs in the hypnogram.
112- freq : str
113- Frequency resolution of the hypnogram (e.g. ``'30s'``).
114- sampling_frequency : float
115- Sampling frequency of the hypnogram in Hz (e.g. ``1/30`` for 30-second epochs).
116- start : :py:class:`pandas.Timestamp` or None
117- Start datetime of the hypnogram. Tz-aware when ``tz`` was provided or a tz-aware
118- datetime was passed as ``start``, timezone-naive otherwise.
119- timedelta : :py:class:`pandas.TimedeltaIndex`
120- Elapsed time of each epoch relative to the first epoch.
121- duration : float
122- Total duration of the hypnogram in minutes (i.e., Time in Bed).
123- n_stages : int
124- Number of allowed sleep stages (2, 3, 4, or 5). Does not include ART and UNS.
125- labels : list
126- List of allowed stage label strings for this hypnogram.
127- mapping : dict
128- Mapping from stage string labels to integer values. Can be overridden by assignment.
129- mapping_int : dict
130- Reverse mapping from integer values to stage string labels.
131- scorer : str or None
132- Name of the scorer, if provided.
133- proba : :py:class:`pandas.DataFrame` or None
134- Per-epoch stage probabilities, if provided.
135-
136147 Examples
137148 --------
138149 Create a 2-stage hypnogram
0 commit comments