##############################
### Modified by Saravanan Thirumuruganathan for building resilient outlier ensembles
### Adapted from the original code by David Card at https://github.com/dallascard/ .


### Description:
### Given unreliable observations of patient classes by multiple observers,
### determine the most likely true class for each patient, class marginals,
### and  individual error rates for each observer, using Expectation Maximization


### References:
### (Dawid and Skene (1979). Maximum Likelihood Estimation of Observer
### Error-Rates Using the EM Algorithm. Journal of the Royal Statistical Society.
### Series C (Applied Statistics), Vol. 28, No. 1, pp. 20-28. 
##############################

import numpy as np
import sys
import csv

"""
Function: dawid_skene()
    Run the Dawid-Skene estimator on response data
Input:
    responses: a dictionary object of responses:
        {patients: {observers: [labels]}}
    tol: tolerance required for convergence of EM
    max_iter: maximum number of iterations of EM
""" 
def run(responses, tol=0.00001, max_iter=10000, init='average'):
    # convert responses to counts
    (patients, observers, classes, counts) = responses_to_counts(responses)
    print "num Patients:", len(patients)
    print "Observers:", observers
    print "Classes:", classes
    
    # initialize
    iter = 0
    converged = False
    old_class_marginals = None
    old_error_rates = None

    patient_classes = initialize(counts)
    
    print "Iter\tlog-likelihood\tdelta-CM\tdelta-ER"    