This readme was generated by an AI tool, then reviewed for accuracy.
This folder contains the analysis code for turning per-subject sleep exports and diary responses into clustered sleep patterns.
The main package is sleep_analysis/.
project.pyis the top-level API.SleepProjectloads all subjects, builds the project-wide matrices, runs clustering, and provides most summary and plotting helpers.subject/subject.pyhandles one subject at a time. It reads CSV files, parses sleep rows, merges split nights, loads the general/evening/morning diaries, attaches diary rows to nights, and builds feature vectors.subject/subject_helper_functions.pyholds the preprocessing rules: timestamp parsing, night anchoring, split-night merging, gap handling, and measured-entry validation.clustering/clusterer.pywraps the clustering methods.clustering/cluster_result.pystores results and builds stable-group summaries, comparison tables, and plotting data.constants.pydefines the expected CSV paths, the feature names, and the validation and merge thresholds.
The pipeline is:
- Find each subject's sleep and diary CSVs from the path templates in
constants.py. - Parse sleep rows and drop clearly broken measurements.
- Merge multi-segment nights into one observation when the gaps and timing rules allow it.
- Merge repeated diary submissions by night anchor.
- Attach diary entries to sleep entries for the same night.
- Build two vector sets: objective vectors with 12 measured sleep features, and with-diary vectors with those 12 features plus 8 selected diary features.
- Run clustering, consensus clustering, and plotting on either all objective vectors, the diary-complete objective subset, or the with-diary vectors.
The notebooks are the main working area.
main.ipynbis a small manual entry point for loading the project, running one clustering, and plotting a few outputs.kmeans_plots.ipynb,hierarchical_plots.ipynb, andgmm_plots.ipynbevaluate candidatekvalues, run consensus clustering, and generate the main figures.DBSCAN_plots.ipynbexplores DBSCAN parameters and DBSCAN-based stable groups.selection_bias.ipynbplots the diary-completeness selection bias summary.
misc_scripts/ contains focused utilities:
diary_field_coverage.pyplots how often diary fields are present and how many nights are diary-complete.methods_pipeline_diagram.pyrenders a workflow figure with counts at each preprocessing stage.validating_diary_attachment.pychecks how well morning diary times match measured sleep times.find_duplicate_diary_rows.pyscans for duplicate diary submissions.demographics.py,progress_graph.py, andtime_usage.pyare one-off reporting scripts.
Typical use starts with:
from sleep_analysis import SleepProject
project = SleepProject().load()
project.set_active_vector_kind("objective")
project.consensus(method="hierarchical", k=5, runs=100, threshold=0.9, linkage="ward")
project.print_stable_groups()The code expects the raw CSV data to exist outside this folder at the mounted paths listed in sleep_analysis/constants.py. If the data lives elsewhere, update those path templates first.
Author disclosure on AI usage:
- AI tools such as LLMs and agentic models were used to assist in the creation of visualisation functions, and some parts of code in small pieces at a time to assist with syntax or suggestions of ways to perform tasks or goals described in plain language.
- AI tools never made high level design decisions or decisions about code structure or architecture.
- Suggested code was always verified by reviewing final versions, and manual testing with expected outputs.