-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomForestManager.swift
102 lines (79 loc) · 3.4 KB
/
RandomForestManager.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//
// RandomForest.swift
// Ride
//
// Created by William Henderson on 3/7/16.
// Copyright © 2016 Knock Softwae, Inc. All rights reserved.
//
import Foundation
class RandomForestManager {
private var modelIdentifier: String?
var _ptr: OpaquePointer!
var classLables: [Int32]!
var classCount = 0
var desiredSampleInterval: TimeInterval {
get {
return Double(randomForestGetDesiredSamplingInterval(_ptr))
}
}
var desiredSessionDuration: TimeInterval {
get {
return Double(randomForestGetDesiredSessionDuration(_ptr))
}
}
var canPredict: Bool {
return randomForestManagerCanPredict(_ptr)
}
init () {
guard let configFilePath = Bundle(for: type(of: self)).path(forResource: "config.json", ofType: nil) else {
return
}
let cConfigFilepath = configFilePath.cString(using: String.Encoding.utf8)
_ptr = createRandomForestManagerFromFile(UnsafeMutablePointer(mutating: cConfigFilepath!))
}
deinit {
deleteRandomForestManager(_ptr)
}
func startup() {
guard let modelUIDCString = randomForestGetModelUniqueIdentifier(_ptr) else {
return
}
modelIdentifier = String(cString: modelUIDCString)
guard let modelID = modelIdentifier else {
return
}
guard modelID.characters.count > 0 else {
return
}
guard let modelPath = Bundle(for: type(of: self)).path(forResource: String(format: "%@.cv", modelID), ofType: nil) else {
return
}
let cModelpath = modelPath.cString(using: String.Encoding.utf8)
randomForestLoadModel(_ptr, UnsafeMutablePointer(mutating: cModelpath!))
self.classCount = Int(randomForestGetClassCount(_ptr))
self.classLables = [Int32](repeating: 0, count: self.classCount)
randomForestGetClassLabels(_ptr, UnsafeMutablePointer(mutating: self.classLables), Int32(self.classCount))
}
private func accelerometerReadings(forSensorData sensorData:NSOrderedSet)->[AccelerometerReading] {
var readings: [AccelerometerReading] = []
for elem in sensorData {
let data = elem as! SensorData
let reading = AccelerometerReading(x: data.x.floatValue, y: data.y.floatValue, z: data.z.floatValue, t: data.date.timeIntervalSinceReferenceDate)
readings.append(reading)
}
return readings
}
func classify(_ sensorDataCollection: SensorDataCollection)
{
let accelVector = self.accelerometerReadings(forSensorData: sensorDataCollection.accelerometerAccelerations)
let confidences = [Float](repeating: 0.0, count: self.classCount)
randomForestClassifyAccelerometerSignal(_ptr, UnsafeMutablePointer(mutating: accelVector), Int32(accelVector.count), UnsafeMutablePointer(mutating: confidences), Int32(self.classCount))
var classConfidences: [Int: Float] = [:]
for (i, score) in confidences.enumerated() {
classConfidences[Int(classLables[i])] = score
}
sensorDataCollection.activityPredictionModelIdentifier = modelIdentifier
sensorDataCollection.setActivityTypePredictions(forClassConfidences: classConfidences)
CoreDataManager.shared.saveContext()
}
}