Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

Sleep analysis

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.py is the top-level API. SleepProject loads all subjects, builds the project-wide matrices, runs clustering, and provides most summary and plotting helpers.
  • subject/subject.py handles 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.py holds the preprocessing rules: timestamp parsing, night anchoring, split-night merging, gap handling, and measured-entry validation.
  • clustering/clusterer.py wraps the clustering methods.
  • clustering/cluster_result.py stores results and builds stable-group summaries, comparison tables, and plotting data.
  • constants.py defines the expected CSV paths, the feature names, and the validation and merge thresholds.

The pipeline is:

  1. Find each subject's sleep and diary CSVs from the path templates in constants.py.
  2. Parse sleep rows and drop clearly broken measurements.
  3. Merge multi-segment nights into one observation when the gaps and timing rules allow it.
  4. Merge repeated diary submissions by night anchor.
  5. Attach diary entries to sleep entries for the same night.
  6. Build two vector sets: objective vectors with 12 measured sleep features, and with-diary vectors with those 12 features plus 8 selected diary features.
  7. 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.ipynb is a small manual entry point for loading the project, running one clustering, and plotting a few outputs.
  • kmeans_plots.ipynb, hierarchical_plots.ipynb, and gmm_plots.ipynb evaluate candidate k values, run consensus clustering, and generate the main figures.
  • DBSCAN_plots.ipynb explores DBSCAN parameters and DBSCAN-based stable groups.
  • selection_bias.ipynb plots the diary-completeness selection bias summary.

misc_scripts/ contains focused utilities:

  • diary_field_coverage.py plots how often diary fields are present and how many nights are diary-complete.
  • methods_pipeline_diagram.py renders a workflow figure with counts at each preprocessing stage.
  • validating_diary_attachment.py checks how well morning diary times match measured sleep times.
  • find_duplicate_diary_rows.py scans for duplicate diary submissions.
  • demographics.py, progress_graph.py, and time_usage.py are 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.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages