## 2 Materials and Methods
### Sleep Stages  
When sleep is analyzed in PSG, it is divided into discrete stages: wake, REM, non-REM (NREM) sleep stage 1 (N1), 2 (N2) and 3 (N3). Each stage is characterized by different criteria, as defined by consensus rules published in the American Academy of Sleep Medicine (AASM) Scoring Manual [14; 15]. N1 (sleep onset) is characterized by a slowing of EEG, the disappearance of occipital alpha waves, decreased EMG, and slow rolling eye movements, while N2 is associated with spindles and K complexes. N3 is characterized by the dominance of slow, high-amplitude waves (>20%), while REM sleep is associated with low voltage, desynchronized EEG with occasional saw tooth waves, low muscle tone and REM [16]. Generally, there is a progression from N1 to N3 and then to REM throughout the night, and this process repeats every 90 min. Each stage is associated with different physiological changes. For example, OSA is relatively less prevalent in N3 versus N2 because of the central control of breathing changes and is more severe in REM due to weakness in the upper airway muscles. Sleep continuity in depressed patients is often impaired, with more frequent wake-up periods, reduced sleep efficiency, and shortened REM [17]. Interpreting visual representations of sleep is a difficult task that requires domain knowledge and clinical experience. Sleep fragmentation and sleep stage distributions are particularly important considerations for clinician judgments, and automating this step with deep learning can help identify sleep-related conditions [18]. The rationale for considering the visual representation of the sleep stages is that clinicians often derive significant insights from visual representations of sleep architecture (as reported in Table 1). Using the 30-second scored sleep stages, we constructed image representations that capture the entire night time spent in each stage (hypondensity), as well as a sleep stage transition plot (hypnogram) [16; 19]. Figure 2 shows the sleep patterns of a patient with WSC with high OSA and severity of depression, as well as the sleep patterns of a patient in the same cohort with low OSA and severity of depression. The rationale was that the information required to build both sets of features is readily available from commercial wearable fitness trackers or sleep-tracking apps. The hypnogram provides a numeric indicator of wake and the sleep stages for the epoch score and often includes markings that include the time before the lights are turned off and the time after the lights are turned on. Labels and timestamps were provided in the dataset for removing these latter values, as well as for each epoch with the corresponding indicator. The \(x\)-axis shows the total duration of sleep throughout the night, and the \(y\)-axis shows the sleep stage label. On average, the participants in Figure 2 had a full night of sleep between 6 and 8 h, as in Patient 65492. It should be mentioned that some people woke up completely in the middle of the night and did not go back to sleep during the study, such as Patient 98255 in Figure 2.  
The hypnodensity graph was introduced in [16] as a hypnogram that does not have a strict single sleep stage label but uses a membership function for each of the sleep stages, allowing more information to be conveyed about sleep trends. A hypnogram typically assigns a single sleep stage (e.g., REM, W, N1, N2 and N3) to each epoch; the cumulative probability of sleep refers to the fact that hypnodensity can show probabilities for multiple stages at the same time. This allows for the better capture of transitions and could help identify patterns during periods where sleep stages fluctuate. This is especially useful when an epoch has multiple labels (i.e., from different sleep scorers), and this uncertainty needs to be emphasized in the graphical representations. Essentially, instead of a numeric indicator as in the hypnogram, five probabilities are given that represent the likelihood of the sleep stage for the current epoch.