Child: [9eb143] (diff)

Download this file

DataSource.java    464 lines (412 with data), 12.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
* Copyright 2013 TecO - Karlsruhe Institute of Technology
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package squirrel.model.io;
import java.io.Serializable;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import squirrel.model.AnnotationFactory;
import weka.core.Attribute;
public class DataSource<T extends DataColumn> implements Iterable<T>, Serializable, Cloneable {
private static final long serialVersionUID = -7901961351301035378L;
/**
* enum of all data types (binary, csv, database)
*/
public enum Type {
CSV, BINARY, DATABASE;
}
/**
* Location of file on disc
*/
private URL location;
/**
* List of data sets
*/
private List<T> dataColumns = new ArrayList<>();
/**
* List of Paths to the media files on disk
*/
private List<String> mediaPaths = new ArrayList<>();
/**
* type of file on disk
*/
private Type type;
/**
* AnnotationFactory to create Annotations
*/
private AnnotationFactory annotationFactory;
/**
* Constructor of data source
*
* @param location - location of file on disk
* @param dataColumns - Iterable of data columns
* @param mediaPaths - Paths to media files on disk
* @param annotationFactory - AnnotationFactory for g
*/
public DataSource(URL location, Iterable<T> dataColumns, List<String> mediaPaths,
AnnotationFactory annotationFactory) {
super();
this.location = location;
for (T t : dataColumns)
this.dataColumns.add(t);
updateIndices();
this.mediaPaths = mediaPaths;
this.annotationFactory = annotationFactory;
}
/**
* Updates the indices of sensor, annotations and timestamp
* @SuppressWarnings("incomplete-switch")
*/
private void updateIndices() {
int cntTs = 0;
int cntAnnot = 0;
int cntSens = 0;
for (DataColumn dc : this) {
switch (dc.getType()) {
case SENSOR:
dc.setSensorIdx(cntSens++);
break;
case FLOATING_POINT_ANNOTATION:
case INTEGER_ANNOTATION:
case LABEL_ANNOTATION:
case NEW_FLOATING_POINT_ANNOTATION:
case NEW_INTEGER_ANNOTATION:
case NEW_LABEL_ANNOTATION:
case NEW_TRAIN_ANNOTATION:
dc.setAnnotationIdx(cntAnnot++);
break;
case TIMESTAMP:
dc.setTimestampIdx(cntTs++);
break;
}
}
}
/**
* Constructor
* @param location
* @param mediaPaths
* @param annotationFactory
*/
public DataSource(URL location, List<String> mediaPaths, AnnotationFactory annotationFactory) {
this(location, Collections.<T> emptyList(), mediaPaths, annotationFactory);
}
/**
* Default constructor
*/
public DataSource() {}
/**
* Getter of Location
* @return URL-Location of file on disk
*/
public URL getLocation() {
return location;
}
/**
* Setter of URL-Location
* @param location - of file on disk
*/
public void setLocation(URL location) {
this.location = location;
}
/**
* Adds a data column to the data source
*
* @param i - Index of data column
* @param dataColumn - you want to add
*/
public void addDataColumn(int i, T dataColumn) {
dataColumns.add(i, dataColumn);
updateIndices();
}
/**
* Adds a data column to the end of the data source
* @param dataColumn - new
*/
public void addDataColumn(T dataColumn) {
dataColumns.add(dataColumn);
updateIndices();
}
/**
* Getter of data column No @code i
* @param i - index of datacolumn
* @return data column of index i
*/
public T getDataColumn(int i) {
return dataColumns.get(i);
}
/**
* Removes data column of index @code i
* @param i - index of data column
*/
public void removeDataColumn(int i) {
dataColumns.remove(i);
updateIndices();
}
/**
* Calculates size of data set
* @return Size of data set
*/
public int getNumberOfColumns() {
return dataColumns.size();
}
@Override
public Iterator<T> iterator() {
return dataColumns.iterator();
}
/**
* Set a new dataset
* @param dataColumn - new set of data
*/
public void setIteratorDataColumn(List<T> dataColumn) {
this.dataColumns = dataColumn;
updateIndices();
}
/**
* Searches @dc at data set and returns its index
* @param dc - searched datacolumn
* @return
*/
public int indexOf(DataColumn dc) {
return dataColumns.indexOf(dc);
}
/**
* Getter of source type
* @return type of file source
*/
public Type getType() {
return type;
}
/**
* Sets type of file source
* @param type - of file source
*/
public void setType(Type type) {
this.type = type;
}
/**
* Gets the annotation factory
* @return annotationfactory
*/
public AnnotationFactory getAnnotationFactory() {
return annotationFactory;
}
/**
* Sets a new annotation factory
* @param annotationFactory - new
*/
public void setAnnotationFactory(AnnotationFactory annotationFactory) {
this.annotationFactory = annotationFactory;
}
/**
* Sets new media paths
* @param mediaPaths - location of media files on disk
*/
public void setMediaPaths(List<String> mediaPaths) {
this.mediaPaths = mediaPaths;
}
/**
* Getter mediapaths
* @return mediaPaths - location of media files on disk
*/
public List<String> getMediaPath() {
return mediaPaths;
}
/**
* Returns a iterable of all sensor columns
* @return ret - iterable of all sensor columns
*/
public Iterable<DataColumn> getSensorColumns() {
List<DataColumn> ret = new ArrayList<>();
for (DataColumn dc : this) {
if (dc.getType().isSensor())
ret.add(dc);
}
return ret;
}
/**
* Returns a iterable of all annotation columns
* @return ret - iterable of all annotation columns
*/
public Iterable<DataColumn> getAnnotationColumns() {
List<DataColumn> ret = new ArrayList<>();
for (DataColumn dc : this) {
if (dc.getType().isAnnotation())
ret.add(dc);
}
return ret;
}
/**
* Return the column selected as the training column.
*
* @return training column or {@code null} if there is none.
*/
public DataColumn getTrainColumn() {
for (DataColumn dc : this) {
if (dc.getType().isTrainAnnotation()) return dc;
}
return null;
}
/**
* Calculates the count of annotations
* @return count of annotations
*/
public int getNumberOfAnnotations() {
int cnt = 0;
for (DataColumn dc : this)
if (dc.getType().isAnnotation())
cnt++;
return cnt;
}
/**
* Calculates the count of old annotations
* @return count of old annotations
*/
public int getNumberOfOldAnnotations() {
int cnt = 0;
for (DataColumn dc : this)
if (dc.getType().isOldAnnotation())
cnt++;
return cnt;
}
/**
* Calculates the count of new annotations
* @return count of new annotations
*/
public int getNumberOfNewAnnotations() {
int cnt = 0;
for (DataColumn dc : this)
if (dc.getType().isNewAnnotation())
cnt++;
return cnt;
}
/**
* Counts all timestamps
* @return count of timestamps
*/
public int getNumberOfTimestamps() {
int cnt = 0;
for (DataColumn dc : this)
if (dc.getType().isTimeStamp())
cnt++;
return cnt;
}
/**
* Calculates the count of sensors
* @return count of annotations
*/
public int getNumberOfSensorColumns() {
int cnt = 0;
for (DataColumn dc : this)
if (dc.getType().isSensor())
cnt++;
return cnt;
}
/**
* Return the zero based idx of the last column to contain timestamps.
*
* @return idx of the last timestamp or a negative value if there are no
* timestamps.
*/
public int idxOfLastTimestamp() {
for (int i = dataColumns.size() - 1; i >= 0; i--) {
if (dataColumns.get(i).getTimestampIdx() >= 0)
return i;
}
return -1;
}
public int toSensorIndex(int column) {
if (dataColumns.get(column).getType().isSensor()) {
int cnt = 0;
for (int i = 0; i < column; i++)
if (dataColumns.get(i).getType().isSensor())
cnt++;
return cnt;
}
return -1;
}
public int toAnnotationIndex(int column) {
if (dataColumns.get(column).getType().isAnnotation()) {
int cnt = 0;
for (int i = 0; i < column; i++)
if (dataColumns.get(i).getType().isAnnotation())
cnt++;
return cnt;
}
return -1;
}
public int sensorIndexToColumn(int sensorIndex) {
for (DataColumn dc : dataColumns) {
if (dc.getSensorIdx() == sensorIndex) {
return dataColumns.indexOf(dc);
}
}
return -1;
}
public int annotationIndexToColumn(int annotationIndex) {
for (DataColumn dc : dataColumns) {
if (dc.getAnnotationIdx() == annotationIndex) {
return dataColumns.indexOf(dc);
}
}
return -1;
}
@Override
public String toString() {
StringBuilder cols = new StringBuilder(1000);
for (T dc : dataColumns) {
cols.append(dc.toString() + ",\n");
}
cols.delete(cols.length() - 2, cols.length());
return String.format(
"[DS] Type: %s\n URL: %s\n Tracks:\n%s\n Media: %s\n AnnFac: %s",
type, location, cols, mediaPaths, annotationFactory);
}
@Override
public DataSource clone() {
DataSource<DataColumn> theClone = null;
try {
theClone = (DataSource) super.clone();
List<DataColumn> cloneData = new ArrayList<>();
for (DataColumn dc : this) {
cloneData.add(dc.clone());
}
theClone.setIteratorDataColumn(cloneData);
} catch (CloneNotSupportedException e) {}
return theClone;
}
public ArrayList<Attribute> getAttributes() {
ArrayList<Attribute> attributeList = new ArrayList<Attribute>();
for (DataColumn dc : this) {
if (dc.isSelected()) {
Attribute attribute = convertTypeToAttribute(dc);
attributeList.add(attribute);
}
}
return attributeList;
}
private Attribute convertTypeToAttribute(DataColumn dc) {
String name = dc.getName();
if (dc.getType().isLabelAnnotation()) {
return new Attribute(name, annotationFactory.getAllowedValues(dc.getAnnotationIdx()));
} else if (dc.getType().isTimeStamp()){
return new Attribute("timeStamp");
} else {
return new Attribute(name);
}
}
}