net.seninp.jmotif.sax.SAXException Java Examples

The following examples show how to use net.seninp.jmotif.sax.SAXException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestIssue11.java    From SAX with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test() {
  try {

    SAXRecords saxTransformW4 = sp.ts2saxViaWindow(series30, 4, paaSize, na.getCuts(alphabetSize),
        nrStrategy, normThreshold);
    assertEquals(27, saxTransformW4.getAllIndices().size());

    SAXRecords saxTransformW30 = sp.ts2saxViaWindow(series30, 30, paaSize,
        na.getCuts(alphabetSize), nrStrategy, normThreshold);
    assertEquals(1, saxTransformW30.getAllIndices().size());

  }
  catch (SAXException e) {
    fail("exception shall not be thrown!");
  }
}
 
Example #2
Source File: TextProcessor.java    From sax-vsm_classic with GNU General Public License v2.0 6 votes vote down vote up
public List<WordBag> labeledSeries2WordBags(Map<String, List<double[]>> data, Params params)
    throws SAXException {

  // make a map of resulting bags
  Map<String, WordBag> preRes = new HashMap<String, WordBag>();

  // process series one by one building word bags
  for (Entry<String, List<double[]>> e : data.entrySet()) {

    String classLabel = e.getKey();
    WordBag bag = new WordBag(classLabel);

    for (double[] series : e.getValue()) {
      WordBag cb = seriesToWordBag("tmp", series, params);
      bag.mergeWith(cb);
    }

    preRes.put(classLabel, bag);
  }

  List<WordBag> res = new ArrayList<WordBag>();
  res.addAll(preRes.values());
  return res;
}
 
Example #3
Source File: SAXVSMClassifier.java    From sax-vsm_classic with GNU General Public License v2.0 6 votes vote down vote up
private static void classify(Params params) throws SAXException {
  // making training bags collection
  List<WordBag> bags = tp.labeledSeries2WordBags(trainData, params);
  // getting TFIDF done
  HashMap<String, HashMap<String, Double>> tfidf = tp.computeTFIDF(bags);
  // classifying
  int testSampleSize = 0;
  int positiveTestCounter = 0;
  for (String label : tfidf.keySet()) {
    List<double[]> testD = testData.get(label);
    for (double[] series : testD) {
      positiveTestCounter = positiveTestCounter
          + tp.classify(label, series, tfidf, params);
      testSampleSize++;
    }
  }

  // accuracy and error
  double accuracy = (double) positiveTestCounter / (double) testSampleSize;
  double error = 1.0d - accuracy;

  // report results
  System.out.println("classification results: " + toLogStr(params, accuracy, error));

}
 
Example #4
Source File: KalpakisConverter.java    From SAX with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException, SAXException {

    SAXProcessor sp = new SAXProcessor();

    for (String fname : filenames) {
      String inFname = pathPrefix + fname;
      String outFname = inFname + ".shingled.txt";

      double[] series = TSProcessor.readFileColumn(inFname, 0, 0);
      System.err.println("read " + inFname + ", " + series.length + " points ...");

      Map<String, Integer> shingledData = sp.ts2Shingles(series, WIN_SIZE, PAA_SIZE, A_SIZE,
          NumerosityReductionStrategy.NONE, 0.001, LEVEL);

      StringBuilder shingles = new StringBuilder(
          BitmapParameters.SHINGLE_SIZE * (shingledData.size() + 2));
      StringBuilder freqs = new StringBuilder(
          BitmapParameters.SHINGLE_SIZE * (shingledData.size() + 2));
      TreeSet<String> keys = new TreeSet<String>(shingledData.keySet());
      for (String shingle : keys) {
        shingles.append(QUOTE).append(shingle).append(QUOTE).append(COMMA);
        freqs.append(shingledData.get(shingle)).append(COMMA);
      }

      BufferedWriter bw = new BufferedWriter(new FileWriter(new File(outFname)));
      bw.write(shingles.delete(shingles.length() - 1, shingles.length()).toString());
      bw.write(CR);
      bw.write(freqs.delete(freqs.length() - 1, freqs.length()).toString());
      bw.write(CR);
      bw.close();

    }

  }
 
Example #5
Source File: TS2SequiturGrammar.java    From grammarviz2_src with GNU General Public License v2.0 5 votes vote down vote up
private static SAXRecords discretize(double[] series) throws SAXException {
    consoleLogger.info("Performing SAX conversion ...");
    if (TS2GrammarParameters.NUM_WORKERS <= 1) {
        return sp.ts2saxViaWindow(series, TS2GrammarParameters.SAX_WINDOW_SIZE,
                                  TS2GrammarParameters.SAX_PAA_SIZE, na.getCuts(TS2GrammarParameters.SAX_ALPHABET_SIZE),
                                  TS2GrammarParameters.SAX_NR_STRATEGY, TS2GrammarParameters.SAX_NORM_THRESHOLD);
    } else {
        return psax.process(series, TS2GrammarParameters.NUM_WORKERS, TS2GrammarParameters.SAX_WINDOW_SIZE,
                                  TS2GrammarParameters.SAX_PAA_SIZE, TS2GrammarParameters.SAX_ALPHABET_SIZE,
                                  TS2GrammarParameters.SAX_NR_STRATEGY, TS2GrammarParameters.SAX_NORM_THRESHOLD);
    }
}
 
Example #6
Source File: PrintSAXProcess.java    From SAX with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws IOException, SAXException {

    double[] ts = TSProcessor.readFileColumn(INPUT_FNAME, 0, 0);

    double[] cuts = na.getCuts(SAX_A_SIZE);

    BufferedWriter bw = new BufferedWriter(new FileWriter(new File("test_sax.txt")));

    // scan across the time series extract sub sequences, and convert them to strings
    char[] previousString = null;

    for (int i = 0; i <= ts.length - SAX_WIN_SIZE; i++) {

      StringBuffer sb = new StringBuffer();
      sb.append(i).append(TAB);

      // fix the current subsection
      double[] subSection = Arrays.copyOfRange(ts, i, i + SAX_WIN_SIZE);
      sb.append(Arrays.toString(subSection).replaceAll("\\s+", "")).append(TAB);

      // Z normalize it
      subSection = tsProcessor.znorm(subSection, SAX_NORM_THRESHOLD);

      // perform PAA conversion if needed
      double[] paa = tsProcessor.paa(subSection, SAX_PAA_SIZE);
      sb.append(Arrays.toString(paa).replaceAll("\\s+", "")).append(TAB);

      // Convert the PAA to a string.
      char[] currentString = tsProcessor.ts2String(paa, cuts);
      sb.append("\"").append(currentString).append("\"").append(TAB);

      if (null != previousString) {

        if (NumerosityReductionStrategy.EXACT.equals(NR_STRATEGY)
            && Arrays.equals(previousString, currentString)) {
          // NumerosityReduction
          sb.append("skipped").append(CR);
          bw.write(sb.toString());
          continue;
        }
        else if (NumerosityReductionStrategy.MINDIST.equals(NR_STRATEGY)
            && sp.checkMinDistIsZero(previousString, currentString)) {
          continue;
        }

      }

      previousString = currentString;
      sb.append("kept").append(CR);
      bw.write(sb.toString());

    }

    bw.close();

  }
 
Example #7
Source File: NormalAlphabet.java    From SAX with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public double[] getCuts(Integer size) throws SAXException {
  switch (size) {
  case 2:
    return case2;
  case 3:
    return case3;
  case 4:
    return case4;
  case 5:
    return case5;
  case 6:
    return case6;
  case 7:
    return case7;
  case 8:
    return case8;
  case 9:
    return case9;
  case 10:
    return case10;
  case 11:
    return case11;
  case 12:
    return case12;
  case 13:
    return case13;
  case 14:
    return case14;
  case 15:
    return case15;
  case 16:
    return case16;
  case 17:
    return case17;
  case 18:
    return case18;
  case 19:
    return case19;
  case 20:
    return case20;
  default:
    throw new SAXException("Invalid alphabet size.");
  }
}
 
Example #8
Source File: NormalAlphabet.java    From SAX with GNU General Public License v2.0 4 votes vote down vote up
@Override
public double[][] getDistanceMatrix(Integer size) throws SAXException {
  switch (size) {
  case 2:
    return distance_case2;
  case 3:
    return distance_case3;
  case 4:
    return distance_case4;
  case 5:
    return distance_case5;
  case 6:
    return distance_case6;
  case 7:
    return distance_case7;
  case 8:
    return distance_case8;
  case 9:
    return distance_case9;
  case 10:
    return distance_case10;
  case 11:
    return distance_case11;
  case 12:
    return distance_case12;
  case 13:
    return distance_case13;
  case 14:
    return distance_case14;
  case 15:
    return distance_case15;
  case 16:
    return distance_case16;
  case 17:
    return distance_case17;
  case 18:
    return distance_case18;
  case 19:
    return distance_case19;
  case 20:
    return distance_case20;
  default:
    throw new SAXException("Invalid alphabet size.");
  }
}
 
Example #9
Source File: NormalAlphabet.java    From SAX with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Produces central cut intervals lines for the normal distribution.
 * 
 * @param size the Alphabet size [2 - 20].
 * @return an array of central lines for the specified alphabet size.
 * @throws SAXException if error occurs.
 */
public double[] getCentralCuts(Integer size) throws SAXException {
  switch (size) {
  case 2:
    return center_case2;
  case 3:
    return center_case3;
  case 4:
    return center_case4;
  case 5:
    return center_case5;
  case 6:
    return center_case6;
  case 7:
    return center_case7;
  case 8:
    return center_case8;
  case 9:
    return center_case9;
  case 10:
    return center_case10;
  case 11:
    return center_case11;
  case 12:
    return center_case12;
  case 13:
    return center_case13;
  case 14:
    return center_case14;
  case 15:
    return center_case15;
  case 16:
    return center_case16;
  case 17:
    return center_case17;
  case 18:
    return center_case18;
  case 19:
    return center_case19;
  case 20:
    return center_case20;
  default:
    throw new SAXException("Invalid alphabet size.");
  }
}
 
Example #10
Source File: TextProcessor.java    From sax-vsm_classic with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Converts time series to a word bag.
 * 
 * @param label the wordbag label.
 * @param ts timeseries.
 * @param params parameters for SAX transform.
 * @return word bag.
 * @throws SAXException if error occurs.
 */
public WordBag seriesToWordBag(String label, double[] ts, Params params) throws SAXException {

  WordBag resultBag = new WordBag(label);

  // scan across the time series extract sub sequences, and convert them to strings
  char[] previousString = null;

  for (int i = 0; i <= ts.length - params.windowSize; i++) {

    // fix the current subsection
    double[] subSection = Arrays.copyOfRange(ts, i, i + params.windowSize);

    // Z normalize it
    subSection = tp.znorm(subSection, params.nThreshold);

    // perform PAA conversion if needed
    double[] paa = tp.paa(subSection, params.paaSize);

    // Convert the PAA to a string.
    char[] currentString = tp.ts2String(paa, na.getCuts(params.alphabetSize));

    if (null != previousString) {

      if (NumerosityReductionStrategy.EXACT.equals(params.nrStartegy)
          && Arrays.equals(previousString, currentString)) {
        // NumerosityReduction
        continue;
      }
      else if (NumerosityReductionStrategy.MINDIST.equals(params.nrStartegy)
          && sp.checkMinDistIsZero(previousString, currentString)) {
        continue;
      }

    }

    previousString = currentString;

    resultBag.addWord(String.valueOf(currentString));
  }

  return resultBag;
}
 
Example #11
Source File: TS2SequiturGrammar.java    From grammarviz2_src with GNU General Public License v2.0 4 votes vote down vote up
private static double[] readTimeSeries() throws SAXException, IOException {
    consoleLogger.info("Reading data ...");
    double[] series = tp.readTS(TS2GrammarParameters.IN_FILE, 0);
    consoleLogger.info("read " + series.length + " points from " + TS2GrammarParameters.IN_FILE);
    return series;
}
 
Example #12
Source File: TextProcessor.java    From sax-vsm_classic with GNU General Public License v2.0 3 votes vote down vote up
public int classify(String classKey, double[] series,
    HashMap<String, HashMap<String, Double>> tfidf, Params params) throws SAXException {

  WordBag test = seriesToWordBag("test", series, params);

  return classify(classKey, test, tfidf);
}
 
Example #13
Source File: Alphabet.java    From SAX with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Get cut intervals corresponding to the alphabet size.
 * 
 * @param size The alphabet size.
 * @return cut intervals for the alphabet.
 * @throws SAXException if error occurs.
 */
public abstract double[] getCuts(Integer size) throws SAXException;
 
Example #14
Source File: Alphabet.java    From SAX with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Get the distance matrix for the alphabet size.
 * 
 * @param size The alphabet size.
 * @return The distance matrix.
 * @throws SAXException if error occurs.
 */
public abstract double[][] getDistanceMatrix(Integer size) throws SAXException;