Loading...
Loading...
Compare original and translation side by side
data-example/sleep-tracker.jsondata-example/sleep-logs/YYYY-MM/YYYY-MM-DD.jsondata-example/fitness-tracker.jsondata-example/hypertension-tracker.jsondata-example/diabetes-tracker.jsondata-example/diet-records/data-example/mood-tracker.jsondata-example/sleep-tracker.jsondata-example/sleep-logs/YYYY-MM/YYYY-MM-DD.jsondata-example/fitness-tracker.jsondata-example/hypertension-tracker.jsondata-example/diabetes-tracker.jsondata-example/diet-records/data-example/mood-tracker.jsonundefinedundefined6.5h ┤ ╭╮
6.0h ┤ ╭─╯╰╮
5.5h ┤ ╭─╯ ╰─╮
5.0h ┼─┘ ╰─
└───────────
3月 4月 5月 6月6.5h ┤ ╭╮
6.0h ┤ ╭─╯╰╮
5.5h ┤ ╭─╯ ╰─╮
5.0h ┼─┘ ╰─
└───────────
Mar Apr May Jun| 质量等级 | 天数 | 占比 | 趋势 |
|---|---|---|---|
| 优秀 | 8 | 9% | ⬆️ |
| 很好 | 12 | 13% | ➡️ |
| 好 | 15 | 17% | ⬆️ |
| 一般 | 42 | 47% | ⬇️ |
| 差 | 10 | 11% | ⬇️ |
| 很差 | 3 | 3% | ➡️ |
| Quality Level | Days | Percentage | Trend |
|---|---|---|---|
| Excellent | 8 | 9% | ⬆️ |
| Very Good | 12 | 13% | ➡️ |
| Good | 15 | 17% | ⬆️ |
| Fair | 42 | 47% | ⬇️ |
| Poor | 10 | 11% | ⬇️ |
| Very Poor | 3 | 3% | ➡️ |
12 ┤ ●
10 ┤ ●
8 ┤ ●
6 ┤
└──────
12月 3月 6月12 ┤ ●
10 ┤ ●
8 ┤ ●
6 ┤
└──────
Dec Mar Jun
---
---{
"sleep_records": [
{
"id": "sleep_20250620001",
"date": "2025-06-20",
"sleep_times": {
"bedtime": "23:00",
"sleep_onset_time": "23:30",
"wake_time": "07:00",
"out_of_bed_time": "07:15"
},
"sleep_metrics": {
"sleep_duration_hours": 7.0,
"time_in_bed_hours": 8.25,
"sleep_latency_minutes": 30,
"sleep_efficiency": 84.8
},
"sleep_quality": {
"subjective_quality": "fair",
"quality_score": 5,
"rested_feeling": "somewhat"
},
"factors": {
"exercise": true,
"exercise_time": "evening",
"caffeine_after_2pm": false,
"screen_time_before_bed_minutes": 60
}
}
]
}{
"sleep_records": [
{
"id": "sleep_20250620001",
"date": "2025-06-20",
"sleep_times": {
"bedtime": "23:00",
"sleep_onset_time": "23:30",
"wake_time": "07:00",
"out_of_bed_time": "07:15"
},
"sleep_metrics": {
"sleep_duration_hours": 7.0,
"time_in_bed_hours": 8.25,
"sleep_latency_minutes": 30,
"sleep_efficiency": 84.8
},
"sleep_quality": {
"subjective_quality": "fair",
"quality_score": 5,
"rested_feeling": "somewhat"
},
"factors": {
"exercise": true,
"exercise_time": "evening",
"caffeine_after_2pm": false,
"screen_time_before_bed_minutes": 60
}
}
]
}def calculate_sleep_quality_score(record):
"""
计算睡眠质量评分(0-10分)
因素权重:
- 睡眠时长:30%
- 睡眠效率:25%
- 入睡潜伏期:20%
- 夜间觉醒:15%
- 主观质量:10%
"""
score = 0
# 睡眠时长评分(理想7-9小时)
duration = record['sleep_duration_hours']
if 7 <= duration <= 9:
duration_score = 10
elif 6 <= duration < 7 or 9 < duration <= 10:
duration_score = 7
else:
duration_score = 4
score += duration_score * 0.30
# 睡眠效率评分(>90%优秀)
efficiency = record['sleep_efficiency']
efficiency_score = min(efficiency / 90 * 10, 10)
score += efficiency_score * 0.25
# 入睡潜伏期评分(<15分钟优秀)
latency = record['sleep_latency_minutes']
if latency <= 15:
latency_score = 10
elif latency <= 30:
latency_score = 7
elif latency <= 45:
latency_score = 4
else:
latency_score = 1
score += latency_score * 0.20
# 夜间觉醒评分(0次优秀)
awakenings = record['awakenings']['count']
awakening_score = max(10 - awakenings * 2, 0)
score += awakening_score * 0.15
# 主观质量评分
quality_map = {
'excellent': 10,
'very_good': 8,
'good': 7,
'fair': 5,
'poor': 3,
'very_poor': 1
}
subjective_score = quality_map.get(
record['sleep_quality']['subjective_quality'],
5
)
score += subjective_score * 0.10
return round(score, 1)def calculate_sleep_quality_score(record):
"""
Calculate sleep quality score (0-10 points)
Factor Weights:
- Sleep Duration: 30%
- Sleep Efficiency: 25%
- Sleep Onset Latency: 20%
- Nighttime Awakenings: 15%
- Subjective Quality: 10%
"""
score = 0
# Sleep Duration Score (ideal 7-9 hours)
duration = record['sleep_duration_hours']
if 7 <= duration <= 9:
duration_score = 10
elif 6 <= duration < 7 or 9 < duration <= 10:
duration_score = 7
else:
duration_score = 4
score += duration_score * 0.30
# Sleep Efficiency Score (>90% excellent)
efficiency = record['sleep_efficiency']
efficiency_score = min(efficiency / 90 * 10, 10)
score += efficiency_score * 0.25
# Sleep Onset Latency Score (<15 minutes excellent)
latency = record['sleep_latency_minutes']
if latency <= 15:
latency_score = 10
elif latency <= 30:
latency_score = 7
elif latency <= 45:
latency_score = 4
else:
latency_score = 1
score += latency_score * 0.20
# Nighttime Awakening Score (0 times excellent)
awakenings = record['awakenings']['count']
awakening_score = max(10 - awakenings * 2, 0)
score += awakening_score * 0.15
# Subjective Quality Score
quality_map = {
'excellent': 10,
'very_good': 8,
'good': 7,
'fair': 5,
'poor': 3,
'very_poor': 1
}
subjective_score = quality_map.get(
record['sleep_quality']['subjective_quality'],
5
)
score += subjective_score * 0.10
return round(score, 1)def calculate_sleep_consistency_score(records):
"""
计算作息规律性评分(0-100分)
因素:
- 上床时间标准差
- 起床时间标准差
- 睡眠时长标准差
- 工作日vs周末差异
"""
# 提取时间数据
bedtimes = [r['bedtime'] for r in records]
wake_times = [r['wake_time'] for r in records]
durations = [r['sleep_duration_hours'] for r in records]
# 计算标准差(分钟)
bedtime_std = time_to_minutes_std(bedtimes)
wake_std = time_to_minutes_std(wake_times)
duration_std = statistics.stdev(durations)
# 计算工作日vs周末差异
weekday_avg = avg([r['sleep_duration_hours']
for r in records if is_weekday(r)])
weekend_avg = avg([r['sleep_duration_hours']
for r in records if is_weekend(r)])
diff = abs(weekday_avg - weekend_avg)
# 综合评分
score = 100
score -= bedtime_std * 0.5 # 上床时间标准差影响
score -= wake_std * 0.5 # 起床时间标准差影响
score -= duration_std * 2 # 睡眠时长标准差影响
score -= diff * 10 # 工作日周末差异影响
return max(0, min(100, round(score)))def calculate_sleep_consistency_score(records):
"""
Calculate sleep consistency score (0-100 points)
Factors:
- Bedtime standard deviation
- Wake-up time standard deviation
- Sleep duration standard deviation
- Weekday vs. weekend difference
"""
# Extract time data
bedtimes = [r['bedtime'] for r in records]
wake_times = [r['wake_time'] for r in records]
durations = [r['sleep_duration_hours'] for r in records]
# Calculate standard deviation (minutes)
bedtime_std = time_to_minutes_std(bedtimes)
wake_std = time_to_minutes_std(wake_times)
duration_std = statistics.stdev(durations)
# Calculate weekday vs. weekend difference
weekday_avg = avg([r['sleep_duration_hours']
for r in records if is_weekday(r)])
weekend_avg = avg([r['sleep_duration_hours']
for r in records if is_weekend(r)])
diff = abs(weekday_avg - weekend_avg)
# Comprehensive score
score = 100
score -= bedtime_std * 0.5 # Impact of bedtime standard deviation
score -= wake_std * 0.5 # Impact of wake-up time standard deviation
score -= duration_std * 2 # Impact of sleep duration standard deviation
score -= diff * 10 # Impact of weekday-weekend difference
return max(0, min(100, round(score)))def calculate_correlation(sleep_data, other_data, lag_days=0):
"""
计算睡眠与其他指标的相关性
参数:
- sleep_data: 睡眠数据列表
- other_data: 其他指标数据列表
- lag_days: 滞后天数(考虑延迟效应)
返回:
- correlation_coefficient: 相关系数
- p_value: 统计显著性
- interpretation: 相关性解释
"""
# 对齐数据(考虑滞后)
aligned = align_data_with_lag(sleep_data, other_data, lag_days)
# 计算Pearson相关系数
from scipy import stats
corr, p_value = stats.pearsonr(
aligned['sleep_values'],
aligned['other_values']
)
# 解释相关性
if abs(corr) < 0.3:
strength = "弱"
elif abs(corr) < 0.7:
strength = "中等"
else:
strength = "强"
direction = "正相关" if corr > 0 else "负相关"
significant = p_value < 0.05
interpretation = f"{strength}{direction}"
if significant:
interpretation += "(统计学显著)"
return {
'correlation_coefficient': round(corr, 3),
'p_value': round(p_value, 4),
'interpretation': interpretation,
'significant': significant
}def calculate_correlation(sleep_data, other_data, lag_days=0):
"""
Calculate correlation between sleep and other indicators
Parameters:
- sleep_data: List of sleep data
- other_data: List of other indicator data
- lag_days: Number of lag days (considering delay effects)
Returns:
- correlation_coefficient: Correlation coefficient
- p_value: Statistical significance
- interpretation: Correlation explanation
"""
# Align data (considering lag)
aligned = align_data_with_lag(sleep_data, other_data, lag_days)
# Calculate Pearson correlation coefficient
from scipy import stats
corr, p_value = stats.pearsonr(
aligned['sleep_values'],
aligned['other_values']
)
# Interpret correlation
if abs(corr) < 0.3:
strength = "Weak"
elif abs(corr) < 0.7:
strength = "Moderate"
else:
strength = "Strong"
direction = "Positive Correlation" if corr > 0 else "Negative Correlation"
significant = p_value < 0.05
interpretation = f"{strength} {direction}"
if significant:
interpretation += " (Statistically Significant)"
return {
'correlation_coefficient': round(corr, 3),
'p_value': round(p_value, 4),
'interpretation': interpretation,
'significant': significant
}