Skip to content

Commit

Permalink
segments reclassify samples that don't have confirmed types and don't…
Browse files Browse the repository at this point in the history
… have cached classifier results #11
  • Loading branch information
sobri909 committed May 10, 2018
1 parent d557238 commit 12a7908
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
5 changes: 4 additions & 1 deletion LocoKit/Base/Timelines/MLClassifierManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ public protocol MLClassifierManager: MLCompositeClassifier {

extension MLClassifierManager {

public var canClassify: Bool { return baseClassifier != nil }
public var canClassify: Bool {
guard let baseClassifier = baseClassifier else { return false }
return baseClassifier.models.count == baseClassifier.requiredTypes.count
}

public func classify(_ classifiable: ActivityTypeClassifiable, filtered: Bool) -> ClassifierResults? {

Expand Down
38 changes: 32 additions & 6 deletions LocoKit/LocalStore/TimelineSegment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ public class TimelineSegment {
public let store: PersistentTimelineStore
public var onUpdate: (() -> Void)?

private var _timelineItems: [TimelineItem]?
private var itemsAreStale = true
private var _timelineItems: [TimelineItem] = []
public var timelineItems: [TimelineItem] {
if let existing = _timelineItems { return existing }
_timelineItems = updatedItems
return _timelineItems!
if itemsAreStale {
_timelineItems = updatedItems
itemsAreStale = false
}
return _timelineItems
}

private let query: String
Expand Down Expand Up @@ -69,10 +72,12 @@ public class TimelineSegment {
// MARK: - Result updating

private func needsUpdate() {
_timelineItems = nil
itemsAreStale = true
onMain {
self.updateTimer?.invalidate()
self.updateTimer = Timer.scheduledTimer(withTimeInterval: 0.01, repeats: false) { [weak self] _ in
self.updateTimer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: false) { [weak self] _ in
self?.reclassifySamples()
self?.process()
self?.onUpdate?()
}
}
Expand All @@ -82,4 +87,25 @@ public class TimelineSegment {
return observer.fetchedRecords.map { store.item(for: $0.row) }
}

private func reclassifySamples() {
store.process {
guard let classifier = self.store.recorder?.classifier, classifier.canClassify else { return }

for item in self.timelineItems {
var count = 0
for sample in item.samples where sample.confirmedType == nil && sample.classifierResults == nil {
sample.classifierResults = classifier.classify(sample, filtered: true)
sample.unfilteredClassifierResults = classifier.classify(sample, filtered: false)
count += 1
}
if count > 0 {
os_log("Reclassified samples: %d", type: .debug, count)
}
}
}
}

private func process() {
}

}

0 comments on commit 12a7908

Please sign in to comment.