-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPyroprint.java
146 lines (138 loc) · 4.63 KB
/
Pyroprint.java
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
public class Pyroprint
implements java.io.Serializable{
private static final int RELEVANT_VALS = 93;
private int lineNum;
private int pyroId;
private String isolateId;
private String commonName;
private String appliedRegion;
double[] pHeightOrig;
double[] pHeightComp;
public Pyroprint(int lineNum, int pyroId,
String isolateId,
String commonName,
String appliedRegion,
double[] pHeightIn){
this.lineNum = lineNum;
this.pyroId = pyroId;
this.isolateId = isolateId;
this.commonName = commonName;
this.appliedRegion = appliedRegion;
this.pHeightComp = new double[RELEVANT_VALS];
this.pHeightOrig = new double[pHeightIn.length];
for(int arrLoc = 0; arrLoc < RELEVANT_VALS; arrLoc++){
this.pHeightComp[arrLoc] = pHeightIn[arrLoc];
this.pHeightOrig[arrLoc] = pHeightIn[arrLoc];
}
for(int arrLoc = RELEVANT_VALS; arrLoc < pHeightIn.length; arrLoc++){
this.pHeightOrig[arrLoc] = pHeightIn[arrLoc];
}
}
//PYROPRINT STUFFS //==>
public int getPyroId(){
return pyroId;
}
public String getIsolateId(){
return isolateId;
}
public String getCommonName(){
return commonName;
}
public void setCommonName(String commonName){
this.commonName = commonName;
}
public String getAppliedRegion(){
return appliedRegion;
}
public int getNumPHeights(){
return pHeightOrig.length;
}
static int getRelevantVals(){
return RELEVANT_VALS;
}
public String toString(){
return this.pyroId + ":" + this.isolateId + ":"
+ this.commonName +":" + this.appliedRegion + ":[" + pHeightComp.length + "]";
}
public boolean equals(Object other){
if(other == null) return false;
if(this.getClass() != other.getClass()) return false;
if(this.pyroId != ((Pyroprint)other).pyroId) return false;
if(!this.appliedRegion.equals(((Pyroprint)other).appliedRegion))
return false;
return true;
}//<==
//MATHS
public double pearsonDist(Pyroprint other){
double covariance = covariance(this.pHeightComp,other.pHeightComp);
double thisStandardDev = standardDev(this.pHeightComp);
double otherStandardDev = standardDev(other.pHeightComp);
return covariance/(thisStandardDev*otherStandardDev);
}
//PRIVATE HELPERS
/**
* Calculates the MEAN of the doubles values in a given array.
*
* @param double array containing values to calculate upon
* @return double type mean of the given data
*/
private double mean(double[] values){//==>
double mean = 0;
for(int dataLoc = 0; dataLoc < values.length; dataLoc++){
mean += values[dataLoc];
}
mean /= values.length;
return mean;
}//<==
/**
* Calculates the covariance betwen two double arrays. For now, it chooses
* the length of the x array to calculate over. In the future, we may need
* to implement something to consider the smaller length and calculate over
* only that one.
*/
private double covariance(double[] x, double[] y){//==>
if(x.length != y.length){
throw new IncomparableVectorException("Vectors are not equal in length. x[ "
+x.length + "] y[" + y.length + "]");
}
double covariance = 0;
double xMean = mean(x);
double yMean = mean(y);
double dataPoints = x.length;
for(int dataLoc = 0; dataLoc < dataPoints; dataLoc++){
covariance += (x[dataLoc] - xMean)*(y[dataLoc] - yMean);
}
covariance /= dataPoints;
return covariance;
}//<==
/**
* Calculates the variance of the double values in a given array. Variance
* here is defined as the average square of the difference between each
* value and the mean.
*
* @param double array containing values to calculate upon
* @return double type variance of the given data
*/
private double variance(double[] values){//==>
return covariance(values,values);
}//<==
/**
* Calculates the standard deviation of a double type array using the
*/
private double standardDev(double[] values){//==>
return Math.sqrt(variance(values));
}
/**
* IncomparableVectorException is a RuntimeException class made for throwing
* the program when two vectors cannot be compared, usually for the
* difference in length.
*/
public class IncomparableVectorException//==>
extends RuntimeException{
public IncomparableVectorException(){
}
public IncomparableVectorException(String msg){
super(msg);
}
}//<==
}