-
Notifications
You must be signed in to change notification settings - Fork 2
/
CometStatistics.java
264 lines (238 loc) · 10.9 KB
/
CometStatistics.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/**
* CometStatistics.java
* Created in 2012 by Benjamin Gyori
* National University of Singapore
* e-mail: ben.gyori@gmail.com
*
* When you use this plugin for your work, please cite
* Gyori BM, Venkatachalam G, et al. OpenComet: An automated tool for
* comet assay image analysis
*
* CometAnalyzer.java is the main internal class for comet analysis.
* cometAnalyzerRun takes an image as input. It analyzes the image
* and extracts comets. The comet head and tail are segmented and
* measurements are calculated. The resulting Comet objects are returned.
*
* This plugin is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3
* as published by the Free Software Foundation.
*
* This work is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this plugin; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
import ij.IJ;
import ij.ImagePlus;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
public class CometStatistics {
Statistics cometAreaStats, cometIntensityStats;
Statistics cometLengthStats, cometDNAStats;
Statistics headAreaStats, headIntensityStats, headLengthStats;
Statistics headDNAStats, headDNAPercentStats;
Statistics tailAreaStats, tailIntensityStats, tailLengthStats;
Statistics tailDNAStats, tailDNAPercentStats;
Statistics tailMomentStats, tailOliveMomentStats;
public CometStatistics(HashMap<ImagePlus,Comet[]> Comets, int flag){
cometAreaStats = new Statistics();
cometIntensityStats = new Statistics();
cometLengthStats = new Statistics();
cometDNAStats = new Statistics();
headAreaStats = new Statistics();
headIntensityStats = new Statistics();
headLengthStats = new Statistics();
headDNAStats = new Statistics();
headDNAPercentStats = new Statistics();
tailAreaStats = new Statistics();
tailIntensityStats = new Statistics();
tailLengthStats = new Statistics();
tailDNAStats = new Statistics();
tailDNAPercentStats = new Statistics();
tailMomentStats = new Statistics();
tailOliveMomentStats = new Statistics();
for (Map.Entry<ImagePlus,Comet[]> element : Comets.entrySet()) {
Comet[] imageComets = element.getValue();
for(int i=0;i<imageComets.length;i++){
if((imageComets[i].status & flag)!=0){
cometAreaStats.add(imageComets[i].cometArea);
cometIntensityStats.add(imageComets[i].cometIntensity);
cometLengthStats.add(imageComets[i].cometLength);
cometDNAStats.add(imageComets[i].cometDNA);
headAreaStats.add(imageComets[i].headArea);
headIntensityStats.add(imageComets[i].headIntensity);
headLengthStats.add(imageComets[i].headLength);
headDNAStats.add(imageComets[i].headDNA);
headDNAPercentStats.add(imageComets[i].headDNAPercent);
tailAreaStats.add(imageComets[i].tailArea);
tailIntensityStats.add(imageComets[i].tailIntensity);
tailLengthStats.add(imageComets[i].tailLength);
tailDNAStats.add(imageComets[i].tailDNA);
tailDNAPercentStats.add(imageComets[i].tailDNApercent);
tailMomentStats.add(imageComets[i].tailMoment);
tailOliveMomentStats.add(imageComets[i].tailOliveMoment);
}
}
}
}
public String getStatisticsString(String sep, String group){
String outStr;
outStr = "Mean" + sep + sep + group + sep +
cometAreaStats.getMean() + sep +
cometIntensityStats.getMean() + sep +
cometLengthStats.getMean() + sep +
cometDNAStats.getMean() + sep +
headAreaStats.getMean() + sep +
headIntensityStats.getMean() + sep +
headLengthStats.getMean() + sep +
headDNAStats.getMean() + sep +
headDNAPercentStats.getMean() + sep +
tailAreaStats.getMean() + sep +
tailIntensityStats.getMean() + sep +
tailLengthStats.getMean() + sep +
tailDNAStats.getMean() + sep +
tailDNAPercentStats.getMean() + sep +
tailMomentStats.getMean() + sep +
tailOliveMomentStats.getMean() + sep + "\n";
outStr += "Median" + sep+ sep + group + sep +
cometAreaStats.getMedian() + sep +
cometIntensityStats.getMedian() + sep +
cometLengthStats.getMedian() + sep +
cometDNAStats.getMedian() + sep +
headAreaStats.getMedian() + sep +
headIntensityStats.getMedian() + sep +
headLengthStats.getMedian() + sep +
headDNAStats.getMedian() + sep +
headDNAPercentStats.getMedian() + sep +
tailAreaStats.getMedian() + sep +
tailIntensityStats.getMedian() + sep +
tailLengthStats.getMedian() + sep +
tailDNAStats.getMedian() + sep +
tailDNAPercentStats.getMedian() + sep +
tailMomentStats.getMedian() + sep +
tailOliveMomentStats.getMedian() + sep + "\n";
outStr += "Stddev" + sep + sep + group + sep +
cometAreaStats.getStddev() + sep +
cometIntensityStats.getStddev() + sep +
cometLengthStats.getStddev() + sep +
cometDNAStats.getStddev() + sep +
headAreaStats.getStddev() + sep +
headIntensityStats.getStddev() + sep +
headLengthStats.getStddev() + sep +
headDNAStats.getStddev() + sep +
headDNAPercentStats.getStddev() + sep +
tailAreaStats.getStddev() + sep +
tailIntensityStats.getStddev() + sep +
tailLengthStats.getStddev() + sep +
tailDNAStats.getStddev() + sep +
tailDNAPercentStats.getStddev() + sep +
tailMomentStats.getStddev() + sep +
tailOliveMomentStats.getStddev() + sep + "\n";
outStr += "Min" + sep + sep + group + sep +
cometAreaStats.getMin() + sep +
cometIntensityStats.getMin() + sep +
cometLengthStats.getMin() + sep +
cometDNAStats.getMin() + sep +
headAreaStats.getMin() + sep +
headIntensityStats.getMin() + sep +
headLengthStats.getMin() + sep +
headDNAStats.getMin() + sep +
headDNAPercentStats.getMin() + sep +
tailAreaStats.getMin() + sep +
tailIntensityStats.getMin() + sep +
tailLengthStats.getMin() + sep +
tailDNAStats.getMin() + sep +
tailDNAPercentStats.getMin() + sep +
tailMomentStats.getMin() + sep +
tailOliveMomentStats.getMin() + sep + "\n";
outStr += "Max" + sep + sep + group + sep +
cometAreaStats.getMax() + sep +
cometIntensityStats.getMax() + sep +
cometLengthStats.getMax() + sep +
cometDNAStats.getMax() + sep +
headAreaStats.getMax() + sep +
headIntensityStats.getMax() + sep +
headLengthStats.getMax() + sep +
headDNAStats.getMax() + sep +
headDNAPercentStats.getMax() + sep +
tailAreaStats.getMax() + sep +
tailIntensityStats.getMax() + sep +
tailLengthStats.getMax() + sep +
tailDNAStats.getMax() + sep +
tailDNAPercentStats.getMax() + sep +
tailMomentStats.getMax() + sep +
tailOliveMomentStats.getMax() + sep + "\n";
return outStr;
}
private class Statistics {
public Statistics(){
valueList = new ArrayList<Double>();
updated = false;
}
public void add(double val){
valueList.add(val);
updated = false;
}
public double getMean(){
if(!updated) updateStatistics();
return mean;
}
public double getMedian(){
if(!updated) updateStatistics();
return median;
}
public double getStddev(){
if(!updated) updateStatistics();
return stddev;
}
public double getMin(){
if(!updated) updateStatistics();
return minval;
}
public double getMax(){
if(!updated) updateStatistics();
return maxval;
}
private void updateStatistics(){
Collections.sort(valueList);
int nElem = valueList.size();
if(nElem == 1){
mean = median = minval = maxval = valueList.get(0);
stddev = 0;
updated = true;
}
else {
mean = median = stddev = 0;
minval = valueList.get(0);
maxval = valueList.get(nElem-1);
median = (nElem %2 ==1) ? (valueList.get((nElem+1)/2-1)) :
(((valueList.get((nElem+1)/2-1))) +
(valueList.get((nElem+1)/2-1)) / 2.0);
double sum1=0.0,sum2=0.0;
Iterator it=valueList.iterator();
int i = 0;
while(it.hasNext()){
i++;
double val= (Double)it.next();
sum1 += val;
sum2 += val*val;
IJ.log(i+": "+val);
}
mean = sum1/nElem;
stddev = Math.sqrt((sum2-(sum1*sum1)/nElem)/(nElem-1));
IJ.log("Mean: "+mean+" Sum1: "+sum1+" n:"+nElem);
}
updated = true;
}
private double mean, median, stddev, minval, maxval;
private ArrayList<Double> valueList;
private boolean updated;
}
}