MySleepStage.Core 1.1.0

dotnet add package MySleepStage.Core --version 1.1.0
                    
NuGet\Install-Package MySleepStage.Core -Version 1.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MySleepStage.Core" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MySleepStage.Core" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="MySleepStage.Core" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MySleepStage.Core --version 1.1.0
                    
#r "nuget: MySleepStage.Core, 1.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package MySleepStage.Core@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MySleepStage.Core&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=MySleepStage.Core&version=1.1.0
                    
Install as a Cake Tool

MySleepStage.Core

睡眠分期分析组件,提供数据清洗、数据预处理、数据平滑和睡眠分期分类功能。

功能特性

  • 数据清洗: 剔除无效数据,包括无人状态超过阈值或移动幅度过大后的数据
  • 数据预处理: 差值计算、加窗平滑、完整平滑(移动平均、高斯滤波、样条插值)
  • 数据平滑: 独立的平滑服务,支持移动平均、高斯滤波、样条插值、自适应阈值过滤
  • 睡眠分期: 支持离床、清醒、浅睡、深睡四种睡眠状态识别

安装

dotnet add package MySleepStage.Core

使用方法

1. 注册服务

using MySleepStage.Core.Extensions;

// 使用默认配置
services.AddSleepStageAnalysis();

// 或自定义配置
services.AddSleepStageAnalysis(options =>
{
    options.WindowSizeMinutes = 5;
    options.MonitoringStartHour = 22;
    options.MonitoringEndHour = 8;
    options.DefaultPreprocessingMode = PreprocessingMode.Auto;
    
    // 平滑配置
    options.MovingAverageWindowSize = 12;
    options.GaussianSigma = 2.5;
    options.SplineSmoothingParameter = 150;
    options.AdaptiveSensitivityFactor = 0.7;
});

2. 数据清洗

using MySleepStage.Core.Services;
using MySleepStage.Core.Models;

public class MyService
{
    private readonly IDataCleaningService _cleaningService;
    
    public MyService(IDataCleaningService cleaningService)
    {
        _cleaningService = cleaningService;
    }
    
    public void ProcessData(List<VitalSignData> rawData)
    {
        // 清洗数据
        var cleanedData = _cleaningService.CleanData(rawData);
        
        // 分割为窗口数据
        var windowDataList = _cleaningService.SplitIntoWindows(cleanedData);
        
        // 单独使用清洗功能
        var afterNoPeople = _cleaningService.RemoveNoPeopleSegments(rawData, thresholdMinutes: 45);
        var afterMovement = _cleaningService.RemoveLargeMovementSegments(afterNoPeople, movementThreshold: 200, afterSeconds: 60);
        var finalData = _cleaningService.FilterByMonitoringPeriod(afterMovement);
    }
}

3. 数据预处理

using MySleepStage.Core.Services;
using MySleepStage.Core.Models;

public class MyService
{
    private readonly IDataPreprocessingService _preprocessingService;
    private readonly IDataSmoothingService _smoothingService;
    
    public MyService(
        IDataPreprocessingService preprocessingService,
        IDataSmoothingService smoothingService)
    {
        _preprocessingService = preprocessingService;
        _smoothingService = smoothingService;
    }
    
    public void ProcessData()
    {
        // 预处理窗口数据
        var preprocessedWindow = _preprocessingService.Preprocess(windowData, PreprocessingMode.Auto);
        
        // 预处理单个数据数组
        double[] data = new double[] { 1.0, 2.0, 3.0, 4.0, 5.0 };
        var result = _preprocessingService.Preprocess(data, PreprocessingMode.DifferenceWithFullSmooth, _smoothingService);
        
        // 批量预处理
        var dataArray = new Dictionary<string, double[]>
        {
            { "HeartRate", heartRateData },
            { "BreathRate", breathRateData }
        };
        var batchResult = _preprocessingService.PreprocessBatch(dataArray, PreprocessingMode.Auto, _smoothingService);
        
        // 生成预处理报告
        var report = _preprocessingService.GenerateReport(originalData, preprocessedData, PreprocessingMode.Auto);
        Console.WriteLine(report);
        
        // 评估差值质量
        var assessment = _preprocessingService.AssessDifferenceQuality(differenceData);
        Console.WriteLine($"质量合格: {assessment.IsQualityAcceptable}, 推荐模式: {assessment.RecommendedMode}");
    }
}

4. 数据平滑

using MySleepStage.Core.Services;

public class MyService
{
    private readonly IDataSmoothingService _smoothingService;
    
    public MyService(IDataSmoothingService smoothingService)
    {
        _smoothingService = smoothingService;
    }
    
    public void SmoothData(double[] data)
    {
        // 三阶段组合平滑(移动平均 → 高斯滤波 → 样条插值)
        var smoothed = _smoothingService.EnhancedCombinedSmooth(data);
        
        // 单独使用各种平滑方法
        var maResult = _smoothingService.MovingAverage(data, windowSize: 12);
        var gaussianResult = _smoothingService.GaussianFilter(data, sigma: 2.5);
        var splineResult = _smoothingService.SplineSmooth(data);
        
        // 自适应阈值过滤
        var adaptiveResult = _smoothingService.AdaptiveThresholdFilter(data, sensitivityFactor: 0.7);
    }
}

5. 睡眠分期

using MySleepStage.Core.Services;
using MySleepStage.Core.Models;

public class MyService
{
    private readonly ISleepStageClassificationService _classificationService;
    
    public MyService(ISleepStageClassificationService classificationService)
    {
        _classificationService = classificationService;
    }
    
    public void AnalyzeSleep(WindowData windowData)
    {
        // 单个窗口分类
        var result = _classificationService.Classify(windowData);
        Console.WriteLine($"睡眠状态: {result.SleepState}, 置信度: {result.Confidence}");
        
        // 批量分类
        var windowDataList = new List<WindowData> { window1, window2, window3 };
        var results = _classificationService.ClassifyBatch(windowDataList);
        
        // 使用高级功能
        var standardized = _classificationService.RobustStandardize(data);
        var boundaries = _classificationService.KMeansClustering(data, clusters: 4);
        var level = _classificationService.SensitivityReducedClassify(value, boundaries, sensitivityFactor: 0.8, lastClassification: 2);
    }
}

6. 完整处理流程

public class SleepAnalysisPipeline
{
    private readonly IDataCleaningService _cleaningService;
    private readonly IDataPreprocessingService _preprocessingService;
    private readonly IDataSmoothingService _smoothingService;
    private readonly ISleepStageClassificationService _classificationService;
    
    public SleepAnalysisPipeline(
        IDataCleaningService cleaningService,
        IDataPreprocessingService preprocessingService,
        IDataSmoothingService smoothingService,
        ISleepStageClassificationService classificationService)
    {
        _cleaningService = cleaningService;
        _preprocessingService = preprocessingService;
        _smoothingService = smoothingService;
        _classificationService = classificationService;
    }
    
    public List<SleepStageResult> Analyze(List<VitalSignData> rawData)
    {
        // 1. 数据清洗
        var cleanedData = _cleaningService.CleanData(rawData);
        
        // 2. 分割窗口
        var windows = _cleaningService.SplitIntoWindows(cleanedData);
        
        // 3. 预处理每个窗口
        foreach (var window in windows)
        {
            _preprocessingService.Preprocess(window, PreprocessingMode.Auto);
        }
        
        // 4. 睡眠分期
        var results = _classificationService.ClassifyBatch(windows);
        
        return results;
    }
}

数据模型

VitalSignData

体征数据点,表示单条体征记录:

属性 类型 说明
Timestamp DateTime 时间戳
HeartRate double 心率
BreathRate double 呼吸率
CoeffientHuman double 人体存在系数
CoeffientMove double 体动系数
HasPeople bool 是否有人

WindowData

分析窗口数据,包含指定时间窗口内的所有体征数据。

SleepState

睡眠状态枚举:

值 说明
OutOfBed 离床
Awake 清醒
LightSleep 浅睡
DeepSleep 深睡

PreprocessingMode

预处理模式:

值 说明
DifferenceOnly 仅差值计算
DifferenceWithWindowSmooth 差值 + 加窗平滑
DifferenceWithFullSmooth 差值 + 完整平滑
Auto 自动模式

DifferenceQualityAssessment

差值序列质量评估结果:

属性 说明
StandardDeviation 标准差
MeanAbsoluteValue 平均绝对值
MaxValue 最大值
MinValue 最小值
PeakToPeak 峰峰值
SpikeRatio 毛刺比例
IsQualityAcceptable 是否满足质量要求
RecommendedMode 推荐的处理模式

SleepStageResult

睡眠分期结果:

属性 说明
WindowStartTime 窗口开始时间
WindowEndTime 窗口结束时间
SleepState 睡眠状态
Confidence 置信度(0-1)
Reason 分类依据描述
DimensionScores 各维度得分

配置选项

数据清洗配置

属性 默认值 说明
WindowSizeMinutes 5 分析窗口大小(分钟)
MonitoringStartHour 22 监测开始时间(小时)
MonitoringEndHour 8 监测结束时间(小时)
NoPeopleThresholdMinutes 45 无人状态阈值(分钟)
MovementThreshold 200 移动幅度阈值
MovementExclusionSeconds 60 大幅度移动后剔除数据的时间(秒)

预处理配置

属性 默认值 说明
DefaultPreprocessingMode Auto 默认预处理模式
WindowSmoothSize 6 加窗平滑窗口大小
DifferenceStdThreshold 10.0 差值序列质量评估标准差阈值
SpikeThreshold 3.0 毛刺检测阈值(相对于标准差的倍数)

平滑配置

属性 默认值 说明
MovingAverageWindowSize 12 移动平均窗口大小
GaussianSigma 2.5 高斯滤波 Sigma 值
SplineSmoothingParameter 150 样条插值平滑参数
AdaptiveSensitivityFactor 0.7 自适应阈值敏感性因子

分类阈值配置

属性 默认值 说明
AwakeMovementThreshold 150 清醒状态体动阈值(大于此值为清醒)
DeepSleepMovementThreshold 50 深睡状态体动阈值(小于此值为深睡)
DeepSleepHumanPresenceThreshold 10 深睡状态人体存在值阈值
DeepSleepHumanFluctuationThreshold 2 深睡状态人体存在波动阈值

服务接口

IDataCleaningService

数据清洗服务接口:

方法 说明
CleanData 清洗原始体征数据
SplitIntoWindows 将清洗后的数据按时间窗口分组
RemoveNoPeopleSegments 剔除无人状态超过阈值的数据段
RemoveLargeMovementSegments 剔除移动过大后的数据
FilterByMonitoringPeriod 过滤非监测时段的数据

IDataPreprocessingService

数据预处理服务接口:

方法 说明
Preprocess 预处理窗口数据或单个数据数组
PreprocessBatch 批量预处理多个数据数组
CalculateDifference 计算差值序列
WindowSmooth 加窗平滑
AssessDifferenceQuality 评估差值序列质量
ApplyFullSmooth 应用完整平滑方案
GenerateReport 生成预处理报告

IDataSmoothingService

数据平滑服务接口:

方法 说明
EnhancedCombinedSmooth 三阶段组合平滑
MovingAverage 移动平均平滑
GaussianFilter 高斯滤波平滑
SplineSmooth 样条插值平滑
AdaptiveThresholdFilter 自适应阈值过滤

ISleepStageClassificationService

睡眠分期分类服务接口:

方法 说明
Classify 对窗口数据进行睡眠分期分类
ClassifyBatch 批量对多个窗口数据进行睡眠分期分类
RobustStandardize 鲁棒标准化方法
KMeansClustering K-means 聚类方法
SensitivityReducedClassify 降敏分类方法

许可证

MIT License

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 148 3/12/2026