Пример #1
0
 public boolean ok(String out, String reference) {
   // log.fine("out1: " + a);
   // log.fine("out2: " + b);
   Scanner sa = new Scanner(out);
   Scanner sb = new Scanner(reference);
   while (sa.hasNext() && sb.hasNext()) {
     if (sa.hasNextDouble() || sb.hasNextDouble()) {
       if (!sa.hasNextDouble() || !sb.hasNextDouble()) return true;
       double da = sa.nextDouble();
       double db = sb.nextDouble();
       double d_abs = Math.abs(da - db);
       double d_rel = d_abs / Math.abs(db);
       if (!(d_abs < EPS || d_rel < EPS)) {
         log.fine("NOK, " + da + " too far from " + db);
         return false;
       }
     } else {
       String xa = sa.next();
       String xb = sb.next();
       if (!xa.equals(xb)) {
         log.fine("NOK, " + xa + " != " + xb);
         return false;
       }
     }
   }
   if (sa.hasNext() || sb.hasNext()) {
     log.fine("NOK: different number of tokens.");
     return false;
   }
   return true;
 }
Пример #2
0
  /**
   * Loads into a double[][] a plain text file of numbers, with newlines dividing the numbers into
   * rows and tabs or spaces delimiting columns. The Y dimension is not flipped.
   */
  public static double[][] loadTextFile(InputStream stream) throws IOException {
    Scanner scan = new Scanner(stream);

    ArrayList rows = new ArrayList();
    int width = -1;

    while (scan.hasNextLine()) {
      String srow = scan.nextLine().trim();
      if (srow.length() > 0) {
        int w = 0;
        if (width == -1) // first time compute width
        {
          ArrayList firstRow = new ArrayList();
          Scanner rowScan = new Scanner(new StringReader(srow));
          while (rowScan.hasNextDouble()) {
            firstRow.add(new Double(rowScan.nextDouble())); // ugh, boxed
            w++;
          }
          width = w;
          double[] row = new double[width];
          for (int i = 0; i < width; i++) row[i] = ((Double) (firstRow.get(i))).doubleValue();
          rows.add(row);
        } else {
          double[] row = new double[width];
          Scanner rowScan = new Scanner(new StringReader(srow));
          while (rowScan.hasNextDouble()) {
            if (w == width) // uh oh
            throw new IOException("Row lengths do not match in text file");
            row[w] = rowScan.nextDouble();
            w++;
          }
          if (w < width) // uh oh
          throw new IOException("Row lengths do not match in text file");
          rows.add(row);
        }
      }
    }

    if (width == -1) // got nothing
    return new double[0][0];

    double[][] fieldTransposed = new double[rows.size()][];
    for (int i = 0; i < rows.size(); i++) fieldTransposed[i] = ((double[]) (rows.get(i)));

    // now transpose because we have width first
    double[][] field = new double[width][fieldTransposed.length];
    for (int i = 0; i < field.length; i++)
      for (int j = 0; j < field[i].length; j++) field[i][j] = fieldTransposed[j][i];

    return field;
  }
Пример #3
0
 public static void main(String[] args) {
   while (scan.hasNextDouble()) {
     double t1 = scan.nextDouble();
     double t2 = scan.nextDouble();
     int i1, i2;
     for (i1 = 0; i1 < s1.length; i1++) {
       if (t1 < s1[i1]) break;
     }
     for (i2 = 0; i2 < s2.length; i2++) {
       if (t2 < s2[i2]) break;
     }
     System.out.println(r[Math.max(i1, i2)]);
   }
 }
  protected static ArrayList<Position> readReferencePositions(String filePath)
      throws FileNotFoundException {
    ArrayList<Position> positions = new ArrayList<Position>();
    Scanner scanner = new Scanner(new File(filePath));

    while (scanner.hasNextDouble()) {
      double lat = scanner.nextDouble();
      double lon = scanner.nextDouble();
      double elevation = scanner.nextDouble();
      positions.add(Position.fromDegrees(lat, lon, elevation));
    }

    return positions;
  }
 public static void main(final String[] args) {
   final Scanner sis = new Scanner(System.in);
   output(HELP_MESSAGE);
   while (sis.hasNext()) {
     if (sis.hasNextInt()) {
       final int next = sis.nextInt();
       output("You entered an Integer = %d", next);
     } else if (sis.hasNextLong()) {
       final long next = sis.nextLong();
       output("You entered a Long = %d", next);
     } else if (sis.hasNextDouble()) {
       final double next = sis.nextDouble();
       output("You entered a Double = %f", next);
     } else if (sis.hasNext("\\d+")) {
       final BigInteger next = sis.nextBigInteger();
       output("You entered a BigInteger = %s", next);
     } else if (sis.hasNextBoolean()) {
       final boolean next = sis.nextBoolean();
       output("You entered a Boolean representation = %s", next);
     } else if (sis.hasNext(DATE_PATTERN)) {
       final String next = sis.next(DATE_PATTERN);
       output("You entered a Date representation = %s", next);
     } else // unclassified
     {
       final String next = sis.next();
       if (isValidURL(next)) {
         output("You entered a valid URL = %s", next);
       } else {
         if (EXIT_COMMANDS.contains(next)) {
           output("Exit command %s issued, exiting!", next);
           break;
         } else if (HELP_COMMANDS.contains(next)) {
           output(HELP_MESSAGE);
         } else {
           output("You entered an unclassified String = %s", next);
         }
       }
     }
   }
   /*
      This will close the underlying Readable, in this case System.in, and free those resources.
      You will not be to read from System.in anymore after this you call .close().
      If you wanted to use System.in for something else, then don't close the Scanner.
   */
   sis.close();
   System.exit(0);
 }
Пример #6
0
  /** @param args the command line arguments */
  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

    Vector vector = new Vector();

    double d = 0.00;
    System.out.print("Please enter a number to add to the array: ");
    while (scan.hasNextDouble()) {
      System.out.print("Please enter a number to add to the array: ");
      d = scan.nextDouble();
      vector.addElement(d);
    }

    for (int i = vector.size() - 1; i >= 0; i--) {
      System.out.print(vector.elementAt(i) + " ");
    }
    // TODO code application logic here
  }
Пример #7
0
 public static void main(String[] args) throws IOException {
   Scanner s = new Scanner(new File("number.txt")).useDelimiter(";");
   int noo1 = 0, noo2 = 0, noo3 = 0, noo4 = 0, noo5 = 0;
   String line;
   while (s.hasNextDouble()) {
     double val = s.nextDouble();
     if ((val > 0) && (val < 20)) noo1++;
     else if ((val > 20) && (val < 40)) noo2++;
     else if ((val > 40) && (val < 60)) noo3++;
     else if ((val > 60) && (val < 80)) noo4++;
     else if ((val > 80) && (val < 100)) noo5++;
   }
   s.close();
   System.out.println("Range 0 to 20: " + noo1);
   System.out.println("Range 20 to 40: " + noo2);
   System.out.println("Range 40 to 60: " + noo3);
   System.out.println("Range 60 to 80: " + noo4);
   System.out.println("Range 80 to 100: " + noo5);
 }
Пример #8
0
  /**
   * Method parse will parse the {@link #print()} String representation of a Tuple instance and
   * return a new Tuple instance.
   *
   * @param string of type String
   * @return Tuple
   */
  public static Tuple parse(String string) {
    if (string == null || string.length() == 0) return null;

    string = string.replaceAll("^ *\\[*", "");
    string = string.replaceAll("\\]* *$", "");

    Scanner scanner = new Scanner(new StringReader(string));
    scanner.useDelimiter("(' *, *')|(^ *')|(' *$)");

    Tuple result = new Tuple();

    while (scanner.hasNext()) {
      if (scanner.hasNextInt()) result.add(scanner.nextInt());
      else if (scanner.hasNextDouble()) result.add(scanner.nextDouble());
      else result.add(scanner.next());
    }

    scanner.close();

    return result;
  }
Пример #9
0
 private static String countFile(String filename)
 {
   String result = "";
   int row = 0;
   int rowCheck = 0;
   int col = 0;
   
   Scanner fileScanner = new Scanner(File filename));
   
   while(fileScanner.hasNextLine())
   {
     Scanner lineScanner = new Scanner(fileScanner.nextLine());
     while(lineScanner.hasNextDouble())
     {
       row++;
       lineScanner.next();
     }
     col++;
                                       
     if(row = 0)
     {
       rowCheck = row;
     }
     else if(row != rowCheck)
     {
       System.out.println("Number of rows in the file is inconsistent.");
     }
                                       
     lineScanner.close();
   }
   
   fileScanner.close();
   
   result = row + " " col;
   return result;
 }
Пример #10
0
  public static void main(String[] args) {
    //////////////////////////
    // The values in following 4 lines should be user input
    int startPos = 200;
    int endPos = 1000;
    int totalNumPixel = 1044;
    double threshhold = 0.0001;
    /////////////////////////
    int numPixel = endPos - startPos;
    double wavelength[] = new double[totalNumPixel];
    double photonCount[] = new double[totalNumPixel];
    double SpecNoBk[] = new double[numPixel];
    double ThisSpectrum[] = new double[numPixel];
    double ThisXaxis[] = new double[numPixel];
    double Po[] = new double[numPixel];
    double Re[] = new double[numPixel];
    double P[] = new double[6];
    double Re2[] = new double[numPixel - 1];
    double mySUM[] = new double[numPixel];
    int ind[];
    double DEV;
    double prevDEV;
    Connection connection = null;
    Statement stmt = null;
    String pattern = "##.##";

    try {
      Scanner in = new Scanner(new FileReader(args[0]));
      int i = 0;
      while (in.hasNextDouble()) {
        wavelength[i] = in.nextDouble();
        photonCount[i] = in.nextDouble();
        ++i;
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }

    ThisSpectrum = Arrays.copyOfRange(photonCount, startPos, endPos);
    ThisXaxis = Arrays.copyOfRange(wavelength, startPos, endPos);
    final WeightedObservedPoints obs = new WeightedObservedPoints();

    for (int i = 0; i < numPixel; i++) {
      obs.add(ThisXaxis[i], ThisSpectrum[i]);
    }

    final PolynomialCurveFitter fitter = PolynomialCurveFitter.create(5);
    P = fitter.fit(obs.toList());
    Polyval pVal = new Polyval(P, ThisXaxis, numPixel);
    Po = pVal.evl();

    for (int i = 0; i < numPixel; i++) {
      Re[i] = ThisSpectrum[i] - Po[i];
    }

    for (int i = 0; i < numPixel - 1; i++) {
      Re2[i] = Re[i + 1] - Re[i];
    }

    DEV = Math.sqrt(StatUtils.populationVariance(Re2, 0, Re2.length));
    for (int i = 0; i < numPixel; i++) {
      mySUM[i] = Po[i] + DEV;
    }

    int jj = 0; // jj is the length of points to be removed
    for (int i = 0; i < numPixel; i++) {
      if (ThisSpectrum[i] > mySUM[i]) {
        jj++;
        ;
      }
    }
    ind = new int[jj];

    int jjj = 0;
    for (int i = 0; i < numPixel; i++) {
      if (ThisSpectrum[i] > mySUM[i]) {
        ind[jjj] = i;
        jjj++;
      }
    }

    int indKeepLength = numPixel - ind.length;
    int indKeep[] = new int[indKeepLength];
    int k = 0;
    for (int i = 0; i < numPixel; i++) {
      if (!ArrayUtils.contains(ind, i)) {
        indKeep[k] = i;
        k++;
      }
    }
    double ThisSpectrumKeep[] = new double[indKeepLength];
    double ThisXaxisKeep[] = new double[indKeepLength];
    double PoKeep[] = new double[indKeepLength];
    double ReKeep[] = new double[indKeepLength];
    double Re2Keep[] = new double[indKeepLength - 1];
    double mySUMKeep[] = new double[indKeepLength];

    for (int i = 0; i < indKeepLength; i++) {
      ThisSpectrumKeep[i] = ThisSpectrum[indKeep[i]];
      ThisXaxisKeep[i] = ThisXaxis[indKeep[i]];
    }

    prevDEV = DEV;

    // at the point, ThisSpectrum and ThisXaxis should have reduced size
    final WeightedObservedPoints obs1 = new WeightedObservedPoints();

    for (int i = 0; i < indKeepLength; i++) {
      obs1.add(ThisXaxisKeep[i], ThisSpectrumKeep[i]);
    }

    while (true) {
      final PolynomialCurveFitter fitter1 = PolynomialCurveFitter.create(5);
      P = fitter1.fit(obs1.toList());
      Polyval pVal1 = new Polyval(P, ThisXaxisKeep, indKeepLength);
      PoKeep = pVal1.evl();

      for (int i = 0; i < indKeepLength; i++) {
        ReKeep[i] = ThisSpectrumKeep[i] - PoKeep[i];
      }

      for (int i = 0; i < indKeepLength - 1; i++) {
        Re2Keep[i] = ReKeep[i + 1] - ReKeep[i];
      }

      DEV = Math.sqrt(StatUtils.populationVariance(Re2Keep, 0, Re2Keep.length));

      for (int i = 0; i < indKeepLength; i++) {
        mySUMKeep[i] = PoKeep[i] + DEV;
      }

      for (int i = 0; i < indKeepLength; i++) {
        if (ThisSpectrumKeep[i] > mySUMKeep[i]) ThisSpectrumKeep[i] = mySUMKeep[i];
      }
      if ((Math.abs(DEV - prevDEV) / DEV) < threshhold) break;
      prevDEV = DEV;

      obs1.clear();
      for (int i = 0; i < indKeepLength; i++) {
        obs1.add(ThisXaxisKeep[i], ThisSpectrumKeep[i]);
      }
    }
    Polyval pVal2 = new Polyval(P, ThisXaxis, numPixel);
    double FLbk[] = pVal2.evl();
    for (int i = 0; i < ThisXaxis.length; i++) {
      SpecNoBk[i] = ThisSpectrum[i] - FLbk[i];
    }

    // the write-to-file part is only for testing purpose, ThisXaxis and SpecNoBk are two outputs
    try {
      FileWriter fr = new FileWriter(args[1]);
      BufferedWriter br = new BufferedWriter(fr);
      PrintWriter out = new PrintWriter(br);
      DecimalFormat df = new DecimalFormat(pattern);
      for (int j = 0; j < ThisXaxis.length; j++) {
        if (Double.toString(wavelength[j]) != null) out.write(ThisXaxis[j] + "\t" + SpecNoBk[j]);
        out.write("\r\n");
      }
      out.close();
    } catch (IOException e) {
      System.out.println(e);
    }
  }
Пример #11
0
  public static void main(String[] args) {

    int minClusterNo = 1;
    int maxClusterNo = Integer.MAX_VALUE;

    int tupleLength = 200;
    int tupleCount = 0;
    List<Double> dataArrayList = new ArrayList<>();

    String dataset = "genes-cancer";
    //        String dataset = "RiMG75";
    String embedding_file =
        "/Users/HanWang/Workspace/sci-kb/data/" + dataset + "/subj_obj_embeddings.txt";

    try {
      Scanner EmbeddingFile = new Scanner(new File(embedding_file));
      while (EmbeddingFile.hasNextLine()) {
        String line = EmbeddingFile.nextLine();
        Scanner scanner = new Scanner(line);
        scanner.useDelimiter(" ");
        while (scanner.hasNextDouble()) {
          dataArrayList.add(scanner.nextDouble());
        }
        scanner.close();
        tupleCount++;
      }
      EmbeddingFile.close();
    } catch (Exception e) {
      System.err.println("Exception: " + e.getMessage());
    }

    double[] data = new double[tupleCount * tupleLength];
    for (int i = 0; i < tupleCount * tupleLength; i++) {
      data[i] = dataArrayList.get(i);
    }

    // Wrap the data in an ArrayTupleList.
    TupleList tupleData = new ArrayTupleList(tupleLength, tupleCount, data);

    // Initial seed
    int[] allIDs = new int[tupleCount];
    for (int i = 0; i < tupleCount; i++) {
      allIDs[i] = i;
    }
    double[] center = TupleMath.average(tupleData, new ArrayIntIterator(allIDs));
    Cluster cluster = new Cluster(allIDs, center);
    TupleList initSeeds = createTwoSeeds(tupleData, cluster);

    // Construct the parameters.
    XMeansParams.Builder builder = new XMeansParams.Builder();
    XMeansParams params =
        builder
            .minClusters(minClusterNo)
            .maxClusters(maxClusterNo)
            .minClusterToMeanThreshold(0)
            .clusterSeeder(new PreassignedSeeder(initSeeds))
            .build();

    // Display the default xmeans parameters.
    //
    ClusterSeeder seeder = params.getClusterSeeder();
    DistanceMetric distMetric = params.getDistanceMetric();
    int maxClusters = params.getMaxClusters();
    int minClusters = params.getMinClusters();
    double minClusterToMeanThreshold = params.getMinClusterToMeanThreshold();
    boolean useOverallBIC = params.getUseOverallBIC();
    int workerThreadCount = params.getWorkerThreadCount();

    System.out.println("XMeans Default Parameters:\n");
    System.out.printf("\tCluster seeding type = %s\n", seeder.getClass().getSimpleName());
    System.out.printf("\tDistance metric = %s\n", distMetric.getClass().getSimpleName());
    System.out.printf("\tmin clusters = %d, max clusters = %d\n", minClusters, maxClusters);
    System.out.printf("\tmin cluster to mean threshold = %f\n", minClusterToMeanThreshold);
    System.out.printf("\tUsing overall BIC = %s\n", String.valueOf(useOverallBIC));
    System.out.printf("\tWorker thread count = %d\n\n", workerThreadCount);

    // Construct the XMeansClusterer
    Clusterer xmeans = new XMeansClusterer(tupleData, params);

    // Add a listener for task life cycle events.
    xmeans.addTaskListener(
        new TaskListener() {

          @Override
          public void taskBegun(TaskEvent e) {
            System.out.printf("%s\n\n", e.getMessage());
          }

          @Override
          public void taskMessage(TaskEvent e) {
            System.out.println("  ... " + e.getMessage());
          }

          @Override
          public void taskProgress(TaskEvent e) {
            // Reports the progress.  Ignore for this example.
          }

          @Override
          public void taskPaused(TaskEvent e) {
            // Reports that the task has been paused. Won't happen in this example,
            // so ignore.
          }

          @Override
          public void taskResumed(TaskEvent e) {
            // Reports when a paused task has been resumed.
            // Ignore for this example.
          }

          @Override
          public void taskEnded(TaskEvent e) {
            // Reports when a task has finished regardless of whether the task succeeded or
            // failed. Check the task outcome to know if it succeeded.
            System.out.printf("\n%s\n\n", e.getMessage());
          }
        });

    // Launch xmeans on another thread.
    Thread t = new Thread(xmeans);
    t.start();

    List<Cluster> clusters = null;

    try {

      // Since a Clusterer implements Future<List<Cluster>>, it has a blocking get() method.
      clusters = xmeans.get();

      // Don't have to worry about these, because the task outcome will tell you what happened.
    } catch (InterruptedException e1) {
      e1.printStackTrace();
    } catch (ExecutionException e2) {
      e2.printStackTrace();
    }

    if (xmeans.getTaskOutcome() == TaskOutcome.SUCCESS) {
      System.out.println("XMeans was successful!");
    } else if (xmeans.getTaskOutcome() == TaskOutcome.ERROR) {
      System.out.println("XMeans failed with the error: " + xmeans.getErrorMessage());
    } else if (xmeans.getTaskOutcome() == TaskOutcome.CANCELLED) {
      System.out.println(
          "XMeans was somehow canceled, even though this method doesn't provide a path to cancellation.");
    } else {
      System.out.println(
          "XMeans finished with unexpected outcome "
              + xmeans.getTaskOutcome()
              + ": please submit a bug report!");
    }

    if (clusters != null) {
      // May not be the same as clusterCount, since XMeans attempted to statistically discern the
      // distribution.
      final int xmeansClusterCount = clusters.size();
      System.out.printf("\nXMeans Generated %d Clusters\n", xmeansClusterCount);

      try {
        File clusterFile =
            new File("/Users/HanWang/Workspace/sci-kb/data/" + dataset + "/clusters.txt");
        if (!clusterFile.exists()) {
          clusterFile.createNewFile();
        }
        FileWriter wr = new FileWriter(clusterFile);
        int memberCount = 0;
        for (int i = 0; i < xmeansClusterCount; i++) {
          Cluster c = clusters.get(i);
          int clusterSize = c.getMemberCount();
          int[] clusterMembers = new int[clusterSize];
          for (int j = 0; j < clusterSize; j++) {
            clusterMembers[j] = c.getMember(j);
            wr.write(clusterMembers[j] + " ");
            memberCount++;
          }
          wr.write(System.getProperty("line.separator"));
          System.out.printf(
              "Cluster %d: size = %d, members = %s\n",
              (i + 1), clusterSize, Arrays.toString(clusterMembers));
        }
        wr.flush();
        wr.close();
        System.out.printf("Total data point: %d\n", memberCount);
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }