return new Context(outputLayout.prefixTable(tableWriter, algo, ds));
}
@Override
public Void doMeasureUser(TestUser user, Context context) {
SparseVector ratings = user.getTestRatings();
SparseVector predictions = user.getPredictions();
if (predictions == null) {
predictions = MutableSparseVector.create();
}
LongSortedSet items = ratings.keySet();
if (!items.containsAll(predictions.keySet())) {
items = LongUtils.setUnion(items, predictions.keySet());
}
logger.debug("outputting {} predictions for user {}", predictions.size(), user.getUserId());
for (LongIterator iter = items.iterator(); iter.hasNext(); /* no increment */) {
long item = iter.nextLong();
Double rating = ratings.containsKey(item) ? ratings.get(item) : null;
Double pred = predictions.containsKey(item) ? predictions.get(item) : null;
try {
List<Object> row = Lists.<Object>newArrayList(user.getUserId(), item, rating, pred);
for (Pair<Symbol, String> chan: channels) {
SparseVector v = predictions.getChannelVector(chan.getLeft());
if (v != null && v.containsKey(item)) {
row.add(v.get(item));
} else {
row.add(null);
}
}
context.writer.writeRow(row);