# colate_progress.py

# Imports
import argparse
import matplotlib.pyplot as plt
import os 
import subprocess

from itertools import dropwhile
from operator import add
from pprint import pprint

# iterations_in_shard_subdir
def iterations_in_shard_subdir(dir_):
    image_files = filter(lambda f: os.path.splitext(f)[1] == '.png',
                         os.listdir(dir_))
    image_number = lambda f: int(os.path.splitext(f)[0])
    sorted_image_files = sorted(image_files, key=image_number)
    valid_image_files = list(dropwhile(lambda f: image_number(f) < 0,
                             sorted_image_files))
    return map(lambda f: os.path.join(dir_, f), valid_image_files)
                    
# main
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('input_path')
    parser.add_argument('output_path')
    parser.add_argument('--repeat-frames', type=int, default=1)
    args = parser.parse_args()

    walker = os.walk(args.input_path)
    input_dir, shard_subdirs, files = next(walker)
    shard_subdirs = sorted(shard_subdirs, key=int)
    shard_dirs = map(lambda d: os.path.join(input_dir, d),
                     shard_subdirs)
    image_paths = reduce(add, map(iterations_in_shard_subdir, shard_dirs))

    if args.repeat_frames > 1:
        duplicated_paths = []
        for path in image_paths:
            duplicated_paths += [path] * args.repeat_frames
        image_paths = duplicated_paths

    listing_path = os.path.join(args.input_path, 'images.txt')
    print '%d images ->' % len(image_paths), listing_path
    with open(listing_path, 'w') as fp:
        fp.write('\n'.join(image_paths))

    h, w = plt.imread(image_paths[0]).shape[:2]

    cmd = ['mencoder',
           'mf://@%s' % listing_path,
           '-mf',
           'w=%d:h=%d:fps=25:type=png' % (w, h),
           '-ovc',
           'lavc', '-lavcopts', 'vcodec=mpeg4:vbitrate=15000:mbd=2',
           '-o', args.output_path]
    print ' '.join(cmd)
    subprocess.check_call(cmd)

if __name__ == '__main__':
    main()

@@ -673,36 +673,16 @@
 _files)%0A
-                    
 %0A# main%0A
@@ -1140,24 +1140,34 @@
 ubdirs)%0A    
+iteration_
 image_paths 
@@ -1175,16 +1175,25 @@
  reduce(
+%0A        
 add, map
@@ -1230,25 +1230,458 @@
 hard_dirs))%0A
-%0A
+    final_image_paths = map(lambda d: os.path.join(input_dir, d, '-1.png'),%0A                            shard_subdirs)%0A%0A    head, tail = os.path.split(args.output_path)%0A    root, ext = os.path.splitext(tail)%0A    final_output_path = os.path.join(head, root + '_-1' + ext)%0A%0A    for image_paths, output_path in %5B(iteration_image_paths, args.output_path),%0A                                     (final_image_paths, final_output_path)%5D:%0A    
     if args.
@@ -1699,32 +1699,36 @@
 es %3E 1:%0A        
+   
+ 
 duplicated_paths
@@ -1733,16 +1733,20 @@
 hs = %5B%5D%0A
+    
         
@@ -1774,32 +1774,36 @@
 hs:%0A            
+   
+ 
 duplicated_paths
@@ -1838,24 +1838,28 @@
 mes%0A        
+    
 image_paths 
@@ -1878,16 +1878,20 @@
 _paths%0A%0A
+    
     list
@@ -1945,16 +1945,20 @@
 s.txt')%0A
+    
     prin
@@ -2007,16 +2007,20 @@
 ng_path%0A
+    
     with
@@ -2059,16 +2059,20 @@
         
+    
 fp.write
@@ -2097,16 +2097,20 @@
 aths))%0A%0A
+    
     h, w
@@ -2150,16 +2150,20 @@
 pe%5B:2%5D%0A%0A
+    
     cmd 
@@ -2177,16 +2177,20 @@
 coder',%0A
+    
         
@@ -2234,15 +2234,23 @@
     
+    
 '-mf',%0A
+    
     
@@ -2309,16 +2309,20 @@
     
+ 
+   
 '-ovc',%0A
     
@@ -2317,16 +2317,20 @@
 '-ovc',%0A
+    
         
@@ -2405,19 +2405,18 @@
     
+ 
+   
 '-o', 
-args.
 outp
@@ -2424,16 +2424,20 @@
 t_path%5D%0A
+    
     prin
@@ -2452,16 +2452,20 @@
 in(cmd)%0A
+    
     subp

Update colate_progress.py to output final images