Python package for high-precision circadian melatonin profile analysis
Project description
melafit
Python package for high-precision circadian melatonin profile analysis. Features a variety of baseline cosine functions for curve fitting (Van Someren & Nagtegaal, 2007) and a robust cost function for superior convergence, even with sparse data (Gabel et al., 2017).
Overview
melafit is a Python package designed for high-precision modeling of 24-hour melatonin secretion. While standard cosinor or harmonic analyses fail to capture the physiological nuances of the melatonin "wave," melafit implements several baseline cosine functions including bimodal, skewed and bimodal-skewed modifications. This approach accounts for the characteristic baseline, asymmetry and dual peaks often seen in high-resolution circadian melatonin data.
Furthermore, the library utilizes a specialized cost function developed to overcome common optimization hurdles (trivial all-zero solutions), ensuring stable convergence even when working with sparse or incomplete time series.
Key Features
- Bimodal Waveform Fitting: Implementation of the Van Someren & Nagtegaal (2007) model for superior physiological accuracy.
- Optimized Convergence: Leverages the robust cost function described in Gabel et al. (2017) to ensure reliable fits across diverse datasets.
- Sparse Data Support: Capable of reconstructing full profiles and estimating circadian phase from limited data points, as well as determining dim light melatonin onset (DLMO) with partial data.
- Research-Ready: Direct derivation of
Amplitude,DLMOn,DLMOff,Midpoint,AreaandCOGmarkers from continuous, fitted waveforms.
Installation
melafit is available on PyPI and can be
installed with pip. However, installing directly into your system Python
environment without a virtual environment is strongly discouraged, as it may
cause conflicts with other packages. The recommended approach is to use
Miniconda as the
package and environment manager to create a dedicated virtual environment, as
described below.
Standard installation
Download file
melafit.yml
to a directory of your choice (<YOUR-DIRECTORY>). Navigate to the directory,
create and activate the conda environment:
cd <YOUR-DIRECTORY>
conda env create -f melafit.yml
conda activate melafit
The environment configuration file
melafit.yml
uses conda-forge as the sole package channel, ensuring reproducibility and
avoiding potential conflicts between packages from different channels.
melafit itself is installed from PyPI
via pip as part of the environment setup. This will create a fully
functional analysis environment, including all supporting packages (numpy,
scipy, pandas, openpyxl and matplotlib).
Developer installation
If you intend to follow the development closely or contribute to the
package, clone the repository first to a dedicated directory
<YOUR-DIRECTORY>. Navigate to it and clone the repository as follows:
cd <YOUR-DIRECTORY>
git clone https://github.com/vitaliy-ch25/melafit.git
cd melafit
Then create and activate the conda environment using the developer
configuration file
melafit-dev.yml,
which installs melafit directly from the cloned directory in editable mode:
conda env create -f melafit-dev.yml
conda activate melafit
With an editable install, any changes to the source code in the cloned directory take effect immediately without reinstalling the package.
Updating
Standard update
Download the latest
melafit.yml
to <YOUR-DIRECTORY>. Navigate to it and run the update command as follows:
cd <YOUR-DIRECTORY>
conda env update -f melafit.yml --prune
This updates both the dependencies and melafit itself to the latest
released version.
Developer update
Navigate to the cloned repository directory and pull the latest version from the main branch:
cd <YOUR-DIRECTORY>/melafit
git pull
The editable install picks up the changes immediately. If dependencies in
melafit-dev.yml
have changed, also run:
conda env update -f melafit-dev.yml --prune
This updates both the dependencies and the melafit package itself to
the latest version.
Getting Started
Code example and some dummy data demonstrating melatonin profile curve fitting
with this package are included in
./examples/ and
./data/. Copy
sample scripts and datasets to your working directory and start from there. If
you have performed the steps above as described, your script will 'see' all
the required packages from any location. Simply make sure to use the virtual
environment melafit you created.
Data preparation
Follow the Excel table format and column naming conventions as in ./data/:
- Participant for study participant ID
- Date for dates of the respective samples
- Time for sample timestamps
- Mel for melatonin level values
Scientific Foundations
If you use melafit in your research, please cite the following foundational publications:
Human-Readable
- Van Someren, E. J., & Nagtegaal, E. (2007). Improving melatonin circadian phase estimates. Sleep Medicine, 8(6), 590-601.
- Gabel, V., et al. (2017). Differential impact in young and older individuals of blue-enriched white light on circadian physiology and alertness during sustained wakefulness. Scientific Reports, 7, 7620.
BibTeX
@article{vansomeren2007,
title={Improving melatonin circadian phase estimates},
author={Van Someren, Eus JW and Nagtegaal, Elsbeth},
journal={Sleep Medicine},
volume={8},
number={6},
pages={590--601},
year={2007},
publisher={Elsevier}
}
@article{gabel2017,
title={Differential impact in young and older individuals of
blue-enriched white light on circadian physiology and alertness
during sustained wakefulness},
author={Gabel, Virginie and Reichert, Carolin F and Maire, Micheline
and Schmidt, Christina and Schlangen, Luc JM
and Kolodyazhniy, Vitaliy and Garbazza, Corrado
and Cajochen, Christian and Viola, Antoine U},
journal={Scientific Reports},
volume={7},
pages={7620},
year={2017},
publisher={Nature Publishing Group}
}
If there is no associated publication on melafit yet, please cite the
package directly using the following reference:
Kolodyazhniy, V., & Cajochen, C. (2026). melafit: High-precision circadian melatonin profile analysis, version <x.y.z>. Retrieved from https://github.com/vitaliy-ch25/melafit on .
Authors
- Vitaliy Kolodyazhniy – Lead Developer
- Christian Cajochen – Scientific Lead
Revision History
v0.2.0 - Simplified API
- New module
results.pywith classes:AnalysisResult(abstract base),AnalysisInfo,AmplitudeResult,MidpointResult,AreaCogResult,FitResultandResultsCollectorfor result management FitResultwrapsscipy.optimize.OptimizeResultin itsresultfield;fit()now returnsFitResultFitResultcan be passed directly to waveform functions andcompute_waveamplitude(),midpoint()andarea_cog()now return their respective result classes instead of tuples/floatsto_dict()on all result classes: timing fields returned asHH:MMstrings, other fields as native typesAnalysisInfo.r2defaults toNaNfor convenience in DLMO workflows- New
string_to_phase()utility function inutils.py(inverse ofphase_to_string) day_profile()accepts separate times and values parametersPARAM_NAMESrenamed toBUILTIN_PARAM_NAMES- Example scripts simplified via
ResultsCollectorand result classes - Unit tests for all new classes and methods
v0.1.3
- Improved documentation
- Developer installation option
v0.1.2
- Improved documentation
v0.1.1 - First PyPI release
- Enhanced function
fit()to support custom waveform functions with user-defined initial parameters and bounds - Changed named parameter order in
fit():cost_fandcost_pare now the last two parameters - Fixed returned type hints in
func_defaults() - Additional unit tests for new functionality
- Improved README
- Package registered in Python Package Index PyPI
v0.1.0 - First public release
- Dictionary support for waveform function parameters throughout the
package: all functions accept both
dictandnp.ndarrayfor parameter input - Named parameter constants:
BCF_PARAM_NAMES,SBCF_PARAM_NAMES,BBCF_PARAM_NAMES,BSBCF_PARAM_NAMESandPARAM_NAMESlookup - New utility functions
params_to_array()andarray_to_params()for conversion between array and named dictionary representations fit()now returns named parameter dictionary asres.pin addition to the standard scipyres.xarrayfit()now acceptscost_pdictionary for passing parameters to the cost function (e.g.{"eps": 1e-6})- New utility function
params_to_string()for human-readable parameter output - Fixed
area_cog(): baseline subtraction and bin size normalization - Unit tests for all public functions in
fitting,markersandutils
Initial revisions (v0.0.1 – v0.0.9)
- Full implementation of melatonin profile analysis as described in Gabel et al. (2017)
- Waveform functions:
bcf,sbcf,bbcf,bsbcf - Markers:
amplitude,midpoint,DLMOn,DLMOff,area,cog - Utilities:
read_data,prepare_part_data,compute_wave,day_profile,abs_threshold,time_to_phase,phase_to_string,phase_diff - Example scripts:
example_dlmo.py(DLMO from partial data) andexample_full_profile.py(full profile analysis) - MIT license, packaging metadata and README
License
This project is licensed under the MIT License. See the LICENSE file for details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file melafit-0.2.0.tar.gz.
File metadata
- Download URL: melafit-0.2.0.tar.gz
- Upload date:
- Size: 29.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
04fa860278dac0069fe7cafc222038dd56b94a0a7071d31ac506d4f2b93b3975
|
|
| MD5 |
ebdb94141a65f0e99c682cfaa1c486e9
|
|
| BLAKE2b-256 |
1b925495b3aaa20be2322d00f214705f2973eefa413f55d65d67171999b76d32
|
Provenance
The following attestation bundles were made for melafit-0.2.0.tar.gz:
Publisher:
python-publish.yml on vitaliy-ch25/melafit
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
melafit-0.2.0.tar.gz -
Subject digest:
04fa860278dac0069fe7cafc222038dd56b94a0a7071d31ac506d4f2b93b3975 - Sigstore transparency entry: 1584745942
- Sigstore integration time:
-
Permalink:
vitaliy-ch25/melafit@1134c0e49f8f86c41be70d0daaab6d2586f7fbe1 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/vitaliy-ch25
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@1134c0e49f8f86c41be70d0daaab6d2586f7fbe1 -
Trigger Event:
release
-
Statement type:
File details
Details for the file melafit-0.2.0-py3-none-any.whl.
File metadata
- Download URL: melafit-0.2.0-py3-none-any.whl
- Upload date:
- Size: 19.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e132fb0fe3db0aa5382da2aabc694efb82573826fe8f7bdb0bc176339ffef1ce
|
|
| MD5 |
251fbc2af92dc5c1cde8546569977e09
|
|
| BLAKE2b-256 |
851689ec6d5957b91b28ec06030c2198c3a3002190d170bbe394ab98c8ce1726
|
Provenance
The following attestation bundles were made for melafit-0.2.0-py3-none-any.whl:
Publisher:
python-publish.yml on vitaliy-ch25/melafit
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
melafit-0.2.0-py3-none-any.whl -
Subject digest:
e132fb0fe3db0aa5382da2aabc694efb82573826fe8f7bdb0bc176339ffef1ce - Sigstore transparency entry: 1584746027
- Sigstore integration time:
-
Permalink:
vitaliy-ch25/melafit@1134c0e49f8f86c41be70d0daaab6d2586f7fbe1 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/vitaliy-ch25
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@1134c0e49f8f86c41be70d0daaab6d2586f7fbe1 -
Trigger Event:
release
-
Statement type: