Download this file

InstancesAdapter.java    262 lines (235 with data), 8.5 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
package classification;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import weka.core.Attribute;
import weka.core.DenseInstance;
import weka.core.Instance;
import weka.core.Instances;
import edu.teco.tacet.track.Annotation;
import edu.teco.tacet.track.AnnotationTrack;
import edu.teco.tacet.track.Datum;
import edu.teco.tacet.track.Range;
import edu.teco.tacet.track.ReadableSensorTrack;
import edu.teco.tacet.track.UniqueDataDecorator;
public class InstancesAdapter {
private AnnotationTrack classTrack;
private List<AnnotationTrack> annotationTracks;
private List<ReadableSensorTrack> sensorTracks;
private Range fullRange;
private ArrayList<Attribute> attributes;
private Datum currentData[];
public InstancesAdapter(AnnotationTrack classTrack,
List<AnnotationTrack> annotationTracks,
List<ReadableSensorTrack> sensorTracks, Range fullRange) {
this.classTrack = classTrack;
this.annotationTracks = annotationTracks;
this.sensorTracks = sensorTracks;
this.currentData = new Datum[sensorTracks.size()];
this.fullRange = fullRange;
this.attributes = convertToAttributes(classTrack, annotationTracks, sensorTracks);
}
public Instances getTrainingsInstace(Range range) {
Instances instances = new Instances("Training", attributes, 0);
instances.setClassIndex(1);
LinkedList<Iterator<? extends Datum>> sensorIterators = new LinkedList<>();
LinkedList<Iterator<? extends Annotation>> annotationIterators = new LinkedList<>();
LinkedList<Iterator<? extends Datum>> sensorIteratorsTs = new LinkedList<>();
for(ReadableSensorTrack t: sensorTracks) {
Iterable<? extends Datum> track = t.getData(fullRange);
sensorIterators.add(track.iterator());
sensorIteratorsTs.add(track.iterator());
}
for(AnnotationTrack t: annotationTracks) {
Iterable<? extends Annotation> track = t.getData(range);
annotationIterators.add(track.iterator());
}
Annotation currentAnnotations = null;
Datum currentData = null;
Iterable<? extends Annotation> classAnnotations = classTrack.getData(fullRange);
for (Annotation a : classAnnotations) {
Range r = a.getRange();
long timeStamp = getNextTimeStamp(r.getStart() - 1, sensorIteratorsTs);
while (timeStamp < r.getEnd()) {
Instance instance = new DenseInstance(attributes.size());
instance.setDataset(instances);
int idx = 0;
instance.setValue(idx, timeStamp);
idx++;
instance.setValue(idx, a.getLabel());
System.out.println(a.getLabel());
idx++;
for (Iterator<? extends Annotation> iter : annotationIterators) {
if (iter.hasNext()) {
if (currentAnnotations == null) {
currentAnnotations = iter.next();
}
while (currentAnnotations.getRange().getEnd() < timeStamp) {
if (iter.hasNext()) {
currentAnnotations = iter.next();
}
}
if (currentAnnotations.getRange()
.contains(timeStamp)) {
String label = currentAnnotations.getLabel();
instance.setValue(idx, label);
}
idx++;
}
}
for (Iterator<? extends Datum> iter : sensorIterators) {
if (iter.hasNext()) {
if (currentData == null) {
currentData = iter.next();
}
while (currentData.timestamp < timeStamp) {
if (iter.hasNext()) {
currentData = iter.next();
}
}
if (currentData.timestamp == timeStamp) {
instance.setValue(idx, currentData.value);
}
idx++;
}
}
instances.add(instance);
timeStamp = getNextTimeStamp(timeStamp, sensorIteratorsTs);
}
}
return instances;
}
public Instances getInstanceToClassify() {
Instances instances = new Instances("Classification", attributes, 0);
instances.setClassIndex(1);
LinkedList<Iterator<? extends Datum>> sensorIterators = new LinkedList<>();
LinkedList<Iterator<? extends Annotation>> annotationIterators = new LinkedList<>();
LinkedList<Iterator<? extends Datum>> sensorIteratorsTs = new LinkedList<>();
for(ReadableSensorTrack t: sensorTracks) {
Iterable<? extends Datum> track = t.getData(fullRange);
sensorIterators.add(track.iterator());
sensorIteratorsTs.add(track.iterator());
}
for(AnnotationTrack t: annotationTracks) {
Iterable<? extends Annotation> track = t.getData(fullRange);
annotationIterators.add(track.iterator());
}
Iterable<? extends Annotation> classAnnotations = classTrack.getData(fullRange);
Iterator<? extends Annotation> classIter = classAnnotations.iterator();
Annotation currentClassAnnot = null;
Annotation currentAnnotations = null;
Datum currentData = null;
if(classIter.hasNext()) {
currentClassAnnot = classIter.next();
}
long timeStamp = getNextTimeStamp(fullRange.getStart() - 1, sensorIteratorsTs);
while (timeStamp < fullRange.getEnd()) {
if(fullRange.contains(timeStamp)) {
Instance instance = new DenseInstance(attributes.size());
instance.setDataset(instances);
int idx = 0;
instance.setValue(idx, timeStamp);
idx++;
while (currentClassAnnot.getRange().getEnd() < timeStamp) {
if(classIter.hasNext()) {
currentClassAnnot = classIter.next();
} else {
break;
}
}
if (currentClassAnnot.contains(timeStamp)) {
String label = currentClassAnnot.getLabel();
instance.setValue(idx, label);
} else {
instance.setValue(idx, instances.attribute(idx).indexOfValue("?"));
}
idx++;
for (Iterator<? extends Annotation> iter : annotationIterators) {
if (iter.hasNext()) {
if (currentAnnotations == null) {
currentAnnotations = iter.next();
}
while (currentAnnotations.getRange().getEnd() < timeStamp) {
if (iter.hasNext()) {
currentAnnotations = iter.next();
}
}
if (currentAnnotations.getRange()
.contains(timeStamp)) {
String label = currentAnnotations.getLabel();
instance.setValue(idx, label);
}
idx++;
}
}
for (Iterator<? extends Datum> iter : sensorIterators) {
if (iter.hasNext()) {
if (currentData == null) {
currentData = iter.next();
}
while (currentData.timestamp < timeStamp) {
if (iter.hasNext()) {
currentData = iter.next();
}
}
if (currentData.timestamp == timeStamp) {
instance.setValue(idx, currentData.value);
}
idx++;
}
}
instances.add(instance);
timeStamp = getNextTimeStamp(timeStamp, sensorIteratorsTs);
} else {
timeStamp = getNextTimeStamp(timeStamp, sensorIteratorsTs);
}
}
return instances;
}
private ArrayList<Attribute> convertToAttributes(AnnotationTrack classTrack, List<AnnotationTrack> selectedAnnotationTracks, List<ReadableSensorTrack> selectedSensorTracks) {
ArrayList<Attribute> attributes = new ArrayList<>();
attributes.add(new Attribute("Timestamp"));
UniqueDataDecorator<Annotation> utd = new UniqueDataDecorator<>(classTrack, Annotation.LABEL_COMPARATOR);
utd.force();
Set<Annotation> uniques =utd.getUniqueData();
List<String> values = new LinkedList<>();
for(Annotation a: uniques) {
values.add(a.getLabel());
}
attributes.add(new Attribute(String.valueOf(classTrack.getId()), values));
for (AnnotationTrack at : selectedAnnotationTracks) {
utd = new UniqueDataDecorator<>(at, Annotation.LABEL_COMPARATOR);
utd.force();
uniques =utd.getUniqueData();
values = new LinkedList<>();
for(Annotation a: uniques)
values.add(a.getLabel());
attributes.add(new Attribute(String.valueOf(at.getId()), values));
}
for (ReadableSensorTrack st : selectedSensorTracks) {
attributes.add(new Attribute(String.valueOf(st.getId())));
}
return attributes;
}
public long getNextTimeStamp(long timeStamp, LinkedList<Iterator<? extends Datum>> sensorIterators) {
long nextTimeStamp = fullRange.getEnd();
if(nextTimeStamp <= timeStamp + 1)
return nextTimeStamp;
for(int i = 0; i < sensorIterators.size(); i++) {
if (currentData[i] == null) {
if (sensorIterators.get(i).hasNext()) {
currentData[i] = sensorIterators.get(i).next();
}
}
while(currentData[i].timestamp <= timeStamp) {
currentData[i] = sensorIterators.get(i).next();
}
if (currentData[i].timestamp < nextTimeStamp) {
nextTimeStamp = currentData[i].timestamp;
}
}
return nextTimeStamp;
}
}