/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package gov.nasa.jpl.memex.pooledtimeseries;

import java.io.IOException;
import java.util.*;
import java.io.*;

import org.apache.commons.codec.digest.DigestUtils;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.util.*;
import org.apache.hadoop.util.*;
import org.apache.hadoop.mapred.lib.*;

import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point;
import org.opencv.core.Size;
import org.opencv.highgui.VideoCapture;
import org.opencv.imgproc.Imgproc;
import org.opencv.video.Video;

public class MeanChiSquareDistanceCalculation {
    public static class Map extends Mapper<LongWritable, Text, IntWritable, DoubleWritable> {
        //DoubleWritable output = new DoubleWritable();

        public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException, NumberFormatException {
            System.out.println(value.toString());
            Configuration conf = context.getConfiguration();
            String meanDistsPath = conf.get("meanDistsFilePath");

            String[] videoPaths = value.toString().split(",");
            ArrayList<double[]> tws = PoT.getTemporalWindows(4);
            ArrayList<FeatureVector> fvList = new ArrayList<FeatureVector>();


            for (String video: videoPaths) {
                ArrayList<double[][]> multiSeries = new ArrayList<double[][]>();

                String ofCachePath = video + ".of.txt";
                String hogCachePath = video + ".hog.txt";

                multiSeries.add(PoT.loadTimeSeries(new File(ofCachePath).toPath()));
                multiSeries.add(PoT.loadTimeSeries(new File(hogCachePath).toPath()));

                FeatureVector fv = new FeatureVector();
                for (int i = 0; i < multiSeries.size(); i++) {
                    fv.feature.add(PoT.computeFeaturesFromSeries(multiSeries.get(i), tws, 1));
                    fv.feature.add(PoT.computeFeaturesFromSeries(multiSeries.get(i), tws, 2));
                    fv.feature.add(PoT.computeFeaturesFromSeries(multiSeries.get(i), tws, 5));
                }
                fvList.add(fv);
            }

            for (int i = 0; i < fvList.get(0).numDim(); i++) {
                //context.write(new IntWritable(i), output.set(
                context.write(new IntWritable(i), new DoubleWritable(
                    PoT.chiSquareDistance(
                        fvList.get(0).feature.get(i),
                        fvList.get(1).feature.get(i)
                    )
                ));
            }
        }
    }

    public static class Reduce extends Reducer<IntWritable, DoubleWritable, NullWritable, DoubleWritable> {
    //public static class Reduce extends Reducer<IntWritable, DoubleWritable, IntWritable, DoubleWritable> {
        public void reduce(IntWritable key, Iterable<DoubleWritable> values, Context context) throws IOException, InterruptedException {
            int sum = 0;
            int count = 0;
            for (DoubleWritable value : values) {
                sum += value.get();
                count++;
            }

            //context.write(key, new Text(String.valueOf(sum / (double) count)));
            //context.write(key, new DoubleWritable(sum / (double) count));
            context.write(null, new DoubleWritable(sum / (double) count));
        }

    }

    public static void main(String[] args) throws Exception {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Configuration baseConf = new Configuration();
        //baseConf.set("mapred.reduce.tasks", "0");
        //baseConf.set("mapred.reduce.tasks", "1");

        Job job = Job.getInstance(baseConf);
        job.setJarByClass(MeanChiSquareDistanceCalculation.class);

        job.setJobName("mean_chi_square_calculation");

        job.setMapOutputKeyClass(IntWritable.class);
        job.setMapOutputValueClass(DoubleWritable.class);
        job.setOutputKeyClass(IntWritable.class);
        job.setOutputValueClass(DoubleWritable.class);

        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);

        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        job.setMapperClass(Map.class);
        job.setReducerClass(Reduce.class);

        job.waitForCompletion(true);
    }
}



@@ -1990,65 +1990,8 @@
 %3E %7B%0A
-        //DoubleWritable output = new DoubleWritable();%0A%0A
     
@@ -3459,72 +3459,8 @@
 ) %7B%0A
-                //context.write(new IntWritable(i), output.set(%0A
     
@@ -3525,16 +3525,16 @@
 itable(%0A
+
         
@@ -3860,117 +3860,8 @@
 %3E %7B%0A
-    //public static class Reduce extends Reducer%3CIntWritable, DoubleWritable, IntWritable, DoubleWritable%3E %7B%0A
     
@@ -4175,166 +4175,8 @@
  %7D%0A%0A
-            //context.write(key, new Text(String.valueOf(sum / (double) count)));%0A            //context.write(key, new DoubleWritable(sum / (double) count));%0A
     
@@ -4377,24 +4377,24 @@
 ARY_NAME);%0A%0A
+
         Conf
@@ -4438,112 +4438,8 @@
 n();
-%0A        //baseConf.set(%22mapred.reduce.tasks%22, %220%22);%0A        //baseConf.set(%22mapred.reduce.tasks%22, %221%22);
 %0A%0A  

Clean up