001 /*
002 * Copyright 2011 Christian Kumpe http://kumpe.de/christian/java
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package de.kumpe.hadooptimizer.examples.analysis;
017
018 import java.io.BufferedReader;
019 import java.io.FileReader;
020 import java.io.IOException;
021 import java.io.PrintWriter;
022 import java.util.Arrays;
023
024 import org.apache.commons.math.stat.descriptive.moment.Mean;
025 import org.apache.commons.math.stat.descriptive.moment.StandardDeviation;
026
027 public class CalcMean {
028 public static void main(final String[] args) throws IOException {
029 final BufferedReader[] ins = new BufferedReader[args.length - 1];
030 Mean[] means = null;
031 StandardDeviation[] standardDeviations = null;
032 for (int i = 0; i < args.length - 1; i++) {
033 ins[i] = new BufferedReader(new FileReader(args[i]));
034 }
035 final PrintWriter out = new PrintWriter(args[args.length - 1]);
036 String line;
037 out.println("# merged " + Arrays.toString(args));
038 outer: for (;;) {
039 for (final BufferedReader in : ins) {
040 line = in.readLine();
041 if (null == line) {
042 break outer;
043 }
044 if (line.startsWith("#")) {
045 out.println(line);
046 continue;
047 }
048 final double[] values = parseLine(line);
049 if (null == values) {
050 break outer;
051 }
052 if (null == means) {
053 means = new Mean[values.length];
054 standardDeviations = new StandardDeviation[values.length];
055 for (int i = 0; i < means.length; i++) {
056 means[i] = new Mean();
057 standardDeviations[i] = new StandardDeviation();
058 }
059 }
060 for (int i = 0; i < values.length; i++) {
061 means[i].increment(values[i]);
062 standardDeviations[i].increment(values[i]);
063 }
064 }
065 if (null != means) {
066 for (final Mean mean : means) {
067 out.print(mean.getResult());
068 out.print('\t');
069 }
070 for (final StandardDeviation standardDeviation : standardDeviations) {
071 out.print(standardDeviation.getResult());
072 out.print('\t');
073 }
074 }
075 means = null;
076 out.println();
077 }
078 for (final BufferedReader in : ins) {
079 in.close();
080 }
081 out.close();
082 }
083
084 private static double[] parseLine(final String line) {
085 final String[] parts = line.split("[ \\[\\],]+");
086 final double[] result = new double[parts.length];
087 for (int i = 0; i < parts.length; i++) {
088 result[i] = Double.parseDouble(parts[i]);
089 }
090 return result;
091 }
092 }