#!/bin/bash

# This script takes a features file as input and computes the representativeness scores.
# Copyright 05/03/2014 Germán Sanchis-Trilles & Luis A. Leiva.
# Dual licensed under the MIT and GPL Version 2 licenses:
# http://opensource.org/licenses/mit-license.php
# http://opensource.org/licenses/GPL-2.0

if [ $# -lt 1 ]; then
  echo "Usage: $0 feats_file"
  exit 1
fi

feats_file="$1"

if [ ! -f $feats_file ]; then
  echo "$feats_file not found. Exiting..."
  exit 1
fi

awk \
  'BEGIN {
    SEPARATOR = "@"
  }
  
  {
    for (i=1; i<=NF; ++i) {
      acc[i]  += $i
      acc2[i] += $i*$i
    }
  }
  
  END { 
    for (i = 1; i <= NF; ++i) {
      mean[i]  = acc[i]/FNR
      stdev[i] = sqrt( (acc2[i] - acc[i]*acc[i]/FNR) / (FNR-1) )
    }
    print mean[1]""SEPARATOR""stdev[1], mean[2]""SEPARATOR""stdev[2], mean[3]""SEPARATOR""stdev[3], mean[4]""SEPARATOR""stdev[4]
  }' $feats_file

