-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImageDataPoint.swift
121 lines (98 loc) · 3.65 KB
/
ImageDataPoint.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//
// ImageDataPoint.swift
// TSneeze
//
// Created by Brian Ledbetter on 6/23/16.
// Copyright © 2016 Brian Ledbetter. All rights reserved.
//
import Foundation
import CoreGraphics
struct ImageDataPoint : SimilarityComparable {
var sigmas : [Double]?
var points : [Double]?
var predefinedClassification : Int?
init(withPoints: [Double], withClassification: Int?) {
self.points = withPoints
self.predefinedClassification = withClassification
}
static func compare(_ first : SimilarityComparable, second: SimilarityComparable) -> (Double, Double?)? {
if (first is ImageDataPoint && second is ImageDataPoint) {
return ImageDataPoint.compareLinearDistance(first as! ImageDataPoint, second: second as! ImageDataPoint)
} else {
return nil
}
}
// returns L2 distance between the two high dimensional points, and an optional sigma value (not in yet)
static func compareLinearDistance(_ first : ImageDataPoint, second : ImageDataPoint) -> (Double, Double?)? {
return (linearDistance(first.points!, vector2: second.points!), nil)
}
// compute L2 / euclidean linear distance between two vectors
static func linearDistance(_ vector1 : [Double], vector2 : [Double]) -> Double {
// var distance = 0.0
// for i in 0..<vector1.count {
// distance += (vector1[i] - vector2[i]) * (vector1[i] - vector2[i])
// }
var sum = 0.0
for i in 0 ..< vector1.count {
sum += abs(vector1[i] - vector2[i])
}
print(sum)
return sum
// var distanceArray = [Double]()
// for i in 0 ..< vector1.count {
// distanceArray.append(vector1[i] - vector2[i])
// }
// return distanceArray.reduce(0, combine: +)
// var probabilities = [Double]()
//
// let sigma = 0.5
// let e = M_E
//
// for distance in distanceArray {
// probabilities.append(pow(distance, 2) / pow(e, (2 * pow(sigma, 2))))
// }
//
// let logSum = probabilities.reduce(0,combine: +)
//
// return logSum
// return distance
}
static func cartesianDistance(_ pointA : Double, pointB : Double, sigmaA: Double?, sigmaB : Double?) -> Double {
if sigmaA == nil {
return hypot(Double(pointA - pointB), Double(pointA - pointB))
}
return 1.0
// question for Kyle: how do you deal with 2 points, each with their own independent sigma? Average the sigmas?
// var probabilities = [Double]()
//
// let sigma = 4.0
// let e = M_E
//
// for distance in distanceArray {
// probabilities.append(pow(distance, 2) / pow(e, (2 * pow(sigma, 2))))
// }
//
// let logSum = probabilities.reduce(0,combine: +)
//
// return logSum
}
}
extension SimilarityComparable {
/*
Similarity Comparables need some helper functions
*/
// utility that creates contiguous vector of numbers(the fill value) of size n
func vectorArray(_ size : Int, fill: Double) -> [Double] {
return [Double](repeating: fill, count: size)
}
// utility that creates 2D array numbers(the fill value) of size n
func multiDVectorArray(_ size : Int, fill: Double, dimensions: Int) -> [[Double]] {
var arrayToReturn = [[Double]]()
for _ in 0 ..< size {
for _ in 0 ..< dimensions {
arrayToReturn.append([Double](repeating: fill, count: size))
}
}
return arrayToReturn
}
}