1 |
|
package co.harlequinmettle.healthfoodconcepts; |
2 |
|
|
3 |
|
import java.util.ArrayList; |
4 |
|
import java.util.Collections; |
5 |
|
|
6 |
|
import android.content.Context; |
7 |
|
|
|
|
| 64,5% |
Uncovered Elements: 44 (124) |
Complexity: 34 |
Complexity Density: 0,39 |
|
8 |
|
public class StatTool { |
9 |
|
|
10 |
|
double sum, sumOfSquares, mean, median, min, max, n, range; |
11 |
|
|
12 |
|
int[] histogram; |
13 |
|
|
14 |
|
double cutOffPoint, interval; |
15 |
|
|
16 |
|
public double standardDeviation; |
17 |
|
|
18 |
|
|
19 |
|
static int nbars = 100; |
20 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
21 |
10
|
public StatTool(int[] foodsToUse, int nutrientID) {... |
22 |
10
|
long time = System.currentTimeMillis(); |
23 |
10
|
doStatsOnList(foodsToUse, nutrientID); |
24 |
|
} |
25 |
|
|
|
|
| 69,8% |
Uncovered Elements: 13 (43) |
Complexity: 9 |
Complexity Density: 0,26 |
|
26 |
10
|
public void doStatsOnList(int[] foodIds, int nutrientID) {... |
27 |
10
|
ArrayList<Float> medianFinder = new ArrayList<Float>(); |
28 |
10
|
outer: for (int i : foodIds) { |
29 |
63846
|
float dataPoint = WhatYouEat.db[nutrientID][i]; |
30 |
|
|
31 |
63846
|
if (dataPoint < 0) |
32 |
12553
|
continue; |
33 |
51293
|
if (WhatYouEat.USING_NOT_ZEROS == WhatYouEat.DATA_CHOICES && dataPoint==0) |
34 |
11602
|
continue outer; |
35 |
|
|
36 |
39691
|
switch (WhatYouEat.Nutrient_Measure) { |
37 |
39691
|
case WhatYouEat.USING_GRAMS: |
38 |
|
|
39 |
39691
|
break; |
40 |
0
|
case WhatYouEat.USING_KCAL: |
41 |
0
|
dataPoint = WhatYouEat.getPer100KcalDataPoint(i, dataPoint); |
42 |
|
|
43 |
0
|
break; |
44 |
|
|
45 |
0
|
case WhatYouEat.USING_SERVING: |
46 |
|
|
47 |
0
|
dataPoint = WhatYouEat.getPerServingDataPoint(i, dataPoint); |
48 |
0
|
if(dataPoint<0) |
49 |
0
|
dataPoint = WhatYouEat.db[nutrientID][i]; |
50 |
|
|
51 |
0
|
default: |
52 |
|
|
53 |
0
|
break; |
54 |
|
} |
55 |
39691
|
n++; |
56 |
39691
|
sum += dataPoint; |
57 |
39691
|
sumOfSquares += dataPoint * dataPoint; |
58 |
39691
|
medianFinder.add(dataPoint); |
59 |
|
} |
60 |
10
|
mean = sum / n; |
61 |
10
|
if (medianFinder.size() < 1) |
62 |
0
|
return; |
63 |
10
|
Collections.sort(medianFinder); |
64 |
10
|
median = ((medianFinder.get((int) (n / 2)) + medianFinder |
65 |
|
.get((int) ((n - 1) / 2))) / 2); |
66 |
10
|
min = medianFinder.get(0); |
67 |
10
|
max = medianFinder.get(medianFinder.size() - 1); |
68 |
|
|
69 |
|
|
70 |
10
|
standardDeviation = (float) Math.sqrt((sumOfSquares - sum * sum / n) |
71 |
|
/ n); |
72 |
10
|
range = max - min; |
73 |
|
|
74 |
10
|
int topPt = (int) (medianFinder.size() * 0.9); |
75 |
|
|
76 |
10
|
cutOffPoint = medianFinder.get(topPt); |
77 |
|
|
78 |
|
|
79 |
10
|
histogram = calculateHistogram(medianFinder); |
80 |
|
|
81 |
|
|
82 |
|
} |
83 |
|
|
|
|
| 84,6% |
Uncovered Elements: 2 (13) |
Complexity: 4 |
Complexity Density: 0,44 |
|
84 |
10
|
public int[] calculateHistogram(ArrayList<Float> _data) {... |
85 |
|
|
86 |
10
|
interval = cutOffPoint / nbars; |
87 |
10
|
int[] histogram = new int[nbars]; |
88 |
10
|
for (float dataPt : _data) { |
89 |
39691
|
if (dataPt < 0) |
90 |
0
|
continue; |
91 |
39691
|
int histo_pt = (int) ((dataPt) / (interval)); |
92 |
39691
|
if (histo_pt >= 0 && histo_pt < nbars) |
93 |
35706
|
histogram[histo_pt]++; |
94 |
|
} |
95 |
10
|
return histogram; |
96 |
|
} |
97 |
|
|
|
|
| 82,5% |
Uncovered Elements: 7 (40) |
Complexity: 9 |
Complexity Density: 0,3 |
|
98 |
470
|
public static float[] simpleStats(int[] foodIds, int nutrientID) {... |
99 |
|
|
100 |
470
|
float sum = 0, mean = 0, min = 1E20f, max = 0, n = 0; |
101 |
470
|
for (int i : foodIds) { |
102 |
2242845
|
float dataPoint = WhatYouEat.db[nutrientID][i]; |
103 |
|
|
104 |
2242845
|
if (dataPoint < 0) |
105 |
973711
|
continue; |
106 |
|
|
107 |
1269134
|
if (dataPoint < 0) { |
108 |
0
|
dataPoint = 0; |
109 |
|
} |
110 |
1269134
|
n++; |
111 |
|
|
112 |
1269134
|
switch (WhatYouEat.Nutrient_Measure) { |
113 |
611528
|
case WhatYouEat.USING_GRAMS: |
114 |
|
|
115 |
611528
|
break; |
116 |
0
|
case WhatYouEat.USING_KCAL: |
117 |
0
|
dataPoint = WhatYouEat.getPer100KcalDataPoint(i, dataPoint); |
118 |
|
|
119 |
0
|
break; |
120 |
657606
|
case WhatYouEat.USING_SERVING: |
121 |
657606
|
dataPoint = WhatYouEat.getPerServingDataPoint(i, dataPoint); |
122 |
657606
|
if(dataPoint<0) |
123 |
657606
|
dataPoint = WhatYouEat.db[nutrientID][i];; |
124 |
657606
|
break; |
125 |
0
|
default: |
126 |
|
|
127 |
0
|
break; |
128 |
|
} |
129 |
|
|
130 |
1269134
|
sum += dataPoint; |
131 |
|
|
132 |
1269134
|
if (dataPoint < min) |
133 |
1470
|
min = dataPoint; |
134 |
1269134
|
if (dataPoint > max) |
135 |
3766
|
max = dataPoint; |
136 |
|
} |
137 |
470
|
mean = sum / n; |
138 |
|
|
139 |
470
|
float[] retrn = { min, mean, max / 3 }; |
140 |
470
|
return retrn; |
141 |
|
} |
142 |
|
|
143 |
|
|
144 |
|
@return |
145 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
146 |
0
|
public double getRange() {... |
147 |
0
|
return range; |
148 |
|
} |
149 |
|
|
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
@see |
154 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
155 |
0
|
@Override... |
156 |
|
public String toString() { |
157 |
|
|
158 |
0
|
return "\nsize: " + n + "\nmin: " + min + "\nmax: " + max + "\nmean: " |
159 |
|
+ mean + "\nmedian: " + median |
160 |
|
|
161 |
|
+ "\nstandard deviation: " + standardDeviation; |
162 |
|
} |
163 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
164 |
0
|
public double getEffectiveRange() {... |
165 |
0
|
return 4 * standardDeviation; |
166 |
|
} |
167 |
|
|
168 |
|
|
169 |
|
@return |
170 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
171 |
0
|
public double getSum() {... |
172 |
0
|
return sum; |
173 |
|
} |
174 |
|
|
175 |
|
|
176 |
|
@return |
177 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
178 |
0
|
public double getSumOfSquares() {... |
179 |
0
|
return sumOfSquares; |
180 |
|
} |
181 |
|
|
182 |
|
|
183 |
|
@return |
184 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
185 |
0
|
public double getMean() {... |
186 |
0
|
return mean; |
187 |
|
} |
188 |
|
|
189 |
|
|
190 |
|
@return |
191 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
192 |
0
|
public double getMedian() {... |
193 |
0
|
return median; |
194 |
|
} |
195 |
|
|
196 |
|
|
197 |
|
@return |
198 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
199 |
0
|
public double getMin() {... |
200 |
0
|
return min; |
201 |
|
} |
202 |
|
|
203 |
|
|
204 |
|
@return |
205 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
206 |
0
|
public double getMax() {... |
207 |
0
|
return max; |
208 |
|
} |
209 |
|
|
210 |
|
|
211 |
|
@return |
212 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
213 |
0
|
public double getN() {... |
214 |
0
|
return n; |
215 |
|
} |
216 |
|
|
217 |
|
|
218 |
|
@return |
219 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
220 |
0
|
public double getStandardDeviation() {... |
221 |
0
|
return standardDeviation; |
222 |
|
} |
223 |
|
|
224 |
|
} |