#!/usr/bin/python

import os
import sys
import re
import math

b = os.listdir( sys.argv[1] )

myscore = dict();

maxscore = 0.0
avgscore = 0.0
maxtrans = None

for i in b:
    if( re.search( "\.jpg", i ) ):
        i = re.sub( "\.jpg", "", i );
        parts = re.split( "_", i );
        j = ( int(parts[1]), int(parts[2]) )
        ax = bx = ay = by = 0
        if( j[0] > 0 ):
            ax = j[0]
            bx = 0
        else:
            ax = 0
            bx = -j[0]
        if( i[1] > 0 ):
            ay = j[1]
            by = 0
        else:
            ay = 0
            by = -j[1]
        amw = amh = bmw = bmh = 0
        amw = 512 - ay
        amh = 512 - ax
        bmw = 512 - by
        bmh = 512 - bx
        w = min( amw, bmw )
        h = min( amh, bmh )
        if( h*w > 0.15*512*512 ):
            myscore[ j ] = float(parts[3])
            avgscore = avgscore + myscore[j]
            if( maxscore < myscore[j] ):
                maxscore = myscore[j]
                maxtrans = j

print "max score", maxscore, maxtrans
avgscore = avgscore / float( len(myscore) )
print "avg score", avgscore

stdvar = 0
for i in myscore:
    stdvar = stdvar + math.pow( myscore[i]-avgscore, 2 )

stdvar = math.sqrt( stdvar / float( len(myscore) ) )
print "stdvar", stdvar

for i in myscore:
    if( myscore[i] > maxscore - stdvar ):
        print i, myscore[i]
