Swiftpack.co - Package - FightTheStroke/FlowManager

FlowManager Package

Swift Documentation Swift Package Manager compatible ver

Documentation Design

Part of the "Epilepsy Care Kit" Project Developed by Jacopo Mangiavacchi, Roberto D'Angelo, Spencer Morris on April-Sept 2020

Copyright © 2020 FightTheStroke Foundation. All rights reserved.

FlowManager Struct implement core logic for analyzing biometric inputs such as BPM and in the future Accelerometer data and returning derivative data such as HRV, accelleration speed, sleep level etc. and events such as anomalies in the data flow and alarm for seizure detection.

This library is designed in order to be used in both real-time and batch scenarios.

In real-time mode the libaray could be used directly by the watchOS and/or iOS application for processing in real time the flow of biometric data coming from the Apple Watch sensors or other remote sensors connected to the watchOS of iOS app over bluetooth.

In batch mode the library could be used again from iOS/watchOS app but also from command line tool or web services API to re-play entire pre-recorded session of data.

Open data structures

The library use the following data structures for defining inputs the parameter ranges such as normal and alarm BPM levels and in general to initialize the main FlowManager struct.

public struct FlowParameters: Codable {
    public var expectedTimeInterval: TimeInterval
    public var acceptedDelay: TimeInterval
    public var ignoreFirstSamples: Int
    public var device: Device
    public var enableLogging: Bool

    // HyperParameters
    public var immediateAlarm: ParamsRange
    public var normalRange: ParamsRange
    public var lightSleepRange: ParamsRange
    public var deepSleepRange: ParamsRange
    public var anomalyMaxLenghtSeconds: Int
    public var bpmSpeedAlarm: Double
    public var hrvBufferSize: Int
    public var averageSpeedBufferSize: Int


To pass to the main library method sample of biometric input data to process and to return the transformed values and the optional list of events this library also define the following structs:

public struct Sample {
    public let time: TimeInterval
    public let bpm: Int
    public let accelerometer: (Double, Double, Double)


public enum AlarmType: String, Codable {
    case bpmValue
    case bpmSpeed

public enum SleepLevel: String, Codable {
    case light
    case deep
    case awake

public enum Event: CustomStringConvertible, Codable {
    case missingData(timeInterval: TimeInterval, expectedSampleMissing: Int)
    case alarm(type: AlarmType)
    case warning(type: AlarmType)
    case backToOk
    case noData


public struct ProcessedFlowResult: Codable {
    public let events: [Event]
    public let bpmSpeed: Double
    public let bpmSpeedAverage: Double
    public let hrv: Double?
    public let movementIndex: Double?
    public let sleepLevel: SleepLevel?


The main FlowManager struct can be easily initialized passing a FlowParameters instance such as in the code below:

let params = FlowParameters(expectedTimeInterval: 1.0,
                            acceptedDelay: 1.0,
                            ignoreFirstSamples: 3,
                            device: .appleWatch,
                            enableLogging: false,
                            immediateAlarm: ParamsRange(min: 50, max: 130),
                            normalRange: ParamsRange(min: 70, max: 110),
                            lightSleepRange: ParamsRange(min: 81, max: 95),
                            deepSleepRange: ParamsRange(min: 70, max: 80),
                            anomalyMaxLenghtSeconds: 10,
                            bpmSpeedAlarm: 0.0)
var flowManager = FlowManager(parameters: params)

Main method

Once instantiated a FlowManager object the biometric data could be processed both in the real-time and batch scenarios simply calling the main processSample method like in the code below:

let sample = Sample(time: measureTime, bpm: bpmValue)

let result: ProcessedFlowResult = flowManager.processSample(sample: sample)

// Pseudo code example for update HRV label on UI
hrvLabel.text = result.hrv

// Pseudo code example for managing alerts
for event in result.events {
    switch event {
    case .missingData:
    case .warning:
    case .alarm:


Stars: 3


Used By

Total: 0


v0.1.17 - 2020-05-27 19:19:01

  • Add Extension delegate to handle configurable data storage extensions
  • Implement CSV based local data storage

v0.1.16 - 2020-05-13 03:38:46

v0.1.15 - 2020-05-09 01:49:11

v0.1.14 - 2020-05-06 18:00:22

V0.1.13 - 2020-05-05 07:43:48

based on latest commit, with improved documentation

V0.1.12 - 2020-05-03 03:39:07

no changes but for some reasons the tag is not updated by Xcode packages. Trying to create a new release to see if it works

v0.1.11 - 2020-05-02 17:35:58

v0.1.10 - 2020-05-01 02:06:50

V0.1.9 - 2020-04-30 05:16:13

update on statistics

V0.1.8 - 2020-04-28 12:10:32

updated json import/export

V0.1.7 - 2020-04-27 09:56:10

made Event enum able to import/export from/to json

V0.1.6 - 2020-04-25 11:30:06

updated loadfromjson string so that it changes itself and don't return anything

V0.1.5 - 2020-04-25 10:24:13

made result from process public

V0.1.4 - 2020-04-25 10:09:43

loadfromjson has to be public and not static

V0.1.3 - 2020-04-25 10:03:55

all parameters are public

v0.1.2 - 2020-04-25 08:15:15

v0.1.1 - 2020-04-25 08:09:49

parameters has to be public as we change them from the app

v0.1.0 - 2020-04-25 06:21:21

v0.0.3 - 2020-04-22 04:49:48

v0.0.2 - 2020-04-20 18:44:03

v0.0.1 - 2020-04-16 20:41:57