/** * 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); } }
/** * 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); }
/** * Constructor. */ public GrammarvizRuleChartPanel() { super(); tp = new TSProcessor(); }
/** * Z-Normalize routine. * * @param series the input timeseries. * @param normalizationThreshold the zNormalization threshold value. * @return Z-normalized time-series. */ public double[] znorm(double[] series, double normalizationThreshold) { double[] res = new double[series.length]; double sd = stDev(series); if (sd < normalizationThreshold) { // return series.clone(); // return array of zeros return res; } double mean = mean(series); for (int i = 0; i < res.length; i++) { res[i] = (series[i] - mean) / sd; } return res; }
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; } }
public static void main(String[] args) throws IOException, SAXException { double[] ts = TSProcessor.readFileColumn(INPUT_FNAME, 0, 0); subSection = tsProcessor.znorm(subSection, SAX_NORM_THRESHOLD); double[] paa = tsProcessor.paa(subSection, SAX_PAA_SIZE); sb.append(Arrays.toString(paa).replaceAll("\\s+", "")).append(TAB); char[] currentString = tsProcessor.ts2String(paa, cuts); sb.append("\"").append(currentString).append("\"").append(TAB);
TSProcessor tsp = new TSProcessor(); subSection = tsp.znorm(subSection, normalizationThreshold); double[] paa = tsp.paa(subSection, this.saxPAASize); char[] currentString = tsp.ts2String(paa, na.getCuts(this.saxAlphabetSize));
double[] motifA = tp.subseriesByCopy(series, bestMotifLiocation, bestMotifLiocation + motifSize); double[] distancesA = new double[count]; double[] motifB = tp.subseriesByCopy(series, i, i + motifSize); double[] distancesB = new double[count]; double distA = ed.distance(tp.znorm(motifA, znormThreshold), tp.znorm(tp.subseriesByCopy(series, locA, locA + motifSize), znormThreshold)); distancesA[j] = distA; double distB = ed.distance(tp.znorm(motifB, znormThreshold), tp.znorm(tp.subseriesByCopy(series, locB, locB + motifSize), znormThreshold)); distancesB[j] = distB; double varA = tp.var(distancesA); double varB = tp.var(distancesB);
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(); } }
/** * 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; }
double[] ts = TSProcessor.readFileColumn(IN_DATA, 0, 0); LOGGER.info("read " + ts.length + " points from " + IN_DATA); double[] sampledTS = tp.subseriesByCopy(ts, SAMPLE_START, (SAMPLE_START + upperLimit)); RulePruner rp = new RulePruner(sampledTS);
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; } }
int window = ruleEnd - ruleStart; double[] cw = tp.subseriesByCopy(ts, ruleStart, ruleStart + window); double[] currentSubsequence = tp.subseriesByCopy(ts, j, j + window); double dist = ed.distance(cw, currentSubsequence); if (dist < cwNNDist) {
/** * 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; }
/** * Normalizes data in interval 0-1. * * @param data the dataset. * @return normalized dataset. */ public double[] normOne(double[] data) { double[] res = new double[data.length]; double max = max(data); for (int i = 0; i < data.length; i++) { res[i] = data[i] / max; } return res; }
/** * Compute the variance of timeseries. * * @param series The timeseries. * @return The variance. */ public double var(double[] series) { double res = 0D; double mean = mean(series); int count = 0; for (double tp : series) { res += (tp - mean) * (tp - mean); count += 1; } if (count > 0) { return res / ((Integer) (count - 1)).doubleValue(); } return Double.NaN; }
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; }
public static void main(String[] args) throws IOException, SAXException { double[] ts = TSProcessor.readFileColumn(INPUT_FNAME, 0, 0); subSection = tsProcessor.znorm(subSection, SAX_NORM_THRESHOLD); double[] paa = tsProcessor.paa(subSection, SAX_PAA_SIZE); sb.append(Arrays.toString(paa).replaceAll("\\s+", "")).append(TAB); char[] currentString = tsProcessor.ts2String(paa, cuts); sb.append("\"").append(currentString).append("\"").append(TAB);