#!/usr/bin/env python
# 
# Uses data produced by concatenating multiple runs of `chi2cont.py` to
# generate a latex table with the chi and p values.
#
# Usage example:
#
#     $ ./chi2cont.py example/hmmlearn.builtin.*.gen.verb.dist >> out.txt
#     $ ./chi2cont.py example/freq.*.gen.verb.dist             >> out.txt
#     $ ./chi2cont.py example/umdhmm.*.gen.verb.dist           >> out.txt
#     $ ./cross_chi2.py out.txt

import argparse, re, csv, sys
import numpy, scipy
from io import StringIO
from numpy import loadtxt, around
from scipy.stats import chisquare

args = argparse.ArgumentParser(
        description="Computes a grid of chi2 values from expected frequencies")
args.add_argument("input", 
        help="input file containing expected frequencies generated by chi2cont.py")
args = args.parse_args()

input_data = StringIO(open(args.input).read().replace(",", "."))
arr = loadtxt(input_data, usecols=(3,4,5,6,7,8), delimiter=";")

def compute_stats(x, y):
    chi, p = chisquare(x, y)
    chi = around(chi, decimals=3)
    p = around(p, decimals=3)
    return " {0} & {1} ".format(chi, p)

table = csv.writer(sys.stdout, delimiter=';')
for x in arr:
    print(repr((x)))
    stats = [compute_stats(x, y) for y in arr]
    table.writerow(stats)
