@Override
 public void keyTyped(KeyEvent e) {
   if (e.getKeyChar() == 's') {
     System.out.println("Writing config!");
     try {
       IOUtils.writeASCII(new File("camera.conf"), this.touchTable.cameraConfig);
       System.out.println("Camera config written");
     } catch (Exception e1) {
       System.out.println("Failed to write camera.conf: " + e1.getMessage());
     }
   } else if (e.getKeyChar() == 'c') {
     this.touchTable.clear();
   } else if (e.getKeyChar() == 'l') {
     System.out.println("Loading config!");
     try {
       TriangleCameraConfig newCC =
           IOUtils.read(new File("camera.conf"), new TriangleCameraConfig());
       this.touchTable.setCameraConfig(newCC);
       System.out.println("Read camera config");
     } catch (Exception e1) {
       System.out.println("Failed to read camera config");
     }
   } else if (e.getKeyChar() == 't') {
     if (this.touchTable.mode instanceof Mode.DRAWING_TRACKED)
       this.touchTable.mode = new Mode.DRAWING(this.touchTable);
     else this.touchTable.mode = new Mode.DRAWING_TRACKED(this.touchTable);
   } else if (e.getKeyChar() == 'm') {
     this.touchTable.mode = new Mode.SERVER(this.touchTable);
   } else if (e.getKeyChar() == 'p') {
     this.touchTable.mode = new Mode.PONG(this.touchTable);
   }
 }
    @Override
    public void map(
        final Text key,
        BytesWritable value,
        final Mapper<Text, BytesWritable, Text, LongWritable>.Context context)
        throws InterruptedException {
      try {
        final long[] largest = new long[] {0};
        final boolean[] anyDayOverLimit = new boolean[] {false};
        IOUtils.deserialize(
            value.getBytes(),
            new ReadableListBinary<Object>(new ArrayList<Object>()) {
              @Override
              protected Object readValue(DataInput in) throws IOException {
                WordDFIDF idf = new WordDFIDF();
                idf.readBinary(in);
                if (idf.wf > wordTimeCountThresh) {
                  anyDayOverLimit[0] = true;
                }
                if (largest[0] < idf.Twf) largest[0] = idf.Twf;

                return new Object();
              }
            });
        if (anyDayOverLimit[0]) // One of the days was over the day limit
        context.write(key, new LongWritable(largest[0]));

      } catch (IOException e) {
        System.err.println("Couldnt read word: " + key);
      }
    }
 @Override
 protected void map(
     final Text key,
     BytesWritable value,
     final Mapper<Text, BytesWritable, Text, BytesWritable>.Context context)
     throws java.io.IOException, InterruptedException {
   if (wordlist.contains(key.toString())) {
     IOUtils.deserialize(
         value.getBytes(),
         new ReadableListBinary<Object>(new ArrayList<Object>()) {
           @Override
           protected Object readValue(DataInput in) throws IOException {
             WordDFIDF idf = new WordDFIDF();
             idf.readBinary(in);
             try {
               ByteArrayOutputStream baos = new ByteArrayOutputStream();
               IOUtils.writeBinary(baos, idf);
               context.write(key, new BytesWritable(baos.toByteArray()));
             } catch (InterruptedException e) {
               throw new IOException("");
             }
             return NullWritable.get();
           }
         });
   }
 };
  @Override
  public void performExperiment() throws IOException {
    BilinearLearnerParameters params = new BilinearLearnerParameters();
    params.put(BilinearLearnerParameters.ETA0_U, 0.02);
    params.put(BilinearLearnerParameters.ETA0_W, 0.02);
    params.put(BilinearLearnerParameters.LAMBDA, 0.001);
    params.put(BilinearLearnerParameters.BICONVEX_TOL, 0.01);
    params.put(BilinearLearnerParameters.BICONVEX_MAXITER, 10);
    params.put(BilinearLearnerParameters.BIAS, true);
    params.put(BilinearLearnerParameters.ETA0_BIAS, 0.5);
    params.put(BilinearLearnerParameters.WINITSTRAT, new SingleValueInitStrat(0.1));
    params.put(BilinearLearnerParameters.UINITSTRAT, new SparseZerosInitStrategy());
    BillMatlabFileDataGenerator bmfdg =
        new BillMatlabFileDataGenerator(new File(BILL_DATA()), 98, true);
    prepareExperimentLog(params);
    for (int i = 0; i < bmfdg.nFolds(); i++) {
      logger.debug("Fold: " + i);
      BilinearSparseOnlineLearner learner = new BilinearSparseOnlineLearner(params);
      learner.reinitParams();

      bmfdg.setFold(i, Mode.TEST);
      List<Pair<Matrix>> testpairs = new ArrayList<Pair<Matrix>>();
      while (true) {
        Pair<Matrix> next = bmfdg.generate();
        if (next == null) break;
        testpairs.add(next);
      }
      logger.debug("...training");
      bmfdg.setFold(i, Mode.TRAINING);
      int j = 0;
      while (true) {
        Pair<Matrix> next = bmfdg.generate();
        if (next == null) break;
        logger.debug("...trying item " + j++);
        learner.process(next.firstObject(), next.secondObject());
        Matrix u = learner.getU();
        Matrix w = learner.getW();
        Matrix bias = MatrixFactory.getDenseDefault().copyMatrix(learner.getBias());
        BilinearEvaluator eval = new RootMeanSumLossEvaluator();
        eval.setLearner(learner);
        double loss = eval.evaluate(testpairs);
        logger.debug(String.format("Saving learner, Fold %d, Item %d", i, j));
        File learnerOut = new File(FOLD_ROOT(i), String.format("learner_%d", j));
        IOUtils.writeBinary(learnerOut, learner);
        logger.debug("W row sparcity: " + SandiaMatrixUtils.rowSparcity(w));
        logger.debug("U row sparcity: " + SandiaMatrixUtils.rowSparcity(u));
        Boolean biasMode = learner.getParams().getTyped(BilinearLearnerParameters.BIAS);
        if (biasMode) {
          logger.debug("Bias: " + SandiaMatrixUtils.diag(bias));
        }
        logger.debug(String.format("... loss: %f", loss));
      }
    }
  }
Esempio n. 5
0
 @Override
 public void failedURL(URL url, String reason) {
   try {
     StringWriter writer = new StringWriter();
     if (url == null) return;
     IOUtils.writeASCII(writer, new WriteableFailedURL(url, reason));
     publisher.send("FAIL".getBytes("UTF-8"), ZMQ.SNDMORE);
     boolean sent = publisher.send(writer.toString().getBytes("UTF-8"), 0);
     if (!sent) {
       throw new IOException("Send failed");
     }
   } catch (IOException e) {
     logger.error("Unable to send failure!");
   }
 }
Esempio n. 6
0
 @Override
 public void newImageDownloaded(WriteableImageOutput written) {
   try {
     StringWriter writer = new StringWriter();
     IOUtils.writeASCII(writer, written);
     publisher.send("IMAGE".getBytes("UTF-8"), ZMQ.SNDMORE);
     boolean sent = publisher.send(writer.toString().getBytes("UTF-8"), 0);
     if (!sent) {
       throw new IOException("Send failed");
     }
   } catch (IOException e) {
     logger.error("Unable to send written image: " + written.url);
     logger.error(e.getMessage());
   }
 }