/** * Calculates the Euclidean distance between two points. Don't use this unless you need that. * * @param subseries The first subsequence -- ASSUMED TO BE Z-normalized. * @param series The second point. * @param from the initial index of the range to be copied, inclusive * @param to the final index of the range to be copied, exclusive. (This index may lie outside the * array.) * @param nThreshold z-Normalization threshold. * @return The Euclidean distance between z-Normalized versions of subsequences. */ private static double distance(double[] subseries, double[] series, int from, int to, double nThreshold) throws Exception { double[] subsequence = tp.znorm(tp.subseriesByCopy(series, from, to), nThreshold); Double sum = 0D; for (int i = 0; i < subseries.length; i++) { double tmp = subseries[i] - subsequence[i]; sum = sum + tmp * tmp; } return Math.sqrt(sum); }
/** * Calculates the Euclidean distance between two points. Don't use this unless you need that. * * @param subseries The first subsequence -- ASSUMED TO BE Z-normalized. * @param series The second point. * @param from the initial index of the range to be copied, inclusive * @param to the final index of the range to be copied, exclusive. (This index may lie outside the * array.) * @param nThreshold z-Normalization threshold. * @return The Euclidean distance between z-Normalized versions of subsequences. */ private static double distance(double[] subseries, double[] series, int from, int to, double nThreshold) throws Exception { double[] subsequence = tp.znorm(tp.subseriesByCopy(series, from, to), nThreshold); Double sum = 0D; for (int i = 0; i < subseries.length; i++) { double tmp = subseries[i] - subsequence[i]; sum = sum + tmp * tmp; } return Math.sqrt(sum); }
/** * Convert the timeseries into SAX string representation. * * @param ts the timeseries. * @param paaSize the PAA size. * @param cuts the alphabet cuts. * @param nThreshold the normalization thresholds. * * @return The SAX representation for timeseries. * @throws SAXException if error occurs. */ public char[] ts2string(double[] ts, int paaSize, double[] cuts, double nThreshold) throws SAXException { if (paaSize == ts.length) { return tsProcessor.ts2String(tsProcessor.znorm(ts, nThreshold), cuts); } else { // perform PAA conversion double[] paa = tsProcessor.paa(tsProcessor.znorm(ts, nThreshold), paaSize); return tsProcessor.ts2String(paa, cuts); } }
/** * Early abandoning distance configure by range. * * @param series the series to use. * @param a the position of subseries a. * @param b the position of subseries b. * @param motifSize the motif length. * @param range the range value. * @param znormThreshold z-normalization threshold. * @return a distance value or NAN if above the threshold. */ private static Double eaDistance(double[] series, int a, int b, Integer motifSize, double range, double znormThreshold) { distCounter++; double cutOff2 = range * range; double[] seriesA = tp.znorm(tp.subseriesByCopy(series, a, a + motifSize), znormThreshold); double[] seriesB = tp.znorm(tp.subseriesByCopy(series, b, b + motifSize), znormThreshold); Double res = 0D; for (int i = 0; i < motifSize; i++) { res = res + distance2(seriesA[i], seriesB[i]); if (res > cutOff2) { eaCounter++; return Double.NaN; } } return Math.sqrt(res); }
/** * Early abandoning distance configure by range. * * @param series the series to use. * @param a the position of subseries a. * @param b the position of subseries b. * @param motifSize the motif length. * @param range the range value. * @param znormThreshold z-normalization threshold. * @return a distance value or NAN if above the threshold. */ private static Double eaDistance(double[] series, int a, int b, Integer motifSize, double range, double znormThreshold) { distCounter++; double cutOff2 = range * range; double[] seriesA = tp.znorm(tp.subseriesByCopy(series, a, a + motifSize), znormThreshold); double[] seriesB = tp.znorm(tp.subseriesByCopy(series, b, b + motifSize), znormThreshold); Double res = 0D; for (int i = 0; i < motifSize; i++) { res = res + distance2(seriesA[i], seriesB[i]); if (res > cutOff2) { eaCounter++; return Double.NaN; } } return Math.sqrt(res); }
/** * Early abandoning distance configure by range. * * @param series the series to use. * @param a the position of subseries a. * @param b the position of subseries b. * @param motifSize the motif length. * @param range the range value. * @param znormThreshold z-normalization threshold. * @return a distance value or NAN if above the threshold. */ private static Double eaDistance(double[] series, int a, int b, Integer motifSize, double range, double znormThreshold) { distCounter++; double cutOff2 = range * range; double[] seriesA = tp.znorm(tp.subseriesByCopy(series, a, a + motifSize), znormThreshold); double[] seriesB = tp.znorm(tp.subseriesByCopy(series, b, b + motifSize), znormThreshold); Double res = 0D; for (int i = 0; i < motifSize; i++) { res = res + distance2(seriesA[i], seriesB[i]); if (res > cutOff2) { eaCounter++; return Double.NaN; } } return Math.sqrt(res); }
/** * Convert the timeseries into SAX string representation. * * @param ts the timeseries. * @param paaSize the PAA size. * @param cuts the alphabet cuts. * @param nThreshold the normalization thresholds. * * @return The SAX representation for timeseries. * @throws SAXException if error occurs. */ public char[] ts2string(double[] ts, int paaSize, double[] cuts, double nThreshold) throws SAXException { if (paaSize == ts.length) { return tsProcessor.ts2String(tsProcessor.znorm(ts, nThreshold), cuts); } else { // perform PAA conversion double[] paa = tsProcessor.paa(tsProcessor.znorm(ts, nThreshold), paaSize); return tsProcessor.ts2String(paa, cuts); } }
/** * Early abandoning distance configure by range. * * @param series the series to use. * @param a the position of subseries a. * @param b the position of subseries b. * @param motifSize the motif length. * @param range the range value. * @param znormThreshold z-normalization threshold. * @return a distance value or NAN if above the threshold. */ private static Double eaDistance(double[] series, int a, int b, Integer motifSize, double range, double znormThreshold) { distCounter++; double cutOff2 = range * range; double[] seriesA = tp.znorm(tp.subseriesByCopy(series, a, a + motifSize), znormThreshold); double[] seriesB = tp.znorm(tp.subseriesByCopy(series, b, b + motifSize), znormThreshold); Double res = 0D; for (int i = 0; i < motifSize; i++) { res = res + distance2(seriesA[i], seriesB[i]); if (res > cutOff2) { eaCounter++; return Double.NaN; } } return Math.sqrt(res); }
subSection = tsProcessor.znorm(subSection, SAX_NORM_THRESHOLD);
subSection = tsProcessor.znorm(subSection, SAX_NORM_THRESHOLD);
/** * Converts an array to a normalized XYSeries to be digested with JFreeChart. * * @param index * @param series * @return * @throws TSException */ private XYSeries toSeries(int index, double[] series) throws Exception { double[] normalizedSubseries = tp.znorm(series, controller.getSession().normalizationThreshold); XYSeries res = new XYSeries("series" + String.valueOf(index)); for (int i = 0; i < normalizedSubseries.length; i++) { res.add(i, normalizedSubseries[i]); } return res; }
/** * Computes the normalized distance. The whole idea is that rules map to subsequences of different * length. * * @param series * @param reference * @param candidate * @param zNormThreshold * @return * @throws Exception */ private static double normalizedDistance(double[] series, RuleInterval reference, RuleInterval candidate, double zNormThreshold) throws Exception { double[] ref = Arrays.copyOfRange(series, reference.getStart(), reference.getEnd()); double[] cand = Arrays.copyOfRange(series, candidate.getStart(), candidate.getEnd()); double divisor = Integer.valueOf(ref.length).doubleValue(); // if the reference is the longest, we shrink it down with PAA // if (ref.length > cand.length) { ref = tp.paa(ref, cand.length); divisor = Integer.valueOf(cand.length).doubleValue(); // update the normalization value } // if the candidate is longest, we shrink it with PAA too // else { cand = tp.paa(cand, ref.length); } return ed.distance(tp.znorm(ref, zNormThreshold), tp.znorm(cand, zNormThreshold)) / divisor; }
/** * Converts the input time series into a SAX data structure via chunking and Z normalization. * * @param ts the input data. * @param paaSize the PAA size. * @param cuts the Alphabet cuts. * @param nThreshold the normalization threshold value. * * @return SAX representation of the time series. * @throws SAXException if error occurs. */ public SAXRecords ts2saxByChunking(double[] ts, int paaSize, double[] cuts, double nThreshold) throws SAXException { SAXRecords saxFrequencyData = new SAXRecords(); // Z normalize it double[] normalizedTS = tsProcessor.znorm(ts, nThreshold); // perform PAA conversion if needed double[] paa = tsProcessor.paa(normalizedTS, paaSize); // Convert the PAA to a string. char[] currentString = tsProcessor.ts2String(paa, cuts); // create the datastructure for (int i = 0; i < currentString.length; i++) { char c = currentString[i]; int pos = (int) Math.floor(i * ts.length / currentString.length); saxFrequencyData.add(String.valueOf(c).toCharArray(), pos); } return saxFrequencyData; }
/** * Converts the input time series into a SAX data structure via chunking and Z normalization. * * @param ts the input data. * @param paaSize the PAA size. * @param cuts the Alphabet cuts. * @param nThreshold the normalization threshold value. * * @return SAX representation of the time series. * @throws SAXException if error occurs. */ public SAXRecords ts2saxByChunking(double[] ts, int paaSize, double[] cuts, double nThreshold) throws SAXException { SAXRecords saxFrequencyData = new SAXRecords(); // Z normalize it double[] normalizedTS = tsProcessor.znorm(ts, nThreshold); // perform PAA conversion if needed double[] paa = tsProcessor.paa(normalizedTS, paaSize); // Convert the PAA to a string. char[] currentString = tsProcessor.ts2String(paa, cuts); // create the datastructure for (int i = 0; i < currentString.length; i++) { char c = currentString[i]; int pos = (int) Math.floor(i * ts.length / currentString.length); saxFrequencyData.add(String.valueOf(c).toCharArray(), pos); } return saxFrequencyData; }
subseries = tsProcessor.znorm(subseries, normThreshold);
subseries = tsProcessor.znorm(subseries, normThreshold);
private static double[] seriesValuesAsHeat(double[] series, String className, HashMap<String, HashMap<String, Double>> tfidf, Params params) throws Exception { Alphabet a = new NormalAlphabet(); double[] weights = new double[series.length]; HashMap<String, Integer> words = new HashMap<String, Integer>(); for (int i = 0; i <= series.length - params.getWindowSize(); i++) { double[] subseries = tsp.subseriesByCopy(series, i, i + params.getWindowSize()); double[] paa = tsp.paa(tsp.znorm(subseries, params.getnThreshold()), params.getPaaSize()); char[] sax = tsp.ts2String(paa, a.getCuts(params.getAlphabetSize())); words.put(String.valueOf(sax), i); } for (Entry<String, HashMap<String, Double>> e : tfidf.entrySet()) { for (Entry<String, Double> e1 : e.getValue().entrySet()) { if (words.containsKey(e1.getKey())) { int idx = words.get(e1.getKey()); double increment = 0.0; if (className.equalsIgnoreCase(e.getKey())) { increment = e1.getValue(); } else { // increment = -e1.getValue(); } for (int i = 0; i < params.getWindowSize(); i++) { weights[idx + i] = weights[idx + i] + increment; } } } } return weights; }
private static Map<Integer, Integer[]> getPatternLocationsForTheClass(String className, Map<String, List<double[]>> trainData, String pattern, Params params) throws IndexOutOfBoundsException, Exception { Alphabet a = new NormalAlphabet(); Map<Integer, Integer[]> res = new HashMap<Integer, Integer[]>(); int seriesCounter = 0; for (double[] series : trainData.get(className)) { List<Integer> arr = new ArrayList<Integer>(); for (int i = 0; i <= series.length - params.getWindowSize(); i++) { double[] paa = tsp.paa(tsp.znorm(tsp.subseriesByCopy(series, i, i + params.getWindowSize()), params.getnThreshold()), params.getPaaSize()); char[] sax = tsp.ts2String(paa, a.getCuts(params.getAlphabetSize())); if (pattern.equalsIgnoreCase(String.valueOf(sax))) { arr.add(i); } } res.put(seriesCounter, arr.toArray(new Integer[0])); seriesCounter++; } return res; } }
subSection = tp.znorm(subSection, params.nThreshold);
subseries = tsProcessor.znorm(subseries, normThreshold);