import logging
import math


def determineMaxQ(dataset, indices, returnAvg=True):
    """
    Determine the maximum possible Q value for a set of data
    """
    logging.info("Determining theoretical maximum Q");
    totalQScore = 0;
    totalSites = 0;

    for i, key in enumerate(indices):
        site = dataset[key];
        if site['siteLabel'][0] == 0:
            totalQScore += 0;  # Can predict site at 100% accuracy

        elif site['siteLabel'][0] == 1:
            totalQScore += -math.log(0.5);

        else:
            raise ValueError("Unknown site type %s for location %s" % (str(site['siteLabel'][0]), str(key)));

        if (i + 1) % 10000 == 0:
            logging.info("Completed %d sites" % (i + 1));

        totalSites += 1;

    if returnAvg:
        return totalQScore / totalSites;
    else:
        return totalQScore;