Parent: [a7a9ff] (diff)

Download this file

ModelFacade.java    431 lines (384 with data), 15.3 kB

  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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
* Copyright 2013-2014 TECO - Karlsruhe Institute of Technology.
*
* This file is part of TACET.
*
* TACET is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TACET 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 TACET. If not, see <http://www.gnu.org/licenses/>.
*/
package squirrel.model;
import java.awt.Dialog;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import squirrel.controller.ClassificatorController;
import squirrel.model.io.CSVDataSource;
import squirrel.model.io.DataColumn;
import squirrel.model.io.DataSink;
import squirrel.model.io.DataSource;
import squirrel.model.io.IOFactory;
import squirrel.model.io.Reader;
import squirrel.util.Range;
import squirrel.view.ProgressView;
public class ModelFacade implements StatisticModel {
private AnnotationModel annotationModel;
private Reader reader;
private SensorDataFilterManager sensorDataFilterManager = null;
private SensorDataModel sensorDataModel = null;
private DataSource<? extends DataColumn> dataSource;
private Range startAndEndTime;
private TimeCoordinator timeCoordinator;
private Double[] sensorAvgList;
private ClassificatorController classificatorController;
public ModelFacade(DataSource<? extends DataColumn> currentDataSource) {
this(null, null, null, null, currentDataSource, null, null);
}
public ModelFacade(AnnotationModel annotationModel, Reader reader,
SensorDataFilterManager sensorDataFilterManager, SensorDataModel sensorDataModel,
DataSource<? extends DataColumn> currentDataSource, Range startAndEndTime,
TimeCoordinator timeCoordinator) {
super();
this.annotationModel = annotationModel;
this.reader = reader;
this.sensorDataFilterManager = sensorDataFilterManager;
this.sensorDataModel = sensorDataModel;
this.dataSource = currentDataSource;
this.startAndEndTime =
startAndEndTime == null ? new Range(getReader().startTimeStamp(), getReader()
.endTimeStamp()) : startAndEndTime;
this.timeCoordinator = timeCoordinator;
}
public List<Annotation[]> getAllAnnoations() {
ArrayList<Annotation[]> tracks = new ArrayList<>();
for (int j = 0; j < dataSource.getNumberOfAnnotations(); j++) {
Iterable<Annotation> annotations =
getAnnotations(new Range(getStartTimeStamp(), getEndTimeStamp()), j);
int count = 0;
for (Annotation annotation : annotations) {
count++;
}
Annotation[] at = new Annotation[count];
count = 0;
for (Annotation annotation : annotations) {
at[count] = annotation;
count++;
}
tracks.add(at);
}
return tracks;
}
public void insertAnnoationModel(AnnotationModelImpl annotationModel) {
this.annotationModel = annotationModel;
annotationModel.setReader(this.reader);
}
public AnnotationModel getAnnotationModel() {
return getAnnotationModelIntern();
}
public DataSource<? extends DataColumn> getCurrentDataSource() {
return dataSource;
}
public SensorDataFilter getFilteredSensorData(Range range, int rate) {
return getSensorDataFilterManager().getFilteredSensorData(range, rate);
}
public SensorDatum getSensorDatum(long timestamp) {
return getSensorDataModel().getSensorDatum(timestamp);
}
public double getSensorValue(long timestamp, int track) {
return getSensorDataModel().getSensorValue(timestamp, track);
}
public Iterable<Annotation> getAnnotations(Range range, int track) {
return getAnnotationModelIntern().getAnnotations(range, track);
}
public Annotation getAnnotation(long timestamp, int track) {
return getAnnotationModelIntern().getAnnotation(timestamp, track);
}
public boolean canInsertAt(Range range, int track) {
return getAnnotationModelIntern().canInsertAt(range, track);
}
public void deleteAnnotations(Range range, int track) {
getAnnotationModelIntern().deleteAnnotations(range, track);
}
public void insertAnnotation(Annotation annot, int track) {
getAnnotationModelIntern().insertAnnotation(annot, track);
}
public void replaceAnnotation(Annotation annotation, int track) {
getAnnotationModelIntern().replace(annotation, track);
}
public double getAverageDistance() {
return reader == null ? getReader().averageDistance() : reader.averageDistance();
}
private AnnotationModel getAnnotationModelIntern() {
if (this.annotationModel == null)
/*
* this.annotationModel = new
* AnnotationModelImpl(getReader().readAnnotations( new
* Range(getStartTimeStamp(), getEndTimeStamp())),
* dataSource.getNumberOfOldAnnotations() +
* dataSource.getNumberOfNewAnnotations());
*/
this.annotationModel =
new AnnotationModelImpl(getReader(),
new Range(getStartTimeStamp(), getStartTimeStamp() + 8196),
dataSource.getNumberOfOldAnnotations()
+ dataSource.getNumberOfNewAnnotations());
return this.annotationModel;
}
public Reader getReader() {
if (reader == null) {
if (dataSource.getType() == DataSource.Type.CSV) {
return IOFactory.createCSVReader((CSVDataSource) dataSource);
}
} else {
return reader;
}
throw new UnsupportedOperationException("Can't creat a reader should never occur");
}
private SensorDataFilterManager getSensorDataFilterManager() {
return sensorDataFilterManager == null ? (sensorDataFilterManager =
new SensorDataFilterManager(8, 8, 0.72, 1000, getSensorDataModel(), startAndEndTime))
: sensorDataFilterManager;
}
private SensorDataModel getSensorDataModel() {
if (sensorDataModel == null) {
// If average distance is > 5 minutes, assume the complete data set
// to fit in memory
if (getReader().averageDistance() > 300000.0) {
this.sensorDataModel = new SparseDataModel(getReader());
System.out.println("INFO: Using Sparse Data Model.");
} else {
this.sensorDataModel =
new HashMapModel(256, 16384, (int) getReader().averageDistance(), getReader());
}
}
return sensorDataModel;
}
public void setReader(Reader reader) {
this.reader = reader;
}
public void export(DataSink dataSink, Dialog view) {
ProgressView progressView = new ProgressView("Export data ...", "This may take a while.");
ExportModel em = new ExportModel(this, dataSink);
em.addPropertyChangeListener(progressView);
progressView.presentAsDialog(view);
em.export();
}
public void setFilterSize(int newSize) {
getSensorDataFilterManager().setFilterSize(newSize);
}
public int getFilterSize() {
return getSensorDataFilterManager().getFilterSize();
}
/**
* Returns the total number of annotations.
* @return number of annotations
*/
@Override
public int countAnnotations() {
int res = 0;
for (int i = 0; i < dataSource.getNumberOfAnnotations(); i++) {
res += countAnnotations(i);
}
return res;
}
/**
* Returns the number of annotations for a given track.
* @param trackNo track number
* @param number of annotations for {@code trackNo}
*/
@Override
public int countAnnotations(int trackNo) {
int res = 0;
Range range = new Range(getStartTimeStamp(), getEndTimeStamp());
for (Annotation anno : getAnnotationModelIntern().getAnnotations(range, trackNo)) {
if (anno != null) {
res++;
}
}
return res;
}
/**
* Returns how many percent of the sensordata is annotated.
* @return percentage of annotations for sensordata
*/
@Override
public float getPercentAnnotation(int trackNo) {
float res;
float timeCount = (getEndTimeStamp() - getStartTimeStamp());
int annoRange = 0;
for (Annotation anno : getAnnotations(startAndEndTime, trackNo)) {
if (anno != null) {
annoRange += anno.getEnd() - anno.getStart();
}
}
res = (annoRange / timeCount);
return res * 100;
}
/**
* Returns the median of sensordata.
* @return median of sensordata
*/
@Override
public double getMedianSensordata(int trackNo) {
double out = Double.NaN;
long ts = getStartTimeStamp() + (long) ((getEndTimeStamp() - getStartTimeStamp()) / 2);
int delta = 0;
while (Double.isNaN(out)) {
out = this.getSensorDataModel().getSensorValue(ts + delta, trackNo);
if (Double.isNaN(out)) {
out = this.getSensorDataModel().getSensorValue(ts - delta, trackNo);
}
delta++;
}
return out;
}
public Iterable<SensorDatum> getSensorDataInRange(Range r) {
return sensorDataModel.getSensorDataRange(r, 1);
}
/**
* Returns the average sensordata.
* @return average sensordata
*/
@Override
public double getAvgSensordata(int trackNo) {
if (sensorAvgList == null) {
int size = getNumberOfSensorTracks();
sensorAvgList = new Double[size];
}
if (sensorAvgList[trackNo] == null) {
long mid = (getEndTimeStamp() - getStartTimeStamp()) / 2 + getStartTimeStamp();
Range range1 = new Range(getStartTimeStamp(), mid);
Range range2 = new Range(mid + 1, getEndTimeStamp());
Iterator<SensorDatum> it1 =
getSensorDataModel().getSensorDataRange(range1, 1).iterator();
Iterator<SensorDatum> it2 =
getSensorDataModel().getSensorDataRange(range2, 1).iterator();
double avg = 0;
int i = 1;
while (it1.hasNext() || it2.hasNext()) {
if (it1.hasNext()) {
avg = avg * (i - 1) / i + it1.next().getValues()[trackNo] / i;
i++;
}
if (it2.hasNext()) {
avg = avg * (i - 1) / i + it2.next().getValues()[trackNo] / i;
i++;
}
}
sensorAvgList[trackNo] = avg;
}
return sensorAvgList[trackNo];
}
/**
* Returns the standard deviation of sensordata.
* @return standard deviation of sensordata
*/
@Override
public double getStandardDeviationSensordata(int trackNo) {
double avg = getAvgSensordata(trackNo);
long mid = (getEndTimeStamp() - getStartTimeStamp()) / 2 + getStartTimeStamp();
Range range1 = new Range(getStartTimeStamp(), mid);
Range range2 = new Range(mid + 1, getEndTimeStamp());
Iterator<SensorDatum> it1 = getSensorDataModel().getSensorDataRange(range1, 1).iterator();
Iterator<SensorDatum> it2 = getSensorDataModel().getSensorDataRange(range2, 1).iterator();
double dev = 0;
double tmp = 0;
int i = 1;
for (i = 1; i < 3; i++) {
if (it1.hasNext())
tmp += Math.pow(it1.next().getValues()[trackNo] - avg, 2);
if (it2.hasNext()) {
tmp += Math.pow(it2.next().getValues()[trackNo] - avg, 2);
i++;
}
}
while (it1.hasNext() || it2.hasNext()) {
if (it1.hasNext()) {
tmp =
tmp * (i - 2) / (i - 1)
+ Math.pow(it1.next().getValues()[trackNo] - avg, 2)
/ (i - 1);
i++;
}
if (it2.hasNext()) {
tmp =
tmp * (i - 2) / (i - 1)
+ Math.pow(it2.next().getValues()[trackNo] - avg, 2)
/ (i - 1);
i++;
}
}
dev = Math.sqrt(tmp);
return dev;
}
@Override
public long getStartTimeStamp() {
return this.startAndEndTime.getStart();
}
@Override
public long getEndTimeStamp() {
return this.startAndEndTime.getEnd();
}
public TimeCoordinator getTimeCoordinator() {
return (this.timeCoordinator == null) ?
this.timeCoordinator =
new TimeCoordinator(1, getEndTimeStamp() - getStartTimeStamp())
: this.timeCoordinator;
}
@Override
public int getNumberOfSensorTracks() {
return this.getCurrentDataSource().getNumberOfSensorColumns();
}
@Override
public int getNumberOfAnnotationTracks() {
return this.getCurrentDataSource().getNumberOfAnnotations();
}
@Override
public double getAvgTimeDistance() {
return this.getReader().averageDistance();
}
public void setClassificatorController(ClassificatorController classificatorController) {
this.classificatorController = classificatorController;
}
// public void classify(Range range) {
// classificatorController.classify(range);
// }
public void mergeAnnotations(int track, Range range) {
Annotation curr;
Annotation prev = null;
for (Annotation a : getAnnotations(range, track)) {
if (prev == null) {
prev = a;
} else {
curr = a;
if (curr.getStart() >= prev.getEnd()) {
if (curr.toValue().equals(prev.toValue())) {
Range between = new Range(prev.getEnd(), curr.getStart());
Iterator<SensorDatum> iter = getSensorDataInRange(between).iterator();
if (iter.hasNext())
iter.next();
if (!iter.hasNext() || prev.getEnd() ==
curr.getStart()) {
prev.setNewRange(new Range(prev.getStart(), curr.getEnd()));
deleteAnnotations(curr.getRange(), track);
deleteAnnotations(prev.getRange(), track);
insertAnnotation(prev, track);
curr = prev;
}
}
}
prev = curr;
}
}
timeCoordinator.setTime(timeCoordinator.getTime());
}
}