entropy_stats: add python script for aggregating multi counts.stt

A python script is provided to accumulate stats from counts.stt
files generated from multiple encoding examples. Command line:
python ../../tools/aggregate_entropy_stats.py [dir of stats files]
 [keyword of filenames] [filename of final stats]

Plus cosmetics on output format of optimized prob tables. The comma
after the last element of each dimension is removed, i.e.,
before: {
          { 128, 128, 128, },
          { 128, 128, 128, },
        }
after:  {
          { 128, 128, 128 },
          { 128, 128, 128 }
        }.
The reason is to avoid moving '}' to a second line after applying
clang-format.

Change-Id: I3b764f4c89b7aefbaf48ac00b8c83713caf75a5d
diff --git a/tools/aggregate_entropy_stats.py b/tools/aggregate_entropy_stats.py
new file mode 100644
index 0000000..7cb4d18
--- /dev/null
+++ b/tools/aggregate_entropy_stats.py
@@ -0,0 +1,39 @@
+#!/usr/bin/env python
+## Copyright (c) 2017, Alliance for Open Media. All rights reserved
+##
+## This source code is subject to the terms of the BSD 2 Clause License and
+## the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
+## was not distributed with this source code in the LICENSE file, you can
+## obtain it at www.aomedia.org/license/software. If the Alliance for Open
+## Media Patent License 1.0 was not distributed with this source code in the
+## PATENTS file, you can obtain it at www.aomedia.org/license/patent.
+##
+"""Aggregate multiple entropy stats output which is written in 32-bit int.
+
+python ./aggregate_entropy_stats.py [dir of stats files] [keyword of filenames]
+ [filename of final stats]
+"""
+
+__author__ = "yuec@google.com"
+
+import os
+import sys
+import numpy as np
+
+def main():
+    dir = sys.argv[1]
+    sum = []
+    for fn in os.listdir(dir):
+        if sys.argv[2] in fn:
+            stats = np.fromfile(dir + fn, dtype=np.int32)
+            if len(sum) == 0:
+                sum = stats
+            else:
+                sum = np.add(sum, stats)
+    if len(sum) == 0:
+        print("No stats file is found. Double-check directory and keyword?")
+    else:
+        sum.tofile(dir+sys.argv[3])
+
+if __name__ == '__main__':
+    main()
diff --git a/tools/aom_entropy_optimizer.c b/tools/aom_entropy_optimizer.c
index 5eec3d8..a67390d 100644
--- a/tools/aom_entropy_optimizer.c
+++ b/tools/aom_entropy_optimizer.c
@@ -131,7 +131,10 @@
     }
     if (tabs > 0) fprintf(probsfile, "%*c", tabs * SPACES_PER_TAB, ' ');
     for (int k = 0; k < total_modes - 1; ++k) {
-      fprintf(probsfile, " %3d,", probs[k]);
+      if (k == total_modes - 2)
+        fprintf(probsfile, " %3d ", probs[k]);
+      else
+        fprintf(probsfile, " %3d,", probs[k]);
       fprintf(logfile, "%d ", counts1d[k]);
     }
     fprintf(logfile, "%d\n", counts1d[total_modes - 1]);
@@ -139,7 +142,13 @@
     assert(cts_each_dim[1] == 2);
 
     for (int k = 0; k < cts_each_dim[0]; ++k) {
-      fprintf(probsfile, " %3d,", get_binary_prob((*ct_ptr)[0], (*ct_ptr)[1]));
+      if (k == cts_each_dim[0] - 1) {
+        fprintf(probsfile, " %3d ",
+                get_binary_prob((*ct_ptr)[0], (*ct_ptr)[1]));
+      } else {
+        fprintf(probsfile, " %3d,",
+                get_binary_prob((*ct_ptr)[0], (*ct_ptr)[1]));
+      }
       fprintf(logfile, "%d %d\n", (*ct_ptr)[0], (*ct_ptr)[1]);
       (*ct_ptr) += 2;
     }
@@ -158,9 +167,15 @@
         return 1;
       }
       if (dim_of_cts == 2 || (dim_of_cts == 3 && flatten_last_dim)) {
-        fprintf(probsfile, "},\n");
+        if (k == cts_each_dim[0] - 1)
+          fprintf(probsfile, "}\n");
+        else
+          fprintf(probsfile, "},\n");
       } else {
-        fprintf(probsfile, "%*c},\n", tabs * SPACES_PER_TAB, ' ');
+        if (k == cts_each_dim[0] - 1)
+          fprintf(probsfile, "%*c}\n", tabs * SPACES_PER_TAB, ' ');
+        else
+          fprintf(probsfile, "%*c},\n", tabs * SPACES_PER_TAB, ' ');
       }
     }
   }